* config/i386/i386.h (TARGET_SUPPORTS_WIDE_INT): New define.
[official-gcc.git] / gcc / cp / cvt.c
blob9aa9006f1928025e9db72127dcc87899c6f11eec
1 /* Language-level data type conversion for GNU C++.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC 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 3, or (at your option)
10 any later version.
12 GCC 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 GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file contains the functions for converting C++ expressions
23 to different data types. The only entry point is `convert'.
24 Every language front end must have a `convert' function
25 but what kind of conversions it does will depend on the language. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "vec.h"
34 #include "double-int.h"
35 #include "input.h"
36 #include "alias.h"
37 #include "symtab.h"
38 #include "wide-int.h"
39 #include "inchash.h"
40 #include "tree.h"
41 #include "stor-layout.h"
42 #include "flags.h"
43 #include "cp-tree.h"
44 #include "intl.h"
45 #include "convert.h"
46 #include "decl.h"
47 #include "target.h"
49 static tree cp_convert_to_pointer (tree, tree, tsubst_flags_t);
50 static tree convert_to_pointer_force (tree, tree, tsubst_flags_t);
51 static tree build_type_conversion (tree, tree);
52 static tree build_up_reference (tree, tree, int, tree, tsubst_flags_t);
53 static void diagnose_ref_binding (location_t, tree, tree, tree);
55 /* Change of width--truncation and extension of integers or reals--
56 is represented with NOP_EXPR. Proper functioning of many things
57 assumes that no other conversions can be NOP_EXPRs.
59 Conversion between integer and pointer is represented with CONVERT_EXPR.
60 Converting integer to real uses FLOAT_EXPR
61 and real to integer uses FIX_TRUNC_EXPR.
63 Here is a list of all the functions that assume that widening and
64 narrowing is always done with a NOP_EXPR:
65 In convert.c, convert_to_integer.
66 In c-typeck.c, build_binary_op_nodefault (boolean ops),
67 and c_common_truthvalue_conversion.
68 In expr.c: expand_expr, for operands of a MULT_EXPR.
69 In fold-const.c: fold.
70 In tree.c: get_narrower and get_unwidened.
72 C++: in multiple-inheritance, converting between pointers may involve
73 adjusting them by a delta stored within the class definition. */
75 /* Subroutines of `convert'. */
77 /* if converting pointer to pointer
78 if dealing with classes, check for derived->base or vice versa
79 else if dealing with method pointers, delegate
80 else convert blindly
81 else if converting class, pass off to build_type_conversion
82 else try C-style pointer conversion. */
84 static tree
85 cp_convert_to_pointer (tree type, tree expr, tsubst_flags_t complain)
87 tree intype = TREE_TYPE (expr);
88 enum tree_code form;
89 tree rval;
90 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
92 if (intype == error_mark_node)
93 return error_mark_node;
95 if (MAYBE_CLASS_TYPE_P (intype))
97 intype = complete_type (intype);
98 if (!COMPLETE_TYPE_P (intype))
100 if (complain & tf_error)
101 error_at (loc, "can%'t convert from incomplete type %qT to %qT",
102 intype, type);
103 return error_mark_node;
106 rval = build_type_conversion (type, expr);
107 if (rval)
109 if ((complain & tf_error)
110 && rval == error_mark_node)
111 error_at (loc, "conversion of %qE from %qT to %qT is ambiguous",
112 expr, intype, type);
113 return rval;
117 /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
118 if (TYPE_PTR_P (type)
119 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
120 || VOID_TYPE_P (TREE_TYPE (type))))
122 if (TYPE_PTRMEMFUNC_P (intype)
123 || TREE_CODE (intype) == METHOD_TYPE)
124 return convert_member_func_to_ptr (type, expr, complain);
125 if (TYPE_PTR_P (TREE_TYPE (expr)))
126 return build_nop (type, expr);
127 intype = TREE_TYPE (expr);
130 if (expr == error_mark_node)
131 return error_mark_node;
133 form = TREE_CODE (intype);
135 if (POINTER_TYPE_P (intype))
137 intype = TYPE_MAIN_VARIANT (intype);
139 if (TYPE_MAIN_VARIANT (type) != intype
140 && TYPE_PTR_P (type)
141 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
142 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
143 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
144 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
146 enum tree_code code = PLUS_EXPR;
147 tree binfo;
148 tree intype_class;
149 tree type_class;
150 bool same_p;
152 intype_class = TREE_TYPE (intype);
153 type_class = TREE_TYPE (type);
155 same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
156 TYPE_MAIN_VARIANT (type_class));
157 binfo = NULL_TREE;
158 /* Try derived to base conversion. */
159 if (!same_p)
160 binfo = lookup_base (intype_class, type_class, ba_check,
161 NULL, complain);
162 if (!same_p && !binfo)
164 /* Try base to derived conversion. */
165 binfo = lookup_base (type_class, intype_class, ba_check,
166 NULL, complain);
167 code = MINUS_EXPR;
169 if (binfo == error_mark_node)
170 return error_mark_node;
171 if (binfo || same_p)
173 if (binfo)
174 expr = build_base_path (code, expr, binfo, 0, complain);
175 /* Add any qualifier conversions. */
176 return build_nop (type, expr);
180 if (TYPE_PTRMEMFUNC_P (type))
182 if (complain & tf_error)
183 error_at (loc, "cannot convert %qE from type %qT to type %qT",
184 expr, intype, type);
185 return error_mark_node;
188 return build_nop (type, expr);
190 else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
191 || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
192 return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
193 /*c_cast_p=*/false, complain);
194 else if (TYPE_PTRMEMFUNC_P (intype))
196 if (!warn_pmf2ptr)
198 if (TREE_CODE (expr) == PTRMEM_CST)
199 return cp_convert_to_pointer (type, PTRMEM_CST_MEMBER (expr),
200 complain);
201 else if (TREE_CODE (expr) == OFFSET_REF)
203 tree object = TREE_OPERAND (expr, 0);
204 return get_member_function_from_ptrfunc (&object,
205 TREE_OPERAND (expr, 1),
206 complain);
209 if (complain & tf_error)
210 error_at (loc, "cannot convert %qE from type %qT to type %qT",
211 expr, intype, type);
212 return error_mark_node;
215 if (null_ptr_cst_p (expr))
217 if (TYPE_PTRMEMFUNC_P (type))
218 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
219 /*c_cast_p=*/false, complain);
221 if (complain & tf_warning)
222 maybe_warn_zero_as_null_pointer_constant (expr, loc);
224 /* A NULL pointer-to-data-member is represented by -1, not by
225 zero. */
226 tree val = (TYPE_PTRDATAMEM_P (type)
227 ? build_int_cst_type (type, -1)
228 : build_int_cst (type, 0));
230 return (TREE_SIDE_EFFECTS (expr)
231 ? build2 (COMPOUND_EXPR, type, expr, val) : val);
233 else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form))
235 if (complain & tf_error)
236 error_at (loc, "invalid conversion from %qT to %qT", intype, type);
237 return error_mark_node;
240 if (INTEGRAL_CODE_P (form))
242 if (TYPE_PRECISION (intype) == POINTER_SIZE)
243 return build1 (CONVERT_EXPR, type, expr);
244 expr = cp_convert (c_common_type_for_size (POINTER_SIZE, 0), expr,
245 complain);
246 /* Modes may be different but sizes should be the same. There
247 is supposed to be some integral type that is the same width
248 as a pointer. */
249 gcc_assert (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))
250 == GET_MODE_SIZE (TYPE_MODE (type)));
252 return convert_to_pointer (type, expr);
255 if (type_unknown_p (expr))
256 return instantiate_type (type, expr, complain);
258 if (complain & tf_error)
259 error_at (loc, "cannot convert %qE from type %qT to type %qT",
260 expr, intype, type);
261 return error_mark_node;
264 /* Like convert, except permit conversions to take place which
265 are not normally allowed due to access restrictions
266 (such as conversion from sub-type to private super-type). */
268 static tree
269 convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain)
271 tree intype = TREE_TYPE (expr);
272 enum tree_code form = TREE_CODE (intype);
274 if (form == POINTER_TYPE)
276 intype = TYPE_MAIN_VARIANT (intype);
278 if (TYPE_MAIN_VARIANT (type) != intype
279 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
280 && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
281 && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
282 && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
284 enum tree_code code = PLUS_EXPR;
285 tree binfo;
287 binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
288 ba_unique, NULL, complain);
289 if (!binfo)
291 binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
292 ba_unique, NULL, complain);
293 code = MINUS_EXPR;
295 if (binfo == error_mark_node)
296 return error_mark_node;
297 if (binfo)
299 expr = build_base_path (code, expr, binfo, 0, complain);
300 if (expr == error_mark_node)
301 return error_mark_node;
302 /* Add any qualifier conversions. */
303 if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
304 TREE_TYPE (type)))
305 expr = build_nop (type, expr);
306 return expr;
311 return cp_convert_to_pointer (type, expr, complain);
314 /* We are passing something to a function which requires a reference.
315 The type we are interested in is in TYPE. The initial
316 value we have to begin with is in ARG.
318 FLAGS controls how we manage access checking.
319 DIRECT_BIND in FLAGS controls how any temporaries are generated.
320 If DIRECT_BIND is set, DECL is the reference we're binding to. */
322 static tree
323 build_up_reference (tree type, tree arg, int flags, tree decl,
324 tsubst_flags_t complain)
326 tree rval;
327 tree argtype = TREE_TYPE (arg);
328 tree target_type = TREE_TYPE (type);
330 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
332 if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))
334 /* Create a new temporary variable. We can't just use a TARGET_EXPR
335 here because it needs to live as long as DECL. */
336 tree targ = arg;
338 arg = make_temporary_var_for_ref_to_temp (decl, target_type);
340 /* Process the initializer for the declaration. */
341 DECL_INITIAL (arg) = targ;
342 cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
343 LOOKUP_ONLYCONVERTING|DIRECT_BIND);
345 else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))
346 return get_target_expr_sfinae (arg, complain);
348 /* If we had a way to wrap this up, and say, if we ever needed its
349 address, transform all occurrences of the register, into a memory
350 reference we could win better. */
351 rval = cp_build_addr_expr (arg, complain);
352 if (rval == error_mark_node)
353 return error_mark_node;
355 if ((flags & LOOKUP_PROTECT)
356 && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
357 && MAYBE_CLASS_TYPE_P (argtype)
358 && MAYBE_CLASS_TYPE_P (target_type))
360 /* We go through lookup_base for the access control. */
361 tree binfo = lookup_base (argtype, target_type, ba_check,
362 NULL, complain);
363 if (binfo == error_mark_node)
364 return error_mark_node;
365 if (binfo == NULL_TREE)
366 return error_not_base_type (target_type, argtype);
367 rval = build_base_path (PLUS_EXPR, rval, binfo, 1, complain);
369 else
370 rval
371 = convert_to_pointer_force (build_pointer_type (target_type),
372 rval, complain);
373 return build_nop (type, rval);
376 /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
377 INTYPE is the original rvalue type and DECL is an optional _DECL node
378 for diagnostics.
380 [dcl.init.ref] says that if an rvalue is used to
381 initialize a reference, then the reference must be to a
382 non-volatile const type. */
384 static void
385 diagnose_ref_binding (location_t loc, tree reftype, tree intype, tree decl)
387 tree ttl = TREE_TYPE (reftype);
389 if (!CP_TYPE_CONST_NON_VOLATILE_P (ttl))
391 const char *msg;
393 if (CP_TYPE_VOLATILE_P (ttl) && decl)
394 msg = G_("initialization of volatile reference type %q#T from "
395 "rvalue of type %qT");
396 else if (CP_TYPE_VOLATILE_P (ttl))
397 msg = G_("conversion to volatile reference type %q#T "
398 "from rvalue of type %qT");
399 else if (decl)
400 msg = G_("initialization of non-const reference type %q#T from "
401 "rvalue of type %qT");
402 else
403 msg = G_("conversion to non-const reference type %q#T from "
404 "rvalue of type %qT");
406 permerror (loc, msg, reftype, intype);
410 /* For C++: Only need to do one-level references, but cannot
411 get tripped up on signed/unsigned differences.
413 DECL is either NULL_TREE or the _DECL node for a reference that is being
414 initialized. It can be error_mark_node if we don't know the _DECL but
415 we know it's an initialization. */
417 tree
418 convert_to_reference (tree reftype, tree expr, int convtype,
419 int flags, tree decl, tsubst_flags_t complain)
421 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
422 tree intype;
423 tree rval = NULL_TREE;
424 tree rval_as_conversion = NULL_TREE;
425 bool can_convert_intype_to_type;
426 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
428 if (TREE_CODE (type) == FUNCTION_TYPE
429 && TREE_TYPE (expr) == unknown_type_node)
430 expr = instantiate_type (type, expr, complain);
432 if (expr == error_mark_node)
433 return error_mark_node;
435 intype = TREE_TYPE (expr);
437 gcc_assert (TREE_CODE (intype) != REFERENCE_TYPE);
438 gcc_assert (TREE_CODE (reftype) == REFERENCE_TYPE);
440 intype = TYPE_MAIN_VARIANT (intype);
442 can_convert_intype_to_type = can_convert_standard (type, intype, complain);
444 if (!can_convert_intype_to_type
445 && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
446 && ! (flags & LOOKUP_NO_CONVERSION))
448 /* Look for a user-defined conversion to lvalue that we can use. */
450 rval_as_conversion
451 = build_type_conversion (reftype, expr);
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;
458 intype = type;
459 can_convert_intype_to_type = 1;
463 if (((convtype & CONV_STATIC)
464 && can_convert_standard (intype, type, complain))
465 || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
468 tree ttl = TREE_TYPE (reftype);
469 tree ttr = lvalue_type (expr);
471 if ((complain & tf_error)
472 && ! real_lvalue_p (expr))
473 diagnose_ref_binding (loc, reftype, intype, decl);
475 if (! (convtype & CONV_CONST)
476 && !at_least_as_qualified_p (ttl, ttr))
478 if (complain & tf_error)
479 permerror (loc, "conversion from %qT to %qT discards qualifiers",
480 ttr, reftype);
481 else
482 return error_mark_node;
486 return build_up_reference (reftype, expr, flags, decl, complain);
488 else if ((convtype & CONV_REINTERPRET) && lvalue_p (expr))
490 /* When casting an lvalue to a reference type, just convert into
491 a pointer to the new type and deference it. This is allowed
492 by San Diego WP section 5.2.9 paragraph 12, though perhaps it
493 should be done directly (jason). (int &)ri ---> *(int*)&ri */
495 /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
496 meant. */
497 if ((complain & tf_warning)
498 && TYPE_PTR_P (intype)
499 && (comptypes (TREE_TYPE (intype), type,
500 COMPARE_BASE | COMPARE_DERIVED)))
501 warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
502 intype, reftype);
504 rval = cp_build_addr_expr (expr, complain);
505 if (rval != error_mark_node)
506 rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
507 rval, 0, complain);
508 if (rval != error_mark_node)
509 rval = build1 (NOP_EXPR, reftype, rval);
511 else
513 rval = convert_for_initialization (NULL_TREE, type, expr, flags,
514 ICR_CONVERTING, 0, 0, complain);
515 if (rval == NULL_TREE || rval == error_mark_node)
516 return rval;
517 if (complain & tf_error)
518 diagnose_ref_binding (loc, reftype, intype, decl);
519 rval = build_up_reference (reftype, rval, flags, decl, complain);
522 if (rval)
524 /* If we found a way to convert earlier, then use it. */
525 return rval;
528 if (complain & tf_error)
529 error_at (loc, "cannot convert type %qT to type %qT", intype, reftype);
531 return error_mark_node;
534 /* We are using a reference VAL for its value. Bash that reference all the
535 way down to its lowest form. */
537 tree
538 convert_from_reference (tree val)
540 if (TREE_TYPE (val)
541 && TREE_CODE (TREE_TYPE (val)) == REFERENCE_TYPE)
543 tree t = TREE_TYPE (TREE_TYPE (val));
544 tree ref = build1 (INDIRECT_REF, t, val);
546 mark_exp_read (val);
547 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
548 so that we get the proper error message if the result is used
549 to assign to. Also, &* is supposed to be a no-op. */
550 TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
551 TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
552 TREE_SIDE_EFFECTS (ref)
553 = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
554 val = ref;
557 return val;
560 /* Really perform an lvalue-to-rvalue conversion, including copying an
561 argument of class type into a temporary. */
563 tree
564 force_rvalue (tree expr, tsubst_flags_t complain)
566 tree type = TREE_TYPE (expr);
567 if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR)
569 vec<tree, va_gc> *args = make_tree_vector_single (expr);
570 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
571 &args, type, LOOKUP_NORMAL, complain);
572 release_tree_vector (args);
573 expr = build_cplus_new (type, expr, complain);
575 else
576 expr = decay_conversion (expr, complain);
578 return expr;
582 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
583 TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
584 unchanged. */
586 static tree
587 ignore_overflows (tree expr, tree orig)
589 if (TREE_CODE (expr) == INTEGER_CST
590 && TREE_CODE (orig) == INTEGER_CST
591 && TREE_OVERFLOW (expr) != TREE_OVERFLOW (orig))
593 gcc_assert (!TREE_OVERFLOW (orig));
594 /* Ensure constant sharing. */
595 expr = wide_int_to_tree (TREE_TYPE (expr), expr);
597 return expr;
600 /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
601 properly. */
603 tree
604 cp_fold_convert (tree type, tree expr)
606 tree conv;
607 if (TREE_TYPE (expr) == type)
608 conv = expr;
609 else if (TREE_CODE (expr) == PTRMEM_CST)
611 /* Avoid wrapping a PTRMEM_CST in NOP_EXPR. */
612 conv = copy_node (expr);
613 TREE_TYPE (conv) = type;
615 else
617 conv = fold_convert (type, expr);
618 conv = ignore_overflows (conv, expr);
620 return conv;
623 /* C++ conversions, preference to static cast conversions. */
625 tree
626 cp_convert (tree type, tree expr, tsubst_flags_t complain)
628 return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL, complain);
631 /* C++ equivalent of convert_and_check but using cp_convert as the
632 conversion function.
634 Convert EXPR to TYPE, warning about conversion problems with constants.
635 Invoke this function on every expression that is converted implicitly,
636 i.e. because of language rules and not because of an explicit cast. */
638 tree
639 cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain)
641 tree result;
643 if (TREE_TYPE (expr) == type)
644 return expr;
646 result = cp_convert (type, expr, complain);
648 if ((complain & tf_warning)
649 && c_inhibit_evaluation_warnings == 0)
651 tree folded = maybe_constant_value (expr);
652 tree stripped = folded;
653 tree folded_result
654 = folded != expr ? cp_convert (type, folded, complain) : result;
656 /* maybe_constant_value wraps an INTEGER_CST with TREE_OVERFLOW in a
657 NOP_EXPR so that it isn't TREE_CONSTANT anymore. */
658 STRIP_NOPS (stripped);
660 if (!TREE_OVERFLOW_P (stripped)
661 && folded_result != error_mark_node)
662 warnings_for_convert_and_check (input_location, type, folded,
663 folded_result);
666 return result;
669 /* Conversion...
671 FLAGS indicates how we should behave. */
673 tree
674 ocp_convert (tree type, tree expr, int convtype, int flags,
675 tsubst_flags_t complain)
677 tree e = expr;
678 enum tree_code code = TREE_CODE (type);
679 const char *invalid_conv_diag;
680 tree e1;
681 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
683 if (error_operand_p (e) || type == error_mark_node)
684 return error_mark_node;
686 complete_type (type);
687 complete_type (TREE_TYPE (expr));
689 if ((invalid_conv_diag
690 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
692 if (complain & tf_error)
693 error (invalid_conv_diag);
694 return error_mark_node;
697 /* FIXME remove when moving to c_fully_fold model. */
698 e = scalar_constant_value (e);
699 if (error_operand_p (e))
700 return error_mark_node;
702 if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
703 /* We need a new temporary; don't take this shortcut. */;
704 else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
706 if (same_type_p (type, TREE_TYPE (e)))
707 /* The call to fold will not always remove the NOP_EXPR as
708 might be expected, since if one of the types is a typedef;
709 the comparison in fold is just equality of pointers, not a
710 call to comptypes. We don't call fold in this case because
711 that can result in infinite recursion; fold will call
712 convert, which will call ocp_convert, etc. */
713 return e;
714 /* For complex data types, we need to perform componentwise
715 conversion. */
716 else if (TREE_CODE (type) == COMPLEX_TYPE)
717 return fold_if_not_in_template (convert_to_complex (type, e));
718 else if (TREE_CODE (type) == VECTOR_TYPE)
719 return fold_if_not_in_template (convert_to_vector (type, e));
720 else if (TREE_CODE (e) == TARGET_EXPR)
722 /* Don't build a NOP_EXPR of class type. Instead, change the
723 type of the temporary. */
724 TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
725 return e;
727 else
729 /* We shouldn't be treating objects of ADDRESSABLE type as
730 rvalues. */
731 gcc_assert (!TREE_ADDRESSABLE (type));
732 return fold_if_not_in_template (build_nop (type, e));
736 e1 = targetm.convert_to_type (type, e);
737 if (e1)
738 return e1;
740 if (code == VOID_TYPE && (convtype & CONV_STATIC))
742 e = convert_to_void (e, ICV_CAST, complain);
743 return e;
746 if (INTEGRAL_CODE_P (code))
748 tree intype = TREE_TYPE (e);
749 tree converted;
751 if (TREE_CODE (type) == ENUMERAL_TYPE)
753 /* enum = enum, enum = int, enum = float, (enum)pointer are all
754 errors. */
755 if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
756 || TREE_CODE (intype) == REAL_TYPE)
757 && ! (convtype & CONV_STATIC))
758 || TYPE_PTR_P (intype))
760 if (complain & tf_error)
761 permerror (loc, "conversion from %q#T to %q#T", intype, type);
762 else
763 return error_mark_node;
766 /* [expr.static.cast]
768 8. A value of integral or enumeration type can be explicitly
769 converted to an enumeration type. The value is unchanged if
770 the original value is within the range of the enumeration
771 values. Otherwise, the resulting enumeration value is
772 unspecified. */
773 if ((complain & tf_warning)
774 && TREE_CODE (e) == INTEGER_CST
775 && ENUM_UNDERLYING_TYPE (type)
776 && !int_fits_type_p (e, ENUM_UNDERLYING_TYPE (type)))
777 warning_at (loc, OPT_Wconversion,
778 "the result of the conversion is unspecified because "
779 "%qE is outside the range of type %qT",
780 expr, type);
782 if (MAYBE_CLASS_TYPE_P (intype))
784 tree rval;
785 rval = build_type_conversion (type, e);
786 if (rval)
787 return rval;
788 if (complain & tf_error)
789 error_at (loc, "%q#T used where a %qT was expected", intype, type);
790 return error_mark_node;
792 if (code == BOOLEAN_TYPE)
794 if (VOID_TYPE_P (intype))
796 if (complain & tf_error)
797 error_at (loc,
798 "could not convert %qE from %<void%> to %<bool%>",
799 expr);
800 return error_mark_node;
803 /* We can't implicitly convert a scoped enum to bool, so convert
804 to the underlying type first. */
805 if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
806 e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
807 return cp_truthvalue_conversion (e);
810 converted = fold_if_not_in_template (convert_to_integer (type, e));
812 /* Ignore any integer overflow caused by the conversion. */
813 return ignore_overflows (converted, e);
815 if (NULLPTR_TYPE_P (type) && e && null_ptr_cst_p (e))
817 if (complain & tf_warning)
818 maybe_warn_zero_as_null_pointer_constant (e, loc);
819 return nullptr_node;
821 if (POINTER_TYPE_P (type) || TYPE_PTRMEM_P (type))
822 return fold_if_not_in_template (cp_convert_to_pointer (type, e, complain));
823 if (code == VECTOR_TYPE)
825 tree in_vtype = TREE_TYPE (e);
826 if (MAYBE_CLASS_TYPE_P (in_vtype))
828 tree ret_val;
829 ret_val = build_type_conversion (type, e);
830 if (ret_val)
831 return ret_val;
832 if (complain & tf_error)
833 error_at (loc, "%q#T used where a %qT was expected",
834 in_vtype, type);
835 return error_mark_node;
837 return fold_if_not_in_template (convert_to_vector (type, e));
839 if (code == REAL_TYPE || code == COMPLEX_TYPE)
841 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
843 tree rval;
844 rval = build_type_conversion (type, e);
845 if (rval)
846 return rval;
847 else if (complain & tf_error)
848 error_at (loc,
849 "%q#T used where a floating point value was expected",
850 TREE_TYPE (e));
852 if (code == REAL_TYPE)
853 return fold_if_not_in_template (convert_to_real (type, e));
854 else if (code == COMPLEX_TYPE)
855 return fold_if_not_in_template (convert_to_complex (type, e));
858 /* New C++ semantics: since assignment is now based on
859 memberwise copying, if the rhs type is derived from the
860 lhs type, then we may still do a conversion. */
861 if (RECORD_OR_UNION_CODE_P (code))
863 tree dtype = TREE_TYPE (e);
864 tree ctor = NULL_TREE;
866 dtype = TYPE_MAIN_VARIANT (dtype);
868 /* Conversion between aggregate types. New C++ semantics allow
869 objects of derived type to be cast to objects of base type.
870 Old semantics only allowed this between pointers.
872 There may be some ambiguity between using a constructor
873 vs. using a type conversion operator when both apply. */
875 ctor = e;
877 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
878 return error_mark_node;
880 if (BRACE_ENCLOSED_INITIALIZER_P (ctor))
881 ctor = perform_implicit_conversion (type, ctor, complain);
882 else if ((flags & LOOKUP_ONLYCONVERTING)
883 && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
884 /* For copy-initialization, first we create a temp of the proper type
885 with a user-defined conversion sequence, then we direct-initialize
886 the target with the temp (see [dcl.init]). */
887 ctor = build_user_type_conversion (type, ctor, flags, complain);
888 else
890 vec<tree, va_gc> *ctor_vec = make_tree_vector_single (ctor);
891 ctor = build_special_member_call (NULL_TREE,
892 complete_ctor_identifier,
893 &ctor_vec,
894 type, flags, complain);
895 release_tree_vector (ctor_vec);
897 if (ctor)
898 return build_cplus_new (type, ctor, complain);
901 if (complain & tf_error)
903 /* If the conversion failed and expr was an invalid use of pointer to
904 member function, try to report a meaningful error. */
905 if (invalid_nonstatic_memfn_p (expr, complain))
906 /* We displayed the error message. */;
907 else
908 error_at (loc, "conversion from %qT to non-scalar type %qT requested",
909 TREE_TYPE (expr), type);
911 return error_mark_node;
914 /* When an expression is used in a void context, its value is discarded and
915 no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
916 stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
917 in a void context. The C++ standard does not define what an `access' to an
918 object is, but there is reason to believe that it is the lvalue to rvalue
919 conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
920 accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
921 indicates that volatile semantics should be the same between C and C++
922 where ever possible. C leaves it implementation defined as to what
923 constitutes an access to a volatile. So, we interpret `*vp' as a read of
924 the volatile object `vp' points to, unless that is an incomplete type. For
925 volatile references we do not do this interpretation, because that would
926 make it impossible to ignore the reference return value from functions. We
927 issue warnings in the confusing cases.
929 The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
930 to void via a cast. If an expression is being implicitly converted, IMPLICIT
931 indicates the context of the implicit conversion. */
933 tree
934 convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
936 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
938 if (expr == error_mark_node
939 || TREE_TYPE (expr) == error_mark_node)
940 return error_mark_node;
942 if (implicit == ICV_CAST)
943 mark_exp_read (expr);
944 else
946 tree exprv = expr;
948 while (TREE_CODE (exprv) == COMPOUND_EXPR)
949 exprv = TREE_OPERAND (exprv, 1);
950 if (DECL_P (exprv)
951 || handled_component_p (exprv)
952 || INDIRECT_REF_P (exprv))
953 /* Expr is not being 'used' here, otherwise we whould have
954 called mark_{rl}value_use use here, which would have in turn
955 called mark_exp_read. Rather, we call mark_exp_read directly
956 to avoid some warnings when
957 -Wunused-but-set-{variable,parameter} is in effect. */
958 mark_exp_read (exprv);
961 if (!TREE_TYPE (expr))
962 return expr;
963 if (invalid_nonstatic_memfn_p (expr, complain))
964 return error_mark_node;
965 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
967 if (complain & tf_error)
968 error_at (loc, "pseudo-destructor is not called");
969 return error_mark_node;
971 if (VOID_TYPE_P (TREE_TYPE (expr)))
972 return expr;
973 switch (TREE_CODE (expr))
975 case COND_EXPR:
977 /* The two parts of a cond expr might be separate lvalues. */
978 tree op1 = TREE_OPERAND (expr,1);
979 tree op2 = TREE_OPERAND (expr,2);
980 bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1))
981 || TREE_SIDE_EFFECTS (op2));
982 tree new_op1, new_op2;
983 new_op1 = NULL_TREE;
984 if (implicit != ICV_CAST && !side_effects)
986 if (op1)
987 new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
988 new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
990 else
992 if (op1)
993 new_op1 = convert_to_void (op1, ICV_CAST, complain);
994 new_op2 = convert_to_void (op2, ICV_CAST, complain);
997 expr = build3 (COND_EXPR, TREE_TYPE (new_op2),
998 TREE_OPERAND (expr, 0), new_op1, new_op2);
999 break;
1002 case COMPOUND_EXPR:
1004 /* The second part of a compound expr contains the value. */
1005 tree op1 = TREE_OPERAND (expr,1);
1006 tree new_op1;
1007 if (implicit != ICV_CAST && !TREE_NO_WARNING (expr))
1008 new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
1009 else
1010 new_op1 = convert_to_void (op1, ICV_CAST, complain);
1012 if (new_op1 != op1)
1014 tree t = build2 (COMPOUND_EXPR, TREE_TYPE (new_op1),
1015 TREE_OPERAND (expr, 0), new_op1);
1016 expr = t;
1019 break;
1022 case NON_LVALUE_EXPR:
1023 case NOP_EXPR:
1024 /* These have already decayed to rvalue. */
1025 break;
1027 case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
1028 break;
1030 case INDIRECT_REF:
1032 tree type = TREE_TYPE (expr);
1033 int is_reference = TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0)))
1034 == REFERENCE_TYPE;
1035 int is_volatile = TYPE_VOLATILE (type);
1036 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1038 /* Can't load the value if we don't know the type. */
1039 if (is_volatile && !is_complete)
1041 if (complain & tf_warning)
1042 switch (implicit)
1044 case ICV_CAST:
1045 warning_at (loc, 0, "conversion to void will not access "
1046 "object of incomplete type %qT", type);
1047 break;
1048 case ICV_SECOND_OF_COND:
1049 warning_at (loc, 0, "indirection will not access object of "
1050 "incomplete type %qT in second operand "
1051 "of conditional expression", type);
1052 break;
1053 case ICV_THIRD_OF_COND:
1054 warning_at (loc, 0, "indirection will not access object of "
1055 "incomplete type %qT in third operand "
1056 "of conditional expression", type);
1057 break;
1058 case ICV_RIGHT_OF_COMMA:
1059 warning_at (loc, 0, "indirection will not access object of "
1060 "incomplete type %qT in right operand of "
1061 "comma operator", type);
1062 break;
1063 case ICV_LEFT_OF_COMMA:
1064 warning_at (loc, 0, "indirection will not access object of "
1065 "incomplete type %qT in left operand of "
1066 "comma operator", type);
1067 break;
1068 case ICV_STATEMENT:
1069 warning_at (loc, 0, "indirection will not access object of "
1070 "incomplete type %qT in statement", type);
1071 break;
1072 case ICV_THIRD_IN_FOR:
1073 warning_at (loc, 0, "indirection will not access object of "
1074 "incomplete type %qT in for increment "
1075 "expression", type);
1076 break;
1077 default:
1078 gcc_unreachable ();
1081 /* Don't load the value if this is an implicit dereference, or if
1082 the type needs to be handled by ctors/dtors. */
1083 else if (is_volatile && is_reference)
1085 if (complain & tf_warning)
1086 switch (implicit)
1088 case ICV_CAST:
1089 warning_at (loc, 0, "conversion to void will not access "
1090 "object of type %qT", type);
1091 break;
1092 case ICV_SECOND_OF_COND:
1093 warning_at (loc, 0, "implicit dereference will not access "
1094 "object of type %qT in second operand of "
1095 "conditional expression", type);
1096 break;
1097 case ICV_THIRD_OF_COND:
1098 warning_at (loc, 0, "implicit dereference will not access "
1099 "object of type %qT in third operand of "
1100 "conditional expression", type);
1101 break;
1102 case ICV_RIGHT_OF_COMMA:
1103 warning_at (loc, 0, "implicit dereference will not access "
1104 "object of type %qT in right operand of "
1105 "comma operator", type);
1106 break;
1107 case ICV_LEFT_OF_COMMA:
1108 warning_at (loc, 0, "implicit dereference will not access "
1109 "object of type %qT in left operand of comma "
1110 "operator", type);
1111 break;
1112 case ICV_STATEMENT:
1113 warning_at (loc, 0, "implicit dereference will not access "
1114 "object of type %qT in statement", type);
1115 break;
1116 case ICV_THIRD_IN_FOR:
1117 warning_at (loc, 0, "implicit dereference will not access "
1118 "object of type %qT in for increment expression",
1119 type);
1120 break;
1121 default:
1122 gcc_unreachable ();
1125 else if (is_volatile && TREE_ADDRESSABLE (type))
1127 if (complain & tf_warning)
1128 switch (implicit)
1130 case ICV_CAST:
1131 warning_at (loc, 0, "conversion to void will not access "
1132 "object of non-trivially-copyable type %qT",
1133 type);
1134 break;
1135 case ICV_SECOND_OF_COND:
1136 warning_at (loc, 0, "indirection will not access object of "
1137 "non-trivially-copyable type %qT in second "
1138 "operand of conditional expression", type);
1139 break;
1140 case ICV_THIRD_OF_COND:
1141 warning_at (loc, 0, "indirection will not access object of "
1142 "non-trivially-copyable type %qT in third "
1143 "operand of conditional expression", type);
1144 break;
1145 case ICV_RIGHT_OF_COMMA:
1146 warning_at (loc, 0, "indirection will not access object of "
1147 "non-trivially-copyable type %qT in right "
1148 "operand of comma operator", type);
1149 break;
1150 case ICV_LEFT_OF_COMMA:
1151 warning_at (loc, 0, "indirection will not access object of "
1152 "non-trivially-copyable type %qT in left "
1153 "operand of comma operator", type);
1154 break;
1155 case ICV_STATEMENT:
1156 warning_at (loc, 0, "indirection will not access object of "
1157 "non-trivially-copyable type %qT in statement",
1158 type);
1159 break;
1160 case ICV_THIRD_IN_FOR:
1161 warning_at (loc, 0, "indirection will not access object of "
1162 "non-trivially-copyable type %qT in for "
1163 "increment expression", type);
1164 break;
1165 default:
1166 gcc_unreachable ();
1169 if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
1171 /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1172 operation is stripped off. Note that we don't warn about
1173 - an expression with TREE_NO_WARNING set. (For an example of
1174 such expressions, see build_over_call in call.c.)
1175 - automatic dereferencing of references, since the user cannot
1176 control it. (See also warn_if_unused_value() in c-common.c.) */
1177 if (warn_unused_value
1178 && implicit != ICV_CAST
1179 && (complain & tf_warning)
1180 && !TREE_NO_WARNING (expr)
1181 && !is_reference)
1182 warning_at (loc, OPT_Wunused_value, "value computed is not used");
1183 expr = TREE_OPERAND (expr, 0);
1186 break;
1189 case VAR_DECL:
1191 /* External variables might be incomplete. */
1192 tree type = TREE_TYPE (expr);
1193 int is_complete = COMPLETE_TYPE_P (complete_type (type));
1195 if (TYPE_VOLATILE (type) && !is_complete && (complain & tf_warning))
1196 switch (implicit)
1198 case ICV_CAST:
1199 warning_at (loc, 0, "conversion to void will not access "
1200 "object %qE of incomplete type %qT", expr, type);
1201 break;
1202 case ICV_SECOND_OF_COND:
1203 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1204 "not be accessed in second operand of "
1205 "conditional expression", expr, type);
1206 break;
1207 case ICV_THIRD_OF_COND:
1208 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1209 "not be accessed in third operand of "
1210 "conditional expression", expr, type);
1211 break;
1212 case ICV_RIGHT_OF_COMMA:
1213 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1214 "not be accessed in right operand of comma operator",
1215 expr, type);
1216 break;
1217 case ICV_LEFT_OF_COMMA:
1218 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1219 "not be accessed in left operand of comma operator",
1220 expr, type);
1221 break;
1222 case ICV_STATEMENT:
1223 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1224 "not be accessed in statement", expr, type);
1225 break;
1226 case ICV_THIRD_IN_FOR:
1227 warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1228 "not be accessed in for increment expression",
1229 expr, type);
1230 break;
1231 default:
1232 gcc_unreachable ();
1235 break;
1238 case TARGET_EXPR:
1239 /* Don't bother with the temporary object returned from a function if
1240 we don't use it and don't need to destroy it. We'll still
1241 allocate space for it in expand_call or declare_return_variable,
1242 but we don't need to track it through all the tree phases. */
1243 if (TARGET_EXPR_IMPLICIT_P (expr)
1244 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (expr)))
1246 tree init = TARGET_EXPR_INITIAL (expr);
1247 if (TREE_CODE (init) == AGGR_INIT_EXPR
1248 && !AGGR_INIT_VIA_CTOR_P (init))
1250 tree fn = AGGR_INIT_EXPR_FN (init);
1251 expr = build_call_array_loc (input_location,
1252 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
1254 aggr_init_expr_nargs (init),
1255 AGGR_INIT_EXPR_ARGP (init));
1258 break;
1260 default:;
1262 expr = resolve_nondeduced_context (expr);
1264 tree probe = expr;
1266 if (TREE_CODE (probe) == ADDR_EXPR)
1267 probe = TREE_OPERAND (expr, 0);
1268 if (type_unknown_p (probe))
1270 /* [over.over] enumerates the places where we can take the address
1271 of an overloaded function, and this is not one of them. */
1272 if (complain & tf_error)
1273 switch (implicit)
1275 case ICV_CAST:
1276 error_at (loc, "conversion to void "
1277 "cannot resolve address of overloaded function");
1278 break;
1279 case ICV_SECOND_OF_COND:
1280 error_at (loc, "second operand of conditional expression "
1281 "cannot resolve address of overloaded function");
1282 break;
1283 case ICV_THIRD_OF_COND:
1284 error_at (loc, "third operand of conditional expression "
1285 "cannot resolve address of overloaded function");
1286 break;
1287 case ICV_RIGHT_OF_COMMA:
1288 error_at (loc, "right operand of comma operator "
1289 "cannot resolve address of overloaded function");
1290 break;
1291 case ICV_LEFT_OF_COMMA:
1292 error_at (loc, "left operand of comma operator "
1293 "cannot resolve address of overloaded function");
1294 break;
1295 case ICV_STATEMENT:
1296 error_at (loc, "statement "
1297 "cannot resolve address of overloaded function");
1298 break;
1299 case ICV_THIRD_IN_FOR:
1300 error_at (loc, "for increment expression "
1301 "cannot resolve address of overloaded function");
1302 break;
1304 else
1305 return error_mark_node;
1306 expr = void_node;
1308 else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
1310 /* Only warn when there is no &. */
1311 if (complain & tf_warning)
1312 switch (implicit)
1314 case ICV_SECOND_OF_COND:
1315 warning_at (loc, OPT_Waddress,
1316 "second operand of conditional expression "
1317 "is a reference, not call, to function %qE", expr);
1318 break;
1319 case ICV_THIRD_OF_COND:
1320 warning_at (loc, OPT_Waddress,
1321 "third operand of conditional expression "
1322 "is a reference, not call, to function %qE", expr);
1323 break;
1324 case ICV_RIGHT_OF_COMMA:
1325 warning_at (loc, OPT_Waddress,
1326 "right operand of comma operator "
1327 "is a reference, not call, to function %qE", expr);
1328 break;
1329 case ICV_LEFT_OF_COMMA:
1330 warning_at (loc, OPT_Waddress,
1331 "left operand of comma operator "
1332 "is a reference, not call, to function %qE", expr);
1333 break;
1334 case ICV_STATEMENT:
1335 warning_at (loc, OPT_Waddress,
1336 "statement is a reference, not call, to function %qE",
1337 expr);
1338 break;
1339 case ICV_THIRD_IN_FOR:
1340 warning_at (loc, OPT_Waddress,
1341 "for increment expression "
1342 "is a reference, not call, to function %qE", expr);
1343 break;
1344 default:
1345 gcc_unreachable ();
1348 if (TREE_CODE (expr) == COMPONENT_REF)
1349 expr = TREE_OPERAND (expr, 0);
1353 if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
1355 if (implicit != ICV_CAST
1356 && warn_unused_value
1357 && !TREE_NO_WARNING (expr)
1358 && !processing_template_decl)
1360 /* The middle end does not warn about expressions that have
1361 been explicitly cast to void, so we must do so here. */
1362 if (!TREE_SIDE_EFFECTS (expr)) {
1363 if (complain & tf_warning)
1364 switch (implicit)
1366 case ICV_SECOND_OF_COND:
1367 warning_at (loc, OPT_Wunused_value,
1368 "second operand of conditional expression "
1369 "has no effect");
1370 break;
1371 case ICV_THIRD_OF_COND:
1372 warning_at (loc, OPT_Wunused_value,
1373 "third operand of conditional expression "
1374 "has no effect");
1375 break;
1376 case ICV_RIGHT_OF_COMMA:
1377 warning_at (loc, OPT_Wunused_value,
1378 "right operand of comma operator has no effect");
1379 break;
1380 case ICV_LEFT_OF_COMMA:
1381 warning_at (loc, OPT_Wunused_value,
1382 "left operand of comma operator has no effect");
1383 break;
1384 case ICV_STATEMENT:
1385 warning_at (loc, OPT_Wunused_value,
1386 "statement has no effect");
1387 break;
1388 case ICV_THIRD_IN_FOR:
1389 warning_at (loc, OPT_Wunused_value,
1390 "for increment expression has no effect");
1391 break;
1392 default:
1393 gcc_unreachable ();
1396 else
1398 tree e;
1399 enum tree_code code;
1400 enum tree_code_class tclass;
1402 e = expr;
1403 /* We might like to warn about (say) "(int) f()", as the
1404 cast has no effect, but the compiler itself will
1405 generate implicit conversions under some
1406 circumstances. (For example a block copy will be
1407 turned into a call to "__builtin_memcpy", with a
1408 conversion of the return value to an appropriate
1409 type.) So, to avoid false positives, we strip
1410 conversions. Do not use STRIP_NOPs because it will
1411 not strip conversions to "void", as that is not a
1412 mode-preserving conversion. */
1413 while (TREE_CODE (e) == NOP_EXPR)
1414 e = TREE_OPERAND (e, 0);
1416 code = TREE_CODE (e);
1417 tclass = TREE_CODE_CLASS (code);
1418 if ((tclass == tcc_comparison
1419 || tclass == tcc_unary
1420 || (tclass == tcc_binary
1421 && !(code == MODIFY_EXPR
1422 || code == INIT_EXPR
1423 || code == PREDECREMENT_EXPR
1424 || code == PREINCREMENT_EXPR
1425 || code == POSTDECREMENT_EXPR
1426 || code == POSTINCREMENT_EXPR))
1427 || code == VEC_PERM_EXPR
1428 || code == VEC_COND_EXPR)
1429 && (complain & tf_warning))
1430 warning_at (loc, OPT_Wunused_value, "value computed is not used");
1433 expr = build1 (CONVERT_EXPR, void_type_node, expr);
1435 if (! TREE_SIDE_EFFECTS (expr))
1436 expr = void_node;
1437 return expr;
1440 /* Create an expression whose value is that of EXPR,
1441 converted to type TYPE. The TREE_TYPE of the value
1442 is always TYPE. This function implements all reasonable
1443 conversions; callers should filter out those that are
1444 not permitted by the language being compiled.
1446 Most of this routine is from build_reinterpret_cast.
1448 The back end cannot call cp_convert (what was convert) because
1449 conversions to/from basetypes may involve memory references
1450 (vbases) and adding or subtracting small values (multiple
1451 inheritance), but it calls convert from the constant folding code
1452 on subtrees of already built trees after it has ripped them apart.
1454 Also, if we ever support range variables, we'll probably also have to
1455 do a little bit more work. */
1457 tree
1458 convert (tree type, tree expr)
1460 tree intype;
1462 if (type == error_mark_node || expr == error_mark_node)
1463 return error_mark_node;
1465 intype = TREE_TYPE (expr);
1467 if (POINTER_TYPE_P (type) && POINTER_TYPE_P (intype))
1468 return fold_if_not_in_template (build_nop (type, expr));
1470 return ocp_convert (type, expr, CONV_OLD_CONVERT,
1471 LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
1472 tf_warning_or_error);
1475 /* Like cp_convert, except permit conversions to take place which
1476 are not normally allowed due to access restrictions
1477 (such as conversion from sub-type to private super-type). */
1479 tree
1480 convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain)
1482 tree e = expr;
1483 enum tree_code code = TREE_CODE (type);
1485 if (code == REFERENCE_TYPE)
1486 return (fold_if_not_in_template
1487 (convert_to_reference (type, e, CONV_C_CAST, 0,
1488 NULL_TREE, complain)));
1490 if (code == POINTER_TYPE)
1491 return fold_if_not_in_template (convert_to_pointer_force (type, e,
1492 complain));
1494 /* From typeck.c convert_for_assignment */
1495 if (((TYPE_PTR_P (TREE_TYPE (e)) && TREE_CODE (e) == ADDR_EXPR
1496 && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1497 || integer_zerop (e)
1498 || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1499 && TYPE_PTRMEMFUNC_P (type))
1500 /* compatible pointer to member functions. */
1501 return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1502 /*c_cast_p=*/1, complain);
1504 return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain);
1507 /* Convert an aggregate EXPR to type XTYPE. If a conversion
1508 exists, return the attempted conversion. This may
1509 return ERROR_MARK_NODE if the conversion is not
1510 allowed (references private members, etc).
1511 If no conversion exists, NULL_TREE is returned.
1513 FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1514 object parameter, or by the second standard conversion sequence if
1515 that doesn't do it. This will probably wait for an overloading rewrite.
1516 (jason 8/9/95) */
1518 static tree
1519 build_type_conversion (tree xtype, tree expr)
1521 /* C++: check to see if we can convert this aggregate type
1522 into the required type. */
1523 return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL,
1524 tf_warning_or_error);
1527 /* Convert the given EXPR to one of a group of types suitable for use in an
1528 expression. DESIRES is a combination of various WANT_* flags (q.v.)
1529 which indicates which types are suitable. If COMPLAIN is true, complain
1530 about ambiguity; otherwise, the caller will deal with it. */
1532 tree
1533 build_expr_type_conversion (int desires, tree expr, bool complain)
1535 tree basetype = TREE_TYPE (expr);
1536 tree conv = NULL_TREE;
1537 tree winner = NULL_TREE;
1539 if (expr == null_node
1540 && (desires & WANT_INT)
1541 && !(desires & WANT_NULL))
1543 source_location loc =
1544 expansion_point_location_if_in_system_header (input_location);
1546 warning_at (loc, OPT_Wconversion_null,
1547 "converting NULL to non-pointer type");
1550 if (basetype == error_mark_node)
1551 return error_mark_node;
1553 if (! MAYBE_CLASS_TYPE_P (basetype))
1554 switch (TREE_CODE (basetype))
1556 case INTEGER_TYPE:
1557 if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1558 return expr;
1559 /* else fall through... */
1561 case BOOLEAN_TYPE:
1562 return (desires & WANT_INT) ? expr : NULL_TREE;
1563 case ENUMERAL_TYPE:
1564 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1565 case REAL_TYPE:
1566 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1567 case POINTER_TYPE:
1568 return (desires & WANT_POINTER) ? expr : NULL_TREE;
1570 case FUNCTION_TYPE:
1571 case ARRAY_TYPE:
1572 return (desires & WANT_POINTER) ? decay_conversion (expr,
1573 tf_warning_or_error)
1574 : NULL_TREE;
1576 case COMPLEX_TYPE:
1577 case VECTOR_TYPE:
1578 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1579 return NULL_TREE;
1580 switch (TREE_CODE (TREE_TYPE (basetype)))
1582 case INTEGER_TYPE:
1583 case BOOLEAN_TYPE:
1584 return (desires & WANT_INT) ? expr : NULL_TREE;
1585 case ENUMERAL_TYPE:
1586 return (desires & WANT_ENUM) ? expr : NULL_TREE;
1587 case REAL_TYPE:
1588 return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1589 default:
1590 return NULL_TREE;
1593 default:
1594 return NULL_TREE;
1597 /* The code for conversions from class type is currently only used for
1598 delete expressions. Other expressions are handled by build_new_op. */
1599 if (!complete_type_or_maybe_complain (basetype, expr, complain))
1600 return error_mark_node;
1601 if (!TYPE_HAS_CONVERSION (basetype))
1602 return NULL_TREE;
1604 for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1606 int win = 0;
1607 tree candidate;
1608 tree cand = TREE_VALUE (conv);
1609 cand = OVL_CURRENT (cand);
1611 if (winner && winner == cand)
1612 continue;
1614 if (DECL_NONCONVERTING_P (cand))
1615 continue;
1617 candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1619 switch (TREE_CODE (candidate))
1621 case BOOLEAN_TYPE:
1622 case INTEGER_TYPE:
1623 win = (desires & WANT_INT); break;
1624 case ENUMERAL_TYPE:
1625 win = (desires & WANT_ENUM); break;
1626 case REAL_TYPE:
1627 win = (desires & WANT_FLOAT); break;
1628 case POINTER_TYPE:
1629 win = (desires & WANT_POINTER); break;
1631 case COMPLEX_TYPE:
1632 case VECTOR_TYPE:
1633 if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1634 break;
1635 switch (TREE_CODE (TREE_TYPE (candidate)))
1637 case BOOLEAN_TYPE:
1638 case INTEGER_TYPE:
1639 win = (desires & WANT_INT); break;
1640 case ENUMERAL_TYPE:
1641 win = (desires & WANT_ENUM); break;
1642 case REAL_TYPE:
1643 win = (desires & WANT_FLOAT); break;
1644 default:
1645 break;
1647 break;
1649 default:
1650 /* A wildcard could be instantiated to match any desired
1651 type, but we can't deduce the template argument. */
1652 if (WILDCARD_TYPE_P (candidate))
1653 win = true;
1654 break;
1657 if (win)
1659 if (TREE_CODE (cand) == TEMPLATE_DECL)
1661 if (complain)
1662 error ("default type conversion can't deduce template"
1663 " argument for %qD", cand);
1664 return error_mark_node;
1667 if (winner)
1669 tree winner_type
1670 = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1672 if (!same_type_ignoring_top_level_qualifiers_p (winner_type,
1673 candidate))
1675 if (complain)
1677 error ("ambiguous default type conversion from %qT",
1678 basetype);
1679 inform (input_location,
1680 " candidate conversions include %qD and %qD",
1681 winner, cand);
1683 return error_mark_node;
1687 winner = cand;
1691 if (winner)
1693 tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1694 return build_user_type_conversion (type, expr, LOOKUP_NORMAL,
1695 tf_warning_or_error);
1698 return NULL_TREE;
1701 /* Implements integral promotion (4.1) and float->double promotion. */
1703 tree
1704 type_promotes_to (tree type)
1706 tree promoted_type;
1708 if (type == error_mark_node)
1709 return error_mark_node;
1711 type = TYPE_MAIN_VARIANT (type);
1713 /* Check for promotions of target-defined types first. */
1714 promoted_type = targetm.promoted_type (type);
1715 if (promoted_type)
1716 return promoted_type;
1718 /* bool always promotes to int (not unsigned), even if it's the same
1719 size. */
1720 if (TREE_CODE (type) == BOOLEAN_TYPE)
1721 type = integer_type_node;
1723 /* Normally convert enums to int, but convert wide enums to something
1724 wider. Scoped enums don't promote, but pretend they do for backward
1725 ABI bug compatibility wrt varargs. */
1726 else if (TREE_CODE (type) == ENUMERAL_TYPE
1727 || type == char16_type_node
1728 || type == char32_type_node
1729 || type == wchar_type_node)
1731 int precision = MAX (TYPE_PRECISION (type),
1732 TYPE_PRECISION (integer_type_node));
1733 tree totype = c_common_type_for_size (precision, 0);
1734 tree prom = type;
1735 if (TREE_CODE (prom) == ENUMERAL_TYPE)
1736 prom = ENUM_UNDERLYING_TYPE (prom);
1737 if (TYPE_UNSIGNED (prom)
1738 && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype))
1739 prom = c_common_type_for_size (precision, 1);
1740 else
1741 prom = totype;
1742 if (SCOPED_ENUM_P (type))
1744 if (abi_version_crosses (6)
1745 && TYPE_MODE (prom) != TYPE_MODE (type))
1746 warning (OPT_Wabi, "scoped enum %qT passed through ... as "
1747 "%qT before -fabi-version=6, %qT after",
1748 type, prom, ENUM_UNDERLYING_TYPE (type));
1749 if (!abi_version_at_least (6))
1750 type = prom;
1752 else
1753 type = prom;
1755 else if (c_promoting_integer_type_p (type))
1757 /* Retain unsignedness if really not getting bigger. */
1758 if (TYPE_UNSIGNED (type)
1759 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1760 type = unsigned_type_node;
1761 else
1762 type = integer_type_node;
1764 else if (type == float_type_node)
1765 type = double_type_node;
1767 return type;
1770 /* The routines below this point are carefully written to conform to
1771 the standard. They use the same terminology, and follow the rules
1772 closely. Although they are used only in pt.c at the moment, they
1773 should presumably be used everywhere in the future. */
1775 /* Attempt to perform qualification conversions on EXPR to convert it
1776 to TYPE. Return the resulting expression, or error_mark_node if
1777 the conversion was impossible. */
1779 tree
1780 perform_qualification_conversions (tree type, tree expr)
1782 tree expr_type;
1784 expr_type = TREE_TYPE (expr);
1786 if (same_type_p (type, expr_type))
1787 return expr;
1788 else if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type)
1789 && comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type)))
1790 return build_nop (type, expr);
1791 else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type)
1792 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
1793 TYPE_PTRMEM_CLASS_TYPE (expr_type))
1794 && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
1795 TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)))
1796 return build_nop (type, expr);
1797 else
1798 return error_mark_node;