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