annotate gcc/c/c-convert.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Language-level data type conversion for GNU C.
kono
parents:
diff changeset
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 /* This file contains the functions for converting C expressions
kono
parents:
diff changeset
22 to different data types. The only entry point is `convert'.
kono
parents:
diff changeset
23 Every language front end must have a `convert' function
kono
parents:
diff changeset
24 but what kind of conversions it does will depend on the language. */
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 #include "config.h"
kono
parents:
diff changeset
27 #include "system.h"
kono
parents:
diff changeset
28 #include "coretypes.h"
kono
parents:
diff changeset
29 #include "target.h"
kono
parents:
diff changeset
30 #include "c-tree.h"
kono
parents:
diff changeset
31 #include "convert.h"
kono
parents:
diff changeset
32 #include "langhooks.h"
kono
parents:
diff changeset
33 #include "ubsan.h"
kono
parents:
diff changeset
34 #include "stringpool.h"
kono
parents:
diff changeset
35 #include "attribs.h"
kono
parents:
diff changeset
36 #include "asan.h"
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 /* Change of width--truncation and extension of integers or reals--
kono
parents:
diff changeset
39 is represented with NOP_EXPR. Proper functioning of many things
kono
parents:
diff changeset
40 assumes that no other conversions can be NOP_EXPRs.
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 Conversion between integer and pointer is represented with CONVERT_EXPR.
kono
parents:
diff changeset
43 Converting integer to real uses FLOAT_EXPR
kono
parents:
diff changeset
44 and real to integer uses FIX_TRUNC_EXPR.
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 Here is a list of all the functions that assume that widening and
kono
parents:
diff changeset
47 narrowing is always done with a NOP_EXPR:
kono
parents:
diff changeset
48 In convert.c, convert_to_integer.
kono
parents:
diff changeset
49 In c-typeck.c, build_binary_op (boolean ops), and
kono
parents:
diff changeset
50 c_common_truthvalue_conversion.
kono
parents:
diff changeset
51 In expr.c: expand_expr, for operands of a MULT_EXPR.
kono
parents:
diff changeset
52 In fold-const.c: fold.
kono
parents:
diff changeset
53 In tree.c: get_narrower and get_unwidened. */
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 /* Subroutines of `convert'. */
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 /* Create an expression whose value is that of EXPR,
kono
parents:
diff changeset
60 converted to type TYPE. The TREE_TYPE of the value
kono
parents:
diff changeset
61 is always TYPE. This function implements all reasonable
kono
parents:
diff changeset
62 conversions; callers should filter out those that are
kono
parents:
diff changeset
63 not permitted by the language being compiled. */
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 tree
kono
parents:
diff changeset
66 convert (tree type, tree expr)
kono
parents:
diff changeset
67 {
kono
parents:
diff changeset
68 tree e = expr;
kono
parents:
diff changeset
69 enum tree_code code = TREE_CODE (type);
kono
parents:
diff changeset
70 const char *invalid_conv_diag;
kono
parents:
diff changeset
71 tree ret;
kono
parents:
diff changeset
72 location_t loc = EXPR_LOCATION (expr);
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 if (type == error_mark_node
kono
parents:
diff changeset
75 || error_operand_p (expr))
kono
parents:
diff changeset
76 return error_mark_node;
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 if ((invalid_conv_diag
kono
parents:
diff changeset
79 = targetm.invalid_conversion (TREE_TYPE (expr), type)))
kono
parents:
diff changeset
80 {
kono
parents:
diff changeset
81 error (invalid_conv_diag);
kono
parents:
diff changeset
82 return error_mark_node;
kono
parents:
diff changeset
83 }
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 if (type == TREE_TYPE (expr))
kono
parents:
diff changeset
86 return expr;
kono
parents:
diff changeset
87 ret = targetm.convert_to_type (type, expr);
kono
parents:
diff changeset
88 if (ret)
kono
parents:
diff changeset
89 return ret;
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 STRIP_TYPE_NOPS (e);
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr))
kono
parents:
diff changeset
94 && (TREE_CODE (TREE_TYPE (expr)) != COMPLEX_TYPE
kono
parents:
diff changeset
95 || TREE_CODE (e) == COMPLEX_EXPR))
kono
parents:
diff changeset
96 return fold_convert_loc (loc, type, expr);
kono
parents:
diff changeset
97 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
kono
parents:
diff changeset
98 return error_mark_node;
kono
parents:
diff changeset
99 if (TREE_CODE (TREE_TYPE (expr)) == VOID_TYPE)
kono
parents:
diff changeset
100 {
kono
parents:
diff changeset
101 error ("void value not ignored as it ought to be");
kono
parents:
diff changeset
102 return error_mark_node;
kono
parents:
diff changeset
103 }
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 switch (code)
kono
parents:
diff changeset
106 {
kono
parents:
diff changeset
107 case VOID_TYPE:
kono
parents:
diff changeset
108 return fold_convert_loc (loc, type, e);
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 case INTEGER_TYPE:
kono
parents:
diff changeset
111 case ENUMERAL_TYPE:
kono
parents:
diff changeset
112 if (sanitize_flags_p (SANITIZE_FLOAT_CAST)
kono
parents:
diff changeset
113 && current_function_decl != NULL_TREE
kono
parents:
diff changeset
114 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
kono
parents:
diff changeset
115 && COMPLETE_TYPE_P (type))
kono
parents:
diff changeset
116 {
kono
parents:
diff changeset
117 expr = save_expr (expr);
kono
parents:
diff changeset
118 tree check = ubsan_instrument_float_cast (loc, type, expr);
kono
parents:
diff changeset
119 expr = fold_build1 (FIX_TRUNC_EXPR, type, expr);
kono
parents:
diff changeset
120 if (check == NULL_TREE)
kono
parents:
diff changeset
121 return expr;
kono
parents:
diff changeset
122 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (expr), check, expr);
kono
parents:
diff changeset
123 }
kono
parents:
diff changeset
124 ret = convert_to_integer (type, e);
kono
parents:
diff changeset
125 goto maybe_fold;
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 case BOOLEAN_TYPE:
kono
parents:
diff changeset
128 return fold_convert_loc
kono
parents:
diff changeset
129 (loc, type, c_objc_common_truthvalue_conversion (input_location, expr));
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 case POINTER_TYPE:
kono
parents:
diff changeset
132 case REFERENCE_TYPE:
kono
parents:
diff changeset
133 ret = convert_to_pointer (type, e);
kono
parents:
diff changeset
134 goto maybe_fold;
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 case REAL_TYPE:
kono
parents:
diff changeset
137 ret = convert_to_real (type, e);
kono
parents:
diff changeset
138 goto maybe_fold;
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 case FIXED_POINT_TYPE:
kono
parents:
diff changeset
141 ret = convert_to_fixed (type, e);
kono
parents:
diff changeset
142 goto maybe_fold;
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 case COMPLEX_TYPE:
kono
parents:
diff changeset
145 ret = convert_to_complex (type, e);
kono
parents:
diff changeset
146 goto maybe_fold;
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 case VECTOR_TYPE:
kono
parents:
diff changeset
149 ret = convert_to_vector (type, e);
kono
parents:
diff changeset
150 goto maybe_fold;
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 case RECORD_TYPE:
kono
parents:
diff changeset
153 case UNION_TYPE:
kono
parents:
diff changeset
154 if (lang_hooks.types_compatible_p (type, TREE_TYPE (expr)))
kono
parents:
diff changeset
155 return e;
kono
parents:
diff changeset
156 break;
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 default:
kono
parents:
diff changeset
159 break;
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 maybe_fold:
kono
parents:
diff changeset
162 if (TREE_CODE (ret) != C_MAYBE_CONST_EXPR)
kono
parents:
diff changeset
163 ret = fold (ret);
kono
parents:
diff changeset
164 return ret;
kono
parents:
diff changeset
165 }
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 error ("conversion to non-scalar type requested");
kono
parents:
diff changeset
168 return error_mark_node;
kono
parents:
diff changeset
169 }