1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file contains the functions for converting C expressions
25 to different data types. The only entry point is `convert'.
26 Every language front end must have a `convert' function
27 but what kind of conversions it does will depend on the language. */
38 static tree cp_convert_to_pointer
PARAMS ((tree
, tree
));
39 static tree convert_to_pointer_force
PARAMS ((tree
, tree
));
40 static tree build_up_reference
PARAMS ((tree
, tree
, int));
42 /* Change of width--truncation and extension of integers or reals--
43 is represented with NOP_EXPR. Proper functioning of many things
44 assumes that no other conversions can be NOP_EXPRs.
46 Conversion between integer and pointer is represented with CONVERT_EXPR.
47 Converting integer to real uses FLOAT_EXPR
48 and real to integer uses FIX_TRUNC_EXPR.
50 Here is a list of all the functions that assume that widening and
51 narrowing is always done with a NOP_EXPR:
52 In convert.c, convert_to_integer.
53 In c-typeck.c, build_binary_op_nodefault (boolean ops),
54 and truthvalue_conversion.
55 In expr.c: expand_expr, for operands of a MULT_EXPR.
56 In fold-const.c: fold.
57 In tree.c: get_narrower and get_unwidened.
59 C++: in multiple-inheritance, converting between pointers may involve
60 adjusting them by a delta stored within the class definition. */
62 /* Subroutines of `convert'. */
64 /* if converting pointer to pointer
65 if dealing with classes, check for derived->base or vice versa
66 else if dealing with method pointers, delegate
68 else if converting class, pass off to build_type_conversion
69 else try C-style pointer conversion */
72 cp_convert_to_pointer (type
, expr
)
75 register tree intype
= TREE_TYPE (expr
);
76 register enum tree_code form
;
79 if (IS_AGGR_TYPE (intype
))
81 intype
= complete_type (intype
);
82 if (!COMPLETE_TYPE_P (intype
))
84 cp_error ("can't convert from incomplete type `%T' to `%T'",
86 return error_mark_node
;
89 rval
= build_type_conversion (type
, expr
, 1);
92 if (rval
== error_mark_node
)
93 cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",
99 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
100 if (TREE_CODE (type
) == POINTER_TYPE
101 && (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
102 || VOID_TYPE_P (TREE_TYPE (type
))))
104 /* Allow an implicit this pointer for pointer to member
106 if (TYPE_PTRMEMFUNC_P (intype
))
108 tree fntype
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype
));
109 tree decl
= maybe_dummy_object (TYPE_METHOD_BASETYPE (fntype
), 0);
110 expr
= build (OFFSET_REF
, fntype
, decl
, expr
);
113 if (TREE_CODE (expr
) == OFFSET_REF
114 && TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
115 expr
= resolve_offset_ref (expr
);
116 if (TREE_CODE (TREE_TYPE (expr
)) == METHOD_TYPE
)
117 expr
= build_addr_func (expr
);
118 if (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
)
120 if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == METHOD_TYPE
)
121 if (pedantic
|| warn_pmf2ptr
)
122 cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr
),
124 return build1 (NOP_EXPR
, type
, expr
);
126 intype
= TREE_TYPE (expr
);
129 form
= TREE_CODE (intype
);
131 if (POINTER_TYPE_P (intype
))
133 intype
= TYPE_MAIN_VARIANT (intype
);
135 if (TYPE_MAIN_VARIANT (type
) != intype
136 && TREE_CODE (type
) == POINTER_TYPE
137 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
138 && IS_AGGR_TYPE (TREE_TYPE (type
))
139 && IS_AGGR_TYPE (TREE_TYPE (intype
))
140 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
141 /* If EXPR is NULL, then we don't need to do any arithmetic
146 The null pointer value is converted to the null pointer
147 value of the destination type. */
148 && !integer_zerop (expr
))
150 enum tree_code code
= PLUS_EXPR
;
151 tree binfo
= get_binfo (TREE_TYPE (type
), TREE_TYPE (intype
), 1);
152 if (binfo
== error_mark_node
)
153 return error_mark_node
;
154 if (binfo
== NULL_TREE
)
156 binfo
= get_binfo (TREE_TYPE (intype
), TREE_TYPE (type
), 1);
157 if (binfo
== error_mark_node
)
158 return error_mark_node
;
163 if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type
))
164 || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype
))
165 || ! BINFO_OFFSET_ZEROP (binfo
))
167 /* Need to get the path we took. */
170 if (code
== PLUS_EXPR
)
171 get_base_distance (TREE_TYPE (type
), TREE_TYPE (intype
),
174 get_base_distance (TREE_TYPE (intype
), TREE_TYPE (type
),
176 return build_vbase_path (code
, type
, expr
, path
, 0);
181 if (TYPE_PTRMEM_P (type
) && TYPE_PTRMEM_P (intype
))
188 b1
= TYPE_OFFSET_BASETYPE (TREE_TYPE (type
));
189 b2
= TYPE_OFFSET_BASETYPE (TREE_TYPE (intype
));
190 binfo
= get_binfo (b2
, b1
, 1);
192 if (binfo
== NULL_TREE
)
194 binfo
= get_binfo (b1
, b2
, 1);
200 if (binfo
== error_mark_node
)
201 return error_mark_node
;
203 if (binfo_from_vbase (binfo
))
205 cp_error ("conversion to `%T' from pointer to member of virtual base `%T'",
207 return error_mark_node
;
210 if (TREE_CODE (expr
) == PTRMEM_CST
)
211 expr
= cplus_expand_constant (expr
);
213 if (binfo
&& ! TREE_VIA_VIRTUAL (binfo
))
214 expr
= size_binop (code
, convert (sizetype
,expr
),
215 BINFO_OFFSET (binfo
));
217 else if (TYPE_PTRMEMFUNC_P (type
))
219 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
221 return error_mark_node
;
224 rval
= build1 (NOP_EXPR
, type
, expr
);
225 TREE_CONSTANT (rval
) = TREE_CONSTANT (expr
);
228 else if (TYPE_PTRMEMFUNC_P (type
) && TYPE_PTRMEMFUNC_P (intype
))
229 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0);
230 else if (TYPE_PTRMEMFUNC_P (intype
))
232 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
234 return error_mark_node
;
237 my_friendly_assert (form
!= OFFSET_TYPE
, 186);
239 if (integer_zerop (expr
))
241 if (TYPE_PTRMEMFUNC_P (type
))
242 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), expr
, 0);
244 if (flag_new_abi
&& TYPE_PTRMEM_P (type
))
245 /* Under the new ABI, a NULL pointer-to-member is represented
246 by -1, not by zero. */
247 expr
= build_int_2 (-1, -1);
249 expr
= build_int_2 (0, 0);
250 TREE_TYPE (expr
) = type
;
254 if (INTEGRAL_CODE_P (form
))
256 if (TYPE_PRECISION (intype
) == POINTER_SIZE
)
257 return build1 (CONVERT_EXPR
, type
, expr
);
258 expr
= cp_convert (type_for_size (POINTER_SIZE
, 0), expr
);
259 /* Modes may be different but sizes should be the same. */
260 if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr
)))
261 != GET_MODE_SIZE (TYPE_MODE (type
)))
262 /* There is supposed to be some integral type
263 that is the same width as a pointer. */
265 return convert_to_pointer (type
, expr
);
268 if (type_unknown_p (expr
))
269 return instantiate_type (type
, expr
, 1);
271 cp_error ("cannot convert `%E' from type `%T' to type `%T'",
273 return error_mark_node
;
276 /* Like convert, except permit conversions to take place which
277 are not normally allowed due to access restrictions
278 (such as conversion from sub-type to private super-type). */
281 convert_to_pointer_force (type
, expr
)
284 register tree intype
= TREE_TYPE (expr
);
285 register enum tree_code form
= TREE_CODE (intype
);
287 if (integer_zerop (expr
))
289 expr
= build_int_2 (0, 0);
290 TREE_TYPE (expr
) = type
;
294 if (form
== POINTER_TYPE
)
296 intype
= TYPE_MAIN_VARIANT (intype
);
298 if (TYPE_MAIN_VARIANT (type
) != intype
299 && TREE_CODE (TREE_TYPE (type
)) == RECORD_TYPE
300 && IS_AGGR_TYPE (TREE_TYPE (type
))
301 && IS_AGGR_TYPE (TREE_TYPE (intype
))
302 && TREE_CODE (TREE_TYPE (intype
)) == RECORD_TYPE
)
304 enum tree_code code
= PLUS_EXPR
;
306 int distance
= get_base_distance (TREE_TYPE (type
),
307 TREE_TYPE (intype
), 0, &path
);
311 cp_error ("type `%T' is ambiguous baseclass of `%s'",
313 TYPE_NAME_STRING (TREE_TYPE (intype
)));
314 return error_mark_node
;
318 distance
= get_base_distance (TREE_TYPE (intype
),
319 TREE_TYPE (type
), 0, &path
);
323 /* Doesn't need any special help from us. */
324 return build1 (NOP_EXPR
, type
, expr
);
328 return build_vbase_path (code
, type
, expr
, path
, 0);
332 return cp_convert_to_pointer (type
, expr
);
335 /* We are passing something to a function which requires a reference.
336 The type we are interested in is in TYPE. The initial
337 value we have to begin with is in ARG.
339 FLAGS controls how we manage access checking.
340 DIRECT_BIND in FLAGS controls how any temporaries are generated. */
343 build_up_reference (type
, arg
, flags
)
348 tree argtype
= TREE_TYPE (arg
);
349 tree target_type
= TREE_TYPE (type
);
350 tree stmt_expr
= NULL_TREE
;
352 my_friendly_assert (TREE_CODE (type
) == REFERENCE_TYPE
, 187);
354 if ((flags
& DIRECT_BIND
) && ! real_lvalue_p (arg
))
356 /* Create a new temporary variable. */
358 if (toplevel_bindings_p ())
359 arg
= get_temp_name (argtype
, 1);
362 arg
= pushdecl (build_decl (VAR_DECL
, NULL_TREE
, argtype
));
363 DECL_ARTIFICIAL (arg
) = 1;
366 /* Process the initializer for the declaration. */
367 DECL_INITIAL (arg
) = targ
;
368 cp_finish_decl (arg
, targ
, NULL_TREE
,
369 LOOKUP_ONLYCONVERTING
|DIRECT_BIND
);
371 else if (!(flags
& DIRECT_BIND
) && ! lvalue_p (arg
))
372 return get_target_expr (arg
);
374 /* If we had a way to wrap this up, and say, if we ever needed it's
375 address, transform all occurrences of the register, into a memory
376 reference we could win better. */
377 rval
= build_unary_op (ADDR_EXPR
, arg
, 1);
378 if (rval
== error_mark_node
)
379 return error_mark_node
;
381 if ((flags
& LOOKUP_PROTECT
)
382 && TYPE_MAIN_VARIANT (argtype
) != TYPE_MAIN_VARIANT (target_type
)
383 && IS_AGGR_TYPE (argtype
)
384 && IS_AGGR_TYPE (target_type
))
386 /* We go through get_binfo for the access control. */
387 tree binfo
= get_binfo (target_type
, argtype
, 1);
388 if (binfo
== error_mark_node
)
389 return error_mark_node
;
390 if (binfo
== NULL_TREE
)
391 return error_not_base_type (target_type
, argtype
);
392 rval
= convert_pointer_to_real (binfo
, rval
);
396 = convert_to_pointer_force (build_pointer_type (target_type
), rval
);
397 rval
= build1 (NOP_EXPR
, type
, rval
);
398 TREE_CONSTANT (rval
) = TREE_CONSTANT (TREE_OPERAND (rval
, 0));
400 /* If we created and initialized a new temporary variable, add the
401 representation of that initialization to the RVAL. */
403 rval
= build (COMPOUND_EXPR
, TREE_TYPE (rval
), stmt_expr
, rval
);
405 /* And return the result. */
409 /* For C++: Only need to do one-level references, but cannot
410 get tripped up on signed/unsigned differences.
412 DECL is either NULL_TREE or the _DECL node for a reference that is being
413 initialized. It can be error_mark_node if we don't know the _DECL but
414 we know it's an initialization. */
417 convert_to_reference (reftype
, expr
, convtype
, flags
, decl
)
422 register tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (reftype
));
423 register tree intype
= TREE_TYPE (expr
);
424 tree rval
= NULL_TREE
;
425 tree rval_as_conversion
= NULL_TREE
;
428 if (TREE_CODE (type
) == FUNCTION_TYPE
&& intype
== unknown_type_node
)
430 expr
= instantiate_type (type
, expr
,
431 (flags
& LOOKUP_COMPLAIN
) != 0);
432 if (expr
== error_mark_node
)
433 return error_mark_node
;
435 intype
= TREE_TYPE (expr
);
438 if (TREE_CODE (intype
) == REFERENCE_TYPE
)
439 my_friendly_abort (364);
441 intype
= TYPE_MAIN_VARIANT (intype
);
443 i
= comp_target_types (type
, intype
, 0);
445 if (i
<= 0 && (convtype
& CONV_IMPLICIT
) && IS_AGGR_TYPE (intype
)
446 && ! (flags
& LOOKUP_NO_CONVERSION
))
448 /* Look for a user-defined conversion to lvalue that we can use. */
451 = build_type_conversion (reftype
, expr
, 1);
453 if (rval_as_conversion
&& rval_as_conversion
!= error_mark_node
454 && real_lvalue_p (rval_as_conversion
))
456 expr
= rval_as_conversion
;
457 rval_as_conversion
= NULL_TREE
;
463 if (((convtype
& CONV_STATIC
) && i
== -1)
464 || ((convtype
& CONV_IMPLICIT
) && i
== 1))
466 if (flags
& LOOKUP_COMPLAIN
)
468 tree ttl
= TREE_TYPE (reftype
);
469 tree ttr
= lvalue_type (expr
);
471 /* [dcl.init.ref] says that if an rvalue is used to
472 initialize a reference, then the reference must be to a
473 non-volatile const type. */
474 if (! real_lvalue_p (expr
)
475 && !CP_TYPE_CONST_NON_VOLATILE_P (ttl
))
479 if (CP_TYPE_VOLATILE_P (ttl
) && decl
)
480 msg
= "initialization of volatile reference type `%#T'";
481 else if (CP_TYPE_VOLATILE_P (ttl
))
482 msg
= "conversion to volatile reference type `%#T'";
484 msg
= "initialization of non-const reference type `%#T'";
486 msg
= "conversion to non-const reference type `%#T'";
488 cp_pedwarn (msg
, reftype
);
489 cp_pedwarn ("from rvalue of type `%T'", intype
);
491 else if (! (convtype
& CONV_CONST
)
492 && !at_least_as_qualified_p (ttl
, ttr
))
493 cp_pedwarn ("conversion from `%T' to `%T' discards qualifiers",
497 return build_up_reference (reftype
, expr
, flags
);
499 else if ((convtype
& CONV_REINTERPRET
) && lvalue_p (expr
))
501 /* When casting an lvalue to a reference type, just convert into
502 a pointer to the new type and deference it. This is allowed
503 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
504 should be done directly (jason). (int &)ri ---> *(int*)&ri */
506 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
508 if (TREE_CODE (intype
) == POINTER_TYPE
509 && (comptypes (TREE_TYPE (intype
), type
,
510 COMPARE_BASE
| COMPARE_RELAXED
)))
511 cp_warning ("casting `%T' to `%T' does not dereference pointer",
514 rval
= build_unary_op (ADDR_EXPR
, expr
, 0);
515 if (rval
!= error_mark_node
)
516 rval
= convert_force (build_pointer_type (TREE_TYPE (reftype
)),
518 if (rval
!= error_mark_node
)
519 rval
= build1 (NOP_EXPR
, reftype
, rval
);
523 rval
= convert_for_initialization (NULL_TREE
, type
, expr
, flags
,
525 if (rval
== NULL_TREE
|| rval
== error_mark_node
)
527 rval
= build_up_reference (reftype
, rval
, flags
);
529 if (rval
&& ! CP_TYPE_CONST_P (TREE_TYPE (reftype
)))
530 cp_pedwarn ("initializing non-const `%T' with `%T' will use a temporary",
536 /* If we found a way to convert earlier, then use it. */
540 my_friendly_assert (TREE_CODE (intype
) != OFFSET_TYPE
, 189);
542 if (flags
& LOOKUP_COMPLAIN
)
543 cp_error ("cannot convert type `%T' to type `%T'", intype
, reftype
);
545 if (flags
& LOOKUP_SPECULATIVELY
)
548 return error_mark_node
;
551 /* We are using a reference VAL for its value. Bash that reference all the
552 way down to its lowest form. */
555 convert_from_reference (val
)
558 tree type
= TREE_TYPE (val
);
560 if (TREE_CODE (type
) == OFFSET_TYPE
)
561 type
= TREE_TYPE (type
);
562 if (TREE_CODE (type
) == REFERENCE_TYPE
)
563 return build_indirect_ref (val
, NULL_PTR
);
567 /* Call this when we know (for any reason) that expr is not, in fact,
568 zero. This routine is like convert_pointer_to, but it pays
569 attention to which specific instance of what type we want to
570 convert to. This routine should eventually become
571 convert_to_pointer after all references to convert_to_pointer
575 convert_pointer_to_real (binfo
, expr
)
578 register tree intype
= TREE_TYPE (expr
);
582 if (intype
== error_mark_node
)
583 return error_mark_node
;
585 if (TREE_CODE (binfo
) == TREE_VEC
)
586 type
= BINFO_TYPE (binfo
);
587 else if (IS_AGGR_TYPE (binfo
))
597 ptr_type
= cp_build_qualified_type (type
,
598 CP_TYPE_QUALS (TREE_TYPE (intype
)));
599 ptr_type
= build_pointer_type (ptr_type
);
600 if (same_type_p (ptr_type
, TYPE_MAIN_VARIANT (intype
)))
603 my_friendly_assert (!integer_zerop (expr
), 191);
605 intype
= TYPE_MAIN_VARIANT (TREE_TYPE (intype
));
606 if (TREE_CODE (type
) == RECORD_TYPE
607 && TREE_CODE (intype
) == RECORD_TYPE
612 = get_base_distance (binfo
, intype
, 0, &path
);
614 /* This function shouldn't be called with unqualified arguments
615 but if it is, give them an error message that they can read. */
618 cp_error ("cannot convert a pointer of type `%T' to a pointer of type `%T'",
622 cp_error ("because `%T' is an ambiguous base class", type
);
623 return error_mark_node
;
626 return build_vbase_path (PLUS_EXPR
, ptr_type
, expr
, path
, 1);
628 rval
= build1 (NOP_EXPR
, ptr_type
,
629 TREE_CODE (expr
) == NOP_EXPR
? TREE_OPERAND (expr
, 0) : expr
);
630 TREE_CONSTANT (rval
) = TREE_CONSTANT (expr
);
634 /* Call this when we know (for any reason) that expr is
635 not, in fact, zero. This routine gets a type out of the first
636 argument and uses it to search for the type to convert to. If there
637 is more than one instance of that type in the expr, the conversion is
638 ambiguous. This routine should eventually go away, and all
639 callers should use convert_to_pointer_real. */
642 convert_pointer_to (binfo
, expr
)
645 return convert_pointer_to_real (binfo
, expr
);
648 /* C++ conversions, preference to static cast conversions. */
651 cp_convert (type
, expr
)
654 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
, LOOKUP_NORMAL
);
659 FLAGS indicates how we should behave. */
662 ocp_convert (type
, expr
, convtype
, flags
)
666 register tree e
= expr
;
667 register enum tree_code code
= TREE_CODE (type
);
669 if (e
== error_mark_node
670 || TREE_TYPE (e
) == error_mark_node
)
671 return error_mark_node
;
673 complete_type (type
);
674 complete_type (TREE_TYPE (expr
));
676 e
= decl_constant_value (e
);
678 if (IS_AGGR_TYPE (type
) && (convtype
& CONV_FORCE_TEMP
)
679 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
680 don't go through finish_struct, so they don't have the synthesized
681 constructors. So don't force a temporary. */
682 && TYPE_HAS_CONSTRUCTOR (type
))
683 /* We need a new temporary; don't take this shortcut. */;
684 else if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (TREE_TYPE (e
)))
686 if (same_type_p (type
, TREE_TYPE (e
)))
687 /* The call to fold will not always remove the NOP_EXPR as
688 might be expected, since if one of the types is a typedef;
689 the comparsion in fold is just equality of pointers, not a
690 call to comptypes. We don't call fold in this case because
691 that can result in infinite recursion; fold will call
692 convert, which will call ocp_convert, etc. */
694 /* For complex data types, we need to perform componentwise
696 else if (TREE_CODE (type
) == COMPLEX_TYPE
)
697 return fold (convert_to_complex (type
, e
));
699 return fold (build1 (NOP_EXPR
, type
, e
));
702 if (code
== VOID_TYPE
&& (convtype
& CONV_STATIC
))
704 e
= convert_to_void (e
, /*implicit=*/NULL
);
708 /* Just convert to the type of the member. */
709 if (code
== OFFSET_TYPE
)
711 type
= TREE_TYPE (type
);
712 code
= TREE_CODE (type
);
715 if (TREE_CODE (e
) == OFFSET_REF
)
716 e
= resolve_offset_ref (e
);
718 if (INTEGRAL_CODE_P (code
))
720 tree intype
= TREE_TYPE (e
);
721 /* enum = enum, enum = int, enum = float, (enum)pointer are all
723 if (TREE_CODE (type
) == ENUMERAL_TYPE
724 && ((ARITHMETIC_TYPE_P (intype
) && ! (convtype
& CONV_STATIC
))
725 || (TREE_CODE (intype
) == POINTER_TYPE
)))
727 cp_pedwarn ("conversion from `%#T' to `%#T'", intype
, type
);
729 if (flag_pedantic_errors
)
730 return error_mark_node
;
732 if (IS_AGGR_TYPE (intype
))
735 rval
= build_type_conversion (type
, e
, 1);
738 if (flags
& LOOKUP_COMPLAIN
)
739 cp_error ("`%#T' used where a `%T' was expected", intype
, type
);
740 if (flags
& LOOKUP_SPECULATIVELY
)
742 return error_mark_node
;
744 if (code
== BOOLEAN_TYPE
)
748 /* Common Ada/Pascal programmer's mistake. We always warn
749 about this since it is so bad. */
750 if (TREE_CODE (expr
) == FUNCTION_DECL
)
752 else if (TREE_CODE (expr
) == ADDR_EXPR
753 && TREE_CODE (TREE_OPERAND (expr
, 0)) == FUNCTION_DECL
)
754 fn
= TREE_OPERAND (expr
, 0);
756 cp_warning ("the address of `%D', will always be `true'", fn
);
757 return truthvalue_conversion (e
);
759 return fold (convert_to_integer (type
, e
));
761 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
762 || TYPE_PTRMEMFUNC_P (type
))
763 return fold (cp_convert_to_pointer (type
, e
));
764 if (code
== REAL_TYPE
|| code
== COMPLEX_TYPE
)
766 if (IS_AGGR_TYPE (TREE_TYPE (e
)))
769 rval
= build_type_conversion (type
, e
, 1);
773 if (flags
& LOOKUP_COMPLAIN
)
774 cp_error ("`%#T' used where a floating point value was expected",
777 if (code
== REAL_TYPE
)
778 return fold (convert_to_real (type
, e
));
779 else if (code
== COMPLEX_TYPE
)
780 return fold (convert_to_complex (type
, e
));
783 /* New C++ semantics: since assignment is now based on
784 memberwise copying, if the rhs type is derived from the
785 lhs type, then we may still do a conversion. */
786 if (IS_AGGR_TYPE_CODE (code
))
788 tree dtype
= TREE_TYPE (e
);
789 tree ctor
= NULL_TREE
;
791 dtype
= TYPE_MAIN_VARIANT (dtype
);
793 /* Conversion between aggregate types. New C++ semantics allow
794 objects of derived type to be cast to objects of base type.
795 Old semantics only allowed this between pointers.
797 There may be some ambiguity between using a constructor
798 vs. using a type conversion operator when both apply. */
802 if (abstract_virtuals_error (NULL_TREE
, type
))
803 return error_mark_node
;
805 if ((flags
& LOOKUP_ONLYCONVERTING
)
806 && ! (IS_AGGR_TYPE (dtype
) && DERIVED_FROM_P (type
, dtype
)))
807 /* For copy-initialization, first we create a temp of the proper type
808 with a user-defined conversion sequence, then we direct-initialize
809 the target with the temp (see [dcl.init]). */
810 ctor
= build_user_type_conversion (type
, ctor
, flags
);
812 ctor
= build_method_call (NULL_TREE
,
813 complete_ctor_identifier
,
814 build_tree_list (NULL_TREE
, ctor
),
815 TYPE_BINFO (type
), flags
);
817 return build_cplus_new (type
, ctor
);
820 /* If TYPE or TREE_TYPE (E) is not on the permanent_obstack,
821 then it won't be hashed and hence compare as not equal,
823 if (code
== ARRAY_TYPE
824 && TREE_TYPE (TREE_TYPE (e
)) == TREE_TYPE (type
)
825 && index_type_equal (TYPE_DOMAIN (TREE_TYPE (e
)), TYPE_DOMAIN (type
)))
828 if (flags
& LOOKUP_COMPLAIN
)
829 cp_error ("conversion from `%T' to non-scalar type `%T' requested",
830 TREE_TYPE (expr
), type
);
831 if (flags
& LOOKUP_SPECULATIVELY
)
833 return error_mark_node
;
836 /* When an expression is used in a void context, its value is discarded and
837 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
838 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
839 in a void context. The C++ standard does not define what an `access' to an
840 object is, but there is reason to beleive that it is the lvalue to rvalue
841 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
842 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
843 indicates that volatile semantics should be the same between C and C++
844 where ever possible. C leaves it implementation defined as to what
845 constitutes an access to a volatile. So, we interpret `*vp' as a read of
846 the volatile object `vp' points to, unless that is an incomplete type. For
847 volatile references we do not do this interpretation, because that would
848 make it impossible to ignore the reference return value from functions. We
849 issue warnings in the confusing cases.
851 IMPLICIT is tells us the context of an implicit void conversion. */
854 convert_to_void (expr
, implicit
)
856 const char *implicit
;
858 if (expr
== error_mark_node
859 || TREE_TYPE (expr
) == error_mark_node
)
860 return error_mark_node
;
861 if (!TREE_TYPE (expr
))
863 if (VOID_TYPE_P (TREE_TYPE (expr
)))
865 switch (TREE_CODE (expr
))
869 /* The two parts of a cond expr might be separate lvalues. */
870 tree op1
= TREE_OPERAND (expr
,1);
871 tree op2
= TREE_OPERAND (expr
,2);
872 tree new_op1
= convert_to_void (op1
, implicit
);
873 tree new_op2
= convert_to_void (op2
, implicit
);
875 if (new_op1
!= op1
|| new_op2
!= op2
)
876 expr
= build (COND_EXPR
,
877 implicit
? TREE_TYPE (expr
) : void_type_node
,
878 TREE_OPERAND (expr
, 0), new_op1
, new_op2
);
884 /* The second part of a compound expr contains the value. */
885 tree op1
= TREE_OPERAND (expr
,1);
886 tree new_op1
= convert_to_void (op1
, implicit
);
889 expr
= build (COMPOUND_EXPR
, TREE_TYPE (new_op1
),
890 TREE_OPERAND (expr
, 0), new_op1
);
894 case NON_LVALUE_EXPR
:
896 /* These have already decayed to rvalue. */
899 case CALL_EXPR
: /* we have a special meaning for volatile void fn() */
904 tree type
= TREE_TYPE (expr
);
905 int is_reference
= TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0)))
907 int is_volatile
= TYPE_VOLATILE (type
);
908 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
910 if (is_volatile
&& !is_complete
)
911 cp_warning ("object of incomplete type `%T' will not be accessed in %s",
912 type
, implicit
? implicit
: "void context");
913 else if (is_reference
&& is_volatile
)
914 cp_warning ("object of type `%T' will not be accessed in %s",
915 TREE_TYPE (TREE_OPERAND (expr
, 0)),
916 implicit
? implicit
: "void context");
917 if (is_reference
|| !is_volatile
|| !is_complete
)
918 expr
= TREE_OPERAND (expr
, 0);
925 /* External variables might be incomplete. */
926 tree type
= TREE_TYPE (expr
);
927 int is_complete
= COMPLETE_TYPE_P (complete_type (type
));
929 if (TYPE_VOLATILE (type
) && !is_complete
)
930 cp_warning ("object `%E' of incomplete type `%T' will not be accessed in %s",
931 expr
, type
, implicit
? implicit
: "void context");
936 expr
= resolve_offset_ref (expr
);
944 if (TREE_CODE (probe
) == ADDR_EXPR
)
945 probe
= TREE_OPERAND (expr
, 0);
946 if (!is_overloaded_fn (probe
))
948 else if (really_overloaded_fn (probe
))
950 /* [over.over] enumerates the places where we can take the address
951 of an overloaded function, and this is not one of them. */
952 cp_pedwarn ("%s has no context for overloaded function name `%E'",
953 implicit
? implicit
: "void cast", expr
);
955 else if (implicit
&& probe
== expr
)
956 /* Only warn when there is no &. */
957 cp_warning ("%s is a reference, not call, to function `%E'",
961 if (expr
!= error_mark_node
&& !VOID_TYPE_P (TREE_TYPE (expr
)))
963 /* FIXME: This is where we should check for expressions with no
964 effects. At the moment we do that in both build_x_component_expr
965 and expand_expr_stmt -- inconsistently too. For the moment
966 leave implicit void conversions unadorned so that expand_expr_stmt
967 has a chance of detecting some of the cases. */
969 expr
= build1 (CONVERT_EXPR
, void_type_node
, expr
);
974 /* Create an expression whose value is that of EXPR,
975 converted to type TYPE. The TREE_TYPE of the value
976 is always TYPE. This function implements all reasonable
977 conversions; callers should filter out those that are
978 not permitted by the language being compiled.
980 Most of this routine is from build_reinterpret_cast.
982 The backend cannot call cp_convert (what was convert) because
983 conversions to/from basetypes may involve memory references
984 (vbases) and adding or subtracting small values (multiple
985 inheritance), but it calls convert from the constant folding code
986 on subtrees of already build trees after it has ripped them apart.
988 Also, if we ever support range variables, we'll probably also have to
989 do a little bit more work. */
997 if (type
== error_mark_node
|| expr
== error_mark_node
)
998 return error_mark_node
;
1000 intype
= TREE_TYPE (expr
);
1002 if (POINTER_TYPE_P (type
) && POINTER_TYPE_P (intype
))
1004 expr
= decl_constant_value (expr
);
1005 return fold (build1 (NOP_EXPR
, type
, expr
));
1008 return ocp_convert (type
, expr
, CONV_OLD_CONVERT
,
1009 LOOKUP_NORMAL
|LOOKUP_NO_CONVERSION
);
1012 /* Like cp_convert, except permit conversions to take place which
1013 are not normally allowed due to access restrictions
1014 (such as conversion from sub-type to private super-type). */
1017 convert_force (type
, expr
, convtype
)
1022 register tree e
= expr
;
1023 register enum tree_code code
= TREE_CODE (type
);
1025 if (code
== REFERENCE_TYPE
)
1026 return fold (convert_to_reference (type
, e
, CONV_C_CAST
, LOOKUP_COMPLAIN
,
1028 else if (TREE_CODE (TREE_TYPE (e
)) == REFERENCE_TYPE
)
1029 e
= convert_from_reference (e
);
1031 if (code
== POINTER_TYPE
)
1032 return fold (convert_to_pointer_force (type
, e
));
1034 /* From typeck.c convert_for_assignment */
1035 if (((TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
&& TREE_CODE (e
) == ADDR_EXPR
1036 && TREE_CODE (TREE_TYPE (e
)) == POINTER_TYPE
1037 && TREE_CODE (TREE_TYPE (TREE_TYPE (e
))) == METHOD_TYPE
)
1038 || integer_zerop (e
)
1039 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e
)))
1040 && TYPE_PTRMEMFUNC_P (type
))
1042 /* compatible pointer to member functions. */
1043 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type
), e
, 1);
1046 return ocp_convert (type
, e
, CONV_C_CAST
|convtype
, LOOKUP_NORMAL
);
1049 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1050 exists, return the attempted conversion. This may
1051 return ERROR_MARK_NODE if the conversion is not
1052 allowed (references private members, etc).
1053 If no conversion exists, NULL_TREE is returned.
1055 If (FOR_SURE & 1) is non-zero, then we allow this type conversion
1056 to take place immediately. Otherwise, we build a SAVE_EXPR
1057 which can be evaluated if the results are ever needed.
1059 Changes to this functions should be mirrored in user_harshness.
1061 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1062 object parameter, or by the second standard conversion sequence if
1063 that doesn't do it. This will probably wait for an overloading rewrite.
1067 build_type_conversion (xtype
, expr
, for_sure
)
1071 /* C++: check to see if we can convert this aggregate type
1072 into the required type. */
1073 return build_user_type_conversion
1074 (xtype
, expr
, for_sure
? LOOKUP_NORMAL
: 0);
1077 /* Convert the given EXPR to one of a group of types suitable for use in an
1078 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1079 which indicates which types are suitable. If COMPLAIN is 1, complain
1080 about ambiguity; otherwise, the caller will deal with it. */
1083 build_expr_type_conversion (desires
, expr
, complain
)
1088 tree basetype
= TREE_TYPE (expr
);
1089 tree conv
= NULL_TREE
;
1090 tree winner
= NULL_TREE
;
1092 if (expr
== null_node
1093 && (desires
& WANT_INT
)
1094 && !(desires
& WANT_NULL
))
1095 cp_warning ("converting NULL to non-pointer type");
1097 if (TREE_CODE (expr
) == OFFSET_REF
)
1098 expr
= resolve_offset_ref (expr
);
1099 expr
= convert_from_reference (expr
);
1100 basetype
= TREE_TYPE (expr
);
1102 if (basetype
== error_mark_node
)
1103 return error_mark_node
;
1105 if (! IS_AGGR_TYPE (basetype
))
1106 switch (TREE_CODE (basetype
))
1109 if ((desires
& WANT_NULL
) && null_ptr_cst_p (expr
))
1111 /* else fall through... */
1114 return (desires
& WANT_INT
) ? expr
: NULL_TREE
;
1116 return (desires
& WANT_ENUM
) ? expr
: NULL_TREE
;
1118 return (desires
& WANT_FLOAT
) ? expr
: NULL_TREE
;
1120 return (desires
& WANT_POINTER
) ? expr
: NULL_TREE
;
1124 return (desires
& WANT_POINTER
) ? default_conversion (expr
)
1130 /* The code for conversions from class type is currently only used for
1131 delete expressions. Other expressions are handled by build_new_op. */
1133 if (! TYPE_HAS_CONVERSION (basetype
))
1136 for (conv
= lookup_conversions (basetype
); conv
; conv
= TREE_CHAIN (conv
))
1140 tree cand
= TREE_VALUE (conv
);
1142 if (winner
&& winner
== cand
)
1145 candidate
= TREE_TYPE (TREE_TYPE (cand
));
1146 if (TREE_CODE (candidate
) == REFERENCE_TYPE
)
1147 candidate
= TREE_TYPE (candidate
);
1149 switch (TREE_CODE (candidate
))
1153 win
= (desires
& WANT_INT
); break;
1155 win
= (desires
& WANT_ENUM
); break;
1157 win
= (desires
& WANT_FLOAT
); break;
1159 win
= (desires
& WANT_POINTER
); break;
1171 cp_error ("ambiguous default type conversion from `%T'",
1173 cp_error (" candidate conversions include `%D' and `%D'",
1176 return error_mark_node
;
1185 tree type
= TREE_TYPE (TREE_TYPE (winner
));
1186 if (TREE_CODE (type
) == REFERENCE_TYPE
)
1187 type
= TREE_TYPE (type
);
1188 return build_user_type_conversion (type
, expr
, LOOKUP_NORMAL
);
1194 /* Implements integral promotion (4.1) and float->double promotion. */
1197 type_promotes_to (type
)
1202 if (type
== error_mark_node
)
1203 return error_mark_node
;
1205 type_quals
= CP_TYPE_QUALS (type
);
1206 type
= TYPE_MAIN_VARIANT (type
);
1208 /* bool always promotes to int (not unsigned), even if it's the same
1210 if (type
== boolean_type_node
)
1211 type
= integer_type_node
;
1213 /* Normally convert enums to int, but convert wide enums to something
1215 else if (TREE_CODE (type
) == ENUMERAL_TYPE
1216 || type
== wchar_type_node
)
1218 int precision
= MAX (TYPE_PRECISION (type
),
1219 TYPE_PRECISION (integer_type_node
));
1220 tree totype
= type_for_size (precision
, 0);
1221 if (TREE_UNSIGNED (type
)
1222 && ! int_fits_type_p (TYPE_MAX_VALUE (type
), totype
))
1223 type
= type_for_size (precision
, 1);
1227 else if (C_PROMOTING_INTEGER_TYPE_P (type
))
1229 /* Retain unsignedness if really not getting bigger. */
1230 if (TREE_UNSIGNED (type
)
1231 && TYPE_PRECISION (type
) == TYPE_PRECISION (integer_type_node
))
1232 type
= unsigned_type_node
;
1234 type
= integer_type_node
;
1236 else if (type
== float_type_node
)
1237 type
= double_type_node
;
1239 return cp_build_qualified_type (type
, type_quals
);
1242 /* The routines below this point are carefully written to conform to
1243 the standard. They use the same terminology, and follow the rules
1244 closely. Although they are used only in pt.c at the moment, they
1245 should presumably be used everywhere in the future. */
1247 /* Attempt to perform qualification conversions on EXPR to convert it
1248 to TYPE. Return the resulting expression, or error_mark_node if
1249 the conversion was impossible. */
1252 perform_qualification_conversions (type
, expr
)
1256 if (TREE_CODE (type
) == POINTER_TYPE
1257 && TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
1258 && comp_ptr_ttypes (TREE_TYPE (type
), TREE_TYPE (TREE_TYPE (expr
))))
1259 return build1 (NOP_EXPR
, type
, expr
);
1261 return error_mark_node
;