138fd1498Szrj /* Language-level data type conversion for GNU C.
238fd1498Szrj Copyright (C) 1987-2018 Free Software Foundation, Inc.
338fd1498Szrj
438fd1498Szrj This file is part of GCC.
538fd1498Szrj
638fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
738fd1498Szrj the terms of the GNU General Public License as published by the Free
838fd1498Szrj Software Foundation; either version 3, or (at your option) any later
938fd1498Szrj version.
1038fd1498Szrj
1138fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
1238fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
1338fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1438fd1498Szrj for more details.
1538fd1498Szrj
1638fd1498Szrj You should have received a copy of the GNU General Public License
1738fd1498Szrj along with GCC; see the file COPYING3. If not see
1838fd1498Szrj <http://www.gnu.org/licenses/>. */
1938fd1498Szrj
2038fd1498Szrj
2138fd1498Szrj /* This file contains the functions for converting C expressions
2238fd1498Szrj to different data types. The only entry point is `convert'.
2338fd1498Szrj Every language front end must have a `convert' function
2438fd1498Szrj but what kind of conversions it does will depend on the language. */
2538fd1498Szrj
2638fd1498Szrj #include "config.h"
2738fd1498Szrj #include "system.h"
2838fd1498Szrj #include "coretypes.h"
2938fd1498Szrj #include "target.h"
3038fd1498Szrj #include "c-tree.h"
3138fd1498Szrj #include "convert.h"
3238fd1498Szrj #include "langhooks.h"
3338fd1498Szrj #include "ubsan.h"
3438fd1498Szrj #include "stringpool.h"
3538fd1498Szrj #include "attribs.h"
3638fd1498Szrj #include "asan.h"
3738fd1498Szrj
3838fd1498Szrj /* Change of width--truncation and extension of integers or reals--
3938fd1498Szrj is represented with NOP_EXPR. Proper functioning of many things
4038fd1498Szrj assumes that no other conversions can be NOP_EXPRs.
4138fd1498Szrj
4238fd1498Szrj Conversion between integer and pointer is represented with CONVERT_EXPR.
4338fd1498Szrj Converting integer to real uses FLOAT_EXPR
4438fd1498Szrj and real to integer uses FIX_TRUNC_EXPR.
4538fd1498Szrj
4638fd1498Szrj Here is a list of all the functions that assume that widening and
4738fd1498Szrj narrowing is always done with a NOP_EXPR:
4838fd1498Szrj In convert.c, convert_to_integer.
4938fd1498Szrj In c-typeck.c, build_binary_op (boolean ops), and
5038fd1498Szrj c_common_truthvalue_conversion.
5138fd1498Szrj In expr.c: expand_expr, for operands of a MULT_EXPR.
5238fd1498Szrj In fold-const.c: fold.
5338fd1498Szrj In tree.c: get_narrower and get_unwidened. */
5438fd1498Szrj
5538fd1498Szrj /* Subroutines of `convert'. */
5638fd1498Szrj
5738fd1498Szrj
5838fd1498Szrj
5938fd1498Szrj /* Create an expression whose value is that of EXPR,
6038fd1498Szrj converted to type TYPE. The TREE_TYPE of the value
6138fd1498Szrj is always TYPE. This function implements all reasonable
6238fd1498Szrj conversions; callers should filter out those that are
6338fd1498Szrj not permitted by the language being compiled. */
6438fd1498Szrj
6538fd1498Szrj tree
convert(tree type,tree expr)6638fd1498Szrj convert (tree type, tree expr)
6738fd1498Szrj {
6838fd1498Szrj tree e = expr;
6938fd1498Szrj enum tree_code code = TREE_CODE (type);
7038fd1498Szrj const char *invalid_conv_diag;
7138fd1498Szrj tree ret;
7238fd1498Szrj location_t loc = EXPR_LOCATION (expr);
7338fd1498Szrj
7438fd1498Szrj if (type == error_mark_node
7538fd1498Szrj || error_operand_p (expr))
7638fd1498Szrj return error_mark_node;
7738fd1498Szrj
7838fd1498Szrj if ((invalid_conv_diag
7938fd1498Szrj = targetm.invalid_conversion (TREE_TYPE (expr), type)))
8038fd1498Szrj {
8138fd1498Szrj error (invalid_conv_diag);
8238fd1498Szrj return error_mark_node;
8338fd1498Szrj }
8438fd1498Szrj
8538fd1498Szrj if (type == TREE_TYPE (expr))
8638fd1498Szrj return expr;
8738fd1498Szrj ret = targetm.convert_to_type (type, expr);
8838fd1498Szrj if (ret)
8938fd1498Szrj return ret;
9038fd1498Szrj
9138fd1498Szrj STRIP_TYPE_NOPS (e);
9238fd1498Szrj
9338fd1498Szrj if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr))
9438fd1498Szrj && (TREE_CODE (TREE_TYPE (expr)) != COMPLEX_TYPE
9538fd1498Szrj || TREE_CODE (e) == COMPLEX_EXPR))
9638fd1498Szrj return fold_convert_loc (loc, type, expr);
9738fd1498Szrj if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
9838fd1498Szrj return error_mark_node;
9938fd1498Szrj if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
10038fd1498Szrj {
10138fd1498Szrj error ("void value not ignored as it ought to be");
10238fd1498Szrj return error_mark_node;
10338fd1498Szrj }
10438fd1498Szrj
10538fd1498Szrj switch (code)
10638fd1498Szrj {
10738fd1498Szrj case VOID_TYPE:
10838fd1498Szrj return fold_convert_loc (loc, type, e);
10938fd1498Szrj
11038fd1498Szrj case INTEGER_TYPE:
11138fd1498Szrj case ENUMERAL_TYPE:
11238fd1498Szrj if (sanitize_flags_p (SANITIZE_FLOAT_CAST)
11338fd1498Szrj && current_function_decl != NULL_TREE
11438fd1498Szrj && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
11538fd1498Szrj && COMPLETE_TYPE_P (type))
11638fd1498Szrj {
11738fd1498Szrj expr = save_expr (expr);
118*58e805e6Szrj expr = c_fully_fold (expr, false, NULL);
11938fd1498Szrj tree check = ubsan_instrument_float_cast (loc, type, expr);
12038fd1498Szrj expr = fold_build1 (FIX_TRUNC_EXPR, type, expr);
12138fd1498Szrj if (check == NULL_TREE)
12238fd1498Szrj return expr;
12338fd1498Szrj return fold_build2 (COMPOUND_EXPR, TREE_TYPE (expr), check, expr);
12438fd1498Szrj }
12538fd1498Szrj ret = convert_to_integer (type, e);
12638fd1498Szrj goto maybe_fold;
12738fd1498Szrj
12838fd1498Szrj case BOOLEAN_TYPE:
12938fd1498Szrj return fold_convert_loc
13038fd1498Szrj (loc, type, c_objc_common_truthvalue_conversion (input_location, expr));
13138fd1498Szrj
13238fd1498Szrj case POINTER_TYPE:
13338fd1498Szrj case REFERENCE_TYPE:
13438fd1498Szrj ret = convert_to_pointer (type, e);
13538fd1498Szrj goto maybe_fold;
13638fd1498Szrj
13738fd1498Szrj case REAL_TYPE:
13838fd1498Szrj ret = convert_to_real (type, e);
13938fd1498Szrj goto maybe_fold;
14038fd1498Szrj
14138fd1498Szrj case FIXED_POINT_TYPE:
14238fd1498Szrj ret = convert_to_fixed (type, e);
14338fd1498Szrj goto maybe_fold;
14438fd1498Szrj
14538fd1498Szrj case COMPLEX_TYPE:
14638fd1498Szrj ret = convert_to_complex (type, e);
14738fd1498Szrj goto maybe_fold;
14838fd1498Szrj
14938fd1498Szrj case VECTOR_TYPE:
15038fd1498Szrj ret = convert_to_vector (type, e);
15138fd1498Szrj goto maybe_fold;
15238fd1498Szrj
15338fd1498Szrj case RECORD_TYPE:
15438fd1498Szrj case UNION_TYPE:
15538fd1498Szrj if (lang_hooks.types_compatible_p (type, TREE_TYPE (expr)))
15638fd1498Szrj return e;
15738fd1498Szrj break;
15838fd1498Szrj
15938fd1498Szrj default:
16038fd1498Szrj break;
16138fd1498Szrj
16238fd1498Szrj maybe_fold:
16338fd1498Szrj if (TREE_CODE (ret) != C_MAYBE_CONST_EXPR)
16438fd1498Szrj ret = fold (ret);
16538fd1498Szrj return ret;
16638fd1498Szrj }
16738fd1498Szrj
16838fd1498Szrj error ("conversion to non-scalar type requested");
16938fd1498Szrj return error_mark_node;
17038fd1498Szrj }
171