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)
5 This file is part of GNU CC.
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)
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.
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. */
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. */
36 #define NULL (char *)0
38 tree
build_user_type_conversion ();
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.
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.
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.
57 C++: in multiple-inheritance, converting between pointers may involve
58 adjusting them by a delta stored within the class definition. */
60 /* Subroutines of `convert'. */
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. */
68 build_thunk (offset
, real_addr
)
69 tree offset
, real_addr
;
71 if (TREE_CODE (real_addr
) != ADDR_EXPR
72 || TREE_CODE (TREE_OPERAND (real_addr
, 0)) != FUNCTION_DECL
)
74 sorry ("MI pointer to member conversion too complex");
75 return error_mark_node
;
77 sorry ("MI pointer to member conversion too complex");
78 return error_mark_node
;
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. */
86 convert_fn_ptr (type
, expr
)
89 #if 0 /* We don't use thunks for pmfs. */
90 if (flag_vtable_thunks
)
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
)
97 error (" in pointer to member conversion");
98 return error_mark_node
;
100 if (binfo
== NULL_TREE
)
102 /* ARM 4.8 restriction. */
103 error ("invalid pointer to member conversion");
104 return error_mark_node
;
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
));
113 return build_ptrmemfunc (type
, expr
, 1);
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
120 else if converting class, pass off to build_type_conversion
121 else try C-style pointer conversion */
124 cp_convert_to_pointer (type
, expr
)
127 register tree intype
= TREE_TYPE (expr
);
128 register enum tree_code form
;
130 if (IS_AGGR_TYPE (intype
))
134 intype
= complete_type (intype
);
135 if (TYPE_SIZE (intype
) == NULL_TREE
)
137 cp_error ("can't convert from incomplete type `%T' to `%T'",
139 return error_mark_node
;
142 rval
= build_type_conversion (CONVERT_EXPR
, type
, expr
, 1);
145 if (rval
== error_mark_node
)
146 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
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
);
157 form
= TREE_CODE (intype
);
159 if (form
== POINTER_TYPE
|| form
== REFERENCE_TYPE
)
161 intype
= TYPE_MAIN_VARIANT (intype
);
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
)
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
)
175 binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 1);
176 if (binfo
== error_mark_node
)
177 return error_mark_node
;
182 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type
))
183 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype
))
184 || ! BINFO_OFFSET_ZEROP (binfo
))
186 /* Need to get the path we took. */
189 if (code
== PLUS_EXPR
)
190 get_base_distance (TREE_TYPE (type
), TREE_TYPE (intype
), 0, &path
);
192 get_base_distance (TREE_TYPE (intype
), TREE_TYPE (type
), 0, &path
);
193 return build_vbase_path (code
, type
, expr
, path
, 0);
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
);
202 if (TREE_CODE (TREE_TYPE (type
)) == OFFSET_TYPE
203 && TREE_CODE (TREE_TYPE (intype
)) == OFFSET_TYPE
)
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
;
214 if (TREE_CODE (TREE_TYPE (intype
)) == METHOD_TYPE
215 || (TREE_CODE (type
) == POINTER_TYPE
216 && TREE_CODE (TREE_TYPE (type
)) == METHOD_TYPE
))
218 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
220 return error_mark_node
;
223 return build1 (NOP_EXPR
, type
, expr
);
226 my_friendly_assert (form
!= OFFSET_TYPE
, 186);
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
));
232 if (integer_zerop (expr
))
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
;
241 if (INTEGRAL_CODE_P (form
))
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. */
252 return convert_to_pointer (type
, expr
);
255 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
257 return error_mark_node
;
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). */
265 convert_to_pointer_force (type
, expr
)
268 register tree intype
= TREE_TYPE (expr
);
269 register enum tree_code form
= TREE_CODE (intype
);
271 if (integer_zerop (expr
))
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
;
280 /* Convert signature pointer/reference to `void *' first. */
281 if (form
== RECORD_TYPE
282 && (IS_SIGNATURE_POINTER (intype
) || IS_SIGNATURE_REFERENCE (intype
)))
284 expr
= build_optr_ref (expr
);
285 intype
= TREE_TYPE (expr
);
286 form
= TREE_CODE (intype
);
289 if (form
== POINTER_TYPE
)
291 intype
= TYPE_MAIN_VARIANT (intype
);
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
)
299 enum tree_code code
= PLUS_EXPR
;
301 int distance
= get_base_distance (TREE_TYPE (type
),
302 TREE_TYPE (intype
), 0, &path
);
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
;
312 distance
= get_base_distance (TREE_TYPE (intype
),
313 TREE_TYPE (type
), 0, &path
);
317 /* Doesn't need any special help from us. */
318 return build1 (NOP_EXPR
, type
, expr
);
322 return build_vbase_path (code
, type
, expr
, path
, 0);
324 return build1 (NOP_EXPR
, type
, expr
);
327 return cp_convert_to_pointer (type
, expr
);
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.
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. */
339 build_up_reference (type
, arg
, flags
, checkconst
)
341 int flags
, checkconst
;
344 int literal_flag
= 0;
345 tree argtype
= TREE_TYPE (arg
);
346 tree target_type
= TREE_TYPE (type
);
347 tree binfo
= NULL_TREE
;
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
))
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
);
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
));
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);
374 switch (TREE_CODE (targ
))
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
))
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);
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);
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
395 if (TREE_READONLY (arg
))
397 arg
= copy_node (arg
);
398 TREE_READONLY (arg
) = 0;
401 rval
= build1 (CONVERT_EXPR
, type
, arg
);
402 TREE_REFERENCE_EXPR (rval
) = 1;
404 /* propagate the const flag on something like:
411 class Derived : public Base {
418 void func2(const Derived& d) {
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
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
))
431 arg
= copy_node (arg
);
432 TREE_READONLY (arg
) = TREE_READONLY (targ
);
434 literal_flag
= TREE_CONSTANT (arg
);
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 */
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;
453 /* Could be a reference to a static member. */
455 tree field
= TREE_OPERAND (targ
, 1);
456 if (TREE_STATIC (field
))
458 rval
= build1 (ADDR_EXPR
, type
, field
);
464 /* We should have farmed out member pointers above. */
465 my_friendly_abort (188);
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));
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
484 TREE_ADDRESSABLE (targ
) = 1;
485 put_var_into_stack (targ
);
490 if (targ
== current_class_ptr
)
492 error ("address of `this' not available");
494 /* This code makes the following core dump the compiler on a sun4,
495 if the code below is used.
499 typedef a_decl* a_ref;
504 void* append(a_ref& item);
508 a_decl (e_decl *parent);
522 a_decl::a_decl(e_decl *parent) {
523 parent->implementations.append(this);
527 TREE_ADDRESSABLE (targ
) = 1; /* so compiler doesn't die later */
528 put_var_into_stack (targ
);
531 return error_mark_node
;
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'",
542 else if (staticp (targ
))
545 TREE_ADDRESSABLE (targ
) = 1;
546 put_var_into_stack (targ
);
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));
558 case PREINCREMENT_EXPR
:
559 case PREDECREMENT_EXPR
:
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));
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
));
578 /* Undo the folding... */
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
));
591 arg
= TREE_OPERAND (targ
, 1);
592 if (arg
== NULL_TREE
)
594 compiler_error ("({ ... }) expression not expanded when needed for reference");
595 return error_mark_node
;
597 rval
= build1 (ADDR_EXPR
, type
, arg
);
598 TREE_REFERENCE_EXPR (rval
) = 1;
605 if (TREE_ADDRESSABLE (targ
) == 0)
607 if (! (flags
&INDIRECT_BIND
)
608 && toplevel_bindings_p ())
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
);
622 if (TREE_CODE (targ
) == CALL_EXPR
&& IS_AGGR_TYPE (argtype
))
624 arg
= build_cplus_new (argtype
, targ
);
626 else if (flags
&INDIRECT_BIND
)
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
);
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
);
642 TREE_TYPE (rval
) = type
;
644 temp
= build (MODIFY_EXPR
, argtype
, temp
, arg
);
645 TREE_SIDE_EFFECTS (temp
) = 1;
646 return build (COMPOUND_EXPR
, type
, temp
, rval
);
650 if (! (flags
&INDIRECT_BIND
))
652 if (TREE_CODE (arg
) == TARGET_EXPR
)
654 tree decl
= TREE_OPERAND (arg
, 0);
657 if (! toplevel_bindings_p () && ! DECL_RTL (decl
))
660 cleanup
= maybe_build_cleanup (decl
);
662 expand_decl_cleanup (decl
, cleanup
);
667 rval
= build1 (ADDR_EXPR
, type
, arg
);
670 if (TYPE_USES_COMPLEX_INHERITANCE (argtype
)
671 || TYPE_USES_COMPLEX_INHERITANCE (target_type
))
673 TREE_TYPE (rval
) = build_pointer_type (argtype
);
674 if (flags
& LOOKUP_PROTECT
)
675 rval
= convert_pointer_to (target_type
, 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
;
684 TREE_CONSTANT (rval
) = literal_flag
;
688 /* For C++: Only need to do one-level references, but cannot
689 get tripped up on signed/unsigned differences.
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. */
696 convert_to_reference (reftype
, expr
, convtype
, flags
, decl
)
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
;
707 if (TREE_CODE (intype
) == REFERENCE_TYPE
)
708 my_friendly_abort (364);
710 intype
= TYPE_MAIN_VARIANT (intype
);
712 i
= comp_target_types (type
, intype
, 0);
714 if (i
<= 0 && (convtype
& CONV_IMPLICIT
) && IS_AGGR_TYPE (intype
)
715 && ! (flags
& LOOKUP_NO_CONVERSION
))
717 /* Look for a user-defined conversion to lvalue that we can use. */
721 = build_type_conversion (CONVERT_EXPR
, reftype
, expr
, 1);
723 rval_as_conversion
= build_type_conversion (CONVERT_EXPR
, type
, expr
, 1);
726 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
727 && real_lvalue_p (rval_as_conversion
))
729 expr
= rval_as_conversion
;
730 rval_as_conversion
= NULL_TREE
;
736 if (((convtype
& CONV_STATIC
) && i
== -1)
737 || ((convtype
& CONV_IMPLICIT
) && i
== 1))
739 if (flags
& LOOKUP_COMPLAIN
)
741 tree ttl
= TREE_TYPE (reftype
);
745 int r
= TREE_READONLY (expr
);
746 int v
= TREE_THIS_VOLATILE (expr
);
747 ttr
= cp_build_type_variant (TREE_TYPE (expr
), r
, v
);
750 if (! real_lvalue_p (expr
) &&
751 (decl
== NULL_TREE
|| ! TYPE_READONLY (ttl
)))
754 /* Ensure semantics of [dcl.init.ref] */
755 cp_pedwarn ("initialization of non-const `%T' from rvalue `%T'",
758 cp_pedwarn ("conversion to `%T' from rvalue `%T'",
761 else if (! (convtype
& CONV_CONST
))
763 if (! TYPE_READONLY (ttl
) && TYPE_READONLY (ttr
))
764 cp_pedwarn ("conversion from `%T' to `%T' discards const",
766 else if (! TYPE_VOLATILE (ttl
) && TYPE_VOLATILE (ttr
))
767 cp_pedwarn ("conversion from `%T' to `%T' discards volatile",
772 return build_up_reference (reftype
, expr
, flags
,
773 ! (convtype
& CONV_CONST
));
775 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
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 */
782 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
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",
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
);
797 tree rval_as_ctor
= NULL_TREE
;
799 if (rval_as_conversion
)
801 if (rval_as_conversion
== error_mark_node
)
803 cp_error ("conversion from `%T' to `%T' is ambiguous",
805 return error_mark_node
;
807 rval_as_conversion
= build_up_reference (reftype
, rval_as_conversion
,
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
)))
822 if (toplevel_bindings_p ())
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
),
829 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
830 | LOOKUP_ONLYCONVERTING
);
832 if (init
== error_mark_node
)
833 return error_mark_node
;
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);
841 init
= build_method_call (NULL_TREE
, ctor_identifier
,
842 build_tree_list (NULL_TREE
, expr
),
844 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
845 |LOOKUP_ONLYCONVERTING
);
847 if (init
== error_mark_node
)
848 return error_mark_node
;
850 rval
= build_cplus_new (type
, init
);
851 rval
= build_up_reference (reftype
, rval
, flags
, 1);
856 if (rval_as_ctor
&& rval_as_conversion
)
858 cp_error ("ambiguous conversion from `%T' to `%T'; both user-defined conversion and constructor apply",
860 return error_mark_node
;
862 else if (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
))
868 rval
= convert (type
, expr
);
869 if (rval
== error_mark_node
)
870 return error_mark_node
;
872 rval
= build_up_reference (reftype
, rval
, flags
, 1);
875 if (rval
&& ! TYPE_READONLY (TREE_TYPE (reftype
)))
876 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
882 /* If we found a way to convert earlier, then use it. */
886 my_friendly_assert (TREE_CODE (intype
) != OFFSET_TYPE
, 189);
888 if (flags
& LOOKUP_COMPLAIN
)
889 cp_error ("cannot convert type `%T' to type `%T'", intype
, reftype
);
891 if (flags
& LOOKUP_SPECULATIVELY
)
894 return error_mark_node
;
897 /* We are using a reference VAL for its value. Bash that reference all the
898 way down to its lowest form. */
901 convert_from_reference (val
)
904 tree type
= TREE_TYPE (val
);
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
);
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.
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
923 convert_to_aggr (type
, expr
, msgp
, protect
)
928 tree basetype
= type
;
929 tree name
= TYPE_IDENTIFIER (basetype
);
930 tree function
, fndecl
, fntype
, parmtypes
, parmlist
, result
;
932 /* See code below that used this. */
936 int can_be_private
, can_be_protected
;
938 if (! TYPE_HAS_CONSTRUCTOR (basetype
))
941 *msgp
= "type `%s' does not have a constructor";
942 return error_mark_node
;
945 access
= access_public_node
;
947 can_be_protected
= IDENTIFIER_CLASS_VALUE (name
) || name
== current_class_name
;
949 parmlist
= build_tree_list (NULL_TREE
, expr
);
950 parmtypes
= tree_cons (NULL_TREE
, TREE_TYPE (expr
), void_list_node
);
952 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype
))
954 parmtypes
= tree_cons (NULL_TREE
, integer_type_node
, parmtypes
);
955 parmlist
= tree_cons (NULL_TREE
, integer_one_node
, parmlist
);
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
);
962 /* No exact conversion was found. See if an approximate
964 fndecl
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (basetype
), 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
;
977 cp
->harshness
= (struct harshness_code
*)
978 alloca (3 * sizeof (struct harshness_code
));
980 compute_conversion_costs (fndecl
, parmlist
, cp
, 2);
981 if ((cp
->h
.code
& EVIL_CODE
) == 0)
983 cp
->u
.field
= fndecl
;
986 if (TREE_PRIVATE (fndecl
))
987 access
= access_private_node
;
988 else if (TREE_PROTECTED (fndecl
))
989 access
= access_protected_node
;
991 access
= access_public_node
;
994 access
= access_public_node
;
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
1002 || purpose_member (basetype
, DECL_ACCESS (fndecl
)))
1005 if (cp
->h
.code
<= TRIVIAL_CODE
)
1011 if (access
== access_private_node
)
1017 fndecl
= DECL_CHAIN (fndecl
);
1019 if (cp
- candidates
)
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)() */
1033 if (cp
->h
.code
& EVIL_CODE
)
1036 *msgp
= "ambiguous type conversion possible for `%s'";
1037 return error_mark_node
;
1040 function
= cp
->function
;
1041 fndecl
= cp
->u
.field
;
1048 *msgp
= "only private and protected conversions apply";
1050 *msgp
= "only private conversions apply";
1051 else if (saw_protected
)
1052 *msgp
= "only protected conversions apply";
1054 *msgp
= "no appropriate conversion to type `%s'";
1056 return error_mark_node
;
1061 if (access
== access_private_node
)
1062 if (! can_be_private
)
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
;
1070 if (access
== access_protected_node
)
1071 if (! can_be_protected
)
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
;
1082 /* It will convert, but we don't do anything about it yet. */
1086 fntype
= TREE_TYPE (function
);
1088 parmlist
= convert_arguments (NULL_TREE
, TYPE_ARG_TYPES (fntype
),
1089 parmlist
, NULL_TREE
, LOOKUP_NORMAL
);
1091 result
= build_call (function
, TREE_TYPE (fntype
), parmlist
);
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
1103 convert_pointer_to_real (binfo
, expr
)
1106 register tree intype
= TREE_TYPE (expr
);
1110 if (TREE_CODE (binfo
) == TREE_VEC
)
1111 type
= BINFO_TYPE (binfo
);
1112 else if (IS_AGGR_TYPE (binfo
))
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
))
1128 if (intype
== error_mark_node
)
1129 return error_mark_node
;
1131 my_friendly_assert (!integer_zerop (expr
), 191);
1133 if (TREE_CODE (type
) == RECORD_TYPE
1134 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
1135 && type
!= TYPE_MAIN_VARIANT (TREE_TYPE (intype
)))
1139 = get_base_distance (binfo
, TYPE_MAIN_VARIANT (TREE_TYPE (intype
)),
1142 /* This function shouldn't be called with unqualified arguments
1143 but if it is, give them an error message that they can read. */
1146 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
1147 TREE_TYPE (intype
), type
);
1150 cp_error ("because `%T' is an ambiguous base class", type
);
1151 return error_mark_node
;
1154 return build_vbase_path (PLUS_EXPR
, ptr_type
, expr
, path
, 1);
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
);
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. */
1170 convert_pointer_to (binfo
, expr
)
1175 if (TREE_CODE (binfo
) == TREE_VEC
)
1176 type
= BINFO_TYPE (binfo
);
1177 else if (IS_AGGR_TYPE (binfo
))
1181 return convert_pointer_to_real (type
, expr
);
1186 FLAGS indicates how we should behave. */
1189 cp_convert (type
, expr
, convtype
, flags
)
1191 int convtype
, flags
;
1193 register tree e
= expr
;
1194 register enum tree_code code
= TREE_CODE (type
);
1196 if (TREE_CODE (e
) == ERROR_MARK
1197 || TREE_CODE (TREE_TYPE (e
)) == ERROR_MARK
)
1198 return error_mark_node
;
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
));
1206 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
1207 return build1 (CONVERT_EXPR
, type
, e
);
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));
1216 /* Just convert to the type of the member. */
1217 if (code
== OFFSET_TYPE
)
1219 type
= TREE_TYPE (type
);
1220 code
= TREE_CODE (type
);
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
);
1230 if (TREE_CODE (e
) == OFFSET_REF
)
1231 e
= resolve_offset_ref (e
);
1233 if (TREE_READONLY_DECL_P (e
))
1234 e
= decl_constant_value (e
);
1236 if (INTEGRAL_CODE_P (code
))
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
))
1245 cp_pedwarn ("conversion from `%#T' to `%#T'", intype
, type
);
1247 if (flag_pedantic_errors
)
1248 return error_mark_node
;
1250 if (IS_AGGR_TYPE (intype
))
1253 rval
= build_type_conversion (CONVERT_EXPR
, type
, e
, 1);
1256 if (flags
& LOOKUP_COMPLAIN
)
1257 cp_error ("`%#T' used where a `%T' was expected", intype
, type
);
1258 if (flags
& LOOKUP_SPECULATIVELY
)
1260 return error_mark_node
;
1262 if (code
== BOOLEAN_TYPE
)
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
);
1270 return fold (convert_to_integer (type
, e
));
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
)
1277 if (IS_AGGR_TYPE (TREE_TYPE (e
)))
1280 rval
= build_type_conversion (CONVERT_EXPR
, type
, e
, 1);
1284 if (flags
& LOOKUP_COMPLAIN
)
1285 cp_error ("`%#T' used where a floating point value was expected",
1288 return fold (convert_to_real (type
, e
));
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
))
1296 tree dtype
= TREE_TYPE (e
);
1297 tree ctor
= NULL_TREE
;
1298 tree conversion
= NULL_TREE
;
1300 dtype
= TYPE_MAIN_VARIANT (dtype
);
1302 /* Conversion of object pointers or signature pointers/references
1303 to signature pointers/references. */
1305 if (TYPE_LANG_SPECIFIC (type
)
1306 && (IS_SIGNATURE_POINTER (type
) || IS_SIGNATURE_REFERENCE (type
)))
1308 tree constructor
= build_signature_pointer_constructor (type
, expr
);
1309 tree sig_ty
= SIGNATURE_TYPE (type
);
1312 if (constructor
== error_mark_node
)
1313 return error_mark_node
;
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;
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.
1329 There may be some ambiguity between using a constructor
1330 vs. using a type conversion operator when both apply. */
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);
1336 if (conversion
== error_mark_node
)
1338 if (flags
& LOOKUP_COMPLAIN
)
1339 error ("ambiguous pointer conversion");
1344 if (TYPE_HAS_CONSTRUCTOR (complete_type (type
)))
1346 if (TYPE_HAS_CONSTRUCTOR (complete_type (type
)) && ! conversion
)
1348 ctor
= build_method_call (NULL_TREE
, ctor_identifier
,
1349 build_tree_list (NULL_TREE
, e
),
1351 (flags
& LOOKUP_NORMAL
) | LOOKUP_SPECULATIVELY
1352 | (convtype
& CONV_NONCONVERTING
? 0 : LOOKUP_ONLYCONVERTING
)
1353 | (flags
& LOOKUP_NO_CONVERSION
)
1354 | (conversion
? LOOKUP_NO_CONVERSION
: 0));
1356 if (ctor
== error_mark_node
)
1358 if (flags
& LOOKUP_COMPLAIN
)
1359 cp_error ("in conversion to type `%T'", type
);
1360 if (flags
& LOOKUP_SPECULATIVELY
)
1362 return error_mark_node
;
1365 if (conversion
&& ctor
)
1367 if (flags
& LOOKUP_COMPLAIN
)
1368 error ("both constructor and type conversion operator apply");
1369 if (flags
& LOOKUP_SPECULATIVELY
)
1371 return error_mark_node
;
1373 else if (conversion
)
1377 ctor
= build_cplus_new (type
, ctor
);
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,
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
)))
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
)
1395 return error_mark_node
;
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. */
1405 convert (type
, expr
)
1408 return cp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
);
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). */
1416 convert_force (type
, expr
, convtype
)
1421 register tree e
= expr
;
1422 register enum tree_code code
= TREE_CODE (type
);
1424 if (code
== REFERENCE_TYPE
)
1425 return fold (convert_to_reference (type
, e
, CONV_C_CAST
, LOOKUP_COMPLAIN
,
1427 else if (TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
1428 e
= convert_from_reference (e
);
1430 if (code
== POINTER_TYPE
)
1431 return fold (convert_to_pointer_force (type
, e
));
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
))
1441 /* compatible pointer to member functions. */
1442 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1);
1445 return cp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
);
1448 /* Subroutine of build_type_conversion. */
1451 build_type_conversion_1 (xtype
, basetype
, expr
, typename
, for_sure
)
1452 tree xtype
, basetype
;
1461 flags
= LOOKUP_PROTECT
|LOOKUP_ONLYCONVERTING
;
1463 flags
= LOOKUP_NORMAL
|LOOKUP_ONLYCONVERTING
;
1465 rval
= build_method_call (expr
, typename
, NULL_TREE
, NULL_TREE
, flags
);
1466 if (rval
== error_mark_node
)
1470 return error_mark_node
;
1473 if (IS_AGGR_TYPE (TREE_TYPE (rval
)))
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
);
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.
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.
1494 Changes to this functions should be mirrored in user_harshness.
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.
1502 build_type_conversion (code
, xtype
, expr
, for_sure
)
1503 enum tree_code code
;
1508 return build_user_type_conversion
1509 (xtype
, expr
, for_sure
? LOOKUP_NORMAL
: 0);
1511 /* C++: check to see if we can convert this aggregate type
1512 into the required type. */
1515 tree winner
= NULL_TREE
;
1517 if (expr
== error_mark_node
)
1518 return error_mark_node
;
1520 basetype
= TREE_TYPE (expr
);
1521 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
1522 basetype
= TREE_TYPE (basetype
);
1524 basetype
= TYPE_MAIN_VARIANT (basetype
);
1525 if (! TYPE_LANG_SPECIFIC (basetype
) || ! TYPE_HAS_CONVERSION (basetype
))
1528 /* Do we have an exact match? */
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
,
1536 /* Nope; try looking for others. */
1537 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1539 tree cand
= TREE_VALUE (conv
);
1541 if (winner
&& winner
== cand
)
1544 if (can_convert (xtype
, TREE_TYPE (TREE_TYPE (cand
))))
1550 cp_error ("ambiguous conversion from `%T' to `%T'", basetype
,
1552 cp_error (" candidate conversions include `%D' and `%D'",
1563 return build_type_conversion_1 (xtype
, basetype
, expr
,
1564 DECL_NAME (winner
), for_sure
);
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. */
1576 build_expr_type_conversion (desires
, expr
, complain
)
1581 tree basetype
= TREE_TYPE (expr
);
1583 tree winner
= NULL_TREE
;
1585 if (TREE_CODE (basetype
) == OFFSET_TYPE
)
1586 expr
= resolve_offset_ref (expr
);
1587 expr
= convert_from_reference (expr
);
1588 basetype
= TREE_TYPE (expr
);
1590 if (! IS_AGGR_TYPE (basetype
))
1591 switch (TREE_CODE (basetype
))
1594 if ((desires
& WANT_NULL
) && TREE_CODE (expr
) == INTEGER_CST
1595 && integer_zerop (expr
))
1597 /* else fall through... */
1600 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1602 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1604 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1606 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1610 return (desires
& WANT_POINTER
) ? default_conversion (expr
)
1616 if (! TYPE_HAS_CONVERSION (basetype
))
1619 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1623 tree cand
= TREE_VALUE (conv
);
1625 if (winner
&& winner
== cand
)
1628 candidate
= TREE_TYPE (TREE_TYPE (cand
));
1629 if (TREE_CODE (candidate
) == REFERENCE_TYPE
)
1630 candidate
= TREE_TYPE (candidate
);
1632 switch (TREE_CODE (candidate
))
1636 win
= (desires
& WANT_INT
); break;
1638 win
= (desires
& WANT_ENUM
); break;
1640 win
= (desires
& WANT_FLOAT
); break;
1642 win
= (desires
& WANT_POINTER
); break;
1651 cp_error ("ambiguous default type conversion from `%T'",
1653 cp_error (" candidate conversions include `%D' and `%D'",
1656 return error_mark_node
;
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);
1675 /* Must convert two aggregate types to non-aggregate type.
1676 Attempts to find a non-ambiguous, "best" type conversion.
1678 Return 1 on success, 0 on failure.
1680 @@ What are the real semantics of this supposed to be??? */
1683 build_default_binary_type_conversion (code
, arg1
, arg2
)
1684 enum tree_code code
;
1690 case TRUNC_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);
1699 case TRUNC_MOD_EXPR
:
1700 case FLOOR_MOD_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);
1712 tree a1
, a2
, p1
, p2
;
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);
1720 wins
= (a1
&& a2
) + (a1
&& p2
) + (p1
&& a2
);
1723 error ("ambiguous default type conversion for `operator +'");
1726 *arg1
= a1
, *arg2
= a2
;
1728 *arg1
= a1
, *arg2
= p2
;
1730 *arg1
= p1
, *arg2
= a2
;
1736 tree a1
, a2
, p1
, p2
;
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);
1744 wins
= (a1
&& a2
) + (p1
&& p2
) + (p1
&& a2
);
1747 error ("ambiguous default type conversion for `operator -'");
1750 *arg1
= a1
, *arg2
= a2
;
1752 *arg1
= p1
, *arg2
= p2
;
1754 *arg1
= p1
, *arg2
= a2
;
1765 tree a1
, a2
, p1
, p2
;
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);
1773 wins
= (a1
&& a2
) + (p1
&& p2
);
1776 cp_error ("ambiguous default type conversion for `%O'", code
);
1779 *arg1
= a1
, *arg2
= a2
;
1781 *arg1
= p1
, *arg2
= p2
;
1785 case TRUTH_ANDIF_EXPR
:
1786 case TRUTH_ORIF_EXPR
:
1787 *arg1
= convert (boolean_type_node
, *arg1
);
1788 *arg2
= convert (boolean_type_node
, *arg2
);
1796 if (*arg1
== error_mark_node
|| *arg2
== error_mark_node
)
1797 cp_error ("ambiguous default type conversion for `%O'", code
);
1805 /* Implements integral promotion (4.1) and float->double promotion. */
1808 type_promotes_to (type
)
1811 int constp
, volatilep
;
1813 if (type
== error_mark_node
)
1814 return error_mark_node
;
1816 constp
= TYPE_READONLY (type
);
1817 volatilep
= TYPE_VOLATILE (type
);
1818 type
= TYPE_MAIN_VARIANT (type
);
1820 /* bool always promotes to int (not unsigned), even if it's the same
1822 if (type
== boolean_type_node
)
1823 type
= integer_type_node
;
1825 /* Normally convert enums to int, but convert wide enums to something
1827 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1828 || type
== wchar_type_node
)
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);
1839 else if (C_PROMOTING_INTEGER_TYPE_P (type
))
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
;
1848 type
= integer_type_node
;
1850 else if (type
== float_type_node
)
1851 type
= double_type_node
;
1853 return cp_build_type_variant (type
, constp
, volatilep
);
1856 /* Work in progress. Ask jason before removing. */
1858 struct z_candidate
{
1865 struct z_candidate
*next
;
1868 #define IDENTITY_RANK 0
1869 #define EXACT_RANK 1
1870 #define PROMO_RANK 2
1872 #define PBOOL_RANK 4
1874 #define ELLIPSIS_RANK 6
1876 #define ICS_RANK(NODE) \
1877 (ICS_ELLIPSIS_FLAG (NODE) ? ELLIPSIS_RANK \
1878 : ICS_USER_FLAG (NODE) ? USER_RANK \
1879 : ICS_STD_RANK (NODE))
1881 #define ICS_STD_RANK(NODE) TREE_COMPLEXITY (NODE)
1883 #define ICS_USER_FLAG(NODE) TREE_LANG_FLAG_0 (NODE)
1884 #define ICS_ELLIPSIS_FLAG(NODE) TREE_LANG_FLAG_1 (NODE)
1886 #define USER_CONV_FN(NODE) TREE_OPERAND (NODE, 1)
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 ();
1898 return (INTEGRAL_TYPE_P (TREE_TYPE (t
)) && integer_zerop (t
));
1902 build_conv (code
, type
, from
)
1903 enum tree_code code
;
1906 tree t
= build1 (code
, type
, from
);
1907 int rank
= ICS_STD_RANK (from
);
1914 if (rank
< STD_RANK
)
1921 if (rank
< EXACT_RANK
)
1927 ICS_STD_RANK (t
) = rank
;
1928 ICS_USER_FLAG (t
) = ICS_USER_FLAG (from
);
1936 if (TREE_CODE (t
) == REFERENCE_TYPE
)
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. */
1946 standard_conversion (to
, from
, expr
)
1947 tree to
, from
, expr
;
1949 enum tree_code fcode
, tcode
;
1952 fcode
= TREE_CODE (from
);
1953 tcode
= TREE_CODE (to
);
1955 conv
= build1 (IDENTITY_CONV
, from
, expr
);
1960 if (fcode
== FUNCTION_TYPE
)
1962 from
= build_pointer_type (from
);
1963 fcode
= TREE_CODE (from
);
1964 conv
= build_conv (LVALUE_CONV
, from
, conv
);
1966 else if (fcode
== ARRAY_TYPE
)
1968 from
= build_pointer_type (TREE_TYPE (from
));
1969 fcode
= TREE_CODE (from
);
1970 conv
= build_conv (LVALUE_CONV
, from
, conv
);
1973 if ((tcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (to
))
1974 && expr
&& null_ptr_cst (expr
))
1976 conv
= build_conv (STD_CONV
, to
, conv
);
1978 else if (tcode
== POINTER_TYPE
&& fcode
== POINTER_TYPE
)
1980 enum tree_code ufcode
= TREE_CODE (TREE_TYPE (from
));
1981 enum tree_code utcode
= TREE_CODE (TREE_TYPE (to
));
1983 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (from
)),
1984 TYPE_MAIN_VARIANT (TREE_TYPE (to
)), 1))
1986 else if (utcode
== VOID_TYPE
&& ufcode
!= OFFSET_TYPE
1987 && ufcode
!= FUNCTION_TYPE
)
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
);
1995 else if (ufcode
== OFFSET_TYPE
&& utcode
== OFFSET_TYPE
)
1997 tree fbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (from
));
1998 tree tbase
= TYPE_OFFSET_BASETYPE (TREE_TYPE (to
));
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
))),
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
);
2012 else if (IS_AGGR_TYPE (TREE_TYPE (from
))
2013 && IS_AGGR_TYPE (TREE_TYPE (to
)))
2015 if (DERIVED_FROM_P (TREE_TYPE (to
), TREE_TYPE (from
)))
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
);
2029 if (! comptypes (from
, to
, 1))
2031 if (! comp_ptr_ttypes (TREE_TYPE (to
), TREE_TYPE (from
)))
2035 conv
= build_conv (QUAL_CONV
, from
, conv
);
2038 else if (TYPE_PTRMEMFUNC_P (to
) && TYPE_PTRMEMFUNC_P (from
))
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
)));
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
))
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
);
2059 else if (tcode
== BOOLEAN_TYPE
)
2061 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
2062 || fcode
== POINTER_TYPE
|| TYPE_PTRMEMFUNC_P (from
)))
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
;
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
)
2075 if (! (INTEGRAL_CODE_P (fcode
) || fcode
== REAL_TYPE
))
2077 conv
= build_conv (STD_CONV
, to
, conv
);
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
;
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
);
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.
2097 Currently does not distinguish in the generated trees between binding to
2098 an lvalue and a temporary. Should it? */
2101 reference_binding (rto
, from
, expr
)
2102 tree rto
, from
, expr
;
2106 tree to
= TREE_TYPE (rto
);
2108 if (TREE_CODE (from
) == REFERENCE_TYPE
)
2109 from
= TREE_TYPE (from
);
2110 else if (! expr
|| ! real_lvalue_p (expr
))
2114 && TYPE_READONLY (to
) >= TYPE_READONLY (from
)
2115 && TYPE_VOLATILE (to
) >= TYPE_VOLATILE (from
))
2117 conv
= build1 (IDENTITY_CONV
, from
, expr
);
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
))
2124 conv
= build_conv (REF_BIND
, rto
, conv
);
2125 ICS_STD_RANK (conv
) = STD_RANK
;
2133 if (! conv
&& TYPE_READONLY (to
) && ! TYPE_VOLATILE (to
))
2135 conv
= standard_conversion
2136 (TYPE_MAIN_VARIANT (to
), TYPE_MAIN_VARIANT (from
), expr
);
2139 conv
= build_conv (REF_BIND
, rto
, conv
);
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);
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
2156 implicit_conversion (to
, from
, expr
, flags
)
2157 tree to
, from
, expr
;
2161 struct z_candidate
*cand
;
2163 if (expr
&& type_unknown_p (expr
))
2165 expr
= instantiate_type (to
, expr
, 0);
2166 if (expr
== error_mark_node
)
2168 from
= TREE_TYPE (expr
);
2171 if (TREE_CODE (to
) == REFERENCE_TYPE
)
2172 conv
= reference_binding (to
, from
, expr
);
2174 conv
= standard_conversion
2175 (TYPE_MAIN_VARIANT (non_reference (to
)),
2176 TYPE_MAIN_VARIANT (non_reference (from
)), expr
);
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
);
2184 else if ((IS_AGGR_TYPE (non_reference (from
))
2185 || IS_AGGR_TYPE (non_reference (to
)))
2186 && (flags
& LOOKUP_NO_CONVERSION
) == 0)
2188 if (TREE_CODE (to
) == REFERENCE_TYPE
2189 && TYPE_READONLY (TREE_TYPE (to
))
2190 && ! TYPE_VOLATILE (TREE_TYPE (to
)))
2192 cand
= build_user_type_conversion_1
2193 (TYPE_MAIN_VARIANT (TREE_TYPE (to
)), expr
, LOOKUP_ONLYCONVERTING
);
2195 conv
= build_conv (REF_BIND
, to
, cand
->second_conv
);
2199 cand
= build_user_type_conversion_1
2200 (to
, expr
, LOOKUP_ONLYCONVERTING
);
2202 conv
= cand
->second_conv
;
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. */
2213 static struct z_candidate
*
2214 add_function_candidate (candidates
, fn
, arglist
, flags
)
2215 struct z_candidate
*candidates
;
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
;
2225 struct z_candidate
*cand
;
2227 /* The `this' and `in_chrg' arguments to constructors are not considered
2228 in overload resolution. */
2229 if (DECL_CONSTRUCTOR_P (fn
))
2231 parmnode
= TREE_CHAIN (parmnode
);
2232 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
2233 parmnode
= TREE_CHAIN (parmnode
);
2236 for (i
= 0; i
< len
; ++i
)
2238 tree arg
= TREE_VALUE (argnode
);
2239 tree argtype
= TREE_TYPE (arg
);
2242 argtype
= cp_build_type_variant
2243 (argtype
, TREE_READONLY (arg
), TREE_THIS_VOLATILE (arg
));
2245 if (parmnode
== void_list_node
)
2248 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
, flags
);
2251 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
2252 ICS_ELLIPSIS_FLAG (t
) = 1;
2255 TREE_VEC_ELT (convs
, i
) = t
;
2260 parmnode
= TREE_CHAIN (parmnode
);
2261 argnode
= TREE_CHAIN (argnode
);
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
))
2276 cand
= (struct z_candidate
*) oballoc (sizeof (struct z_candidate
));
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
;
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. */
2294 static struct z_candidate
*
2295 add_conv_candidate (candidates
, fn
, obj
, arglist
)
2296 struct z_candidate
*candidates
;
2297 tree fn
, obj
, arglist
;
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
;
2306 struct z_candidate
*cand
;
2307 int flags
= LOOKUP_NORMAL
;
2309 for (i
= 0; i
< len
; ++i
)
2311 tree arg
= i
== 0 ? obj
: TREE_VALUE (argnode
);
2312 tree argtype
= TREE_TYPE (arg
);
2315 argtype
= cp_build_type_variant
2316 (argtype
, TREE_READONLY (arg
), TREE_THIS_VOLATILE (arg
));
2319 t
= implicit_conversion (totype
, argtype
, arg
, flags
);
2320 else if (parmnode
== void_list_node
)
2323 t
= implicit_conversion (TREE_VALUE (parmnode
), argtype
, arg
, flags
);
2326 t
= build1 (IDENTITY_CONV
, argtype
, arg
);
2327 ICS_ELLIPSIS_FLAG (t
) = 1;
2330 TREE_VEC_ELT (convs
, i
) = t
;
2338 parmnode
= TREE_CHAIN (parmnode
);
2339 argnode
= TREE_CHAIN (argnode
);
2345 for (; parmnode
&& parmnode
!= void_list_node
;
2346 parmnode
= TREE_CHAIN (parmnode
))
2347 if (! TREE_PURPOSE (parmnode
))
2353 cand
= (struct z_candidate
*) oballoc (sizeof (struct z_candidate
));
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
;
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
);
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)
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
;
2397 struct z_candidate
*cand
;
2403 convs
= make_tree_vec (args
[2] ? 3 : (args
[1] ? 2 : 1));
2405 for (i
= 0; i
< 2; ++i
)
2410 t
= implicit_conversion (types
[i
], argtypes
[i
], args
[i
], flags
);
2414 /* We need something for printing the candidate. */
2415 t
= build1 (IDENTITY_CONV
, types
[i
], NULL_TREE
);
2417 TREE_VEC_ELT (convs
, i
) = t
;
2420 /* For COND_EXPR we rearranged the arguments; undo that now. */
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
);
2427 TREE_VEC_ELT (convs
, 0) = t
;
2432 cand
= (struct z_candidate
*) oballoc (sizeof (struct z_candidate
));
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
;
2449 return TYPE_SIZE (complete_type (t
)) != NULL_TREE
;
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. */
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
;
2467 case POSTINCREMENT_EXPR
:
2468 case POSTDECREMENT_EXPR
:
2469 args
[1] = integer_zero_node
;
2470 type2
= integer_type_node
;
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); */
2494 case POSTDECREMENT_EXPR
:
2495 case PREDECREMENT_EXPR
:
2496 if (TREE_CODE (type1
) == BOOLEAN_TYPE
)
2498 case POSTINCREMENT_EXPR
:
2499 case PREINCREMENT_EXPR
:
2500 if (ARITHMETIC_TYPE_P (type1
) || ptr_complete_ob (type1
))
2502 type1
= build_reference_type (type1
);
2507 /* 7 For every cv-qualified or cv-unqualified complete object type T, there
2508 exist candidate operator functions of the form
2512 8 For every function type T, there exist candidate operator functions of
2514 T& operator*(T*); */
2517 if (TREE_CODE (type1
) == POINTER_TYPE
2518 && (ptr_complete_ob (type1
)
2519 || TREE_CODE (TREE_TYPE (type1
)) == FUNCTION_TYPE
))
2523 /* 9 For every type T, there exist candidate operator functions of the form
2526 10For every promoted arithmetic type T, there exist candidate operator
2527 functions of the form
2531 case CONVERT_EXPR
: /* unary + */
2532 if (TREE_CODE (type1
) == POINTER_TYPE
2533 && TREE_CODE (TREE_TYPE (type1
)) != OFFSET_TYPE
)
2536 if (ARITHMETIC_TYPE_P (type1
))
2540 /* 11For every promoted integral type T, there exist candidate operator
2541 functions of the form
2545 if (INTEGRAL_TYPE_P (type1
))
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. */
2557 if (TREE_CODE (type1
) == POINTER_TYPE
2558 && (TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
)))
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
)));
2565 if (IS_AGGR_TYPE (c1
) && DERIVED_FROM_P (c2
, c1
)
2566 && (TYPE_PTRMEMFUNC_P (type2
)
2567 || is_complete (TREE_TYPE (TREE_TYPE (type2
)))))
2572 /* 13For every pair of promoted arithmetic types L and R, there exist can-
2573 didate operator functions of the form
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
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*);
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);
2600 16For every pointer type T, there exist candidate operator functions of
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);
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); */
2615 if (ptr_complete_ob (type1
) && ptr_complete_ob (type2
))
2617 if (ptr_complete_ob (type1
) && INTEGRAL_TYPE_P (type2
))
2619 type2
= ptrdiff_type_node
;
2623 case TRUNC_DIV_EXPR
:
2624 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2630 if (TYPE_PTRMEMFUNC_P (type1
) && TYPE_PTRMEMFUNC_P (type2
)
2631 || TYPE_PTRMEM_P (type1
) && TYPE_PTRMEM_P (type2
))
2633 if ((TYPE_PTRMEMFUNC_P (type1
) || TYPE_PTRMEM_P (type1
))
2634 && null_ptr_cst (args
[1]))
2639 if ((TYPE_PTRMEMFUNC_P (type2
) || TYPE_PTRMEM_P (type2
))
2640 && null_ptr_cst (args
[0]))
2651 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
)
2652 || TYPE_PTR_P (type1
) && TYPE_PTR_P (type2
))
2654 if (TYPE_PTR_P (type1
) && null_ptr_cst (args
[1]))
2659 if (null_ptr_cst (args
[0]) && TYPE_PTR_P (type2
))
2667 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2670 if (INTEGRAL_TYPE_P (type1
) && ptr_complete_ob (type2
))
2672 type1
= ptrdiff_type_node
;
2675 if (ptr_complete_ob (type1
) && INTEGRAL_TYPE_P (type2
))
2677 type2
= ptrdiff_type_node
;
2682 /* 18For every pair of promoted integral types L and R, there exist candi-
2683 date operator functions of the form
2690 where LR is the result of the usual arithmetic conversions between
2693 case TRUNC_MOD_EXPR
:
2699 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
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);
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*);
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
2719 VQ T& operator=(VQ T&, T);
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-
2725 T*VQ& operator+=(T*VQ&, I);
2726 T*VQ& operator-=(T*VQ&, I);
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
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); */
2744 if (ptr_complete_ob (type1
) && INTEGRAL_TYPE_P (type2
))
2746 type2
= ptrdiff_type_node
;
2750 case TRUNC_DIV_EXPR
:
2751 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
2755 case TRUNC_MOD_EXPR
:
2761 if (INTEGRAL_TYPE_P (type1
) && INTEGRAL_TYPE_P (type2
))
2766 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
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])))
2781 my_friendly_abort (367);
2783 type1
= build_reference_type (type1
);
2787 if (TREE_CODE (type1
) == ENUMERAL_TYPE
&& type1
== type2
)
2789 else if (TREE_CODE (type1
) == ENUMERAL_TYPE
2790 || TREE_CODE (type2
) == ENUMERAL_TYPE
)
2792 if (ARITHMETIC_TYPE_P (type1
) && ARITHMETIC_TYPE_P (type2
))
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
)))
2800 if (TREE_CODE (type1
) == REFERENCE_TYPE
2801 || TREE_CODE (type2
) == REFERENCE_TYPE
)
2803 if (((TYPE_PTRMEMFUNC_P (type1
) || TREE_CODE (type1
) == POINTER_TYPE
)
2804 && null_ptr_cst (args
[1]))
2805 || IS_AGGR_TYPE (type1
))
2810 if (((TYPE_PTRMEMFUNC_P (type2
) || TREE_CODE (type2
) == POINTER_TYPE
)
2811 && null_ptr_cst (args
[0]))
2812 || IS_AGGR_TYPE (type2
))
2820 my_friendly_abort (367);
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
)))
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
);
2838 return build_builtin_candidate
2839 (candidates
, fnname
, type1
, type2
, args
, argtypes
, flags
);
2843 type_decays_to (type
)
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
);
2853 /* There are three conditions of builtin candidates:
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... */
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
;
2869 tree type
, argtypes
[3], types
[2];
2871 for (i
= 0; i
< 3; ++i
)
2874 argtypes
[i
] = cp_build_type_variant
2875 (TREE_TYPE (args
[i
]), TREE_READONLY (args
[i
]),
2876 TREE_THIS_VOLATILE (args
[i
]));
2878 argtypes
[i
] = NULL_TREE
;
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&); */
2888 case POSTINCREMENT_EXPR
:
2889 case PREINCREMENT_EXPR
:
2890 case POSTDECREMENT_EXPR
:
2891 case PREDECREMENT_EXPR
:
2896 /* 24There also exist candidate operator functions of the form
2897 bool operator!(bool);
2898 bool operator&&(bool, bool);
2899 bool operator||(bool, bool); */
2901 case TRUTH_NOT_EXPR
:
2902 return build_builtin_candidate
2903 (candidates
, fnname
, boolean_type_node
,
2904 NULL_TREE
, args
, argtypes
, flags
);
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
);
2921 types
[0] = types
[1] = NULL_TREE
;
2923 for (i
= 0; i
< 2; ++i
)
2927 else if (IS_AGGR_TYPE (argtypes
[i
]))
2929 tree convs
= lookup_conversions (argtypes
[i
]);
2931 if (code
== COND_EXPR
)
2933 if (real_lvalue_p (args
[i
]))
2934 types
[i
] = tree_cons
2935 (NULL_TREE
, build_reference_type (argtypes
[i
]), types
[i
]);
2937 types
[i
] = tree_cons
2938 (NULL_TREE
, TYPE_MAIN_VARIANT (argtypes
[i
]), types
[i
]);
2941 else if (! convs
|| (i
== 0 && code
== MODIFY_EXPR
))
2944 for (; convs
; convs
= TREE_CHAIN (convs
))
2946 type
= TREE_TYPE (TREE_TYPE (TREE_VALUE (convs
)));
2949 && (TREE_CODE (type
) != REFERENCE_TYPE
2950 || TYPE_READONLY (TREE_TYPE (type
))))
2953 if (code
== COND_EXPR
&& TREE_CODE (type
) == REFERENCE_TYPE
)
2954 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2956 type
= non_reference (type
);
2957 if (i
!= 0 || ! ref1
)
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
);
2965 if (! value_member (type
, types
[i
]))
2966 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
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
)
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
);
2982 types
[i
] = tree_cons (NULL_TREE
, type
, types
[i
]);
2986 for (; types
[0]; types
[0] = TREE_CHAIN (types
[0]))
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
);
2994 candidates
= add_builtin_candidate
2995 (candidates
, code
, code2
, fnname
, TREE_VALUE (types
[0]),
2996 NULL_TREE
, args
, argtypes
, flags
);
3002 static struct z_candidate
*
3003 add_template_candidate (candidates
, tmpl
, arglist
, flags
)
3004 struct z_candidate
*candidates
;
3008 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl
));
3009 tree
*targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
3010 struct z_candidate
*cand
;
3014 i
= type_unification (DECL_TEMPLATE_PARMS (tmpl
), targs
,
3015 TYPE_ARG_TYPES (TREE_TYPE (tmpl
)),
3016 arglist
, &dummy
, 0, 0);
3020 fn
= instantiate_template (tmpl
, targs
);
3021 if (fn
== error_mark_node
)
3024 cand
= add_function_candidate (candidates
, fn
, arglist
, flags
);
3025 cand
->template = DECL_TEMPLATE_INFO (fn
);
3031 struct z_candidate
*cands
;
3033 for (; cands
; cands
= cands
->next
)
3039 static struct z_candidate
*
3040 splice_viable (cands
)
3041 struct z_candidate
*cands
;
3043 struct z_candidate
**p
= &cands
;
3060 /* Fix this to work on non-lvalues. */
3061 return build_unary_op (ADDR_EXPR
, obj
, 0);
3065 print_z_candidates (candidates
)
3066 struct z_candidate
*candidates
;
3071 if (TREE_CODE (candidates
->fn
) == IDENTIFIER_NODE
)
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)));
3083 cp_error ("candidates are: %D(%T) <builtin>", candidates
->fn
,
3084 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)));
3087 cp_error_at ("candidates are: %D", candidates
->fn
);
3088 candidates
= candidates
->next
;
3090 for (; candidates
; candidates
= candidates
->next
)
3092 if (TREE_CODE (candidates
->fn
) == IDENTIFIER_NODE
)
3094 if (candidates
->fn
== ansi_opname
[COND_EXPR
])
3095 cp_error (" %D(%T, %T, %T) <builtin>",
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)));
3105 cp_error (" %D(%T) <builtin>", candidates
->fn
,
3106 TREE_TYPE (TREE_VEC_ELT (candidates
->convs
, 0)));
3109 cp_error_at (" %D", candidates
->fn
);
3113 /* Returns the best overload candidate to perform the requested
3116 static struct z_candidate
*
3117 build_user_type_conversion_1 (totype
, expr
, flags
)
3121 struct z_candidate
*candidates
, *cand
;
3122 tree fromtype
= TREE_TYPE (expr
);
3123 tree ctors
= NULL_TREE
, convs
= NULL_TREE
, *p
;
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
);
3133 flags
|= LOOKUP_NO_CONVERSION
;
3137 ctors
= TREE_VALUE (ctors
);
3138 args
= build_tree_list (NULL_TREE
, expr
);
3140 for (; ctors
; ctors
= DECL_CHAIN (ctors
))
3142 if ((flags
& LOOKUP_ONLYCONVERTING
) && DECL_NONCONVERTING_P (ctors
))
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
);
3151 args
= build_tree_list (NULL_TREE
, build_this (expr
));
3153 for (; convs
; convs
= TREE_CHAIN (convs
))
3155 tree fn
= TREE_VALUE (convs
);
3156 tree ics
= implicit_conversion
3157 (totype
, TREE_TYPE (TREE_TYPE (fn
)), 0, LOOKUP_NO_CONVERSION
);
3159 for (; fn
; fn
= DECL_CHAIN (fn
))
3161 candidates
= add_function_candidate (candidates
, fn
, args
, flags
);
3162 candidates
->second_conv
= ics
;
3163 candidates
->basetype_path
= TREE_PURPOSE (convs
);
3167 if (! any_viable (candidates
))
3170 if (flags
& LOOKUP_COMPLAIN
)
3172 if (candidates
&& ! candidates
->next
)
3173 /* say why this one won't work or try to be loose */;
3175 cp_error ("no viable candidates");
3182 candidates
= splice_viable (candidates
);
3183 cand
= tourney (candidates
, totype
);
3187 if (flags
& LOOKUP_COMPLAIN
)
3189 cp_error ("ambiguous user-defined type conversion");
3190 print_z_candidates (candidates
);
3193 cand
= candidates
; /* any one will do */
3194 cand
->second_conv
= build1 (AMBIG_CONV
, totype
, expr
);
3195 ICS_USER_FLAG (cand
->second_conv
) = 1;
3200 for (p
= &(cand
->second_conv
); TREE_CODE (*p
) != IDENTITY_CONV
; )
3201 p
= &(TREE_OPERAND (*p
, 0));
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;
3214 build_user_type_conversion (totype
, expr
, flags
)
3215 tree totype
, expr
, flags
;
3217 struct z_candidate
*cand
3218 = build_user_type_conversion_1 (totype
, expr
, flags
);
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
));
3230 build_new_function_call (fn
, args
, obj
)
3233 struct z_candidate
*candidates
= 0, *cand
;
3235 if (obj
== NULL_TREE
&& TREE_CODE (fn
) == TREE_LIST
)
3237 tree t
= TREE_VALUE (fn
);
3239 for (; t
; t
= DECL_CHAIN (t
))
3241 if (TREE_CODE (t
) == TEMPLATE_DECL
)
3242 candidates
= add_template_candidate
3243 (candidates
, t
, args
, LOOKUP_NORMAL
);
3245 candidates
= add_function_candidate
3246 (candidates
, t
, args
, LOOKUP_NORMAL
);
3249 if (! any_viable (candidates
))
3251 if (candidates
&& ! candidates
->next
)
3252 return build_function_call (candidates
->fn
, args
);
3254 cp_error ("no viable candidates");
3255 return error_mark_node
;
3257 candidates
= splice_viable (candidates
);
3258 cand
= tourney (candidates
, NULL_TREE
);
3262 cp_error ("ambiguous function call");
3263 print_z_candidates (candidates
);
3264 return error_mark_node
;
3267 return build_over_call (cand
->fn
, cand
->convs
, args
, LOOKUP_NORMAL
);
3270 return build_function_call (fn
, args
);
3274 build_object_call (obj
, args
)
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
);
3282 fns
= lookup_fnfields (TYPE_BINFO (type
), ansi_opname
[CALL_EXPR
], 0);
3286 tree fn
= TREE_VALUE (fns
);
3287 mem_args
= tree_cons (NULL_TREE
, build_this (obj
), args
);
3289 for (; fn
; fn
= DECL_CHAIN (fn
))
3291 candidates
= add_function_candidate
3292 (candidates
, fn
, mem_args
, LOOKUP_NORMAL
);
3293 candidates
->basetype_path
= TREE_PURPOSE (fns
);
3297 convs
= lookup_conversions (type
);
3299 for (; convs
; convs
= TREE_CHAIN (convs
))
3301 tree fn
= TREE_VALUE (convs
);
3302 tree totype
= TREE_TYPE (TREE_TYPE (fn
));
3304 if (TREE_CODE (totype
) == POINTER_TYPE
3305 && TREE_CODE (TREE_TYPE (totype
)) == FUNCTION_TYPE
)
3307 candidates
= add_conv_candidate (candidates
, fn
, obj
, args
);
3308 candidates
->basetype_path
= TREE_PURPOSE (convs
);
3312 if (! any_viable (candidates
))
3314 cp_error ("no viable candidates");
3315 print_z_candidates (candidates
);
3316 return error_mark_node
;
3319 candidates
= splice_viable (candidates
);
3320 cand
= tourney (candidates
, NULL_TREE
);
3324 cp_error ("ambiguous object call");
3325 print_z_candidates (candidates
);
3326 return error_mark_node
;
3329 if (DECL_NAME (cand
->fn
) == ansi_opname
[CALL_EXPR
])
3330 return build_over_call (cand
->fn
, cand
->convs
, mem_args
, LOOKUP_NORMAL
);
3332 obj
= convert_like (TREE_VEC_ELT (cand
->convs
, 0), obj
);
3335 return build_function_call (obj
, args
);
3339 op_error (code
, code2
, arg1
, arg2
, arg3
, problem
)
3340 enum tree_code code
, code2
;
3341 tree arg1
, arg2
, arg3
;
3345 = (code
== MODIFY_EXPR
? assignop_tab
[code2
] : opname_tab
[code
]);
3350 cp_error ("%s for `%T ? %T : %T'", problem
,
3351 TREE_TYPE (arg1
), TREE_TYPE (arg2
), TREE_TYPE (arg3
));
3353 case POSTINCREMENT_EXPR
:
3354 case POSTDECREMENT_EXPR
:
3355 cp_error ("%s for `%T%s'", problem
, TREE_TYPE (arg1
), opname
);
3358 cp_error ("%s for `%T[%T]'", problem
,
3359 TREE_TYPE (arg1
), TREE_TYPE (arg2
));
3363 cp_error ("%s for `%T %s %T'", problem
,
3364 TREE_TYPE (arg1
), opname
, TREE_TYPE (arg2
));
3366 cp_error ("%s for `%s%T'", problem
, opname
, TREE_TYPE (arg1
));
3371 build_new_op (code
, flags
, arg1
, arg2
, arg3
)
3372 enum tree_code code
;
3374 tree arg1
, arg2
, arg3
;
3376 struct z_candidate
*candidates
= 0, *cand
;
3377 tree fns
, mem_arglist
, arglist
, fnname
, *p
;
3378 enum tree_code code2
= NOP_EXPR
;
3380 if (arg1
== error_mark_node
)
3381 return error_mark_node
;
3383 if (code
== MODIFY_EXPR
)
3385 code2
= TREE_CODE (arg3
);
3387 fnname
= ansi_assopname
[code2
];
3390 fnname
= ansi_opname
[code
];
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
);
3405 rval
= build_method_call
3406 (build_indirect_ref (build1 (NOP_EXPR
, arg1
, error_mark_node
),
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. */
3414 TREE_TYPE (rval
) = arg1
;
3415 TREE_CALLS_NEW (rval
) = 1;
3419 case VEC_DELETE_EXPR
:
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
);
3429 arglist
= tree_cons (NULL_TREE
, arg1
, build_tree_list (NULL_TREE
, arg2
));
3431 arg1
= TREE_TYPE (arg1
);
3433 /* This handles the case where we're trying to delete
3438 if (TREE_CODE (TREE_TYPE (arg1
)) == ARRAY_TYPE
)
3440 /* Strip off the pointer and the array. */
3441 arg1
= TREE_TYPE (TREE_TYPE (arg1
));
3443 while (TREE_CODE (arg1
) == ARRAY_TYPE
)
3444 arg1
= (TREE_TYPE (arg1
));
3446 arg1
= build_pointer_type (arg1
);
3450 rval
= build_method_call
3451 (build_indirect_ref (build1 (NOP_EXPR
, arg1
,
3454 fnname
, arglist
, NULL_TREE
, flags
);
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
;
3464 return build_object_call (arg1
, arg2
);
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
);
3475 if (! IS_OVERLOAD_TYPE (TREE_TYPE (arg1
))
3476 && (! arg2
|| ! IS_OVERLOAD_TYPE (TREE_TYPE (arg2
)))
3477 && (! arg3
|| ! IS_OVERLOAD_TYPE (TREE_TYPE (arg3
))))
3480 if (code
== POSTINCREMENT_EXPR
|| code
== POSTDECREMENT_EXPR
)
3481 arg2
= integer_zero_node
;
3483 fns
= lookup_name_nonclass (fnname
);
3484 /* + Koenig lookup */
3487 arglist
= tree_cons (NULL_TREE
, arg1
, tree_cons
3488 (NULL_TREE
, arg2
, build_tree_list (NULL_TREE
, arg3
)));
3490 arglist
= tree_cons (NULL_TREE
, arg1
, build_tree_list (NULL_TREE
, arg2
));
3492 arglist
= build_tree_list (NULL_TREE
, arg1
);
3494 if (fns
&& TREE_CODE (fns
) == TREE_LIST
)
3495 fns
= TREE_VALUE (fns
);
3496 for (; fns
; fns
= DECL_CHAIN (fns
))
3498 if (TREE_CODE (fns
) == TEMPLATE_DECL
)
3499 candidates
= add_template_candidate (candidates
, fns
, arglist
, flags
);
3501 candidates
= add_function_candidate (candidates
, fns
, arglist
, flags
);
3504 if (IS_AGGR_TYPE (TREE_TYPE (arg1
)))
3505 fns
= lookup_fnfields (TYPE_BINFO (TREE_TYPE (arg1
)), fnname
, 0);
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
))
3515 if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
3516 candidates
= add_function_candidate
3517 (candidates
, fn
, mem_arglist
, flags
);
3519 candidates
= add_function_candidate (candidates
, fn
, arglist
, flags
);
3521 candidates
->basetype_path
= TREE_PURPOSE (fns
);
3525 #if 0 /* Don't handle builtin COND_EXPR for now */
3526 if (code
!= COND_EXPR
)
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
)
3545 args
[2] = NULL_TREE
;
3548 candidates
= add_builtin_candidates
3549 (candidates
, code
, code2
, fnname
, args
, flags
);
3552 if (! any_viable (candidates
))
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
;
3566 code
= PREDECREMENT_EXPR
;
3567 return build_new_op (code
, flags
, arg1
, NULL_TREE
, NULL_TREE
);
3571 /*return build_unary_op (code, arg1, 1);*/
3573 /*return build (COMPOUND_EXPR, TREE_TYPE (arg2),
3574 break_out_cleanups (arg1), arg2);*/
3576 /*return build_x_arrow (arg1);*/
3577 #if 0 /* Don't handle builtin COND_EXPR for now */
3582 if (flags
& LOOKUP_COMPLAIN
)
3584 op_error (code
, code2
, arg1
, arg2
, arg3
, "no match");
3585 print_z_candidates (candidates
);
3587 return error_mark_node
;
3589 candidates
= splice_viable (candidates
);
3590 cand
= tourney (candidates
, NULL_TREE
);
3594 if (flags
& LOOKUP_COMPLAIN
)
3596 op_error (code
, code2
, arg1
, arg2
, arg3
, "ambiguous overload");
3597 print_z_candidates (candidates
);
3599 return error_mark_node
;
3602 if (TREE_CODE (cand
->fn
) == FUNCTION_DECL
)
3604 extern int warn_synth
;
3606 && fnname
== ansi_opname
[MODIFY_EXPR
]
3607 && DECL_ARTIFICIAL (cand
->fn
)
3609 && ! candidates
->next
->next
)
3611 cp_warning ("using synthesized `%#D' for copy assignment",
3613 cp_warning_at (" where cfront would use `%#D'",
3615 ? candidates
->next
->fn
3619 if (DECL_FUNCTION_MEMBER_P (cand
->fn
))
3620 enforce_access (cand
->basetype_path
, cand
->fn
);
3622 return build_over_call
3623 (cand
->fn
, cand
->convs
,
3624 TREE_CODE (TREE_TYPE (cand
->fn
)) == METHOD_TYPE
3625 ? mem_arglist
: arglist
,
3629 arg1
= convert_from_reference
3630 (convert_like (TREE_VEC_ELT (cand
->convs
, 0), arg1
));
3632 arg2
= convert_like (TREE_VEC_ELT (cand
->convs
, 1), arg2
);
3634 arg3
= convert_like (TREE_VEC_ELT (cand
->convs
, 2), arg3
);
3639 return build_modify_expr (arg1
, code2
, arg2
);
3642 return build_indirect_ref (arg1
, "unary *");
3647 case TRUNC_DIV_EXPR
:
3658 case TRUNC_MOD_EXPR
:
3662 case TRUTH_ANDIF_EXPR
:
3663 case TRUTH_ORIF_EXPR
:
3664 return build_binary_op_nodefault (code
, arg1
, arg2
, code
);
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);
3677 return build_array_ref (arg1
, arg2
);
3680 return build_conditional_expr (arg1
, arg2
, arg3
);
3683 my_friendly_abort (367);
3688 enforce_access (basetype_path
, function
)
3689 tree basetype_path
, function
;
3691 tree access
= compute_access (basetype_path
, function
);
3693 if (access
== access_private_node
)
3695 cp_error_at ("`%+#D' is %s", function
,
3696 TREE_PRIVATE (function
) ? "private"
3697 : "from private base class");
3698 error ("within this context");
3700 else if (access
== access_protected_node
)
3702 cp_error_at ("`%+#D' %s", function
,
3703 TREE_PROTECTED (function
) ? "is protected"
3704 : "has protected accessibility");
3705 error ("within this context");
3709 /* Perform the conversions in CONVS on the expression EXPR. */
3712 convert_like (convs
, expr
)
3715 switch (TREE_CODE (convs
))
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
);
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
);
3734 if (type_unknown_p (expr
))
3735 expr
= instantiate_type (TREE_TYPE (convs
), expr
, 1);
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
);
3743 expr
= convert_like (TREE_OPERAND (convs
, 0), expr
);
3744 if (expr
== error_mark_node
)
3745 return error_mark_node
;
3747 switch (TREE_CODE (convs
))
3751 return build_user_type_conversion
3752 (TREE_TYPE (convs
), expr
, LOOKUP_NORMAL
);
3754 return convert_to_reference
3755 (TREE_TYPE (convs
), expr
,
3756 CONV_IMPLICIT
, LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
|INDIRECT_BIND
,
3759 return decay_conversion (expr
);
3761 return cp_convert (TREE_TYPE (convs
), expr
, CONV_IMPLICIT
,
3762 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
3766 convert_default_arg (type
, arg
)
3769 arg
= break_out_target_exprs (arg
);
3771 if (TREE_CODE (arg
) == CONSTRUCTOR
)
3773 arg
= digest_init (type
, arg
, 0);
3774 arg
= convert_for_initialization (0, type
, arg
, LOOKUP_NORMAL
,
3775 "default argument", 0, 0);
3779 /* This could get clobbered by the following call. */
3780 if (TREE_HAS_CONSTRUCTOR (arg
))
3781 arg
= copy_node (arg
);
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
);
3797 build_over_call (fn
, convs
, args
, flags
)
3798 tree fn
, convs
, args
;
3801 tree converted_args
= NULL_TREE
;
3802 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3803 tree conv
, arg
, val
;
3806 if (args
&& TREE_CODE (args
) != TREE_LIST
)
3807 args
= build_tree_list (NULL_TREE
, args
);
3810 if (DECL_CONSTRUCTOR_P (fn
))
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
);
3817 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
3819 converted_args
= tree_cons
3820 (NULL_TREE
, integer_one_node
, converted_args
);
3821 parm
= TREE_CHAIN (parm
);
3824 /* Bypass access control for 'this' parameter. */
3825 else if (TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
3827 converted_args
= tree_cons
3828 (NULL_TREE
, convert_force (TREE_VALUE (parm
), TREE_VALUE (arg
), CONV_C_CAST
),
3830 parm
= TREE_CHAIN (parm
);
3831 arg
= TREE_CHAIN (arg
);
3835 for (; conv
= TREE_VEC_ELT (convs
, i
), arg
&& parm
;
3836 parm
= TREE_CHAIN (parm
), arg
= TREE_CHAIN (arg
), ++i
)
3838 tree type
= TREE_VALUE (parm
);
3839 val
= convert_like (conv
, TREE_VALUE (arg
));
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
);
3847 converted_args
= tree_cons (NULL_TREE
, val
, converted_args
);
3850 /* Default arguments */
3851 for (; parm
&& parm
!= void_list_node
; parm
= TREE_CHAIN (parm
))
3852 converted_args
= tree_cons
3854 convert_default_arg (TREE_VALUE (parm
), TREE_PURPOSE (parm
)),
3858 for (; arg
; arg
= TREE_CHAIN (arg
))
3860 val
= TREE_VALUE (arg
);
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 `...'",
3872 /* Convert `short' and `char' to full-size `int'. */
3873 val
= default_conversion (val
);
3875 converted_args
= tree_cons (NULL_TREE
, val
, converted_args
);
3878 converted_args
= nreverse (converted_args
);
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
);
3887 if (DECL_VINDEX (fn
) && (flags
& LOOKUP_NONVIRTUAL
) == 0)
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),
3895 else if (DECL_INLINE (fn
))
3896 fn
= inline_conversion (fn
);
3898 fn
= build_addr_func (fn
);
3900 fn
= build_call (fn
, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn
))), converted_args
);
3901 if (TREE_TYPE (fn
) == void_type_node
)
3903 return convert_from_reference (require_complete_type (fn
));
3906 /* Compare two implicit conversion sequences that differ only in their
3907 qualification conversion. Subroutine of compare_ics. */
3910 compare_qual (ics1
, ics2
)
3913 tree to1
= TREE_TYPE (ics1
);
3914 tree to2
= TREE_TYPE (ics2
);
3916 to1
= TREE_TYPE (to1
);
3917 to2
= TREE_TYPE (to2
);
3919 if (TREE_CODE (to1
) == OFFSET_TYPE
)
3921 to1
= TREE_TYPE (to1
);
3922 to2
= TREE_TYPE (to2
);
3925 if (TYPE_READONLY (to1
) >= TYPE_READONLY (to2
)
3926 && TYPE_VOLATILE (to1
) > TYPE_VOLATILE (to2
))
3928 else if (TYPE_READONLY (to1
) > TYPE_READONLY (to2
)
3929 && TYPE_VOLATILE (to1
) == TYPE_VOLATILE (to2
))
3931 else if (TYPE_READONLY (to1
) <= TYPE_READONLY (to2
)
3932 && TYPE_VOLATILE (to1
) < TYPE_VOLATILE (to2
))
3934 else if (TYPE_READONLY (to1
) < TYPE_READONLY (to2
)
3935 && TYPE_VOLATILE (to1
) == TYPE_VOLATILE (to2
))
3940 /* Compare two implicit conversion sequences according to the rules set out in
3941 [over.ics.rank]. Return values:
3943 1: ics1 is better than ics2
3944 -1: ics2 is better than ics1
3945 0: ics1 and ics2 are indistinguishable */
3948 compare_ics (ics1
, ics2
)
3953 if (ICS_RANK (ics1
) > ICS_RANK (ics2
))
3955 else if (ICS_RANK (ics1
) < ICS_RANK (ics2
))
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. */
3964 if (ICS_RANK (ics1
) == USER_RANK
)
3968 for (t1
= ics1
; TREE_CODE (t1
) != USER_CONV
; t1
= TREE_OPERAND (t1
, 0))
3969 if (TREE_CODE (t1
) == AMBIG_CONV
)
3971 for (t2
= ics2
; TREE_CODE (t2
) != USER_CONV
; t2
= TREE_OPERAND (t2
, 0))
3972 if (TREE_CODE (t2
) == AMBIG_CONV
)
3975 if (USER_CONV_FN (t1
) != USER_CONV_FN (t2
))
3977 else if (ICS_STD_RANK (ics1
) > ICS_STD_RANK (ics2
))
3979 else if (ICS_STD_RANK (ics1
) < ICS_STD_RANK (ics2
))
3982 /* else fall through */
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
3991 if (TREE_CODE (ics1
) == QUAL_CONV
)
3992 main1
= TREE_OPERAND (ics1
, 0);
3996 if (TREE_CODE (ics2
) == QUAL_CONV
)
3997 main2
= TREE_OPERAND (ics2
, 0);
4001 if (TREE_CODE (main1
) != TREE_CODE (main2
))
4004 if (TREE_CODE (main1
) == IDENTITY_CONV
4005 && (TREE_CODE (TREE_TYPE (main1
)) == POINTER_TYPE
4006 || TYPE_PTRMEMFUNC_P (TREE_TYPE (main1
))))
4008 if (TREE_TYPE (main1
) == TREE_TYPE (main2
))
4009 return compare_qual (ics1
, ics2
);
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
);
4019 if (TREE_CODE (main1
) == PTR_CONV
|| TREE_CODE (main1
) == PMEM_CONV
4020 || TREE_CODE (main1
) == REF_BIND
|| TREE_CODE (main1
) == BASE_CONV
)
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));
4028 /* Standard conversion sequence S1 is a better conversion sequence than
4029 standard conversion sequence S2 if...
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
4036 if (TREE_CODE (main1
) == REF_BIND
)
4038 if (TYPE_MAIN_VARIANT (TREE_TYPE (to1
))
4039 == TYPE_MAIN_VARIANT (TREE_TYPE (to2
)))
4040 return compare_qual (ics1
, ics2
);
4042 else if (TREE_CODE (main1
) != BASE_CONV
&& from1
== from2
&& to1
== to2
)
4043 return compare_qual (ics1
, ics2
);
4045 if (TYPE_PTRMEMFUNC_P (to1
))
4047 to1
= TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (to1
));
4048 from1
= TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (from1
));
4050 else if (TREE_CODE (main1
) != BASE_CONV
)
4052 to1
= TREE_TYPE (to1
);
4053 if (TREE_CODE (main1
) != REF_BIND
)
4054 from1
= TREE_TYPE (from1
);
4056 if (TREE_CODE (to1
) == OFFSET_TYPE
)
4058 to1
= TYPE_OFFSET_BASETYPE (to1
);
4059 from1
= TYPE_OFFSET_BASETYPE (from1
);
4063 if (TYPE_PTRMEMFUNC_P (to2
))
4065 to2
= TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (to2
));
4066 from2
= TYPE_METHOD_BASETYPE (TYPE_PTRMEMFUNC_FN_TYPE (from2
));
4068 else if (TREE_CODE (main1
) != BASE_CONV
)
4070 to2
= TREE_TYPE (to2
);
4071 if (TREE_CODE (main1
) != REF_BIND
)
4072 from2
= TREE_TYPE (from2
);
4074 if (TREE_CODE (to2
) == OFFSET_TYPE
)
4076 to2
= TYPE_OFFSET_BASETYPE (to2
);
4077 from2
= TYPE_OFFSET_BASETYPE (from2
);
4081 if (! (IS_AGGR_TYPE (from1
) && IS_AGGR_TYPE (from2
)))
4084 /* The sense of pmem conversions is reversed from that of the other
4086 if (TREE_CODE (main1
) == PMEM_CONV
)
4088 tree t
= from1
; from1
= from2
; from2
= t
;
4089 t
= to1
; to1
= to2
; to2
= t
;
4092 distf
= get_base_distance (from1
, from2
, 0, 0);
4095 distf
= -get_base_distance (from2
, from1
, 0, 0);
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*. */
4105 if (TREE_CODE (to1
) == VOID_TYPE
&& TREE_CODE (to2
) == VOID_TYPE
)
4112 else if (TREE_CODE (to2
) == VOID_TYPE
&& IS_AGGR_TYPE (to1
)
4113 && get_base_distance (to1
, from1
, 0, 0) != -1)
4115 else if (TREE_CODE (to1
) == VOID_TYPE
&& IS_AGGR_TYPE (to2
)
4116 && get_base_distance (to2
, from2
, 0, 0) != -1)
4119 if (! (IS_AGGR_TYPE (to1
) && IS_AGGR_TYPE (to2
)))
4122 /* If class B is derived directly or indirectly from class A and class
4123 C is derived directly or indirectly from B */
4125 distt
= get_base_distance (to1
, to2
, 0, 0);
4128 distt
= -get_base_distance (to2
, to1
, 0, 0);
4133 /* --conversion of C* to B* is better than conversion of C* to A*, */
4141 /* --conversion of B* to A* is better than conversion of C* to A*, */
4142 else if (distt
== 0)
4153 /* Compare two candidates for overloading as described in
4154 [over.match.best]. Return values:
4156 1: cand1 is better than cand2
4157 -1: cand2 is better than cand1
4158 0: cand1 and cand2 are indistinguishable */
4161 joust (cand1
, cand2
)
4162 struct z_candidate
*cand1
, *cand2
;
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 */
4172 /* for some argument j, ICSj(F1) is a better conversion sequence than
4175 for (i
= 0; i
< TREE_VEC_LENGTH (cand1
->convs
); ++i
)
4177 int comp
= compare_ics (TREE_VEC_ELT (cand1
->convs
, i
),
4178 TREE_VEC_ELT (cand2
->convs
, i
));
4182 if (winner
&& comp
!= winner
)
4192 F1 is a non-template function and F2 is a template function */
4194 if (! cand1
->template && cand2
->template)
4196 else if (cand1
->template && ! cand2
->template)
4198 else if (cand1
->template && cand2
->template)
4199 winner
= more_specialized
4200 (TI_TEMPLATE (cand1
->template), TI_TEMPLATE (cand2
->template));
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. */
4210 if (! winner
&& cand1
->second_conv
)
4211 winner
= compare_ics (cand1
->second_conv
, cand2
->second_conv
);
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
)
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))
4221 if (i
== TREE_VEC_LENGTH (cand1
->convs
))
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
4233 static struct z_candidate
*
4234 tourney (candidates
)
4235 struct z_candidate
*candidates
;
4237 struct z_candidate
*champ
= candidates
, *challenger
;
4240 /* Walk through the list once, comparing each current champ to the next
4241 candidate, knocking out a candidate or two with each comparison. */
4243 for (challenger
= champ
->next
; challenger
; )
4245 fate
= joust (champ
, challenger
);
4247 challenger
= challenger
->next
;
4252 champ
= challenger
->next
;
4259 challenger
= champ
->next
;
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
4267 for (challenger
= candidates
; challenger
!= champ
;
4268 challenger
= challenger
->next
)
4270 fate
= joust (champ
, challenger
);