c: Implement C23 nullptr (N3042)
[official-gcc.git] / gcc / c / c-convert.cc
blob6e7491339d427d72fb8eb3630894dd041d4fbcef
1 /* Language-level data type conversion for GNU C.
2 Copyright (C) 1987-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file contains the functions for converting C expressions
22 to different data types. The only entry point is `convert'.
23 Every language front end must have a `convert' function
24 but what kind of conversions it does will depend on the language. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "c-tree.h"
31 #include "convert.h"
32 #include "langhooks.h"
33 #include "ubsan.h"
34 #include "stringpool.h"
35 #include "attribs.h"
36 #include "asan.h"
38 /* Change of width--truncation and extension of integers or reals--
39 is represented with NOP_EXPR. Proper functioning of many things
40 assumes that no other conversions can be NOP_EXPRs.
42 Conversion between integer and pointer is represented with CONVERT_EXPR.
43 Converting integer to real uses FLOAT_EXPR
44 and real to integer uses FIX_TRUNC_EXPR.
46 Here is a list of all the functions that assume that widening and
47 narrowing is always done with a NOP_EXPR:
48 In convert.cc, convert_to_integer.
49 In c-typeck.cc, build_binary_op (boolean ops), and
50 c_common_truthvalue_conversion.
51 In expr.cc: expand_expr, for operands of a MULT_EXPR.
52 In fold-const.cc: fold.
53 In tree.cc: get_narrower and get_unwidened. */
55 /* Subroutines of `convert'. */
59 /* Create an expression whose value is that of EXPR,
60 converted to type TYPE. The TREE_TYPE of the value
61 is always TYPE. This function implements all reasonable
62 conversions; callers should filter out those that are
63 not permitted by the language being compiled.
64 INIT_CONST is true if the conversion is for arithmetic types for a static
65 initializer and folding must apply accordingly (discarding floating-point
66 exceptions and assuming the default rounding mode is in effect). */
68 static tree
69 c_convert (tree type, tree expr, bool init_const)
71 tree e = expr;
72 enum tree_code code = TREE_CODE (type);
73 const char *invalid_conv_diag;
74 tree ret;
75 location_t loc = EXPR_LOCATION (expr);
77 if (type == error_mark_node
78 || error_operand_p (expr))
79 return error_mark_node;
81 if ((invalid_conv_diag
82 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
84 error (invalid_conv_diag);
85 return error_mark_node;
88 if (type == TREE_TYPE (expr))
89 return expr;
90 ret = targetm.convert_to_type (type, expr);
91 if (ret)
92 return ret;
94 STRIP_TYPE_NOPS (e);
96 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr))
97 && (TREE_CODE (TREE_TYPE (expr)) != COMPLEX_TYPE
98 || TREE_CODE (e) == COMPLEX_EXPR))
99 return fold_convert_loc (loc, type, expr);
100 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
101 return error_mark_node;
102 if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
104 error ("void value not ignored as it ought to be");
105 return error_mark_node;
108 switch (code)
110 case VOID_TYPE:
111 return fold_convert_loc (loc, type, e);
113 case INTEGER_TYPE:
114 case ENUMERAL_TYPE:
115 if (sanitize_flags_p (SANITIZE_FLOAT_CAST)
116 && current_function_decl != NULL_TREE
117 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
118 && COMPLETE_TYPE_P (type))
120 expr = save_expr (expr);
121 expr = c_fully_fold (expr, init_const, NULL);
122 tree check = ubsan_instrument_float_cast (loc, type, expr);
123 expr = fold_build1 (FIX_TRUNC_EXPR, type, expr);
124 if (check == NULL_TREE)
125 return expr;
126 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (expr), check, expr);
128 ret = convert_to_integer (type, e);
129 goto maybe_fold;
131 case BOOLEAN_TYPE:
132 return fold_convert_loc
133 (loc, type, c_objc_common_truthvalue_conversion (input_location, expr));
135 case POINTER_TYPE:
136 /* The type nullptr_t may be converted to a pointer type. The result is
137 a null pointer value. */
138 if (NULLPTR_TYPE_P (TREE_TYPE (e)))
140 /* To make sure that (void *)nullptr is not a null pointer constant,
141 build_c_cast will create an additional NOP_EXPR around the result
142 of this conversion. */
143 if (TREE_SIDE_EFFECTS (e))
144 ret = build2 (COMPOUND_EXPR, type, e, build_int_cst (type, 0));
145 else
146 ret = build_int_cst (type, 0);
147 goto maybe_fold;
149 gcc_fallthrough ();
150 case REFERENCE_TYPE:
151 ret = convert_to_pointer (type, e);
152 goto maybe_fold;
154 case REAL_TYPE:
155 ret = convert_to_real (type, e);
156 goto maybe_fold;
158 case FIXED_POINT_TYPE:
159 ret = convert_to_fixed (type, e);
160 goto maybe_fold;
162 case COMPLEX_TYPE:
163 ret = convert_to_complex (type, e);
164 goto maybe_fold;
166 case VECTOR_TYPE:
167 if (gnu_vector_type_p (type)
168 || gnu_vector_type_p (TREE_TYPE (e))
169 /* Allow conversions between compatible non-GNU vector types
170 when -flax-vector-conversions is passed. The whole purpose
171 of the option is to bend the normal type rules and accept
172 nonconforming code. */
173 || (flag_lax_vector_conversions
174 && VECTOR_TYPE_P (TREE_TYPE (e))
175 && vector_types_convertible_p (type, TREE_TYPE (e), false)))
177 ret = convert_to_vector (type, e);
178 goto maybe_fold;
180 break;
182 case RECORD_TYPE:
183 case UNION_TYPE:
184 if (lang_hooks.types_compatible_p (type, TREE_TYPE (expr)))
185 return e;
186 break;
188 default:
189 break;
191 maybe_fold:
192 if (TREE_CODE (ret) != C_MAYBE_CONST_EXPR)
193 ret = init_const ? fold_init (ret) : fold (ret);
194 return ret;
197 /* If we are converting to nullptr_t, don't say "non-scalar type" because
198 the nullptr_t type is a scalar type. Only nullptr_t shall be converted
199 to nullptr_t. */
200 if (code == NULLPTR_TYPE)
202 error ("conversion from %qT to %qT", TREE_TYPE (e), type);
203 inform (input_location, "only %qT can be converted to %qT", type, type);
205 else
206 error ("conversion to non-scalar type requested");
207 return error_mark_node;
210 /* Create an expression whose value is that of EXPR, converted to type TYPE.
211 The TREE_TYPE of the value is always TYPE. This function implements all
212 reasonable conversions; callers should filter out those that are not
213 permitted by the language being compiled. */
215 tree
216 convert (tree type, tree expr)
218 return c_convert (type, expr, false);
221 /* Create an expression whose value is that of EXPR, converted to type TYPE, in
222 a static initializer. The TREE_TYPE of the value is always TYPE. This
223 function implements all reasonable conversions; callers should filter out
224 those that are not permitted by the language being compiled. */
226 tree
227 convert_init (tree type, tree expr)
229 return c_convert (type, expr, true);