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