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 tree cand = TREE_VALUE (conv);
644
645 if (winner && winner == cand)
646 continue;
647
648 tmp = convert_harshness (type, TREE_TYPE (TREE_TYPE (cand)), NULL_TREE);
649 if ((tmp.code < USER_CODE) && (tmp.distance >= 0))
650 {
651 if (winner)
652 return EVIL_CODE;
653 else
654 {
655 winner = cand;
656 code = tmp.code;
657 }
658 }
659 }
660
661 if (winner)
662 return code;
663
664 return -1;
665 }
666
667 int
668 can_convert (to, from)
669 tree to, from;
670 {
671 struct harshness_code h;
672 h = convert_harshness (to, from, NULL_TREE);
673 return (h.code < USER_CODE) && (h.distance >= 0);
674 }
675
676 int
677 can_convert_arg (to, from, arg)
678 tree to, from, arg;
679 {
680 struct harshness_code h;
681 h = convert_harshness (to, from, arg);
682 return (h.code < USER_CODE) && (h.distance >= 0);
683 }
684
685 #ifdef DEBUG_MATCHING
686 static char *
687 print_harshness (h)
688 struct harshness_code *h;
689 {
690 static char buf[1024];
691 char tmp[1024];
692
693 bzero (buf, 1024 * sizeof (char));
694 strcat (buf, "codes=[");
695 if (h->code & EVIL_CODE)
696 strcat (buf, "EVIL");
697 if (h->code & CONST_CODE)
698 strcat (buf, " CONST");
699 if (h->code & ELLIPSIS_CODE)
700 strcat (buf, " ELLIPSIS");
701 if (h->code & USER_CODE)
702 strcat (buf, " USER");
703 if (h->code & STD_CODE)
704 strcat (buf, " STD");
705 if (h->code & PROMO_CODE)
706 strcat (buf, " PROMO");
707 if (h->code & QUAL_CODE)
708 strcat (buf, " QUAL");
709 if (h->code & TRIVIAL_CODE)
710 strcat (buf, " TRIVIAL");
711 if (buf[0] == '\0')
712 strcat (buf, "0");
713
714 sprintf (tmp, "] distance=%d int_penalty=%d", h->distance, h->int_penalty);
715
716 strcat (buf, tmp);
717
718 return buf;
719 }
720 #endif
721
722 /* Algorithm: For each argument, calculate how difficult it is to
723 make FUNCTION accept that argument. If we can easily tell that
724 FUNCTION won't be acceptable to one of the arguments, then we
725 don't need to compute the ease of converting the other arguments,
726 since it will never show up in the intersection of all arguments'
727 favorite functions.
728
729 Conversions between builtin and user-defined types are allowed, but
730 no function involving such a conversion is preferred to one which
731 does not require such a conversion. Furthermore, such conversions
732 must be unique. */
733
734 void
735 compute_conversion_costs (function, tta_in, cp, arglen)
736 tree function;
737 tree tta_in;
738 struct candidate *cp;
739 int arglen;
740 {
741 tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
742 tree ttf = ttf_in;
743 tree tta = tta_in;
744
745 /* Start out with no strikes against. */
746 int evil_strikes = 0;
747 int ellipsis_strikes = 0;
748 int user_strikes = 0;
749 int b_or_d_strikes = 0;
750 int easy_strikes = 0;
751
752 int strike_index = 0, win;
753 struct harshness_code lose;
754 extern int cp_silent;
755
756 #ifdef GATHER_STATISTICS
757 n_compute_conversion_costs++;
758 #endif
759
760 #ifndef DEBUG_MATCHING
761 /* We don't emit any warnings or errors while trying out each candidate. */
762 cp_silent = 1;
763 #endif
764
765 cp->function = function;
766 cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
767 cp->u.bad_arg = 0; /* optimistic! */
768
769 cp->h.code = 0;
770 cp->h.distance = 0;
771 cp->h.int_penalty = 0;
772 bzero ((char *) cp->harshness,
773 (cp->h_len + 1) * sizeof (struct harshness_code));
774
775 while (ttf && tta)
776 {
777 struct harshness_code h;
778
779 if (ttf == void_list_node)
780 break;
781
782 if (type_unknown_p (TREE_VALUE (tta)))
783 {
784 /* Must perform some instantiation here. */
785 tree rhs = TREE_VALUE (tta);
786 tree lhstype = TREE_VALUE (ttf);
787
788 /* Keep quiet about possible contravariance violations. */
789 int old_inhibit_warnings = inhibit_warnings;
790 inhibit_warnings = 1;
791
792 /* @@ This is to undo what `grokdeclarator' does to
793 parameter types. It really should go through
794 something more general. */
795
796 TREE_TYPE (tta) = unknown_type_node;
797 rhs = instantiate_type (lhstype, rhs, 0);
798 inhibit_warnings = old_inhibit_warnings;
799
800 if (TREE_CODE (rhs) == ERROR_MARK)
801 h.code = EVIL_CODE;
802 else
803 h = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
804 }
805 else
806 {
807 #ifdef DEBUG_MATCHING
808 static tree old_function = NULL_TREE;
809
810 if (!old_function || function != old_function)
811 {
812 cp_error ("trying %D", function);
813 old_function = function;
814 }
815
816 cp_error (" doing (%T) %E against arg %T",
817 TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta),
818 TREE_VALUE (ttf));
819 #endif
820
821 h = convert_harshness (TREE_VALUE (ttf),
822 TREE_TYPE (TREE_VALUE (tta)),
823 TREE_VALUE (tta));
824
825 #ifdef DEBUG_MATCHING
826 cp_error (" evaluated %s", print_harshness (&h));
827 #endif
828 }
829
830 cp->harshness[strike_index] = h;
831 if ((h.code & EVIL_CODE)
832 || ((h.code & STD_CODE) && h.distance < 0))
833 {
834 cp->u.bad_arg = strike_index;
835 evil_strikes = 1;
836 }
837 else if (h.code & ELLIPSIS_CODE)
838 ellipsis_strikes += 1;
839 #if 0
840 /* This is never set by `convert_harshness'. */
841 else if (h.code & USER_CODE)
842 {
843 user_strikes += 1;
844 }
845 #endif
846 else
847 {
848 if ((h.code & STD_CODE) && h.distance)
849 {
850 if (h.distance > b_or_d_strikes)
851 b_or_d_strikes = h.distance;
852 }
853 else
854 easy_strikes += (h.code & (STD_CODE|PROMO_CODE|TRIVIAL_CODE));
855 cp->h.code |= h.code;
856 /* Make sure we communicate this. */
857 cp->h.int_penalty += h.int_penalty;
858 }
859
860 ttf = TREE_CHAIN (ttf);
861 tta = TREE_CHAIN (tta);
862 strike_index += 1;
863 }
864
865 if (tta)
866 {
867 /* ran out of formals, and parmlist is fixed size. */
868 if (ttf /* == void_type_node */)
869 {
870 cp->h.code = EVIL_CODE;
871 cp->u.bad_arg = -1;
872 cp_silent = 0;
873 return;
874 }
875 else
876 {
877 struct harshness_code h;
878 int l = list_length (tta);
879 ellipsis_strikes += l;
880 h.code = ELLIPSIS_CODE;
881 h.distance = 0;
882 h.int_penalty = 0;
883 for (; l; --l)
884 cp->harshness[strike_index++] = h;
885 }
886 }
887 else if (ttf && ttf != void_list_node)
888 {
889 /* ran out of actuals, and no defaults. */
890 if (TREE_PURPOSE (ttf) == NULL_TREE)
891 {
892 cp->h.code = EVIL_CODE;
893 cp->u.bad_arg = -2;
894 cp_silent = 0;
895 return;
896 }
897 /* Store index of first default. */
898 cp->harshness[arglen].distance = strike_index+1;
899 }
900 else
901 cp->harshness[arglen].distance = 0;
902
903 /* Argument list lengths work out, so don't need to check them again. */
904 if (evil_strikes)
905 {
906 /* We do not check for derived->base conversions here, since in
907 no case would they give evil strike counts, unless such conversions
908 are somehow ambiguous. */
909
910 /* See if any user-defined conversions apply.
911 But make sure that we do not loop. */
912 static int dont_convert_types = 0;
913
914 if (dont_convert_types)
915 {
916 cp->h.code = EVIL_CODE;
917 cp_silent = 0;
918 return;
919 }
920
921 win = 0; /* Only get one chance to win. */
922 ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
923 tta = tta_in;
924 strike_index = 0;
925 evil_strikes = 0;
926
927 while (ttf && tta)
928 {
929 if (ttf == void_list_node)
930 break;
931
932 lose = cp->harshness[strike_index];
933 if ((lose.code & EVIL_CODE)
934 || ((lose.code & STD_CODE) && lose.distance < 0))
935 {
936 tree actual_type = TREE_TYPE (TREE_VALUE (tta));
937 tree formal_type = TREE_VALUE (ttf);
938 int extra_conversions = 0;
939
940 dont_convert_types = 1;
941
942 if (TREE_CODE (formal_type) == REFERENCE_TYPE)
943 formal_type = TREE_TYPE (formal_type);
944 if (TREE_CODE (actual_type) == REFERENCE_TYPE)
945 actual_type = TREE_TYPE (actual_type);
946
947 if (formal_type != error_mark_node
948 && actual_type != error_mark_node)
949 {
950 formal_type = TYPE_MAIN_VARIANT (formal_type);
951 actual_type = TYPE_MAIN_VARIANT (actual_type);
952
953 if (TYPE_HAS_CONSTRUCTOR (formal_type))
954 {
955 /* If it has a constructor for this type,
956 try to use it. */
957 /* @@ There is no way to save this result yet, so
958 success is a NULL_TREE for now. */
959 if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
960 != error_mark_node)
961 win++;
962 }
963 if (TYPE_LANG_SPECIFIC (actual_type)
964 && TYPE_HAS_CONVERSION (actual_type))
965 {
966 int extra = user_harshness (formal_type, actual_type);
967
968 if (extra == EVIL_CODE)
969 win += 2;
970 else if (extra >= 0)
971 {
972 win++;
973 extra_conversions = extra;
974 }
975 }
976 }
977 dont_convert_types = 0;
978
979 if (win == 1)
980 {
981 user_strikes += 1;
982 cp->harshness[strike_index].code
983 = USER_CODE | (extra_conversions ? STD_CODE : 0);
984 win = 0;
985 }
986 else
987 {
988 if (cp->u.bad_arg > strike_index)
989 cp->u.bad_arg = strike_index;
990
991 evil_strikes = win ? 2 : 1;
992 break;
993 }
994 }
995
996 ttf = TREE_CHAIN (ttf);
997 tta = TREE_CHAIN (tta);
998 strike_index += 1;
999 }
1000 }
1001
1002 /* Const member functions get a small penalty because defaulting
1003 to const is less useful than defaulting to non-const. */
1004 /* This is bogus, it does not correspond to anything in the ARM.
1005 This code will be fixed when this entire section is rewritten
1006 to conform to the ARM. (mrs) */
1007 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1008 {
1009 tree this_parm = TREE_VALUE (ttf_in);
1010
1011 if (TREE_CODE (this_parm) == RECORD_TYPE /* Is `this' a sig ptr? */
1012 ? TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (this_parm))))
1013 : TYPE_READONLY (TREE_TYPE (this_parm)))
1014 {
1015 cp->harshness[0].code |= TRIVIAL_CODE;
1016 ++easy_strikes;
1017 }
1018 else
1019 {
1020 /* Calling a non-const member function from a const member function
1021 is probably invalid, but for now we let it only draw a warning.
1022 We indicate that such a mismatch has occurred by setting the
1023 harshness to a maximum value. */
1024 if (TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
1025 && (TYPE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))))
1026 cp->harshness[0].code |= CONST_CODE;
1027 }
1028 }
1029
1030 if (evil_strikes)
1031 cp->h.code = EVIL_CODE;
1032 if (ellipsis_strikes)
1033 cp->h.code |= ELLIPSIS_CODE;
1034 if (user_strikes)
1035 cp->h.code |= USER_CODE;
1036 cp_silent = 0;
1037 #ifdef DEBUG_MATCHING
1038 cp_error ("final eval %s", print_harshness (&cp->h));
1039 #endif
1040 }
1041
1042 /* Subroutine of ideal_candidate. See if X or Y is a better match
1043 than the other. */
1044 static int
1045 strictly_better (x, y)
1046 unsigned short x, y;
1047 {
1048 unsigned short xor;
1049
1050 if (x == y)
1051 return 0;
1052
1053 xor = x ^ y;
1054 if (xor >= x || xor >= y)
1055 return 1;
1056 return 0;
1057 }
1058
1059 /* When one of several possible overloaded functions and/or methods
1060 can be called, choose the best candidate for overloading.
1061
1062 BASETYPE is the context from which we start method resolution
1063 or NULL if we are comparing overloaded functions.
1064 CANDIDATES is the array of candidates we have to choose from.
1065 N_CANDIDATES is the length of CANDIDATES.
1066 PARMS is a TREE_LIST of parameters to the function we'll ultimately
1067 choose. It is modified in place when resolving methods. It is not
1068 modified in place when resolving overloaded functions.
1069 LEN is the length of the parameter list. */
1070
1071 static struct candidate *
1072 ideal_candidate (candidates, n_candidates, len)
1073 struct candidate *candidates;
1074 int n_candidates;
1075 int len;
1076 {
1077 struct candidate *cp = candidates+n_candidates;
1078 int i, j = -1, best_code;
1079
1080 /* For each argument, sort the functions from best to worst for the arg.
1081 For each function that's not best for this arg, set its overall
1082 harshness to EVIL so that other args won't like it. The candidate
1083 list for the last argument is the intersection of all the best-liked
1084 functions. */
1085
1086 qsort (candidates, n_candidates, sizeof (struct candidate),
1087 rank_for_overload);
1088 best_code = cp[-1].h.code;
1089
1090 /* If they're at least as good as each other, do an arg-by-arg check. */
1091 if (! strictly_better (cp[-1].h.code, cp[-2].h.code))
1092 {
1093 int better = 0;
1094 int worse = 0;
1095
1096 for (j = 0; j < n_candidates; j++)
1097 if (! strictly_better (candidates[j].h.code, best_code))
1098 break;
1099
1100 qsort (candidates+j, n_candidates-j, sizeof (struct candidate),
1101 rank_for_ideal);
1102 for (i = 0; i < len; i++)
1103 {
1104 if (cp[-1].harshness[i].code < cp[-2].harshness[i].code)
1105 better = 1;
1106 else if (cp[-1].harshness[i].code > cp[-2].harshness[i].code)
1107 worse = 1;
1108 else if (cp[-1].harshness[i].code & STD_CODE)
1109 {
1110 /* If it involves a standard conversion, let the
1111 inheritance lattice be the final arbiter. */
1112 if (cp[-1].harshness[i].distance > cp[-2].harshness[i].distance)
1113 worse = 1;
1114 else if (cp[-1].harshness[i].distance < cp[-2].harshness[i].distance)
1115 better = 1;
1116 }
1117 else if (cp[-1].harshness[i].code & PROMO_CODE)
1118 {
1119 /* For integral promotions, take into account a finer
1120 granularity for determining which types should be favored
1121 over others in such promotions. */
1122 if (cp[-1].harshness[i].int_penalty > cp[-2].harshness[i].int_penalty)
1123 worse = 1;
1124 else if (cp[-1].harshness[i].int_penalty < cp[-2].harshness[i].int_penalty)
1125 better = 1;
1126 }
1127 }
1128
1129 if (! better || worse)
1130 return NULL;
1131 }
1132 return cp-1;
1133 }
1134
1135 /* Assume that if the class referred to is not in the
1136 current class hierarchy, that it may be remote.
1137 PARENT is assumed to be of aggregate type here. */
1138 static int
1139 may_be_remote (parent)
1140 tree parent;
1141 {
1142 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
1143 return 0;
1144
1145 if (current_class_type == NULL_TREE)
1146 return 0;
1147
1148 if (parent == current_class_type)
1149 return 0;
1150
1151 if (UNIQUELY_DERIVED_FROM_P (parent, current_class_type))
1152 return 0;
1153 return 1;
1154 }
1155
1156 tree
1157 build_vfield_ref (datum, type)
1158 tree datum, type;
1159 {
1160 tree rval;
1161 int old_assume_nonnull_objects = flag_assume_nonnull_objects;
1162
1163 if (datum == error_mark_node)
1164 return error_mark_node;
1165
1166 /* Vtable references are always made from non-null objects. */
1167 flag_assume_nonnull_objects = 1;
1168 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
1169 datum = convert_from_reference (datum);
1170
1171 if (! TYPE_USES_COMPLEX_INHERITANCE (type))
1172 rval = build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
1173 datum, CLASSTYPE_VFIELD (type));
1174 else
1175 rval = build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), NULL_TREE, 0);
1176 flag_assume_nonnull_objects = old_assume_nonnull_objects;
1177
1178 return rval;
1179 }
1180
1181 /* Build a call to a member of an object. I.e., one that overloads
1182 operator ()(), or is a pointer-to-function or pointer-to-method. */
1183 static tree
1184 build_field_call (basetype_path, instance_ptr, name, parms)
1185 tree basetype_path, instance_ptr, name, parms;
1186 {
1187 tree field, instance;
1188
1189 if (instance_ptr == current_class_ptr)
1190 {
1191 /* Check to see if we really have a reference to an instance variable
1192 with `operator()()' overloaded. */
1193 field = IDENTIFIER_CLASS_VALUE (name);
1194
1195 if (field == NULL_TREE)
1196 {
1197 cp_error ("`this' has no member named `%D'", name);
1198 return error_mark_node;
1199 }
1200
1201 if (TREE_CODE (field) == FIELD_DECL)
1202 {
1203 /* If it's a field, try overloading operator (),
1204 or calling if the field is a pointer-to-function. */
1205 instance = build_component_ref_1 (current_class_ref, field, 0);
1206 if (instance == error_mark_node)
1207 return error_mark_node;
1208
1209 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1210 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
1211 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms, NULL_TREE);
1212
1213 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
1214 {
1215 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
1216 return build_function_call (instance, parms);
1217 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
1218 return build_function_call (instance, tree_cons (NULL_TREE, current_class_ptr, parms));
1219 }
1220 }
1221 return NULL_TREE;
1222 }
1223
1224 /* Check to see if this is not really a reference to an instance variable
1225 with `operator()()' overloaded. */
1226 field = lookup_field (basetype_path, name, 1, 0);
1227
1228 /* This can happen if the reference was ambiguous or for access
1229 violations. */
1230 if (field == error_mark_node)
1231 return error_mark_node;
1232
1233 if (field)
1234 {
1235 tree basetype;
1236 tree ftype = TREE_TYPE (field);
1237
1238 if (TREE_CODE (ftype) == REFERENCE_TYPE)
1239 ftype = TREE_TYPE (ftype);
1240
1241 if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
1242 {
1243 /* Make the next search for this field very short. */
1244 basetype = DECL_FIELD_CONTEXT (field);
1245 instance_ptr = convert_pointer_to (basetype, instance_ptr);
1246
1247 instance = build_indirect_ref (instance_ptr, NULL_PTR);
1248 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
1249 build_component_ref_1 (instance, field, 0),
1250 parms, NULL_TREE);
1251 }
1252 if (TREE_CODE (ftype) == POINTER_TYPE)
1253 {
1254 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
1255 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
1256 {
1257 /* This is a member which is a pointer to function. */
1258 tree ref
1259 = build_component_ref_1 (build_indirect_ref (instance_ptr,
1260 NULL_PTR),
1261 field, LOOKUP_COMPLAIN);
1262 if (ref == error_mark_node)
1263 return error_mark_node;
1264 return build_function_call (ref, parms);
1265 }
1266 }
1267 else if (TREE_CODE (ftype) == METHOD_TYPE)
1268 {
1269 error ("invalid call via pointer-to-member function");
1270 return error_mark_node;
1271 }
1272 else
1273 return NULL_TREE;
1274 }
1275 return NULL_TREE;
1276 }
1277
1278 tree
1279 find_scoped_type (type, inner_name, inner_types)
1280 tree type, inner_name, inner_types;
1281 {
1282 tree tags = CLASSTYPE_TAGS (type);
1283
1284 while (tags)
1285 {
1286 /* The TREE_PURPOSE of an enum tag (which becomes a member of the
1287 enclosing class) is set to the name for the enum type. So, if
1288 inner_name is `bar', and we strike `baz' for `enum bar { baz }',
1289 then this test will be true. */
1290 if (TREE_PURPOSE (tags) == inner_name)
1291 {
1292 if (inner_types == NULL_TREE)
1293 return TYPE_NAME (TREE_VALUE (tags));
1294 return resolve_scope_to_name (TREE_VALUE (tags), inner_types);
1295 }
1296 tags = TREE_CHAIN (tags);
1297 }
1298
1299 /* Look for a TYPE_DECL. */
1300 for (tags = TYPE_FIELDS (type); tags; tags = TREE_CHAIN (tags))
1301 if (TREE_CODE (tags) == TYPE_DECL && DECL_NAME (tags) == inner_name)
1302 {
1303 /* Code by raeburn. */
1304 if (inner_types == NULL_TREE)
1305 return tags;
1306 return resolve_scope_to_name (TREE_TYPE (tags), inner_types);
1307 }
1308
1309 return NULL_TREE;
1310 }
1311
1312 /* Resolve an expression NAME1::NAME2::...::NAMEn to
1313 the name that names the above nested type. INNER_TYPES
1314 is a chain of nested type names (held together by SCOPE_REFs);
1315 OUTER_TYPE is the type we know to enclose INNER_TYPES.
1316 Returns NULL_TREE if there is an error. */
1317 tree
1318 resolve_scope_to_name (outer_type, inner_stuff)
1319 tree outer_type, inner_stuff;
1320 {
1321 register tree tmp;
1322 tree inner_name, inner_type;
1323
1324 if (outer_type == NULL_TREE && current_class_type != NULL_TREE)
1325 {
1326 /* We first try to look for a nesting in our current class context,
1327 then try any enclosing classes. */
1328 tree type = current_class_type;
1329
1330 while (type && (TREE_CODE (type) == RECORD_TYPE
1331 || TREE_CODE (type) == UNION_TYPE))
1332 {
1333 tree rval = resolve_scope_to_name (type, inner_stuff);
1334
1335 if (rval != NULL_TREE)
1336 return rval;
1337 type = DECL_CONTEXT (TYPE_NAME (type));
1338 }
1339 }
1340
1341 if (TREE_CODE (inner_stuff) == SCOPE_REF)
1342 {
1343 inner_name = TREE_OPERAND (inner_stuff, 0);
1344 inner_type = TREE_OPERAND (inner_stuff, 1);
1345 }
1346 else
1347 {
1348 inner_name = inner_stuff;
1349 inner_type = NULL_TREE;
1350 }
1351
1352 if (outer_type == NULL_TREE)
1353 {
1354 tree x;
1355 /* If we have something that's already a type by itself,
1356 use that. */
1357 if (IDENTIFIER_HAS_TYPE_VALUE (inner_name))
1358 {
1359 if (inner_type)
1360 return resolve_scope_to_name (IDENTIFIER_TYPE_VALUE (inner_name),
1361 inner_type);
1362 return inner_name;
1363 }
1364
1365 x = lookup_name (inner_name, 0);
1366
1367 if (x && TREE_CODE (x) == NAMESPACE_DECL)
1368 {
1369 x = lookup_namespace_name (x, inner_type);
1370 return x;
1371 }
1372 return NULL_TREE;
1373 }
1374
1375 if (! IS_AGGR_TYPE (outer_type))
1376 return NULL_TREE;
1377
1378 /* Look for member classes or enums. */
1379 tmp = find_scoped_type (outer_type, inner_name, inner_type);
1380
1381 /* If it's not a type in this class, then go down into the
1382 base classes and search there. */
1383 if (! tmp && TYPE_BINFO (outer_type))
1384 {
1385 tree binfos = TYPE_BINFO_BASETYPES (outer_type);
1386 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1387
1388 for (i = 0; i < n_baselinks; i++)
1389 {
1390 tree base_binfo = TREE_VEC_ELT (binfos, i);
1391 tmp = resolve_scope_to_name (BINFO_TYPE (base_binfo), inner_stuff);
1392 if (tmp)
1393 return tmp;
1394 }
1395 tmp = NULL_TREE;
1396 }
1397
1398 return tmp;
1399 }
1400
1401 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
1402 This is how virtual function calls are avoided. */
1403 tree
1404 build_scoped_method_call (exp, basetype, name, parms)
1405 tree exp, basetype, name, parms;
1406 {
1407 /* Because this syntactic form does not allow
1408 a pointer to a base class to be `stolen',
1409 we need not protect the derived->base conversion
1410 that happens here.
1411
1412 @@ But we do have to check access privileges later. */
1413 tree binfo, decl;
1414 tree type = TREE_TYPE (exp);
1415
1416 if (type == error_mark_node
1417 || basetype == error_mark_node)
1418 return error_mark_node;
1419
1420 if (current_template_parms)
1421 {
1422 if (TREE_CODE (name) == BIT_NOT_EXPR)
1423 {
1424 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1425 name = build_min_nt (BIT_NOT_EXPR, type);
1426 }
1427 name = build_min_nt (SCOPE_REF, basetype, name);
1428 return build_min_nt (METHOD_CALL_EXPR, name, exp, parms, NULL_TREE);
1429 }
1430
1431 if (TREE_CODE (type) == REFERENCE_TYPE)
1432 type = TREE_TYPE (type);
1433
1434 /* Destructors can be "called" for simple types; see 5.2.4 and 12.4 Note
1435 that explicit ~int is caught in the parser; this deals with typedefs
1436 and template parms. */
1437 if (TREE_CODE (name) == BIT_NOT_EXPR && ! IS_AGGR_TYPE (basetype))
1438 {
1439 if (type != basetype)
1440 cp_error ("type of `%E' does not match destructor type `%T' (type was `%T')",
1441 exp, basetype, type);
1442 name = TREE_OPERAND (name, 0);
1443 if (basetype != name && basetype != get_type_value (name))
1444 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1445 basetype, name);
1446 return convert (void_type_node, exp);
1447 }
1448
1449 if (! is_aggr_type (basetype, 1))
1450 return error_mark_node;
1451
1452 if (! IS_AGGR_TYPE (type))
1453 {
1454 cp_error ("base object `%E' of scoped method call is of non-aggregate type `%T'",
1455 exp, type);
1456 return error_mark_node;
1457 }
1458
1459 if ((binfo = binfo_or_else (basetype, type)))
1460 {
1461 if (binfo == error_mark_node)
1462 return error_mark_node;
1463 if (TREE_CODE (exp) == INDIRECT_REF)
1464 decl = build_indirect_ref (convert_pointer_to (binfo,
1465 build_unary_op (ADDR_EXPR, exp, 0)), NULL_PTR);
1466 else
1467 decl = build_scoped_ref (exp, basetype);
1468
1469 /* Call to a destructor. */
1470 if (TREE_CODE (name) == BIT_NOT_EXPR)
1471 {
1472 /* Explicit call to destructor. */
1473 name = TREE_OPERAND (name, 0);
1474 if (! (name == TYPE_MAIN_VARIANT (TREE_TYPE (decl))
1475 || name == constructor_name (TREE_TYPE (decl))
1476 || TREE_TYPE (decl) == get_type_value (name)))
1477 {
1478 cp_error
1479 ("qualified type `%T' does not match destructor name `~%T'",
1480 TREE_TYPE (decl), name);
1481 return error_mark_node;
1482 }
1483 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
1484 return convert (void_type_node, exp);
1485
1486 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
1487 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR,
1488 0);
1489 }
1490
1491 /* Call to a method. */
1492 return build_method_call (decl, name, parms, binfo,
1493 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1494 }
1495 return error_mark_node;
1496 }
1497
1498 static void
1499 print_candidates (candidates)
1500 tree candidates;
1501 {
1502 cp_error_at ("candidates are: %D", TREE_VALUE (candidates));
1503 candidates = TREE_CHAIN (candidates);
1504
1505 while (candidates)
1506 {
1507 cp_error_at (" %D", TREE_VALUE (candidates));
1508 candidates = TREE_CHAIN (candidates);
1509 }
1510 }
1511
1512 static void
1513 print_n_candidates (candidates, n)
1514 struct candidate *candidates;
1515 int n;
1516 {
1517 int i;
1518
1519 cp_error_at ("candidates are: %D", candidates[0].function);
1520 for (i = 1; i < n; i++)
1521 cp_error_at (" %D", candidates[i].function);
1522 }
1523
1524 /* We want the address of a function or method. We avoid creating a
1525 pointer-to-member function. */
1526 tree
1527 build_addr_func (function)
1528 tree function;
1529 {
1530 tree type = TREE_TYPE (function);
1531
1532 /* We have to do these by hand to avoid real pointer to member
1533 functions. */
1534 if (TREE_CODE (type) == METHOD_TYPE)
1535 {
1536 tree addr;
1537
1538 type = build_pointer_type (type);
1539
1540 if (mark_addressable (function) == 0)
1541 return error_mark_node;
1542
1543 addr = build1 (ADDR_EXPR, type, function);
1544
1545 /* Address of a static or external variable or function counts
1546 as a constant */
1547 if (staticp (function))
1548 TREE_CONSTANT (addr) = 1;
1549
1550 function = addr;
1551 }
1552 else
1553 function = default_conversion (function);
1554
1555 return function;
1556 }
1557
1558 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
1559 POINTER_TYPE to those. Note, pointer to member function types
1560 (TYPE_PTRMEMFUNC_P) must be handled by our callers. */
1561 tree
1562 build_call (function, result_type, parms)
1563 tree function, result_type, parms;
1564 {
1565 int is_constructor = 0;
1566
1567 function = build_addr_func (function);
1568
1569 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
1570 {
1571 sorry ("unable to call pointer to member function here");
1572 return error_mark_node;
1573 }
1574
1575 if (TREE_CODE (function) == ADDR_EXPR
1576 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1577 && DECL_CONSTRUCTOR_P (TREE_OPERAND (function, 0)))
1578 is_constructor = 1;
1579
1580 function = build_nt (CALL_EXPR, function, parms, NULL_TREE);
1581 TREE_HAS_CONSTRUCTOR (function) = is_constructor;
1582 TREE_TYPE (function) = result_type;
1583 TREE_SIDE_EFFECTS (function) = 1;
1584
1585 return function;
1586 }
1587
1588 static tree
1589 default_parm_conversions (parms, last)
1590 tree parms, *last;
1591 {
1592 tree parm, parmtypes = NULL_TREE;
1593
1594 *last = NULL_TREE;
1595
1596 for (parm = parms; parm; parm = TREE_CHAIN (parm))
1597 {
1598 tree t = TREE_TYPE (TREE_VALUE (parm));
1599
1600 if (TREE_CODE (t) == OFFSET_TYPE
1601 || TREE_CODE (t) == METHOD_TYPE
1602 || TREE_CODE (t) == FUNCTION_TYPE)
1603 {
1604 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
1605 t = TREE_TYPE (TREE_VALUE (parm));
1606 }
1607
1608 if (t == error_mark_node)
1609 return error_mark_node;
1610
1611 *last = build_tree_list (NULL_TREE, t);
1612 parmtypes = chainon (parmtypes, *last);
1613 }
1614
1615 return parmtypes;
1616 }
1617
1618
1619 /* Build something of the form ptr->method (args)
1620 or object.method (args). This can also build
1621 calls to constructors, and find friends.
1622
1623 Member functions always take their class variable
1624 as a pointer.
1625
1626 INSTANCE is a class instance.
1627
1628 NAME is the name of the method desired, usually an IDENTIFIER_NODE.
1629
1630 PARMS help to figure out what that NAME really refers to.
1631
1632 BASETYPE_PATH, if non-NULL, contains a chain from the type of INSTANCE
1633 down to the real instance type to use for access checking. We need this
1634 information to get protected accesses correct. This parameter is used
1635 by build_member_call.
1636
1637 FLAGS is the logical disjunction of zero or more LOOKUP_
1638 flags. See cp-tree.h for more info.
1639
1640 If this is all OK, calls build_function_call with the resolved
1641 member function.
1642
1643 This function must also handle being called to perform
1644 initialization, promotion/coercion of arguments, and
1645 instantiation of default parameters.
1646
1647 Note that NAME may refer to an instance variable name. If
1648 `operator()()' is defined for the type of that field, then we return
1649 that result. */
1650 tree
1651 build_method_call (instance, name, parms, basetype_path, flags)
1652 tree instance, name, parms, basetype_path;
1653 int flags;
1654 {
1655 register tree function, fntype, value_type;
1656 register tree basetype, save_basetype;
1657 register tree baselink, result, parmtypes, parm;
1658 tree last;
1659 int pass;
1660 tree access = access_public_node;
1661 tree orig_basetype = basetype_path ? BINFO_TYPE (basetype_path) : NULL_TREE;
1662
1663 /* Range of cases for vtable optimization. */
1664 enum vtable_needs { not_needed, maybe_needed, unneeded, needed };
1665 enum vtable_needs need_vtbl = not_needed;
1666
1667 char *name_kind;
1668 tree save_name = name;
1669 int ever_seen = 0;
1670 tree instance_ptr = NULL_TREE;
1671 int all_virtual = flag_all_virtual;
1672 int static_call_context = 0;
1673 tree found_fns = NULL_TREE;
1674
1675 /* Keep track of `const' and `volatile' objects. */
1676 int constp, volatilep;
1677
1678 #ifdef GATHER_STATISTICS
1679 n_build_method_call++;
1680 #endif
1681
1682 if (instance == error_mark_node
1683 || name == error_mark_node
1684 || parms == error_mark_node
1685 || (instance != NULL_TREE && TREE_TYPE (instance) == error_mark_node))
1686 return error_mark_node;
1687
1688 if (current_template_parms)
1689 {
1690 if (TREE_CODE (name) == BIT_NOT_EXPR)
1691 {
1692 tree type = get_aggr_from_typedef (TREE_OPERAND (name, 0), 1);
1693 name = build_min_nt (BIT_NOT_EXPR, type);
1694 }
1695
1696 return build_min_nt (METHOD_CALL_EXPR, name, instance, parms, NULL_TREE);
1697 }
1698
1699 /* This is the logic that magically deletes the second argument to
1700 operator delete, if it is not needed. */
1701 if (name == ansi_opname[(int) DELETE_EXPR] && list_length (parms)==2)
1702 {
1703 tree save_last = TREE_CHAIN (parms);
1704 tree result;
1705 /* get rid of unneeded argument */
1706 TREE_CHAIN (parms) = NULL_TREE;
1707 result = build_method_call (instance, name, parms, basetype_path,
1708 (LOOKUP_SPECULATIVELY|flags)
1709 &~LOOKUP_COMPLAIN);
1710 /* If it finds a match, return it. */
1711 if (result)
1712 return build_method_call (instance, name, parms, basetype_path, flags);
1713 /* If it doesn't work, two argument delete must work */
1714 TREE_CHAIN (parms) = save_last;
1715 }
1716 /* We already know whether it's needed or not for vec delete. */
1717 else if (name == ansi_opname[(int) VEC_DELETE_EXPR]
1718 && TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
1719 && ! TYPE_VEC_DELETE_TAKES_SIZE (TREE_TYPE (instance)))
1720 TREE_CHAIN (parms) = NULL_TREE;
1721
1722 if (TREE_CODE (name) == BIT_NOT_EXPR)
1723 {
1724 flags |= LOOKUP_DESTRUCTOR;
1725 name = TREE_OPERAND (name, 0);
1726 if (parms)
1727 error ("destructors take no parameters");
1728 basetype = TREE_TYPE (instance);
1729 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1730 basetype = TREE_TYPE (basetype);
1731 if (! (name == basetype
1732 || (IS_AGGR_TYPE (basetype)
1733 && name == constructor_name (basetype))
1734 || basetype == get_type_value (name)))
1735 {
1736 cp_error ("destructor name `~%D' does not match type `%T' of expression",
1737 name, basetype);
1738 return convert (void_type_node, instance);
1739 }
1740
1741 if (! TYPE_HAS_DESTRUCTOR (basetype))
1742 return convert (void_type_node, instance);
1743 instance = default_conversion (instance);
1744 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1745 return build_delete (build_pointer_type (basetype),
1746 instance_ptr, integer_two_node,
1747 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
1748 }
1749
1750 {
1751 char *xref_name;
1752
1753 /* Initialize name for error reporting. */
1754 if (IDENTIFIER_OPNAME_P (name) && ! IDENTIFIER_TYPENAME_P (name))
1755 {
1756 char *p = operator_name_string (name);
1757 xref_name = (char *)alloca (strlen (p) + 10);
1758 sprintf (xref_name, "operator %s", p);
1759 }
1760 else if (TREE_CODE (name) == SCOPE_REF)
1761 xref_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
1762 else
1763 xref_name = IDENTIFIER_POINTER (name);
1764
1765 GNU_xref_call (current_function_decl, xref_name);
1766 }
1767
1768 if (instance == NULL_TREE)
1769 {
1770 basetype = NULL_TREE;
1771 /* Check cases where this is really a call to raise
1772 an exception. */
1773 if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
1774 {
1775 basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
1776 if (basetype)
1777 basetype = TREE_VALUE (basetype);
1778 }
1779 else if (TREE_CODE (name) == SCOPE_REF
1780 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
1781 {
1782 if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
1783 return error_mark_node;
1784 basetype = purpose_member (TREE_OPERAND (name, 1),
1785 CLASSTYPE_TAGS (IDENTIFIER_TYPE_VALUE (TREE_OPERAND (name, 0))));
1786 if (basetype)
1787 basetype = TREE_VALUE (basetype);
1788 }
1789
1790 if (basetype != NULL_TREE)
1791 ;
1792 /* call to a constructor... */
1793 else if (basetype_path)
1794 {
1795 basetype = BINFO_TYPE (basetype_path);
1796 if (name == TYPE_IDENTIFIER (basetype))
1797 name = ctor_identifier;
1798 }
1799 else if (IDENTIFIER_HAS_TYPE_VALUE (name))
1800 {
1801 basetype = IDENTIFIER_TYPE_VALUE (name);
1802 name = ctor_identifier;
1803 }
1804 else
1805 {
1806 tree typedef_name = lookup_name (name, 1);
1807 if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
1808 {
1809 /* Canonicalize the typedef name. */
1810 basetype = TREE_TYPE (typedef_name);
1811 name = ctor_identifier;
1812 }
1813 else
1814 {
1815 cp_error ("no constructor named `%T' in scope",
1816 name);
1817 return error_mark_node;
1818 }
1819 }
1820
1821 if (! IS_AGGR_TYPE (basetype))
1822 {
1823 non_aggr_error:
1824 if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
1825 cp_error ("request for member `%D' in `%E', which is of non-aggregate type `%T'",
1826 name, instance, basetype);
1827
1828 return error_mark_node;
1829 }
1830 }
1831 else if (instance == current_class_ref || instance == current_class_ptr)
1832 {
1833 /* When doing initialization, we side-effect the TREE_TYPE of
1834 current_class_ref, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
1835 basetype = TREE_TYPE (current_class_ref);
1836
1837 /* Anything manifestly `this' in constructors and destructors
1838 has a known type, so virtual function tables are not needed. */
1839 if (TYPE_VIRTUAL_P (basetype)
1840 && !(flags & LOOKUP_NONVIRTUAL))
1841 need_vtbl = (dtor_label || ctor_label)
1842 ? unneeded : maybe_needed;
1843
1844 /* If `this' is a signature pointer and `name' is not a constructor,
1845 we are calling a signature member function. In that case, set the
1846 `basetype' to the signature type and dereference the `optr' field. */
1847 if (IS_SIGNATURE_POINTER (basetype)
1848 && TYPE_IDENTIFIER (basetype) != name)
1849 {
1850 basetype = SIGNATURE_TYPE (basetype);
1851 instance_ptr = build_optr_ref (instance);
1852 instance_ptr = convert (build_pointer_type (basetype), instance_ptr);
1853 basetype_path = TYPE_BINFO (basetype);
1854 }
1855 else
1856 {
1857 instance = current_class_ref;
1858 instance_ptr = current_class_ptr;
1859 basetype_path = TYPE_BINFO (current_class_type);
1860 }
1861 result = build_field_call (basetype_path, instance_ptr, name, parms);
1862
1863 if (result)
1864 return result;
1865 }
1866 else if (TREE_CODE (instance) == RESULT_DECL)
1867 {
1868 basetype = TREE_TYPE (instance);
1869 /* Should we ever have to make a virtual function reference
1870 from a RESULT_DECL, know that it must be of fixed type
1871 within the scope of this function. */
1872 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1873 need_vtbl = maybe_needed;
1874 instance_ptr = build1 (ADDR_EXPR, build_pointer_type (basetype), instance);
1875 }
1876 else
1877 {
1878 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
1879 tree inst_ptr_basetype;
1880
1881 static_call_context =
1882 (TREE_CODE (instance) == INDIRECT_REF
1883 && TREE_CODE (TREE_OPERAND (instance, 0)) == NOP_EXPR
1884 && TREE_OPERAND (TREE_OPERAND (instance, 0), 0) == error_mark_node);
1885
1886 if (TREE_CODE (instance) == OFFSET_REF)
1887 instance = resolve_offset_ref (instance);
1888
1889 /* the base type of an instance variable is pointer to class */
1890 basetype = TREE_TYPE (instance);
1891
1892 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1893 {
1894 basetype = TREE_TYPE (basetype);
1895 if (! IS_AGGR_TYPE (basetype))
1896 goto non_aggr_error;
1897 /* Call to convert not needed because we are remaining
1898 within the same type. */
1899 instance_ptr = build1 (NOP_EXPR, build_pointer_type (basetype),
1900 instance);
1901 inst_ptr_basetype = TYPE_MAIN_VARIANT (basetype);
1902 }
1903 else
1904 {
1905 if (! IS_AGGR_TYPE (basetype)
1906 && ! (TYPE_LANG_SPECIFIC (basetype)
1907 && (IS_SIGNATURE_POINTER (basetype)
1908 || IS_SIGNATURE_REFERENCE (basetype))))
1909 goto non_aggr_error;
1910
1911 /* If `instance' is a signature pointer/reference and `name' is
1912 not a constructor, we are calling a signature member function.
1913 In that case set the `basetype' to the signature type. */
1914 if ((IS_SIGNATURE_POINTER (basetype)
1915 || IS_SIGNATURE_REFERENCE (basetype))
1916 && TYPE_IDENTIFIER (basetype) != name)
1917 basetype = SIGNATURE_TYPE (basetype);
1918
1919 if ((IS_SIGNATURE (basetype)
1920 && (instance_ptr = instance))
1921 || (lvalue_p (instance)
1922 && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
1923 || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
1924 {
1925 if (instance_ptr == error_mark_node)
1926 return error_mark_node;
1927 }
1928 else if (TREE_CODE (instance) == NOP_EXPR
1929 || TREE_CODE (instance) == CONSTRUCTOR)
1930 {
1931 /* A cast is not an lvalue. Initialize a fresh temp
1932 with the value we are casting from, and proceed with
1933 that temporary. We can't cast to a reference type,
1934 so that simplifies the initialization to something
1935 we can manage. */
1936 tree temp = get_temp_name (TREE_TYPE (instance), 0);
1937 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
1938 expand_aggr_init (temp, instance, 0, flags);
1939 else
1940 {
1941 store_init_value (temp, instance);
1942 expand_decl_init (temp);
1943 }
1944 instance = temp;
1945 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1946 }
1947 else
1948 {
1949 if (TREE_CODE (instance) != CALL_EXPR)
1950 my_friendly_abort (125);
1951 if (TYPE_NEEDS_CONSTRUCTING (basetype))
1952 instance = build_cplus_new (basetype, instance);
1953 else
1954 {
1955 instance = get_temp_name (basetype, 0);
1956 TREE_ADDRESSABLE (instance) = 1;
1957 }
1958 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
1959 }
1960 /* @@ Should we call comp_target_types here? */
1961 if (IS_SIGNATURE (basetype))
1962 inst_ptr_basetype = basetype;
1963 else
1964 inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
1965 if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
1966 basetype = inst_ptr_basetype;
1967 else
1968 {
1969 instance_ptr = convert (build_pointer_type (basetype), instance_ptr);
1970 if (instance_ptr == error_mark_node)
1971 return error_mark_node;
1972 }
1973 }
1974
1975 /* After converting `instance_ptr' above, `inst_ptr_basetype' was
1976 not updated, so we use `basetype' instead. */
1977 if (basetype_path == NULL_TREE
1978 && IS_SIGNATURE (basetype))
1979 basetype_path = TYPE_BINFO (basetype);
1980 else if (basetype_path == NULL_TREE ||
1981 BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (inst_ptr_basetype))
1982 basetype_path = TYPE_BINFO (inst_ptr_basetype);
1983
1984 result = build_field_call (basetype_path, instance_ptr, name, parms);
1985 if (result)
1986 return result;
1987
1988 if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
1989 {
1990 if (TREE_SIDE_EFFECTS (instance_ptr))
1991 {
1992 /* This action is needed because the instance is needed
1993 for providing the base of the virtual function table.
1994 Without using a SAVE_EXPR, the function we are building
1995 may be called twice, or side effects on the instance
1996 variable (such as a post-increment), may happen twice. */
1997 instance_ptr = save_expr (instance_ptr);
1998 instance = build_indirect_ref (instance_ptr, NULL_PTR);
1999 }
2000 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
2001 {
2002 /* This happens when called for operator new (). */
2003 instance = build_indirect_ref (instance, NULL_PTR);
2004 }
2005
2006 need_vtbl = maybe_needed;
2007 }
2008 }
2009
2010 if (save_name == ctor_identifier)
2011 save_name = TYPE_IDENTIFIER (basetype);
2012
2013 if (TYPE_SIZE (complete_type (basetype)) == 0)
2014 {
2015 /* This is worth complaining about, I think. */
2016 cp_error ("cannot lookup method in incomplete type `%T'", basetype);
2017 return error_mark_node;
2018 }
2019
2020 save_basetype = TYPE_MAIN_VARIANT (basetype);
2021
2022 parmtypes = default_parm_conversions (parms, &last);
2023 if (parmtypes == error_mark_node)
2024 {
2025 return error_mark_node;
2026 }
2027
2028 if (instance && IS_SIGNATURE (basetype))
2029 {
2030 /* @@ Should this be the constp/volatilep flags for the optr field
2031 of the signature pointer? */
2032 constp = TYPE_READONLY (basetype);
2033 volatilep = TYPE_VOLATILE (basetype);
2034 parms = tree_cons (NULL_TREE, instance_ptr, parms);
2035 }
2036 else if (instance)
2037 {
2038 /* TREE_READONLY (instance) fails for references. */
2039 constp = TYPE_READONLY (TREE_TYPE (TREE_TYPE (instance_ptr)));
2040 volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (instance_ptr)));
2041 parms = tree_cons (NULL_TREE, instance_ptr, parms);
2042 }
2043 else
2044 {
2045 /* Raw constructors are always in charge. */
2046 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2047 && ! (flags & LOOKUP_HAS_IN_CHARGE))
2048 {
2049 flags |= LOOKUP_HAS_IN_CHARGE;
2050 parms = tree_cons (NULL_TREE, integer_one_node, parms);
2051 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
2052 }
2053
2054 constp = 0;
2055 volatilep = 0;
2056 instance_ptr = build_int_2 (0, 0);
2057 TREE_TYPE (instance_ptr) = build_pointer_type (basetype);
2058 parms = tree_cons (NULL_TREE, instance_ptr, parms);
2059 }
2060
2061 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (instance_ptr), parmtypes);
2062
2063 if (last == NULL_TREE)
2064 last = parmtypes;
2065
2066 /* Look up function name in the structure type definition. */
2067
2068 /* FIXME Axe most of this now? */
2069 if ((IDENTIFIER_HAS_TYPE_VALUE (name)
2070 && ! IDENTIFIER_OPNAME_P (name)
2071 && IS_AGGR_TYPE (IDENTIFIER_TYPE_VALUE (name)))
2072 || name == constructor_name (basetype)
2073 || name == ctor_identifier)
2074 {
2075 tree tmp = NULL_TREE;
2076 if (IDENTIFIER_TYPE_VALUE (name) == basetype
2077 || name == constructor_name (basetype)
2078 || name == ctor_identifier)
2079 tmp = TYPE_BINFO (basetype);
2080 else
2081 tmp = get_binfo (IDENTIFIER_TYPE_VALUE (name), basetype, 0);
2082
2083 if (tmp != NULL_TREE)
2084 {
2085 name_kind = "constructor";
2086
2087 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
2088 && ! (flags & LOOKUP_HAS_IN_CHARGE))
2089 {
2090 /* Constructors called for initialization
2091 only are never in charge. */
2092 tree tmplist;
2093
2094 flags |= LOOKUP_HAS_IN_CHARGE;
2095 tmplist = tree_cons (NULL_TREE, integer_zero_node,
2096 TREE_CHAIN (parms));
2097 TREE_CHAIN (parms) = tmplist;
2098 tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
2099 TREE_CHAIN (parmtypes) = tmplist;
2100 }
2101 basetype = BINFO_TYPE (tmp);
2102 }
2103 else
2104 name_kind = "method";
2105 }
2106 else
2107 name_kind = "method";
2108
2109 if (basetype_path == NULL_TREE
2110 || BINFO_TYPE (basetype_path) != TYPE_MAIN_VARIANT (basetype))
2111 basetype_path = TYPE_BINFO (basetype);
2112 result = lookup_fnfields (basetype_path, name,
2113 (flags & LOOKUP_COMPLAIN));
2114 if (result == error_mark_node)
2115 return error_mark_node;
2116
2117 for (pass = 0; pass < 2; pass++)
2118 {
2119 struct candidate *candidates;
2120 struct candidate *cp;
2121 int len;
2122 unsigned best = 1;
2123
2124 baselink = result;
2125
2126 if (pass > 0)
2127 {
2128 candidates
2129 = (struct candidate *) alloca ((ever_seen+1)
2130 * sizeof (struct candidate));
2131 bzero ((char *) candidates, (ever_seen + 1) * sizeof (struct candidate));
2132 cp = candidates;
2133 len = list_length (parms);
2134 ever_seen = 0;
2135
2136 /* First see if a global function has a shot at it. */
2137 if (flags & LOOKUP_GLOBAL)
2138 {
2139 tree friend_parms;
2140 tree parm = instance_ptr;
2141
2142 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
2143 parm = convert_from_reference (parm);
2144 else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
2145 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
2146 else
2147 my_friendly_abort (167);
2148
2149 friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
2150
2151 cp->h_len = len;
2152 cp->harshness = (struct harshness_code *)
2153 alloca ((len + 1) * sizeof (struct harshness_code));
2154
2155 result = build_overload_call_real (name, friend_parms, 0, cp, 1);
2156
2157 /* If it turns out to be the one we were actually looking for
2158 (it was probably a friend function), the return the
2159 good result. */
2160 if (TREE_CODE (result) == CALL_EXPR)
2161 return result;
2162
2163 while ((cp->h.code & EVIL_CODE) == 0)
2164 {
2165 /* non-standard uses: set the field to 0 to indicate
2166 we are using a non-member function. */
2167 cp->u.field = 0;
2168 if (cp->harshness[len].distance == 0
2169 && cp->h.code < best)
2170 best = cp->h.code;
2171 cp += 1;
2172 }
2173 }
2174 }
2175
2176 if (baselink)
2177 {
2178 /* We have a hit (of sorts). If the parameter list is
2179 "error_mark_node", or some variant thereof, it won't
2180 match any methods. Since we have verified that the is
2181 some method vaguely matching this one (in name at least),
2182 silently return.
2183
2184 Don't stop for friends, however. */
2185 basetype_path = TREE_PURPOSE (baselink);
2186
2187 function = TREE_VALUE (baselink);
2188 if (TREE_CODE (basetype_path) == TREE_LIST)
2189 basetype_path = TREE_VALUE (basetype_path);
2190 basetype = BINFO_TYPE (basetype_path);
2191
2192 for (; function; function = DECL_CHAIN (function))
2193 {
2194 #ifdef GATHER_STATISTICS
2195 n_inner_fields_searched++;
2196 #endif
2197 ever_seen++;
2198 if (pass > 0)
2199 found_fns = tree_cons (NULL_TREE, function, found_fns);
2200
2201 /* Not looking for friends here. */
2202 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
2203 && ! DECL_STATIC_FUNCTION_P (function))
2204 continue;
2205
2206 if (pass > 0)
2207 {
2208 tree these_parms = parms;
2209
2210 #ifdef GATHER_STATISTICS
2211 n_inner_fields_searched++;
2212 #endif
2213 cp->h_len = len;
2214 cp->harshness = (struct harshness_code *)
2215 alloca ((len + 1) * sizeof (struct harshness_code));
2216
2217 if (DECL_STATIC_FUNCTION_P (function))
2218 these_parms = TREE_CHAIN (these_parms);
2219 compute_conversion_costs (function, these_parms, cp, len);
2220
2221 if ((cp->h.code & EVIL_CODE) == 0)
2222 {
2223 cp->u.field = function;
2224 cp->function = function;
2225 cp->basetypes = basetype_path;
2226
2227 /* Don't allow non-converting constructors to convert. */
2228 if (flags & LOOKUP_ONLYCONVERTING
2229 && DECL_LANG_SPECIFIC (function)
2230 && DECL_NONCONVERTING_P (function))
2231 continue;
2232
2233 /* No "two-level" conversions. */
2234 if (flags & LOOKUP_NO_CONVERSION
2235 && (cp->h.code & USER_CODE))
2236 continue;
2237
2238 cp++;
2239 }
2240 }
2241 }
2242 }
2243
2244 if (pass == 0)
2245 {
2246 tree igv = lookup_name_nonclass (name);
2247
2248 /* No exact match could be found. Now try to find match
2249 using default conversions. */
2250 if ((flags & LOOKUP_GLOBAL) && igv)
2251 {
2252 if (TREE_CODE (igv) == FUNCTION_DECL)
2253 ever_seen += 1;
2254 else if (TREE_CODE (igv) == TREE_LIST)
2255 ever_seen += count_functions (igv);
2256 }
2257
2258 if (ever_seen == 0)
2259 {
2260 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2261 == LOOKUP_SPECULATIVELY)
2262 return NULL_TREE;
2263
2264 TREE_CHAIN (last) = void_list_node;
2265 if (flags & LOOKUP_GLOBAL)
2266 cp_error ("no global or member function `%D(%A)' defined",
2267 save_name, parmtypes);
2268 else
2269 cp_error ("no member function `%T::%D(%A)' defined",
2270 save_basetype, save_name, TREE_CHAIN (parmtypes));
2271 return error_mark_node;
2272 }
2273 continue;
2274 }
2275
2276 if (cp - candidates != 0)
2277 {
2278 /* Rank from worst to best. Then cp will point to best one.
2279 Private fields have their bits flipped. For unsigned
2280 numbers, this should make them look very large.
2281 If the best alternate has a (signed) negative value,
2282 then all we ever saw were private members. */
2283 if (cp - candidates > 1)
2284 {
2285 int n_candidates = cp - candidates;
2286 extern int warn_synth;
2287 TREE_VALUE (parms) = instance_ptr;
2288 cp = ideal_candidate (candidates, n_candidates, len);
2289 if (cp == (struct candidate *)0)
2290 {
2291 if (flags & LOOKUP_COMPLAIN)
2292 {
2293 TREE_CHAIN (last) = void_list_node;
2294 cp_error ("call of overloaded %s `%D(%A)' is ambiguous",
2295 name_kind, save_name, TREE_CHAIN (parmtypes));
2296 print_n_candidates (candidates, n_candidates);
2297 }
2298 return error_mark_node;
2299 }
2300 if (cp->h.code & EVIL_CODE)
2301 return error_mark_node;
2302 if (warn_synth
2303 && DECL_NAME (cp->function) == ansi_opname[MODIFY_EXPR]
2304 && DECL_ARTIFICIAL (cp->function)
2305 && n_candidates == 2)
2306 {
2307 cp_warning ("using synthesized `%#D' for copy assignment",
2308 cp->function);
2309 cp_warning_at (" where cfront would use `%#D'",
2310 candidates->function);
2311 }
2312 }
2313 else if (cp[-1].h.code & EVIL_CODE)
2314 {
2315 if (flags & LOOKUP_COMPLAIN)
2316 cp_error ("ambiguous type conversion requested for %s `%D'",
2317 name_kind, save_name);
2318 return error_mark_node;
2319 }
2320 else
2321 cp--;
2322
2323 /* The global function was the best, so use it. */
2324 if (cp->u.field == 0)
2325 {
2326 /* We must convert the instance pointer into a reference type.
2327 Global overloaded functions can only either take
2328 aggregate objects (which come for free from references)
2329 or reference data types anyway. */
2330 TREE_VALUE (parms) = copy_node (instance_ptr);
2331 TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
2332 return build_function_call (cp->function, parms);
2333 }
2334
2335 function = cp->function;
2336 basetype_path = cp->basetypes;
2337 if (! DECL_STATIC_FUNCTION_P (function))
2338 TREE_VALUE (parms) = cp->arg;
2339 goto found_and_maybe_warn;
2340 }
2341
2342 if (flags & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
2343 {
2344 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2345 == LOOKUP_SPECULATIVELY)
2346 return NULL_TREE;
2347
2348 if (DECL_STATIC_FUNCTION_P (cp->function))
2349 parms = TREE_CHAIN (parms);
2350 if (ever_seen)
2351 {
2352 if (flags & LOOKUP_SPECULATIVELY)
2353 return NULL_TREE;
2354 if (static_call_context
2355 && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
2356 cp_error ("object missing in call to `%D'", cp->function);
2357 else if (ever_seen > 1)
2358 {
2359 TREE_CHAIN (last) = void_list_node;
2360 cp_error ("no matching function for call to `%T::%D (%A)%V'",
2361 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (instance_ptr))),
2362 save_name, TREE_CHAIN (parmtypes),
2363 TREE_TYPE (TREE_TYPE (instance_ptr)));
2364 TREE_CHAIN (last) = NULL_TREE;
2365 print_candidates (found_fns);
2366 }
2367 else
2368 report_type_mismatch (cp, parms, name_kind);
2369 return error_mark_node;
2370 }
2371
2372 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
2373 == LOOKUP_COMPLAIN)
2374 {
2375 cp_error ("%T has no method named %D", save_basetype, save_name);
2376 return error_mark_node;
2377 }
2378 return NULL_TREE;
2379 }
2380 continue;
2381
2382 found_and_maybe_warn:
2383 if ((cp->harshness[0].code & CONST_CODE)
2384 /* 12.1p2: Constructors can be called for const objects. */
2385 && ! DECL_CONSTRUCTOR_P (cp->function))
2386 {
2387 if (flags & LOOKUP_COMPLAIN)
2388 {
2389 cp_error_at ("non-const member function `%D'", cp->function);
2390 error ("called for const object at this point in file");
2391 }
2392 /* Not good enough for a match. */
2393 else
2394 return error_mark_node;
2395 }
2396 goto found;
2397 }
2398 /* Silently return error_mark_node. */
2399 return error_mark_node;
2400
2401 found:
2402 if (flags & LOOKUP_PROTECT)
2403 access = compute_access (basetype_path, function);
2404
2405 if (access == access_private_node)
2406 {
2407 if (flags & LOOKUP_COMPLAIN)
2408 {
2409 cp_error_at ("%s `%+#D' is %s", name_kind, function,
2410 TREE_PRIVATE (function) ? "private"
2411 : "from private base class");
2412 error ("within this context");
2413 }
2414 return error_mark_node;
2415 }
2416 else if (access == access_protected_node)
2417 {
2418 if (flags & LOOKUP_COMPLAIN)
2419 {
2420 cp_error_at ("%s `%+#D' %s", name_kind, function,
2421 TREE_PROTECTED (function) ? "is protected"
2422 : "has protected accessibility");
2423 error ("within this context");
2424 }
2425 return error_mark_node;
2426 }
2427
2428 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
2429 type (if it exists) is a pointer to. */
2430
2431 if (DECL_ABSTRACT_VIRTUAL_P (function)
2432 && instance == current_class_ref
2433 && DECL_CONSTRUCTOR_P (current_function_decl)
2434 && ! (flags & LOOKUP_NONVIRTUAL)
2435 && value_member (function, get_abstract_virtuals (basetype)))
2436 cp_error ("abstract virtual `%#D' called from constructor", function);
2437
2438 if (IS_SIGNATURE (basetype))
2439 {
2440 if (static_call_context)
2441 {
2442 cp_error ("cannot call signature member function `%T::%D' without signature pointer/reference",
2443 basetype, save_name);
2444 return error_mark_node;
2445 }
2446 return build_signature_method_call (basetype, instance, function, parms);
2447 }
2448
2449 function = DECL_MAIN_VARIANT (function);
2450 mark_used (function);
2451
2452 /* Is it a synthesized method that needs to be synthesized? */
2453 if (DECL_ARTIFICIAL (function) && ! DECL_INITIAL (function)
2454 /* Kludge: don't synthesize for default args. */
2455 && current_function_decl)
2456 synthesize_method (function);
2457
2458 if (pedantic && DECL_THIS_INLINE (function) && ! DECL_ARTIFICIAL (function)
2459 && ! DECL_INITIAL (function) && ! DECL_PENDING_INLINE_INFO (function)
2460 && ! (DECL_TEMPLATE_INFO (function)
2461 && TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (function))))
2462 cp_warning ("inline function `%#D' called before definition", function);
2463
2464 fntype = TREE_TYPE (function);
2465 if (TREE_CODE (fntype) == POINTER_TYPE)
2466 fntype = TREE_TYPE (fntype);
2467 basetype = DECL_CLASS_CONTEXT (function);
2468
2469 /* If we are referencing a virtual function from an object
2470 of effectively static type, then there is no need
2471 to go through the virtual function table. */
2472 if (need_vtbl == maybe_needed)
2473 {
2474 int fixed_type = resolves_to_fixed_type_p (instance, 0);
2475
2476 if (all_virtual == 1
2477 && DECL_VINDEX (function)
2478 && may_be_remote (basetype))
2479 need_vtbl = needed;
2480 else if (DECL_VINDEX (function))
2481 need_vtbl = fixed_type ? unneeded : needed;
2482 else
2483 need_vtbl = not_needed;
2484 }
2485
2486 if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context
2487 && !DECL_CONSTRUCTOR_P (function))
2488 {
2489 /* Let's be nasty to the user now, and give reasonable
2490 error messages. */
2491 instance_ptr = current_class_ptr;
2492 if (instance_ptr)
2493 {
2494 if (basetype != current_class_type)
2495 {
2496 if (basetype == error_mark_node)
2497 return error_mark_node;
2498 else
2499 {
2500 if (orig_basetype != NULL_TREE)
2501 error_not_base_type (orig_basetype, current_class_type);
2502 else
2503 error_not_base_type (function, current_class_type);
2504 return error_mark_node;
2505 }
2506 }
2507 }
2508 /* Only allow a static member function to call another static member
2509 function. */
2510 else if (DECL_LANG_SPECIFIC (function)
2511 && !DECL_STATIC_FUNCTION_P (function))
2512 {
2513 cp_error ("cannot call member function `%D' without object",
2514 function);
2515 return error_mark_node;
2516 }
2517 }
2518
2519 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
2520
2521 if (TYPE_SIZE (complete_type (value_type)) == 0)
2522 {
2523 if (flags & LOOKUP_COMPLAIN)
2524 incomplete_type_error (0, value_type);
2525 return error_mark_node;
2526 }
2527
2528 if (DECL_STATIC_FUNCTION_P (function))
2529 parms = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
2530 TREE_CHAIN (parms), function, LOOKUP_NORMAL);
2531 else if (need_vtbl == unneeded)
2532 {
2533 int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
2534 basetype = TREE_TYPE (instance);
2535 if (TYPE_METHOD_BASETYPE (TREE_TYPE (function))
2536 != TYPE_MAIN_VARIANT (basetype))
2537 {
2538 basetype = DECL_CLASS_CONTEXT (function);
2539 instance_ptr = convert_pointer_to (basetype, instance_ptr);
2540 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2541 }
2542 parms = tree_cons (NULL_TREE, instance_ptr,
2543 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, sub_flags));
2544 }
2545 else
2546 {
2547 if ((flags & LOOKUP_NONVIRTUAL) == 0)
2548 basetype = DECL_CONTEXT (function);
2549
2550 /* First parm could be integer_zerop with casts like
2551 ((Object*)0)->Object::IsA() */
2552 if (!integer_zerop (TREE_VALUE (parms)))
2553 {
2554 /* Since we can't have inheritance with a union, doing get_binfo
2555 on it won't work. We do all the convert_pointer_to_real
2556 stuff to handle MI correctly...for unions, that's not
2557 an issue, so we must short-circuit that extra work here. */
2558 tree tmp = TREE_TYPE (TREE_TYPE (TREE_VALUE (parms)));
2559 if (tmp != NULL_TREE && TREE_CODE (tmp) == UNION_TYPE)
2560 instance_ptr = TREE_VALUE (parms);
2561 else
2562 {
2563 tree binfo = get_binfo (basetype,
2564 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
2565 0);
2566 instance_ptr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
2567 }
2568 instance_ptr
2569 = convert_pointer_to (build_type_variant (basetype,
2570 constp, volatilep),
2571 instance_ptr);
2572
2573 if (TREE_CODE (instance_ptr) == COND_EXPR)
2574 {
2575 instance_ptr = save_expr (instance_ptr);
2576 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2577 }
2578 else if (TREE_CODE (instance_ptr) == NOP_EXPR
2579 && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
2580 && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
2581 ;
2582 /* The call to `convert_pointer_to' may return error_mark_node. */
2583 else if (TREE_CODE (instance_ptr) == ERROR_MARK)
2584 return instance_ptr;
2585 else if (instance == NULL_TREE
2586 || TREE_CODE (instance) != INDIRECT_REF
2587 || TREE_OPERAND (instance, 0) != instance_ptr)
2588 instance = build_indirect_ref (instance_ptr, NULL_PTR);
2589 }
2590 parms = tree_cons (NULL_TREE, instance_ptr,
2591 convert_arguments (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), function, LOOKUP_NORMAL));
2592 }
2593
2594 if (parms == error_mark_node
2595 || (parms && TREE_CHAIN (parms) == error_mark_node))
2596 return error_mark_node;
2597
2598 if (need_vtbl == needed)
2599 {
2600 function = build_vfn_ref (&TREE_VALUE (parms), instance,
2601 DECL_VINDEX (function));
2602 TREE_TYPE (function) = build_pointer_type (fntype);
2603 }
2604
2605 if (TREE_CODE (function) == FUNCTION_DECL)
2606 GNU_xref_call (current_function_decl,
2607 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (function)));
2608
2609 result = build_call (function, value_type, parms);
2610 result = convert_from_reference (result);
2611 return result;
2612 }
2613
2614 /* Similar to `build_method_call', but for overloaded non-member functions.
2615 The name of this function comes through NAME. The name depends
2616 on PARMS.
2617
2618 Note that this function must handle simple `C' promotions,
2619 as well as variable numbers of arguments (...), and
2620 default arguments to boot.
2621
2622 If the overloading is successful, we return a tree node which
2623 contains the call to the function.
2624
2625 If overloading produces candidates which are probable, but not definite,
2626 we hold these candidates. If FINAL_CP is non-zero, then we are free
2627 to assume that final_cp points to enough storage for all candidates that
2628 this function might generate. The `harshness' array is preallocated for
2629 the first candidate, but not for subsequent ones.
2630
2631 Note that the DECL_RTL of FUNCTION must be made to agree with this
2632 function's new name. */
2633
2634 tree
2635 build_overload_call_real (fnname, parms, flags, final_cp, require_complete)
2636 tree fnname, parms;
2637 int flags;
2638 struct candidate *final_cp;
2639 int require_complete;
2640 {
2641 /* must check for overloading here */
2642 tree functions, function, parm;
2643 tree parmtypes, last;
2644 register tree outer;
2645 int length;
2646 int parmlength = list_length (parms);
2647
2648 struct candidate *candidates, *cp;
2649
2650 if (final_cp)
2651 {
2652 final_cp[0].h.code = 0;
2653 final_cp[0].h.distance = 0;
2654 final_cp[0].function = 0;
2655 /* end marker. */
2656 final_cp[1].h.code = EVIL_CODE;
2657 }
2658
2659 parmtypes = default_parm_conversions (parms, &last);
2660 if (parmtypes == error_mark_node)
2661 {
2662 if (final_cp)
2663 final_cp->h.code = EVIL_CODE;
2664 return error_mark_node;
2665 }
2666
2667 if (last)
2668 TREE_CHAIN (last) = void_list_node;
2669 else
2670 parmtypes = void_list_node;
2671
2672 if (is_overloaded_fn (fnname))
2673 {
2674 functions = fnname;
2675 if (TREE_CODE (fnname) == TREE_LIST)
2676 fnname = TREE_PURPOSE (functions);
2677 else if (TREE_CODE (fnname) == FUNCTION_DECL)
2678 fnname = DECL_NAME (functions);
2679 }
2680 else
2681 functions = lookup_name_nonclass (fnname);
2682
2683 if (functions == NULL_TREE)
2684 {
2685 if (flags & LOOKUP_SPECULATIVELY)
2686 return NULL_TREE;
2687 if (flags & LOOKUP_COMPLAIN)
2688 error ("only member functions apply");
2689 if (final_cp)
2690 final_cp->h.code = EVIL_CODE;
2691 return error_mark_node;
2692 }
2693
2694 if (TREE_CODE (functions) == FUNCTION_DECL && ! IDENTIFIER_OPNAME_P (fnname))
2695 {
2696 functions = DECL_MAIN_VARIANT (functions);
2697 if (final_cp)
2698 {
2699 /* We are just curious whether this is a viable alternative or
2700 not. */
2701 compute_conversion_costs (functions, parms, final_cp, parmlength);
2702 return functions;
2703 }
2704 else
2705 return build_function_call_real (functions, parms, 1, flags);
2706 }
2707
2708 if (TREE_CODE (functions) == TREE_LIST
2709 && TREE_VALUE (functions) == NULL_TREE)
2710 {
2711 if (flags & LOOKUP_SPECULATIVELY)
2712 return NULL_TREE;
2713
2714 if (flags & LOOKUP_COMPLAIN)
2715 cp_error ("function `%D' declared overloaded, but no instances of that function declared",
2716 TREE_PURPOSE (functions));
2717 if (final_cp)
2718 final_cp->h.code = EVIL_CODE;
2719 return error_mark_node;
2720 }
2721
2722 length = count_functions (functions);
2723
2724 if (final_cp)
2725 candidates = final_cp;
2726 else
2727 {
2728 candidates
2729 = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
2730 bzero ((char *) candidates, (length + 1) * sizeof (struct candidate));
2731 }
2732
2733 cp = candidates;
2734
2735 my_friendly_assert (is_overloaded_fn (functions), 169);
2736
2737 functions = get_first_fn (functions);
2738
2739 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
2740 for (outer = functions; outer; outer = DECL_CHAIN (outer))
2741 {
2742 int template_cost = 0;
2743 function = outer;
2744 if (TREE_CODE (function) != FUNCTION_DECL
2745 && ! (TREE_CODE (function) == TEMPLATE_DECL
2746 && TREE_CODE (DECL_TEMPLATE_RESULT (function)) == FUNCTION_DECL))
2747 {
2748 enum tree_code code = TREE_CODE (function);
2749 if (code == TEMPLATE_DECL)
2750 code = TREE_CODE (DECL_TEMPLATE_RESULT (function));
2751 if (code == CONST_DECL)
2752 cp_error_at
2753 ("enumeral value `%D' conflicts with function of same name",
2754 function);
2755 else if (code == VAR_DECL)
2756 {
2757 if (TREE_STATIC (function))
2758 cp_error_at
2759 ("variable `%D' conflicts with function of same name",
2760 function);
2761 else
2762 cp_error_at
2763 ("constant field `%D' conflicts with function of same name",
2764 function);
2765 }
2766 else if (code == TYPE_DECL)
2767 continue;
2768 else
2769 my_friendly_abort (2);
2770 error ("at this point in file");
2771 continue;
2772 }
2773 if (TREE_CODE (function) == TEMPLATE_DECL)
2774 {
2775 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (function));
2776 tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
2777 int i;
2778
2779 i = type_unification (DECL_TEMPLATE_PARMS (function), targs,
2780 TYPE_ARG_TYPES (TREE_TYPE (function)),
2781 parms, &template_cost, 0);
2782 if (i == 0)
2783 {
2784 function = instantiate_template (function, targs);
2785 if (function == error_mark_node)
2786 return function;
2787 }
2788 }
2789
2790 if (TREE_CODE (function) == TEMPLATE_DECL)
2791 {
2792 /* Unconverted template -- failed match. */
2793 cp->function = function;
2794 cp->u.bad_arg = -4;
2795 cp->h.code = EVIL_CODE;
2796 }
2797 else
2798 {
2799 struct candidate *cp2;
2800
2801 /* Check that this decl is not the same as a function that's in
2802 the list due to some template instantiation. */
2803 cp2 = candidates;
2804 while (cp2 != cp)
2805 if (cp2->function == function)
2806 break;
2807 else
2808 cp2 += 1;
2809 if (cp2->function == function)
2810 continue;
2811
2812 function = DECL_MAIN_VARIANT (function);
2813
2814 /* Can't use alloca here, since result might be
2815 passed to calling function. */
2816 cp->h_len = parmlength;
2817 cp->harshness = (struct harshness_code *)
2818 oballoc ((parmlength + 1) * sizeof (struct harshness_code));
2819
2820 compute_conversion_costs (function, parms, cp, parmlength);
2821
2822 /* Make sure this is clear as well. */
2823 cp->h.int_penalty += template_cost;
2824
2825 if ((cp[0].h.code & EVIL_CODE) == 0)
2826 {
2827 cp[1].h.code = EVIL_CODE;
2828 cp++;
2829 }
2830 }
2831 }
2832
2833 if (cp - candidates)
2834 {
2835 tree rval = error_mark_node;
2836
2837 /* Leave marker. */
2838 cp[0].h.code = EVIL_CODE;
2839 if (cp - candidates > 1)
2840 {
2841 struct candidate *best_cp
2842 = ideal_candidate (candidates, cp - candidates, parmlength);
2843 if (best_cp == (struct candidate *)0)
2844 {
2845 if (flags & LOOKUP_COMPLAIN)
2846 {
2847 cp_error ("call of overloaded `%D' is ambiguous", fnname);
2848 print_n_candidates (candidates, cp - candidates);
2849 }
2850 return error_mark_node;
2851 }
2852 else
2853 rval = best_cp->function;
2854 }
2855 else
2856 {
2857 cp -= 1;
2858 if (cp->h.code & EVIL_CODE)
2859 {
2860 if (flags & LOOKUP_COMPLAIN)
2861 error ("type conversion ambiguous");
2862 }
2863 else
2864 rval = cp->function;
2865 }
2866
2867 if (final_cp)
2868 return rval;
2869
2870 return build_function_call_real (rval, parms, require_complete, flags);
2871 }
2872
2873 if (flags & LOOKUP_SPECULATIVELY)
2874 return NULL_TREE;
2875
2876 if (flags & LOOKUP_COMPLAIN)
2877 report_type_mismatch (cp, parms, "function",
2878 decl_as_string (cp->function, 1));
2879
2880 return error_mark_node;
2881 }
2882
2883 /* This requires a complete type on the result of the call. */
2884 tree
2885 build_overload_call (fnname, parms, flags)
2886 tree fnname, parms;
2887 int flags;
2888 {
2889 return build_overload_call_real (fnname, parms, flags, (struct candidate *)0, 1);
2890 }