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