]>
Commit | Line | Data |
---|---|---|
b0fc3e72 | 1 | /* Subroutines shared by all languages that are variants of C. |
d353bf18 | 2 | Copyright (C) 1992-2015 Free Software Foundation, Inc. |
b0fc3e72 | 3 | |
f12b58b3 | 4 | This file is part of GCC. |
b0fc3e72 | 5 | |
f12b58b3 | 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 | |
8c4c00c1 | 8 | Software Foundation; either version 3, or (at your option) any later |
f12b58b3 | 9 | version. |
b0fc3e72 | 10 | |
f12b58b3 | 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. | |
b0fc3e72 | 15 | |
16 | You should have received a copy of the GNU General Public License | |
8c4c00c1 | 17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
b0fc3e72 | 19 | |
b20a8bb4 | 20 | #define GCC_C_COMMON_C |
21 | ||
b0fc3e72 | 22 | #include "config.h" |
405711de | 23 | #include "system.h" |
805e22b2 | 24 | #include "coretypes.h" |
4cba6f60 | 25 | #include "target.h" |
26 | #include "function.h" | |
4cba6f60 | 27 | #include "tree.h" |
7ff8db31 | 28 | #include "c-common.h" |
4cba6f60 | 29 | #include "gimple-expr.h" |
30 | #include "tm_p.h" | |
31 | #include "stringpool.h" | |
32 | #include "cgraph.h" | |
33 | #include "diagnostic.h" | |
e48d0f41 | 34 | #include "intl.h" |
9ed99284 | 35 | #include "stor-layout.h" |
36 | #include "calls.h" | |
9ed99284 | 37 | #include "attribs.h" |
38 | #include "varasm.h" | |
39 | #include "trans-mem.h" | |
6c536c4f | 40 | #include "c-objc.h" |
218e3e4e | 41 | #include "common/common-target.h" |
96554925 | 42 | #include "langhooks.h" |
f3dde807 | 43 | #include "tree-inline.h" |
69579044 | 44 | #include "toplev.h" |
4ee9c684 | 45 | #include "tree-iterator.h" |
e08bd2f4 | 46 | #include "opts.h" |
a8783bee | 47 | #include "gimplify.h" |
fd6f6435 | 48 | |
90cc7820 | 49 | cpp_reader *parse_in; /* Declared in c-pragma.h. */ |
a654e028 | 50 | |
de801c28 | 51 | /* Mode used to build pointers (VOIDmode means ptr_mode). */ |
52 | ||
53 | machine_mode c_default_pointer_mode = VOIDmode; | |
54 | ||
72040e7e | 55 | /* The following symbols are subsumed in the c_global_trees array, and |
44e9fa65 | 56 | listed here individually for documentation purposes. |
72040e7e | 57 | |
58 | INTEGER_TYPE and REAL_TYPE nodes for the standard data types. | |
59 | ||
60 | tree short_integer_type_node; | |
61 | tree long_integer_type_node; | |
62 | tree long_long_integer_type_node; | |
63 | ||
64 | tree short_unsigned_type_node; | |
65 | tree long_unsigned_type_node; | |
66 | tree long_long_unsigned_type_node; | |
67 | ||
3c2239cf | 68 | tree truthvalue_type_node; |
69 | tree truthvalue_false_node; | |
70 | tree truthvalue_true_node; | |
72040e7e | 71 | |
72 | tree ptrdiff_type_node; | |
73 | ||
74 | tree unsigned_char_type_node; | |
75 | tree signed_char_type_node; | |
76 | tree wchar_type_node; | |
72040e7e | 77 | |
924bbf02 | 78 | tree char16_type_node; |
79 | tree char32_type_node; | |
80 | ||
72040e7e | 81 | tree float_type_node; |
82 | tree double_type_node; | |
83 | tree long_double_type_node; | |
84 | ||
85 | tree complex_integer_type_node; | |
86 | tree complex_float_type_node; | |
87 | tree complex_double_type_node; | |
88 | tree complex_long_double_type_node; | |
89 | ||
c4503c0a | 90 | tree dfloat32_type_node; |
91 | tree dfloat64_type_node; | |
92 | tree_dfloat128_type_node; | |
93 | ||
72040e7e | 94 | tree intQI_type_node; |
95 | tree intHI_type_node; | |
96 | tree intSI_type_node; | |
97 | tree intDI_type_node; | |
98 | tree intTI_type_node; | |
99 | ||
100 | tree unsigned_intQI_type_node; | |
101 | tree unsigned_intHI_type_node; | |
102 | tree unsigned_intSI_type_node; | |
103 | tree unsigned_intDI_type_node; | |
104 | tree unsigned_intTI_type_node; | |
105 | ||
106 | tree widest_integer_literal_type_node; | |
107 | tree widest_unsigned_literal_type_node; | |
108 | ||
109 | Nodes for types `void *' and `const void *'. | |
110 | ||
111 | tree ptr_type_node, const_ptr_type_node; | |
112 | ||
113 | Nodes for types `char *' and `const char *'. | |
114 | ||
115 | tree string_type_node, const_string_type_node; | |
116 | ||
117 | Type `char[SOMENUMBER]'. | |
118 | Used when an array of char is needed and the size is irrelevant. | |
119 | ||
120 | tree char_array_type_node; | |
121 | ||
72040e7e | 122 | Type `wchar_t[SOMENUMBER]' or something like it. |
123 | Used when a wide string literal is created. | |
124 | ||
125 | tree wchar_array_type_node; | |
126 | ||
924bbf02 | 127 | Type `char16_t[SOMENUMBER]' or something like it. |
128 | Used when a UTF-16 string literal is created. | |
129 | ||
130 | tree char16_array_type_node; | |
131 | ||
132 | Type `char32_t[SOMENUMBER]' or something like it. | |
133 | Used when a UTF-32 string literal is created. | |
134 | ||
135 | tree char32_array_type_node; | |
136 | ||
72040e7e | 137 | Type `int ()' -- used for implicit declaration of functions. |
138 | ||
139 | tree default_function_type; | |
140 | ||
72040e7e | 141 | A VOID_TYPE node, packaged in a TREE_LIST. |
142 | ||
143 | tree void_list_node; | |
144 | ||
734c98be | 145 | The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__, |
65b7f83f | 146 | and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__ |
147 | VAR_DECLS, but C++ does.) | |
71d9fc9b | 148 | |
65b7f83f | 149 | tree function_name_decl_node; |
734c98be | 150 | tree pretty_function_name_decl_node; |
65b7f83f | 151 | tree c99_function_name_decl_node; |
152 | ||
153 | Stack of nested function name VAR_DECLs. | |
1cae46be | 154 | |
65b7f83f | 155 | tree saved_function_name_decls; |
71d9fc9b | 156 | |
72040e7e | 157 | */ |
158 | ||
159 | tree c_global_trees[CTI_MAX]; | |
573aba85 | 160 | \f |
574a6990 | 161 | /* Switches common to the C front ends. */ |
162 | ||
f7070933 | 163 | /* Nonzero means don't output line number information. */ |
164 | ||
165 | char flag_no_line_commands; | |
166 | ||
167 | /* Nonzero causes -E output not to be done, but directives such as | |
168 | #define that have side effects are still obeyed. */ | |
169 | ||
170 | char flag_no_output; | |
171 | ||
172 | /* Nonzero means dump macros in some fashion. */ | |
173 | ||
174 | char flag_dump_macros; | |
175 | ||
176 | /* Nonzero means pass #include lines through to the output. */ | |
177 | ||
178 | char flag_dump_includes; | |
179 | ||
d718b525 | 180 | /* Nonzero means process PCH files while preprocessing. */ |
181 | ||
182 | bool flag_pch_preprocess; | |
183 | ||
573aba85 | 184 | /* The file name to which we should write a precompiled header, or |
185 | NULL if no header will be written in this compile. */ | |
186 | ||
187 | const char *pch_file; | |
188 | ||
1ed9d5f5 | 189 | /* Nonzero if an ISO standard was selected. It rejects macros in the |
190 | user's namespace. */ | |
191 | int flag_iso; | |
192 | ||
574a6990 | 193 | /* C/ObjC language option variables. */ |
194 | ||
195 | ||
574a6990 | 196 | /* Nonzero means allow type mismatches in conditional expressions; |
197 | just make their values `void'. */ | |
198 | ||
199 | int flag_cond_mismatch; | |
200 | ||
201 | /* Nonzero means enable C89 Amendment 1 features. */ | |
202 | ||
203 | int flag_isoc94; | |
204 | ||
32074525 | 205 | /* Nonzero means use the ISO C99 (or C11) dialect of C. */ |
574a6990 | 206 | |
207 | int flag_isoc99; | |
208 | ||
32074525 | 209 | /* Nonzero means use the ISO C11 dialect of C. */ |
39012afb | 210 | |
32074525 | 211 | int flag_isoc11; |
39012afb | 212 | |
8b332087 | 213 | /* Nonzero means that we have builtin functions, and main is an int. */ |
574a6990 | 214 | |
215 | int flag_hosted = 1; | |
216 | ||
574a6990 | 217 | |
218 | /* ObjC language option variables. */ | |
219 | ||
220 | ||
574a6990 | 221 | /* Tells the compiler that this is a special run. Do not perform any |
222 | compiling, instead we are to test some platform dependent features | |
223 | and output a C header file with appropriate definitions. */ | |
224 | ||
225 | int print_struct_values; | |
226 | ||
f0b5f617 | 227 | /* Tells the compiler what is the constant string class for ObjC. */ |
574a6990 | 228 | |
229 | const char *constant_string_class_name; | |
230 | ||
574a6990 | 231 | |
232 | /* C++ language option variables. */ | |
233 | ||
229a58b1 | 234 | /* The reference version of the ABI for -Wabi. */ |
235 | ||
236 | int warn_abi_version = -1; | |
574a6990 | 237 | |
574a6990 | 238 | /* Nonzero means generate separate instantiation control files and |
239 | juggle them at link time. */ | |
240 | ||
241 | int flag_use_repository; | |
242 | ||
d875b9d2 | 243 | /* The C++ dialect being used. Default set in c_common_post_options. */ |
0fe6eeac | 244 | |
d875b9d2 | 245 | enum cxx_dialect cxx_dialect = cxx_unset; |
0fe6eeac | 246 | |
9dcd0d49 | 247 | /* Maximum template instantiation depth. This limit exists to limit the |
8ce59854 | 248 | time it takes to notice excessively recursive template instantiations. |
574a6990 | 249 | |
8ce59854 | 250 | The default is lower than the 1024 recommended by the C++0x standard |
251 | because G++ runs out of stack before 1024 with highly recursive template | |
252 | argument deduction substitution (g++.dg/cpp0x/enum11.C). */ | |
253 | ||
254 | int max_tinst_depth = 900; | |
574a6990 | 255 | |
988fc1d1 | 256 | /* The elements of `ridpointers' are identifier nodes for the reserved |
257 | type names and storage classes. It is indexed by a RID_... value. */ | |
258 | tree *ridpointers; | |
259 | ||
e60a6f7b | 260 | tree (*make_fname_decl) (location_t, tree, int); |
9e5a737d | 261 | |
48d94ede | 262 | /* Nonzero means don't warn about problems that occur when the code is |
263 | executed. */ | |
264 | int c_inhibit_evaluation_warnings; | |
e78703c1 | 265 | |
93be21c0 | 266 | /* Whether we are building a boolean conversion inside |
267 | convert_for_assignment, or some other late binary operation. If | |
268 | build_binary_op is called for C (from code shared by C and C++) in | |
269 | this case, then the operands have already been folded and the | |
270 | result will not be folded again, so C_MAYBE_CONST_EXPR should not | |
271 | be generated. */ | |
272 | bool in_late_binary_op; | |
273 | ||
7f5f3953 | 274 | /* Whether lexing has been completed, so subsequent preprocessor |
275 | errors should use the compiler's input_location. */ | |
276 | bool done_lexing = false; | |
277 | ||
2c0e001b | 278 | /* Information about how a function name is generated. */ |
65b7f83f | 279 | struct fname_var_t |
280 | { | |
e99c3a1d | 281 | tree *const decl; /* pointer to the VAR_DECL. */ |
282 | const unsigned rid; /* RID number for the identifier. */ | |
283 | const int pretty; /* How pretty is it? */ | |
65b7f83f | 284 | }; |
285 | ||
2c0e001b | 286 | /* The three ways of getting then name of the current function. */ |
65b7f83f | 287 | |
288 | const struct fname_var_t fname_vars[] = | |
289 | { | |
2c0e001b | 290 | /* C99 compliant __func__, must be first. */ |
65b7f83f | 291 | {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0}, |
2c0e001b | 292 | /* GCC __FUNCTION__ compliant. */ |
65b7f83f | 293 | {&function_name_decl_node, RID_FUNCTION_NAME, 0}, |
2c0e001b | 294 | /* GCC __PRETTY_FUNCTION__ compliant. */ |
65b7f83f | 295 | {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1}, |
296 | {NULL, 0, 0}, | |
297 | }; | |
298 | ||
79396169 | 299 | /* Global visibility options. */ |
300 | struct visibility_flags visibility_options; | |
301 | ||
2d2f6a15 | 302 | static tree check_case_value (location_t, tree); |
be23b16f | 303 | static bool check_case_bounds (location_t, tree, tree, tree *, tree *, |
304 | bool *); | |
be43ff5a | 305 | |
1cae46be | 306 | static tree handle_packed_attribute (tree *, tree, tree, int, bool *); |
307 | static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *); | |
308 | static tree handle_common_attribute (tree *, tree, tree, int, bool *); | |
309 | static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *); | |
5de92639 | 310 | static tree handle_hot_attribute (tree *, tree, tree, int, bool *); |
311 | static tree handle_cold_attribute (tree *, tree, tree, int, bool *); | |
a9196da9 | 312 | static tree handle_no_sanitize_address_attribute (tree *, tree, tree, |
313 | int, bool *); | |
d413ffdd | 314 | static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree, |
315 | int, bool *); | |
05f893e1 | 316 | static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int, |
317 | bool *); | |
947aa916 | 318 | static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 319 | static tree handle_noinline_attribute (tree *, tree, tree, int, bool *); |
bdb1f0d1 | 320 | static tree handle_noclone_attribute (tree *, tree, tree, int, bool *); |
85fbea97 | 321 | static tree handle_noicf_attribute (tree *, tree, tree, int, bool *); |
7bd95dfd | 322 | static tree handle_leaf_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 323 | static tree handle_always_inline_attribute (tree *, tree, tree, int, |
324 | bool *); | |
1b16fc45 | 325 | static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *); |
326 | static tree handle_artificial_attribute (tree *, tree, tree, int, bool *); | |
0cdd9887 | 327 | static tree handle_flatten_attribute (tree *, tree, tree, int, bool *); |
10fc867f | 328 | static tree handle_error_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 329 | static tree handle_used_attribute (tree *, tree, tree, int, bool *); |
330 | static tree handle_unused_attribute (tree *, tree, tree, int, bool *); | |
62eec3b4 | 331 | static tree handle_externally_visible_attribute (tree *, tree, tree, int, |
332 | bool *); | |
6b722052 | 333 | static tree handle_no_reorder_attribute (tree *, tree, tree, int, |
334 | bool *); | |
1cae46be | 335 | static tree handle_const_attribute (tree *, tree, tree, int, bool *); |
336 | static tree handle_transparent_union_attribute (tree *, tree, tree, | |
337 | int, bool *); | |
292237f3 | 338 | static tree handle_scalar_storage_order_attribute (tree *, tree, tree, |
339 | int, bool *); | |
1cae46be | 340 | static tree handle_constructor_attribute (tree *, tree, tree, int, bool *); |
341 | static tree handle_destructor_attribute (tree *, tree, tree, int, bool *); | |
342 | static tree handle_mode_attribute (tree *, tree, tree, int, bool *); | |
343 | static tree handle_section_attribute (tree *, tree, tree, int, bool *); | |
344 | static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); | |
345 | static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; | |
4641882f | 346 | static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ; |
85c0a25c | 347 | static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *); |
348 | static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *); | |
1cae46be | 349 | static tree handle_alias_attribute (tree *, tree, tree, int, bool *); |
f4a30bd7 | 350 | static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ; |
1cae46be | 351 | static tree handle_visibility_attribute (tree *, tree, tree, int, |
352 | bool *); | |
353 | static tree handle_tls_model_attribute (tree *, tree, tree, int, | |
354 | bool *); | |
355 | static tree handle_no_instrument_function_attribute (tree *, tree, | |
356 | tree, int, bool *); | |
357 | static tree handle_malloc_attribute (tree *, tree, tree, int, bool *); | |
26d1c5ff | 358 | static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 359 | static tree handle_no_limit_stack_attribute (tree *, tree, tree, int, |
360 | bool *); | |
361 | static tree handle_pure_attribute (tree *, tree, tree, int, bool *); | |
4c0315d0 | 362 | static tree handle_tm_attribute (tree *, tree, tree, int, bool *); |
363 | static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *); | |
fc09b200 | 364 | static tree handle_novops_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 365 | static tree handle_deprecated_attribute (tree *, tree, tree, int, |
366 | bool *); | |
367 | static tree handle_vector_size_attribute (tree *, tree, tree, int, | |
368 | bool *); | |
369 | static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *); | |
370 | static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *); | |
371 | static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *); | |
8a8cdb8d | 372 | static tree handle_warn_unused_result_attribute (tree *, tree, tree, int, |
373 | bool *); | |
bf6c8de0 | 374 | static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *); |
b5c26b42 | 375 | static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *); |
4a29c97c | 376 | static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *); |
237e78b1 | 377 | static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *); |
378 | static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *); | |
24470055 | 379 | static tree handle_target_attribute (tree *, tree, tree, int, bool *); |
ab50af2a | 380 | static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *); |
46f8e3b0 | 381 | static tree handle_optimize_attribute (tree *, tree, tree, int, bool *); |
4c0315d0 | 382 | static tree ignore_attribute (tree *, tree, tree, int, bool *); |
48b14f50 | 383 | static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *); |
8ce86007 | 384 | static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *); |
a96c3cc1 | 385 | static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *); |
d7dcba40 | 386 | static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *); |
bc7bff74 | 387 | static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int, |
388 | bool *); | |
c58a4cfd | 389 | static tree handle_simd_attribute (tree *, tree, tree, int, bool *); |
bc7bff74 | 390 | static tree handle_omp_declare_target_attribute (tree *, tree, tree, int, |
391 | bool *); | |
74691f46 | 392 | static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *); |
058a1b7a | 393 | static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *); |
394 | static tree handle_bnd_legacy (tree *, tree, tree, int, bool *); | |
395 | static tree handle_bnd_instrument (tree *, tree, tree, int, bool *); | |
1cae46be | 396 | |
d01f58f9 | 397 | static void check_function_nonnull (tree, int, tree *); |
1cae46be | 398 | static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); |
399 | static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT); | |
400 | static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); | |
860251be | 401 | static int resort_field_decl_cmp (const void *, const void *); |
dbf6c367 | 402 | |
5c6e5756 | 403 | /* Reserved words. The third field is a mask: keywords are disabled |
404 | if they match the mask. | |
405 | ||
406 | Masks for languages: | |
407 | C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC | |
408 | C --std=c99: D_CXXONLY | D_OBJC | |
409 | ObjC is like C except that D_OBJC and D_CXX_OBJC are not set | |
410 | C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | |
411 | C++ --std=c0x: D_CONLY | D_OBJC | |
412 | ObjC++ is like C++ except that D_OBJC is not set | |
413 | ||
414 | If -fno-asm is used, D_ASM is added to the mask. If | |
415 | -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in | |
416 | C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords. | |
b27e241e | 417 | In C with -Wc++-compat, we warn if D_CXXWARN is set. |
5c6e5756 | 418 | |
b27e241e | 419 | Note the complication of the D_CXX_OBJC keywords. These are |
420 | reserved words such as 'class'. In C++, 'class' is a reserved | |
421 | word. In Objective-C++ it is too. In Objective-C, it is a | |
422 | reserved word too, but only if it follows an '@' sign. | |
423 | */ | |
5c6e5756 | 424 | const struct c_common_resword c_common_reswords[] = |
425 | { | |
83e25171 | 426 | { "_Alignas", RID_ALIGNAS, D_CONLY }, |
427 | { "_Alignof", RID_ALIGNOF, D_CONLY }, | |
b560fabd | 428 | { "_Atomic", RID_ATOMIC, D_CONLY }, |
5c6e5756 | 429 | { "_Bool", RID_BOOL, D_CONLY }, |
430 | { "_Complex", RID_COMPLEX, 0 }, | |
d037099f | 431 | { "_Cilk_spawn", RID_CILK_SPAWN, 0 }, |
432 | { "_Cilk_sync", RID_CILK_SYNC, 0 }, | |
40750995 | 433 | { "_Cilk_for", RID_CILK_FOR, 0 }, |
c1800156 | 434 | { "_Imaginary", RID_IMAGINARY, D_CONLY }, |
5c6e5756 | 435 | { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT }, |
436 | { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT }, | |
437 | { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT }, | |
438 | { "_Fract", RID_FRACT, D_CONLY | D_EXT }, | |
439 | { "_Accum", RID_ACCUM, D_CONLY | D_EXT }, | |
440 | { "_Sat", RID_SAT, D_CONLY | D_EXT }, | |
f80e7755 | 441 | { "_Static_assert", RID_STATIC_ASSERT, D_CONLY }, |
985c6e3a | 442 | { "_Noreturn", RID_NORETURN, D_CONLY }, |
7aa04c8d | 443 | { "_Generic", RID_GENERIC, D_CONLY }, |
d184e0c0 | 444 | { "_Thread_local", RID_THREAD, D_CONLY }, |
5c6e5756 | 445 | { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, |
446 | { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, | |
447 | { "__alignof", RID_ALIGNOF, 0 }, | |
448 | { "__alignof__", RID_ALIGNOF, 0 }, | |
449 | { "__asm", RID_ASM, 0 }, | |
450 | { "__asm__", RID_ASM, 0 }, | |
451 | { "__attribute", RID_ATTRIBUTE, 0 }, | |
452 | { "__attribute__", RID_ATTRIBUTE, 0 }, | |
4fba5eb9 | 453 | { "__auto_type", RID_AUTO_TYPE, D_CONLY }, |
e6014a82 | 454 | { "__bases", RID_BASES, D_CXXONLY }, |
6f58cf06 | 455 | { "__builtin_call_with_static_chain", |
456 | RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY }, | |
5c6e5756 | 457 | { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, |
bff4ad11 | 458 | { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY }, |
bf0cb017 | 459 | { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 }, |
5c6e5756 | 460 | { "__builtin_offsetof", RID_OFFSETOF, 0 }, |
461 | { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, | |
462 | { "__builtin_va_arg", RID_VA_ARG, 0 }, | |
463 | { "__complex", RID_COMPLEX, 0 }, | |
464 | { "__complex__", RID_COMPLEX, 0 }, | |
465 | { "__const", RID_CONST, 0 }, | |
466 | { "__const__", RID_CONST, 0 }, | |
467 | { "__decltype", RID_DECLTYPE, D_CXXONLY }, | |
e6014a82 | 468 | { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY }, |
5c6e5756 | 469 | { "__extension__", RID_EXTENSION, 0 }, |
470 | { "__func__", RID_C99_FUNCTION_NAME, 0 }, | |
471 | { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY }, | |
472 | { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY }, | |
473 | { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY }, | |
474 | { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY }, | |
475 | { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY }, | |
476 | { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY }, | |
477 | { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY }, | |
478 | { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY }, | |
23407dc9 | 479 | { "__imag", RID_IMAGPART, 0 }, |
480 | { "__imag__", RID_IMAGPART, 0 }, | |
481 | { "__inline", RID_INLINE, 0 }, | |
482 | { "__inline__", RID_INLINE, 0 }, | |
5c6e5756 | 483 | { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY }, |
484 | { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY }, | |
485 | { "__is_class", RID_IS_CLASS, D_CXXONLY }, | |
5c6e5756 | 486 | { "__is_empty", RID_IS_EMPTY, D_CXXONLY }, |
487 | { "__is_enum", RID_IS_ENUM, D_CXXONLY }, | |
aa4313eb | 488 | { "__is_final", RID_IS_FINAL, D_CXXONLY }, |
23407dc9 | 489 | { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY }, |
5c6e5756 | 490 | { "__is_pod", RID_IS_POD, D_CXXONLY }, |
491 | { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY }, | |
56c12fd4 | 492 | { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY }, |
c1c67b4f | 493 | { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY }, |
494 | { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY }, | |
f76a9aa8 | 495 | { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY }, |
496 | { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY }, | |
717e52f9 | 497 | { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY }, |
5c6e5756 | 498 | { "__is_union", RID_IS_UNION, D_CXXONLY }, |
5c6e5756 | 499 | { "__label__", RID_LABEL, 0 }, |
500 | { "__null", RID_NULL, 0 }, | |
501 | { "__real", RID_REALPART, 0 }, | |
502 | { "__real__", RID_REALPART, 0 }, | |
503 | { "__restrict", RID_RESTRICT, 0 }, | |
504 | { "__restrict__", RID_RESTRICT, 0 }, | |
505 | { "__signed", RID_SIGNED, 0 }, | |
506 | { "__signed__", RID_SIGNED, 0 }, | |
507 | { "__thread", RID_THREAD, 0 }, | |
4c0315d0 | 508 | { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 }, |
509 | { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 }, | |
510 | { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 }, | |
5c6e5756 | 511 | { "__typeof", RID_TYPEOF, 0 }, |
512 | { "__typeof__", RID_TYPEOF, 0 }, | |
23407dc9 | 513 | { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY }, |
5c6e5756 | 514 | { "__volatile", RID_VOLATILE, 0 }, |
515 | { "__volatile__", RID_VOLATILE, 0 }, | |
fa769cc5 | 516 | { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN }, |
517 | { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN }, | |
5c6e5756 | 518 | { "asm", RID_ASM, D_ASM }, |
519 | { "auto", RID_AUTO, 0 }, | |
dbd982c9 | 520 | { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 521 | { "break", RID_BREAK, 0 }, |
522 | { "case", RID_CASE, 0 }, | |
51030405 | 523 | { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN }, |
5c6e5756 | 524 | { "char", RID_CHAR, 0 }, |
fa769cc5 | 525 | { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN }, |
526 | { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN }, | |
51030405 | 527 | { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN }, |
5c6e5756 | 528 | { "const", RID_CONST, 0 }, |
fa769cc5 | 529 | { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN }, |
5c6e5756 | 530 | { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN }, |
531 | { "continue", RID_CONTINUE, 0 }, | |
fa769cc5 | 532 | { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN }, |
5c6e5756 | 533 | { "default", RID_DEFAULT, 0 }, |
51030405 | 534 | { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 535 | { "do", RID_DO, 0 }, |
536 | { "double", RID_DOUBLE, 0 }, | |
537 | { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN }, | |
538 | { "else", RID_ELSE, 0 }, | |
539 | { "enum", RID_ENUM, 0 }, | |
51030405 | 540 | { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN }, |
541 | { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 542 | { "extern", RID_EXTERN, 0 }, |
51030405 | 543 | { "false", RID_FALSE, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 544 | { "float", RID_FLOAT, 0 }, |
545 | { "for", RID_FOR, 0 }, | |
51030405 | 546 | { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 547 | { "goto", RID_GOTO, 0 }, |
548 | { "if", RID_IF, 0 }, | |
549 | { "inline", RID_INLINE, D_EXT89 }, | |
550 | { "int", RID_INT, 0 }, | |
551 | { "long", RID_LONG, 0 }, | |
552 | { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN }, | |
51030405 | 553 | { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN }, |
554 | { "new", RID_NEW, D_CXXONLY | D_CXXWARN }, | |
fa769cc5 | 555 | { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN }, |
556 | { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN }, | |
51030405 | 557 | { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN }, |
558 | { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN }, | |
559 | { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN }, | |
560 | { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 561 | { "register", RID_REGISTER, 0 }, |
562 | { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN }, | |
563 | { "restrict", RID_RESTRICT, D_CONLY | D_C99 }, | |
564 | { "return", RID_RETURN, 0 }, | |
565 | { "short", RID_SHORT, 0 }, | |
566 | { "signed", RID_SIGNED, 0 }, | |
567 | { "sizeof", RID_SIZEOF, 0 }, | |
568 | { "static", RID_STATIC, 0 }, | |
fa769cc5 | 569 | { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN }, |
5c6e5756 | 570 | { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN }, |
571 | { "struct", RID_STRUCT, 0 }, | |
572 | { "switch", RID_SWITCH, 0 }, | |
51030405 | 573 | { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN }, |
574 | { "this", RID_THIS, D_CXXONLY | D_CXXWARN }, | |
fa769cc5 | 575 | { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN }, |
51030405 | 576 | { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN }, |
577 | { "true", RID_TRUE, D_CXXONLY | D_CXXWARN }, | |
578 | { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 579 | { "typedef", RID_TYPEDEF, 0 }, |
51030405 | 580 | { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN }, |
581 | { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 582 | { "typeof", RID_TYPEOF, D_ASM | D_EXT }, |
583 | { "union", RID_UNION, 0 }, | |
584 | { "unsigned", RID_UNSIGNED, 0 }, | |
51030405 | 585 | { "using", RID_USING, D_CXXONLY | D_CXXWARN }, |
586 | { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 587 | { "void", RID_VOID, 0 }, |
588 | { "volatile", RID_VOLATILE, 0 }, | |
589 | { "wchar_t", RID_WCHAR, D_CXXONLY }, | |
590 | { "while", RID_WHILE, 0 }, | |
56c12fd4 | 591 | |
6d02e6b2 | 592 | /* C++ transactional memory. */ |
593 | { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM }, | |
594 | { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM }, | |
595 | { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM }, | |
596 | { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM }, | |
597 | ||
56c12fd4 | 598 | /* Concepts-related keywords */ |
599 | { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN }, | |
600 | { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN }, | |
601 | ||
5c6e5756 | 602 | /* These Objective-C keywords are recognized only immediately after |
603 | an '@'. */ | |
604 | { "compatibility_alias", RID_AT_ALIAS, D_OBJC }, | |
605 | { "defs", RID_AT_DEFS, D_OBJC }, | |
606 | { "encode", RID_AT_ENCODE, D_OBJC }, | |
607 | { "end", RID_AT_END, D_OBJC }, | |
608 | { "implementation", RID_AT_IMPLEMENTATION, D_OBJC }, | |
609 | { "interface", RID_AT_INTERFACE, D_OBJC }, | |
610 | { "protocol", RID_AT_PROTOCOL, D_OBJC }, | |
611 | { "selector", RID_AT_SELECTOR, D_OBJC }, | |
612 | { "finally", RID_AT_FINALLY, D_OBJC }, | |
069761fb | 613 | { "optional", RID_AT_OPTIONAL, D_OBJC }, |
614 | { "required", RID_AT_REQUIRED, D_OBJC }, | |
86c110ac | 615 | { "property", RID_AT_PROPERTY, D_OBJC }, |
4a8875ed | 616 | { "package", RID_AT_PACKAGE, D_OBJC }, |
e1f293c0 | 617 | { "synthesize", RID_AT_SYNTHESIZE, D_OBJC }, |
618 | { "dynamic", RID_AT_DYNAMIC, D_OBJC }, | |
5c6e5756 | 619 | /* These are recognized only in protocol-qualifier context |
620 | (see above) */ | |
621 | { "bycopy", RID_BYCOPY, D_OBJC }, | |
622 | { "byref", RID_BYREF, D_OBJC }, | |
623 | { "in", RID_IN, D_OBJC }, | |
624 | { "inout", RID_INOUT, D_OBJC }, | |
625 | { "oneway", RID_ONEWAY, D_OBJC }, | |
626 | { "out", RID_OUT, D_OBJC }, | |
86c110ac | 627 | /* These are recognized inside a property attribute list */ |
7590f0e5 | 628 | { "assign", RID_ASSIGN, D_OBJC }, |
629 | { "copy", RID_COPY, D_OBJC }, | |
86c110ac | 630 | { "getter", RID_GETTER, D_OBJC }, |
7590f0e5 | 631 | { "nonatomic", RID_NONATOMIC, D_OBJC }, |
632 | { "readonly", RID_READONLY, D_OBJC }, | |
633 | { "readwrite", RID_READWRITE, D_OBJC }, | |
634 | { "retain", RID_RETAIN, D_OBJC }, | |
86c110ac | 635 | { "setter", RID_SETTER, D_OBJC }, |
5c6e5756 | 636 | }; |
637 | ||
638 | const unsigned int num_c_common_reswords = | |
639 | sizeof c_common_reswords / sizeof (struct c_common_resword); | |
640 | ||
058a1b7a | 641 | /* Table of machine-independent attributes common to all C-like languages. |
642 | ||
643 | All attributes referencing arguments should be additionally processed | |
644 | in chkp_copy_function_type_adding_bounds for correct instrumentation | |
645 | by Pointer Bounds Checker. | |
646 | Current list of processed common attributes: nonnull. */ | |
f8e93a2e | 647 | const struct attribute_spec c_common_attribute_table[] = |
648 | { | |
ac86af5d | 649 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
650 | affects_type_identity } */ | |
f8e93a2e | 651 | { "packed", 0, 0, false, false, false, |
ac86af5d | 652 | handle_packed_attribute , false}, |
f8e93a2e | 653 | { "nocommon", 0, 0, true, false, false, |
ac86af5d | 654 | handle_nocommon_attribute, false}, |
f8e93a2e | 655 | { "common", 0, 0, true, false, false, |
ac86af5d | 656 | handle_common_attribute, false }, |
f8e93a2e | 657 | /* FIXME: logically, noreturn attributes should be listed as |
658 | "false, true, true" and apply to function types. But implementing this | |
659 | would require all the places in the compiler that use TREE_THIS_VOLATILE | |
660 | on a decl to identify non-returning functions to be located and fixed | |
661 | to check the function type instead. */ | |
662 | { "noreturn", 0, 0, true, false, false, | |
ac86af5d | 663 | handle_noreturn_attribute, false }, |
f8e93a2e | 664 | { "volatile", 0, 0, true, false, false, |
ac86af5d | 665 | handle_noreturn_attribute, false }, |
947aa916 | 666 | { "stack_protect", 0, 0, true, false, false, |
667 | handle_stack_protect_attribute, false }, | |
f8e93a2e | 668 | { "noinline", 0, 0, true, false, false, |
ac86af5d | 669 | handle_noinline_attribute, false }, |
bdb1f0d1 | 670 | { "noclone", 0, 0, true, false, false, |
ac86af5d | 671 | handle_noclone_attribute, false }, |
85fbea97 | 672 | { "no_icf", 0, 0, true, false, false, |
673 | handle_noicf_attribute, false }, | |
7bd95dfd | 674 | { "leaf", 0, 0, true, false, false, |
ac86af5d | 675 | handle_leaf_attribute, false }, |
f8e93a2e | 676 | { "always_inline", 0, 0, true, false, false, |
ac86af5d | 677 | handle_always_inline_attribute, false }, |
541e4101 | 678 | { "gnu_inline", 0, 0, true, false, false, |
ac86af5d | 679 | handle_gnu_inline_attribute, false }, |
1b16fc45 | 680 | { "artificial", 0, 0, true, false, false, |
ac86af5d | 681 | handle_artificial_attribute, false }, |
0cdd9887 | 682 | { "flatten", 0, 0, true, false, false, |
ac86af5d | 683 | handle_flatten_attribute, false }, |
f8e93a2e | 684 | { "used", 0, 0, true, false, false, |
ac86af5d | 685 | handle_used_attribute, false }, |
f8e93a2e | 686 | { "unused", 0, 0, false, false, false, |
ac86af5d | 687 | handle_unused_attribute, false }, |
62eec3b4 | 688 | { "externally_visible", 0, 0, true, false, false, |
ac86af5d | 689 | handle_externally_visible_attribute, false }, |
6b722052 | 690 | { "no_reorder", 0, 0, true, false, false, |
691 | handle_no_reorder_attribute, false }, | |
f8e93a2e | 692 | /* The same comments as for noreturn attributes apply to const ones. */ |
693 | { "const", 0, 0, true, false, false, | |
ac86af5d | 694 | handle_const_attribute, false }, |
292237f3 | 695 | { "scalar_storage_order", 1, 1, false, false, false, |
696 | handle_scalar_storage_order_attribute, false }, | |
f8e93a2e | 697 | { "transparent_union", 0, 0, false, false, false, |
ac86af5d | 698 | handle_transparent_union_attribute, false }, |
9af7fd5b | 699 | { "constructor", 0, 1, true, false, false, |
ac86af5d | 700 | handle_constructor_attribute, false }, |
9af7fd5b | 701 | { "destructor", 0, 1, true, false, false, |
ac86af5d | 702 | handle_destructor_attribute, false }, |
f8e93a2e | 703 | { "mode", 1, 1, false, true, false, |
ac86af5d | 704 | handle_mode_attribute, false }, |
f8e93a2e | 705 | { "section", 1, 1, true, false, false, |
ac86af5d | 706 | handle_section_attribute, false }, |
f8e93a2e | 707 | { "aligned", 0, 1, false, false, false, |
ac86af5d | 708 | handle_aligned_attribute, false }, |
f8e93a2e | 709 | { "weak", 0, 0, true, false, false, |
ac86af5d | 710 | handle_weak_attribute, false }, |
4641882f | 711 | { "noplt", 0, 0, true, false, false, |
712 | handle_noplt_attribute, false }, | |
85c0a25c | 713 | { "ifunc", 1, 1, true, false, false, |
ac86af5d | 714 | handle_ifunc_attribute, false }, |
f8e93a2e | 715 | { "alias", 1, 1, true, false, false, |
ac86af5d | 716 | handle_alias_attribute, false }, |
f4a30bd7 | 717 | { "weakref", 0, 1, true, false, false, |
ac86af5d | 718 | handle_weakref_attribute, false }, |
f8e93a2e | 719 | { "no_instrument_function", 0, 0, true, false, false, |
ac86af5d | 720 | handle_no_instrument_function_attribute, |
721 | false }, | |
f8e93a2e | 722 | { "malloc", 0, 0, true, false, false, |
ac86af5d | 723 | handle_malloc_attribute, false }, |
26d1c5ff | 724 | { "returns_twice", 0, 0, true, false, false, |
ac86af5d | 725 | handle_returns_twice_attribute, false }, |
f8e93a2e | 726 | { "no_stack_limit", 0, 0, true, false, false, |
ac86af5d | 727 | handle_no_limit_stack_attribute, false }, |
f8e93a2e | 728 | { "pure", 0, 0, true, false, false, |
ac86af5d | 729 | handle_pure_attribute, false }, |
4c0315d0 | 730 | { "transaction_callable", 0, 0, false, true, false, |
731 | handle_tm_attribute, false }, | |
732 | { "transaction_unsafe", 0, 0, false, true, false, | |
6d02e6b2 | 733 | handle_tm_attribute, true }, |
4c0315d0 | 734 | { "transaction_safe", 0, 0, false, true, false, |
6d02e6b2 | 735 | handle_tm_attribute, true }, |
736 | { "transaction_safe_dynamic", 0, 0, true, false, false, | |
4c0315d0 | 737 | handle_tm_attribute, false }, |
738 | { "transaction_may_cancel_outer", 0, 0, false, true, false, | |
739 | handle_tm_attribute, false }, | |
740 | /* ??? These two attributes didn't make the transition from the | |
741 | Intel language document to the multi-vendor language document. */ | |
742 | { "transaction_pure", 0, 0, false, true, false, | |
743 | handle_tm_attribute, false }, | |
744 | { "transaction_wrap", 1, 1, true, false, false, | |
745 | handle_tm_wrap_attribute, false }, | |
fc09b200 | 746 | /* For internal use (marking of builtins) only. The name contains space |
747 | to prevent its usage in source code. */ | |
748 | { "no vops", 0, 0, true, false, false, | |
ac86af5d | 749 | handle_novops_attribute, false }, |
45c4e798 | 750 | { "deprecated", 0, 1, false, false, false, |
ac86af5d | 751 | handle_deprecated_attribute, false }, |
f8e93a2e | 752 | { "vector_size", 1, 1, false, true, false, |
ac86af5d | 753 | handle_vector_size_attribute, false }, |
b212f378 | 754 | { "visibility", 1, 1, false, false, false, |
ac86af5d | 755 | handle_visibility_attribute, false }, |
24dfead4 | 756 | { "tls_model", 1, 1, true, false, false, |
ac86af5d | 757 | handle_tls_model_attribute, false }, |
dbf6c367 | 758 | { "nonnull", 0, -1, false, true, true, |
ac86af5d | 759 | handle_nonnull_attribute, false }, |
fa987697 | 760 | { "nothrow", 0, 0, true, false, false, |
ac86af5d | 761 | handle_nothrow_attribute, false }, |
762 | { "may_alias", 0, 0, false, true, false, NULL, false }, | |
7acb29a3 | 763 | { "cleanup", 1, 1, true, false, false, |
ac86af5d | 764 | handle_cleanup_attribute, false }, |
8a8cdb8d | 765 | { "warn_unused_result", 0, 0, false, true, true, |
ac86af5d | 766 | handle_warn_unused_result_attribute, false }, |
50ca527f | 767 | { "sentinel", 0, 1, false, true, true, |
ac86af5d | 768 | handle_sentinel_attribute, false }, |
b5c26b42 | 769 | /* For internal use (marking of builtins) only. The name contains space |
770 | to prevent its usage in source code. */ | |
771 | { "type generic", 0, 0, false, true, true, | |
ac86af5d | 772 | handle_type_generic_attribute, false }, |
4a29c97c | 773 | { "alloc_size", 1, 2, false, true, true, |
ac86af5d | 774 | handle_alloc_size_attribute, false }, |
5de92639 | 775 | { "cold", 0, 0, true, false, false, |
ac86af5d | 776 | handle_cold_attribute, false }, |
5de92639 | 777 | { "hot", 0, 0, true, false, false, |
ac86af5d | 778 | handle_hot_attribute, false }, |
d413ffdd | 779 | { "no_address_safety_analysis", |
780 | 0, 0, true, false, false, | |
781 | handle_no_address_safety_analysis_attribute, | |
782 | false }, | |
a9196da9 | 783 | { "no_sanitize_address", 0, 0, true, false, false, |
784 | handle_no_sanitize_address_attribute, | |
785 | false }, | |
d1e96383 | 786 | { "no_sanitize_thread", 0, 0, true, false, false, |
787 | handle_no_sanitize_address_attribute, | |
788 | false }, | |
05f893e1 | 789 | { "no_sanitize_undefined", 0, 0, true, false, false, |
790 | handle_no_sanitize_undefined_attribute, | |
791 | false }, | |
10fc867f | 792 | { "warning", 1, 1, true, false, false, |
ac86af5d | 793 | handle_error_attribute, false }, |
10fc867f | 794 | { "error", 1, 1, true, false, false, |
ac86af5d | 795 | handle_error_attribute, false }, |
24470055 | 796 | { "target", 1, -1, true, false, false, |
ac86af5d | 797 | handle_target_attribute, false }, |
ab50af2a | 798 | { "target_clones", 1, -1, true, false, false, |
799 | handle_target_clones_attribute, false }, | |
46f8e3b0 | 800 | { "optimize", 1, -1, true, false, false, |
ac86af5d | 801 | handle_optimize_attribute, false }, |
4c0315d0 | 802 | /* For internal use only. The leading '*' both prevents its usage in |
803 | source code and signals that it may be overridden by machine tables. */ | |
804 | { "*tm regparm", 0, 0, false, true, true, | |
805 | ignore_attribute, false }, | |
48b14f50 | 806 | { "no_split_stack", 0, 0, true, false, false, |
ac86af5d | 807 | handle_no_split_stack_attribute, false }, |
8ce86007 | 808 | /* For internal use (marking of builtins and runtime functions) only. |
809 | The name contains space to prevent its usage in source code. */ | |
810 | { "fn spec", 1, 1, false, true, true, | |
ac86af5d | 811 | handle_fnspec_attribute, false }, |
a96c3cc1 | 812 | { "warn_unused", 0, 0, false, false, false, |
813 | handle_warn_unused_attribute, false }, | |
d7dcba40 | 814 | { "returns_nonnull", 0, 0, false, true, true, |
815 | handle_returns_nonnull_attribute, false }, | |
bc7bff74 | 816 | { "omp declare simd", 0, -1, true, false, false, |
817 | handle_omp_declare_simd_attribute, false }, | |
74acc703 | 818 | { "cilk simd function", 0, -1, true, false, false, |
819 | handle_omp_declare_simd_attribute, false }, | |
9841c6ee | 820 | { "simd", 0, 1, true, false, false, |
c58a4cfd | 821 | handle_simd_attribute, false }, |
bc7bff74 | 822 | { "omp declare target", 0, 0, true, false, false, |
823 | handle_omp_declare_target_attribute, false }, | |
237e78b1 | 824 | { "alloc_align", 1, 1, false, true, true, |
825 | handle_alloc_align_attribute, false }, | |
826 | { "assume_aligned", 1, 2, false, true, true, | |
827 | handle_assume_aligned_attribute, false }, | |
74691f46 | 828 | { "designated_init", 0, 0, false, true, false, |
829 | handle_designated_init_attribute, false }, | |
058a1b7a | 830 | { "bnd_variable_size", 0, 0, true, false, false, |
831 | handle_bnd_variable_size_attribute, false }, | |
832 | { "bnd_legacy", 0, 0, true, false, false, | |
833 | handle_bnd_legacy, false }, | |
834 | { "bnd_instrument", 0, 0, true, false, false, | |
835 | handle_bnd_instrument, false }, | |
ac86af5d | 836 | { NULL, 0, 0, false, false, false, NULL, false } |
f8e93a2e | 837 | }; |
838 | ||
839 | /* Give the specifications for the format attributes, used by C and all | |
058a1b7a | 840 | descendants. |
f8e93a2e | 841 | |
058a1b7a | 842 | All attributes referencing arguments should be additionally processed |
843 | in chkp_copy_function_type_adding_bounds for correct instrumentation | |
844 | by Pointer Bounds Checker. | |
845 | Current list of processed format attributes: format, format_arg. */ | |
f8e93a2e | 846 | const struct attribute_spec c_common_format_attribute_table[] = |
847 | { | |
ac86af5d | 848 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
849 | affects_type_identity } */ | |
f8e93a2e | 850 | { "format", 3, 3, false, true, true, |
ac86af5d | 851 | handle_format_attribute, false }, |
f8e93a2e | 852 | { "format_arg", 1, 1, false, true, true, |
ac86af5d | 853 | handle_format_arg_attribute, false }, |
854 | { NULL, 0, 0, false, false, false, NULL, false } | |
f8e93a2e | 855 | }; |
856 | ||
6d5d708e | 857 | /* Return identifier for address space AS. */ |
34208e18 | 858 | |
6d5d708e | 859 | const char * |
860 | c_addr_space_name (addr_space_t as) | |
861 | { | |
34208e18 | 862 | int rid = RID_FIRST_ADDR_SPACE + as; |
863 | gcc_assert (ridpointers [rid]); | |
864 | return IDENTIFIER_POINTER (ridpointers [rid]); | |
6d5d708e | 865 | } |
866 | ||
2c0e001b | 867 | /* Push current bindings for the function name VAR_DECLS. */ |
f4e3c278 | 868 | |
869 | void | |
1cae46be | 870 | start_fname_decls (void) |
f4e3c278 | 871 | { |
65b7f83f | 872 | unsigned ix; |
873 | tree saved = NULL_TREE; | |
1cae46be | 874 | |
65b7f83f | 875 | for (ix = 0; fname_vars[ix].decl; ix++) |
876 | { | |
877 | tree decl = *fname_vars[ix].decl; | |
f4e3c278 | 878 | |
65b7f83f | 879 | if (decl) |
880 | { | |
ceb7b692 | 881 | saved = tree_cons (decl, build_int_cst (integer_type_node, ix), |
882 | saved); | |
65b7f83f | 883 | *fname_vars[ix].decl = NULL_TREE; |
884 | } | |
885 | } | |
886 | if (saved || saved_function_name_decls) | |
887 | /* Normally they'll have been NULL, so only push if we've got a | |
888 | stack, or they are non-NULL. */ | |
889 | saved_function_name_decls = tree_cons (saved, NULL_TREE, | |
890 | saved_function_name_decls); | |
891 | } | |
892 | ||
2363ef00 | 893 | /* Finish up the current bindings, adding them into the current function's |
894 | statement tree. This must be done _before_ finish_stmt_tree is called. | |
895 | If there is no current function, we must be at file scope and no statements | |
896 | are involved. Pop the previous bindings. */ | |
65b7f83f | 897 | |
898 | void | |
1cae46be | 899 | finish_fname_decls (void) |
65b7f83f | 900 | { |
901 | unsigned ix; | |
2363ef00 | 902 | tree stmts = NULL_TREE; |
65b7f83f | 903 | tree stack = saved_function_name_decls; |
904 | ||
905 | for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack)) | |
2363ef00 | 906 | append_to_statement_list (TREE_VALUE (stack), &stmts); |
1cae46be | 907 | |
2363ef00 | 908 | if (stmts) |
65b7f83f | 909 | { |
2363ef00 | 910 | tree *bodyp = &DECL_SAVED_TREE (current_function_decl); |
5c423bd6 | 911 | |
2363ef00 | 912 | if (TREE_CODE (*bodyp) == BIND_EXPR) |
913 | bodyp = &BIND_EXPR_BODY (*bodyp); | |
81010c97 | 914 | |
bc2b76e0 | 915 | append_to_statement_list_force (*bodyp, &stmts); |
2363ef00 | 916 | *bodyp = stmts; |
65b7f83f | 917 | } |
1cae46be | 918 | |
65b7f83f | 919 | for (ix = 0; fname_vars[ix].decl; ix++) |
920 | *fname_vars[ix].decl = NULL_TREE; | |
1cae46be | 921 | |
65b7f83f | 922 | if (stack) |
f4e3c278 | 923 | { |
2c0e001b | 924 | /* We had saved values, restore them. */ |
65b7f83f | 925 | tree saved; |
926 | ||
927 | for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved)) | |
928 | { | |
929 | tree decl = TREE_PURPOSE (saved); | |
f9ae6f95 | 930 | unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved)); |
1cae46be | 931 | |
65b7f83f | 932 | *fname_vars[ix].decl = decl; |
933 | } | |
934 | stack = TREE_CHAIN (stack); | |
f4e3c278 | 935 | } |
65b7f83f | 936 | saved_function_name_decls = stack; |
937 | } | |
938 | ||
81010c97 | 939 | /* Return the text name of the current function, suitably prettified |
5fc7fa69 | 940 | by PRETTY_P. Return string must be freed by caller. */ |
65b7f83f | 941 | |
942 | const char * | |
1cae46be | 943 | fname_as_string (int pretty_p) |
65b7f83f | 944 | { |
9ad4bb1e | 945 | const char *name = "top level"; |
5fc7fa69 | 946 | char *namep; |
8115b8be | 947 | int vrb = 2, len; |
948 | cpp_string cstr = { 0, 0 }, strname; | |
9ad4bb1e | 949 | |
84166705 | 950 | if (!pretty_p) |
9ad4bb1e | 951 | { |
952 | name = ""; | |
953 | vrb = 0; | |
954 | } | |
955 | ||
956 | if (current_function_decl) | |
dc24ddbd | 957 | name = lang_hooks.decl_printable_name (current_function_decl, vrb); |
9ad4bb1e | 958 | |
8115b8be | 959 | len = strlen (name) + 3; /* Two for '"'s. One for NULL. */ |
5fc7fa69 | 960 | |
8115b8be | 961 | namep = XNEWVEC (char, len); |
962 | snprintf (namep, len, "\"%s\"", name); | |
963 | strname.text = (unsigned char *) namep; | |
964 | strname.len = len - 1; | |
5fc7fa69 | 965 | |
924bbf02 | 966 | if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING)) |
8115b8be | 967 | { |
968 | XDELETEVEC (namep); | |
969 | return (const char *) cstr.text; | |
5fc7fa69 | 970 | } |
5fc7fa69 | 971 | |
972 | return namep; | |
65b7f83f | 973 | } |
974 | ||
65b7f83f | 975 | /* Return the VAR_DECL for a const char array naming the current |
976 | function. If the VAR_DECL has not yet been created, create it | |
977 | now. RID indicates how it should be formatted and IDENTIFIER_NODE | |
978 | ID is its name (unfortunately C and C++ hold the RID values of | |
979 | keywords in different places, so we can't derive RID from ID in | |
e3b80d49 | 980 | this language independent code. LOC is the location of the |
981 | function. */ | |
65b7f83f | 982 | |
983 | tree | |
e3b80d49 | 984 | fname_decl (location_t loc, unsigned int rid, tree id) |
65b7f83f | 985 | { |
986 | unsigned ix; | |
987 | tree decl = NULL_TREE; | |
988 | ||
989 | for (ix = 0; fname_vars[ix].decl; ix++) | |
990 | if (fname_vars[ix].rid == rid) | |
991 | break; | |
992 | ||
993 | decl = *fname_vars[ix].decl; | |
994 | if (!decl) | |
f4e3c278 | 995 | { |
2222b3c6 | 996 | /* If a tree is built here, it would normally have the lineno of |
997 | the current statement. Later this tree will be moved to the | |
998 | beginning of the function and this line number will be wrong. | |
999 | To avoid this problem set the lineno to 0 here; that prevents | |
7299020b | 1000 | it from appearing in the RTL. */ |
2363ef00 | 1001 | tree stmts; |
9a6486a6 | 1002 | location_t saved_location = input_location; |
9a6486a6 | 1003 | input_location = UNKNOWN_LOCATION; |
1cae46be | 1004 | |
2363ef00 | 1005 | stmts = push_stmt_list (); |
e60a6f7b | 1006 | decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty); |
2363ef00 | 1007 | stmts = pop_stmt_list (stmts); |
1008 | if (!IS_EMPTY_STMT (stmts)) | |
1009 | saved_function_name_decls | |
1010 | = tree_cons (decl, stmts, saved_function_name_decls); | |
65b7f83f | 1011 | *fname_vars[ix].decl = decl; |
9a6486a6 | 1012 | input_location = saved_location; |
f4e3c278 | 1013 | } |
65b7f83f | 1014 | if (!ix && !current_function_decl) |
e3b80d49 | 1015 | pedwarn (loc, 0, "%qD is not defined outside of function scope", decl); |
81010c97 | 1016 | |
65b7f83f | 1017 | return decl; |
f4e3c278 | 1018 | } |
1019 | ||
070236f0 | 1020 | /* Given a STRING_CST, give it a suitable array-of-chars data type. */ |
b0fc3e72 | 1021 | |
1022 | tree | |
1cae46be | 1023 | fix_string_type (tree value) |
b0fc3e72 | 1024 | { |
070236f0 | 1025 | int length = TREE_STRING_LENGTH (value); |
1026 | int nchars; | |
00d26680 | 1027 | tree e_type, i_type, a_type; |
1028 | ||
73be5127 | 1029 | /* Compute the number of elements, for the array type. */ |
924bbf02 | 1030 | if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value)) |
1031 | { | |
1032 | nchars = length; | |
1033 | e_type = char_type_node; | |
1034 | } | |
1035 | else if (TREE_TYPE (value) == char16_array_type_node) | |
1036 | { | |
1037 | nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT); | |
1038 | e_type = char16_type_node; | |
1039 | } | |
1040 | else if (TREE_TYPE (value) == char32_array_type_node) | |
1041 | { | |
1042 | nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT); | |
1043 | e_type = char32_type_node; | |
1044 | } | |
1045 | else | |
1046 | { | |
1047 | nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT); | |
1048 | e_type = wchar_type_node; | |
1049 | } | |
b0fc3e72 | 1050 | |
1d752508 | 1051 | /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous |
1052 | limit in C++98 Annex B is very large (65536) and is not normative, | |
1053 | so we do not diagnose it (warn_overlength_strings is forced off | |
1054 | in c_common_post_options). */ | |
1055 | if (warn_overlength_strings) | |
1056 | { | |
1057 | const int nchars_max = flag_isoc99 ? 4095 : 509; | |
1058 | const int relevant_std = flag_isoc99 ? 99 : 90; | |
1059 | if (nchars - 1 > nchars_max) | |
1060 | /* Translators: The %d after 'ISO C' will be 90 or 99. Do not | |
1061 | separate the %d from the 'C'. 'ISO' should not be | |
1062 | translated, but it may be moved after 'C%d' in languages | |
1063 | where modifiers follow nouns. */ | |
21ca8540 | 1064 | pedwarn (input_location, OPT_Woverlength_strings, |
8864917d | 1065 | "string length %qd is greater than the length %qd " |
1d752508 | 1066 | "ISO C%d compilers are required to support", |
1067 | nchars - 1, nchars_max, relevant_std); | |
1068 | } | |
82cfc7f7 | 1069 | |
390be14e | 1070 | /* Create the array type for the string constant. The ISO C++ |
1071 | standard says that a string literal has type `const char[N]' or | |
1072 | `const wchar_t[N]'. We use the same logic when invoked as a C | |
1073 | front-end with -Wwrite-strings. | |
1074 | ??? We should change the type of an expression depending on the | |
1075 | state of a warning flag. We should just be warning -- see how | |
1076 | this is handled in the C++ front-end for the deprecated implicit | |
1077 | conversion from string literals to `char*' or `wchar_t*'. | |
00d26680 | 1078 | |
1079 | The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified | |
1080 | array type being the unqualified version of that type. | |
1081 | Therefore, if we are constructing an array of const char, we must | |
1082 | construct the matching unqualified array type first. The C front | |
1083 | end does not require this, but it does no harm, so we do it | |
1084 | unconditionally. */ | |
ceb7b692 | 1085 | i_type = build_index_type (size_int (nchars - 1)); |
00d26680 | 1086 | a_type = build_array_type (e_type, i_type); |
390be14e | 1087 | if (c_dialect_cxx() || warn_write_strings) |
aebc8537 | 1088 | a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST); |
3a10ba35 | 1089 | |
00d26680 | 1090 | TREE_TYPE (value) = a_type; |
b8e3b7ad | 1091 | TREE_CONSTANT (value) = 1; |
a814bad5 | 1092 | TREE_READONLY (value) = 1; |
b0fc3e72 | 1093 | TREE_STATIC (value) = 1; |
1094 | return value; | |
1095 | } | |
1096 | \f | |
9c691dbd | 1097 | /* Fold X for consideration by one of the warning functions when checking |
1098 | whether an expression has a constant value. */ | |
1099 | ||
1100 | static tree | |
1101 | fold_for_warn (tree x) | |
1102 | { | |
1103 | if (c_dialect_cxx ()) | |
1104 | return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL); | |
1105 | else | |
1106 | /* The C front-end has already folded X appropriately. */ | |
1107 | return x; | |
1108 | } | |
1109 | ||
2a1736ed | 1110 | /* Print a warning if a constant expression had overflow in folding. |
1111 | Invoke this function on every expression that the language | |
1112 | requires to be a constant expression. | |
1113 | Note the ANSI C standard says it is erroneous for a | |
1114 | constant expression to overflow. */ | |
b2806639 | 1115 | |
1116 | void | |
1cae46be | 1117 | constant_expression_warning (tree value) |
07317e69 | 1118 | { |
48e1416a | 1119 | if (warn_overflow && pedantic |
07317e69 | 1120 | && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
1121 | || TREE_CODE (value) == FIXED_CST | |
1122 | || TREE_CODE (value) == VECTOR_CST | |
1123 | || TREE_CODE (value) == COMPLEX_CST) | |
1124 | && TREE_OVERFLOW (value)) | |
21ca8540 | 1125 | pedwarn (input_location, OPT_Woverflow, "overflow in constant expression"); |
07317e69 | 1126 | } |
1127 | ||
1128 | /* The same as above but print an unconditional error. */ | |
1129 | void | |
1130 | constant_expression_error (tree value) | |
b2806639 | 1131 | { |
837e1122 | 1132 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
9421ebb9 | 1133 | || TREE_CODE (value) == FIXED_CST |
886cfd4f | 1134 | || TREE_CODE (value) == VECTOR_CST |
837e1122 | 1135 | || TREE_CODE (value) == COMPLEX_CST) |
07317e69 | 1136 | && TREE_OVERFLOW (value)) |
1137 | error ("overflow in constant expression"); | |
2a1736ed | 1138 | } |
1139 | ||
f170d67f | 1140 | /* Print a warning if an expression had overflow in folding and its |
1141 | operands hadn't. | |
1142 | ||
2a1736ed | 1143 | Invoke this function on every expression that |
1144 | (1) appears in the source code, and | |
f170d67f | 1145 | (2) is a constant expression that overflowed, and |
2a1736ed | 1146 | (3) is not already checked by convert_and_check; |
f170d67f | 1147 | however, do not invoke this function on operands of explicit casts |
1148 | or when the expression is the result of an operator and any operand | |
1149 | already overflowed. */ | |
2a1736ed | 1150 | |
1151 | void | |
e60a6f7b | 1152 | overflow_warning (location_t loc, tree value) |
2a1736ed | 1153 | { |
48d94ede | 1154 | if (c_inhibit_evaluation_warnings != 0) |
1155 | return; | |
f170d67f | 1156 | |
1157 | switch (TREE_CODE (value)) | |
886cfd4f | 1158 | { |
f170d67f | 1159 | case INTEGER_CST: |
e60a6f7b | 1160 | warning_at (loc, OPT_Woverflow, "integer overflow in expression"); |
f170d67f | 1161 | break; |
48e1416a | 1162 | |
f170d67f | 1163 | case REAL_CST: |
e60a6f7b | 1164 | warning_at (loc, OPT_Woverflow, |
1165 | "floating point overflow in expression"); | |
f170d67f | 1166 | break; |
48e1416a | 1167 | |
9421ebb9 | 1168 | case FIXED_CST: |
e60a6f7b | 1169 | warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression"); |
9421ebb9 | 1170 | break; |
1171 | ||
f170d67f | 1172 | case VECTOR_CST: |
e60a6f7b | 1173 | warning_at (loc, OPT_Woverflow, "vector overflow in expression"); |
f170d67f | 1174 | break; |
48e1416a | 1175 | |
f170d67f | 1176 | case COMPLEX_CST: |
1177 | if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) | |
e60a6f7b | 1178 | warning_at (loc, OPT_Woverflow, |
1179 | "complex integer overflow in expression"); | |
f170d67f | 1180 | else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) |
e60a6f7b | 1181 | warning_at (loc, OPT_Woverflow, |
1182 | "complex floating point overflow in expression"); | |
f170d67f | 1183 | break; |
1184 | ||
1185 | default: | |
1186 | break; | |
886cfd4f | 1187 | } |
2a1736ed | 1188 | } |
1189 | ||
03033af4 | 1190 | /* Warn about uses of logical || / && operator in a context where it |
1191 | is likely that the bitwise equivalent was intended by the | |
1192 | programmer. We have seen an expression in which CODE is a binary | |
9c20c4fc | 1193 | operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding |
1194 | had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ | |
b13d1547 | 1195 | void |
9c20c4fc | 1196 | warn_logical_operator (location_t location, enum tree_code code, tree type, |
48e1416a | 1197 | enum tree_code code_left, tree op_left, |
03033af4 | 1198 | enum tree_code ARG_UNUSED (code_right), tree op_right) |
b13d1547 | 1199 | { |
9c20c4fc | 1200 | int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); |
1201 | int in0_p, in1_p, in_p; | |
1202 | tree low0, low1, low, high0, high1, high, lhs, rhs, tem; | |
1203 | bool strict_overflow_p = false; | |
1204 | ||
03033af4 | 1205 | if (code != TRUTH_ANDIF_EXPR |
1206 | && code != TRUTH_AND_EXPR | |
1207 | && code != TRUTH_ORIF_EXPR | |
1208 | && code != TRUTH_OR_EXPR) | |
1209 | return; | |
1210 | ||
439606a9 | 1211 | /* We don't want to warn if either operand comes from a macro |
1212 | expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet; | |
1213 | see PR61534. */ | |
1214 | if (from_macro_expansion_at (EXPR_LOCATION (op_left)) | |
1215 | || from_macro_expansion_at (EXPR_LOCATION (op_right))) | |
1216 | return; | |
1217 | ||
03033af4 | 1218 | /* Warn if &&/|| are being used in a context where it is |
1219 | likely that the bitwise equivalent was intended by the | |
1220 | programmer. That is, an expression such as op && MASK | |
1221 | where op should not be any boolean expression, nor a | |
1222 | constant, and mask seems to be a non-boolean integer constant. */ | |
9c691dbd | 1223 | if (TREE_CODE (op_right) == CONST_DECL) |
1224 | /* An enumerator counts as a constant. */ | |
1225 | op_right = DECL_INITIAL (op_right); | |
03033af4 | 1226 | if (!truth_value_p (code_left) |
1227 | && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) | |
1228 | && !CONSTANT_CLASS_P (op_left) | |
1229 | && !TREE_NO_WARNING (op_left) | |
1230 | && TREE_CODE (op_right) == INTEGER_CST | |
1231 | && !integer_zerop (op_right) | |
1232 | && !integer_onep (op_right)) | |
b13d1547 | 1233 | { |
9c20c4fc | 1234 | if (or_op) |
03033af4 | 1235 | warning_at (location, OPT_Wlogical_op, "logical %<or%>" |
1236 | " applied to non-boolean constant"); | |
1237 | else | |
1238 | warning_at (location, OPT_Wlogical_op, "logical %<and%>" | |
1239 | " applied to non-boolean constant"); | |
1240 | TREE_NO_WARNING (op_left) = true; | |
9c20c4fc | 1241 | return; |
1242 | } | |
1243 | ||
1244 | /* We do not warn for constants because they are typical of macro | |
1245 | expansions that test for features. */ | |
9c691dbd | 1246 | if (CONSTANT_CLASS_P (fold_for_warn (op_left)) |
1247 | || CONSTANT_CLASS_P (fold_for_warn (op_right))) | |
9c20c4fc | 1248 | return; |
1249 | ||
1250 | /* This warning only makes sense with logical operands. */ | |
1251 | if (!(truth_value_p (TREE_CODE (op_left)) | |
1252 | || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) | |
1253 | || !(truth_value_p (TREE_CODE (op_right)) | |
1254 | || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) | |
1255 | return; | |
1256 | ||
5e84569c | 1257 | /* The range computations only work with scalars. */ |
1258 | if (VECTOR_TYPE_P (TREE_TYPE (op_left)) | |
1259 | || VECTOR_TYPE_P (TREE_TYPE (op_right))) | |
1260 | return; | |
9c20c4fc | 1261 | |
686369e8 | 1262 | /* We first test whether either side separately is trivially true |
1263 | (with OR) or trivially false (with AND). If so, do not warn. | |
1264 | This is a common idiom for testing ranges of data types in | |
1265 | portable code. */ | |
1266 | lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); | |
1267 | if (!lhs) | |
1268 | return; | |
1269 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) | |
9c20c4fc | 1270 | lhs = C_MAYBE_CONST_EXPR_EXPR (lhs); |
1271 | ||
686369e8 | 1272 | /* If this is an OR operation, invert both sides; now, the result |
1273 | should be always false to get a warning. */ | |
1274 | if (or_op) | |
1275 | in0_p = !in0_p; | |
1276 | ||
1277 | tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); | |
d42e7c5a | 1278 | if (tem && integer_zerop (tem)) |
686369e8 | 1279 | return; |
1280 | ||
1281 | rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); | |
1282 | if (!rhs) | |
1283 | return; | |
1284 | if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) | |
9c20c4fc | 1285 | rhs = C_MAYBE_CONST_EXPR_EXPR (rhs); |
48e1416a | 1286 | |
686369e8 | 1287 | /* If this is an OR operation, invert both sides; now, the result |
1288 | should be always false to get a warning. */ | |
9c20c4fc | 1289 | if (or_op) |
686369e8 | 1290 | in1_p = !in1_p; |
48e1416a | 1291 | |
686369e8 | 1292 | tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); |
d42e7c5a | 1293 | if (tem && integer_zerop (tem)) |
686369e8 | 1294 | return; |
1295 | ||
1296 | /* If both expressions have the same operand, if we can merge the | |
485f6b9c | 1297 | ranges, ... */ |
686369e8 | 1298 | if (operand_equal_p (lhs, rhs, 0) |
9c20c4fc | 1299 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, |
485f6b9c | 1300 | in1_p, low1, high1)) |
9c20c4fc | 1301 | { |
485f6b9c | 1302 | tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high); |
1303 | /* ... and if the range test is always false, then warn. */ | |
1304 | if (tem && integer_zerop (tem)) | |
1305 | { | |
1306 | if (or_op) | |
1307 | warning_at (location, OPT_Wlogical_op, | |
1308 | "logical %<or%> of collectively exhaustive tests is " | |
1309 | "always true"); | |
1310 | else | |
1311 | warning_at (location, OPT_Wlogical_op, | |
1312 | "logical %<and%> of mutually exclusive tests is " | |
1313 | "always false"); | |
1314 | } | |
1315 | /* Or warn if the operands have exactly the same range, e.g. | |
1316 | A > 0 && A > 0. */ | |
0200602e | 1317 | else if (tree_int_cst_equal (low0, low1) |
1318 | && tree_int_cst_equal (high0, high1)) | |
485f6b9c | 1319 | { |
1320 | if (or_op) | |
1321 | warning_at (location, OPT_Wlogical_op, | |
1322 | "logical %<or%> of equal expressions"); | |
1323 | else | |
1324 | warning_at (location, OPT_Wlogical_op, | |
1325 | "logical %<and%> of equal expressions"); | |
1326 | } | |
b13d1547 | 1327 | } |
1328 | } | |
1329 | ||
6784a472 | 1330 | /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs |
1331 | with constant indices. */ | |
1332 | ||
1333 | static tree | |
1334 | find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data) | |
1335 | { | |
1336 | tree expr = *expr_p; | |
1337 | ||
1338 | if ((TREE_CODE (expr) == ARRAY_REF | |
1339 | || TREE_CODE (expr) == ARRAY_RANGE_REF) | |
1340 | && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST) | |
1341 | { | |
1342 | *(bool *) data = true; | |
1343 | *walk_subtrees = 0; | |
1344 | } | |
1345 | ||
1346 | return NULL_TREE; | |
1347 | } | |
1348 | ||
1349 | /* Warn if a self-comparison always evaluates to true or false. LOC | |
1350 | is the location of the comparison with code CODE, LHS and RHS are | |
1351 | operands of the comparison. */ | |
1352 | ||
1353 | void | |
1354 | warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs) | |
1355 | { | |
1356 | if (TREE_CODE_CLASS (code) != tcc_comparison) | |
1357 | return; | |
1358 | ||
d9e4fe27 | 1359 | /* Don't warn for various macro expansions. */ |
1360 | if (from_macro_expansion_at (loc) | |
1361 | || from_macro_expansion_at (EXPR_LOCATION (lhs)) | |
1362 | || from_macro_expansion_at (EXPR_LOCATION (rhs))) | |
1363 | return; | |
1364 | ||
6784a472 | 1365 | /* We do not warn for constants because they are typical of macro |
1366 | expansions that test for features, sizeof, and similar. */ | |
9c691dbd | 1367 | if (CONSTANT_CLASS_P (fold_for_warn (lhs)) |
1368 | || CONSTANT_CLASS_P (fold_for_warn (rhs))) | |
6784a472 | 1369 | return; |
1370 | ||
1371 | /* Don't warn for e.g. | |
1372 | HOST_WIDE_INT n; | |
1373 | ... | |
1374 | if (n == (long) n) ... | |
1375 | */ | |
1376 | if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR) | |
1377 | || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR)) | |
1378 | return; | |
1379 | ||
f58dd84b | 1380 | /* Don't warn if either LHS or RHS has an IEEE floating-point type. |
1381 | It could be a NaN, and NaN never compares equal to anything, even | |
1382 | itself. */ | |
1383 | if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs))) | |
1384 | return; | |
1385 | ||
6784a472 | 1386 | if (operand_equal_p (lhs, rhs, 0)) |
1387 | { | |
1388 | /* Don't warn about array references with constant indices; | |
1389 | these are likely to come from a macro. */ | |
1390 | bool found = false; | |
1391 | walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r, | |
1392 | &found); | |
1393 | if (found) | |
1394 | return; | |
1395 | const bool always_true = (code == EQ_EXPR || code == LE_EXPR | |
1396 | || code == GE_EXPR || code == UNLE_EXPR | |
1397 | || code == UNGE_EXPR || code == UNEQ_EXPR); | |
1398 | if (always_true) | |
1399 | warning_at (loc, OPT_Wtautological_compare, | |
1400 | "self-comparison always evaluates to true"); | |
1401 | else | |
1402 | warning_at (loc, OPT_Wtautological_compare, | |
1403 | "self-comparison always evaluates to false"); | |
1404 | } | |
1405 | } | |
1406 | ||
32dc1512 | 1407 | /* Warn about logical not used on the left hand side operand of a comparison. |
1408 | This function assumes that the LHS is inside of TRUTH_NOT_EXPR. | |
dc6229e8 | 1409 | Do not warn if RHS is of a boolean type. */ |
32dc1512 | 1410 | |
1411 | void | |
1412 | warn_logical_not_parentheses (location_t location, enum tree_code code, | |
dc6229e8 | 1413 | tree rhs) |
32dc1512 | 1414 | { |
dc6229e8 | 1415 | if (TREE_CODE_CLASS (code) != tcc_comparison |
1416 | || TREE_TYPE (rhs) == NULL_TREE | |
1417 | || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE) | |
32dc1512 | 1418 | return; |
1419 | ||
16f958b3 | 1420 | /* Don't warn for !x == 0 or !y != 0, those are equivalent to |
1421 | !(x == 0) or !(y != 0). */ | |
1422 | if ((code == EQ_EXPR || code == NE_EXPR) | |
1423 | && integer_zerop (rhs)) | |
1424 | return; | |
1425 | ||
32dc1512 | 1426 | warning_at (location, OPT_Wlogical_not_parentheses, |
1427 | "logical not is only applied to the left hand side of " | |
1428 | "comparison"); | |
1429 | } | |
b13d1547 | 1430 | |
3d177e8c | 1431 | /* Warn if EXP contains any computations whose results are not used. |
1432 | Return true if a warning is printed; false otherwise. LOCUS is the | |
1433 | (potential) location of the expression. */ | |
1434 | ||
1435 | bool | |
1436 | warn_if_unused_value (const_tree exp, location_t locus) | |
1437 | { | |
1438 | restart: | |
1439 | if (TREE_USED (exp) || TREE_NO_WARNING (exp)) | |
1440 | return false; | |
1441 | ||
1442 | /* Don't warn about void constructs. This includes casting to void, | |
1443 | void function calls, and statement expressions with a final cast | |
1444 | to void. */ | |
1445 | if (VOID_TYPE_P (TREE_TYPE (exp))) | |
1446 | return false; | |
1447 | ||
1448 | if (EXPR_HAS_LOCATION (exp)) | |
1449 | locus = EXPR_LOCATION (exp); | |
1450 | ||
1451 | switch (TREE_CODE (exp)) | |
1452 | { | |
1453 | case PREINCREMENT_EXPR: | |
1454 | case POSTINCREMENT_EXPR: | |
1455 | case PREDECREMENT_EXPR: | |
1456 | case POSTDECREMENT_EXPR: | |
1457 | case MODIFY_EXPR: | |
1458 | case INIT_EXPR: | |
1459 | case TARGET_EXPR: | |
1460 | case CALL_EXPR: | |
1461 | case TRY_CATCH_EXPR: | |
1462 | case WITH_CLEANUP_EXPR: | |
1463 | case EXIT_EXPR: | |
1464 | case VA_ARG_EXPR: | |
1465 | return false; | |
1466 | ||
1467 | case BIND_EXPR: | |
1468 | /* For a binding, warn if no side effect within it. */ | |
1469 | exp = BIND_EXPR_BODY (exp); | |
1470 | goto restart; | |
1471 | ||
1472 | case SAVE_EXPR: | |
1473 | case NON_LVALUE_EXPR: | |
d85dbdb3 | 1474 | case NOP_EXPR: |
3d177e8c | 1475 | exp = TREE_OPERAND (exp, 0); |
1476 | goto restart; | |
1477 | ||
1478 | case TRUTH_ORIF_EXPR: | |
1479 | case TRUTH_ANDIF_EXPR: | |
1480 | /* In && or ||, warn if 2nd operand has no side effect. */ | |
1481 | exp = TREE_OPERAND (exp, 1); | |
1482 | goto restart; | |
1483 | ||
1484 | case COMPOUND_EXPR: | |
1485 | if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) | |
1486 | return true; | |
1487 | /* Let people do `(foo (), 0)' without a warning. */ | |
1488 | if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) | |
1489 | return false; | |
1490 | exp = TREE_OPERAND (exp, 1); | |
1491 | goto restart; | |
1492 | ||
1493 | case COND_EXPR: | |
1494 | /* If this is an expression with side effects, don't warn; this | |
1495 | case commonly appears in macro expansions. */ | |
1496 | if (TREE_SIDE_EFFECTS (exp)) | |
1497 | return false; | |
1498 | goto warn; | |
1499 | ||
1500 | case INDIRECT_REF: | |
1501 | /* Don't warn about automatic dereferencing of references, since | |
1502 | the user cannot control it. */ | |
1503 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE) | |
1504 | { | |
1505 | exp = TREE_OPERAND (exp, 0); | |
1506 | goto restart; | |
1507 | } | |
1508 | /* Fall through. */ | |
1509 | ||
1510 | default: | |
1511 | /* Referencing a volatile value is a side effect, so don't warn. */ | |
1512 | if ((DECL_P (exp) || REFERENCE_CLASS_P (exp)) | |
1513 | && TREE_THIS_VOLATILE (exp)) | |
1514 | return false; | |
1515 | ||
1516 | /* If this is an expression which has no operands, there is no value | |
1517 | to be unused. There are no such language-independent codes, | |
1518 | but front ends may define such. */ | |
1519 | if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0) | |
1520 | return false; | |
1521 | ||
1522 | warn: | |
1523 | return warning_at (locus, OPT_Wunused_value, "value computed is not used"); | |
1524 | } | |
1525 | } | |
1526 | ||
1527 | ||
bcf22371 | 1528 | /* Print a warning about casts that might indicate violation |
1529 | of strict aliasing rules if -Wstrict-aliasing is used and | |
1e31ff37 | 1530 | strict aliasing mode is in effect. OTYPE is the original |
1531 | TREE_TYPE of EXPR, and TYPE the type we're casting to. */ | |
bcf22371 | 1532 | |
e6fa0ea6 | 1533 | bool |
1e31ff37 | 1534 | strict_aliasing_warning (tree otype, tree type, tree expr) |
bcf22371 | 1535 | { |
45bb3afb | 1536 | /* Strip pointer conversion chains and get to the correct original type. */ |
1537 | STRIP_NOPS (expr); | |
1538 | otype = TREE_TYPE (expr); | |
1539 | ||
f06537f2 | 1540 | if (!(flag_strict_aliasing |
1541 | && POINTER_TYPE_P (type) | |
1542 | && POINTER_TYPE_P (otype) | |
1543 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1544 | /* If the type we are casting to is a ref-all pointer | |
1545 | dereferencing it is always valid. */ | |
1546 | || TYPE_REF_CAN_ALIAS_ALL (type)) | |
e6fa0ea6 | 1547 | return false; |
1548 | ||
1549 | if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR | |
bcf22371 | 1550 | && (DECL_P (TREE_OPERAND (expr, 0)) |
e6fa0ea6 | 1551 | || handled_component_p (TREE_OPERAND (expr, 0)))) |
bcf22371 | 1552 | { |
1553 | /* Casting the address of an object to non void pointer. Warn | |
1554 | if the cast breaks type based aliasing. */ | |
e6fa0ea6 | 1555 | if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) |
1556 | { | |
1557 | warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " | |
1558 | "might break strict-aliasing rules"); | |
1559 | return true; | |
1560 | } | |
bcf22371 | 1561 | else |
1562 | { | |
48e1416a | 1563 | /* warn_strict_aliasing >= 3. This includes the default (3). |
e6fa0ea6 | 1564 | Only warn if the cast is dereferenced immediately. */ |
32c2fdea | 1565 | alias_set_type set1 = |
e6fa0ea6 | 1566 | get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); |
32c2fdea | 1567 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); |
bcf22371 | 1568 | |
62d823d0 | 1569 | if (set1 != set2 && set2 != 0 |
1570 | && (set1 == 0 || !alias_sets_conflict_p (set1, set2))) | |
e6fa0ea6 | 1571 | { |
1572 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1573 | "pointer will break strict-aliasing rules"); | |
1574 | return true; | |
1575 | } | |
1576 | else if (warn_strict_aliasing == 2 | |
879f881c | 1577 | && !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1578 | { |
1579 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1580 | "pointer might break strict-aliasing rules"); | |
1581 | return true; | |
1582 | } | |
bcf22371 | 1583 | } |
1584 | } | |
e6fa0ea6 | 1585 | else |
1586 | if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) | |
1587 | { | |
1588 | /* At this level, warn for any conversions, even if an address is | |
1589 | not taken in the same statement. This will likely produce many | |
1590 | false positives, but could be useful to pinpoint problems that | |
1591 | are not revealed at higher levels. */ | |
32c2fdea | 1592 | alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); |
1593 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); | |
1594 | if (!COMPLETE_TYPE_P (type) | |
879f881c | 1595 | || !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1596 | { |
1597 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1598 | "pointer might break strict-aliasing rules"); | |
1599 | return true; | |
1600 | } | |
1601 | } | |
1602 | ||
1603 | return false; | |
bcf22371 | 1604 | } |
1605 | ||
f003f5dc | 1606 | /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with |
1607 | sizeof as last operand of certain builtins. */ | |
1608 | ||
1609 | void | |
57f872a2 | 1610 | sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee, |
f1f41a6c | 1611 | vec<tree, va_gc> *params, tree *sizeof_arg, |
f003f5dc | 1612 | bool (*comp_types) (tree, tree)) |
1613 | { | |
1614 | tree type, dest = NULL_TREE, src = NULL_TREE, tem; | |
57f872a2 | 1615 | bool strop = false, cmp = false; |
1616 | unsigned int idx = ~0; | |
1617 | location_t loc; | |
f003f5dc | 1618 | |
1619 | if (TREE_CODE (callee) != FUNCTION_DECL | |
1620 | || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL | |
f1f41a6c | 1621 | || vec_safe_length (params) <= 1) |
f003f5dc | 1622 | return; |
1623 | ||
f003f5dc | 1624 | switch (DECL_FUNCTION_CODE (callee)) |
1625 | { | |
1626 | case BUILT_IN_STRNCMP: | |
1627 | case BUILT_IN_STRNCASECMP: | |
57f872a2 | 1628 | cmp = true; |
1629 | /* FALLTHRU */ | |
f003f5dc | 1630 | case BUILT_IN_STRNCPY: |
57f872a2 | 1631 | case BUILT_IN_STRNCPY_CHK: |
f003f5dc | 1632 | case BUILT_IN_STRNCAT: |
57f872a2 | 1633 | case BUILT_IN_STRNCAT_CHK: |
1634 | case BUILT_IN_STPNCPY: | |
1635 | case BUILT_IN_STPNCPY_CHK: | |
f003f5dc | 1636 | strop = true; |
1637 | /* FALLTHRU */ | |
1638 | case BUILT_IN_MEMCPY: | |
57f872a2 | 1639 | case BUILT_IN_MEMCPY_CHK: |
f003f5dc | 1640 | case BUILT_IN_MEMMOVE: |
57f872a2 | 1641 | case BUILT_IN_MEMMOVE_CHK: |
f1f41a6c | 1642 | if (params->length () < 3) |
57f872a2 | 1643 | return; |
f1f41a6c | 1644 | src = (*params)[1]; |
1645 | dest = (*params)[0]; | |
57f872a2 | 1646 | idx = 2; |
1647 | break; | |
1648 | case BUILT_IN_BCOPY: | |
f1f41a6c | 1649 | if (params->length () < 3) |
57f872a2 | 1650 | return; |
f1f41a6c | 1651 | src = (*params)[0]; |
1652 | dest = (*params)[1]; | |
57f872a2 | 1653 | idx = 2; |
1654 | break; | |
f003f5dc | 1655 | case BUILT_IN_MEMCMP: |
57f872a2 | 1656 | case BUILT_IN_BCMP: |
f1f41a6c | 1657 | if (params->length () < 3) |
f003f5dc | 1658 | return; |
f1f41a6c | 1659 | src = (*params)[1]; |
1660 | dest = (*params)[0]; | |
57f872a2 | 1661 | idx = 2; |
1662 | cmp = true; | |
f003f5dc | 1663 | break; |
1664 | case BUILT_IN_MEMSET: | |
57f872a2 | 1665 | case BUILT_IN_MEMSET_CHK: |
f1f41a6c | 1666 | if (params->length () < 3) |
f003f5dc | 1667 | return; |
f1f41a6c | 1668 | dest = (*params)[0]; |
57f872a2 | 1669 | idx = 2; |
1670 | break; | |
1671 | case BUILT_IN_BZERO: | |
f1f41a6c | 1672 | dest = (*params)[0]; |
57f872a2 | 1673 | idx = 1; |
f003f5dc | 1674 | break; |
1675 | case BUILT_IN_STRNDUP: | |
f1f41a6c | 1676 | src = (*params)[0]; |
f003f5dc | 1677 | strop = true; |
57f872a2 | 1678 | idx = 1; |
1679 | break; | |
1680 | case BUILT_IN_MEMCHR: | |
f1f41a6c | 1681 | if (params->length () < 3) |
57f872a2 | 1682 | return; |
f1f41a6c | 1683 | src = (*params)[0]; |
57f872a2 | 1684 | idx = 2; |
1685 | break; | |
1686 | case BUILT_IN_SNPRINTF: | |
1687 | case BUILT_IN_SNPRINTF_CHK: | |
1688 | case BUILT_IN_VSNPRINTF: | |
1689 | case BUILT_IN_VSNPRINTF_CHK: | |
f1f41a6c | 1690 | dest = (*params)[0]; |
57f872a2 | 1691 | idx = 1; |
1692 | strop = true; | |
f003f5dc | 1693 | break; |
1694 | default: | |
1695 | break; | |
1696 | } | |
1697 | ||
57f872a2 | 1698 | if (idx >= 3) |
1699 | return; | |
1700 | ||
1701 | if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node) | |
1702 | return; | |
1703 | ||
1704 | type = TYPE_P (sizeof_arg[idx]) | |
1705 | ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]); | |
1706 | if (!POINTER_TYPE_P (type)) | |
1707 | return; | |
1708 | ||
f003f5dc | 1709 | if (dest |
1710 | && (tem = tree_strip_nop_conversions (dest)) | |
1711 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
1712 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
1713 | return; | |
1714 | ||
1715 | if (src | |
1716 | && (tem = tree_strip_nop_conversions (src)) | |
1717 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
1718 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
1719 | return; | |
1720 | ||
57f872a2 | 1721 | loc = sizeof_arg_loc[idx]; |
1722 | ||
1723 | if (dest && !cmp) | |
f003f5dc | 1724 | { |
57f872a2 | 1725 | if (!TYPE_P (sizeof_arg[idx]) |
1726 | && operand_equal_p (dest, sizeof_arg[idx], 0) | |
f003f5dc | 1727 | && comp_types (TREE_TYPE (dest), type)) |
1728 | { | |
57f872a2 | 1729 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
f003f5dc | 1730 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1731 | "argument to %<sizeof%> in %qD call is the same " | |
1732 | "expression as the destination; did you mean to " | |
1733 | "remove the addressof?", callee); | |
1734 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
1735 | == TYPE_PRECISION (char_type_node)) | |
1736 | || strop) | |
1737 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1738 | "argument to %<sizeof%> in %qD call is the same " | |
1739 | "expression as the destination; did you mean to " | |
1740 | "provide an explicit length?", callee); | |
1741 | else | |
1742 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1743 | "argument to %<sizeof%> in %qD call is the same " | |
1744 | "expression as the destination; did you mean to " | |
1745 | "dereference it?", callee); | |
1746 | return; | |
1747 | } | |
1748 | ||
1749 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
1750 | && !strop | |
1751 | && comp_types (TREE_TYPE (dest), type) | |
1752 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1753 | { | |
1754 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1755 | "argument to %<sizeof%> in %qD call is the same " | |
1756 | "pointer type %qT as the destination; expected %qT " | |
1757 | "or an explicit length", callee, TREE_TYPE (dest), | |
1758 | TREE_TYPE (TREE_TYPE (dest))); | |
1759 | return; | |
1760 | } | |
1761 | } | |
1762 | ||
57f872a2 | 1763 | if (src && !cmp) |
f003f5dc | 1764 | { |
57f872a2 | 1765 | if (!TYPE_P (sizeof_arg[idx]) |
1766 | && operand_equal_p (src, sizeof_arg[idx], 0) | |
f003f5dc | 1767 | && comp_types (TREE_TYPE (src), type)) |
1768 | { | |
57f872a2 | 1769 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
f003f5dc | 1770 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
1771 | "argument to %<sizeof%> in %qD call is the same " | |
1772 | "expression as the source; did you mean to " | |
1773 | "remove the addressof?", callee); | |
1774 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
1775 | == TYPE_PRECISION (char_type_node)) | |
1776 | || strop) | |
1777 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1778 | "argument to %<sizeof%> in %qD call is the same " | |
1779 | "expression as the source; did you mean to " | |
1780 | "provide an explicit length?", callee); | |
1781 | else | |
1782 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1783 | "argument to %<sizeof%> in %qD call is the same " | |
1784 | "expression as the source; did you mean to " | |
1785 | "dereference it?", callee); | |
1786 | return; | |
1787 | } | |
1788 | ||
1789 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
1790 | && !strop | |
1791 | && comp_types (TREE_TYPE (src), type) | |
1792 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1793 | { | |
1794 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1795 | "argument to %<sizeof%> in %qD call is the same " | |
1796 | "pointer type %qT as the source; expected %qT " | |
1797 | "or an explicit length", callee, TREE_TYPE (src), | |
1798 | TREE_TYPE (TREE_TYPE (src))); | |
1799 | return; | |
1800 | } | |
1801 | } | |
57f872a2 | 1802 | |
1803 | if (dest) | |
1804 | { | |
1805 | if (!TYPE_P (sizeof_arg[idx]) | |
1806 | && operand_equal_p (dest, sizeof_arg[idx], 0) | |
1807 | && comp_types (TREE_TYPE (dest), type)) | |
1808 | { | |
1809 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
1810 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1811 | "argument to %<sizeof%> in %qD call is the same " | |
1812 | "expression as the first source; did you mean to " | |
1813 | "remove the addressof?", callee); | |
1814 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
1815 | == TYPE_PRECISION (char_type_node)) | |
1816 | || strop) | |
1817 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1818 | "argument to %<sizeof%> in %qD call is the same " | |
1819 | "expression as the first source; did you mean to " | |
1820 | "provide an explicit length?", callee); | |
1821 | else | |
1822 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1823 | "argument to %<sizeof%> in %qD call is the same " | |
1824 | "expression as the first source; did you mean to " | |
1825 | "dereference it?", callee); | |
1826 | return; | |
1827 | } | |
1828 | ||
1829 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
1830 | && !strop | |
1831 | && comp_types (TREE_TYPE (dest), type) | |
1832 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1833 | { | |
1834 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1835 | "argument to %<sizeof%> in %qD call is the same " | |
1836 | "pointer type %qT as the first source; expected %qT " | |
1837 | "or an explicit length", callee, TREE_TYPE (dest), | |
1838 | TREE_TYPE (TREE_TYPE (dest))); | |
1839 | return; | |
1840 | } | |
1841 | } | |
1842 | ||
1843 | if (src) | |
1844 | { | |
1845 | if (!TYPE_P (sizeof_arg[idx]) | |
1846 | && operand_equal_p (src, sizeof_arg[idx], 0) | |
1847 | && comp_types (TREE_TYPE (src), type)) | |
1848 | { | |
1849 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
1850 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1851 | "argument to %<sizeof%> in %qD call is the same " | |
1852 | "expression as the second source; did you mean to " | |
1853 | "remove the addressof?", callee); | |
1854 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
1855 | == TYPE_PRECISION (char_type_node)) | |
1856 | || strop) | |
1857 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1858 | "argument to %<sizeof%> in %qD call is the same " | |
1859 | "expression as the second source; did you mean to " | |
1860 | "provide an explicit length?", callee); | |
1861 | else | |
1862 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1863 | "argument to %<sizeof%> in %qD call is the same " | |
1864 | "expression as the second source; did you mean to " | |
1865 | "dereference it?", callee); | |
1866 | return; | |
1867 | } | |
1868 | ||
1869 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
1870 | && !strop | |
1871 | && comp_types (TREE_TYPE (src), type) | |
1872 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1873 | { | |
1874 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1875 | "argument to %<sizeof%> in %qD call is the same " | |
1876 | "pointer type %qT as the second source; expected %qT " | |
1877 | "or an explicit length", callee, TREE_TYPE (src), | |
1878 | TREE_TYPE (TREE_TYPE (src))); | |
1879 | return; | |
1880 | } | |
1881 | } | |
1882 | ||
f003f5dc | 1883 | } |
1884 | ||
3f08e399 | 1885 | /* Warn for unlikely, improbable, or stupid DECL declarations |
1886 | of `main'. */ | |
1887 | ||
1888 | void | |
1889 | check_main_parameter_types (tree decl) | |
1890 | { | |
d0af78c5 | 1891 | function_args_iterator iter; |
1892 | tree type; | |
3f08e399 | 1893 | int argct = 0; |
1894 | ||
d0af78c5 | 1895 | FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) |
1896 | { | |
1897 | /* XXX void_type_node belies the abstraction. */ | |
1898 | if (type == void_type_node || type == error_mark_node ) | |
1899 | break; | |
1900 | ||
2026249a | 1901 | tree t = type; |
1902 | if (TYPE_ATOMIC (t)) | |
1903 | pedwarn (input_location, OPT_Wmain, | |
1904 | "%<_Atomic%>-qualified parameter type %qT of %q+D", | |
1905 | type, decl); | |
1906 | while (POINTER_TYPE_P (t)) | |
1907 | { | |
1908 | t = TREE_TYPE (t); | |
1909 | if (TYPE_ATOMIC (t)) | |
1910 | pedwarn (input_location, OPT_Wmain, | |
1911 | "%<_Atomic%>-qualified parameter type %qT of %q+D", | |
1912 | type, decl); | |
1913 | } | |
1914 | ||
d0af78c5 | 1915 | ++argct; |
1916 | switch (argct) | |
1917 | { | |
1918 | case 1: | |
1919 | if (TYPE_MAIN_VARIANT (type) != integer_type_node) | |
1920 | pedwarn (input_location, OPT_Wmain, | |
1921 | "first argument of %q+D should be %<int%>", decl); | |
1922 | break; | |
1923 | ||
1924 | case 2: | |
1925 | if (TREE_CODE (type) != POINTER_TYPE | |
1926 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
1927 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
1928 | != char_type_node)) | |
1929 | pedwarn (input_location, OPT_Wmain, | |
1930 | "second argument of %q+D should be %<char **%>", decl); | |
1931 | break; | |
1932 | ||
1933 | case 3: | |
1934 | if (TREE_CODE (type) != POINTER_TYPE | |
1935 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
1936 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
1937 | != char_type_node)) | |
1938 | pedwarn (input_location, OPT_Wmain, | |
1939 | "third argument of %q+D should probably be " | |
1940 | "%<char **%>", decl); | |
1941 | break; | |
1942 | } | |
1943 | } | |
3f08e399 | 1944 | |
1945 | /* It is intentional that this message does not mention the third | |
1946 | argument because it's only mentioned in an appendix of the | |
1947 | standard. */ | |
1948 | if (argct > 0 && (argct < 2 || argct > 3)) | |
d0af78c5 | 1949 | pedwarn (input_location, OPT_Wmain, |
1950 | "%q+D takes only zero or two arguments", decl); | |
90e645fa | 1951 | |
1952 | if (stdarg_p (TREE_TYPE (decl))) | |
1953 | pedwarn (input_location, OPT_Wmain, | |
1954 | "%q+D declared as variadic function", decl); | |
3f08e399 | 1955 | } |
1956 | ||
73437615 | 1957 | /* vector_targets_convertible_p is used for vector pointer types. The |
1958 | callers perform various checks that the qualifiers are satisfactory, | |
1959 | while OTOH vector_targets_convertible_p ignores the number of elements | |
1960 | in the vectors. That's fine with vector pointers as we can consider, | |
1961 | say, a vector of 8 elements as two consecutive vectors of 4 elements, | |
1962 | and that does not require and conversion of the pointer values. | |
1963 | In contrast, vector_types_convertible_p and | |
1964 | vector_types_compatible_elements_p are used for vector value types. */ | |
ed7c4e62 | 1965 | /* True if pointers to distinct types T1 and T2 can be converted to |
1966 | each other without an explicit cast. Only returns true for opaque | |
1967 | vector types. */ | |
1968 | bool | |
1969 | vector_targets_convertible_p (const_tree t1, const_tree t2) | |
1970 | { | |
6290f0db | 1971 | if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2) |
8d125f7d | 1972 | && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
ed7c4e62 | 1973 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
1974 | return true; | |
1975 | ||
1976 | return false; | |
1977 | } | |
1978 | ||
73437615 | 1979 | /* vector_types_convertible_p is used for vector value types. |
1980 | It could in principle call vector_targets_convertible_p as a subroutine, | |
1981 | but then the check for vector type would be duplicated with its callers, | |
1982 | and also the purpose of vector_targets_convertible_p would become | |
1983 | muddled. | |
1984 | Where vector_types_convertible_p returns true, a conversion might still be | |
1985 | needed to make the types match. | |
1986 | In contrast, vector_targets_convertible_p is used for vector pointer | |
1987 | values, and vector_types_compatible_elements_p is used specifically | |
1988 | in the context for binary operators, as a check if use is possible without | |
1989 | conversion. */ | |
546c4794 | 1990 | /* True if vector types T1 and T2 can be converted to each other |
1991 | without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2 | |
1992 | can only be converted with -flax-vector-conversions yet that is not | |
1993 | in effect, emit a note telling the user about that option if such | |
1994 | a note has not previously been emitted. */ | |
1995 | bool | |
9f627b1a | 1996 | vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note) |
8b4b9810 | 1997 | { |
546c4794 | 1998 | static bool emitted_lax_note = false; |
ae6db8ab | 1999 | bool convertible_lax; |
2000 | ||
8d125f7d | 2001 | if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
ae6db8ab | 2002 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
2003 | return true; | |
2004 | ||
2005 | convertible_lax = | |
2006 | (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) | |
2007 | && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || | |
432dd330 | 2008 | TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)) |
ae6db8ab | 2009 | && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) |
2010 | == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); | |
546c4794 | 2011 | |
2012 | if (!convertible_lax || flag_lax_vector_conversions) | |
2013 | return convertible_lax; | |
2014 | ||
2015 | if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) | |
29f51994 | 2016 | && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
546c4794 | 2017 | return true; |
2018 | ||
2019 | if (emit_lax_note && !emitted_lax_note) | |
2020 | { | |
2021 | emitted_lax_note = true; | |
5bcc316e | 2022 | inform (input_location, "use -flax-vector-conversions to permit " |
546c4794 | 2023 | "conversions between vectors with differing " |
2024 | "element types or numbers of subparts"); | |
2025 | } | |
2026 | ||
2027 | return false; | |
8b4b9810 | 2028 | } |
2029 | ||
bf0cb017 | 2030 | /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes |
2031 | and have vector types, V0 has the same type as V1, and the number of | |
2032 | elements of V0, V1, MASK is the same. | |
2033 | ||
2034 | In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was | |
2035 | called with two arguments. In this case implementation passes the | |
2036 | first argument twice in order to share the same tree code. This fact | |
2037 | could enable the mask-values being twice the vector length. This is | |
2038 | an implementation accident and this semantics is not guaranteed to | |
2039 | the user. */ | |
2040 | tree | |
68ea4406 | 2041 | c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask, |
2042 | bool complain) | |
bf0cb017 | 2043 | { |
2044 | tree ret; | |
2045 | bool wrap = true; | |
2046 | bool maybe_const = false; | |
2047 | bool two_arguments = false; | |
2048 | ||
2049 | if (v1 == NULL_TREE) | |
2050 | { | |
2051 | two_arguments = true; | |
2052 | v1 = v0; | |
2053 | } | |
2054 | ||
2055 | if (v0 == error_mark_node || v1 == error_mark_node | |
2056 | || mask == error_mark_node) | |
2057 | return error_mark_node; | |
2058 | ||
6290f0db | 2059 | if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask))) |
bf0cb017 | 2060 | { |
68ea4406 | 2061 | if (complain) |
2062 | error_at (loc, "__builtin_shuffle last argument must " | |
2063 | "be an integer vector"); | |
bf0cb017 | 2064 | return error_mark_node; |
2065 | } | |
2066 | ||
6290f0db | 2067 | if (!VECTOR_TYPE_P (TREE_TYPE (v0)) |
2068 | || !VECTOR_TYPE_P (TREE_TYPE (v1))) | |
bf0cb017 | 2069 | { |
68ea4406 | 2070 | if (complain) |
2071 | error_at (loc, "__builtin_shuffle arguments must be vectors"); | |
bf0cb017 | 2072 | return error_mark_node; |
2073 | } | |
2074 | ||
2075 | if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1))) | |
2076 | { | |
68ea4406 | 2077 | if (complain) |
2078 | error_at (loc, "__builtin_shuffle argument vectors must be of " | |
2079 | "the same type"); | |
bf0cb017 | 2080 | return error_mark_node; |
2081 | } | |
2082 | ||
2083 | if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)) | |
2084 | != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)) | |
2085 | && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)) | |
2086 | != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))) | |
2087 | { | |
68ea4406 | 2088 | if (complain) |
2089 | error_at (loc, "__builtin_shuffle number of elements of the " | |
2090 | "argument vector(s) and the mask vector should " | |
2091 | "be the same"); | |
bf0cb017 | 2092 | return error_mark_node; |
2093 | } | |
2094 | ||
2095 | if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0)))) | |
2096 | != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask))))) | |
2097 | { | |
68ea4406 | 2098 | if (complain) |
2099 | error_at (loc, "__builtin_shuffle argument vector(s) inner type " | |
2100 | "must have the same size as inner type of the mask"); | |
bf0cb017 | 2101 | return error_mark_node; |
2102 | } | |
2103 | ||
2104 | if (!c_dialect_cxx ()) | |
2105 | { | |
2106 | /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */ | |
2107 | v0 = c_fully_fold (v0, false, &maybe_const); | |
2108 | wrap &= maybe_const; | |
2109 | ||
2110 | if (two_arguments) | |
2111 | v1 = v0 = save_expr (v0); | |
2112 | else | |
2113 | { | |
2114 | v1 = c_fully_fold (v1, false, &maybe_const); | |
2115 | wrap &= maybe_const; | |
2116 | } | |
2117 | ||
2118 | mask = c_fully_fold (mask, false, &maybe_const); | |
2119 | wrap &= maybe_const; | |
2120 | } | |
68ea4406 | 2121 | else if (two_arguments) |
2122 | v1 = v0 = save_expr (v0); | |
bf0cb017 | 2123 | |
2124 | ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask); | |
2125 | ||
2126 | if (!c_dialect_cxx () && !wrap) | |
2127 | ret = c_wrap_maybe_const (ret, true); | |
2128 | ||
2129 | return ret; | |
2130 | } | |
2131 | ||
7f506bca | 2132 | /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum |
2133 | to integral type. */ | |
2134 | ||
2135 | static tree | |
2136 | c_common_get_narrower (tree op, int *unsignedp_ptr) | |
2137 | { | |
2138 | op = get_narrower (op, unsignedp_ptr); | |
2139 | ||
2140 | if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE | |
2141 | && ENUM_IS_SCOPED (TREE_TYPE (op))) | |
2142 | { | |
2143 | /* C++0x scoped enumerations don't implicitly convert to integral | |
2144 | type; if we stripped an explicit conversion to a larger type we | |
2145 | need to replace it so common_type will still work. */ | |
a51edb4c | 2146 | tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)), |
2147 | TYPE_UNSIGNED (TREE_TYPE (op))); | |
7f506bca | 2148 | op = fold_convert (type, op); |
2149 | } | |
2150 | return op; | |
2151 | } | |
2152 | ||
2561cea2 | 2153 | /* This is a helper function of build_binary_op. |
2154 | ||
2155 | For certain operations if both args were extended from the same | |
2156 | smaller type, do the arithmetic in that type and then extend. | |
2157 | ||
2158 | BITWISE indicates a bitwise operation. | |
2159 | For them, this optimization is safe only if | |
2160 | both args are zero-extended or both are sign-extended. | |
2161 | Otherwise, we might change the result. | |
2162 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
48e1416a | 2163 | but calculated in (unsigned short) it would be (unsigned short)-1. |
2561cea2 | 2164 | */ |
7f506bca | 2165 | tree |
2166 | shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise) | |
2561cea2 | 2167 | { |
2168 | int unsigned0, unsigned1; | |
2169 | tree arg0, arg1; | |
2170 | int uns; | |
2171 | tree type; | |
2172 | ||
2173 | /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents | |
2174 | excessive narrowing when we call get_narrower below. For | |
2175 | example, suppose that OP0 is of unsigned int extended | |
2176 | from signed char and that RESULT_TYPE is long long int. | |
2177 | If we explicitly cast OP0 to RESULT_TYPE, OP0 would look | |
2178 | like | |
48e1416a | 2179 | |
2561cea2 | 2180 | (long long int) (unsigned int) signed_char |
2181 | ||
2182 | which get_narrower would narrow down to | |
48e1416a | 2183 | |
2561cea2 | 2184 | (unsigned int) signed char |
48e1416a | 2185 | |
2561cea2 | 2186 | If we do not cast OP0 first, get_narrower would return |
2187 | signed_char, which is inconsistent with the case of the | |
2188 | explicit cast. */ | |
2189 | op0 = convert (result_type, op0); | |
2190 | op1 = convert (result_type, op1); | |
2191 | ||
7f506bca | 2192 | arg0 = c_common_get_narrower (op0, &unsigned0); |
2193 | arg1 = c_common_get_narrower (op1, &unsigned1); | |
ab2c1de8 | 2194 | |
2561cea2 | 2195 | /* UNS is 1 if the operation to be done is an unsigned one. */ |
2196 | uns = TYPE_UNSIGNED (result_type); | |
2197 | ||
2198 | /* Handle the case that OP0 (or OP1) does not *contain* a conversion | |
2199 | but it *requires* conversion to FINAL_TYPE. */ | |
48e1416a | 2200 | |
2561cea2 | 2201 | if ((TYPE_PRECISION (TREE_TYPE (op0)) |
2202 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2203 | && TREE_TYPE (op0) != result_type) | |
2204 | unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); | |
2205 | if ((TYPE_PRECISION (TREE_TYPE (op1)) | |
2206 | == TYPE_PRECISION (TREE_TYPE (arg1))) | |
2207 | && TREE_TYPE (op1) != result_type) | |
2208 | unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
48e1416a | 2209 | |
2561cea2 | 2210 | /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ |
48e1416a | 2211 | |
2561cea2 | 2212 | /* For bitwise operations, signedness of nominal type |
2213 | does not matter. Consider only how operands were extended. */ | |
2214 | if (bitwise) | |
2215 | uns = unsigned0; | |
48e1416a | 2216 | |
2561cea2 | 2217 | /* Note that in all three cases below we refrain from optimizing |
2218 | an unsigned operation on sign-extended args. | |
2219 | That would not be valid. */ | |
48e1416a | 2220 | |
2561cea2 | 2221 | /* Both args variable: if both extended in same way |
2222 | from same width, do it in that width. | |
2223 | Do it unsigned if args were zero-extended. */ | |
2224 | if ((TYPE_PRECISION (TREE_TYPE (arg0)) | |
2225 | < TYPE_PRECISION (result_type)) | |
2226 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2227 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2228 | && unsigned0 == unsigned1 | |
2229 | && (unsigned0 || !uns)) | |
2230 | return c_common_signed_or_unsigned_type | |
2231 | (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); | |
2232 | ||
2233 | else if (TREE_CODE (arg0) == INTEGER_CST | |
2234 | && (unsigned1 || !uns) | |
2235 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2236 | < TYPE_PRECISION (result_type)) | |
2237 | && (type | |
2238 | = c_common_signed_or_unsigned_type (unsigned1, | |
2239 | TREE_TYPE (arg1))) | |
2240 | && !POINTER_TYPE_P (type) | |
2241 | && int_fits_type_p (arg0, type)) | |
2242 | return type; | |
2243 | ||
2244 | else if (TREE_CODE (arg1) == INTEGER_CST | |
2245 | && (unsigned0 || !uns) | |
2246 | && (TYPE_PRECISION (TREE_TYPE (arg0)) | |
2247 | < TYPE_PRECISION (result_type)) | |
2248 | && (type | |
2249 | = c_common_signed_or_unsigned_type (unsigned0, | |
2250 | TREE_TYPE (arg0))) | |
2251 | && !POINTER_TYPE_P (type) | |
2252 | && int_fits_type_p (arg1, type)) | |
2253 | return type; | |
2254 | ||
2255 | return result_type; | |
2256 | } | |
2257 | ||
e53013a8 | 2258 | /* Returns true iff any integer value of type FROM_TYPE can be represented as |
2259 | real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */ | |
2260 | ||
2261 | static bool | |
2262 | int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type) | |
2263 | { | |
2264 | tree type_low_bound = TYPE_MIN_VALUE (from_type); | |
2265 | tree type_high_bound = TYPE_MAX_VALUE (from_type); | |
2266 | REAL_VALUE_TYPE real_low_bound = | |
2267 | real_value_from_int_cst (0, type_low_bound); | |
2268 | REAL_VALUE_TYPE real_high_bound = | |
2269 | real_value_from_int_cst (0, type_high_bound); | |
2270 | ||
2271 | return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound) | |
2272 | && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound); | |
2273 | } | |
2274 | ||
2275 | /* Checks if expression EXPR of complex/real/integer type cannot be converted | |
2276 | to the complex/real/integer type TYPE. Function returns non-zero when: | |
22a75734 | 2277 | * EXPR is a constant which cannot be exactly converted to TYPE. |
2278 | * EXPR is not a constant and size of EXPR's type > than size of TYPE, | |
e53013a8 | 2279 | for EXPR type and TYPE being both integers or both real, or both |
2280 | complex. | |
2281 | * EXPR is not a constant of complex type and TYPE is a real or | |
2282 | an integer. | |
22a75734 | 2283 | * EXPR is not a constant of real type and TYPE is an integer. |
2284 | * EXPR is not a constant of integer type which cannot be | |
2285 | exactly converted to real type. | |
e53013a8 | 2286 | |
7dfa155b | 2287 | Function allows conversions between types of different signedness and |
ca9d7d74 | 2288 | can return SAFE_CONVERSION (zero) in that case. Function can produce |
e53013a8 | 2289 | signedness warnings if PRODUCE_WARNS is true. |
2290 | ||
2291 | Function allows conversions from complex constants to non-complex types, | |
2292 | provided that imaginary part is zero and real part can be safely converted | |
2293 | to TYPE. */ | |
22a75734 | 2294 | |
ca9d7d74 | 2295 | enum conversion_safety |
22a75734 | 2296 | unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns) |
d31d55f0 | 2297 | { |
ca9d7d74 | 2298 | enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */ |
27259707 | 2299 | tree expr_type = TREE_TYPE (expr); |
22a75734 | 2300 | loc = expansion_point_location_if_in_system_header (loc); |
d31d55f0 | 2301 | |
7dfa155b | 2302 | if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST) |
5b16c152 | 2303 | { |
e53013a8 | 2304 | /* If type is complex, we are interested in compatibility with |
2305 | underlying type. */ | |
2306 | if (TREE_CODE (type) == COMPLEX_TYPE) | |
2307 | type = TREE_TYPE (type); | |
2308 | ||
d31d55f0 | 2309 | /* Warn for real constant that is not an exact integer converted |
7dfa155b | 2310 | to integer type. */ |
27259707 | 2311 | if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2312 | && TREE_CODE (type) == INTEGER_TYPE) |
2313 | { | |
2314 | if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) | |
ca9d7d74 | 2315 | give_warning = UNSAFE_REAL; |
7dfa155b | 2316 | } |
da1fb07b | 2317 | /* Warn for an integer constant that does not fit into integer type. */ |
27259707 | 2318 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2319 | && TREE_CODE (type) == INTEGER_TYPE |
2320 | && !int_fits_type_p (expr, type)) | |
2321 | { | |
2322 | if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type) | |
27259707 | 2323 | && tree_int_cst_sgn (expr) < 0) |
7dfa155b | 2324 | { |
2325 | if (produce_warns) | |
2326 | warning_at (loc, OPT_Wsign_conversion, "negative integer" | |
2327 | " implicitly converted to unsigned type"); | |
2328 | } | |
2329 | else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type)) | |
2330 | { | |
2331 | if (produce_warns) | |
2332 | warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned" | |
2333 | " constant value to negative integer"); | |
2334 | } | |
7ee0d227 | 2335 | else |
ca9d7d74 | 2336 | give_warning = UNSAFE_OTHER; |
7dfa155b | 2337 | } |
d31d55f0 | 2338 | else if (TREE_CODE (type) == REAL_TYPE) |
7dfa155b | 2339 | { |
2340 | /* Warn for an integer constant that does not fit into real type. */ | |
2341 | if (TREE_CODE (expr_type) == INTEGER_TYPE) | |
2342 | { | |
2343 | REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); | |
2344 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
ca9d7d74 | 2345 | give_warning = UNSAFE_REAL; |
7dfa155b | 2346 | } |
2347 | /* Warn for a real constant that does not fit into a smaller | |
2348 | real type. */ | |
2349 | else if (TREE_CODE (expr_type) == REAL_TYPE | |
2350 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
2351 | { | |
2352 | REAL_VALUE_TYPE a = TREE_REAL_CST (expr); | |
2353 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
ca9d7d74 | 2354 | give_warning = UNSAFE_REAL; |
7dfa155b | 2355 | } |
2356 | } | |
2357 | } | |
e53013a8 | 2358 | |
2359 | else if (TREE_CODE (expr) == COMPLEX_CST) | |
2360 | { | |
2361 | tree imag_part = TREE_IMAGPART (expr); | |
2362 | /* Conversion from complex constant with zero imaginary part, | |
2363 | perform check for conversion of real part. */ | |
2364 | if ((TREE_CODE (imag_part) == REAL_CST | |
2365 | && real_zerop (imag_part)) | |
2366 | || (TREE_CODE (imag_part) == INTEGER_CST | |
2367 | && integer_zerop (imag_part))) | |
2368 | /* Note: in this branch we use recursive call to unsafe_conversion_p | |
2369 | with different type of EXPR, but it is still safe, because when EXPR | |
2370 | is a constant, it's type is not used in text of generated warnings | |
2371 | (otherwise they could sound misleading). */ | |
2372 | return unsafe_conversion_p (loc, type, TREE_REALPART (expr), | |
2373 | produce_warns); | |
2374 | /* Conversion from complex constant with non-zero imaginary part. */ | |
2375 | else | |
2376 | { | |
2377 | /* Conversion to complex type. | |
2378 | Perform checks for both real and imaginary parts. */ | |
2379 | if (TREE_CODE (type) == COMPLEX_TYPE) | |
2380 | { | |
2381 | /* Unfortunately, produce_warns must be false in two subsequent | |
2382 | calls of unsafe_conversion_p, because otherwise we could | |
2383 | produce strange "double" warnings, if both real and imaginary | |
2384 | parts have conversion problems related to signedness. | |
2385 | ||
2386 | For example: | |
2387 | int32_t _Complex a = 0x80000000 + 0x80000000i; | |
2388 | ||
2389 | Possible solution: add a separate function for checking | |
2390 | constants and combine result of two calls appropriately. */ | |
2391 | enum conversion_safety re_safety = | |
2392 | unsafe_conversion_p (loc, type, TREE_REALPART (expr), false); | |
2393 | enum conversion_safety im_safety = | |
2394 | unsafe_conversion_p (loc, type, imag_part, false); | |
2395 | ||
2396 | /* Merge the results into appropriate single warning. */ | |
2397 | ||
2398 | /* Note: this case includes SAFE_CONVERSION, i.e. success. */ | |
2399 | if (re_safety == im_safety) | |
2400 | give_warning = re_safety; | |
2401 | else if (!re_safety && im_safety) | |
2402 | give_warning = im_safety; | |
2403 | else if (re_safety && !im_safety) | |
2404 | give_warning = re_safety; | |
2405 | else | |
2406 | give_warning = UNSAFE_OTHER; | |
2407 | } | |
2408 | /* Warn about conversion from complex to real or integer type. */ | |
2409 | else | |
2410 | give_warning = UNSAFE_IMAGINARY; | |
2411 | } | |
2412 | } | |
2413 | ||
2414 | /* Checks for remaining case: EXPR is not constant. */ | |
7dfa155b | 2415 | else |
2416 | { | |
d31d55f0 | 2417 | /* Warn for real types converted to integer types. */ |
2561cea2 | 2418 | if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2419 | && TREE_CODE (type) == INTEGER_TYPE) |
ca9d7d74 | 2420 | give_warning = UNSAFE_REAL; |
d31d55f0 | 2421 | |
2561cea2 | 2422 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2423 | && TREE_CODE (type) == INTEGER_TYPE) |
2424 | { | |
69609004 | 2425 | /* Don't warn about unsigned char y = 0xff, x = (int) y; */ |
f9d856a4 | 2426 | expr = get_unwidened (expr, 0); |
2561cea2 | 2427 | expr_type = TREE_TYPE (expr); |
69609004 | 2428 | |
2561cea2 | 2429 | /* Don't warn for short y; short x = ((int)y & 0xff); */ |
48e1416a | 2430 | if (TREE_CODE (expr) == BIT_AND_EXPR |
7dfa155b | 2431 | || TREE_CODE (expr) == BIT_IOR_EXPR |
2561cea2 | 2432 | || TREE_CODE (expr) == BIT_XOR_EXPR) |
2433 | { | |
27259707 | 2434 | /* If both args were extended from a shortest type, |
2435 | use that type if that is safe. */ | |
48e1416a | 2436 | expr_type = shorten_binary_op (expr_type, |
2437 | TREE_OPERAND (expr, 0), | |
2438 | TREE_OPERAND (expr, 1), | |
2561cea2 | 2439 | /* bitwise */1); |
2440 | ||
2561cea2 | 2441 | if (TREE_CODE (expr) == BIT_AND_EXPR) |
2442 | { | |
2443 | tree op0 = TREE_OPERAND (expr, 0); | |
2444 | tree op1 = TREE_OPERAND (expr, 1); | |
30de145b | 2445 | bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
2446 | bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
2447 | ||
2448 | /* If one of the operands is a non-negative constant | |
2449 | that fits in the target type, then the type of the | |
2450 | other operand does not matter. */ | |
2561cea2 | 2451 | if ((TREE_CODE (op0) == INTEGER_CST |
2452 | && int_fits_type_p (op0, c_common_signed_type (type)) | |
2453 | && int_fits_type_p (op0, c_common_unsigned_type (type))) | |
2454 | || (TREE_CODE (op1) == INTEGER_CST | |
27259707 | 2455 | && int_fits_type_p (op1, c_common_signed_type (type)) |
48e1416a | 2456 | && int_fits_type_p (op1, |
27259707 | 2457 | c_common_unsigned_type (type)))) |
ca9d7d74 | 2458 | return SAFE_CONVERSION; |
30de145b | 2459 | /* If constant is unsigned and fits in the target |
2460 | type, then the result will also fit. */ | |
2461 | else if ((TREE_CODE (op0) == INTEGER_CST | |
48e1416a | 2462 | && unsigned0 |
30de145b | 2463 | && int_fits_type_p (op0, type)) |
2464 | || (TREE_CODE (op1) == INTEGER_CST | |
2465 | && unsigned1 | |
2466 | && int_fits_type_p (op1, type))) | |
ca9d7d74 | 2467 | return SAFE_CONVERSION; |
2561cea2 | 2468 | } |
2469 | } | |
7dfa155b | 2470 | /* Warn for integer types converted to smaller integer types. */ |
48e1416a | 2471 | if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
ca9d7d74 | 2472 | give_warning = UNSAFE_OTHER; |
7ee0d227 | 2473 | |
2474 | /* When they are the same width but different signedness, | |
2475 | then the value may change. */ | |
7dfa155b | 2476 | else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type) |
2561cea2 | 2477 | && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type)) |
7ee0d227 | 2478 | /* Even when converted to a bigger type, if the type is |
2479 | unsigned but expr is signed, then negative values | |
2480 | will be changed. */ | |
7dfa155b | 2481 | || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type))) |
2482 | && produce_warns) | |
200dd99c | 2483 | warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT " |
2484 | "may change the sign of the result", | |
2485 | type, expr_type); | |
7dfa155b | 2486 | } |
d31d55f0 | 2487 | |
2488 | /* Warn for integer types converted to real types if and only if | |
7dfa155b | 2489 | all the range of values of the integer type cannot be |
2490 | represented by the real type. */ | |
2561cea2 | 2491 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2492 | && TREE_CODE (type) == REAL_TYPE) |
2493 | { | |
4c2cfa81 | 2494 | /* Don't warn about char y = 0xff; float x = (int) y; */ |
2495 | expr = get_unwidened (expr, 0); | |
2496 | expr_type = TREE_TYPE (expr); | |
2497 | ||
e53013a8 | 2498 | if (!int_safely_convertible_to_real_p (expr_type, type)) |
ca9d7d74 | 2499 | give_warning = UNSAFE_OTHER; |
7dfa155b | 2500 | } |
d31d55f0 | 2501 | |
2502 | /* Warn for real types converted to smaller real types. */ | |
2561cea2 | 2503 | else if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2504 | && TREE_CODE (type) == REAL_TYPE |
2505 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
ca9d7d74 | 2506 | give_warning = UNSAFE_REAL; |
e53013a8 | 2507 | |
2508 | /* Check conversion between two complex types. */ | |
2509 | else if (TREE_CODE (expr_type) == COMPLEX_TYPE | |
2510 | && TREE_CODE (type) == COMPLEX_TYPE) | |
2511 | { | |
2512 | /* Extract underlying types (i.e., type of real and imaginary | |
2513 | parts) of expr_type and type. */ | |
2514 | tree from_type = TREE_TYPE (expr_type); | |
2515 | tree to_type = TREE_TYPE (type); | |
2516 | ||
2517 | /* Warn for real types converted to integer types. */ | |
2518 | if (TREE_CODE (from_type) == REAL_TYPE | |
2519 | && TREE_CODE (to_type) == INTEGER_TYPE) | |
2520 | give_warning = UNSAFE_REAL; | |
2521 | ||
2522 | /* Warn for real types converted to smaller real types. */ | |
2523 | else if (TREE_CODE (from_type) == REAL_TYPE | |
2524 | && TREE_CODE (to_type) == REAL_TYPE | |
2525 | && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type)) | |
2526 | give_warning = UNSAFE_REAL; | |
2527 | ||
2528 | /* Check conversion for complex integer types. Here implementation | |
2529 | is simpler than for real-domain integers because it does not | |
2530 | involve sophisticated cases, such as bitmasks, casts, etc. */ | |
2531 | else if (TREE_CODE (from_type) == INTEGER_TYPE | |
2532 | && TREE_CODE (to_type) == INTEGER_TYPE) | |
2533 | { | |
2534 | /* Warn for integer types converted to smaller integer types. */ | |
2535 | if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type)) | |
2536 | give_warning = UNSAFE_OTHER; | |
2537 | ||
2538 | /* Check for different signedness, see case for real-domain | |
2539 | integers (above) for a more detailed comment. */ | |
2540 | else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type) | |
2541 | && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type)) | |
2542 | || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type))) | |
2543 | && produce_warns) | |
2544 | warning_at (loc, OPT_Wsign_conversion, | |
2545 | "conversion to %qT from %qT " | |
2546 | "may change the sign of the result", | |
2547 | type, expr_type); | |
2548 | } | |
2549 | else if (TREE_CODE (from_type) == INTEGER_TYPE | |
2550 | && TREE_CODE (to_type) == REAL_TYPE | |
2551 | && !int_safely_convertible_to_real_p (from_type, to_type)) | |
2552 | give_warning = UNSAFE_OTHER; | |
2553 | } | |
2554 | ||
2555 | /* Warn for complex types converted to real or integer types. */ | |
2556 | else if (TREE_CODE (expr_type) == COMPLEX_TYPE | |
2557 | && TREE_CODE (type) != COMPLEX_TYPE) | |
2558 | give_warning = UNSAFE_IMAGINARY; | |
7dfa155b | 2559 | } |
2560 | ||
2561 | return give_warning; | |
2562 | } | |
2563 | ||
2564 | /* Warns if the conversion of EXPR to TYPE may alter a value. | |
2565 | This is a helper function for warnings_for_convert_and_check. */ | |
2566 | ||
2567 | static void | |
22a75734 | 2568 | conversion_warning (location_t loc, tree type, tree expr) |
7dfa155b | 2569 | { |
7dfa155b | 2570 | tree expr_type = TREE_TYPE (expr); |
ca9d7d74 | 2571 | enum conversion_safety conversion_kind; |
d31d55f0 | 2572 | |
ca9d7d74 | 2573 | if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion) |
7dfa155b | 2574 | return; |
d31d55f0 | 2575 | |
ec704957 | 2576 | /* This may happen, because for LHS op= RHS we preevaluate |
2577 | RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which | |
2578 | means we could no longer see the code of the EXPR. */ | |
2579 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
2580 | expr = C_MAYBE_CONST_EXPR_EXPR (expr); | |
2581 | if (TREE_CODE (expr) == SAVE_EXPR) | |
2582 | expr = TREE_OPERAND (expr, 0); | |
2583 | ||
7dfa155b | 2584 | switch (TREE_CODE (expr)) |
2585 | { | |
2586 | case EQ_EXPR: | |
2587 | case NE_EXPR: | |
2588 | case LE_EXPR: | |
2589 | case GE_EXPR: | |
2590 | case LT_EXPR: | |
2591 | case GT_EXPR: | |
2592 | case TRUTH_ANDIF_EXPR: | |
2593 | case TRUTH_ORIF_EXPR: | |
2594 | case TRUTH_AND_EXPR: | |
2595 | case TRUTH_OR_EXPR: | |
2596 | case TRUTH_XOR_EXPR: | |
2597 | case TRUTH_NOT_EXPR: | |
2598 | /* Conversion from boolean to a signed:1 bit-field (which only | |
2599 | can hold the values 0 and -1) doesn't lose information - but | |
2600 | it does change the value. */ | |
2601 | if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) | |
2602 | warning_at (loc, OPT_Wconversion, | |
2603 | "conversion to %qT from boolean expression", type); | |
2604 | return; | |
2605 | ||
2606 | case REAL_CST: | |
2607 | case INTEGER_CST: | |
e53013a8 | 2608 | case COMPLEX_CST: |
22a75734 | 2609 | conversion_kind = unsafe_conversion_p (loc, type, expr, true); |
ca9d7d74 | 2610 | if (conversion_kind == UNSAFE_REAL) |
2611 | warning_at (loc, OPT_Wfloat_conversion, | |
2612 | "conversion to %qT alters %qT constant value", | |
2613 | type, expr_type); | |
2614 | else if (conversion_kind) | |
7dfa155b | 2615 | warning_at (loc, OPT_Wconversion, |
2616 | "conversion to %qT alters %qT constant value", | |
2617 | type, expr_type); | |
2618 | return; | |
2619 | ||
2620 | case COND_EXPR: | |
2621 | { | |
0e4e775a | 2622 | /* In case of COND_EXPR, we do not care about the type of |
2623 | COND_EXPR, only about the conversion of each operand. */ | |
2624 | tree op1 = TREE_OPERAND (expr, 1); | |
2625 | tree op2 = TREE_OPERAND (expr, 2); | |
2626 | ||
22a75734 | 2627 | conversion_warning (loc, type, op1); |
2628 | conversion_warning (loc, type, op2); | |
0e4e775a | 2629 | return; |
7dfa155b | 2630 | } |
2631 | ||
2632 | default: /* 'expr' is not a constant. */ | |
22a75734 | 2633 | conversion_kind = unsafe_conversion_p (loc, type, expr, true); |
ca9d7d74 | 2634 | if (conversion_kind == UNSAFE_REAL) |
2635 | warning_at (loc, OPT_Wfloat_conversion, | |
2636 | "conversion to %qT from %qT may alter its value", | |
2637 | type, expr_type); | |
e53013a8 | 2638 | else if (conversion_kind == UNSAFE_IMAGINARY) |
2639 | warning_at (loc, OPT_Wconversion, | |
2640 | "conversion to %qT from %qT discards imaginary component", | |
2641 | type, expr_type); | |
ca9d7d74 | 2642 | else if (conversion_kind) |
7dfa155b | 2643 | warning_at (loc, OPT_Wconversion, |
200dd99c | 2644 | "conversion to %qT from %qT may alter its value", |
2645 | type, expr_type); | |
d31d55f0 | 2646 | } |
2647 | } | |
2648 | ||
59dd8856 | 2649 | /* Produce warnings after a conversion. RESULT is the result of |
2650 | converting EXPR to TYPE. This is a helper function for | |
2651 | convert_and_check and cp_convert_and_check. */ | |
2a1736ed | 2652 | |
59dd8856 | 2653 | void |
22a75734 | 2654 | warnings_for_convert_and_check (location_t loc, tree type, tree expr, |
2655 | tree result) | |
2a1736ed | 2656 | { |
22a75734 | 2657 | loc = expansion_point_location_if_in_system_header (loc); |
61f69bc9 | 2658 | |
da1fb07b | 2659 | if (TREE_CODE (expr) == INTEGER_CST |
2660 | && (TREE_CODE (type) == INTEGER_TYPE | |
2661 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
2662 | && !int_fits_type_p (expr, type)) | |
2663 | { | |
d31d55f0 | 2664 | /* Do not diagnose overflow in a constant expression merely |
2665 | because a conversion overflowed. */ | |
da1fb07b | 2666 | if (TREE_OVERFLOW (result)) |
eddad94a | 2667 | TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); |
2668 | ||
da1fb07b | 2669 | if (TYPE_UNSIGNED (type)) |
d31d55f0 | 2670 | { |
da1fb07b | 2671 | /* This detects cases like converting -129 or 256 to |
2672 | unsigned char. */ | |
2673 | if (!int_fits_type_p (expr, c_common_signed_type (type))) | |
61f69bc9 | 2674 | warning_at (loc, OPT_Woverflow, |
2675 | "large integer implicitly truncated to unsigned type"); | |
7ee0d227 | 2676 | else |
22a75734 | 2677 | conversion_warning (loc, type, expr); |
da1fb07b | 2678 | } |
48e1416a | 2679 | else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) |
22a75734 | 2680 | warning_at (loc, OPT_Woverflow, |
e0913805 | 2681 | "overflow in implicit constant conversion"); |
2682 | /* No warning for converting 0x80000000 to int. */ | |
2683 | else if (pedantic | |
2684 | && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE | |
2685 | || TYPE_PRECISION (TREE_TYPE (expr)) | |
2686 | != TYPE_PRECISION (type))) | |
61f69bc9 | 2687 | warning_at (loc, OPT_Woverflow, |
2688 | "overflow in implicit constant conversion"); | |
e0913805 | 2689 | |
7ee0d227 | 2690 | else |
22a75734 | 2691 | conversion_warning (loc, type, expr); |
2a1736ed | 2692 | } |
9421ebb9 | 2693 | else if ((TREE_CODE (result) == INTEGER_CST |
2694 | || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) | |
61f69bc9 | 2695 | warning_at (loc, OPT_Woverflow, |
2696 | "overflow in implicit constant conversion"); | |
7ee0d227 | 2697 | else |
22a75734 | 2698 | conversion_warning (loc, type, expr); |
59dd8856 | 2699 | } |
2700 | ||
2701 | ||
2702 | /* Convert EXPR to TYPE, warning about conversion problems with constants. | |
2703 | Invoke this function on every expression that is converted implicitly, | |
2704 | i.e. because of language rules and not because of an explicit cast. */ | |
2705 | ||
2706 | tree | |
22a75734 | 2707 | convert_and_check (location_t loc, tree type, tree expr) |
59dd8856 | 2708 | { |
2709 | tree result; | |
c6418a4e | 2710 | tree expr_for_warning; |
2711 | ||
2712 | /* Convert from a value with possible excess precision rather than | |
2713 | via the semantic type, but do not warn about values not fitting | |
2714 | exactly in the semantic type. */ | |
2715 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
2716 | { | |
2717 | tree orig_type = TREE_TYPE (expr); | |
2718 | expr = TREE_OPERAND (expr, 0); | |
2719 | expr_for_warning = convert (orig_type, expr); | |
2720 | if (orig_type == type) | |
2721 | return expr_for_warning; | |
2722 | } | |
2723 | else | |
2724 | expr_for_warning = expr; | |
59dd8856 | 2725 | |
2726 | if (TREE_TYPE (expr) == type) | |
2727 | return expr; | |
48e1416a | 2728 | |
59dd8856 | 2729 | result = convert (type, expr); |
2730 | ||
48d94ede | 2731 | if (c_inhibit_evaluation_warnings == 0 |
2732 | && !TREE_OVERFLOW_P (expr) | |
2733 | && result != error_mark_node) | |
22a75734 | 2734 | warnings_for_convert_and_check (loc, type, expr_for_warning, result); |
59dd8856 | 2735 | |
da1fb07b | 2736 | return result; |
b2806639 | 2737 | } |
2738 | \f | |
4e91a871 | 2739 | /* A node in a list that describes references to variables (EXPR), which are |
2740 | either read accesses if WRITER is zero, or write accesses, in which case | |
2741 | WRITER is the parent of EXPR. */ | |
2742 | struct tlist | |
2743 | { | |
2744 | struct tlist *next; | |
2745 | tree expr, writer; | |
2746 | }; | |
2747 | ||
2748 | /* Used to implement a cache the results of a call to verify_tree. We only | |
2749 | use this for SAVE_EXPRs. */ | |
2750 | struct tlist_cache | |
2751 | { | |
2752 | struct tlist_cache *next; | |
2753 | struct tlist *cache_before_sp; | |
2754 | struct tlist *cache_after_sp; | |
2755 | tree expr; | |
481c6ce6 | 2756 | }; |
2757 | ||
4e91a871 | 2758 | /* Obstack to use when allocating tlist structures, and corresponding |
2759 | firstobj. */ | |
2760 | static struct obstack tlist_obstack; | |
2761 | static char *tlist_firstobj = 0; | |
2762 | ||
2763 | /* Keep track of the identifiers we've warned about, so we can avoid duplicate | |
2764 | warnings. */ | |
2765 | static struct tlist *warned_ids; | |
2766 | /* SAVE_EXPRs need special treatment. We process them only once and then | |
2767 | cache the results. */ | |
2768 | static struct tlist_cache *save_expr_cache; | |
2769 | ||
1cae46be | 2770 | static void add_tlist (struct tlist **, struct tlist *, tree, int); |
2771 | static void merge_tlist (struct tlist **, struct tlist *, int); | |
2772 | static void verify_tree (tree, struct tlist **, struct tlist **, tree); | |
2773 | static int warning_candidate_p (tree); | |
79973b57 | 2774 | static bool candidate_equal_p (const_tree, const_tree); |
1cae46be | 2775 | static void warn_for_collisions (struct tlist *); |
2776 | static void warn_for_collisions_1 (tree, tree, struct tlist *, int); | |
2777 | static struct tlist *new_tlist (struct tlist *, tree, tree); | |
481c6ce6 | 2778 | |
4e91a871 | 2779 | /* Create a new struct tlist and fill in its fields. */ |
2780 | static struct tlist * | |
1cae46be | 2781 | new_tlist (struct tlist *next, tree t, tree writer) |
4e91a871 | 2782 | { |
2783 | struct tlist *l; | |
9318f22c | 2784 | l = XOBNEW (&tlist_obstack, struct tlist); |
4e91a871 | 2785 | l->next = next; |
2786 | l->expr = t; | |
2787 | l->writer = writer; | |
2788 | return l; | |
2789 | } | |
2790 | ||
2791 | /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER | |
2792 | is nonnull, we ignore any node we find which has a writer equal to it. */ | |
2793 | ||
2794 | static void | |
1cae46be | 2795 | add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy) |
4e91a871 | 2796 | { |
2797 | while (add) | |
2798 | { | |
2799 | struct tlist *next = add->next; | |
84166705 | 2800 | if (!copy) |
4e91a871 | 2801 | add->next = *to; |
79973b57 | 2802 | if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer)) |
4e91a871 | 2803 | *to = copy ? new_tlist (*to, add->expr, add->writer) : add; |
2804 | add = next; | |
2805 | } | |
2806 | } | |
2807 | ||
2808 | /* Merge the nodes of ADD into TO. This merging process is done so that for | |
2809 | each variable that already exists in TO, no new node is added; however if | |
2810 | there is a write access recorded in ADD, and an occurrence on TO is only | |
2811 | a read access, then the occurrence in TO will be modified to record the | |
2812 | write. */ | |
481c6ce6 | 2813 | |
2814 | static void | |
1cae46be | 2815 | merge_tlist (struct tlist **to, struct tlist *add, int copy) |
4e91a871 | 2816 | { |
2817 | struct tlist **end = to; | |
2818 | ||
2819 | while (*end) | |
2820 | end = &(*end)->next; | |
2821 | ||
2822 | while (add) | |
2823 | { | |
2824 | int found = 0; | |
2825 | struct tlist *tmp2; | |
2826 | struct tlist *next = add->next; | |
2827 | ||
2828 | for (tmp2 = *to; tmp2; tmp2 = tmp2->next) | |
79973b57 | 2829 | if (candidate_equal_p (tmp2->expr, add->expr)) |
4e91a871 | 2830 | { |
2831 | found = 1; | |
84166705 | 2832 | if (!tmp2->writer) |
4e91a871 | 2833 | tmp2->writer = add->writer; |
2834 | } | |
84166705 | 2835 | if (!found) |
4e91a871 | 2836 | { |
312243bb | 2837 | *end = copy ? new_tlist (NULL, add->expr, add->writer) : add; |
4e91a871 | 2838 | end = &(*end)->next; |
2839 | *end = 0; | |
2840 | } | |
2841 | add = next; | |
2842 | } | |
2843 | } | |
2844 | ||
2845 | /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable | |
2846 | references in list LIST conflict with it, excluding reads if ONLY writers | |
2847 | is nonzero. */ | |
2848 | ||
2849 | static void | |
1cae46be | 2850 | warn_for_collisions_1 (tree written, tree writer, struct tlist *list, |
2851 | int only_writes) | |
4e91a871 | 2852 | { |
2853 | struct tlist *tmp; | |
2854 | ||
2855 | /* Avoid duplicate warnings. */ | |
2856 | for (tmp = warned_ids; tmp; tmp = tmp->next) | |
79973b57 | 2857 | if (candidate_equal_p (tmp->expr, written)) |
4e91a871 | 2858 | return; |
2859 | ||
2860 | while (list) | |
2861 | { | |
79973b57 | 2862 | if (candidate_equal_p (list->expr, written) |
2863 | && !candidate_equal_p (list->writer, writer) | |
2864 | && (!only_writes || list->writer)) | |
4e91a871 | 2865 | { |
2866 | warned_ids = new_tlist (warned_ids, written, NULL_TREE); | |
3df42822 | 2867 | warning_at (EXPR_LOC_OR_LOC (writer, input_location), |
6513b50d | 2868 | OPT_Wsequence_point, "operation on %qE may be undefined", |
2869 | list->expr); | |
4e91a871 | 2870 | } |
2871 | list = list->next; | |
2872 | } | |
2873 | } | |
2874 | ||
2875 | /* Given a list LIST of references to variables, find whether any of these | |
2876 | can cause conflicts due to missing sequence points. */ | |
2877 | ||
2878 | static void | |
1cae46be | 2879 | warn_for_collisions (struct tlist *list) |
4e91a871 | 2880 | { |
2881 | struct tlist *tmp; | |
1cae46be | 2882 | |
4e91a871 | 2883 | for (tmp = list; tmp; tmp = tmp->next) |
2884 | { | |
2885 | if (tmp->writer) | |
2886 | warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); | |
2887 | } | |
2888 | } | |
2889 | ||
734c98be | 2890 | /* Return nonzero if X is a tree that can be verified by the sequence point |
4e91a871 | 2891 | warnings. */ |
2892 | static int | |
1cae46be | 2893 | warning_candidate_p (tree x) |
481c6ce6 | 2894 | { |
6ef8d12f | 2895 | if (DECL_P (x) && DECL_ARTIFICIAL (x)) |
2896 | return 0; | |
2897 | ||
027fc6ef | 2898 | if (TREE_CODE (x) == BLOCK) |
2899 | return 0; | |
2900 | ||
6ef8d12f | 2901 | /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c |
79973b57 | 2902 | (lvalue_p) crash on TRY/CATCH. */ |
6ef8d12f | 2903 | if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x))) |
2904 | return 0; | |
2905 | ||
2906 | if (!lvalue_p (x)) | |
2907 | return 0; | |
2908 | ||
2909 | /* No point to track non-const calls, they will never satisfy | |
2910 | operand_equal_p. */ | |
2911 | if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0) | |
2912 | return 0; | |
2913 | ||
2914 | if (TREE_CODE (x) == STRING_CST) | |
2915 | return 0; | |
2916 | ||
2917 | return 1; | |
79973b57 | 2918 | } |
2919 | ||
2920 | /* Return nonzero if X and Y appear to be the same candidate (or NULL) */ | |
2921 | static bool | |
2922 | candidate_equal_p (const_tree x, const_tree y) | |
2923 | { | |
2924 | return (x == y) || (x && y && operand_equal_p (x, y, 0)); | |
4e91a871 | 2925 | } |
481c6ce6 | 2926 | |
4e91a871 | 2927 | /* Walk the tree X, and record accesses to variables. If X is written by the |
2928 | parent tree, WRITER is the parent. | |
2929 | We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this | |
2930 | expression or its only operand forces a sequence point, then everything up | |
2931 | to the sequence point is stored in PBEFORE_SP. Everything else gets stored | |
2932 | in PNO_SP. | |
2933 | Once we return, we will have emitted warnings if any subexpression before | |
2934 | such a sequence point could be undefined. On a higher level, however, the | |
2935 | sequence point may not be relevant, and we'll merge the two lists. | |
2936 | ||
2937 | Example: (b++, a) + b; | |
2938 | The call that processes the COMPOUND_EXPR will store the increment of B | |
2939 | in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that | |
2940 | processes the PLUS_EXPR will need to merge the two lists so that | |
2941 | eventually, all accesses end up on the same list (and we'll warn about the | |
2942 | unordered subexpressions b++ and b. | |
2943 | ||
2944 | A note on merging. If we modify the former example so that our expression | |
2945 | becomes | |
2946 | (b++, b) + a | |
2947 | care must be taken not simply to add all three expressions into the final | |
2948 | PNO_SP list. The function merge_tlist takes care of that by merging the | |
2949 | before-SP list of the COMPOUND_EXPR into its after-SP list in a special | |
2950 | way, so that no more than one access to B is recorded. */ | |
481c6ce6 | 2951 | |
4e91a871 | 2952 | static void |
1cae46be | 2953 | verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp, |
2954 | tree writer) | |
4e91a871 | 2955 | { |
2956 | struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; | |
2957 | enum tree_code code; | |
ce45a448 | 2958 | enum tree_code_class cl; |
481c6ce6 | 2959 | |
e5b75768 | 2960 | /* X may be NULL if it is the operand of an empty statement expression |
2961 | ({ }). */ | |
2962 | if (x == NULL) | |
2963 | return; | |
2964 | ||
4e91a871 | 2965 | restart: |
2966 | code = TREE_CODE (x); | |
e916c70c | 2967 | cl = TREE_CODE_CLASS (code); |
481c6ce6 | 2968 | |
4e91a871 | 2969 | if (warning_candidate_p (x)) |
79973b57 | 2970 | *pno_sp = new_tlist (*pno_sp, x, writer); |
4e91a871 | 2971 | |
2972 | switch (code) | |
2973 | { | |
67b28e3e | 2974 | case CONSTRUCTOR: |
8e71dad2 | 2975 | case SIZEOF_EXPR: |
67b28e3e | 2976 | return; |
2977 | ||
4e91a871 | 2978 | case COMPOUND_EXPR: |
2979 | case TRUTH_ANDIF_EXPR: | |
2980 | case TRUTH_ORIF_EXPR: | |
2981 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
2982 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
2983 | warn_for_collisions (tmp_nosp); | |
2984 | merge_tlist (pbefore_sp, tmp_before, 0); | |
2985 | merge_tlist (pbefore_sp, tmp_nosp, 0); | |
2986 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); | |
2987 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
2988 | return; | |
2989 | ||
2990 | case COND_EXPR: | |
2991 | tmp_before = tmp_list2 = 0; | |
2992 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); | |
2993 | warn_for_collisions (tmp_list2); | |
2994 | merge_tlist (pbefore_sp, tmp_before, 0); | |
312243bb | 2995 | merge_tlist (pbefore_sp, tmp_list2, 0); |
4e91a871 | 2996 | |
2997 | tmp_list3 = tmp_nosp = 0; | |
2998 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); | |
2999 | warn_for_collisions (tmp_nosp); | |
3000 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3001 | ||
3002 | tmp_list3 = tmp_list2 = 0; | |
3003 | verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); | |
3004 | warn_for_collisions (tmp_list2); | |
3005 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3006 | /* Rather than add both tmp_nosp and tmp_list2, we have to merge the | |
3007 | two first, to avoid warning for (a ? b++ : b++). */ | |
3008 | merge_tlist (&tmp_nosp, tmp_list2, 0); | |
3009 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
3010 | return; | |
3011 | ||
481c6ce6 | 3012 | case PREDECREMENT_EXPR: |
3013 | case PREINCREMENT_EXPR: | |
3014 | case POSTDECREMENT_EXPR: | |
3015 | case POSTINCREMENT_EXPR: | |
4e91a871 | 3016 | verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); |
3017 | return; | |
3018 | ||
3019 | case MODIFY_EXPR: | |
3020 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
3021 | verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); | |
3022 | verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); | |
3023 | /* Expressions inside the LHS are not ordered wrt. the sequence points | |
3024 | in the RHS. Example: | |
3025 | *a = (a++, 2) | |
3026 | Despite the fact that the modification of "a" is in the before_sp | |
3027 | list (tmp_before), it conflicts with the use of "a" in the LHS. | |
3028 | We can handle this by adding the contents of tmp_list3 | |
3029 | to those of tmp_before, and redoing the collision warnings for that | |
3030 | list. */ | |
3031 | add_tlist (&tmp_before, tmp_list3, x, 1); | |
3032 | warn_for_collisions (tmp_before); | |
3033 | /* Exclude the LHS itself here; we first have to merge it into the | |
3034 | tmp_nosp list. This is done to avoid warning for "a = a"; if we | |
3035 | didn't exclude the LHS, we'd get it twice, once as a read and once | |
3036 | as a write. */ | |
3037 | add_tlist (pno_sp, tmp_list3, x, 0); | |
3038 | warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); | |
3039 | ||
3040 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3041 | if (warning_candidate_p (TREE_OPERAND (x, 0))) | |
3042 | merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); | |
3043 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); | |
3044 | return; | |
481c6ce6 | 3045 | |
3046 | case CALL_EXPR: | |
4e91a871 | 3047 | /* We need to warn about conflicts among arguments and conflicts between |
3048 | args and the function address. Side effects of the function address, | |
3049 | however, are not ordered by the sequence point of the call. */ | |
c2f47e15 | 3050 | { |
3051 | call_expr_arg_iterator iter; | |
3052 | tree arg; | |
48e1416a | 3053 | tmp_before = tmp_nosp = 0; |
c2f47e15 | 3054 | verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE); |
3055 | FOR_EACH_CALL_EXPR_ARG (arg, iter, x) | |
3056 | { | |
3057 | tmp_list2 = tmp_list3 = 0; | |
3058 | verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE); | |
3059 | merge_tlist (&tmp_list3, tmp_list2, 0); | |
3060 | add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); | |
3061 | } | |
3062 | add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); | |
3063 | warn_for_collisions (tmp_before); | |
3064 | add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); | |
3065 | return; | |
3066 | } | |
481c6ce6 | 3067 | |
3068 | case TREE_LIST: | |
3069 | /* Scan all the list, e.g. indices of multi dimensional array. */ | |
3070 | while (x) | |
3071 | { | |
4e91a871 | 3072 | tmp_before = tmp_nosp = 0; |
3073 | verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
3074 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
3075 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
481c6ce6 | 3076 | x = TREE_CHAIN (x); |
3077 | } | |
4e91a871 | 3078 | return; |
481c6ce6 | 3079 | |
4e91a871 | 3080 | case SAVE_EXPR: |
3081 | { | |
3082 | struct tlist_cache *t; | |
3083 | for (t = save_expr_cache; t; t = t->next) | |
79973b57 | 3084 | if (candidate_equal_p (t->expr, x)) |
4e91a871 | 3085 | break; |
481c6ce6 | 3086 | |
84166705 | 3087 | if (!t) |
481c6ce6 | 3088 | { |
9318f22c | 3089 | t = XOBNEW (&tlist_obstack, struct tlist_cache); |
4e91a871 | 3090 | t->next = save_expr_cache; |
3091 | t->expr = x; | |
3092 | save_expr_cache = t; | |
3093 | ||
3094 | tmp_before = tmp_nosp = 0; | |
3095 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3096 | warn_for_collisions (tmp_nosp); | |
3097 | ||
3098 | tmp_list3 = 0; | |
312243bb | 3099 | merge_tlist (&tmp_list3, tmp_nosp, 0); |
4e91a871 | 3100 | t->cache_before_sp = tmp_before; |
3101 | t->cache_after_sp = tmp_list3; | |
481c6ce6 | 3102 | } |
4e91a871 | 3103 | merge_tlist (pbefore_sp, t->cache_before_sp, 1); |
3104 | add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); | |
3105 | return; | |
3106 | } | |
481c6ce6 | 3107 | |
012916cb | 3108 | case ADDR_EXPR: |
3109 | x = TREE_OPERAND (x, 0); | |
3110 | if (DECL_P (x)) | |
3111 | return; | |
3112 | writer = 0; | |
3113 | goto restart; | |
3114 | ||
ce45a448 | 3115 | default: |
3116 | /* For other expressions, simply recurse on their operands. | |
a0c938f0 | 3117 | Manual tail recursion for unary expressions. |
ce45a448 | 3118 | Other non-expressions need not be processed. */ |
3119 | if (cl == tcc_unary) | |
3120 | { | |
ce45a448 | 3121 | x = TREE_OPERAND (x, 0); |
3122 | writer = 0; | |
3123 | goto restart; | |
3124 | } | |
3125 | else if (IS_EXPR_CODE_CLASS (cl)) | |
3126 | { | |
3127 | int lp; | |
c2f47e15 | 3128 | int max = TREE_OPERAND_LENGTH (x); |
ce45a448 | 3129 | for (lp = 0; lp < max; lp++) |
3130 | { | |
3131 | tmp_before = tmp_nosp = 0; | |
3132 | verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0); | |
3133 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
3134 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
3135 | } | |
3136 | } | |
3137 | return; | |
481c6ce6 | 3138 | } |
481c6ce6 | 3139 | } |
3140 | ||
974e2c0c | 3141 | /* Try to warn for undefined behavior in EXPR due to missing sequence |
481c6ce6 | 3142 | points. */ |
3143 | ||
4b987fac | 3144 | DEBUG_FUNCTION void |
1cae46be | 3145 | verify_sequence_points (tree expr) |
481c6ce6 | 3146 | { |
4e91a871 | 3147 | struct tlist *before_sp = 0, *after_sp = 0; |
481c6ce6 | 3148 | |
4e91a871 | 3149 | warned_ids = 0; |
3150 | save_expr_cache = 0; | |
3151 | if (tlist_firstobj == 0) | |
481c6ce6 | 3152 | { |
4e91a871 | 3153 | gcc_obstack_init (&tlist_obstack); |
4fd61bc6 | 3154 | tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0); |
481c6ce6 | 3155 | } |
3156 | ||
4e91a871 | 3157 | verify_tree (expr, &before_sp, &after_sp, 0); |
3158 | warn_for_collisions (after_sp); | |
3159 | obstack_free (&tlist_obstack, tlist_firstobj); | |
481c6ce6 | 3160 | } |
b0fc3e72 | 3161 | \f |
3162 | /* Validate the expression after `case' and apply default promotions. */ | |
3163 | ||
2ca392fd | 3164 | static tree |
2d2f6a15 | 3165 | check_case_value (location_t loc, tree value) |
b0fc3e72 | 3166 | { |
3167 | if (value == NULL_TREE) | |
3168 | return value; | |
3169 | ||
b96dc121 | 3170 | if (TREE_CODE (value) == INTEGER_CST) |
3171 | /* Promote char or short to int. */ | |
3172 | value = perform_integral_promotions (value); | |
3173 | else if (value != error_mark_node) | |
b0fc3e72 | 3174 | { |
2d2f6a15 | 3175 | error_at (loc, "case label does not reduce to an integer constant"); |
b0fc3e72 | 3176 | value = error_mark_node; |
3177 | } | |
b0fc3e72 | 3178 | |
6433f1c2 | 3179 | constant_expression_warning (value); |
3180 | ||
b0fc3e72 | 3181 | return value; |
3182 | } | |
3183 | \f | |
2ca392fd | 3184 | /* See if the case values LOW and HIGH are in the range of the original |
5c9dae64 | 3185 | type (i.e. before the default conversion to int) of the switch testing |
2ca392fd | 3186 | expression. |
3187 | TYPE is the promoted type of the testing expression, and ORIG_TYPE is | |
91275768 | 3188 | the type before promoting it. CASE_LOW_P is a pointer to the lower |
2ca392fd | 3189 | bound of the case label, and CASE_HIGH_P is the upper bound or NULL |
3190 | if the case is not a case range. | |
3191 | The caller has to make sure that we are not called with NULL for | |
be23b16f | 3192 | CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there |
3193 | was a case value that doesn't fit into the range of the ORIG_TYPE. | |
442e3cb9 | 3194 | Returns true if the case label is in range of ORIG_TYPE (saturated or |
2ca392fd | 3195 | untouched) or false if the label is out of range. */ |
3196 | ||
3197 | static bool | |
f61a9bc2 | 3198 | check_case_bounds (location_t loc, tree type, tree orig_type, |
be23b16f | 3199 | tree *case_low_p, tree *case_high_p, |
3200 | bool *outside_range_p) | |
2ca392fd | 3201 | { |
3202 | tree min_value, max_value; | |
3203 | tree case_low = *case_low_p; | |
3204 | tree case_high = case_high_p ? *case_high_p : case_low; | |
3205 | ||
3206 | /* If there was a problem with the original type, do nothing. */ | |
3207 | if (orig_type == error_mark_node) | |
3208 | return true; | |
3209 | ||
3210 | min_value = TYPE_MIN_VALUE (orig_type); | |
3211 | max_value = TYPE_MAX_VALUE (orig_type); | |
3212 | ||
73f67931 | 3213 | /* We'll really need integer constants here. */ |
3214 | case_low = fold (case_low); | |
3215 | case_high = fold (case_high); | |
3216 | ||
2ca392fd | 3217 | /* Case label is less than minimum for type. */ |
3218 | if (tree_int_cst_compare (case_low, min_value) < 0 | |
3219 | && tree_int_cst_compare (case_high, min_value) < 0) | |
3220 | { | |
f61a9bc2 | 3221 | warning_at (loc, 0, "case label value is less than minimum value " |
3222 | "for type"); | |
be23b16f | 3223 | *outside_range_p = true; |
2ca392fd | 3224 | return false; |
3225 | } | |
b27ac6b5 | 3226 | |
2ca392fd | 3227 | /* Case value is greater than maximum for type. */ |
3228 | if (tree_int_cst_compare (case_low, max_value) > 0 | |
3229 | && tree_int_cst_compare (case_high, max_value) > 0) | |
3230 | { | |
f61a9bc2 | 3231 | warning_at (loc, 0, "case label value exceeds maximum value for type"); |
be23b16f | 3232 | *outside_range_p = true; |
2ca392fd | 3233 | return false; |
3234 | } | |
3235 | ||
3236 | /* Saturate lower case label value to minimum. */ | |
3237 | if (tree_int_cst_compare (case_high, min_value) >= 0 | |
3238 | && tree_int_cst_compare (case_low, min_value) < 0) | |
3239 | { | |
f61a9bc2 | 3240 | warning_at (loc, 0, "lower value in case label range" |
3241 | " less than minimum value for type"); | |
be23b16f | 3242 | *outside_range_p = true; |
2ca392fd | 3243 | case_low = min_value; |
3244 | } | |
b27ac6b5 | 3245 | |
2ca392fd | 3246 | /* Saturate upper case label value to maximum. */ |
3247 | if (tree_int_cst_compare (case_low, max_value) <= 0 | |
3248 | && tree_int_cst_compare (case_high, max_value) > 0) | |
3249 | { | |
f61a9bc2 | 3250 | warning_at (loc, 0, "upper value in case label range" |
3251 | " exceeds maximum value for type"); | |
be23b16f | 3252 | *outside_range_p = true; |
2ca392fd | 3253 | case_high = max_value; |
3254 | } | |
3255 | ||
3256 | if (*case_low_p != case_low) | |
3257 | *case_low_p = convert (type, case_low); | |
3258 | if (case_high_p && *case_high_p != case_high) | |
3259 | *case_high_p = convert (type, case_high); | |
3260 | ||
3261 | return true; | |
3262 | } | |
3263 | \f | |
b0fc3e72 | 3264 | /* Return an integer type with BITS bits of precision, |
3265 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
3266 | ||
3267 | tree | |
1cae46be | 3268 | c_common_type_for_size (unsigned int bits, int unsignedp) |
b0fc3e72 | 3269 | { |
9f75f026 | 3270 | int i; |
3271 | ||
46375237 | 3272 | if (bits == TYPE_PRECISION (integer_type_node)) |
3273 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3274 | ||
bacde65a | 3275 | if (bits == TYPE_PRECISION (signed_char_type_node)) |
b0fc3e72 | 3276 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
3277 | ||
bacde65a | 3278 | if (bits == TYPE_PRECISION (short_integer_type_node)) |
b0fc3e72 | 3279 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
3280 | ||
bacde65a | 3281 | if (bits == TYPE_PRECISION (long_integer_type_node)) |
b0fc3e72 | 3282 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
3283 | ||
bacde65a | 3284 | if (bits == TYPE_PRECISION (long_long_integer_type_node)) |
b0fc3e72 | 3285 | return (unsignedp ? long_long_unsigned_type_node |
3286 | : long_long_integer_type_node); | |
3287 | ||
9f75f026 | 3288 | for (i = 0; i < NUM_INT_N_ENTS; i ++) |
3289 | if (int_n_enabled_p[i] | |
3290 | && bits == int_n_data[i].bitsize) | |
3291 | return (unsignedp ? int_n_trees[i].unsigned_type | |
3292 | : int_n_trees[i].signed_type); | |
6388cfe2 | 3293 | |
f57fa2ea | 3294 | if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) |
3295 | return (unsignedp ? widest_unsigned_literal_type_node | |
3296 | : widest_integer_literal_type_node); | |
3297 | ||
bacde65a | 3298 | if (bits <= TYPE_PRECISION (intQI_type_node)) |
3299 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3300 | ||
3301 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
3302 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3303 | ||
3304 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
3305 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3306 | ||
3307 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
3308 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3309 | ||
b0fc3e72 | 3310 | return 0; |
3311 | } | |
3312 | ||
9421ebb9 | 3313 | /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits |
3314 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed; | |
3315 | and saturating if SATP is nonzero, otherwise not saturating. */ | |
3316 | ||
3317 | tree | |
3318 | c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit, | |
3319 | int unsignedp, int satp) | |
3320 | { | |
3754d046 | 3321 | machine_mode mode; |
9421ebb9 | 3322 | if (ibit == 0) |
3323 | mode = unsignedp ? UQQmode : QQmode; | |
3324 | else | |
3325 | mode = unsignedp ? UHAmode : HAmode; | |
3326 | ||
3327 | for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode)) | |
3328 | if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit) | |
3329 | break; | |
3330 | ||
3331 | if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode)) | |
3332 | { | |
3333 | sorry ("GCC cannot support operators with integer types and " | |
3334 | "fixed-point types that have too many integral and " | |
3335 | "fractional bits together"); | |
3336 | return 0; | |
3337 | } | |
3338 | ||
3339 | return c_common_type_for_mode (mode, satp); | |
3340 | } | |
3341 | ||
5b247e9f | 3342 | /* Used for communication between c_common_type_for_mode and |
3343 | c_register_builtin_type. */ | |
c1917557 | 3344 | tree registered_builtin_types; |
5b247e9f | 3345 | |
b0fc3e72 | 3346 | /* Return a data type that has machine mode MODE. |
3347 | If the mode is an integer, | |
9421ebb9 | 3348 | then UNSIGNEDP selects between signed and unsigned types. |
3349 | If the mode is a fixed-point mode, | |
3350 | then UNSIGNEDP selects between saturating and nonsaturating types. */ | |
b0fc3e72 | 3351 | |
3352 | tree | |
3754d046 | 3353 | c_common_type_for_mode (machine_mode mode, int unsignedp) |
b0fc3e72 | 3354 | { |
5b247e9f | 3355 | tree t; |
9f75f026 | 3356 | int i; |
5b247e9f | 3357 | |
46375237 | 3358 | if (mode == TYPE_MODE (integer_type_node)) |
3359 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3360 | ||
b0fc3e72 | 3361 | if (mode == TYPE_MODE (signed_char_type_node)) |
3362 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3363 | ||
3364 | if (mode == TYPE_MODE (short_integer_type_node)) | |
3365 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3366 | ||
b0fc3e72 | 3367 | if (mode == TYPE_MODE (long_integer_type_node)) |
3368 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3369 | ||
3370 | if (mode == TYPE_MODE (long_long_integer_type_node)) | |
3371 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
3372 | ||
9f75f026 | 3373 | for (i = 0; i < NUM_INT_N_ENTS; i ++) |
3374 | if (int_n_enabled_p[i] | |
3375 | && mode == int_n_data[i].m) | |
3376 | return (unsignedp ? int_n_trees[i].unsigned_type | |
3377 | : int_n_trees[i].signed_type); | |
6388cfe2 | 3378 | |
f57fa2ea | 3379 | if (mode == TYPE_MODE (widest_integer_literal_type_node)) |
44e9fa65 | 3380 | return unsignedp ? widest_unsigned_literal_type_node |
4ee9c684 | 3381 | : widest_integer_literal_type_node; |
f57fa2ea | 3382 | |
88ae7f04 | 3383 | if (mode == QImode) |
bacde65a | 3384 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
3385 | ||
88ae7f04 | 3386 | if (mode == HImode) |
bacde65a | 3387 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
3388 | ||
88ae7f04 | 3389 | if (mode == SImode) |
bacde65a | 3390 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
3391 | ||
88ae7f04 | 3392 | if (mode == DImode) |
bacde65a | 3393 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
ab2c1de8 | 3394 | |
cc1cc1c7 | 3395 | #if HOST_BITS_PER_WIDE_INT >= 64 |
6274009c | 3396 | if (mode == TYPE_MODE (intTI_type_node)) |
3397 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
cc1cc1c7 | 3398 | #endif |
6274009c | 3399 | |
b0fc3e72 | 3400 | if (mode == TYPE_MODE (float_type_node)) |
3401 | return float_type_node; | |
3402 | ||
3403 | if (mode == TYPE_MODE (double_type_node)) | |
3404 | return double_type_node; | |
3405 | ||
3406 | if (mode == TYPE_MODE (long_double_type_node)) | |
3407 | return long_double_type_node; | |
3408 | ||
545c2bde | 3409 | if (mode == TYPE_MODE (void_type_node)) |
3410 | return void_type_node; | |
b27ac6b5 | 3411 | |
b0fc3e72 | 3412 | if (mode == TYPE_MODE (build_pointer_type (char_type_node))) |
61b9b73c | 3413 | return (unsignedp |
3414 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
3415 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 3416 | |
3417 | if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) | |
61b9b73c | 3418 | return (unsignedp |
3419 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
3420 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 3421 | |
0dfc45b5 | 3422 | if (COMPLEX_MODE_P (mode)) |
3423 | { | |
3754d046 | 3424 | machine_mode inner_mode; |
0dfc45b5 | 3425 | tree inner_type; |
3426 | ||
3427 | if (mode == TYPE_MODE (complex_float_type_node)) | |
3428 | return complex_float_type_node; | |
3429 | if (mode == TYPE_MODE (complex_double_type_node)) | |
3430 | return complex_double_type_node; | |
3431 | if (mode == TYPE_MODE (complex_long_double_type_node)) | |
3432 | return complex_long_double_type_node; | |
3433 | ||
3434 | if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp) | |
3435 | return complex_integer_type_node; | |
3436 | ||
3437 | inner_mode = GET_MODE_INNER (mode); | |
3438 | inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
3439 | if (inner_type != NULL_TREE) | |
3440 | return build_complex_type (inner_type); | |
3441 | } | |
3442 | else if (VECTOR_MODE_P (mode)) | |
4917c376 | 3443 | { |
3754d046 | 3444 | machine_mode inner_mode = GET_MODE_INNER (mode); |
4917c376 | 3445 | tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); |
3446 | if (inner_type != NULL_TREE) | |
3447 | return build_vector_type_for_mode (inner_type, mode); | |
88ae7f04 | 3448 | } |
e2ea7e3a | 3449 | |
c4503c0a | 3450 | if (mode == TYPE_MODE (dfloat32_type_node)) |
3451 | return dfloat32_type_node; | |
3452 | if (mode == TYPE_MODE (dfloat64_type_node)) | |
3453 | return dfloat64_type_node; | |
3454 | if (mode == TYPE_MODE (dfloat128_type_node)) | |
3455 | return dfloat128_type_node; | |
3456 | ||
9421ebb9 | 3457 | if (ALL_SCALAR_FIXED_POINT_MODE_P (mode)) |
3458 | { | |
3459 | if (mode == TYPE_MODE (short_fract_type_node)) | |
3460 | return unsignedp ? sat_short_fract_type_node : short_fract_type_node; | |
3461 | if (mode == TYPE_MODE (fract_type_node)) | |
3462 | return unsignedp ? sat_fract_type_node : fract_type_node; | |
3463 | if (mode == TYPE_MODE (long_fract_type_node)) | |
3464 | return unsignedp ? sat_long_fract_type_node : long_fract_type_node; | |
3465 | if (mode == TYPE_MODE (long_long_fract_type_node)) | |
3466 | return unsignedp ? sat_long_long_fract_type_node | |
3467 | : long_long_fract_type_node; | |
3468 | ||
3469 | if (mode == TYPE_MODE (unsigned_short_fract_type_node)) | |
3470 | return unsignedp ? sat_unsigned_short_fract_type_node | |
3471 | : unsigned_short_fract_type_node; | |
3472 | if (mode == TYPE_MODE (unsigned_fract_type_node)) | |
3473 | return unsignedp ? sat_unsigned_fract_type_node | |
3474 | : unsigned_fract_type_node; | |
3475 | if (mode == TYPE_MODE (unsigned_long_fract_type_node)) | |
3476 | return unsignedp ? sat_unsigned_long_fract_type_node | |
3477 | : unsigned_long_fract_type_node; | |
3478 | if (mode == TYPE_MODE (unsigned_long_long_fract_type_node)) | |
3479 | return unsignedp ? sat_unsigned_long_long_fract_type_node | |
3480 | : unsigned_long_long_fract_type_node; | |
3481 | ||
3482 | if (mode == TYPE_MODE (short_accum_type_node)) | |
3483 | return unsignedp ? sat_short_accum_type_node : short_accum_type_node; | |
3484 | if (mode == TYPE_MODE (accum_type_node)) | |
3485 | return unsignedp ? sat_accum_type_node : accum_type_node; | |
3486 | if (mode == TYPE_MODE (long_accum_type_node)) | |
3487 | return unsignedp ? sat_long_accum_type_node : long_accum_type_node; | |
3488 | if (mode == TYPE_MODE (long_long_accum_type_node)) | |
3489 | return unsignedp ? sat_long_long_accum_type_node | |
3490 | : long_long_accum_type_node; | |
3491 | ||
3492 | if (mode == TYPE_MODE (unsigned_short_accum_type_node)) | |
3493 | return unsignedp ? sat_unsigned_short_accum_type_node | |
3494 | : unsigned_short_accum_type_node; | |
3495 | if (mode == TYPE_MODE (unsigned_accum_type_node)) | |
3496 | return unsignedp ? sat_unsigned_accum_type_node | |
3497 | : unsigned_accum_type_node; | |
3498 | if (mode == TYPE_MODE (unsigned_long_accum_type_node)) | |
3499 | return unsignedp ? sat_unsigned_long_accum_type_node | |
3500 | : unsigned_long_accum_type_node; | |
3501 | if (mode == TYPE_MODE (unsigned_long_long_accum_type_node)) | |
3502 | return unsignedp ? sat_unsigned_long_long_accum_type_node | |
3503 | : unsigned_long_long_accum_type_node; | |
3504 | ||
3505 | if (mode == QQmode) | |
3506 | return unsignedp ? sat_qq_type_node : qq_type_node; | |
3507 | if (mode == HQmode) | |
3508 | return unsignedp ? sat_hq_type_node : hq_type_node; | |
3509 | if (mode == SQmode) | |
3510 | return unsignedp ? sat_sq_type_node : sq_type_node; | |
3511 | if (mode == DQmode) | |
3512 | return unsignedp ? sat_dq_type_node : dq_type_node; | |
3513 | if (mode == TQmode) | |
3514 | return unsignedp ? sat_tq_type_node : tq_type_node; | |
3515 | ||
3516 | if (mode == UQQmode) | |
3517 | return unsignedp ? sat_uqq_type_node : uqq_type_node; | |
3518 | if (mode == UHQmode) | |
3519 | return unsignedp ? sat_uhq_type_node : uhq_type_node; | |
3520 | if (mode == USQmode) | |
3521 | return unsignedp ? sat_usq_type_node : usq_type_node; | |
3522 | if (mode == UDQmode) | |
3523 | return unsignedp ? sat_udq_type_node : udq_type_node; | |
3524 | if (mode == UTQmode) | |
3525 | return unsignedp ? sat_utq_type_node : utq_type_node; | |
3526 | ||
3527 | if (mode == HAmode) | |
3528 | return unsignedp ? sat_ha_type_node : ha_type_node; | |
3529 | if (mode == SAmode) | |
3530 | return unsignedp ? sat_sa_type_node : sa_type_node; | |
3531 | if (mode == DAmode) | |
3532 | return unsignedp ? sat_da_type_node : da_type_node; | |
3533 | if (mode == TAmode) | |
3534 | return unsignedp ? sat_ta_type_node : ta_type_node; | |
3535 | ||
3536 | if (mode == UHAmode) | |
3537 | return unsignedp ? sat_uha_type_node : uha_type_node; | |
3538 | if (mode == USAmode) | |
3539 | return unsignedp ? sat_usa_type_node : usa_type_node; | |
3540 | if (mode == UDAmode) | |
3541 | return unsignedp ? sat_uda_type_node : uda_type_node; | |
3542 | if (mode == UTAmode) | |
3543 | return unsignedp ? sat_uta_type_node : uta_type_node; | |
3544 | } | |
3545 | ||
5b247e9f | 3546 | for (t = registered_builtin_types; t; t = TREE_CHAIN (t)) |
baec58e1 | 3547 | if (TYPE_MODE (TREE_VALUE (t)) == mode |
3548 | && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t))) | |
5b247e9f | 3549 | return TREE_VALUE (t); |
3550 | ||
b0fc3e72 | 3551 | return 0; |
3552 | } | |
20d39783 | 3553 | |
11773141 | 3554 | tree |
3555 | c_common_unsigned_type (tree type) | |
3556 | { | |
3557 | return c_common_signed_or_unsigned_type (1, type); | |
3558 | } | |
3559 | ||
20d39783 | 3560 | /* Return a signed type the same as TYPE in other respects. */ |
3561 | ||
3562 | tree | |
1cae46be | 3563 | c_common_signed_type (tree type) |
20d39783 | 3564 | { |
4070745f | 3565 | return c_common_signed_or_unsigned_type (0, type); |
20d39783 | 3566 | } |
3567 | ||
3568 | /* Return a type the same as TYPE except unsigned or | |
3569 | signed according to UNSIGNEDP. */ | |
3570 | ||
3571 | tree | |
1cae46be | 3572 | c_common_signed_or_unsigned_type (int unsignedp, tree type) |
20d39783 | 3573 | { |
7a91101f | 3574 | tree type1; |
9f75f026 | 3575 | int i; |
20d39783 | 3576 | |
7a91101f | 3577 | /* This block of code emulates the behavior of the old |
3578 | c_common_unsigned_type. In particular, it returns | |
3579 | long_unsigned_type_node if passed a long, even when a int would | |
3580 | have the same size. This is necessary for warnings to work | |
3581 | correctly in archs where sizeof(int) == sizeof(long) */ | |
3582 | ||
3583 | type1 = TYPE_MAIN_VARIANT (type); | |
3584 | if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node) | |
3585 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3586 | if (type1 == integer_type_node || type1 == unsigned_type_node) | |
3587 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3588 | if (type1 == short_integer_type_node || type1 == short_unsigned_type_node) | |
3589 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3590 | if (type1 == long_integer_type_node || type1 == long_unsigned_type_node) | |
3591 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3592 | if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node) | |
3593 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
9f75f026 | 3594 | |
3595 | for (i = 0; i < NUM_INT_N_ENTS; i ++) | |
3596 | if (int_n_enabled_p[i] | |
3597 | && (type1 == int_n_trees[i].unsigned_type | |
3598 | || type1 == int_n_trees[i].signed_type)) | |
3599 | return (unsignedp ? int_n_trees[i].unsigned_type | |
3600 | : int_n_trees[i].signed_type); | |
3601 | ||
7a91101f | 3602 | if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node) |
3603 | return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node; | |
3604 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
3605 | if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node) | |
3606 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
3607 | #endif | |
3608 | if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node) | |
3609 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3610 | if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node) | |
3611 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3612 | if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node) | |
3613 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3614 | if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node) | |
3615 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3616 | ||
9f92e1a2 | 3617 | #define C_COMMON_FIXED_TYPES(NAME) \ |
3618 | if (type1 == short_ ## NAME ## _type_node \ | |
3619 | || type1 == unsigned_short_ ## NAME ## _type_node) \ | |
3620 | return unsignedp ? unsigned_short_ ## NAME ## _type_node \ | |
3621 | : short_ ## NAME ## _type_node; \ | |
3622 | if (type1 == NAME ## _type_node \ | |
3623 | || type1 == unsigned_ ## NAME ## _type_node) \ | |
3624 | return unsignedp ? unsigned_ ## NAME ## _type_node \ | |
3625 | : NAME ## _type_node; \ | |
3626 | if (type1 == long_ ## NAME ## _type_node \ | |
3627 | || type1 == unsigned_long_ ## NAME ## _type_node) \ | |
3628 | return unsignedp ? unsigned_long_ ## NAME ## _type_node \ | |
3629 | : long_ ## NAME ## _type_node; \ | |
3630 | if (type1 == long_long_ ## NAME ## _type_node \ | |
3631 | || type1 == unsigned_long_long_ ## NAME ## _type_node) \ | |
3632 | return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \ | |
3633 | : long_long_ ## NAME ## _type_node; | |
3634 | ||
3635 | #define C_COMMON_FIXED_MODE_TYPES(NAME) \ | |
3636 | if (type1 == NAME ## _type_node \ | |
3637 | || type1 == u ## NAME ## _type_node) \ | |
3638 | return unsignedp ? u ## NAME ## _type_node \ | |
3639 | : NAME ## _type_node; | |
3640 | ||
3641 | #define C_COMMON_FIXED_TYPES_SAT(NAME) \ | |
3642 | if (type1 == sat_ ## short_ ## NAME ## _type_node \ | |
3643 | || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \ | |
3644 | return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \ | |
3645 | : sat_ ## short_ ## NAME ## _type_node; \ | |
3646 | if (type1 == sat_ ## NAME ## _type_node \ | |
3647 | || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \ | |
3648 | return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \ | |
3649 | : sat_ ## NAME ## _type_node; \ | |
3650 | if (type1 == sat_ ## long_ ## NAME ## _type_node \ | |
3651 | || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \ | |
3652 | return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \ | |
3653 | : sat_ ## long_ ## NAME ## _type_node; \ | |
3654 | if (type1 == sat_ ## long_long_ ## NAME ## _type_node \ | |
3655 | || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \ | |
3656 | return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \ | |
3657 | : sat_ ## long_long_ ## NAME ## _type_node; | |
3658 | ||
3659 | #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \ | |
3660 | if (type1 == sat_ ## NAME ## _type_node \ | |
3661 | || type1 == sat_ ## u ## NAME ## _type_node) \ | |
3662 | return unsignedp ? sat_ ## u ## NAME ## _type_node \ | |
3663 | : sat_ ## NAME ## _type_node; | |
3664 | ||
3665 | C_COMMON_FIXED_TYPES (fract); | |
3666 | C_COMMON_FIXED_TYPES_SAT (fract); | |
3667 | C_COMMON_FIXED_TYPES (accum); | |
3668 | C_COMMON_FIXED_TYPES_SAT (accum); | |
3669 | ||
3670 | C_COMMON_FIXED_MODE_TYPES (qq); | |
3671 | C_COMMON_FIXED_MODE_TYPES (hq); | |
3672 | C_COMMON_FIXED_MODE_TYPES (sq); | |
3673 | C_COMMON_FIXED_MODE_TYPES (dq); | |
3674 | C_COMMON_FIXED_MODE_TYPES (tq); | |
3675 | C_COMMON_FIXED_MODE_TYPES_SAT (qq); | |
3676 | C_COMMON_FIXED_MODE_TYPES_SAT (hq); | |
3677 | C_COMMON_FIXED_MODE_TYPES_SAT (sq); | |
3678 | C_COMMON_FIXED_MODE_TYPES_SAT (dq); | |
3679 | C_COMMON_FIXED_MODE_TYPES_SAT (tq); | |
3680 | C_COMMON_FIXED_MODE_TYPES (ha); | |
3681 | C_COMMON_FIXED_MODE_TYPES (sa); | |
3682 | C_COMMON_FIXED_MODE_TYPES (da); | |
3683 | C_COMMON_FIXED_MODE_TYPES (ta); | |
3684 | C_COMMON_FIXED_MODE_TYPES_SAT (ha); | |
3685 | C_COMMON_FIXED_MODE_TYPES_SAT (sa); | |
3686 | C_COMMON_FIXED_MODE_TYPES_SAT (da); | |
3687 | C_COMMON_FIXED_MODE_TYPES_SAT (ta); | |
9421ebb9 | 3688 | |
4f7f7efd | 3689 | /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not |
3690 | the precision; they have precision set to match their range, but | |
3691 | may use a wider mode to match an ABI. If we change modes, we may | |
3692 | wind up with bad conversions. For INTEGER_TYPEs in C, must check | |
3693 | the precision as well, so as to yield correct results for | |
3694 | bit-field types. C++ does not have these separate bit-field | |
3695 | types, and producing a signed or unsigned variant of an | |
3696 | ENUMERAL_TYPE may cause other problems as well. */ | |
3697 | ||
ac265864 | 3698 | if (!INTEGRAL_TYPE_P (type) |
3699 | || TYPE_UNSIGNED (type) == unsignedp) | |
3700 | return type; | |
3701 | ||
4f7f7efd | 3702 | #define TYPE_OK(node) \ |
3703 | (TYPE_MODE (type) == TYPE_MODE (node) \ | |
0c4abe5b | 3704 | && TYPE_PRECISION (type) == TYPE_PRECISION (node)) |
4f7f7efd | 3705 | if (TYPE_OK (signed_char_type_node)) |
20d39783 | 3706 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
4f7f7efd | 3707 | if (TYPE_OK (integer_type_node)) |
20d39783 | 3708 | return unsignedp ? unsigned_type_node : integer_type_node; |
4f7f7efd | 3709 | if (TYPE_OK (short_integer_type_node)) |
20d39783 | 3710 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
4f7f7efd | 3711 | if (TYPE_OK (long_integer_type_node)) |
20d39783 | 3712 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
4f7f7efd | 3713 | if (TYPE_OK (long_long_integer_type_node)) |
20d39783 | 3714 | return (unsignedp ? long_long_unsigned_type_node |
3715 | : long_long_integer_type_node); | |
9f75f026 | 3716 | |
3717 | for (i = 0; i < NUM_INT_N_ENTS; i ++) | |
3718 | if (int_n_enabled_p[i] | |
3719 | && TYPE_MODE (type) == int_n_data[i].m | |
3720 | && TYPE_PRECISION (type) == int_n_data[i].bitsize) | |
3721 | return (unsignedp ? int_n_trees[i].unsigned_type | |
3722 | : int_n_trees[i].signed_type); | |
3723 | ||
4f7f7efd | 3724 | if (TYPE_OK (widest_integer_literal_type_node)) |
20d39783 | 3725 | return (unsignedp ? widest_unsigned_literal_type_node |
3726 | : widest_integer_literal_type_node); | |
ef11801e | 3727 | |
3728 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
4f7f7efd | 3729 | if (TYPE_OK (intTI_type_node)) |
ef11801e | 3730 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; |
3731 | #endif | |
4f7f7efd | 3732 | if (TYPE_OK (intDI_type_node)) |
ef11801e | 3733 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
4f7f7efd | 3734 | if (TYPE_OK (intSI_type_node)) |
ef11801e | 3735 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
4f7f7efd | 3736 | if (TYPE_OK (intHI_type_node)) |
ef11801e | 3737 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
4f7f7efd | 3738 | if (TYPE_OK (intQI_type_node)) |
ef11801e | 3739 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
4f7f7efd | 3740 | #undef TYPE_OK |
ef11801e | 3741 | |
0c4abe5b | 3742 | return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp); |
20d39783 | 3743 | } |
b268e47e | 3744 | |
c0e47fd4 | 3745 | /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */ |
3746 | ||
3747 | tree | |
3748 | c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp) | |
3749 | { | |
9f75f026 | 3750 | int i; |
3751 | ||
c0e47fd4 | 3752 | /* Extended integer types of the same width as a standard type have |
3753 | lesser rank, so those of the same width as int promote to int or | |
3754 | unsigned int and are valid for printf formats expecting int or | |
3755 | unsigned int. To avoid such special cases, avoid creating | |
3756 | extended integer types for bit-fields if a standard integer type | |
3757 | is available. */ | |
3758 | if (width == TYPE_PRECISION (integer_type_node)) | |
3759 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3760 | if (width == TYPE_PRECISION (signed_char_type_node)) | |
3761 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3762 | if (width == TYPE_PRECISION (short_integer_type_node)) | |
3763 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3764 | if (width == TYPE_PRECISION (long_integer_type_node)) | |
3765 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3766 | if (width == TYPE_PRECISION (long_long_integer_type_node)) | |
3767 | return (unsignedp ? long_long_unsigned_type_node | |
3768 | : long_long_integer_type_node); | |
9f75f026 | 3769 | for (i = 0; i < NUM_INT_N_ENTS; i ++) |
3770 | if (int_n_enabled_p[i] | |
3771 | && width == int_n_data[i].bitsize) | |
3772 | return (unsignedp ? int_n_trees[i].unsigned_type | |
3773 | : int_n_trees[i].signed_type); | |
c0e47fd4 | 3774 | return build_nonstandard_integer_type (width, unsignedp); |
3775 | } | |
3776 | ||
b268e47e | 3777 | /* The C version of the register_builtin_type langhook. */ |
3778 | ||
3779 | void | |
3780 | c_register_builtin_type (tree type, const char* name) | |
3781 | { | |
3782 | tree decl; | |
3783 | ||
e60a6f7b | 3784 | decl = build_decl (UNKNOWN_LOCATION, |
3785 | TYPE_DECL, get_identifier (name), type); | |
b268e47e | 3786 | DECL_ARTIFICIAL (decl) = 1; |
3787 | if (!TYPE_NAME (type)) | |
3788 | TYPE_NAME (type) = decl; | |
3789 | pushdecl (decl); | |
5b247e9f | 3790 | |
3791 | registered_builtin_types = tree_cons (0, type, registered_builtin_types); | |
b268e47e | 3792 | } |
a9b9d10c | 3793 | \f |
aff9e656 | 3794 | /* Print an error message for invalid operands to arith operation |
8e70fb09 | 3795 | CODE with TYPE0 for operand 0, and TYPE1 for operand 1. |
3796 | LOCATION is the location of the message. */ | |
b0fc3e72 | 3797 | |
3798 | void | |
8e70fb09 | 3799 | binary_op_error (location_t location, enum tree_code code, |
3800 | tree type0, tree type1) | |
b0fc3e72 | 3801 | { |
19cb6b50 | 3802 | const char *opname; |
f03946e4 | 3803 | |
b0fc3e72 | 3804 | switch (code) |
3805 | { | |
b0fc3e72 | 3806 | case PLUS_EXPR: |
3807 | opname = "+"; break; | |
3808 | case MINUS_EXPR: | |
3809 | opname = "-"; break; | |
3810 | case MULT_EXPR: | |
3811 | opname = "*"; break; | |
3812 | case MAX_EXPR: | |
3813 | opname = "max"; break; | |
3814 | case MIN_EXPR: | |
3815 | opname = "min"; break; | |
3816 | case EQ_EXPR: | |
3817 | opname = "=="; break; | |
3818 | case NE_EXPR: | |
3819 | opname = "!="; break; | |
3820 | case LE_EXPR: | |
3821 | opname = "<="; break; | |
3822 | case GE_EXPR: | |
3823 | opname = ">="; break; | |
3824 | case LT_EXPR: | |
3825 | opname = "<"; break; | |
3826 | case GT_EXPR: | |
3827 | opname = ">"; break; | |
3828 | case LSHIFT_EXPR: | |
3829 | opname = "<<"; break; | |
3830 | case RSHIFT_EXPR: | |
3831 | opname = ">>"; break; | |
3832 | case TRUNC_MOD_EXPR: | |
66618a1e | 3833 | case FLOOR_MOD_EXPR: |
b0fc3e72 | 3834 | opname = "%"; break; |
3835 | case TRUNC_DIV_EXPR: | |
66618a1e | 3836 | case FLOOR_DIV_EXPR: |
b0fc3e72 | 3837 | opname = "/"; break; |
3838 | case BIT_AND_EXPR: | |
3839 | opname = "&"; break; | |
3840 | case BIT_IOR_EXPR: | |
3841 | opname = "|"; break; | |
3842 | case TRUTH_ANDIF_EXPR: | |
3843 | opname = "&&"; break; | |
3844 | case TRUTH_ORIF_EXPR: | |
3845 | opname = "||"; break; | |
3846 | case BIT_XOR_EXPR: | |
3847 | opname = "^"; break; | |
31f820d2 | 3848 | default: |
315ba355 | 3849 | gcc_unreachable (); |
b0fc3e72 | 3850 | } |
8e70fb09 | 3851 | error_at (location, |
3852 | "invalid operands to binary %s (have %qT and %qT)", opname, | |
3853 | type0, type1); | |
b0fc3e72 | 3854 | } |
3855 | \f | |
03fe1dc2 | 3856 | /* Given an expression as a tree, return its original type. Do this |
3857 | by stripping any conversion that preserves the sign and precision. */ | |
3858 | static tree | |
3859 | expr_original_type (tree expr) | |
3860 | { | |
3861 | STRIP_SIGN_NOPS (expr); | |
3862 | return TREE_TYPE (expr); | |
3863 | } | |
3864 | ||
b0fc3e72 | 3865 | /* Subroutine of build_binary_op, used for comparison operations. |
3866 | See if the operands have both been converted from subword integer types | |
3867 | and, if so, perhaps change them both back to their original type. | |
5b511807 | 3868 | This function is also responsible for converting the two operands |
3869 | to the proper common type for comparison. | |
b0fc3e72 | 3870 | |
3871 | The arguments of this function are all pointers to local variables | |
3872 | of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, | |
3873 | RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. | |
3874 | ||
2623625f | 3875 | LOC is the location of the comparison. |
3876 | ||
b0fc3e72 | 3877 | If this function returns nonzero, it means that the comparison has |
3878 | a constant value. What this function returns is an expression for | |
3879 | that value. */ | |
3880 | ||
3881 | tree | |
2623625f | 3882 | shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr, |
3883 | tree *restype_ptr, enum tree_code *rescode_ptr) | |
b0fc3e72 | 3884 | { |
19cb6b50 | 3885 | tree type; |
b0fc3e72 | 3886 | tree op0 = *op0_ptr; |
3887 | tree op1 = *op1_ptr; | |
3888 | int unsignedp0, unsignedp1; | |
3889 | int real1, real2; | |
3890 | tree primop0, primop1; | |
3891 | enum tree_code code = *rescode_ptr; | |
3892 | ||
3893 | /* Throw away any conversions to wider types | |
3894 | already present in the operands. */ | |
3895 | ||
7f506bca | 3896 | primop0 = c_common_get_narrower (op0, &unsignedp0); |
3897 | primop1 = c_common_get_narrower (op1, &unsignedp1); | |
b0fc3e72 | 3898 | |
119d06b2 | 3899 | /* If primopN is first sign-extended from primopN's precision to opN's |
3900 | precision, then zero-extended from opN's precision to | |
3901 | *restype_ptr precision, shortenings might be invalid. */ | |
3902 | if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0)) | |
3903 | && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr) | |
3904 | && !unsignedp0 | |
3905 | && TYPE_UNSIGNED (TREE_TYPE (op0))) | |
3906 | primop0 = op0; | |
3907 | if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1)) | |
3908 | && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr) | |
3909 | && !unsignedp1 | |
3910 | && TYPE_UNSIGNED (TREE_TYPE (op1))) | |
3911 | primop1 = op1; | |
3912 | ||
b0fc3e72 | 3913 | /* Handle the case that OP0 does not *contain* a conversion |
3914 | but it *requires* conversion to FINAL_TYPE. */ | |
3915 | ||
3916 | if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) | |
78a8ed03 | 3917 | unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
b0fc3e72 | 3918 | if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) |
78a8ed03 | 3919 | unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1)); |
b0fc3e72 | 3920 | |
3921 | /* If one of the operands must be floated, we cannot optimize. */ | |
3922 | real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; | |
3923 | real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; | |
ab2c1de8 | 3924 | |
b0fc3e72 | 3925 | /* If first arg is constant, swap the args (changing operation |
2bd278cc | 3926 | so value is preserved), for canonicalization. Don't do this if |
3927 | the second arg is 0. */ | |
b0fc3e72 | 3928 | |
2bd278cc | 3929 | if (TREE_CONSTANT (primop0) |
9421ebb9 | 3930 | && !integer_zerop (primop1) && !real_zerop (primop1) |
3931 | && !fixed_zerop (primop1)) | |
b0fc3e72 | 3932 | { |
dfcf26a5 | 3933 | std::swap (primop0, primop1); |
3934 | std::swap (op0, op1); | |
b0fc3e72 | 3935 | *op0_ptr = op0; |
3936 | *op1_ptr = op1; | |
dfcf26a5 | 3937 | std::swap (unsignedp0, unsignedp1); |
3938 | std::swap (real1, real2); | |
b0fc3e72 | 3939 | |
3940 | switch (code) | |
3941 | { | |
3942 | case LT_EXPR: | |
3943 | code = GT_EXPR; | |
3944 | break; | |
3945 | case GT_EXPR: | |
3946 | code = LT_EXPR; | |
3947 | break; | |
3948 | case LE_EXPR: | |
3949 | code = GE_EXPR; | |
3950 | break; | |
3951 | case GE_EXPR: | |
3952 | code = LE_EXPR; | |
3953 | break; | |
31f820d2 | 3954 | default: |
3955 | break; | |
b0fc3e72 | 3956 | } |
3957 | *rescode_ptr = code; | |
3958 | } | |
3959 | ||
3960 | /* If comparing an integer against a constant more bits wide, | |
3961 | maybe we can deduce a value of 1 or 0 independent of the data. | |
3962 | Or else truncate the constant now | |
3963 | rather than extend the variable at run time. | |
3964 | ||
3965 | This is only interesting if the constant is the wider arg. | |
3966 | Also, it is not safe if the constant is unsigned and the | |
3967 | variable arg is signed, since in this case the variable | |
3968 | would be sign-extended and then regarded as unsigned. | |
3969 | Our technique fails in this case because the lowest/highest | |
3970 | possible unsigned results don't follow naturally from the | |
3971 | lowest/highest possible values of the variable operand. | |
3972 | For just EQ_EXPR and NE_EXPR there is another technique that | |
3973 | could be used: see if the constant can be faithfully represented | |
3974 | in the other operand's type, by truncating it and reextending it | |
3975 | and see if that preserves the constant's value. */ | |
3976 | ||
3977 | if (!real1 && !real2 | |
9421ebb9 | 3978 | && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE |
b0fc3e72 | 3979 | && TREE_CODE (primop1) == INTEGER_CST |
3980 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) | |
3981 | { | |
3982 | int min_gt, max_gt, min_lt, max_lt; | |
3983 | tree maxval, minval; | |
3984 | /* 1 if comparison is nominally unsigned. */ | |
78a8ed03 | 3985 | int unsignedp = TYPE_UNSIGNED (*restype_ptr); |
b0fc3e72 | 3986 | tree val; |
3987 | ||
4070745f | 3988 | type = c_common_signed_or_unsigned_type (unsignedp0, |
3989 | TREE_TYPE (primop0)); | |
cda09c61 | 3990 | |
b0fc3e72 | 3991 | maxval = TYPE_MAX_VALUE (type); |
3992 | minval = TYPE_MIN_VALUE (type); | |
3993 | ||
3994 | if (unsignedp && !unsignedp0) | |
4070745f | 3995 | *restype_ptr = c_common_signed_type (*restype_ptr); |
b0fc3e72 | 3996 | |
3997 | if (TREE_TYPE (primop1) != *restype_ptr) | |
18dbec6f | 3998 | { |
9a5e8086 | 3999 | /* Convert primop1 to target type, but do not introduce |
4000 | additional overflow. We know primop1 is an int_cst. */ | |
e913b5cd | 4001 | primop1 = force_fit_type (*restype_ptr, |
796b6678 | 4002 | wide_int::from |
4003 | (primop1, | |
4004 | TYPE_PRECISION (*restype_ptr), | |
4005 | TYPE_SIGN (TREE_TYPE (primop1))), | |
e913b5cd | 4006 | 0, TREE_OVERFLOW (primop1)); |
18dbec6f | 4007 | } |
b0fc3e72 | 4008 | if (type != *restype_ptr) |
4009 | { | |
4010 | minval = convert (*restype_ptr, minval); | |
4011 | maxval = convert (*restype_ptr, maxval); | |
4012 | } | |
4013 | ||
d99d10ca | 4014 | min_gt = tree_int_cst_lt (primop1, minval); |
4015 | max_gt = tree_int_cst_lt (primop1, maxval); | |
4016 | min_lt = tree_int_cst_lt (minval, primop1); | |
4017 | max_lt = tree_int_cst_lt (maxval, primop1); | |
b0fc3e72 | 4018 | |
4019 | val = 0; | |
4020 | /* This used to be a switch, but Genix compiler can't handle that. */ | |
4021 | if (code == NE_EXPR) | |
4022 | { | |
4023 | if (max_lt || min_gt) | |
3c2239cf | 4024 | val = truthvalue_true_node; |
b0fc3e72 | 4025 | } |
4026 | else if (code == EQ_EXPR) | |
4027 | { | |
4028 | if (max_lt || min_gt) | |
3c2239cf | 4029 | val = truthvalue_false_node; |
b0fc3e72 | 4030 | } |
4031 | else if (code == LT_EXPR) | |
4032 | { | |
4033 | if (max_lt) | |
3c2239cf | 4034 | val = truthvalue_true_node; |
b0fc3e72 | 4035 | if (!min_lt) |
3c2239cf | 4036 | val = truthvalue_false_node; |
b0fc3e72 | 4037 | } |
4038 | else if (code == GT_EXPR) | |
4039 | { | |
4040 | if (min_gt) | |
3c2239cf | 4041 | val = truthvalue_true_node; |
b0fc3e72 | 4042 | if (!max_gt) |
3c2239cf | 4043 | val = truthvalue_false_node; |
b0fc3e72 | 4044 | } |
4045 | else if (code == LE_EXPR) | |
4046 | { | |
4047 | if (!max_gt) | |
3c2239cf | 4048 | val = truthvalue_true_node; |
b0fc3e72 | 4049 | if (min_gt) |
3c2239cf | 4050 | val = truthvalue_false_node; |
b0fc3e72 | 4051 | } |
4052 | else if (code == GE_EXPR) | |
4053 | { | |
4054 | if (!min_lt) | |
3c2239cf | 4055 | val = truthvalue_true_node; |
b0fc3e72 | 4056 | if (max_lt) |
3c2239cf | 4057 | val = truthvalue_false_node; |
b0fc3e72 | 4058 | } |
4059 | ||
4060 | /* If primop0 was sign-extended and unsigned comparison specd, | |
4061 | we did a signed comparison above using the signed type bounds. | |
4062 | But the comparison we output must be unsigned. | |
4063 | ||
4064 | Also, for inequalities, VAL is no good; but if the signed | |
4065 | comparison had *any* fixed result, it follows that the | |
4066 | unsigned comparison just tests the sign in reverse | |
4067 | (positive values are LE, negative ones GE). | |
4068 | So we can generate an unsigned comparison | |
4069 | against an extreme value of the signed type. */ | |
4070 | ||
4071 | if (unsignedp && !unsignedp0) | |
4072 | { | |
4073 | if (val != 0) | |
4074 | switch (code) | |
4075 | { | |
4076 | case LT_EXPR: | |
4077 | case GE_EXPR: | |
4078 | primop1 = TYPE_MIN_VALUE (type); | |
4079 | val = 0; | |
4080 | break; | |
4081 | ||
4082 | case LE_EXPR: | |
4083 | case GT_EXPR: | |
4084 | primop1 = TYPE_MAX_VALUE (type); | |
4085 | val = 0; | |
4086 | break; | |
31f820d2 | 4087 | |
4088 | default: | |
4089 | break; | |
b0fc3e72 | 4090 | } |
11773141 | 4091 | type = c_common_unsigned_type (type); |
b0fc3e72 | 4092 | } |
4093 | ||
2d3d3af7 | 4094 | if (TREE_CODE (primop0) != INTEGER_CST |
01f11119 | 4095 | /* Don't warn if it's from a (non-system) macro. */ |
4096 | && !(from_macro_expansion_at | |
4097 | (expansion_point_location_if_in_system_header | |
4098 | (EXPR_LOCATION (primop0))))) | |
b0fc3e72 | 4099 | { |
3c2239cf | 4100 | if (val == truthvalue_false_node) |
03fe1dc2 | 4101 | warning_at (loc, OPT_Wtype_limits, |
4102 | "comparison is always false due to limited range of data type"); | |
3c2239cf | 4103 | if (val == truthvalue_true_node) |
03fe1dc2 | 4104 | warning_at (loc, OPT_Wtype_limits, |
4105 | "comparison is always true due to limited range of data type"); | |
b0fc3e72 | 4106 | } |
4107 | ||
4108 | if (val != 0) | |
4109 | { | |
4110 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4111 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 4112 | return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); |
b0fc3e72 | 4113 | return val; |
4114 | } | |
4115 | ||
4116 | /* Value is not predetermined, but do the comparison | |
4117 | in the type of the operand that is not constant. | |
4118 | TYPE is already properly set. */ | |
4119 | } | |
c4503c0a | 4120 | |
4121 | /* If either arg is decimal float and the other is float, find the | |
4122 | proper common type to use for comparison. */ | |
7fd22aae | 4123 | else if (real1 && real2 |
4124 | && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0))) | |
4125 | && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))) | |
4126 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4127 | ||
4128 | /* If either arg is decimal float and the other is float, fail. */ | |
c4503c0a | 4129 | else if (real1 && real2 |
4130 | && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0))) | |
4131 | || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))) | |
7fd22aae | 4132 | return 0; |
c4503c0a | 4133 | |
b0fc3e72 | 4134 | else if (real1 && real2 |
2203bd5c | 4135 | && (TYPE_PRECISION (TREE_TYPE (primop0)) |
4136 | == TYPE_PRECISION (TREE_TYPE (primop1)))) | |
b0fc3e72 | 4137 | type = TREE_TYPE (primop0); |
4138 | ||
4139 | /* If args' natural types are both narrower than nominal type | |
4140 | and both extend in the same manner, compare them | |
4141 | in the type of the wider arg. | |
4142 | Otherwise must actually extend both to the nominal | |
4143 | common type lest different ways of extending | |
4144 | alter the result. | |
4145 | (eg, (short)-1 == (unsigned short)-1 should be 0.) */ | |
4146 | ||
4147 | else if (unsignedp0 == unsignedp1 && real1 == real2 | |
4148 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) | |
4149 | && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) | |
4150 | { | |
4151 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4070745f | 4152 | type = c_common_signed_or_unsigned_type (unsignedp0 |
78a8ed03 | 4153 | || TYPE_UNSIGNED (*restype_ptr), |
4070745f | 4154 | type); |
b0fc3e72 | 4155 | /* Make sure shorter operand is extended the right way |
4156 | to match the longer operand. */ | |
4070745f | 4157 | primop0 |
4158 | = convert (c_common_signed_or_unsigned_type (unsignedp0, | |
4159 | TREE_TYPE (primop0)), | |
4160 | primop0); | |
4161 | primop1 | |
4162 | = convert (c_common_signed_or_unsigned_type (unsignedp1, | |
4163 | TREE_TYPE (primop1)), | |
4164 | primop1); | |
b0fc3e72 | 4165 | } |
4166 | else | |
4167 | { | |
4168 | /* Here we must do the comparison on the nominal type | |
4169 | using the args exactly as we received them. */ | |
4170 | type = *restype_ptr; | |
4171 | primop0 = op0; | |
4172 | primop1 = op1; | |
4173 | ||
4174 | if (!real1 && !real2 && integer_zerop (primop1) | |
78a8ed03 | 4175 | && TYPE_UNSIGNED (*restype_ptr)) |
b0fc3e72 | 4176 | { |
4177 | tree value = 0; | |
03fe1dc2 | 4178 | /* All unsigned values are >= 0, so we warn. However, |
4179 | if OP0 is a constant that is >= 0, the signedness of | |
4180 | the comparison isn't an issue, so suppress the | |
4181 | warning. */ | |
4182 | bool warn = | |
3df42822 | 4183 | warn_type_limits && !in_system_header_at (loc) |
03fe1dc2 | 4184 | && !(TREE_CODE (primop0) == INTEGER_CST |
4185 | && !TREE_OVERFLOW (convert (c_common_signed_type (type), | |
4186 | primop0))) | |
4187 | /* Do not warn for enumeration types. */ | |
4188 | && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE); | |
4189 | ||
b0fc3e72 | 4190 | switch (code) |
4191 | { | |
4192 | case GE_EXPR: | |
03fe1dc2 | 4193 | if (warn) |
4194 | warning_at (loc, OPT_Wtype_limits, | |
4195 | "comparison of unsigned expression >= 0 is always true"); | |
3c2239cf | 4196 | value = truthvalue_true_node; |
b0fc3e72 | 4197 | break; |
4198 | ||
4199 | case LT_EXPR: | |
03fe1dc2 | 4200 | if (warn) |
4201 | warning_at (loc, OPT_Wtype_limits, | |
4202 | "comparison of unsigned expression < 0 is always false"); | |
3c2239cf | 4203 | value = truthvalue_false_node; |
31f820d2 | 4204 | break; |
4205 | ||
4206 | default: | |
4207 | break; | |
b0fc3e72 | 4208 | } |
4209 | ||
4210 | if (value != 0) | |
4211 | { | |
4212 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4213 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 4214 | return build2 (COMPOUND_EXPR, TREE_TYPE (value), |
4215 | primop0, value); | |
b0fc3e72 | 4216 | return value; |
4217 | } | |
4218 | } | |
4219 | } | |
4220 | ||
4221 | *op0_ptr = convert (type, primop0); | |
4222 | *op1_ptr = convert (type, primop1); | |
4223 | ||
3c2239cf | 4224 | *restype_ptr = truthvalue_type_node; |
b0fc3e72 | 4225 | |
4226 | return 0; | |
4227 | } | |
4228 | \f | |
1c26100f | 4229 | /* Return a tree for the sum or difference (RESULTCODE says which) |
4230 | of pointer PTROP and integer INTOP. */ | |
4231 | ||
4232 | tree | |
389dd41b | 4233 | pointer_int_sum (location_t loc, enum tree_code resultcode, |
42f9a786 | 4234 | tree ptrop, tree intop, bool complain) |
1c26100f | 4235 | { |
add6ee5e | 4236 | tree size_exp, ret; |
1c26100f | 4237 | |
1c26100f | 4238 | /* The result is a pointer of the same type that is being added. */ |
1c26100f | 4239 | tree result_type = TREE_TYPE (ptrop); |
4240 | ||
4241 | if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) | |
4242 | { | |
42f9a786 | 4243 | if (complain && warn_pointer_arith) |
4244 | pedwarn (loc, OPT_Wpointer_arith, | |
4245 | "pointer of type %<void *%> used in arithmetic"); | |
4246 | else if (!complain) | |
4247 | return error_mark_node; | |
1c26100f | 4248 | size_exp = integer_one_node; |
4249 | } | |
4250 | else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) | |
4251 | { | |
42f9a786 | 4252 | if (complain && warn_pointer_arith) |
4253 | pedwarn (loc, OPT_Wpointer_arith, | |
4254 | "pointer to a function used in arithmetic"); | |
4255 | else if (!complain) | |
4256 | return error_mark_node; | |
1c26100f | 4257 | size_exp = integer_one_node; |
4258 | } | |
1c26100f | 4259 | else |
4260 | size_exp = size_in_bytes (TREE_TYPE (result_type)); | |
4261 | ||
add6ee5e | 4262 | /* We are manipulating pointer values, so we don't need to warn |
4263 | about relying on undefined signed overflow. We disable the | |
4264 | warning here because we use integer types so fold won't know that | |
4265 | they are really pointers. */ | |
4266 | fold_defer_overflow_warnings (); | |
4267 | ||
1c26100f | 4268 | /* If what we are about to multiply by the size of the elements |
4269 | contains a constant term, apply distributive law | |
4270 | and multiply that constant term separately. | |
4271 | This helps produce common subexpressions. */ | |
1c26100f | 4272 | if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) |
84166705 | 4273 | && !TREE_CONSTANT (intop) |
1c26100f | 4274 | && TREE_CONSTANT (TREE_OPERAND (intop, 1)) |
4275 | && TREE_CONSTANT (size_exp) | |
4276 | /* If the constant comes from pointer subtraction, | |
4277 | skip this optimization--it would cause an error. */ | |
4278 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE | |
4279 | /* If the constant is unsigned, and smaller than the pointer size, | |
4280 | then we must skip this optimization. This is because it could cause | |
4281 | an overflow error if the constant is negative but INTOP is not. */ | |
84166705 | 4282 | && (!TYPE_UNSIGNED (TREE_TYPE (intop)) |
1c26100f | 4283 | || (TYPE_PRECISION (TREE_TYPE (intop)) |
4284 | == TYPE_PRECISION (TREE_TYPE (ptrop))))) | |
4285 | { | |
4286 | enum tree_code subcode = resultcode; | |
4287 | tree int_type = TREE_TYPE (intop); | |
4288 | if (TREE_CODE (intop) == MINUS_EXPR) | |
4289 | subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); | |
4290 | /* Convert both subexpression types to the type of intop, | |
4291 | because weird cases involving pointer arithmetic | |
4292 | can result in a sum or difference with different type args. */ | |
8e70fb09 | 4293 | ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)), |
4294 | subcode, ptrop, | |
1c26100f | 4295 | convert (int_type, TREE_OPERAND (intop, 1)), 1); |
4296 | intop = convert (int_type, TREE_OPERAND (intop, 0)); | |
4297 | } | |
4298 | ||
4299 | /* Convert the integer argument to a type the same size as sizetype | |
4300 | so the multiply won't overflow spuriously. */ | |
1c26100f | 4301 | if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) |
78a8ed03 | 4302 | || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype)) |
1cae46be | 4303 | intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype), |
78a8ed03 | 4304 | TYPE_UNSIGNED (sizetype)), intop); |
1c26100f | 4305 | |
4306 | /* Replace the integer argument with a suitable product by the object size. | |
c6feb9f1 | 4307 | Do this multiplication as signed, then convert to the appropriate type |
8032877c | 4308 | for the pointer operation and disregard an overflow that occurred only |
c6feb9f1 | 4309 | because of the sign-extension change in the latter conversion. */ |
4310 | { | |
f576a2e4 | 4311 | tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop, |
4312 | convert (TREE_TYPE (intop), size_exp)); | |
c6feb9f1 | 4313 | intop = convert (sizetype, t); |
4314 | if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t)) | |
e913b5cd | 4315 | intop = wide_int_to_tree (TREE_TYPE (intop), intop); |
c6feb9f1 | 4316 | } |
0de36bdb | 4317 | |
499e523f | 4318 | /* Create the sum or difference. */ |
0de36bdb | 4319 | if (resultcode == MINUS_EXPR) |
389dd41b | 4320 | intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop); |
1c26100f | 4321 | |
2cc66f2a | 4322 | ret = fold_build_pointer_plus_loc (loc, ptrop, intop); |
add6ee5e | 4323 | |
4324 | fold_undefer_and_ignore_overflow_warnings (); | |
4325 | ||
4326 | return ret; | |
1c26100f | 4327 | } |
4328 | \f | |
f59e3889 | 4329 | /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded |
4330 | and if NON_CONST is known not to be permitted in an evaluated part | |
4331 | of a constant expression. */ | |
4332 | ||
4333 | tree | |
4334 | c_wrap_maybe_const (tree expr, bool non_const) | |
4335 | { | |
4336 | bool nowarning = TREE_NO_WARNING (expr); | |
4337 | location_t loc = EXPR_LOCATION (expr); | |
4338 | ||
4339 | /* This should never be called for C++. */ | |
4340 | if (c_dialect_cxx ()) | |
4341 | gcc_unreachable (); | |
4342 | ||
4343 | /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */ | |
4344 | STRIP_TYPE_NOPS (expr); | |
4345 | expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr); | |
4346 | C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const; | |
4347 | if (nowarning) | |
4348 | TREE_NO_WARNING (expr) = 1; | |
4349 | protected_set_expr_location (expr, loc); | |
4350 | ||
4351 | return expr; | |
4352 | } | |
4353 | ||
a75b1c71 | 4354 | /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but |
4355 | for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR | |
4356 | around the SAVE_EXPR if needed so that c_fully_fold does not need | |
4357 | to look inside SAVE_EXPRs. */ | |
4358 | ||
4359 | tree | |
4360 | c_save_expr (tree expr) | |
4361 | { | |
4362 | bool maybe_const = true; | |
4363 | if (c_dialect_cxx ()) | |
4364 | return save_expr (expr); | |
4365 | expr = c_fully_fold (expr, false, &maybe_const); | |
4366 | expr = save_expr (expr); | |
4367 | if (!maybe_const) | |
f59e3889 | 4368 | expr = c_wrap_maybe_const (expr, true); |
a75b1c71 | 4369 | return expr; |
4370 | } | |
4371 | ||
6b68e71a | 4372 | /* Return whether EXPR is a declaration whose address can never be |
4373 | NULL. */ | |
4374 | ||
4375 | bool | |
9f627b1a | 4376 | decl_with_nonnull_addr_p (const_tree expr) |
6b68e71a | 4377 | { |
4378 | return (DECL_P (expr) | |
4379 | && (TREE_CODE (expr) == PARM_DECL | |
4380 | || TREE_CODE (expr) == LABEL_DECL | |
4381 | || !DECL_WEAK (expr))); | |
4382 | } | |
4383 | ||
b0fc3e72 | 4384 | /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, |
45a78cc0 | 4385 | or for an `if' or `while' statement or ?..: exp. It should already |
4386 | have been validated to be of suitable type; otherwise, a bad | |
4387 | diagnostic may result. | |
b0fc3e72 | 4388 | |
8e70fb09 | 4389 | The EXPR is located at LOCATION. |
4390 | ||
b0fc3e72 | 4391 | This preparation consists of taking the ordinary |
4392 | representation of an expression expr and producing a valid tree | |
4393 | boolean expression describing whether expr is nonzero. We could | |
3c2239cf | 4394 | simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1), |
b0fc3e72 | 4395 | but we optimize comparisons, &&, ||, and !. |
4396 | ||
3c2239cf | 4397 | The resulting type should always be `truthvalue_type_node'. */ |
b0fc3e72 | 4398 | |
4399 | tree | |
8e70fb09 | 4400 | c_common_truthvalue_conversion (location_t location, tree expr) |
b0fc3e72 | 4401 | { |
b0fc3e72 | 4402 | switch (TREE_CODE (expr)) |
4403 | { | |
318a728f | 4404 | case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR: |
e1232ce2 | 4405 | case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: |
4406 | case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR: | |
4407 | case ORDERED_EXPR: case UNORDERED_EXPR: | |
ce04dcdc | 4408 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4409 | return expr; | |
e60a6f7b | 4410 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
ce04dcdc | 4411 | TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); |
e60a6f7b | 4412 | goto ret; |
ce04dcdc | 4413 | |
b0fc3e72 | 4414 | case TRUTH_ANDIF_EXPR: |
4415 | case TRUTH_ORIF_EXPR: | |
4416 | case TRUTH_AND_EXPR: | |
4417 | case TRUTH_OR_EXPR: | |
31f6e93c | 4418 | case TRUTH_XOR_EXPR: |
ce04dcdc | 4419 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4420 | return expr; | |
e60a6f7b | 4421 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
48e1416a | 4422 | c_common_truthvalue_conversion (location, |
e60a6f7b | 4423 | TREE_OPERAND (expr, 0)), |
4424 | c_common_truthvalue_conversion (location, | |
4425 | TREE_OPERAND (expr, 1))); | |
4426 | goto ret; | |
3e851b85 | 4427 | |
f6e28f72 | 4428 | case TRUTH_NOT_EXPR: |
ce04dcdc | 4429 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4430 | return expr; | |
e60a6f7b | 4431 | expr = build1 (TREE_CODE (expr), truthvalue_type_node, |
4432 | c_common_truthvalue_conversion (location, | |
4433 | TREE_OPERAND (expr, 0))); | |
4434 | goto ret; | |
f6e28f72 | 4435 | |
b0fc3e72 | 4436 | case ERROR_MARK: |
4437 | return expr; | |
ab2c1de8 | 4438 | |
b0fc3e72 | 4439 | case INTEGER_CST: |
eddad94a | 4440 | return integer_zerop (expr) ? truthvalue_false_node |
4441 | : truthvalue_true_node; | |
b0fc3e72 | 4442 | |
4443 | case REAL_CST: | |
5000e21c | 4444 | return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0) |
4445 | ? truthvalue_true_node | |
4446 | : truthvalue_false_node; | |
b0fc3e72 | 4447 | |
9421ebb9 | 4448 | case FIXED_CST: |
4449 | return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr), | |
4450 | &FCONST0 (TYPE_MODE (TREE_TYPE (expr)))) | |
4451 | ? truthvalue_true_node | |
4452 | : truthvalue_false_node; | |
4453 | ||
ce04dcdc | 4454 | case FUNCTION_DECL: |
b6889cb0 | 4455 | expr = build_unary_op (location, ADDR_EXPR, expr, 0); |
ce04dcdc | 4456 | /* Fall through. */ |
4457 | ||
b0fc3e72 | 4458 | case ADDR_EXPR: |
ce871053 | 4459 | { |
ee7d4d6a | 4460 | tree inner = TREE_OPERAND (expr, 0); |
6b68e71a | 4461 | if (decl_with_nonnull_addr_p (inner)) |
ce871053 | 4462 | { |
6b68e71a | 4463 | /* Common Ada/Pascal programmer's mistake. */ |
8e70fb09 | 4464 | warning_at (location, |
4465 | OPT_Waddress, | |
4466 | "the address of %qD will always evaluate as %<true%>", | |
4467 | inner); | |
ce871053 | 4468 | return truthvalue_true_node; |
4469 | } | |
d473d901 | 4470 | break; |
ce871053 | 4471 | } |
b0fc3e72 | 4472 | |
2203bd5c | 4473 | case COMPLEX_EXPR: |
e60a6f7b | 4474 | expr = build_binary_op (EXPR_LOCATION (expr), |
8e70fb09 | 4475 | (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) |
95809de4 | 4476 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
8e70fb09 | 4477 | c_common_truthvalue_conversion (location, |
4478 | TREE_OPERAND (expr, 0)), | |
4479 | c_common_truthvalue_conversion (location, | |
4480 | TREE_OPERAND (expr, 1)), | |
2203bd5c | 4481 | 0); |
e60a6f7b | 4482 | goto ret; |
2203bd5c | 4483 | |
b0fc3e72 | 4484 | case NEGATE_EXPR: |
4485 | case ABS_EXPR: | |
4486 | case FLOAT_EXPR: | |
c6418a4e | 4487 | case EXCESS_PRECISION_EXPR: |
d10cfa8d | 4488 | /* These don't change whether an object is nonzero or zero. */ |
8e70fb09 | 4489 | return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); |
b0fc3e72 | 4490 | |
4491 | case LROTATE_EXPR: | |
4492 | case RROTATE_EXPR: | |
d10cfa8d | 4493 | /* These don't change whether an object is zero or nonzero, but |
b0fc3e72 | 4494 | we can't ignore them if their second arg has side-effects. */ |
4495 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) | |
e60a6f7b | 4496 | { |
4497 | expr = build2 (COMPOUND_EXPR, truthvalue_type_node, | |
4498 | TREE_OPERAND (expr, 1), | |
48e1416a | 4499 | c_common_truthvalue_conversion |
e60a6f7b | 4500 | (location, TREE_OPERAND (expr, 0))); |
4501 | goto ret; | |
4502 | } | |
b0fc3e72 | 4503 | else |
8e70fb09 | 4504 | return c_common_truthvalue_conversion (location, |
4505 | TREE_OPERAND (expr, 0)); | |
73be5127 | 4506 | |
b0fc3e72 | 4507 | case COND_EXPR: |
4508 | /* Distribute the conversion into the arms of a COND_EXPR. */ | |
a75b1c71 | 4509 | if (c_dialect_cxx ()) |
e60a6f7b | 4510 | { |
d0389adc | 4511 | tree op1 = TREE_OPERAND (expr, 1); |
4512 | tree op2 = TREE_OPERAND (expr, 2); | |
4513 | /* In C++ one of the arms might have void type if it is throw. */ | |
4514 | if (!VOID_TYPE_P (TREE_TYPE (op1))) | |
4515 | op1 = c_common_truthvalue_conversion (location, op1); | |
4516 | if (!VOID_TYPE_P (TREE_TYPE (op2))) | |
4517 | op2 = c_common_truthvalue_conversion (location, op2); | |
389dd41b | 4518 | expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node, |
d0389adc | 4519 | TREE_OPERAND (expr, 0), op1, op2); |
e60a6f7b | 4520 | goto ret; |
4521 | } | |
a75b1c71 | 4522 | else |
e60a6f7b | 4523 | { |
4524 | /* Folding will happen later for C. */ | |
4525 | expr = build3 (COND_EXPR, truthvalue_type_node, | |
4526 | TREE_OPERAND (expr, 0), | |
4527 | c_common_truthvalue_conversion (location, | |
4528 | TREE_OPERAND (expr, 1)), | |
4529 | c_common_truthvalue_conversion (location, | |
4530 | TREE_OPERAND (expr, 2))); | |
4531 | goto ret; | |
4532 | } | |
b0fc3e72 | 4533 | |
72dd6141 | 4534 | CASE_CONVERT: |
f8913d47 | 4535 | { |
4536 | tree totype = TREE_TYPE (expr); | |
4537 | tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0)); | |
4538 | ||
db3d1ffc | 4539 | if (POINTER_TYPE_P (totype) |
4540 | && TREE_CODE (fromtype) == REFERENCE_TYPE) | |
4541 | { | |
4542 | tree inner = expr; | |
4543 | STRIP_NOPS (inner); | |
4544 | ||
4545 | if (DECL_P (inner)) | |
4546 | warning_at (location, | |
4547 | OPT_Waddress, | |
4548 | "the compiler can assume that the address of " | |
4549 | "%qD will always evaluate to %<true%>", | |
4550 | inner); | |
4551 | } | |
4552 | ||
f8913d47 | 4553 | /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, |
4554 | since that affects how `default_conversion' will behave. */ | |
4555 | if (TREE_CODE (totype) == REFERENCE_TYPE | |
4556 | || TREE_CODE (fromtype) == REFERENCE_TYPE) | |
4557 | break; | |
4558 | /* Don't strip a conversion from C++0x scoped enum, since they | |
4559 | don't implicitly convert to other types. */ | |
4560 | if (TREE_CODE (fromtype) == ENUMERAL_TYPE | |
4561 | && ENUM_IS_SCOPED (fromtype)) | |
4562 | break; | |
4563 | /* If this isn't narrowing the argument, we can ignore it. */ | |
4564 | if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype)) | |
4565 | return c_common_truthvalue_conversion (location, | |
4566 | TREE_OPERAND (expr, 0)); | |
4567 | } | |
b0fc3e72 | 4568 | break; |
4569 | ||
16837b18 | 4570 | case MODIFY_EXPR: |
60a0513e | 4571 | if (!TREE_NO_WARNING (expr) |
4572 | && warn_parentheses) | |
4573 | { | |
4574 | warning (OPT_Wparentheses, | |
4575 | "suggest parentheses around assignment used as truth value"); | |
4576 | TREE_NO_WARNING (expr) = 1; | |
4577 | } | |
16837b18 | 4578 | break; |
73be5127 | 4579 | |
31f820d2 | 4580 | default: |
4581 | break; | |
b0fc3e72 | 4582 | } |
4583 | ||
2ba726d2 | 4584 | if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) |
a0748b7d | 4585 | { |
93be21c0 | 4586 | tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr)); |
e60a6f7b | 4587 | expr = (build_binary_op |
8e70fb09 | 4588 | (EXPR_LOCATION (expr), |
4589 | (TREE_SIDE_EFFECTS (expr) | |
a0748b7d | 4590 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
b6889cb0 | 4591 | c_common_truthvalue_conversion |
4592 | (location, | |
4593 | build_unary_op (location, REALPART_EXPR, t, 0)), | |
4594 | c_common_truthvalue_conversion | |
4595 | (location, | |
4596 | build_unary_op (location, IMAGPART_EXPR, t, 0)), | |
a0748b7d | 4597 | 0)); |
e60a6f7b | 4598 | goto ret; |
a0748b7d | 4599 | } |
2ba726d2 | 4600 | |
9421ebb9 | 4601 | if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE) |
4602 | { | |
4603 | tree fixed_zero_node = build_fixed (TREE_TYPE (expr), | |
4604 | FCONST0 (TYPE_MODE | |
4605 | (TREE_TYPE (expr)))); | |
43158006 | 4606 | return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1); |
9421ebb9 | 4607 | } |
e60a6f7b | 4608 | else |
4609 | return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1); | |
9421ebb9 | 4610 | |
e60a6f7b | 4611 | ret: |
4612 | protected_set_expr_location (expr, location); | |
4613 | return expr; | |
b0fc3e72 | 4614 | } |
4615 | \f | |
3237155d | 4616 | static void def_builtin_1 (enum built_in_function fncode, |
4617 | const char *name, | |
4618 | enum built_in_class fnclass, | |
4619 | tree fntype, tree libtype, | |
4620 | bool both_p, bool fallback_p, bool nonansi_p, | |
4621 | tree fnattrs, bool implicit_p); | |
0d4238dc | 4622 | |
a5b1863e | 4623 | |
4624 | /* Apply the TYPE_QUALS to the new DECL. */ | |
4625 | ||
4626 | void | |
1cae46be | 4627 | c_apply_type_quals_to_decl (int type_quals, tree decl) |
a5b1863e | 4628 | { |
adfb367f | 4629 | tree type = TREE_TYPE (decl); |
b27ac6b5 | 4630 | |
e4eabbe4 | 4631 | if (type == error_mark_node) |
4632 | return; | |
adfb367f | 4633 | |
98a33d9f | 4634 | if ((type_quals & TYPE_QUAL_CONST) |
4635 | || (type && TREE_CODE (type) == REFERENCE_TYPE)) | |
4636 | /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr | |
4637 | constructor can produce constant init, so rely on cp_finish_decl to | |
4638 | clear TREE_READONLY if the variable has non-constant init. */ | |
a5b1863e | 4639 | TREE_READONLY (decl) = 1; |
4640 | if (type_quals & TYPE_QUAL_VOLATILE) | |
4641 | { | |
4642 | TREE_SIDE_EFFECTS (decl) = 1; | |
4643 | TREE_THIS_VOLATILE (decl) = 1; | |
4644 | } | |
d91a20bc | 4645 | if (type_quals & TYPE_QUAL_RESTRICT) |
a5b1863e | 4646 | { |
adfb367f | 4647 | while (type && TREE_CODE (type) == ARRAY_TYPE) |
4648 | /* Allow 'restrict' on arrays of pointers. | |
4649 | FIXME currently we just ignore it. */ | |
4650 | type = TREE_TYPE (type); | |
4651 | if (!type | |
4652 | || !POINTER_TYPE_P (type) | |
4653 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))) | |
b0b1af64 | 4654 | error ("invalid use of %<restrict%>"); |
a5b1863e | 4655 | } |
4656 | } | |
4657 | ||
b594087e | 4658 | struct c_type_hasher : ggc_ptr_hash<tree_node> |
2ef51f0e | 4659 | { |
4660 | static hashval_t hash (tree); | |
4661 | static bool equal (tree, tree); | |
4662 | }; | |
4663 | ||
4ee9c684 | 4664 | /* Hash function for the problem of multiple type definitions in |
4665 | different files. This must hash all types that will compare | |
4666 | equal via comptypes to the same value. In practice it hashes | |
0bed3869 | 4667 | on some of the simple stuff and leaves the details to comptypes. */ |
4ee9c684 | 4668 | |
2ef51f0e | 4669 | hashval_t |
4670 | c_type_hasher::hash (tree t) | |
4ee9c684 | 4671 | { |
ecf2703d | 4672 | int n_elements; |
4ee9c684 | 4673 | int shift, size; |
4ee9c684 | 4674 | tree t2; |
4675 | switch (TREE_CODE (t)) | |
4676 | { | |
fbf0afd1 | 4677 | /* For pointers, hash on pointee type plus some swizzling. */ |
2363ef00 | 4678 | case POINTER_TYPE: |
2ef51f0e | 4679 | return hash (TREE_TYPE (t)) ^ 0x3003003; |
2363ef00 | 4680 | /* Hash on number of elements and total size. */ |
4681 | case ENUMERAL_TYPE: | |
4682 | shift = 3; | |
4683 | t2 = TYPE_VALUES (t); | |
4684 | break; | |
4685 | case RECORD_TYPE: | |
4686 | shift = 0; | |
4687 | t2 = TYPE_FIELDS (t); | |
4688 | break; | |
4689 | case QUAL_UNION_TYPE: | |
4690 | shift = 1; | |
4691 | t2 = TYPE_FIELDS (t); | |
4692 | break; | |
4693 | case UNION_TYPE: | |
4694 | shift = 2; | |
4695 | t2 = TYPE_FIELDS (t); | |
4696 | break; | |
4697 | default: | |
231bd014 | 4698 | gcc_unreachable (); |
4ee9c684 | 4699 | } |
ecf2703d | 4700 | /* FIXME: We want to use a DECL_CHAIN iteration method here, but |
4701 | TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */ | |
4702 | n_elements = list_length (t2); | |
78c2e180 | 4703 | /* We might have a VLA here. */ |
4704 | if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST) | |
4705 | size = 0; | |
4706 | else | |
f9ae6f95 | 4707 | size = TREE_INT_CST_LOW (TYPE_SIZE (t)); |
ecf2703d | 4708 | return ((size << 24) | (n_elements << shift)); |
4ee9c684 | 4709 | } |
4710 | ||
2ef51f0e | 4711 | bool |
4712 | c_type_hasher::equal (tree t1, tree t2) | |
4713 | { | |
4714 | return lang_hooks.types_compatible_p (t1, t2); | |
4715 | } | |
4716 | ||
4717 | static GTY(()) hash_table<c_type_hasher> *type_hash_table; | |
1ecd4018 | 4718 | |
b5ba9f3a | 4719 | /* Return the typed-based alias set for T, which may be an expression |
f7c44134 | 4720 | or a type. Return -1 if we don't do anything special. */ |
ab2c1de8 | 4721 | |
32c2fdea | 4722 | alias_set_type |
1cae46be | 4723 | c_common_get_alias_set (tree t) |
b5ba9f3a | 4724 | { |
be4f2de7 | 4725 | tree u; |
1cae46be | 4726 | |
e58c17e7 | 4727 | /* For VLAs, use the alias set of the element type rather than the |
4728 | default of alias set 0 for types compared structurally. */ | |
4729 | if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t)) | |
4730 | { | |
4731 | if (TREE_CODE (t) == ARRAY_TYPE) | |
4732 | return get_alias_set (TREE_TYPE (t)); | |
4733 | return -1; | |
4734 | } | |
4735 | ||
be4f2de7 | 4736 | /* Permit type-punning when accessing a union, provided the access |
4737 | is directly through the union. For example, this code does not | |
4738 | permit taking the address of a union member and then storing | |
4739 | through it. Even the type-punning allowed here is a GCC | |
4740 | extension, albeit a common and useful one; the C standard says | |
4741 | that such accesses have implementation-defined behavior. */ | |
4742 | for (u = t; | |
4743 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
4744 | u = TREE_OPERAND (u, 0)) | |
4745 | if (TREE_CODE (u) == COMPONENT_REF | |
4746 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
4747 | return 0; | |
1e2513d9 | 4748 | |
9fcc3e54 | 4749 | /* That's all the expressions we handle specially. */ |
84166705 | 4750 | if (!TYPE_P (t)) |
9fcc3e54 | 4751 | return -1; |
4752 | ||
d716ce75 | 4753 | /* The C standard guarantees that any object may be accessed via an |
9fcc3e54 | 4754 | lvalue that has character type. */ |
4755 | if (t == char_type_node | |
4756 | || t == signed_char_type_node | |
4757 | || t == unsigned_char_type_node) | |
f7c44134 | 4758 | return 0; |
a5b1863e | 4759 | |
1607663f | 4760 | /* The C standard specifically allows aliasing between signed and |
4761 | unsigned variants of the same type. We treat the signed | |
4762 | variant as canonical. */ | |
78a8ed03 | 4763 | if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t)) |
a8868e19 | 4764 | { |
4070745f | 4765 | tree t1 = c_common_signed_type (t); |
1607663f | 4766 | |
a8868e19 | 4767 | /* t1 == t can happen for boolean nodes which are always unsigned. */ |
4768 | if (t1 != t) | |
4769 | return get_alias_set (t1); | |
4770 | } | |
1e2513d9 | 4771 | |
4ee9c684 | 4772 | /* Handle the case of multiple type nodes referring to "the same" type, |
4773 | which occurs with IMA. These share an alias set. FIXME: Currently only | |
4774 | C90 is handled. (In C99 type compatibility is not transitive, which | |
4775 | complicates things mightily. The alias set splay trees can theoretically | |
4776 | represent this, but insertion is tricky when you consider all the | |
4777 | different orders things might arrive in.) */ | |
4778 | ||
4779 | if (c_language != clk_c || flag_isoc99) | |
4780 | return -1; | |
4781 | ||
0bed3869 | 4782 | /* Save time if there's only one input file. */ |
e08bd2f4 | 4783 | if (num_in_fnames == 1) |
4ee9c684 | 4784 | return -1; |
4785 | ||
4786 | /* Pointers need special handling if they point to any type that | |
4787 | needs special handling (below). */ | |
4788 | if (TREE_CODE (t) == POINTER_TYPE) | |
4789 | { | |
4790 | tree t2; | |
4791 | /* Find bottom type under any nested POINTERs. */ | |
b27ac6b5 | 4792 | for (t2 = TREE_TYPE (t); |
af592f67 | 4793 | TREE_CODE (t2) == POINTER_TYPE; |
4794 | t2 = TREE_TYPE (t2)) | |
4795 | ; | |
8e539fdc | 4796 | if (!RECORD_OR_UNION_TYPE_P (t2) |
4797 | && TREE_CODE (t2) != ENUMERAL_TYPE) | |
af592f67 | 4798 | return -1; |
4ee9c684 | 4799 | if (TYPE_SIZE (t2) == 0) |
af592f67 | 4800 | return -1; |
4ee9c684 | 4801 | } |
4802 | /* These are the only cases that need special handling. */ | |
8e539fdc | 4803 | if (!RECORD_OR_UNION_TYPE_P (t) |
4ee9c684 | 4804 | && TREE_CODE (t) != ENUMERAL_TYPE |
4ee9c684 | 4805 | && TREE_CODE (t) != POINTER_TYPE) |
4806 | return -1; | |
4807 | /* Undefined? */ | |
4808 | if (TYPE_SIZE (t) == 0) | |
4809 | return -1; | |
4810 | ||
b27ac6b5 | 4811 | /* Look up t in hash table. Only one of the compatible types within each |
4ee9c684 | 4812 | alias set is recorded in the table. */ |
4813 | if (!type_hash_table) | |
2ef51f0e | 4814 | type_hash_table = hash_table<c_type_hasher>::create_ggc (1021); |
4815 | tree *slot = type_hash_table->find_slot (t, INSERT); | |
4ee9c684 | 4816 | if (*slot != NULL) |
ad16cb2c | 4817 | { |
4818 | TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot); | |
4819 | return TYPE_ALIAS_SET ((tree)*slot); | |
4820 | } | |
4ee9c684 | 4821 | else |
4822 | /* Our caller will assign and record (in t) a new alias set; all we need | |
4823 | to do is remember t in the hash table. */ | |
4824 | *slot = t; | |
4825 | ||
f7c44134 | 4826 | return -1; |
b5ba9f3a | 4827 | } |
902b4e01 | 4828 | \f |
e60a6f7b | 4829 | /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where |
a179a7dc | 4830 | the IS_SIZEOF parameter indicates which operator is being applied. |
e60a6f7b | 4831 | The COMPLAIN flag controls whether we should diagnose possibly |
4832 | ill-formed constructs or not. LOC is the location of the SIZEOF or | |
a179a7dc | 4833 | TYPEOF operator. If MIN_ALIGNOF, the least alignment required for |
4834 | a type in any context should be returned, rather than the normal | |
4835 | alignment for that type. */ | |
908c697e | 4836 | |
902b4e01 | 4837 | tree |
e60a6f7b | 4838 | c_sizeof_or_alignof_type (location_t loc, |
a179a7dc | 4839 | tree type, bool is_sizeof, bool min_alignof, |
4840 | int complain) | |
902b4e01 | 4841 | { |
d4c4d95c | 4842 | const char *op_name; |
4843 | tree value = NULL; | |
4844 | enum tree_code type_code = TREE_CODE (type); | |
1cae46be | 4845 | |
908c697e | 4846 | op_name = is_sizeof ? "sizeof" : "__alignof__"; |
1cae46be | 4847 | |
d4c4d95c | 4848 | if (type_code == FUNCTION_TYPE) |
902b4e01 | 4849 | { |
908c697e | 4850 | if (is_sizeof) |
d4c4d95c | 4851 | { |
9205a6cc | 4852 | if (complain && warn_pointer_arith) |
4853 | pedwarn (loc, OPT_Wpointer_arith, | |
8864917d | 4854 | "invalid application of %<sizeof%> to a function type"); |
ebd21de4 | 4855 | else if (!complain) |
4856 | return error_mark_node; | |
d4c4d95c | 4857 | value = size_one_node; |
4858 | } | |
4859 | else | |
83e25171 | 4860 | { |
4861 | if (complain) | |
4862 | { | |
4863 | if (c_dialect_cxx ()) | |
29438999 | 4864 | pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit " |
83e25171 | 4865 | "%<alignof%> applied to a function type"); |
4866 | else | |
29438999 | 4867 | pedwarn (loc, OPT_Wpedantic, "ISO C does not permit " |
83e25171 | 4868 | "%<_Alignof%> applied to a function type"); |
4869 | } | |
4870 | value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); | |
4871 | } | |
d4c4d95c | 4872 | } |
4873 | else if (type_code == VOID_TYPE || type_code == ERROR_MARK) | |
4874 | { | |
1cae46be | 4875 | if (type_code == VOID_TYPE |
9205a6cc | 4876 | && complain && warn_pointer_arith) |
4877 | pedwarn (loc, OPT_Wpointer_arith, | |
8864917d | 4878 | "invalid application of %qs to a void type", op_name); |
ebd21de4 | 4879 | else if (!complain) |
4880 | return error_mark_node; | |
d4c4d95c | 4881 | value = size_one_node; |
902b4e01 | 4882 | } |
3df19e1b | 4883 | else if (!COMPLETE_TYPE_P (type) |
4884 | && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE)) | |
902b4e01 | 4885 | { |
d3a4d008 | 4886 | if (complain) |
3df19e1b | 4887 | error_at (loc, "invalid application of %qs to incomplete type %qT", |
e60a6f7b | 4888 | op_name, type); |
9c719c74 | 4889 | return error_mark_node; |
902b4e01 | 4890 | } |
3df19e1b | 4891 | else if (c_dialect_cxx () && type_code == ARRAY_TYPE |
4892 | && !COMPLETE_TYPE_P (TREE_TYPE (type))) | |
4893 | { | |
4894 | if (complain) | |
4895 | error_at (loc, "invalid application of %qs to array type %qT of " | |
4896 | "incomplete element type", op_name, type); | |
4897 | return error_mark_node; | |
4898 | } | |
902b4e01 | 4899 | else |
d4c4d95c | 4900 | { |
908c697e | 4901 | if (is_sizeof) |
d4c4d95c | 4902 | /* Convert in case a char is more than one unit. */ |
389dd41b | 4903 | value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
4904 | size_int (TYPE_PRECISION (char_type_node) | |
4905 | / BITS_PER_UNIT)); | |
a179a7dc | 4906 | else if (min_alignof) |
c2c4ae8d | 4907 | value = size_int (min_align_of_type (type)); |
d4c4d95c | 4908 | else |
d37625c0 | 4909 | value = size_int (TYPE_ALIGN_UNIT (type)); |
d4c4d95c | 4910 | } |
902b4e01 | 4911 | |
5a1fe2db | 4912 | /* VALUE will have the middle-end integer type sizetype. |
4913 | However, we should really return a value of type `size_t', | |
4914 | which is just a typedef for an ordinary integer type. */ | |
389dd41b | 4915 | value = fold_convert_loc (loc, size_type_node, value); |
1cae46be | 4916 | |
d4c4d95c | 4917 | return value; |
902b4e01 | 4918 | } |
4919 | ||
4920 | /* Implement the __alignof keyword: Return the minimum required | |
097b5c8b | 4921 | alignment of EXPR, measured in bytes. For VAR_DECLs, |
4922 | FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set | |
e60a6f7b | 4923 | from an "aligned" __attribute__ specification). LOC is the |
4924 | location of the ALIGNOF operator. */ | |
72040e7e | 4925 | |
902b4e01 | 4926 | tree |
e60a6f7b | 4927 | c_alignof_expr (location_t loc, tree expr) |
902b4e01 | 4928 | { |
4929 | tree t; | |
4930 | ||
097b5c8b | 4931 | if (VAR_OR_FUNCTION_DECL_P (expr)) |
d37625c0 | 4932 | t = size_int (DECL_ALIGN_UNIT (expr)); |
1cae46be | 4933 | |
902b4e01 | 4934 | else if (TREE_CODE (expr) == COMPONENT_REF |
4935 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
4936 | { | |
e60a6f7b | 4937 | error_at (loc, "%<__alignof%> applied to a bit-field"); |
902b4e01 | 4938 | t = size_one_node; |
4939 | } | |
4940 | else if (TREE_CODE (expr) == COMPONENT_REF | |
7cc7e163 | 4941 | && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) |
d37625c0 | 4942 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1))); |
1cae46be | 4943 | |
aa3e402e | 4944 | else if (INDIRECT_REF_P (expr)) |
902b4e01 | 4945 | { |
4946 | tree t = TREE_OPERAND (expr, 0); | |
4947 | tree best = t; | |
4948 | int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
1cae46be | 4949 | |
72dd6141 | 4950 | while (CONVERT_EXPR_P (t) |
7cc7e163 | 4951 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) |
902b4e01 | 4952 | { |
4953 | int thisalign; | |
4954 | ||
4955 | t = TREE_OPERAND (t, 0); | |
4956 | thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
4957 | if (thisalign > bestalign) | |
4958 | best = t, bestalign = thisalign; | |
4959 | } | |
e60a6f7b | 4960 | return c_alignof (loc, TREE_TYPE (TREE_TYPE (best))); |
902b4e01 | 4961 | } |
4962 | else | |
e60a6f7b | 4963 | return c_alignof (loc, TREE_TYPE (expr)); |
902b4e01 | 4964 | |
389dd41b | 4965 | return fold_convert_loc (loc, size_type_node, t); |
902b4e01 | 4966 | } |
4967 | \f | |
8fe4a266 | 4968 | /* Handle C and C++ default attributes. */ |
4969 | ||
4970 | enum built_in_attribute | |
4971 | { | |
4972 | #define DEF_ATTR_NULL_TREE(ENUM) ENUM, | |
4973 | #define DEF_ATTR_INT(ENUM, VALUE) ENUM, | |
c8010b80 | 4974 | #define DEF_ATTR_STRING(ENUM, VALUE) ENUM, |
8fe4a266 | 4975 | #define DEF_ATTR_IDENT(ENUM, STRING) ENUM, |
4976 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM, | |
8fe4a266 | 4977 | #include "builtin-attrs.def" |
4978 | #undef DEF_ATTR_NULL_TREE | |
4979 | #undef DEF_ATTR_INT | |
c8010b80 | 4980 | #undef DEF_ATTR_STRING |
8fe4a266 | 4981 | #undef DEF_ATTR_IDENT |
4982 | #undef DEF_ATTR_TREE_LIST | |
8fe4a266 | 4983 | ATTR_LAST |
4984 | }; | |
4985 | ||
4986 | static GTY(()) tree built_in_attributes[(int) ATTR_LAST]; | |
4987 | ||
1cae46be | 4988 | static void c_init_attributes (void); |
8fe4a266 | 4989 | |
27213ba3 | 4990 | enum c_builtin_type |
72040e7e | 4991 | { |
d2d4bdde | 4992 | #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME, |
4993 | #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME, | |
4994 | #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME, | |
4995 | #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME, | |
4996 | #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, | |
4997 | #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, | |
0a39fd54 | 4998 | #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME, |
3c77ca67 | 4999 | #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5000 | ARG6) NAME, | |
5001 | #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5002 | ARG6, ARG7) NAME, | |
5003 | #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5004 | ARG6, ARG7, ARG8) NAME, | |
43895be5 | 5005 | #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5006 | ARG6, ARG7, ARG8, ARG9) NAME, | |
5007 | #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5008 | ARG6, ARG7, ARG8, ARG9, ARG10) NAME, | |
5009 | #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5010 | ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME, | |
d2d4bdde | 5011 | #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME, |
5012 | #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME, | |
5013 | #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME, | |
03901330 | 5014 | #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, |
0a39fd54 | 5015 | #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, |
3c77ca67 | 5016 | #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ |
6349b8cc | 5017 | NAME, |
e561d5e1 | 5018 | #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5019 | ARG6) NAME, | |
6349b8cc | 5020 | #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5021 | ARG6, ARG7) NAME, | |
d2d4bdde | 5022 | #define DEF_POINTER_TYPE(NAME, TYPE) NAME, |
5023 | #include "builtin-types.def" | |
5024 | #undef DEF_PRIMITIVE_TYPE | |
5025 | #undef DEF_FUNCTION_TYPE_0 | |
5026 | #undef DEF_FUNCTION_TYPE_1 | |
5027 | #undef DEF_FUNCTION_TYPE_2 | |
5028 | #undef DEF_FUNCTION_TYPE_3 | |
5029 | #undef DEF_FUNCTION_TYPE_4 | |
0a39fd54 | 5030 | #undef DEF_FUNCTION_TYPE_5 |
5031 | #undef DEF_FUNCTION_TYPE_6 | |
27213ba3 | 5032 | #undef DEF_FUNCTION_TYPE_7 |
bc7bff74 | 5033 | #undef DEF_FUNCTION_TYPE_8 |
43895be5 | 5034 | #undef DEF_FUNCTION_TYPE_9 |
5035 | #undef DEF_FUNCTION_TYPE_10 | |
5036 | #undef DEF_FUNCTION_TYPE_11 | |
d2d4bdde | 5037 | #undef DEF_FUNCTION_TYPE_VAR_0 |
5038 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
5039 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
03901330 | 5040 | #undef DEF_FUNCTION_TYPE_VAR_3 |
0a39fd54 | 5041 | #undef DEF_FUNCTION_TYPE_VAR_4 |
5042 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
e561d5e1 | 5043 | #undef DEF_FUNCTION_TYPE_VAR_6 |
6349b8cc | 5044 | #undef DEF_FUNCTION_TYPE_VAR_7 |
d2d4bdde | 5045 | #undef DEF_POINTER_TYPE |
27213ba3 | 5046 | BT_LAST |
5047 | }; | |
5048 | ||
5049 | typedef enum c_builtin_type builtin_type; | |
d2d4bdde | 5050 | |
27213ba3 | 5051 | /* A temporary array for c_common_nodes_and_builtins. Used in |
5052 | communication with def_fn_type. */ | |
5053 | static tree builtin_types[(int) BT_LAST + 1]; | |
d2d4bdde | 5054 | |
27213ba3 | 5055 | /* A helper function for c_common_nodes_and_builtins. Build function type |
5056 | for DEF with return type RET and N arguments. If VAR is true, then the | |
5057 | function should be variadic after those N arguments. | |
5058 | ||
5059 | Takes special care not to ICE if any of the types involved are | |
5060 | error_mark_node, which indicates that said type is not in fact available | |
5061 | (see builtin_type_for_size). In which case the function type as a whole | |
5062 | should be error_mark_node. */ | |
5063 | ||
5064 | static void | |
5065 | def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...) | |
5066 | { | |
3a939d12 | 5067 | tree t; |
5068 | tree *args = XALLOCAVEC (tree, n); | |
27213ba3 | 5069 | va_list list; |
5070 | int i; | |
5071 | ||
5072 | va_start (list, n); | |
5073 | for (i = 0; i < n; ++i) | |
5074 | { | |
7d339f93 | 5075 | builtin_type a = (builtin_type) va_arg (list, int); |
27213ba3 | 5076 | t = builtin_types[a]; |
5077 | if (t == error_mark_node) | |
5078 | goto egress; | |
3a939d12 | 5079 | args[i] = t; |
27213ba3 | 5080 | } |
27213ba3 | 5081 | |
27213ba3 | 5082 | t = builtin_types[ret]; |
5083 | if (t == error_mark_node) | |
5084 | goto egress; | |
3a939d12 | 5085 | if (var) |
5086 | t = build_varargs_function_type_array (t, n, args); | |
5087 | else | |
5088 | t = build_function_type_array (t, n, args); | |
27213ba3 | 5089 | |
5090 | egress: | |
5091 | builtin_types[def] = t; | |
451c8e2f | 5092 | va_end (list); |
27213ba3 | 5093 | } |
5094 | ||
dce22712 | 5095 | /* Build builtin functions common to both C and C++ language |
5096 | frontends. */ | |
5097 | ||
5098 | static void | |
5099 | c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node) | |
5100 | { | |
5101 | #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ | |
5102 | builtin_types[ENUM] = VALUE; | |
5103 | #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ | |
5104 | def_fn_type (ENUM, RETURN, 0, 0); | |
5105 | #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ | |
5106 | def_fn_type (ENUM, RETURN, 0, 1, ARG1); | |
5107 | #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ | |
5108 | def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2); | |
5109 | #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
5110 | def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3); | |
5111 | #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
5112 | def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4); | |
5113 | #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
5114 | def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
5115 | #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5116 | ARG6) \ | |
5117 | def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); | |
5118 | #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5119 | ARG6, ARG7) \ | |
5120 | def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); | |
bc7bff74 | 5121 | #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5122 | ARG6, ARG7, ARG8) \ | |
5123 | def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \ | |
5124 | ARG7, ARG8); | |
43895be5 | 5125 | #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5126 | ARG6, ARG7, ARG8, ARG9) \ | |
5127 | def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \ | |
5128 | ARG7, ARG8, ARG9); | |
5129 | #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5130 | ARG6, ARG7, ARG8, ARG9, ARG10) \ | |
5131 | def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \ | |
5132 | ARG7, ARG8, ARG9, ARG10); | |
5133 | #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5134 | ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \ | |
5135 | def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \ | |
5136 | ARG7, ARG8, ARG9, ARG10, ARG11); | |
dce22712 | 5137 | #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ |
5138 | def_fn_type (ENUM, RETURN, 1, 0); | |
5139 | #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ | |
5140 | def_fn_type (ENUM, RETURN, 1, 1, ARG1); | |
5141 | #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ | |
5142 | def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2); | |
5143 | #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
5144 | def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3); | |
5145 | #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
5146 | def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4); | |
5147 | #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
5148 | def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
e561d5e1 | 5149 | #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5150 | ARG6) \ | |
5151 | def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); | |
6349b8cc | 5152 | #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5153 | ARG6, ARG7) \ | |
5154 | def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); | |
dce22712 | 5155 | #define DEF_POINTER_TYPE(ENUM, TYPE) \ |
5156 | builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]); | |
5157 | ||
5158 | #include "builtin-types.def" | |
5159 | ||
5160 | #undef DEF_PRIMITIVE_TYPE | |
3c77ca67 | 5161 | #undef DEF_FUNCTION_TYPE_0 |
dce22712 | 5162 | #undef DEF_FUNCTION_TYPE_1 |
5163 | #undef DEF_FUNCTION_TYPE_2 | |
5164 | #undef DEF_FUNCTION_TYPE_3 | |
5165 | #undef DEF_FUNCTION_TYPE_4 | |
5166 | #undef DEF_FUNCTION_TYPE_5 | |
5167 | #undef DEF_FUNCTION_TYPE_6 | |
3c77ca67 | 5168 | #undef DEF_FUNCTION_TYPE_7 |
5169 | #undef DEF_FUNCTION_TYPE_8 | |
43895be5 | 5170 | #undef DEF_FUNCTION_TYPE_9 |
5171 | #undef DEF_FUNCTION_TYPE_10 | |
5172 | #undef DEF_FUNCTION_TYPE_11 | |
dce22712 | 5173 | #undef DEF_FUNCTION_TYPE_VAR_0 |
5174 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
5175 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
5176 | #undef DEF_FUNCTION_TYPE_VAR_3 | |
5177 | #undef DEF_FUNCTION_TYPE_VAR_4 | |
5178 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
e561d5e1 | 5179 | #undef DEF_FUNCTION_TYPE_VAR_6 |
6349b8cc | 5180 | #undef DEF_FUNCTION_TYPE_VAR_7 |
dce22712 | 5181 | #undef DEF_POINTER_TYPE |
5182 | builtin_types[(int) BT_LAST] = NULL_TREE; | |
5183 | ||
5184 | c_init_attributes (); | |
5185 | ||
5186 | #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ | |
5187 | NONANSI_P, ATTRS, IMPLICIT, COND) \ | |
5188 | if (NAME && COND) \ | |
5189 | def_builtin_1 (ENUM, NAME, CLASS, \ | |
5190 | builtin_types[(int) TYPE], \ | |
5191 | builtin_types[(int) LIBTYPE], \ | |
5192 | BOTH_P, FALLBACK_P, NONANSI_P, \ | |
5193 | built_in_attributes[(int) ATTRS], IMPLICIT); | |
5194 | #include "builtins.def" | |
dce22712 | 5195 | |
87eb1c28 | 5196 | targetm.init_builtins (); |
5197 | ||
471eff36 | 5198 | build_common_builtin_nodes (); |
dce22712 | 5199 | |
a89e6c15 | 5200 | if (flag_cilkplus) |
d037099f | 5201 | cilk_init_builtins (); |
dce22712 | 5202 | } |
5203 | ||
9e6687c8 | 5204 | /* Like get_identifier, but avoid warnings about null arguments when |
5205 | the argument may be NULL for targets where GCC lacks stdint.h type | |
5206 | information. */ | |
5207 | ||
5208 | static inline tree | |
5209 | c_get_ident (const char *id) | |
5210 | { | |
5211 | return get_identifier (id); | |
5212 | } | |
5213 | ||
27213ba3 | 5214 | /* Build tree nodes and builtin functions common to both C and C++ language |
5215 | frontends. */ | |
5216 | ||
5217 | void | |
5218 | c_common_nodes_and_builtins (void) | |
5219 | { | |
924bbf02 | 5220 | int char16_type_size; |
5221 | int char32_type_size; | |
174fcc61 | 5222 | int wchar_type_size; |
5223 | tree array_domain_type; | |
2d47cc32 | 5224 | tree va_list_ref_type_node; |
8a15c04a | 5225 | tree va_list_arg_type_node; |
9f75f026 | 5226 | int i; |
a66c9326 | 5227 | |
c38a75b7 | 5228 | build_common_tree_nodes (flag_signed_char, flag_short_double); |
e593356b | 5229 | |
174fcc61 | 5230 | /* Define `int' and `char' first so that dbx will output them first. */ |
d946ea19 | 5231 | record_builtin_type (RID_INT, NULL, integer_type_node); |
174fcc61 | 5232 | record_builtin_type (RID_CHAR, "char", char_type_node); |
5233 | ||
5234 | /* `signed' is the same as `int'. FIXME: the declarations of "signed", | |
5235 | "unsigned long", "long long unsigned" and "unsigned short" were in C++ | |
5236 | but not C. Are the conditionals here needed? */ | |
c0f19401 | 5237 | if (c_dialect_cxx ()) |
d946ea19 | 5238 | record_builtin_type (RID_SIGNED, NULL, integer_type_node); |
174fcc61 | 5239 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); |
5240 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); | |
5241 | record_builtin_type (RID_MAX, "long unsigned int", | |
5242 | long_unsigned_type_node); | |
9f75f026 | 5243 | |
5244 | for (i = 0; i < NUM_INT_N_ENTS; i ++) | |
6388cfe2 | 5245 | { |
9f75f026 | 5246 | char name[25]; |
5247 | ||
5248 | sprintf (name, "__int%d", int_n_data[i].bitsize); | |
76738f56 | 5249 | record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name, |
9f75f026 | 5250 | int_n_trees[i].signed_type); |
5251 | sprintf (name, "__int%d unsigned", int_n_data[i].bitsize); | |
76738f56 | 5252 | record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type); |
6388cfe2 | 5253 | } |
9f75f026 | 5254 | |
c0f19401 | 5255 | if (c_dialect_cxx ()) |
174fcc61 | 5256 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); |
5257 | record_builtin_type (RID_MAX, "long long int", | |
5258 | long_long_integer_type_node); | |
5259 | record_builtin_type (RID_MAX, "long long unsigned int", | |
5260 | long_long_unsigned_type_node); | |
c0f19401 | 5261 | if (c_dialect_cxx ()) |
174fcc61 | 5262 | record_builtin_type (RID_MAX, "long long unsigned", |
5263 | long_long_unsigned_type_node); | |
5264 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); | |
5265 | record_builtin_type (RID_MAX, "short unsigned int", | |
5266 | short_unsigned_type_node); | |
c0f19401 | 5267 | if (c_dialect_cxx ()) |
174fcc61 | 5268 | record_builtin_type (RID_MAX, "unsigned short", |
5269 | short_unsigned_type_node); | |
5270 | ||
5271 | /* Define both `signed char' and `unsigned char'. */ | |
5272 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); | |
5273 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); | |
5274 | ||
771d21fa | 5275 | /* These are types that c_common_type_for_size and |
5276 | c_common_type_for_mode use. */ | |
e60a6f7b | 5277 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5278 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5279 | intQI_type_node)); |
e60a6f7b | 5280 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5281 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5282 | intHI_type_node)); |
e60a6f7b | 5283 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5284 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5285 | intSI_type_node)); |
e60a6f7b | 5286 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5287 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5288 | intDI_type_node)); |
174fcc61 | 5289 | #if HOST_BITS_PER_WIDE_INT >= 64 |
9f75f026 | 5290 | /* Note that this is different than the __int128 type that's part of |
5291 | the generic __intN support. */ | |
f1515a39 | 5292 | if (targetm.scalar_mode_supported_p (TImode)) |
e60a6f7b | 5293 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5294 | TYPE_DECL, | |
f1515a39 | 5295 | get_identifier ("__int128_t"), |
5296 | intTI_type_node)); | |
174fcc61 | 5297 | #endif |
e60a6f7b | 5298 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5299 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5300 | unsigned_intQI_type_node)); |
e60a6f7b | 5301 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5302 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5303 | unsigned_intHI_type_node)); |
e60a6f7b | 5304 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5305 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5306 | unsigned_intSI_type_node)); |
e60a6f7b | 5307 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5308 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5309 | unsigned_intDI_type_node)); |
174fcc61 | 5310 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 5311 | if (targetm.scalar_mode_supported_p (TImode)) |
e60a6f7b | 5312 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5313 | TYPE_DECL, | |
f1515a39 | 5314 | get_identifier ("__uint128_t"), |
5315 | unsigned_intTI_type_node)); | |
174fcc61 | 5316 | #endif |
5317 | ||
5318 | /* Create the widest literal types. */ | |
5319 | widest_integer_literal_type_node | |
5320 | = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
e60a6f7b | 5321 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5322 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5323 | widest_integer_literal_type_node)); |
174fcc61 | 5324 | |
5325 | widest_unsigned_literal_type_node | |
5326 | = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
e60a6f7b | 5327 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5328 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5329 | widest_unsigned_literal_type_node)); |
174fcc61 | 5330 | |
654ef926 | 5331 | signed_size_type_node = c_common_signed_type (size_type_node); |
174fcc61 | 5332 | |
73673831 | 5333 | pid_type_node = |
5334 | TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE))); | |
5335 | ||
d946ea19 | 5336 | record_builtin_type (RID_FLOAT, NULL, float_type_node); |
5337 | record_builtin_type (RID_DOUBLE, NULL, double_type_node); | |
174fcc61 | 5338 | record_builtin_type (RID_MAX, "long double", long_double_type_node); |
5339 | ||
c4503c0a | 5340 | /* Only supported decimal floating point extension if the target |
5341 | actually supports underlying modes. */ | |
48e1416a | 5342 | if (targetm.scalar_mode_supported_p (SDmode) |
c4503c0a | 5343 | && targetm.scalar_mode_supported_p (DDmode) |
5344 | && targetm.scalar_mode_supported_p (TDmode)) | |
5345 | { | |
5346 | record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node); | |
5347 | record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node); | |
5348 | record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node); | |
5349 | } | |
5350 | ||
9421ebb9 | 5351 | if (targetm.fixed_point_supported_p ()) |
5352 | { | |
5353 | record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node); | |
5354 | record_builtin_type (RID_FRACT, NULL, fract_type_node); | |
5355 | record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node); | |
5356 | record_builtin_type (RID_MAX, "long long _Fract", | |
5357 | long_long_fract_type_node); | |
5358 | record_builtin_type (RID_MAX, "unsigned short _Fract", | |
5359 | unsigned_short_fract_type_node); | |
5360 | record_builtin_type (RID_MAX, "unsigned _Fract", | |
5361 | unsigned_fract_type_node); | |
5362 | record_builtin_type (RID_MAX, "unsigned long _Fract", | |
5363 | unsigned_long_fract_type_node); | |
5364 | record_builtin_type (RID_MAX, "unsigned long long _Fract", | |
5365 | unsigned_long_long_fract_type_node); | |
5366 | record_builtin_type (RID_MAX, "_Sat short _Fract", | |
5367 | sat_short_fract_type_node); | |
5368 | record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node); | |
5369 | record_builtin_type (RID_MAX, "_Sat long _Fract", | |
5370 | sat_long_fract_type_node); | |
5371 | record_builtin_type (RID_MAX, "_Sat long long _Fract", | |
5372 | sat_long_long_fract_type_node); | |
5373 | record_builtin_type (RID_MAX, "_Sat unsigned short _Fract", | |
5374 | sat_unsigned_short_fract_type_node); | |
5375 | record_builtin_type (RID_MAX, "_Sat unsigned _Fract", | |
5376 | sat_unsigned_fract_type_node); | |
5377 | record_builtin_type (RID_MAX, "_Sat unsigned long _Fract", | |
5378 | sat_unsigned_long_fract_type_node); | |
5379 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract", | |
5380 | sat_unsigned_long_long_fract_type_node); | |
5381 | record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node); | |
5382 | record_builtin_type (RID_ACCUM, NULL, accum_type_node); | |
5383 | record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node); | |
5384 | record_builtin_type (RID_MAX, "long long _Accum", | |
5385 | long_long_accum_type_node); | |
5386 | record_builtin_type (RID_MAX, "unsigned short _Accum", | |
5387 | unsigned_short_accum_type_node); | |
5388 | record_builtin_type (RID_MAX, "unsigned _Accum", | |
5389 | unsigned_accum_type_node); | |
5390 | record_builtin_type (RID_MAX, "unsigned long _Accum", | |
5391 | unsigned_long_accum_type_node); | |
5392 | record_builtin_type (RID_MAX, "unsigned long long _Accum", | |
5393 | unsigned_long_long_accum_type_node); | |
5394 | record_builtin_type (RID_MAX, "_Sat short _Accum", | |
5395 | sat_short_accum_type_node); | |
5396 | record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node); | |
5397 | record_builtin_type (RID_MAX, "_Sat long _Accum", | |
5398 | sat_long_accum_type_node); | |
5399 | record_builtin_type (RID_MAX, "_Sat long long _Accum", | |
5400 | sat_long_long_accum_type_node); | |
5401 | record_builtin_type (RID_MAX, "_Sat unsigned short _Accum", | |
5402 | sat_unsigned_short_accum_type_node); | |
5403 | record_builtin_type (RID_MAX, "_Sat unsigned _Accum", | |
5404 | sat_unsigned_accum_type_node); | |
5405 | record_builtin_type (RID_MAX, "_Sat unsigned long _Accum", | |
5406 | sat_unsigned_long_accum_type_node); | |
5407 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum", | |
5408 | sat_unsigned_long_long_accum_type_node); | |
5409 | ||
5410 | } | |
5411 | ||
e60a6f7b | 5412 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5413 | TYPE_DECL, | |
dc24ddbd | 5414 | get_identifier ("complex int"), |
5415 | complex_integer_type_node)); | |
e60a6f7b | 5416 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5417 | TYPE_DECL, | |
dc24ddbd | 5418 | get_identifier ("complex float"), |
5419 | complex_float_type_node)); | |
e60a6f7b | 5420 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5421 | TYPE_DECL, | |
dc24ddbd | 5422 | get_identifier ("complex double"), |
5423 | complex_double_type_node)); | |
5424 | lang_hooks.decls.pushdecl | |
e60a6f7b | 5425 | (build_decl (UNKNOWN_LOCATION, |
5426 | TYPE_DECL, get_identifier ("complex long double"), | |
20325f61 | 5427 | complex_long_double_type_node)); |
174fcc61 | 5428 | |
e256d445 | 5429 | if (c_dialect_cxx ()) |
5430 | /* For C++, make fileptr_type_node a distinct void * type until | |
5431 | FILE type is defined. */ | |
e086912e | 5432 | fileptr_type_node = build_variant_type_copy (ptr_type_node); |
e256d445 | 5433 | |
d946ea19 | 5434 | record_builtin_type (RID_VOID, NULL, void_type_node); |
174fcc61 | 5435 | |
6753bca0 | 5436 | /* Set the TYPE_NAME for any variants that were built before |
5437 | record_builtin_type gave names to the built-in types. */ | |
5438 | { | |
5439 | tree void_name = TYPE_NAME (void_type_node); | |
5440 | TYPE_NAME (void_type_node) = NULL_TREE; | |
5441 | TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST)) | |
5442 | = void_name; | |
5443 | TYPE_NAME (void_type_node) = void_name; | |
5444 | } | |
5445 | ||
174fcc61 | 5446 | void_list_node = build_void_list_node (); |
5447 | ||
5448 | /* Make a type to be the domain of a few array types | |
5449 | whose domains don't really matter. | |
5450 | 200 is small enough that it always fits in size_t | |
5451 | and large enough that it can hold most function names for the | |
5452 | initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
5453 | array_domain_type = build_index_type (size_int (200)); | |
5454 | ||
5455 | /* Make a type for arrays of characters. | |
5456 | With luck nothing will ever really depend on the length of this | |
5457 | array type. */ | |
5458 | char_array_type_node | |
5459 | = build_array_type (char_type_node, array_domain_type); | |
5460 | ||
d2d4bdde | 5461 | string_type_node = build_pointer_type (char_type_node); |
5462 | const_string_type_node | |
5463 | = build_pointer_type (build_qualified_type | |
5464 | (char_type_node, TYPE_QUAL_CONST)); | |
5465 | ||
174fcc61 | 5466 | /* This is special for C++ so functions can be overloaded. */ |
18ef7ac2 | 5467 | wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE); |
174fcc61 | 5468 | wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); |
5469 | wchar_type_size = TYPE_PRECISION (wchar_type_node); | |
f3449a3c | 5470 | underlying_wchar_type_node = wchar_type_node; |
c0f19401 | 5471 | if (c_dialect_cxx ()) |
174fcc61 | 5472 | { |
78a8ed03 | 5473 | if (TYPE_UNSIGNED (wchar_type_node)) |
174fcc61 | 5474 | wchar_type_node = make_unsigned_type (wchar_type_size); |
5475 | else | |
5476 | wchar_type_node = make_signed_type (wchar_type_size); | |
5477 | record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); | |
5478 | } | |
174fcc61 | 5479 | |
5480 | /* This is for wide string constants. */ | |
5481 | wchar_array_type_node | |
5482 | = build_array_type (wchar_type_node, array_domain_type); | |
5483 | ||
924bbf02 | 5484 | /* Define 'char16_t'. */ |
5485 | char16_type_node = get_identifier (CHAR16_TYPE); | |
5486 | char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node)); | |
5487 | char16_type_size = TYPE_PRECISION (char16_type_node); | |
5488 | if (c_dialect_cxx ()) | |
5489 | { | |
5490 | char16_type_node = make_unsigned_type (char16_type_size); | |
5491 | ||
60777f69 | 5492 | if (cxx_dialect >= cxx11) |
924bbf02 | 5493 | record_builtin_type (RID_CHAR16, "char16_t", char16_type_node); |
5494 | } | |
5495 | ||
5496 | /* This is for UTF-16 string constants. */ | |
5497 | char16_array_type_node | |
5498 | = build_array_type (char16_type_node, array_domain_type); | |
5499 | ||
5500 | /* Define 'char32_t'. */ | |
5501 | char32_type_node = get_identifier (CHAR32_TYPE); | |
5502 | char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node)); | |
5503 | char32_type_size = TYPE_PRECISION (char32_type_node); | |
5504 | if (c_dialect_cxx ()) | |
5505 | { | |
5506 | char32_type_node = make_unsigned_type (char32_type_size); | |
5507 | ||
60777f69 | 5508 | if (cxx_dialect >= cxx11) |
924bbf02 | 5509 | record_builtin_type (RID_CHAR32, "char32_t", char32_type_node); |
5510 | } | |
5511 | ||
5512 | /* This is for UTF-32 string constants. */ | |
5513 | char32_array_type_node | |
5514 | = build_array_type (char32_type_node, array_domain_type); | |
5515 | ||
6bf5ed8d | 5516 | wint_type_node = |
5517 | TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); | |
5518 | ||
5519 | intmax_type_node = | |
5520 | TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); | |
5521 | uintmax_type_node = | |
5522 | TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); | |
5523 | ||
f3449a3c | 5524 | if (SIG_ATOMIC_TYPE) |
5525 | sig_atomic_type_node = | |
9e6687c8 | 5526 | TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE))); |
f3449a3c | 5527 | if (INT8_TYPE) |
5528 | int8_type_node = | |
9e6687c8 | 5529 | TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE))); |
f3449a3c | 5530 | if (INT16_TYPE) |
5531 | int16_type_node = | |
9e6687c8 | 5532 | TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE))); |
f3449a3c | 5533 | if (INT32_TYPE) |
5534 | int32_type_node = | |
9e6687c8 | 5535 | TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE))); |
f3449a3c | 5536 | if (INT64_TYPE) |
5537 | int64_type_node = | |
9e6687c8 | 5538 | TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE))); |
f3449a3c | 5539 | if (UINT8_TYPE) |
5540 | uint8_type_node = | |
9e6687c8 | 5541 | TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE))); |
f3449a3c | 5542 | if (UINT16_TYPE) |
d1081017 | 5543 | c_uint16_type_node = uint16_type_node = |
9e6687c8 | 5544 | TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE))); |
f3449a3c | 5545 | if (UINT32_TYPE) |
d1081017 | 5546 | c_uint32_type_node = uint32_type_node = |
9e6687c8 | 5547 | TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE))); |
f3449a3c | 5548 | if (UINT64_TYPE) |
d1081017 | 5549 | c_uint64_type_node = uint64_type_node = |
9e6687c8 | 5550 | TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE))); |
f3449a3c | 5551 | if (INT_LEAST8_TYPE) |
5552 | int_least8_type_node = | |
9e6687c8 | 5553 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE))); |
f3449a3c | 5554 | if (INT_LEAST16_TYPE) |
5555 | int_least16_type_node = | |
9e6687c8 | 5556 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE))); |
f3449a3c | 5557 | if (INT_LEAST32_TYPE) |
5558 | int_least32_type_node = | |
9e6687c8 | 5559 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE))); |
f3449a3c | 5560 | if (INT_LEAST64_TYPE) |
5561 | int_least64_type_node = | |
9e6687c8 | 5562 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE))); |
f3449a3c | 5563 | if (UINT_LEAST8_TYPE) |
5564 | uint_least8_type_node = | |
9e6687c8 | 5565 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE))); |
f3449a3c | 5566 | if (UINT_LEAST16_TYPE) |
5567 | uint_least16_type_node = | |
9e6687c8 | 5568 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE))); |
f3449a3c | 5569 | if (UINT_LEAST32_TYPE) |
5570 | uint_least32_type_node = | |
9e6687c8 | 5571 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE))); |
f3449a3c | 5572 | if (UINT_LEAST64_TYPE) |
5573 | uint_least64_type_node = | |
9e6687c8 | 5574 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE))); |
f3449a3c | 5575 | if (INT_FAST8_TYPE) |
5576 | int_fast8_type_node = | |
9e6687c8 | 5577 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE))); |
f3449a3c | 5578 | if (INT_FAST16_TYPE) |
5579 | int_fast16_type_node = | |
9e6687c8 | 5580 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE))); |
f3449a3c | 5581 | if (INT_FAST32_TYPE) |
5582 | int_fast32_type_node = | |
9e6687c8 | 5583 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE))); |
f3449a3c | 5584 | if (INT_FAST64_TYPE) |
5585 | int_fast64_type_node = | |
9e6687c8 | 5586 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE))); |
f3449a3c | 5587 | if (UINT_FAST8_TYPE) |
5588 | uint_fast8_type_node = | |
9e6687c8 | 5589 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE))); |
f3449a3c | 5590 | if (UINT_FAST16_TYPE) |
5591 | uint_fast16_type_node = | |
9e6687c8 | 5592 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE))); |
f3449a3c | 5593 | if (UINT_FAST32_TYPE) |
5594 | uint_fast32_type_node = | |
9e6687c8 | 5595 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE))); |
f3449a3c | 5596 | if (UINT_FAST64_TYPE) |
5597 | uint_fast64_type_node = | |
9e6687c8 | 5598 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE))); |
f3449a3c | 5599 | if (INTPTR_TYPE) |
5600 | intptr_type_node = | |
9e6687c8 | 5601 | TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE))); |
f3449a3c | 5602 | if (UINTPTR_TYPE) |
5603 | uintptr_type_node = | |
9e6687c8 | 5604 | TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE))); |
f3449a3c | 5605 | |
3a939d12 | 5606 | default_function_type |
5607 | = build_varargs_function_type_list (integer_type_node, NULL_TREE); | |
6bf5ed8d | 5608 | ptrdiff_type_node |
5609 | = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); | |
11773141 | 5610 | unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node); |
6bf5ed8d | 5611 | |
dc24ddbd | 5612 | lang_hooks.decls.pushdecl |
e60a6f7b | 5613 | (build_decl (UNKNOWN_LOCATION, |
5614 | TYPE_DECL, get_identifier ("__builtin_va_list"), | |
20325f61 | 5615 | va_list_type_node)); |
202d6e5f | 5616 | if (targetm.enum_va_list_p) |
acd6f472 | 5617 | { |
5618 | int l; | |
5619 | const char *pname; | |
5620 | tree ptype; | |
5f57a8b1 | 5621 | |
202d6e5f | 5622 | for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l) |
acd6f472 | 5623 | { |
5624 | lang_hooks.decls.pushdecl | |
5625 | (build_decl (UNKNOWN_LOCATION, | |
5626 | TYPE_DECL, get_identifier (pname), | |
5627 | ptype)); | |
ab2c1de8 | 5628 | |
acd6f472 | 5629 | } |
5630 | } | |
8a15c04a | 5631 | |
8a15c04a | 5632 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
2d47cc32 | 5633 | { |
5634 | va_list_arg_type_node = va_list_ref_type_node = | |
5635 | build_pointer_type (TREE_TYPE (va_list_type_node)); | |
5636 | } | |
8a15c04a | 5637 | else |
2d47cc32 | 5638 | { |
5639 | va_list_arg_type_node = va_list_type_node; | |
5640 | va_list_ref_type_node = build_reference_type (va_list_type_node); | |
5641 | } | |
1cae46be | 5642 | |
dce22712 | 5643 | if (!flag_preprocess_only) |
5644 | c_define_builtins (va_list_ref_type_node, va_list_arg_type_node); | |
ffa8918b | 5645 | |
5c62f199 | 5646 | main_identifier_node = get_identifier ("main"); |
ae84079f | 5647 | |
5648 | /* Create the built-in __null node. It is important that this is | |
5649 | not shared. */ | |
271e739a | 5650 | null_node = make_int_cst (1, 1); |
ae84079f | 5651 | TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0); |
27213ba3 | 5652 | |
5653 | /* Since builtin_types isn't gc'ed, don't export these nodes. */ | |
5654 | memset (builtin_types, 0, sizeof (builtin_types)); | |
72040e7e | 5655 | } |
a66c9326 | 5656 | |
79b01846 | 5657 | /* The number of named compound-literals generated thus far. */ |
5658 | static GTY(()) int compound_literal_number; | |
5659 | ||
5660 | /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */ | |
5661 | ||
5662 | void | |
5663 | set_compound_literal_name (tree decl) | |
5664 | { | |
5665 | char *name; | |
5666 | ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", | |
5667 | compound_literal_number); | |
5668 | compound_literal_number++; | |
5669 | DECL_NAME (decl) = get_identifier (name); | |
5670 | } | |
5671 | ||
2e474820 | 5672 | /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type |
5673 | TYPE and operand OP. */ | |
5674 | ||
5675 | static tree | |
5676 | build_va_arg_1 (location_t loc, tree type, tree op) | |
5677 | { | |
5678 | tree expr = build1 (VA_ARG_EXPR, type, op); | |
5679 | SET_EXPR_LOCATION (expr, loc); | |
5680 | return expr; | |
5681 | } | |
5682 | ||
5683 | /* Return a VA_ARG_EXPR corresponding to a source-level expression | |
5684 | va_arg (EXPR, TYPE) at source location LOC. */ | |
5685 | ||
a66c9326 | 5686 | tree |
e60a6f7b | 5687 | build_va_arg (location_t loc, tree expr, tree type) |
a66c9326 | 5688 | { |
c37be9ec | 5689 | tree va_type = TREE_TYPE (expr); |
5690 | tree canon_va_type = (va_type == error_mark_node | |
5691 | ? NULL_TREE | |
5692 | : targetm.canonical_va_list_type (va_type)); | |
5693 | ||
2e474820 | 5694 | if (va_type == error_mark_node |
5695 | || canon_va_type == NULL_TREE) | |
c37be9ec | 5696 | { |
2e474820 | 5697 | /* Let's handle things neutrallly, if expr: |
5698 | - has undeclared type, or | |
5699 | - is not an va_list type. */ | |
5700 | return build_va_arg_1 (loc, type, expr); | |
c37be9ec | 5701 | } |
f7fec1c7 | 5702 | |
2e474820 | 5703 | if (TREE_CODE (canon_va_type) != ARRAY_TYPE) |
5704 | { | |
5705 | /* Case 1: Not an array type. */ | |
5706 | ||
5707 | /* Take the address, to get '&ap'. */ | |
5708 | mark_addressable (expr); | |
5709 | expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr); | |
5710 | ||
5711 | /* Verify that &ap is still recognized as having va_list type. */ | |
5712 | tree canon_expr_type | |
5713 | = targetm.canonical_va_list_type (TREE_TYPE (expr)); | |
5714 | gcc_assert (canon_expr_type != NULL_TREE); | |
5715 | ||
5716 | return build_va_arg_1 (loc, type, expr); | |
5717 | } | |
5718 | ||
5719 | /* Case 2: Array type. | |
5720 | ||
5721 | Background: | |
5722 | ||
5723 | For contrast, let's start with the simple case (case 1). If | |
5724 | canon_va_type is not an array type, but say a char *, then when | |
5725 | passing-by-value a va_list, the type of the va_list param decl is | |
5726 | the same as for another va_list decl (all ap's are char *): | |
5727 | ||
5728 | f2_1 (char * ap) | |
5729 | D.1815 = VA_ARG (&ap, 0B, 1); | |
5730 | return D.1815; | |
5731 | ||
5732 | f2 (int i) | |
5733 | char * ap.0; | |
5734 | char * ap; | |
5735 | __builtin_va_start (&ap, 0); | |
5736 | ap.0 = ap; | |
5737 | res = f2_1 (ap.0); | |
5738 | __builtin_va_end (&ap); | |
5739 | D.1812 = res; | |
5740 | return D.1812; | |
5741 | ||
5742 | However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a | |
5743 | va_list the type of the va_list param decl (case 2b, struct * ap) is not | |
5744 | the same as for another va_list decl (case 2a, struct ap[1]). | |
5745 | ||
5746 | f2_1 (struct * ap) | |
5747 | D.1844 = VA_ARG (ap, 0B, 0); | |
5748 | return D.1844; | |
5749 | ||
5750 | f2 (int i) | |
5751 | struct ap[1]; | |
5752 | __builtin_va_start (&ap, 0); | |
5753 | res = f2_1 (&ap); | |
5754 | __builtin_va_end (&ap); | |
5755 | D.1841 = res; | |
5756 | return D.1841; | |
5757 | ||
5758 | Case 2b is different because: | |
5759 | - on the callee side, the parm decl has declared type va_list, but | |
5760 | grokdeclarator changes the type of the parm decl to a pointer to the | |
5761 | array elem type. | |
5762 | - on the caller side, the pass-by-value uses &ap. | |
5763 | ||
5764 | We unify these two cases (case 2a: va_list is array type, | |
5765 | case 2b: va_list is pointer to array elem type), by adding '&' for the | |
5766 | array type case, such that we have a pointer to array elem in both | |
5767 | cases. */ | |
5768 | ||
5769 | if (TREE_CODE (va_type) == ARRAY_TYPE) | |
5770 | { | |
5771 | /* Case 2a: va_list is array type. */ | |
5772 | ||
5773 | /* Take the address, to get '&ap'. Make sure it's a pointer to array | |
5774 | elem type. */ | |
5775 | mark_addressable (expr); | |
5776 | expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)), | |
5777 | expr); | |
5778 | ||
5779 | /* Verify that &ap is still recognized as having va_list type. */ | |
5780 | tree canon_expr_type | |
5781 | = targetm.canonical_va_list_type (TREE_TYPE (expr)); | |
5782 | gcc_assert (canon_expr_type != NULL_TREE); | |
5783 | } | |
5784 | else | |
5785 | { | |
5786 | /* Case 2b: va_list is pointer to array elem type. */ | |
5787 | gcc_assert (POINTER_TYPE_P (va_type)); | |
5788 | gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type)); | |
5789 | ||
5790 | /* Don't take the address. We've already got '&ap'. */ | |
5791 | ; | |
5792 | } | |
5793 | ||
5794 | return build_va_arg_1 (loc, type, expr); | |
a66c9326 | 5795 | } |
0d4238dc | 5796 | |
5797 | ||
dd878098 | 5798 | /* Linked list of disabled built-in functions. */ |
5799 | ||
6dc50383 | 5800 | struct disabled_builtin |
dd878098 | 5801 | { |
5802 | const char *name; | |
5803 | struct disabled_builtin *next; | |
6dc50383 | 5804 | }; |
dd878098 | 5805 | static disabled_builtin *disabled_builtins = NULL; |
5806 | ||
1cae46be | 5807 | static bool builtin_function_disabled_p (const char *); |
dd878098 | 5808 | |
5809 | /* Disable a built-in function specified by -fno-builtin-NAME. If NAME | |
5810 | begins with "__builtin_", give an error. */ | |
5811 | ||
5812 | void | |
1cae46be | 5813 | disable_builtin_function (const char *name) |
dd878098 | 5814 | { |
5815 | if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0) | |
b0b1af64 | 5816 | error ("cannot disable built-in function %qs", name); |
dd878098 | 5817 | else |
5818 | { | |
e85905e5 | 5819 | disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin); |
5820 | new_disabled_builtin->name = name; | |
5821 | new_disabled_builtin->next = disabled_builtins; | |
5822 | disabled_builtins = new_disabled_builtin; | |
dd878098 | 5823 | } |
5824 | } | |
5825 | ||
5826 | ||
5827 | /* Return true if the built-in function NAME has been disabled, false | |
5828 | otherwise. */ | |
5829 | ||
5830 | static bool | |
1cae46be | 5831 | builtin_function_disabled_p (const char *name) |
dd878098 | 5832 | { |
5833 | disabled_builtin *p; | |
5834 | for (p = disabled_builtins; p != NULL; p = p->next) | |
5835 | { | |
5836 | if (strcmp (name, p->name) == 0) | |
5837 | return true; | |
5838 | } | |
5839 | return false; | |
5840 | } | |
5841 | ||
5842 | ||
3237155d | 5843 | /* Worker for DEF_BUILTIN. |
5844 | Possibly define a builtin function with one or two names. | |
5845 | Does not declare a non-__builtin_ function if flag_no_builtin, or if | |
5846 | nonansi_p and flag_no_nonansi_builtin. */ | |
0d4238dc | 5847 | |
3237155d | 5848 | static void |
5849 | def_builtin_1 (enum built_in_function fncode, | |
5850 | const char *name, | |
5851 | enum built_in_class fnclass, | |
5852 | tree fntype, tree libtype, | |
5853 | bool both_p, bool fallback_p, bool nonansi_p, | |
5854 | tree fnattrs, bool implicit_p) | |
0d4238dc | 5855 | { |
3237155d | 5856 | tree decl; |
5857 | const char *libname; | |
5858 | ||
27213ba3 | 5859 | if (fntype == error_mark_node) |
5860 | return; | |
5861 | ||
3237155d | 5862 | gcc_assert ((!both_p && !fallback_p) |
5863 | || !strncmp (name, "__builtin_", | |
5864 | strlen ("__builtin_"))); | |
5865 | ||
5866 | libname = name + strlen ("__builtin_"); | |
54be5d7e | 5867 | decl = add_builtin_function (name, fntype, fncode, fnclass, |
5868 | (fallback_p ? libname : NULL), | |
5869 | fnattrs); | |
b9a16870 | 5870 | |
5871 | set_builtin_decl (fncode, decl, implicit_p); | |
5872 | ||
3237155d | 5873 | if (both_p |
5874 | && !flag_no_builtin && !builtin_function_disabled_p (libname) | |
dd878098 | 5875 | && !(nonansi_p && flag_no_nonansi_builtin)) |
54be5d7e | 5876 | add_builtin_function (libname, libtype, fncode, fnclass, |
5877 | NULL, fnattrs); | |
0d4238dc | 5878 | } |
e94026da | 5879 | \f |
d7aeef06 | 5880 | /* Nonzero if the type T promotes to int. This is (nearly) the |
5881 | integral promotions defined in ISO C99 6.3.1.1/2. */ | |
5882 | ||
5883 | bool | |
9f627b1a | 5884 | c_promoting_integer_type_p (const_tree t) |
d7aeef06 | 5885 | { |
5886 | switch (TREE_CODE (t)) | |
5887 | { | |
5888 | case INTEGER_TYPE: | |
5889 | return (TYPE_MAIN_VARIANT (t) == char_type_node | |
5890 | || TYPE_MAIN_VARIANT (t) == signed_char_type_node | |
5891 | || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node | |
5892 | || TYPE_MAIN_VARIANT (t) == short_integer_type_node | |
7aa1e6eb | 5893 | || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node |
5894 | || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node)); | |
d7aeef06 | 5895 | |
5896 | case ENUMERAL_TYPE: | |
5897 | /* ??? Technically all enumerations not larger than an int | |
5898 | promote to an int. But this is used along code paths | |
5899 | that only want to notice a size change. */ | |
5900 | return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node); | |
5901 | ||
5902 | case BOOLEAN_TYPE: | |
5903 | return 1; | |
5904 | ||
5905 | default: | |
5906 | return 0; | |
5907 | } | |
5908 | } | |
5909 | ||
e94026da | 5910 | /* Return 1 if PARMS specifies a fixed number of parameters |
5911 | and none of their types is affected by default promotions. */ | |
5912 | ||
5913 | int | |
9f627b1a | 5914 | self_promoting_args_p (const_tree parms) |
e94026da | 5915 | { |
9f627b1a | 5916 | const_tree t; |
e94026da | 5917 | for (t = parms; t; t = TREE_CHAIN (t)) |
5918 | { | |
19cb6b50 | 5919 | tree type = TREE_VALUE (t); |
43f74bc4 | 5920 | |
e1d8e198 | 5921 | if (type == error_mark_node) |
5922 | continue; | |
5923 | ||
e94026da | 5924 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
5925 | return 0; | |
5926 | ||
5927 | if (type == 0) | |
5928 | return 0; | |
5929 | ||
5930 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
5931 | return 0; | |
5932 | ||
d7aeef06 | 5933 | if (c_promoting_integer_type_p (type)) |
e94026da | 5934 | return 0; |
5935 | } | |
5936 | return 1; | |
5937 | } | |
605fb01e | 5938 | |
c10de5e7 | 5939 | /* Recursively remove any '*' or '&' operator from TYPE. */ |
5940 | tree | |
5941 | strip_pointer_operator (tree t) | |
5942 | { | |
5943 | while (POINTER_TYPE_P (t)) | |
5944 | t = TREE_TYPE (t); | |
5945 | return t; | |
5946 | } | |
5947 | ||
57a0ed23 | 5948 | /* Recursively remove pointer or array type from TYPE. */ |
5949 | tree | |
5950 | strip_pointer_or_array_types (tree t) | |
5951 | { | |
5952 | while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t)) | |
5953 | t = TREE_TYPE (t); | |
5954 | return t; | |
5955 | } | |
5956 | ||
e41f0d80 | 5957 | /* Used to compare case labels. K1 and K2 are actually tree nodes |
5958 | representing case labels, or NULL_TREE for a `default' label. | |
5959 | Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after | |
5960 | K2, and 0 if K1 and K2 are equal. */ | |
5961 | ||
5962 | int | |
1cae46be | 5963 | case_compare (splay_tree_key k1, splay_tree_key k2) |
e41f0d80 | 5964 | { |
5965 | /* Consider a NULL key (such as arises with a `default' label) to be | |
5966 | smaller than anything else. */ | |
5967 | if (!k1) | |
5968 | return k2 ? -1 : 0; | |
5969 | else if (!k2) | |
5970 | return k1 ? 1 : 0; | |
5971 | ||
5972 | return tree_int_cst_compare ((tree) k1, (tree) k2); | |
5973 | } | |
5974 | ||
e60a6f7b | 5975 | /* Process a case label, located at LOC, for the range LOW_VALUE |
5976 | ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE | |
5977 | then this case label is actually a `default' label. If only | |
5978 | HIGH_VALUE is NULL_TREE, then case label was declared using the | |
5979 | usual C/C++ syntax, rather than the GNU case range extension. | |
5980 | CASES is a tree containing all the case ranges processed so far; | |
be23b16f | 5981 | COND is the condition for the switch-statement itself. |
5982 | OUTSIDE_RANGE_P says whether there was a case value that doesn't | |
5983 | fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR | |
5984 | created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */ | |
e41f0d80 | 5985 | |
5986 | tree | |
e60a6f7b | 5987 | c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type, |
be23b16f | 5988 | tree low_value, tree high_value, bool *outside_range_p) |
e41f0d80 | 5989 | { |
5990 | tree type; | |
5991 | tree label; | |
5992 | tree case_label; | |
5993 | splay_tree_node node; | |
5994 | ||
5995 | /* Create the LABEL_DECL itself. */ | |
e60a6f7b | 5996 | label = create_artificial_label (loc); |
e41f0d80 | 5997 | |
5998 | /* If there was an error processing the switch condition, bail now | |
5999 | before we get more confused. */ | |
6000 | if (!cond || cond == error_mark_node) | |
4ee9c684 | 6001 | goto error_out; |
e41f0d80 | 6002 | |
1cae46be | 6003 | if ((low_value && TREE_TYPE (low_value) |
6004 | && POINTER_TYPE_P (TREE_TYPE (low_value))) | |
e41f0d80 | 6005 | || (high_value && TREE_TYPE (high_value) |
6006 | && POINTER_TYPE_P (TREE_TYPE (high_value)))) | |
b96dc121 | 6007 | { |
e60a6f7b | 6008 | error_at (loc, "pointers are not permitted as case values"); |
b96dc121 | 6009 | goto error_out; |
6010 | } | |
e41f0d80 | 6011 | |
6012 | /* Case ranges are a GNU extension. */ | |
8864917d | 6013 | if (high_value) |
29438999 | 6014 | pedwarn (loc, OPT_Wpedantic, |
8864917d | 6015 | "range expressions in switch statements are non-standard"); |
e41f0d80 | 6016 | |
6017 | type = TREE_TYPE (cond); | |
6018 | if (low_value) | |
6019 | { | |
2d2f6a15 | 6020 | low_value = check_case_value (loc, low_value); |
22a75734 | 6021 | low_value = convert_and_check (loc, type, low_value); |
96722196 | 6022 | if (low_value == error_mark_node) |
6023 | goto error_out; | |
e41f0d80 | 6024 | } |
6025 | if (high_value) | |
6026 | { | |
2d2f6a15 | 6027 | high_value = check_case_value (loc, high_value); |
22a75734 | 6028 | high_value = convert_and_check (loc, type, high_value); |
96722196 | 6029 | if (high_value == error_mark_node) |
6030 | goto error_out; | |
e41f0d80 | 6031 | } |
6032 | ||
96722196 | 6033 | if (low_value && high_value) |
6034 | { | |
6035 | /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't | |
a0c938f0 | 6036 | really a case range, even though it was written that way. |
6037 | Remove the HIGH_VALUE to simplify later processing. */ | |
96722196 | 6038 | if (tree_int_cst_equal (low_value, high_value)) |
6039 | high_value = NULL_TREE; | |
6040 | else if (!tree_int_cst_lt (low_value, high_value)) | |
e60a6f7b | 6041 | warning_at (loc, 0, "empty range specified"); |
96722196 | 6042 | } |
e41f0d80 | 6043 | |
2ca392fd | 6044 | /* See if the case is in range of the type of the original testing |
6045 | expression. If both low_value and high_value are out of range, | |
6046 | don't insert the case label and return NULL_TREE. */ | |
6047 | if (low_value | |
f61a9bc2 | 6048 | && !check_case_bounds (loc, type, orig_type, |
be23b16f | 6049 | &low_value, high_value ? &high_value : NULL, |
6050 | outside_range_p)) | |
2ca392fd | 6051 | return NULL_TREE; |
6052 | ||
e41f0d80 | 6053 | /* Look up the LOW_VALUE in the table of case labels we already |
6054 | have. */ | |
6055 | node = splay_tree_lookup (cases, (splay_tree_key) low_value); | |
6056 | /* If there was not an exact match, check for overlapping ranges. | |
6057 | There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; | |
6058 | that's a `default' label and the only overlap is an exact match. */ | |
6059 | if (!node && (low_value || high_value)) | |
6060 | { | |
6061 | splay_tree_node low_bound; | |
6062 | splay_tree_node high_bound; | |
6063 | ||
6064 | /* Even though there wasn't an exact match, there might be an | |
6065 | overlap between this case range and another case range. | |
6066 | Since we've (inductively) not allowed any overlapping case | |
6067 | ranges, we simply need to find the greatest low case label | |
6068 | that is smaller that LOW_VALUE, and the smallest low case | |
6069 | label that is greater than LOW_VALUE. If there is an overlap | |
6070 | it will occur in one of these two ranges. */ | |
6071 | low_bound = splay_tree_predecessor (cases, | |
6072 | (splay_tree_key) low_value); | |
6073 | high_bound = splay_tree_successor (cases, | |
6074 | (splay_tree_key) low_value); | |
6075 | ||
6076 | /* Check to see if the LOW_BOUND overlaps. It is smaller than | |
6077 | the LOW_VALUE, so there is no need to check unless the | |
6078 | LOW_BOUND is in fact itself a case range. */ | |
6079 | if (low_bound | |
6080 | && CASE_HIGH ((tree) low_bound->value) | |
6081 | && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), | |
6082 | low_value) >= 0) | |
6083 | node = low_bound; | |
6084 | /* Check to see if the HIGH_BOUND overlaps. The low end of that | |
6085 | range is bigger than the low end of the current range, so we | |
6086 | are only interested if the current range is a real range, and | |
6087 | not an ordinary case label. */ | |
1cae46be | 6088 | else if (high_bound |
e41f0d80 | 6089 | && high_value |
6090 | && (tree_int_cst_compare ((tree) high_bound->key, | |
6091 | high_value) | |
6092 | <= 0)) | |
6093 | node = high_bound; | |
6094 | } | |
6095 | /* If there was an overlap, issue an error. */ | |
6096 | if (node) | |
6097 | { | |
eaae3b75 | 6098 | tree duplicate = CASE_LABEL ((tree) node->value); |
e41f0d80 | 6099 | |
6100 | if (high_value) | |
6101 | { | |
e60a6f7b | 6102 | error_at (loc, "duplicate (or overlapping) case value"); |
6103 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
6104 | "this is the first entry overlapping that value"); | |
e41f0d80 | 6105 | } |
6106 | else if (low_value) | |
6107 | { | |
e60a6f7b | 6108 | error_at (loc, "duplicate case value") ; |
6109 | error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here"); | |
e41f0d80 | 6110 | } |
6111 | else | |
6112 | { | |
e60a6f7b | 6113 | error_at (loc, "multiple default labels in one switch"); |
6114 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
6115 | "this is the first default label"); | |
e41f0d80 | 6116 | } |
4ee9c684 | 6117 | goto error_out; |
e41f0d80 | 6118 | } |
6119 | ||
6120 | /* Add a CASE_LABEL to the statement-tree. */ | |
b6e3dd65 | 6121 | case_label = add_stmt (build_case_label (low_value, high_value, label)); |
e41f0d80 | 6122 | /* Register this case label in the splay tree. */ |
1cae46be | 6123 | splay_tree_insert (cases, |
e41f0d80 | 6124 | (splay_tree_key) low_value, |
6125 | (splay_tree_value) case_label); | |
6126 | ||
6127 | return case_label; | |
4ee9c684 | 6128 | |
6129 | error_out: | |
daf6dff5 | 6130 | /* Add a label so that the back-end doesn't think that the beginning of |
4ee9c684 | 6131 | the switch is unreachable. Note that we do not add a case label, as |
a53ff4c1 | 6132 | that just leads to duplicates and thence to failure later on. */ |
4ee9c684 | 6133 | if (!cases->root) |
6134 | { | |
e60a6f7b | 6135 | tree t = create_artificial_label (loc); |
6136 | add_stmt (build_stmt (loc, LABEL_EXPR, t)); | |
4ee9c684 | 6137 | } |
6138 | return error_mark_node; | |
6139 | } | |
6140 | ||
6141 | /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. | |
6142 | Used to verify that case values match up with enumerator values. */ | |
6143 | ||
6144 | static void | |
6145 | match_case_to_enum_1 (tree key, tree type, tree label) | |
6146 | { | |
e913b5cd | 6147 | char buf[WIDE_INT_PRINT_BUFFER_SIZE]; |
6148 | ||
dd76621f | 6149 | if (tree_fits_uhwi_p (key)) |
6150 | print_dec (key, buf, UNSIGNED); | |
6151 | else if (tree_fits_shwi_p (key)) | |
6152 | print_dec (key, buf, SIGNED); | |
4ee9c684 | 6153 | else |
e913b5cd | 6154 | print_hex (key, buf); |
4ee9c684 | 6155 | |
6156 | if (TYPE_NAME (type) == 0) | |
712d2297 | 6157 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
6158 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
6159 | "case value %qs not in enumerated type", | |
6160 | buf); | |
4ee9c684 | 6161 | else |
712d2297 | 6162 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
6163 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
6164 | "case value %qs not in enumerated type %qT", | |
6165 | buf, type); | |
4ee9c684 | 6166 | } |
6167 | ||
359d87c6 | 6168 | /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. |
6169 | Used to verify that case values match up with enumerator values. */ | |
6170 | ||
4ee9c684 | 6171 | static int |
6172 | match_case_to_enum (splay_tree_node node, void *data) | |
6173 | { | |
6174 | tree label = (tree) node->value; | |
4fd61bc6 | 6175 | tree type = (tree) data; |
4ee9c684 | 6176 | |
6177 | /* Skip default case. */ | |
6178 | if (!CASE_LOW (label)) | |
6179 | return 0; | |
6180 | ||
359d87c6 | 6181 | /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear |
4ee9c684 | 6182 | when we did our enum->case scan. Reset our scratch bit after. */ |
359d87c6 | 6183 | if (!CASE_LOW_SEEN (label)) |
4ee9c684 | 6184 | match_case_to_enum_1 (CASE_LOW (label), type, label); |
6185 | else | |
359d87c6 | 6186 | CASE_LOW_SEEN (label) = 0; |
4ee9c684 | 6187 | |
359d87c6 | 6188 | /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is |
6189 | not set, that means that CASE_HIGH did not appear when we did our | |
6190 | enum->case scan. Reset our scratch bit after. */ | |
4ee9c684 | 6191 | if (CASE_HIGH (label)) |
6192 | { | |
359d87c6 | 6193 | if (!CASE_HIGH_SEEN (label)) |
6194 | match_case_to_enum_1 (CASE_HIGH (label), type, label); | |
6195 | else | |
6196 | CASE_HIGH_SEEN (label) = 0; | |
4ee9c684 | 6197 | } |
6198 | ||
6199 | return 0; | |
6200 | } | |
6201 | ||
e7911019 | 6202 | /* Handle -Wswitch*. Called from the front end after parsing the |
6203 | switch construct. */ | |
6204 | /* ??? Should probably be somewhere generic, since other languages | |
6205 | besides C and C++ would want this. At the moment, however, C/C++ | |
6206 | are the only tree-ssa languages that support enumerations at all, | |
6207 | so the point is moot. */ | |
4ee9c684 | 6208 | |
e7911019 | 6209 | void |
6210 | c_do_switch_warnings (splay_tree cases, location_t switch_location, | |
be23b16f | 6211 | tree type, tree cond, bool bool_cond_p, |
6212 | bool outside_range_p) | |
4ee9c684 | 6213 | { |
b27ac6b5 | 6214 | splay_tree_node default_node; |
359d87c6 | 6215 | splay_tree_node node; |
6216 | tree chain; | |
4ee9c684 | 6217 | |
be23b16f | 6218 | if (!warn_switch && !warn_switch_enum && !warn_switch_default |
6219 | && !warn_switch_bool) | |
4ee9c684 | 6220 | return; |
6221 | ||
4ee9c684 | 6222 | default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); |
8b6866af | 6223 | if (!default_node) |
5fb6a912 | 6224 | warning_at (switch_location, OPT_Wswitch_default, |
6225 | "switch missing default case"); | |
4ee9c684 | 6226 | |
be23b16f | 6227 | /* There are certain cases where -Wswitch-bool warnings aren't |
6228 | desirable, such as | |
6229 | switch (boolean) | |
6230 | { | |
6231 | case true: ... | |
6232 | case false: ... | |
6233 | } | |
6234 | so be careful here. */ | |
6235 | if (warn_switch_bool && bool_cond_p) | |
6236 | { | |
6237 | splay_tree_node min_node; | |
6238 | /* If there's a default node, it's also the value with the minimal | |
6239 | key. So look at the penultimate key (if any). */ | |
6240 | if (default_node) | |
6241 | min_node = splay_tree_successor (cases, (splay_tree_key) NULL); | |
6242 | else | |
6243 | min_node = splay_tree_min (cases); | |
6244 | tree min = min_node ? (tree) min_node->key : NULL_TREE; | |
6245 | ||
6246 | splay_tree_node max_node = splay_tree_max (cases); | |
6247 | /* This might be a case range, so look at the value with the | |
6248 | maximal key and then check CASE_HIGH. */ | |
6249 | tree max = max_node ? (tree) max_node->value : NULL_TREE; | |
6250 | if (max) | |
6251 | max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max); | |
6252 | ||
6253 | /* If there's a case value > 1 or < 0, that is outside bool | |
6254 | range, warn. */ | |
6255 | if (outside_range_p | |
6256 | || (max && wi::gts_p (max, 1)) | |
6257 | || (min && wi::lts_p (min, 0)) | |
6258 | /* And handle the | |
6259 | switch (boolean) | |
6260 | { | |
6261 | case true: ... | |
6262 | case false: ... | |
6263 | default: ... | |
6264 | } | |
6265 | case, where we want to warn. */ | |
6266 | || (default_node | |
6267 | && max && wi::eq_p (max, 1) | |
6268 | && min && wi::eq_p (min, 0))) | |
6269 | warning_at (switch_location, OPT_Wswitch_bool, | |
6270 | "switch condition has boolean value"); | |
6271 | } | |
6272 | ||
47ae02b7 | 6273 | /* From here on, we only care about enumerated types. */ |
359d87c6 | 6274 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) |
6275 | return; | |
6276 | ||
561017b5 | 6277 | /* From here on, we only care about -Wswitch and -Wswitch-enum. */ |
6278 | if (!warn_switch_enum && !warn_switch) | |
359d87c6 | 6279 | return; |
6280 | ||
561017b5 | 6281 | /* Check the cases. Warn about case values which are not members of |
6282 | the enumerated type. For -Wswitch-enum, or for -Wswitch when | |
6283 | there is no default case, check that exactly all enumeration | |
6284 | literals are covered by the cases. */ | |
6285 | ||
359d87c6 | 6286 | /* Clearing COND if it is not an integer constant simplifies |
6287 | the tests inside the loop below. */ | |
6288 | if (TREE_CODE (cond) != INTEGER_CST) | |
6289 | cond = NULL_TREE; | |
6290 | ||
6291 | /* The time complexity here is O(N*lg(N)) worst case, but for the | |
6292 | common case of monotonically increasing enumerators, it is | |
6293 | O(N), since the nature of the splay tree will keep the next | |
6294 | element adjacent to the root at all times. */ | |
4ee9c684 | 6295 | |
359d87c6 | 6296 | for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) |
6297 | { | |
6298 | tree value = TREE_VALUE (chain); | |
3f00a6c0 | 6299 | if (TREE_CODE (value) == CONST_DECL) |
6300 | value = DECL_INITIAL (value); | |
359d87c6 | 6301 | node = splay_tree_lookup (cases, (splay_tree_key) value); |
6302 | if (node) | |
4ee9c684 | 6303 | { |
359d87c6 | 6304 | /* Mark the CASE_LOW part of the case entry as seen. */ |
6305 | tree label = (tree) node->value; | |
6306 | CASE_LOW_SEEN (label) = 1; | |
6307 | continue; | |
6308 | } | |
6309 | ||
6310 | /* Even though there wasn't an exact match, there might be a | |
f0b5f617 | 6311 | case range which includes the enumerator's value. */ |
359d87c6 | 6312 | node = splay_tree_predecessor (cases, (splay_tree_key) value); |
6313 | if (node && CASE_HIGH ((tree) node->value)) | |
6314 | { | |
6315 | tree label = (tree) node->value; | |
6316 | int cmp = tree_int_cst_compare (CASE_HIGH (label), value); | |
6317 | if (cmp >= 0) | |
4ee9c684 | 6318 | { |
359d87c6 | 6319 | /* If we match the upper bound exactly, mark the CASE_HIGH |
6320 | part of the case entry as seen. */ | |
6321 | if (cmp == 0) | |
6322 | CASE_HIGH_SEEN (label) = 1; | |
6323 | continue; | |
4ee9c684 | 6324 | } |
6325 | } | |
6326 | ||
359d87c6 | 6327 | /* We've now determined that this enumerated literal isn't |
6328 | handled by the case labels of the switch statement. */ | |
4ee9c684 | 6329 | |
359d87c6 | 6330 | /* If the switch expression is a constant, we only really care |
6331 | about whether that constant is handled by the switch. */ | |
6332 | if (cond && tree_int_cst_compare (cond, value)) | |
6333 | continue; | |
4ee9c684 | 6334 | |
6cbbbc89 | 6335 | /* If there is a default_node, the only relevant option is |
561017b5 | 6336 | Wswitch-enum. Otherwise, if both are enabled then we prefer |
6cbbbc89 | 6337 | to warn using -Wswitch because -Wswitch is enabled by -Wall |
6338 | while -Wswitch-enum is explicit. */ | |
561017b5 | 6339 | warning_at (switch_location, |
6340 | (default_node || !warn_switch | |
6341 | ? OPT_Wswitch_enum | |
6342 | : OPT_Wswitch), | |
6343 | "enumeration value %qE not handled in switch", | |
6344 | TREE_PURPOSE (chain)); | |
4ee9c684 | 6345 | } |
359d87c6 | 6346 | |
6347 | /* Warn if there are case expressions that don't correspond to | |
6348 | enumerators. This can occur since C and C++ don't enforce | |
6349 | type-checking of assignments to enumeration variables. | |
6350 | ||
6351 | The time complexity here is now always O(N) worst case, since | |
6352 | we should have marked both the lower bound and upper bound of | |
6353 | every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN | |
6354 | above. This scan also resets those fields. */ | |
6cbbbc89 | 6355 | |
359d87c6 | 6356 | splay_tree_foreach (cases, match_case_to_enum, type); |
e41f0d80 | 6357 | } |
6358 | ||
9dd48740 | 6359 | /* Finish an expression taking the address of LABEL (an |
dda49785 | 6360 | IDENTIFIER_NODE). Returns an expression for the address. |
6361 | ||
6362 | LOC is the location for the expression returned. */ | |
d0a47c8d | 6363 | |
1cae46be | 6364 | tree |
dda49785 | 6365 | finish_label_address_expr (tree label, location_t loc) |
d0a47c8d | 6366 | { |
6367 | tree result; | |
6368 | ||
29438999 | 6369 | pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard"); |
d0a47c8d | 6370 | |
9dd48740 | 6371 | if (label == error_mark_node) |
6372 | return error_mark_node; | |
6373 | ||
d0a47c8d | 6374 | label = lookup_label (label); |
6375 | if (label == NULL_TREE) | |
6376 | result = null_pointer_node; | |
6377 | else | |
6378 | { | |
6379 | TREE_USED (label) = 1; | |
6380 | result = build1 (ADDR_EXPR, ptr_type_node, label); | |
344cd9b2 | 6381 | /* The current function is not necessarily uninlinable. |
d0a47c8d | 6382 | Computed gotos are incompatible with inlining, but the value |
6383 | here could be used only in a diagnostic, for example. */ | |
dda49785 | 6384 | protected_set_expr_location (result, loc); |
d0a47c8d | 6385 | } |
6386 | ||
6387 | return result; | |
6388 | } | |
4f9a1c9b | 6389 | \f |
6390 | ||
6391 | /* Given a boolean expression ARG, return a tree representing an increment | |
6392 | or decrement (as indicated by CODE) of ARG. The front end must check for | |
6393 | invalid cases (e.g., decrement in C++). */ | |
6394 | tree | |
1cae46be | 6395 | boolean_increment (enum tree_code code, tree arg) |
4f9a1c9b | 6396 | { |
6397 | tree val; | |
69db191c | 6398 | tree true_res = build_int_cst (TREE_TYPE (arg), 1); |
c0f19401 | 6399 | |
4f9a1c9b | 6400 | arg = stabilize_reference (arg); |
6401 | switch (code) | |
6402 | { | |
6403 | case PREINCREMENT_EXPR: | |
14ae0310 | 6404 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 6405 | break; |
6406 | case POSTINCREMENT_EXPR: | |
14ae0310 | 6407 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 6408 | arg = save_expr (arg); |
14ae0310 | 6409 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
6410 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 6411 | break; |
6412 | case PREDECREMENT_EXPR: | |
14ae0310 | 6413 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
389dd41b | 6414 | invert_truthvalue_loc (input_location, arg)); |
4f9a1c9b | 6415 | break; |
6416 | case POSTDECREMENT_EXPR: | |
14ae0310 | 6417 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
389dd41b | 6418 | invert_truthvalue_loc (input_location, arg)); |
4f9a1c9b | 6419 | arg = save_expr (arg); |
14ae0310 | 6420 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
6421 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 6422 | break; |
6423 | default: | |
231bd014 | 6424 | gcc_unreachable (); |
4f9a1c9b | 6425 | } |
6426 | TREE_SIDE_EFFECTS (val) = 1; | |
6427 | return val; | |
6428 | } | |
76a6e674 | 6429 | \f |
f3449a3c | 6430 | /* Built-in macros for stddef.h and stdint.h, that require macros |
6431 | defined in this file. */ | |
79cf3ec1 | 6432 | void |
1cae46be | 6433 | c_stddef_cpp_builtins(void) |
1ed9d5f5 | 6434 | { |
63994318 | 6435 | builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0); |
6436 | builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0); | |
6437 | builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0); | |
6438 | builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0); | |
36bccbfc | 6439 | builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0); |
6440 | builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0); | |
b0726616 | 6441 | builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0); |
6442 | builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0); | |
f3449a3c | 6443 | if (SIG_ATOMIC_TYPE) |
6444 | builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0); | |
6445 | if (INT8_TYPE) | |
6446 | builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0); | |
6447 | if (INT16_TYPE) | |
6448 | builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0); | |
6449 | if (INT32_TYPE) | |
6450 | builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0); | |
6451 | if (INT64_TYPE) | |
6452 | builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0); | |
6453 | if (UINT8_TYPE) | |
6454 | builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0); | |
6455 | if (UINT16_TYPE) | |
6456 | builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0); | |
6457 | if (UINT32_TYPE) | |
6458 | builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0); | |
6459 | if (UINT64_TYPE) | |
6460 | builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0); | |
6461 | if (INT_LEAST8_TYPE) | |
6462 | builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0); | |
6463 | if (INT_LEAST16_TYPE) | |
6464 | builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0); | |
6465 | if (INT_LEAST32_TYPE) | |
6466 | builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0); | |
6467 | if (INT_LEAST64_TYPE) | |
6468 | builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0); | |
6469 | if (UINT_LEAST8_TYPE) | |
6470 | builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0); | |
6471 | if (UINT_LEAST16_TYPE) | |
6472 | builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0); | |
6473 | if (UINT_LEAST32_TYPE) | |
6474 | builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0); | |
6475 | if (UINT_LEAST64_TYPE) | |
6476 | builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0); | |
6477 | if (INT_FAST8_TYPE) | |
6478 | builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0); | |
6479 | if (INT_FAST16_TYPE) | |
6480 | builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0); | |
6481 | if (INT_FAST32_TYPE) | |
6482 | builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0); | |
6483 | if (INT_FAST64_TYPE) | |
6484 | builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0); | |
6485 | if (UINT_FAST8_TYPE) | |
6486 | builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0); | |
6487 | if (UINT_FAST16_TYPE) | |
6488 | builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0); | |
6489 | if (UINT_FAST32_TYPE) | |
6490 | builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0); | |
6491 | if (UINT_FAST64_TYPE) | |
6492 | builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0); | |
6493 | if (INTPTR_TYPE) | |
6494 | builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0); | |
6495 | if (UINTPTR_TYPE) | |
6496 | builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0); | |
574006c3 | 6497 | } |
6498 | ||
7d3b509a | 6499 | static void |
1cae46be | 6500 | c_init_attributes (void) |
7d3b509a | 6501 | { |
6502 | /* Fill in the built_in_attributes array. */ | |
7c446c95 | 6503 | #define DEF_ATTR_NULL_TREE(ENUM) \ |
7d3b509a | 6504 | built_in_attributes[(int) ENUM] = NULL_TREE; |
7c446c95 | 6505 | #define DEF_ATTR_INT(ENUM, VALUE) \ |
ceb7b692 | 6506 | built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE); |
c8010b80 | 6507 | #define DEF_ATTR_STRING(ENUM, VALUE) \ |
6508 | built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE); | |
7d3b509a | 6509 | #define DEF_ATTR_IDENT(ENUM, STRING) \ |
6510 | built_in_attributes[(int) ENUM] = get_identifier (STRING); | |
6511 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \ | |
6512 | built_in_attributes[(int) ENUM] \ | |
6513 | = tree_cons (built_in_attributes[(int) PURPOSE], \ | |
6514 | built_in_attributes[(int) VALUE], \ | |
6515 | built_in_attributes[(int) CHAIN]); | |
7d3b509a | 6516 | #include "builtin-attrs.def" |
6517 | #undef DEF_ATTR_NULL_TREE | |
6518 | #undef DEF_ATTR_INT | |
6519 | #undef DEF_ATTR_IDENT | |
6520 | #undef DEF_ATTR_TREE_LIST | |
76a6e674 | 6521 | } |
5f3cead1 | 6522 | |
33199a81 | 6523 | /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain |
6524 | identifier as an argument, so the front end shouldn't look it up. */ | |
6525 | ||
6526 | bool | |
47b19b94 | 6527 | attribute_takes_identifier_p (const_tree attr_id) |
33199a81 | 6528 | { |
9bf1c74e | 6529 | const struct attribute_spec *spec = lookup_attribute_spec (attr_id); |
02cb1060 | 6530 | if (spec == NULL) |
6531 | /* Unknown attribute that we'll end up ignoring, return true so we | |
6532 | don't complain about an identifier argument. */ | |
6533 | return true; | |
6534 | else if (!strcmp ("mode", spec->name) | |
6535 | || !strcmp ("format", spec->name) | |
6536 | || !strcmp ("cleanup", spec->name)) | |
47b19b94 | 6537 | return true; |
6538 | else | |
6539 | return targetm.attribute_takes_identifier_p (attr_id); | |
33199a81 | 6540 | } |
6541 | ||
f8e93a2e | 6542 | /* Attribute handlers common to C front ends. */ |
6543 | ||
6544 | /* Handle a "packed" attribute; arguments as in | |
6545 | struct attribute_spec.handler. */ | |
6546 | ||
6547 | static tree | |
9a03a746 | 6548 | handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
09347743 | 6549 | int flags, bool *no_add_attrs) |
f8e93a2e | 6550 | { |
f40175cb | 6551 | if (TYPE_P (*node)) |
f8e93a2e | 6552 | { |
6553 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 6554 | *node = build_variant_type_copy (*node); |
f40175cb | 6555 | TYPE_PACKED (*node) = 1; |
f8e93a2e | 6556 | } |
6557 | else if (TREE_CODE (*node) == FIELD_DECL) | |
c2ab04f9 | 6558 | { |
9fd767c5 | 6559 | if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT |
6560 | /* Still pack bitfields. */ | |
6561 | && ! DECL_INITIAL (*node)) | |
c2ab04f9 | 6562 | warning (OPT_Wattributes, |
6563 | "%qE attribute ignored for field of type %qT", | |
6564 | name, TREE_TYPE (*node)); | |
6565 | else | |
6566 | DECL_PACKED (*node) = 1; | |
6567 | } | |
f8e93a2e | 6568 | /* We can't set DECL_PACKED for a VAR_DECL, because the bit is |
f40175cb | 6569 | used for DECL_REGISTER. It wouldn't mean anything anyway. |
6570 | We can't set DECL_PACKED on the type of a TYPE_DECL, because | |
6571 | that changes what the typedef is typing. */ | |
f8e93a2e | 6572 | else |
6573 | { | |
9b2d6d13 | 6574 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6575 | *no_add_attrs = true; |
6576 | } | |
6577 | ||
6578 | return NULL_TREE; | |
6579 | } | |
6580 | ||
6581 | /* Handle a "nocommon" attribute; arguments as in | |
6582 | struct attribute_spec.handler. */ | |
6583 | ||
6584 | static tree | |
1cae46be | 6585 | handle_nocommon_attribute (tree *node, tree name, |
9a03a746 | 6586 | tree ARG_UNUSED (args), |
6587 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6588 | { |
f48c7f4a | 6589 | if (VAR_P (*node)) |
f8e93a2e | 6590 | DECL_COMMON (*node) = 0; |
6591 | else | |
6592 | { | |
9b2d6d13 | 6593 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6594 | *no_add_attrs = true; |
6595 | } | |
6596 | ||
6597 | return NULL_TREE; | |
6598 | } | |
6599 | ||
6600 | /* Handle a "common" attribute; arguments as in | |
6601 | struct attribute_spec.handler. */ | |
6602 | ||
6603 | static tree | |
9a03a746 | 6604 | handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6605 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6606 | { |
f48c7f4a | 6607 | if (VAR_P (*node)) |
f8e93a2e | 6608 | DECL_COMMON (*node) = 1; |
6609 | else | |
6610 | { | |
9b2d6d13 | 6611 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6612 | *no_add_attrs = true; |
6613 | } | |
6614 | ||
6615 | return NULL_TREE; | |
6616 | } | |
6617 | ||
6618 | /* Handle a "noreturn" attribute; arguments as in | |
6619 | struct attribute_spec.handler. */ | |
6620 | ||
6621 | static tree | |
9a03a746 | 6622 | handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6623 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6624 | { |
6625 | tree type = TREE_TYPE (*node); | |
6626 | ||
6627 | /* See FIXME comment in c_common_attribute_table. */ | |
8c582e4f | 6628 | if (TREE_CODE (*node) == FUNCTION_DECL |
6629 | || objc_method_decl (TREE_CODE (*node))) | |
f8e93a2e | 6630 | TREE_THIS_VOLATILE (*node) = 1; |
6631 | else if (TREE_CODE (type) == POINTER_TYPE | |
6632 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
6633 | TREE_TYPE (*node) | |
9d4eeb52 | 6634 | = (build_qualified_type |
6635 | (build_pointer_type | |
6636 | (build_type_variant (TREE_TYPE (type), | |
6637 | TYPE_READONLY (TREE_TYPE (type)), 1)), | |
6638 | TYPE_QUALS (type))); | |
f8e93a2e | 6639 | else |
6640 | { | |
9b2d6d13 | 6641 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6642 | *no_add_attrs = true; |
6643 | } | |
6644 | ||
6645 | return NULL_TREE; | |
6646 | } | |
6647 | ||
5de92639 | 6648 | /* Handle a "hot" and attribute; arguments as in |
6649 | struct attribute_spec.handler. */ | |
6650 | ||
6651 | static tree | |
6652 | handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
46f8e3b0 | 6653 | int ARG_UNUSED (flags), bool *no_add_attrs) |
5de92639 | 6654 | { |
758a38ab | 6655 | if (TREE_CODE (*node) == FUNCTION_DECL |
6656 | || TREE_CODE (*node) == LABEL_DECL) | |
5de92639 | 6657 | { |
6658 | if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL) | |
6659 | { | |
4a026b48 | 6660 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " |
6661 | "with attribute %qs", name, "cold"); | |
5de92639 | 6662 | *no_add_attrs = true; |
6663 | } | |
24470055 | 6664 | /* Most of the rest of the hot processing is done later with |
6665 | lookup_attribute. */ | |
5de92639 | 6666 | } |
6667 | else | |
6668 | { | |
6669 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6670 | *no_add_attrs = true; | |
6671 | } | |
6672 | ||
6673 | return NULL_TREE; | |
6674 | } | |
758a38ab | 6675 | |
5de92639 | 6676 | /* Handle a "cold" and attribute; arguments as in |
6677 | struct attribute_spec.handler. */ | |
6678 | ||
6679 | static tree | |
6680 | handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
6681 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6682 | { | |
758a38ab | 6683 | if (TREE_CODE (*node) == FUNCTION_DECL |
6684 | || TREE_CODE (*node) == LABEL_DECL) | |
5de92639 | 6685 | { |
6686 | if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL) | |
6687 | { | |
4a026b48 | 6688 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " |
6689 | "with attribute %qs", name, "hot"); | |
5de92639 | 6690 | *no_add_attrs = true; |
6691 | } | |
24470055 | 6692 | /* Most of the rest of the cold processing is done later with |
6693 | lookup_attribute. */ | |
5de92639 | 6694 | } |
6695 | else | |
6696 | { | |
6697 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6698 | *no_add_attrs = true; | |
6699 | } | |
6700 | ||
6701 | return NULL_TREE; | |
6702 | } | |
6703 | ||
a9196da9 | 6704 | /* Handle a "no_sanitize_address" attribute; arguments as in |
d413ffdd | 6705 | struct attribute_spec.handler. */ |
6706 | ||
6707 | static tree | |
a9196da9 | 6708 | handle_no_sanitize_address_attribute (tree *node, tree name, tree, int, |
6709 | bool *no_add_attrs) | |
d413ffdd | 6710 | { |
6711 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6712 | { | |
6713 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6714 | *no_add_attrs = true; | |
6715 | } | |
6716 | ||
6717 | return NULL_TREE; | |
6718 | } | |
6719 | ||
a9196da9 | 6720 | /* Handle a "no_address_safety_analysis" attribute; arguments as in |
6721 | struct attribute_spec.handler. */ | |
6722 | ||
6723 | static tree | |
6724 | handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int, | |
6725 | bool *no_add_attrs) | |
6726 | { | |
6727 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6728 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6729 | else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node))) | |
6730 | DECL_ATTRIBUTES (*node) | |
6731 | = tree_cons (get_identifier ("no_sanitize_address"), | |
6732 | NULL_TREE, DECL_ATTRIBUTES (*node)); | |
6733 | *no_add_attrs = true; | |
6734 | return NULL_TREE; | |
6735 | } | |
6736 | ||
05f893e1 | 6737 | /* Handle a "no_sanitize_undefined" attribute; arguments as in |
6738 | struct attribute_spec.handler. */ | |
6739 | ||
6740 | static tree | |
6741 | handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int, | |
6742 | bool *no_add_attrs) | |
6743 | { | |
6744 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6745 | { | |
6746 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6747 | *no_add_attrs = true; | |
6748 | } | |
6749 | ||
6750 | return NULL_TREE; | |
6751 | } | |
6752 | ||
947aa916 | 6753 | /* Handle a "stack_protect" attribute; arguments as in |
6754 | struct attribute_spec.handler. */ | |
6755 | static tree | |
6756 | handle_stack_protect_attribute (tree *node, tree name, tree, int, | |
6757 | bool *no_add_attrs) | |
6758 | { | |
6759 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6760 | { | |
6761 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6762 | *no_add_attrs = true; | |
6763 | } | |
6764 | else | |
6765 | DECL_ATTRIBUTES (*node) | |
6766 | = tree_cons (get_identifier ("stack_protect"), | |
6767 | NULL_TREE, DECL_ATTRIBUTES (*node)); | |
6768 | ||
6769 | return NULL_TREE; | |
6770 | } | |
6771 | ||
f8e93a2e | 6772 | /* Handle a "noinline" attribute; arguments as in |
6773 | struct attribute_spec.handler. */ | |
6774 | ||
6775 | static tree | |
1cae46be | 6776 | handle_noinline_attribute (tree *node, tree name, |
9a03a746 | 6777 | tree ARG_UNUSED (args), |
6778 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6779 | { |
6780 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
4a026b48 | 6781 | { |
6782 | if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node))) | |
6783 | { | |
6784 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " | |
6785 | "with attribute %qs", name, "always_inline"); | |
6786 | *no_add_attrs = true; | |
6787 | } | |
6788 | else | |
6789 | DECL_UNINLINABLE (*node) = 1; | |
6790 | } | |
f8e93a2e | 6791 | else |
6792 | { | |
9b2d6d13 | 6793 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6794 | *no_add_attrs = true; |
6795 | } | |
6796 | ||
6797 | return NULL_TREE; | |
6798 | } | |
6799 | ||
bdb1f0d1 | 6800 | /* Handle a "noclone" attribute; arguments as in |
6801 | struct attribute_spec.handler. */ | |
6802 | ||
6803 | static tree | |
6804 | handle_noclone_attribute (tree *node, tree name, | |
6805 | tree ARG_UNUSED (args), | |
6806 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6807 | { | |
6808 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6809 | { | |
6810 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6811 | *no_add_attrs = true; | |
6812 | } | |
6813 | ||
6814 | return NULL_TREE; | |
6815 | } | |
6816 | ||
85fbea97 | 6817 | /* Handle a "no_icf" attribute; arguments as in |
6818 | struct attribute_spec.handler. */ | |
6819 | ||
6820 | static tree | |
6821 | handle_noicf_attribute (tree *node, tree name, | |
6822 | tree ARG_UNUSED (args), | |
6823 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6824 | { | |
6825 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6826 | { | |
6827 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6828 | *no_add_attrs = true; | |
6829 | } | |
6830 | ||
6831 | return NULL_TREE; | |
6832 | } | |
6833 | ||
6834 | ||
f8e93a2e | 6835 | /* Handle a "always_inline" attribute; arguments as in |
6836 | struct attribute_spec.handler. */ | |
6837 | ||
6838 | static tree | |
1cae46be | 6839 | handle_always_inline_attribute (tree *node, tree name, |
9a03a746 | 6840 | tree ARG_UNUSED (args), |
6841 | int ARG_UNUSED (flags), | |
09347743 | 6842 | bool *no_add_attrs) |
f8e93a2e | 6843 | { |
6844 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6845 | { | |
4a026b48 | 6846 | if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node))) |
6847 | { | |
6848 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " | |
6849 | "with %qs attribute", name, "noinline"); | |
6850 | *no_add_attrs = true; | |
6851 | } | |
ab50af2a | 6852 | else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node))) |
6853 | { | |
6854 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " | |
6855 | "with %qs attribute", name, "target_clones"); | |
6856 | *no_add_attrs = true; | |
6857 | } | |
4a026b48 | 6858 | else |
6859 | /* Set the attribute and mark it for disregarding inline | |
6860 | limits. */ | |
6861 | DECL_DISREGARD_INLINE_LIMITS (*node) = 1; | |
f8e93a2e | 6862 | } |
6863 | else | |
6864 | { | |
9b2d6d13 | 6865 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6866 | *no_add_attrs = true; |
541e4101 | 6867 | } |
6868 | ||
6869 | return NULL_TREE; | |
6870 | } | |
6871 | ||
6872 | /* Handle a "gnu_inline" attribute; arguments as in | |
6873 | struct attribute_spec.handler. */ | |
6874 | ||
6875 | static tree | |
6876 | handle_gnu_inline_attribute (tree *node, tree name, | |
6877 | tree ARG_UNUSED (args), | |
6878 | int ARG_UNUSED (flags), | |
6879 | bool *no_add_attrs) | |
6880 | { | |
6881 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6882 | { | |
6883 | /* Do nothing else, just set the attribute. We'll get at | |
6884 | it later with lookup_attribute. */ | |
6885 | } | |
6886 | else | |
6887 | { | |
6888 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6889 | *no_add_attrs = true; | |
7bd95dfd | 6890 | } |
6891 | ||
6892 | return NULL_TREE; | |
6893 | } | |
6894 | ||
6895 | /* Handle a "leaf" attribute; arguments as in | |
6896 | struct attribute_spec.handler. */ | |
6897 | ||
6898 | static tree | |
6899 | handle_leaf_attribute (tree *node, tree name, | |
6900 | tree ARG_UNUSED (args), | |
6901 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6902 | { | |
6903 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6904 | { | |
6905 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6906 | *no_add_attrs = true; | |
6907 | } | |
6908 | if (!TREE_PUBLIC (*node)) | |
6909 | { | |
6910 | warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name); | |
6911 | *no_add_attrs = true; | |
f8e93a2e | 6912 | } |
6913 | ||
6914 | return NULL_TREE; | |
6915 | } | |
6916 | ||
1b16fc45 | 6917 | /* Handle an "artificial" attribute; arguments as in |
6918 | struct attribute_spec.handler. */ | |
6919 | ||
6920 | static tree | |
6921 | handle_artificial_attribute (tree *node, tree name, | |
6922 | tree ARG_UNUSED (args), | |
6923 | int ARG_UNUSED (flags), | |
6924 | bool *no_add_attrs) | |
6925 | { | |
6926 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6927 | { | |
6928 | /* Do nothing else, just set the attribute. We'll get at | |
6929 | it later with lookup_attribute. */ | |
6930 | } | |
6931 | else | |
6932 | { | |
6933 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6934 | *no_add_attrs = true; | |
6935 | } | |
6936 | ||
6937 | return NULL_TREE; | |
6938 | } | |
6939 | ||
0cdd9887 | 6940 | /* Handle a "flatten" attribute; arguments as in |
6941 | struct attribute_spec.handler. */ | |
6942 | ||
6943 | static tree | |
6944 | handle_flatten_attribute (tree *node, tree name, | |
a0c938f0 | 6945 | tree args ATTRIBUTE_UNUSED, |
6946 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
0cdd9887 | 6947 | { |
6948 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6949 | /* Do nothing else, just set the attribute. We'll get at | |
6950 | it later with lookup_attribute. */ | |
6951 | ; | |
6952 | else | |
6953 | { | |
6954 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6955 | *no_add_attrs = true; | |
6956 | } | |
6957 | ||
6958 | return NULL_TREE; | |
6959 | } | |
6960 | ||
10fc867f | 6961 | /* Handle a "warning" or "error" attribute; arguments as in |
6962 | struct attribute_spec.handler. */ | |
6963 | ||
6964 | static tree | |
6965 | handle_error_attribute (tree *node, tree name, tree args, | |
6966 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6967 | { | |
6968 | if (TREE_CODE (*node) == FUNCTION_DECL | |
df936998 | 6969 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) |
10fc867f | 6970 | /* Do nothing else, just set the attribute. We'll get at |
6971 | it later with lookup_attribute. */ | |
6972 | ; | |
6973 | else | |
6974 | { | |
6975 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6976 | *no_add_attrs = true; | |
6977 | } | |
6978 | ||
6979 | return NULL_TREE; | |
6980 | } | |
0cdd9887 | 6981 | |
f8e93a2e | 6982 | /* Handle a "used" attribute; arguments as in |
6983 | struct attribute_spec.handler. */ | |
6984 | ||
6985 | static tree | |
9a03a746 | 6986 | handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args), |
6987 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6988 | { |
d0a31bd8 | 6989 | tree node = *pnode; |
6990 | ||
6991 | if (TREE_CODE (node) == FUNCTION_DECL | |
f48c7f4a | 6992 | || (VAR_P (node) && TREE_STATIC (node)) |
a4e3ffad | 6993 | || (TREE_CODE (node) == TYPE_DECL)) |
f54ed8bc | 6994 | { |
f54ed8bc | 6995 | TREE_USED (node) = 1; |
9423c9b7 | 6996 | DECL_PRESERVE_P (node) = 1; |
f48c7f4a | 6997 | if (VAR_P (node)) |
abc6c64f | 6998 | DECL_READ_P (node) = 1; |
f54ed8bc | 6999 | } |
f8e93a2e | 7000 | else |
7001 | { | |
9b2d6d13 | 7002 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7003 | *no_add_attrs = true; |
7004 | } | |
7005 | ||
7006 | return NULL_TREE; | |
7007 | } | |
7008 | ||
7009 | /* Handle a "unused" attribute; arguments as in | |
7010 | struct attribute_spec.handler. */ | |
7011 | ||
7012 | static tree | |
9a03a746 | 7013 | handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
7014 | int flags, bool *no_add_attrs) | |
f8e93a2e | 7015 | { |
7016 | if (DECL_P (*node)) | |
7017 | { | |
7018 | tree decl = *node; | |
7019 | ||
7020 | if (TREE_CODE (decl) == PARM_DECL | |
4e81b384 | 7021 | || VAR_OR_FUNCTION_DECL_P (decl) |
f8e93a2e | 7022 | || TREE_CODE (decl) == LABEL_DECL |
7023 | || TREE_CODE (decl) == TYPE_DECL) | |
abc6c64f | 7024 | { |
7025 | TREE_USED (decl) = 1; | |
f48c7f4a | 7026 | if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL) |
abc6c64f | 7027 | DECL_READ_P (decl) = 1; |
7028 | } | |
f8e93a2e | 7029 | else |
7030 | { | |
9b2d6d13 | 7031 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7032 | *no_add_attrs = true; |
7033 | } | |
7034 | } | |
7035 | else | |
7036 | { | |
7037 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 7038 | *node = build_variant_type_copy (*node); |
f8e93a2e | 7039 | TREE_USED (*node) = 1; |
7040 | } | |
7041 | ||
7042 | return NULL_TREE; | |
7043 | } | |
7044 | ||
62eec3b4 | 7045 | /* Handle a "externally_visible" attribute; arguments as in |
7046 | struct attribute_spec.handler. */ | |
7047 | ||
7048 | static tree | |
7049 | handle_externally_visible_attribute (tree *pnode, tree name, | |
7050 | tree ARG_UNUSED (args), | |
7051 | int ARG_UNUSED (flags), | |
7052 | bool *no_add_attrs) | |
7053 | { | |
7054 | tree node = *pnode; | |
7055 | ||
b443c459 | 7056 | if (VAR_OR_FUNCTION_DECL_P (node)) |
62eec3b4 | 7057 | { |
ba12ea31 | 7058 | if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL |
7059 | && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node)) | |
7060 | { | |
7061 | warning (OPT_Wattributes, | |
7062 | "%qE attribute have effect only on public objects", name); | |
7063 | *no_add_attrs = true; | |
7064 | } | |
62eec3b4 | 7065 | } |
62eec3b4 | 7066 | else |
7067 | { | |
7068 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7069 | *no_add_attrs = true; | |
7070 | } | |
7071 | ||
7072 | return NULL_TREE; | |
7073 | } | |
7074 | ||
6b722052 | 7075 | /* Handle the "no_reorder" attribute. Arguments as in |
7076 | struct attribute_spec.handler. */ | |
7077 | ||
7078 | static tree | |
7079 | handle_no_reorder_attribute (tree *pnode, | |
7080 | tree name, | |
7081 | tree, | |
7082 | int, | |
7083 | bool *no_add_attrs) | |
7084 | { | |
7085 | tree node = *pnode; | |
7086 | ||
b443c459 | 7087 | if (!VAR_OR_FUNCTION_DECL_P (node) |
6b722052 | 7088 | && !(TREE_STATIC (node) || DECL_EXTERNAL (node))) |
7089 | { | |
7090 | warning (OPT_Wattributes, | |
7091 | "%qE attribute only affects top level objects", | |
7092 | name); | |
7093 | *no_add_attrs = true; | |
7094 | } | |
7095 | ||
7096 | return NULL_TREE; | |
7097 | } | |
7098 | ||
f8e93a2e | 7099 | /* Handle a "const" attribute; arguments as in |
7100 | struct attribute_spec.handler. */ | |
7101 | ||
7102 | static tree | |
9a03a746 | 7103 | handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
7104 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7105 | { |
7106 | tree type = TREE_TYPE (*node); | |
7107 | ||
7108 | /* See FIXME comment on noreturn in c_common_attribute_table. */ | |
7109 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
7110 | TREE_READONLY (*node) = 1; | |
7111 | else if (TREE_CODE (type) == POINTER_TYPE | |
7112 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
7113 | TREE_TYPE (*node) | |
9d4eeb52 | 7114 | = (build_qualified_type |
7115 | (build_pointer_type | |
7116 | (build_type_variant (TREE_TYPE (type), 1, | |
7117 | TREE_THIS_VOLATILE (TREE_TYPE (type)))), | |
7118 | TYPE_QUALS (type))); | |
f8e93a2e | 7119 | else |
7120 | { | |
9b2d6d13 | 7121 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7122 | *no_add_attrs = true; |
7123 | } | |
7124 | ||
7125 | return NULL_TREE; | |
7126 | } | |
7127 | ||
292237f3 | 7128 | /* Handle a "scalar_storage_order" attribute; arguments as in |
7129 | struct attribute_spec.handler. */ | |
7130 | ||
7131 | static tree | |
7132 | handle_scalar_storage_order_attribute (tree *node, tree name, tree args, | |
7133 | int flags, bool *no_add_attrs) | |
7134 | { | |
7135 | tree id = TREE_VALUE (args); | |
7136 | tree type; | |
7137 | ||
7138 | if (TREE_CODE (*node) == TYPE_DECL | |
7139 | && ! (flags & ATTR_FLAG_CXX11)) | |
7140 | node = &TREE_TYPE (*node); | |
7141 | type = *node; | |
7142 | ||
7143 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) | |
7144 | { | |
7145 | error ("scalar_storage_order is not supported because endianness " | |
7146 | "is not uniform"); | |
7147 | return NULL_TREE; | |
7148 | } | |
7149 | ||
7150 | if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ()) | |
7151 | { | |
7152 | bool reverse = false; | |
7153 | ||
7154 | if (TREE_CODE (id) == STRING_CST | |
7155 | && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0) | |
7156 | reverse = !BYTES_BIG_ENDIAN; | |
7157 | else if (TREE_CODE (id) == STRING_CST | |
7158 | && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0) | |
7159 | reverse = BYTES_BIG_ENDIAN; | |
7160 | else | |
7161 | { | |
7162 | error ("scalar_storage_order argument must be one of \"big-endian\"" | |
7163 | " or \"little-endian\""); | |
7164 | return NULL_TREE; | |
7165 | } | |
7166 | ||
7167 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
7168 | { | |
7169 | if (reverse) | |
7170 | /* A type variant isn't good enough, since we don't want a cast | |
7171 | to such a type to be removed as a no-op. */ | |
7172 | *node = type = build_duplicate_type (type); | |
7173 | } | |
7174 | ||
7175 | TYPE_REVERSE_STORAGE_ORDER (type) = reverse; | |
7176 | return NULL_TREE; | |
7177 | } | |
7178 | ||
7179 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7180 | *no_add_attrs = true; | |
7181 | return NULL_TREE; | |
7182 | } | |
7183 | ||
f8e93a2e | 7184 | /* Handle a "transparent_union" attribute; arguments as in |
7185 | struct attribute_spec.handler. */ | |
7186 | ||
7187 | static tree | |
1cae46be | 7188 | handle_transparent_union_attribute (tree *node, tree name, |
9a03a746 | 7189 | tree ARG_UNUSED (args), int flags, |
09347743 | 7190 | bool *no_add_attrs) |
f8e93a2e | 7191 | { |
881eb642 | 7192 | tree type; |
03908818 | 7193 | |
7194 | *no_add_attrs = true; | |
f8e93a2e | 7195 | |
ffcdbf9c | 7196 | if (TREE_CODE (*node) == TYPE_DECL |
7197 | && ! (flags & ATTR_FLAG_CXX11)) | |
881eb642 | 7198 | node = &TREE_TYPE (*node); |
7199 | type = *node; | |
f8e93a2e | 7200 | |
03908818 | 7201 | if (TREE_CODE (type) == UNION_TYPE) |
f8e93a2e | 7202 | { |
fca86134 | 7203 | /* Make sure that the first field will work for a transparent union. |
7204 | If the type isn't complete yet, leave the check to the code in | |
7205 | finish_struct. */ | |
7206 | if (TYPE_SIZE (type)) | |
7207 | { | |
7208 | tree first = first_field (type); | |
7209 | if (first == NULL_TREE | |
7210 | || DECL_ARTIFICIAL (first) | |
7211 | || TYPE_MODE (type) != DECL_MODE (first)) | |
7212 | goto ignored; | |
7213 | } | |
7214 | ||
f8e93a2e | 7215 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
03908818 | 7216 | { |
fca86134 | 7217 | /* If the type isn't complete yet, setting the flag |
7218 | on a variant wouldn't ever be checked. */ | |
7219 | if (!TYPE_SIZE (type)) | |
7220 | goto ignored; | |
7221 | ||
7222 | /* build_duplicate_type doesn't work for C++. */ | |
7223 | if (c_dialect_cxx ()) | |
03908818 | 7224 | goto ignored; |
7225 | ||
292237f3 | 7226 | /* A type variant isn't good enough, since we don't want a cast |
7227 | to such a type to be removed as a no-op. */ | |
03908818 | 7228 | *node = type = build_duplicate_type (type); |
7229 | } | |
7230 | ||
c25b3e3f | 7231 | for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
7232 | TYPE_TRANSPARENT_AGGR (t) = 1; | |
03908818 | 7233 | return NULL_TREE; |
f8e93a2e | 7234 | } |
7235 | ||
03908818 | 7236 | ignored: |
7237 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 7238 | return NULL_TREE; |
7239 | } | |
7240 | ||
9af7fd5b | 7241 | /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to |
7242 | get the requested priority for a constructor or destructor, | |
7243 | possibly issuing diagnostics for invalid or reserved | |
7244 | priorities. */ | |
7245 | ||
7246 | static priority_type | |
7247 | get_priority (tree args, bool is_destructor) | |
7248 | { | |
7249 | HOST_WIDE_INT pri; | |
6c181a06 | 7250 | tree arg; |
9af7fd5b | 7251 | |
7252 | if (!args) | |
7253 | return DEFAULT_INIT_PRIORITY; | |
48e1416a | 7254 | |
28fbc04f | 7255 | if (!SUPPORTS_INIT_PRIORITY) |
7256 | { | |
7257 | if (is_destructor) | |
7258 | error ("destructor priorities are not supported"); | |
7259 | else | |
7260 | error ("constructor priorities are not supported"); | |
7261 | return DEFAULT_INIT_PRIORITY; | |
7262 | } | |
7263 | ||
6c181a06 | 7264 | arg = TREE_VALUE (args); |
253e1cae | 7265 | if (TREE_CODE (arg) == IDENTIFIER_NODE) |
7266 | goto invalid; | |
7267 | if (arg == error_mark_node) | |
7268 | return DEFAULT_INIT_PRIORITY; | |
c28ddc97 | 7269 | arg = default_conversion (arg); |
e913b5cd | 7270 | if (!tree_fits_shwi_p (arg) |
6c181a06 | 7271 | || !INTEGRAL_TYPE_P (TREE_TYPE (arg))) |
9af7fd5b | 7272 | goto invalid; |
7273 | ||
e913b5cd | 7274 | pri = tree_to_shwi (arg); |
9af7fd5b | 7275 | if (pri < 0 || pri > MAX_INIT_PRIORITY) |
7276 | goto invalid; | |
7277 | ||
7278 | if (pri <= MAX_RESERVED_INIT_PRIORITY) | |
7279 | { | |
7280 | if (is_destructor) | |
7281 | warning (0, | |
7282 | "destructor priorities from 0 to %d are reserved " | |
48e1416a | 7283 | "for the implementation", |
9af7fd5b | 7284 | MAX_RESERVED_INIT_PRIORITY); |
7285 | else | |
7286 | warning (0, | |
7287 | "constructor priorities from 0 to %d are reserved " | |
48e1416a | 7288 | "for the implementation", |
9af7fd5b | 7289 | MAX_RESERVED_INIT_PRIORITY); |
7290 | } | |
7291 | return pri; | |
7292 | ||
7293 | invalid: | |
7294 | if (is_destructor) | |
7295 | error ("destructor priorities must be integers from 0 to %d inclusive", | |
7296 | MAX_INIT_PRIORITY); | |
7297 | else | |
7298 | error ("constructor priorities must be integers from 0 to %d inclusive", | |
7299 | MAX_INIT_PRIORITY); | |
7300 | return DEFAULT_INIT_PRIORITY; | |
7301 | } | |
7302 | ||
f8e93a2e | 7303 | /* Handle a "constructor" attribute; arguments as in |
7304 | struct attribute_spec.handler. */ | |
7305 | ||
7306 | static tree | |
9af7fd5b | 7307 | handle_constructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 7308 | int ARG_UNUSED (flags), |
09347743 | 7309 | bool *no_add_attrs) |
f8e93a2e | 7310 | { |
7311 | tree decl = *node; | |
7312 | tree type = TREE_TYPE (decl); | |
7313 | ||
7314 | if (TREE_CODE (decl) == FUNCTION_DECL | |
7315 | && TREE_CODE (type) == FUNCTION_TYPE | |
7316 | && decl_function_context (decl) == 0) | |
7317 | { | |
9af7fd5b | 7318 | priority_type priority; |
f8e93a2e | 7319 | DECL_STATIC_CONSTRUCTOR (decl) = 1; |
9af7fd5b | 7320 | priority = get_priority (args, /*is_destructor=*/false); |
7321 | SET_DECL_INIT_PRIORITY (decl, priority); | |
f8e93a2e | 7322 | TREE_USED (decl) = 1; |
7323 | } | |
7324 | else | |
7325 | { | |
9b2d6d13 | 7326 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7327 | *no_add_attrs = true; |
7328 | } | |
7329 | ||
7330 | return NULL_TREE; | |
7331 | } | |
7332 | ||
7333 | /* Handle a "destructor" attribute; arguments as in | |
7334 | struct attribute_spec.handler. */ | |
7335 | ||
7336 | static tree | |
9af7fd5b | 7337 | handle_destructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 7338 | int ARG_UNUSED (flags), |
09347743 | 7339 | bool *no_add_attrs) |
f8e93a2e | 7340 | { |
7341 | tree decl = *node; | |
7342 | tree type = TREE_TYPE (decl); | |
7343 | ||
7344 | if (TREE_CODE (decl) == FUNCTION_DECL | |
7345 | && TREE_CODE (type) == FUNCTION_TYPE | |
7346 | && decl_function_context (decl) == 0) | |
7347 | { | |
9af7fd5b | 7348 | priority_type priority; |
f8e93a2e | 7349 | DECL_STATIC_DESTRUCTOR (decl) = 1; |
9af7fd5b | 7350 | priority = get_priority (args, /*is_destructor=*/true); |
7351 | SET_DECL_FINI_PRIORITY (decl, priority); | |
f8e93a2e | 7352 | TREE_USED (decl) = 1; |
7353 | } | |
7354 | else | |
7355 | { | |
9b2d6d13 | 7356 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7357 | *no_add_attrs = true; |
7358 | } | |
7359 | ||
7360 | return NULL_TREE; | |
7361 | } | |
7362 | ||
1c58e3f1 | 7363 | /* Nonzero if the mode is a valid vector mode for this architecture. |
7364 | This returns nonzero even if there is no hardware support for the | |
7365 | vector mode, but we can emulate with narrower modes. */ | |
7366 | ||
7367 | static int | |
3754d046 | 7368 | vector_mode_valid_p (machine_mode mode) |
1c58e3f1 | 7369 | { |
7370 | enum mode_class mclass = GET_MODE_CLASS (mode); | |
3754d046 | 7371 | machine_mode innermode; |
1c58e3f1 | 7372 | |
7373 | /* Doh! What's going on? */ | |
7374 | if (mclass != MODE_VECTOR_INT | |
7375 | && mclass != MODE_VECTOR_FLOAT | |
7376 | && mclass != MODE_VECTOR_FRACT | |
7377 | && mclass != MODE_VECTOR_UFRACT | |
7378 | && mclass != MODE_VECTOR_ACCUM | |
7379 | && mclass != MODE_VECTOR_UACCUM) | |
7380 | return 0; | |
7381 | ||
7382 | /* Hardware support. Woo hoo! */ | |
7383 | if (targetm.vector_mode_supported_p (mode)) | |
7384 | return 1; | |
7385 | ||
7386 | innermode = GET_MODE_INNER (mode); | |
7387 | ||
7388 | /* We should probably return 1 if requesting V4DI and we have no DI, | |
7389 | but we have V2DI, but this is probably very unlikely. */ | |
7390 | ||
7391 | /* If we have support for the inner mode, we can safely emulate it. | |
7392 | We may not have V2DI, but me can emulate with a pair of DIs. */ | |
7393 | return targetm.scalar_mode_supported_p (innermode); | |
7394 | } | |
7395 | ||
7396 | ||
f8e93a2e | 7397 | /* Handle a "mode" attribute; arguments as in |
7398 | struct attribute_spec.handler. */ | |
7399 | ||
7400 | static tree | |
9a03a746 | 7401 | handle_mode_attribute (tree *node, tree name, tree args, |
7402 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7403 | { |
7404 | tree type = *node; | |
d1dd9ac0 | 7405 | tree ident = TREE_VALUE (args); |
f8e93a2e | 7406 | |
7407 | *no_add_attrs = true; | |
ab2c1de8 | 7408 | |
d1dd9ac0 | 7409 | if (TREE_CODE (ident) != IDENTIFIER_NODE) |
9b2d6d13 | 7410 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7411 | else |
7412 | { | |
7413 | int j; | |
d1dd9ac0 | 7414 | const char *p = IDENTIFIER_POINTER (ident); |
f8e93a2e | 7415 | int len = strlen (p); |
3754d046 | 7416 | machine_mode mode = VOIDmode; |
f8e93a2e | 7417 | tree typefm; |
b2aef146 | 7418 | bool valid_mode; |
f8e93a2e | 7419 | |
7420 | if (len > 4 && p[0] == '_' && p[1] == '_' | |
7421 | && p[len - 1] == '_' && p[len - 2] == '_') | |
7422 | { | |
4fd61bc6 | 7423 | char *newp = (char *) alloca (len - 1); |
f8e93a2e | 7424 | |
7425 | strcpy (newp, &p[2]); | |
7426 | newp[len - 4] = '\0'; | |
7427 | p = newp; | |
7428 | } | |
7429 | ||
7430 | /* Change this type to have a type with the specified mode. | |
7431 | First check for the special modes. */ | |
84166705 | 7432 | if (!strcmp (p, "byte")) |
f8e93a2e | 7433 | mode = byte_mode; |
7434 | else if (!strcmp (p, "word")) | |
7435 | mode = word_mode; | |
84166705 | 7436 | else if (!strcmp (p, "pointer")) |
f8e93a2e | 7437 | mode = ptr_mode; |
0ef89dfd | 7438 | else if (!strcmp (p, "libgcc_cmp_return")) |
7439 | mode = targetm.libgcc_cmp_return_mode (); | |
7440 | else if (!strcmp (p, "libgcc_shift_count")) | |
7441 | mode = targetm.libgcc_shift_count_mode (); | |
1bd43494 | 7442 | else if (!strcmp (p, "unwind_word")) |
7443 | mode = targetm.unwind_word_mode (); | |
f8e93a2e | 7444 | else |
7445 | for (j = 0; j < NUM_MACHINE_MODES; j++) | |
7446 | if (!strcmp (p, GET_MODE_NAME (j))) | |
743a6f47 | 7447 | { |
3754d046 | 7448 | mode = (machine_mode) j; |
743a6f47 | 7449 | break; |
7450 | } | |
f8e93a2e | 7451 | |
7452 | if (mode == VOIDmode) | |
4917c376 | 7453 | { |
d1dd9ac0 | 7454 | error ("unknown machine mode %qE", ident); |
4917c376 | 7455 | return NULL_TREE; |
7456 | } | |
7457 | ||
b2aef146 | 7458 | valid_mode = false; |
7459 | switch (GET_MODE_CLASS (mode)) | |
4917c376 | 7460 | { |
b2aef146 | 7461 | case MODE_INT: |
7462 | case MODE_PARTIAL_INT: | |
7463 | case MODE_FLOAT: | |
c4503c0a | 7464 | case MODE_DECIMAL_FLOAT: |
9421ebb9 | 7465 | case MODE_FRACT: |
7466 | case MODE_UFRACT: | |
7467 | case MODE_ACCUM: | |
7468 | case MODE_UACCUM: | |
b2aef146 | 7469 | valid_mode = targetm.scalar_mode_supported_p (mode); |
7470 | break; | |
7471 | ||
7472 | case MODE_COMPLEX_INT: | |
7473 | case MODE_COMPLEX_FLOAT: | |
7474 | valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode)); | |
7475 | break; | |
7476 | ||
7477 | case MODE_VECTOR_INT: | |
7478 | case MODE_VECTOR_FLOAT: | |
9421ebb9 | 7479 | case MODE_VECTOR_FRACT: |
7480 | case MODE_VECTOR_UFRACT: | |
7481 | case MODE_VECTOR_ACCUM: | |
7482 | case MODE_VECTOR_UACCUM: | |
9b2d6d13 | 7483 | warning (OPT_Wattributes, "specifying vector types with " |
7484 | "__attribute__ ((mode)) is deprecated"); | |
7485 | warning (OPT_Wattributes, | |
7486 | "use __attribute__ ((vector_size)) instead"); | |
b2aef146 | 7487 | valid_mode = vector_mode_valid_p (mode); |
7488 | break; | |
4917c376 | 7489 | |
b2aef146 | 7490 | default: |
7491 | break; | |
7492 | } | |
7493 | if (!valid_mode) | |
7494 | { | |
1e5fcbe2 | 7495 | error ("unable to emulate %qs", p); |
b2aef146 | 7496 | return NULL_TREE; |
7497 | } | |
4917c376 | 7498 | |
b2aef146 | 7499 | if (POINTER_TYPE_P (type)) |
ead34f59 | 7500 | { |
6d5d708e | 7501 | addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type)); |
3754d046 | 7502 | tree (*fn)(tree, machine_mode, bool); |
b2aef146 | 7503 | |
6d5d708e | 7504 | if (!targetm.addr_space.valid_pointer_mode (mode, as)) |
ead34f59 | 7505 | { |
1e5fcbe2 | 7506 | error ("invalid pointer mode %qs", p); |
ead34f59 | 7507 | return NULL_TREE; |
7508 | } | |
7509 | ||
a0c938f0 | 7510 | if (TREE_CODE (type) == POINTER_TYPE) |
b2aef146 | 7511 | fn = build_pointer_type_for_mode; |
805e22b2 | 7512 | else |
b2aef146 | 7513 | fn = build_reference_type_for_mode; |
7514 | typefm = fn (TREE_TYPE (type), mode, false); | |
ead34f59 | 7515 | } |
b2aef146 | 7516 | else |
9421ebb9 | 7517 | { |
7518 | /* For fixed-point modes, we need to test if the signness of type | |
7519 | and the machine mode are consistent. */ | |
7520 | if (ALL_FIXED_POINT_MODE_P (mode) | |
7521 | && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode)) | |
7522 | { | |
bf776685 | 7523 | error ("signedness of type and machine mode %qs don%'t match", p); |
9421ebb9 | 7524 | return NULL_TREE; |
7525 | } | |
7526 | /* For fixed-point modes, we need to pass saturating info. */ | |
7527 | typefm = lang_hooks.types.type_for_mode (mode, | |
7528 | ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type) | |
7529 | : TYPE_UNSIGNED (type)); | |
7530 | } | |
3a648ab9 | 7531 | |
b2aef146 | 7532 | if (typefm == NULL_TREE) |
7533 | { | |
743a6f47 | 7534 | error ("no data type for mode %qs", p); |
b2aef146 | 7535 | return NULL_TREE; |
7536 | } | |
3a648ab9 | 7537 | else if (TREE_CODE (type) == ENUMERAL_TYPE) |
7538 | { | |
7539 | /* For enumeral types, copy the precision from the integer | |
7540 | type returned above. If not an INTEGER_TYPE, we can't use | |
7541 | this mode for this type. */ | |
7542 | if (TREE_CODE (typefm) != INTEGER_TYPE) | |
7543 | { | |
743a6f47 | 7544 | error ("cannot use mode %qs for enumeral types", p); |
3a648ab9 | 7545 | return NULL_TREE; |
7546 | } | |
7547 | ||
10080eac | 7548 | if (flags & ATTR_FLAG_TYPE_IN_PLACE) |
7549 | { | |
7550 | TYPE_PRECISION (type) = TYPE_PRECISION (typefm); | |
7551 | typefm = type; | |
7552 | } | |
7553 | else | |
7554 | { | |
7555 | /* We cannot build a type variant, as there's code that assumes | |
7556 | that TYPE_MAIN_VARIANT has the same mode. This includes the | |
7557 | debug generators. Instead, create a subrange type. This | |
7558 | results in all of the enumeral values being emitted only once | |
7559 | in the original, and the subtype gets them by reference. */ | |
7560 | if (TYPE_UNSIGNED (type)) | |
7561 | typefm = make_unsigned_type (TYPE_PRECISION (typefm)); | |
7562 | else | |
7563 | typefm = make_signed_type (TYPE_PRECISION (typefm)); | |
7564 | TREE_TYPE (typefm) = type; | |
7565 | } | |
3a648ab9 | 7566 | } |
4bf450a1 | 7567 | else if (VECTOR_MODE_P (mode) |
7568 | ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm)) | |
7569 | : TREE_CODE (type) != TREE_CODE (typefm)) | |
743a6f47 | 7570 | { |
7571 | error ("mode %qs applied to inappropriate type", p); | |
7572 | return NULL_TREE; | |
7573 | } | |
7574 | ||
b2aef146 | 7575 | *node = typefm; |
f8e93a2e | 7576 | } |
7577 | ||
7578 | return NULL_TREE; | |
7579 | } | |
7580 | ||
7581 | /* Handle a "section" attribute; arguments as in | |
7582 | struct attribute_spec.handler. */ | |
7583 | ||
7584 | static tree | |
9a03a746 | 7585 | handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
7586 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7587 | { |
7588 | tree decl = *node; | |
7589 | ||
9866562d | 7590 | if (!targetm_common.have_named_sections) |
f8e93a2e | 7591 | { |
9866562d | 7592 | error_at (DECL_SOURCE_LOCATION (*node), |
7593 | "section attributes are not supported for this target"); | |
7594 | goto fail; | |
7595 | } | |
065efcb1 | 7596 | |
9866562d | 7597 | user_defined_section_attribute = true; |
f8e93a2e | 7598 | |
b443c459 | 7599 | if (!VAR_OR_FUNCTION_DECL_P (decl)) |
9866562d | 7600 | { |
7601 | error ("section attribute not allowed for %q+D", *node); | |
7602 | goto fail; | |
f8e93a2e | 7603 | } |
9866562d | 7604 | |
7605 | if (TREE_CODE (TREE_VALUE (args)) != STRING_CST) | |
f8e93a2e | 7606 | { |
9866562d | 7607 | error ("section attribute argument not a string constant"); |
7608 | goto fail; | |
f8e93a2e | 7609 | } |
7610 | ||
f48c7f4a | 7611 | if (VAR_P (decl) |
9866562d | 7612 | && current_function_decl != NULL_TREE |
7613 | && !TREE_STATIC (decl)) | |
7614 | { | |
7615 | error_at (DECL_SOURCE_LOCATION (decl), | |
7616 | "section attribute cannot be specified for local variables"); | |
7617 | goto fail; | |
7618 | } | |
7619 | ||
7620 | /* The decl may have already been given a section attribute | |
7621 | from a previous declaration. Ensure they match. */ | |
7622 | if (DECL_SECTION_NAME (decl) != NULL | |
7623 | && strcmp (DECL_SECTION_NAME (decl), | |
7624 | TREE_STRING_POINTER (TREE_VALUE (args))) != 0) | |
7625 | { | |
7626 | error ("section of %q+D conflicts with previous declaration", *node); | |
7627 | goto fail; | |
7628 | } | |
7629 | ||
f48c7f4a | 7630 | if (VAR_P (decl) |
9866562d | 7631 | && !targetm.have_tls && targetm.emutls.tmpl_section |
7632 | && DECL_THREAD_LOCAL_P (decl)) | |
7633 | { | |
7634 | error ("section of %q+D cannot be overridden", *node); | |
7635 | goto fail; | |
7636 | } | |
7637 | ||
7638 | set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args))); | |
7639 | return NULL_TREE; | |
7640 | ||
7641 | fail: | |
7642 | *no_add_attrs = true; | |
f8e93a2e | 7643 | return NULL_TREE; |
7644 | } | |
7645 | ||
83e25171 | 7646 | /* Check whether ALIGN is a valid user-specified alignment. If so, |
7647 | return its base-2 log; if not, output an error and return -1. If | |
7648 | ALLOW_ZERO then 0 is valid and should result in a return of -1 with | |
7649 | no error. */ | |
7650 | int | |
7651 | check_user_alignment (const_tree align, bool allow_zero) | |
7652 | { | |
7653 | int i; | |
7654 | ||
3e5a8b00 | 7655 | if (error_operand_p (align)) |
7656 | return -1; | |
5abaa10a | 7657 | if (TREE_CODE (align) != INTEGER_CST |
7658 | || !INTEGRAL_TYPE_P (TREE_TYPE (align))) | |
83e25171 | 7659 | { |
7660 | error ("requested alignment is not an integer constant"); | |
7661 | return -1; | |
7662 | } | |
7663 | else if (allow_zero && integer_zerop (align)) | |
7664 | return -1; | |
1a087624 | 7665 | else if (tree_int_cst_sgn (align) == -1 |
7666 | || (i = tree_log2 (align)) == -1) | |
83e25171 | 7667 | { |
1a087624 | 7668 | error ("requested alignment is not a positive power of 2"); |
83e25171 | 7669 | return -1; |
7670 | } | |
7671 | else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG) | |
7672 | { | |
7673 | error ("requested alignment is too large"); | |
7674 | return -1; | |
7675 | } | |
7676 | return i; | |
7677 | } | |
7678 | ||
ffcdbf9c | 7679 | /* |
7680 | If in c++-11, check if the c++-11 alignment constraint with respect | |
7681 | to fundamental alignment (in [dcl.align]) are satisfied. If not in | |
7682 | c++-11 mode, does nothing. | |
7683 | ||
7684 | [dcl.align]2/ says: | |
7685 | ||
7686 | [* if the constant expression evaluates to a fundamental alignment, | |
7687 | the alignment requirement of the declared entity shall be the | |
7688 | specified fundamental alignment. | |
7689 | ||
7690 | * if the constant expression evaluates to an extended alignment | |
7691 | and the implementation supports that alignment in the context | |
7692 | of the declaration, the alignment of the declared entity shall | |
7693 | be that alignment | |
7694 | ||
7695 | * if the constant expression evaluates to an extended alignment | |
7696 | and the implementation does not support that alignment in the | |
7697 | context of the declaration, the program is ill-formed]. */ | |
7698 | ||
7699 | static bool | |
7700 | check_cxx_fundamental_alignment_constraints (tree node, | |
7701 | unsigned align_log, | |
7702 | int flags) | |
7703 | { | |
7704 | bool alignment_too_large_p = false; | |
7705 | unsigned requested_alignment = 1U << align_log; | |
7706 | unsigned max_align = 0; | |
7707 | ||
7708 | if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat) | |
7709 | || (node == NULL_TREE || node == error_mark_node)) | |
7710 | return true; | |
7711 | ||
7712 | if (cxx_fundamental_alignment_p (requested_alignment)) | |
7713 | return true; | |
7714 | ||
7715 | if (DECL_P (node)) | |
7716 | { | |
7717 | if (TREE_STATIC (node)) | |
7718 | { | |
7719 | /* For file scope variables and static members, the target | |
7720 | supports alignments that are at most | |
7721 | MAX_OFILE_ALIGNMENT. */ | |
7722 | if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT)) | |
7723 | alignment_too_large_p = true; | |
7724 | } | |
7725 | else | |
7726 | { | |
7727 | #ifdef BIGGEST_FIELD_ALIGNMENT | |
7728 | #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT | |
7729 | #else | |
7730 | #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT | |
7731 | #endif | |
7732 | /* For non-static members, the target supports either | |
7733 | alignments that at most either BIGGEST_FIELD_ALIGNMENT | |
7734 | if it is defined or BIGGEST_ALIGNMENT. */ | |
7735 | max_align = MAX_TARGET_FIELD_ALIGNMENT; | |
7736 | if (TREE_CODE (node) == FIELD_DECL | |
7737 | && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT)) | |
7738 | alignment_too_large_p = true; | |
7739 | #undef MAX_TARGET_FIELD_ALIGNMENT | |
7740 | /* For stack variables, the target supports at most | |
7741 | MAX_STACK_ALIGNMENT. */ | |
7742 | else if (decl_function_context (node) != NULL | |
7743 | && requested_alignment > (max_align = MAX_STACK_ALIGNMENT)) | |
7744 | alignment_too_large_p = true; | |
7745 | } | |
7746 | } | |
7747 | else if (TYPE_P (node)) | |
7748 | { | |
7749 | /* Let's be liberal for types. */ | |
7750 | if (requested_alignment > (max_align = BIGGEST_ALIGNMENT)) | |
7751 | alignment_too_large_p = true; | |
7752 | } | |
7753 | ||
7754 | if (alignment_too_large_p) | |
7755 | pedwarn (input_location, OPT_Wattributes, | |
7756 | "requested alignment %d is larger than %d", | |
7757 | requested_alignment, max_align); | |
7758 | ||
7759 | return !alignment_too_large_p; | |
7760 | } | |
7761 | ||
f8e93a2e | 7762 | /* Handle a "aligned" attribute; arguments as in |
7763 | struct attribute_spec.handler. */ | |
7764 | ||
7765 | static tree | |
9a03a746 | 7766 | handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
09347743 | 7767 | int flags, bool *no_add_attrs) |
f8e93a2e | 7768 | { |
7769 | tree decl = NULL_TREE; | |
7770 | tree *type = NULL; | |
7771 | int is_type = 0; | |
caf62483 | 7772 | tree align_expr; |
f8e93a2e | 7773 | int i; |
7774 | ||
caf62483 | 7775 | if (args) |
7776 | { | |
7777 | align_expr = TREE_VALUE (args); | |
3e5a8b00 | 7778 | if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE |
7779 | && TREE_CODE (align_expr) != FUNCTION_DECL) | |
caf62483 | 7780 | align_expr = default_conversion (align_expr); |
7781 | } | |
7782 | else | |
7783 | align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT); | |
7784 | ||
f8e93a2e | 7785 | if (DECL_P (*node)) |
7786 | { | |
7787 | decl = *node; | |
7788 | type = &TREE_TYPE (decl); | |
7789 | is_type = TREE_CODE (*node) == TYPE_DECL; | |
7790 | } | |
7791 | else if (TYPE_P (*node)) | |
7792 | type = node, is_type = 1; | |
7793 | ||
ffcdbf9c | 7794 | if ((i = check_user_alignment (align_expr, false)) == -1 |
7795 | || !check_cxx_fundamental_alignment_constraints (*node, i, flags)) | |
83e25171 | 7796 | *no_add_attrs = true; |
f8e93a2e | 7797 | else if (is_type) |
7798 | { | |
2ec3af9c | 7799 | if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
7800 | /* OK, modify the type in place. */; | |
f8e93a2e | 7801 | /* If we have a TYPE_DECL, then copy the type, so that we |
7802 | don't accidentally modify a builtin type. See pushdecl. */ | |
2ec3af9c | 7803 | else if (decl && TREE_TYPE (decl) != error_mark_node |
7804 | && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) | |
f8e93a2e | 7805 | { |
7806 | tree tt = TREE_TYPE (decl); | |
e086912e | 7807 | *type = build_variant_type_copy (*type); |
f8e93a2e | 7808 | DECL_ORIGINAL_TYPE (decl) = tt; |
7809 | TYPE_NAME (*type) = decl; | |
7810 | TREE_USED (*type) = TREE_USED (decl); | |
7811 | TREE_TYPE (decl) = *type; | |
7812 | } | |
2ec3af9c | 7813 | else |
e086912e | 7814 | *type = build_variant_type_copy (*type); |
f8e93a2e | 7815 | |
7cfdc2f0 | 7816 | TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT; |
f8e93a2e | 7817 | TYPE_USER_ALIGN (*type) = 1; |
7818 | } | |
097b5c8b | 7819 | else if (! VAR_OR_FUNCTION_DECL_P (decl) |
f8e93a2e | 7820 | && TREE_CODE (decl) != FIELD_DECL) |
7821 | { | |
3cf8b391 | 7822 | error ("alignment may not be specified for %q+D", decl); |
f8e93a2e | 7823 | *no_add_attrs = true; |
7824 | } | |
ffcdbf9c | 7825 | else if (DECL_USER_ALIGN (decl) |
7826 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) | |
7827 | /* C++-11 [dcl.align/4]: | |
7828 | ||
7829 | When multiple alignment-specifiers are specified for an | |
7830 | entity, the alignment requirement shall be set to the | |
7831 | strictest specified alignment. | |
7832 | ||
7833 | This formally comes from the c++11 specification but we are | |
7834 | doing it for the GNU attribute syntax as well. */ | |
7835 | *no_add_attrs = true; | |
097b5c8b | 7836 | else if (TREE_CODE (decl) == FUNCTION_DECL |
7cfdc2f0 | 7837 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) |
097b5c8b | 7838 | { |
7839 | if (DECL_USER_ALIGN (decl)) | |
7840 | error ("alignment for %q+D was previously specified as %d " | |
7841 | "and may not be decreased", decl, | |
7842 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
7843 | else | |
7844 | error ("alignment for %q+D must be at least %d", decl, | |
7845 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
7846 | *no_add_attrs = true; | |
7847 | } | |
f8e93a2e | 7848 | else |
7849 | { | |
7cfdc2f0 | 7850 | DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT; |
f8e93a2e | 7851 | DECL_USER_ALIGN (decl) = 1; |
7852 | } | |
7853 | ||
7854 | return NULL_TREE; | |
7855 | } | |
7856 | ||
7857 | /* Handle a "weak" attribute; arguments as in | |
7858 | struct attribute_spec.handler. */ | |
7859 | ||
7860 | static tree | |
f948b309 | 7861 | handle_weak_attribute (tree *node, tree name, |
9a03a746 | 7862 | tree ARG_UNUSED (args), |
7863 | int ARG_UNUSED (flags), | |
7864 | bool * ARG_UNUSED (no_add_attrs)) | |
f8e93a2e | 7865 | { |
f948b309 | 7866 | if (TREE_CODE (*node) == FUNCTION_DECL |
059a60f3 | 7867 | && DECL_DECLARED_INLINE_P (*node)) |
7868 | { | |
0725e25c | 7869 | warning (OPT_Wattributes, "inline function %q+D declared weak", *node); |
059a60f3 | 7870 | *no_add_attrs = true; |
7871 | } | |
85c0a25c | 7872 | else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
7873 | { | |
7874 | error ("indirect function %q+D cannot be declared weak", *node); | |
7875 | *no_add_attrs = true; | |
7876 | return NULL_TREE; | |
7877 | } | |
b443c459 | 7878 | else if (VAR_OR_FUNCTION_DECL_P (*node)) |
9e6bcade | 7879 | declare_weak (*node); |
f948b309 | 7880 | else |
7881 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 7882 | |
7883 | return NULL_TREE; | |
7884 | } | |
7885 | ||
4641882f | 7886 | /* Handle a "noplt" attribute; arguments as in |
7887 | struct attribute_spec.handler. */ | |
7888 | ||
7889 | static tree | |
7890 | handle_noplt_attribute (tree *node, tree name, | |
7891 | tree ARG_UNUSED (args), | |
7892 | int ARG_UNUSED (flags), | |
7893 | bool * ARG_UNUSED (no_add_attrs)) | |
7894 | { | |
7895 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
7896 | { | |
7897 | warning (OPT_Wattributes, | |
7898 | "%qE attribute is only applicable on functions", name); | |
7899 | *no_add_attrs = true; | |
7900 | return NULL_TREE; | |
7901 | } | |
7902 | return NULL_TREE; | |
7903 | } | |
7904 | ||
85c0a25c | 7905 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
7906 | struct attribute_spec.handler, except that IS_ALIAS tells us | |
7907 | whether this is an alias as opposed to ifunc attribute. */ | |
f8e93a2e | 7908 | |
7909 | static tree | |
85c0a25c | 7910 | handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args, |
7911 | bool *no_add_attrs) | |
f8e93a2e | 7912 | { |
7913 | tree decl = *node; | |
7914 | ||
85c0a25c | 7915 | if (TREE_CODE (decl) != FUNCTION_DECL |
f48c7f4a | 7916 | && (!is_alias || !VAR_P (decl))) |
9e830260 | 7917 | { |
7918 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7919 | *no_add_attrs = true; | |
7920 | } | |
7921 | else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) | |
48e1416a | 7922 | || (TREE_CODE (decl) != FUNCTION_DECL |
0a3ecdc1 | 7923 | && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) |
7924 | /* A static variable declaration is always a tentative definition, | |
7925 | but the alias is a non-tentative definition which overrides. */ | |
48e1416a | 7926 | || (TREE_CODE (decl) != FUNCTION_DECL |
0a3ecdc1 | 7927 | && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl))) |
f8e93a2e | 7928 | { |
85c0a25c | 7929 | error ("%q+D defined both normally and as %qE attribute", decl, name); |
f8e93a2e | 7930 | *no_add_attrs = true; |
85c0a25c | 7931 | return NULL_TREE; |
f8e93a2e | 7932 | } |
85c0a25c | 7933 | else if (!is_alias |
7934 | && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl)) | |
7935 | || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))) | |
7936 | { | |
7937 | error ("weak %q+D cannot be defined %qE", decl, name); | |
7938 | *no_add_attrs = true; | |
7939 | return NULL_TREE; | |
7940 | } | |
8c42f0d9 | 7941 | |
7942 | /* Note that the very first time we process a nested declaration, | |
7943 | decl_function_context will not be set. Indeed, *would* never | |
7944 | be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that | |
7945 | we do below. After such frobbery, pushdecl would set the context. | |
7946 | In any case, this is never what we want. */ | |
7947 | else if (decl_function_context (decl) == 0 && current_function_decl == NULL) | |
f8e93a2e | 7948 | { |
7949 | tree id; | |
7950 | ||
7951 | id = TREE_VALUE (args); | |
7952 | if (TREE_CODE (id) != STRING_CST) | |
7953 | { | |
85c0a25c | 7954 | error ("attribute %qE argument not a string", name); |
f8e93a2e | 7955 | *no_add_attrs = true; |
7956 | return NULL_TREE; | |
7957 | } | |
7958 | id = get_identifier (TREE_STRING_POINTER (id)); | |
7959 | /* This counts as a use of the object pointed to. */ | |
7960 | TREE_USED (id) = 1; | |
7961 | ||
7962 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
7963 | DECL_INITIAL (decl) = error_mark_node; | |
7964 | else | |
f2526cce | 7965 | TREE_STATIC (decl) = 1; |
85c0a25c | 7966 | |
7967 | if (!is_alias) | |
7968 | /* ifuncs are also aliases, so set that attribute too. */ | |
7969 | DECL_ATTRIBUTES (decl) | |
7970 | = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl)); | |
f8e93a2e | 7971 | } |
7972 | else | |
7973 | { | |
9b2d6d13 | 7974 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7975 | *no_add_attrs = true; |
7976 | } | |
7977 | ||
8e857c41 | 7978 | if (decl_in_symtab_p (*node)) |
7979 | { | |
7980 | struct symtab_node *n = symtab_node::get (decl); | |
7981 | if (n && n->refuse_visibility_changes) | |
7982 | { | |
7983 | if (is_alias) | |
7984 | error ("%+D declared alias after being used", decl); | |
7985 | else | |
7986 | error ("%+D declared ifunc after being used", decl); | |
7987 | } | |
7988 | } | |
7989 | ||
7990 | ||
f8e93a2e | 7991 | return NULL_TREE; |
7992 | } | |
7993 | ||
85c0a25c | 7994 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
7995 | struct attribute_spec.handler. */ | |
7996 | ||
7997 | static tree | |
7998 | handle_ifunc_attribute (tree *node, tree name, tree args, | |
7999 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8000 | { | |
8001 | return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs); | |
8002 | } | |
8003 | ||
8004 | /* Handle an "alias" or "ifunc" attribute; arguments as in | |
8005 | struct attribute_spec.handler. */ | |
8006 | ||
8007 | static tree | |
8008 | handle_alias_attribute (tree *node, tree name, tree args, | |
8009 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8010 | { | |
8011 | return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs); | |
8012 | } | |
8013 | ||
f4a30bd7 | 8014 | /* Handle a "weakref" attribute; arguments as in struct |
8015 | attribute_spec.handler. */ | |
8016 | ||
8017 | static tree | |
8018 | handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
8019 | int flags, bool *no_add_attrs) | |
8020 | { | |
8021 | tree attr = NULL_TREE; | |
8022 | ||
83852912 | 8023 | /* We must ignore the attribute when it is associated with |
8024 | local-scoped decls, since attribute alias is ignored and many | |
8025 | such symbols do not even have a DECL_WEAK field. */ | |
64a7bd81 | 8026 | if (decl_function_context (*node) |
8027 | || current_function_decl | |
b443c459 | 8028 | || !VAR_OR_FUNCTION_DECL_P (*node)) |
83852912 | 8029 | { |
8030 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8031 | *no_add_attrs = true; | |
8032 | return NULL_TREE; | |
8033 | } | |
8034 | ||
85c0a25c | 8035 | if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
8036 | { | |
8037 | error ("indirect function %q+D cannot be declared weakref", *node); | |
8038 | *no_add_attrs = true; | |
8039 | return NULL_TREE; | |
8040 | } | |
8041 | ||
f4a30bd7 | 8042 | /* The idea here is that `weakref("name")' mutates into `weakref, |
8043 | alias("name")', and weakref without arguments, in turn, | |
8044 | implicitly adds weak. */ | |
8045 | ||
8046 | if (args) | |
8047 | { | |
8048 | attr = tree_cons (get_identifier ("alias"), args, attr); | |
8049 | attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr); | |
8050 | ||
8051 | *no_add_attrs = true; | |
0a3ecdc1 | 8052 | |
8053 | decl_attributes (node, attr, flags); | |
f4a30bd7 | 8054 | } |
8055 | else | |
8056 | { | |
8057 | if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node))) | |
712d2297 | 8058 | error_at (DECL_SOURCE_LOCATION (*node), |
8059 | "weakref attribute must appear before alias attribute"); | |
f4a30bd7 | 8060 | |
0a3ecdc1 | 8061 | /* Can't call declare_weak because it wants this to be TREE_PUBLIC, |
8062 | and that isn't supported; and because it wants to add it to | |
8063 | the list of weak decls, which isn't helpful. */ | |
8064 | DECL_WEAK (*node) = 1; | |
f4a30bd7 | 8065 | } |
8066 | ||
8e857c41 | 8067 | if (decl_in_symtab_p (*node)) |
8068 | { | |
8069 | struct symtab_node *n = symtab_node::get (*node); | |
8070 | if (n && n->refuse_visibility_changes) | |
8071 | error ("%+D declared weakref after being used", *node); | |
8072 | } | |
8073 | ||
f4a30bd7 | 8074 | return NULL_TREE; |
8075 | } | |
8076 | ||
f8e93a2e | 8077 | /* Handle an "visibility" attribute; arguments as in |
8078 | struct attribute_spec.handler. */ | |
8079 | ||
8080 | static tree | |
1cae46be | 8081 | handle_visibility_attribute (tree *node, tree name, tree args, |
9a03a746 | 8082 | int ARG_UNUSED (flags), |
4a2849cb | 8083 | bool *ARG_UNUSED (no_add_attrs)) |
f8e93a2e | 8084 | { |
8085 | tree decl = *node; | |
9c40570a | 8086 | tree id = TREE_VALUE (args); |
4a2849cb | 8087 | enum symbol_visibility vis; |
f8e93a2e | 8088 | |
b212f378 | 8089 | if (TYPE_P (*node)) |
8090 | { | |
4a2849cb | 8091 | if (TREE_CODE (*node) == ENUMERAL_TYPE) |
8092 | /* OK */; | |
8e539fdc | 8093 | else if (!RECORD_OR_UNION_TYPE_P (*node)) |
4a2849cb | 8094 | { |
8095 | warning (OPT_Wattributes, "%qE attribute ignored on non-class types", | |
8096 | name); | |
8097 | return NULL_TREE; | |
8098 | } | |
8099 | else if (TYPE_FIELDS (*node)) | |
8100 | { | |
8101 | error ("%qE attribute ignored because %qT is already defined", | |
8102 | name, *node); | |
8103 | return NULL_TREE; | |
8104 | } | |
b212f378 | 8105 | } |
84166705 | 8106 | else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl)) |
f8e93a2e | 8107 | { |
9b2d6d13 | 8108 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
9c40570a | 8109 | return NULL_TREE; |
f8e93a2e | 8110 | } |
f8e93a2e | 8111 | |
9c40570a | 8112 | if (TREE_CODE (id) != STRING_CST) |
8113 | { | |
07e3a3d2 | 8114 | error ("visibility argument not a string"); |
9c40570a | 8115 | return NULL_TREE; |
f8e93a2e | 8116 | } |
b27ac6b5 | 8117 | |
b212f378 | 8118 | /* If this is a type, set the visibility on the type decl. */ |
8119 | if (TYPE_P (decl)) | |
8120 | { | |
8121 | decl = TYPE_NAME (decl); | |
84166705 | 8122 | if (!decl) |
a0c938f0 | 8123 | return NULL_TREE; |
e147aab3 | 8124 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
8125 | { | |
9b2d6d13 | 8126 | warning (OPT_Wattributes, "%qE attribute ignored on types", |
e147aab3 | 8127 | name); |
8128 | return NULL_TREE; | |
8129 | } | |
b212f378 | 8130 | } |
f8e93a2e | 8131 | |
9c40570a | 8132 | if (strcmp (TREE_STRING_POINTER (id), "default") == 0) |
4a2849cb | 8133 | vis = VISIBILITY_DEFAULT; |
9c40570a | 8134 | else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0) |
4a2849cb | 8135 | vis = VISIBILITY_INTERNAL; |
9c40570a | 8136 | else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0) |
4a2849cb | 8137 | vis = VISIBILITY_HIDDEN; |
9c40570a | 8138 | else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0) |
4a2849cb | 8139 | vis = VISIBILITY_PROTECTED; |
9c40570a | 8140 | else |
4a2849cb | 8141 | { |
8142 | error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\""); | |
8143 | vis = VISIBILITY_DEFAULT; | |
8144 | } | |
8145 | ||
8146 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
098a01e7 | 8147 | && vis != DECL_VISIBILITY (decl)) |
8148 | { | |
8149 | tree attributes = (TYPE_P (*node) | |
8150 | ? TYPE_ATTRIBUTES (*node) | |
8151 | : DECL_ATTRIBUTES (decl)); | |
8152 | if (lookup_attribute ("visibility", attributes)) | |
8153 | error ("%qD redeclared with different visibility", decl); | |
8154 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
8155 | && lookup_attribute ("dllimport", attributes)) | |
8156 | error ("%qD was declared %qs which implies default visibility", | |
8157 | decl, "dllimport"); | |
8158 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
8159 | && lookup_attribute ("dllexport", attributes)) | |
8160 | error ("%qD was declared %qs which implies default visibility", | |
8161 | decl, "dllexport"); | |
8162 | } | |
4a2849cb | 8163 | |
8164 | DECL_VISIBILITY (decl) = vis; | |
b212f378 | 8165 | DECL_VISIBILITY_SPECIFIED (decl) = 1; |
8166 | ||
4a2849cb | 8167 | /* Go ahead and attach the attribute to the node as well. This is needed |
8168 | so we can determine whether we have VISIBILITY_DEFAULT because the | |
8169 | visibility was not specified, or because it was explicitly overridden | |
8170 | from the containing scope. */ | |
9c40570a | 8171 | |
f8e93a2e | 8172 | return NULL_TREE; |
8173 | } | |
8174 | ||
3aa0c315 | 8175 | /* Determine the ELF symbol visibility for DECL, which is either a |
8176 | variable or a function. It is an error to use this function if a | |
8177 | definition of DECL is not available in this translation unit. | |
8178 | Returns true if the final visibility has been determined by this | |
8179 | function; false if the caller is free to make additional | |
8180 | modifications. */ | |
8181 | ||
8182 | bool | |
8183 | c_determine_visibility (tree decl) | |
8184 | { | |
b443c459 | 8185 | gcc_assert (VAR_OR_FUNCTION_DECL_P (decl)); |
3aa0c315 | 8186 | |
8187 | /* If the user explicitly specified the visibility with an | |
8188 | attribute, honor that. DECL_VISIBILITY will have been set during | |
920f5a70 | 8189 | the processing of the attribute. We check for an explicit |
8190 | attribute, rather than just checking DECL_VISIBILITY_SPECIFIED, | |
8191 | to distinguish the use of an attribute from the use of a "#pragma | |
8192 | GCC visibility push(...)"; in the latter case we still want other | |
8193 | considerations to be able to overrule the #pragma. */ | |
8194 | if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)) | |
8195 | || (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
8196 | && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) | |
8197 | || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))))) | |
3aa0c315 | 8198 | return true; |
8199 | ||
4a2849cb | 8200 | /* Set default visibility to whatever the user supplied with |
8201 | visibility_specified depending on #pragma GCC visibility. */ | |
8202 | if (!DECL_VISIBILITY_SPECIFIED (decl)) | |
8203 | { | |
2d9d8740 | 8204 | if (visibility_options.inpragma |
8205 | || DECL_VISIBILITY (decl) != default_visibility) | |
8206 | { | |
8207 | DECL_VISIBILITY (decl) = default_visibility; | |
8208 | DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; | |
8209 | /* If visibility changed and DECL already has DECL_RTL, ensure | |
8210 | symbol flags are updated. */ | |
f48c7f4a | 8211 | if (((VAR_P (decl) && TREE_STATIC (decl)) |
2d9d8740 | 8212 | || TREE_CODE (decl) == FUNCTION_DECL) |
8213 | && DECL_RTL_SET_P (decl)) | |
8214 | make_decl_rtl (decl); | |
8215 | } | |
4a2849cb | 8216 | } |
3aa0c315 | 8217 | return false; |
8218 | } | |
8219 | ||
24dfead4 | 8220 | /* Handle an "tls_model" attribute; arguments as in |
8221 | struct attribute_spec.handler. */ | |
8222 | ||
8223 | static tree | |
1cae46be | 8224 | handle_tls_model_attribute (tree *node, tree name, tree args, |
9a03a746 | 8225 | int ARG_UNUSED (flags), bool *no_add_attrs) |
24dfead4 | 8226 | { |
1b53eb20 | 8227 | tree id; |
24dfead4 | 8228 | tree decl = *node; |
1b53eb20 | 8229 | enum tls_model kind; |
24dfead4 | 8230 | |
1b53eb20 | 8231 | *no_add_attrs = true; |
8232 | ||
f48c7f4a | 8233 | if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl)) |
24dfead4 | 8234 | { |
9b2d6d13 | 8235 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
1b53eb20 | 8236 | return NULL_TREE; |
24dfead4 | 8237 | } |
24dfead4 | 8238 | |
1b53eb20 | 8239 | kind = DECL_TLS_MODEL (decl); |
8240 | id = TREE_VALUE (args); | |
8241 | if (TREE_CODE (id) != STRING_CST) | |
8242 | { | |
8243 | error ("tls_model argument not a string"); | |
8244 | return NULL_TREE; | |
24dfead4 | 8245 | } |
8246 | ||
1b53eb20 | 8247 | if (!strcmp (TREE_STRING_POINTER (id), "local-exec")) |
8248 | kind = TLS_MODEL_LOCAL_EXEC; | |
8249 | else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec")) | |
8250 | kind = TLS_MODEL_INITIAL_EXEC; | |
8251 | else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic")) | |
8252 | kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC; | |
8253 | else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic")) | |
8254 | kind = TLS_MODEL_GLOBAL_DYNAMIC; | |
8255 | else | |
8256 | error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\""); | |
8257 | ||
5e68df57 | 8258 | set_decl_tls_model (decl, kind); |
24dfead4 | 8259 | return NULL_TREE; |
8260 | } | |
8261 | ||
f8e93a2e | 8262 | /* Handle a "no_instrument_function" attribute; arguments as in |
8263 | struct attribute_spec.handler. */ | |
8264 | ||
8265 | static tree | |
1cae46be | 8266 | handle_no_instrument_function_attribute (tree *node, tree name, |
9a03a746 | 8267 | tree ARG_UNUSED (args), |
8268 | int ARG_UNUSED (flags), | |
09347743 | 8269 | bool *no_add_attrs) |
f8e93a2e | 8270 | { |
8271 | tree decl = *node; | |
8272 | ||
8273 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8274 | { | |
712d2297 | 8275 | error_at (DECL_SOURCE_LOCATION (decl), |
8276 | "%qE attribute applies only to functions", name); | |
f8e93a2e | 8277 | *no_add_attrs = true; |
8278 | } | |
f8e93a2e | 8279 | else |
8280 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
8281 | ||
8282 | return NULL_TREE; | |
8283 | } | |
8284 | ||
8285 | /* Handle a "malloc" attribute; arguments as in | |
8286 | struct attribute_spec.handler. */ | |
8287 | ||
8288 | static tree | |
9a03a746 | 8289 | handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8290 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 8291 | { |
a5147fca | 8292 | if (TREE_CODE (*node) == FUNCTION_DECL |
8293 | && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) | |
f8e93a2e | 8294 | DECL_IS_MALLOC (*node) = 1; |
f8e93a2e | 8295 | else |
8296 | { | |
9b2d6d13 | 8297 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8298 | *no_add_attrs = true; |
8299 | } | |
8300 | ||
8301 | return NULL_TREE; | |
8302 | } | |
8303 | ||
4a29c97c | 8304 | /* Handle a "alloc_size" attribute; arguments as in |
8305 | struct attribute_spec.handler. */ | |
8306 | ||
8307 | static tree | |
8308 | handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
8309 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8310 | { | |
2802826e | 8311 | unsigned arg_count = type_num_arguments (*node); |
4a29c97c | 8312 | for (; args; args = TREE_CHAIN (args)) |
8313 | { | |
8314 | tree position = TREE_VALUE (args); | |
caf62483 | 8315 | if (position && TREE_CODE (position) != IDENTIFIER_NODE |
8316 | && TREE_CODE (position) != FUNCTION_DECL) | |
8317 | position = default_conversion (position); | |
4a29c97c | 8318 | |
237e78b1 | 8319 | if (!tree_fits_uhwi_p (position) |
8320 | || !arg_count | |
8321 | || !IN_RANGE (tree_to_uhwi (position), 1, arg_count)) | |
4a29c97c | 8322 | { |
48e1416a | 8323 | warning (OPT_Wattributes, |
4a29c97c | 8324 | "alloc_size parameter outside range"); |
8325 | *no_add_attrs = true; | |
8326 | return NULL_TREE; | |
8327 | } | |
8328 | } | |
8329 | return NULL_TREE; | |
8330 | } | |
8331 | ||
237e78b1 | 8332 | /* Handle a "alloc_align" attribute; arguments as in |
8333 | struct attribute_spec.handler. */ | |
8334 | ||
8335 | static tree | |
8336 | handle_alloc_align_attribute (tree *node, tree, tree args, int, | |
8337 | bool *no_add_attrs) | |
8338 | { | |
8339 | unsigned arg_count = type_num_arguments (*node); | |
8340 | tree position = TREE_VALUE (args); | |
8341 | if (position && TREE_CODE (position) != IDENTIFIER_NODE) | |
8342 | position = default_conversion (position); | |
8343 | ||
8344 | if (!tree_fits_uhwi_p (position) | |
8345 | || !arg_count | |
8346 | || !IN_RANGE (tree_to_uhwi (position), 1, arg_count)) | |
8347 | { | |
8348 | warning (OPT_Wattributes, | |
8349 | "alloc_align parameter outside range"); | |
8350 | *no_add_attrs = true; | |
8351 | return NULL_TREE; | |
8352 | } | |
8353 | return NULL_TREE; | |
8354 | } | |
8355 | ||
8356 | /* Handle a "assume_aligned" attribute; arguments as in | |
8357 | struct attribute_spec.handler. */ | |
8358 | ||
8359 | static tree | |
8360 | handle_assume_aligned_attribute (tree *, tree, tree args, int, | |
8361 | bool *no_add_attrs) | |
8362 | { | |
8363 | for (; args; args = TREE_CHAIN (args)) | |
8364 | { | |
8365 | tree position = TREE_VALUE (args); | |
8366 | if (position && TREE_CODE (position) != IDENTIFIER_NODE | |
8367 | && TREE_CODE (position) != FUNCTION_DECL) | |
8368 | position = default_conversion (position); | |
8369 | ||
8370 | if (TREE_CODE (position) != INTEGER_CST) | |
8371 | { | |
8372 | warning (OPT_Wattributes, | |
8373 | "assume_aligned parameter not integer constant"); | |
8374 | *no_add_attrs = true; | |
8375 | return NULL_TREE; | |
8376 | } | |
8377 | } | |
8378 | return NULL_TREE; | |
8379 | } | |
8380 | ||
8ce86007 | 8381 | /* Handle a "fn spec" attribute; arguments as in |
8382 | struct attribute_spec.handler. */ | |
8383 | ||
8384 | static tree | |
8385 | handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name), | |
8386 | tree args, int ARG_UNUSED (flags), | |
8387 | bool *no_add_attrs ATTRIBUTE_UNUSED) | |
8388 | { | |
8389 | gcc_assert (args | |
8390 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST | |
8391 | && !TREE_CHAIN (args)); | |
8392 | return NULL_TREE; | |
8393 | } | |
8394 | ||
058a1b7a | 8395 | /* Handle a "bnd_variable_size" attribute; arguments as in |
8396 | struct attribute_spec.handler. */ | |
8397 | ||
8398 | static tree | |
8399 | handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
8400 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8401 | { | |
8402 | if (TREE_CODE (*node) != FIELD_DECL) | |
8403 | { | |
8404 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8405 | *no_add_attrs = true; | |
8406 | } | |
8407 | ||
8408 | return NULL_TREE; | |
8409 | } | |
8410 | ||
8411 | /* Handle a "bnd_legacy" attribute; arguments as in | |
8412 | struct attribute_spec.handler. */ | |
8413 | ||
8414 | static tree | |
8415 | handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args), | |
8416 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8417 | { | |
8418 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
8419 | { | |
8420 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8421 | *no_add_attrs = true; | |
8422 | } | |
8423 | ||
8424 | return NULL_TREE; | |
8425 | } | |
8426 | ||
8427 | /* Handle a "bnd_instrument" attribute; arguments as in | |
8428 | struct attribute_spec.handler. */ | |
8429 | ||
8430 | static tree | |
8431 | handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args), | |
8432 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8433 | { | |
8434 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
8435 | { | |
8436 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8437 | *no_add_attrs = true; | |
8438 | } | |
8439 | ||
8440 | return NULL_TREE; | |
8441 | } | |
8442 | ||
a96c3cc1 | 8443 | /* Handle a "warn_unused" attribute; arguments as in |
8444 | struct attribute_spec.handler. */ | |
8445 | ||
8446 | static tree | |
8447 | handle_warn_unused_attribute (tree *node, tree name, | |
8448 | tree args ATTRIBUTE_UNUSED, | |
8449 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
8450 | { | |
8451 | if (TYPE_P (*node)) | |
8452 | /* Do nothing else, just set the attribute. We'll get at | |
8453 | it later with lookup_attribute. */ | |
8454 | ; | |
8455 | else | |
8456 | { | |
8457 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8458 | *no_add_attrs = true; | |
8459 | } | |
8460 | ||
8461 | return NULL_TREE; | |
8462 | } | |
8463 | ||
bc7bff74 | 8464 | /* Handle an "omp declare simd" attribute; arguments as in |
8465 | struct attribute_spec.handler. */ | |
8466 | ||
8467 | static tree | |
8468 | handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *) | |
8469 | { | |
8470 | return NULL_TREE; | |
8471 | } | |
8472 | ||
c58a4cfd | 8473 | /* Handle a "simd" attribute. */ |
8474 | ||
8475 | static tree | |
9841c6ee | 8476 | handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs) |
c58a4cfd | 8477 | { |
8478 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
8479 | { | |
8480 | if (lookup_attribute ("cilk simd function", | |
8481 | DECL_ATTRIBUTES (*node)) != NULL) | |
8482 | { | |
8483 | error_at (DECL_SOURCE_LOCATION (*node), | |
8484 | "%<__simd__%> attribute cannot be used in the same " | |
8485 | "function marked as a Cilk Plus SIMD-enabled function"); | |
8486 | *no_add_attrs = true; | |
8487 | } | |
8488 | else | |
9841c6ee | 8489 | { |
8490 | tree t = get_identifier ("omp declare simd"); | |
8491 | tree attr = NULL_TREE; | |
8492 | if (args) | |
8493 | { | |
8494 | tree id = TREE_VALUE (args); | |
8495 | ||
8496 | if (TREE_CODE (id) != STRING_CST) | |
8497 | { | |
8498 | error ("attribute %qE argument not a string", name); | |
8499 | *no_add_attrs = true; | |
8500 | return NULL_TREE; | |
8501 | } | |
8502 | ||
8503 | if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0) | |
8504 | attr = build_omp_clause (DECL_SOURCE_LOCATION (*node), | |
8505 | OMP_CLAUSE_NOTINBRANCH); | |
8506 | else | |
8507 | if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0) | |
8508 | attr = build_omp_clause (DECL_SOURCE_LOCATION (*node), | |
8509 | OMP_CLAUSE_INBRANCH); | |
8510 | else | |
8511 | { | |
8512 | error ("only %<inbranch%> and %<notinbranch%> flags are " | |
8513 | "allowed for %<__simd__%> attribute"); | |
8514 | *no_add_attrs = true; | |
8515 | return NULL_TREE; | |
8516 | } | |
8517 | } | |
8518 | ||
8519 | DECL_ATTRIBUTES (*node) = tree_cons (t, | |
8520 | build_tree_list (NULL_TREE, | |
8521 | attr), | |
8522 | DECL_ATTRIBUTES (*node)); | |
8523 | } | |
c58a4cfd | 8524 | } |
8525 | else | |
8526 | { | |
8527 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8528 | *no_add_attrs = true; | |
8529 | } | |
8530 | ||
8531 | return NULL_TREE; | |
8532 | } | |
8533 | ||
bc7bff74 | 8534 | /* Handle an "omp declare target" attribute; arguments as in |
8535 | struct attribute_spec.handler. */ | |
8536 | ||
8537 | static tree | |
8538 | handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *) | |
8539 | { | |
8540 | return NULL_TREE; | |
8541 | } | |
8542 | ||
26d1c5ff | 8543 | /* Handle a "returns_twice" attribute; arguments as in |
8544 | struct attribute_spec.handler. */ | |
8545 | ||
8546 | static tree | |
8547 | handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
8548 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8549 | { | |
8550 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
8551 | DECL_IS_RETURNS_TWICE (*node) = 1; | |
8552 | else | |
8553 | { | |
9b2d6d13 | 8554 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
26d1c5ff | 8555 | *no_add_attrs = true; |
8556 | } | |
8557 | ||
8558 | return NULL_TREE; | |
8559 | } | |
8560 | ||
f8e93a2e | 8561 | /* Handle a "no_limit_stack" attribute; arguments as in |
8562 | struct attribute_spec.handler. */ | |
8563 | ||
8564 | static tree | |
1cae46be | 8565 | handle_no_limit_stack_attribute (tree *node, tree name, |
9a03a746 | 8566 | tree ARG_UNUSED (args), |
8567 | int ARG_UNUSED (flags), | |
09347743 | 8568 | bool *no_add_attrs) |
f8e93a2e | 8569 | { |
8570 | tree decl = *node; | |
8571 | ||
8572 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8573 | { | |
712d2297 | 8574 | error_at (DECL_SOURCE_LOCATION (decl), |
8575 | "%qE attribute applies only to functions", name); | |
f8e93a2e | 8576 | *no_add_attrs = true; |
8577 | } | |
8578 | else if (DECL_INITIAL (decl)) | |
8579 | { | |
712d2297 | 8580 | error_at (DECL_SOURCE_LOCATION (decl), |
8581 | "can%'t set %qE attribute after definition", name); | |
f8e93a2e | 8582 | *no_add_attrs = true; |
8583 | } | |
8584 | else | |
8585 | DECL_NO_LIMIT_STACK (decl) = 1; | |
8586 | ||
8587 | return NULL_TREE; | |
8588 | } | |
8589 | ||
8590 | /* Handle a "pure" attribute; arguments as in | |
8591 | struct attribute_spec.handler. */ | |
8592 | ||
8593 | static tree | |
9a03a746 | 8594 | handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8595 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 8596 | { |
8597 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
9c2a0c05 | 8598 | DECL_PURE_P (*node) = 1; |
f8e93a2e | 8599 | /* ??? TODO: Support types. */ |
8600 | else | |
8601 | { | |
9b2d6d13 | 8602 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8603 | *no_add_attrs = true; |
8604 | } | |
8605 | ||
8606 | return NULL_TREE; | |
8607 | } | |
8608 | ||
4c0315d0 | 8609 | /* Digest an attribute list destined for a transactional memory statement. |
8610 | ALLOWED is the set of attributes that are allowed for this statement; | |
8611 | return the attribute we parsed. Multiple attributes are never allowed. */ | |
8612 | ||
8613 | int | |
8614 | parse_tm_stmt_attr (tree attrs, int allowed) | |
8615 | { | |
8616 | tree a_seen = NULL; | |
8617 | int m_seen = 0; | |
8618 | ||
8619 | for ( ; attrs ; attrs = TREE_CHAIN (attrs)) | |
8620 | { | |
8621 | tree a = TREE_PURPOSE (attrs); | |
8622 | int m = 0; | |
8623 | ||
8624 | if (is_attribute_p ("outer", a)) | |
8625 | m = TM_STMT_ATTR_OUTER; | |
8626 | ||
8627 | if ((m & allowed) == 0) | |
8628 | { | |
8629 | warning (OPT_Wattributes, "%qE attribute directive ignored", a); | |
8630 | continue; | |
8631 | } | |
8632 | ||
8633 | if (m_seen == 0) | |
8634 | { | |
8635 | a_seen = a; | |
8636 | m_seen = m; | |
8637 | } | |
8638 | else if (m_seen == m) | |
8639 | warning (OPT_Wattributes, "%qE attribute duplicated", a); | |
8640 | else | |
8641 | warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen); | |
8642 | } | |
8643 | ||
8644 | return m_seen; | |
8645 | } | |
8646 | ||
8647 | /* Transform a TM attribute name into a maskable integer and back. | |
8648 | Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding | |
8649 | to how the lack of an attribute is treated. */ | |
8650 | ||
8651 | int | |
8652 | tm_attr_to_mask (tree attr) | |
8653 | { | |
8654 | if (attr == NULL) | |
8655 | return 0; | |
8656 | if (is_attribute_p ("transaction_safe", attr)) | |
8657 | return TM_ATTR_SAFE; | |
8658 | if (is_attribute_p ("transaction_callable", attr)) | |
8659 | return TM_ATTR_CALLABLE; | |
8660 | if (is_attribute_p ("transaction_pure", attr)) | |
8661 | return TM_ATTR_PURE; | |
8662 | if (is_attribute_p ("transaction_unsafe", attr)) | |
8663 | return TM_ATTR_IRREVOCABLE; | |
8664 | if (is_attribute_p ("transaction_may_cancel_outer", attr)) | |
8665 | return TM_ATTR_MAY_CANCEL_OUTER; | |
8666 | return 0; | |
8667 | } | |
8668 | ||
8669 | tree | |
8670 | tm_mask_to_attr (int mask) | |
8671 | { | |
8672 | const char *str; | |
8673 | switch (mask) | |
8674 | { | |
8675 | case TM_ATTR_SAFE: | |
8676 | str = "transaction_safe"; | |
8677 | break; | |
8678 | case TM_ATTR_CALLABLE: | |
8679 | str = "transaction_callable"; | |
8680 | break; | |
8681 | case TM_ATTR_PURE: | |
8682 | str = "transaction_pure"; | |
8683 | break; | |
8684 | case TM_ATTR_IRREVOCABLE: | |
8685 | str = "transaction_unsafe"; | |
8686 | break; | |
8687 | case TM_ATTR_MAY_CANCEL_OUTER: | |
8688 | str = "transaction_may_cancel_outer"; | |
8689 | break; | |
8690 | default: | |
8691 | gcc_unreachable (); | |
8692 | } | |
8693 | return get_identifier (str); | |
8694 | } | |
8695 | ||
8696 | /* Return the first TM attribute seen in LIST. */ | |
8697 | ||
8698 | tree | |
8699 | find_tm_attribute (tree list) | |
8700 | { | |
8701 | for (; list ; list = TREE_CHAIN (list)) | |
8702 | { | |
8703 | tree name = TREE_PURPOSE (list); | |
8704 | if (tm_attr_to_mask (name) != 0) | |
8705 | return name; | |
8706 | } | |
8707 | return NULL_TREE; | |
8708 | } | |
8709 | ||
8710 | /* Handle the TM attributes; arguments as in struct attribute_spec.handler. | |
8711 | Here we accept only function types, and verify that none of the other | |
8712 | function TM attributes are also applied. */ | |
8713 | /* ??? We need to accept class types for C++, but not C. This greatly | |
8714 | complicates this function, since we can no longer rely on the extra | |
8715 | processing given by function_type_required. */ | |
8716 | ||
8717 | static tree | |
8718 | handle_tm_attribute (tree *node, tree name, tree args, | |
8719 | int flags, bool *no_add_attrs) | |
8720 | { | |
8721 | /* Only one path adds the attribute; others don't. */ | |
8722 | *no_add_attrs = true; | |
8723 | ||
8724 | switch (TREE_CODE (*node)) | |
8725 | { | |
8726 | case RECORD_TYPE: | |
8727 | case UNION_TYPE: | |
8728 | /* Only tm_callable and tm_safe apply to classes. */ | |
8729 | if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE)) | |
8730 | goto ignored; | |
8731 | /* FALLTHRU */ | |
8732 | ||
8733 | case FUNCTION_TYPE: | |
8734 | case METHOD_TYPE: | |
8735 | { | |
8736 | tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node)); | |
8737 | if (old_name == name) | |
8738 | ; | |
8739 | else if (old_name != NULL_TREE) | |
8740 | error ("type was previously declared %qE", old_name); | |
8741 | else | |
8742 | *no_add_attrs = false; | |
8743 | } | |
8744 | break; | |
8745 | ||
6d02e6b2 | 8746 | case FUNCTION_DECL: |
8747 | { | |
8748 | /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also | |
8749 | want to set transaction_safe on the type. */ | |
8750 | gcc_assert (is_attribute_p ("transaction_safe_dynamic", name)); | |
8751 | if (!TYPE_P (DECL_CONTEXT (*node))) | |
8752 | error_at (DECL_SOURCE_LOCATION (*node), | |
8753 | "%<transaction_safe_dynamic%> may only be specified for " | |
8754 | "a virtual function"); | |
8755 | *no_add_attrs = false; | |
8756 | decl_attributes (&TREE_TYPE (*node), | |
8757 | build_tree_list (get_identifier ("transaction_safe"), | |
8758 | NULL_TREE), | |
8759 | 0); | |
8760 | break; | |
8761 | } | |
8762 | ||
4c0315d0 | 8763 | case POINTER_TYPE: |
8764 | { | |
8765 | enum tree_code subcode = TREE_CODE (TREE_TYPE (*node)); | |
8766 | if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE) | |
8767 | { | |
8768 | tree fn_tmp = TREE_TYPE (*node); | |
8769 | decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0); | |
8770 | *node = build_pointer_type (fn_tmp); | |
8771 | break; | |
8772 | } | |
8773 | } | |
8774 | /* FALLTHRU */ | |
8775 | ||
8776 | default: | |
8777 | /* If a function is next, pass it on to be tried next. */ | |
8778 | if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) | |
8779 | return tree_cons (name, args, NULL); | |
8780 | ||
8781 | ignored: | |
8782 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8783 | break; | |
8784 | } | |
8785 | ||
8786 | return NULL_TREE; | |
8787 | } | |
8788 | ||
8789 | /* Handle the TM_WRAP attribute; arguments as in | |
8790 | struct attribute_spec.handler. */ | |
8791 | ||
8792 | static tree | |
8793 | handle_tm_wrap_attribute (tree *node, tree name, tree args, | |
8794 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8795 | { | |
8796 | tree decl = *node; | |
8797 | ||
8798 | /* We don't need the attribute even on success, since we | |
8799 | record the entry in an external table. */ | |
8800 | *no_add_attrs = true; | |
8801 | ||
8802 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8803 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8804 | else | |
8805 | { | |
8806 | tree wrap_decl = TREE_VALUE (args); | |
3e5a8b00 | 8807 | if (error_operand_p (wrap_decl)) |
8808 | ; | |
8809 | else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE | |
b443c459 | 8810 | && !VAR_OR_FUNCTION_DECL_P (wrap_decl)) |
4c0315d0 | 8811 | error ("%qE argument not an identifier", name); |
8812 | else | |
8813 | { | |
8814 | if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE) | |
8815 | wrap_decl = lookup_name (wrap_decl); | |
8816 | if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL) | |
8817 | { | |
8818 | if (lang_hooks.types_compatible_p (TREE_TYPE (decl), | |
8819 | TREE_TYPE (wrap_decl))) | |
8820 | record_tm_replacement (wrap_decl, decl); | |
8821 | else | |
8822 | error ("%qD is not compatible with %qD", wrap_decl, decl); | |
8823 | } | |
8824 | else | |
cdf34fca | 8825 | error ("%qE argument is not a function", name); |
4c0315d0 | 8826 | } |
8827 | } | |
8828 | ||
8829 | return NULL_TREE; | |
8830 | } | |
8831 | ||
8832 | /* Ignore the given attribute. Used when this attribute may be usefully | |
8833 | overridden by the target, but is not used generically. */ | |
8834 | ||
8835 | static tree | |
8836 | ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name), | |
8837 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8838 | bool *no_add_attrs) | |
8839 | { | |
8840 | *no_add_attrs = true; | |
8841 | return NULL_TREE; | |
8842 | } | |
8843 | ||
fc09b200 | 8844 | /* Handle a "no vops" attribute; arguments as in |
8845 | struct attribute_spec.handler. */ | |
8846 | ||
8847 | static tree | |
8848 | handle_novops_attribute (tree *node, tree ARG_UNUSED (name), | |
8849 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8850 | bool *ARG_UNUSED (no_add_attrs)) | |
8851 | { | |
8852 | gcc_assert (TREE_CODE (*node) == FUNCTION_DECL); | |
8853 | DECL_IS_NOVOPS (*node) = 1; | |
8854 | return NULL_TREE; | |
8855 | } | |
8856 | ||
f8e93a2e | 8857 | /* Handle a "deprecated" attribute; arguments as in |
8858 | struct attribute_spec.handler. */ | |
1cae46be | 8859 | |
f8e93a2e | 8860 | static tree |
1cae46be | 8861 | handle_deprecated_attribute (tree *node, tree name, |
45c4e798 | 8862 | tree args, int flags, |
09347743 | 8863 | bool *no_add_attrs) |
f8e93a2e | 8864 | { |
8865 | tree type = NULL_TREE; | |
8866 | int warn = 0; | |
782858b8 | 8867 | tree what = NULL_TREE; |
1cae46be | 8868 | |
45c4e798 | 8869 | if (!args) |
8870 | *no_add_attrs = true; | |
8871 | else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST) | |
8872 | { | |
8873 | error ("deprecated message is not a string"); | |
8874 | *no_add_attrs = true; | |
8875 | } | |
8876 | ||
f8e93a2e | 8877 | if (DECL_P (*node)) |
8878 | { | |
8879 | tree decl = *node; | |
8880 | type = TREE_TYPE (decl); | |
1cae46be | 8881 | |
f8e93a2e | 8882 | if (TREE_CODE (decl) == TYPE_DECL |
8883 | || TREE_CODE (decl) == PARM_DECL | |
b443c459 | 8884 | || VAR_OR_FUNCTION_DECL_P (decl) |
40c8d1dd | 8885 | || TREE_CODE (decl) == FIELD_DECL |
5a4c69dd | 8886 | || TREE_CODE (decl) == CONST_DECL |
40c8d1dd | 8887 | || objc_method_decl (TREE_CODE (decl))) |
f8e93a2e | 8888 | TREE_DEPRECATED (decl) = 1; |
8889 | else | |
8890 | warn = 1; | |
8891 | } | |
8892 | else if (TYPE_P (*node)) | |
8893 | { | |
8894 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 8895 | *node = build_variant_type_copy (*node); |
f8e93a2e | 8896 | TREE_DEPRECATED (*node) = 1; |
8897 | type = *node; | |
8898 | } | |
8899 | else | |
8900 | warn = 1; | |
1cae46be | 8901 | |
f8e93a2e | 8902 | if (warn) |
8903 | { | |
8904 | *no_add_attrs = true; | |
8905 | if (type && TYPE_NAME (type)) | |
8906 | { | |
8907 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
782858b8 | 8908 | what = TYPE_NAME (*node); |
f8e93a2e | 8909 | else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL |
8910 | && DECL_NAME (TYPE_NAME (type))) | |
782858b8 | 8911 | what = DECL_NAME (TYPE_NAME (type)); |
f8e93a2e | 8912 | } |
8913 | if (what) | |
9b2d6d13 | 8914 | warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what); |
f8e93a2e | 8915 | else |
9b2d6d13 | 8916 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8917 | } |
8918 | ||
8919 | return NULL_TREE; | |
8920 | } | |
8921 | ||
f8e93a2e | 8922 | /* Handle a "vector_size" attribute; arguments as in |
8923 | struct attribute_spec.handler. */ | |
8924 | ||
8925 | static tree | |
1cae46be | 8926 | handle_vector_size_attribute (tree *node, tree name, tree args, |
9a03a746 | 8927 | int ARG_UNUSED (flags), |
09347743 | 8928 | bool *no_add_attrs) |
f8e93a2e | 8929 | { |
8930 | unsigned HOST_WIDE_INT vecsize, nunits; | |
3754d046 | 8931 | machine_mode orig_mode; |
4917c376 | 8932 | tree type = *node, new_type, size; |
f8e93a2e | 8933 | |
8934 | *no_add_attrs = true; | |
8935 | ||
4917c376 | 8936 | size = TREE_VALUE (args); |
3e5a8b00 | 8937 | if (size && TREE_CODE (size) != IDENTIFIER_NODE |
8938 | && TREE_CODE (size) != FUNCTION_DECL) | |
caf62483 | 8939 | size = default_conversion (size); |
4917c376 | 8940 | |
e913b5cd | 8941 | if (!tree_fits_uhwi_p (size)) |
f8e93a2e | 8942 | { |
9b2d6d13 | 8943 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8944 | return NULL_TREE; |
8945 | } | |
8946 | ||
8947 | /* Get the vector size (in bytes). */ | |
e913b5cd | 8948 | vecsize = tree_to_uhwi (size); |
f8e93a2e | 8949 | |
8950 | /* We need to provide for vector pointers, vector arrays, and | |
8951 | functions returning vectors. For example: | |
8952 | ||
8953 | __attribute__((vector_size(16))) short *foo; | |
8954 | ||
8955 | In this case, the mode is SI, but the type being modified is | |
8956 | HI, so we need to look further. */ | |
8957 | ||
8958 | while (POINTER_TYPE_P (type) | |
8959 | || TREE_CODE (type) == FUNCTION_TYPE | |
5bfb0742 | 8960 | || TREE_CODE (type) == METHOD_TYPE |
2cb0e5d9 | 8961 | || TREE_CODE (type) == ARRAY_TYPE |
8962 | || TREE_CODE (type) == OFFSET_TYPE) | |
f8e93a2e | 8963 | type = TREE_TYPE (type); |
8964 | ||
8965 | /* Get the mode of the type being modified. */ | |
8966 | orig_mode = TYPE_MODE (type); | |
8967 | ||
2cb0e5d9 | 8968 | if ((!INTEGRAL_TYPE_P (type) |
8969 | && !SCALAR_FLOAT_TYPE_P (type) | |
8970 | && !FIXED_POINT_TYPE_P (type)) | |
cee7491d | 8971 | || (!SCALAR_FLOAT_MODE_P (orig_mode) |
9421ebb9 | 8972 | && GET_MODE_CLASS (orig_mode) != MODE_INT |
8973 | && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode)) | |
e913b5cd | 8974 | || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)) |
7ec31215 | 8975 | || TREE_CODE (type) == BOOLEAN_TYPE) |
f8e93a2e | 8976 | { |
782858b8 | 8977 | error ("invalid vector type for attribute %qE", name); |
f8e93a2e | 8978 | return NULL_TREE; |
8979 | } | |
8980 | ||
e913b5cd | 8981 | if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type))) |
39cc3e6d | 8982 | { |
8983 | error ("vector size not an integral multiple of component size"); | |
8984 | return NULL; | |
8985 | } | |
8986 | ||
8987 | if (vecsize == 0) | |
8988 | { | |
8989 | error ("zero vector size"); | |
8990 | return NULL; | |
8991 | } | |
8992 | ||
f8e93a2e | 8993 | /* Calculate how many units fit in the vector. */ |
e913b5cd | 8994 | nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
83e2a11b | 8995 | if (nunits & (nunits - 1)) |
f8e93a2e | 8996 | { |
83e2a11b | 8997 | error ("number of components of the vector not a power of two"); |
f8e93a2e | 8998 | return NULL_TREE; |
8999 | } | |
9000 | ||
83e2a11b | 9001 | new_type = build_vector_type (type, nunits); |
f8e93a2e | 9002 | |
9003 | /* Build back pointers if needed. */ | |
d991e6e8 | 9004 | *node = lang_hooks.types.reconstruct_complex_type (*node, new_type); |
f8e93a2e | 9005 | |
9006 | return NULL_TREE; | |
9007 | } | |
9008 | ||
dbf6c367 | 9009 | /* Handle the "nonnull" attribute. */ |
9010 | static tree | |
9a03a746 | 9011 | handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), |
9012 | tree args, int ARG_UNUSED (flags), | |
09347743 | 9013 | bool *no_add_attrs) |
dbf6c367 | 9014 | { |
9015 | tree type = *node; | |
9016 | unsigned HOST_WIDE_INT attr_arg_num; | |
9017 | ||
9018 | /* If no arguments are specified, all pointer arguments should be | |
d716ce75 | 9019 | non-null. Verify a full prototype is given so that the arguments |
dbf6c367 | 9020 | will have the correct types when we actually check them later. */ |
84166705 | 9021 | if (!args) |
dbf6c367 | 9022 | { |
a36cf284 | 9023 | if (!prototype_p (type)) |
dbf6c367 | 9024 | { |
9025 | error ("nonnull attribute without arguments on a non-prototype"); | |
4ee9c684 | 9026 | *no_add_attrs = true; |
dbf6c367 | 9027 | } |
9028 | return NULL_TREE; | |
9029 | } | |
9030 | ||
9031 | /* Argument list specified. Verify that each argument number references | |
9032 | a pointer argument. */ | |
caf62483 | 9033 | for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args)) |
dbf6c367 | 9034 | { |
4ee9c684 | 9035 | unsigned HOST_WIDE_INT arg_num = 0, ck_num; |
dbf6c367 | 9036 | |
caf62483 | 9037 | tree arg = TREE_VALUE (args); |
9038 | if (arg && TREE_CODE (arg) != IDENTIFIER_NODE | |
9039 | && TREE_CODE (arg) != FUNCTION_DECL) | |
9040 | arg = default_conversion (arg); | |
9041 | ||
9042 | if (!get_nonnull_operand (arg, &arg_num)) | |
dbf6c367 | 9043 | { |
07e3a3d2 | 9044 | error ("nonnull argument has invalid operand number (argument %lu)", |
dbf6c367 | 9045 | (unsigned long) attr_arg_num); |
9046 | *no_add_attrs = true; | |
9047 | return NULL_TREE; | |
9048 | } | |
9049 | ||
d0af78c5 | 9050 | if (prototype_p (type)) |
dbf6c367 | 9051 | { |
d0af78c5 | 9052 | function_args_iterator iter; |
9053 | tree argument; | |
9054 | ||
9055 | function_args_iter_init (&iter, type); | |
9056 | for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter)) | |
dbf6c367 | 9057 | { |
d0af78c5 | 9058 | argument = function_args_iter_cond (&iter); |
9059 | if (argument == NULL_TREE || ck_num == arg_num) | |
dbf6c367 | 9060 | break; |
dbf6c367 | 9061 | } |
9062 | ||
84166705 | 9063 | if (!argument |
d0af78c5 | 9064 | || TREE_CODE (argument) == VOID_TYPE) |
dbf6c367 | 9065 | { |
07e3a3d2 | 9066 | error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)", |
dbf6c367 | 9067 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
9068 | *no_add_attrs = true; | |
9069 | return NULL_TREE; | |
9070 | } | |
9071 | ||
d0af78c5 | 9072 | if (TREE_CODE (argument) != POINTER_TYPE) |
dbf6c367 | 9073 | { |
07e3a3d2 | 9074 | error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)", |
dbf6c367 | 9075 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
9076 | *no_add_attrs = true; | |
9077 | return NULL_TREE; | |
9078 | } | |
9079 | } | |
9080 | } | |
9081 | ||
9082 | return NULL_TREE; | |
9083 | } | |
9084 | ||
9085 | /* Check the argument list of a function call for null in argument slots | |
d01f58f9 | 9086 | that are marked as requiring a non-null pointer argument. The NARGS |
9087 | arguments are passed in the array ARGARRAY. | |
9088 | */ | |
dbf6c367 | 9089 | |
9090 | static void | |
d01f58f9 | 9091 | check_function_nonnull (tree attrs, int nargs, tree *argarray) |
dbf6c367 | 9092 | { |
9ca77b08 | 9093 | tree a; |
d01f58f9 | 9094 | int i; |
dbf6c367 | 9095 | |
9ca77b08 | 9096 | attrs = lookup_attribute ("nonnull", attrs); |
9097 | if (attrs == NULL_TREE) | |
9098 | return; | |
9099 | ||
9100 | a = attrs; | |
9101 | /* See if any of the nonnull attributes has no arguments. If so, | |
9102 | then every pointer argument is checked (in which case the check | |
9103 | for pointer type is done in check_nonnull_arg). */ | |
9104 | if (TREE_VALUE (a) != NULL_TREE) | |
9105 | do | |
9106 | a = lookup_attribute ("nonnull", TREE_CHAIN (a)); | |
9107 | while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE); | |
9108 | ||
9109 | if (a != NULL_TREE) | |
9110 | for (i = 0; i < nargs; i++) | |
9111 | check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i], | |
9112 | i + 1); | |
9113 | else | |
dbf6c367 | 9114 | { |
9ca77b08 | 9115 | /* Walk the argument list. If we encounter an argument number we |
9116 | should check for non-null, do it. */ | |
9117 | for (i = 0; i < nargs; i++) | |
dbf6c367 | 9118 | { |
9ca77b08 | 9119 | for (a = attrs; ; a = TREE_CHAIN (a)) |
4ee9c684 | 9120 | { |
9ca77b08 | 9121 | a = lookup_attribute ("nonnull", a); |
9122 | if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1)) | |
9123 | break; | |
4ee9c684 | 9124 | } |
9ca77b08 | 9125 | |
9126 | if (a != NULL_TREE) | |
9127 | check_function_arguments_recurse (check_nonnull_arg, NULL, | |
9128 | argarray[i], i + 1); | |
dbf6c367 | 9129 | } |
9130 | } | |
9131 | } | |
9132 | ||
50ca527f | 9133 | /* Check that the Nth argument of a function call (counting backwards |
d01f58f9 | 9134 | from the end) is a (pointer)0. The NARGS arguments are passed in the |
9135 | array ARGARRAY. */ | |
bf6c8de0 | 9136 | |
9137 | static void | |
774e9d58 | 9138 | check_function_sentinel (const_tree fntype, int nargs, tree *argarray) |
bf6c8de0 | 9139 | { |
774e9d58 | 9140 | tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype)); |
bf6c8de0 | 9141 | |
9142 | if (attr) | |
9143 | { | |
d01f58f9 | 9144 | int len = 0; |
9145 | int pos = 0; | |
9146 | tree sentinel; | |
774e9d58 | 9147 | function_args_iterator iter; |
9148 | tree t; | |
a0c938f0 | 9149 | |
d01f58f9 | 9150 | /* Skip over the named arguments. */ |
774e9d58 | 9151 | FOREACH_FUNCTION_ARGS (fntype, t, iter) |
a0c938f0 | 9152 | { |
774e9d58 | 9153 | if (len == nargs) |
9154 | break; | |
d01f58f9 | 9155 | len++; |
9156 | } | |
50ca527f | 9157 | |
d01f58f9 | 9158 | if (TREE_VALUE (attr)) |
9159 | { | |
9160 | tree p = TREE_VALUE (TREE_VALUE (attr)); | |
f9ae6f95 | 9161 | pos = TREE_INT_CST_LOW (p); |
d01f58f9 | 9162 | } |
50ca527f | 9163 | |
d01f58f9 | 9164 | /* The sentinel must be one of the varargs, i.e. |
9165 | in position >= the number of fixed arguments. */ | |
9166 | if ((nargs - 1 - pos) < len) | |
9167 | { | |
77a357e3 | 9168 | warning (OPT_Wformat_, |
d01f58f9 | 9169 | "not enough variable arguments to fit a sentinel"); |
9170 | return; | |
bf6c8de0 | 9171 | } |
d01f58f9 | 9172 | |
9173 | /* Validate the sentinel. */ | |
9174 | sentinel = argarray[nargs - 1 - pos]; | |
9175 | if ((!POINTER_TYPE_P (TREE_TYPE (sentinel)) | |
9176 | || !integer_zerop (sentinel)) | |
9177 | /* Although __null (in C++) is only an integer we allow it | |
9178 | nevertheless, as we are guaranteed that it's exactly | |
9179 | as wide as a pointer, and we don't want to force | |
9180 | users to cast the NULL they have written there. | |
9181 | We warn with -Wstrict-null-sentinel, though. */ | |
9182 | && (warn_strict_null_sentinel || null_node != sentinel)) | |
77a357e3 | 9183 | warning (OPT_Wformat_, "missing sentinel in function call"); |
bf6c8de0 | 9184 | } |
9185 | } | |
9186 | ||
dbf6c367 | 9187 | /* Helper for check_function_nonnull; given a list of operands which |
9188 | must be non-null in ARGS, determine if operand PARAM_NUM should be | |
9189 | checked. */ | |
9190 | ||
9191 | static bool | |
1cae46be | 9192 | nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 9193 | { |
4ee9c684 | 9194 | unsigned HOST_WIDE_INT arg_num = 0; |
dbf6c367 | 9195 | |
9196 | for (; args; args = TREE_CHAIN (args)) | |
9197 | { | |
231bd014 | 9198 | bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num); |
9199 | ||
9200 | gcc_assert (found); | |
dbf6c367 | 9201 | |
9202 | if (arg_num == param_num) | |
9203 | return true; | |
9204 | } | |
9205 | return false; | |
9206 | } | |
9207 | ||
9208 | /* Check that the function argument PARAM (which is operand number | |
9209 | PARAM_NUM) is non-null. This is called by check_function_nonnull | |
9210 | via check_function_arguments_recurse. */ | |
9211 | ||
9212 | static void | |
9a03a746 | 9213 | check_nonnull_arg (void * ARG_UNUSED (ctx), tree param, |
1cae46be | 9214 | unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 9215 | { |
9216 | /* Just skip checking the argument if it's not a pointer. This can | |
9217 | happen if the "nonnull" attribute was given without an operand | |
9218 | list (which means to check every pointer argument). */ | |
9219 | ||
9220 | if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) | |
9221 | return; | |
9222 | ||
9223 | if (integer_zerop (param)) | |
155b601b | 9224 | warning (OPT_Wnonnull, "null argument where non-null required " |
9225 | "(argument %lu)", (unsigned long) param_num); | |
dbf6c367 | 9226 | } |
9227 | ||
9228 | /* Helper for nonnull attribute handling; fetch the operand number | |
9229 | from the attribute argument list. */ | |
9230 | ||
9231 | static bool | |
1cae46be | 9232 | get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) |
dbf6c367 | 9233 | { |
e913b5cd | 9234 | /* Verify the arg number is a small constant. */ |
e1d65c9f | 9235 | if (tree_fits_uhwi_p (arg_num_expr)) |
e913b5cd | 9236 | { |
f9ae6f95 | 9237 | *valp = TREE_INT_CST_LOW (arg_num_expr); |
e913b5cd | 9238 | return true; |
9239 | } | |
9240 | else | |
dbf6c367 | 9241 | return false; |
dbf6c367 | 9242 | } |
fa987697 | 9243 | |
9244 | /* Handle a "nothrow" attribute; arguments as in | |
9245 | struct attribute_spec.handler. */ | |
9246 | ||
9247 | static tree | |
9a03a746 | 9248 | handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
9249 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
fa987697 | 9250 | { |
9251 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
9252 | TREE_NOTHROW (*node) = 1; | |
9253 | /* ??? TODO: Support types. */ | |
9254 | else | |
9255 | { | |
9b2d6d13 | 9256 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
fa987697 | 9257 | *no_add_attrs = true; |
9258 | } | |
9259 | ||
9260 | return NULL_TREE; | |
9261 | } | |
7acb29a3 | 9262 | |
9263 | /* Handle a "cleanup" attribute; arguments as in | |
9264 | struct attribute_spec.handler. */ | |
9265 | ||
9266 | static tree | |
1cae46be | 9267 | handle_cleanup_attribute (tree *node, tree name, tree args, |
9a03a746 | 9268 | int ARG_UNUSED (flags), bool *no_add_attrs) |
7acb29a3 | 9269 | { |
9270 | tree decl = *node; | |
9271 | tree cleanup_id, cleanup_decl; | |
9272 | ||
9273 | /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do | |
9274 | for global destructors in C++. This requires infrastructure that | |
9275 | we don't have generically at the moment. It's also not a feature | |
9276 | we'd be missing too much, since we do have attribute constructor. */ | |
f48c7f4a | 9277 | if (!VAR_P (decl) || TREE_STATIC (decl)) |
7acb29a3 | 9278 | { |
9b2d6d13 | 9279 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
7acb29a3 | 9280 | *no_add_attrs = true; |
9281 | return NULL_TREE; | |
9282 | } | |
9283 | ||
9284 | /* Verify that the argument is a function in scope. */ | |
9285 | /* ??? We could support pointers to functions here as well, if | |
9286 | that was considered desirable. */ | |
9287 | cleanup_id = TREE_VALUE (args); | |
9288 | if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE) | |
9289 | { | |
07e3a3d2 | 9290 | error ("cleanup argument not an identifier"); |
7acb29a3 | 9291 | *no_add_attrs = true; |
9292 | return NULL_TREE; | |
9293 | } | |
d1c41717 | 9294 | cleanup_decl = lookup_name (cleanup_id); |
7acb29a3 | 9295 | if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL) |
9296 | { | |
07e3a3d2 | 9297 | error ("cleanup argument not a function"); |
7acb29a3 | 9298 | *no_add_attrs = true; |
9299 | return NULL_TREE; | |
9300 | } | |
9301 | ||
1cae46be | 9302 | /* That the function has proper type is checked with the |
7acb29a3 | 9303 | eventual call to build_function_call. */ |
9304 | ||
9305 | return NULL_TREE; | |
9306 | } | |
8a8cdb8d | 9307 | |
9308 | /* Handle a "warn_unused_result" attribute. No special handling. */ | |
9309 | ||
9310 | static tree | |
9311 | handle_warn_unused_result_attribute (tree *node, tree name, | |
9a03a746 | 9312 | tree ARG_UNUSED (args), |
9313 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8a8cdb8d | 9314 | { |
9315 | /* Ignore the attribute for functions not returning any value. */ | |
9316 | if (VOID_TYPE_P (TREE_TYPE (*node))) | |
9317 | { | |
9b2d6d13 | 9318 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
8a8cdb8d | 9319 | *no_add_attrs = true; |
9320 | } | |
9321 | ||
9322 | return NULL_TREE; | |
9323 | } | |
bf6c8de0 | 9324 | |
9325 | /* Handle a "sentinel" attribute. */ | |
9326 | ||
9327 | static tree | |
50ca527f | 9328 | handle_sentinel_attribute (tree *node, tree name, tree args, |
bf6c8de0 | 9329 | int ARG_UNUSED (flags), bool *no_add_attrs) |
9330 | { | |
a36cf284 | 9331 | if (!prototype_p (*node)) |
bf6c8de0 | 9332 | { |
9b2d6d13 | 9333 | warning (OPT_Wattributes, |
9334 | "%qE attribute requires prototypes with named arguments", name); | |
bf6c8de0 | 9335 | *no_add_attrs = true; |
bf6c8de0 | 9336 | } |
50ca527f | 9337 | else |
9338 | { | |
c33080b9 | 9339 | if (!stdarg_p (*node)) |
a0c938f0 | 9340 | { |
9b2d6d13 | 9341 | warning (OPT_Wattributes, |
9342 | "%qE attribute only applies to variadic functions", name); | |
50ca527f | 9343 | *no_add_attrs = true; |
9344 | } | |
9345 | } | |
a0c938f0 | 9346 | |
50ca527f | 9347 | if (args) |
bf6c8de0 | 9348 | { |
50ca527f | 9349 | tree position = TREE_VALUE (args); |
3e5a8b00 | 9350 | if (position && TREE_CODE (position) != IDENTIFIER_NODE |
9351 | && TREE_CODE (position) != FUNCTION_DECL) | |
9352 | position = default_conversion (position); | |
50ca527f | 9353 | |
3e5a8b00 | 9354 | if (TREE_CODE (position) != INTEGER_CST |
9355 | || !INTEGRAL_TYPE_P (TREE_TYPE (position))) | |
a0c938f0 | 9356 | { |
48e1416a | 9357 | warning (OPT_Wattributes, |
01b54db5 | 9358 | "requested position is not an integer constant"); |
50ca527f | 9359 | *no_add_attrs = true; |
9360 | } | |
9361 | else | |
a0c938f0 | 9362 | { |
50ca527f | 9363 | if (tree_int_cst_lt (position, integer_zero_node)) |
9364 | { | |
01b54db5 | 9365 | warning (OPT_Wattributes, |
9366 | "requested position is less than zero"); | |
50ca527f | 9367 | *no_add_attrs = true; |
9368 | } | |
9369 | } | |
bf6c8de0 | 9370 | } |
a0c938f0 | 9371 | |
bf6c8de0 | 9372 | return NULL_TREE; |
9373 | } | |
b5c26b42 | 9374 | |
9375 | /* Handle a "type_generic" attribute. */ | |
9376 | ||
9377 | static tree | |
9378 | handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name), | |
9379 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
9380 | bool * ARG_UNUSED (no_add_attrs)) | |
9381 | { | |
19fbe3a4 | 9382 | /* Ensure we have a function type. */ |
9383 | gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE); | |
48e1416a | 9384 | |
19fbe3a4 | 9385 | /* Ensure we have a variadic function. */ |
c33080b9 | 9386 | gcc_assert (!prototype_p (*node) || stdarg_p (*node)); |
b5c26b42 | 9387 | |
9388 | return NULL_TREE; | |
9389 | } | |
46f8e3b0 | 9390 | |
24470055 | 9391 | /* Handle a "target" attribute. */ |
46f8e3b0 | 9392 | |
9393 | static tree | |
24470055 | 9394 | handle_target_attribute (tree *node, tree name, tree args, int flags, |
46f8e3b0 | 9395 | bool *no_add_attrs) |
9396 | { | |
9397 | /* Ensure we have a function type. */ | |
9398 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
9399 | { | |
9400 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
9401 | *no_add_attrs = true; | |
9402 | } | |
ab50af2a | 9403 | else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node))) |
9404 | { | |
9405 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " | |
9406 | "with %qs attribute", name, "target_clones"); | |
9407 | *no_add_attrs = true; | |
9408 | } | |
46f8e3b0 | 9409 | else if (! targetm.target_option.valid_attribute_p (*node, name, args, |
ae0c3984 | 9410 | flags)) |
46f8e3b0 | 9411 | *no_add_attrs = true; |
9412 | ||
9413 | return NULL_TREE; | |
9414 | } | |
9415 | ||
ab50af2a | 9416 | /* Handle a "target_clones" attribute. */ |
9417 | ||
9418 | static tree | |
9419 | handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
9420 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
9421 | { | |
9422 | /* Ensure we have a function type. */ | |
9423 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
9424 | { | |
9425 | if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node))) | |
9426 | { | |
9427 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " | |
9428 | "with %qs attribute", name, "always_inline"); | |
9429 | *no_add_attrs = true; | |
9430 | } | |
9431 | else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node))) | |
9432 | { | |
9433 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " | |
9434 | "with %qs attribute", name, "target"); | |
9435 | *no_add_attrs = true; | |
9436 | } | |
9437 | else | |
9438 | /* Do not inline functions with multiple clone targets. */ | |
9439 | DECL_UNINLINABLE (*node) = 1; | |
9440 | } | |
9441 | else | |
9442 | { | |
9443 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
9444 | *no_add_attrs = true; | |
9445 | } | |
9446 | return NULL_TREE; | |
9447 | } | |
9448 | ||
46f8e3b0 | 9449 | /* Arguments being collected for optimization. */ |
9450 | typedef const char *const_char_p; /* For DEF_VEC_P. */ | |
f1f41a6c | 9451 | static GTY(()) vec<const_char_p, va_gc> *optimize_args; |
46f8e3b0 | 9452 | |
9453 | ||
9454 | /* Inner function to convert a TREE_LIST to argv string to parse the optimize | |
9455 | options in ARGS. ATTR_P is true if this is for attribute(optimize), and | |
9456 | false for #pragma GCC optimize. */ | |
9457 | ||
9458 | bool | |
9459 | parse_optimize_options (tree args, bool attr_p) | |
9460 | { | |
9461 | bool ret = true; | |
9462 | unsigned opt_argc; | |
9463 | unsigned i; | |
9464 | const char **opt_argv; | |
615ef0bb | 9465 | struct cl_decoded_option *decoded_options; |
9466 | unsigned int decoded_options_count; | |
46f8e3b0 | 9467 | tree ap; |
9468 | ||
9469 | /* Build up argv vector. Just in case the string is stored away, use garbage | |
9470 | collected strings. */ | |
f1f41a6c | 9471 | vec_safe_truncate (optimize_args, 0); |
9472 | vec_safe_push (optimize_args, (const char *) NULL); | |
46f8e3b0 | 9473 | |
9474 | for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap)) | |
9475 | { | |
9476 | tree value = TREE_VALUE (ap); | |
9477 | ||
9478 | if (TREE_CODE (value) == INTEGER_CST) | |
9479 | { | |
9480 | char buffer[20]; | |
f9ae6f95 | 9481 | sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value)); |
f1f41a6c | 9482 | vec_safe_push (optimize_args, ggc_strdup (buffer)); |
46f8e3b0 | 9483 | } |
9484 | ||
9485 | else if (TREE_CODE (value) == STRING_CST) | |
9486 | { | |
9487 | /* Split string into multiple substrings. */ | |
9488 | size_t len = TREE_STRING_LENGTH (value); | |
9489 | char *p = ASTRDUP (TREE_STRING_POINTER (value)); | |
9490 | char *end = p + len; | |
9491 | char *comma; | |
9492 | char *next_p = p; | |
9493 | ||
9494 | while (next_p != NULL) | |
9495 | { | |
9496 | size_t len2; | |
9497 | char *q, *r; | |
9498 | ||
9499 | p = next_p; | |
9500 | comma = strchr (p, ','); | |
9501 | if (comma) | |
9502 | { | |
9503 | len2 = comma - p; | |
9504 | *comma = '\0'; | |
9505 | next_p = comma+1; | |
9506 | } | |
9507 | else | |
9508 | { | |
9509 | len2 = end - p; | |
9510 | next_p = NULL; | |
9511 | } | |
9512 | ||
ba72912a | 9513 | r = q = (char *) ggc_alloc_atomic (len2 + 3); |
46f8e3b0 | 9514 | |
9515 | /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx | |
9516 | options. */ | |
9517 | if (*p == '-' && p[1] != 'O' && p[1] != 'f') | |
9518 | { | |
9519 | ret = false; | |
9520 | if (attr_p) | |
9521 | warning (OPT_Wattributes, | |
ebd7c4c1 | 9522 | "bad option %s to optimize attribute", p); |
46f8e3b0 | 9523 | else |
9524 | warning (OPT_Wpragmas, | |
e44b0a1f | 9525 | "bad option %s to pragma attribute", p); |
46f8e3b0 | 9526 | continue; |
9527 | } | |
9528 | ||
9529 | if (*p != '-') | |
9530 | { | |
9531 | *r++ = '-'; | |
9532 | ||
9533 | /* Assume that Ox is -Ox, a numeric value is -Ox, a s by | |
9534 | itself is -Os, and any other switch begins with a -f. */ | |
9535 | if ((*p >= '0' && *p <= '9') | |
9536 | || (p[0] == 's' && p[1] == '\0')) | |
9537 | *r++ = 'O'; | |
9538 | else if (*p != 'O') | |
9539 | *r++ = 'f'; | |
9540 | } | |
9541 | ||
9542 | memcpy (r, p, len2); | |
9543 | r[len2] = '\0'; | |
f1f41a6c | 9544 | vec_safe_push (optimize_args, (const char *) q); |
46f8e3b0 | 9545 | } |
9546 | ||
9547 | } | |
9548 | } | |
9549 | ||
f1f41a6c | 9550 | opt_argc = optimize_args->length (); |
46f8e3b0 | 9551 | opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1)); |
9552 | ||
9553 | for (i = 1; i < opt_argc; i++) | |
f1f41a6c | 9554 | opt_argv[i] = (*optimize_args)[i]; |
46f8e3b0 | 9555 | |
9556 | /* Now parse the options. */ | |
f3f006ad | 9557 | decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv, |
9558 | &decoded_options, | |
9559 | &decoded_options_count); | |
9560 | decode_options (&global_options, &global_options_set, | |
3c6c0e40 | 9561 | decoded_options, decoded_options_count, |
9562 | input_location, global_dc); | |
46f8e3b0 | 9563 | |
4bec06b3 | 9564 | targetm.override_options_after_change(); |
9565 | ||
f1f41a6c | 9566 | optimize_args->truncate (0); |
46f8e3b0 | 9567 | return ret; |
9568 | } | |
9569 | ||
9570 | /* For handling "optimize" attribute. arguments as in | |
9571 | struct attribute_spec.handler. */ | |
9572 | ||
9573 | static tree | |
9574 | handle_optimize_attribute (tree *node, tree name, tree args, | |
9575 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
9576 | { | |
9577 | /* Ensure we have a function type. */ | |
9578 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
9579 | { | |
9580 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
9581 | *no_add_attrs = true; | |
9582 | } | |
9583 | else | |
9584 | { | |
9585 | struct cl_optimization cur_opts; | |
9586 | tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node); | |
9587 | ||
9588 | /* Save current options. */ | |
2c5d2e39 | 9589 | cl_optimization_save (&cur_opts, &global_options); |
46f8e3b0 | 9590 | |
9591 | /* If we previously had some optimization options, use them as the | |
9592 | default. */ | |
9593 | if (old_opts) | |
2c5d2e39 | 9594 | cl_optimization_restore (&global_options, |
9595 | TREE_OPTIMIZATION (old_opts)); | |
46f8e3b0 | 9596 | |
9597 | /* Parse options, and update the vector. */ | |
9598 | parse_optimize_options (args, true); | |
9599 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) | |
55310327 | 9600 | = build_optimization_node (&global_options); |
46f8e3b0 | 9601 | |
9602 | /* Restore current options. */ | |
2c5d2e39 | 9603 | cl_optimization_restore (&global_options, &cur_opts); |
46f8e3b0 | 9604 | } |
9605 | ||
9606 | return NULL_TREE; | |
9607 | } | |
48b14f50 | 9608 | |
9609 | /* Handle a "no_split_stack" attribute. */ | |
9610 | ||
9611 | static tree | |
9612 | handle_no_split_stack_attribute (tree *node, tree name, | |
9613 | tree ARG_UNUSED (args), | |
9614 | int ARG_UNUSED (flags), | |
9615 | bool *no_add_attrs) | |
9616 | { | |
9617 | tree decl = *node; | |
9618 | ||
9619 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
9620 | { | |
9621 | error_at (DECL_SOURCE_LOCATION (decl), | |
9622 | "%qE attribute applies only to functions", name); | |
9623 | *no_add_attrs = true; | |
9624 | } | |
9625 | else if (DECL_INITIAL (decl)) | |
9626 | { | |
9627 | error_at (DECL_SOURCE_LOCATION (decl), | |
9628 | "can%'t set %qE attribute after definition", name); | |
9629 | *no_add_attrs = true; | |
9630 | } | |
9631 | ||
9632 | return NULL_TREE; | |
9633 | } | |
d7dcba40 | 9634 | |
9635 | /* Handle a "returns_nonnull" attribute; arguments as in | |
9636 | struct attribute_spec.handler. */ | |
9637 | ||
9638 | static tree | |
9639 | handle_returns_nonnull_attribute (tree *node, tree, tree, int, | |
9640 | bool *no_add_attrs) | |
9641 | { | |
9642 | // Even without a prototype we still have a return type we can check. | |
9643 | if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE) | |
9644 | { | |
9645 | error ("returns_nonnull attribute on a function not returning a pointer"); | |
9646 | *no_add_attrs = true; | |
9647 | } | |
9648 | return NULL_TREE; | |
9649 | } | |
9650 | ||
74691f46 | 9651 | /* Handle a "designated_init" attribute; arguments as in |
9652 | struct attribute_spec.handler. */ | |
9653 | ||
9654 | static tree | |
9655 | handle_designated_init_attribute (tree *node, tree name, tree, int, | |
9656 | bool *no_add_attrs) | |
9657 | { | |
9658 | if (TREE_CODE (*node) != RECORD_TYPE) | |
9659 | { | |
9660 | error ("%qE attribute is only valid on %<struct%> type", name); | |
9661 | *no_add_attrs = true; | |
9662 | } | |
9663 | return NULL_TREE; | |
9664 | } | |
9665 | ||
dbf6c367 | 9666 | \f |
774e9d58 | 9667 | /* Check for valid arguments being passed to a function with FNTYPE. |
9668 | There are NARGS arguments in the array ARGARRAY. */ | |
dbf6c367 | 9669 | void |
774e9d58 | 9670 | check_function_arguments (const_tree fntype, int nargs, tree *argarray) |
dbf6c367 | 9671 | { |
9672 | /* Check for null being passed in a pointer argument that must be | |
9673 | non-null. We also need to do this if format checking is enabled. */ | |
9674 | ||
9675 | if (warn_nonnull) | |
774e9d58 | 9676 | check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
dbf6c367 | 9677 | |
9678 | /* Check for errors in format strings. */ | |
9679 | ||
068bea1e | 9680 | if (warn_format || warn_suggest_attribute_format) |
774e9d58 | 9681 | check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
95c90e04 | 9682 | |
9683 | if (warn_format) | |
774e9d58 | 9684 | check_function_sentinel (fntype, nargs, argarray); |
dbf6c367 | 9685 | } |
9686 | ||
9687 | /* Generic argument checking recursion routine. PARAM is the argument to | |
9688 | be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked | |
9689 | once the argument is resolved. CTX is context for the callback. */ | |
9690 | void | |
1cae46be | 9691 | check_function_arguments_recurse (void (*callback) |
9692 | (void *, tree, unsigned HOST_WIDE_INT), | |
9693 | void *ctx, tree param, | |
9694 | unsigned HOST_WIDE_INT param_num) | |
dbf6c367 | 9695 | { |
72dd6141 | 9696 | if (CONVERT_EXPR_P (param) |
c44afe23 | 9697 | && (TYPE_PRECISION (TREE_TYPE (param)) |
9698 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0))))) | |
dbf6c367 | 9699 | { |
9700 | /* Strip coercion. */ | |
9701 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 9702 | TREE_OPERAND (param, 0), param_num); |
dbf6c367 | 9703 | return; |
9704 | } | |
9705 | ||
9706 | if (TREE_CODE (param) == CALL_EXPR) | |
9707 | { | |
c2f47e15 | 9708 | tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param))); |
dbf6c367 | 9709 | tree attrs; |
9710 | bool found_format_arg = false; | |
9711 | ||
9712 | /* See if this is a call to a known internationalization function | |
9713 | that modifies a format arg. Such a function may have multiple | |
9714 | format_arg attributes (for example, ngettext). */ | |
9715 | ||
9716 | for (attrs = TYPE_ATTRIBUTES (type); | |
9717 | attrs; | |
9718 | attrs = TREE_CHAIN (attrs)) | |
9719 | if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs))) | |
9720 | { | |
c2f47e15 | 9721 | tree inner_arg; |
dbf6c367 | 9722 | tree format_num_expr; |
9723 | int format_num; | |
9724 | int i; | |
c2f47e15 | 9725 | call_expr_arg_iterator iter; |
dbf6c367 | 9726 | |
9727 | /* Extract the argument number, which was previously checked | |
9728 | to be valid. */ | |
9729 | format_num_expr = TREE_VALUE (TREE_VALUE (attrs)); | |
ddb1be65 | 9730 | |
e913b5cd | 9731 | format_num = tree_to_uhwi (format_num_expr); |
dbf6c367 | 9732 | |
c2f47e15 | 9733 | for (inner_arg = first_call_expr_arg (param, &iter), i = 1; |
9734 | inner_arg != 0; | |
9735 | inner_arg = next_call_expr_arg (&iter), i++) | |
dbf6c367 | 9736 | if (i == format_num) |
9737 | { | |
9738 | check_function_arguments_recurse (callback, ctx, | |
c2f47e15 | 9739 | inner_arg, param_num); |
dbf6c367 | 9740 | found_format_arg = true; |
9741 | break; | |
9742 | } | |
9743 | } | |
9744 | ||
9745 | /* If we found a format_arg attribute and did a recursive check, | |
9746 | we are done with checking this argument. Otherwise, we continue | |
9747 | and this will be considered a non-literal. */ | |
9748 | if (found_format_arg) | |
9749 | return; | |
9750 | } | |
9751 | ||
9752 | if (TREE_CODE (param) == COND_EXPR) | |
9753 | { | |
9c691dbd | 9754 | tree cond = fold_for_warn (TREE_OPERAND (param, 0)); |
dbf6c367 | 9755 | /* Check both halves of the conditional expression. */ |
9c691dbd | 9756 | if (!integer_zerop (cond)) |
9757 | check_function_arguments_recurse (callback, ctx, | |
9758 | TREE_OPERAND (param, 1), param_num); | |
9759 | if (!integer_nonzerop (cond)) | |
9760 | check_function_arguments_recurse (callback, ctx, | |
9761 | TREE_OPERAND (param, 2), param_num); | |
dbf6c367 | 9762 | return; |
9763 | } | |
9764 | ||
9765 | (*callback) (ctx, param, param_num); | |
9766 | } | |
1f3233d1 | 9767 | |
60cce472 | 9768 | /* Checks for a builtin function FNDECL that the number of arguments |
9769 | NARGS against the required number REQUIRED and issues an error if | |
9770 | there is a mismatch. Returns true if the number of arguments is | |
9771 | correct, otherwise false. */ | |
d43cee80 | 9772 | |
9773 | static bool | |
60cce472 | 9774 | builtin_function_validate_nargs (tree fndecl, int nargs, int required) |
d43cee80 | 9775 | { |
9776 | if (nargs < required) | |
9777 | { | |
60cce472 | 9778 | error_at (input_location, |
9779 | "not enough arguments to function %qE", fndecl); | |
d43cee80 | 9780 | return false; |
9781 | } | |
9782 | else if (nargs > required) | |
9783 | { | |
60cce472 | 9784 | error_at (input_location, |
9785 | "too many arguments to function %qE", fndecl); | |
d43cee80 | 9786 | return false; |
9787 | } | |
9788 | return true; | |
9789 | } | |
9790 | ||
9791 | /* Verifies the NARGS arguments ARGS to the builtin function FNDECL. | |
9792 | Returns false if there was an error, otherwise true. */ | |
9793 | ||
9794 | bool | |
9795 | check_builtin_function_arguments (tree fndecl, int nargs, tree *args) | |
9796 | { | |
9797 | if (!DECL_BUILT_IN (fndecl) | |
9798 | || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) | |
9799 | return true; | |
9800 | ||
9801 | switch (DECL_FUNCTION_CODE (fndecl)) | |
9802 | { | |
9803 | case BUILT_IN_CONSTANT_P: | |
60cce472 | 9804 | return builtin_function_validate_nargs (fndecl, nargs, 1); |
d43cee80 | 9805 | |
9806 | case BUILT_IN_ISFINITE: | |
9807 | case BUILT_IN_ISINF: | |
c319d56a | 9808 | case BUILT_IN_ISINF_SIGN: |
d43cee80 | 9809 | case BUILT_IN_ISNAN: |
9810 | case BUILT_IN_ISNORMAL: | |
10902624 | 9811 | case BUILT_IN_SIGNBIT: |
60cce472 | 9812 | if (builtin_function_validate_nargs (fndecl, nargs, 1)) |
d43cee80 | 9813 | { |
9814 | if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE) | |
9815 | { | |
9816 | error ("non-floating-point argument in call to " | |
9817 | "function %qE", fndecl); | |
9818 | return false; | |
9819 | } | |
9820 | return true; | |
9821 | } | |
9822 | return false; | |
9823 | ||
9824 | case BUILT_IN_ISGREATER: | |
9825 | case BUILT_IN_ISGREATEREQUAL: | |
9826 | case BUILT_IN_ISLESS: | |
9827 | case BUILT_IN_ISLESSEQUAL: | |
9828 | case BUILT_IN_ISLESSGREATER: | |
9829 | case BUILT_IN_ISUNORDERED: | |
60cce472 | 9830 | if (builtin_function_validate_nargs (fndecl, nargs, 2)) |
d43cee80 | 9831 | { |
9832 | enum tree_code code0, code1; | |
9833 | code0 = TREE_CODE (TREE_TYPE (args[0])); | |
9834 | code1 = TREE_CODE (TREE_TYPE (args[1])); | |
9835 | if (!((code0 == REAL_TYPE && code1 == REAL_TYPE) | |
9836 | || (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
9837 | || (code0 == INTEGER_TYPE && code1 == REAL_TYPE))) | |
9838 | { | |
9839 | error ("non-floating-point arguments in call to " | |
9840 | "function %qE", fndecl); | |
9841 | return false; | |
9842 | } | |
9843 | return true; | |
9844 | } | |
9845 | return false; | |
9846 | ||
19fbe3a4 | 9847 | case BUILT_IN_FPCLASSIFY: |
60cce472 | 9848 | if (builtin_function_validate_nargs (fndecl, nargs, 6)) |
19fbe3a4 | 9849 | { |
9850 | unsigned i; | |
48e1416a | 9851 | |
19fbe3a4 | 9852 | for (i=0; i<5; i++) |
9853 | if (TREE_CODE (args[i]) != INTEGER_CST) | |
9854 | { | |
9855 | error ("non-const integer argument %u in call to function %qE", | |
9856 | i+1, fndecl); | |
9857 | return false; | |
9858 | } | |
9859 | ||
9860 | if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE) | |
9861 | { | |
9862 | error ("non-floating-point argument in call to function %qE", | |
9863 | fndecl); | |
9864 | return false; | |
9865 | } | |
9866 | return true; | |
9867 | } | |
9868 | return false; | |
9869 | ||
fca0886c | 9870 | case BUILT_IN_ASSUME_ALIGNED: |
9871 | if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2))) | |
9872 | { | |
9873 | if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE) | |
9874 | { | |
9875 | error ("non-integer argument 3 in call to function %qE", fndecl); | |
9876 | return false; | |
9877 | } | |
9878 | return true; | |
9879 | } | |
9880 | return false; | |
9881 | ||
0c93c8a9 | 9882 | case BUILT_IN_ADD_OVERFLOW: |
9883 | case BUILT_IN_SUB_OVERFLOW: | |
9884 | case BUILT_IN_MUL_OVERFLOW: | |
9885 | if (builtin_function_validate_nargs (fndecl, nargs, 3)) | |
9886 | { | |
9887 | unsigned i; | |
9888 | for (i = 0; i < 2; i++) | |
9889 | if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i]))) | |
9890 | { | |
9891 | error ("argument %u in call to function %qE does not have " | |
9892 | "integral type", i + 1, fndecl); | |
9893 | return false; | |
9894 | } | |
9895 | if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE | |
9896 | || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE) | |
9897 | { | |
9898 | error ("argument 3 in call to function %qE does not have " | |
9899 | "pointer to integer type", fndecl); | |
9900 | return false; | |
9901 | } | |
9902 | return true; | |
9903 | } | |
9904 | return false; | |
9905 | ||
d43cee80 | 9906 | default: |
9907 | return true; | |
9908 | } | |
9909 | } | |
9910 | ||
860251be | 9911 | /* Function to help qsort sort FIELD_DECLs by name order. */ |
9912 | ||
9913 | int | |
9914 | field_decl_cmp (const void *x_p, const void *y_p) | |
9915 | { | |
4fd61bc6 | 9916 | const tree *const x = (const tree *const) x_p; |
9917 | const tree *const y = (const tree *const) y_p; | |
9918 | ||
860251be | 9919 | if (DECL_NAME (*x) == DECL_NAME (*y)) |
9920 | /* A nontype is "greater" than a type. */ | |
9921 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
9922 | if (DECL_NAME (*x) == NULL_TREE) | |
9923 | return -1; | |
9924 | if (DECL_NAME (*y) == NULL_TREE) | |
9925 | return 1; | |
9926 | if (DECL_NAME (*x) < DECL_NAME (*y)) | |
9927 | return -1; | |
9928 | return 1; | |
9929 | } | |
9930 | ||
9931 | static struct { | |
9932 | gt_pointer_operator new_value; | |
9933 | void *cookie; | |
9934 | } resort_data; | |
9935 | ||
9936 | /* This routine compares two fields like field_decl_cmp but using the | |
9937 | pointer operator in resort_data. */ | |
9938 | ||
9939 | static int | |
9940 | resort_field_decl_cmp (const void *x_p, const void *y_p) | |
9941 | { | |
4fd61bc6 | 9942 | const tree *const x = (const tree *const) x_p; |
9943 | const tree *const y = (const tree *const) y_p; | |
860251be | 9944 | |
9945 | if (DECL_NAME (*x) == DECL_NAME (*y)) | |
9946 | /* A nontype is "greater" than a type. */ | |
9947 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
9948 | if (DECL_NAME (*x) == NULL_TREE) | |
9949 | return -1; | |
9950 | if (DECL_NAME (*y) == NULL_TREE) | |
9951 | return 1; | |
9952 | { | |
9953 | tree d1 = DECL_NAME (*x); | |
9954 | tree d2 = DECL_NAME (*y); | |
9955 | resort_data.new_value (&d1, resort_data.cookie); | |
9956 | resort_data.new_value (&d2, resort_data.cookie); | |
9957 | if (d1 < d2) | |
9958 | return -1; | |
9959 | } | |
9960 | return 1; | |
9961 | } | |
9962 | ||
9963 | /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ | |
9964 | ||
9965 | void | |
9966 | resort_sorted_fields (void *obj, | |
9a03a746 | 9967 | void * ARG_UNUSED (orig_obj), |
4ee9c684 | 9968 | gt_pointer_operator new_value, |
9969 | void *cookie) | |
860251be | 9970 | { |
9a03a746 | 9971 | struct sorted_fields_type *sf = (struct sorted_fields_type *) obj; |
860251be | 9972 | resort_data.new_value = new_value; |
9973 | resort_data.cookie = cookie; | |
9974 | qsort (&sf->elts[0], sf->len, sizeof (tree), | |
4ee9c684 | 9975 | resort_field_decl_cmp); |
860251be | 9976 | } |
9977 | ||
209c9752 | 9978 | /* Subroutine of c_parse_error. |
9979 | Return the result of concatenating LHS and RHS. RHS is really | |
9980 | a string literal, its first character is indicated by RHS_START and | |
cfee01e3 | 9981 | RHS_SIZE is its length (including the terminating NUL character). |
209c9752 | 9982 | |
9983 | The caller is responsible for deleting the returned pointer. */ | |
9984 | ||
9985 | static char * | |
9986 | catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) | |
9987 | { | |
9988 | const int lhs_size = strlen (lhs); | |
9989 | char *result = XNEWVEC (char, lhs_size + rhs_size); | |
9990 | strncpy (result, lhs, lhs_size); | |
9991 | strncpy (result + lhs_size, rhs_start, rhs_size); | |
9992 | return result; | |
9993 | } | |
9994 | ||
380c6697 | 9995 | /* Issue the error given by GMSGID, indicating that it occurred before |
92b128ed | 9996 | TOKEN, which had the associated VALUE. */ |
9997 | ||
9998 | void | |
48e1416a | 9999 | c_parse_error (const char *gmsgid, enum cpp_ttype token_type, |
ba99525e | 10000 | tree value, unsigned char token_flags) |
92b128ed | 10001 | { |
209c9752 | 10002 | #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2)) |
10003 | ||
10004 | char *message = NULL; | |
92b128ed | 10005 | |
ba99525e | 10006 | if (token_type == CPP_EOF) |
380c6697 | 10007 | message = catenate_messages (gmsgid, " at end of input"); |
48e1416a | 10008 | else if (token_type == CPP_CHAR |
10009 | || token_type == CPP_WCHAR | |
ba99525e | 10010 | || token_type == CPP_CHAR16 |
30b1ba42 | 10011 | || token_type == CPP_CHAR32 |
10012 | || token_type == CPP_UTF8CHAR) | |
92b128ed | 10013 | { |
f9ae6f95 | 10014 | unsigned int val = TREE_INT_CST_LOW (value); |
924bbf02 | 10015 | const char *prefix; |
10016 | ||
ba99525e | 10017 | switch (token_type) |
924bbf02 | 10018 | { |
10019 | default: | |
10020 | prefix = ""; | |
10021 | break; | |
10022 | case CPP_WCHAR: | |
10023 | prefix = "L"; | |
10024 | break; | |
10025 | case CPP_CHAR16: | |
10026 | prefix = "u"; | |
10027 | break; | |
10028 | case CPP_CHAR32: | |
10029 | prefix = "U"; | |
10030 | break; | |
30b1ba42 | 10031 | case CPP_UTF8CHAR: |
10032 | prefix = "u8"; | |
10033 | break; | |
924bbf02 | 10034 | } |
10035 | ||
92b128ed | 10036 | if (val <= UCHAR_MAX && ISGRAPH (val)) |
a0c938f0 | 10037 | message = catenate_messages (gmsgid, " before %s'%c'"); |
92b128ed | 10038 | else |
a0c938f0 | 10039 | message = catenate_messages (gmsgid, " before %s'\\x%x'"); |
209c9752 | 10040 | |
924bbf02 | 10041 | error (message, prefix, val); |
209c9752 | 10042 | free (message); |
10043 | message = NULL; | |
92b128ed | 10044 | } |
1898176c | 10045 | else if (token_type == CPP_CHAR_USERDEF |
10046 | || token_type == CPP_WCHAR_USERDEF | |
10047 | || token_type == CPP_CHAR16_USERDEF | |
30b1ba42 | 10048 | || token_type == CPP_CHAR32_USERDEF |
10049 | || token_type == CPP_UTF8CHAR_USERDEF) | |
1898176c | 10050 | message = catenate_messages (gmsgid, |
10051 | " before user-defined character literal"); | |
10052 | else if (token_type == CPP_STRING_USERDEF | |
10053 | || token_type == CPP_WSTRING_USERDEF | |
10054 | || token_type == CPP_STRING16_USERDEF | |
10055 | || token_type == CPP_STRING32_USERDEF | |
10056 | || token_type == CPP_UTF8STRING_USERDEF) | |
10057 | message = catenate_messages (gmsgid, " before user-defined string literal"); | |
48e1416a | 10058 | else if (token_type == CPP_STRING |
10059 | || token_type == CPP_WSTRING | |
ba99525e | 10060 | || token_type == CPP_STRING16 |
538ba11a | 10061 | || token_type == CPP_STRING32 |
10062 | || token_type == CPP_UTF8STRING) | |
380c6697 | 10063 | message = catenate_messages (gmsgid, " before string constant"); |
ba99525e | 10064 | else if (token_type == CPP_NUMBER) |
380c6697 | 10065 | message = catenate_messages (gmsgid, " before numeric constant"); |
ba99525e | 10066 | else if (token_type == CPP_NAME) |
209c9752 | 10067 | { |
380c6697 | 10068 | message = catenate_messages (gmsgid, " before %qE"); |
782858b8 | 10069 | error (message, value); |
209c9752 | 10070 | free (message); |
10071 | message = NULL; | |
10072 | } | |
ba99525e | 10073 | else if (token_type == CPP_PRAGMA) |
b75b98aa | 10074 | message = catenate_messages (gmsgid, " before %<#pragma%>"); |
ba99525e | 10075 | else if (token_type == CPP_PRAGMA_EOL) |
b75b98aa | 10076 | message = catenate_messages (gmsgid, " before end of line"); |
07b8f133 | 10077 | else if (token_type == CPP_DECLTYPE) |
10078 | message = catenate_messages (gmsgid, " before %<decltype%>"); | |
ba99525e | 10079 | else if (token_type < N_TTYPES) |
209c9752 | 10080 | { |
380c6697 | 10081 | message = catenate_messages (gmsgid, " before %qs token"); |
ba99525e | 10082 | error (message, cpp_type2name (token_type, token_flags)); |
209c9752 | 10083 | free (message); |
10084 | message = NULL; | |
10085 | } | |
92b128ed | 10086 | else |
380c6697 | 10087 | error (gmsgid); |
209c9752 | 10088 | |
10089 | if (message) | |
10090 | { | |
10091 | error (message); | |
10092 | free (message); | |
10093 | } | |
a0c938f0 | 10094 | #undef catenate_messages |
92b128ed | 10095 | } |
10096 | ||
3a79f5da | 10097 | /* Return the gcc option code associated with the reason for a cpp |
10098 | message, or 0 if none. */ | |
10099 | ||
10100 | static int | |
10101 | c_option_controlling_cpp_error (int reason) | |
10102 | { | |
7ff8db31 | 10103 | const struct cpp_reason_option_codes_t *entry; |
3a79f5da | 10104 | |
7ff8db31 | 10105 | for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++) |
3a79f5da | 10106 | { |
10107 | if (entry->reason == reason) | |
10108 | return entry->option_code; | |
10109 | } | |
10110 | return 0; | |
10111 | } | |
10112 | ||
7f5f3953 | 10113 | /* Callback from cpp_error for PFILE to print diagnostics from the |
3a79f5da | 10114 | preprocessor. The diagnostic is of type LEVEL, with REASON set |
10115 | to the reason code if LEVEL is represents a warning, at location | |
f0479000 | 10116 | RICHLOC unless this is after lexing and the compiler's location |
10117 | should be used instead; MSG is the translated message and AP | |
7f5f3953 | 10118 | the arguments. Returns true if a diagnostic was emitted, false |
10119 | otherwise. */ | |
10120 | ||
10121 | bool | |
3a79f5da | 10122 | c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason, |
f0479000 | 10123 | rich_location *richloc, |
7f5f3953 | 10124 | const char *msg, va_list *ap) |
10125 | { | |
10126 | diagnostic_info diagnostic; | |
10127 | diagnostic_t dlevel; | |
5ae82d58 | 10128 | bool save_warn_system_headers = global_dc->dc_warn_system_headers; |
7f5f3953 | 10129 | bool ret; |
10130 | ||
10131 | switch (level) | |
10132 | { | |
10133 | case CPP_DL_WARNING_SYSHDR: | |
10134 | if (flag_no_output) | |
10135 | return false; | |
5ae82d58 | 10136 | global_dc->dc_warn_system_headers = 1; |
7f5f3953 | 10137 | /* Fall through. */ |
10138 | case CPP_DL_WARNING: | |
10139 | if (flag_no_output) | |
10140 | return false; | |
10141 | dlevel = DK_WARNING; | |
10142 | break; | |
10143 | case CPP_DL_PEDWARN: | |
10144 | if (flag_no_output && !flag_pedantic_errors) | |
10145 | return false; | |
10146 | dlevel = DK_PEDWARN; | |
10147 | break; | |
10148 | case CPP_DL_ERROR: | |
10149 | dlevel = DK_ERROR; | |
10150 | break; | |
10151 | case CPP_DL_ICE: | |
10152 | dlevel = DK_ICE; | |
10153 | break; | |
10154 | case CPP_DL_NOTE: | |
10155 | dlevel = DK_NOTE; | |
10156 | break; | |
ff903809 | 10157 | case CPP_DL_FATAL: |
10158 | dlevel = DK_FATAL; | |
10159 | break; | |
7f5f3953 | 10160 | default: |
10161 | gcc_unreachable (); | |
10162 | } | |
10163 | if (done_lexing) | |
d0f713f4 | 10164 | richloc->set_range (line_table, 0, input_location, true); |
7f5f3953 | 10165 | diagnostic_set_info_translated (&diagnostic, msg, ap, |
f0479000 | 10166 | richloc, dlevel); |
3a79f5da | 10167 | diagnostic_override_option_index (&diagnostic, |
10168 | c_option_controlling_cpp_error (reason)); | |
7f5f3953 | 10169 | ret = report_diagnostic (&diagnostic); |
10170 | if (level == CPP_DL_WARNING_SYSHDR) | |
5ae82d58 | 10171 | global_dc->dc_warn_system_headers = save_warn_system_headers; |
7f5f3953 | 10172 | return ret; |
10173 | } | |
10174 | ||
624d37a6 | 10175 | /* Convert a character from the host to the target execution character |
10176 | set. cpplib handles this, mostly. */ | |
10177 | ||
10178 | HOST_WIDE_INT | |
10179 | c_common_to_target_charset (HOST_WIDE_INT c) | |
10180 | { | |
10181 | /* Character constants in GCC proper are sign-extended under -fsigned-char, | |
10182 | zero-extended under -fno-signed-char. cpplib insists that characters | |
10183 | and character constants are always unsigned. Hence we must convert | |
10184 | back and forth. */ | |
10185 | cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1); | |
10186 | ||
10187 | uc = cpp_host_to_exec_charset (parse_in, uc); | |
10188 | ||
10189 | if (flag_signed_char) | |
10190 | return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE) | |
10191 | >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE); | |
10192 | else | |
10193 | return uc; | |
10194 | } | |
10195 | ||
7549df0d | 10196 | /* Fold an offsetof-like expression. EXPR is a nested sequence of component |
10197 | references with an INDIRECT_REF of a constant at the bottom; much like the | |
10198 | traditional rendering of offsetof as a macro. Return the folded result. */ | |
af28855b | 10199 | |
7549df0d | 10200 | tree |
aeaccb75 | 10201 | fold_offsetof_1 (tree expr, enum tree_code ctx) |
af28855b | 10202 | { |
af28855b | 10203 | tree base, off, t; |
aeaccb75 | 10204 | tree_code code = TREE_CODE (expr); |
10205 | switch (code) | |
af28855b | 10206 | { |
10207 | case ERROR_MARK: | |
10208 | return expr; | |
10209 | ||
6b11d2e3 | 10210 | case VAR_DECL: |
10211 | error ("cannot apply %<offsetof%> to static data member %qD", expr); | |
10212 | return error_mark_node; | |
10213 | ||
d897f7c2 | 10214 | case CALL_EXPR: |
cf1a89a3 | 10215 | case TARGET_EXPR: |
d897f7c2 | 10216 | error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded"); |
10217 | return error_mark_node; | |
10218 | ||
d897f7c2 | 10219 | case NOP_EXPR: |
10220 | case INDIRECT_REF: | |
7549df0d | 10221 | if (!TREE_CONSTANT (TREE_OPERAND (expr, 0))) |
64ed018c | 10222 | { |
10223 | error ("cannot apply %<offsetof%> to a non constant address"); | |
10224 | return error_mark_node; | |
10225 | } | |
7549df0d | 10226 | return TREE_OPERAND (expr, 0); |
d897f7c2 | 10227 | |
af28855b | 10228 | case COMPONENT_REF: |
aeaccb75 | 10229 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code); |
af28855b | 10230 | if (base == error_mark_node) |
10231 | return base; | |
10232 | ||
10233 | t = TREE_OPERAND (expr, 1); | |
10234 | if (DECL_C_BIT_FIELD (t)) | |
10235 | { | |
10236 | error ("attempt to take address of bit-field structure " | |
782858b8 | 10237 | "member %qD", t); |
af28855b | 10238 | return error_mark_node; |
10239 | } | |
389dd41b | 10240 | off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t), |
e913b5cd | 10241 | size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t)) |
389dd41b | 10242 | / BITS_PER_UNIT)); |
af28855b | 10243 | break; |
10244 | ||
10245 | case ARRAY_REF: | |
aeaccb75 | 10246 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code); |
af28855b | 10247 | if (base == error_mark_node) |
10248 | return base; | |
10249 | ||
10250 | t = TREE_OPERAND (expr, 1); | |
64ed018c | 10251 | |
10252 | /* Check if the offset goes beyond the upper bound of the array. */ | |
7549df0d | 10253 | if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0) |
e0559d69 | 10254 | { |
10255 | tree upbound = array_ref_up_bound (expr); | |
10256 | if (upbound != NULL_TREE | |
10257 | && TREE_CODE (upbound) == INTEGER_CST | |
10258 | && !tree_int_cst_equal (upbound, | |
10259 | TYPE_MAX_VALUE (TREE_TYPE (upbound)))) | |
10260 | { | |
aeaccb75 | 10261 | if (ctx != ARRAY_REF && ctx != COMPONENT_REF) |
10262 | upbound = size_binop (PLUS_EXPR, upbound, | |
10263 | build_int_cst (TREE_TYPE (upbound), 1)); | |
e0559d69 | 10264 | if (tree_int_cst_lt (upbound, t)) |
10265 | { | |
10266 | tree v; | |
10267 | ||
10268 | for (v = TREE_OPERAND (expr, 0); | |
10269 | TREE_CODE (v) == COMPONENT_REF; | |
10270 | v = TREE_OPERAND (v, 0)) | |
10271 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) | |
10272 | == RECORD_TYPE) | |
10273 | { | |
1767a056 | 10274 | tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); |
10275 | for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) | |
e0559d69 | 10276 | if (TREE_CODE (fld_chain) == FIELD_DECL) |
10277 | break; | |
10278 | ||
10279 | if (fld_chain) | |
10280 | break; | |
10281 | } | |
10282 | /* Don't warn if the array might be considered a poor | |
10283 | man's flexible array member with a very permissive | |
10284 | definition thereof. */ | |
10285 | if (TREE_CODE (v) == ARRAY_REF | |
10286 | || TREE_CODE (v) == COMPONENT_REF) | |
10287 | warning (OPT_Warray_bounds, | |
10288 | "index %E denotes an offset " | |
10289 | "greater than size of %qT", | |
10290 | t, TREE_TYPE (TREE_OPERAND (expr, 0))); | |
10291 | } | |
10292 | } | |
10293 | } | |
7549df0d | 10294 | |
10295 | t = convert (sizetype, t); | |
10296 | off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); | |
af28855b | 10297 | break; |
10298 | ||
ede90cc2 | 10299 | case COMPOUND_EXPR: |
10300 | /* Handle static members of volatile structs. */ | |
10301 | t = TREE_OPERAND (expr, 1); | |
f48c7f4a | 10302 | gcc_assert (VAR_P (t)); |
7549df0d | 10303 | return fold_offsetof_1 (t); |
ede90cc2 | 10304 | |
af28855b | 10305 | default: |
231bd014 | 10306 | gcc_unreachable (); |
af28855b | 10307 | } |
10308 | ||
7549df0d | 10309 | return fold_build_pointer_plus (base, off); |
af28855b | 10310 | } |
10311 | ||
7549df0d | 10312 | /* Likewise, but convert it to the return type of offsetof. */ |
10313 | ||
af28855b | 10314 | tree |
7549df0d | 10315 | fold_offsetof (tree expr) |
af28855b | 10316 | { |
7549df0d | 10317 | return convert (size_type_node, fold_offsetof_1 (expr)); |
af28855b | 10318 | } |
10319 | ||
b9bdfa0b | 10320 | /* Warn for A ?: C expressions (with B omitted) where A is a boolean |
10321 | expression, because B will always be true. */ | |
10322 | ||
10323 | void | |
10324 | warn_for_omitted_condop (location_t location, tree cond) | |
10325 | { | |
10326 | if (truth_value_p (TREE_CODE (cond))) | |
10327 | warning_at (location, OPT_Wparentheses, | |
10328 | "the omitted middle operand in ?: will always be %<true%>, " | |
10329 | "suggest explicit middle operand"); | |
10330 | } | |
10331 | ||
a1f90215 | 10332 | /* Give an error for storing into ARG, which is 'const'. USE indicates |
10333 | how ARG was being used. */ | |
10334 | ||
10335 | void | |
f2697631 | 10336 | readonly_error (location_t loc, tree arg, enum lvalue_use use) |
a1f90215 | 10337 | { |
10338 | gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement | |
10339 | || use == lv_asm); | |
10340 | /* Using this macro rather than (for example) arrays of messages | |
10341 | ensures that all the format strings are checked at compile | |
10342 | time. */ | |
10343 | #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ | |
10344 | : (use == lv_increment ? (I) \ | |
10345 | : (use == lv_decrement ? (D) : (AS)))) | |
10346 | if (TREE_CODE (arg) == COMPONENT_REF) | |
10347 | { | |
10348 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
f2697631 | 10349 | error_at (loc, READONLY_MSG (G_("assignment of member " |
10350 | "%qD in read-only object"), | |
10351 | G_("increment of member " | |
10352 | "%qD in read-only object"), | |
10353 | G_("decrement of member " | |
10354 | "%qD in read-only object"), | |
10355 | G_("member %qD in read-only object " | |
10356 | "used as %<asm%> output")), | |
10357 | TREE_OPERAND (arg, 1)); | |
a1f90215 | 10358 | else |
f2697631 | 10359 | error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"), |
10360 | G_("increment of read-only member %qD"), | |
10361 | G_("decrement of read-only member %qD"), | |
10362 | G_("read-only member %qD used as %<asm%> output")), | |
10363 | TREE_OPERAND (arg, 1)); | |
a1f90215 | 10364 | } |
f48c7f4a | 10365 | else if (VAR_P (arg)) |
f2697631 | 10366 | error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"), |
10367 | G_("increment of read-only variable %qD"), | |
10368 | G_("decrement of read-only variable %qD"), | |
10369 | G_("read-only variable %qD used as %<asm%> output")), | |
10370 | arg); | |
a1f90215 | 10371 | else if (TREE_CODE (arg) == PARM_DECL) |
f2697631 | 10372 | error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"), |
10373 | G_("increment of read-only parameter %qD"), | |
10374 | G_("decrement of read-only parameter %qD"), | |
10375 | G_("read-only parameter %qD use as %<asm%> output")), | |
10376 | arg); | |
a1f90215 | 10377 | else if (TREE_CODE (arg) == RESULT_DECL) |
10378 | { | |
10379 | gcc_assert (c_dialect_cxx ()); | |
f2697631 | 10380 | error_at (loc, READONLY_MSG (G_("assignment of " |
10381 | "read-only named return value %qD"), | |
10382 | G_("increment of " | |
10383 | "read-only named return value %qD"), | |
10384 | G_("decrement of " | |
10385 | "read-only named return value %qD"), | |
10386 | G_("read-only named return value %qD " | |
10387 | "used as %<asm%>output")), | |
10388 | arg); | |
a1f90215 | 10389 | } |
10390 | else if (TREE_CODE (arg) == FUNCTION_DECL) | |
f2697631 | 10391 | error_at (loc, READONLY_MSG (G_("assignment of function %qD"), |
10392 | G_("increment of function %qD"), | |
10393 | G_("decrement of function %qD"), | |
10394 | G_("function %qD used as %<asm%> output")), | |
10395 | arg); | |
a1f90215 | 10396 | else |
f2697631 | 10397 | error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"), |
10398 | G_("increment of read-only location %qE"), | |
10399 | G_("decrement of read-only location %qE"), | |
10400 | G_("read-only location %qE used as %<asm%> output")), | |
10401 | arg); | |
a1f90215 | 10402 | } |
10403 | ||
e35976b1 | 10404 | /* Print an error message for an invalid lvalue. USE says |
fdd84b77 | 10405 | how the lvalue is being used and so selects the error message. LOC |
10406 | is the location for the error. */ | |
ab6bb714 | 10407 | |
e35976b1 | 10408 | void |
fdd84b77 | 10409 | lvalue_error (location_t loc, enum lvalue_use use) |
ab6bb714 | 10410 | { |
e35976b1 | 10411 | switch (use) |
ab6bb714 | 10412 | { |
e35976b1 | 10413 | case lv_assign: |
fdd84b77 | 10414 | error_at (loc, "lvalue required as left operand of assignment"); |
e35976b1 | 10415 | break; |
10416 | case lv_increment: | |
fdd84b77 | 10417 | error_at (loc, "lvalue required as increment operand"); |
e35976b1 | 10418 | break; |
10419 | case lv_decrement: | |
fdd84b77 | 10420 | error_at (loc, "lvalue required as decrement operand"); |
e35976b1 | 10421 | break; |
10422 | case lv_addressof: | |
fdd84b77 | 10423 | error_at (loc, "lvalue required as unary %<&%> operand"); |
e35976b1 | 10424 | break; |
10425 | case lv_asm: | |
fdd84b77 | 10426 | error_at (loc, "lvalue required in asm statement"); |
e35976b1 | 10427 | break; |
10428 | default: | |
10429 | gcc_unreachable (); | |
ab6bb714 | 10430 | } |
ab6bb714 | 10431 | } |
b1bbc8e5 | 10432 | |
10433 | /* Print an error message for an invalid indirection of type TYPE. | |
10434 | ERRSTRING is the name of the operator for the indirection. */ | |
10435 | ||
10436 | void | |
10437 | invalid_indirection_error (location_t loc, tree type, ref_operator errstring) | |
10438 | { | |
10439 | switch (errstring) | |
10440 | { | |
10441 | case RO_NULL: | |
10442 | gcc_assert (c_dialect_cxx ()); | |
10443 | error_at (loc, "invalid type argument (have %qT)", type); | |
10444 | break; | |
10445 | case RO_ARRAY_INDEXING: | |
10446 | error_at (loc, | |
10447 | "invalid type argument of array indexing (have %qT)", | |
10448 | type); | |
10449 | break; | |
10450 | case RO_UNARY_STAR: | |
10451 | error_at (loc, | |
10452 | "invalid type argument of unary %<*%> (have %qT)", | |
10453 | type); | |
10454 | break; | |
10455 | case RO_ARROW: | |
10456 | error_at (loc, | |
10457 | "invalid type argument of %<->%> (have %qT)", | |
10458 | type); | |
10459 | break; | |
7354a89b | 10460 | case RO_ARROW_STAR: |
10461 | error_at (loc, | |
10462 | "invalid type argument of %<->*%> (have %qT)", | |
10463 | type); | |
10464 | break; | |
b1bbc8e5 | 10465 | case RO_IMPLICIT_CONVERSION: |
10466 | error_at (loc, | |
10467 | "invalid type argument of implicit conversion (have %qT)", | |
10468 | type); | |
10469 | break; | |
10470 | default: | |
10471 | gcc_unreachable (); | |
10472 | } | |
10473 | } | |
c271bdb2 | 10474 | \f |
10475 | /* *PTYPE is an incomplete array. Complete it with a domain based on | |
10476 | INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT | |
10477 | is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
10478 | 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ | |
10479 | ||
10480 | int | |
10481 | complete_array_type (tree *ptype, tree initial_value, bool do_default) | |
10482 | { | |
10483 | tree maxindex, type, main_type, elt, unqual_elt; | |
10484 | int failure = 0, quals; | |
6753bca0 | 10485 | hashval_t hashcode = 0; |
f5298614 | 10486 | bool overflow_p = false; |
c271bdb2 | 10487 | |
10488 | maxindex = size_zero_node; | |
10489 | if (initial_value) | |
10490 | { | |
10491 | if (TREE_CODE (initial_value) == STRING_CST) | |
10492 | { | |
10493 | int eltsize | |
10494 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
10495 | maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1); | |
10496 | } | |
10497 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) | |
10498 | { | |
f1f41a6c | 10499 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value); |
c271bdb2 | 10500 | |
f1f41a6c | 10501 | if (vec_safe_is_empty (v)) |
c271bdb2 | 10502 | { |
10503 | if (pedantic) | |
10504 | failure = 3; | |
7542c3b4 | 10505 | maxindex = ssize_int (-1); |
c271bdb2 | 10506 | } |
10507 | else | |
10508 | { | |
10509 | tree curindex; | |
c75b4594 | 10510 | unsigned HOST_WIDE_INT cnt; |
10511 | constructor_elt *ce; | |
cee43f7e | 10512 | bool fold_p = false; |
c271bdb2 | 10513 | |
f1f41a6c | 10514 | if ((*v)[0].index) |
f5298614 | 10515 | maxindex = (*v)[0].index, fold_p = true; |
10516 | ||
c271bdb2 | 10517 | curindex = maxindex; |
10518 | ||
f1f41a6c | 10519 | for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++) |
c271bdb2 | 10520 | { |
cee43f7e | 10521 | bool curfold_p = false; |
c75b4594 | 10522 | if (ce->index) |
cee43f7e | 10523 | curindex = ce->index, curfold_p = true; |
c271bdb2 | 10524 | else |
cee43f7e | 10525 | { |
10526 | if (fold_p) | |
f5298614 | 10527 | { |
10528 | /* Since we treat size types now as ordinary | |
10529 | unsigned types, we need an explicit overflow | |
10530 | check. */ | |
10531 | tree orig = curindex; | |
10532 | curindex = fold_convert (sizetype, curindex); | |
10533 | overflow_p |= tree_int_cst_lt (curindex, orig); | |
10534 | } | |
389dd41b | 10535 | curindex = size_binop (PLUS_EXPR, curindex, |
10536 | size_one_node); | |
cee43f7e | 10537 | } |
c271bdb2 | 10538 | if (tree_int_cst_lt (maxindex, curindex)) |
cee43f7e | 10539 | maxindex = curindex, fold_p = curfold_p; |
c271bdb2 | 10540 | } |
f5298614 | 10541 | if (fold_p) |
10542 | { | |
10543 | tree orig = maxindex; | |
10544 | maxindex = fold_convert (sizetype, maxindex); | |
10545 | overflow_p |= tree_int_cst_lt (maxindex, orig); | |
10546 | } | |
c271bdb2 | 10547 | } |
10548 | } | |
10549 | else | |
10550 | { | |
10551 | /* Make an error message unless that happened already. */ | |
10552 | if (initial_value != error_mark_node) | |
10553 | failure = 1; | |
10554 | } | |
10555 | } | |
10556 | else | |
10557 | { | |
10558 | failure = 2; | |
10559 | if (!do_default) | |
10560 | return failure; | |
10561 | } | |
10562 | ||
10563 | type = *ptype; | |
10564 | elt = TREE_TYPE (type); | |
10565 | quals = TYPE_QUALS (strip_array_types (elt)); | |
10566 | if (quals == 0) | |
10567 | unqual_elt = elt; | |
10568 | else | |
6d5d708e | 10569 | unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals)); |
c271bdb2 | 10570 | |
10571 | /* Using build_distinct_type_copy and modifying things afterward instead | |
10572 | of using build_array_type to create a new type preserves all of the | |
10573 | TYPE_LANG_FLAG_? bits that the front end may have set. */ | |
10574 | main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); | |
10575 | TREE_TYPE (main_type) = unqual_elt; | |
783bb57e | 10576 | TYPE_DOMAIN (main_type) |
10577 | = build_range_type (TREE_TYPE (maxindex), | |
10578 | build_int_cst (TREE_TYPE (maxindex), 0), maxindex); | |
c271bdb2 | 10579 | layout_type (main_type); |
10580 | ||
6753bca0 | 10581 | /* Make sure we have the canonical MAIN_TYPE. */ |
10582 | hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode); | |
48e1416a | 10583 | hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)), |
6753bca0 | 10584 | hashcode); |
10585 | main_type = type_hash_canon (hashcode, main_type); | |
10586 | ||
796735dc | 10587 | /* Fix the canonical type. */ |
10588 | if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type)) | |
10589 | || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type))) | |
10590 | SET_TYPE_STRUCTURAL_EQUALITY (main_type); | |
10591 | else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type) | |
10592 | || (TYPE_CANONICAL (TYPE_DOMAIN (main_type)) | |
10593 | != TYPE_DOMAIN (main_type))) | |
48e1416a | 10594 | TYPE_CANONICAL (main_type) |
796735dc | 10595 | = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)), |
10596 | TYPE_CANONICAL (TYPE_DOMAIN (main_type))); | |
10597 | else | |
10598 | TYPE_CANONICAL (main_type) = main_type; | |
10599 | ||
c271bdb2 | 10600 | if (quals == 0) |
10601 | type = main_type; | |
10602 | else | |
10603 | type = c_build_qualified_type (main_type, quals); | |
10604 | ||
4f5b8f2a | 10605 | if (COMPLETE_TYPE_P (type) |
10606 | && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST | |
f5298614 | 10607 | && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))) |
4f5b8f2a | 10608 | { |
10609 | error ("size of array is too large"); | |
10610 | /* If we proceed with the array type as it is, we'll eventually | |
08f817b3 | 10611 | crash in tree_to_[su]hwi(). */ |
4f5b8f2a | 10612 | type = error_mark_node; |
10613 | } | |
10614 | ||
c271bdb2 | 10615 | *ptype = type; |
10616 | return failure; | |
10617 | } | |
ab6bb714 | 10618 | |
93426222 | 10619 | /* Like c_mark_addressable but don't check register qualifier. */ |
10620 | void | |
10621 | c_common_mark_addressable_vec (tree t) | |
10622 | { | |
10623 | while (handled_component_p (t)) | |
10624 | t = TREE_OPERAND (t, 0); | |
f48c7f4a | 10625 | if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL) |
93426222 | 10626 | return; |
10627 | TREE_ADDRESSABLE (t) = 1; | |
10628 | } | |
10629 | ||
10630 | ||
b6a5fc45 | 10631 | \f |
10632 | /* Used to help initialize the builtin-types.def table. When a type of | |
10633 | the correct size doesn't exist, use error_mark_node instead of NULL. | |
10634 | The later results in segfaults even when a decl using the type doesn't | |
10635 | get invoked. */ | |
10636 | ||
10637 | tree | |
10638 | builtin_type_for_size (int size, bool unsignedp) | |
10639 | { | |
a51edb4c | 10640 | tree type = c_common_type_for_size (size, unsignedp); |
b6a5fc45 | 10641 | return type ? type : error_mark_node; |
10642 | } | |
10643 | ||
10644 | /* A helper function for resolve_overloaded_builtin in resolving the | |
10645 | overloaded __sync_ builtins. Returns a positive power of 2 if the | |
10646 | first operand of PARAMS is a pointer to a supported data type. | |
10647 | Returns 0 if an error is encountered. */ | |
10648 | ||
10649 | static int | |
f1f41a6c | 10650 | sync_resolve_size (tree function, vec<tree, va_gc> *params) |
b6a5fc45 | 10651 | { |
10652 | tree type; | |
10653 | int size; | |
10654 | ||
f1f41a6c | 10655 | if (!params) |
b6a5fc45 | 10656 | { |
10657 | error ("too few arguments to function %qE", function); | |
10658 | return 0; | |
10659 | } | |
10660 | ||
f1f41a6c | 10661 | type = TREE_TYPE ((*params)[0]); |
0d284870 | 10662 | if (TREE_CODE (type) == ARRAY_TYPE) |
10663 | { | |
10664 | /* Force array-to-pointer decay for C++. */ | |
10665 | gcc_assert (c_dialect_cxx()); | |
10666 | (*params)[0] = default_conversion ((*params)[0]); | |
10667 | type = TREE_TYPE ((*params)[0]); | |
10668 | } | |
b6a5fc45 | 10669 | if (TREE_CODE (type) != POINTER_TYPE) |
10670 | goto incompatible; | |
10671 | ||
10672 | type = TREE_TYPE (type); | |
10673 | if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) | |
10674 | goto incompatible; | |
10675 | ||
e913b5cd | 10676 | size = tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
27213ba3 | 10677 | if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16) |
b6a5fc45 | 10678 | return size; |
10679 | ||
10680 | incompatible: | |
10681 | error ("incompatible type for argument %d of %qE", 1, function); | |
10682 | return 0; | |
10683 | } | |
10684 | ||
a0c938f0 | 10685 | /* A helper function for resolve_overloaded_builtin. Adds casts to |
b6a5fc45 | 10686 | PARAMS to make arguments match up with those of FUNCTION. Drops |
10687 | the variadic arguments at the end. Returns false if some error | |
10688 | was encountered; true on success. */ | |
10689 | ||
10690 | static bool | |
1cd6e20d | 10691 | sync_resolve_params (location_t loc, tree orig_function, tree function, |
f1f41a6c | 10692 | vec<tree, va_gc> *params, bool orig_format) |
b6a5fc45 | 10693 | { |
d0af78c5 | 10694 | function_args_iterator iter; |
b6a5fc45 | 10695 | tree ptype; |
b9c74b4d | 10696 | unsigned int parmnum; |
b6a5fc45 | 10697 | |
d0af78c5 | 10698 | function_args_iter_init (&iter, TREE_TYPE (function)); |
b6a5fc45 | 10699 | /* We've declared the implementation functions to use "volatile void *" |
10700 | as the pointer parameter, so we shouldn't get any complaints from the | |
10701 | call to check_function_arguments what ever type the user used. */ | |
d0af78c5 | 10702 | function_args_iter_next (&iter); |
f1f41a6c | 10703 | ptype = TREE_TYPE (TREE_TYPE ((*params)[0])); |
b560fabd | 10704 | ptype = TYPE_MAIN_VARIANT (ptype); |
b6a5fc45 | 10705 | |
10706 | /* For the rest of the values, we need to cast these to FTYPE, so that we | |
10707 | don't get warnings for passing pointer types, etc. */ | |
b9c74b4d | 10708 | parmnum = 0; |
d0af78c5 | 10709 | while (1) |
b6a5fc45 | 10710 | { |
d0af78c5 | 10711 | tree val, arg_type; |
10712 | ||
10713 | arg_type = function_args_iter_cond (&iter); | |
10714 | /* XXX void_type_node belies the abstraction. */ | |
10715 | if (arg_type == void_type_node) | |
10716 | break; | |
b6a5fc45 | 10717 | |
b9c74b4d | 10718 | ++parmnum; |
f1f41a6c | 10719 | if (params->length () <= parmnum) |
b6a5fc45 | 10720 | { |
1cd6e20d | 10721 | error_at (loc, "too few arguments to function %qE", orig_function); |
b6a5fc45 | 10722 | return false; |
10723 | } | |
10724 | ||
0f6a7cb7 | 10725 | /* Only convert parameters if arg_type is unsigned integer type with |
10726 | new format sync routines, i.e. don't attempt to convert pointer | |
10727 | arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n), | |
10728 | bool arguments (e.g. WEAK argument) or signed int arguments (memmodel | |
10729 | kinds). */ | |
10730 | if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type)) | |
1cd6e20d | 10731 | { |
10732 | /* Ideally for the first conversion we'd use convert_for_assignment | |
10733 | so that we get warnings for anything that doesn't match the pointer | |
10734 | type. This isn't portable across the C and C++ front ends atm. */ | |
f1f41a6c | 10735 | val = (*params)[parmnum]; |
1cd6e20d | 10736 | val = convert (ptype, val); |
10737 | val = convert (arg_type, val); | |
f1f41a6c | 10738 | (*params)[parmnum] = val; |
1cd6e20d | 10739 | } |
b6a5fc45 | 10740 | |
d0af78c5 | 10741 | function_args_iter_next (&iter); |
b6a5fc45 | 10742 | } |
10743 | ||
1cd6e20d | 10744 | /* __atomic routines are not variadic. */ |
f1f41a6c | 10745 | if (!orig_format && params->length () != parmnum + 1) |
1cd6e20d | 10746 | { |
10747 | error_at (loc, "too many arguments to function %qE", orig_function); | |
10748 | return false; | |
10749 | } | |
10750 | ||
b6a5fc45 | 10751 | /* The definition of these primitives is variadic, with the remaining |
10752 | being "an optional list of variables protected by the memory barrier". | |
10753 | No clue what that's supposed to mean, precisely, but we consider all | |
10754 | call-clobbered variables to be protected so we're safe. */ | |
f1f41a6c | 10755 | params->truncate (parmnum + 1); |
b6a5fc45 | 10756 | |
10757 | return true; | |
10758 | } | |
10759 | ||
a0c938f0 | 10760 | /* A helper function for resolve_overloaded_builtin. Adds a cast to |
b6a5fc45 | 10761 | RESULT to make it match the type of the first pointer argument in |
10762 | PARAMS. */ | |
10763 | ||
10764 | static tree | |
1cd6e20d | 10765 | sync_resolve_return (tree first_param, tree result, bool orig_format) |
b6a5fc45 | 10766 | { |
b9c74b4d | 10767 | tree ptype = TREE_TYPE (TREE_TYPE (first_param)); |
1cd6e20d | 10768 | tree rtype = TREE_TYPE (result); |
10080eac | 10769 | ptype = TYPE_MAIN_VARIANT (ptype); |
1cd6e20d | 10770 | |
10771 | /* New format doesn't require casting unless the types are the same size. */ | |
10772 | if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype))) | |
10773 | return convert (ptype, result); | |
10774 | else | |
10775 | return result; | |
10776 | } | |
10777 | ||
10778 | /* This function verifies the PARAMS to generic atomic FUNCTION. | |
10779 | It returns the size if all the parameters are the same size, otherwise | |
10780 | 0 is returned if the parameters are invalid. */ | |
10781 | ||
10782 | static int | |
f1f41a6c | 10783 | get_atomic_generic_size (location_t loc, tree function, |
10784 | vec<tree, va_gc> *params) | |
1cd6e20d | 10785 | { |
10786 | unsigned int n_param; | |
10787 | unsigned int n_model; | |
10788 | unsigned int x; | |
10789 | int size_0; | |
10790 | tree type_0; | |
10791 | ||
10792 | /* Determine the parameter makeup. */ | |
10793 | switch (DECL_FUNCTION_CODE (function)) | |
10794 | { | |
10795 | case BUILT_IN_ATOMIC_EXCHANGE: | |
10796 | n_param = 4; | |
10797 | n_model = 1; | |
10798 | break; | |
10799 | case BUILT_IN_ATOMIC_LOAD: | |
10800 | case BUILT_IN_ATOMIC_STORE: | |
10801 | n_param = 3; | |
10802 | n_model = 1; | |
10803 | break; | |
10804 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
10805 | n_param = 6; | |
10806 | n_model = 2; | |
10807 | break; | |
10808 | default: | |
1d581089 | 10809 | gcc_unreachable (); |
1cd6e20d | 10810 | } |
10811 | ||
f1f41a6c | 10812 | if (vec_safe_length (params) != n_param) |
1cd6e20d | 10813 | { |
10814 | error_at (loc, "incorrect number of arguments to function %qE", function); | |
10815 | return 0; | |
10816 | } | |
10817 | ||
10818 | /* Get type of first parameter, and determine its size. */ | |
f1f41a6c | 10819 | type_0 = TREE_TYPE ((*params)[0]); |
0d284870 | 10820 | if (TREE_CODE (type_0) == ARRAY_TYPE) |
10821 | { | |
10822 | /* Force array-to-pointer decay for C++. */ | |
10823 | gcc_assert (c_dialect_cxx()); | |
10824 | (*params)[0] = default_conversion ((*params)[0]); | |
10825 | type_0 = TREE_TYPE ((*params)[0]); | |
10826 | } | |
1d581089 | 10827 | if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0))) |
10828 | { | |
10829 | error_at (loc, "argument 1 of %qE must be a non-void pointer type", | |
10830 | function); | |
10831 | return 0; | |
10832 | } | |
10833 | ||
10834 | /* Types must be compile time constant sizes. */ | |
10835 | if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST) | |
1cd6e20d | 10836 | { |
1d581089 | 10837 | error_at (loc, |
10838 | "argument 1 of %qE must be a pointer to a constant size type", | |
10839 | function); | |
1cd6e20d | 10840 | return 0; |
10841 | } | |
1d581089 | 10842 | |
e913b5cd | 10843 | size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0))); |
1cd6e20d | 10844 | |
1d581089 | 10845 | /* Zero size objects are not allowed. */ |
10846 | if (size_0 == 0) | |
10847 | { | |
10848 | error_at (loc, | |
10849 | "argument 1 of %qE must be a pointer to a nonzero size object", | |
10850 | function); | |
10851 | return 0; | |
10852 | } | |
10853 | ||
1cd6e20d | 10854 | /* Check each other parameter is a pointer and the same size. */ |
10855 | for (x = 0; x < n_param - n_model; x++) | |
10856 | { | |
10857 | int size; | |
f1f41a6c | 10858 | tree type = TREE_TYPE ((*params)[x]); |
a04e8d62 | 10859 | /* __atomic_compare_exchange has a bool in the 4th position, skip it. */ |
1cd6e20d | 10860 | if (n_param == 6 && x == 3) |
10861 | continue; | |
10862 | if (!POINTER_TYPE_P (type)) | |
10863 | { | |
10864 | error_at (loc, "argument %d of %qE must be a pointer type", x + 1, | |
10865 | function); | |
10866 | return 0; | |
10867 | } | |
1f6be080 | 10868 | tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type)); |
10869 | size = type_size ? tree_to_uhwi (type_size) : 0; | |
1cd6e20d | 10870 | if (size != size_0) |
10871 | { | |
10872 | error_at (loc, "size mismatch in argument %d of %qE", x + 1, | |
10873 | function); | |
10874 | return 0; | |
10875 | } | |
10876 | } | |
10877 | ||
10878 | /* Check memory model parameters for validity. */ | |
10879 | for (x = n_param - n_model ; x < n_param; x++) | |
10880 | { | |
f1f41a6c | 10881 | tree p = (*params)[x]; |
1cd6e20d | 10882 | if (TREE_CODE (p) == INTEGER_CST) |
10883 | { | |
e913b5cd | 10884 | int i = tree_to_uhwi (p); |
a372f7ca | 10885 | if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST)) |
1cd6e20d | 10886 | { |
10887 | warning_at (loc, OPT_Winvalid_memory_model, | |
10888 | "invalid memory model argument %d of %qE", x + 1, | |
10889 | function); | |
1cd6e20d | 10890 | } |
10891 | } | |
10892 | else | |
10893 | if (!INTEGRAL_TYPE_P (TREE_TYPE (p))) | |
10894 | { | |
10895 | error_at (loc, "non-integer memory model argument %d of %qE", x + 1, | |
10896 | function); | |
10897 | return 0; | |
10898 | } | |
10899 | } | |
10900 | ||
10901 | return size_0; | |
10902 | } | |
10903 | ||
10904 | ||
10905 | /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N | |
10906 | at the beginning of the parameter list PARAMS representing the size of the | |
10907 | objects. This is to match the library ABI requirement. LOC is the location | |
10908 | of the function call. | |
10909 | The new function is returned if it needed rebuilding, otherwise NULL_TREE is | |
10910 | returned to allow the external call to be constructed. */ | |
10911 | ||
10912 | static tree | |
10913 | add_atomic_size_parameter (unsigned n, location_t loc, tree function, | |
f1f41a6c | 10914 | vec<tree, va_gc> *params) |
1cd6e20d | 10915 | { |
10916 | tree size_node; | |
10917 | ||
10918 | /* Insert a SIZE_T parameter as the first param. If there isn't | |
10919 | enough space, allocate a new vector and recursively re-build with that. */ | |
f1f41a6c | 10920 | if (!params->space (1)) |
1cd6e20d | 10921 | { |
10922 | unsigned int z, len; | |
f1f41a6c | 10923 | vec<tree, va_gc> *v; |
1cd6e20d | 10924 | tree f; |
10925 | ||
f1f41a6c | 10926 | len = params->length (); |
10927 | vec_alloc (v, len + 1); | |
5a672e62 | 10928 | v->quick_push (build_int_cst (size_type_node, n)); |
1cd6e20d | 10929 | for (z = 0; z < len; z++) |
f1f41a6c | 10930 | v->quick_push ((*params)[z]); |
ec761d5a | 10931 | f = build_function_call_vec (loc, vNULL, function, v, NULL); |
f1f41a6c | 10932 | vec_free (v); |
1cd6e20d | 10933 | return f; |
10934 | } | |
10935 | ||
10936 | /* Add the size parameter and leave as a function call for processing. */ | |
10937 | size_node = build_int_cst (size_type_node, n); | |
f1f41a6c | 10938 | params->quick_insert (0, size_node); |
1cd6e20d | 10939 | return NULL_TREE; |
10940 | } | |
10941 | ||
10942 | ||
a056826c | 10943 | /* Return whether atomic operations for naturally aligned N-byte |
10944 | arguments are supported, whether inline or through libatomic. */ | |
10945 | static bool | |
10946 | atomic_size_supported_p (int n) | |
10947 | { | |
10948 | switch (n) | |
10949 | { | |
10950 | case 1: | |
10951 | case 2: | |
10952 | case 4: | |
10953 | case 8: | |
10954 | return true; | |
10955 | ||
10956 | case 16: | |
10957 | return targetm.scalar_mode_supported_p (TImode); | |
10958 | ||
10959 | default: | |
10960 | return false; | |
10961 | } | |
10962 | } | |
10963 | ||
1cd6e20d | 10964 | /* This will process an __atomic_exchange function call, determine whether it |
10965 | needs to be mapped to the _N variation, or turned into a library call. | |
10966 | LOC is the location of the builtin call. | |
10967 | FUNCTION is the DECL that has been invoked; | |
10968 | PARAMS is the argument list for the call. The return value is non-null | |
10969 | TRUE is returned if it is translated into the proper format for a call to the | |
10970 | external library, and NEW_RETURN is set the tree for that function. | |
10971 | FALSE is returned if processing for the _N variation is required, and | |
47ae02b7 | 10972 | NEW_RETURN is set to the return value the result is copied into. */ |
1cd6e20d | 10973 | static bool |
10974 | resolve_overloaded_atomic_exchange (location_t loc, tree function, | |
f1f41a6c | 10975 | vec<tree, va_gc> *params, tree *new_return) |
1cd6e20d | 10976 | { |
10977 | tree p0, p1, p2, p3; | |
10978 | tree I_type, I_type_ptr; | |
10979 | int n = get_atomic_generic_size (loc, function, params); | |
10980 | ||
1d581089 | 10981 | /* Size of 0 is an error condition. */ |
10982 | if (n == 0) | |
10983 | { | |
10984 | *new_return = error_mark_node; | |
10985 | return true; | |
10986 | } | |
10987 | ||
1cd6e20d | 10988 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 10989 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 10990 | { |
10991 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10992 | return true; | |
10993 | } | |
10994 | ||
10995 | /* Otherwise there is a lockfree match, transform the call from: | |
10996 | void fn(T* mem, T* desired, T* return, model) | |
10997 | into | |
10998 | *return = (T) (fn (In* mem, (In) *desired, model)) */ | |
10999 | ||
f1f41a6c | 11000 | p0 = (*params)[0]; |
11001 | p1 = (*params)[1]; | |
11002 | p2 = (*params)[2]; | |
11003 | p3 = (*params)[3]; | |
1cd6e20d | 11004 | |
11005 | /* Create pointer to appropriate size. */ | |
11006 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
11007 | I_type_ptr = build_pointer_type (I_type); | |
11008 | ||
11009 | /* Convert object pointer to required type. */ | |
11010 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 11011 | (*params)[0] = p0; |
1cd6e20d | 11012 | /* Convert new value to required type, and dereference it. */ |
11013 | p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
11014 | p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); | |
f1f41a6c | 11015 | (*params)[1] = p1; |
1cd6e20d | 11016 | |
11017 | /* Move memory model to the 3rd position, and end param list. */ | |
f1f41a6c | 11018 | (*params)[2] = p3; |
11019 | params->truncate (3); | |
1cd6e20d | 11020 | |
11021 | /* Convert return pointer and dereference it for later assignment. */ | |
11022 | *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR); | |
11023 | ||
11024 | return false; | |
b6a5fc45 | 11025 | } |
11026 | ||
1cd6e20d | 11027 | |
11028 | /* This will process an __atomic_compare_exchange function call, determine | |
11029 | whether it needs to be mapped to the _N variation, or turned into a lib call. | |
11030 | LOC is the location of the builtin call. | |
11031 | FUNCTION is the DECL that has been invoked; | |
11032 | PARAMS is the argument list for the call. The return value is non-null | |
11033 | TRUE is returned if it is translated into the proper format for a call to the | |
11034 | external library, and NEW_RETURN is set the tree for that function. | |
11035 | FALSE is returned if processing for the _N variation is required. */ | |
11036 | ||
11037 | static bool | |
11038 | resolve_overloaded_atomic_compare_exchange (location_t loc, tree function, | |
f1f41a6c | 11039 | vec<tree, va_gc> *params, |
1cd6e20d | 11040 | tree *new_return) |
11041 | { | |
11042 | tree p0, p1, p2; | |
11043 | tree I_type, I_type_ptr; | |
11044 | int n = get_atomic_generic_size (loc, function, params); | |
11045 | ||
1d581089 | 11046 | /* Size of 0 is an error condition. */ |
11047 | if (n == 0) | |
11048 | { | |
11049 | *new_return = error_mark_node; | |
11050 | return true; | |
11051 | } | |
11052 | ||
1cd6e20d | 11053 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 11054 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 11055 | { |
11056 | /* The library generic format does not have the weak parameter, so | |
11057 | remove it from the param list. Since a parameter has been removed, | |
11058 | we can be sure that there is room for the SIZE_T parameter, meaning | |
11059 | there will not be a recursive rebuilding of the parameter list, so | |
11060 | there is no danger this will be done twice. */ | |
11061 | if (n > 0) | |
11062 | { | |
f1f41a6c | 11063 | (*params)[3] = (*params)[4]; |
11064 | (*params)[4] = (*params)[5]; | |
11065 | params->truncate (5); | |
1cd6e20d | 11066 | } |
11067 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
11068 | return true; | |
11069 | } | |
11070 | ||
11071 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
11072 | bool fn(T* mem, T* desired, T* return, weak, success, failure) | |
11073 | into | |
11074 | bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */ | |
ab2c1de8 | 11075 | |
f1f41a6c | 11076 | p0 = (*params)[0]; |
11077 | p1 = (*params)[1]; | |
11078 | p2 = (*params)[2]; | |
1cd6e20d | 11079 | |
11080 | /* Create pointer to appropriate size. */ | |
11081 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
11082 | I_type_ptr = build_pointer_type (I_type); | |
11083 | ||
11084 | /* Convert object pointer to required type. */ | |
11085 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 11086 | (*params)[0] = p0; |
1cd6e20d | 11087 | |
11088 | /* Convert expected pointer to required type. */ | |
11089 | p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1); | |
f1f41a6c | 11090 | (*params)[1] = p1; |
1cd6e20d | 11091 | |
11092 | /* Convert desired value to required type, and dereference it. */ | |
11093 | p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR); | |
11094 | p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2); | |
f1f41a6c | 11095 | (*params)[2] = p2; |
1cd6e20d | 11096 | |
11097 | /* The rest of the parameters are fine. NULL means no special return value | |
11098 | processing.*/ | |
11099 | *new_return = NULL; | |
11100 | return false; | |
11101 | } | |
11102 | ||
11103 | ||
11104 | /* This will process an __atomic_load function call, determine whether it | |
11105 | needs to be mapped to the _N variation, or turned into a library call. | |
11106 | LOC is the location of the builtin call. | |
11107 | FUNCTION is the DECL that has been invoked; | |
11108 | PARAMS is the argument list for the call. The return value is non-null | |
11109 | TRUE is returned if it is translated into the proper format for a call to the | |
11110 | external library, and NEW_RETURN is set the tree for that function. | |
11111 | FALSE is returned if processing for the _N variation is required, and | |
47ae02b7 | 11112 | NEW_RETURN is set to the return value the result is copied into. */ |
1cd6e20d | 11113 | |
11114 | static bool | |
11115 | resolve_overloaded_atomic_load (location_t loc, tree function, | |
f1f41a6c | 11116 | vec<tree, va_gc> *params, tree *new_return) |
1cd6e20d | 11117 | { |
11118 | tree p0, p1, p2; | |
11119 | tree I_type, I_type_ptr; | |
11120 | int n = get_atomic_generic_size (loc, function, params); | |
11121 | ||
1d581089 | 11122 | /* Size of 0 is an error condition. */ |
11123 | if (n == 0) | |
11124 | { | |
11125 | *new_return = error_mark_node; | |
11126 | return true; | |
11127 | } | |
11128 | ||
1cd6e20d | 11129 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 11130 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 11131 | { |
11132 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
11133 | return true; | |
11134 | } | |
11135 | ||
11136 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
11137 | void fn(T* mem, T* return, model) | |
11138 | into | |
11139 | *return = (T) (fn ((In *) mem, model)) */ | |
11140 | ||
f1f41a6c | 11141 | p0 = (*params)[0]; |
11142 | p1 = (*params)[1]; | |
11143 | p2 = (*params)[2]; | |
1cd6e20d | 11144 | |
11145 | /* Create pointer to appropriate size. */ | |
11146 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
11147 | I_type_ptr = build_pointer_type (I_type); | |
11148 | ||
11149 | /* Convert object pointer to required type. */ | |
11150 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 11151 | (*params)[0] = p0; |
1cd6e20d | 11152 | |
11153 | /* Move memory model to the 2nd position, and end param list. */ | |
f1f41a6c | 11154 | (*params)[1] = p2; |
11155 | params->truncate (2); | |
1cd6e20d | 11156 | |
11157 | /* Convert return pointer and dereference it for later assignment. */ | |
11158 | *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
11159 | ||
11160 | return false; | |
11161 | } | |
11162 | ||
11163 | ||
11164 | /* This will process an __atomic_store function call, determine whether it | |
11165 | needs to be mapped to the _N variation, or turned into a library call. | |
11166 | LOC is the location of the builtin call. | |
11167 | FUNCTION is the DECL that has been invoked; | |
11168 | PARAMS is the argument list for the call. The return value is non-null | |
11169 | TRUE is returned if it is translated into the proper format for a call to the | |
11170 | external library, and NEW_RETURN is set the tree for that function. | |
11171 | FALSE is returned if processing for the _N variation is required, and | |
47ae02b7 | 11172 | NEW_RETURN is set to the return value the result is copied into. */ |
1cd6e20d | 11173 | |
11174 | static bool | |
11175 | resolve_overloaded_atomic_store (location_t loc, tree function, | |
f1f41a6c | 11176 | vec<tree, va_gc> *params, tree *new_return) |
1cd6e20d | 11177 | { |
11178 | tree p0, p1; | |
11179 | tree I_type, I_type_ptr; | |
11180 | int n = get_atomic_generic_size (loc, function, params); | |
11181 | ||
1d581089 | 11182 | /* Size of 0 is an error condition. */ |
11183 | if (n == 0) | |
11184 | { | |
11185 | *new_return = error_mark_node; | |
11186 | return true; | |
11187 | } | |
11188 | ||
1cd6e20d | 11189 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 11190 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 11191 | { |
11192 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
11193 | return true; | |
11194 | } | |
11195 | ||
11196 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
11197 | void fn(T* mem, T* value, model) | |
11198 | into | |
11199 | fn ((In *) mem, (In) *value, model) */ | |
11200 | ||
f1f41a6c | 11201 | p0 = (*params)[0]; |
11202 | p1 = (*params)[1]; | |
1cd6e20d | 11203 | |
11204 | /* Create pointer to appropriate size. */ | |
11205 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
11206 | I_type_ptr = build_pointer_type (I_type); | |
11207 | ||
11208 | /* Convert object pointer to required type. */ | |
11209 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 11210 | (*params)[0] = p0; |
1cd6e20d | 11211 | |
11212 | /* Convert new value to required type, and dereference it. */ | |
11213 | p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
11214 | p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); | |
f1f41a6c | 11215 | (*params)[1] = p1; |
1cd6e20d | 11216 | |
11217 | /* The memory model is in the right spot already. Return is void. */ | |
11218 | *new_return = NULL_TREE; | |
11219 | ||
11220 | return false; | |
11221 | } | |
11222 | ||
11223 | ||
b6a5fc45 | 11224 | /* Some builtin functions are placeholders for other expressions. This |
11225 | function should be called immediately after parsing the call expression | |
11226 | before surrounding code has committed to the type of the expression. | |
11227 | ||
e60a6f7b | 11228 | LOC is the location of the builtin call. |
11229 | ||
b6a5fc45 | 11230 | FUNCTION is the DECL that has been invoked; it is known to be a builtin. |
11231 | PARAMS is the argument list for the call. The return value is non-null | |
11232 | when expansion is complete, and null if normal processing should | |
11233 | continue. */ | |
11234 | ||
11235 | tree | |
f1f41a6c | 11236 | resolve_overloaded_builtin (location_t loc, tree function, |
11237 | vec<tree, va_gc> *params) | |
b6a5fc45 | 11238 | { |
11239 | enum built_in_function orig_code = DECL_FUNCTION_CODE (function); | |
1cd6e20d | 11240 | bool orig_format = true; |
11241 | tree new_return = NULL_TREE; | |
11242 | ||
65441f6f | 11243 | switch (DECL_BUILT_IN_CLASS (function)) |
11244 | { | |
11245 | case BUILT_IN_NORMAL: | |
11246 | break; | |
11247 | case BUILT_IN_MD: | |
11248 | if (targetm.resolve_overloaded_builtin) | |
e60a6f7b | 11249 | return targetm.resolve_overloaded_builtin (loc, function, params); |
65441f6f | 11250 | else |
a0c938f0 | 11251 | return NULL_TREE; |
65441f6f | 11252 | default: |
11253 | return NULL_TREE; | |
11254 | } | |
a0c938f0 | 11255 | |
65441f6f | 11256 | /* Handle BUILT_IN_NORMAL here. */ |
b6a5fc45 | 11257 | switch (orig_code) |
11258 | { | |
1cd6e20d | 11259 | case BUILT_IN_ATOMIC_EXCHANGE: |
11260 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
11261 | case BUILT_IN_ATOMIC_LOAD: | |
11262 | case BUILT_IN_ATOMIC_STORE: | |
11263 | { | |
11264 | /* Handle these 4 together so that they can fall through to the next | |
11265 | case if the call is transformed to an _N variant. */ | |
11266 | switch (orig_code) | |
11267 | { | |
11268 | case BUILT_IN_ATOMIC_EXCHANGE: | |
11269 | { | |
11270 | if (resolve_overloaded_atomic_exchange (loc, function, params, | |
11271 | &new_return)) | |
11272 | return new_return; | |
11273 | /* Change to the _N variant. */ | |
11274 | orig_code = BUILT_IN_ATOMIC_EXCHANGE_N; | |
11275 | break; | |
11276 | } | |
11277 | ||
11278 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
11279 | { | |
11280 | if (resolve_overloaded_atomic_compare_exchange (loc, function, | |
11281 | params, | |
11282 | &new_return)) | |
11283 | return new_return; | |
11284 | /* Change to the _N variant. */ | |
11285 | orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N; | |
11286 | break; | |
11287 | } | |
11288 | case BUILT_IN_ATOMIC_LOAD: | |
11289 | { | |
11290 | if (resolve_overloaded_atomic_load (loc, function, params, | |
11291 | &new_return)) | |
11292 | return new_return; | |
11293 | /* Change to the _N variant. */ | |
11294 | orig_code = BUILT_IN_ATOMIC_LOAD_N; | |
11295 | break; | |
11296 | } | |
11297 | case BUILT_IN_ATOMIC_STORE: | |
11298 | { | |
11299 | if (resolve_overloaded_atomic_store (loc, function, params, | |
11300 | &new_return)) | |
11301 | return new_return; | |
11302 | /* Change to the _N variant. */ | |
11303 | orig_code = BUILT_IN_ATOMIC_STORE_N; | |
11304 | break; | |
11305 | } | |
11306 | default: | |
11307 | gcc_unreachable (); | |
11308 | } | |
11309 | /* Fallthrough to the normal processing. */ | |
11310 | } | |
11311 | case BUILT_IN_ATOMIC_EXCHANGE_N: | |
11312 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N: | |
11313 | case BUILT_IN_ATOMIC_LOAD_N: | |
11314 | case BUILT_IN_ATOMIC_STORE_N: | |
11315 | case BUILT_IN_ATOMIC_ADD_FETCH_N: | |
11316 | case BUILT_IN_ATOMIC_SUB_FETCH_N: | |
11317 | case BUILT_IN_ATOMIC_AND_FETCH_N: | |
11318 | case BUILT_IN_ATOMIC_NAND_FETCH_N: | |
11319 | case BUILT_IN_ATOMIC_XOR_FETCH_N: | |
11320 | case BUILT_IN_ATOMIC_OR_FETCH_N: | |
11321 | case BUILT_IN_ATOMIC_FETCH_ADD_N: | |
11322 | case BUILT_IN_ATOMIC_FETCH_SUB_N: | |
11323 | case BUILT_IN_ATOMIC_FETCH_AND_N: | |
11324 | case BUILT_IN_ATOMIC_FETCH_NAND_N: | |
11325 | case BUILT_IN_ATOMIC_FETCH_XOR_N: | |
11326 | case BUILT_IN_ATOMIC_FETCH_OR_N: | |
11327 | { | |
11328 | orig_format = false; | |
11329 | /* Fallthru for parameter processing. */ | |
11330 | } | |
2797f13a | 11331 | case BUILT_IN_SYNC_FETCH_AND_ADD_N: |
11332 | case BUILT_IN_SYNC_FETCH_AND_SUB_N: | |
11333 | case BUILT_IN_SYNC_FETCH_AND_OR_N: | |
11334 | case BUILT_IN_SYNC_FETCH_AND_AND_N: | |
11335 | case BUILT_IN_SYNC_FETCH_AND_XOR_N: | |
11336 | case BUILT_IN_SYNC_FETCH_AND_NAND_N: | |
11337 | case BUILT_IN_SYNC_ADD_AND_FETCH_N: | |
11338 | case BUILT_IN_SYNC_SUB_AND_FETCH_N: | |
11339 | case BUILT_IN_SYNC_OR_AND_FETCH_N: | |
11340 | case BUILT_IN_SYNC_AND_AND_FETCH_N: | |
11341 | case BUILT_IN_SYNC_XOR_AND_FETCH_N: | |
11342 | case BUILT_IN_SYNC_NAND_AND_FETCH_N: | |
11343 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N: | |
11344 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N: | |
11345 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N: | |
11346 | case BUILT_IN_SYNC_LOCK_RELEASE_N: | |
b6a5fc45 | 11347 | { |
11348 | int n = sync_resolve_size (function, params); | |
b9c74b4d | 11349 | tree new_function, first_param, result; |
b9a16870 | 11350 | enum built_in_function fncode; |
b6a5fc45 | 11351 | |
11352 | if (n == 0) | |
11353 | return error_mark_node; | |
11354 | ||
b9a16870 | 11355 | fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1); |
11356 | new_function = builtin_decl_explicit (fncode); | |
1cd6e20d | 11357 | if (!sync_resolve_params (loc, function, new_function, params, |
11358 | orig_format)) | |
b6a5fc45 | 11359 | return error_mark_node; |
11360 | ||
f1f41a6c | 11361 | first_param = (*params)[0]; |
ec761d5a | 11362 | result = build_function_call_vec (loc, vNULL, new_function, params, |
11363 | NULL); | |
1cd6e20d | 11364 | if (result == error_mark_node) |
11365 | return result; | |
2797f13a | 11366 | if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N |
1cd6e20d | 11367 | && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N |
11368 | && orig_code != BUILT_IN_ATOMIC_STORE_N) | |
11369 | result = sync_resolve_return (first_param, result, orig_format); | |
b6a5fc45 | 11370 | |
1cd6e20d | 11371 | /* If new_return is set, assign function to that expr and cast the |
11372 | result to void since the generic interface returned void. */ | |
11373 | if (new_return) | |
11374 | { | |
11375 | /* Cast function result from I{1,2,4,8,16} to the required type. */ | |
11376 | result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result); | |
11377 | result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return, | |
11378 | result); | |
11379 | TREE_SIDE_EFFECTS (result) = 1; | |
11380 | protected_set_expr_location (result, loc); | |
11381 | result = convert (void_type_node, result); | |
11382 | } | |
b6a5fc45 | 11383 | return result; |
11384 | } | |
11385 | ||
11386 | default: | |
65441f6f | 11387 | return NULL_TREE; |
b6a5fc45 | 11388 | } |
11389 | } | |
11390 | ||
73437615 | 11391 | /* vector_types_compatible_elements_p is used in type checks of vectors |
11392 | values used as operands of binary operators. Where it returns true, and | |
11393 | the other checks of the caller succeed (being vector types in he first | |
11394 | place, and matching number of elements), we can just treat the types | |
11395 | as essentially the same. | |
11396 | Contrast with vector_targets_convertible_p, which is used for vector | |
11397 | pointer types, and vector_types_convertible_p, which will allow | |
11398 | language-specific matches under the control of flag_lax_vector_conversions, | |
11399 | and might still require a conversion. */ | |
11400 | /* True if vector types T1 and T2 can be inputs to the same binary | |
11401 | operator without conversion. | |
11402 | We don't check the overall vector size here because some of our callers | |
11403 | want to give different error messages when the vectors are compatible | |
11404 | except for the element count. */ | |
11405 | ||
491255f5 | 11406 | bool |
73437615 | 11407 | vector_types_compatible_elements_p (tree t1, tree t2) |
491255f5 | 11408 | { |
73437615 | 11409 | bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2); |
11410 | t1 = TREE_TYPE (t1); | |
11411 | t2 = TREE_TYPE (t2); | |
11412 | ||
491255f5 | 11413 | enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); |
11414 | ||
9421ebb9 | 11415 | gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE) |
11416 | && (c2 == INTEGER_TYPE || c2 == REAL_TYPE | |
11417 | || c2 == FIXED_POINT_TYPE)); | |
491255f5 | 11418 | |
73437615 | 11419 | t1 = c_common_signed_type (t1); |
11420 | t2 = c_common_signed_type (t2); | |
491255f5 | 11421 | /* Equality works here because c_common_signed_type uses |
11422 | TYPE_MAIN_VARIANT. */ | |
73437615 | 11423 | if (t1 == t2) |
11424 | return true; | |
11425 | if (opaque && c1 == c2 | |
11426 | && (c1 == INTEGER_TYPE || c1 == REAL_TYPE) | |
11427 | && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) | |
11428 | return true; | |
11429 | return false; | |
491255f5 | 11430 | } |
11431 | ||
be7350e7 | 11432 | /* Check for missing format attributes on function pointers. LTYPE is |
11433 | the new type or left-hand side type. RTYPE is the old type or | |
11434 | right-hand side type. Returns TRUE if LTYPE is missing the desired | |
11435 | attribute. */ | |
11436 | ||
11437 | bool | |
11438 | check_missing_format_attribute (tree ltype, tree rtype) | |
11439 | { | |
11440 | tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype); | |
11441 | tree ra; | |
11442 | ||
11443 | for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra)) | |
11444 | if (is_attribute_p ("format", TREE_PURPOSE (ra))) | |
11445 | break; | |
11446 | if (ra) | |
11447 | { | |
11448 | tree la; | |
11449 | for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la)) | |
11450 | if (is_attribute_p ("format", TREE_PURPOSE (la))) | |
11451 | break; | |
11452 | return !la; | |
11453 | } | |
11454 | else | |
11455 | return false; | |
11456 | } | |
11457 | ||
2840aae4 | 11458 | /* Subscripting with type char is likely to lose on a machine where |
11459 | chars are signed. So warn on any machine, but optionally. Don't | |
11460 | warn for unsigned char since that type is safe. Don't warn for | |
11461 | signed char because anyone who uses that must have done so | |
11462 | deliberately. Furthermore, we reduce the false positive load by | |
11463 | warning only for non-constant value of type char. */ | |
11464 | ||
11465 | void | |
92b63884 | 11466 | warn_array_subscript_with_type_char (location_t loc, tree index) |
2840aae4 | 11467 | { |
11468 | if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node | |
11469 | && TREE_CODE (index) != INTEGER_CST) | |
92b63884 | 11470 | warning_at (loc, OPT_Wchar_subscripts, |
11471 | "array subscript has type %<char%>"); | |
2840aae4 | 11472 | } |
11473 | ||
e534436e | 11474 | /* Implement -Wparentheses for the unexpected C precedence rules, to |
11475 | cover cases like x + y << z which readers are likely to | |
11476 | misinterpret. We have seen an expression in which CODE is a binary | |
82012ffe | 11477 | operator used to combine expressions ARG_LEFT and ARG_RIGHT, which |
11478 | before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and | |
11479 | CODE_RIGHT may be ERROR_MARK, which means that that side of the | |
11480 | expression was not formed using a binary or unary operator, or it | |
11481 | was enclosed in parentheses. */ | |
e534436e | 11482 | |
11483 | void | |
b0e7825e | 11484 | warn_about_parentheses (location_t loc, enum tree_code code, |
269f7979 | 11485 | enum tree_code code_left, tree arg_left, |
82012ffe | 11486 | enum tree_code code_right, tree arg_right) |
e534436e | 11487 | { |
11488 | if (!warn_parentheses) | |
11489 | return; | |
11490 | ||
82012ffe | 11491 | /* This macro tests that the expression ARG with original tree code |
11492 | CODE appears to be a boolean expression. or the result of folding a | |
11493 | boolean expression. */ | |
11494 | #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ | |
11495 | (truth_value_p (TREE_CODE (ARG)) \ | |
11496 | || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ | |
11497 | /* Folding may create 0 or 1 integers from other expressions. */ \ | |
11498 | || ((CODE) != INTEGER_CST \ | |
11499 | && (integer_onep (ARG) || integer_zerop (ARG)))) | |
11500 | ||
48e1416a | 11501 | switch (code) |
e534436e | 11502 | { |
82012ffe | 11503 | case LSHIFT_EXPR: |
b0e7825e | 11504 | if (code_left == PLUS_EXPR) |
11505 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11506 | "suggest parentheses around %<+%> inside %<<<%>"); | |
11507 | else if (code_right == PLUS_EXPR) | |
11508 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11509 | "suggest parentheses around %<+%> inside %<<<%>"); | |
11510 | else if (code_left == MINUS_EXPR) | |
11511 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11512 | "suggest parentheses around %<-%> inside %<<<%>"); | |
11513 | else if (code_right == MINUS_EXPR) | |
11514 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11515 | "suggest parentheses around %<-%> inside %<<<%>"); | |
82012ffe | 11516 | return; |
e534436e | 11517 | |
82012ffe | 11518 | case RSHIFT_EXPR: |
b0e7825e | 11519 | if (code_left == PLUS_EXPR) |
11520 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11521 | "suggest parentheses around %<+%> inside %<>>%>"); | |
11522 | else if (code_right == PLUS_EXPR) | |
11523 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11524 | "suggest parentheses around %<+%> inside %<>>%>"); | |
11525 | else if (code_left == MINUS_EXPR) | |
11526 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11527 | "suggest parentheses around %<-%> inside %<>>%>"); | |
11528 | else if (code_right == MINUS_EXPR) | |
11529 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11530 | "suggest parentheses around %<-%> inside %<>>%>"); | |
82012ffe | 11531 | return; |
e534436e | 11532 | |
82012ffe | 11533 | case TRUTH_ORIF_EXPR: |
b0e7825e | 11534 | if (code_left == TRUTH_ANDIF_EXPR) |
11535 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11536 | "suggest parentheses around %<&&%> within %<||%>"); | |
11537 | else if (code_right == TRUTH_ANDIF_EXPR) | |
11538 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11539 | "suggest parentheses around %<&&%> within %<||%>"); | |
82012ffe | 11540 | return; |
11541 | ||
11542 | case BIT_IOR_EXPR: | |
e534436e | 11543 | if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR |
b0e7825e | 11544 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
11545 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11546 | "suggest parentheses around arithmetic in operand of %<|%>"); | |
11547 | else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR | |
11548 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
11549 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11550 | "suggest parentheses around arithmetic in operand of %<|%>"); |
e534436e | 11551 | /* Check cases like x|y==z */ |
b0e7825e | 11552 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11553 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11554 | "suggest parentheses around comparison in operand of %<|%>"); | |
11555 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11556 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11557 | "suggest parentheses around comparison in operand of %<|%>"); |
11558 | /* Check cases like !x | y */ | |
11559 | else if (code_left == TRUTH_NOT_EXPR | |
11560 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
b0e7825e | 11561 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11562 | "suggest parentheses around operand of " | |
11563 | "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>"); | |
82012ffe | 11564 | return; |
e534436e | 11565 | |
82012ffe | 11566 | case BIT_XOR_EXPR: |
e534436e | 11567 | if (code_left == BIT_AND_EXPR |
b0e7825e | 11568 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
11569 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11570 | "suggest parentheses around arithmetic in operand of %<^%>"); | |
11571 | else if (code_right == BIT_AND_EXPR | |
11572 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
11573 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11574 | "suggest parentheses around arithmetic in operand of %<^%>"); |
e534436e | 11575 | /* Check cases like x^y==z */ |
b0e7825e | 11576 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11577 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11578 | "suggest parentheses around comparison in operand of %<^%>"); | |
11579 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11580 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11581 | "suggest parentheses around comparison in operand of %<^%>"); |
11582 | return; | |
e534436e | 11583 | |
82012ffe | 11584 | case BIT_AND_EXPR: |
b0e7825e | 11585 | if (code_left == PLUS_EXPR) |
11586 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11587 | "suggest parentheses around %<+%> in operand of %<&%>"); | |
11588 | else if (code_right == PLUS_EXPR) | |
11589 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11590 | "suggest parentheses around %<+%> in operand of %<&%>"); |
b0e7825e | 11591 | else if (code_left == MINUS_EXPR) |
11592 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11593 | "suggest parentheses around %<-%> in operand of %<&%>"); | |
11594 | else if (code_right == MINUS_EXPR) | |
11595 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11596 | "suggest parentheses around %<-%> in operand of %<&%>"); |
e534436e | 11597 | /* Check cases like x&y==z */ |
b0e7825e | 11598 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11599 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11600 | "suggest parentheses around comparison in operand of %<&%>"); | |
11601 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11602 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11603 | "suggest parentheses around comparison in operand of %<&%>"); |
11604 | /* Check cases like !x & y */ | |
11605 | else if (code_left == TRUTH_NOT_EXPR | |
11606 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
b0e7825e | 11607 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11608 | "suggest parentheses around operand of " | |
11609 | "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>"); | |
82012ffe | 11610 | return; |
e534436e | 11611 | |
82012ffe | 11612 | case EQ_EXPR: |
b0e7825e | 11613 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11614 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11615 | "suggest parentheses around comparison in operand of %<==%>"); | |
11616 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11617 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11618 | "suggest parentheses around comparison in operand of %<==%>"); |
11619 | return; | |
11620 | case NE_EXPR: | |
b0e7825e | 11621 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11622 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11623 | "suggest parentheses around comparison in operand of %<!=%>"); | |
11624 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11625 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11626 | "suggest parentheses around comparison in operand of %<!=%>"); |
11627 | return; | |
11628 | ||
11629 | default: | |
b0e7825e | 11630 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
11631 | { | |
11632 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
269f7979 | 11633 | && code_left != NE_EXPR && code_left != EQ_EXPR |
11634 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) | |
b0e7825e | 11635 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11636 | "comparisons like %<X<=Y<=Z%> do not " | |
11637 | "have their mathematical meaning"); | |
11638 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison | |
269f7979 | 11639 | && code_right != NE_EXPR && code_right != EQ_EXPR |
b0e7825e | 11640 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))) |
11641 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11642 | "comparisons like %<X<=Y<=Z%> do not " | |
11643 | "have their mathematical meaning"); | |
11644 | } | |
82012ffe | 11645 | return; |
6ce0c450 | 11646 | } |
82012ffe | 11647 | #undef NOT_A_BOOLEAN_EXPR_P |
e534436e | 11648 | } |
11649 | ||
92fccaaa | 11650 | /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ |
11651 | ||
11652 | void | |
11653 | warn_for_unused_label (tree label) | |
11654 | { | |
11655 | if (!TREE_USED (label)) | |
11656 | { | |
11657 | if (DECL_INITIAL (label)) | |
11658 | warning (OPT_Wunused_label, "label %q+D defined but not used", label); | |
11659 | else | |
11660 | warning (OPT_Wunused_label, "label %q+D declared but not defined", label); | |
11661 | } | |
11662 | } | |
2840aae4 | 11663 | |
b6889cb0 | 11664 | /* Warn for division by zero according to the value of DIVISOR. LOC |
11665 | is the location of the division operator. */ | |
f092582b | 11666 | |
11667 | void | |
b6889cb0 | 11668 | warn_for_div_by_zero (location_t loc, tree divisor) |
f092582b | 11669 | { |
9421ebb9 | 11670 | /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning |
11671 | about division by zero. Do not issue a warning if DIVISOR has a | |
f092582b | 11672 | floating-point type, since we consider 0.0/0.0 a valid way of |
11673 | generating a NaN. */ | |
48d94ede | 11674 | if (c_inhibit_evaluation_warnings == 0 |
9421ebb9 | 11675 | && (integer_zerop (divisor) || fixed_zerop (divisor))) |
b6889cb0 | 11676 | warning_at (loc, OPT_Wdiv_by_zero, "division by zero"); |
f092582b | 11677 | } |
11678 | ||
13869a99 | 11679 | /* Subroutine of build_binary_op. Give warnings for comparisons |
11680 | between signed and unsigned quantities that may fail. Do the | |
11681 | checking based on the original operand trees ORIG_OP0 and ORIG_OP1, | |
11682 | so that casts will be considered, but default promotions won't | |
8e70fb09 | 11683 | be. |
11684 | ||
11685 | LOCATION is the location of the comparison operator. | |
13869a99 | 11686 | |
11687 | The arguments of this function map directly to local variables | |
11688 | of build_binary_op. */ | |
11689 | ||
48e1416a | 11690 | void |
8e70fb09 | 11691 | warn_for_sign_compare (location_t location, |
48e1416a | 11692 | tree orig_op0, tree orig_op1, |
11693 | tree op0, tree op1, | |
13869a99 | 11694 | tree result_type, enum tree_code resultcode) |
11695 | { | |
11696 | int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); | |
11697 | int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); | |
11698 | int unsignedp0, unsignedp1; | |
48e1416a | 11699 | |
13869a99 | 11700 | /* In C++, check for comparison of different enum types. */ |
11701 | if (c_dialect_cxx() | |
11702 | && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE | |
11703 | && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE | |
11704 | && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) | |
895b662f | 11705 | != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) |
13869a99 | 11706 | { |
8e70fb09 | 11707 | warning_at (location, |
11708 | OPT_Wsign_compare, "comparison between types %qT and %qT", | |
11709 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); | |
13869a99 | 11710 | } |
11711 | ||
11712 | /* Do not warn if the comparison is being done in a signed type, | |
11713 | since the signed type will only be chosen if it can represent | |
11714 | all the values of the unsigned type. */ | |
11715 | if (!TYPE_UNSIGNED (result_type)) | |
11716 | /* OK */; | |
11717 | /* Do not warn if both operands are unsigned. */ | |
11718 | else if (op0_signed == op1_signed) | |
11719 | /* OK */; | |
11720 | else | |
11721 | { | |
895b662f | 11722 | tree sop, uop, base_type; |
13869a99 | 11723 | bool ovf; |
895b662f | 11724 | |
13869a99 | 11725 | if (op0_signed) |
11726 | sop = orig_op0, uop = orig_op1; | |
48e1416a | 11727 | else |
13869a99 | 11728 | sop = orig_op1, uop = orig_op0; |
11729 | ||
48e1416a | 11730 | STRIP_TYPE_NOPS (sop); |
13869a99 | 11731 | STRIP_TYPE_NOPS (uop); |
895b662f | 11732 | base_type = (TREE_CODE (result_type) == COMPLEX_TYPE |
11733 | ? TREE_TYPE (result_type) : result_type); | |
13869a99 | 11734 | |
11735 | /* Do not warn if the signed quantity is an unsuffixed integer | |
11736 | literal (or some static constant expression involving such | |
11737 | literals or a conditional expression involving such literals) | |
11738 | and it is non-negative. */ | |
11739 | if (tree_expr_nonnegative_warnv_p (sop, &ovf)) | |
11740 | /* OK */; | |
11741 | /* Do not warn if the comparison is an equality operation, the | |
11742 | unsigned quantity is an integral constant, and it would fit | |
11743 | in the result if the result were signed. */ | |
11744 | else if (TREE_CODE (uop) == INTEGER_CST | |
11745 | && (resultcode == EQ_EXPR || resultcode == NE_EXPR) | |
895b662f | 11746 | && int_fits_type_p (uop, c_common_signed_type (base_type))) |
13869a99 | 11747 | /* OK */; |
11748 | /* In C, do not warn if the unsigned quantity is an enumeration | |
11749 | constant and its maximum value would fit in the result if the | |
11750 | result were signed. */ | |
11751 | else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST | |
11752 | && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE | |
11753 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), | |
895b662f | 11754 | c_common_signed_type (base_type))) |
13869a99 | 11755 | /* OK */; |
48e1416a | 11756 | else |
8e70fb09 | 11757 | warning_at (location, |
48e1416a | 11758 | OPT_Wsign_compare, |
8e70fb09 | 11759 | "comparison between signed and unsigned integer expressions"); |
13869a99 | 11760 | } |
48e1416a | 11761 | |
13869a99 | 11762 | /* Warn if two unsigned values are being compared in a size larger |
11763 | than their original size, and one (and only one) is the result of | |
11764 | a `~' operator. This comparison will always fail. | |
48e1416a | 11765 | |
13869a99 | 11766 | Also warn if one operand is a constant, and the constant does not |
11767 | have all bits set that are set in the ~ operand when it is | |
11768 | extended. */ | |
11769 | ||
7f506bca | 11770 | op0 = c_common_get_narrower (op0, &unsignedp0); |
11771 | op1 = c_common_get_narrower (op1, &unsignedp1); | |
48e1416a | 11772 | |
13869a99 | 11773 | if ((TREE_CODE (op0) == BIT_NOT_EXPR) |
11774 | ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) | |
11775 | { | |
11776 | if (TREE_CODE (op0) == BIT_NOT_EXPR) | |
7f506bca | 11777 | op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); |
13869a99 | 11778 | if (TREE_CODE (op1) == BIT_NOT_EXPR) |
7f506bca | 11779 | op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); |
13869a99 | 11780 | |
e913b5cd | 11781 | if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1)) |
13869a99 | 11782 | { |
11783 | tree primop; | |
11784 | HOST_WIDE_INT constant, mask; | |
11785 | int unsignedp; | |
11786 | unsigned int bits; | |
48e1416a | 11787 | |
e913b5cd | 11788 | if (tree_fits_shwi_p (op0)) |
13869a99 | 11789 | { |
11790 | primop = op1; | |
11791 | unsignedp = unsignedp1; | |
e913b5cd | 11792 | constant = tree_to_shwi (op0); |
13869a99 | 11793 | } |
11794 | else | |
11795 | { | |
11796 | primop = op0; | |
11797 | unsignedp = unsignedp0; | |
e913b5cd | 11798 | constant = tree_to_shwi (op1); |
13869a99 | 11799 | } |
48e1416a | 11800 | |
13869a99 | 11801 | bits = TYPE_PRECISION (TREE_TYPE (primop)); |
11802 | if (bits < TYPE_PRECISION (result_type) | |
11803 | && bits < HOST_BITS_PER_LONG && unsignedp) | |
11804 | { | |
85f5e2ee | 11805 | mask = (~ (unsigned HOST_WIDE_INT) 0) << bits; |
13869a99 | 11806 | if ((mask & constant) != mask) |
11807 | { | |
11808 | if (constant == 0) | |
76fdceeb | 11809 | warning_at (location, OPT_Wsign_compare, |
11810 | "promoted ~unsigned is always non-zero"); | |
13869a99 | 11811 | else |
48e1416a | 11812 | warning_at (location, OPT_Wsign_compare, |
8e70fb09 | 11813 | "comparison of promoted ~unsigned with constant"); |
13869a99 | 11814 | } |
11815 | } | |
11816 | } | |
11817 | else if (unsignedp0 && unsignedp1 | |
11818 | && (TYPE_PRECISION (TREE_TYPE (op0)) | |
11819 | < TYPE_PRECISION (result_type)) | |
11820 | && (TYPE_PRECISION (TREE_TYPE (op1)) | |
11821 | < TYPE_PRECISION (result_type))) | |
8e70fb09 | 11822 | warning_at (location, OPT_Wsign_compare, |
13869a99 | 11823 | "comparison of promoted ~unsigned with unsigned"); |
11824 | } | |
11825 | } | |
11826 | ||
5ba33bf4 | 11827 | /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common |
11828 | type via c_common_type. If -Wdouble-promotion is in use, and the | |
11829 | conditions for warning have been met, issue a warning. GMSGID is | |
11830 | the warning message. It must have two %T specifiers for the type | |
11831 | that was converted (generally "float") and the type to which it was | |
11832 | converted (generally "double), respectively. LOC is the location | |
11833 | to which the awrning should refer. */ | |
11834 | ||
11835 | void | |
11836 | do_warn_double_promotion (tree result_type, tree type1, tree type2, | |
11837 | const char *gmsgid, location_t loc) | |
11838 | { | |
11839 | tree source_type; | |
11840 | ||
11841 | if (!warn_double_promotion) | |
11842 | return; | |
11843 | /* If the conversion will not occur at run-time, there is no need to | |
11844 | warn about it. */ | |
11845 | if (c_inhibit_evaluation_warnings) | |
11846 | return; | |
11847 | if (TYPE_MAIN_VARIANT (result_type) != double_type_node | |
11848 | && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) | |
11849 | return; | |
11850 | if (TYPE_MAIN_VARIANT (type1) == float_type_node | |
11851 | || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) | |
11852 | source_type = type1; | |
11853 | else if (TYPE_MAIN_VARIANT (type2) == float_type_node | |
11854 | || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) | |
11855 | source_type = type2; | |
11856 | else | |
11857 | return; | |
11858 | warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); | |
11859 | } | |
11860 | ||
0949f227 | 11861 | /* Possibly warn about unused parameters. */ |
11862 | ||
11863 | void | |
11864 | do_warn_unused_parameter (tree fn) | |
11865 | { | |
11866 | tree decl; | |
11867 | ||
11868 | for (decl = DECL_ARGUMENTS (fn); | |
11869 | decl; decl = DECL_CHAIN (decl)) | |
11870 | if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL | |
11871 | && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl) | |
11872 | && !TREE_NO_WARNING (decl)) | |
11873 | warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter, | |
11874 | "unused parameter %qD", decl); | |
11875 | } | |
11876 | ||
11877 | ||
41771881 | 11878 | /* Setup a TYPE_DECL node as a typedef representation. |
11879 | ||
11880 | X is a TYPE_DECL for a typedef statement. Create a brand new | |
11881 | ..._TYPE node (which will be just a variant of the existing | |
11882 | ..._TYPE node with identical properties) and then install X | |
11883 | as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. | |
11884 | ||
11885 | The whole point here is to end up with a situation where each | |
11886 | and every ..._TYPE node the compiler creates will be uniquely | |
11887 | associated with AT MOST one node representing a typedef name. | |
11888 | This way, even though the compiler substitutes corresponding | |
11889 | ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very | |
11890 | early on, later parts of the compiler can always do the reverse | |
11891 | translation and get back the corresponding typedef name. For | |
11892 | example, given: | |
ab2c1de8 | 11893 | |
41771881 | 11894 | typedef struct S MY_TYPE; |
11895 | MY_TYPE object; | |
11896 | ||
11897 | Later parts of the compiler might only know that `object' was of | |
11898 | type `struct S' if it were not for code just below. With this | |
11899 | code however, later parts of the compiler see something like: | |
11900 | ||
11901 | struct S' == struct S | |
11902 | typedef struct S' MY_TYPE; | |
11903 | struct S' object; | |
11904 | ||
11905 | And they can then deduce (from the node for type struct S') that | |
11906 | the original object declaration was: | |
11907 | ||
11908 | MY_TYPE object; | |
11909 | ||
11910 | Being able to do this is important for proper support of protoize, | |
11911 | and also for generating precise symbolic debugging information | |
11912 | which takes full account of the programmer's (typedef) vocabulary. | |
11913 | ||
11914 | Obviously, we don't want to generate a duplicate ..._TYPE node if | |
11915 | the TYPE_DECL node that we are now processing really represents a | |
11916 | standard built-in type. */ | |
11917 | ||
11918 | void | |
11919 | set_underlying_type (tree x) | |
11920 | { | |
11921 | if (x == error_mark_node) | |
11922 | return; | |
11923 | if (DECL_IS_BUILTIN (x)) | |
11924 | { | |
11925 | if (TYPE_NAME (TREE_TYPE (x)) == 0) | |
11926 | TYPE_NAME (TREE_TYPE (x)) = x; | |
11927 | } | |
11928 | else if (TREE_TYPE (x) != error_mark_node | |
11929 | && DECL_ORIGINAL_TYPE (x) == NULL_TREE) | |
11930 | { | |
11931 | tree tt = TREE_TYPE (x); | |
11932 | DECL_ORIGINAL_TYPE (x) = tt; | |
11933 | tt = build_variant_type_copy (tt); | |
11934 | TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
11935 | TYPE_NAME (tt) = x; | |
11936 | TREE_USED (tt) = TREE_USED (x); | |
11937 | TREE_TYPE (x) = tt; | |
11938 | } | |
11939 | } | |
11940 | ||
1a4c44c5 | 11941 | /* Record the types used by the current global variable declaration |
11942 | being parsed, so that we can decide later to emit their debug info. | |
11943 | Those types are in types_used_by_cur_var_decl, and we are going to | |
11944 | store them in the types_used_by_vars_hash hash table. | |
11945 | DECL is the declaration of the global variable that has been parsed. */ | |
11946 | ||
11947 | void | |
11948 | record_types_used_by_current_var_decl (tree decl) | |
11949 | { | |
11950 | gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl)); | |
11951 | ||
f1f41a6c | 11952 | while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ()) |
1a4c44c5 | 11953 | { |
f1f41a6c | 11954 | tree type = types_used_by_cur_var_decl->pop (); |
aef48c9a | 11955 | types_used_by_var_decl_insert (type, decl); |
1a4c44c5 | 11956 | } |
11957 | } | |
11958 | ||
a4e3ffad | 11959 | /* If DECL is a typedef that is declared in the current function, |
11960 | record it for the purpose of -Wunused-local-typedefs. */ | |
11961 | ||
11962 | void | |
11963 | record_locally_defined_typedef (tree decl) | |
11964 | { | |
11965 | struct c_language_function *l; | |
11966 | ||
11967 | if (!warn_unused_local_typedefs | |
11968 | || cfun == NULL | |
11969 | /* if this is not a locally defined typedef then we are not | |
11970 | interested. */ | |
11971 | || !is_typedef_decl (decl) | |
11972 | || !decl_function_context (decl)) | |
11973 | return; | |
11974 | ||
11975 | l = (struct c_language_function *) cfun->language; | |
f1f41a6c | 11976 | vec_safe_push (l->local_typedefs, decl); |
a4e3ffad | 11977 | } |
11978 | ||
11979 | /* If T is a TYPE_DECL declared locally, mark it as used. */ | |
11980 | ||
11981 | void | |
11982 | maybe_record_typedef_use (tree t) | |
11983 | { | |
11984 | if (!is_typedef_decl (t)) | |
11985 | return; | |
11986 | ||
11987 | TREE_USED (t) = true; | |
11988 | } | |
11989 | ||
11990 | /* Warn if there are some unused locally defined typedefs in the | |
11991 | current function. */ | |
11992 | ||
11993 | void | |
11994 | maybe_warn_unused_local_typedefs (void) | |
11995 | { | |
11996 | int i; | |
11997 | tree decl; | |
11998 | /* The number of times we have emitted -Wunused-local-typedefs | |
11999 | warnings. If this is different from errorcount, that means some | |
12000 | unrelated errors have been issued. In which case, we'll avoid | |
12001 | emitting "unused-local-typedefs" warnings. */ | |
12002 | static int unused_local_typedefs_warn_count; | |
12003 | struct c_language_function *l; | |
12004 | ||
12005 | if (cfun == NULL) | |
12006 | return; | |
12007 | ||
12008 | if ((l = (struct c_language_function *) cfun->language) == NULL) | |
12009 | return; | |
12010 | ||
12011 | if (warn_unused_local_typedefs | |
12012 | && errorcount == unused_local_typedefs_warn_count) | |
12013 | { | |
f1f41a6c | 12014 | FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl) |
a4e3ffad | 12015 | if (!TREE_USED (decl)) |
12016 | warning_at (DECL_SOURCE_LOCATION (decl), | |
12017 | OPT_Wunused_local_typedefs, | |
12018 | "typedef %qD locally defined but not used", decl); | |
12019 | unused_local_typedefs_warn_count = errorcount; | |
12020 | } | |
12021 | ||
f1f41a6c | 12022 | vec_free (l->local_typedefs); |
a4e3ffad | 12023 | } |
12024 | ||
78bf4156 | 12025 | /* Warn about boolean expression compared with an integer value different |
12026 | from true/false. Warns also e.g. about "(i1 == i2) == 2". | |
12027 | LOC is the location of the comparison, CODE is its code, OP0 and OP1 | |
12028 | are the operands of the comparison. The caller must ensure that | |
12029 | either operand is a boolean expression. */ | |
12030 | ||
12031 | void | |
12032 | maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0, | |
12033 | tree op1) | |
12034 | { | |
12035 | if (TREE_CODE_CLASS (code) != tcc_comparison) | |
12036 | return; | |
12037 | ||
9c691dbd | 12038 | tree f, cst; |
12039 | if (f = fold_for_warn (op0), | |
12040 | TREE_CODE (f) == INTEGER_CST) | |
12041 | cst = op0 = f; | |
12042 | else if (f = fold_for_warn (op1), | |
12043 | TREE_CODE (f) == INTEGER_CST) | |
12044 | cst = op1 = f; | |
12045 | else | |
78bf4156 | 12046 | return; |
12047 | ||
12048 | if (!integer_zerop (cst) && !integer_onep (cst)) | |
12049 | { | |
a720ab1c | 12050 | int sign = (TREE_CODE (op0) == INTEGER_CST |
12051 | ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst)); | |
78bf4156 | 12052 | if (code == EQ_EXPR |
12053 | || ((code == GT_EXPR || code == GE_EXPR) && sign < 0) | |
12054 | || ((code == LT_EXPR || code == LE_EXPR) && sign > 0)) | |
12055 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " | |
12056 | "with boolean expression is always false", cst); | |
12057 | else | |
12058 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " | |
12059 | "with boolean expression is always true", cst); | |
12060 | } | |
a720ab1c | 12061 | else if (integer_zerop (cst) || integer_onep (cst)) |
12062 | { | |
14744a16 | 12063 | /* If the non-constant operand isn't of a boolean type, we |
12064 | don't want to warn here. */ | |
12065 | tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0; | |
12066 | /* Handle booleans promoted to integers. */ | |
12067 | if (CONVERT_EXPR_P (noncst) | |
12068 | && TREE_TYPE (noncst) == integer_type_node | |
12069 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE) | |
12070 | /* Warn. */; | |
12071 | else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE | |
12072 | && !truth_value_p (TREE_CODE (noncst))) | |
12073 | return; | |
a720ab1c | 12074 | /* Do some magic to get the right diagnostics. */ |
12075 | bool flag = TREE_CODE (op0) == INTEGER_CST; | |
12076 | flag = integer_zerop (cst) ? flag : !flag; | |
12077 | if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag)) | |
12078 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " | |
12079 | "with boolean expression is always true", cst); | |
12080 | else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag)) | |
12081 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " | |
12082 | "with boolean expression is always false", cst); | |
12083 | } | |
78bf4156 | 12084 | } |
12085 | ||
8d669e79 | 12086 | /* Warn if signed left shift overflows. We don't warn |
12087 | about left-shifting 1 into the sign bit in C++14; cf. | |
12088 | <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457> | |
12089 | LOC is a location of the shift; OP0 and OP1 are the operands. | |
12090 | Return true if an overflow is detected, false otherwise. */ | |
12091 | ||
12092 | bool | |
12093 | maybe_warn_shift_overflow (location_t loc, tree op0, tree op1) | |
12094 | { | |
12095 | if (TREE_CODE (op0) != INTEGER_CST | |
12096 | || TREE_CODE (op1) != INTEGER_CST) | |
12097 | return false; | |
12098 | ||
12099 | tree type0 = TREE_TYPE (op0); | |
12100 | unsigned int prec0 = TYPE_PRECISION (type0); | |
12101 | ||
12102 | /* Left-hand operand must be signed. */ | |
12103 | if (TYPE_UNSIGNED (type0)) | |
12104 | return false; | |
12105 | ||
f4809955 | 12106 | unsigned int min_prec = (wi::min_precision (op0, SIGNED) |
12107 | + TREE_INT_CST_LOW (op1)); | |
99e82b02 | 12108 | /* Handle the case of left-shifting 1 into the sign bit. |
12109 | * However, shifting 1 _out_ of the sign bit, as in | |
12110 | * INT_MIN << 1, is considered an overflow. | |
12111 | */ | |
12112 | if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1) | |
8d669e79 | 12113 | { |
12114 | /* Never warn for C++14 onwards. */ | |
12115 | if (cxx_dialect >= cxx14) | |
12116 | return false; | |
12117 | /* Otherwise only if -Wshift-overflow=2. But return | |
12118 | true to signal an overflow for the sake of integer | |
12119 | constant expressions. */ | |
12120 | if (warn_shift_overflow < 2) | |
12121 | return true; | |
12122 | } | |
12123 | ||
8d669e79 | 12124 | bool overflowed = min_prec > prec0; |
12125 | if (overflowed && c_inhibit_evaluation_warnings == 0) | |
12126 | warning_at (loc, OPT_Wshift_overflow_, | |
12127 | "result of %qE requires %u bits to represent, " | |
12128 | "but %qT only has %u bits", | |
12129 | build2_loc (loc, LSHIFT_EXPR, type0, op0, op1), | |
12130 | min_prec, type0, prec0); | |
12131 | ||
12132 | return overflowed; | |
12133 | } | |
12134 | ||
f352a3fb | 12135 | /* The C and C++ parsers both use vectors to hold function arguments. |
12136 | For efficiency, we keep a cache of unused vectors. This is the | |
12137 | cache. */ | |
12138 | ||
f1f41a6c | 12139 | typedef vec<tree, va_gc> *tree_gc_vec; |
12140 | static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache; | |
f352a3fb | 12141 | |
12142 | /* Return a new vector from the cache. If the cache is empty, | |
12143 | allocate a new vector. These vectors are GC'ed, so it is OK if the | |
12144 | pointer is not released.. */ | |
12145 | ||
f1f41a6c | 12146 | vec<tree, va_gc> * |
f352a3fb | 12147 | make_tree_vector (void) |
12148 | { | |
f1f41a6c | 12149 | if (tree_vector_cache && !tree_vector_cache->is_empty ()) |
12150 | return tree_vector_cache->pop (); | |
f352a3fb | 12151 | else |
12152 | { | |
f1f41a6c | 12153 | /* Passing 0 to vec::alloc returns NULL, and our callers require |
f352a3fb | 12154 | that we always return a non-NULL value. The vector code uses |
12155 | 4 when growing a NULL vector, so we do too. */ | |
f1f41a6c | 12156 | vec<tree, va_gc> *v; |
12157 | vec_alloc (v, 4); | |
12158 | return v; | |
f352a3fb | 12159 | } |
12160 | } | |
12161 | ||
12162 | /* Release a vector of trees back to the cache. */ | |
12163 | ||
12164 | void | |
f1f41a6c | 12165 | release_tree_vector (vec<tree, va_gc> *vec) |
f352a3fb | 12166 | { |
12167 | if (vec != NULL) | |
12168 | { | |
f1f41a6c | 12169 | vec->truncate (0); |
12170 | vec_safe_push (tree_vector_cache, vec); | |
f352a3fb | 12171 | } |
12172 | } | |
12173 | ||
12174 | /* Get a new tree vector holding a single tree. */ | |
12175 | ||
f1f41a6c | 12176 | vec<tree, va_gc> * |
f352a3fb | 12177 | make_tree_vector_single (tree t) |
12178 | { | |
f1f41a6c | 12179 | vec<tree, va_gc> *ret = make_tree_vector (); |
12180 | ret->quick_push (t); | |
f352a3fb | 12181 | return ret; |
12182 | } | |
12183 | ||
c66c81be | 12184 | /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */ |
12185 | ||
f1f41a6c | 12186 | vec<tree, va_gc> * |
c66c81be | 12187 | make_tree_vector_from_list (tree list) |
12188 | { | |
f1f41a6c | 12189 | vec<tree, va_gc> *ret = make_tree_vector (); |
c66c81be | 12190 | for (; list; list = TREE_CHAIN (list)) |
f1f41a6c | 12191 | vec_safe_push (ret, TREE_VALUE (list)); |
c66c81be | 12192 | return ret; |
12193 | } | |
12194 | ||
f352a3fb | 12195 | /* Get a new tree vector which is a copy of an existing one. */ |
12196 | ||
f1f41a6c | 12197 | vec<tree, va_gc> * |
12198 | make_tree_vector_copy (const vec<tree, va_gc> *orig) | |
f352a3fb | 12199 | { |
f1f41a6c | 12200 | vec<tree, va_gc> *ret; |
f352a3fb | 12201 | unsigned int ix; |
12202 | tree t; | |
12203 | ||
12204 | ret = make_tree_vector (); | |
f1f41a6c | 12205 | vec_safe_reserve (ret, vec_safe_length (orig)); |
12206 | FOR_EACH_VEC_SAFE_ELT (orig, ix, t) | |
12207 | ret->quick_push (t); | |
f352a3fb | 12208 | return ret; |
12209 | } | |
12210 | ||
a9ffdd35 | 12211 | /* Return true if KEYWORD starts a type specifier. */ |
12212 | ||
12213 | bool | |
12214 | keyword_begins_type_specifier (enum rid keyword) | |
12215 | { | |
12216 | switch (keyword) | |
12217 | { | |
4fba5eb9 | 12218 | case RID_AUTO_TYPE: |
a9ffdd35 | 12219 | case RID_INT: |
12220 | case RID_CHAR: | |
12221 | case RID_FLOAT: | |
12222 | case RID_DOUBLE: | |
12223 | case RID_VOID: | |
a9ffdd35 | 12224 | case RID_UNSIGNED: |
12225 | case RID_LONG: | |
12226 | case RID_SHORT: | |
12227 | case RID_SIGNED: | |
12228 | case RID_DFLOAT32: | |
12229 | case RID_DFLOAT64: | |
12230 | case RID_DFLOAT128: | |
12231 | case RID_FRACT: | |
12232 | case RID_ACCUM: | |
12233 | case RID_BOOL: | |
12234 | case RID_WCHAR: | |
12235 | case RID_CHAR16: | |
12236 | case RID_CHAR32: | |
12237 | case RID_SAT: | |
12238 | case RID_COMPLEX: | |
12239 | case RID_TYPEOF: | |
12240 | case RID_STRUCT: | |
12241 | case RID_CLASS: | |
12242 | case RID_UNION: | |
12243 | case RID_ENUM: | |
12244 | return true; | |
12245 | default: | |
9f75f026 | 12246 | if (keyword >= RID_FIRST_INT_N |
12247 | && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS | |
12248 | && int_n_enabled_p[keyword-RID_FIRST_INT_N]) | |
12249 | return true; | |
a9ffdd35 | 12250 | return false; |
12251 | } | |
12252 | } | |
12253 | ||
12254 | /* Return true if KEYWORD names a type qualifier. */ | |
12255 | ||
12256 | bool | |
12257 | keyword_is_type_qualifier (enum rid keyword) | |
12258 | { | |
12259 | switch (keyword) | |
12260 | { | |
12261 | case RID_CONST: | |
12262 | case RID_VOLATILE: | |
12263 | case RID_RESTRICT: | |
b560fabd | 12264 | case RID_ATOMIC: |
a9ffdd35 | 12265 | return true; |
12266 | default: | |
12267 | return false; | |
12268 | } | |
12269 | } | |
12270 | ||
12271 | /* Return true if KEYWORD names a storage class specifier. | |
12272 | ||
12273 | RID_TYPEDEF is not included in this list despite `typedef' being | |
12274 | listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as | |
12275 | such for syntactic convenience only. */ | |
12276 | ||
12277 | bool | |
12278 | keyword_is_storage_class_specifier (enum rid keyword) | |
12279 | { | |
12280 | switch (keyword) | |
12281 | { | |
12282 | case RID_STATIC: | |
12283 | case RID_EXTERN: | |
12284 | case RID_REGISTER: | |
12285 | case RID_AUTO: | |
12286 | case RID_MUTABLE: | |
12287 | case RID_THREAD: | |
12288 | return true; | |
12289 | default: | |
12290 | return false; | |
12291 | } | |
12292 | } | |
12293 | ||
fad3f658 | 12294 | /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */ |
12295 | ||
12296 | static bool | |
12297 | keyword_is_function_specifier (enum rid keyword) | |
12298 | { | |
12299 | switch (keyword) | |
12300 | { | |
12301 | case RID_INLINE: | |
985c6e3a | 12302 | case RID_NORETURN: |
fad3f658 | 12303 | case RID_VIRTUAL: |
12304 | case RID_EXPLICIT: | |
12305 | return true; | |
12306 | default: | |
12307 | return false; | |
12308 | } | |
12309 | } | |
12310 | ||
12311 | /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a | |
12312 | declaration-specifier (C99 6.7). */ | |
12313 | ||
12314 | bool | |
12315 | keyword_is_decl_specifier (enum rid keyword) | |
12316 | { | |
12317 | if (keyword_is_storage_class_specifier (keyword) | |
12318 | || keyword_is_type_qualifier (keyword) | |
12319 | || keyword_is_function_specifier (keyword)) | |
12320 | return true; | |
12321 | ||
12322 | switch (keyword) | |
12323 | { | |
12324 | case RID_TYPEDEF: | |
12325 | case RID_FRIEND: | |
12326 | case RID_CONSTEXPR: | |
12327 | return true; | |
12328 | default: | |
12329 | return false; | |
12330 | } | |
12331 | } | |
12332 | ||
9b88d08d | 12333 | /* Initialize language-specific-bits of tree_contains_struct. */ |
12334 | ||
12335 | void | |
12336 | c_common_init_ts (void) | |
12337 | { | |
12338 | MARK_TS_TYPED (C_MAYBE_CONST_EXPR); | |
12339 | MARK_TS_TYPED (EXCESS_PRECISION_EXPR); | |
3c6d4197 | 12340 | MARK_TS_TYPED (ARRAY_NOTATION_REF); |
9b88d08d | 12341 | } |
12342 | ||
244db24d | 12343 | /* Build a user-defined numeric literal out of an integer constant type VALUE |
12344 | with identifier SUFFIX. */ | |
12345 | ||
12346 | tree | |
324ca377 | 12347 | build_userdef_literal (tree suffix_id, tree value, |
12348 | enum overflow_type overflow, tree num_string) | |
244db24d | 12349 | { |
12350 | tree literal = make_node (USERDEF_LITERAL); | |
12351 | USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id; | |
12352 | USERDEF_LITERAL_VALUE (literal) = value; | |
324ca377 | 12353 | USERDEF_LITERAL_OVERFLOW (literal) = overflow; |
244db24d | 12354 | USERDEF_LITERAL_NUM_STRING (literal) = num_string; |
12355 | return literal; | |
12356 | } | |
12357 | ||
7059d45d | 12358 | /* For vector[index], convert the vector to a |
c61ef207 | 12359 | pointer of the underlying type. Return true if the resulting |
12360 | ARRAY_REF should not be an lvalue. */ | |
12361 | ||
12362 | bool | |
7059d45d | 12363 | convert_vector_to_pointer_for_subscript (location_t loc, |
c61ef207 | 12364 | tree *vecp, tree index) |
7059d45d | 12365 | { |
c61ef207 | 12366 | bool ret = false; |
6290f0db | 12367 | if (VECTOR_TYPE_P (TREE_TYPE (*vecp))) |
7059d45d | 12368 | { |
12369 | tree type = TREE_TYPE (*vecp); | |
12370 | tree type1; | |
12371 | ||
c61ef207 | 12372 | ret = !lvalue_p (*vecp); |
7059d45d | 12373 | if (TREE_CODE (index) == INTEGER_CST) |
e913b5cd | 12374 | if (!tree_fits_uhwi_p (index) |
aa59f000 | 12375 | || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type)) |
7059d45d | 12376 | warning_at (loc, OPT_Warray_bounds, "index value is out of bound"); |
12377 | ||
c61ef207 | 12378 | if (ret) |
12379 | { | |
f9e245b2 | 12380 | tree tmp = create_tmp_var_raw (type); |
c61ef207 | 12381 | DECL_SOURCE_LOCATION (tmp) = loc; |
12382 | *vecp = c_save_expr (*vecp); | |
12383 | if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR) | |
12384 | { | |
12385 | bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp); | |
12386 | *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp); | |
12387 | *vecp | |
12388 | = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp, | |
12389 | *vecp, NULL_TREE, NULL_TREE), | |
12390 | non_const); | |
12391 | } | |
12392 | else | |
12393 | *vecp = build4 (TARGET_EXPR, type, tmp, *vecp, | |
12394 | NULL_TREE, NULL_TREE); | |
12395 | SET_EXPR_LOCATION (*vecp, loc); | |
12396 | c_common_mark_addressable_vec (tmp); | |
12397 | } | |
12398 | else | |
12399 | c_common_mark_addressable_vec (*vecp); | |
7059d45d | 12400 | type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); |
7059d45d | 12401 | type1 = build_pointer_type (TREE_TYPE (*vecp)); |
b2ca6510 | 12402 | bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1); |
12403 | if (!ref_all | |
12404 | && !DECL_P (*vecp)) | |
12405 | { | |
12406 | /* If the original vector isn't declared may_alias and it | |
12407 | isn't a bare vector look if the subscripting would | |
12408 | alias the vector we subscript, and if not, force ref-all. */ | |
12409 | alias_set_type vecset = get_alias_set (*vecp); | |
12410 | alias_set_type sset = get_alias_set (type); | |
12411 | if (!alias_sets_must_conflict_p (sset, vecset) | |
12412 | && !alias_set_subset_of (sset, vecset)) | |
12413 | ref_all = true; | |
12414 | } | |
12415 | type = build_pointer_type_for_mode (type, ptr_mode, ref_all); | |
7059d45d | 12416 | *vecp = build1 (ADDR_EXPR, type1, *vecp); |
12417 | *vecp = convert (type, *vecp); | |
12418 | } | |
c61ef207 | 12419 | return ret; |
7059d45d | 12420 | } |
12421 | ||
41ed701a | 12422 | /* Determine which of the operands, if any, is a scalar that needs to be |
12423 | converted to a vector, for the range of operations. */ | |
12424 | enum stv_conv | |
12425 | scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1, | |
12426 | bool complain) | |
12427 | { | |
12428 | tree type0 = TREE_TYPE (op0); | |
12429 | tree type1 = TREE_TYPE (op1); | |
12430 | bool integer_only_op = false; | |
12431 | enum stv_conv ret = stv_firstarg; | |
12432 | ||
6290f0db | 12433 | gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1)); |
41ed701a | 12434 | switch (code) |
12435 | { | |
12436 | /* Most GENERIC binary expressions require homogeneous arguments. | |
12437 | LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first | |
12438 | argument that is a vector and a second one that is a scalar, so | |
12439 | we never return stv_secondarg for them. */ | |
12440 | case RSHIFT_EXPR: | |
12441 | case LSHIFT_EXPR: | |
12442 | if (TREE_CODE (type0) == INTEGER_TYPE | |
12443 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
12444 | { | |
22a75734 | 12445 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
41ed701a | 12446 | { |
12447 | if (complain) | |
12448 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
12449 | "involves truncation", type0, type1); | |
12450 | return stv_error; | |
12451 | } | |
12452 | else | |
12453 | return stv_firstarg; | |
12454 | } | |
12455 | break; | |
12456 | ||
12457 | case BIT_IOR_EXPR: | |
12458 | case BIT_XOR_EXPR: | |
12459 | case BIT_AND_EXPR: | |
12460 | integer_only_op = true; | |
12461 | /* ... fall through ... */ | |
12462 | ||
7b463b19 | 12463 | case VEC_COND_EXPR: |
12464 | ||
41ed701a | 12465 | case PLUS_EXPR: |
12466 | case MINUS_EXPR: | |
12467 | case MULT_EXPR: | |
12468 | case TRUNC_DIV_EXPR: | |
12469 | case CEIL_DIV_EXPR: | |
12470 | case FLOOR_DIV_EXPR: | |
12471 | case ROUND_DIV_EXPR: | |
12472 | case EXACT_DIV_EXPR: | |
12473 | case TRUNC_MOD_EXPR: | |
12474 | case FLOOR_MOD_EXPR: | |
12475 | case RDIV_EXPR: | |
12476 | case EQ_EXPR: | |
12477 | case NE_EXPR: | |
12478 | case LE_EXPR: | |
12479 | case GE_EXPR: | |
12480 | case LT_EXPR: | |
12481 | case GT_EXPR: | |
12482 | /* What about UNLT_EXPR? */ | |
6290f0db | 12483 | if (VECTOR_TYPE_P (type0)) |
41ed701a | 12484 | { |
41ed701a | 12485 | ret = stv_secondarg; |
a4f59596 | 12486 | std::swap (type0, type1); |
12487 | std::swap (op0, op1); | |
41ed701a | 12488 | } |
12489 | ||
12490 | if (TREE_CODE (type0) == INTEGER_TYPE | |
12491 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
12492 | { | |
22a75734 | 12493 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
41ed701a | 12494 | { |
12495 | if (complain) | |
12496 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
12497 | "involves truncation", type0, type1); | |
12498 | return stv_error; | |
12499 | } | |
12500 | return ret; | |
12501 | } | |
12502 | else if (!integer_only_op | |
12503 | /* Allow integer --> real conversion if safe. */ | |
12504 | && (TREE_CODE (type0) == REAL_TYPE | |
12505 | || TREE_CODE (type0) == INTEGER_TYPE) | |
12506 | && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1))) | |
12507 | { | |
22a75734 | 12508 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
41ed701a | 12509 | { |
12510 | if (complain) | |
12511 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
12512 | "involves truncation", type0, type1); | |
12513 | return stv_error; | |
12514 | } | |
12515 | return ret; | |
12516 | } | |
12517 | default: | |
12518 | break; | |
12519 | } | |
12520 | ||
12521 | return stv_nothing; | |
12522 | } | |
12523 | ||
ffcdbf9c | 12524 | /* Return true iff ALIGN is an integral constant that is a fundamental |
12525 | alignment, as defined by [basic.align] in the c++-11 | |
12526 | specifications. | |
12527 | ||
12528 | That is: | |
12529 | ||
12530 | [A fundamental alignment is represented by an alignment less than or | |
12531 | equal to the greatest alignment supported by the implementation | |
12532 | in all contexts, which is equal to | |
12533 | alignof(max_align_t)]. */ | |
12534 | ||
12535 | bool | |
12536 | cxx_fundamental_alignment_p (unsigned align) | |
12537 | { | |
12538 | return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node), | |
12539 | TYPE_ALIGN (long_double_type_node))); | |
12540 | } | |
12541 | ||
46da3601 | 12542 | /* Return true if T is a pointer to a zero-sized aggregate. */ |
12543 | ||
12544 | bool | |
12545 | pointer_to_zero_sized_aggr_p (tree t) | |
12546 | { | |
12547 | if (!POINTER_TYPE_P (t)) | |
12548 | return false; | |
12549 | t = TREE_TYPE (t); | |
12550 | return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t))); | |
12551 | } | |
12552 | ||
547c6b1f | 12553 | /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function |
12554 | with no library fallback or for an ADDR_EXPR whose operand is such type | |
12555 | issues an error pointing to the location LOC. | |
12556 | Returns true when the expression has been diagnosed and false | |
12557 | otherwise. */ | |
12558 | bool | |
12559 | reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */) | |
12560 | { | |
12561 | if (TREE_CODE (expr) == ADDR_EXPR) | |
12562 | expr = TREE_OPERAND (expr, 0); | |
12563 | ||
12564 | if (TREE_TYPE (expr) | |
12565 | && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE | |
12566 | && DECL_P (expr) | |
12567 | /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids | |
12568 | false positives for user-declared built-ins such as abs or | |
12569 | strlen, and for C++ operators new and delete. | |
12570 | The c_decl_implicit() test avoids false positives for implicitly | |
12571 | declared built-ins with library fallbacks (such as abs). */ | |
12572 | && DECL_BUILT_IN (expr) | |
12573 | && DECL_IS_BUILTIN (expr) | |
12574 | && !c_decl_implicit (expr) | |
12575 | && !DECL_ASSEMBLER_NAME_SET_P (expr)) | |
12576 | { | |
12577 | if (loc == UNKNOWN_LOCATION) | |
12578 | loc = EXPR_LOC_OR_LOC (expr, input_location); | |
12579 | ||
12580 | /* Reject arguments that are built-in functions with | |
12581 | no library fallback. */ | |
12582 | error_at (loc, "built-in function %qE must be directly called", expr); | |
12583 | ||
12584 | return true; | |
12585 | } | |
12586 | ||
12587 | return false; | |
12588 | } | |
12589 | ||
ef17a71a | 12590 | /* If we're creating an if-else-if condition chain, first see if we |
12591 | already have this COND in the CHAIN. If so, warn and don't add COND | |
12592 | into the vector, otherwise add the COND there. LOC is the location | |
12593 | of COND. */ | |
12594 | ||
12595 | void | |
12596 | warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain) | |
12597 | { | |
12598 | /* No chain has been created yet. Do nothing. */ | |
12599 | if (*chain == NULL) | |
12600 | return; | |
12601 | ||
12602 | if (TREE_SIDE_EFFECTS (cond)) | |
12603 | { | |
12604 | /* Uh-oh! This condition has a side-effect, thus invalidates | |
12605 | the whole chain. */ | |
12606 | delete *chain; | |
12607 | *chain = NULL; | |
12608 | return; | |
12609 | } | |
12610 | ||
12611 | unsigned int ix; | |
12612 | tree t; | |
12613 | bool found = false; | |
12614 | FOR_EACH_VEC_ELT (**chain, ix, t) | |
12615 | if (operand_equal_p (cond, t, 0)) | |
12616 | { | |
12617 | if (warning_at (loc, OPT_Wduplicated_cond, | |
12618 | "duplicated %<if%> condition")) | |
12619 | inform (EXPR_LOCATION (t), "previously used here"); | |
12620 | found = true; | |
12621 | break; | |
12622 | } | |
12623 | ||
12624 | if (!found | |
12625 | && !CONSTANT_CLASS_P (cond) | |
12626 | /* Don't infinitely grow the chain. */ | |
12627 | && (*chain)->length () < 512) | |
12628 | (*chain)->safe_push (cond); | |
12629 | } | |
12630 | ||
81b1b2a8 | 12631 | /* Check if array size calculations overflow or if the array covers more |
12632 | than half of the address space. Return true if the size of the array | |
12633 | is valid, false otherwise. TYPE is the type of the array and NAME is | |
12634 | the name of the array, or NULL_TREE for unnamed arrays. */ | |
12635 | ||
12636 | bool | |
12637 | valid_array_size_p (location_t loc, tree type, tree name) | |
12638 | { | |
12639 | if (type != error_mark_node | |
12640 | && COMPLETE_TYPE_P (type) | |
12641 | && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST | |
12642 | && !valid_constant_size_p (TYPE_SIZE_UNIT (type))) | |
12643 | { | |
12644 | if (name) | |
12645 | error_at (loc, "size of array %qE is too large", name); | |
12646 | else | |
12647 | error_at (loc, "size of unnamed array is too large"); | |
12648 | return false; | |
12649 | } | |
12650 | return true; | |
12651 | } | |
12652 | ||
7bedc3a0 | 12653 | #include "gt-c-family-c-common.h" |