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