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