* gcc.dg/pr26570.c: Clean up coverage files.
[official-gcc.git] / gcc / cp / cvt.c
blob220edddb535f622ba1a3fc9715deb33ab9ebc30c
1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC 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)
11 any later version.
13 GCC 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 GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, 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. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "convert.h"
37 #include "toplev.h"
38 #include "decl.h"
39 #include "target.h"
41 static tree cp_convert_to_pointer (tree, tree, bool);
42 static tree convert_to_pointer_force (tree, tree);
43 static tree build_type_conversion (tree, tree);
44 static tree build_up_reference (tree, tree, int, tree);
45 static void warn_ref_binding (tree, tree, tree);
47 /* Change of width--truncation and extension of integers or reals--
48 is represented with NOP_EXPR. Proper functioning of many things
49 assumes that no other conversions can be NOP_EXPRs.
51 Conversion between integer and pointer is represented with CONVERT_EXPR.
52 Converting integer to real uses FLOAT_EXPR
53 and real to integer uses FIX_TRUNC_EXPR.
55 Here is a list of all the functions that assume that widening and
56 narrowing is always done with a NOP_EXPR:
57 In convert.c, convert_to_integer.
58 In c-typeck.c, build_binary_op_nodefault (boolean ops),
59 and c_common_truthvalue_conversion.
60 In expr.c: expand_expr, for operands of a MULT_EXPR.
61 In fold-const.c: fold.
62 In tree.c: get_narrower and get_unwidened.
64 C++: in multiple-inheritance, converting between pointers may involve
65 adjusting them by a delta stored within the class definition. */
67 /* Subroutines of `convert'. */
69 /* if converting pointer to pointer
70 if dealing with classes, check for derived->base or vice versa
71 else if dealing with method pointers, delegate
72 else convert blindly
73 else if converting class, pass off to build_type_conversion
74 else try C-style pointer conversion. If FORCE is true then allow
75 conversions via virtual bases (these are permitted by reinterpret_cast,
76 but not static_cast). */
78 static tree
79 cp_convert_to_pointer (tree type, tree expr, bool force)
81 tree intype = TREE_TYPE (expr);
82 enum tree_code form;
83 tree rval;
84 if (intype == error_mark_node)
85 return error_mark_node;
87 if (IS_AGGR_TYPE (intype))
89 intype = complete_type (intype);
90 if (!COMPLETE_TYPE_P (intype))
92 error ("can't convert from incomplete type %qT to %qT",
93 intype, type);
94 return error_mark_node;
97 rval = build_type_conversion (type, expr);
98 if (rval)
100 if (rval == error_mark_node)
101 error ("conversion of %qE from %qT to %qT is ambiguous",
102 expr, intype, type);
103 return rval;
107 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
108 if (TREE_CODE (type) == POINTER_TYPE
109 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
110 || VOID_TYPE_P (TREE_TYPE (type))))
112 if (TYPE_PTRMEMFUNC_P (intype)
113 || TREE_CODE (intype) == METHOD_TYPE)
114 return convert_member_func_to_ptr (type, expr);
115 if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
116 return build_nop (type, expr);
117 intype = TREE_TYPE (expr);
120 if (expr == error_mark_node)
121 return error_mark_node;
123 form = TREE_CODE (intype);
125 if (POINTER_TYPE_P (intype))
127 intype = TYPE_MAIN_VARIANT (intype);
129 if (TYPE_MAIN_VARIANT (type) != intype
130 && TREE_CODE (type) == POINTER_TYPE
131 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
132 && IS_AGGR_TYPE (TREE_TYPE (type))
133 && IS_AGGR_TYPE (TREE_TYPE (intype))
134 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
136 enum tree_code code = PLUS_EXPR;
137 tree binfo;
138 tree intype_class;
139 tree type_class;
140 bool same_p;
142 intype_class = TREE_TYPE (intype);
143 type_class = TREE_TYPE (type);
145 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
146 TYPE_MAIN_VARIANT (type_class));
147 binfo = NULL_TREE;
148 /* Try derived to base conversion. */
149 if (!same_p)
150 binfo = lookup_base (intype_class, type_class, ba_check, NULL);
151 if (!same_p && !binfo)
153 /* Try base to derived conversion. */
154 binfo = lookup_base (type_class, intype_class, ba_check, NULL);
155 code = MINUS_EXPR;
157 if (binfo == error_mark_node)
158 return error_mark_node;
159 if (binfo || same_p)
161 if (binfo)
162 expr = build_base_path (code, expr, binfo, 0);
163 /* Add any qualifier conversions. */
164 return build_nop (type, expr);
168 if (TYPE_PTRMEMFUNC_P (type))
170 error ("cannot convert %qE from type %qT to type %qT",
171 expr, intype, type);
172 return error_mark_node;
175 return build_nop (type, expr);
177 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (intype))
179 tree b1;
180 tree b2;
181 tree binfo;
182 enum tree_code code = PLUS_EXPR;
183 base_kind bk;
185 b1 = TYPE_PTRMEM_CLASS_TYPE (type);
186 b2 = TYPE_PTRMEM_CLASS_TYPE (intype);
187 binfo = lookup_base (b1, b2, ba_check, &bk);
188 if (!binfo)
190 binfo = lookup_base (b2, b1, ba_check, &bk);
191 code = MINUS_EXPR;
193 if (binfo == error_mark_node)
194 return error_mark_node;
196 if (bk == bk_via_virtual)
198 if (force)
199 warning (0, "pointer to member cast from %qT to %qT is via"
200 " virtual base", intype, type);
201 else
203 error ("pointer to member cast from %qT to %qT is"
204 " via virtual base", intype, type);
205 return error_mark_node;
207 /* This is a reinterpret cast, whose result is unspecified.
208 We choose to do nothing. */
209 return build1 (NOP_EXPR, type, expr);
212 if (TREE_CODE (expr) == PTRMEM_CST)
213 expr = cplus_expand_constant (expr);
215 if (binfo && !integer_zerop (BINFO_OFFSET (binfo)))
216 expr = size_binop (code,
217 build_nop (sizetype, expr),
218 BINFO_OFFSET (binfo));
219 return build_nop (type, expr);
221 else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
222 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
223 /*c_cast_p=*/false);
224 else if (TYPE_PTRMEMFUNC_P (intype))
226 if (!warn_pmf2ptr)
228 if (TREE_CODE (expr) == PTRMEM_CST)
229 return cp_convert_to_pointer (type,
230 PTRMEM_CST_MEMBER (expr),
231 force);
232 else if (TREE_CODE (expr) == OFFSET_REF)
234 tree object = TREE_OPERAND (expr, 0);
235 return get_member_function_from_ptrfunc (&object,
236 TREE_OPERAND (expr, 1));
239 error ("cannot convert %qE from type %qT to type %qT",
240 expr, intype, type);
241 return error_mark_node;
244 if (integer_zerop (expr))
246 if (TYPE_PTRMEMFUNC_P (type))
247 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
248 /*c_cast_p=*/false);
250 if (TYPE_PTRMEM_P (type))
252 /* A NULL pointer-to-member is represented by -1, not by
253 zero. */
254 expr = build_int_cst_type (type, -1);
256 else
257 expr = build_int_cst (type, 0);
259 return expr;
261 else if (TYPE_PTR_TO_MEMBER_P (type) && INTEGRAL_CODE_P (form))
263 error ("invalid conversion from %qT to %qT", intype, type);
264 return error_mark_node;
267 if (INTEGRAL_CODE_P (form))
269 if (TYPE_PRECISION (intype) == POINTER_SIZE)
270 return build1 (CONVERT_EXPR, type, expr);
271 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr);
272 /* Modes may be different but sizes should be the same. There
273 is supposed to be some integral type that is the same width
274 as a pointer. */
275 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
276 == GET_MODE_SIZE (TYPE_MODE (type)));
278 return convert_to_pointer (type, expr);
281 if (type_unknown_p (expr))
282 return instantiate_type (type, expr, tf_warning_or_error);
284 error ("cannot convert %qE from type %qT to type %qT",
285 expr, intype, type);
286 return error_mark_node;
289 /* Like convert, except permit conversions to take place which
290 are not normally allowed due to access restrictions
291 (such as conversion from sub-type to private super-type). */
293 static tree
294 convert_to_pointer_force (tree type, tree expr)
296 tree intype = TREE_TYPE (expr);
297 enum tree_code form = TREE_CODE (intype);
299 if (form == POINTER_TYPE)
301 intype = TYPE_MAIN_VARIANT (intype);
303 if (TYPE_MAIN_VARIANT (type) != intype
304 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
305 && IS_AGGR_TYPE (TREE_TYPE (type))
306 && IS_AGGR_TYPE (TREE_TYPE (intype))
307 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
309 enum tree_code code = PLUS_EXPR;
310 tree binfo;
312 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
313 ba_unique, NULL);
314 if (!binfo)
316 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
317 ba_unique, NULL);
318 code = MINUS_EXPR;
320 if (binfo == error_mark_node)
321 return error_mark_node;
322 if (binfo)
324 expr = build_base_path (code, expr, binfo, 0);
325 if (expr == error_mark_node)
326 return error_mark_node;
327 /* Add any qualifier conversions. */
328 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
329 TREE_TYPE (type)))
330 expr = build_nop (type, expr);
331 return expr;
336 return cp_convert_to_pointer (type, expr, true);
339 /* We are passing something to a function which requires a reference.
340 The type we are interested in is in TYPE. The initial
341 value we have to begin with is in ARG.
343 FLAGS controls how we manage access checking.
344 DIRECT_BIND in FLAGS controls how any temporaries are generated.
345 If DIRECT_BIND is set, DECL is the reference we're binding to. */
347 static tree
348 build_up_reference (tree type, tree arg, int flags, tree decl)
350 tree rval;
351 tree argtype = TREE_TYPE (arg);
352 tree target_type = TREE_TYPE (type);
354 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
356 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
358 /* Create a new temporary variable. We can't just use a TARGET_EXPR
359 here because it needs to live as long as DECL. */
360 tree targ = arg;
362 arg = make_temporary_var_for_ref_to_temp (decl, TREE_TYPE (arg));
364 /* Process the initializer for the declaration. */
365 DECL_INITIAL (arg) = targ;
366 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
367 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
369 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
370 return get_target_expr (arg);
372 /* If we had a way to wrap this up, and say, if we ever needed its
373 address, transform all occurrences of the register, into a memory
374 reference we could win better. */
375 rval = build_unary_op (ADDR_EXPR, arg, 1);
376 if (rval == error_mark_node)
377 return error_mark_node;
379 if ((flags & LOOKUP_PROTECT)
380 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
381 && IS_AGGR_TYPE (argtype)
382 && IS_AGGR_TYPE (target_type))
384 /* We go through lookup_base for the access control. */
385 tree binfo = lookup_base (argtype, target_type, ba_check, NULL);
386 if (binfo == error_mark_node)
387 return error_mark_node;
388 if (binfo == NULL_TREE)
389 return error_not_base_type (target_type, argtype);
390 rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
392 else
393 rval
394 = convert_to_pointer_force (build_pointer_type (target_type), rval);
395 return build_nop (type, rval);
398 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
399 INTYPE is the original rvalue type and DECL is an optional _DECL node
400 for diagnostics.
402 [dcl.init.ref] says that if an rvalue is used to
403 initialize a reference, then the reference must be to a
404 non-volatile const type. */
406 static void
407 warn_ref_binding (tree reftype, tree intype, tree decl)
409 tree ttl = TREE_TYPE (reftype);
411 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
413 const char *msg;
415 if (CP_TYPE_VOLATILE_P (ttl) && decl)
416 msg = "initialization of volatile reference type %q#T from"
417 " rvalue of type %qT";
418 else if (CP_TYPE_VOLATILE_P (ttl))
419 msg = "conversion to volatile reference type %q#T "
420 " from rvalue of type %qT";
421 else if (decl)
422 msg = "initialization of non-const reference type %q#T from"
423 " rvalue of type %qT";
424 else
425 msg = "conversion to non-const reference type %q#T from"
426 " rvalue of type %qT";
428 pedwarn (msg, reftype, intype);
432 /* For C++: Only need to do one-level references, but cannot
433 get tripped up on signed/unsigned differences.
435 DECL is either NULL_TREE or the _DECL node for a reference that is being
436 initialized. It can be error_mark_node if we don't know the _DECL but
437 we know it's an initialization. */
439 tree
440 convert_to_reference (tree reftype, tree expr, int convtype,
441 int flags, tree decl)
443 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
444 tree intype;
445 tree rval = NULL_TREE;
446 tree rval_as_conversion = NULL_TREE;
447 bool can_convert_intype_to_type;
449 if (TREE_CODE (type) == FUNCTION_TYPE
450 && TREE_TYPE (expr) == unknown_type_node)
451 expr = instantiate_type (type, expr,
452 (flags & LOOKUP_COMPLAIN)
453 ? tf_warning_or_error : tf_none);
455 if (expr == error_mark_node)
456 return error_mark_node;
458 intype = TREE_TYPE (expr);
460 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
461 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
463 intype = TYPE_MAIN_VARIANT (intype);
465 can_convert_intype_to_type = can_convert (type, intype);
466 if (!can_convert_intype_to_type
467 && (convtype & CONV_IMPLICIT) && IS_AGGR_TYPE (intype)
468 && ! (flags & LOOKUP_NO_CONVERSION))
470 /* Look for a user-defined conversion to lvalue that we can use. */
472 rval_as_conversion
473 = build_type_conversion (reftype, expr);
475 if (rval_as_conversion && rval_as_conversion != error_mark_node
476 && real_lvalue_p (rval_as_conversion))
478 expr = rval_as_conversion;
479 rval_as_conversion = NULL_TREE;
480 intype = type;
481 can_convert_intype_to_type = 1;
485 if (((convtype & CONV_STATIC) && can_convert (intype, type))
486 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
488 if (flags & LOOKUP_COMPLAIN)
490 tree ttl = TREE_TYPE (reftype);
491 tree ttr = lvalue_type (expr);
493 if (! real_lvalue_p (expr))
494 warn_ref_binding (reftype, intype, decl);
496 if (! (convtype & CONV_CONST)
497 && !at_least_as_qualified_p (ttl, ttr))
498 pedwarn ("conversion from %qT to %qT discards qualifiers",
499 ttr, reftype);
502 return build_up_reference (reftype, expr, flags, decl);
504 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
506 /* When casting an lvalue to a reference type, just convert into
507 a pointer to the new type and deference it. This is allowed
508 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
509 should be done directly (jason). (int &)ri ---> *(int*)&ri */
511 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
512 meant. */
513 if (TREE_CODE (intype) == POINTER_TYPE
514 && (comptypes (TREE_TYPE (intype), type,
515 COMPARE_BASE | COMPARE_DERIVED)))
516 warning (0, "casting %qT to %qT does not dereference pointer",
517 intype, reftype);
519 rval = build_unary_op (ADDR_EXPR, expr, 0);
520 if (rval != error_mark_node)
521 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
522 rval, 0);
523 if (rval != error_mark_node)
524 rval = build1 (NOP_EXPR, reftype, rval);
526 else
528 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
529 "converting", 0, 0);
530 if (rval == NULL_TREE || rval == error_mark_node)
531 return rval;
532 warn_ref_binding (reftype, intype, decl);
533 rval = build_up_reference (reftype, rval, flags, decl);
536 if (rval)
538 /* If we found a way to convert earlier, then use it. */
539 return rval;
542 if (flags & LOOKUP_COMPLAIN)
543 error ("cannot convert type %qT to type %qT", intype, reftype);
545 return error_mark_node;
548 /* We are using a reference VAL for its value. Bash that reference all the
549 way down to its lowest form. */
551 tree
552 convert_from_reference (tree val)
554 if (TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
556 tree t = canonical_type_variant (TREE_TYPE (TREE_TYPE (val)));
557 tree ref = build1 (INDIRECT_REF, t, val);
559 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
560 so that we get the proper error message if the result is used
561 to assign to. Also, &* is supposed to be a no-op. */
562 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
563 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
564 TREE_SIDE_EFFECTS (ref)
565 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
566 REFERENCE_REF_P (ref) = 1;
567 val = ref;
570 return val;
573 /* Really perform an lvalue-to-rvalue conversion, including copying an
574 argument of class type into a temporary. */
576 tree
577 force_rvalue (tree expr)
579 if (IS_AGGR_TYPE (TREE_TYPE (expr)) && TREE_CODE (expr) != TARGET_EXPR)
580 expr = ocp_convert (TREE_TYPE (expr), expr,
581 CONV_IMPLICIT|CONV_FORCE_TEMP, LOOKUP_NORMAL);
582 else
583 expr = decay_conversion (expr);
585 return expr;
588 /* C++ conversions, preference to static cast conversions. */
590 tree
591 cp_convert (tree type, tree expr)
593 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL);
596 /* Conversion...
598 FLAGS indicates how we should behave. */
600 tree
601 ocp_convert (tree type, tree expr, int convtype, int flags)
603 tree e = expr;
604 enum tree_code code = TREE_CODE (type);
605 const char *invalid_conv_diag;
607 if (error_operand_p (e) || type == error_mark_node)
608 return error_mark_node;
610 complete_type (type);
611 complete_type (TREE_TYPE (expr));
613 if ((invalid_conv_diag
614 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
616 error (invalid_conv_diag);
617 return error_mark_node;
620 e = integral_constant_value (e);
622 if (IS_AGGR_TYPE (type) && (convtype & CONV_FORCE_TEMP)
623 /* Some internal structures (vtable_entry_type, sigtbl_ptr_type)
624 don't go through finish_struct, so they don't have the synthesized
625 constructors. So don't force a temporary. */
626 && TYPE_HAS_CONSTRUCTOR (type))
627 /* We need a new temporary; don't take this shortcut. */;
628 else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (e)))
630 if (same_type_p (type, TREE_TYPE (e)))
631 /* The call to fold will not always remove the NOP_EXPR as
632 might be expected, since if one of the types is a typedef;
633 the comparison in fold is just equality of pointers, not a
634 call to comptypes. We don't call fold in this case because
635 that can result in infinite recursion; fold will call
636 convert, which will call ocp_convert, etc. */
637 return e;
638 /* For complex data types, we need to perform componentwise
639 conversion. */
640 else if (TREE_CODE (type) == COMPLEX_TYPE)
641 return fold_if_not_in_template (convert_to_complex (type, e));
642 else if (TREE_CODE (e) == TARGET_EXPR)
644 /* Don't build a NOP_EXPR of class type. Instead, change the
645 type of the temporary. Only allow this for cv-qual changes,
646 though. */
647 gcc_assert (same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (e)),
648 TYPE_MAIN_VARIANT (type)));
649 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
650 return e;
652 else
654 /* We shouldn't be treating objects of ADDRESSABLE type as
655 rvalues. */
656 gcc_assert (!TREE_ADDRESSABLE (type));
657 return fold_if_not_in_template (build_nop (type, e));
661 if (code == VOID_TYPE && (convtype & CONV_STATIC))
663 e = convert_to_void (e, /*implicit=*/NULL);
664 return e;
667 if (INTEGRAL_CODE_P (code))
669 tree intype = TREE_TYPE (e);
670 /* enum = enum, enum = int, enum = float, (enum)pointer are all
671 errors. */
672 if (TREE_CODE (type) == ENUMERAL_TYPE
673 && (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
674 || TREE_CODE (intype) == REAL_TYPE)
675 && ! (convtype & CONV_STATIC))
676 || TREE_CODE (intype) == POINTER_TYPE))
678 if (flags & LOOKUP_COMPLAIN)
679 pedwarn ("conversion from %q#T to %q#T", intype, type);
681 if (flag_pedantic_errors)
682 return error_mark_node;
684 if (IS_AGGR_TYPE (intype))
686 tree rval;
687 rval = build_type_conversion (type, e);
688 if (rval)
689 return rval;
690 if (flags & LOOKUP_COMPLAIN)
691 error ("%q#T used where a %qT was expected", intype, type);
692 return error_mark_node;
694 if (code == BOOLEAN_TYPE)
695 return cp_truthvalue_conversion (e);
697 return fold_if_not_in_template (convert_to_integer (type, e));
699 if (POINTER_TYPE_P (type) || TYPE_PTR_TO_MEMBER_P (type))
700 return fold_if_not_in_template (cp_convert_to_pointer (type, e, false));
701 if (code == VECTOR_TYPE)
703 tree in_vtype = TREE_TYPE (e);
704 if (IS_AGGR_TYPE (in_vtype))
706 tree ret_val;
707 ret_val = build_type_conversion (type, e);
708 if (ret_val)
709 return ret_val;
710 if (flags & LOOKUP_COMPLAIN)
711 error ("%q#T used where a %qT was expected", in_vtype, type);
712 return error_mark_node;
714 return fold_if_not_in_template (convert_to_vector (type, e));
716 if (code == REAL_TYPE || code == COMPLEX_TYPE)
718 if (IS_AGGR_TYPE (TREE_TYPE (e)))
720 tree rval;
721 rval = build_type_conversion (type, e);
722 if (rval)
723 return rval;
724 else
725 if (flags & LOOKUP_COMPLAIN)
726 error ("%q#T used where a floating point value was expected",
727 TREE_TYPE (e));
729 if (code == REAL_TYPE)
730 return fold_if_not_in_template (convert_to_real (type, e));
731 else if (code == COMPLEX_TYPE)
732 return fold_if_not_in_template (convert_to_complex (type, e));
735 /* New C++ semantics: since assignment is now based on
736 memberwise copying, if the rhs type is derived from the
737 lhs type, then we may still do a conversion. */
738 if (IS_AGGR_TYPE_CODE (code))
740 tree dtype = TREE_TYPE (e);
741 tree ctor = NULL_TREE;
743 dtype = TYPE_MAIN_VARIANT (dtype);
745 /* Conversion between aggregate types. New C++ semantics allow
746 objects of derived type to be cast to objects of base type.
747 Old semantics only allowed this between pointers.
749 There may be some ambiguity between using a constructor
750 vs. using a type conversion operator when both apply. */
752 ctor = e;
754 if (abstract_virtuals_error (NULL_TREE, type))
755 return error_mark_node;
757 if ((flags & LOOKUP_ONLYCONVERTING)
758 && ! (IS_AGGR_TYPE (dtype) && DERIVED_FROM_P (type, dtype)))
759 /* For copy-initialization, first we create a temp of the proper type
760 with a user-defined conversion sequence, then we direct-initialize
761 the target with the temp (see [dcl.init]). */
762 ctor = build_user_type_conversion (type, ctor, flags);
763 else
764 ctor = build_special_member_call (NULL_TREE,
765 complete_ctor_identifier,
766 build_tree_list (NULL_TREE, ctor),
767 type, flags);
768 if (ctor)
769 return build_cplus_new (type, ctor);
772 if (flags & LOOKUP_COMPLAIN)
773 error ("conversion from %qT to non-scalar type %qT requested",
774 TREE_TYPE (expr), type);
775 return error_mark_node;
778 /* When an expression is used in a void context, its value is discarded and
779 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
780 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
781 in a void context. The C++ standard does not define what an `access' to an
782 object is, but there is reason to believe that it is the lvalue to rvalue
783 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
784 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
785 indicates that volatile semantics should be the same between C and C++
786 where ever possible. C leaves it implementation defined as to what
787 constitutes an access to a volatile. So, we interpret `*vp' as a read of
788 the volatile object `vp' points to, unless that is an incomplete type. For
789 volatile references we do not do this interpretation, because that would
790 make it impossible to ignore the reference return value from functions. We
791 issue warnings in the confusing cases.
793 IMPLICIT is tells us the context of an implicit void conversion. */
795 tree
796 convert_to_void (tree expr, const char *implicit)
798 if (expr == error_mark_node
799 || TREE_TYPE (expr) == error_mark_node)
800 return error_mark_node;
801 if (!TREE_TYPE (expr))
802 return expr;
803 if (invalid_nonstatic_memfn_p (expr))
804 return error_mark_node;
805 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
807 error ("pseudo-destructor is not called");
808 return error_mark_node;
810 if (VOID_TYPE_P (TREE_TYPE (expr)))
811 return expr;
812 switch (TREE_CODE (expr))
814 case COND_EXPR:
816 /* The two parts of a cond expr might be separate lvalues. */
817 tree op1 = TREE_OPERAND (expr,1);
818 tree op2 = TREE_OPERAND (expr,2);
819 tree new_op1 = convert_to_void
820 (op1, (implicit && !TREE_SIDE_EFFECTS (op2)
821 ? "second operand of conditional" : NULL));
822 tree new_op2 = convert_to_void
823 (op2, (implicit && !TREE_SIDE_EFFECTS (op1)
824 ? "third operand of conditional" : NULL));
826 expr = build3 (COND_EXPR, TREE_TYPE (new_op1),
827 TREE_OPERAND (expr, 0), new_op1, new_op2);
828 break;
831 case COMPOUND_EXPR:
833 /* The second part of a compound expr contains the value. */
834 tree op1 = TREE_OPERAND (expr,1);
835 tree new_op1 = convert_to_void
836 (op1, (implicit && !TREE_NO_WARNING (expr)
837 ? "right-hand operand of comma" : NULL));
839 if (new_op1 != op1)
841 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
842 TREE_OPERAND (expr, 0), new_op1);
843 expr = t;
846 break;
849 case NON_LVALUE_EXPR:
850 case NOP_EXPR:
851 /* These have already decayed to rvalue. */
852 break;
854 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
855 break;
857 case INDIRECT_REF:
859 tree type = TREE_TYPE (expr);
860 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
861 == REFERENCE_TYPE;
862 int is_volatile = TYPE_VOLATILE (type);
863 int is_complete = COMPLETE_TYPE_P (complete_type (type));
865 /* Can't load the value if we don't know the type. */
866 if (is_volatile && !is_complete)
867 warning (0, "object of incomplete type %qT will not be accessed in %s",
868 type, implicit ? implicit : "void context");
869 /* Don't load the value if this is an implicit dereference, or if
870 the type needs to be handled by ctors/dtors. */
871 else if (is_volatile && (is_reference || TREE_ADDRESSABLE (type)))
872 warning (0, "object of type %qT will not be accessed in %s",
873 TREE_TYPE (TREE_OPERAND (expr, 0)),
874 implicit ? implicit : "void context");
875 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
876 expr = TREE_OPERAND (expr, 0);
878 break;
881 case VAR_DECL:
883 /* External variables might be incomplete. */
884 tree type = TREE_TYPE (expr);
885 int is_complete = COMPLETE_TYPE_P (complete_type (type));
887 if (TYPE_VOLATILE (type) && !is_complete)
888 warning (0, "object %qE of incomplete type %qT will not be accessed in %s",
889 expr, type, implicit ? implicit : "void context");
890 break;
893 case TARGET_EXPR:
894 /* Don't bother with the temporary object returned from a function if
895 we don't use it and don't need to destroy it. We'll still
896 allocate space for it in expand_call or declare_return_variable,
897 but we don't need to track it through all the tree phases. */
898 if (TARGET_EXPR_IMPLICIT_P (expr)
899 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
901 tree init = TARGET_EXPR_INITIAL (expr);
902 if (TREE_CODE (init) == AGGR_INIT_EXPR
903 && !AGGR_INIT_VIA_CTOR_P (init))
905 tree fn = TREE_OPERAND (init, 0);
906 expr = build3 (CALL_EXPR, TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
907 fn, TREE_OPERAND (init, 1), NULL_TREE);
910 break;
912 default:;
915 tree probe = expr;
917 if (TREE_CODE (probe) == ADDR_EXPR)
918 probe = TREE_OPERAND (expr, 0);
919 if (type_unknown_p (probe))
921 /* [over.over] enumerates the places where we can take the address
922 of an overloaded function, and this is not one of them. */
923 pedwarn ("%s cannot resolve address of overloaded function",
924 implicit ? implicit : "void cast");
925 expr = void_zero_node;
927 else if (implicit && probe == expr && is_overloaded_fn (probe))
929 /* Only warn when there is no &. */
930 warning (0, "%s is a reference, not call, to function %qE",
931 implicit, expr);
932 if (TREE_CODE (expr) == COMPONENT_REF)
933 expr = TREE_OPERAND (expr, 0);
937 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
939 if (implicit
940 && warn_unused_value
941 && !TREE_NO_WARNING (expr)
942 && !processing_template_decl)
944 /* The middle end does not warn about expressions that have
945 been explicitly cast to void, so we must do so here. */
946 if (!TREE_SIDE_EFFECTS (expr))
947 warning (OPT_Wunused_value, "%s has no effect", implicit);
948 else
950 tree e;
951 enum tree_code code;
952 enum tree_code_class class;
954 e = expr;
955 /* We might like to warn about (say) "(int) f()", as the
956 cast has no effect, but the compiler itself will
957 generate implicit conversions under some
958 circumstances. (For example a block copy will be
959 turned into a call to "__builtin_memcpy", with a
960 conversion of the return value to an appropriate
961 type.) So, to avoid false positives, we strip
962 conversions. Do not use STRIP_NOPs because it will
963 not strip conversions to "void", as that is not a
964 mode-preserving conversion. */
965 while (TREE_CODE (e) == NOP_EXPR)
966 e = TREE_OPERAND (e, 0);
968 code = TREE_CODE (e);
969 class = TREE_CODE_CLASS (code);
970 if (class == tcc_comparison
971 || class == tcc_unary
972 || (class == tcc_binary
973 && !(code == MODIFY_EXPR
974 || code == INIT_EXPR
975 || code == PREDECREMENT_EXPR
976 || code == PREINCREMENT_EXPR
977 || code == POSTDECREMENT_EXPR
978 || code == POSTINCREMENT_EXPR)))
979 warning (OPT_Wunused_value, "value computed is not used");
982 expr = build1 (CONVERT_EXPR, void_type_node, expr);
984 if (! TREE_SIDE_EFFECTS (expr))
985 expr = void_zero_node;
986 return expr;
989 /* Create an expression whose value is that of EXPR,
990 converted to type TYPE. The TREE_TYPE of the value
991 is always TYPE. This function implements all reasonable
992 conversions; callers should filter out those that are
993 not permitted by the language being compiled.
995 Most of this routine is from build_reinterpret_cast.
997 The back end cannot call cp_convert (what was convert) because
998 conversions to/from basetypes may involve memory references
999 (vbases) and adding or subtracting small values (multiple
1000 inheritance), but it calls convert from the constant folding code
1001 on subtrees of already built trees after it has ripped them apart.
1003 Also, if we ever support range variables, we'll probably also have to
1004 do a little bit more work. */
1006 tree
1007 convert (tree type, tree expr)
1009 tree intype;
1011 if (type == error_mark_node || expr == error_mark_node)
1012 return error_mark_node;
1014 intype = TREE_TYPE (expr);
1016 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1017 return fold_if_not_in_template (build_nop (type, expr));
1019 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1020 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION);
1023 /* Like cp_convert, except permit conversions to take place which
1024 are not normally allowed due to access restrictions
1025 (such as conversion from sub-type to private super-type). */
1027 tree
1028 convert_force (tree type, tree expr, int convtype)
1030 tree e = expr;
1031 enum tree_code code = TREE_CODE (type);
1033 if (code == REFERENCE_TYPE)
1034 return (fold_if_not_in_template
1035 (convert_to_reference (type, e, CONV_C_CAST, LOOKUP_COMPLAIN,
1036 NULL_TREE)));
1038 if (code == POINTER_TYPE)
1039 return fold_if_not_in_template (convert_to_pointer_force (type, e));
1041 /* From typeck.c convert_for_assignment */
1042 if (((TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE && TREE_CODE (e) == ADDR_EXPR
1043 && TREE_CODE (TREE_TYPE (e)) == POINTER_TYPE
1044 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1045 || integer_zerop (e)
1046 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1047 && TYPE_PTRMEMFUNC_P (type))
1048 /* compatible pointer to member functions. */
1049 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1050 /*c_cast_p=*/1);
1052 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL);
1055 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1056 exists, return the attempted conversion. This may
1057 return ERROR_MARK_NODE if the conversion is not
1058 allowed (references private members, etc).
1059 If no conversion exists, NULL_TREE is returned.
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.
1064 (jason 8/9/95) */
1066 static tree
1067 build_type_conversion (tree xtype, tree expr)
1069 /* C++: check to see if we can convert this aggregate type
1070 into the required type. */
1071 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL);
1074 /* Convert the given EXPR to one of a group of types suitable for use in an
1075 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1076 which indicates which types are suitable. If COMPLAIN is true, complain
1077 about ambiguity; otherwise, the caller will deal with it. */
1079 tree
1080 build_expr_type_conversion (int desires, tree expr, bool complain)
1082 tree basetype = TREE_TYPE (expr);
1083 tree conv = NULL_TREE;
1084 tree winner = NULL_TREE;
1086 if (expr == null_node
1087 && (desires & WANT_INT)
1088 && !(desires & WANT_NULL))
1089 warning (OPT_Wconversion, "converting NULL to non-pointer type");
1091 basetype = TREE_TYPE (expr);
1093 if (basetype == error_mark_node)
1094 return error_mark_node;
1096 if (! IS_AGGR_TYPE (basetype))
1097 switch (TREE_CODE (basetype))
1099 case INTEGER_TYPE:
1100 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1101 return expr;
1102 /* else fall through... */
1104 case BOOLEAN_TYPE:
1105 return (desires & WANT_INT) ? expr : NULL_TREE;
1106 case ENUMERAL_TYPE:
1107 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1108 case REAL_TYPE:
1109 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1110 case POINTER_TYPE:
1111 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1113 case FUNCTION_TYPE:
1114 case ARRAY_TYPE:
1115 return (desires & WANT_POINTER) ? decay_conversion (expr)
1116 : NULL_TREE;
1118 case VECTOR_TYPE:
1119 if ((desires & WANT_VECTOR) == 0)
1120 return NULL_TREE;
1121 switch (TREE_CODE (TREE_TYPE (basetype)))
1123 case INTEGER_TYPE:
1124 case BOOLEAN_TYPE:
1125 return (desires & WANT_INT) ? expr : NULL_TREE;
1126 case ENUMERAL_TYPE:
1127 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1128 case REAL_TYPE:
1129 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1130 default:
1131 return NULL_TREE;
1134 default:
1135 return NULL_TREE;
1138 /* The code for conversions from class type is currently only used for
1139 delete expressions. Other expressions are handled by build_new_op. */
1140 if (!complete_type_or_else (basetype, expr))
1141 return error_mark_node;
1142 if (!TYPE_HAS_CONVERSION (basetype))
1143 return NULL_TREE;
1145 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1147 int win = 0;
1148 tree candidate;
1149 tree cand = TREE_VALUE (conv);
1151 if (winner && winner == cand)
1152 continue;
1154 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1156 switch (TREE_CODE (candidate))
1158 case BOOLEAN_TYPE:
1159 case INTEGER_TYPE:
1160 win = (desires & WANT_INT); break;
1161 case ENUMERAL_TYPE:
1162 win = (desires & WANT_ENUM); break;
1163 case REAL_TYPE:
1164 win = (desires & WANT_FLOAT); break;
1165 case POINTER_TYPE:
1166 win = (desires & WANT_POINTER); break;
1168 case VECTOR_TYPE:
1169 if ((desires & WANT_VECTOR) == 0)
1170 break;
1171 switch (TREE_CODE (TREE_TYPE (candidate)))
1173 case BOOLEAN_TYPE:
1174 case INTEGER_TYPE:
1175 win = (desires & WANT_INT); break;
1176 case ENUMERAL_TYPE:
1177 win = (desires & WANT_ENUM); break;
1178 case REAL_TYPE:
1179 win = (desires & WANT_FLOAT); break;
1180 default:
1181 break;
1183 break;
1185 default:
1186 break;
1189 if (win)
1191 if (winner)
1193 if (complain)
1195 error ("ambiguous default type conversion from %qT",
1196 basetype);
1197 error (" candidate conversions include %qD and %qD",
1198 winner, cand);
1200 return error_mark_node;
1202 else
1203 winner = cand;
1207 if (winner)
1209 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1210 return build_user_type_conversion (type, expr, LOOKUP_NORMAL);
1213 return NULL_TREE;
1216 /* Implements integral promotion (4.1) and float->double promotion. */
1218 tree
1219 type_promotes_to (tree type)
1221 if (type == error_mark_node)
1222 return error_mark_node;
1224 type = TYPE_MAIN_VARIANT (type);
1226 /* bool always promotes to int (not unsigned), even if it's the same
1227 size. */
1228 if (type == boolean_type_node)
1229 type = integer_type_node;
1231 /* Normally convert enums to int, but convert wide enums to something
1232 wider. */
1233 else if (TREE_CODE (type) == ENUMERAL_TYPE
1234 || type == wchar_type_node)
1236 int precision = MAX (TYPE_PRECISION (type),
1237 TYPE_PRECISION (integer_type_node));
1238 tree totype = c_common_type_for_size (precision, 0);
1239 if (TYPE_UNSIGNED (type)
1240 && ! int_fits_type_p (TYPE_MAX_VALUE (type), totype))
1241 type = c_common_type_for_size (precision, 1);
1242 else
1243 type = totype;
1245 else if (c_promoting_integer_type_p (type))
1247 /* Retain unsignedness if really not getting bigger. */
1248 if (TYPE_UNSIGNED (type)
1249 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1250 type = unsigned_type_node;
1251 else
1252 type = integer_type_node;
1254 else if (type == float_type_node)
1255 type = double_type_node;
1257 return type;
1260 /* The routines below this point are carefully written to conform to
1261 the standard. They use the same terminology, and follow the rules
1262 closely. Although they are used only in pt.c at the moment, they
1263 should presumably be used everywhere in the future. */
1265 /* Attempt to perform qualification conversions on EXPR to convert it
1266 to TYPE. Return the resulting expression, or error_mark_node if
1267 the conversion was impossible. */
1269 tree
1270 perform_qualification_conversions (tree type, tree expr)
1272 tree expr_type;
1274 expr_type = TREE_TYPE (expr);
1276 if (same_type_p (type, expr_type))
1277 return expr;
1278 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1279 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1280 return build_nop (type, expr);
1281 else if (TYPE_PTR_TO_MEMBER_P (type)
1282 && TYPE_PTR_TO_MEMBER_P (expr_type)
1283 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1284 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1285 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1286 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1287 return build_nop (type, expr);
1288 else
1289 return error_mark_node;