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