]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/cvt.c
a4c68e2180b76db37d4bec9a77080f42748d6b56
[gcc.git] / gcc / cp / cvt.c
1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 /* This file contains the functions for converting C expressions
24 to different data types. The only entry point is `convert'.
25 Every language front end must have a `convert' function
26 but what kind of conversions it does will depend on the language. */
27
28 #include "config.h"
29 #include "tree.h"
30 #include "flags.h"
31 #include "cp-tree.h"
32 #include "class.h"
33 #include "convert.h"
34
35 #undef NULL
36 #define NULL (char *)0
37
38 tree build_user_type_conversion ();
39
40 /* Change of width--truncation and extension of integers or reals--
41 is represented with NOP_EXPR. Proper functioning of many things
42 assumes that no other conversions can be NOP_EXPRs.
43
44 Conversion between integer and pointer is represented with CONVERT_EXPR.
45 Converting integer to real uses FLOAT_EXPR
46 and real to integer uses FIX_TRUNC_EXPR.
47
48 Here is a list of all the functions that assume that widening and
49 narrowing is always done with a NOP_EXPR:
50 In convert.c, convert_to_integer.
51 In c-typeck.c, build_binary_op_nodefault (boolean ops),
52 and truthvalue_conversion.
53 In expr.c: expand_expr, for operands of a MULT_EXPR.
54 In fold-const.c: fold.
55 In tree.c: get_narrower and get_unwidened.
56
57 C++: in multiple-inheritance, converting between pointers may involve
58 adjusting them by a delta stored within the class definition. */
59 \f
60 /* Subroutines of `convert'. */
61
62 /* Build a thunk. What it is, is an entry point that when called will
63 adjust the this pointer (the first argument) by offset, and then
64 goto the real address of the function given by REAL_ADDR that we
65 would like called. What we return is the address of the thunk. */
66
67 static tree
68 build_thunk (offset, real_addr)
69 tree offset, real_addr;
70 {
71 if (TREE_CODE (real_addr) != ADDR_EXPR
72 || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)
73 {
74 sorry ("MI pointer to member conversion too complex");
75 return error_mark_node;
76 }
77 sorry ("MI pointer to member conversion too complex");
78 return error_mark_node;
79 }
80
81 /* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into
82 another `pointer to method'. This may involved the creation of
83 a thunk to handle the this offset calculation. */
84
85 static tree
86 convert_fn_ptr (type, expr)
87 tree type, expr;
88 {
89 #if 0 /* We don't use thunks for pmfs. */
90 if (flag_vtable_thunks)
91 {
92 tree intype = TREE_TYPE (expr);
93 tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype)),
94 TYPE_METHOD_BASETYPE (TREE_TYPE (type)), 1);
95 if (binfo == error_mark_node)
96 {
97 error (" in pointer to member conversion");
98 return error_mark_node;
99 }
100 if (binfo == NULL_TREE)
101 {
102 /* ARM 4.8 restriction. */
103 error ("invalid pointer to member conversion");
104 return error_mark_node;
105 }
106
107 if (BINFO_OFFSET_ZEROP (binfo))
108 return build1 (NOP_EXPR, type, expr);
109 return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));
110 }
111 else
112 #endif
113 return build_ptrmemfunc (type, expr, 1);
114 }
115
116 /* if converting pointer to pointer
117 if dealing with classes, check for derived->base or vice versa
118 else if dealing with method pointers, delegate
119 else convert blindly
120 else if converting class, pass off to build_type_conversion
121 else try C-style pointer conversion */
122
123 static tree
124 cp_convert_to_pointer (type, expr)
125 tree type, expr;
126 {
127 register tree intype = TREE_TYPE (expr);
128 register enum tree_code form;
129
130 if (IS_AGGR_TYPE (intype))
131 {
132 tree rval;
133
134 intype = complete_type (intype);
135 if (TYPE_SIZE (intype) == NULL_TREE)
136 {
137 cp_error ("can't convert from incomplete type `%T' to `%T'",
138 intype, type);
139 return error_mark_node;
140 }
141
142 rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);
143 if (rval)
144 {
145 if (rval == error_mark_node)
146 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
147 expr, intype, type);
148 return rval;
149 }
150 }
151
152 if (TYPE_PTRMEMFUNC_P (type))
153 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
154 if (TYPE_PTRMEMFUNC_P (intype))
155 intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);
156
157 form = TREE_CODE (intype);
158
159 if (form == POINTER_TYPE || form == REFERENCE_TYPE)
160 {
161 intype = TYPE_MAIN_VARIANT (intype);
162
163 if (TYPE_MAIN_VARIANT (type) != intype
164 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
165 && IS_AGGR_TYPE (TREE_TYPE (type))
166 && IS_AGGR_TYPE (TREE_TYPE (intype))
167 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
168 {
169 enum tree_code code = PLUS_EXPR;
170 tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);
171 if (binfo == error_mark_node)
172 return error_mark_node;
173 if (binfo == NULL_TREE)
174 {
175 binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);
176 if (binfo == error_mark_node)
177 return error_mark_node;
178 code = MINUS_EXPR;
179 }
180 if (binfo)
181 {
182 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))
183 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))
184 || ! BINFO_OFFSET_ZEROP (binfo))
185 {
186 /* Need to get the path we took. */
187 tree path;
188
189 if (code == PLUS_EXPR)
190 get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);
191 else
192 get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);
193 return build_vbase_path (code, type, expr, path, 0);
194 }
195 }
196 }
197 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
198 && TREE_CODE (type) == POINTER_TYPE
199 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
200 return convert_fn_ptr (type, expr);
201
202 if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE
203 && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)
204 {
205 tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));
206 tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));
207 tree binfo = get_binfo (b1, b2, 1);
208 if (binfo == NULL_TREE)
209 binfo = get_binfo (b2, b1, 1);
210 if (binfo == error_mark_node)
211 return error_mark_node;
212 }
213
214 if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE
215 || (TREE_CODE (type) == POINTER_TYPE
216 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))
217 {
218 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
219 expr, intype, type);
220 return error_mark_node;
221 }
222
223 return build1 (NOP_EXPR, type, expr);
224 }
225
226 my_friendly_assert (form != OFFSET_TYPE, 186);
227
228 if (TYPE_LANG_SPECIFIC (intype)
229 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
230 return convert_to_pointer (type, build_optr_ref (expr));
231
232 if (integer_zerop (expr))
233 {
234 if (type == TREE_TYPE (null_pointer_node))
235 return null_pointer_node;
236 expr = build_int_2 (0, 0);
237 TREE_TYPE (expr) = type;
238 return expr;
239 }
240
241 if (INTEGRAL_CODE_P (form))
242 {
243 if (type_precision (intype) == POINTER_SIZE)
244 return build1 (CONVERT_EXPR, type, expr);
245 expr = convert (type_for_size (POINTER_SIZE, 0), expr);
246 /* Modes may be different but sizes should be the same. */
247 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
248 != GET_MODE_SIZE (TYPE_MODE (type)))
249 /* There is supposed to be some integral type
250 that is the same width as a pointer. */
251 abort ();
252 return convert_to_pointer (type, expr);
253 }
254
255 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
256 expr, intype, type);
257 return error_mark_node;
258 }
259
260 /* Like convert, except permit conversions to take place which
261 are not normally allowed due to access restrictions
262 (such as conversion from sub-type to private super-type). */
263
264 static tree
265 convert_to_pointer_force (type, expr)
266 tree type, expr;
267 {
268 register tree intype = TREE_TYPE (expr);
269 register enum tree_code form = TREE_CODE (intype);
270
271 if (integer_zerop (expr))
272 {
273 if (type == TREE_TYPE (null_pointer_node))
274 return null_pointer_node;
275 expr = build_int_2 (0, 0);
276 TREE_TYPE (expr) = type;
277 return expr;
278 }
279
280 /* Convert signature pointer/reference to `void *' first. */
281 if (form == RECORD_TYPE
282 && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))
283 {
284 expr = build_optr_ref (expr);
285 intype = TREE_TYPE (expr);
286 form = TREE_CODE (intype);
287 }
288
289 if (form == POINTER_TYPE)
290 {
291 intype = TYPE_MAIN_VARIANT (intype);
292
293 if (TYPE_MAIN_VARIANT (type) != intype
294 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
295 && IS_AGGR_TYPE (TREE_TYPE (type))
296 && IS_AGGR_TYPE (TREE_TYPE (intype))
297 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
298 {
299 enum tree_code code = PLUS_EXPR;
300 tree path;
301 int distance = get_base_distance (TREE_TYPE (type),
302 TREE_TYPE (intype), 0, &path);
303 if (distance == -2)
304 {
305 ambig:
306 cp_error ("type `%T' is ambiguous baseclass of `%s'", TREE_TYPE (type),
307 TYPE_NAME_STRING (TREE_TYPE (intype)));
308 return error_mark_node;
309 }
310 if (distance == -1)
311 {
312 distance = get_base_distance (TREE_TYPE (intype),
313 TREE_TYPE (type), 0, &path);
314 if (distance == -2)
315 goto ambig;
316 if (distance < 0)
317 /* Doesn't need any special help from us. */
318 return build1 (NOP_EXPR, type, expr);
319
320 code = MINUS_EXPR;
321 }
322 return build_vbase_path (code, type, expr, path, 0);
323 }
324 return build1 (NOP_EXPR, type, expr);
325 }
326
327 return cp_convert_to_pointer (type, expr);
328 }
329
330 /* We are passing something to a function which requires a reference.
331 The type we are interested in is in TYPE. The initial
332 value we have to begin with is in ARG.
333
334 FLAGS controls how we manage access checking.
335 INDIRECT_BIND in FLAGS controls how any temporarys are generated.
336 CHECKCONST controls if we report error messages on const subversion. */
337
338 static tree
339 build_up_reference (type, arg, flags, checkconst)
340 tree type, arg;
341 int flags, checkconst;
342 {
343 tree rval, targ;
344 int literal_flag = 0;
345 tree argtype = TREE_TYPE (arg);
346 tree target_type = TREE_TYPE (type);
347 tree binfo = NULL_TREE;
348
349 my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);
350 if ((flags & LOOKUP_PROTECT)
351 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
352 && IS_AGGR_TYPE (argtype)
353 && IS_AGGR_TYPE (target_type))
354 {
355 binfo = get_binfo (target_type, argtype, 1);
356 if (binfo == error_mark_node)
357 return error_mark_node;
358 if (binfo == NULL_TREE)
359 return error_not_base_type (target_type, argtype);
360 }
361
362 /* Pass along const and volatile down into the type. */
363 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
364 target_type = cp_build_type_variant (target_type, TYPE_READONLY (type),
365 TYPE_VOLATILE (type));
366 targ = arg;
367 if (TREE_CODE (targ) == SAVE_EXPR)
368 targ = TREE_OPERAND (targ, 0);
369 while (TREE_CODE (targ) == NOP_EXPR
370 && (TYPE_MAIN_VARIANT (argtype)
371 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (targ, 0)))))
372 targ = TREE_OPERAND (targ, 0);
373
374 switch (TREE_CODE (targ))
375 {
376 case INDIRECT_REF:
377 /* This is a call to a constructor which did not know what it was
378 initializing until now: it needs to initialize a temporary. */
379 if (TREE_HAS_CONSTRUCTOR (targ))
380 {
381 tree temp = build_cplus_new (argtype, TREE_OPERAND (targ, 0));
382 TREE_HAS_CONSTRUCTOR (targ) = 0;
383 return build_up_reference (type, temp, flags, 1);
384 }
385 /* Let &* cancel out to simplify resulting code.
386 Also, throw away intervening NOP_EXPRs. */
387 arg = TREE_OPERAND (targ, 0);
388 if (TREE_CODE (arg) == NOP_EXPR || TREE_CODE (arg) == NON_LVALUE_EXPR
389 || (TREE_CODE (arg) == CONVERT_EXPR && TREE_REFERENCE_EXPR (arg)))
390 arg = TREE_OPERAND (arg, 0);
391
392 /* in doing a &*, we have to get rid of the const'ness on the pointer
393 value. Haven't thought about volatile here. Pointers come to mind
394 here. */
395 if (TREE_READONLY (arg))
396 {
397 arg = copy_node (arg);
398 TREE_READONLY (arg) = 0;
399 }
400
401 rval = build1 (CONVERT_EXPR, type, arg);
402 TREE_REFERENCE_EXPR (rval) = 1;
403
404 /* propagate the const flag on something like:
405
406 class Base {
407 public:
408 int foo;
409 };
410
411 class Derived : public Base {
412 public:
413 int bar;
414 };
415
416 void func(Base&);
417
418 void func2(const Derived& d) {
419 func(d);
420 }
421
422 on the d parameter. The below could have been avoided, if the flags
423 were down in the tree, not sure why they are not. (mrs) */
424 /* The below code may have to be propagated to other parts of this
425 switch. */
426 if (TREE_READONLY (targ) && !TREE_READONLY (arg)
427 && (TREE_CODE (arg) == PARM_DECL || TREE_CODE (arg) == VAR_DECL)
428 && TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
429 && (TYPE_READONLY (target_type) && checkconst))
430 {
431 arg = copy_node (arg);
432 TREE_READONLY (arg) = TREE_READONLY (targ);
433 }
434 literal_flag = TREE_CONSTANT (arg);
435
436 goto done;
437
438 /* Get this out of a register if we happened to be in one by accident.
439 Also, build up references to non-lvalues it we must. */
440 /* For &x[y], return (&) x+y */
441 case ARRAY_REF:
442 if (mark_addressable (TREE_OPERAND (targ, 0)) == 0)
443 return error_mark_node;
444 rval = build_binary_op (PLUS_EXPR, TREE_OPERAND (targ, 0),
445 TREE_OPERAND (targ, 1), 1);
446 TREE_TYPE (rval) = type;
447 if (TREE_CONSTANT (TREE_OPERAND (targ, 1))
448 && staticp (TREE_OPERAND (targ, 0)))
449 TREE_CONSTANT (rval) = 1;
450 goto done;
451
452 case SCOPE_REF:
453 /* Could be a reference to a static member. */
454 {
455 tree field = TREE_OPERAND (targ, 1);
456 if (TREE_STATIC (field))
457 {
458 rval = build1 (ADDR_EXPR, type, field);
459 literal_flag = 1;
460 goto done;
461 }
462 }
463
464 /* We should have farmed out member pointers above. */
465 my_friendly_abort (188);
466
467 case COMPONENT_REF:
468 rval = build_component_addr (targ, build_pointer_type (argtype),
469 "attempt to make a reference to bit-field structure member `%s'");
470 TREE_TYPE (rval) = type;
471 literal_flag = staticp (TREE_OPERAND (targ, 0));
472
473 goto done;
474
475 /* Anything not already handled and not a true memory reference
476 needs to have a reference built up. Do so silently for
477 things like integers and return values from function,
478 but complain if we need a reference to something declared
479 as `register'. */
480
481 case RESULT_DECL:
482 if (staticp (targ))
483 literal_flag = 1;
484 TREE_ADDRESSABLE (targ) = 1;
485 put_var_into_stack (targ);
486 break;
487
488 case PARM_DECL:
489 #if 0
490 if (targ == current_class_ptr)
491 {
492 error ("address of `this' not available");
493 /* #if 0 */
494 /* This code makes the following core dump the compiler on a sun4,
495 if the code below is used.
496
497 class e_decl;
498 class a_decl;
499 typedef a_decl* a_ref;
500
501 class a_s {
502 public:
503 a_s();
504 void* append(a_ref& item);
505 };
506 class a_decl {
507 public:
508 a_decl (e_decl *parent);
509 a_s generic_s;
510 a_s decls;
511 e_decl* parent;
512 };
513
514 class e_decl {
515 public:
516 e_decl();
517 a_s implementations;
518 };
519
520 void foobar(void *);
521
522 a_decl::a_decl(e_decl *parent) {
523 parent->implementations.append(this);
524 }
525 */
526
527 TREE_ADDRESSABLE (targ) = 1; /* so compiler doesn't die later */
528 put_var_into_stack (targ);
529 break;
530 /* #else */
531 return error_mark_node;
532 /* #endif */
533 }
534 #endif
535 /* Fall through. */
536 case VAR_DECL:
537 case CONST_DECL:
538 if (DECL_REGISTER (targ) && !TREE_ADDRESSABLE (targ)
539 && !DECL_ARTIFICIAL (targ))
540 cp_warning ("address needed to build reference for `%D', which is declared `register'",
541 targ);
542 else if (staticp (targ))
543 literal_flag = 1;
544
545 TREE_ADDRESSABLE (targ) = 1;
546 put_var_into_stack (targ);
547 break;
548
549 case COMPOUND_EXPR:
550 {
551 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 1),
552 LOOKUP_PROTECT, checkconst);
553 rval = build (COMPOUND_EXPR, type, TREE_OPERAND (targ, 0), real_reference);
554 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 1));
555 return rval;
556 }
557
558 case PREINCREMENT_EXPR:
559 case PREDECREMENT_EXPR:
560 case MODIFY_EXPR:
561 case INIT_EXPR:
562 {
563 tree real_reference = build_up_reference (type, TREE_OPERAND (targ, 0),
564 LOOKUP_PROTECT, checkconst);
565 rval = build (COMPOUND_EXPR, type, arg, real_reference);
566 TREE_CONSTANT (rval) = staticp (TREE_OPERAND (targ, 0));
567 return rval;
568 }
569
570 case COND_EXPR:
571 return build (COND_EXPR, type,
572 TREE_OPERAND (targ, 0),
573 build_up_reference (type, TREE_OPERAND (targ, 1),
574 LOOKUP_PROTECT, checkconst),
575 build_up_reference (type, TREE_OPERAND (targ, 2),
576 LOOKUP_PROTECT, checkconst));
577
578 /* Undo the folding... */
579 case MIN_EXPR:
580 case MAX_EXPR:
581 return build (COND_EXPR, type,
582 build (TREE_CODE (targ) == MIN_EXPR ? LT_EXPR : GT_EXPR,
583 boolean_type_node, TREE_OPERAND (targ, 0),
584 TREE_OPERAND (targ, 1)),
585 build_up_reference (type, TREE_OPERAND (targ, 0),
586 LOOKUP_PROTECT, checkconst),
587 build_up_reference (type, TREE_OPERAND (targ, 1),
588 LOOKUP_PROTECT, checkconst));
589
590 case BIND_EXPR:
591 arg = TREE_OPERAND (targ, 1);
592 if (arg == NULL_TREE)
593 {
594 compiler_error ("({ ... }) expression not expanded when needed for reference");
595 return error_mark_node;
596 }
597 rval = build1 (ADDR_EXPR, type, arg);
598 TREE_REFERENCE_EXPR (rval) = 1;
599 return rval;
600
601 default:
602 break;
603 }
604
605 if (TREE_ADDRESSABLE (targ) == 0)
606 {
607 if (! (flags&INDIRECT_BIND)
608 && toplevel_bindings_p ())
609 {
610 tree temp = get_temp_name (argtype, 0);
611 /* Give this new temp some rtl and initialize it. */
612 DECL_INITIAL (temp) = targ;
613 TREE_STATIC (temp) = 1;
614 cp_finish_decl (temp, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);
615 /* Do this after declaring it static. */
616 rval = build_unary_op (ADDR_EXPR, temp, 0);
617 TREE_TYPE (rval) = type;
618 literal_flag = TREE_CONSTANT (rval);
619 goto done;
620 }
621
622 if (TREE_CODE (targ) == CALL_EXPR && IS_AGGR_TYPE (argtype))
623 {
624 arg = build_cplus_new (argtype, targ);
625 }
626 else if (flags&INDIRECT_BIND)
627 {
628 /* This should be the default, not the below code. */
629 /* All callers except grok_reference_init should probably
630 use INDIRECT_BIND. */
631 tree slot = build (VAR_DECL, argtype);
632 layout_decl (slot, 0);
633 arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);
634 }
635 else
636 {
637 tree temp = get_temp_name (argtype, 0);
638 rval = build_unary_op (ADDR_EXPR, temp, 0);
639 if (binfo && !BINFO_OFFSET_ZEROP (binfo))
640 rval = convert_pointer_to (target_type, rval);
641 else
642 TREE_TYPE (rval) = type;
643
644 temp = build (MODIFY_EXPR, argtype, temp, arg);
645 TREE_SIDE_EFFECTS (temp) = 1;
646 return build (COMPOUND_EXPR, type, temp, rval);
647 }
648 }
649
650 if (! (flags&INDIRECT_BIND))
651 {
652 if (TREE_CODE (arg) == TARGET_EXPR)
653 {
654 tree decl = TREE_OPERAND (arg, 0);
655 tree cleanup;
656
657 if (! toplevel_bindings_p () && ! DECL_RTL (decl))
658 {
659 expand_decl (decl);
660 cleanup = maybe_build_cleanup (decl);
661 if (cleanup)
662 expand_decl_cleanup (decl, cleanup);
663 }
664 }
665 }
666
667 rval = build1 (ADDR_EXPR, type, arg);
668
669 done:
670 if (TYPE_USES_COMPLEX_INHERITANCE (argtype)
671 || TYPE_USES_COMPLEX_INHERITANCE (target_type))
672 {
673 TREE_TYPE (rval) = build_pointer_type (argtype);
674 if (flags & LOOKUP_PROTECT)
675 rval = convert_pointer_to (target_type, rval);
676 else
677 rval
678 = convert_to_pointer_force (build_pointer_type (target_type), rval);
679 TREE_TYPE (rval) = type;
680 if (TREE_CODE (rval) == PLUS_EXPR || TREE_CODE (rval) == MINUS_EXPR)
681 TREE_TYPE (TREE_OPERAND (rval, 0))
682 = TREE_TYPE (TREE_OPERAND (rval, 1)) = type;
683 }
684 TREE_CONSTANT (rval) = literal_flag;
685 return rval;
686 }
687
688 /* For C++: Only need to do one-level references, but cannot
689 get tripped up on signed/unsigned differences.
690
691 DECL is either NULL_TREE or the _DECL node for a reference that is being
692 initialized. It can be error_mark_node if we don't know the _DECL but
693 we know it's an initialization. */
694
695 tree
696 convert_to_reference (reftype, expr, convtype, flags, decl)
697 tree reftype, expr;
698 int convtype, flags;
699 tree decl;
700 {
701 register tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
702 register tree intype = TREE_TYPE (expr);
703 tree rval = NULL_TREE;
704 tree rval_as_conversion = NULL_TREE;
705 int i;
706
707 if (TREE_CODE (intype) == REFERENCE_TYPE)
708 my_friendly_abort (364);
709
710 intype = TYPE_MAIN_VARIANT (intype);
711
712 i = comp_target_types (type, intype, 0);
713
714 if (i <= 0 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
715 && ! (flags & LOOKUP_NO_CONVERSION))
716 {
717 /* Look for a user-defined conversion to lvalue that we can use. */
718
719 #ifdef NEW_OVER
720 rval_as_conversion
721 = build_type_conversion (CONVERT_EXPR, reftype, expr, 1);
722 #else
723 rval_as_conversion = build_type_conversion (CONVERT_EXPR, type, expr, 1);
724 #endif
725
726 if (rval_as_conversion && rval_as_conversion != error_mark_node
727 && real_lvalue_p (rval_as_conversion))
728 {
729 expr = rval_as_conversion;
730 rval_as_conversion = NULL_TREE;
731 intype = type;
732 i = 1;
733 }
734 }
735
736 if (((convtype & CONV_STATIC) && i == -1)
737 || ((convtype & CONV_IMPLICIT) && i == 1))
738 {
739 if (flags & LOOKUP_COMPLAIN)
740 {
741 tree ttl = TREE_TYPE (reftype);
742 tree ttr;
743
744 {
745 int r = TREE_READONLY (expr);
746 int v = TREE_THIS_VOLATILE (expr);
747 ttr = cp_build_type_variant (TREE_TYPE (expr), r, v);
748 }
749
750 if (! real_lvalue_p (expr) &&
751 (decl == NULL_TREE || ! TYPE_READONLY (ttl)))
752 {
753 if (decl)
754 /* Ensure semantics of [dcl.init.ref] */
755 cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'",
756 reftype, intype);
757 else
758 cp_pedwarn ("conversion to `%T' from rvalue `%T'",
759 reftype, intype);
760 }
761 else if (! (convtype & CONV_CONST))
762 {
763 if (! TYPE_READONLY (ttl) && TYPE_READONLY (ttr))
764 cp_pedwarn ("conversion from `%T' to `%T' discards const",
765 ttr, reftype);
766 else if (! TYPE_VOLATILE (ttl) && TYPE_VOLATILE (ttr))
767 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
768 ttr, reftype);
769 }
770 }
771
772 return build_up_reference (reftype, expr, flags,
773 ! (convtype & CONV_CONST));
774 }
775 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
776 {
777 /* When casting an lvalue to a reference type, just convert into
778 a pointer to the new type and deference it. This is allowed
779 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
780 should be done directly (jason). (int &)ri ---> *(int*)&ri */
781
782 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
783 meant. */
784 if (TREE_CODE (intype) == POINTER_TYPE
785 && (comptypes (TREE_TYPE (intype), type, -1)))
786 cp_warning ("casting `%T' to `%T' does not dereference pointer",
787 intype, reftype);
788
789 rval = build_unary_op (ADDR_EXPR, expr, 0);
790 if (rval != error_mark_node)
791 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)), rval, 0);
792 if (rval != error_mark_node)
793 rval = build1 (NOP_EXPR, reftype, rval);
794 }
795 else if (decl)
796 {
797 tree rval_as_ctor = NULL_TREE;
798
799 if (rval_as_conversion)
800 {
801 if (rval_as_conversion == error_mark_node)
802 {
803 cp_error ("conversion from `%T' to `%T' is ambiguous",
804 intype, reftype);
805 return error_mark_node;
806 }
807 rval_as_conversion = build_up_reference (reftype, rval_as_conversion,
808 flags, 1);
809 }
810
811 /* Definitely need to go through a constructor here. */
812 if (TYPE_HAS_CONSTRUCTOR (type)
813 && ! CLASSTYPE_ABSTRACT_VIRTUALS (type)
814 && (rval = build_method_call
815 (NULL_TREE, ctor_identifier,
816 build_tree_list (NULL_TREE, expr), TYPE_BINFO (type),
817 LOOKUP_NO_CONVERSION|LOOKUP_SPECULATIVELY
818 | LOOKUP_ONLYCONVERTING)))
819 {
820 tree init;
821
822 if (toplevel_bindings_p ())
823 {
824 extern tree static_aggregates;
825 tree t = get_temp_name (type, toplevel_bindings_p ());
826 init = build_method_call (t, ctor_identifier,
827 build_tree_list (NULL_TREE, expr),
828 TYPE_BINFO (type),
829 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
830 | LOOKUP_ONLYCONVERTING);
831
832 if (init == error_mark_node)
833 return error_mark_node;
834
835 make_decl_rtl (t, NULL_PTR, 1);
836 static_aggregates = perm_tree_cons (expr, t, static_aggregates);
837 rval = build_unary_op (ADDR_EXPR, t, 0);
838 }
839 else
840 {
841 init = build_method_call (NULL_TREE, ctor_identifier,
842 build_tree_list (NULL_TREE, expr),
843 TYPE_BINFO (type),
844 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION
845 |LOOKUP_ONLYCONVERTING);
846
847 if (init == error_mark_node)
848 return error_mark_node;
849
850 rval = build_cplus_new (type, init);
851 rval = build_up_reference (reftype, rval, flags, 1);
852 }
853 rval_as_ctor = rval;
854 }
855
856 if (rval_as_ctor && rval_as_conversion)
857 {
858 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
859 intype, reftype);
860 return error_mark_node;
861 }
862 else if (rval_as_ctor)
863 rval = rval_as_ctor;
864 else if (rval_as_conversion)
865 rval = rval_as_conversion;
866 else if (! IS_AGGR_TYPE (type) && ! IS_AGGR_TYPE (intype))
867 {
868 rval = convert (type, expr);
869 if (rval == error_mark_node)
870 return error_mark_node;
871
872 rval = build_up_reference (reftype, rval, flags, 1);
873 }
874
875 if (rval && ! TYPE_READONLY (TREE_TYPE (reftype)))
876 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
877 reftype, intype);
878 }
879
880 if (rval)
881 {
882 /* If we found a way to convert earlier, then use it. */
883 return rval;
884 }
885
886 my_friendly_assert (TREE_CODE (intype) != OFFSET_TYPE, 189);
887
888 if (flags & LOOKUP_COMPLAIN)
889 cp_error ("cannot convert type `%T' to type `%T'", intype, reftype);
890
891 if (flags & LOOKUP_SPECULATIVELY)
892 return NULL_TREE;
893
894 return error_mark_node;
895 }
896
897 /* We are using a reference VAL for its value. Bash that reference all the
898 way down to its lowest form. */
899
900 tree
901 convert_from_reference (val)
902 tree val;
903 {
904 tree type = TREE_TYPE (val);
905
906 if (TREE_CODE (type) == OFFSET_TYPE)
907 type = TREE_TYPE (type);
908 if (TREE_CODE (type) == REFERENCE_TYPE)
909 return build_indirect_ref (val, NULL_PTR);
910 return val;
911 }
912 \f
913 /* See if there is a constructor of type TYPE which will convert
914 EXPR. The reference manual seems to suggest (8.5.6) that we need
915 not worry about finding constructors for base classes, then converting
916 to the derived class.
917
918 MSGP is a pointer to a message that would be an appropriate error
919 string. If MSGP is NULL, then we are not interested in reporting
920 errors. */
921
922 tree
923 convert_to_aggr (type, expr, msgp, protect)
924 tree type, expr;
925 char **msgp;
926 int protect;
927 {
928 tree basetype = type;
929 tree name = TYPE_IDENTIFIER (basetype);
930 tree function, fndecl, fntype, parmtypes, parmlist, result;
931 #if 0
932 /* See code below that used this. */
933 tree method_name;
934 #endif
935 tree access;
936 int can_be_private, can_be_protected;
937
938 if (! TYPE_HAS_CONSTRUCTOR (basetype))
939 {
940 if (msgp)
941 *msgp = "type `%s' does not have a constructor";
942 return error_mark_node;
943 }
944
945 access = access_public_node;
946 can_be_private = 0;
947 can_be_protected = IDENTIFIER_CLASS_VALUE (name) || name == current_class_name;
948
949 parmlist = build_tree_list (NULL_TREE, expr);
950 parmtypes = tree_cons (NULL_TREE, TREE_TYPE (expr), void_list_node);
951
952 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
953 {
954 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
955 parmlist = tree_cons (NULL_TREE, integer_one_node, parmlist);
956 }
957
958 /* The type of the first argument will be filled in inside the loop. */
959 parmlist = tree_cons (NULL_TREE, integer_zero_node, parmlist);
960 parmtypes = tree_cons (NULL_TREE, build_pointer_type (basetype), parmtypes);
961
962 /* No exact conversion was found. See if an approximate
963 one will do. */
964 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype), 0);
965
966 {
967 int saw_private = 0;
968 int saw_protected = 0;
969 struct candidate *candidates =
970 (struct candidate *) alloca ((decl_list_length (fndecl)+1) * sizeof (struct candidate));
971 struct candidate *cp = candidates;
972
973 while (fndecl)
974 {
975 function = fndecl;
976 cp->h_len = 2;
977 cp->harshness = (struct harshness_code *)
978 alloca (3 * sizeof (struct harshness_code));
979
980 compute_conversion_costs (fndecl, parmlist, cp, 2);
981 if ((cp->h.code & EVIL_CODE) == 0)
982 {
983 cp->u.field = fndecl;
984 if (protect)
985 {
986 if (TREE_PRIVATE (fndecl))
987 access = access_private_node;
988 else if (TREE_PROTECTED (fndecl))
989 access = access_protected_node;
990 else
991 access = access_public_node;
992 }
993 else
994 access = access_public_node;
995
996 if (access == access_private_node
997 ? (basetype == current_class_type
998 || is_friend (basetype, cp->function)
999 || purpose_member (basetype, DECL_ACCESS (fndecl)))
1000 : access == access_protected_node
1001 ? (can_be_protected
1002 || purpose_member (basetype, DECL_ACCESS (fndecl)))
1003 : 1)
1004 {
1005 if (cp->h.code <= TRIVIAL_CODE)
1006 goto found_and_ok;
1007 cp++;
1008 }
1009 else
1010 {
1011 if (access == access_private_node)
1012 saw_private = 1;
1013 else
1014 saw_protected = 1;
1015 }
1016 }
1017 fndecl = DECL_CHAIN (fndecl);
1018 }
1019 if (cp - candidates)
1020 {
1021 /* Rank from worst to best. Then cp will point to best one.
1022 Private fields have their bits flipped. For unsigned
1023 numbers, this should make them look very large.
1024 If the best alternate has a (signed) negative value,
1025 then all we ever saw were private members. */
1026 if (cp - candidates > 1)
1027 qsort (candidates, /* char *base */
1028 cp - candidates, /* int nel */
1029 sizeof (struct candidate), /* int width */
1030 rank_for_overload); /* int (*compar)() */
1031
1032 --cp;
1033 if (cp->h.code & EVIL_CODE)
1034 {
1035 if (msgp)
1036 *msgp = "ambiguous type conversion possible for `%s'";
1037 return error_mark_node;
1038 }
1039
1040 function = cp->function;
1041 fndecl = cp->u.field;
1042 goto found_and_ok;
1043 }
1044 else if (msgp)
1045 {
1046 if (saw_private)
1047 if (saw_protected)
1048 *msgp = "only private and protected conversions apply";
1049 else
1050 *msgp = "only private conversions apply";
1051 else if (saw_protected)
1052 *msgp = "only protected conversions apply";
1053 else
1054 *msgp = "no appropriate conversion to type `%s'";
1055 }
1056 return error_mark_node;
1057 }
1058 /* NOTREACHED */
1059
1060 found:
1061 if (access == access_private_node)
1062 if (! can_be_private)
1063 {
1064 if (msgp)
1065 *msgp = TREE_PRIVATE (fndecl)
1066 ? "conversion to type `%s' is private"
1067 : "conversion to type `%s' is from private base class";
1068 return error_mark_node;
1069 }
1070 if (access == access_protected_node)
1071 if (! can_be_protected)
1072 {
1073 if (msgp)
1074 *msgp = TREE_PRIVATE (fndecl)
1075 ? "conversion to type `%s' is protected"
1076 : "conversion to type `%s' is from protected base class";
1077 return error_mark_node;
1078 }
1079 function = fndecl;
1080 found_and_ok:
1081
1082 /* It will convert, but we don't do anything about it yet. */
1083 if (msgp == 0)
1084 return NULL_TREE;
1085
1086 fntype = TREE_TYPE (function);
1087
1088 parmlist = convert_arguments (NULL_TREE, TYPE_ARG_TYPES (fntype),
1089 parmlist, NULL_TREE, LOOKUP_NORMAL);
1090
1091 result = build_call (function, TREE_TYPE (fntype), parmlist);
1092 return result;
1093 }
1094
1095 /* Call this when we know (for any reason) that expr is not, in fact,
1096 zero. This routine is like convert_pointer_to, but it pays
1097 attention to which specific instance of what type we want to
1098 convert to. This routine should eventually become
1099 convert_to_pointer after all references to convert_to_pointer
1100 are removed. */
1101
1102 tree
1103 convert_pointer_to_real (binfo, expr)
1104 tree binfo, expr;
1105 {
1106 register tree intype = TREE_TYPE (expr);
1107 tree ptr_type;
1108 tree type, rval;
1109
1110 if (TREE_CODE (binfo) == TREE_VEC)
1111 type = BINFO_TYPE (binfo);
1112 else if (IS_AGGR_TYPE (binfo))
1113 {
1114 type = binfo;
1115 }
1116 else
1117 {
1118 type = binfo;
1119 binfo = NULL_TREE;
1120 }
1121
1122 ptr_type = cp_build_type_variant (type, TYPE_READONLY (TREE_TYPE (intype)),
1123 TYPE_VOLATILE (TREE_TYPE (intype)));
1124 ptr_type = build_pointer_type (ptr_type);
1125 if (ptr_type == TYPE_MAIN_VARIANT (intype))
1126 return expr;
1127
1128 if (intype == error_mark_node)
1129 return error_mark_node;
1130
1131 my_friendly_assert (!integer_zerop (expr), 191);
1132
1133 if (TREE_CODE (type) == RECORD_TYPE
1134 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE
1135 && type != TYPE_MAIN_VARIANT (TREE_TYPE (intype)))
1136 {
1137 tree path;
1138 int distance
1139 = get_base_distance (binfo, TYPE_MAIN_VARIANT (TREE_TYPE (intype)),
1140 0, &path);
1141
1142 /* This function shouldn't be called with unqualified arguments
1143 but if it is, give them an error message that they can read. */
1144 if (distance < 0)
1145 {
1146 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1147 TREE_TYPE (intype), type);
1148
1149 if (distance == -2)
1150 cp_error ("because `%T' is an ambiguous base class", type);
1151 return error_mark_node;
1152 }
1153
1154 return build_vbase_path (PLUS_EXPR, ptr_type, expr, path, 1);
1155 }
1156 rval = build1 (NOP_EXPR, ptr_type,
1157 TREE_CODE (expr) == NOP_EXPR ? TREE_OPERAND (expr, 0) : expr);
1158 TREE_CONSTANT (rval) = TREE_CONSTANT (expr);
1159 return rval;
1160 }
1161
1162 /* Call this when we know (for any reason) that expr is
1163 not, in fact, zero. This routine gets a type out of the first
1164 argument and uses it to search for the type to convert to. If there
1165 is more than one instance of that type in the expr, the conversion is
1166 ambiguous. This routine should eventually go away, and all
1167 callers should use convert_to_pointer_real. */
1168
1169 tree
1170 convert_pointer_to (binfo, expr)
1171 tree binfo, expr;
1172 {
1173 tree type;
1174
1175 if (TREE_CODE (binfo) == TREE_VEC)
1176 type = BINFO_TYPE (binfo);
1177 else if (IS_AGGR_TYPE (binfo))
1178 type = binfo;
1179 else
1180 type = binfo;
1181 return convert_pointer_to_real (type, expr);
1182 }
1183 \f
1184 /* Conversion...
1185
1186 FLAGS indicates how we should behave. */
1187
1188 tree
1189 cp_convert (type, expr, convtype, flags)
1190 tree type, expr;
1191 int convtype, flags;
1192 {
1193 register tree e = expr;
1194 register enum tree_code code = TREE_CODE (type);
1195
1196 if (TREE_CODE (e) == ERROR_MARK
1197 || TREE_CODE (TREE_TYPE (e)) == ERROR_MARK)
1198 return error_mark_node;
1199
1200 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP))
1201 /* We need a new temporary; don't take this shortcut. */;
1202 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
1203 /* Trivial conversion: cv-qualifiers do not matter on rvalues. */
1204 return fold (build1 (NOP_EXPR, type, e));
1205
1206 if (code == VOID_TYPE && (convtype & CONV_STATIC))
1207 return build1 (CONVERT_EXPR, type, e);
1208
1209 #if 0
1210 /* This is incorrect. A truncation can't be stripped this way.
1211 Extensions will be stripped by the use of get_unwidened. */
1212 if (TREE_CODE (e) == NOP_EXPR)
1213 return convert (type, TREE_OPERAND (e, 0));
1214 #endif
1215
1216 /* Just convert to the type of the member. */
1217 if (code == OFFSET_TYPE)
1218 {
1219 type = TREE_TYPE (type);
1220 code = TREE_CODE (type);
1221 }
1222
1223 #if 0
1224 if (code == REFERENCE_TYPE)
1225 return fold (convert_to_reference (type, e, convtype, flags, NULL_TREE));
1226 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1227 e = convert_from_reference (e);
1228 #endif
1229
1230 if (TREE_CODE (e) == OFFSET_REF)
1231 e = resolve_offset_ref (e);
1232
1233 if (TREE_READONLY_DECL_P (e))
1234 e = decl_constant_value (e);
1235
1236 if (INTEGRAL_CODE_P (code))
1237 {
1238 tree intype = TREE_TYPE (e);
1239 /* enum = enum, enum = int, enum = float are all errors. */
1240 if (flag_int_enum_equivalence == 0
1241 && TREE_CODE (type) == ENUMERAL_TYPE
1242 && ARITHMETIC_TYPE_P (intype)
1243 && ! (convtype & CONV_STATIC))
1244 {
1245 cp_pedwarn ("conversion from `%#T' to `%#T'", intype, type);
1246
1247 if (flag_pedantic_errors)
1248 return error_mark_node;
1249 }
1250 if (IS_AGGR_TYPE (intype))
1251 {
1252 tree rval;
1253 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1254 if (rval)
1255 return rval;
1256 if (flags & LOOKUP_COMPLAIN)
1257 cp_error ("`%#T' used where a `%T' was expected", intype, type);
1258 if (flags & LOOKUP_SPECULATIVELY)
1259 return NULL_TREE;
1260 return error_mark_node;
1261 }
1262 if (code == BOOLEAN_TYPE)
1263 {
1264 /* Common Ada/Pascal programmer's mistake. We always warn
1265 about this since it is so bad. */
1266 if (TREE_CODE (expr) == FUNCTION_DECL)
1267 cp_warning ("the address of `%D', will always be `true'", expr);
1268 return truthvalue_conversion (e);
1269 }
1270 return fold (convert_to_integer (type, e));
1271 }
1272 if (code == POINTER_TYPE || code == REFERENCE_TYPE
1273 || TYPE_PTRMEMFUNC_P (type))
1274 return fold (cp_convert_to_pointer (type, e));
1275 if (code == REAL_TYPE)
1276 {
1277 if (IS_AGGR_TYPE (TREE_TYPE (e)))
1278 {
1279 tree rval;
1280 rval = build_type_conversion (CONVERT_EXPR, type, e, 1);
1281 if (rval)
1282 return rval;
1283 else
1284 if (flags & LOOKUP_COMPLAIN)
1285 cp_error ("`%#T' used where a floating point value was expected",
1286 TREE_TYPE (e));
1287 }
1288 return fold (convert_to_real (type, e));
1289 }
1290
1291 /* New C++ semantics: since assignment is now based on
1292 memberwise copying, if the rhs type is derived from the
1293 lhs type, then we may still do a conversion. */
1294 if (IS_AGGR_TYPE_CODE (code))
1295 {
1296 tree dtype = TREE_TYPE (e);
1297 tree ctor = NULL_TREE;
1298 tree conversion = NULL_TREE;
1299
1300 dtype = TYPE_MAIN_VARIANT (dtype);
1301
1302 /* Conversion of object pointers or signature pointers/references
1303 to signature pointers/references. */
1304
1305 if (TYPE_LANG_SPECIFIC (type)
1306 && (IS_SIGNATURE_POINTER (type) || IS_SIGNATURE_REFERENCE (type)))
1307 {
1308 tree constructor = build_signature_pointer_constructor (type, expr);
1309 tree sig_ty = SIGNATURE_TYPE (type);
1310 tree sig_ptr;
1311
1312 if (constructor == error_mark_node)
1313 return error_mark_node;
1314
1315 sig_ptr = get_temp_name (type, 1);
1316 DECL_INITIAL (sig_ptr) = constructor;
1317 CLEAR_SIGNATURE (sig_ty);
1318 cp_finish_decl (sig_ptr, constructor, NULL_TREE, 0, 0);
1319 SET_SIGNATURE (sig_ty);
1320 TREE_READONLY (sig_ptr) = 1;
1321
1322 return sig_ptr;
1323 }
1324
1325 /* Conversion between aggregate types. New C++ semantics allow
1326 objects of derived type to be cast to objects of base type.
1327 Old semantics only allowed this between pointers.
1328
1329 There may be some ambiguity between using a constructor
1330 vs. using a type conversion operator when both apply. */
1331
1332 if (IS_AGGR_TYPE (dtype) && ! DERIVED_FROM_P (type, dtype)
1333 && TYPE_HAS_CONVERSION (dtype))
1334 conversion = build_type_conversion (CONVERT_EXPR, type, e, 1);
1335
1336 if (conversion == error_mark_node)
1337 {
1338 if (flags & LOOKUP_COMPLAIN)
1339 error ("ambiguous pointer conversion");
1340 return conversion;
1341 }
1342
1343 #ifndef NEW_OVER
1344 if (TYPE_HAS_CONSTRUCTOR (complete_type (type)))
1345 #else
1346 if (TYPE_HAS_CONSTRUCTOR (complete_type (type)) && ! conversion)
1347 #endif
1348 ctor = build_method_call (NULL_TREE, ctor_identifier,
1349 build_tree_list (NULL_TREE, e),
1350 TYPE_BINFO (type),
1351 (flags & LOOKUP_NORMAL) | LOOKUP_SPECULATIVELY
1352 | (convtype & CONV_NONCONVERTING ? 0 : LOOKUP_ONLYCONVERTING)
1353 | (flags & LOOKUP_NO_CONVERSION)
1354 | (conversion ? LOOKUP_NO_CONVERSION : 0));
1355
1356 if (ctor == error_mark_node)
1357 {
1358 if (flags & LOOKUP_COMPLAIN)
1359 cp_error ("in conversion to type `%T'", type);
1360 if (flags & LOOKUP_SPECULATIVELY)
1361 return NULL_TREE;
1362 return error_mark_node;
1363 }
1364
1365 if (conversion && ctor)
1366 {
1367 if (flags & LOOKUP_COMPLAIN)
1368 error ("both constructor and type conversion operator apply");
1369 if (flags & LOOKUP_SPECULATIVELY)
1370 return NULL_TREE;
1371 return error_mark_node;
1372 }
1373 else if (conversion)
1374 return conversion;
1375 else if (ctor)
1376 {
1377 ctor = build_cplus_new (type, ctor);
1378 return ctor;
1379 }
1380 }
1381
1382 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
1383 then the it won't be hashed and hence compare as not equal,
1384 even when it is. */
1385 if (code == ARRAY_TYPE
1386 && TREE_TYPE (TREE_TYPE (e)) == TREE_TYPE (type)
1387 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e)), TYPE_DOMAIN (type)))
1388 return e;
1389
1390 if (flags & LOOKUP_COMPLAIN)
1391 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
1392 TREE_TYPE (expr), type);
1393 if (flags & LOOKUP_SPECULATIVELY)
1394 return NULL_TREE;
1395 return error_mark_node;
1396 }
1397
1398 /* Create an expression whose value is that of EXPR,
1399 converted to type TYPE. The TREE_TYPE of the value
1400 is always TYPE. This function implements all reasonable
1401 conversions; callers should filter out those that are
1402 not permitted by the language being compiled. */
1403
1404 tree
1405 convert (type, expr)
1406 tree type, expr;
1407 {
1408 return cp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
1409 }
1410
1411 /* Like convert, except permit conversions to take place which
1412 are not normally allowed due to access restrictions
1413 (such as conversion from sub-type to private super-type). */
1414
1415 tree
1416 convert_force (type, expr, convtype)
1417 tree type;
1418 tree expr;
1419 int convtype;
1420 {
1421 register tree e = expr;
1422 register enum tree_code code = TREE_CODE (type);
1423
1424 if (code == REFERENCE_TYPE)
1425 return fold (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1426 NULL_TREE));
1427 else if (TREE_CODE (TREE_TYPE (e)) == REFERENCE_TYPE)
1428 e = convert_from_reference (e);
1429
1430 if (code == POINTER_TYPE)
1431 return fold (convert_to_pointer_force (type, e));
1432
1433 /* From typeck.c convert_for_assignment */
1434 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1435 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1436 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1437 || integer_zerop (e)
1438 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1439 && TYPE_PTRMEMFUNC_P (type))
1440 {
1441 /* compatible pointer to member functions. */
1442 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1);
1443 }
1444
1445 return cp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1446 }
1447
1448 /* Subroutine of build_type_conversion. */
1449
1450 static tree
1451 build_type_conversion_1 (xtype, basetype, expr, typename, for_sure)
1452 tree xtype, basetype;
1453 tree expr;
1454 tree typename;
1455 int for_sure;
1456 {
1457 tree rval;
1458 int flags;
1459
1460 if (for_sure == 0)
1461 flags = LOOKUP_PROTECT|LOOKUP_ONLYCONVERTING;
1462 else
1463 flags = LOOKUP_NORMAL|LOOKUP_ONLYCONVERTING;
1464
1465 rval = build_method_call (expr, typename, NULL_TREE, NULL_TREE, flags);
1466 if (rval == error_mark_node)
1467 {
1468 if (for_sure == 0)
1469 return NULL_TREE;
1470 return error_mark_node;
1471 }
1472
1473 if (IS_AGGR_TYPE (TREE_TYPE (rval)))
1474 return rval;
1475
1476 if (warn_cast_qual
1477 && TREE_TYPE (xtype)
1478 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (rval)))
1479 > TREE_READONLY (TREE_TYPE (xtype))))
1480 warning ("user-defined conversion casting away `const'");
1481 return convert (xtype, rval);
1482 }
1483
1484 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1485 exists, return the attempted conversion. This may
1486 return ERROR_MARK_NODE if the conversion is not
1487 allowed (references private members, etc).
1488 If no conversion exists, NULL_TREE is returned.
1489
1490 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1491 to take place immediately. Otherwise, we build a SAVE_EXPR
1492 which can be evaluated if the results are ever needed.
1493
1494 Changes to this functions should be mirrored in user_harshness.
1495
1496 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1497 object parameter, or by the second standard conversion sequence if
1498 that doesn't do it. This will probably wait for an overloading rewrite.
1499 (jason 8/9/95) */
1500
1501 tree
1502 build_type_conversion (code, xtype, expr, for_sure)
1503 enum tree_code code;
1504 tree xtype, expr;
1505 int for_sure;
1506 {
1507 #ifdef NEW_OVER
1508 return build_user_type_conversion
1509 (xtype, expr, for_sure ? LOOKUP_NORMAL : 0);
1510 #else
1511 /* C++: check to see if we can convert this aggregate type
1512 into the required type. */
1513 tree basetype;
1514 tree conv;
1515 tree winner = NULL_TREE;
1516
1517 if (expr == error_mark_node)
1518 return error_mark_node;
1519
1520 basetype = TREE_TYPE (expr);
1521 if (TREE_CODE (basetype) == REFERENCE_TYPE)
1522 basetype = TREE_TYPE (basetype);
1523
1524 basetype = TYPE_MAIN_VARIANT (basetype);
1525 if (! TYPE_LANG_SPECIFIC (basetype) || ! TYPE_HAS_CONVERSION (basetype))
1526 return NULL_TREE;
1527
1528 /* Do we have an exact match? */
1529 {
1530 tree typename = build_typename_overload (xtype);
1531 if (lookup_fnfields (TYPE_BINFO (basetype), typename, 0))
1532 return build_type_conversion_1 (xtype, basetype, expr, typename,
1533 for_sure);
1534 }
1535
1536 /* Nope; try looking for others. */
1537 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1538 {
1539 tree cand = TREE_VALUE (conv);
1540
1541 if (winner && winner == cand)
1542 continue;
1543
1544 if (can_convert (xtype, TREE_TYPE (TREE_TYPE (cand))))
1545 {
1546 if (winner)
1547 {
1548 if (for_sure)
1549 {
1550 cp_error ("ambiguous conversion from `%T' to `%T'", basetype,
1551 xtype);
1552 cp_error (" candidate conversions include `%D' and `%D'",
1553 winner, cand);
1554 }
1555 return NULL_TREE;
1556 }
1557 else
1558 winner = cand;
1559 }
1560 }
1561
1562 if (winner)
1563 return build_type_conversion_1 (xtype, basetype, expr,
1564 DECL_NAME (winner), for_sure);
1565
1566 return NULL_TREE;
1567 #endif
1568 }
1569
1570 /* Convert the given EXPR to one of a group of types suitable for use in an
1571 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1572 which indicates which types are suitable. If COMPLAIN is 1, complain
1573 about ambiguity; otherwise, the caller will deal with it. */
1574
1575 tree
1576 build_expr_type_conversion (desires, expr, complain)
1577 int desires;
1578 tree expr;
1579 int complain;
1580 {
1581 tree basetype = TREE_TYPE (expr);
1582 tree conv;
1583 tree winner = NULL_TREE;
1584
1585 if (TREE_CODE (basetype) == OFFSET_TYPE)
1586 expr = resolve_offset_ref (expr);
1587 expr = convert_from_reference (expr);
1588 basetype = TREE_TYPE (expr);
1589
1590 if (! IS_AGGR_TYPE (basetype))
1591 switch (TREE_CODE (basetype))
1592 {
1593 case INTEGER_TYPE:
1594 if ((desires & WANT_NULL) && TREE_CODE (expr) == INTEGER_CST
1595 && integer_zerop (expr))
1596 return expr;
1597 /* else fall through... */
1598
1599 case BOOLEAN_TYPE:
1600 return (desires & WANT_INT) ? expr : NULL_TREE;
1601 case ENUMERAL_TYPE:
1602 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1603 case REAL_TYPE:
1604 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1605 case POINTER_TYPE:
1606 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1607
1608 case FUNCTION_TYPE:
1609 case ARRAY_TYPE:
1610 return (desires & WANT_POINTER) ? default_conversion (expr)
1611 : NULL_TREE;
1612 default:
1613 return NULL_TREE;
1614 }
1615
1616 if (! TYPE_HAS_CONVERSION (basetype))
1617 return NULL_TREE;
1618
1619 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1620 {
1621 int win = 0;
1622 tree candidate;
1623 tree cand = TREE_VALUE (conv);
1624
1625 if (winner && winner == cand)
1626 continue;
1627
1628 candidate = TREE_TYPE (TREE_TYPE (cand));
1629 if (TREE_CODE (candidate) == REFERENCE_TYPE)
1630 candidate = TREE_TYPE (candidate);
1631
1632 switch (TREE_CODE (candidate))
1633 {
1634 case BOOLEAN_TYPE:
1635 case INTEGER_TYPE:
1636 win = (desires & WANT_INT); break;
1637 case ENUMERAL_TYPE:
1638 win = (desires & WANT_ENUM); break;
1639 case REAL_TYPE:
1640 win = (desires & WANT_FLOAT); break;
1641 case POINTER_TYPE:
1642 win = (desires & WANT_POINTER); break;
1643 }
1644
1645 if (win)
1646 {
1647 if (winner)
1648 {
1649 if (complain)
1650 {
1651 cp_error ("ambiguous default type conversion from `%T'",
1652 basetype);
1653 cp_error (" candidate conversions include `%D' and `%D'",
1654 winner, cand);
1655 }
1656 return error_mark_node;
1657 }
1658 else
1659 winner = cand;
1660 }
1661 }
1662
1663 if (winner)
1664 {
1665 tree type = TREE_TYPE (TREE_TYPE (winner));
1666 if (TREE_CODE (type) == REFERENCE_TYPE)
1667 type = TREE_TYPE (type);
1668 return build_type_conversion_1 (type, basetype, expr,
1669 DECL_NAME (winner), 1);
1670 }
1671
1672 return NULL_TREE;
1673 }
1674
1675 /* Must convert two aggregate types to non-aggregate type.
1676 Attempts to find a non-ambiguous, "best" type conversion.
1677
1678 Return 1 on success, 0 on failure.
1679
1680 @@ What are the real semantics of this supposed to be??? */
1681
1682 int
1683 build_default_binary_type_conversion (code, arg1, arg2)
1684 enum tree_code code;
1685 tree *arg1, *arg2;
1686 {
1687 switch (code)
1688 {
1689 case MULT_EXPR:
1690 case TRUNC_DIV_EXPR:
1691 case CEIL_DIV_EXPR:
1692 case FLOOR_DIV_EXPR:
1693 case ROUND_DIV_EXPR:
1694 case EXACT_DIV_EXPR:
1695 *arg1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1696 *arg2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1697 break;
1698
1699 case TRUNC_MOD_EXPR:
1700 case FLOOR_MOD_EXPR:
1701 case LSHIFT_EXPR:
1702 case RSHIFT_EXPR:
1703 case BIT_AND_EXPR:
1704 case BIT_XOR_EXPR:
1705 case BIT_IOR_EXPR:
1706 *arg1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg1, 0);
1707 *arg2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, *arg2, 0);
1708 break;
1709
1710 case PLUS_EXPR:
1711 {
1712 tree a1, a2, p1, p2;
1713 int wins;
1714
1715 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1716 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1717 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1718 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1719
1720 wins = (a1 && a2) + (a1 && p2) + (p1 && a2);
1721
1722 if (wins > 1)
1723 error ("ambiguous default type conversion for `operator +'");
1724
1725 if (a1 && a2)
1726 *arg1 = a1, *arg2 = a2;
1727 else if (a1 && p2)
1728 *arg1 = a1, *arg2 = p2;
1729 else
1730 *arg1 = p1, *arg2 = a2;
1731 break;
1732 }
1733
1734 case MINUS_EXPR:
1735 {
1736 tree a1, a2, p1, p2;
1737 int wins;
1738
1739 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1740 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1741 p1 = build_expr_type_conversion (WANT_POINTER, *arg1, 0);
1742 p2 = build_expr_type_conversion (WANT_POINTER, *arg2, 0);
1743
1744 wins = (a1 && a2) + (p1 && p2) + (p1 && a2);
1745
1746 if (wins > 1)
1747 error ("ambiguous default type conversion for `operator -'");
1748
1749 if (a1 && a2)
1750 *arg1 = a1, *arg2 = a2;
1751 else if (p1 && p2)
1752 *arg1 = p1, *arg2 = p2;
1753 else
1754 *arg1 = p1, *arg2 = a2;
1755 break;
1756 }
1757
1758 case GT_EXPR:
1759 case LT_EXPR:
1760 case GE_EXPR:
1761 case LE_EXPR:
1762 case EQ_EXPR:
1763 case NE_EXPR:
1764 {
1765 tree a1, a2, p1, p2;
1766 int wins;
1767
1768 a1 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg1, 0);
1769 a2 = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, *arg2, 0);
1770 p1 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg1, 0);
1771 p2 = build_expr_type_conversion (WANT_POINTER | WANT_NULL, *arg2, 0);
1772
1773 wins = (a1 && a2) + (p1 && p2);
1774
1775 if (wins > 1)
1776 cp_error ("ambiguous default type conversion for `%O'", code);
1777
1778 if (a1 && a2)
1779 *arg1 = a1, *arg2 = a2;
1780 else
1781 *arg1 = p1, *arg2 = p2;
1782 break;
1783 }
1784
1785 case TRUTH_ANDIF_EXPR:
1786 case TRUTH_ORIF_EXPR:
1787 *arg1 = convert (boolean_type_node, *arg1);
1788 *arg2 = convert (boolean_type_node, *arg2);
1789 break;
1790
1791 default:
1792 *arg1 = NULL_TREE;
1793 *arg2 = NULL_TREE;
1794 }
1795
1796 if (*arg1 == error_mark_node || *arg2 == error_mark_node)
1797 cp_error ("ambiguous default type conversion for `%O'", code);
1798
1799 if (*arg1 && *arg2)
1800 return 1;
1801
1802 return 0;
1803 }
1804
1805 /* Implements integral promotion (4.1) and float->double promotion. */
1806
1807 tree
1808 type_promotes_to (type)
1809 tree type;
1810 {
1811 int constp, volatilep;
1812
1813 if (type == error_mark_node)
1814 return error_mark_node;
1815
1816 constp = TYPE_READONLY (type);
1817 volatilep = TYPE_VOLATILE (type);
1818 type = TYPE_MAIN_VARIANT (type);
1819
1820 /* bool always promotes to int (not unsigned), even if it's the same
1821 size. */
1822 if (type == boolean_type_node)
1823 type = integer_type_node;
1824
1825 /* Normally convert enums to int, but convert wide enums to something
1826 wider. */
1827 else if (TREE_CODE (type) == ENUMERAL_TYPE
1828 || type == wchar_type_node)
1829 {
1830 int precision = MAX (TYPE_PRECISION (type),
1831 TYPE_PRECISION (integer_type_node));
1832 tree totype = type_for_size (precision, 0);
1833 if (TREE_UNSIGNED (type)
1834 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1835 type = type_for_size (precision, 1);
1836 else
1837 type = totype;
1838 }
1839 else if (C_PROMOTING_INTEGER_TYPE_P (type))
1840 {
1841 /* Traditionally, unsignedness is preserved in default promotions.
1842 Otherwise, retain unsignedness if really not getting bigger. */
1843 if (TREE_UNSIGNED (type)
1844 && (flag_traditional
1845 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
1846 type = unsigned_type_node;
1847 else
1848 type = integer_type_node;
1849 }
1850 else if (type == float_type_node)
1851 type = double_type_node;
1852
1853 return cp_build_type_variant (type, constp, volatilep);
1854 }
1855
1856 /* Work in progress. Ask jason before removing. */
1857
1858 struct z_candidate {
1859 tree fn;
1860 tree convs;
1861 tree second_conv;
1862 int viable;
1863 tree basetype_path;
1864 tree template;
1865 struct z_candidate *next;
1866 };
1867
1868 #define IDENTITY_RANK 0
1869 #define EXACT_RANK 1
1870 #define PROMO_RANK 2
1871 #define STD_RANK 3
1872 #define PBOOL_RANK 4
1873 #define USER_RANK 5
1874 #define ELLIPSIS_RANK 6
1875
1876 #define ICS_RANK(NODE) \
1877 (ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
1878 : ICS_USER_FLAG (NODE) ? USER_RANK \
1879 : ICS_STD_RANK (NODE))
1880
1881 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
1882
1883 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
1884 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
1885
1886 #define USER_CONV_FN(NODE) TREE_OPERAND (NODE, 1)
1887
1888 static struct z_candidate * build_user_type_conversion_1 ();
1889 static tree convert_like ();
1890 static tree build_over_call ();
1891 static struct z_candidate * tourney ();
1892 static void enforce_access ();
1893
1894 int
1895 null_ptr_cst (t)
1896 tree t;
1897 {
1898 return (INTEGRAL_TYPE_P (TREE_TYPE (t)) && integer_zerop (t));
1899 }
1900
1901 tree
1902 build_conv (code, type, from)
1903 enum tree_code code;
1904 tree type, from;
1905 {
1906 tree t = build1 (code, type, from);
1907 int rank = ICS_STD_RANK (from);
1908 switch (code)
1909 {
1910 case PTR_CONV:
1911 case PMEM_CONV:
1912 case BASE_CONV:
1913 case STD_CONV:
1914 if (rank < STD_RANK)
1915 rank = STD_RANK;
1916 break;
1917
1918 case LVALUE_CONV:
1919 case QUAL_CONV:
1920 case RVALUE_CONV:
1921 if (rank < EXACT_RANK)
1922 rank = EXACT_RANK;
1923
1924 default:
1925 break;
1926 }
1927 ICS_STD_RANK (t) = rank;
1928 ICS_USER_FLAG (t) = ICS_USER_FLAG (from);
1929 return t;
1930 }
1931
1932 tree
1933 non_reference (t)
1934 tree t;
1935 {
1936 if (TREE_CODE (t) == REFERENCE_TYPE)
1937 t = TREE_TYPE (t);
1938 return t;
1939 }
1940
1941 /* Returns the standard conversion path (see [conv]) from type FROM to type
1942 TO, if any. For proper handling of null pointer constants, you must
1943 also pass the expression EXPR to convert from. */
1944
1945 tree
1946 standard_conversion (to, from, expr)
1947 tree to, from, expr;
1948 {
1949 enum tree_code fcode, tcode;
1950 tree conv;
1951
1952 fcode = TREE_CODE (from);
1953 tcode = TREE_CODE (to);
1954
1955 conv = build1 (IDENTITY_CONV, from, expr);
1956
1957 if (from == to)
1958 return conv;
1959
1960 if (fcode == FUNCTION_TYPE)
1961 {
1962 from = build_pointer_type (from);
1963 fcode = TREE_CODE (from);
1964 conv = build_conv (LVALUE_CONV, from, conv);
1965 }
1966 else if (fcode == ARRAY_TYPE)
1967 {
1968 from = build_pointer_type (TREE_TYPE (from));
1969 fcode = TREE_CODE (from);
1970 conv = build_conv (LVALUE_CONV, from, conv);
1971 }
1972
1973 if ((tcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (to))
1974 && expr && null_ptr_cst (expr))
1975 {
1976 conv = build_conv (STD_CONV, to, conv);
1977 }
1978 else if (tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1979 {
1980 enum tree_code ufcode = TREE_CODE (TREE_TYPE (from));
1981 enum tree_code utcode = TREE_CODE (TREE_TYPE (to));
1982
1983 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (from)),
1984 TYPE_MAIN_VARIANT (TREE_TYPE (to)), 1))
1985 /* OK for now */;
1986 else if (utcode == VOID_TYPE && ufcode != OFFSET_TYPE
1987 && ufcode != FUNCTION_TYPE)
1988 {
1989 from = build_pointer_type
1990 (cp_build_type_variant (void_type_node,
1991 TYPE_READONLY (TREE_TYPE (from)),
1992 TYPE_VOLATILE (TREE_TYPE (from))));
1993 conv = build_conv (PTR_CONV, from, conv);
1994 }
1995 else if (ufcode == OFFSET_TYPE && utcode == OFFSET_TYPE)
1996 {
1997 tree fbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (from));
1998 tree tbase = TYPE_OFFSET_BASETYPE (TREE_TYPE (to));
1999
2000 if (DERIVED_FROM_P (fbase, tbase)
2001 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (from))),
2002 TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (to))),
2003 1)))
2004 {
2005 from = build_offset_type (tbase, TREE_TYPE (TREE_TYPE (from)));
2006 from = build_pointer_type (from);
2007 conv = build_conv (PMEM_CONV, from, conv);
2008 }
2009 else
2010 return 0;
2011 }
2012 else if (IS_AGGR_TYPE (TREE_TYPE (from))
2013 && IS_AGGR_TYPE (TREE_TYPE (to)))
2014 {
2015 if (DERIVED_FROM_P (TREE_TYPE (to), TREE_TYPE (from)))
2016 {
2017 from = cp_build_type_variant (TREE_TYPE (to),
2018 TYPE_READONLY (TREE_TYPE (from)),
2019 TYPE_VOLATILE (TREE_TYPE (from)));
2020 from = build_pointer_type (from);
2021 conv = build_conv (PTR_CONV, from, conv);
2022 }
2023 else
2024 return 0;
2025 }
2026 else
2027 return 0;
2028
2029 if (! comptypes (from, to, 1))
2030 {
2031 if (! comp_ptr_ttypes (TREE_TYPE (to), TREE_TYPE (from)))
2032 return 0;
2033
2034 from = to;
2035 conv = build_conv (QUAL_CONV, from, conv);
2036 }
2037 }
2038 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
2039 {
2040 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
2041 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
2042 tree fbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fromfn)));
2043 tree tbase = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (tofn)));
2044
2045 if (! DERIVED_FROM_P (fbase, tbase)
2046 || ! comptypes (TREE_TYPE (fromfn), TREE_TYPE (tofn), 1)
2047 || ! compparms (TREE_CHAIN (TYPE_ARG_TYPES (fromfn)),
2048 TREE_CHAIN (TYPE_ARG_TYPES (tofn)), 1)
2049 || TYPE_READONLY (fbase) != TYPE_READONLY (tbase)
2050 || TYPE_VOLATILE (fbase) != TYPE_VOLATILE (tbase))
2051 return 0;
2052
2053 from = cp_build_type_variant (tbase, TYPE_READONLY (fbase),
2054 TYPE_VOLATILE (fbase));
2055 from = build_cplus_method_type (from, TREE_TYPE (fromfn),
2056 TREE_CHAIN (TYPE_ARG_TYPES (fromfn)));
2057 conv = build_conv (PMEM_CONV, from, conv);
2058 }
2059 else if (tcode == BOOLEAN_TYPE)
2060 {
2061 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE
2062 || fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)))
2063 return 0;
2064
2065 conv = build_conv (STD_CONV, to, conv);
2066 if (fcode == POINTER_TYPE || TYPE_PTRMEMFUNC_P (from)
2067 && ICS_STD_RANK (conv) < PBOOL_RANK)
2068 ICS_STD_RANK (conv) = PBOOL_RANK;
2069 }
2070 /* We don't check for ENUMERAL_TYPE here because there are no standard
2071 conversions to enum type. */
2072 else if (tcode == INTEGER_TYPE || tcode == BOOLEAN_TYPE
2073 || tcode == REAL_TYPE)
2074 {
2075 if (! (INTEGRAL_CODE_P (fcode) || fcode == REAL_TYPE))
2076 return 0;
2077 conv = build_conv (STD_CONV, to, conv);
2078
2079 /* Give this a better rank if it's a promotion. */
2080 if (to == type_promotes_to (from)
2081 && ICS_STD_RANK (TREE_OPERAND (conv, 0)) <= PROMO_RANK)
2082 ICS_STD_RANK (conv) = PROMO_RANK;
2083 }
2084 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
2085 && DERIVED_FROM_P (to, from))
2086 conv = build_conv (BASE_CONV, to, conv);
2087 else
2088 return 0;
2089
2090 return conv;
2091 }
2092
2093 /* Returns the conversion path from type FROM to reference type TO for
2094 purposes of reference binding. For lvalue binding, either pass a
2095 reference type to FROM or an lvalue expression to EXPR.
2096
2097 Currently does not distinguish in the generated trees between binding to
2098 an lvalue and a temporary. Should it? */
2099
2100 tree
2101 reference_binding (rto, from, expr)
2102 tree rto, from, expr;
2103 {
2104 tree conv;
2105 int lvalue = 1;
2106 tree to = TREE_TYPE (rto);
2107
2108 if (TREE_CODE (from) == REFERENCE_TYPE)
2109 from = TREE_TYPE (from);
2110 else if (! expr || ! real_lvalue_p (expr))
2111 lvalue = 0;
2112
2113 if (lvalue
2114 && TYPE_READONLY (to) >= TYPE_READONLY (from)
2115 && TYPE_VOLATILE (to) >= TYPE_VOLATILE (from))
2116 {
2117 conv = build1 (IDENTITY_CONV, from, expr);
2118
2119 if (TYPE_MAIN_VARIANT (to) == TYPE_MAIN_VARIANT (from))
2120 conv = build_conv (REF_BIND, rto, conv);
2121 else if (IS_AGGR_TYPE (to) && IS_AGGR_TYPE (from)
2122 && DERIVED_FROM_P (to, from))
2123 {
2124 conv = build_conv (REF_BIND, rto, conv);
2125 ICS_STD_RANK (conv) = STD_RANK;
2126 }
2127 else
2128 conv = NULL_TREE;
2129 }
2130 else
2131 conv = NULL_TREE;
2132
2133 if (! conv && TYPE_READONLY (to) && ! TYPE_VOLATILE (to))
2134 {
2135 conv = standard_conversion
2136 (TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from), expr);
2137 if (conv)
2138 {
2139 conv = build_conv (REF_BIND, rto, conv);
2140
2141 /* Bind directly to a base subobject of a class rvalue. */
2142 if (TREE_CODE (TREE_OPERAND (conv, 0)) == BASE_CONV)
2143 TREE_OPERAND (conv, 0) = TREE_OPERAND (TREE_OPERAND (conv, 0), 0);
2144 }
2145 }
2146
2147 return conv;
2148 }
2149
2150 /* Returns the implicit conversion sequence (see [over.ics]) from type FROM
2151 to type TO. The optional expression EXPR may affect the conversion.
2152 FLAGS are the usual overloading flags. Only LOOKUP_NO_CONVERSION is
2153 significant. */
2154
2155 tree
2156 implicit_conversion (to, from, expr, flags)
2157 tree to, from, expr;
2158 int flags;
2159 {
2160 tree conv;
2161 struct z_candidate *cand;
2162
2163 if (expr && type_unknown_p (expr))
2164 {
2165 expr = instantiate_type (to, expr, 0);
2166 if (expr == error_mark_node)
2167 return 0;
2168 from = TREE_TYPE (expr);
2169 }
2170
2171 if (TREE_CODE (to) == REFERENCE_TYPE)
2172 conv = reference_binding (to, from, expr);
2173 else
2174 conv = standard_conversion
2175 (TYPE_MAIN_VARIANT (non_reference (to)),
2176 TYPE_MAIN_VARIANT (non_reference (from)), expr);
2177
2178 if (conv)
2179 {
2180 if (TREE_CODE (conv) == IDENTITY_CONV && IS_AGGR_TYPE (to)
2181 && (TREE_CODE (from) == REFERENCE_TYPE || (expr && real_lvalue_p (expr))))
2182 conv = build_conv (RVALUE_CONV, to, conv);
2183 }
2184 else if ((IS_AGGR_TYPE (non_reference (from))
2185 || IS_AGGR_TYPE (non_reference (to)))
2186 && (flags & LOOKUP_NO_CONVERSION) == 0)
2187 {
2188 if (TREE_CODE (to) == REFERENCE_TYPE
2189 && TYPE_READONLY (TREE_TYPE (to))
2190 && ! TYPE_VOLATILE (TREE_TYPE (to)))
2191 {
2192 cand = build_user_type_conversion_1
2193 (TYPE_MAIN_VARIANT (TREE_TYPE (to)), expr, LOOKUP_ONLYCONVERTING);
2194 if (cand)
2195 conv = build_conv (REF_BIND, to, cand->second_conv);
2196 }
2197 else
2198 {
2199 cand = build_user_type_conversion_1
2200 (to, expr, LOOKUP_ONLYCONVERTING);
2201 if (cand)
2202 conv = cand->second_conv;
2203 }
2204 }
2205
2206 return conv;
2207 }
2208
2209 /* Create an overload candidate for the function or method FN called with
2210 the argument list ARGLIST and add it to CANDIDATES. FLAGS is passed on
2211 to implicit_conversion. */
2212
2213 static struct z_candidate *
2214 add_function_candidate (candidates, fn, arglist, flags)
2215 struct z_candidate *candidates;
2216 tree fn, arglist;
2217 int flags;
2218 {
2219 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2220 int i, len = list_length (arglist);
2221 tree convs = make_tree_vec (len);
2222 tree parmnode = parmlist;
2223 tree argnode = arglist;
2224 int viable = 1;
2225 struct z_candidate *cand;
2226
2227 /* The `this' and `in_chrg' arguments to constructors are not considered
2228 in overload resolution. */
2229 if (DECL_CONSTRUCTOR_P (fn))
2230 {
2231 parmnode = TREE_CHAIN (parmnode);
2232 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
2233 parmnode = TREE_CHAIN (parmnode);
2234 }
2235
2236 for (i = 0; i < len; ++i)
2237 {
2238 tree arg = TREE_VALUE (argnode);
2239 tree argtype = TREE_TYPE (arg);
2240 tree t;
2241
2242 argtype = cp_build_type_variant
2243 (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
2244
2245 if (parmnode == void_list_node)
2246 break;
2247 else if (parmnode)
2248 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
2249 else
2250 {
2251 t = build1 (IDENTITY_CONV, argtype, arg);
2252 ICS_ELLIPSIS_FLAG (t) = 1;
2253 }
2254
2255 TREE_VEC_ELT (convs, i) = t;
2256 if (! t)
2257 break;
2258
2259 if (parmnode)
2260 parmnode = TREE_CHAIN (parmnode);
2261 argnode = TREE_CHAIN (argnode);
2262 }
2263
2264 if (i < len)
2265 viable = 0;
2266
2267 /* Make sure there are default args for the rest of the parms. */
2268 for (; parmnode && parmnode != void_list_node;
2269 parmnode = TREE_CHAIN (parmnode))
2270 if (! TREE_PURPOSE (parmnode))
2271 {
2272 viable = 0;
2273 break;
2274 }
2275
2276 cand = (struct z_candidate *) oballoc (sizeof (struct z_candidate));
2277
2278 cand->fn = fn;
2279 cand->convs = convs;
2280 cand->second_conv = NULL_TREE;
2281 cand->viable = viable;
2282 cand->basetype_path = NULL_TREE;
2283 cand->template = NULL_TREE;
2284 cand->next = candidates;
2285
2286 return cand;
2287 }
2288
2289 /* Create an overload candidate for the conversion function FN which will
2290 be invoked for expression OBJ, producing a pointer-to-function which
2291 will in turn be called with the argument list ARGLIST, and add it to
2292 CANDIDATES. FLAGS is passed on to implicit_conversion. */
2293
2294 static struct z_candidate *
2295 add_conv_candidate (candidates, fn, obj, arglist)
2296 struct z_candidate *candidates;
2297 tree fn, obj, arglist;
2298 {
2299 tree totype = TREE_TYPE (TREE_TYPE (fn));
2300 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (totype));
2301 int i, len = list_length (arglist) + 1;
2302 tree convs = make_tree_vec (len);
2303 tree parmnode = parmlist;
2304 tree argnode = arglist;
2305 int viable = 1;
2306 struct z_candidate *cand;
2307 int flags = LOOKUP_NORMAL;
2308
2309 for (i = 0; i < len; ++i)
2310 {
2311 tree arg = i == 0 ? obj : TREE_VALUE (argnode);
2312 tree argtype = TREE_TYPE (arg);
2313 tree t;
2314
2315 argtype = cp_build_type_variant
2316 (argtype, TREE_READONLY (arg), TREE_THIS_VOLATILE (arg));
2317
2318 if (i == 0)
2319 t = implicit_conversion (totype, argtype, arg, flags);
2320 else if (parmnode == void_list_node)
2321 break;
2322 else if (parmnode)
2323 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, flags);
2324 else
2325 {
2326 t = build1 (IDENTITY_CONV, argtype, arg);
2327 ICS_ELLIPSIS_FLAG (t) = 1;
2328 }
2329
2330 TREE_VEC_ELT (convs, i) = t;
2331 if (! t)
2332 break;
2333
2334 if (i == 0)
2335 continue;
2336
2337 if (parmnode)
2338 parmnode = TREE_CHAIN (parmnode);
2339 argnode = TREE_CHAIN (argnode);
2340 }
2341
2342 if (i < len)
2343 viable = 0;
2344
2345 for (; parmnode && parmnode != void_list_node;
2346 parmnode = TREE_CHAIN (parmnode))
2347 if (! TREE_PURPOSE (parmnode))
2348 {
2349 viable = 0;
2350 break;
2351 }
2352
2353 cand = (struct z_candidate *) oballoc (sizeof (struct z_candidate));
2354
2355 cand->fn = fn;
2356 cand->convs = convs;
2357 cand->second_conv = NULL_TREE;
2358 cand->viable = viable;
2359 cand->basetype_path = NULL_TREE;
2360 cand->template = NULL_TREE;
2361 cand->next = candidates;
2362
2363 return cand;
2364 }
2365
2366 int
2367 ptr_complete_ob (t)
2368 tree t;
2369 {
2370 return (TREE_CODE (t) == POINTER_TYPE
2371 && TREE_CODE (TREE_TYPE (t)) != OFFSET_TYPE
2372 && TREE_CODE (TREE_TYPE (t)) != FUNCTION_TYPE
2373 && TREE_CODE (TREE_TYPE (t)) != VOID_TYPE
2374 && TYPE_SIZE (complete_type (TREE_TYPE (t))) != NULL_TREE);
2375 }
2376
2377 #define TYPE_PTRMEM_P(NODE) \
2378 (TREE_CODE (NODE) == POINTER_TYPE \
2379 && TREE_CODE (TREE_TYPE (NODE)) == OFFSET_TYPE)
2380 #define TYPE_PTR_P(NODE) \
2381 (TREE_CODE (NODE) == POINTER_TYPE \
2382 && TREE_CODE (TREE_TYPE (NODE)) != OFFSET_TYPE)
2383 #define TYPE_PTROB_P(NODE) \
2384 (TYPE_PTR_P (NODE) && TREE_CODE (TREE_TYPE (NODE)) != FUNCTION_TYPE \
2385 && TREE_CODE (TREE_TYPE (NODE)) != VOID_TYPE)
2386
2387 static struct z_candidate *
2388 build_builtin_candidate (candidates, fnname, type1, type2,
2389 args, argtypes, flags)
2390 struct z_candidate *candidates;
2391 tree fnname, type1, type2, *args, *argtypes;
2392 int flags;
2393
2394 {
2395 tree t, convs;
2396 int viable = 1, i;
2397 struct z_candidate *cand;
2398 tree types[2];
2399
2400 types[0] = type1;
2401 types[1] = type2;
2402
2403 convs = make_tree_vec (args[2] ? 3 : (args[1] ? 2 : 1));
2404
2405 for (i = 0; i < 2; ++i)
2406 {
2407 if (! args[i])
2408 break;
2409
2410 t = implicit_conversion (types[i], argtypes[i], args[i], flags);
2411 if (! t)
2412 {
2413 viable = 0;
2414 /* We need something for printing the candidate. */
2415 t = build1 (IDENTITY_CONV, types[i], NULL_TREE);
2416 }
2417 TREE_VEC_ELT (convs, i) = t;
2418 }
2419
2420 /* For COND_EXPR we rearranged the arguments; undo that now. */
2421 if (args[2])
2422 {
2423 TREE_VEC_ELT (convs, 2) = TREE_VEC_ELT (convs, 1);
2424 TREE_VEC_ELT (convs, 1) = TREE_VEC_ELT (convs, 0);
2425 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], flags);
2426 if (t)
2427 TREE_VEC_ELT (convs, 0) = t;
2428 else
2429 viable = 0;
2430 }
2431
2432 cand = (struct z_candidate *) oballoc (sizeof (struct z_candidate));
2433
2434 cand->fn = fnname;
2435 cand->convs = convs;
2436 cand->second_conv = NULL_TREE;
2437 cand->viable = viable;
2438 cand->basetype_path = NULL_TREE;
2439 cand->template = NULL_TREE;
2440 cand->next = candidates;
2441
2442 return cand;
2443 }
2444
2445 int
2446 is_complete (t)
2447 tree t;
2448 {
2449 return TYPE_SIZE (complete_type (t)) != NULL_TREE;
2450 }
2451
2452 /* Create any builtin operator overload candidates for the operator in
2453 question given the converted operand types TYPE1 and TYPE2. The other
2454 args are passed through from add_builtin_candidates to
2455 build_builtin_candidate. */
2456
2457 static struct z_candidate *
2458 add_builtin_candidate (candidates, code, code2, fnname, type1, type2,
2459 args, argtypes, flags)
2460 struct z_candidate *candidates;
2461 enum tree_code code, code2;
2462 tree fnname, type1, type2, *args, *argtypes;
2463 int flags;
2464 {
2465 switch (code)
2466 {
2467 case POSTINCREMENT_EXPR:
2468 case POSTDECREMENT_EXPR:
2469 args[1] = integer_zero_node;
2470 type2 = integer_type_node;
2471 }
2472
2473 switch (code)
2474 {
2475
2476 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2477 and VQ is either volatile or empty, there exist candidate operator
2478 functions of the form
2479 VQ T& operator++(VQ T&);
2480 T operator++(VQ T&, int);
2481 5 For every pair T, VQ), where T is an enumeration type or an arithmetic
2482 type other than bool, and VQ is either volatile or empty, there exist
2483 candidate operator functions of the form
2484 VQ T& operator--(VQ T&);
2485 T operator--(VQ T&, int);
2486 6 For every pair T, VQ), where T is a cv-qualified or cv-unqualified
2487 complete object type, and VQ is either volatile or empty, there exist
2488 candidate operator functions of the form
2489 T*VQ& operator++(T*VQ&);
2490 T*VQ& operator--(T*VQ&);
2491 T* operator++(T*VQ&, int);
2492 T* operator--(T*VQ&, int); */
2493
2494 case POSTDECREMENT_EXPR:
2495 case PREDECREMENT_EXPR:
2496 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2497 return candidates;
2498 case POSTINCREMENT_EXPR:
2499 case PREINCREMENT_EXPR:
2500 if (ARITHMETIC_TYPE_P (type1) || ptr_complete_ob (type1))
2501 {
2502 type1 = build_reference_type (type1);
2503 break;
2504 }
2505 return candidates;
2506
2507 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
2508 exist candidate operator functions of the form
2509
2510 T& operator*(T*);
2511
2512 8 For every function type T, there exist candidate operator functions of
2513 the form
2514 T& operator*(T*); */
2515
2516 case INDIRECT_REF:
2517 if (TREE_CODE (type1) == POINTER_TYPE
2518 && (ptr_complete_ob (type1)
2519 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2520 break;
2521 return candidates;
2522
2523 /* 9 For every type T, there exist candidate operator functions of the form
2524 T* operator+(T*);
2525
2526 10For every promoted arithmetic type T, there exist candidate operator
2527 functions of the form
2528 T operator+(T);
2529 T operator-(T); */
2530
2531 case CONVERT_EXPR: /* unary + */
2532 if (TREE_CODE (type1) == POINTER_TYPE
2533 && TREE_CODE (TREE_TYPE (type1)) != OFFSET_TYPE)
2534 break;
2535 case NEGATE_EXPR:
2536 if (ARITHMETIC_TYPE_P (type1))
2537 break;
2538 return candidates;
2539
2540 /* 11For every promoted integral type T, there exist candidate operator
2541 functions of the form
2542 T operator~(T); */
2543
2544 case BIT_NOT_EXPR:
2545 if (INTEGRAL_TYPE_P (type1))
2546 break;
2547 return candidates;
2548
2549 /* 12For every quintuple C1, C2, T, CV1, CV2), where C2 is a class type, C1
2550 is the same type as C2 or is a derived class of C2, T is a complete
2551 object type or a function type, and CV1 and CV2 are cv-qualifier-seqs,
2552 there exist candidate operator functions of the form
2553 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2554 where CV12 is the union of CV1 and CV2. */
2555
2556 case MEMBER_REF:
2557 if (TREE_CODE (type1) == POINTER_TYPE
2558 && (TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2)))
2559 {
2560 tree c1 = TREE_TYPE (type1);
2561 tree c2 = (TYPE_PTRMEMFUNC_P (type2)
2562 ? TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (type2))
2563 : TYPE_OFFSET_BASETYPE (TREE_TYPE (type2)));
2564
2565 if (IS_AGGR_TYPE (c1) && DERIVED_FROM_P (c2, c1)
2566 && (TYPE_PTRMEMFUNC_P (type2)
2567 || is_complete (TREE_TYPE (TREE_TYPE (type2)))))
2568 break;
2569 }
2570 return candidates;
2571
2572 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2573 didate operator functions of the form
2574 LR operator*(L, R);
2575 LR operator/(L, R);
2576 LR operator+(L, R);
2577 LR operator-(L, R);
2578 bool operator<(L, R);
2579 bool operator>(L, R);
2580 bool operator<=(L, R);
2581 bool operator>=(L, R);
2582 bool operator==(L, R);
2583 bool operator!=(L, R);
2584 where LR is the result of the usual arithmetic conversions between
2585 types L and R.
2586
2587 14For every pair of types T and I, where T is a cv-qualified or cv-
2588 unqualified complete object type and I is a promoted integral type,
2589 there exist candidate operator functions of the form
2590 T* operator+(T*, I);
2591 T& operator[](T*, I);
2592 T* operator-(T*, I);
2593 T* operator+(I, T*);
2594 T& operator[](I, T*);
2595
2596 15For every T, where T is a pointer to complete object type, there exist
2597 candidate operator functions of the form112)
2598 ptrdiff_t operator-(T, T);
2599
2600 16For every pointer type T, there exist candidate operator functions of
2601 the form
2602 bool operator<(T, T);
2603 bool operator>(T, T);
2604 bool operator<=(T, T);
2605 bool operator>=(T, T);
2606 bool operator==(T, T);
2607 bool operator!=(T, T);
2608
2609 17For every pointer to member type T, there exist candidate operator
2610 functions of the form
2611 bool operator==(T, T);
2612 bool operator!=(T, T); */
2613
2614 case MINUS_EXPR:
2615 if (ptr_complete_ob (type1) && ptr_complete_ob (type2))
2616 break;
2617 if (ptr_complete_ob (type1) && INTEGRAL_TYPE_P (type2))
2618 {
2619 type2 = ptrdiff_type_node;
2620 break;
2621 }
2622 case MULT_EXPR:
2623 case TRUNC_DIV_EXPR:
2624 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2625 break;
2626 return candidates;
2627
2628 case EQ_EXPR:
2629 case NE_EXPR:
2630 if (TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)
2631 || TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2632 break;
2633 if ((TYPE_PTRMEMFUNC_P (type1) || TYPE_PTRMEM_P (type1))
2634 && null_ptr_cst (args[1]))
2635 {
2636 type2 = type1;
2637 break;
2638 }
2639 if ((TYPE_PTRMEMFUNC_P (type2) || TYPE_PTRMEM_P (type2))
2640 && null_ptr_cst (args[0]))
2641 {
2642 type1 = type2;
2643 break;
2644 }
2645 case LT_EXPR:
2646 case GT_EXPR:
2647 case LE_EXPR:
2648 case GE_EXPR:
2649 case MAX_EXPR:
2650 case MIN_EXPR:
2651 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)
2652 || TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2653 break;
2654 if (TYPE_PTR_P (type1) && null_ptr_cst (args[1]))
2655 {
2656 type2 = type1;
2657 break;
2658 }
2659 if (null_ptr_cst (args[0]) && TYPE_PTR_P (type2))
2660 {
2661 type1 = type2;
2662 break;
2663 }
2664 return candidates;
2665
2666 case PLUS_EXPR:
2667 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2668 break;
2669 case ARRAY_REF:
2670 if (INTEGRAL_TYPE_P (type1) && ptr_complete_ob (type2))
2671 {
2672 type1 = ptrdiff_type_node;
2673 break;
2674 }
2675 if (ptr_complete_ob (type1) && INTEGRAL_TYPE_P (type2))
2676 {
2677 type2 = ptrdiff_type_node;
2678 break;
2679 }
2680 return candidates;
2681
2682 /* 18For every pair of promoted integral types L and R, there exist candi-
2683 date operator functions of the form
2684 LR operator%(L, R);
2685 LR operator&(L, R);
2686 LR operator^(L, R);
2687 LR operator|(L, R);
2688 L operator<<(L, R);
2689 L operator>>(L, R);
2690 where LR is the result of the usual arithmetic conversions between
2691 types L and R. */
2692
2693 case TRUNC_MOD_EXPR:
2694 case BIT_AND_EXPR:
2695 case BIT_IOR_EXPR:
2696 case BIT_XOR_EXPR:
2697 case LSHIFT_EXPR:
2698 case RSHIFT_EXPR:
2699 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
2700 break;
2701 return candidates;
2702
2703 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
2704 type, VQ is either volatile or empty, and R is a promoted arithmetic
2705 type, there exist candidate operator functions of the form
2706 VQ L& operator=(VQ L&, R);
2707 VQ L& operator*=(VQ L&, R);
2708 VQ L& operator/=(VQ L&, R);
2709 VQ L& operator+=(VQ L&, R);
2710 VQ L& operator-=(VQ L&, R);
2711
2712 20For every pair T, VQ), where T is any type and VQ is either volatile
2713 or empty, there exist candidate operator functions of the form
2714 T*VQ& operator=(T*VQ&, T*);
2715
2716 21For every pair T, VQ), where T is a pointer to member type and VQ is
2717 either volatile or empty, there exist candidate operator functions of
2718 the form
2719 VQ T& operator=(VQ T&, T);
2720
2721 22For every triple T, VQ, I), where T is a cv-qualified or cv-
2722 unqualified complete object type, VQ is either volatile or empty, and
2723 I is a promoted integral type, there exist candidate operator func-
2724 tions of the form
2725 T*VQ& operator+=(T*VQ&, I);
2726 T*VQ& operator-=(T*VQ&, I);
2727
2728 23For every triple L, VQ, R), where L is an integral or enumeration
2729 type, VQ is either volatile or empty, and R is a promoted integral
2730 type, there exist candidate operator functions of the form
2731
2732 VQ L& operator%=(VQ L&, R);
2733 VQ L& operator<<=(VQ L&, R);
2734 VQ L& operator>>=(VQ L&, R);
2735 VQ L& operator&=(VQ L&, R);
2736 VQ L& operator^=(VQ L&, R);
2737 VQ L& operator|=(VQ L&, R); */
2738
2739 case MODIFY_EXPR:
2740 switch (code2)
2741 {
2742 case PLUS_EXPR:
2743 case MINUS_EXPR:
2744 if (ptr_complete_ob (type1) && INTEGRAL_TYPE_P (type2))
2745 {
2746 type2 = ptrdiff_type_node;
2747 break;
2748 }
2749 case MULT_EXPR:
2750 case TRUNC_DIV_EXPR:
2751 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2752 break;
2753 return candidates;
2754
2755 case TRUNC_MOD_EXPR:
2756 case BIT_AND_EXPR:
2757 case BIT_IOR_EXPR:
2758 case BIT_XOR_EXPR:
2759 case LSHIFT_EXPR:
2760 case RSHIFT_EXPR:
2761 if (INTEGRAL_TYPE_P (type1) && INTEGRAL_TYPE_P (type2))
2762 break;
2763 return candidates;
2764
2765 case NOP_EXPR:
2766 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2767 break;
2768 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2769 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2770 || (TYPE_PTRMEM_P (type1) && TYPE_PTRMEM_P (type2))
2771 || ((TYPE_PTRMEMFUNC_P (type1)
2772 || TREE_CODE (type1) == POINTER_TYPE)
2773 && null_ptr_cst (args[1])))
2774 {
2775 type2 = type1;
2776 break;
2777 }
2778 return candidates;
2779
2780 default:
2781 my_friendly_abort (367);
2782 }
2783 type1 = build_reference_type (type1);
2784 break;
2785
2786 case COND_EXPR:
2787 if (TREE_CODE (type1) == ENUMERAL_TYPE && type1 == type2)
2788 break;
2789 else if (TREE_CODE (type1) == ENUMERAL_TYPE
2790 || TREE_CODE (type2) == ENUMERAL_TYPE)
2791 return candidates;
2792 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2793 break;
2794 if (TREE_CODE (type1) == TREE_CODE (type2)
2795 && (TREE_CODE (type1) == REFERENCE_TYPE
2796 || TREE_CODE (type1) == POINTER_TYPE
2797 || TYPE_PTRMEMFUNC_P (type1)
2798 || IS_AGGR_TYPE (type1)))
2799 break;
2800 if (TREE_CODE (type1) == REFERENCE_TYPE
2801 || TREE_CODE (type2) == REFERENCE_TYPE)
2802 return candidates;
2803 if (((TYPE_PTRMEMFUNC_P (type1) || TREE_CODE (type1) == POINTER_TYPE)
2804 && null_ptr_cst (args[1]))
2805 || IS_AGGR_TYPE (type1))
2806 {
2807 type2 = type1;
2808 break;
2809 }
2810 if (((TYPE_PTRMEMFUNC_P (type2) || TREE_CODE (type2) == POINTER_TYPE)
2811 && null_ptr_cst (args[0]))
2812 || IS_AGGR_TYPE (type2))
2813 {
2814 type1 = type2;
2815 break;
2816 }
2817 return candidates;
2818
2819 default:
2820 my_friendly_abort (367);
2821 }
2822
2823 /* If we're dealing with two pointer types, we need candidates
2824 for both of them. */
2825 if (type2 && type1 != type2
2826 && TREE_CODE (type1) == TREE_CODE (type2)
2827 && (TREE_CODE (type1) == REFERENCE_TYPE
2828 || TREE_CODE (type1) == POINTER_TYPE
2829 || TYPE_PTRMEMFUNC_P (type1)
2830 || IS_AGGR_TYPE (type1)))
2831 {
2832 candidates = build_builtin_candidate
2833 (candidates, fnname, type1, type1, args, argtypes, flags);
2834 return build_builtin_candidate
2835 (candidates, fnname, type2, type2, args, argtypes, flags);
2836 }
2837
2838 return build_builtin_candidate
2839 (candidates, fnname, type1, type2, args, argtypes, flags);
2840 }
2841
2842 tree
2843 type_decays_to (type)
2844 tree type;
2845 {
2846 if (TREE_CODE (type) == ARRAY_TYPE)
2847 return build_pointer_type (TREE_TYPE (type));
2848 if (TREE_CODE (type) == FUNCTION_TYPE)
2849 return build_pointer_type (type);
2850 return type;
2851 }
2852
2853 /* There are three conditions of builtin candidates:
2854
2855 1) bool-taking candidates. These are the same regardless of the input.
2856 2) pointer-pair taking candidates. These are generated for each type
2857 one of the input types converts to.
2858 3) arithmetic candidates. According to the WP, we should generate
2859 all of these, but I'm trying not to... */
2860
2861 static struct z_candidate *
2862 add_builtin_candidates (candidates, code, code2, fnname, args, flags)
2863 struct z_candidate *candidates;
2864 enum tree_code code, code2;
2865 tree fnname, *args;
2866 int flags;
2867 {
2868 int ref1, i;
2869 tree type, argtypes[3], types[2];
2870
2871 for (i = 0; i < 3; ++i)
2872 {
2873 if (args[i])
2874 argtypes[i] = cp_build_type_variant
2875 (TREE_TYPE (args[i]), TREE_READONLY (args[i]),
2876 TREE_THIS_VOLATILE (args[i]));
2877 else
2878 argtypes[i] = NULL_TREE;
2879 }
2880
2881 switch (code)
2882 {
2883 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
2884 and VQ is either volatile or empty, there exist candidate operator
2885 functions of the form
2886 VQ T& operator++(VQ T&); */
2887
2888 case POSTINCREMENT_EXPR:
2889 case PREINCREMENT_EXPR:
2890 case POSTDECREMENT_EXPR:
2891 case PREDECREMENT_EXPR:
2892 case MODIFY_EXPR:
2893 ref1 = 1;
2894 break;
2895
2896 /* 24There also exist candidate operator functions of the form
2897 bool operator!(bool);
2898 bool operator&&(bool, bool);
2899 bool operator||(bool, bool); */
2900
2901 case TRUTH_NOT_EXPR:
2902 return build_builtin_candidate
2903 (candidates, fnname, boolean_type_node,
2904 NULL_TREE, args, argtypes, flags);
2905
2906 case TRUTH_ORIF_EXPR:
2907 case TRUTH_ANDIF_EXPR:
2908 return build_builtin_candidate
2909 (candidates, fnname, boolean_type_node,
2910 boolean_type_node, args, argtypes, flags);
2911
2912 case ADDR_EXPR:
2913 case COMPOUND_EXPR:
2914 case COMPONENT_REF:
2915 return candidates;
2916
2917 default:
2918 ref1 = 0;
2919 }
2920
2921 types[0] = types[1] = NULL_TREE;
2922
2923 for (i = 0; i < 2; ++i)
2924 {
2925 if (! args[i])
2926 ;
2927 else if (IS_AGGR_TYPE (argtypes[i]))
2928 {
2929 tree convs = lookup_conversions (argtypes[i]);
2930
2931 if (code == COND_EXPR)
2932 {
2933 if (real_lvalue_p (args[i]))
2934 types[i] = tree_cons
2935 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2936
2937 types[i] = tree_cons
2938 (NULL_TREE, TYPE_MAIN_VARIANT (argtypes[i]), types[i]);
2939 }
2940
2941 else if (! convs || (i == 0 && code == MODIFY_EXPR))
2942 return candidates;
2943
2944 for (; convs; convs = TREE_CHAIN (convs))
2945 {
2946 type = TREE_TYPE (TREE_TYPE (TREE_VALUE (convs)));
2947
2948 if (i == 0 && ref1
2949 && (TREE_CODE (type) != REFERENCE_TYPE
2950 || TYPE_READONLY (TREE_TYPE (type))))
2951 continue;
2952
2953 if (code == COND_EXPR && TREE_CODE (type) == REFERENCE_TYPE)
2954 types[i] = tree_cons (NULL_TREE, type, types[i]);
2955
2956 type = non_reference (type);
2957 if (i != 0 || ! ref1)
2958 {
2959 type = type_decays_to (TYPE_MAIN_VARIANT (type));
2960 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2961 types[i] = tree_cons (NULL_TREE, type, types[i]);
2962 type = type_promotes_to (type);
2963 }
2964
2965 if (! value_member (type, types[i]))
2966 types[i] = tree_cons (NULL_TREE, type, types[i]);
2967 }
2968 }
2969 else
2970 {
2971 if (code == COND_EXPR && real_lvalue_p (args[i]))
2972 types[i] = tree_cons
2973 (NULL_TREE, build_reference_type (argtypes[i]), types[i]);
2974 type = non_reference (argtypes[i]);
2975 if (i != 0 || ! ref1)
2976 {
2977 type = type_decays_to (TYPE_MAIN_VARIANT (type));
2978 if (code == COND_EXPR && TREE_CODE (type) == ENUMERAL_TYPE)
2979 types[i] = tree_cons (NULL_TREE, type, types[i]);
2980 type = type_promotes_to (type);
2981 }
2982 types[i] = tree_cons (NULL_TREE, type, types[i]);
2983 }
2984 }
2985
2986 for (; types[0]; types[0] = TREE_CHAIN (types[0]))
2987 {
2988 if (types[1])
2989 for (type = types[1]; type; type = TREE_CHAIN (type))
2990 candidates = add_builtin_candidate
2991 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2992 TREE_VALUE (type), args, argtypes, flags);
2993 else
2994 candidates = add_builtin_candidate
2995 (candidates, code, code2, fnname, TREE_VALUE (types[0]),
2996 NULL_TREE, args, argtypes, flags);
2997 }
2998
2999 return candidates;
3000 }
3001
3002 static struct z_candidate *
3003 add_template_candidate (candidates, tmpl, arglist, flags)
3004 struct z_candidate *candidates;
3005 tree tmpl, arglist;
3006 int flags;
3007 {
3008 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
3009 tree *targs = (tree *) alloca (sizeof (tree) * ntparms);
3010 struct z_candidate *cand;
3011 int i, dummy;
3012 tree fn;
3013
3014 i = type_unification (DECL_TEMPLATE_PARMS (tmpl), targs,
3015 TYPE_ARG_TYPES (TREE_TYPE (tmpl)),
3016 arglist, &dummy, 0, 0);
3017 if (i != 0)
3018 return candidates;
3019
3020 fn = instantiate_template (tmpl, targs);
3021 if (fn == error_mark_node)
3022 return candidates;
3023
3024 cand = add_function_candidate (candidates, fn, arglist, flags);
3025 cand->template = DECL_TEMPLATE_INFO (fn);
3026 return cand;
3027 }
3028
3029 static int
3030 any_viable (cands)
3031 struct z_candidate *cands;
3032 {
3033 for (; cands; cands = cands->next)
3034 if (cands->viable)
3035 return 1;
3036 return 0;
3037 }
3038
3039 static struct z_candidate *
3040 splice_viable (cands)
3041 struct z_candidate *cands;
3042 {
3043 struct z_candidate **p = &cands;
3044
3045 for (; *p; )
3046 {
3047 if ((*p)->viable)
3048 p = &((*p)->next);
3049 else
3050 *p = (*p)->next;
3051 }
3052
3053 return cands;
3054 }
3055
3056 tree
3057 build_this (obj)
3058 tree obj;
3059 {
3060 /* Fix this to work on non-lvalues. */
3061 return build_unary_op (ADDR_EXPR, obj, 0);
3062 }
3063
3064 static void
3065 print_z_candidates (candidates)
3066 struct z_candidate *candidates;
3067 {
3068 if (! candidates)
3069 return;
3070
3071 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
3072 {
3073 if (candidates->fn == ansi_opname [COND_EXPR])
3074 cp_error ("candidates are: %D(%T, %T, %T) <builtin>", candidates->fn,
3075 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
3076 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
3077 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
3078 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
3079 cp_error ("candidates are: %D(%T, %T) <builtin>", candidates->fn,
3080 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
3081 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
3082 else
3083 cp_error ("candidates are: %D(%T) <builtin>", candidates->fn,
3084 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
3085 }
3086 else
3087 cp_error_at ("candidates are: %D", candidates->fn);
3088 candidates = candidates->next;
3089
3090 for (; candidates; candidates = candidates->next)
3091 {
3092 if (TREE_CODE (candidates->fn) == IDENTIFIER_NODE)
3093 {
3094 if (candidates->fn == ansi_opname [COND_EXPR])
3095 cp_error (" %D(%T, %T, %T) <builtin>",
3096 candidates->fn,
3097 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
3098 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)),
3099 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 2)));
3100 else if (TREE_VEC_LENGTH (candidates->convs) == 2)
3101 cp_error (" %D(%T, %T) <builtin>", candidates->fn,
3102 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)),
3103 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 1)));
3104 else
3105 cp_error (" %D(%T) <builtin>", candidates->fn,
3106 TREE_TYPE (TREE_VEC_ELT (candidates->convs, 0)));
3107 }
3108 else
3109 cp_error_at (" %D", candidates->fn);
3110 }
3111 }
3112
3113 /* Returns the best overload candidate to perform the requested
3114 conversion. */
3115
3116 static struct z_candidate *
3117 build_user_type_conversion_1 (totype, expr, flags)
3118 tree totype, expr;
3119 int flags;
3120 {
3121 struct z_candidate *candidates, *cand;
3122 tree fromtype = TREE_TYPE (expr);
3123 tree ctors = NULL_TREE, convs = NULL_TREE, *p;
3124 tree args;
3125
3126 if (IS_AGGR_TYPE (totype))
3127 ctors = lookup_fnfields (TYPE_BINFO (totype), ctor_identifier, 0);
3128 if (IS_AGGR_TYPE (fromtype)
3129 && (! IS_AGGR_TYPE (totype) || ! DERIVED_FROM_P (totype, fromtype)))
3130 convs = lookup_conversions (fromtype);
3131
3132 candidates = 0;
3133 flags |= LOOKUP_NO_CONVERSION;
3134
3135 if (ctors)
3136 {
3137 ctors = TREE_VALUE (ctors);
3138 args = build_tree_list (NULL_TREE, expr);
3139 }
3140 for (; ctors; ctors = DECL_CHAIN (ctors))
3141 {
3142 if ((flags & LOOKUP_ONLYCONVERTING) && DECL_NONCONVERTING_P (ctors))
3143 continue;
3144
3145 candidates = add_function_candidate (candidates, ctors, args, flags);
3146 candidates->second_conv = build1 (IDENTITY_CONV, totype, NULL_TREE);
3147 candidates->basetype_path = TYPE_BINFO (totype);
3148 }
3149
3150 if (convs)
3151 args = build_tree_list (NULL_TREE, build_this (expr));
3152
3153 for (; convs; convs = TREE_CHAIN (convs))
3154 {
3155 tree fn = TREE_VALUE (convs);
3156 tree ics = implicit_conversion
3157 (totype, TREE_TYPE (TREE_TYPE (fn)), 0, LOOKUP_NO_CONVERSION);
3158 if (ics)
3159 for (; fn; fn = DECL_CHAIN (fn))
3160 {
3161 candidates = add_function_candidate (candidates, fn, args, flags);
3162 candidates->second_conv = ics;
3163 candidates->basetype_path = TREE_PURPOSE (convs);
3164 }
3165 }
3166
3167 if (! any_viable (candidates))
3168 {
3169 #if 0
3170 if (flags & LOOKUP_COMPLAIN)
3171 {
3172 if (candidates && ! candidates->next)
3173 /* say why this one won't work or try to be loose */;
3174 else
3175 cp_error ("no viable candidates");
3176 }
3177 #endif
3178
3179 return 0;
3180 }
3181
3182 candidates = splice_viable (candidates);
3183 cand = tourney (candidates, totype);
3184
3185 if (cand == 0)
3186 {
3187 if (flags & LOOKUP_COMPLAIN)
3188 {
3189 cp_error ("ambiguous user-defined type conversion");
3190 print_z_candidates (candidates);
3191 }
3192
3193 cand = candidates; /* any one will do */
3194 cand->second_conv = build1 (AMBIG_CONV, totype, expr);
3195 ICS_USER_FLAG (cand->second_conv) = 1;
3196
3197 return cand;
3198 }
3199
3200 for (p = &(cand->second_conv); TREE_CODE (*p) != IDENTITY_CONV; )
3201 p = &(TREE_OPERAND (*p, 0));
3202
3203 *p = build
3204 (USER_CONV,
3205 (DECL_CONSTRUCTOR_P (cand->fn)
3206 ? totype : non_reference (TREE_TYPE (TREE_TYPE (cand->fn)))),
3207 NULL_TREE, cand->fn, cand->convs, cand->basetype_path);
3208 ICS_USER_FLAG (cand->second_conv) = 1;
3209
3210 return cand;
3211 }
3212
3213 tree
3214 build_user_type_conversion (totype, expr, flags)
3215 tree totype, expr, flags;
3216 {
3217 struct z_candidate *cand
3218 = build_user_type_conversion_1 (totype, expr, flags);
3219
3220 if (cand)
3221 {
3222 if (TREE_CODE (cand->second_conv) == AMBIG_CONV)
3223 return error_mark_node;
3224 return convert_from_reference (convert_like (cand->second_conv, expr));
3225 }
3226 return NULL_TREE;
3227 }
3228
3229 tree
3230 build_new_function_call (fn, args, obj)
3231 tree fn, args, obj;
3232 {
3233 struct z_candidate *candidates = 0, *cand;
3234
3235 if (obj == NULL_TREE && TREE_CODE (fn) == TREE_LIST)
3236 {
3237 tree t = TREE_VALUE (fn);
3238
3239 for (; t; t = DECL_CHAIN (t))
3240 {
3241 if (TREE_CODE (t) == TEMPLATE_DECL)
3242 candidates = add_template_candidate
3243 (candidates, t, args, LOOKUP_NORMAL);
3244 else
3245 candidates = add_function_candidate
3246 (candidates, t, args, LOOKUP_NORMAL);
3247 }
3248
3249 if (! any_viable (candidates))
3250 {
3251 if (candidates && ! candidates->next)
3252 return build_function_call (candidates->fn, args);
3253 else
3254 cp_error ("no viable candidates");
3255 return error_mark_node;
3256 }
3257 candidates = splice_viable (candidates);
3258 cand = tourney (candidates, NULL_TREE);
3259
3260 if (cand == 0)
3261 {
3262 cp_error ("ambiguous function call");
3263 print_z_candidates (candidates);
3264 return error_mark_node;
3265 }
3266
3267 return build_over_call (cand->fn, cand->convs, args, LOOKUP_NORMAL);
3268 }
3269
3270 return build_function_call (fn, args);
3271 }
3272
3273 tree
3274 build_object_call (obj, args)
3275 tree obj, args;
3276 {
3277 struct z_candidate *candidates = 0, *cand;
3278 tree fns, convs, mem_args, *p;
3279 enum tree_code code2 = NOP_EXPR;
3280 tree type = TREE_TYPE (obj);
3281
3282 fns = lookup_fnfields (TYPE_BINFO (type), ansi_opname [CALL_EXPR], 0);
3283
3284 if (fns)
3285 {
3286 tree fn = TREE_VALUE (fns);
3287 mem_args = tree_cons (NULL_TREE, build_this (obj), args);
3288
3289 for (; fn; fn = DECL_CHAIN (fn))
3290 {
3291 candidates = add_function_candidate
3292 (candidates, fn, mem_args, LOOKUP_NORMAL);
3293 candidates->basetype_path = TREE_PURPOSE (fns);
3294 }
3295 }
3296
3297 convs = lookup_conversions (type);
3298
3299 for (; convs; convs = TREE_CHAIN (convs))
3300 {
3301 tree fn = TREE_VALUE (convs);
3302 tree totype = TREE_TYPE (TREE_TYPE (fn));
3303
3304 if (TREE_CODE (totype) == POINTER_TYPE
3305 && TREE_CODE (TREE_TYPE (totype)) == FUNCTION_TYPE)
3306 {
3307 candidates = add_conv_candidate (candidates, fn, obj, args);
3308 candidates->basetype_path = TREE_PURPOSE (convs);
3309 }
3310 }
3311
3312 if (! any_viable (candidates))
3313 {
3314 cp_error ("no viable candidates");
3315 print_z_candidates (candidates);
3316 return error_mark_node;
3317 }
3318
3319 candidates = splice_viable (candidates);
3320 cand = tourney (candidates, NULL_TREE);
3321
3322 if (cand == 0)
3323 {
3324 cp_error ("ambiguous object call");
3325 print_z_candidates (candidates);
3326 return error_mark_node;
3327 }
3328
3329 if (DECL_NAME (cand->fn) == ansi_opname [CALL_EXPR])
3330 return build_over_call (cand->fn, cand->convs, mem_args, LOOKUP_NORMAL);
3331
3332 obj = convert_like (TREE_VEC_ELT (cand->convs, 0), obj);
3333
3334 /* FIXME */
3335 return build_function_call (obj, args);
3336 }
3337
3338 static void
3339 op_error (code, code2, arg1, arg2, arg3, problem)
3340 enum tree_code code, code2;
3341 tree arg1, arg2, arg3;
3342 char *problem;
3343 {
3344 char * opname
3345 = (code == MODIFY_EXPR ? assignop_tab [code2] : opname_tab [code]);
3346
3347 switch (code)
3348 {
3349 case COND_EXPR:
3350 cp_error ("%s for `%T ? %T : %T'", problem,
3351 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
3352 break;
3353 case POSTINCREMENT_EXPR:
3354 case POSTDECREMENT_EXPR:
3355 cp_error ("%s for `%T%s'", problem, TREE_TYPE (arg1), opname);
3356 break;
3357 case ARRAY_REF:
3358 cp_error ("%s for `%T[%T]'", problem,
3359 TREE_TYPE (arg1), TREE_TYPE (arg2));
3360 break;
3361 default:
3362 if (arg2)
3363 cp_error ("%s for `%T %s %T'", problem,
3364 TREE_TYPE (arg1), opname, TREE_TYPE (arg2));
3365 else
3366 cp_error ("%s for `%s%T'", problem, opname, TREE_TYPE (arg1));
3367 }
3368 }
3369
3370 tree
3371 build_new_op (code, flags, arg1, arg2, arg3)
3372 enum tree_code code;
3373 int flags;
3374 tree arg1, arg2, arg3;
3375 {
3376 struct z_candidate *candidates = 0, *cand;
3377 tree fns, mem_arglist, arglist, fnname, *p;
3378 enum tree_code code2 = NOP_EXPR;
3379
3380 if (arg1 == error_mark_node)
3381 return error_mark_node;
3382
3383 if (code == MODIFY_EXPR)
3384 {
3385 code2 = TREE_CODE (arg3);
3386 arg3 = NULL_TREE;
3387 fnname = ansi_assopname[code2];
3388 }
3389 else
3390 fnname = ansi_opname[code];
3391
3392 switch (code)
3393 {
3394 case NEW_EXPR:
3395 case VEC_NEW_EXPR:
3396 {
3397 tree rval;
3398
3399 arglist = tree_cons (NULL_TREE, arg2, arg3);
3400 if (flags & LOOKUP_GLOBAL)
3401 return build_new_function_call
3402 (lookup_name_nonclass (fnname), arglist, NULL_TREE);
3403
3404 /* FIXME */
3405 rval = build_method_call
3406 (build_indirect_ref (build1 (NOP_EXPR, arg1, error_mark_node),
3407 "new"),
3408 fnname, arglist, NULL_TREE, flags);
3409 if (rval == error_mark_node)
3410 /* User might declare fancy operator new, but invoke it
3411 like standard one. */
3412 return rval;
3413
3414 TREE_TYPE (rval) = arg1;
3415 TREE_CALLS_NEW (rval) = 1;
3416 return rval;
3417 }
3418
3419 case VEC_DELETE_EXPR:
3420 case DELETE_EXPR:
3421 {
3422 tree rval;
3423
3424 if (flags & LOOKUP_GLOBAL)
3425 return build_new_function_call
3426 (lookup_name_nonclass (fnname),
3427 build_tree_list (NULL_TREE, arg1), NULL_TREE);
3428
3429 arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3430
3431 arg1 = TREE_TYPE (arg1);
3432
3433 /* This handles the case where we're trying to delete
3434 X (*a)[10];
3435 a=new X[5][10];
3436 delete[] a; */
3437
3438 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
3439 {
3440 /* Strip off the pointer and the array. */
3441 arg1 = TREE_TYPE (TREE_TYPE (arg1));
3442
3443 while (TREE_CODE (arg1) == ARRAY_TYPE)
3444 arg1 = (TREE_TYPE (arg1));
3445
3446 arg1 = build_pointer_type (arg1);
3447 }
3448
3449 /* FIXME */
3450 rval = build_method_call
3451 (build_indirect_ref (build1 (NOP_EXPR, arg1,
3452 error_mark_node),
3453 NULL_PTR),
3454 fnname, arglist, NULL_TREE, flags);
3455 #if 0
3456 /* This can happen when operator delete is protected. */
3457 my_friendly_assert (rval != error_mark_node, 250);
3458 TREE_TYPE (rval) = void_type_node;
3459 #endif
3460 return rval;
3461 }
3462
3463 case CALL_EXPR:
3464 return build_object_call (arg1, arg2);
3465 }
3466
3467 /* The comma operator can have void args. */
3468 if (TREE_CODE (arg1) == OFFSET_REF)
3469 arg1 = resolve_offset_ref (arg1);
3470 if (arg2 && TREE_CODE (arg2) == OFFSET_REF)
3471 arg2 = resolve_offset_ref (arg2);
3472 if (arg3 && TREE_CODE (arg3) == OFFSET_REF)
3473 arg3 = resolve_offset_ref (arg3);
3474
3475 if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1))
3476 && (! arg2 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2)))
3477 && (! arg3 || ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3))))
3478 return NULL_TREE;
3479
3480 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
3481 arg2 = integer_zero_node;
3482
3483 fns = lookup_name_nonclass (fnname);
3484 /* + Koenig lookup */
3485
3486 if (arg2 && arg3)
3487 arglist = tree_cons (NULL_TREE, arg1, tree_cons
3488 (NULL_TREE, arg2, build_tree_list (NULL_TREE, arg3)));
3489 else if (arg2)
3490 arglist = tree_cons (NULL_TREE, arg1, build_tree_list (NULL_TREE, arg2));
3491 else
3492 arglist = build_tree_list (NULL_TREE, arg1);
3493
3494 if (fns && TREE_CODE (fns) == TREE_LIST)
3495 fns = TREE_VALUE (fns);
3496 for (; fns; fns = DECL_CHAIN (fns))
3497 {
3498 if (TREE_CODE (fns) == TEMPLATE_DECL)
3499 candidates = add_template_candidate (candidates, fns, arglist, flags);
3500 else
3501 candidates = add_function_candidate (candidates, fns, arglist, flags);
3502 }
3503
3504 if (IS_AGGR_TYPE (TREE_TYPE (arg1)))
3505 fns = lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1)), fnname, 0);
3506 else
3507 fns = NULL_TREE;
3508
3509 if (fns)
3510 {
3511 tree fn = TREE_VALUE (fns);
3512 mem_arglist = tree_cons (NULL_TREE, build_this (arg1), TREE_CHAIN (arglist));
3513 for (; fn; fn = DECL_CHAIN (fn))
3514 {
3515 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3516 candidates = add_function_candidate
3517 (candidates, fn, mem_arglist, flags);
3518 else
3519 candidates = add_function_candidate (candidates, fn, arglist, flags);
3520
3521 candidates->basetype_path = TREE_PURPOSE (fns);
3522 }
3523 }
3524
3525 #if 0 /* Don't handle builtin COND_EXPR for now */
3526 if (code != COND_EXPR)
3527 #endif
3528 {
3529 tree args[3];
3530
3531 /* Rearrange the arguments for ?: so that add_builtin_candidate only has
3532 to know about two args; a builtin candidate will always have a first
3533 parameter of type bool. We'll handle that in
3534 build_builtin_candidate. */
3535 if (code == COND_EXPR)
3536 {
3537 args[0] = arg2;
3538 args[1] = arg3;
3539 args[2] = arg1;
3540 }
3541 else
3542 {
3543 args[0] = arg1;
3544 args[1] = arg2;
3545 args[2] = NULL_TREE;
3546 }
3547
3548 candidates = add_builtin_candidates
3549 (candidates, code, code2, fnname, args, flags);
3550 }
3551
3552 if (! any_viable (candidates))
3553 {
3554 switch (code)
3555 {
3556 case POSTINCREMENT_EXPR:
3557 case POSTDECREMENT_EXPR:
3558 /* Look for an `operator++ (int)'. If they didn't have
3559 one, then we fall back to the old way of doing things. */
3560 if (flags & LOOKUP_COMPLAIN)
3561 cp_pedwarn ("no `%D (int)' declared for postfix `%s', trying prefix operator instead",
3562 fnname, opname_tab [code]);
3563 if (code == POSTINCREMENT_EXPR)
3564 code = PREINCREMENT_EXPR;
3565 else
3566 code = PREDECREMENT_EXPR;
3567 return build_new_op (code, flags, arg1, NULL_TREE, NULL_TREE);
3568
3569 /* FIXME */
3570 case ADDR_EXPR:
3571 /*return build_unary_op (code, arg1, 1);*/
3572 case COMPOUND_EXPR:
3573 /*return build (COMPOUND_EXPR, TREE_TYPE (arg2),
3574 break_out_cleanups (arg1), arg2);*/
3575 case COMPONENT_REF:
3576 /*return build_x_arrow (arg1);*/
3577 #if 0 /* Don't handle builtin COND_EXPR for now */
3578 case COND_EXPR:
3579 #endif
3580 return NULL_TREE;
3581 }
3582 if (flags & LOOKUP_COMPLAIN)
3583 {
3584 op_error (code, code2, arg1, arg2, arg3, "no match");
3585 print_z_candidates (candidates);
3586 }
3587 return error_mark_node;
3588 }
3589 candidates = splice_viable (candidates);
3590 cand = tourney (candidates, NULL_TREE);
3591
3592 if (cand == 0)
3593 {
3594 if (flags & LOOKUP_COMPLAIN)
3595 {
3596 op_error (code, code2, arg1, arg2, arg3, "ambiguous overload");
3597 print_z_candidates (candidates);
3598 }
3599 return error_mark_node;
3600 }
3601
3602 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
3603 {
3604 extern int warn_synth;
3605 if (warn_synth
3606 && fnname == ansi_opname[MODIFY_EXPR]
3607 && DECL_ARTIFICIAL (cand->fn)
3608 && candidates->next
3609 && ! candidates->next->next)
3610 {
3611 cp_warning ("using synthesized `%#D' for copy assignment",
3612 cand->fn);
3613 cp_warning_at (" where cfront would use `%#D'",
3614 cand == candidates
3615 ? candidates->next->fn
3616 : candidates->fn);
3617 }
3618
3619 if (DECL_FUNCTION_MEMBER_P (cand->fn))
3620 enforce_access (cand->basetype_path, cand->fn);
3621
3622 return build_over_call
3623 (cand->fn, cand->convs,
3624 TREE_CODE (TREE_TYPE (cand->fn)) == METHOD_TYPE
3625 ? mem_arglist : arglist,
3626 LOOKUP_NORMAL);
3627 }
3628
3629 arg1 = convert_from_reference
3630 (convert_like (TREE_VEC_ELT (cand->convs, 0), arg1));
3631 if (arg2)
3632 arg2 = convert_like (TREE_VEC_ELT (cand->convs, 1), arg2);
3633 if (arg3)
3634 arg3 = convert_like (TREE_VEC_ELT (cand->convs, 2), arg3);
3635
3636 switch (code)
3637 {
3638 case MODIFY_EXPR:
3639 return build_modify_expr (arg1, code2, arg2);
3640
3641 case INDIRECT_REF:
3642 return build_indirect_ref (arg1, "unary *");
3643
3644 case PLUS_EXPR:
3645 case MINUS_EXPR:
3646 case MULT_EXPR:
3647 case TRUNC_DIV_EXPR:
3648 case GT_EXPR:
3649 case LT_EXPR:
3650 case GE_EXPR:
3651 case LE_EXPR:
3652 case EQ_EXPR:
3653 case NE_EXPR:
3654 case MAX_EXPR:
3655 case MIN_EXPR:
3656 case LSHIFT_EXPR:
3657 case RSHIFT_EXPR:
3658 case TRUNC_MOD_EXPR:
3659 case BIT_AND_EXPR:
3660 case BIT_IOR_EXPR:
3661 case BIT_XOR_EXPR:
3662 case TRUTH_ANDIF_EXPR:
3663 case TRUTH_ORIF_EXPR:
3664 return build_binary_op_nodefault (code, arg1, arg2, code);
3665
3666 case CONVERT_EXPR:
3667 case NEGATE_EXPR:
3668 case BIT_NOT_EXPR:
3669 case TRUTH_NOT_EXPR:
3670 case PREINCREMENT_EXPR:
3671 case POSTINCREMENT_EXPR:
3672 case PREDECREMENT_EXPR:
3673 case POSTDECREMENT_EXPR:
3674 return build_unary_op (code, arg1, 1);
3675
3676 case ARRAY_REF:
3677 return build_array_ref (arg1, arg2);
3678
3679 case COND_EXPR:
3680 return build_conditional_expr (arg1, arg2, arg3);
3681
3682 default:
3683 my_friendly_abort (367);
3684 }
3685 }
3686
3687 void
3688 enforce_access (basetype_path, function)
3689 tree basetype_path, function;
3690 {
3691 tree access = compute_access (basetype_path, function);
3692
3693 if (access == access_private_node)
3694 {
3695 cp_error_at ("`%+#D' is %s", function,
3696 TREE_PRIVATE (function) ? "private"
3697 : "from private base class");
3698 error ("within this context");
3699 }
3700 else if (access == access_protected_node)
3701 {
3702 cp_error_at ("`%+#D' %s", function,
3703 TREE_PROTECTED (function) ? "is protected"
3704 : "has protected accessibility");
3705 error ("within this context");
3706 }
3707 }
3708
3709 /* Perform the conversions in CONVS on the expression EXPR. */
3710
3711 static tree
3712 convert_like (convs, expr)
3713 tree convs, expr;
3714 {
3715 switch (TREE_CODE (convs))
3716 {
3717 case USER_CONV:
3718 {
3719 tree fn = TREE_OPERAND (convs, 1);
3720 enforce_access (TREE_OPERAND (convs, 3), fn);
3721 expr = build_over_call
3722 (TREE_OPERAND (convs, 1), TREE_OPERAND (convs, 2),
3723 DECL_CONSTRUCTOR_P (fn) ? expr : build_this (expr), LOOKUP_NORMAL);
3724
3725 /* If this is a constructor or a function returning an aggr type,
3726 we need to build up a TARGET_EXPR. */
3727 if (DECL_CONSTRUCTOR_P (fn)
3728 || IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (fn))))
3729 expr = build_cplus_new (TREE_TYPE (convs), expr);
3730
3731 return expr;
3732 }
3733 case IDENTITY_CONV:
3734 if (type_unknown_p (expr))
3735 expr = instantiate_type (TREE_TYPE (convs), expr, 1);
3736 return expr;
3737 case AMBIG_CONV:
3738 /* Call build_user_type_conversion again for the error. */
3739 return build_user_type_conversion
3740 (TREE_TYPE (convs), TREE_OPERAND (convs, 0), LOOKUP_NORMAL);
3741 };
3742
3743 expr = convert_like (TREE_OPERAND (convs, 0), expr);
3744 if (expr == error_mark_node)
3745 return error_mark_node;
3746
3747 switch (TREE_CODE (convs))
3748 {
3749 case BASE_CONV:
3750 case RVALUE_CONV:
3751 return build_user_type_conversion
3752 (TREE_TYPE (convs), expr, LOOKUP_NORMAL);
3753 case REF_BIND:
3754 return convert_to_reference
3755 (TREE_TYPE (convs), expr,
3756 CONV_IMPLICIT, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION|INDIRECT_BIND,
3757 error_mark_node);
3758 case LVALUE_CONV:
3759 return decay_conversion (expr);
3760 }
3761 return cp_convert (TREE_TYPE (convs), expr, CONV_IMPLICIT,
3762 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
3763 }
3764
3765 static tree
3766 convert_default_arg (type, arg)
3767 tree type, arg;
3768 {
3769 arg = break_out_target_exprs (arg);
3770
3771 if (TREE_CODE (arg) == CONSTRUCTOR)
3772 {
3773 arg = digest_init (type, arg, 0);
3774 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3775 "default argument", 0, 0);
3776 }
3777 else
3778 {
3779 /* This could get clobbered by the following call. */
3780 if (TREE_HAS_CONSTRUCTOR (arg))
3781 arg = copy_node (arg);
3782
3783 arg = convert_for_initialization (0, type, arg, LOOKUP_NORMAL,
3784 "default argument", 0, 0);
3785 #ifdef PROMOTE_PROTOTYPES
3786 if ((TREE_CODE (type) == INTEGER_TYPE
3787 || TREE_CODE (type) == ENUMERAL_TYPE)
3788 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3789 arg = default_conversion (arg);
3790 #endif
3791 }
3792
3793 return arg;
3794 }
3795
3796 static tree
3797 build_over_call (fn, convs, args, flags)
3798 tree fn, convs, args;
3799 int flags;
3800 {
3801 tree converted_args = NULL_TREE;
3802 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
3803 tree conv, arg, val;
3804 int i = 0;
3805
3806 if (args && TREE_CODE (args) != TREE_LIST)
3807 args = build_tree_list (NULL_TREE, args);
3808 arg = args;
3809
3810 if (DECL_CONSTRUCTOR_P (fn))
3811 {
3812 tree t = build_int_2 (0, 0);
3813 TREE_TYPE (t) = build_pointer_type (DECL_CONTEXT (fn));
3814 converted_args = tree_cons (NULL_TREE, t, converted_args);
3815 parm = TREE_CHAIN (parm);
3816
3817 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
3818 {
3819 converted_args = tree_cons
3820 (NULL_TREE, integer_one_node, converted_args);
3821 parm = TREE_CHAIN (parm);
3822 }
3823 }
3824 /* Bypass access control for 'this' parameter. */
3825 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
3826 {
3827 converted_args = tree_cons
3828 (NULL_TREE, convert_force (TREE_VALUE (parm), TREE_VALUE (arg), CONV_C_CAST),
3829 converted_args);
3830 parm = TREE_CHAIN (parm);
3831 arg = TREE_CHAIN (arg);
3832 ++i;
3833 }
3834
3835 for (; conv = TREE_VEC_ELT (convs, i), arg && parm;
3836 parm = TREE_CHAIN (parm), arg = TREE_CHAIN (arg), ++i)
3837 {
3838 tree type = TREE_VALUE (parm);
3839 val = convert_like (conv, TREE_VALUE (arg));
3840
3841 #ifdef PROMOTE_PROTOTYPES
3842 if ((TREE_CODE (type) == INTEGER_TYPE
3843 || TREE_CODE (type) == ENUMERAL_TYPE)
3844 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3845 val = default_conversion (val);
3846 #endif
3847 converted_args = tree_cons (NULL_TREE, val, converted_args);
3848 }
3849
3850 /* Default arguments */
3851 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm))
3852 converted_args = tree_cons
3853 (NULL_TREE,
3854 convert_default_arg (TREE_VALUE (parm), TREE_PURPOSE (parm)),
3855 converted_args);
3856
3857 /* Ellipsis */
3858 for (; arg; arg = TREE_CHAIN (arg))
3859 {
3860 val = TREE_VALUE (arg);
3861
3862 if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
3863 && (TYPE_PRECISION (TREE_TYPE (val))
3864 < TYPE_PRECISION (double_type_node)))
3865 /* Convert `float' to `double'. */
3866 val = convert (double_type_node, val);
3867 else if (TYPE_LANG_SPECIFIC (TREE_TYPE (val))
3868 && ! TYPE_HAS_TRIVIAL_INIT_REF (TREE_TYPE (val)))
3869 cp_warning ("cannot pass objects of type `%T' through `...'",
3870 TREE_TYPE (val));
3871 else
3872 /* Convert `short' and `char' to full-size `int'. */
3873 val = default_conversion (val);
3874
3875 converted_args = tree_cons (NULL_TREE, val, converted_args);
3876 }
3877
3878 converted_args = nreverse (converted_args);
3879
3880 mark_used (fn);
3881 /* Is it a synthesized method that needs to be synthesized? */
3882 if (DECL_ARTIFICIAL (fn) && ! DECL_INITIAL (fn)
3883 /* Kludge: don't synthesize for default args. */
3884 && current_function_decl)
3885 synthesize_method (fn);
3886
3887 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
3888 {
3889 tree t = build_pointer_type (TREE_TYPE (fn));
3890 fn = build_vfn_ref (&TREE_VALUE (converted_args),
3891 build_indirect_ref (TREE_VALUE (args), 0),
3892 DECL_VINDEX (fn));
3893 TREE_TYPE (fn) = t;
3894 }
3895 else if (DECL_INLINE (fn))
3896 fn = inline_conversion (fn);
3897 else
3898 fn = build_addr_func (fn);
3899
3900 fn = build_call (fn, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))), converted_args);
3901 if (TREE_TYPE (fn) == void_type_node)
3902 return fn;
3903 return convert_from_reference (require_complete_type (fn));
3904 }
3905
3906 /* Compare two implicit conversion sequences that differ only in their
3907 qualification conversion. Subroutine of compare_ics. */
3908
3909 static int
3910 compare_qual (ics1, ics2)
3911 tree ics1, ics2;
3912 {
3913 tree to1 = TREE_TYPE (ics1);
3914 tree to2 = TREE_TYPE (ics2);
3915
3916 to1 = TREE_TYPE (to1);
3917 to2 = TREE_TYPE (to2);
3918
3919 if (TREE_CODE (to1) == OFFSET_TYPE)
3920 {
3921 to1 = TREE_TYPE (to1);
3922 to2 = TREE_TYPE (to2);
3923 }
3924
3925 if (TYPE_READONLY (to1) >= TYPE_READONLY (to2)
3926 && TYPE_VOLATILE (to1) > TYPE_VOLATILE (to2))
3927 return -1;
3928 else if (TYPE_READONLY (to1) > TYPE_READONLY (to2)
3929 && TYPE_VOLATILE (to1) == TYPE_VOLATILE (to2))
3930 return -1;
3931 else if (TYPE_READONLY (to1) <= TYPE_READONLY (to2)
3932 && TYPE_VOLATILE (to1) < TYPE_VOLATILE (to2))
3933 return 1;
3934 else if (TYPE_READONLY (to1) < TYPE_READONLY (to2)
3935 && TYPE_VOLATILE (to1) == TYPE_VOLATILE (to2))
3936 return 1;
3937 return 0;
3938 }
3939
3940 /* Compare two implicit conversion sequences according to the rules set out in
3941 [over.ics.rank]. Return values:
3942
3943 1: ics1 is better than ics2
3944 -1: ics2 is better than ics1
3945 0: ics1 and ics2 are indistinguishable */
3946
3947 static int
3948 compare_ics (ics1, ics2)
3949 tree ics1, ics2;
3950 {
3951 tree main1, main2;
3952
3953 if (ICS_RANK (ics1) > ICS_RANK (ics2))
3954 return -1;
3955 else if (ICS_RANK (ics1) < ICS_RANK (ics2))
3956 return 1;
3957
3958 /* User-defined conversion sequence U1 is a better conversion sequence
3959 than another user-defined conversion sequence U2 if they contain the
3960 same user-defined conversion operator or constructor and if the sec-
3961 ond standard conversion sequence of U1 is better than the second
3962 standard conversion sequence of U2. */
3963
3964 if (ICS_RANK (ics1) == USER_RANK)
3965 {
3966 tree t1, t2;
3967
3968 for (t1 = ics1; TREE_CODE (t1) != USER_CONV; t1 = TREE_OPERAND (t1, 0))
3969 if (TREE_CODE (t1) == AMBIG_CONV)
3970 return 0;
3971 for (t2 = ics2; TREE_CODE (t2) != USER_CONV; t2 = TREE_OPERAND (t2, 0))
3972 if (TREE_CODE (t2) == AMBIG_CONV)
3973 return 0;
3974
3975 if (USER_CONV_FN (t1) != USER_CONV_FN (t2))
3976 return 0;
3977 else if (ICS_STD_RANK (ics1) > ICS_STD_RANK (ics2))
3978 return -1;
3979 else if (ICS_STD_RANK (ics1) < ICS_STD_RANK (ics2))
3980 return 1;
3981
3982 /* else fall through */
3983 }
3984
3985 #if 0 /* Handled by ranking */
3986 /* A conversion that is not a conversion of a pointer, or pointer to
3987 member, to bool is better than another conversion that is such a
3988 conversion. */
3989 #endif
3990
3991 if (TREE_CODE (ics1) == QUAL_CONV)
3992 main1 = TREE_OPERAND (ics1, 0);
3993 else
3994 main1 = ics1;
3995
3996 if (TREE_CODE (ics2) == QUAL_CONV)
3997 main2 = TREE_OPERAND (ics2, 0);
3998 else
3999 main2 = ics2;
4000
4001 if (TREE_CODE (main1) != TREE_CODE (main2))
4002 return 0;
4003
4004 if (TREE_CODE (main1) == IDENTITY_CONV
4005 && (TREE_CODE (TREE_TYPE (main1)) == POINTER_TYPE
4006 || TYPE_PTRMEMFUNC_P (TREE_TYPE (main1))))
4007 {
4008 if (TREE_TYPE (main1) == TREE_TYPE (main2))
4009 return compare_qual (ics1, ics2);
4010
4011 #if 0 /* This is now handled by making identity better than anything else. */
4012 /* existing practice, not WP-endorsed: const char * -> const char *
4013 is better than char * -> const char *. (jason 6/29/96) */
4014 if (TREE_TYPE (ics1) == TREE_TYPE (ics2))
4015 return -compare_qual (main1, main2);
4016 #endif
4017 }
4018
4019 if (TREE_CODE (main1) == PTR_CONV || TREE_CODE (main1) == PMEM_CONV
4020 || TREE_CODE (main1) == REF_BIND || TREE_CODE (main1) == BASE_CONV)
4021 {
4022 tree to1 = TREE_TYPE (main1);
4023 tree from1 = TREE_TYPE (TREE_OPERAND (main1, 0));
4024 tree to2 = TREE_TYPE (main2);
4025 tree from2 = TREE_TYPE (TREE_OPERAND (main2, 0));
4026 int distf, distt;
4027
4028 /* Standard conversion sequence S1 is a better conversion sequence than
4029 standard conversion sequence S2 if...
4030
4031 S1 and S2 differ only in their qualification conversion and they
4032 yield types identical except for cv-qualifiers and S2 adds all the
4033 qualifiers that S1 adds (and in the same places) and S2 adds yet
4034 more cv-qualifiers than S1, or the similar case with reference
4035 binding15). */
4036 if (TREE_CODE (main1) == REF_BIND)
4037 {
4038 if (TYPE_MAIN_VARIANT (TREE_TYPE (to1))
4039 == TYPE_MAIN_VARIANT (TREE_TYPE (to2)))
4040 return compare_qual (ics1, ics2);
4041 }
4042 else if (TREE_CODE (main1) != BASE_CONV && from1 == from2 && to1 == to2)
4043 return compare_qual (ics1, ics2);
4044
4045 if (TYPE_PTRMEMFUNC_P (to1))
4046 {
4047 to1 = TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (to1));
4048 from1 = TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (from1));
4049 }
4050 else if (TREE_CODE (main1) != BASE_CONV)
4051 {
4052 to1 = TREE_TYPE (to1);
4053 if (TREE_CODE (main1) != REF_BIND)
4054 from1 = TREE_TYPE (from1);
4055
4056 if (TREE_CODE (to1) == OFFSET_TYPE)
4057 {
4058 to1 = TYPE_OFFSET_BASETYPE (to1);
4059 from1 = TYPE_OFFSET_BASETYPE (from1);
4060 }
4061 }
4062
4063 if (TYPE_PTRMEMFUNC_P (to2))
4064 {
4065 to2 = TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (to2));
4066 from2 = TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (from2));
4067 }
4068 else if (TREE_CODE (main1) != BASE_CONV)
4069 {
4070 to2 = TREE_TYPE (to2);
4071 if (TREE_CODE (main1) != REF_BIND)
4072 from2 = TREE_TYPE (from2);
4073
4074 if (TREE_CODE (to2) == OFFSET_TYPE)
4075 {
4076 to2 = TYPE_OFFSET_BASETYPE (to2);
4077 from2 = TYPE_OFFSET_BASETYPE (from2);
4078 }
4079 }
4080
4081 if (! (IS_AGGR_TYPE (from1) && IS_AGGR_TYPE (from2)))
4082 return 0;
4083
4084 /* The sense of pmem conversions is reversed from that of the other
4085 conversions. */
4086 if (TREE_CODE (main1) == PMEM_CONV)
4087 {
4088 tree t = from1; from1 = from2; from2 = t;
4089 t = to1; to1 = to2; to2 = t;
4090 }
4091
4092 distf = get_base_distance (from1, from2, 0, 0);
4093 if (distf == -1)
4094 {
4095 distf = -get_base_distance (from2, from1, 0, 0);
4096 if (distf == 1)
4097 return 0;
4098 }
4099
4100 /* If class B is derived directly or indirectly from class A,
4101 conver- sion of B* to A* is better than conversion of B* to
4102 void*, and conversion of A* to void* is better than
4103 conversion of B* to void*. */
4104
4105 if (TREE_CODE (to1) == VOID_TYPE && TREE_CODE (to2) == VOID_TYPE)
4106 {
4107 if (distf > 0)
4108 return 1;
4109 else if (distf < 0)
4110 return -1;
4111 }
4112 else if (TREE_CODE (to2) == VOID_TYPE && IS_AGGR_TYPE (to1)
4113 && get_base_distance (to1, from1, 0, 0) != -1)
4114 return 1;
4115 else if (TREE_CODE (to1) == VOID_TYPE && IS_AGGR_TYPE (to2)
4116 && get_base_distance (to2, from2, 0, 0) != -1)
4117 return -1;
4118
4119 if (! (IS_AGGR_TYPE (to1) && IS_AGGR_TYPE (to2)))
4120 return 0;
4121
4122 /* If class B is derived directly or indirectly from class A and class
4123 C is derived directly or indirectly from B */
4124
4125 distt = get_base_distance (to1, to2, 0, 0);
4126 if (distt == -1)
4127 {
4128 distt = -get_base_distance (to2, to1, 0, 0);
4129 if (distt == 1)
4130 return 0;
4131 }
4132
4133 /* --conversion of C* to B* is better than conversion of C* to A*, */
4134 if (distf == 0)
4135 {
4136 if (distt > 0)
4137 return -1;
4138 else if (distt < 0)
4139 return 1;
4140 }
4141 /* --conversion of B* to A* is better than conversion of C* to A*, */
4142 else if (distt == 0)
4143 {
4144 if (distf > 0)
4145 return 1;
4146 else if (distf < 0)
4147 return -1;
4148 }
4149 }
4150 return 0;
4151 }
4152
4153 /* Compare two candidates for overloading as described in
4154 [over.match.best]. Return values:
4155
4156 1: cand1 is better than cand2
4157 -1: cand2 is better than cand1
4158 0: cand1 and cand2 are indistinguishable */
4159
4160 static int
4161 joust (cand1, cand2)
4162 struct z_candidate *cand1, *cand2;
4163 {
4164 int winner = 0;
4165 int i;
4166
4167 /* a viable function F1
4168 is defined to be a better function than another viable function F2 if
4169 for all arguments i, ICSi(F1) is not a worse conversion sequence than
4170 ICSi(F2), and then */
4171
4172 /* for some argument j, ICSj(F1) is a better conversion sequence than
4173 ICSj(F2) */
4174
4175 for (i = 0; i < TREE_VEC_LENGTH (cand1->convs); ++i)
4176 {
4177 int comp = compare_ics (TREE_VEC_ELT (cand1->convs, i),
4178 TREE_VEC_ELT (cand2->convs, i));
4179
4180 if (comp != 0)
4181 {
4182 if (winner && comp != winner)
4183 return 0;
4184 winner = comp;
4185 }
4186 }
4187
4188 if (winner)
4189 return winner;
4190
4191 /* or, if not that,
4192 F1 is a non-template function and F2 is a template function */
4193
4194 if (! cand1->template && cand2->template)
4195 return 1;
4196 else if (cand1->template && ! cand2->template)
4197 return -1;
4198 else if (cand1->template && cand2->template)
4199 winner = more_specialized
4200 (TI_TEMPLATE (cand1->template), TI_TEMPLATE (cand2->template));
4201
4202 /* or, if not that,
4203 the context is an initialization by user-defined conversion (see
4204 _dcl.init_ and _over.match.user_) and the standard conversion
4205 sequence from the return type of F1 to the destination type (i.e.,
4206 the type of the entity being initialized) is a better conversion
4207 sequence than the standard conversion sequence from the return type
4208 of F2 to the destination type. */
4209
4210 if (! winner && cand1->second_conv)
4211 winner = compare_ics (cand1->second_conv, cand2->second_conv);
4212
4213 /* If the built-in candidates are the same, arbitrarily pick one. */
4214 if (! winner && cand1->fn == cand2->fn
4215 && TREE_CODE (cand1->fn) == IDENTIFIER_NODE)
4216 {
4217 for (i = 0; i < TREE_VEC_LENGTH (cand1->convs); ++i)
4218 if (! comptypes (TREE_TYPE (TREE_VEC_ELT (cand1->convs, i)),
4219 TREE_TYPE (TREE_VEC_ELT (cand2->convs, i)), 1))
4220 break;
4221 if (i == TREE_VEC_LENGTH (cand1->convs))
4222 return 1;
4223 }
4224
4225 return winner;
4226 }
4227
4228 /* Given a list of candidates for overloading, find the best one, if any.
4229 This algorithm has a worst case of O(2n) (winner is last), and a best
4230 case of O(n/2) (totally ambiguous); much better than a sorting
4231 algorithm. */
4232
4233 static struct z_candidate *
4234 tourney (candidates)
4235 struct z_candidate *candidates;
4236 {
4237 struct z_candidate *champ = candidates, *challenger;
4238 int fate;
4239
4240 /* Walk through the list once, comparing each current champ to the next
4241 candidate, knocking out a candidate or two with each comparison. */
4242
4243 for (challenger = champ->next; challenger; )
4244 {
4245 fate = joust (champ, challenger);
4246 if (fate == 1)
4247 challenger = challenger->next;
4248 else
4249 {
4250 if (fate == 0)
4251 {
4252 champ = challenger->next;
4253 if (champ == 0)
4254 return 0;
4255 }
4256 else
4257 champ = challenger;
4258
4259 challenger = champ->next;
4260 }
4261 }
4262
4263 /* Make sure the champ is better than all the candidates it hasn't yet
4264 been compared to. This may do one more comparison than necessary. Oh
4265 well. */
4266
4267 for (challenger = candidates; challenger != champ;
4268 challenger = challenger->next)
4269 {
4270 fate = joust (champ, challenger);
4271 if (fate != 1)
4272 return 0;
4273 }
4274
4275 return champ;
4276 }
This page took 0.229942 seconds and 5 git commands to generate.