86th Cygnus<->FSF quick merge
[gcc.git] / gcc / cp / call.c
1 /* Functions related to invoking methods and overloaded functions.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com) and
4 hacked by Brendan Kehoe (brendan@cygnus.com).
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23
24 /* High-level class interface. */
25
26 #include "config.h"
27 #include "tree.h"
28 #include <stdio.h>
29 #include "cp-tree.h"
30 #include "class.h"
31 #include "output.h"
32 #include "flags.h"
33
34 #include "obstack.h"
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
37
38 extern void sorry ();
39
40 extern int inhibit_warnings;
41 extern tree ctor_label, dtor_label;
42
43 /* Compute the ease with which a conversion can be performed
44 between an expected and the given type. */
45 static struct harshness_code convert_harshness PROTO((register tree, register tree, tree));
46
47 #define EVIL_RETURN(ARG) ((ARG).code = EVIL_CODE, (ARG))
48 #define STD_RETURN(ARG) ((ARG).code = STD_CODE, (ARG))
49 #define QUAL_RETURN(ARG) ((ARG).code = QUAL_CODE, (ARG))
50 #define TRIVIAL_RETURN(ARG) ((ARG).code = TRIVIAL_CODE, (ARG))
51 #define ZERO_RETURN(ARG) ((ARG).code = 0, (ARG))
52
53 /* Ordering function for overload resolution. Compare two candidates
54 by gross quality. */
55 int
56 rank_for_overload (x, y)
57 struct candidate *x, *y;
58 {
59 if (y->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
60 return y->h.code - x->h.code;
61 if (x->h.code & (EVIL_CODE|ELLIPSIS_CODE|USER_CODE))
62 return -1;
63
64 /* This is set by compute_conversion_costs, for calling a non-const
65 member function from a const member function. */
66 if ((y->harshness[0].code & CONST_CODE) ^ (x->harshness[0].code & CONST_CODE))
67 return y->harshness[0].code - x->harshness[0].code;
68
69 if (y->h.code & STD_CODE)
70 {
71 if (x->h.code & STD_CODE)
72 return y->h.distance - x->h.distance;
73 return 1;
74 }
75 if (x->h.code & STD_CODE)
76 return -1;
77
78 return y->h.code - x->h.code;
79 }
80
81 /* Compare two candidates, argument by argument. */
82 int
83 rank_for_ideal (x, y)
84 struct candidate *x, *y;
85 {
86 int i;
87
88 if (x->h_len != y->h_len)
89 abort ();
90
91 for (i = 0; i < x->h_len; i++)
92 {
93 if (y->harshness[i].code - x->harshness[i].code)
94 return y->harshness[i].code - x->harshness[i].code;
95 if ((y->harshness[i].code & STD_CODE)
96 && (y->harshness[i].distance - x->harshness[i].distance))
97 return y->harshness[i].distance - x->harshness[i].distance;
98
99 /* They're both the same code. Now see if we're dealing with an
100 integral promotion that needs a finer grain of accuracy. */
101 if (y->harshness[0].code & PROMO_CODE
102 && (y->harshness[i].int_penalty ^ x->harshness[i].int_penalty))
103 return y->harshness[i].int_penalty - x->harshness[i].int_penalty;
104 }
105 return 0;
106 }
107
108 /* TYPE is the type we wish to convert to. PARM is the parameter
109 we have to work with. We use a somewhat arbitrary cost function
110 to measure this conversion. */
111 static struct harshness_code
112 convert_harshness (type, parmtype, parm)
113 register tree type, parmtype;
114 tree parm;
115 {
116 struct harshness_code h;
117 register enum tree_code codel;
118 register enum tree_code coder;
119 int lvalue;
120
121 h.code = 0;
122 h.distance = 0;
123 h.int_penalty = 0;
124
125 #ifdef GATHER_STATISTICS
126 n_convert_harshness++;
127 #endif
128
129 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
130 {
131 if (parm)
132 parm = convert_from_reference (parm);
133 parmtype = TREE_TYPE (parmtype);
134 lvalue = 1;
135 }
136 else if (parm)
137 lvalue = lvalue_p (parm);
138 else
139 lvalue = 0;
140
141 if (TYPE_PTRMEMFUNC_P (type))
142 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
143 if (TYPE_PTRMEMFUNC_P (parmtype))
144 parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
145
146 codel = TREE_CODE (type);
147 coder = TREE_CODE (parmtype);
148
149 if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
150 return ZERO_RETURN (h);
151
152 if (coder == ERROR_MARK)
153 return EVIL_RETURN (h);
154
155 if (codel == REFERENCE_TYPE)
156 {
157 tree ttl, ttr;
158 int constp = parm ? TREE_READONLY (parm) : TYPE_READONLY (parmtype);
159 int volatilep = (parm ? TREE_THIS_VOLATILE (parm)
160 : TYPE_VOLATILE (parmtype));
161 register tree intype = TYPE_MAIN_VARIANT (parmtype);
162 register enum tree_code form = TREE_CODE (intype);
163 int penalty = 0;
164
165 ttl = TREE_TYPE (type);
166
167 /* Only allow const reference binding if we were given a parm to deal
168 with, since it isn't really a conversion. This is a hack to
169 prevent build_type_conversion from finding this conversion, but
170 still allow overloading to find it. */
171 if (! lvalue && ! (parm && TYPE_READONLY (ttl)))
172 return EVIL_RETURN (h);
173
174 if ((TYPE_READONLY (ttl) < constp)
175 || (TYPE_VOLATILE (ttl) < volatilep))
176 return EVIL_RETURN (h);
177
178 /* When passing a non-const argument into a const reference, dig it a
179 little, so a non-const reference is preferred over this one. */
180 penalty = ((TYPE_READONLY (ttl) > constp)
181 + (TYPE_VOLATILE (ttl) > volatilep));
182
183 ttl = TYPE_MAIN_VARIANT (ttl);
184
185 if (form == OFFSET_TYPE)
186 {
187 intype = TREE_TYPE (intype);
188 form = TREE_CODE (intype);
189 }
190
191 ttr = intype;
192
193 if (TREE_CODE (ttl) == ARRAY_TYPE && TREE_CODE (ttr) == ARRAY_TYPE)
194 {
195 if (comptypes (ttl, ttr, 1))
196 return ZERO_RETURN (h);
197 return EVIL_RETURN (h);
198 }
199
200 h = convert_harshness (ttl, ttr, NULL_TREE);
201 if (penalty && h.code == 0)
202 {
203 h.code = QUAL_CODE;
204 h.int_penalty = penalty;
205 }
206 return h;
207 }
208
209 if (codel == POINTER_TYPE && fntype_p (parmtype))
210 {
211 tree p1, p2;
212 struct harshness_code h1, h2;
213
214 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
215 type = TREE_TYPE (type);
216
217 if (coder == POINTER_TYPE)
218 {
219 parmtype = TREE_TYPE (parmtype);
220 coder = TREE_CODE (parmtype);
221 }
222
223 if (coder != TREE_CODE (type))
224 return EVIL_RETURN (h);
225
226 if (type != parmtype && coder == METHOD_TYPE)
227 {
228 tree ttl = TYPE_METHOD_BASETYPE (type);
229 tree ttr = TYPE_METHOD_BASETYPE (parmtype);
230
231 int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
232 if (b_or_d < 0)
233 {
234 b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
235 if (b_or_d < 0)
236 return EVIL_RETURN (h);
237 h.distance = -b_or_d;
238 }
239 else
240 h.distance = b_or_d;
241 h.code = STD_CODE;
242
243 type = build_function_type
244 (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
245 parmtype = build_function_type
246 (TREE_TYPE (parmtype), TREE_CHAIN (TYPE_ARG_TYPES (parmtype)));
247 }
248
249 /* We allow the default conversion between function type
250 and pointer-to-function type for free. */
251 if (comptypes (type, parmtype, 1))
252 return h;
253
254 if (pedantic)
255 return EVIL_RETURN (h);
256
257 /* Compare return types. */
258 p1 = TREE_TYPE (type);
259 p2 = TREE_TYPE (parmtype);
260 h2 = convert_harshness (p1, p2, NULL_TREE);
261 if (h2.code & EVIL_CODE)
262 return h2;
263
264 h1.code = TRIVIAL_CODE;
265 h1.distance = 0;
266
267 if (h2.distance != 0)
268 {
269 tree binfo;
270
271 /* This only works for pointers. */
272 if (TREE_CODE (p1) != POINTER_TYPE
273 && TREE_CODE (p1) != REFERENCE_TYPE)
274 return EVIL_RETURN (h);
275
276 p1 = TREE_TYPE (p1);
277 p2 = TREE_TYPE (p2);
278 /* Don't die if we happen to be dealing with void*. */
279 if (!IS_AGGR_TYPE (p1) || !IS_AGGR_TYPE (p2))
280 return EVIL_RETURN (h);
281 if (h2.distance < 0)
282 binfo = get_binfo (p2, p1, 0);
283 else
284 binfo = get_binfo (p1, p2, 0);
285
286 if (! BINFO_OFFSET_ZEROP (binfo))
287 {
288 #if 0
289 static int explained = 0;
290 if (h2.distance < 0)
291 message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p2, p1);
292 else
293 message_2_types (sorry, "cannot cast `%s' to `%s' at function call site", p1, p2);
294
295 if (! explained++)
296 sorry ("(because pointer values change during conversion)");
297 #endif
298 return EVIL_RETURN (h);
299 }
300 }
301
302 h1.code |= h2.code;
303 if (h2.distance > h1.distance)
304 h1.distance = h2.distance;
305
306 p1 = TYPE_ARG_TYPES (type);
307 p2 = TYPE_ARG_TYPES (parmtype);
308 while (p1 && TREE_VALUE (p1) != void_type_node
309 && p2 && TREE_VALUE (p2) != void_type_node)
310 {
311 h2 = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2),
312 NULL_TREE);
313 if (h2.code & EVIL_CODE)
314 return h2;
315
316 if (h2.distance)
317 {
318 /* This only works for pointers and references. */
319 if (TREE_CODE (TREE_VALUE (p1)) != POINTER_TYPE
320 && TREE_CODE (TREE_VALUE (p1)) != REFERENCE_TYPE)
321 return EVIL_RETURN (h);
322 h2.distance = - h2.distance;
323 }
324
325 h1.code |= h2.code;
326 if (h2.distance > h1.distance)
327 h1.distance = h2.distance;
328 p1 = TREE_CHAIN (p1);
329 p2 = TREE_CHAIN (p2);
330 }
331 if (p1 == p2)
332 return h1;
333 if (p2)
334 {
335 if (p1)
336 return EVIL_RETURN (h);
337 h1.code |= ELLIPSIS_CODE;
338 return h1;
339 }
340 if (p1)
341 {
342 if (TREE_PURPOSE (p1) == NULL_TREE)
343 h1.code |= EVIL_CODE;
344 return h1;
345 }
346 }
347 else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
348 {
349 tree ttl, ttr;
350
351 /* Get to the OFFSET_TYPE that this might be. */
352 type = TREE_TYPE (type);
353
354 if (coder != TREE_CODE (type))
355 return EVIL_RETURN (h);
356
357 ttl = TYPE_OFFSET_BASETYPE (type);
358 ttr = TYPE_OFFSET_BASETYPE (parmtype);
359
360 if (ttl == ttr)
361 h.code = 0;
362 else
363 {
364 int b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
365 if (b_or_d < 0)
366 {
367 b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
368 if (b_or_d < 0)
369 return EVIL_RETURN (h);
370 h.distance = -b_or_d;
371 }
372 else
373 h.distance = b_or_d;
374 h.code = STD_CODE;
375 }
376
377 /* Now test the OFFSET_TYPE's target compatibility. */
378 type = TREE_TYPE (type);
379 parmtype = TREE_TYPE (parmtype);
380 }
381
382 if (coder == UNKNOWN_TYPE)
383 {
384 if (codel == FUNCTION_TYPE
385 || codel == METHOD_TYPE
386 || (codel == POINTER_TYPE
387 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
388 || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
389 return TRIVIAL_RETURN (h);
390 return EVIL_RETURN (h);
391 }
392
393 if (coder == VOID_TYPE)
394 return EVIL_RETURN (h);
395
396 if (codel == BOOLEAN_TYPE)
397 {
398 if (INTEGRAL_CODE_P (coder) || coder == REAL_TYPE)
399 return STD_RETURN (h);
400 else if (coder == POINTER_TYPE || coder == OFFSET_TYPE)
401 {
402 /* Make this worse than any conversion to another pointer.
403 FIXME this is how I think the language should work, but it may not
404 end up being how the language is standardized (jason 1/30/95). */
405 h.distance = 32767;
406 return STD_RETURN (h);
407 }
408 return EVIL_RETURN (h);
409 }
410
411 if (INTEGRAL_CODE_P (codel))
412 {
413 /* Control equivalence of ints an enums. */
414
415 if (codel == ENUMERAL_TYPE
416 && flag_int_enum_equivalence == 0)
417 {
418 /* Enums can be converted to ints, but not vice-versa. */
419 if (coder != ENUMERAL_TYPE
420 || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
421 return EVIL_RETURN (h);
422 }
423
424 /* else enums and ints (almost) freely interconvert. */
425
426 if (INTEGRAL_CODE_P (coder))
427 {
428 if (TYPE_MAIN_VARIANT (type)
429 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
430 {
431 h.code = PROMO_CODE;
432 }
433 else
434 h.code = STD_CODE;
435
436 return h;
437 }
438 else if (coder == REAL_TYPE)
439 {
440 h.code = STD_CODE;
441 h.distance = 0;
442 return h;
443 }
444 }
445
446 if (codel == REAL_TYPE)
447 {
448 if (coder == REAL_TYPE)
449 {
450 if (TYPE_MAIN_VARIANT (type)
451 == TYPE_MAIN_VARIANT (type_promotes_to (parmtype)))
452 h.code = PROMO_CODE;
453 else
454 h.code = STD_CODE;
455
456 return h;
457 }
458 else if (INTEGRAL_CODE_P (coder))
459 {
460 h.code = STD_CODE;
461 h.distance = 0;
462 return h;
463 }
464 }
465
466 /* Convert arrays which have not previously been converted. */
467 if (coder == ARRAY_TYPE)
468 {
469 coder = POINTER_TYPE;
470 if (parm)
471 {
472 parm = decay_conversion (parm);
473 parmtype = TREE_TYPE (parm);
474 }
475 else
476 parmtype = build_pointer_type (TREE_TYPE (parmtype));
477 }
478
479 /* Conversions among pointers */
480 if (codel == POINTER_TYPE && coder == POINTER_TYPE)
481 {
482 register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
483 register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
484 int penalty = 4 * (ttl != ttr);
485
486 /* Anything converts to void *. Since this may be `const void *'
487 (etc.) use VOID_TYPE instead of void_type_node. Otherwise, the
488 targets must be the same, except that we do allow (at some cost)
489 conversion between signed and unsigned pointer types. */
490
491 if ((TREE_CODE (ttl) == METHOD_TYPE
492 || TREE_CODE (ttl) == FUNCTION_TYPE)
493 && TREE_CODE (ttl) == TREE_CODE (ttr))
494 {
495 if (comptypes (ttl, ttr, -1))
496 {
497 h.code = penalty ? STD_CODE : 0;
498 h.distance = 0;
499 }
500 else
501 h.code = EVIL_CODE;
502 return h;
503 }
504
505 #if 1
506 if (TREE_CODE (ttl) != VOID_TYPE
507 && (TREE_CODE (ttr) != VOID_TYPE || !parm || !integer_zerop (parm)))
508 {
509 if (comp_target_types (type, parmtype, 1) <= 0)
510 return EVIL_RETURN (h);
511 }
512 #else
513 if (!(TREE_CODE (ttl) == VOID_TYPE
514 || TREE_CODE (ttr) == VOID_TYPE
515 || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
516 && (ttl = unsigned_type (ttl),
517 ttr = unsigned_type (ttr),
518 penalty = 10, 0))
519 || (comp_target_types (ttl, ttr, 0) > 0)))
520 return EVIL_RETURN (h);
521 #endif
522
523 if (ttr == ttl)
524 {
525 tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
526
527 h.code = 0;
528 /* Note conversion from `T*' to `const T*',
529 or `T*' to `volatile T*'. */
530 if ((TYPE_READONLY (tmp1) < TREE_READONLY (tmp2))
531 || (TYPE_VOLATILE (tmp1) < TYPE_VOLATILE (tmp2)))
532 h.code = EVIL_CODE;
533 else if ((TYPE_READONLY (tmp1) != TREE_READONLY (tmp2))
534 || (TYPE_VOLATILE (tmp1) != TYPE_VOLATILE (tmp2)))
535 h.code |= QUAL_CODE;
536
537 h.distance = 0;
538 return h;
539 }
540
541
542 if (TREE_CODE (ttl) == RECORD_TYPE && TREE_CODE (ttr) == RECORD_TYPE)
543 {
544 int b_or_d = get_base_distance (ttl, ttr, 0, (tree*)0);
545 if (b_or_d < 0)
546 {
547 b_or_d = get_base_distance (ttr, ttl, 0, (tree*)0);
548 if (b_or_d < 0)
549 return EVIL_RETURN (h);
550 h.distance = -b_or_d;
551 }
552 else
553 h.distance = b_or_d;
554 h.code = STD_CODE;
555 return h;
556 }
557
558 /* If converting from a `class*' to a `void*', make it
559 less favorable than any inheritance relationship. */
560 if (TREE_CODE (ttl) == VOID_TYPE && IS_AGGR_TYPE (ttr))
561 {
562 h.code = STD_CODE;
563 h.distance = CLASSTYPE_MAX_DEPTH (ttr)+1;
564 return h;
565 }
566
567 h.code = penalty ? STD_CODE : PROMO_CODE;
568 /* Catch things like `const char *' -> `const void *'
569 vs `const char *' -> `void *'. */
570 if (ttl != ttr)
571 {
572 tree tmp1 = TREE_TYPE (type), tmp2 = TREE_TYPE (parmtype);
573 if ((TYPE_READONLY (tmp1) < TREE_READONLY (tmp2))
574 || (TYPE_VOLATILE (tmp1) < TYPE_VOLATILE (tmp2)))
575 h.code = EVIL_CODE;
576 else if ((TYPE_READONLY (tmp1) > TREE_READONLY (tmp2))
577 || (TYPE_VOLATILE (tmp1) > TYPE_VOLATILE (tmp2)))
578 h.code |= QUAL_CODE;
579 }
580 return h;
581 }
582
583 if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
584 {
585 /* This is not a bad match, but don't let it beat
586 integer-enum combinations. */
587 if (parm && integer_zerop (parm))
588 {
589 h.code = STD_CODE;
590 h.distance = 0;
591 return h;
592 }
593 }
594
595 /* C++: Since the `this' parameter of a signature member function
596 is represented as a signature pointer to handle default implementations
597 correctly, we can have the case that `type' is a signature pointer
598 while `parmtype' is a pointer to a signature table. We don't really
599 do any conversions in this case, so just return 0. */
600
601 if (codel == RECORD_TYPE && coder == POINTER_TYPE
602 && IS_SIGNATURE_POINTER (type) && IS_SIGNATURE (TREE_TYPE (parmtype)))
603 return ZERO_RETURN (h);
604
605 if (codel == RECORD_TYPE && coder == RECORD_TYPE)
606 {
607 int b_or_d = get_base_distance (type, parmtype, 0, (tree*)0);
608 if (b_or_d < 0)
609 {
610 b_or_d = get_base_distance (parmtype, type, 0, (tree*)0);
611 if (b_or_d < 0)
612 return EVIL_RETURN (h);
613 h.distance = -b_or_d;
614 }
615 else
616 h.distance = b_or_d;
617 h.code = STD_CODE;
618 return h;
619 }
620 return EVIL_RETURN (h);
621 }
622
623 /* A clone of build_type_conversion for checking user-defined conversions in
624 overload resolution. */
625
626 int
627 user_harshness (type, parmtype)
628 register tree type, parmtype;
629 {
630 tree conv;
631 tree winner = NULL_TREE;
632 int code;
633
634 {
635 tree typename = build_typename_overload (type);
636 if (lookup_fnfields (TYPE_BINFO (parmtype), typename, 0))
637 return 0;
638 }
639
640 for (conv = lookup_conversions (parmtype); conv; conv = TREE_CHAIN (conv))
641 {
642 struct harshness_code tmp;
643
644 if (winner && TREE_PURPOSE (winner) == TREE_PURPOSE (conv))
645 continue;
646
647 if (tmp = convert_harshness (type, TREE_VALUE (conv), NULL_TREE),
648 (tmp.code < USER_CODE) && (tmp.distance >= 0))
649 {
650 if (winner)
651 return EVIL_CODE;
652 else
653 {
654 winner = conv;
655 code = tmp.code;
656 }
657 }
658 }
659
660 if (winner)
661 return code;
662
663 return -1;
664 }
665
666 int
667 can_convert (to, from)
668 tree to, from;
669 {
670 struct harshness_code h;
671 h = convert_harshness (to, from, NULL_TREE);
672 return (h.code < USER_CODE) && (h.distance >= 0);
673 }
674
675 int
676 can_convert_arg (to, from, arg)
677 tree to, from, arg;
678 {
679 struct harshness_code h;
680 h = convert_harshness (to, from, arg);
681 return (h.code < USER_CODE) && (h.distance >= 0);
682 }
683
684 #ifdef DEBUG_MATCHING
685 static char *
686 print_harshness (h)
687 struct harshness_code *h;
688 {
689 static char buf[1024];
690 char tmp[1024];
691
692 bzero (buf, 1024 * sizeof (char));
693 strcat (buf, "codes=[");
694 if (h->code & EVIL_CODE)
695 strcat (buf, "EVIL");
696 if (h->code & CONST_CODE)
697 strcat (buf, " CONST");
698 if (h->code & ELLIPSIS_CODE)
699 strcat (buf, " ELLIPSIS");
700 if (h->code & USER_CODE)
701 strcat (buf, " USER");
702 if (h->code & STD_CODE)
703 strcat (buf, " STD");
704 if (h->code & PROMO_CODE)
705 strcat (buf, " PROMO");
706 if (h->code & QUAL_CODE)
707 strcat (buf, " QUAL");
708 if (h->code & TRIVIAL_CODE)
709 strcat (buf, " TRIVIAL");
710 if (buf[0] == '\0')
711 strcat (buf, "0");
712
713 sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
714
715 strcat (buf, tmp);
716
717 return buf;
718 }
719 #endif
720
721 /* Algorithm: For each argument, calculate how difficult it is to
722 make FUNCTION accept that argument. If we can easily tell that
723 FUNCTION won't be acceptable to one of the arguments, then we
724 don't need to compute the ease of converting the other arguments,
725 since it will never show up in the intersection of all arguments'
726 favorite functions.
727
728 Conversions between builtin and user-defined types are allowed, but
729 no function involving such a conversion is preferred to one which
730 does not require such a conversion. Furthermore, such conversions
731 must be unique. */
732
733 void
734 compute_conversion_costs (function, tta_in, cp, arglen)
735 tree function;
736 tree tta_in;
737 struct candidate *cp;
738 int arglen;
739 {
740 tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
741 tree ttf = ttf_in;
742 tree tta = tta_in;
743
744 /* Start out with no strikes against. */
745 int evil_strikes = 0;
746 int ellipsis_strikes = 0;
747 int user_strikes = 0;
748 int b_or_d_strikes = 0;
749 int easy_strikes = 0;
750
751 int strike_index = 0, win;
752 struct harshness_code lose;
753 extern int cp_silent;
754
755 #ifdef GATHER_STATISTICS
756 n_compute_conversion_costs++;
757 #endif
758
759 #ifndef DEBUG_MATCHING
760 /* We don't emit any warnings or errors while trying out each candidate. */
761 cp_silent = 1;
762 #endif
763
764 cp->function = function;
765 cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
766 cp->u.bad_arg = 0; /* optimistic! */
767
768 cp->h.code = 0;
769 cp->h.distance = 0;
770 cp->h.int_penalty = 0;
771 bzero ((char *) cp->harshness,
772 (cp->h_len + 1) * sizeof (struct harshness_code));
773
774 while (ttf && tta)
775 {
776 struct harshness_code h;
777
778 if (ttf == void_list_node)
779 break;
780
781 if (type_unknown_p (TREE_VALUE (tta)))
782 {
783 /* Must perform some instantiation here. */
784 tree rhs = TREE_VALUE (tta);
785 tree lhstype = TREE_VALUE (ttf);
786
787 /* Keep quiet about possible contravariance violations. */
788 int old_inhibit_warnings = inhibit_warnings;
789 inhibit_warnings = 1;
790
791 /* @@ This is to undo what `grokdeclarator' does to
792 parameter types. It really should go through
793 something more general. */
794
795 TREE_TYPE (tta) = unknown_type_node;
796 rhs = instantiate_type (lhstype, rhs, 0);
797 inhibit_warnings = old_inhibit_warnings;
798
799 if (TREE_CODE (rhs) == ERROR_MARK)
800 h.code = EVIL_CODE;
801 else
802 h = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
803 }
804 else
805 {
806 #ifdef DEBUG_MATCHING
807 static tree old_function = NULL_TREE;
808
809 if (!old_function || function != old_function)
810 {
811 cp_error ("trying %D", function);
812 old_function = function;
813 }
814
815 cp_error (" doing (%T) %E against arg %T",
816 TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
817 TREE_VALUE (ttf));
818 #endif
819
820 h = convert_harshness (TREE_VALUE (ttf),
821 TREE_TYPE (TREE_VALUE (tta)),
822 TREE_VALUE (tta));
823
824 #ifdef DEBUG_MATCHING
825 cp_error (" evaluated %s", print_harshness (&h));
826 #endif
827 }
828
829 cp->harshness[strike_index] = h;
830 if ((h.code & EVIL_CODE)
831 || ((h.code & STD_CODE) && h.distance < 0))
832 {
833 cp->u.bad_arg = strike_index;
834 evil_strikes = 1;
835 }
836 else if (h.code & ELLIPSIS_CODE)
837 ellipsis_strikes += 1;
838 #if 0
839 /* This is never set by `convert_harshness'. */
840 else if (h.code & USER_CODE)
841 {
842 user_strikes += 1;
843 }
844 #endif
845 else
846 {
847 if ((h.code & STD_CODE) && h.distance)
848 {
849 if (h.distance > b_or_d_strikes)
850 b_or_d_strikes = h.distance;
851 }
852 else
853 easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
854 cp->h.code |= h.code;
855 /* Make sure we communicate this. */
856 cp->h.int_penalty += h.int_penalty;
857 }
858
859 ttf = TREE_CHAIN (ttf);
860 tta = TREE_CHAIN (tta);
861 strike_index += 1;
862 }
863
864 if (tta)
865 {
866 /* ran out of formals, and parmlist is fixed size. */
867 if (ttf /* == void_type_node */)
868 {
869 cp->h.code = EVIL_CODE;
870 cp->u.bad_arg = -1;
871 cp_silent = 0;
872 return;
873 }
874 else
875 {
876 struct harshness_code h;
877 int l = list_length (tta);
878 ellipsis_strikes += l;
879 h.code = ELLIPSIS_CODE;
880 h.distance = 0;
881 h.int_penalty = 0;
882 for (; l; --l)
883 cp->harshness[strike_index++] = h;
884 }
885 }
886 else if (ttf && ttf != void_list_node)
887 {
888 /* ran out of actuals, and no defaults. */
889 if (TREE_PURPOSE (ttf) == NULL_TREE)
890 {
891 cp->h.code = EVIL_CODE;
892 cp->u.bad_arg = -2;
893 cp_silent = 0;
894 return;
895 }
896 /* Store index of first default. */
897 cp->harshness[arglen].distance = strike_index+1;
898 }
899 else
900 cp->harshness[arglen].distance = 0;
901
902 /* Argument list lengths work out, so don't need to check them again. */
903 if (evil_strikes)
904 {
905 /* We do not check for derived->base conversions here, since in
906 no case would they give evil strike counts, unless such conversions
907 are somehow ambiguous. */
908
909 /* See if any user-defined conversions apply.
910 But make sure that we do not loop. */
911 static int dont_convert_types = 0;
912
913 if (dont_convert_types)
914 {
915 cp->h.code = EVIL_CODE;
916 cp_silent = 0;
917 return;
918 }
919
920 win = 0; /* Only get one chance to win. */
921 ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
922 tta = tta_in;
923 strike_index = 0;
924 evil_strikes = 0;
925
926 while (ttf && tta)
927 {
928 if (ttf == void_list_node)
929 break;
930
931 lose = cp->harshness[strike_index];
932 if ((lose.code & EVIL_CODE)
933 || ((lose.code & STD_CODE) && lose.distance < 0))
934 {
935 tree actual_type = TREE_TYPE (TREE_VALUE (tta));
936 tree formal_type = TREE_VALUE (ttf);
937 int extra_conversions = 0;
938
939 dont_convert_types = 1;
940
941 if (TREE_CODE (formal_type) == REFERENCE_TYPE)
942 formal_type = TREE_TYPE (formal_type);
943 if (TREE_CODE (actual_type) == REFERENCE_TYPE)
944 actual_type = TREE_TYPE (actual_type);
945
946 if (formal_type != error_mark_node
947 && actual_type != error_mark_node)
948 {
949 formal_type = TYPE_MAIN_VARIANT (formal_type);
950 actual_type = TYPE_MAIN_VARIANT (actual_type);
951
952 if (TYPE_HAS_CONSTRUCTOR (formal_type))
953 {
954 /* If it has a constructor for this type,
955 try to use it. */
956 /* @@ There is no way to save this result yet, so
957 success is a NULL_TREE for now. */
958 if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
959 != error_mark_node)
960 win++;
961 }
962 if (TYPE_LANG_SPECIFIC (actual_type)
963 && TYPE_HAS_CONVERSION (actual_type))
964 {
965 int extra = user_harshness (formal_type, actual_type);
966
967 if (extra == EVIL_CODE)
968 win += 2;
969 else if (extra >= 0)
970 {
971 win++;
972 extra_conversions = extra;
973 }
974 }
975 }
976 dont_convert_types = 0;
977
978 if (win == 1)
979 {
980 user_strikes += 1;
981 cp->harshness[strike_index].code
982 = USER_CODE | (extra_conversions ? STD_CODE : 0);
983 win = 0;
984 }
985 else
986 {
987 if (cp->u.bad_arg > strike_index)
988 cp->u.bad_arg = strike_index;
989
990 evil_strikes = win ? 2 : 1;
991 break;
992 }
993 }
994
995 ttf = TREE_CHAIN (ttf);
996 tta = TREE_CHAIN (tta);
997 strike_index += 1;
998 }
999 }
1000
1001 /* Const member functions get a small penalty because defaulting
1002 to const is less useful than defaulting to non-const. */
1003 /* This is bogus, it does not correspond to anything in the ARM.
1004 This code will be fixed when this entire section is rewritten
1005 to conform to the ARM. (mrs) */
1006 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1007 {
1008 tree this_parm = TREE_VALUE (ttf_in);
1009
1010 if (TREE_CODE (this_parm) == RECORD_TYPE /* Is `this' a sig ptr? */
1011 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
1012 : TYPE_READONLY (TREE_TYPE (this_parm)))
1013 {
1014 cp->harshness[0].code |= TRIVIAL_CODE;
1015 ++easy_strikes;
1016 }
1017 else
1018 {
1019 /* Calling a non-const member function from a const member function
1020 is probably invalid, but for now we let it only draw a warning.
1021 We indicate that such a mismatch has occurred by setting the
1022 harshness to a maximum value. */
1023 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
1024 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
1025 cp->harshness[0].code |= CONST_CODE;
1026 }
1027 }
1028
1029 if (evil_strikes)
1030 cp->h.code = EVIL_CODE;
1031 if (ellipsis_strikes)
1032 cp->h.code |= ELLIPSIS_CODE;
1033 if (user_strikes)
1034 cp->h.code |= USER_CODE;
1035 cp_silent = 0;
1036 #ifdef DEBUG_MATCHING
1037 cp_error ("final eval %s", print_harshness (&cp->h));
1038 #endif
1039 }
1040
1041 /* Subroutine of ideal_candidate. See if X or Y is a better match
1042 than the other. */
1043 static int
1044 strictly_better (x, y)
1045 unsigned short x, y;
1046 {
1047 unsigned short xor;
1048
1049 if (x == y)
1050 return 0;
1051
1052 xor = x ^ y;
1053 if (xor >= x || xor >= y)
1054 return 1;
1055 return 0;
1056 }
1057
1058 /* When one of several possible overloaded functions and/or methods
1059 can be called, choose the best candidate for overloading.
1060
1061 BASETYPE is the context from which we start method resolution
1062 or NULL if we are comparing overloaded functions.
1063 CANDIDATES is the array of candidates we have to choose from.
1064 N_CANDIDATES is the length of CANDIDATES.
1065 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1066 choose. It is modified in place when resolving methods. It is not
1067 modified in place when resolving overloaded functions.
1068 LEN is the length of the parameter list. */
1069
1070 static struct candidate *
1071 ideal_candidate (candidates, n_candidates, len)
1072 struct candidate *candidates;
1073 int n_candidates;
1074 int len;
1075 {
1076 struct candidate *cp = candidates+n_candidates;
1077 int i, j = -1, best_code;
1078
1079 /* For each argument, sort the functions from best to worst for the arg.
1080 For each function that's not best for this arg, set its overall
1081 harshness to EVIL so that other args won't like it. The candidate
1082 list for the last argument is the intersection of all the best-liked
1083 functions. */
1084
1085 qsort (candidates, n_candidates, sizeof (struct candidate),
1086 rank_for_overload);
1087 best_code = cp[-1].h.code;
1088
1089 /* If they're at least as good as each other, do an arg-by-arg check. */
1090 if (! strictly_better (cp[-1].h.code, cp[-2].h.code))
1091 {
1092 int better = 0;
1093 int worse = 0;
1094
1095 for (j = 0; j < n_candidates; j++)
1096 if (! strictly_better (candidates[j].h.code, best_code))
1097 break;
1098
1099 qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
1100 rank_for_ideal);
1101 for (i = 0; i < len; i++)
1102 {
1103 if (cp[-1].harshness[i].code < cp[-2].harshness[i].code)
1104 better = 1;
1105 else if (cp[-1].harshness[i].code > cp[-2].harshness[i].code)
1106 worse = 1;
1107 else if (cp[-1].harshness[i].code & STD_CODE)
1108 {
1109 /* If it involves a standard conversion, let the
1110 inheritance lattice be the final arbiter. */
1111 if (cp[-1].harshness[i].distance > cp[-2].harshness[i].distance)
1112 worse = 1;
1113 else if (cp[-1].harshness[i].distance < cp[-2].harshness[i].distance)
1114 better = 1;
1115 }
1116 else if (cp[-1].harshness[i].code & PROMO_CODE)
1117 {
1118 /* For integral promotions, take into account a finer
1119 granularity for determining which types should be favored
1120 over others in such promotions. */
1121 if (cp[-1].harshness[i].int_penalty > cp[-2].harshness[i].int_penalty)
1122 worse = 1;
1123 else if (cp[-1].harshness[i].int_penalty < cp[-2].harshness[i].int_penalty)
1124 better = 1;
1125 }
1126 }
1127
1128 if (! better || worse)
1129 return NULL;
1130 }
1131 return cp-1;
1132 }
1133
1134 /* Assume that if the class referred to is not in the
1135 current class hierarchy, that it may be remote.
1136 PARENT is assumed to be of aggregate type here. */
1137 static int
1138 may_be_remote (parent)
1139 tree parent;
1140 {
1141 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
1142 return 0;
1143
1144 if (current_class_type == NULL_TREE)
1145 return 0;
1146
1147 if (parent == current_class_type)
1148 return 0;
1149
1150 if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
1151 return 0;
1152 return 1;
1153 }
1154
1155 tree
1156 build_vfield_ref (datum, type)
1157 tree datum, type;
1158 {
1159 tree rval;
1160 int old_assume_nonnull_objects = flag_assume_nonnull_objects;
1161
1162 if (datum == error_mark_node)
1163 return error_mark_node;
1164
1165 /* Vtable references are always made from non-null objects. */
1166 flag_assume_nonnull_objects = 1;
1167 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
1168 datum = convert_from_reference (datum);
1169
1170 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
1171 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
1172 datum, CLASSTYPE_VFIELD (type));
1173 else
1174 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
1175 flag_assume_nonnull_objects = old_assume_nonnull_objects;
1176
1177 return rval;
1178 }
1179
1180 /* Build a call to a member of an object. I.e., one that overloads
1181 operator ()(), or is a pointer-to-function or pointer-to-method. */
1182 static tree
1183 build_field_call (basetype_path, instance_ptr, name, parms)
1184 tree basetype_path, instance_ptr, name, parms;
1185 {
1186 tree field, instance;
1187
1188 if (instance_ptr == current_class_ptr)
1189 {
1190 /* Check to see if we really have a reference to an instance variable
1191 with `operator()()' overloaded. */
1192 field = IDENTIFIER_CLASS_VALUE (name);
1193
1194 if (field == NULL_TREE)
1195 {
1196 cp_error ("`this' has no member named `%D'", name);
1197 return error_mark_node;
1198 }
1199
1200 if (TREE_CODE (field) == FIELD_DECL)
1201 {
1202 /* If it's a field, try overloading operator (),
1203 or calling if the field is a pointer-to-function. */
1204 instance = build_component_ref_1 (current_class_ref, field, 0);
1205 if (instance == error_mark_node)
1206 return error_mark_node;
1207
1208 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1209 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
1210 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
1211
1212 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1213 {
1214 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
1215 return build_function_call (instance, parms);
1216 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
1217 return build_function_call (instance, tree_cons (NULL_TREE, current_class_ptr, parms));
1218 }
1219 }
1220 return NULL_TREE;
1221 }
1222
1223 /* Check to see if this is not really a reference to an instance variable
1224 with `operator()()' overloaded. */
1225 field = lookup_field (basetype_path, name, 1, 0);
1226
1227 /* This can happen if the reference was ambiguous or for access
1228 violations. */
1229 if (field == error_mark_node)
1230 return error_mark_node;
1231
1232 if (field)
1233 {
1234 tree basetype;
1235 tree ftype = TREE_TYPE (field);
1236
1237 if (TREE_CODE (ftype) == REFERENCE_TYPE)
1238 ftype = TREE_TYPE (ftype);
1239
1240 if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
1241 {
1242 /* Make the next search for this field very short. */
1243 basetype = DECL_FIELD_CONTEXT (field);
1244 instance_ptr = convert_pointer_to (basetype, instance_ptr);
1245
1246 instance = build_indirect_ref (instance_ptr, NULL_PTR);
1247 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
1248 build_component_ref_1 (instance, field, 0),
1249 parms, NULL_TREE);
1250 }
1251 if (TREE_CODE (ftype) == POINTER_TYPE)
1252 {
1253 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
1254 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
1255 {
1256 /* This is a member which is a pointer to function. */
1257 tree ref
1258 = build_component_ref_1 (build_indirect_ref (instance_ptr,
1259 NULL_PTR),
1260 field, LOOKUP_COMPLAIN);
1261 if (ref == error_mark_node)
1262 return error_mark_node;
1263 return build_function_call (ref, parms);
1264 }
1265 }
1266 else if (TREE_CODE (ftype) == METHOD_TYPE)
1267 {
1268 error ("invalid call via pointer-to-member function");
1269 return error_mark_node;
1270 }
1271 else
1272 return NULL_TREE;
1273 }
1274 return NULL_TREE;
1275 }
1276
1277 tree
1278 find_scoped_type (type, inner_name, inner_types)
1279 tree type, inner_name, inner_types;
1280 {
1281 tree tags = CLASSTYPE_TAGS (type);
1282
1283 while (tags)
1284 {
1285 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1286 enclosing class) is set to the name for the enum type. So, if
1287 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1288 then this test will be true. */
1289 if (TREE_PURPOSE (tags) == inner_name)
1290 {
1291 if (inner_types == NULL_TREE)
1292 return TYPE_NAME (TREE_VALUE (tags));
1293 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
1294 }
1295 tags = TREE_CHAIN (tags);
1296 }
1297
1298 /* Look for a TYPE_DECL. */
1299 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
1300 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
1301 {
1302 /* Code by raeburn. */
1303 if (inner_types == NULL_TREE)
1304 return tags;
1305 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
1306 }
1307
1308 return NULL_TREE;
1309 }
1310
1311 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1312 the name that names the above nested type. INNER_TYPES
1313 is a chain of nested type names (held together by SCOPE_REFs);
1314 OUTER_TYPE is the type we know to enclose INNER_TYPES.
1315 Returns NULL_TREE if there is an error. */
1316 tree
1317 resolve_scope_to_name (outer_type, inner_stuff)
1318 tree outer_type, inner_stuff;
1319 {
1320 register tree tmp;
1321 tree inner_name, inner_type;
1322
1323 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
1324 {
1325 /* We first try to look for a nesting in our current class context,
1326 then try any enclosing classes. */
1327 tree type = current_class_type;
1328
1329 while (type && (TREE_CODE (type) == RECORD_TYPE
1330 || TREE_CODE (type) == UNION_TYPE))
1331 {
1332 tree rval = resolve_scope_to_name (type, inner_stuff);
1333
1334 if (rval != NULL_TREE)
1335 return rval;
1336 type = DECL_CONTEXT (TYPE_NAME (type));
1337 }
1338 }
1339
1340 if (TREE_CODE (inner_stuff) == SCOPE_REF)
1341 {
1342 inner_name = TREE_OPERAND (inner_stuff, 0);
1343 inner_type = TREE_OPERAND (inner_stuff, 1);
1344 }
1345 else
1346 {
1347 inner_name = inner_stuff;
1348 inner_type = NULL_TREE;
1349 }
1350
1351 if (outer_type == NULL_TREE)
1352 {
1353 tree x;
1354 /* If we have something that's already a type by itself,
1355 use that. */
1356 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
1357 {
1358 if (inner_type)
1359 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
1360 inner_type);
1361 return inner_name;
1362 }
1363
1364 x = lookup_name (inner_name, 0);
1365
1366 if (x && TREE_CODE (x) == NAMESPACE_DECL)
1367 {
1368 x = lookup_namespace_name (x, inner_type);
1369 return x;
1370 }
1371 return NULL_TREE;
1372 }
1373
1374 if (! IS_AGGR_TYPE (outer_type))
1375 return NULL_TREE;
1376
1377 /* Look for member classes or enums. */
1378 tmp = find_scoped_type (outer_type, inner_name, inner_type);
1379
1380 /* If it's not a type in this class, then go down into the
1381 base classes and search there. */
1382 if (! tmp && TYPE_BINFO (outer_type))
1383 {
1384 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
1385 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1386
1387 for (i = 0; i < n_baselinks; i++)
1388 {
1389 tree base_binfo = TREE_VEC_ELT (binfos, i);
1390 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
1391 if (tmp)
1392 return tmp;
1393 }
1394 tmp = NULL_TREE;
1395 }
1396
1397 return tmp;
1398 }
1399
1400 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1401 This is how virtual function calls are avoided. */
1402 tree
1403 build_scoped_method_call (exp, basetype, name, parms)
1404 tree exp, basetype, name, parms;
1405 {
1406 /* Because this syntactic form does not allow
1407 a pointer to a base class to be `stolen',
1408 we need not protect the derived->base conversion
1409 that happens here.
1410
1411 @@ But we do have to check access privileges later. */
1412 tree binfo, decl;
1413 tree type = TREE_TYPE (exp);
1414
1415 if (type == error_mark_node
1416 || basetype == error_mark_node)
1417 return error_mark_node;
1418
1419 if (current_template_parms)
1420 {
1421 if (TREE_CODE (name) == BIT_NOT_EXPR)
1422 {
1423 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1424 name = build_min_nt (BIT_NOT_EXPR, type);
1425 }
1426 name = build_min_nt (SCOPE_REF, basetype, name);
1427 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
1428 }
1429
1430 if (TREE_CODE (type) == REFERENCE_TYPE)
1431 type = TREE_TYPE (type);
1432
1433 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1434 that explicit ~int is caught in the parser; this deals with typedefs
1435 and template parms. */
1436 if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype))
1437 {
1438 if (type != basetype)
1439 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1440 exp, basetype, type);
1441 name = TREE_OPERAND (name, 0);
1442 if (basetype != name && basetype != get_type_value (name))
1443 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1444 basetype, name);
1445 return convert (void_type_node, exp);
1446 }
1447
1448 if (! is_aggr_type (basetype, 1))
1449 return error_mark_node;
1450
1451 if (! IS_AGGR_TYPE (type))
1452 {
1453 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1454 exp, type);
1455 return error_mark_node;
1456 }
1457
1458 if ((binfo = binfo_or_else (basetype, type)))
1459 {
1460 if (binfo == error_mark_node)
1461 return error_mark_node;
1462 if (TREE_CODE (exp) == INDIRECT_REF)
1463 decl = build_indirect_ref (convert_pointer_to (binfo,
1464 build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
1465 else
1466 decl = build_scoped_ref (exp, basetype);
1467
1468 /* Call to a destructor. */
1469 if (TREE_CODE (name) == BIT_NOT_EXPR)
1470 {
1471 /* Explicit call to destructor. */
1472 name = TREE_OPERAND (name, 0);
1473 if (! (name == TYPE_MAIN_VARIANT (TREE_TYPE (decl))
1474 || name == constructor_name (TREE_TYPE (decl))
1475 || TREE_TYPE (decl) == get_type_value (name)))
1476 {
1477 cp_error
1478 ("qualified type `%T' does not match destructor name `~%T'",
1479 TREE_TYPE (decl), name);
1480 return error_mark_node;
1481 }
1482 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
1483 return convert (void_type_node, exp);
1484
1485 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
1486 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
1487 0);
1488 }
1489
1490 /* Call to a method. */
1491 return build_method_call (decl, name, parms, binfo,
1492 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1493 }
1494 return error_mark_node;
1495 }
1496
1497 static void
1498 print_candidates (candidates)
1499 tree candidates;
1500 {
1501 cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
1502 candidates = TREE_CHAIN (candidates);
1503
1504 while (candidates)
1505 {
1506 cp_error_at (" %D", TREE_VALUE (candidates));
1507 candidates = TREE_CHAIN (candidates);
1508 }
1509 }
1510
1511 static void
1512 print_n_candidates (candidates, n)
1513 struct candidate *candidates;
1514 int n;
1515 {
1516 int i;
1517
1518 cp_error_at ("candidates are: %D", candidates[0].function);
1519 for (i = 1; i < n; i++)
1520 cp_error_at (" %D", candidates[i].function);
1521 }
1522
1523 /* We want the address of a function or method. We avoid creating a
1524 pointer-to-member function. */
1525 tree
1526 build_addr_func (function)
1527 tree function;
1528 {
1529 tree type = TREE_TYPE (function);
1530
1531 /* We have to do these by hand to avoid real pointer to member
1532 functions. */
1533 if (TREE_CODE (type) == METHOD_TYPE)
1534 {
1535 tree addr;
1536
1537 type = build_pointer_type (type);
1538
1539 if (mark_addressable (function) == 0)
1540 return error_mark_node;
1541
1542 addr = build1 (ADDR_EXPR, type, function);
1543
1544 /* Address of a static or external variable or function counts
1545 as a constant */
1546 if (staticp (function))
1547 TREE_CONSTANT (addr) = 1;
1548
1549 function = addr;
1550 }
1551 else
1552 function = default_conversion (function);
1553
1554 return function;
1555 }
1556
1557 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
1558 POINTER_TYPE to those. Note, pointer to member function types
1559 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
1560 tree
1561 build_call (function, result_type, parms)
1562 tree function, result_type, parms;
1563 {
1564 int is_constructor = 0;
1565
1566 function = build_addr_func (function);
1567
1568 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
1569 {
1570 sorry ("unable to call pointer to member function here");
1571 return error_mark_node;
1572 }
1573
1574 if (TREE_CODE (function) == ADDR_EXPR
1575 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1576 && DECL_CONSTRUCTOR_P (TREE_OPERAND (function, 0)))
1577 is_constructor = 1;
1578
1579 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
1580 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
1581 TREE_TYPE (function) = result_type;
1582 TREE_SIDE_EFFECTS (function) = 1;
1583
1584 return function;
1585 }
1586
1587
1588 /* Build something of the form ptr->method (args)
1589 or object.method (args). This can also build
1590 calls to constructors, and find friends.
1591
1592 Member functions always take their class variable
1593 as a pointer.
1594
1595 INSTANCE is a class instance.
1596
1597 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1598
1599 PARMS help to figure out what that NAME really refers to.
1600
1601 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1602 down to the real instance type to use for access checking. We need this
1603 information to get protected accesses correct. This parameter is used
1604 by build_member_call.
1605
1606 FLAGS is the logical disjunction of zero or more LOOKUP_
1607 flags. See cp-tree.h for more info.
1608
1609 If this is all OK, calls build_function_call with the resolved
1610 member function.
1611
1612 This function must also handle being called to perform
1613 initialization, promotion/coercion of arguments, and
1614 instantiation of default parameters.
1615
1616 Note that NAME may refer to an instance variable name. If
1617 `operator()()' is defined for the type of that field, then we return
1618 that result. */
1619 tree
1620 build_method_call (instance, name, parms, basetype_path, flags)
1621 tree instance, name, parms, basetype_path;
1622 int flags;
1623 {
1624 register tree function, fntype, value_type;
1625 register tree basetype, save_basetype;
1626 register tree baselink, result, parmtypes, parm;
1627 tree last;
1628 int pass;
1629 tree access = access_public_node;
1630 tree orig_basetype = basetype_path ? BINFO_TYPE (basetype_path) : NULL_TREE;
1631
1632 /* Range of cases for vtable optimization. */
1633 enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
1634 enum vtable_needs need_vtbl = not_needed;
1635
1636 char *name_kind;
1637 tree save_name = name;
1638 int ever_seen = 0;
1639 tree instance_ptr = NULL_TREE;
1640 int all_virtual = flag_all_virtual;
1641 int static_call_context = 0;
1642 tree found_fns = NULL_TREE;
1643
1644 /* Keep track of `const' and `volatile' objects. */
1645 int constp, volatilep;
1646
1647 #ifdef GATHER_STATISTICS
1648 n_build_method_call++;
1649 #endif
1650
1651 if (instance == error_mark_node
1652 || name == error_mark_node
1653 || parms == error_mark_node
1654 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
1655 return error_mark_node;
1656
1657 if (current_template_parms)
1658 {
1659 if (TREE_CODE (name) == BIT_NOT_EXPR)
1660 {
1661 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1662 name = build_min_nt (BIT_NOT_EXPR, type);
1663 }
1664
1665 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
1666 }
1667
1668 /* This is the logic that magically deletes the second argument to
1669 operator delete, if it is not needed. */
1670 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
1671 {
1672 tree save_last = TREE_CHAIN (parms);
1673 tree result;
1674 /* get rid of unneeded argument */
1675 TREE_CHAIN (parms) = NULL_TREE;
1676 result = build_method_call (instance, name, parms, basetype_path,
1677 (LOOKUP_SPECULATIVELY|flags)
1678 &~LOOKUP_COMPLAIN);
1679 /* If it finds a match, return it. */
1680 if (result)
1681 return build_method_call (instance, name, parms, basetype_path, flags);
1682 /* If it doesn't work, two argument delete must work */
1683 TREE_CHAIN (parms) = save_last;
1684 }
1685 /* We already know whether it's needed or not for vec delete. */
1686 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
1687 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1688 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
1689 TREE_CHAIN (parms) = NULL_TREE;
1690
1691 if (TREE_CODE (name) == BIT_NOT_EXPR)
1692 {
1693 flags |= LOOKUP_DESTRUCTOR;
1694 name = TREE_OPERAND (name, 0);
1695 if (parms)
1696 error ("destructors take no parameters");
1697 basetype = TREE_TYPE (instance);
1698 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1699 basetype = TREE_TYPE (basetype);
1700 if (! (name == basetype
1701 || (IS_AGGR_TYPE (basetype)
1702 && name == constructor_name (basetype))
1703 || basetype == get_type_value (name)))
1704 {
1705 cp_error ("destructor name `~%D' does not match type `%T' of expression",
1706 name, basetype);
1707 return convert (void_type_node, instance);
1708 }
1709
1710 if (! TYPE_HAS_DESTRUCTOR (basetype))
1711 return convert (void_type_node, instance);
1712 instance = default_conversion (instance);
1713 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1714 return build_delete (build_pointer_type (basetype),
1715 instance_ptr, integer_two_node,
1716 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
1717 }
1718
1719 {
1720 char *xref_name;
1721
1722 /* Initialize name for error reporting. */
1723 if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
1724 {
1725 char *p = operator_name_string (name);
1726 xref_name = (char *)alloca (strlen (p) + 10);
1727 sprintf (xref_name, "operator %s", p);
1728 }
1729 else if (TREE_CODE (name) == SCOPE_REF)
1730 xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
1731 else
1732 xref_name = IDENTIFIER_POINTER (name);
1733
1734 GNU_xref_call (current_function_decl, xref_name);
1735 }
1736
1737 if (instance == NULL_TREE)
1738 {
1739 basetype = NULL_TREE;
1740 /* Check cases where this is really a call to raise
1741 an exception. */
1742 if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
1743 {
1744 basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
1745 if (basetype)
1746 basetype = TREE_VALUE (basetype);
1747 }
1748 else if (TREE_CODE (name) == SCOPE_REF
1749 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
1750 {
1751 if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
1752 return error_mark_node;
1753 basetype = purpose_member (TREE_OPERAND (name, 1),
1754 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
1755 if (basetype)
1756 basetype = TREE_VALUE (basetype);
1757 }
1758
1759 if (basetype != NULL_TREE)
1760 ;
1761 /* call to a constructor... */
1762 else if (basetype_path)
1763 {
1764 basetype = BINFO_TYPE (basetype_path);
1765 if (name == TYPE_IDENTIFIER (basetype))
1766 name = ctor_identifier;
1767 }
1768 else if (IDENTIFIER_HAS_TYPE_VALUE (name))
1769 {
1770 basetype = IDENTIFIER_TYPE_VALUE (name);
1771 name = ctor_identifier;
1772 }
1773 else
1774 {
1775 tree typedef_name = lookup_name (name, 1);
1776 if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
1777 {
1778 /* Canonicalize the typedef name. */
1779 basetype = TREE_TYPE (typedef_name);
1780 name = ctor_identifier;
1781 }
1782 else
1783 {
1784 cp_error ("no constructor named `%T' in scope",
1785 name);
1786 return error_mark_node;
1787 }
1788 }
1789
1790 if (! IS_AGGR_TYPE (basetype))
1791 {
1792 non_aggr_error:
1793 if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
1794 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1795 name, instance, basetype);
1796
1797 return error_mark_node;
1798 }
1799 }
1800 else if (instance == current_class_ref || instance == current_class_ptr)
1801 {
1802 /* When doing initialization, we side-effect the TREE_TYPE of
1803 current_class_ref, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
1804 basetype = TREE_TYPE (current_class_ref);
1805
1806 /* Anything manifestly `this' in constructors and destructors
1807 has a known type, so virtual function tables are not needed. */
1808 if (TYPE_VIRTUAL_P (basetype)
1809 && !(flags & LOOKUP_NONVIRTUAL))
1810 need_vtbl = (dtor_label || ctor_label)
1811 ? unneeded : maybe_needed;
1812
1813 /* If `this' is a signature pointer and `name' is not a constructor,
1814 we are calling a signature member function. In that case, set the
1815 `basetype' to the signature type and dereference the `optr' field. */
1816 if (IS_SIGNATURE_POINTER (basetype)
1817 && TYPE_IDENTIFIER (basetype) != name)
1818 {
1819 basetype = SIGNATURE_TYPE (basetype);
1820 instance_ptr = build_optr_ref (instance);
1821 instance_ptr = convert (build_pointer_type (basetype), instance_ptr);
1822 basetype_path = TYPE_BINFO (basetype);
1823 }
1824 else
1825 {
1826 instance = current_class_ref;
1827 instance_ptr = current_class_ptr;
1828 basetype_path = TYPE_BINFO (current_class_type);
1829 }
1830 result = build_field_call (basetype_path, instance_ptr, name, parms);
1831
1832 if (result)
1833 return result;
1834 }
1835 else if (TREE_CODE (instance) == RESULT_DECL)
1836 {
1837 basetype = TREE_TYPE (instance);
1838 /* Should we ever have to make a virtual function reference
1839 from a RESULT_DECL, know that it must be of fixed type
1840 within the scope of this function. */
1841 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1842 need_vtbl = maybe_needed;
1843 instance_ptr = build1 (ADDR_EXPR, build_pointer_type (basetype), instance);
1844 }
1845 else
1846 {
1847 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
1848 tree inst_ptr_basetype;
1849
1850 static_call_context =
1851 (TREE_CODE (instance) == INDIRECT_REF
1852 && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
1853 && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
1854
1855 if (TREE_CODE (instance) == OFFSET_REF)
1856 instance = resolve_offset_ref (instance);
1857
1858 /* the base type of an instance variable is pointer to class */
1859 basetype = TREE_TYPE (instance);
1860
1861 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1862 {
1863 basetype = TREE_TYPE (basetype);
1864 if (! IS_AGGR_TYPE (basetype))
1865 goto non_aggr_error;
1866 /* Call to convert not needed because we are remaining
1867 within the same type. */
1868 instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
1869 instance);
1870 inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
1871 }
1872 else
1873 {
1874 if (! IS_AGGR_TYPE (basetype)
1875 && ! (TYPE_LANG_SPECIFIC (basetype)
1876 && (IS_SIGNATURE_POINTER (basetype)
1877 || IS_SIGNATURE_REFERENCE (basetype))))
1878 goto non_aggr_error;
1879
1880 /* If `instance' is a signature pointer/reference and `name' is
1881 not a constructor, we are calling a signature member function.
1882 In that case set the `basetype' to the signature type. */
1883 if ((IS_SIGNATURE_POINTER (basetype)
1884 || IS_SIGNATURE_REFERENCE (basetype))
1885 && TYPE_IDENTIFIER (basetype) != name)
1886 basetype = SIGNATURE_TYPE (basetype);
1887
1888 if ((IS_SIGNATURE (basetype)
1889 && (instance_ptr = instance))
1890 || (lvalue_p (instance)
1891 && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
1892 || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
1893 {
1894 if (instance_ptr == error_mark_node)
1895 return error_mark_node;
1896 }
1897 else if (TREE_CODE (instance) == NOP_EXPR
1898 || TREE_CODE (instance) == CONSTRUCTOR)
1899 {
1900 /* A cast is not an lvalue. Initialize a fresh temp
1901 with the value we are casting from, and proceed with
1902 that temporary. We can't cast to a reference type,
1903 so that simplifies the initialization to something
1904 we can manage. */
1905 tree temp = get_temp_name (TREE_TYPE (instance), 0);
1906 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
1907 expand_aggr_init (temp, instance, 0, flags);
1908 else
1909 {
1910 store_init_value (temp, instance);
1911 expand_decl_init (temp);
1912 }
1913 instance = temp;
1914 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1915 }
1916 else
1917 {
1918 if (TREE_CODE (instance) != CALL_EXPR)
1919 my_friendly_abort (125);
1920 if (TYPE_NEEDS_CONSTRUCTING (basetype))
1921 instance = build_cplus_new (basetype, instance);
1922 else
1923 {
1924 instance = get_temp_name (basetype, 0);
1925 TREE_ADDRESSABLE (instance) = 1;
1926 }
1927 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1928 }
1929 /* @@ Should we call comp_target_types here? */
1930 if (IS_SIGNATURE (basetype))
1931 inst_ptr_basetype = basetype;
1932 else
1933 inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
1934 if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
1935 basetype = inst_ptr_basetype;
1936 else
1937 {
1938 instance_ptr = convert (build_pointer_type (basetype), instance_ptr);
1939 if (instance_ptr == error_mark_node)
1940 return error_mark_node;
1941 }
1942 }
1943
1944 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1945 not updated, so we use `basetype' instead. */
1946 if (basetype_path == NULL_TREE
1947 && IS_SIGNATURE (basetype))
1948 basetype_path = TYPE_BINFO (basetype);
1949 else if (basetype_path == NULL_TREE ||
1950 BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
1951 basetype_path = TYPE_BINFO (inst_ptr_basetype);
1952
1953 result = build_field_call (basetype_path, instance_ptr, name, parms);
1954 if (result)
1955 return result;
1956
1957 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1958 {
1959 if (TREE_SIDE_EFFECTS (instance_ptr))
1960 {
1961 /* This action is needed because the instance is needed
1962 for providing the base of the virtual function table.
1963 Without using a SAVE_EXPR, the function we are building
1964 may be called twice, or side effects on the instance
1965 variable (such as a post-increment), may happen twice. */
1966 instance_ptr = save_expr (instance_ptr);
1967 instance = build_indirect_ref (instance_ptr, NULL_PTR);
1968 }
1969 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1970 {
1971 /* This happens when called for operator new (). */
1972 instance = build_indirect_ref (instance, NULL_PTR);
1973 }
1974
1975 need_vtbl = maybe_needed;
1976 }
1977 }
1978
1979 if (save_name == ctor_identifier)
1980 save_name = TYPE_IDENTIFIER (basetype);
1981
1982 if (TYPE_SIZE (complete_type (basetype)) == 0)
1983 {
1984 /* This is worth complaining about, I think. */
1985 cp_error ("cannot lookup method in incomplete type `%T'", basetype);
1986 return error_mark_node;
1987 }
1988
1989 save_basetype = TYPE_MAIN_VARIANT (basetype);
1990
1991 last = NULL_TREE;
1992 for (parmtypes = NULL_TREE, parm = parms; parm; parm = TREE_CHAIN (parm))
1993 {
1994 tree t = TREE_TYPE (TREE_VALUE (parm));
1995 if (TREE_CODE (t) == OFFSET_TYPE)
1996 {
1997 /* Convert OFFSET_TYPE entities to their normal selves. */
1998 TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
1999 t = TREE_TYPE (TREE_VALUE (parm));
2000 }
2001 if (TREE_CODE (t) == METHOD_TYPE)
2002 {
2003 cp_pedwarn ("assuming & on `%E'", TREE_VALUE (parm));
2004 TREE_VALUE (parm) = build_unary_op (ADDR_EXPR, TREE_VALUE (parm), 0);
2005 }
2006 #if 0
2007 /* This breaks reference-to-array parameters. */
2008 if (TREE_CODE (t) == ARRAY_TYPE)
2009 {
2010 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
2011 This eliminates needless calls to `compute_conversion_costs'. */
2012 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
2013 t = TREE_TYPE (TREE_VALUE (parm));
2014 }
2015 #endif
2016 if (t == error_mark_node)
2017 return error_mark_node;
2018 last = build_tree_list (NULL_TREE, t);
2019 parmtypes = chainon (parmtypes, last);
2020 }
2021
2022 if (instance && IS_SIGNATURE (basetype))
2023 {
2024 /* @@ Should this be the constp/volatilep flags for the optr field
2025 of the signature pointer? */
2026 constp = TYPE_READONLY (basetype);
2027 volatilep = TYPE_VOLATILE (basetype);
2028 parms = tree_cons (NULL_TREE, instance_ptr, parms);
2029 }
2030 else if (instance)
2031 {
2032 /* TREE_READONLY (instance) fails for references. */
2033 constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
2034 volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
2035 parms = tree_cons (NULL_TREE, instance_ptr, parms);
2036 }
2037 else
2038 {
2039 /* Raw constructors are always in charge. */
2040 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2041 && ! (flags & LOOKUP_HAS_IN_CHARGE))
2042 {
2043 flags |= LOOKUP_HAS_IN_CHARGE;
2044 parms = tree_cons (NULL_TREE, integer_one_node, parms);
2045 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
2046 }
2047
2048 constp = 0;
2049 volatilep = 0;
2050 instance_ptr = build_int_2 (0, 0);
2051 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
2052 parms = tree_cons (NULL_TREE, instance_ptr, parms);
2053 }
2054
2055 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
2056
2057 if (last == NULL_TREE)
2058 last = parmtypes;
2059
2060 /* Look up function name in the structure type definition. */
2061
2062 /* FIXME Axe most of this now? */
2063 if ((IDENTIFIER_HAS_TYPE_VALUE (name)
2064 && ! IDENTIFIER_OPNAME_P (name)
2065 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name)))
2066 || name == constructor_name (basetype)
2067 || name == ctor_identifier)
2068 {
2069 tree tmp = NULL_TREE;
2070 if (IDENTIFIER_TYPE_VALUE (name) == basetype
2071 || name == constructor_name (basetype)
2072 || name == ctor_identifier)
2073 tmp = TYPE_BINFO (basetype);
2074 else
2075 tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
2076
2077 if (tmp != NULL_TREE)
2078 {
2079 name_kind = "constructor";
2080
2081 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2082 && ! (flags & LOOKUP_HAS_IN_CHARGE))
2083 {
2084 /* Constructors called for initialization
2085 only are never in charge. */
2086 tree tmplist;
2087
2088 flags |= LOOKUP_HAS_IN_CHARGE;
2089 tmplist = tree_cons (NULL_TREE, integer_zero_node,
2090 TREE_CHAIN (parms));
2091 TREE_CHAIN (parms) = tmplist;
2092 tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
2093 TREE_CHAIN (parmtypes) = tmplist;
2094 }
2095 basetype = BINFO_TYPE (tmp);
2096 }
2097 else
2098 name_kind = "method";
2099 }
2100 else
2101 name_kind = "method";
2102
2103 if (basetype_path == NULL_TREE
2104 || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
2105 basetype_path = TYPE_BINFO (basetype);
2106 result = lookup_fnfields (basetype_path, name,
2107 (flags & LOOKUP_COMPLAIN));
2108 if (result == error_mark_node)
2109 return error_mark_node;
2110
2111 for (pass = 0; pass < 2; pass++)
2112 {
2113 struct candidate *candidates;
2114 struct candidate *cp;
2115 int len;
2116 unsigned best = 1;
2117
2118 baselink = result;
2119
2120 if (pass > 0)
2121 {
2122 candidates
2123 = (struct candidate *) alloca ((ever_seen+1)
2124 * sizeof (struct candidate));
2125 bzero ((char *) candidates, (ever_seen + 1) * sizeof (struct candidate));
2126 cp = candidates;
2127 len = list_length (parms);
2128 ever_seen = 0;
2129
2130 /* First see if a global function has a shot at it. */
2131 if (flags & LOOKUP_GLOBAL)
2132 {
2133 tree friend_parms;
2134 tree parm = instance_ptr;
2135
2136 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
2137 parm = convert_from_reference (parm);
2138 else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
2139 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
2140 else
2141 my_friendly_abort (167);
2142
2143 friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
2144
2145 cp->h_len = len;
2146 cp->harshness = (struct harshness_code *)
2147 alloca ((len + 1) * sizeof (struct harshness_code));
2148
2149 result = build_overload_call (name, friend_parms, 0, cp);
2150
2151 /* If it turns out to be the one we were actually looking for
2152 (it was probably a friend function), the return the
2153 good result. */
2154 if (TREE_CODE (result) == CALL_EXPR)
2155 return result;
2156
2157 while ((cp->h.code & EVIL_CODE) == 0)
2158 {
2159 /* non-standard uses: set the field to 0 to indicate
2160 we are using a non-member function. */
2161 cp->u.field = 0;
2162 if (cp->harshness[len].distance == 0
2163 && cp->h.code < best)
2164 best = cp->h.code;
2165 cp += 1;
2166 }
2167 }
2168 }
2169
2170 if (baselink)
2171 {
2172 /* We have a hit (of sorts). If the parameter list is
2173 "error_mark_node", or some variant thereof, it won't
2174 match any methods. Since we have verified that the is
2175 some method vaguely matching this one (in name at least),
2176 silently return.
2177
2178 Don't stop for friends, however. */
2179 basetype_path = TREE_PURPOSE (baselink);
2180
2181 function = TREE_VALUE (baselink);
2182 if (TREE_CODE (basetype_path) == TREE_LIST)
2183 basetype_path = TREE_VALUE (basetype_path);
2184 basetype = BINFO_TYPE (basetype_path);
2185
2186 for (; function; function = DECL_CHAIN (function))
2187 {
2188 #ifdef GATHER_STATISTICS
2189 n_inner_fields_searched++;
2190 #endif
2191 ever_seen++;
2192 if (pass > 0)
2193 found_fns = tree_cons (NULL_TREE, function, found_fns);
2194
2195 /* Not looking for friends here. */
2196 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
2197 && ! DECL_STATIC_FUNCTION_P (function))
2198 continue;
2199
2200 if (pass > 0)
2201 {
2202 tree these_parms = parms;
2203
2204 #ifdef GATHER_STATISTICS
2205 n_inner_fields_searched++;
2206 #endif
2207 cp->h_len = len;
2208 cp->harshness = (struct harshness_code *)
2209 alloca ((len + 1) * sizeof (struct harshness_code));
2210
2211 if (DECL_STATIC_FUNCTION_P (function))
2212 these_parms = TREE_CHAIN (these_parms);
2213 compute_conversion_costs (function, these_parms, cp, len);
2214
2215 if ((cp->h.code & EVIL_CODE) == 0)
2216 {
2217 cp->u.field = function;
2218 cp->function = function;
2219 cp->basetypes = basetype_path;
2220
2221 /* Don't allow non-converting constructors to convert. */
2222 if (flags & LOOKUP_ONLYCONVERTING
2223 && DECL_LANG_SPECIFIC (function)
2224 && DECL_NONCONVERTING_P (function))
2225 continue;
2226
2227 /* No "two-level" conversions. */
2228 if (flags & LOOKUP_NO_CONVERSION
2229 && (cp->h.code & USER_CODE))
2230 continue;
2231
2232 cp++;
2233 }
2234 }
2235 }
2236 }
2237
2238 if (pass == 0)
2239 {
2240 tree igv = lookup_name_nonclass (name);
2241
2242 /* No exact match could be found. Now try to find match
2243 using default conversions. */
2244 if ((flags & LOOKUP_GLOBAL) && igv)
2245 {
2246 if (TREE_CODE (igv) == FUNCTION_DECL)
2247 ever_seen += 1;
2248 else if (TREE_CODE (igv) == TREE_LIST)
2249 ever_seen += count_functions (igv);
2250 }
2251
2252 if (ever_seen == 0)
2253 {
2254 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2255 == LOOKUP_SPECULATIVELY)
2256 return NULL_TREE;
2257
2258 TREE_CHAIN (last) = void_list_node;
2259 if (flags & LOOKUP_GLOBAL)
2260 cp_error ("no global or member function `%D(%A)' defined",
2261 save_name, parmtypes);
2262 else
2263 cp_error ("no member function `%T::%D(%A)' defined",
2264 save_basetype, save_name, TREE_CHAIN (parmtypes));
2265 return error_mark_node;
2266 }
2267 continue;
2268 }
2269
2270 if (cp - candidates != 0)
2271 {
2272 /* Rank from worst to best. Then cp will point to best one.
2273 Private fields have their bits flipped. For unsigned
2274 numbers, this should make them look very large.
2275 If the best alternate has a (signed) negative value,
2276 then all we ever saw were private members. */
2277 if (cp - candidates > 1)
2278 {
2279 int n_candidates = cp - candidates;
2280 extern int warn_synth;
2281 TREE_VALUE (parms) = instance_ptr;
2282 cp = ideal_candidate (candidates, n_candidates, len);
2283 if (cp == (struct candidate *)0)
2284 {
2285 if (flags & LOOKUP_COMPLAIN)
2286 {
2287 TREE_CHAIN (last) = void_list_node;
2288 cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2289 name_kind, save_name, TREE_CHAIN (parmtypes));
2290 print_n_candidates (candidates, n_candidates);
2291 }
2292 return error_mark_node;
2293 }
2294 if (cp->h.code & EVIL_CODE)
2295 return error_mark_node;
2296 if (warn_synth
2297 && DECL_NAME (cp->function) == ansi_opname[MODIFY_EXPR]
2298 && DECL_ARTIFICIAL (cp->function)
2299 && n_candidates == 2)
2300 {
2301 cp_warning ("using synthesized `%#D' for copy assignment",
2302 cp->function);
2303 cp_warning_at (" where cfront would use `%#D'",
2304 candidates->function);
2305 }
2306 }
2307 else if (cp[-1].h.code & EVIL_CODE)
2308 {
2309 if (flags & LOOKUP_COMPLAIN)
2310 cp_error ("ambiguous type conversion requested for %s `%D'",
2311 name_kind, save_name);
2312 return error_mark_node;
2313 }
2314 else
2315 cp--;
2316
2317 /* The global function was the best, so use it. */
2318 if (cp->u.field == 0)
2319 {
2320 /* We must convert the instance pointer into a reference type.
2321 Global overloaded functions can only either take
2322 aggregate objects (which come for free from references)
2323 or reference data types anyway. */
2324 TREE_VALUE (parms) = copy_node (instance_ptr);
2325 TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
2326 return build_function_call (cp->function, parms);
2327 }
2328
2329 function = cp->function;
2330 basetype_path = cp->basetypes;
2331 if (! DECL_STATIC_FUNCTION_P (function))
2332 TREE_VALUE (parms) = cp->arg;
2333 goto found_and_maybe_warn;
2334 }
2335
2336 if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
2337 {
2338 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2339 == LOOKUP_SPECULATIVELY)
2340 return NULL_TREE;
2341
2342 if (DECL_STATIC_FUNCTION_P (cp->function))
2343 parms = TREE_CHAIN (parms);
2344 if (ever_seen)
2345 {
2346 if (flags & LOOKUP_SPECULATIVELY)
2347 return NULL_TREE;
2348 if (static_call_context
2349 && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
2350 cp_error ("object missing in call to `%D'", cp->function);
2351 else if (ever_seen > 1)
2352 {
2353 TREE_CHAIN (last) = void_list_node;
2354 cp_error ("no matching function for call to `%T::%D (%A)%V'",
2355 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr))),
2356 save_name, TREE_CHAIN (parmtypes),
2357 TREE_TYPE (TREE_TYPE (instance_ptr)));
2358 TREE_CHAIN (last) = NULL_TREE;
2359 print_candidates (found_fns);
2360 }
2361 else
2362 report_type_mismatch (cp, parms, name_kind);
2363 return error_mark_node;
2364 }
2365
2366 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2367 == LOOKUP_COMPLAIN)
2368 {
2369 cp_error ("%T has no method named %D", save_basetype, save_name);
2370 return error_mark_node;
2371 }
2372 return NULL_TREE;
2373 }
2374 continue;
2375
2376 found_and_maybe_warn:
2377 if ((cp->harshness[0].code & CONST_CODE)
2378 /* 12.1p2: Constructors can be called for const objects. */
2379 && ! DECL_CONSTRUCTOR_P (cp->function))
2380 {
2381 if (flags & LOOKUP_COMPLAIN)
2382 {
2383 cp_error_at ("non-const member function `%D'", cp->function);
2384 error ("called for const object at this point in file");
2385 }
2386 /* Not good enough for a match. */
2387 else
2388 return error_mark_node;
2389 }
2390 goto found;
2391 }
2392 /* Silently return error_mark_node. */
2393 return error_mark_node;
2394
2395 found:
2396 if (flags & LOOKUP_PROTECT)
2397 access = compute_access (basetype_path, function);
2398
2399 if (access == access_private_node)
2400 {
2401 if (flags & LOOKUP_COMPLAIN)
2402 {
2403 cp_error_at ("%s `%+#D' is %s", name_kind, function,
2404 TREE_PRIVATE (function) ? "private"
2405 : "from private base class");
2406 error ("within this context");
2407 }
2408 return error_mark_node;
2409 }
2410 else if (access == access_protected_node)
2411 {
2412 if (flags & LOOKUP_COMPLAIN)
2413 {
2414 cp_error_at ("%s `%+#D' %s", name_kind, function,
2415 TREE_PROTECTED (function) ? "is protected"
2416 : "has protected accessibility");
2417 error ("within this context");
2418 }
2419 return error_mark_node;
2420 }
2421
2422 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2423 type (if it exists) is a pointer to. */
2424
2425 if (DECL_ABSTRACT_VIRTUAL_P (function)
2426 && instance == current_class_ref
2427 && DECL_CONSTRUCTOR_P (current_function_decl)
2428 && ! (flags & LOOKUP_NONVIRTUAL)
2429 && value_member (function, get_abstract_virtuals (basetype)))
2430 cp_error ("abstract virtual `%#D' called from constructor", function);
2431
2432 if (IS_SIGNATURE (basetype) && static_call_context)
2433 {
2434 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2435 basetype, save_name);
2436 return error_mark_node;
2437 }
2438 else if (IS_SIGNATURE (basetype))
2439 return build_signature_method_call (basetype, instance, function, parms);
2440
2441 function = DECL_MAIN_VARIANT (function);
2442 mark_used (function);
2443
2444 /* Is it a synthesized method that needs to be synthesized? */
2445 if (DECL_ARTIFICIAL (function) && ! DECL_INITIAL (function)
2446 /* Kludge: don't synthesize for default args. */
2447 && current_function_decl)
2448 synthesize_method (function);
2449
2450 if (pedantic && DECL_THIS_INLINE (function) && ! DECL_ARTIFICIAL (function)
2451 && ! DECL_INITIAL (function) && ! DECL_PENDING_INLINE_INFO (function)
2452 && ! (DECL_TEMPLATE_INFO (function)
2453 && TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (function))))
2454 cp_warning ("inline function `%#D' called before definition", function);
2455
2456 fntype = TREE_TYPE (function);
2457 if (TREE_CODE (fntype) == POINTER_TYPE)
2458 fntype = TREE_TYPE (fntype);
2459 basetype = DECL_CLASS_CONTEXT (function);
2460
2461 /* If we are referencing a virtual function from an object
2462 of effectively static type, then there is no need
2463 to go through the virtual function table. */
2464 if (need_vtbl == maybe_needed)
2465 {
2466 int fixed_type = resolves_to_fixed_type_p (instance, 0);
2467
2468 if (all_virtual == 1
2469 && DECL_VINDEX (function)
2470 && may_be_remote (basetype))
2471 need_vtbl = needed;
2472 else if (DECL_VINDEX (function))
2473 need_vtbl = fixed_type ? unneeded : needed;
2474 else
2475 need_vtbl = not_needed;
2476 }
2477
2478 if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
2479 && !DECL_CONSTRUCTOR_P (function))
2480 {
2481 /* Let's be nasty to the user now, and give reasonable
2482 error messages. */
2483 instance_ptr = current_class_ptr;
2484 if (instance_ptr)
2485 {
2486 if (basetype != current_class_type)
2487 {
2488 if (basetype == error_mark_node)
2489 return error_mark_node;
2490 else
2491 {
2492 if (orig_basetype != NULL_TREE)
2493 error_not_base_type (orig_basetype, current_class_type);
2494 else
2495 error_not_base_type (function, current_class_type);
2496 return error_mark_node;
2497 }
2498 }
2499 }
2500 /* Only allow a static member function to call another static member
2501 function. */
2502 else if (DECL_LANG_SPECIFIC (function)
2503 && !DECL_STATIC_FUNCTION_P (function))
2504 {
2505 cp_error ("cannot call member function `%D' without object",
2506 function);
2507 return error_mark_node;
2508 }
2509 }
2510
2511 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2512
2513 if (TYPE_SIZE (complete_type (value_type)) == 0)
2514 {
2515 if (flags & LOOKUP_COMPLAIN)
2516 incomplete_type_error (0, value_type);
2517 return error_mark_node;
2518 }
2519
2520 if (DECL_STATIC_FUNCTION_P (function))
2521 parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2522 TREE_CHAIN (parms), function, LOOKUP_NORMAL);
2523 else if (need_vtbl == unneeded)
2524 {
2525 int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
2526 basetype = TREE_TYPE (instance);
2527 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function)) != TYPE_MAIN_VARIANT (basetype)
2528 && TYPE_USES_COMPLEX_INHERITANCE (basetype))
2529 {
2530 basetype = DECL_CLASS_CONTEXT (function);
2531 instance_ptr = convert_pointer_to (basetype, instance_ptr);
2532 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2533 }
2534 parms = tree_cons (NULL_TREE, instance_ptr,
2535 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, sub_flags));
2536 }
2537 else
2538 {
2539 if ((flags & LOOKUP_NONVIRTUAL) == 0)
2540 basetype = DECL_CONTEXT (function);
2541
2542 /* First parm could be integer_zerop with casts like
2543 ((Object*)0)->Object::IsA() */
2544 if (!integer_zerop (TREE_VALUE (parms)))
2545 {
2546 /* Since we can't have inheritance with a union, doing get_binfo
2547 on it won't work. We do all the convert_pointer_to_real
2548 stuff to handle MI correctly...for unions, that's not
2549 an issue, so we must short-circuit that extra work here. */
2550 tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
2551 if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
2552 instance_ptr = TREE_VALUE (parms);
2553 else
2554 {
2555 tree binfo = get_binfo (basetype,
2556 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
2557 0);
2558 instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
2559 }
2560 instance_ptr
2561 = convert_pointer_to (build_type_variant (basetype,
2562 constp, volatilep),
2563 instance_ptr);
2564
2565 if (TREE_CODE (instance_ptr) == COND_EXPR)
2566 {
2567 instance_ptr = save_expr (instance_ptr);
2568 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2569 }
2570 else if (TREE_CODE (instance_ptr) == NOP_EXPR
2571 && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
2572 && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
2573 ;
2574 /* The call to `convert_pointer_to' may return error_mark_node. */
2575 else if (TREE_CODE (instance_ptr) == ERROR_MARK)
2576 return instance_ptr;
2577 else if (instance == NULL_TREE
2578 || TREE_CODE (instance) != INDIRECT_REF
2579 || TREE_OPERAND (instance, 0) != instance_ptr)
2580 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2581 }
2582 parms = tree_cons (NULL_TREE, instance_ptr,
2583 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, LOOKUP_NORMAL));
2584 }
2585
2586 if (parms == error_mark_node
2587 || (parms && TREE_CHAIN (parms) == error_mark_node))
2588 return error_mark_node;
2589
2590 if (need_vtbl == needed)
2591 {
2592 function = build_vfn_ref (&TREE_VALUE (parms), instance,
2593 DECL_VINDEX (function));
2594 TREE_TYPE (function) = build_pointer_type (fntype);
2595 }
2596
2597 if (TREE_CODE (function) == FUNCTION_DECL)
2598 GNU_xref_call (current_function_decl,
2599 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
2600
2601 result = build_call (function, value_type, parms);
2602 result = convert_from_reference (result);
2603 return result;
2604 }
2605
2606 /* Similar to `build_method_call', but for overloaded non-member functions.
2607 The name of this function comes through NAME. The name depends
2608 on PARMS.
2609
2610 Note that this function must handle simple `C' promotions,
2611 as well as variable numbers of arguments (...), and
2612 default arguments to boot.
2613
2614 If the overloading is successful, we return a tree node which
2615 contains the call to the function.
2616
2617 If overloading produces candidates which are probable, but not definite,
2618 we hold these candidates. If FINAL_CP is non-zero, then we are free
2619 to assume that final_cp points to enough storage for all candidates that
2620 this function might generate. The `harshness' array is preallocated for
2621 the first candidate, but not for subsequent ones.
2622
2623 Note that the DECL_RTL of FUNCTION must be made to agree with this
2624 function's new name. */
2625
2626 tree
2627 build_overload_call_real (fnname, parms, flags, final_cp, require_complete)
2628 tree fnname, parms;
2629 int flags;
2630 struct candidate *final_cp;
2631 int require_complete;
2632 {
2633 /* must check for overloading here */
2634 tree functions, function, parm;
2635 tree parmtypes = NULL_TREE, last = NULL_TREE;
2636 register tree outer;
2637 int length;
2638 int parmlength = list_length (parms);
2639
2640 struct candidate *candidates, *cp;
2641
2642 if (final_cp)
2643 {
2644 final_cp[0].h.code = 0;
2645 final_cp[0].h.distance = 0;
2646 final_cp[0].function = 0;
2647 /* end marker. */
2648 final_cp[1].h.code = EVIL_CODE;
2649 }
2650
2651 for (parm = parms; parm; parm = TREE_CHAIN (parm))
2652 {
2653 register tree t = TREE_TYPE (TREE_VALUE (parm));
2654
2655 if (t == error_mark_node)
2656 {
2657 if (final_cp)
2658 final_cp->h.code = EVIL_CODE;
2659 return error_mark_node;
2660 }
2661 if (TREE_CODE (t) == OFFSET_TYPE)
2662 #if 0
2663 /* This breaks reference-to-array parameters. */
2664 || TREE_CODE (t) == ARRAY_TYPE
2665 #endif
2666 {
2667 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
2668 Also convert OFFSET_TYPE entities to their normal selves.
2669 This eliminates needless calls to `compute_conversion_costs'. */
2670 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
2671 t = TREE_TYPE (TREE_VALUE (parm));
2672 }
2673 last = build_tree_list (NULL_TREE, t);
2674 parmtypes = chainon (parmtypes, last);
2675 }
2676 if (last)
2677 TREE_CHAIN (last) = void_list_node;
2678 else
2679 parmtypes = void_list_node;
2680
2681 if (is_overloaded_fn (fnname))
2682 {
2683 functions = fnname;
2684 if (TREE_CODE (fnname) == TREE_LIST)
2685 fnname = TREE_PURPOSE (functions);
2686 else if (TREE_CODE (fnname) == FUNCTION_DECL)
2687 fnname = DECL_NAME (functions);
2688 }
2689 else
2690 functions = lookup_name_nonclass (fnname);
2691
2692 if (functions == NULL_TREE)
2693 {
2694 if (flags & LOOKUP_SPECULATIVELY)
2695 return NULL_TREE;
2696 if (flags & LOOKUP_COMPLAIN)
2697 error ("only member functions apply");
2698 if (final_cp)
2699 final_cp->h.code = EVIL_CODE;
2700 return error_mark_node;
2701 }
2702
2703 if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
2704 {
2705 functions = DECL_MAIN_VARIANT (functions);
2706 if (final_cp)
2707 {
2708 /* We are just curious whether this is a viable alternative or
2709 not. */
2710 compute_conversion_costs (functions, parms, final_cp, parmlength);
2711 return functions;
2712 }
2713 else
2714 return build_function_call_real (functions, parms, 1, flags);
2715 }
2716
2717 if (TREE_CODE (functions) == TREE_LIST
2718 && TREE_VALUE (functions) == NULL_TREE)
2719 {
2720 if (flags & LOOKUP_SPECULATIVELY)
2721 return NULL_TREE;
2722
2723 if (flags & LOOKUP_COMPLAIN)
2724 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2725 TREE_PURPOSE (functions));
2726 if (final_cp)
2727 final_cp->h.code = EVIL_CODE;
2728 return error_mark_node;
2729 }
2730
2731 length = count_functions (functions);
2732
2733 if (final_cp)
2734 candidates = final_cp;
2735 else
2736 {
2737 candidates
2738 = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
2739 bzero ((char *) candidates, (length + 1) * sizeof (struct candidate));
2740 }
2741
2742 cp = candidates;
2743
2744 my_friendly_assert (is_overloaded_fn (functions), 169);
2745
2746 functions = get_first_fn (functions);
2747
2748 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
2749 for (outer = functions; outer; outer = DECL_CHAIN (outer))
2750 {
2751 int template_cost = 0;
2752 function = outer;
2753 if (TREE_CODE (function) != FUNCTION_DECL
2754 && ! (TREE_CODE (function) == TEMPLATE_DECL
2755 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
2756 {
2757 enum tree_code code = TREE_CODE (function);
2758 if (code == TEMPLATE_DECL)
2759 code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
2760 if (code == CONST_DECL)
2761 cp_error_at
2762 ("enumeral value `%D' conflicts with function of same name",
2763 function);
2764 else if (code == VAR_DECL)
2765 {
2766 if (TREE_STATIC (function))
2767 cp_error_at
2768 ("variable `%D' conflicts with function of same name",
2769 function);
2770 else
2771 cp_error_at
2772 ("constant field `%D' conflicts with function of same name",
2773 function);
2774 }
2775 else if (code == TYPE_DECL)
2776 continue;
2777 else
2778 my_friendly_abort (2);
2779 error ("at this point in file");
2780 continue;
2781 }
2782 if (TREE_CODE (function) == TEMPLATE_DECL)
2783 {
2784 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
2785 tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
2786 int i;
2787
2788 i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
2789 TYPE_ARG_TYPES (TREE_TYPE (function)),
2790 parms, &template_cost, 0);
2791 if (i == 0)
2792 {
2793 function = instantiate_template (function, targs);
2794 if (function == error_mark_node)
2795 return function;
2796 }
2797 }
2798
2799 if (TREE_CODE (function) == TEMPLATE_DECL)
2800 {
2801 /* Unconverted template -- failed match. */
2802 cp->function = function;
2803 cp->u.bad_arg = -4;
2804 cp->h.code = EVIL_CODE;
2805 }
2806 else
2807 {
2808 struct candidate *cp2;
2809
2810 /* Check that this decl is not the same as a function that's in
2811 the list due to some template instantiation. */
2812 cp2 = candidates;
2813 while (cp2 != cp)
2814 if (cp2->function == function)
2815 break;
2816 else
2817 cp2 += 1;
2818 if (cp2->function == function)
2819 continue;
2820
2821 function = DECL_MAIN_VARIANT (function);
2822
2823 /* Can't use alloca here, since result might be
2824 passed to calling function. */
2825 cp->h_len = parmlength;
2826 cp->harshness = (struct harshness_code *)
2827 oballoc ((parmlength + 1) * sizeof (struct harshness_code));
2828
2829 compute_conversion_costs (function, parms, cp, parmlength);
2830
2831 /* Make sure this is clear as well. */
2832 cp->h.int_penalty += template_cost;
2833
2834 if ((cp[0].h.code & EVIL_CODE) == 0)
2835 {
2836 cp[1].h.code = EVIL_CODE;
2837 cp++;
2838 }
2839 }
2840 }
2841
2842 if (cp - candidates)
2843 {
2844 tree rval = error_mark_node;
2845
2846 /* Leave marker. */
2847 cp[0].h.code = EVIL_CODE;
2848 if (cp - candidates > 1)
2849 {
2850 struct candidate *best_cp
2851 = ideal_candidate (candidates, cp - candidates, parmlength);
2852 if (best_cp == (struct candidate *)0)
2853 {
2854 if (flags & LOOKUP_COMPLAIN)
2855 {
2856 cp_error ("call of overloaded `%D' is ambiguous", fnname);
2857 print_n_candidates (candidates, cp - candidates);
2858 }
2859 return error_mark_node;
2860 }
2861 else
2862 rval = best_cp->function;
2863 }
2864 else
2865 {
2866 cp -= 1;
2867 if (cp->h.code & EVIL_CODE)
2868 {
2869 if (flags & LOOKUP_COMPLAIN)
2870 error ("type conversion ambiguous");
2871 }
2872 else
2873 rval = cp->function;
2874 }
2875
2876 if (final_cp)
2877 return rval;
2878
2879 return build_function_call_real (rval, parms, require_complete, flags);
2880 }
2881
2882 if (flags & LOOKUP_SPECULATIVELY)
2883 return NULL_TREE;
2884
2885 if (flags & LOOKUP_COMPLAIN)
2886 report_type_mismatch (cp, parms, "function",
2887 decl_as_string (cp->function, 1));
2888
2889 return error_mark_node;
2890 }
2891
2892 /* This requires a complete type on the result of the call. */
2893 tree
2894 build_overload_call (fnname, parms, flags, final_cp)
2895 tree fnname, parms;
2896 int flags;
2897 struct candidate *final_cp;
2898 {
2899 return build_overload_call_real (fnname, parms, flags, final_cp, 1);
2900 }