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