]>
Commit | Line | Data |
---|---|---|
b0fc3e72 | 1 | /* Subroutines shared by all languages that are variants of C. |
3aea1f79 | 2 | Copyright (C) 1992-2014 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 | |
20 | #include "config.h" | |
405711de | 21 | #include "system.h" |
805e22b2 | 22 | #include "coretypes.h" |
23 | #include "tm.h" | |
e48d0f41 | 24 | #include "intl.h" |
b0fc3e72 | 25 | #include "tree.h" |
9ed99284 | 26 | #include "fold-const.h" |
27 | #include "stor-layout.h" | |
28 | #include "calls.h" | |
29 | #include "stringpool.h" | |
30 | #include "attribs.h" | |
31 | #include "varasm.h" | |
32 | #include "trans-mem.h" | |
b0fc3e72 | 33 | #include "flags.h" |
a3fa7feb | 34 | #include "c-pragma.h" |
e41f0d80 | 35 | #include "c-common.h" |
6c536c4f | 36 | #include "c-objc.h" |
d8c9779c | 37 | #include "tm_p.h" |
4e91a871 | 38 | #include "obstack.h" |
a654e028 | 39 | #include "cpplib.h" |
8ee295a7 | 40 | #include "target.h" |
218e3e4e | 41 | #include "common/common-target.h" |
96554925 | 42 | #include "langhooks.h" |
f3dde807 | 43 | #include "tree-inline.h" |
69579044 | 44 | #include "toplev.h" |
7f5f3953 | 45 | #include "diagnostic.h" |
4ee9c684 | 46 | #include "tree-iterator.h" |
47 | #include "hashtab.h" | |
e08bd2f4 | 48 | #include "opts.h" |
62eec3b4 | 49 | #include "cgraph.h" |
b9fc964a | 50 | #include "target-def.h" |
a8783bee | 51 | #include "gimplify.h" |
fd6f6435 | 52 | |
90cc7820 | 53 | cpp_reader *parse_in; /* Declared in c-pragma.h. */ |
a654e028 | 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; | |
6388cfe2 | 63 | tree int128_integer_type_node; |
72040e7e | 64 | |
65 | tree short_unsigned_type_node; | |
66 | tree long_unsigned_type_node; | |
67 | tree long_long_unsigned_type_node; | |
6388cfe2 | 68 | tree int128_unsigned_type_node; |
72040e7e | 69 | |
3c2239cf | 70 | tree truthvalue_type_node; |
71 | tree truthvalue_false_node; | |
72 | tree truthvalue_true_node; | |
72040e7e | 73 | |
74 | tree ptrdiff_type_node; | |
75 | ||
76 | tree unsigned_char_type_node; | |
77 | tree signed_char_type_node; | |
78 | tree wchar_type_node; | |
72040e7e | 79 | |
924bbf02 | 80 | tree char16_type_node; |
81 | tree char32_type_node; | |
82 | ||
72040e7e | 83 | tree float_type_node; |
84 | tree double_type_node; | |
85 | tree long_double_type_node; | |
86 | ||
87 | tree complex_integer_type_node; | |
88 | tree complex_float_type_node; | |
89 | tree complex_double_type_node; | |
90 | tree complex_long_double_type_node; | |
91 | ||
c4503c0a | 92 | tree dfloat32_type_node; |
93 | tree dfloat64_type_node; | |
94 | tree_dfloat128_type_node; | |
95 | ||
72040e7e | 96 | tree intQI_type_node; |
97 | tree intHI_type_node; | |
98 | tree intSI_type_node; | |
99 | tree intDI_type_node; | |
100 | tree intTI_type_node; | |
101 | ||
102 | tree unsigned_intQI_type_node; | |
103 | tree unsigned_intHI_type_node; | |
104 | tree unsigned_intSI_type_node; | |
105 | tree unsigned_intDI_type_node; | |
106 | tree unsigned_intTI_type_node; | |
107 | ||
108 | tree widest_integer_literal_type_node; | |
109 | tree widest_unsigned_literal_type_node; | |
110 | ||
111 | Nodes for types `void *' and `const void *'. | |
112 | ||
113 | tree ptr_type_node, const_ptr_type_node; | |
114 | ||
115 | Nodes for types `char *' and `const char *'. | |
116 | ||
117 | tree string_type_node, const_string_type_node; | |
118 | ||
119 | Type `char[SOMENUMBER]'. | |
120 | Used when an array of char is needed and the size is irrelevant. | |
121 | ||
122 | tree char_array_type_node; | |
123 | ||
72040e7e | 124 | Type `wchar_t[SOMENUMBER]' or something like it. |
125 | Used when a wide string literal is created. | |
126 | ||
127 | tree wchar_array_type_node; | |
128 | ||
924bbf02 | 129 | Type `char16_t[SOMENUMBER]' or something like it. |
130 | Used when a UTF-16 string literal is created. | |
131 | ||
132 | tree char16_array_type_node; | |
133 | ||
134 | Type `char32_t[SOMENUMBER]' or something like it. | |
135 | Used when a UTF-32 string literal is created. | |
136 | ||
137 | tree char32_array_type_node; | |
138 | ||
72040e7e | 139 | Type `int ()' -- used for implicit declaration of functions. |
140 | ||
141 | tree default_function_type; | |
142 | ||
72040e7e | 143 | A VOID_TYPE node, packaged in a TREE_LIST. |
144 | ||
145 | tree void_list_node; | |
146 | ||
734c98be | 147 | The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__, |
65b7f83f | 148 | and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__ |
149 | VAR_DECLS, but C++ does.) | |
71d9fc9b | 150 | |
65b7f83f | 151 | tree function_name_decl_node; |
734c98be | 152 | tree pretty_function_name_decl_node; |
65b7f83f | 153 | tree c99_function_name_decl_node; |
154 | ||
155 | Stack of nested function name VAR_DECLs. | |
1cae46be | 156 | |
65b7f83f | 157 | tree saved_function_name_decls; |
71d9fc9b | 158 | |
72040e7e | 159 | */ |
160 | ||
161 | tree c_global_trees[CTI_MAX]; | |
573aba85 | 162 | \f |
574a6990 | 163 | /* Switches common to the C front ends. */ |
164 | ||
f7070933 | 165 | /* Nonzero means don't output line number information. */ |
166 | ||
167 | char flag_no_line_commands; | |
168 | ||
169 | /* Nonzero causes -E output not to be done, but directives such as | |
170 | #define that have side effects are still obeyed. */ | |
171 | ||
172 | char flag_no_output; | |
173 | ||
174 | /* Nonzero means dump macros in some fashion. */ | |
175 | ||
176 | char flag_dump_macros; | |
177 | ||
178 | /* Nonzero means pass #include lines through to the output. */ | |
179 | ||
180 | char flag_dump_includes; | |
181 | ||
d718b525 | 182 | /* Nonzero means process PCH files while preprocessing. */ |
183 | ||
184 | bool flag_pch_preprocess; | |
185 | ||
573aba85 | 186 | /* The file name to which we should write a precompiled header, or |
187 | NULL if no header will be written in this compile. */ | |
188 | ||
189 | const char *pch_file; | |
190 | ||
1ed9d5f5 | 191 | /* Nonzero if an ISO standard was selected. It rejects macros in the |
192 | user's namespace. */ | |
193 | int flag_iso; | |
194 | ||
574a6990 | 195 | /* C/ObjC language option variables. */ |
196 | ||
197 | ||
574a6990 | 198 | /* Nonzero means allow type mismatches in conditional expressions; |
199 | just make their values `void'. */ | |
200 | ||
201 | int flag_cond_mismatch; | |
202 | ||
203 | /* Nonzero means enable C89 Amendment 1 features. */ | |
204 | ||
205 | int flag_isoc94; | |
206 | ||
32074525 | 207 | /* Nonzero means use the ISO C99 (or C11) dialect of C. */ |
574a6990 | 208 | |
209 | int flag_isoc99; | |
210 | ||
32074525 | 211 | /* Nonzero means use the ISO C11 dialect of C. */ |
39012afb | 212 | |
32074525 | 213 | int flag_isoc11; |
39012afb | 214 | |
8b332087 | 215 | /* Nonzero means that we have builtin functions, and main is an int. */ |
574a6990 | 216 | |
217 | int flag_hosted = 1; | |
218 | ||
574a6990 | 219 | |
220 | /* ObjC language option variables. */ | |
221 | ||
222 | ||
574a6990 | 223 | /* Tells the compiler that this is a special run. Do not perform any |
224 | compiling, instead we are to test some platform dependent features | |
225 | and output a C header file with appropriate definitions. */ | |
226 | ||
227 | int print_struct_values; | |
228 | ||
f0b5f617 | 229 | /* Tells the compiler what is the constant string class for ObjC. */ |
574a6990 | 230 | |
231 | const char *constant_string_class_name; | |
232 | ||
574a6990 | 233 | |
234 | /* C++ language option variables. */ | |
235 | ||
236 | ||
574a6990 | 237 | /* Nonzero means generate separate instantiation control files and |
238 | juggle them at link time. */ | |
239 | ||
240 | int flag_use_repository; | |
241 | ||
6dcdb5de | 242 | /* The C++ dialect being used. C++98 is the default. */ |
0fe6eeac | 243 | |
6dcdb5de | 244 | enum cxx_dialect cxx_dialect = cxx98; |
0fe6eeac | 245 | |
9dcd0d49 | 246 | /* Maximum template instantiation depth. This limit exists to limit the |
8ce59854 | 247 | time it takes to notice excessively recursive template instantiations. |
574a6990 | 248 | |
8ce59854 | 249 | The default is lower than the 1024 recommended by the C++0x standard |
250 | because G++ runs out of stack before 1024 with highly recursive template | |
251 | argument deduction substitution (g++.dg/cpp0x/enum11.C). */ | |
252 | ||
253 | int max_tinst_depth = 900; | |
574a6990 | 254 | |
988fc1d1 | 255 | /* The elements of `ridpointers' are identifier nodes for the reserved |
256 | type names and storage classes. It is indexed by a RID_... value. */ | |
257 | tree *ridpointers; | |
258 | ||
e60a6f7b | 259 | tree (*make_fname_decl) (location_t, tree, int); |
9e5a737d | 260 | |
48d94ede | 261 | /* Nonzero means don't warn about problems that occur when the code is |
262 | executed. */ | |
263 | int c_inhibit_evaluation_warnings; | |
e78703c1 | 264 | |
93be21c0 | 265 | /* Whether we are building a boolean conversion inside |
266 | convert_for_assignment, or some other late binary operation. If | |
267 | build_binary_op is called for C (from code shared by C and C++) in | |
268 | this case, then the operands have already been folded and the | |
269 | result will not be folded again, so C_MAYBE_CONST_EXPR should not | |
270 | be generated. */ | |
271 | bool in_late_binary_op; | |
272 | ||
7f5f3953 | 273 | /* Whether lexing has been completed, so subsequent preprocessor |
274 | errors should use the compiler's input_location. */ | |
275 | bool done_lexing = false; | |
276 | ||
2c0e001b | 277 | /* Information about how a function name is generated. */ |
65b7f83f | 278 | struct fname_var_t |
279 | { | |
e99c3a1d | 280 | tree *const decl; /* pointer to the VAR_DECL. */ |
281 | const unsigned rid; /* RID number for the identifier. */ | |
282 | const int pretty; /* How pretty is it? */ | |
65b7f83f | 283 | }; |
284 | ||
2c0e001b | 285 | /* The three ways of getting then name of the current function. */ |
65b7f83f | 286 | |
287 | const struct fname_var_t fname_vars[] = | |
288 | { | |
2c0e001b | 289 | /* C99 compliant __func__, must be first. */ |
65b7f83f | 290 | {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0}, |
2c0e001b | 291 | /* GCC __FUNCTION__ compliant. */ |
65b7f83f | 292 | {&function_name_decl_node, RID_FUNCTION_NAME, 0}, |
2c0e001b | 293 | /* GCC __PRETTY_FUNCTION__ compliant. */ |
65b7f83f | 294 | {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1}, |
295 | {NULL, 0, 0}, | |
296 | }; | |
297 | ||
79396169 | 298 | /* Global visibility options. */ |
299 | struct visibility_flags visibility_options; | |
300 | ||
a75b1c71 | 301 | static tree c_fully_fold_internal (tree expr, bool, bool *, bool *); |
2ca392fd | 302 | static tree check_case_value (tree); |
303 | static bool check_case_bounds (tree, tree, tree *, tree *); | |
be43ff5a | 304 | |
1cae46be | 305 | static tree handle_packed_attribute (tree *, tree, tree, int, bool *); |
306 | static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *); | |
307 | static tree handle_common_attribute (tree *, tree, tree, int, bool *); | |
308 | static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *); | |
5de92639 | 309 | static tree handle_hot_attribute (tree *, tree, tree, int, bool *); |
310 | static tree handle_cold_attribute (tree *, tree, tree, int, bool *); | |
a9196da9 | 311 | static tree handle_no_sanitize_address_attribute (tree *, tree, tree, |
312 | int, bool *); | |
d413ffdd | 313 | static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree, |
314 | int, bool *); | |
05f893e1 | 315 | static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int, |
316 | bool *); | |
1cae46be | 317 | static tree handle_noinline_attribute (tree *, tree, tree, int, bool *); |
bdb1f0d1 | 318 | static tree handle_noclone_attribute (tree *, tree, tree, int, bool *); |
7bd95dfd | 319 | static tree handle_leaf_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 320 | static tree handle_always_inline_attribute (tree *, tree, tree, int, |
321 | bool *); | |
1b16fc45 | 322 | static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *); |
323 | static tree handle_artificial_attribute (tree *, tree, tree, int, bool *); | |
0cdd9887 | 324 | static tree handle_flatten_attribute (tree *, tree, tree, int, bool *); |
10fc867f | 325 | static tree handle_error_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 326 | static tree handle_used_attribute (tree *, tree, tree, int, bool *); |
327 | static tree handle_unused_attribute (tree *, tree, tree, int, bool *); | |
62eec3b4 | 328 | static tree handle_externally_visible_attribute (tree *, tree, tree, int, |
329 | bool *); | |
1cae46be | 330 | static tree handle_const_attribute (tree *, tree, tree, int, bool *); |
331 | static tree handle_transparent_union_attribute (tree *, tree, tree, | |
332 | int, bool *); | |
333 | static tree handle_constructor_attribute (tree *, tree, tree, int, bool *); | |
334 | static tree handle_destructor_attribute (tree *, tree, tree, int, bool *); | |
335 | static tree handle_mode_attribute (tree *, tree, tree, int, bool *); | |
336 | static tree handle_section_attribute (tree *, tree, tree, int, bool *); | |
337 | static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); | |
338 | static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; | |
85c0a25c | 339 | static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *); |
340 | static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *); | |
1cae46be | 341 | static tree handle_alias_attribute (tree *, tree, tree, int, bool *); |
f4a30bd7 | 342 | static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ; |
1cae46be | 343 | static tree handle_visibility_attribute (tree *, tree, tree, int, |
344 | bool *); | |
345 | static tree handle_tls_model_attribute (tree *, tree, tree, int, | |
346 | bool *); | |
347 | static tree handle_no_instrument_function_attribute (tree *, tree, | |
348 | tree, int, bool *); | |
349 | static tree handle_malloc_attribute (tree *, tree, tree, int, bool *); | |
26d1c5ff | 350 | static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 351 | static tree handle_no_limit_stack_attribute (tree *, tree, tree, int, |
352 | bool *); | |
353 | static tree handle_pure_attribute (tree *, tree, tree, int, bool *); | |
4c0315d0 | 354 | static tree handle_tm_attribute (tree *, tree, tree, int, bool *); |
355 | static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *); | |
fc09b200 | 356 | static tree handle_novops_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 357 | static tree handle_deprecated_attribute (tree *, tree, tree, int, |
358 | bool *); | |
359 | static tree handle_vector_size_attribute (tree *, tree, tree, int, | |
360 | bool *); | |
361 | static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *); | |
362 | static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *); | |
363 | static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *); | |
8a8cdb8d | 364 | static tree handle_warn_unused_result_attribute (tree *, tree, tree, int, |
365 | bool *); | |
bf6c8de0 | 366 | static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *); |
b5c26b42 | 367 | static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *); |
4a29c97c | 368 | static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *); |
237e78b1 | 369 | static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *); |
370 | static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *); | |
24470055 | 371 | static tree handle_target_attribute (tree *, tree, tree, int, bool *); |
46f8e3b0 | 372 | static tree handle_optimize_attribute (tree *, tree, tree, int, bool *); |
4c0315d0 | 373 | static tree ignore_attribute (tree *, tree, tree, int, bool *); |
48b14f50 | 374 | static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *); |
8ce86007 | 375 | static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *); |
a96c3cc1 | 376 | static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *); |
d7dcba40 | 377 | static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *); |
bc7bff74 | 378 | static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int, |
379 | bool *); | |
380 | static tree handle_omp_declare_target_attribute (tree *, tree, tree, int, | |
381 | bool *); | |
1cae46be | 382 | |
d01f58f9 | 383 | static void check_function_nonnull (tree, int, tree *); |
1cae46be | 384 | static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); |
385 | static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT); | |
386 | static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); | |
860251be | 387 | static int resort_field_decl_cmp (const void *, const void *); |
dbf6c367 | 388 | |
5c6e5756 | 389 | /* Reserved words. The third field is a mask: keywords are disabled |
390 | if they match the mask. | |
391 | ||
392 | Masks for languages: | |
393 | C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC | |
394 | C --std=c99: D_CXXONLY | D_OBJC | |
395 | ObjC is like C except that D_OBJC and D_CXX_OBJC are not set | |
396 | C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | |
397 | C++ --std=c0x: D_CONLY | D_OBJC | |
398 | ObjC++ is like C++ except that D_OBJC is not set | |
399 | ||
400 | If -fno-asm is used, D_ASM is added to the mask. If | |
401 | -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in | |
402 | C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords. | |
b27e241e | 403 | In C with -Wc++-compat, we warn if D_CXXWARN is set. |
5c6e5756 | 404 | |
b27e241e | 405 | Note the complication of the D_CXX_OBJC keywords. These are |
406 | reserved words such as 'class'. In C++, 'class' is a reserved | |
407 | word. In Objective-C++ it is too. In Objective-C, it is a | |
408 | reserved word too, but only if it follows an '@' sign. | |
409 | */ | |
5c6e5756 | 410 | const struct c_common_resword c_common_reswords[] = |
411 | { | |
83e25171 | 412 | { "_Alignas", RID_ALIGNAS, D_CONLY }, |
413 | { "_Alignof", RID_ALIGNOF, D_CONLY }, | |
b560fabd | 414 | { "_Atomic", RID_ATOMIC, D_CONLY }, |
5c6e5756 | 415 | { "_Bool", RID_BOOL, D_CONLY }, |
416 | { "_Complex", RID_COMPLEX, 0 }, | |
d037099f | 417 | { "_Cilk_spawn", RID_CILK_SPAWN, 0 }, |
418 | { "_Cilk_sync", RID_CILK_SYNC, 0 }, | |
c1800156 | 419 | { "_Imaginary", RID_IMAGINARY, D_CONLY }, |
5c6e5756 | 420 | { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT }, |
421 | { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT }, | |
422 | { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT }, | |
423 | { "_Fract", RID_FRACT, D_CONLY | D_EXT }, | |
424 | { "_Accum", RID_ACCUM, D_CONLY | D_EXT }, | |
425 | { "_Sat", RID_SAT, D_CONLY | D_EXT }, | |
f80e7755 | 426 | { "_Static_assert", RID_STATIC_ASSERT, D_CONLY }, |
985c6e3a | 427 | { "_Noreturn", RID_NORETURN, D_CONLY }, |
7aa04c8d | 428 | { "_Generic", RID_GENERIC, D_CONLY }, |
d184e0c0 | 429 | { "_Thread_local", RID_THREAD, D_CONLY }, |
5c6e5756 | 430 | { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, |
431 | { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, | |
432 | { "__alignof", RID_ALIGNOF, 0 }, | |
433 | { "__alignof__", RID_ALIGNOF, 0 }, | |
434 | { "__asm", RID_ASM, 0 }, | |
435 | { "__asm__", RID_ASM, 0 }, | |
436 | { "__attribute", RID_ATTRIBUTE, 0 }, | |
437 | { "__attribute__", RID_ATTRIBUTE, 0 }, | |
4fba5eb9 | 438 | { "__auto_type", RID_AUTO_TYPE, D_CONLY }, |
e6014a82 | 439 | { "__bases", RID_BASES, D_CXXONLY }, |
5c6e5756 | 440 | { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, |
bff4ad11 | 441 | { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY }, |
bf0cb017 | 442 | { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 }, |
5c6e5756 | 443 | { "__builtin_offsetof", RID_OFFSETOF, 0 }, |
444 | { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, | |
445 | { "__builtin_va_arg", RID_VA_ARG, 0 }, | |
446 | { "__complex", RID_COMPLEX, 0 }, | |
447 | { "__complex__", RID_COMPLEX, 0 }, | |
448 | { "__const", RID_CONST, 0 }, | |
449 | { "__const__", RID_CONST, 0 }, | |
450 | { "__decltype", RID_DECLTYPE, D_CXXONLY }, | |
e6014a82 | 451 | { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY }, |
5c6e5756 | 452 | { "__extension__", RID_EXTENSION, 0 }, |
453 | { "__func__", RID_C99_FUNCTION_NAME, 0 }, | |
454 | { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY }, | |
455 | { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY }, | |
456 | { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY }, | |
457 | { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY }, | |
458 | { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY }, | |
459 | { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY }, | |
460 | { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY }, | |
461 | { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY }, | |
23407dc9 | 462 | { "__imag", RID_IMAGPART, 0 }, |
463 | { "__imag__", RID_IMAGPART, 0 }, | |
464 | { "__inline", RID_INLINE, 0 }, | |
465 | { "__inline__", RID_INLINE, 0 }, | |
6388cfe2 | 466 | { "__int128", RID_INT128, 0 }, |
5c6e5756 | 467 | { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY }, |
468 | { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY }, | |
469 | { "__is_class", RID_IS_CLASS, D_CXXONLY }, | |
470 | { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY }, | |
471 | { "__is_empty", RID_IS_EMPTY, D_CXXONLY }, | |
472 | { "__is_enum", RID_IS_ENUM, D_CXXONLY }, | |
aa4313eb | 473 | { "__is_final", RID_IS_FINAL, D_CXXONLY }, |
23407dc9 | 474 | { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY }, |
5c6e5756 | 475 | { "__is_pod", RID_IS_POD, D_CXXONLY }, |
476 | { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY }, | |
c1c67b4f | 477 | { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY }, |
478 | { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY }, | |
5c6e5756 | 479 | { "__is_union", RID_IS_UNION, D_CXXONLY }, |
5c6e5756 | 480 | { "__label__", RID_LABEL, 0 }, |
481 | { "__null", RID_NULL, 0 }, | |
482 | { "__real", RID_REALPART, 0 }, | |
483 | { "__real__", RID_REALPART, 0 }, | |
484 | { "__restrict", RID_RESTRICT, 0 }, | |
485 | { "__restrict__", RID_RESTRICT, 0 }, | |
486 | { "__signed", RID_SIGNED, 0 }, | |
487 | { "__signed__", RID_SIGNED, 0 }, | |
488 | { "__thread", RID_THREAD, 0 }, | |
4c0315d0 | 489 | { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 }, |
490 | { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 }, | |
491 | { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 }, | |
5c6e5756 | 492 | { "__typeof", RID_TYPEOF, 0 }, |
493 | { "__typeof__", RID_TYPEOF, 0 }, | |
23407dc9 | 494 | { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY }, |
5c6e5756 | 495 | { "__volatile", RID_VOLATILE, 0 }, |
496 | { "__volatile__", RID_VOLATILE, 0 }, | |
ffcdbf9c | 497 | { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
e463efd7 | 498 | { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 499 | { "asm", RID_ASM, D_ASM }, |
500 | { "auto", RID_AUTO, 0 }, | |
dbd982c9 | 501 | { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 502 | { "break", RID_BREAK, 0 }, |
503 | { "case", RID_CASE, 0 }, | |
51030405 | 504 | { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN }, |
5c6e5756 | 505 | { "char", RID_CHAR, 0 }, |
51030405 | 506 | { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
507 | { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
508 | { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 509 | { "const", RID_CONST, 0 }, |
17814aca | 510 | { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 511 | { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN }, |
512 | { "continue", RID_CONTINUE, 0 }, | |
51030405 | 513 | { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 514 | { "default", RID_DEFAULT, 0 }, |
51030405 | 515 | { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 516 | { "do", RID_DO, 0 }, |
517 | { "double", RID_DOUBLE, 0 }, | |
518 | { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN }, | |
519 | { "else", RID_ELSE, 0 }, | |
520 | { "enum", RID_ENUM, 0 }, | |
51030405 | 521 | { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN }, |
522 | { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 523 | { "extern", RID_EXTERN, 0 }, |
51030405 | 524 | { "false", RID_FALSE, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 525 | { "float", RID_FLOAT, 0 }, |
526 | { "for", RID_FOR, 0 }, | |
51030405 | 527 | { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 528 | { "goto", RID_GOTO, 0 }, |
529 | { "if", RID_IF, 0 }, | |
530 | { "inline", RID_INLINE, D_EXT89 }, | |
531 | { "int", RID_INT, 0 }, | |
532 | { "long", RID_LONG, 0 }, | |
533 | { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN }, | |
51030405 | 534 | { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN }, |
535 | { "new", RID_NEW, D_CXXONLY | D_CXXWARN }, | |
98fe9664 | 536 | { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
6fe11077 | 537 | { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
51030405 | 538 | { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN }, |
539 | { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN }, | |
540 | { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN }, | |
541 | { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 542 | { "register", RID_REGISTER, 0 }, |
543 | { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN }, | |
544 | { "restrict", RID_RESTRICT, D_CONLY | D_C99 }, | |
545 | { "return", RID_RETURN, 0 }, | |
546 | { "short", RID_SHORT, 0 }, | |
547 | { "signed", RID_SIGNED, 0 }, | |
548 | { "sizeof", RID_SIZEOF, 0 }, | |
549 | { "static", RID_STATIC, 0 }, | |
550 | { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
551 | { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN }, | |
552 | { "struct", RID_STRUCT, 0 }, | |
553 | { "switch", RID_SWITCH, 0 }, | |
51030405 | 554 | { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN }, |
555 | { "this", RID_THIS, D_CXXONLY | D_CXXWARN }, | |
3740094c | 556 | { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
51030405 | 557 | { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN }, |
558 | { "true", RID_TRUE, D_CXXONLY | D_CXXWARN }, | |
559 | { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 560 | { "typedef", RID_TYPEDEF, 0 }, |
51030405 | 561 | { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN }, |
562 | { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 563 | { "typeof", RID_TYPEOF, D_ASM | D_EXT }, |
564 | { "union", RID_UNION, 0 }, | |
565 | { "unsigned", RID_UNSIGNED, 0 }, | |
51030405 | 566 | { "using", RID_USING, D_CXXONLY | D_CXXWARN }, |
567 | { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 568 | { "void", RID_VOID, 0 }, |
569 | { "volatile", RID_VOLATILE, 0 }, | |
570 | { "wchar_t", RID_WCHAR, D_CXXONLY }, | |
571 | { "while", RID_WHILE, 0 }, | |
572 | /* These Objective-C keywords are recognized only immediately after | |
573 | an '@'. */ | |
574 | { "compatibility_alias", RID_AT_ALIAS, D_OBJC }, | |
575 | { "defs", RID_AT_DEFS, D_OBJC }, | |
576 | { "encode", RID_AT_ENCODE, D_OBJC }, | |
577 | { "end", RID_AT_END, D_OBJC }, | |
578 | { "implementation", RID_AT_IMPLEMENTATION, D_OBJC }, | |
579 | { "interface", RID_AT_INTERFACE, D_OBJC }, | |
580 | { "protocol", RID_AT_PROTOCOL, D_OBJC }, | |
581 | { "selector", RID_AT_SELECTOR, D_OBJC }, | |
582 | { "finally", RID_AT_FINALLY, D_OBJC }, | |
583 | { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC }, | |
069761fb | 584 | { "optional", RID_AT_OPTIONAL, D_OBJC }, |
585 | { "required", RID_AT_REQUIRED, D_OBJC }, | |
86c110ac | 586 | { "property", RID_AT_PROPERTY, D_OBJC }, |
4a8875ed | 587 | { "package", RID_AT_PACKAGE, D_OBJC }, |
e1f293c0 | 588 | { "synthesize", RID_AT_SYNTHESIZE, D_OBJC }, |
589 | { "dynamic", RID_AT_DYNAMIC, D_OBJC }, | |
5c6e5756 | 590 | /* These are recognized only in protocol-qualifier context |
591 | (see above) */ | |
592 | { "bycopy", RID_BYCOPY, D_OBJC }, | |
593 | { "byref", RID_BYREF, D_OBJC }, | |
594 | { "in", RID_IN, D_OBJC }, | |
595 | { "inout", RID_INOUT, D_OBJC }, | |
596 | { "oneway", RID_ONEWAY, D_OBJC }, | |
597 | { "out", RID_OUT, D_OBJC }, | |
86c110ac | 598 | /* These are recognized inside a property attribute list */ |
7590f0e5 | 599 | { "assign", RID_ASSIGN, D_OBJC }, |
600 | { "copy", RID_COPY, D_OBJC }, | |
86c110ac | 601 | { "getter", RID_GETTER, D_OBJC }, |
7590f0e5 | 602 | { "nonatomic", RID_NONATOMIC, D_OBJC }, |
603 | { "readonly", RID_READONLY, D_OBJC }, | |
604 | { "readwrite", RID_READWRITE, D_OBJC }, | |
605 | { "retain", RID_RETAIN, D_OBJC }, | |
86c110ac | 606 | { "setter", RID_SETTER, D_OBJC }, |
5c6e5756 | 607 | }; |
608 | ||
609 | const unsigned int num_c_common_reswords = | |
610 | sizeof c_common_reswords / sizeof (struct c_common_resword); | |
611 | ||
f8e93a2e | 612 | /* Table of machine-independent attributes common to all C-like languages. */ |
613 | const struct attribute_spec c_common_attribute_table[] = | |
614 | { | |
ac86af5d | 615 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
616 | affects_type_identity } */ | |
f8e93a2e | 617 | { "packed", 0, 0, false, false, false, |
ac86af5d | 618 | handle_packed_attribute , false}, |
f8e93a2e | 619 | { "nocommon", 0, 0, true, false, false, |
ac86af5d | 620 | handle_nocommon_attribute, false}, |
f8e93a2e | 621 | { "common", 0, 0, true, false, false, |
ac86af5d | 622 | handle_common_attribute, false }, |
f8e93a2e | 623 | /* FIXME: logically, noreturn attributes should be listed as |
624 | "false, true, true" and apply to function types. But implementing this | |
625 | would require all the places in the compiler that use TREE_THIS_VOLATILE | |
626 | on a decl to identify non-returning functions to be located and fixed | |
627 | to check the function type instead. */ | |
628 | { "noreturn", 0, 0, true, false, false, | |
ac86af5d | 629 | handle_noreturn_attribute, false }, |
f8e93a2e | 630 | { "volatile", 0, 0, true, false, false, |
ac86af5d | 631 | handle_noreturn_attribute, false }, |
f8e93a2e | 632 | { "noinline", 0, 0, true, false, false, |
ac86af5d | 633 | handle_noinline_attribute, false }, |
bdb1f0d1 | 634 | { "noclone", 0, 0, true, false, false, |
ac86af5d | 635 | handle_noclone_attribute, false }, |
7bd95dfd | 636 | { "leaf", 0, 0, true, false, false, |
ac86af5d | 637 | handle_leaf_attribute, false }, |
f8e93a2e | 638 | { "always_inline", 0, 0, true, false, false, |
ac86af5d | 639 | handle_always_inline_attribute, false }, |
541e4101 | 640 | { "gnu_inline", 0, 0, true, false, false, |
ac86af5d | 641 | handle_gnu_inline_attribute, false }, |
1b16fc45 | 642 | { "artificial", 0, 0, true, false, false, |
ac86af5d | 643 | handle_artificial_attribute, false }, |
0cdd9887 | 644 | { "flatten", 0, 0, true, false, false, |
ac86af5d | 645 | handle_flatten_attribute, false }, |
f8e93a2e | 646 | { "used", 0, 0, true, false, false, |
ac86af5d | 647 | handle_used_attribute, false }, |
f8e93a2e | 648 | { "unused", 0, 0, false, false, false, |
ac86af5d | 649 | handle_unused_attribute, false }, |
62eec3b4 | 650 | { "externally_visible", 0, 0, true, false, false, |
ac86af5d | 651 | handle_externally_visible_attribute, false }, |
f8e93a2e | 652 | /* The same comments as for noreturn attributes apply to const ones. */ |
653 | { "const", 0, 0, true, false, false, | |
ac86af5d | 654 | handle_const_attribute, false }, |
f8e93a2e | 655 | { "transparent_union", 0, 0, false, false, false, |
ac86af5d | 656 | handle_transparent_union_attribute, false }, |
9af7fd5b | 657 | { "constructor", 0, 1, true, false, false, |
ac86af5d | 658 | handle_constructor_attribute, false }, |
9af7fd5b | 659 | { "destructor", 0, 1, true, false, false, |
ac86af5d | 660 | handle_destructor_attribute, false }, |
f8e93a2e | 661 | { "mode", 1, 1, false, true, false, |
ac86af5d | 662 | handle_mode_attribute, false }, |
f8e93a2e | 663 | { "section", 1, 1, true, false, false, |
ac86af5d | 664 | handle_section_attribute, false }, |
f8e93a2e | 665 | { "aligned", 0, 1, false, false, false, |
ac86af5d | 666 | handle_aligned_attribute, false }, |
f8e93a2e | 667 | { "weak", 0, 0, true, false, false, |
ac86af5d | 668 | handle_weak_attribute, false }, |
85c0a25c | 669 | { "ifunc", 1, 1, true, false, false, |
ac86af5d | 670 | handle_ifunc_attribute, false }, |
f8e93a2e | 671 | { "alias", 1, 1, true, false, false, |
ac86af5d | 672 | handle_alias_attribute, false }, |
f4a30bd7 | 673 | { "weakref", 0, 1, true, false, false, |
ac86af5d | 674 | handle_weakref_attribute, false }, |
f8e93a2e | 675 | { "no_instrument_function", 0, 0, true, false, false, |
ac86af5d | 676 | handle_no_instrument_function_attribute, |
677 | false }, | |
f8e93a2e | 678 | { "malloc", 0, 0, true, false, false, |
ac86af5d | 679 | handle_malloc_attribute, false }, |
26d1c5ff | 680 | { "returns_twice", 0, 0, true, false, false, |
ac86af5d | 681 | handle_returns_twice_attribute, false }, |
f8e93a2e | 682 | { "no_stack_limit", 0, 0, true, false, false, |
ac86af5d | 683 | handle_no_limit_stack_attribute, false }, |
f8e93a2e | 684 | { "pure", 0, 0, true, false, false, |
ac86af5d | 685 | handle_pure_attribute, false }, |
4c0315d0 | 686 | { "transaction_callable", 0, 0, false, true, false, |
687 | handle_tm_attribute, false }, | |
688 | { "transaction_unsafe", 0, 0, false, true, false, | |
689 | handle_tm_attribute, false }, | |
690 | { "transaction_safe", 0, 0, false, true, false, | |
691 | handle_tm_attribute, false }, | |
692 | { "transaction_may_cancel_outer", 0, 0, false, true, false, | |
693 | handle_tm_attribute, false }, | |
694 | /* ??? These two attributes didn't make the transition from the | |
695 | Intel language document to the multi-vendor language document. */ | |
696 | { "transaction_pure", 0, 0, false, true, false, | |
697 | handle_tm_attribute, false }, | |
698 | { "transaction_wrap", 1, 1, true, false, false, | |
699 | handle_tm_wrap_attribute, false }, | |
fc09b200 | 700 | /* For internal use (marking of builtins) only. The name contains space |
701 | to prevent its usage in source code. */ | |
702 | { "no vops", 0, 0, true, false, false, | |
ac86af5d | 703 | handle_novops_attribute, false }, |
45c4e798 | 704 | { "deprecated", 0, 1, false, false, false, |
ac86af5d | 705 | handle_deprecated_attribute, false }, |
f8e93a2e | 706 | { "vector_size", 1, 1, false, true, false, |
ac86af5d | 707 | handle_vector_size_attribute, false }, |
b212f378 | 708 | { "visibility", 1, 1, false, false, false, |
ac86af5d | 709 | handle_visibility_attribute, false }, |
24dfead4 | 710 | { "tls_model", 1, 1, true, false, false, |
ac86af5d | 711 | handle_tls_model_attribute, false }, |
dbf6c367 | 712 | { "nonnull", 0, -1, false, true, true, |
ac86af5d | 713 | handle_nonnull_attribute, false }, |
fa987697 | 714 | { "nothrow", 0, 0, true, false, false, |
ac86af5d | 715 | handle_nothrow_attribute, false }, |
716 | { "may_alias", 0, 0, false, true, false, NULL, false }, | |
7acb29a3 | 717 | { "cleanup", 1, 1, true, false, false, |
ac86af5d | 718 | handle_cleanup_attribute, false }, |
8a8cdb8d | 719 | { "warn_unused_result", 0, 0, false, true, true, |
ac86af5d | 720 | handle_warn_unused_result_attribute, false }, |
50ca527f | 721 | { "sentinel", 0, 1, false, true, true, |
ac86af5d | 722 | handle_sentinel_attribute, false }, |
b5c26b42 | 723 | /* For internal use (marking of builtins) only. The name contains space |
724 | to prevent its usage in source code. */ | |
725 | { "type generic", 0, 0, false, true, true, | |
ac86af5d | 726 | handle_type_generic_attribute, false }, |
4a29c97c | 727 | { "alloc_size", 1, 2, false, true, true, |
ac86af5d | 728 | handle_alloc_size_attribute, false }, |
5de92639 | 729 | { "cold", 0, 0, true, false, false, |
ac86af5d | 730 | handle_cold_attribute, false }, |
5de92639 | 731 | { "hot", 0, 0, true, false, false, |
ac86af5d | 732 | handle_hot_attribute, false }, |
d413ffdd | 733 | { "no_address_safety_analysis", |
734 | 0, 0, true, false, false, | |
735 | handle_no_address_safety_analysis_attribute, | |
736 | false }, | |
a9196da9 | 737 | { "no_sanitize_address", 0, 0, true, false, false, |
738 | handle_no_sanitize_address_attribute, | |
739 | false }, | |
05f893e1 | 740 | { "no_sanitize_undefined", 0, 0, true, false, false, |
741 | handle_no_sanitize_undefined_attribute, | |
742 | false }, | |
10fc867f | 743 | { "warning", 1, 1, true, false, false, |
ac86af5d | 744 | handle_error_attribute, false }, |
10fc867f | 745 | { "error", 1, 1, true, false, false, |
ac86af5d | 746 | handle_error_attribute, false }, |
24470055 | 747 | { "target", 1, -1, true, false, false, |
ac86af5d | 748 | handle_target_attribute, false }, |
46f8e3b0 | 749 | { "optimize", 1, -1, true, false, false, |
ac86af5d | 750 | handle_optimize_attribute, false }, |
4c0315d0 | 751 | /* For internal use only. The leading '*' both prevents its usage in |
752 | source code and signals that it may be overridden by machine tables. */ | |
753 | { "*tm regparm", 0, 0, false, true, true, | |
754 | ignore_attribute, false }, | |
48b14f50 | 755 | { "no_split_stack", 0, 0, true, false, false, |
ac86af5d | 756 | handle_no_split_stack_attribute, false }, |
8ce86007 | 757 | /* For internal use (marking of builtins and runtime functions) only. |
758 | The name contains space to prevent its usage in source code. */ | |
759 | { "fn spec", 1, 1, false, true, true, | |
ac86af5d | 760 | handle_fnspec_attribute, false }, |
a96c3cc1 | 761 | { "warn_unused", 0, 0, false, false, false, |
762 | handle_warn_unused_attribute, false }, | |
d7dcba40 | 763 | { "returns_nonnull", 0, 0, false, true, true, |
764 | handle_returns_nonnull_attribute, false }, | |
bc7bff74 | 765 | { "omp declare simd", 0, -1, true, false, false, |
766 | handle_omp_declare_simd_attribute, false }, | |
74acc703 | 767 | { "cilk simd function", 0, -1, true, false, false, |
768 | handle_omp_declare_simd_attribute, false }, | |
bc7bff74 | 769 | { "omp declare target", 0, 0, true, false, false, |
770 | handle_omp_declare_target_attribute, false }, | |
237e78b1 | 771 | { "alloc_align", 1, 1, false, true, true, |
772 | handle_alloc_align_attribute, false }, | |
773 | { "assume_aligned", 1, 2, false, true, true, | |
774 | handle_assume_aligned_attribute, false }, | |
ac86af5d | 775 | { NULL, 0, 0, false, false, false, NULL, false } |
f8e93a2e | 776 | }; |
777 | ||
778 | /* Give the specifications for the format attributes, used by C and all | |
d716ce75 | 779 | descendants. */ |
f8e93a2e | 780 | |
781 | const struct attribute_spec c_common_format_attribute_table[] = | |
782 | { | |
ac86af5d | 783 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
784 | affects_type_identity } */ | |
f8e93a2e | 785 | { "format", 3, 3, false, true, true, |
ac86af5d | 786 | handle_format_attribute, false }, |
f8e93a2e | 787 | { "format_arg", 1, 1, false, true, true, |
ac86af5d | 788 | handle_format_arg_attribute, false }, |
789 | { NULL, 0, 0, false, false, false, NULL, false } | |
f8e93a2e | 790 | }; |
791 | ||
6d5d708e | 792 | /* Return identifier for address space AS. */ |
34208e18 | 793 | |
6d5d708e | 794 | const char * |
795 | c_addr_space_name (addr_space_t as) | |
796 | { | |
34208e18 | 797 | int rid = RID_FIRST_ADDR_SPACE + as; |
798 | gcc_assert (ridpointers [rid]); | |
799 | return IDENTIFIER_POINTER (ridpointers [rid]); | |
6d5d708e | 800 | } |
801 | ||
2c0e001b | 802 | /* Push current bindings for the function name VAR_DECLS. */ |
f4e3c278 | 803 | |
804 | void | |
1cae46be | 805 | start_fname_decls (void) |
f4e3c278 | 806 | { |
65b7f83f | 807 | unsigned ix; |
808 | tree saved = NULL_TREE; | |
1cae46be | 809 | |
65b7f83f | 810 | for (ix = 0; fname_vars[ix].decl; ix++) |
811 | { | |
812 | tree decl = *fname_vars[ix].decl; | |
f4e3c278 | 813 | |
65b7f83f | 814 | if (decl) |
815 | { | |
ceb7b692 | 816 | saved = tree_cons (decl, build_int_cst (integer_type_node, ix), |
817 | saved); | |
65b7f83f | 818 | *fname_vars[ix].decl = NULL_TREE; |
819 | } | |
820 | } | |
821 | if (saved || saved_function_name_decls) | |
822 | /* Normally they'll have been NULL, so only push if we've got a | |
823 | stack, or they are non-NULL. */ | |
824 | saved_function_name_decls = tree_cons (saved, NULL_TREE, | |
825 | saved_function_name_decls); | |
826 | } | |
827 | ||
2363ef00 | 828 | /* Finish up the current bindings, adding them into the current function's |
829 | statement tree. This must be done _before_ finish_stmt_tree is called. | |
830 | If there is no current function, we must be at file scope and no statements | |
831 | are involved. Pop the previous bindings. */ | |
65b7f83f | 832 | |
833 | void | |
1cae46be | 834 | finish_fname_decls (void) |
65b7f83f | 835 | { |
836 | unsigned ix; | |
2363ef00 | 837 | tree stmts = NULL_TREE; |
65b7f83f | 838 | tree stack = saved_function_name_decls; |
839 | ||
840 | for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack)) | |
2363ef00 | 841 | append_to_statement_list (TREE_VALUE (stack), &stmts); |
1cae46be | 842 | |
2363ef00 | 843 | if (stmts) |
65b7f83f | 844 | { |
2363ef00 | 845 | tree *bodyp = &DECL_SAVED_TREE (current_function_decl); |
5c423bd6 | 846 | |
2363ef00 | 847 | if (TREE_CODE (*bodyp) == BIND_EXPR) |
848 | bodyp = &BIND_EXPR_BODY (*bodyp); | |
81010c97 | 849 | |
bc2b76e0 | 850 | append_to_statement_list_force (*bodyp, &stmts); |
2363ef00 | 851 | *bodyp = stmts; |
65b7f83f | 852 | } |
1cae46be | 853 | |
65b7f83f | 854 | for (ix = 0; fname_vars[ix].decl; ix++) |
855 | *fname_vars[ix].decl = NULL_TREE; | |
1cae46be | 856 | |
65b7f83f | 857 | if (stack) |
f4e3c278 | 858 | { |
2c0e001b | 859 | /* We had saved values, restore them. */ |
65b7f83f | 860 | tree saved; |
861 | ||
862 | for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved)) | |
863 | { | |
864 | tree decl = TREE_PURPOSE (saved); | |
865 | unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved)); | |
1cae46be | 866 | |
65b7f83f | 867 | *fname_vars[ix].decl = decl; |
868 | } | |
869 | stack = TREE_CHAIN (stack); | |
f4e3c278 | 870 | } |
65b7f83f | 871 | saved_function_name_decls = stack; |
872 | } | |
873 | ||
81010c97 | 874 | /* Return the text name of the current function, suitably prettified |
5fc7fa69 | 875 | by PRETTY_P. Return string must be freed by caller. */ |
65b7f83f | 876 | |
877 | const char * | |
1cae46be | 878 | fname_as_string (int pretty_p) |
65b7f83f | 879 | { |
9ad4bb1e | 880 | const char *name = "top level"; |
5fc7fa69 | 881 | char *namep; |
8115b8be | 882 | int vrb = 2, len; |
883 | cpp_string cstr = { 0, 0 }, strname; | |
9ad4bb1e | 884 | |
84166705 | 885 | if (!pretty_p) |
9ad4bb1e | 886 | { |
887 | name = ""; | |
888 | vrb = 0; | |
889 | } | |
890 | ||
891 | if (current_function_decl) | |
dc24ddbd | 892 | name = lang_hooks.decl_printable_name (current_function_decl, vrb); |
9ad4bb1e | 893 | |
8115b8be | 894 | len = strlen (name) + 3; /* Two for '"'s. One for NULL. */ |
5fc7fa69 | 895 | |
8115b8be | 896 | namep = XNEWVEC (char, len); |
897 | snprintf (namep, len, "\"%s\"", name); | |
898 | strname.text = (unsigned char *) namep; | |
899 | strname.len = len - 1; | |
5fc7fa69 | 900 | |
924bbf02 | 901 | if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING)) |
8115b8be | 902 | { |
903 | XDELETEVEC (namep); | |
904 | return (const char *) cstr.text; | |
5fc7fa69 | 905 | } |
5fc7fa69 | 906 | |
907 | return namep; | |
65b7f83f | 908 | } |
909 | ||
65b7f83f | 910 | /* Return the VAR_DECL for a const char array naming the current |
911 | function. If the VAR_DECL has not yet been created, create it | |
912 | now. RID indicates how it should be formatted and IDENTIFIER_NODE | |
913 | ID is its name (unfortunately C and C++ hold the RID values of | |
914 | keywords in different places, so we can't derive RID from ID in | |
e3b80d49 | 915 | this language independent code. LOC is the location of the |
916 | function. */ | |
65b7f83f | 917 | |
918 | tree | |
e3b80d49 | 919 | fname_decl (location_t loc, unsigned int rid, tree id) |
65b7f83f | 920 | { |
921 | unsigned ix; | |
922 | tree decl = NULL_TREE; | |
923 | ||
924 | for (ix = 0; fname_vars[ix].decl; ix++) | |
925 | if (fname_vars[ix].rid == rid) | |
926 | break; | |
927 | ||
928 | decl = *fname_vars[ix].decl; | |
929 | if (!decl) | |
f4e3c278 | 930 | { |
2222b3c6 | 931 | /* If a tree is built here, it would normally have the lineno of |
932 | the current statement. Later this tree will be moved to the | |
933 | beginning of the function and this line number will be wrong. | |
934 | To avoid this problem set the lineno to 0 here; that prevents | |
7299020b | 935 | it from appearing in the RTL. */ |
2363ef00 | 936 | tree stmts; |
9a6486a6 | 937 | location_t saved_location = input_location; |
9a6486a6 | 938 | input_location = UNKNOWN_LOCATION; |
1cae46be | 939 | |
2363ef00 | 940 | stmts = push_stmt_list (); |
e60a6f7b | 941 | decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty); |
2363ef00 | 942 | stmts = pop_stmt_list (stmts); |
943 | if (!IS_EMPTY_STMT (stmts)) | |
944 | saved_function_name_decls | |
945 | = tree_cons (decl, stmts, saved_function_name_decls); | |
65b7f83f | 946 | *fname_vars[ix].decl = decl; |
9a6486a6 | 947 | input_location = saved_location; |
f4e3c278 | 948 | } |
65b7f83f | 949 | if (!ix && !current_function_decl) |
e3b80d49 | 950 | pedwarn (loc, 0, "%qD is not defined outside of function scope", decl); |
81010c97 | 951 | |
65b7f83f | 952 | return decl; |
f4e3c278 | 953 | } |
954 | ||
070236f0 | 955 | /* Given a STRING_CST, give it a suitable array-of-chars data type. */ |
b0fc3e72 | 956 | |
957 | tree | |
1cae46be | 958 | fix_string_type (tree value) |
b0fc3e72 | 959 | { |
070236f0 | 960 | int length = TREE_STRING_LENGTH (value); |
961 | int nchars; | |
00d26680 | 962 | tree e_type, i_type, a_type; |
963 | ||
73be5127 | 964 | /* Compute the number of elements, for the array type. */ |
924bbf02 | 965 | if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value)) |
966 | { | |
967 | nchars = length; | |
968 | e_type = char_type_node; | |
969 | } | |
970 | else if (TREE_TYPE (value) == char16_array_type_node) | |
971 | { | |
972 | nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT); | |
973 | e_type = char16_type_node; | |
974 | } | |
975 | else if (TREE_TYPE (value) == char32_array_type_node) | |
976 | { | |
977 | nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT); | |
978 | e_type = char32_type_node; | |
979 | } | |
980 | else | |
981 | { | |
982 | nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT); | |
983 | e_type = wchar_type_node; | |
984 | } | |
b0fc3e72 | 985 | |
1d752508 | 986 | /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous |
987 | limit in C++98 Annex B is very large (65536) and is not normative, | |
988 | so we do not diagnose it (warn_overlength_strings is forced off | |
989 | in c_common_post_options). */ | |
990 | if (warn_overlength_strings) | |
991 | { | |
992 | const int nchars_max = flag_isoc99 ? 4095 : 509; | |
993 | const int relevant_std = flag_isoc99 ? 99 : 90; | |
994 | if (nchars - 1 > nchars_max) | |
995 | /* Translators: The %d after 'ISO C' will be 90 or 99. Do not | |
996 | separate the %d from the 'C'. 'ISO' should not be | |
997 | translated, but it may be moved after 'C%d' in languages | |
998 | where modifiers follow nouns. */ | |
21ca8540 | 999 | pedwarn (input_location, OPT_Woverlength_strings, |
8864917d | 1000 | "string length %qd is greater than the length %qd " |
1d752508 | 1001 | "ISO C%d compilers are required to support", |
1002 | nchars - 1, nchars_max, relevant_std); | |
1003 | } | |
82cfc7f7 | 1004 | |
390be14e | 1005 | /* Create the array type for the string constant. The ISO C++ |
1006 | standard says that a string literal has type `const char[N]' or | |
1007 | `const wchar_t[N]'. We use the same logic when invoked as a C | |
1008 | front-end with -Wwrite-strings. | |
1009 | ??? We should change the type of an expression depending on the | |
1010 | state of a warning flag. We should just be warning -- see how | |
1011 | this is handled in the C++ front-end for the deprecated implicit | |
1012 | conversion from string literals to `char*' or `wchar_t*'. | |
00d26680 | 1013 | |
1014 | The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified | |
1015 | array type being the unqualified version of that type. | |
1016 | Therefore, if we are constructing an array of const char, we must | |
1017 | construct the matching unqualified array type first. The C front | |
1018 | end does not require this, but it does no harm, so we do it | |
1019 | unconditionally. */ | |
ceb7b692 | 1020 | i_type = build_index_type (size_int (nchars - 1)); |
00d26680 | 1021 | a_type = build_array_type (e_type, i_type); |
390be14e | 1022 | if (c_dialect_cxx() || warn_write_strings) |
aebc8537 | 1023 | a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST); |
3a10ba35 | 1024 | |
00d26680 | 1025 | TREE_TYPE (value) = a_type; |
b8e3b7ad | 1026 | TREE_CONSTANT (value) = 1; |
a814bad5 | 1027 | TREE_READONLY (value) = 1; |
b0fc3e72 | 1028 | TREE_STATIC (value) = 1; |
1029 | return value; | |
1030 | } | |
1031 | \f | |
fc501191 | 1032 | /* If DISABLE is true, stop issuing warnings. This is used when |
1033 | parsing code that we know will not be executed. This function may | |
1034 | be called multiple times, and works as a stack. */ | |
1035 | ||
1036 | static void | |
1037 | c_disable_warnings (bool disable) | |
1038 | { | |
1039 | if (disable) | |
1040 | { | |
1041 | ++c_inhibit_evaluation_warnings; | |
1042 | fold_defer_overflow_warnings (); | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | /* If ENABLE is true, reenable issuing warnings. */ | |
1047 | ||
1048 | static void | |
1049 | c_enable_warnings (bool enable) | |
1050 | { | |
1051 | if (enable) | |
1052 | { | |
1053 | --c_inhibit_evaluation_warnings; | |
1054 | fold_undefer_and_ignore_overflow_warnings (); | |
1055 | } | |
1056 | } | |
1057 | ||
a75b1c71 | 1058 | /* Fully fold EXPR, an expression that was not folded (beyond integer |
1059 | constant expressions and null pointer constants) when being built | |
1060 | up. If IN_INIT, this is in a static initializer and certain | |
1061 | changes are made to the folding done. Clear *MAYBE_CONST if | |
1062 | MAYBE_CONST is not NULL and EXPR is definitely not a constant | |
1063 | expression because it contains an evaluated operator (in C99) or an | |
1064 | operator outside of sizeof returning an integer constant (in C90) | |
1065 | not permitted in constant expressions, or because it contains an | |
1066 | evaluated arithmetic overflow. (*MAYBE_CONST should typically be | |
1067 | set to true by callers before calling this function.) Return the | |
1068 | folded expression. Function arguments have already been folded | |
1069 | before calling this function, as have the contents of SAVE_EXPR, | |
1070 | TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and | |
1071 | C_MAYBE_CONST_EXPR. */ | |
1072 | ||
1073 | tree | |
1074 | c_fully_fold (tree expr, bool in_init, bool *maybe_const) | |
1075 | { | |
1076 | tree ret; | |
c6418a4e | 1077 | tree eptype = NULL_TREE; |
a75b1c71 | 1078 | bool dummy = true; |
1079 | bool maybe_const_itself = true; | |
389dd41b | 1080 | location_t loc = EXPR_LOCATION (expr); |
a75b1c71 | 1081 | |
1082 | /* This function is not relevant to C++ because C++ folds while | |
1083 | parsing, and may need changes to be correct for C++ when C++ | |
1084 | stops folding while parsing. */ | |
1085 | if (c_dialect_cxx ()) | |
1086 | gcc_unreachable (); | |
1087 | ||
1088 | if (!maybe_const) | |
1089 | maybe_const = &dummy; | |
c6418a4e | 1090 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) |
1091 | { | |
1092 | eptype = TREE_TYPE (expr); | |
1093 | expr = TREE_OPERAND (expr, 0); | |
1094 | } | |
a75b1c71 | 1095 | ret = c_fully_fold_internal (expr, in_init, maybe_const, |
1096 | &maybe_const_itself); | |
c6418a4e | 1097 | if (eptype) |
389dd41b | 1098 | ret = fold_convert_loc (loc, eptype, ret); |
a75b1c71 | 1099 | *maybe_const &= maybe_const_itself; |
1100 | return ret; | |
1101 | } | |
1102 | ||
1103 | /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for | |
1104 | c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands | |
1105 | not permitted, while *MAYBE_CONST_ITSELF is cleared because of | |
1106 | arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from | |
1107 | both evaluated and unevaluated subexpressions while | |
1108 | *MAYBE_CONST_ITSELF is carried from only evaluated | |
1109 | subexpressions). */ | |
1110 | ||
1111 | static tree | |
1112 | c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands, | |
1113 | bool *maybe_const_itself) | |
1114 | { | |
1115 | tree ret = expr; | |
1116 | enum tree_code code = TREE_CODE (expr); | |
1117 | enum tree_code_class kind = TREE_CODE_CLASS (code); | |
1118 | location_t loc = EXPR_LOCATION (expr); | |
1119 | tree op0, op1, op2, op3; | |
1120 | tree orig_op0, orig_op1, orig_op2; | |
1121 | bool op0_const = true, op1_const = true, op2_const = true; | |
1122 | bool op0_const_self = true, op1_const_self = true, op2_const_self = true; | |
1123 | bool nowarning = TREE_NO_WARNING (expr); | |
fc501191 | 1124 | bool unused_p; |
a75b1c71 | 1125 | |
1126 | /* This function is not relevant to C++ because C++ folds while | |
1127 | parsing, and may need changes to be correct for C++ when C++ | |
1128 | stops folding while parsing. */ | |
1129 | if (c_dialect_cxx ()) | |
1130 | gcc_unreachable (); | |
1131 | ||
1132 | /* Constants, declarations, statements, errors, SAVE_EXPRs and | |
1133 | anything else not counted as an expression cannot usefully be | |
1134 | folded further at this point. */ | |
1135 | if (!IS_EXPR_CODE_CLASS (kind) | |
1136 | || kind == tcc_statement | |
1137 | || code == SAVE_EXPR) | |
1138 | return expr; | |
1139 | ||
1140 | /* Operands of variable-length expressions (function calls) have | |
1141 | already been folded, as have __builtin_* function calls, and such | |
1142 | expressions cannot occur in constant expressions. */ | |
1143 | if (kind == tcc_vl_exp) | |
1144 | { | |
1145 | *maybe_const_operands = false; | |
1146 | ret = fold (expr); | |
1147 | goto out; | |
1148 | } | |
1149 | ||
1150 | if (code == C_MAYBE_CONST_EXPR) | |
1151 | { | |
1152 | tree pre = C_MAYBE_CONST_EXPR_PRE (expr); | |
1153 | tree inner = C_MAYBE_CONST_EXPR_EXPR (expr); | |
1154 | if (C_MAYBE_CONST_EXPR_NON_CONST (expr)) | |
1155 | *maybe_const_operands = false; | |
1156 | if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr)) | |
1157 | *maybe_const_itself = false; | |
1158 | if (pre && !in_init) | |
1159 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner); | |
1160 | else | |
1161 | ret = inner; | |
1162 | goto out; | |
1163 | } | |
1164 | ||
1165 | /* Assignment, increment, decrement, function call and comma | |
1166 | operators, and statement expressions, cannot occur in constant | |
1167 | expressions if evaluated / outside of sizeof. (Function calls | |
1168 | were handled above, though VA_ARG_EXPR is treated like a function | |
1169 | call here, and statement expressions are handled through | |
1170 | C_MAYBE_CONST_EXPR to avoid folding inside them.) */ | |
1171 | switch (code) | |
1172 | { | |
1173 | case MODIFY_EXPR: | |
1174 | case PREDECREMENT_EXPR: | |
1175 | case PREINCREMENT_EXPR: | |
1176 | case POSTDECREMENT_EXPR: | |
1177 | case POSTINCREMENT_EXPR: | |
1178 | case COMPOUND_EXPR: | |
1179 | *maybe_const_operands = false; | |
1180 | break; | |
1181 | ||
1182 | case VA_ARG_EXPR: | |
1183 | case TARGET_EXPR: | |
1184 | case BIND_EXPR: | |
1185 | case OBJ_TYPE_REF: | |
1186 | *maybe_const_operands = false; | |
1187 | ret = fold (expr); | |
1188 | goto out; | |
1189 | ||
1190 | default: | |
1191 | break; | |
1192 | } | |
1193 | ||
1194 | /* Fold individual tree codes as appropriate. */ | |
1195 | switch (code) | |
1196 | { | |
1197 | case COMPOUND_LITERAL_EXPR: | |
1198 | /* Any non-constancy will have been marked in a containing | |
1199 | C_MAYBE_CONST_EXPR; there is no more folding to do here. */ | |
1200 | goto out; | |
1201 | ||
1202 | case COMPONENT_REF: | |
1203 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1204 | op1 = TREE_OPERAND (expr, 1); | |
1205 | op2 = TREE_OPERAND (expr, 2); | |
1206 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1207 | maybe_const_itself); | |
f59e3889 | 1208 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1209 | if (op0 != orig_op0) |
1210 | ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2); | |
1211 | if (ret != expr) | |
1212 | { | |
1213 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1214 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1215 | } | |
1216 | goto out; | |
1217 | ||
1218 | case ARRAY_REF: | |
1219 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1220 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1221 | op2 = TREE_OPERAND (expr, 2); | |
1222 | op3 = TREE_OPERAND (expr, 3); | |
1223 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1224 | maybe_const_itself); | |
f59e3889 | 1225 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1226 | op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, |
1227 | maybe_const_itself); | |
f59e3889 | 1228 | STRIP_TYPE_NOPS (op1); |
a75b1c71 | 1229 | op1 = decl_constant_value_for_optimization (op1); |
1230 | if (op0 != orig_op0 || op1 != orig_op1) | |
1231 | ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3); | |
1232 | if (ret != expr) | |
1233 | { | |
1234 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1235 | TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); | |
1236 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1237 | } | |
1238 | ret = fold (ret); | |
1239 | goto out; | |
1240 | ||
1241 | case COMPOUND_EXPR: | |
1242 | case MODIFY_EXPR: | |
1243 | case PREDECREMENT_EXPR: | |
1244 | case PREINCREMENT_EXPR: | |
1245 | case POSTDECREMENT_EXPR: | |
1246 | case POSTINCREMENT_EXPR: | |
1247 | case PLUS_EXPR: | |
1248 | case MINUS_EXPR: | |
1249 | case MULT_EXPR: | |
1250 | case POINTER_PLUS_EXPR: | |
1251 | case TRUNC_DIV_EXPR: | |
1252 | case CEIL_DIV_EXPR: | |
1253 | case FLOOR_DIV_EXPR: | |
1254 | case TRUNC_MOD_EXPR: | |
1255 | case RDIV_EXPR: | |
1256 | case EXACT_DIV_EXPR: | |
1257 | case LSHIFT_EXPR: | |
1258 | case RSHIFT_EXPR: | |
1259 | case BIT_IOR_EXPR: | |
1260 | case BIT_XOR_EXPR: | |
1261 | case BIT_AND_EXPR: | |
1262 | case LT_EXPR: | |
1263 | case LE_EXPR: | |
1264 | case GT_EXPR: | |
1265 | case GE_EXPR: | |
1266 | case EQ_EXPR: | |
1267 | case NE_EXPR: | |
1268 | case COMPLEX_EXPR: | |
1269 | case TRUTH_AND_EXPR: | |
1270 | case TRUTH_OR_EXPR: | |
1271 | case TRUTH_XOR_EXPR: | |
1272 | case UNORDERED_EXPR: | |
1273 | case ORDERED_EXPR: | |
1274 | case UNLT_EXPR: | |
1275 | case UNLE_EXPR: | |
1276 | case UNGT_EXPR: | |
1277 | case UNGE_EXPR: | |
1278 | case UNEQ_EXPR: | |
1279 | /* Binary operations evaluating both arguments (increment and | |
1280 | decrement are binary internally in GCC). */ | |
1281 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1282 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1283 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1284 | maybe_const_itself); | |
f59e3889 | 1285 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1286 | if (code != MODIFY_EXPR |
1287 | && code != PREDECREMENT_EXPR | |
1288 | && code != PREINCREMENT_EXPR | |
1289 | && code != POSTDECREMENT_EXPR | |
1290 | && code != POSTINCREMENT_EXPR) | |
1291 | op0 = decl_constant_value_for_optimization (op0); | |
1292 | /* The RHS of a MODIFY_EXPR was fully folded when building that | |
1293 | expression for the sake of conversion warnings. */ | |
1294 | if (code != MODIFY_EXPR) | |
1295 | op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, | |
1296 | maybe_const_itself); | |
f59e3889 | 1297 | STRIP_TYPE_NOPS (op1); |
a75b1c71 | 1298 | op1 = decl_constant_value_for_optimization (op1); |
1299 | if (op0 != orig_op0 || op1 != orig_op1 || in_init) | |
1300 | ret = in_init | |
389dd41b | 1301 | ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) |
1302 | : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); | |
a75b1c71 | 1303 | else |
1304 | ret = fold (expr); | |
672d914b | 1305 | if (TREE_OVERFLOW_P (ret) |
1306 | && !TREE_OVERFLOW_P (op0) | |
1307 | && !TREE_OVERFLOW_P (op1)) | |
1308 | overflow_warning (EXPR_LOCATION (expr), ret); | |
7c834436 | 1309 | if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR) |
1310 | && TREE_CODE (orig_op1) != INTEGER_CST | |
1311 | && TREE_CODE (op1) == INTEGER_CST | |
1312 | && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE | |
1313 | || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE) | |
1314 | && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE | |
1315 | && c_inhibit_evaluation_warnings == 0) | |
1316 | { | |
1317 | if (tree_int_cst_sgn (op1) < 0) | |
1318 | warning_at (loc, 0, (code == LSHIFT_EXPR | |
1319 | ? G_("left shift count is negative") | |
1320 | : G_("right shift count is negative"))); | |
1321 | else if (compare_tree_int (op1, | |
1322 | TYPE_PRECISION (TREE_TYPE (orig_op0))) | |
1323 | >= 0) | |
1324 | warning_at (loc, 0, (code == LSHIFT_EXPR | |
1325 | ? G_("left shift count >= width of type") | |
1326 | : G_("right shift count >= width of type"))); | |
1327 | } | |
a75b1c71 | 1328 | goto out; |
1329 | ||
1330 | case INDIRECT_REF: | |
1331 | case FIX_TRUNC_EXPR: | |
1332 | case FLOAT_EXPR: | |
1333 | CASE_CONVERT: | |
e66325ea | 1334 | case ADDR_SPACE_CONVERT_EXPR: |
2b19dfe4 | 1335 | case VIEW_CONVERT_EXPR: |
a75b1c71 | 1336 | case NON_LVALUE_EXPR: |
1337 | case NEGATE_EXPR: | |
1338 | case BIT_NOT_EXPR: | |
1339 | case TRUTH_NOT_EXPR: | |
1340 | case ADDR_EXPR: | |
1341 | case CONJ_EXPR: | |
1342 | case REALPART_EXPR: | |
1343 | case IMAGPART_EXPR: | |
1344 | /* Unary operations. */ | |
1345 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1346 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1347 | maybe_const_itself); | |
f59e3889 | 1348 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1349 | if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR) |
1350 | op0 = decl_constant_value_for_optimization (op0); | |
737a23cc | 1351 | /* ??? Cope with user tricks that amount to offsetof. The middle-end is |
1352 | not prepared to deal with them if they occur in initializers. */ | |
1353 | if (op0 != orig_op0 | |
1354 | && code == ADDR_EXPR | |
1355 | && (op1 = get_base_address (op0)) != NULL_TREE | |
1356 | && TREE_CODE (op1) == INDIRECT_REF | |
1357 | && TREE_CONSTANT (TREE_OPERAND (op1, 0))) | |
7549df0d | 1358 | ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0)); |
737a23cc | 1359 | else if (op0 != orig_op0 || in_init) |
a75b1c71 | 1360 | ret = in_init |
389dd41b | 1361 | ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0) |
1362 | : fold_build1_loc (loc, code, TREE_TYPE (expr), op0); | |
a75b1c71 | 1363 | else |
1364 | ret = fold (expr); | |
1365 | if (code == INDIRECT_REF | |
1366 | && ret != expr | |
1367 | && TREE_CODE (ret) == INDIRECT_REF) | |
1368 | { | |
1369 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1370 | TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); | |
1371 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1372 | } | |
672d914b | 1373 | switch (code) |
1374 | { | |
1375 | case FIX_TRUNC_EXPR: | |
1376 | case FLOAT_EXPR: | |
1377 | CASE_CONVERT: | |
1378 | /* Don't warn about explicit conversions. We will already | |
1379 | have warned about suspect implicit conversions. */ | |
1380 | break; | |
1381 | ||
1382 | default: | |
1383 | if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0)) | |
1384 | overflow_warning (EXPR_LOCATION (expr), ret); | |
1385 | break; | |
1386 | } | |
a75b1c71 | 1387 | goto out; |
1388 | ||
1389 | case TRUTH_ANDIF_EXPR: | |
1390 | case TRUTH_ORIF_EXPR: | |
1391 | /* Binary operations not necessarily evaluating both | |
1392 | arguments. */ | |
1393 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1394 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1395 | op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); | |
f59e3889 | 1396 | STRIP_TYPE_NOPS (op0); |
672d914b | 1397 | |
1398 | unused_p = (op0 == (code == TRUTH_ANDIF_EXPR | |
1399 | ? truthvalue_false_node | |
1400 | : truthvalue_true_node)); | |
fc501191 | 1401 | c_disable_warnings (unused_p); |
a75b1c71 | 1402 | op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); |
f59e3889 | 1403 | STRIP_TYPE_NOPS (op1); |
fc501191 | 1404 | c_enable_warnings (unused_p); |
672d914b | 1405 | |
a75b1c71 | 1406 | if (op0 != orig_op0 || op1 != orig_op1 || in_init) |
1407 | ret = in_init | |
389dd41b | 1408 | ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) |
1409 | : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); | |
a75b1c71 | 1410 | else |
1411 | ret = fold (expr); | |
1412 | *maybe_const_operands &= op0_const; | |
1413 | *maybe_const_itself &= op0_const_self; | |
1414 | if (!(flag_isoc99 | |
1415 | && op0_const | |
1416 | && op0_const_self | |
1417 | && (code == TRUTH_ANDIF_EXPR | |
1418 | ? op0 == truthvalue_false_node | |
1419 | : op0 == truthvalue_true_node))) | |
1420 | *maybe_const_operands &= op1_const; | |
1421 | if (!(op0_const | |
1422 | && op0_const_self | |
1423 | && (code == TRUTH_ANDIF_EXPR | |
1424 | ? op0 == truthvalue_false_node | |
1425 | : op0 == truthvalue_true_node))) | |
1426 | *maybe_const_itself &= op1_const_self; | |
1427 | goto out; | |
1428 | ||
1429 | case COND_EXPR: | |
1430 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1431 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1432 | orig_op2 = op2 = TREE_OPERAND (expr, 2); | |
1433 | op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); | |
672d914b | 1434 | |
f59e3889 | 1435 | STRIP_TYPE_NOPS (op0); |
fc501191 | 1436 | c_disable_warnings (op0 == truthvalue_false_node); |
a75b1c71 | 1437 | op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); |
f59e3889 | 1438 | STRIP_TYPE_NOPS (op1); |
fc501191 | 1439 | c_enable_warnings (op0 == truthvalue_false_node); |
672d914b | 1440 | |
fc501191 | 1441 | c_disable_warnings (op0 == truthvalue_true_node); |
a75b1c71 | 1442 | op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self); |
f59e3889 | 1443 | STRIP_TYPE_NOPS (op2); |
fc501191 | 1444 | c_enable_warnings (op0 == truthvalue_true_node); |
672d914b | 1445 | |
a75b1c71 | 1446 | if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2) |
389dd41b | 1447 | ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2); |
a75b1c71 | 1448 | else |
1449 | ret = fold (expr); | |
1450 | *maybe_const_operands &= op0_const; | |
1451 | *maybe_const_itself &= op0_const_self; | |
1452 | if (!(flag_isoc99 | |
1453 | && op0_const | |
1454 | && op0_const_self | |
1455 | && op0 == truthvalue_false_node)) | |
1456 | *maybe_const_operands &= op1_const; | |
1457 | if (!(op0_const | |
1458 | && op0_const_self | |
1459 | && op0 == truthvalue_false_node)) | |
1460 | *maybe_const_itself &= op1_const_self; | |
1461 | if (!(flag_isoc99 | |
1462 | && op0_const | |
1463 | && op0_const_self | |
1464 | && op0 == truthvalue_true_node)) | |
1465 | *maybe_const_operands &= op2_const; | |
1466 | if (!(op0_const | |
1467 | && op0_const_self | |
1468 | && op0 == truthvalue_true_node)) | |
1469 | *maybe_const_itself &= op2_const_self; | |
1470 | goto out; | |
1471 | ||
c6418a4e | 1472 | case EXCESS_PRECISION_EXPR: |
1473 | /* Each case where an operand with excess precision may be | |
1474 | encountered must remove the EXCESS_PRECISION_EXPR around | |
1475 | inner operands and possibly put one around the whole | |
1476 | expression or possibly convert to the semantic type (which | |
1477 | c_fully_fold does); we cannot tell at this stage which is | |
1478 | appropriate in any particular case. */ | |
1479 | gcc_unreachable (); | |
1480 | ||
a75b1c71 | 1481 | default: |
1482 | /* Various codes may appear through folding built-in functions | |
1483 | and their arguments. */ | |
1484 | goto out; | |
1485 | } | |
1486 | ||
1487 | out: | |
1488 | /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks | |
1489 | have been done by this point, so remove them again. */ | |
1490 | nowarning |= TREE_NO_WARNING (ret); | |
1491 | STRIP_TYPE_NOPS (ret); | |
1492 | if (nowarning && !TREE_NO_WARNING (ret)) | |
1493 | { | |
1494 | if (!CAN_HAVE_LOCATION_P (ret)) | |
1495 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
1496 | TREE_NO_WARNING (ret) = 1; | |
1497 | } | |
1498 | if (ret != expr) | |
1499 | protected_set_expr_location (ret, loc); | |
1500 | return ret; | |
1501 | } | |
1502 | ||
1503 | /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type, | |
1504 | return EXP. Otherwise, return either EXP or its known constant | |
1505 | value (if it has one), but return EXP if EXP has mode BLKmode. ??? | |
1506 | Is the BLKmode test appropriate? */ | |
1507 | ||
1508 | tree | |
1509 | decl_constant_value_for_optimization (tree exp) | |
1510 | { | |
1511 | tree ret; | |
1512 | ||
1513 | /* This function is only used by C, for c_fully_fold and other | |
1514 | optimization, and may not be correct for C++. */ | |
1515 | if (c_dialect_cxx ()) | |
1516 | gcc_unreachable (); | |
1517 | ||
1518 | if (!optimize | |
1519 | || TREE_CODE (exp) != VAR_DECL | |
1520 | || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE | |
1521 | || DECL_MODE (exp) == BLKmode) | |
1522 | return exp; | |
1523 | ||
1524 | ret = decl_constant_value (exp); | |
1525 | /* Avoid unwanted tree sharing between the initializer and current | |
1526 | function's body where the tree can be modified e.g. by the | |
1527 | gimplifier. */ | |
1528 | if (ret != exp && TREE_STATIC (exp)) | |
1529 | ret = unshare_expr (ret); | |
1530 | return ret; | |
1531 | } | |
1532 | ||
2a1736ed | 1533 | /* Print a warning if a constant expression had overflow in folding. |
1534 | Invoke this function on every expression that the language | |
1535 | requires to be a constant expression. | |
1536 | Note the ANSI C standard says it is erroneous for a | |
1537 | constant expression to overflow. */ | |
b2806639 | 1538 | |
1539 | void | |
1cae46be | 1540 | constant_expression_warning (tree value) |
07317e69 | 1541 | { |
48e1416a | 1542 | if (warn_overflow && pedantic |
07317e69 | 1543 | && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
1544 | || TREE_CODE (value) == FIXED_CST | |
1545 | || TREE_CODE (value) == VECTOR_CST | |
1546 | || TREE_CODE (value) == COMPLEX_CST) | |
1547 | && TREE_OVERFLOW (value)) | |
21ca8540 | 1548 | pedwarn (input_location, OPT_Woverflow, "overflow in constant expression"); |
07317e69 | 1549 | } |
1550 | ||
1551 | /* The same as above but print an unconditional error. */ | |
1552 | void | |
1553 | constant_expression_error (tree value) | |
b2806639 | 1554 | { |
837e1122 | 1555 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
9421ebb9 | 1556 | || TREE_CODE (value) == FIXED_CST |
886cfd4f | 1557 | || TREE_CODE (value) == VECTOR_CST |
837e1122 | 1558 | || TREE_CODE (value) == COMPLEX_CST) |
07317e69 | 1559 | && TREE_OVERFLOW (value)) |
1560 | error ("overflow in constant expression"); | |
2a1736ed | 1561 | } |
1562 | ||
f170d67f | 1563 | /* Print a warning if an expression had overflow in folding and its |
1564 | operands hadn't. | |
1565 | ||
2a1736ed | 1566 | Invoke this function on every expression that |
1567 | (1) appears in the source code, and | |
f170d67f | 1568 | (2) is a constant expression that overflowed, and |
2a1736ed | 1569 | (3) is not already checked by convert_and_check; |
f170d67f | 1570 | however, do not invoke this function on operands of explicit casts |
1571 | or when the expression is the result of an operator and any operand | |
1572 | already overflowed. */ | |
2a1736ed | 1573 | |
1574 | void | |
e60a6f7b | 1575 | overflow_warning (location_t loc, tree value) |
2a1736ed | 1576 | { |
48d94ede | 1577 | if (c_inhibit_evaluation_warnings != 0) |
1578 | return; | |
f170d67f | 1579 | |
1580 | switch (TREE_CODE (value)) | |
886cfd4f | 1581 | { |
f170d67f | 1582 | case INTEGER_CST: |
e60a6f7b | 1583 | warning_at (loc, OPT_Woverflow, "integer overflow in expression"); |
f170d67f | 1584 | break; |
48e1416a | 1585 | |
f170d67f | 1586 | case REAL_CST: |
e60a6f7b | 1587 | warning_at (loc, OPT_Woverflow, |
1588 | "floating point overflow in expression"); | |
f170d67f | 1589 | break; |
48e1416a | 1590 | |
9421ebb9 | 1591 | case FIXED_CST: |
e60a6f7b | 1592 | warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression"); |
9421ebb9 | 1593 | break; |
1594 | ||
f170d67f | 1595 | case VECTOR_CST: |
e60a6f7b | 1596 | warning_at (loc, OPT_Woverflow, "vector overflow in expression"); |
f170d67f | 1597 | break; |
48e1416a | 1598 | |
f170d67f | 1599 | case COMPLEX_CST: |
1600 | if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) | |
e60a6f7b | 1601 | warning_at (loc, OPT_Woverflow, |
1602 | "complex integer overflow in expression"); | |
f170d67f | 1603 | else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) |
e60a6f7b | 1604 | warning_at (loc, OPT_Woverflow, |
1605 | "complex floating point overflow in expression"); | |
f170d67f | 1606 | break; |
1607 | ||
1608 | default: | |
1609 | break; | |
886cfd4f | 1610 | } |
2a1736ed | 1611 | } |
1612 | ||
03033af4 | 1613 | /* Warn about uses of logical || / && operator in a context where it |
1614 | is likely that the bitwise equivalent was intended by the | |
1615 | programmer. We have seen an expression in which CODE is a binary | |
9c20c4fc | 1616 | operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding |
1617 | had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ | |
b13d1547 | 1618 | void |
9c20c4fc | 1619 | warn_logical_operator (location_t location, enum tree_code code, tree type, |
48e1416a | 1620 | enum tree_code code_left, tree op_left, |
03033af4 | 1621 | enum tree_code ARG_UNUSED (code_right), tree op_right) |
b13d1547 | 1622 | { |
9c20c4fc | 1623 | int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); |
1624 | int in0_p, in1_p, in_p; | |
1625 | tree low0, low1, low, high0, high1, high, lhs, rhs, tem; | |
1626 | bool strict_overflow_p = false; | |
1627 | ||
03033af4 | 1628 | if (code != TRUTH_ANDIF_EXPR |
1629 | && code != TRUTH_AND_EXPR | |
1630 | && code != TRUTH_ORIF_EXPR | |
1631 | && code != TRUTH_OR_EXPR) | |
1632 | return; | |
1633 | ||
1634 | /* Warn if &&/|| are being used in a context where it is | |
1635 | likely that the bitwise equivalent was intended by the | |
1636 | programmer. That is, an expression such as op && MASK | |
1637 | where op should not be any boolean expression, nor a | |
1638 | constant, and mask seems to be a non-boolean integer constant. */ | |
1639 | if (!truth_value_p (code_left) | |
1640 | && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) | |
1641 | && !CONSTANT_CLASS_P (op_left) | |
1642 | && !TREE_NO_WARNING (op_left) | |
1643 | && TREE_CODE (op_right) == INTEGER_CST | |
1644 | && !integer_zerop (op_right) | |
1645 | && !integer_onep (op_right)) | |
b13d1547 | 1646 | { |
9c20c4fc | 1647 | if (or_op) |
03033af4 | 1648 | warning_at (location, OPT_Wlogical_op, "logical %<or%>" |
1649 | " applied to non-boolean constant"); | |
1650 | else | |
1651 | warning_at (location, OPT_Wlogical_op, "logical %<and%>" | |
1652 | " applied to non-boolean constant"); | |
1653 | TREE_NO_WARNING (op_left) = true; | |
9c20c4fc | 1654 | return; |
1655 | } | |
1656 | ||
1657 | /* We do not warn for constants because they are typical of macro | |
1658 | expansions that test for features. */ | |
1659 | if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right)) | |
1660 | return; | |
1661 | ||
1662 | /* This warning only makes sense with logical operands. */ | |
1663 | if (!(truth_value_p (TREE_CODE (op_left)) | |
1664 | || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) | |
1665 | || !(truth_value_p (TREE_CODE (op_right)) | |
1666 | || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) | |
1667 | return; | |
1668 | ||
9c20c4fc | 1669 | |
686369e8 | 1670 | /* We first test whether either side separately is trivially true |
1671 | (with OR) or trivially false (with AND). If so, do not warn. | |
1672 | This is a common idiom for testing ranges of data types in | |
1673 | portable code. */ | |
1674 | lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); | |
1675 | if (!lhs) | |
1676 | return; | |
1677 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) | |
9c20c4fc | 1678 | lhs = C_MAYBE_CONST_EXPR_EXPR (lhs); |
1679 | ||
686369e8 | 1680 | /* If this is an OR operation, invert both sides; now, the result |
1681 | should be always false to get a warning. */ | |
1682 | if (or_op) | |
1683 | in0_p = !in0_p; | |
1684 | ||
1685 | tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); | |
d42e7c5a | 1686 | if (tem && integer_zerop (tem)) |
686369e8 | 1687 | return; |
1688 | ||
1689 | rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); | |
1690 | if (!rhs) | |
1691 | return; | |
1692 | if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) | |
9c20c4fc | 1693 | rhs = C_MAYBE_CONST_EXPR_EXPR (rhs); |
48e1416a | 1694 | |
686369e8 | 1695 | /* If this is an OR operation, invert both sides; now, the result |
1696 | should be always false to get a warning. */ | |
9c20c4fc | 1697 | if (or_op) |
686369e8 | 1698 | in1_p = !in1_p; |
48e1416a | 1699 | |
686369e8 | 1700 | tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); |
d42e7c5a | 1701 | if (tem && integer_zerop (tem)) |
686369e8 | 1702 | return; |
1703 | ||
1704 | /* If both expressions have the same operand, if we can merge the | |
1705 | ranges, and if the range test is always false, then warn. */ | |
1706 | if (operand_equal_p (lhs, rhs, 0) | |
9c20c4fc | 1707 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, |
1708 | in1_p, low1, high1) | |
389dd41b | 1709 | && 0 != (tem = build_range_check (UNKNOWN_LOCATION, |
686369e8 | 1710 | type, lhs, in_p, low, high)) |
1711 | && integer_zerop (tem)) | |
9c20c4fc | 1712 | { |
9c20c4fc | 1713 | if (or_op) |
1714 | warning_at (location, OPT_Wlogical_op, | |
1715 | "logical %<or%> " | |
1716 | "of collectively exhaustive tests is always true"); | |
1717 | else | |
1718 | warning_at (location, OPT_Wlogical_op, | |
1719 | "logical %<and%> " | |
1720 | "of mutually exclusive tests is always false"); | |
b13d1547 | 1721 | } |
1722 | } | |
1723 | ||
1724 | ||
3d177e8c | 1725 | /* Warn if EXP contains any computations whose results are not used. |
1726 | Return true if a warning is printed; false otherwise. LOCUS is the | |
1727 | (potential) location of the expression. */ | |
1728 | ||
1729 | bool | |
1730 | warn_if_unused_value (const_tree exp, location_t locus) | |
1731 | { | |
1732 | restart: | |
1733 | if (TREE_USED (exp) || TREE_NO_WARNING (exp)) | |
1734 | return false; | |
1735 | ||
1736 | /* Don't warn about void constructs. This includes casting to void, | |
1737 | void function calls, and statement expressions with a final cast | |
1738 | to void. */ | |
1739 | if (VOID_TYPE_P (TREE_TYPE (exp))) | |
1740 | return false; | |
1741 | ||
1742 | if (EXPR_HAS_LOCATION (exp)) | |
1743 | locus = EXPR_LOCATION (exp); | |
1744 | ||
1745 | switch (TREE_CODE (exp)) | |
1746 | { | |
1747 | case PREINCREMENT_EXPR: | |
1748 | case POSTINCREMENT_EXPR: | |
1749 | case PREDECREMENT_EXPR: | |
1750 | case POSTDECREMENT_EXPR: | |
1751 | case MODIFY_EXPR: | |
1752 | case INIT_EXPR: | |
1753 | case TARGET_EXPR: | |
1754 | case CALL_EXPR: | |
1755 | case TRY_CATCH_EXPR: | |
1756 | case WITH_CLEANUP_EXPR: | |
1757 | case EXIT_EXPR: | |
1758 | case VA_ARG_EXPR: | |
1759 | return false; | |
1760 | ||
1761 | case BIND_EXPR: | |
1762 | /* For a binding, warn if no side effect within it. */ | |
1763 | exp = BIND_EXPR_BODY (exp); | |
1764 | goto restart; | |
1765 | ||
1766 | case SAVE_EXPR: | |
1767 | case NON_LVALUE_EXPR: | |
d85dbdb3 | 1768 | case NOP_EXPR: |
3d177e8c | 1769 | exp = TREE_OPERAND (exp, 0); |
1770 | goto restart; | |
1771 | ||
1772 | case TRUTH_ORIF_EXPR: | |
1773 | case TRUTH_ANDIF_EXPR: | |
1774 | /* In && or ||, warn if 2nd operand has no side effect. */ | |
1775 | exp = TREE_OPERAND (exp, 1); | |
1776 | goto restart; | |
1777 | ||
1778 | case COMPOUND_EXPR: | |
1779 | if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) | |
1780 | return true; | |
1781 | /* Let people do `(foo (), 0)' without a warning. */ | |
1782 | if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) | |
1783 | return false; | |
1784 | exp = TREE_OPERAND (exp, 1); | |
1785 | goto restart; | |
1786 | ||
1787 | case COND_EXPR: | |
1788 | /* If this is an expression with side effects, don't warn; this | |
1789 | case commonly appears in macro expansions. */ | |
1790 | if (TREE_SIDE_EFFECTS (exp)) | |
1791 | return false; | |
1792 | goto warn; | |
1793 | ||
1794 | case INDIRECT_REF: | |
1795 | /* Don't warn about automatic dereferencing of references, since | |
1796 | the user cannot control it. */ | |
1797 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE) | |
1798 | { | |
1799 | exp = TREE_OPERAND (exp, 0); | |
1800 | goto restart; | |
1801 | } | |
1802 | /* Fall through. */ | |
1803 | ||
1804 | default: | |
1805 | /* Referencing a volatile value is a side effect, so don't warn. */ | |
1806 | if ((DECL_P (exp) || REFERENCE_CLASS_P (exp)) | |
1807 | && TREE_THIS_VOLATILE (exp)) | |
1808 | return false; | |
1809 | ||
1810 | /* If this is an expression which has no operands, there is no value | |
1811 | to be unused. There are no such language-independent codes, | |
1812 | but front ends may define such. */ | |
1813 | if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0) | |
1814 | return false; | |
1815 | ||
1816 | warn: | |
1817 | return warning_at (locus, OPT_Wunused_value, "value computed is not used"); | |
1818 | } | |
1819 | } | |
1820 | ||
1821 | ||
bcf22371 | 1822 | /* Print a warning about casts that might indicate violation |
1823 | of strict aliasing rules if -Wstrict-aliasing is used and | |
1e31ff37 | 1824 | strict aliasing mode is in effect. OTYPE is the original |
1825 | TREE_TYPE of EXPR, and TYPE the type we're casting to. */ | |
bcf22371 | 1826 | |
e6fa0ea6 | 1827 | bool |
1e31ff37 | 1828 | strict_aliasing_warning (tree otype, tree type, tree expr) |
bcf22371 | 1829 | { |
45bb3afb | 1830 | /* Strip pointer conversion chains and get to the correct original type. */ |
1831 | STRIP_NOPS (expr); | |
1832 | otype = TREE_TYPE (expr); | |
1833 | ||
f06537f2 | 1834 | if (!(flag_strict_aliasing |
1835 | && POINTER_TYPE_P (type) | |
1836 | && POINTER_TYPE_P (otype) | |
1837 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1838 | /* If the type we are casting to is a ref-all pointer | |
1839 | dereferencing it is always valid. */ | |
1840 | || TYPE_REF_CAN_ALIAS_ALL (type)) | |
e6fa0ea6 | 1841 | return false; |
1842 | ||
1843 | if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR | |
bcf22371 | 1844 | && (DECL_P (TREE_OPERAND (expr, 0)) |
e6fa0ea6 | 1845 | || handled_component_p (TREE_OPERAND (expr, 0)))) |
bcf22371 | 1846 | { |
1847 | /* Casting the address of an object to non void pointer. Warn | |
1848 | if the cast breaks type based aliasing. */ | |
e6fa0ea6 | 1849 | if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) |
1850 | { | |
1851 | warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " | |
1852 | "might break strict-aliasing rules"); | |
1853 | return true; | |
1854 | } | |
bcf22371 | 1855 | else |
1856 | { | |
48e1416a | 1857 | /* warn_strict_aliasing >= 3. This includes the default (3). |
e6fa0ea6 | 1858 | Only warn if the cast is dereferenced immediately. */ |
32c2fdea | 1859 | alias_set_type set1 = |
e6fa0ea6 | 1860 | get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); |
32c2fdea | 1861 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); |
bcf22371 | 1862 | |
62d823d0 | 1863 | if (set1 != set2 && set2 != 0 |
1864 | && (set1 == 0 || !alias_sets_conflict_p (set1, set2))) | |
e6fa0ea6 | 1865 | { |
1866 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1867 | "pointer will break strict-aliasing rules"); | |
1868 | return true; | |
1869 | } | |
1870 | else if (warn_strict_aliasing == 2 | |
879f881c | 1871 | && !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1872 | { |
1873 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1874 | "pointer might break strict-aliasing rules"); | |
1875 | return true; | |
1876 | } | |
bcf22371 | 1877 | } |
1878 | } | |
e6fa0ea6 | 1879 | else |
1880 | if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) | |
1881 | { | |
1882 | /* At this level, warn for any conversions, even if an address is | |
1883 | not taken in the same statement. This will likely produce many | |
1884 | false positives, but could be useful to pinpoint problems that | |
1885 | are not revealed at higher levels. */ | |
32c2fdea | 1886 | alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); |
1887 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); | |
1888 | if (!COMPLETE_TYPE_P (type) | |
879f881c | 1889 | || !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1890 | { |
1891 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1892 | "pointer might break strict-aliasing rules"); | |
1893 | return true; | |
1894 | } | |
1895 | } | |
1896 | ||
1897 | return false; | |
bcf22371 | 1898 | } |
1899 | ||
f003f5dc | 1900 | /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with |
1901 | sizeof as last operand of certain builtins. */ | |
1902 | ||
1903 | void | |
57f872a2 | 1904 | sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee, |
f1f41a6c | 1905 | vec<tree, va_gc> *params, tree *sizeof_arg, |
f003f5dc | 1906 | bool (*comp_types) (tree, tree)) |
1907 | { | |
1908 | tree type, dest = NULL_TREE, src = NULL_TREE, tem; | |
57f872a2 | 1909 | bool strop = false, cmp = false; |
1910 | unsigned int idx = ~0; | |
1911 | location_t loc; | |
f003f5dc | 1912 | |
1913 | if (TREE_CODE (callee) != FUNCTION_DECL | |
1914 | || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL | |
f1f41a6c | 1915 | || vec_safe_length (params) <= 1) |
f003f5dc | 1916 | return; |
1917 | ||
f003f5dc | 1918 | switch (DECL_FUNCTION_CODE (callee)) |
1919 | { | |
1920 | case BUILT_IN_STRNCMP: | |
1921 | case BUILT_IN_STRNCASECMP: | |
57f872a2 | 1922 | cmp = true; |
1923 | /* FALLTHRU */ | |
f003f5dc | 1924 | case BUILT_IN_STRNCPY: |
57f872a2 | 1925 | case BUILT_IN_STRNCPY_CHK: |
f003f5dc | 1926 | case BUILT_IN_STRNCAT: |
57f872a2 | 1927 | case BUILT_IN_STRNCAT_CHK: |
1928 | case BUILT_IN_STPNCPY: | |
1929 | case BUILT_IN_STPNCPY_CHK: | |
f003f5dc | 1930 | strop = true; |
1931 | /* FALLTHRU */ | |
1932 | case BUILT_IN_MEMCPY: | |
57f872a2 | 1933 | case BUILT_IN_MEMCPY_CHK: |
f003f5dc | 1934 | case BUILT_IN_MEMMOVE: |
57f872a2 | 1935 | case BUILT_IN_MEMMOVE_CHK: |
f1f41a6c | 1936 | if (params->length () < 3) |
57f872a2 | 1937 | return; |
f1f41a6c | 1938 | src = (*params)[1]; |
1939 | dest = (*params)[0]; | |
57f872a2 | 1940 | idx = 2; |
1941 | break; | |
1942 | case BUILT_IN_BCOPY: | |
f1f41a6c | 1943 | if (params->length () < 3) |
57f872a2 | 1944 | return; |
f1f41a6c | 1945 | src = (*params)[0]; |
1946 | dest = (*params)[1]; | |
57f872a2 | 1947 | idx = 2; |
1948 | break; | |
f003f5dc | 1949 | case BUILT_IN_MEMCMP: |
57f872a2 | 1950 | case BUILT_IN_BCMP: |
f1f41a6c | 1951 | if (params->length () < 3) |
f003f5dc | 1952 | return; |
f1f41a6c | 1953 | src = (*params)[1]; |
1954 | dest = (*params)[0]; | |
57f872a2 | 1955 | idx = 2; |
1956 | cmp = true; | |
f003f5dc | 1957 | break; |
1958 | case BUILT_IN_MEMSET: | |
57f872a2 | 1959 | case BUILT_IN_MEMSET_CHK: |
f1f41a6c | 1960 | if (params->length () < 3) |
f003f5dc | 1961 | return; |
f1f41a6c | 1962 | dest = (*params)[0]; |
57f872a2 | 1963 | idx = 2; |
1964 | break; | |
1965 | case BUILT_IN_BZERO: | |
f1f41a6c | 1966 | dest = (*params)[0]; |
57f872a2 | 1967 | idx = 1; |
f003f5dc | 1968 | break; |
1969 | case BUILT_IN_STRNDUP: | |
f1f41a6c | 1970 | src = (*params)[0]; |
f003f5dc | 1971 | strop = true; |
57f872a2 | 1972 | idx = 1; |
1973 | break; | |
1974 | case BUILT_IN_MEMCHR: | |
f1f41a6c | 1975 | if (params->length () < 3) |
57f872a2 | 1976 | return; |
f1f41a6c | 1977 | src = (*params)[0]; |
57f872a2 | 1978 | idx = 2; |
1979 | break; | |
1980 | case BUILT_IN_SNPRINTF: | |
1981 | case BUILT_IN_SNPRINTF_CHK: | |
1982 | case BUILT_IN_VSNPRINTF: | |
1983 | case BUILT_IN_VSNPRINTF_CHK: | |
f1f41a6c | 1984 | dest = (*params)[0]; |
57f872a2 | 1985 | idx = 1; |
1986 | strop = true; | |
f003f5dc | 1987 | break; |
1988 | default: | |
1989 | break; | |
1990 | } | |
1991 | ||
57f872a2 | 1992 | if (idx >= 3) |
1993 | return; | |
1994 | ||
1995 | if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node) | |
1996 | return; | |
1997 | ||
1998 | type = TYPE_P (sizeof_arg[idx]) | |
1999 | ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]); | |
2000 | if (!POINTER_TYPE_P (type)) | |
2001 | return; | |
2002 | ||
f003f5dc | 2003 | if (dest |
2004 | && (tem = tree_strip_nop_conversions (dest)) | |
2005 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
2006 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
2007 | return; | |
2008 | ||
2009 | if (src | |
2010 | && (tem = tree_strip_nop_conversions (src)) | |
2011 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
2012 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
2013 | return; | |
2014 | ||
57f872a2 | 2015 | loc = sizeof_arg_loc[idx]; |
2016 | ||
2017 | if (dest && !cmp) | |
f003f5dc | 2018 | { |
57f872a2 | 2019 | if (!TYPE_P (sizeof_arg[idx]) |
2020 | && operand_equal_p (dest, sizeof_arg[idx], 0) | |
f003f5dc | 2021 | && comp_types (TREE_TYPE (dest), type)) |
2022 | { | |
57f872a2 | 2023 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
f003f5dc | 2024 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
2025 | "argument to %<sizeof%> in %qD call is the same " | |
2026 | "expression as the destination; did you mean to " | |
2027 | "remove the addressof?", callee); | |
2028 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2029 | == TYPE_PRECISION (char_type_node)) | |
2030 | || strop) | |
2031 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2032 | "argument to %<sizeof%> in %qD call is the same " | |
2033 | "expression as the destination; did you mean to " | |
2034 | "provide an explicit length?", callee); | |
2035 | else | |
2036 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2037 | "argument to %<sizeof%> in %qD call is the same " | |
2038 | "expression as the destination; did you mean to " | |
2039 | "dereference it?", callee); | |
2040 | return; | |
2041 | } | |
2042 | ||
2043 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
2044 | && !strop | |
2045 | && comp_types (TREE_TYPE (dest), type) | |
2046 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2047 | { | |
2048 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2049 | "argument to %<sizeof%> in %qD call is the same " | |
2050 | "pointer type %qT as the destination; expected %qT " | |
2051 | "or an explicit length", callee, TREE_TYPE (dest), | |
2052 | TREE_TYPE (TREE_TYPE (dest))); | |
2053 | return; | |
2054 | } | |
2055 | } | |
2056 | ||
57f872a2 | 2057 | if (src && !cmp) |
f003f5dc | 2058 | { |
57f872a2 | 2059 | if (!TYPE_P (sizeof_arg[idx]) |
2060 | && operand_equal_p (src, sizeof_arg[idx], 0) | |
f003f5dc | 2061 | && comp_types (TREE_TYPE (src), type)) |
2062 | { | |
57f872a2 | 2063 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
f003f5dc | 2064 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
2065 | "argument to %<sizeof%> in %qD call is the same " | |
2066 | "expression as the source; did you mean to " | |
2067 | "remove the addressof?", callee); | |
2068 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2069 | == TYPE_PRECISION (char_type_node)) | |
2070 | || strop) | |
2071 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2072 | "argument to %<sizeof%> in %qD call is the same " | |
2073 | "expression as the source; did you mean to " | |
2074 | "provide an explicit length?", callee); | |
2075 | else | |
2076 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2077 | "argument to %<sizeof%> in %qD call is the same " | |
2078 | "expression as the source; did you mean to " | |
2079 | "dereference it?", callee); | |
2080 | return; | |
2081 | } | |
2082 | ||
2083 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
2084 | && !strop | |
2085 | && comp_types (TREE_TYPE (src), type) | |
2086 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2087 | { | |
2088 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2089 | "argument to %<sizeof%> in %qD call is the same " | |
2090 | "pointer type %qT as the source; expected %qT " | |
2091 | "or an explicit length", callee, TREE_TYPE (src), | |
2092 | TREE_TYPE (TREE_TYPE (src))); | |
2093 | return; | |
2094 | } | |
2095 | } | |
57f872a2 | 2096 | |
2097 | if (dest) | |
2098 | { | |
2099 | if (!TYPE_P (sizeof_arg[idx]) | |
2100 | && operand_equal_p (dest, sizeof_arg[idx], 0) | |
2101 | && comp_types (TREE_TYPE (dest), type)) | |
2102 | { | |
2103 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
2104 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2105 | "argument to %<sizeof%> in %qD call is the same " | |
2106 | "expression as the first source; did you mean to " | |
2107 | "remove the addressof?", callee); | |
2108 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2109 | == TYPE_PRECISION (char_type_node)) | |
2110 | || strop) | |
2111 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2112 | "argument to %<sizeof%> in %qD call is the same " | |
2113 | "expression as the first source; did you mean to " | |
2114 | "provide an explicit length?", callee); | |
2115 | else | |
2116 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2117 | "argument to %<sizeof%> in %qD call is the same " | |
2118 | "expression as the first source; did you mean to " | |
2119 | "dereference it?", callee); | |
2120 | return; | |
2121 | } | |
2122 | ||
2123 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
2124 | && !strop | |
2125 | && comp_types (TREE_TYPE (dest), type) | |
2126 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2127 | { | |
2128 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2129 | "argument to %<sizeof%> in %qD call is the same " | |
2130 | "pointer type %qT as the first source; expected %qT " | |
2131 | "or an explicit length", callee, TREE_TYPE (dest), | |
2132 | TREE_TYPE (TREE_TYPE (dest))); | |
2133 | return; | |
2134 | } | |
2135 | } | |
2136 | ||
2137 | if (src) | |
2138 | { | |
2139 | if (!TYPE_P (sizeof_arg[idx]) | |
2140 | && operand_equal_p (src, sizeof_arg[idx], 0) | |
2141 | && comp_types (TREE_TYPE (src), type)) | |
2142 | { | |
2143 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
2144 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2145 | "argument to %<sizeof%> in %qD call is the same " | |
2146 | "expression as the second source; did you mean to " | |
2147 | "remove the addressof?", callee); | |
2148 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2149 | == TYPE_PRECISION (char_type_node)) | |
2150 | || strop) | |
2151 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2152 | "argument to %<sizeof%> in %qD call is the same " | |
2153 | "expression as the second source; did you mean to " | |
2154 | "provide an explicit length?", callee); | |
2155 | else | |
2156 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2157 | "argument to %<sizeof%> in %qD call is the same " | |
2158 | "expression as the second source; did you mean to " | |
2159 | "dereference it?", callee); | |
2160 | return; | |
2161 | } | |
2162 | ||
2163 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
2164 | && !strop | |
2165 | && comp_types (TREE_TYPE (src), type) | |
2166 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2167 | { | |
2168 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2169 | "argument to %<sizeof%> in %qD call is the same " | |
2170 | "pointer type %qT as the second source; expected %qT " | |
2171 | "or an explicit length", callee, TREE_TYPE (src), | |
2172 | TREE_TYPE (TREE_TYPE (src))); | |
2173 | return; | |
2174 | } | |
2175 | } | |
2176 | ||
f003f5dc | 2177 | } |
2178 | ||
3f08e399 | 2179 | /* Warn for unlikely, improbable, or stupid DECL declarations |
2180 | of `main'. */ | |
2181 | ||
2182 | void | |
2183 | check_main_parameter_types (tree decl) | |
2184 | { | |
d0af78c5 | 2185 | function_args_iterator iter; |
2186 | tree type; | |
3f08e399 | 2187 | int argct = 0; |
2188 | ||
d0af78c5 | 2189 | FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) |
2190 | { | |
2191 | /* XXX void_type_node belies the abstraction. */ | |
2192 | if (type == void_type_node || type == error_mark_node ) | |
2193 | break; | |
2194 | ||
2195 | ++argct; | |
2196 | switch (argct) | |
2197 | { | |
2198 | case 1: | |
2199 | if (TYPE_MAIN_VARIANT (type) != integer_type_node) | |
2200 | pedwarn (input_location, OPT_Wmain, | |
2201 | "first argument of %q+D should be %<int%>", decl); | |
2202 | break; | |
2203 | ||
2204 | case 2: | |
2205 | if (TREE_CODE (type) != POINTER_TYPE | |
2206 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
2207 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
2208 | != char_type_node)) | |
2209 | pedwarn (input_location, OPT_Wmain, | |
2210 | "second argument of %q+D should be %<char **%>", decl); | |
2211 | break; | |
2212 | ||
2213 | case 3: | |
2214 | if (TREE_CODE (type) != POINTER_TYPE | |
2215 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
2216 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
2217 | != char_type_node)) | |
2218 | pedwarn (input_location, OPT_Wmain, | |
2219 | "third argument of %q+D should probably be " | |
2220 | "%<char **%>", decl); | |
2221 | break; | |
2222 | } | |
2223 | } | |
3f08e399 | 2224 | |
2225 | /* It is intentional that this message does not mention the third | |
2226 | argument because it's only mentioned in an appendix of the | |
2227 | standard. */ | |
2228 | if (argct > 0 && (argct < 2 || argct > 3)) | |
d0af78c5 | 2229 | pedwarn (input_location, OPT_Wmain, |
2230 | "%q+D takes only zero or two arguments", decl); | |
3f08e399 | 2231 | } |
2232 | ||
73437615 | 2233 | /* vector_targets_convertible_p is used for vector pointer types. The |
2234 | callers perform various checks that the qualifiers are satisfactory, | |
2235 | while OTOH vector_targets_convertible_p ignores the number of elements | |
2236 | in the vectors. That's fine with vector pointers as we can consider, | |
2237 | say, a vector of 8 elements as two consecutive vectors of 4 elements, | |
2238 | and that does not require and conversion of the pointer values. | |
2239 | In contrast, vector_types_convertible_p and | |
2240 | vector_types_compatible_elements_p are used for vector value types. */ | |
ed7c4e62 | 2241 | /* True if pointers to distinct types T1 and T2 can be converted to |
2242 | each other without an explicit cast. Only returns true for opaque | |
2243 | vector types. */ | |
2244 | bool | |
2245 | vector_targets_convertible_p (const_tree t1, const_tree t2) | |
2246 | { | |
2247 | if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE | |
8d125f7d | 2248 | && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
ed7c4e62 | 2249 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
2250 | return true; | |
2251 | ||
2252 | return false; | |
2253 | } | |
2254 | ||
73437615 | 2255 | /* vector_types_convertible_p is used for vector value types. |
2256 | It could in principle call vector_targets_convertible_p as a subroutine, | |
2257 | but then the check for vector type would be duplicated with its callers, | |
2258 | and also the purpose of vector_targets_convertible_p would become | |
2259 | muddled. | |
2260 | Where vector_types_convertible_p returns true, a conversion might still be | |
2261 | needed to make the types match. | |
2262 | In contrast, vector_targets_convertible_p is used for vector pointer | |
2263 | values, and vector_types_compatible_elements_p is used specifically | |
2264 | in the context for binary operators, as a check if use is possible without | |
2265 | conversion. */ | |
546c4794 | 2266 | /* True if vector types T1 and T2 can be converted to each other |
2267 | without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2 | |
2268 | can only be converted with -flax-vector-conversions yet that is not | |
2269 | in effect, emit a note telling the user about that option if such | |
2270 | a note has not previously been emitted. */ | |
2271 | bool | |
9f627b1a | 2272 | vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note) |
8b4b9810 | 2273 | { |
546c4794 | 2274 | static bool emitted_lax_note = false; |
ae6db8ab | 2275 | bool convertible_lax; |
2276 | ||
8d125f7d | 2277 | if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
ae6db8ab | 2278 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
2279 | return true; | |
2280 | ||
2281 | convertible_lax = | |
2282 | (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) | |
2283 | && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || | |
432dd330 | 2284 | TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)) |
ae6db8ab | 2285 | && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) |
2286 | == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); | |
546c4794 | 2287 | |
2288 | if (!convertible_lax || flag_lax_vector_conversions) | |
2289 | return convertible_lax; | |
2290 | ||
2291 | if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) | |
29f51994 | 2292 | && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
546c4794 | 2293 | return true; |
2294 | ||
2295 | if (emit_lax_note && !emitted_lax_note) | |
2296 | { | |
2297 | emitted_lax_note = true; | |
5bcc316e | 2298 | inform (input_location, "use -flax-vector-conversions to permit " |
546c4794 | 2299 | "conversions between vectors with differing " |
2300 | "element types or numbers of subparts"); | |
2301 | } | |
2302 | ||
2303 | return false; | |
8b4b9810 | 2304 | } |
2305 | ||
bf0cb017 | 2306 | /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes |
2307 | and have vector types, V0 has the same type as V1, and the number of | |
2308 | elements of V0, V1, MASK is the same. | |
2309 | ||
2310 | In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was | |
2311 | called with two arguments. In this case implementation passes the | |
2312 | first argument twice in order to share the same tree code. This fact | |
2313 | could enable the mask-values being twice the vector length. This is | |
2314 | an implementation accident and this semantics is not guaranteed to | |
2315 | the user. */ | |
2316 | tree | |
68ea4406 | 2317 | c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask, |
2318 | bool complain) | |
bf0cb017 | 2319 | { |
2320 | tree ret; | |
2321 | bool wrap = true; | |
2322 | bool maybe_const = false; | |
2323 | bool two_arguments = false; | |
2324 | ||
2325 | if (v1 == NULL_TREE) | |
2326 | { | |
2327 | two_arguments = true; | |
2328 | v1 = v0; | |
2329 | } | |
2330 | ||
2331 | if (v0 == error_mark_node || v1 == error_mark_node | |
2332 | || mask == error_mark_node) | |
2333 | return error_mark_node; | |
2334 | ||
2335 | if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE | |
2336 | || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE) | |
2337 | { | |
68ea4406 | 2338 | if (complain) |
2339 | error_at (loc, "__builtin_shuffle last argument must " | |
2340 | "be an integer vector"); | |
bf0cb017 | 2341 | return error_mark_node; |
2342 | } | |
2343 | ||
2344 | if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE | |
2345 | || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE) | |
2346 | { | |
68ea4406 | 2347 | if (complain) |
2348 | error_at (loc, "__builtin_shuffle arguments must be vectors"); | |
bf0cb017 | 2349 | return error_mark_node; |
2350 | } | |
2351 | ||
2352 | if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1))) | |
2353 | { | |
68ea4406 | 2354 | if (complain) |
2355 | error_at (loc, "__builtin_shuffle argument vectors must be of " | |
2356 | "the same type"); | |
bf0cb017 | 2357 | return error_mark_node; |
2358 | } | |
2359 | ||
2360 | if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)) | |
2361 | != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)) | |
2362 | && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)) | |
2363 | != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))) | |
2364 | { | |
68ea4406 | 2365 | if (complain) |
2366 | error_at (loc, "__builtin_shuffle number of elements of the " | |
2367 | "argument vector(s) and the mask vector should " | |
2368 | "be the same"); | |
bf0cb017 | 2369 | return error_mark_node; |
2370 | } | |
2371 | ||
2372 | if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0)))) | |
2373 | != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask))))) | |
2374 | { | |
68ea4406 | 2375 | if (complain) |
2376 | error_at (loc, "__builtin_shuffle argument vector(s) inner type " | |
2377 | "must have the same size as inner type of the mask"); | |
bf0cb017 | 2378 | return error_mark_node; |
2379 | } | |
2380 | ||
2381 | if (!c_dialect_cxx ()) | |
2382 | { | |
2383 | /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */ | |
2384 | v0 = c_fully_fold (v0, false, &maybe_const); | |
2385 | wrap &= maybe_const; | |
2386 | ||
2387 | if (two_arguments) | |
2388 | v1 = v0 = save_expr (v0); | |
2389 | else | |
2390 | { | |
2391 | v1 = c_fully_fold (v1, false, &maybe_const); | |
2392 | wrap &= maybe_const; | |
2393 | } | |
2394 | ||
2395 | mask = c_fully_fold (mask, false, &maybe_const); | |
2396 | wrap &= maybe_const; | |
2397 | } | |
68ea4406 | 2398 | else if (two_arguments) |
2399 | v1 = v0 = save_expr (v0); | |
bf0cb017 | 2400 | |
2401 | ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask); | |
2402 | ||
2403 | if (!c_dialect_cxx () && !wrap) | |
2404 | ret = c_wrap_maybe_const (ret, true); | |
2405 | ||
2406 | return ret; | |
2407 | } | |
2408 | ||
7f506bca | 2409 | /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum |
2410 | to integral type. */ | |
2411 | ||
2412 | static tree | |
2413 | c_common_get_narrower (tree op, int *unsignedp_ptr) | |
2414 | { | |
2415 | op = get_narrower (op, unsignedp_ptr); | |
2416 | ||
2417 | if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE | |
2418 | && ENUM_IS_SCOPED (TREE_TYPE (op))) | |
2419 | { | |
2420 | /* C++0x scoped enumerations don't implicitly convert to integral | |
2421 | type; if we stripped an explicit conversion to a larger type we | |
2422 | need to replace it so common_type will still work. */ | |
a51edb4c | 2423 | tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)), |
2424 | TYPE_UNSIGNED (TREE_TYPE (op))); | |
7f506bca | 2425 | op = fold_convert (type, op); |
2426 | } | |
2427 | return op; | |
2428 | } | |
2429 | ||
2561cea2 | 2430 | /* This is a helper function of build_binary_op. |
2431 | ||
2432 | For certain operations if both args were extended from the same | |
2433 | smaller type, do the arithmetic in that type and then extend. | |
2434 | ||
2435 | BITWISE indicates a bitwise operation. | |
2436 | For them, this optimization is safe only if | |
2437 | both args are zero-extended or both are sign-extended. | |
2438 | Otherwise, we might change the result. | |
2439 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
48e1416a | 2440 | but calculated in (unsigned short) it would be (unsigned short)-1. |
2561cea2 | 2441 | */ |
7f506bca | 2442 | tree |
2443 | shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise) | |
2561cea2 | 2444 | { |
2445 | int unsigned0, unsigned1; | |
2446 | tree arg0, arg1; | |
2447 | int uns; | |
2448 | tree type; | |
2449 | ||
2450 | /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents | |
2451 | excessive narrowing when we call get_narrower below. For | |
2452 | example, suppose that OP0 is of unsigned int extended | |
2453 | from signed char and that RESULT_TYPE is long long int. | |
2454 | If we explicitly cast OP0 to RESULT_TYPE, OP0 would look | |
2455 | like | |
48e1416a | 2456 | |
2561cea2 | 2457 | (long long int) (unsigned int) signed_char |
2458 | ||
2459 | which get_narrower would narrow down to | |
48e1416a | 2460 | |
2561cea2 | 2461 | (unsigned int) signed char |
48e1416a | 2462 | |
2561cea2 | 2463 | If we do not cast OP0 first, get_narrower would return |
2464 | signed_char, which is inconsistent with the case of the | |
2465 | explicit cast. */ | |
2466 | op0 = convert (result_type, op0); | |
2467 | op1 = convert (result_type, op1); | |
2468 | ||
7f506bca | 2469 | arg0 = c_common_get_narrower (op0, &unsigned0); |
2470 | arg1 = c_common_get_narrower (op1, &unsigned1); | |
2561cea2 | 2471 | |
2472 | /* UNS is 1 if the operation to be done is an unsigned one. */ | |
2473 | uns = TYPE_UNSIGNED (result_type); | |
2474 | ||
2475 | /* Handle the case that OP0 (or OP1) does not *contain* a conversion | |
2476 | but it *requires* conversion to FINAL_TYPE. */ | |
48e1416a | 2477 | |
2561cea2 | 2478 | if ((TYPE_PRECISION (TREE_TYPE (op0)) |
2479 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2480 | && TREE_TYPE (op0) != result_type) | |
2481 | unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); | |
2482 | if ((TYPE_PRECISION (TREE_TYPE (op1)) | |
2483 | == TYPE_PRECISION (TREE_TYPE (arg1))) | |
2484 | && TREE_TYPE (op1) != result_type) | |
2485 | unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
48e1416a | 2486 | |
2561cea2 | 2487 | /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ |
48e1416a | 2488 | |
2561cea2 | 2489 | /* For bitwise operations, signedness of nominal type |
2490 | does not matter. Consider only how operands were extended. */ | |
2491 | if (bitwise) | |
2492 | uns = unsigned0; | |
48e1416a | 2493 | |
2561cea2 | 2494 | /* Note that in all three cases below we refrain from optimizing |
2495 | an unsigned operation on sign-extended args. | |
2496 | That would not be valid. */ | |
48e1416a | 2497 | |
2561cea2 | 2498 | /* Both args variable: if both extended in same way |
2499 | from same width, do it in that width. | |
2500 | Do it unsigned if args were zero-extended. */ | |
2501 | if ((TYPE_PRECISION (TREE_TYPE (arg0)) | |
2502 | < TYPE_PRECISION (result_type)) | |
2503 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2504 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2505 | && unsigned0 == unsigned1 | |
2506 | && (unsigned0 || !uns)) | |
2507 | return c_common_signed_or_unsigned_type | |
2508 | (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); | |
2509 | ||
2510 | else if (TREE_CODE (arg0) == INTEGER_CST | |
2511 | && (unsigned1 || !uns) | |
2512 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2513 | < TYPE_PRECISION (result_type)) | |
2514 | && (type | |
2515 | = c_common_signed_or_unsigned_type (unsigned1, | |
2516 | TREE_TYPE (arg1))) | |
2517 | && !POINTER_TYPE_P (type) | |
2518 | && int_fits_type_p (arg0, type)) | |
2519 | return type; | |
2520 | ||
2521 | else if (TREE_CODE (arg1) == INTEGER_CST | |
2522 | && (unsigned0 || !uns) | |
2523 | && (TYPE_PRECISION (TREE_TYPE (arg0)) | |
2524 | < TYPE_PRECISION (result_type)) | |
2525 | && (type | |
2526 | = c_common_signed_or_unsigned_type (unsigned0, | |
2527 | TREE_TYPE (arg0))) | |
2528 | && !POINTER_TYPE_P (type) | |
2529 | && int_fits_type_p (arg1, type)) | |
2530 | return type; | |
2531 | ||
2532 | return result_type; | |
2533 | } | |
2534 | ||
22a75734 | 2535 | /* Checks if expression EXPR of real/integer type cannot be converted |
ca9d7d74 | 2536 | to the real/integer type TYPE. Function returns non-zero when: |
22a75734 | 2537 | * EXPR is a constant which cannot be exactly converted to TYPE. |
2538 | * EXPR is not a constant and size of EXPR's type > than size of TYPE, | |
7dfa155b | 2539 | for EXPR type and TYPE being both integers or both real. |
22a75734 | 2540 | * EXPR is not a constant of real type and TYPE is an integer. |
2541 | * EXPR is not a constant of integer type which cannot be | |
2542 | exactly converted to real type. | |
7dfa155b | 2543 | Function allows conversions between types of different signedness and |
ca9d7d74 | 2544 | can return SAFE_CONVERSION (zero) in that case. Function can produce |
2545 | signedness warnings if PRODUCE_WARNS is true. */ | |
22a75734 | 2546 | |
ca9d7d74 | 2547 | enum conversion_safety |
22a75734 | 2548 | unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns) |
d31d55f0 | 2549 | { |
ca9d7d74 | 2550 | enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */ |
27259707 | 2551 | tree expr_type = TREE_TYPE (expr); |
22a75734 | 2552 | loc = expansion_point_location_if_in_system_header (loc); |
d31d55f0 | 2553 | |
7dfa155b | 2554 | if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST) |
5b16c152 | 2555 | { |
d31d55f0 | 2556 | /* Warn for real constant that is not an exact integer converted |
7dfa155b | 2557 | to integer type. */ |
27259707 | 2558 | if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2559 | && TREE_CODE (type) == INTEGER_TYPE) |
2560 | { | |
2561 | if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) | |
ca9d7d74 | 2562 | give_warning = UNSAFE_REAL; |
7dfa155b | 2563 | } |
da1fb07b | 2564 | /* Warn for an integer constant that does not fit into integer type. */ |
27259707 | 2565 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2566 | && TREE_CODE (type) == INTEGER_TYPE |
2567 | && !int_fits_type_p (expr, type)) | |
2568 | { | |
2569 | if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type) | |
27259707 | 2570 | && tree_int_cst_sgn (expr) < 0) |
7dfa155b | 2571 | { |
2572 | if (produce_warns) | |
2573 | warning_at (loc, OPT_Wsign_conversion, "negative integer" | |
2574 | " implicitly converted to unsigned type"); | |
2575 | } | |
2576 | else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type)) | |
2577 | { | |
2578 | if (produce_warns) | |
2579 | warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned" | |
2580 | " constant value to negative integer"); | |
2581 | } | |
7ee0d227 | 2582 | else |
ca9d7d74 | 2583 | give_warning = UNSAFE_OTHER; |
7dfa155b | 2584 | } |
d31d55f0 | 2585 | else if (TREE_CODE (type) == REAL_TYPE) |
7dfa155b | 2586 | { |
2587 | /* Warn for an integer constant that does not fit into real type. */ | |
2588 | if (TREE_CODE (expr_type) == INTEGER_TYPE) | |
2589 | { | |
2590 | REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); | |
2591 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
ca9d7d74 | 2592 | give_warning = UNSAFE_REAL; |
7dfa155b | 2593 | } |
2594 | /* Warn for a real constant that does not fit into a smaller | |
2595 | real type. */ | |
2596 | else if (TREE_CODE (expr_type) == REAL_TYPE | |
2597 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
2598 | { | |
2599 | REAL_VALUE_TYPE a = TREE_REAL_CST (expr); | |
2600 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
ca9d7d74 | 2601 | give_warning = UNSAFE_REAL; |
7dfa155b | 2602 | } |
2603 | } | |
2604 | } | |
2605 | else | |
2606 | { | |
d31d55f0 | 2607 | /* Warn for real types converted to integer types. */ |
2561cea2 | 2608 | if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2609 | && TREE_CODE (type) == INTEGER_TYPE) |
ca9d7d74 | 2610 | give_warning = UNSAFE_REAL; |
d31d55f0 | 2611 | |
2561cea2 | 2612 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2613 | && TREE_CODE (type) == INTEGER_TYPE) |
2614 | { | |
69609004 | 2615 | /* Don't warn about unsigned char y = 0xff, x = (int) y; */ |
f9d856a4 | 2616 | expr = get_unwidened (expr, 0); |
2561cea2 | 2617 | expr_type = TREE_TYPE (expr); |
69609004 | 2618 | |
2561cea2 | 2619 | /* Don't warn for short y; short x = ((int)y & 0xff); */ |
48e1416a | 2620 | if (TREE_CODE (expr) == BIT_AND_EXPR |
7dfa155b | 2621 | || TREE_CODE (expr) == BIT_IOR_EXPR |
2561cea2 | 2622 | || TREE_CODE (expr) == BIT_XOR_EXPR) |
2623 | { | |
27259707 | 2624 | /* If both args were extended from a shortest type, |
2625 | use that type if that is safe. */ | |
48e1416a | 2626 | expr_type = shorten_binary_op (expr_type, |
2627 | TREE_OPERAND (expr, 0), | |
2628 | TREE_OPERAND (expr, 1), | |
2561cea2 | 2629 | /* bitwise */1); |
2630 | ||
2561cea2 | 2631 | if (TREE_CODE (expr) == BIT_AND_EXPR) |
2632 | { | |
2633 | tree op0 = TREE_OPERAND (expr, 0); | |
2634 | tree op1 = TREE_OPERAND (expr, 1); | |
30de145b | 2635 | bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
2636 | bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
2637 | ||
2638 | /* If one of the operands is a non-negative constant | |
2639 | that fits in the target type, then the type of the | |
2640 | other operand does not matter. */ | |
2561cea2 | 2641 | if ((TREE_CODE (op0) == INTEGER_CST |
2642 | && int_fits_type_p (op0, c_common_signed_type (type)) | |
2643 | && int_fits_type_p (op0, c_common_unsigned_type (type))) | |
2644 | || (TREE_CODE (op1) == INTEGER_CST | |
27259707 | 2645 | && int_fits_type_p (op1, c_common_signed_type (type)) |
48e1416a | 2646 | && int_fits_type_p (op1, |
27259707 | 2647 | c_common_unsigned_type (type)))) |
ca9d7d74 | 2648 | return SAFE_CONVERSION; |
30de145b | 2649 | /* If constant is unsigned and fits in the target |
2650 | type, then the result will also fit. */ | |
2651 | else if ((TREE_CODE (op0) == INTEGER_CST | |
48e1416a | 2652 | && unsigned0 |
30de145b | 2653 | && int_fits_type_p (op0, type)) |
2654 | || (TREE_CODE (op1) == INTEGER_CST | |
2655 | && unsigned1 | |
2656 | && int_fits_type_p (op1, type))) | |
ca9d7d74 | 2657 | return SAFE_CONVERSION; |
2561cea2 | 2658 | } |
2659 | } | |
7dfa155b | 2660 | /* Warn for integer types converted to smaller integer types. */ |
48e1416a | 2661 | if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
ca9d7d74 | 2662 | give_warning = UNSAFE_OTHER; |
7ee0d227 | 2663 | |
2664 | /* When they are the same width but different signedness, | |
2665 | then the value may change. */ | |
7dfa155b | 2666 | else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type) |
2561cea2 | 2667 | && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type)) |
7ee0d227 | 2668 | /* Even when converted to a bigger type, if the type is |
2669 | unsigned but expr is signed, then negative values | |
2670 | will be changed. */ | |
7dfa155b | 2671 | || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type))) |
2672 | && produce_warns) | |
200dd99c | 2673 | warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT " |
2674 | "may change the sign of the result", | |
2675 | type, expr_type); | |
7dfa155b | 2676 | } |
d31d55f0 | 2677 | |
2678 | /* Warn for integer types converted to real types if and only if | |
7dfa155b | 2679 | all the range of values of the integer type cannot be |
2680 | represented by the real type. */ | |
2561cea2 | 2681 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2682 | && TREE_CODE (type) == REAL_TYPE) |
2683 | { | |
4c2cfa81 | 2684 | tree type_low_bound, type_high_bound; |
7dfa155b | 2685 | REAL_VALUE_TYPE real_low_bound, real_high_bound; |
4c2cfa81 | 2686 | |
2687 | /* Don't warn about char y = 0xff; float x = (int) y; */ | |
2688 | expr = get_unwidened (expr, 0); | |
2689 | expr_type = TREE_TYPE (expr); | |
2690 | ||
7dfa155b | 2691 | type_low_bound = TYPE_MIN_VALUE (expr_type); |
2692 | type_high_bound = TYPE_MAX_VALUE (expr_type); | |
2693 | real_low_bound = real_value_from_int_cst (0, type_low_bound); | |
2694 | real_high_bound = real_value_from_int_cst (0, type_high_bound); | |
d31d55f0 | 2695 | |
7dfa155b | 2696 | if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound) |
2697 | || !exact_real_truncate (TYPE_MODE (type), &real_high_bound)) | |
ca9d7d74 | 2698 | give_warning = UNSAFE_OTHER; |
7dfa155b | 2699 | } |
d31d55f0 | 2700 | |
2701 | /* Warn for real types converted to smaller real types. */ | |
2561cea2 | 2702 | else if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2703 | && TREE_CODE (type) == REAL_TYPE |
2704 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
ca9d7d74 | 2705 | give_warning = UNSAFE_REAL; |
7dfa155b | 2706 | } |
2707 | ||
2708 | return give_warning; | |
2709 | } | |
2710 | ||
2711 | /* Warns if the conversion of EXPR to TYPE may alter a value. | |
2712 | This is a helper function for warnings_for_convert_and_check. */ | |
2713 | ||
2714 | static void | |
22a75734 | 2715 | conversion_warning (location_t loc, tree type, tree expr) |
7dfa155b | 2716 | { |
7dfa155b | 2717 | tree expr_type = TREE_TYPE (expr); |
ca9d7d74 | 2718 | enum conversion_safety conversion_kind; |
d31d55f0 | 2719 | |
ca9d7d74 | 2720 | if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion) |
7dfa155b | 2721 | return; |
d31d55f0 | 2722 | |
ec704957 | 2723 | /* This may happen, because for LHS op= RHS we preevaluate |
2724 | RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which | |
2725 | means we could no longer see the code of the EXPR. */ | |
2726 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
2727 | expr = C_MAYBE_CONST_EXPR_EXPR (expr); | |
2728 | if (TREE_CODE (expr) == SAVE_EXPR) | |
2729 | expr = TREE_OPERAND (expr, 0); | |
2730 | ||
7dfa155b | 2731 | switch (TREE_CODE (expr)) |
2732 | { | |
2733 | case EQ_EXPR: | |
2734 | case NE_EXPR: | |
2735 | case LE_EXPR: | |
2736 | case GE_EXPR: | |
2737 | case LT_EXPR: | |
2738 | case GT_EXPR: | |
2739 | case TRUTH_ANDIF_EXPR: | |
2740 | case TRUTH_ORIF_EXPR: | |
2741 | case TRUTH_AND_EXPR: | |
2742 | case TRUTH_OR_EXPR: | |
2743 | case TRUTH_XOR_EXPR: | |
2744 | case TRUTH_NOT_EXPR: | |
2745 | /* Conversion from boolean to a signed:1 bit-field (which only | |
2746 | can hold the values 0 and -1) doesn't lose information - but | |
2747 | it does change the value. */ | |
2748 | if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) | |
2749 | warning_at (loc, OPT_Wconversion, | |
2750 | "conversion to %qT from boolean expression", type); | |
2751 | return; | |
2752 | ||
2753 | case REAL_CST: | |
2754 | case INTEGER_CST: | |
22a75734 | 2755 | conversion_kind = unsafe_conversion_p (loc, type, expr, true); |
ca9d7d74 | 2756 | if (conversion_kind == UNSAFE_REAL) |
2757 | warning_at (loc, OPT_Wfloat_conversion, | |
2758 | "conversion to %qT alters %qT constant value", | |
2759 | type, expr_type); | |
2760 | else if (conversion_kind) | |
7dfa155b | 2761 | warning_at (loc, OPT_Wconversion, |
2762 | "conversion to %qT alters %qT constant value", | |
2763 | type, expr_type); | |
2764 | return; | |
2765 | ||
2766 | case COND_EXPR: | |
2767 | { | |
0e4e775a | 2768 | /* In case of COND_EXPR, we do not care about the type of |
2769 | COND_EXPR, only about the conversion of each operand. */ | |
2770 | tree op1 = TREE_OPERAND (expr, 1); | |
2771 | tree op2 = TREE_OPERAND (expr, 2); | |
2772 | ||
22a75734 | 2773 | conversion_warning (loc, type, op1); |
2774 | conversion_warning (loc, type, op2); | |
0e4e775a | 2775 | return; |
7dfa155b | 2776 | } |
2777 | ||
2778 | default: /* 'expr' is not a constant. */ | |
22a75734 | 2779 | conversion_kind = unsafe_conversion_p (loc, type, expr, true); |
ca9d7d74 | 2780 | if (conversion_kind == UNSAFE_REAL) |
2781 | warning_at (loc, OPT_Wfloat_conversion, | |
2782 | "conversion to %qT from %qT may alter its value", | |
2783 | type, expr_type); | |
2784 | else if (conversion_kind) | |
7dfa155b | 2785 | warning_at (loc, OPT_Wconversion, |
200dd99c | 2786 | "conversion to %qT from %qT may alter its value", |
2787 | type, expr_type); | |
d31d55f0 | 2788 | } |
2789 | } | |
2790 | ||
59dd8856 | 2791 | /* Produce warnings after a conversion. RESULT is the result of |
2792 | converting EXPR to TYPE. This is a helper function for | |
2793 | convert_and_check and cp_convert_and_check. */ | |
2a1736ed | 2794 | |
59dd8856 | 2795 | void |
22a75734 | 2796 | warnings_for_convert_and_check (location_t loc, tree type, tree expr, |
2797 | tree result) | |
2a1736ed | 2798 | { |
22a75734 | 2799 | loc = expansion_point_location_if_in_system_header (loc); |
61f69bc9 | 2800 | |
da1fb07b | 2801 | if (TREE_CODE (expr) == INTEGER_CST |
2802 | && (TREE_CODE (type) == INTEGER_TYPE | |
2803 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
2804 | && !int_fits_type_p (expr, type)) | |
2805 | { | |
d31d55f0 | 2806 | /* Do not diagnose overflow in a constant expression merely |
2807 | because a conversion overflowed. */ | |
da1fb07b | 2808 | if (TREE_OVERFLOW (result)) |
eddad94a | 2809 | TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); |
2810 | ||
da1fb07b | 2811 | if (TYPE_UNSIGNED (type)) |
d31d55f0 | 2812 | { |
da1fb07b | 2813 | /* This detects cases like converting -129 or 256 to |
2814 | unsigned char. */ | |
2815 | if (!int_fits_type_p (expr, c_common_signed_type (type))) | |
61f69bc9 | 2816 | warning_at (loc, OPT_Woverflow, |
2817 | "large integer implicitly truncated to unsigned type"); | |
7ee0d227 | 2818 | else |
22a75734 | 2819 | conversion_warning (loc, type, expr); |
da1fb07b | 2820 | } |
48e1416a | 2821 | else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) |
22a75734 | 2822 | warning_at (loc, OPT_Woverflow, |
e0913805 | 2823 | "overflow in implicit constant conversion"); |
2824 | /* No warning for converting 0x80000000 to int. */ | |
2825 | else if (pedantic | |
2826 | && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE | |
2827 | || TYPE_PRECISION (TREE_TYPE (expr)) | |
2828 | != TYPE_PRECISION (type))) | |
61f69bc9 | 2829 | warning_at (loc, OPT_Woverflow, |
2830 | "overflow in implicit constant conversion"); | |
e0913805 | 2831 | |
7ee0d227 | 2832 | else |
22a75734 | 2833 | conversion_warning (loc, type, expr); |
2a1736ed | 2834 | } |
9421ebb9 | 2835 | else if ((TREE_CODE (result) == INTEGER_CST |
2836 | || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) | |
61f69bc9 | 2837 | warning_at (loc, OPT_Woverflow, |
2838 | "overflow in implicit constant conversion"); | |
7ee0d227 | 2839 | else |
22a75734 | 2840 | conversion_warning (loc, type, expr); |
59dd8856 | 2841 | } |
2842 | ||
2843 | ||
2844 | /* Convert EXPR to TYPE, warning about conversion problems with constants. | |
2845 | Invoke this function on every expression that is converted implicitly, | |
2846 | i.e. because of language rules and not because of an explicit cast. */ | |
2847 | ||
2848 | tree | |
22a75734 | 2849 | convert_and_check (location_t loc, tree type, tree expr) |
59dd8856 | 2850 | { |
2851 | tree result; | |
c6418a4e | 2852 | tree expr_for_warning; |
2853 | ||
2854 | /* Convert from a value with possible excess precision rather than | |
2855 | via the semantic type, but do not warn about values not fitting | |
2856 | exactly in the semantic type. */ | |
2857 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
2858 | { | |
2859 | tree orig_type = TREE_TYPE (expr); | |
2860 | expr = TREE_OPERAND (expr, 0); | |
2861 | expr_for_warning = convert (orig_type, expr); | |
2862 | if (orig_type == type) | |
2863 | return expr_for_warning; | |
2864 | } | |
2865 | else | |
2866 | expr_for_warning = expr; | |
59dd8856 | 2867 | |
2868 | if (TREE_TYPE (expr) == type) | |
2869 | return expr; | |
48e1416a | 2870 | |
59dd8856 | 2871 | result = convert (type, expr); |
2872 | ||
48d94ede | 2873 | if (c_inhibit_evaluation_warnings == 0 |
2874 | && !TREE_OVERFLOW_P (expr) | |
2875 | && result != error_mark_node) | |
22a75734 | 2876 | warnings_for_convert_and_check (loc, type, expr_for_warning, result); |
59dd8856 | 2877 | |
da1fb07b | 2878 | return result; |
b2806639 | 2879 | } |
2880 | \f | |
4e91a871 | 2881 | /* A node in a list that describes references to variables (EXPR), which are |
2882 | either read accesses if WRITER is zero, or write accesses, in which case | |
2883 | WRITER is the parent of EXPR. */ | |
2884 | struct tlist | |
2885 | { | |
2886 | struct tlist *next; | |
2887 | tree expr, writer; | |
2888 | }; | |
2889 | ||
2890 | /* Used to implement a cache the results of a call to verify_tree. We only | |
2891 | use this for SAVE_EXPRs. */ | |
2892 | struct tlist_cache | |
2893 | { | |
2894 | struct tlist_cache *next; | |
2895 | struct tlist *cache_before_sp; | |
2896 | struct tlist *cache_after_sp; | |
2897 | tree expr; | |
481c6ce6 | 2898 | }; |
2899 | ||
4e91a871 | 2900 | /* Obstack to use when allocating tlist structures, and corresponding |
2901 | firstobj. */ | |
2902 | static struct obstack tlist_obstack; | |
2903 | static char *tlist_firstobj = 0; | |
2904 | ||
2905 | /* Keep track of the identifiers we've warned about, so we can avoid duplicate | |
2906 | warnings. */ | |
2907 | static struct tlist *warned_ids; | |
2908 | /* SAVE_EXPRs need special treatment. We process them only once and then | |
2909 | cache the results. */ | |
2910 | static struct tlist_cache *save_expr_cache; | |
2911 | ||
1cae46be | 2912 | static void add_tlist (struct tlist **, struct tlist *, tree, int); |
2913 | static void merge_tlist (struct tlist **, struct tlist *, int); | |
2914 | static void verify_tree (tree, struct tlist **, struct tlist **, tree); | |
2915 | static int warning_candidate_p (tree); | |
79973b57 | 2916 | static bool candidate_equal_p (const_tree, const_tree); |
1cae46be | 2917 | static void warn_for_collisions (struct tlist *); |
2918 | static void warn_for_collisions_1 (tree, tree, struct tlist *, int); | |
2919 | static struct tlist *new_tlist (struct tlist *, tree, tree); | |
481c6ce6 | 2920 | |
4e91a871 | 2921 | /* Create a new struct tlist and fill in its fields. */ |
2922 | static struct tlist * | |
1cae46be | 2923 | new_tlist (struct tlist *next, tree t, tree writer) |
4e91a871 | 2924 | { |
2925 | struct tlist *l; | |
9318f22c | 2926 | l = XOBNEW (&tlist_obstack, struct tlist); |
4e91a871 | 2927 | l->next = next; |
2928 | l->expr = t; | |
2929 | l->writer = writer; | |
2930 | return l; | |
2931 | } | |
2932 | ||
2933 | /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER | |
2934 | is nonnull, we ignore any node we find which has a writer equal to it. */ | |
2935 | ||
2936 | static void | |
1cae46be | 2937 | add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy) |
4e91a871 | 2938 | { |
2939 | while (add) | |
2940 | { | |
2941 | struct tlist *next = add->next; | |
84166705 | 2942 | if (!copy) |
4e91a871 | 2943 | add->next = *to; |
79973b57 | 2944 | if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer)) |
4e91a871 | 2945 | *to = copy ? new_tlist (*to, add->expr, add->writer) : add; |
2946 | add = next; | |
2947 | } | |
2948 | } | |
2949 | ||
2950 | /* Merge the nodes of ADD into TO. This merging process is done so that for | |
2951 | each variable that already exists in TO, no new node is added; however if | |
2952 | there is a write access recorded in ADD, and an occurrence on TO is only | |
2953 | a read access, then the occurrence in TO will be modified to record the | |
2954 | write. */ | |
481c6ce6 | 2955 | |
2956 | static void | |
1cae46be | 2957 | merge_tlist (struct tlist **to, struct tlist *add, int copy) |
4e91a871 | 2958 | { |
2959 | struct tlist **end = to; | |
2960 | ||
2961 | while (*end) | |
2962 | end = &(*end)->next; | |
2963 | ||
2964 | while (add) | |
2965 | { | |
2966 | int found = 0; | |
2967 | struct tlist *tmp2; | |
2968 | struct tlist *next = add->next; | |
2969 | ||
2970 | for (tmp2 = *to; tmp2; tmp2 = tmp2->next) | |
79973b57 | 2971 | if (candidate_equal_p (tmp2->expr, add->expr)) |
4e91a871 | 2972 | { |
2973 | found = 1; | |
84166705 | 2974 | if (!tmp2->writer) |
4e91a871 | 2975 | tmp2->writer = add->writer; |
2976 | } | |
84166705 | 2977 | if (!found) |
4e91a871 | 2978 | { |
312243bb | 2979 | *end = copy ? new_tlist (NULL, add->expr, add->writer) : add; |
4e91a871 | 2980 | end = &(*end)->next; |
2981 | *end = 0; | |
2982 | } | |
2983 | add = next; | |
2984 | } | |
2985 | } | |
2986 | ||
2987 | /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable | |
2988 | references in list LIST conflict with it, excluding reads if ONLY writers | |
2989 | is nonzero. */ | |
2990 | ||
2991 | static void | |
1cae46be | 2992 | warn_for_collisions_1 (tree written, tree writer, struct tlist *list, |
2993 | int only_writes) | |
4e91a871 | 2994 | { |
2995 | struct tlist *tmp; | |
2996 | ||
2997 | /* Avoid duplicate warnings. */ | |
2998 | for (tmp = warned_ids; tmp; tmp = tmp->next) | |
79973b57 | 2999 | if (candidate_equal_p (tmp->expr, written)) |
4e91a871 | 3000 | return; |
3001 | ||
3002 | while (list) | |
3003 | { | |
79973b57 | 3004 | if (candidate_equal_p (list->expr, written) |
3005 | && !candidate_equal_p (list->writer, writer) | |
3006 | && (!only_writes || list->writer)) | |
4e91a871 | 3007 | { |
3008 | warned_ids = new_tlist (warned_ids, written, NULL_TREE); | |
3df42822 | 3009 | warning_at (EXPR_LOC_OR_LOC (writer, input_location), |
6513b50d | 3010 | OPT_Wsequence_point, "operation on %qE may be undefined", |
3011 | list->expr); | |
4e91a871 | 3012 | } |
3013 | list = list->next; | |
3014 | } | |
3015 | } | |
3016 | ||
3017 | /* Given a list LIST of references to variables, find whether any of these | |
3018 | can cause conflicts due to missing sequence points. */ | |
3019 | ||
3020 | static void | |
1cae46be | 3021 | warn_for_collisions (struct tlist *list) |
4e91a871 | 3022 | { |
3023 | struct tlist *tmp; | |
1cae46be | 3024 | |
4e91a871 | 3025 | for (tmp = list; tmp; tmp = tmp->next) |
3026 | { | |
3027 | if (tmp->writer) | |
3028 | warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); | |
3029 | } | |
3030 | } | |
3031 | ||
734c98be | 3032 | /* Return nonzero if X is a tree that can be verified by the sequence point |
4e91a871 | 3033 | warnings. */ |
3034 | static int | |
1cae46be | 3035 | warning_candidate_p (tree x) |
481c6ce6 | 3036 | { |
6ef8d12f | 3037 | if (DECL_P (x) && DECL_ARTIFICIAL (x)) |
3038 | return 0; | |
3039 | ||
027fc6ef | 3040 | if (TREE_CODE (x) == BLOCK) |
3041 | return 0; | |
3042 | ||
6ef8d12f | 3043 | /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c |
79973b57 | 3044 | (lvalue_p) crash on TRY/CATCH. */ |
6ef8d12f | 3045 | if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x))) |
3046 | return 0; | |
3047 | ||
3048 | if (!lvalue_p (x)) | |
3049 | return 0; | |
3050 | ||
3051 | /* No point to track non-const calls, they will never satisfy | |
3052 | operand_equal_p. */ | |
3053 | if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0) | |
3054 | return 0; | |
3055 | ||
3056 | if (TREE_CODE (x) == STRING_CST) | |
3057 | return 0; | |
3058 | ||
3059 | return 1; | |
79973b57 | 3060 | } |
3061 | ||
3062 | /* Return nonzero if X and Y appear to be the same candidate (or NULL) */ | |
3063 | static bool | |
3064 | candidate_equal_p (const_tree x, const_tree y) | |
3065 | { | |
3066 | return (x == y) || (x && y && operand_equal_p (x, y, 0)); | |
4e91a871 | 3067 | } |
481c6ce6 | 3068 | |
4e91a871 | 3069 | /* Walk the tree X, and record accesses to variables. If X is written by the |
3070 | parent tree, WRITER is the parent. | |
3071 | We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this | |
3072 | expression or its only operand forces a sequence point, then everything up | |
3073 | to the sequence point is stored in PBEFORE_SP. Everything else gets stored | |
3074 | in PNO_SP. | |
3075 | Once we return, we will have emitted warnings if any subexpression before | |
3076 | such a sequence point could be undefined. On a higher level, however, the | |
3077 | sequence point may not be relevant, and we'll merge the two lists. | |
3078 | ||
3079 | Example: (b++, a) + b; | |
3080 | The call that processes the COMPOUND_EXPR will store the increment of B | |
3081 | in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that | |
3082 | processes the PLUS_EXPR will need to merge the two lists so that | |
3083 | eventually, all accesses end up on the same list (and we'll warn about the | |
3084 | unordered subexpressions b++ and b. | |
3085 | ||
3086 | A note on merging. If we modify the former example so that our expression | |
3087 | becomes | |
3088 | (b++, b) + a | |
3089 | care must be taken not simply to add all three expressions into the final | |
3090 | PNO_SP list. The function merge_tlist takes care of that by merging the | |
3091 | before-SP list of the COMPOUND_EXPR into its after-SP list in a special | |
3092 | way, so that no more than one access to B is recorded. */ | |
481c6ce6 | 3093 | |
4e91a871 | 3094 | static void |
1cae46be | 3095 | verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp, |
3096 | tree writer) | |
4e91a871 | 3097 | { |
3098 | struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; | |
3099 | enum tree_code code; | |
ce45a448 | 3100 | enum tree_code_class cl; |
481c6ce6 | 3101 | |
e5b75768 | 3102 | /* X may be NULL if it is the operand of an empty statement expression |
3103 | ({ }). */ | |
3104 | if (x == NULL) | |
3105 | return; | |
3106 | ||
4e91a871 | 3107 | restart: |
3108 | code = TREE_CODE (x); | |
e916c70c | 3109 | cl = TREE_CODE_CLASS (code); |
481c6ce6 | 3110 | |
4e91a871 | 3111 | if (warning_candidate_p (x)) |
79973b57 | 3112 | *pno_sp = new_tlist (*pno_sp, x, writer); |
4e91a871 | 3113 | |
3114 | switch (code) | |
3115 | { | |
67b28e3e | 3116 | case CONSTRUCTOR: |
8e71dad2 | 3117 | case SIZEOF_EXPR: |
67b28e3e | 3118 | return; |
3119 | ||
4e91a871 | 3120 | case COMPOUND_EXPR: |
3121 | case TRUTH_ANDIF_EXPR: | |
3122 | case TRUTH_ORIF_EXPR: | |
3123 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
3124 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3125 | warn_for_collisions (tmp_nosp); | |
3126 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3127 | merge_tlist (pbefore_sp, tmp_nosp, 0); | |
3128 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); | |
3129 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3130 | return; | |
3131 | ||
3132 | case COND_EXPR: | |
3133 | tmp_before = tmp_list2 = 0; | |
3134 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); | |
3135 | warn_for_collisions (tmp_list2); | |
3136 | merge_tlist (pbefore_sp, tmp_before, 0); | |
312243bb | 3137 | merge_tlist (pbefore_sp, tmp_list2, 0); |
4e91a871 | 3138 | |
3139 | tmp_list3 = tmp_nosp = 0; | |
3140 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); | |
3141 | warn_for_collisions (tmp_nosp); | |
3142 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3143 | ||
3144 | tmp_list3 = tmp_list2 = 0; | |
3145 | verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); | |
3146 | warn_for_collisions (tmp_list2); | |
3147 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3148 | /* Rather than add both tmp_nosp and tmp_list2, we have to merge the | |
3149 | two first, to avoid warning for (a ? b++ : b++). */ | |
3150 | merge_tlist (&tmp_nosp, tmp_list2, 0); | |
3151 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
3152 | return; | |
3153 | ||
481c6ce6 | 3154 | case PREDECREMENT_EXPR: |
3155 | case PREINCREMENT_EXPR: | |
3156 | case POSTDECREMENT_EXPR: | |
3157 | case POSTINCREMENT_EXPR: | |
4e91a871 | 3158 | verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); |
3159 | return; | |
3160 | ||
3161 | case MODIFY_EXPR: | |
3162 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
3163 | verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); | |
3164 | verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); | |
3165 | /* Expressions inside the LHS are not ordered wrt. the sequence points | |
3166 | in the RHS. Example: | |
3167 | *a = (a++, 2) | |
3168 | Despite the fact that the modification of "a" is in the before_sp | |
3169 | list (tmp_before), it conflicts with the use of "a" in the LHS. | |
3170 | We can handle this by adding the contents of tmp_list3 | |
3171 | to those of tmp_before, and redoing the collision warnings for that | |
3172 | list. */ | |
3173 | add_tlist (&tmp_before, tmp_list3, x, 1); | |
3174 | warn_for_collisions (tmp_before); | |
3175 | /* Exclude the LHS itself here; we first have to merge it into the | |
3176 | tmp_nosp list. This is done to avoid warning for "a = a"; if we | |
3177 | didn't exclude the LHS, we'd get it twice, once as a read and once | |
3178 | as a write. */ | |
3179 | add_tlist (pno_sp, tmp_list3, x, 0); | |
3180 | warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); | |
3181 | ||
3182 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3183 | if (warning_candidate_p (TREE_OPERAND (x, 0))) | |
3184 | merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); | |
3185 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); | |
3186 | return; | |
481c6ce6 | 3187 | |
3188 | case CALL_EXPR: | |
4e91a871 | 3189 | /* We need to warn about conflicts among arguments and conflicts between |
3190 | args and the function address. Side effects of the function address, | |
3191 | however, are not ordered by the sequence point of the call. */ | |
c2f47e15 | 3192 | { |
3193 | call_expr_arg_iterator iter; | |
3194 | tree arg; | |
48e1416a | 3195 | tmp_before = tmp_nosp = 0; |
c2f47e15 | 3196 | verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE); |
3197 | FOR_EACH_CALL_EXPR_ARG (arg, iter, x) | |
3198 | { | |
3199 | tmp_list2 = tmp_list3 = 0; | |
3200 | verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE); | |
3201 | merge_tlist (&tmp_list3, tmp_list2, 0); | |
3202 | add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); | |
3203 | } | |
3204 | add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); | |
3205 | warn_for_collisions (tmp_before); | |
3206 | add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); | |
3207 | return; | |
3208 | } | |
481c6ce6 | 3209 | |
3210 | case TREE_LIST: | |
3211 | /* Scan all the list, e.g. indices of multi dimensional array. */ | |
3212 | while (x) | |
3213 | { | |
4e91a871 | 3214 | tmp_before = tmp_nosp = 0; |
3215 | verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
3216 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
3217 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
481c6ce6 | 3218 | x = TREE_CHAIN (x); |
3219 | } | |
4e91a871 | 3220 | return; |
481c6ce6 | 3221 | |
4e91a871 | 3222 | case SAVE_EXPR: |
3223 | { | |
3224 | struct tlist_cache *t; | |
3225 | for (t = save_expr_cache; t; t = t->next) | |
79973b57 | 3226 | if (candidate_equal_p (t->expr, x)) |
4e91a871 | 3227 | break; |
481c6ce6 | 3228 | |
84166705 | 3229 | if (!t) |
481c6ce6 | 3230 | { |
9318f22c | 3231 | t = XOBNEW (&tlist_obstack, struct tlist_cache); |
4e91a871 | 3232 | t->next = save_expr_cache; |
3233 | t->expr = x; | |
3234 | save_expr_cache = t; | |
3235 | ||
3236 | tmp_before = tmp_nosp = 0; | |
3237 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3238 | warn_for_collisions (tmp_nosp); | |
3239 | ||
3240 | tmp_list3 = 0; | |
312243bb | 3241 | merge_tlist (&tmp_list3, tmp_nosp, 0); |
4e91a871 | 3242 | t->cache_before_sp = tmp_before; |
3243 | t->cache_after_sp = tmp_list3; | |
481c6ce6 | 3244 | } |
4e91a871 | 3245 | merge_tlist (pbefore_sp, t->cache_before_sp, 1); |
3246 | add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); | |
3247 | return; | |
3248 | } | |
481c6ce6 | 3249 | |
012916cb | 3250 | case ADDR_EXPR: |
3251 | x = TREE_OPERAND (x, 0); | |
3252 | if (DECL_P (x)) | |
3253 | return; | |
3254 | writer = 0; | |
3255 | goto restart; | |
3256 | ||
ce45a448 | 3257 | default: |
3258 | /* For other expressions, simply recurse on their operands. | |
a0c938f0 | 3259 | Manual tail recursion for unary expressions. |
ce45a448 | 3260 | Other non-expressions need not be processed. */ |
3261 | if (cl == tcc_unary) | |
3262 | { | |
ce45a448 | 3263 | x = TREE_OPERAND (x, 0); |
3264 | writer = 0; | |
3265 | goto restart; | |
3266 | } | |
3267 | else if (IS_EXPR_CODE_CLASS (cl)) | |
3268 | { | |
3269 | int lp; | |
c2f47e15 | 3270 | int max = TREE_OPERAND_LENGTH (x); |
ce45a448 | 3271 | for (lp = 0; lp < max; lp++) |
3272 | { | |
3273 | tmp_before = tmp_nosp = 0; | |
3274 | verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0); | |
3275 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
3276 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
3277 | } | |
3278 | } | |
3279 | return; | |
481c6ce6 | 3280 | } |
481c6ce6 | 3281 | } |
3282 | ||
974e2c0c | 3283 | /* Try to warn for undefined behavior in EXPR due to missing sequence |
481c6ce6 | 3284 | points. */ |
3285 | ||
4b987fac | 3286 | DEBUG_FUNCTION void |
1cae46be | 3287 | verify_sequence_points (tree expr) |
481c6ce6 | 3288 | { |
4e91a871 | 3289 | struct tlist *before_sp = 0, *after_sp = 0; |
481c6ce6 | 3290 | |
4e91a871 | 3291 | warned_ids = 0; |
3292 | save_expr_cache = 0; | |
3293 | if (tlist_firstobj == 0) | |
481c6ce6 | 3294 | { |
4e91a871 | 3295 | gcc_obstack_init (&tlist_obstack); |
4fd61bc6 | 3296 | tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0); |
481c6ce6 | 3297 | } |
3298 | ||
4e91a871 | 3299 | verify_tree (expr, &before_sp, &after_sp, 0); |
3300 | warn_for_collisions (after_sp); | |
3301 | obstack_free (&tlist_obstack, tlist_firstobj); | |
481c6ce6 | 3302 | } |
b0fc3e72 | 3303 | \f |
3304 | /* Validate the expression after `case' and apply default promotions. */ | |
3305 | ||
2ca392fd | 3306 | static tree |
1cae46be | 3307 | check_case_value (tree value) |
b0fc3e72 | 3308 | { |
3309 | if (value == NULL_TREE) | |
3310 | return value; | |
3311 | ||
b96dc121 | 3312 | if (TREE_CODE (value) == INTEGER_CST) |
3313 | /* Promote char or short to int. */ | |
3314 | value = perform_integral_promotions (value); | |
3315 | else if (value != error_mark_node) | |
b0fc3e72 | 3316 | { |
3317 | error ("case label does not reduce to an integer constant"); | |
3318 | value = error_mark_node; | |
3319 | } | |
b0fc3e72 | 3320 | |
6433f1c2 | 3321 | constant_expression_warning (value); |
3322 | ||
b0fc3e72 | 3323 | return value; |
3324 | } | |
3325 | \f | |
2ca392fd | 3326 | /* See if the case values LOW and HIGH are in the range of the original |
5c9dae64 | 3327 | type (i.e. before the default conversion to int) of the switch testing |
2ca392fd | 3328 | expression. |
3329 | TYPE is the promoted type of the testing expression, and ORIG_TYPE is | |
91275768 | 3330 | the type before promoting it. CASE_LOW_P is a pointer to the lower |
2ca392fd | 3331 | bound of the case label, and CASE_HIGH_P is the upper bound or NULL |
3332 | if the case is not a case range. | |
3333 | The caller has to make sure that we are not called with NULL for | |
5c9dae64 | 3334 | CASE_LOW_P (i.e. the default case). |
442e3cb9 | 3335 | Returns true if the case label is in range of ORIG_TYPE (saturated or |
2ca392fd | 3336 | untouched) or false if the label is out of range. */ |
3337 | ||
3338 | static bool | |
3339 | check_case_bounds (tree type, tree orig_type, | |
3340 | tree *case_low_p, tree *case_high_p) | |
3341 | { | |
3342 | tree min_value, max_value; | |
3343 | tree case_low = *case_low_p; | |
3344 | tree case_high = case_high_p ? *case_high_p : case_low; | |
3345 | ||
3346 | /* If there was a problem with the original type, do nothing. */ | |
3347 | if (orig_type == error_mark_node) | |
3348 | return true; | |
3349 | ||
3350 | min_value = TYPE_MIN_VALUE (orig_type); | |
3351 | max_value = TYPE_MAX_VALUE (orig_type); | |
3352 | ||
3353 | /* Case label is less than minimum for type. */ | |
3354 | if (tree_int_cst_compare (case_low, min_value) < 0 | |
3355 | && tree_int_cst_compare (case_high, min_value) < 0) | |
3356 | { | |
c3ceba8e | 3357 | warning (0, "case label value is less than minimum value for type"); |
2ca392fd | 3358 | return false; |
3359 | } | |
b27ac6b5 | 3360 | |
2ca392fd | 3361 | /* Case value is greater than maximum for type. */ |
3362 | if (tree_int_cst_compare (case_low, max_value) > 0 | |
3363 | && tree_int_cst_compare (case_high, max_value) > 0) | |
3364 | { | |
c3ceba8e | 3365 | warning (0, "case label value exceeds maximum value for type"); |
2ca392fd | 3366 | return false; |
3367 | } | |
3368 | ||
3369 | /* Saturate lower case label value to minimum. */ | |
3370 | if (tree_int_cst_compare (case_high, min_value) >= 0 | |
3371 | && tree_int_cst_compare (case_low, min_value) < 0) | |
3372 | { | |
c3ceba8e | 3373 | warning (0, "lower value in case label range" |
2ca392fd | 3374 | " less than minimum value for type"); |
3375 | case_low = min_value; | |
3376 | } | |
b27ac6b5 | 3377 | |
2ca392fd | 3378 | /* Saturate upper case label value to maximum. */ |
3379 | if (tree_int_cst_compare (case_low, max_value) <= 0 | |
3380 | && tree_int_cst_compare (case_high, max_value) > 0) | |
3381 | { | |
c3ceba8e | 3382 | warning (0, "upper value in case label range" |
2ca392fd | 3383 | " exceeds maximum value for type"); |
3384 | case_high = max_value; | |
3385 | } | |
3386 | ||
3387 | if (*case_low_p != case_low) | |
3388 | *case_low_p = convert (type, case_low); | |
3389 | if (case_high_p && *case_high_p != case_high) | |
3390 | *case_high_p = convert (type, case_high); | |
3391 | ||
3392 | return true; | |
3393 | } | |
3394 | \f | |
b0fc3e72 | 3395 | /* Return an integer type with BITS bits of precision, |
3396 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
3397 | ||
3398 | tree | |
1cae46be | 3399 | c_common_type_for_size (unsigned int bits, int unsignedp) |
b0fc3e72 | 3400 | { |
46375237 | 3401 | if (bits == TYPE_PRECISION (integer_type_node)) |
3402 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3403 | ||
bacde65a | 3404 | if (bits == TYPE_PRECISION (signed_char_type_node)) |
b0fc3e72 | 3405 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
3406 | ||
bacde65a | 3407 | if (bits == TYPE_PRECISION (short_integer_type_node)) |
b0fc3e72 | 3408 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
3409 | ||
bacde65a | 3410 | if (bits == TYPE_PRECISION (long_integer_type_node)) |
b0fc3e72 | 3411 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
3412 | ||
bacde65a | 3413 | if (bits == TYPE_PRECISION (long_long_integer_type_node)) |
b0fc3e72 | 3414 | return (unsignedp ? long_long_unsigned_type_node |
3415 | : long_long_integer_type_node); | |
3416 | ||
6388cfe2 | 3417 | if (int128_integer_type_node |
3418 | && bits == TYPE_PRECISION (int128_integer_type_node)) | |
3419 | return (unsignedp ? int128_unsigned_type_node | |
3420 | : int128_integer_type_node); | |
3421 | ||
f57fa2ea | 3422 | if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) |
3423 | return (unsignedp ? widest_unsigned_literal_type_node | |
3424 | : widest_integer_literal_type_node); | |
3425 | ||
bacde65a | 3426 | if (bits <= TYPE_PRECISION (intQI_type_node)) |
3427 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3428 | ||
3429 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
3430 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3431 | ||
3432 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
3433 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3434 | ||
3435 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
3436 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3437 | ||
b0fc3e72 | 3438 | return 0; |
3439 | } | |
3440 | ||
9421ebb9 | 3441 | /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits |
3442 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed; | |
3443 | and saturating if SATP is nonzero, otherwise not saturating. */ | |
3444 | ||
3445 | tree | |
3446 | c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit, | |
3447 | int unsignedp, int satp) | |
3448 | { | |
3449 | enum machine_mode mode; | |
3450 | if (ibit == 0) | |
3451 | mode = unsignedp ? UQQmode : QQmode; | |
3452 | else | |
3453 | mode = unsignedp ? UHAmode : HAmode; | |
3454 | ||
3455 | for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode)) | |
3456 | if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit) | |
3457 | break; | |
3458 | ||
3459 | if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode)) | |
3460 | { | |
3461 | sorry ("GCC cannot support operators with integer types and " | |
3462 | "fixed-point types that have too many integral and " | |
3463 | "fractional bits together"); | |
3464 | return 0; | |
3465 | } | |
3466 | ||
3467 | return c_common_type_for_mode (mode, satp); | |
3468 | } | |
3469 | ||
5b247e9f | 3470 | /* Used for communication between c_common_type_for_mode and |
3471 | c_register_builtin_type. */ | |
3472 | static GTY(()) tree registered_builtin_types; | |
3473 | ||
b0fc3e72 | 3474 | /* Return a data type that has machine mode MODE. |
3475 | If the mode is an integer, | |
9421ebb9 | 3476 | then UNSIGNEDP selects between signed and unsigned types. |
3477 | If the mode is a fixed-point mode, | |
3478 | then UNSIGNEDP selects between saturating and nonsaturating types. */ | |
b0fc3e72 | 3479 | |
3480 | tree | |
1cae46be | 3481 | c_common_type_for_mode (enum machine_mode mode, int unsignedp) |
b0fc3e72 | 3482 | { |
5b247e9f | 3483 | tree t; |
3484 | ||
46375237 | 3485 | if (mode == TYPE_MODE (integer_type_node)) |
3486 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3487 | ||
b0fc3e72 | 3488 | if (mode == TYPE_MODE (signed_char_type_node)) |
3489 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3490 | ||
3491 | if (mode == TYPE_MODE (short_integer_type_node)) | |
3492 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3493 | ||
b0fc3e72 | 3494 | if (mode == TYPE_MODE (long_integer_type_node)) |
3495 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3496 | ||
3497 | if (mode == TYPE_MODE (long_long_integer_type_node)) | |
3498 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
3499 | ||
6388cfe2 | 3500 | if (int128_integer_type_node |
3501 | && mode == TYPE_MODE (int128_integer_type_node)) | |
3502 | return unsignedp ? int128_unsigned_type_node : int128_integer_type_node; | |
3503 | ||
f57fa2ea | 3504 | if (mode == TYPE_MODE (widest_integer_literal_type_node)) |
44e9fa65 | 3505 | return unsignedp ? widest_unsigned_literal_type_node |
4ee9c684 | 3506 | : widest_integer_literal_type_node; |
f57fa2ea | 3507 | |
88ae7f04 | 3508 | if (mode == QImode) |
bacde65a | 3509 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
3510 | ||
88ae7f04 | 3511 | if (mode == HImode) |
bacde65a | 3512 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
3513 | ||
88ae7f04 | 3514 | if (mode == SImode) |
bacde65a | 3515 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
3516 | ||
88ae7f04 | 3517 | if (mode == DImode) |
bacde65a | 3518 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
3519 | ||
cc1cc1c7 | 3520 | #if HOST_BITS_PER_WIDE_INT >= 64 |
6274009c | 3521 | if (mode == TYPE_MODE (intTI_type_node)) |
3522 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
cc1cc1c7 | 3523 | #endif |
6274009c | 3524 | |
b0fc3e72 | 3525 | if (mode == TYPE_MODE (float_type_node)) |
3526 | return float_type_node; | |
3527 | ||
3528 | if (mode == TYPE_MODE (double_type_node)) | |
3529 | return double_type_node; | |
3530 | ||
3531 | if (mode == TYPE_MODE (long_double_type_node)) | |
3532 | return long_double_type_node; | |
3533 | ||
545c2bde | 3534 | if (mode == TYPE_MODE (void_type_node)) |
3535 | return void_type_node; | |
b27ac6b5 | 3536 | |
b0fc3e72 | 3537 | if (mode == TYPE_MODE (build_pointer_type (char_type_node))) |
61b9b73c | 3538 | return (unsignedp |
3539 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
3540 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 3541 | |
3542 | if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) | |
61b9b73c | 3543 | return (unsignedp |
3544 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
3545 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 3546 | |
0dfc45b5 | 3547 | if (COMPLEX_MODE_P (mode)) |
3548 | { | |
3549 | enum machine_mode inner_mode; | |
3550 | tree inner_type; | |
3551 | ||
3552 | if (mode == TYPE_MODE (complex_float_type_node)) | |
3553 | return complex_float_type_node; | |
3554 | if (mode == TYPE_MODE (complex_double_type_node)) | |
3555 | return complex_double_type_node; | |
3556 | if (mode == TYPE_MODE (complex_long_double_type_node)) | |
3557 | return complex_long_double_type_node; | |
3558 | ||
3559 | if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp) | |
3560 | return complex_integer_type_node; | |
3561 | ||
3562 | inner_mode = GET_MODE_INNER (mode); | |
3563 | inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
3564 | if (inner_type != NULL_TREE) | |
3565 | return build_complex_type (inner_type); | |
3566 | } | |
3567 | else if (VECTOR_MODE_P (mode)) | |
4917c376 | 3568 | { |
3569 | enum machine_mode inner_mode = GET_MODE_INNER (mode); | |
3570 | tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
3571 | if (inner_type != NULL_TREE) | |
3572 | return build_vector_type_for_mode (inner_type, mode); | |
88ae7f04 | 3573 | } |
e2ea7e3a | 3574 | |
c4503c0a | 3575 | if (mode == TYPE_MODE (dfloat32_type_node)) |
3576 | return dfloat32_type_node; | |
3577 | if (mode == TYPE_MODE (dfloat64_type_node)) | |
3578 | return dfloat64_type_node; | |
3579 | if (mode == TYPE_MODE (dfloat128_type_node)) | |
3580 | return dfloat128_type_node; | |
3581 | ||
9421ebb9 | 3582 | if (ALL_SCALAR_FIXED_POINT_MODE_P (mode)) |
3583 | { | |
3584 | if (mode == TYPE_MODE (short_fract_type_node)) | |
3585 | return unsignedp ? sat_short_fract_type_node : short_fract_type_node; | |
3586 | if (mode == TYPE_MODE (fract_type_node)) | |
3587 | return unsignedp ? sat_fract_type_node : fract_type_node; | |
3588 | if (mode == TYPE_MODE (long_fract_type_node)) | |
3589 | return unsignedp ? sat_long_fract_type_node : long_fract_type_node; | |
3590 | if (mode == TYPE_MODE (long_long_fract_type_node)) | |
3591 | return unsignedp ? sat_long_long_fract_type_node | |
3592 | : long_long_fract_type_node; | |
3593 | ||
3594 | if (mode == TYPE_MODE (unsigned_short_fract_type_node)) | |
3595 | return unsignedp ? sat_unsigned_short_fract_type_node | |
3596 | : unsigned_short_fract_type_node; | |
3597 | if (mode == TYPE_MODE (unsigned_fract_type_node)) | |
3598 | return unsignedp ? sat_unsigned_fract_type_node | |
3599 | : unsigned_fract_type_node; | |
3600 | if (mode == TYPE_MODE (unsigned_long_fract_type_node)) | |
3601 | return unsignedp ? sat_unsigned_long_fract_type_node | |
3602 | : unsigned_long_fract_type_node; | |
3603 | if (mode == TYPE_MODE (unsigned_long_long_fract_type_node)) | |
3604 | return unsignedp ? sat_unsigned_long_long_fract_type_node | |
3605 | : unsigned_long_long_fract_type_node; | |
3606 | ||
3607 | if (mode == TYPE_MODE (short_accum_type_node)) | |
3608 | return unsignedp ? sat_short_accum_type_node : short_accum_type_node; | |
3609 | if (mode == TYPE_MODE (accum_type_node)) | |
3610 | return unsignedp ? sat_accum_type_node : accum_type_node; | |
3611 | if (mode == TYPE_MODE (long_accum_type_node)) | |
3612 | return unsignedp ? sat_long_accum_type_node : long_accum_type_node; | |
3613 | if (mode == TYPE_MODE (long_long_accum_type_node)) | |
3614 | return unsignedp ? sat_long_long_accum_type_node | |
3615 | : long_long_accum_type_node; | |
3616 | ||
3617 | if (mode == TYPE_MODE (unsigned_short_accum_type_node)) | |
3618 | return unsignedp ? sat_unsigned_short_accum_type_node | |
3619 | : unsigned_short_accum_type_node; | |
3620 | if (mode == TYPE_MODE (unsigned_accum_type_node)) | |
3621 | return unsignedp ? sat_unsigned_accum_type_node | |
3622 | : unsigned_accum_type_node; | |
3623 | if (mode == TYPE_MODE (unsigned_long_accum_type_node)) | |
3624 | return unsignedp ? sat_unsigned_long_accum_type_node | |
3625 | : unsigned_long_accum_type_node; | |
3626 | if (mode == TYPE_MODE (unsigned_long_long_accum_type_node)) | |
3627 | return unsignedp ? sat_unsigned_long_long_accum_type_node | |
3628 | : unsigned_long_long_accum_type_node; | |
3629 | ||
3630 | if (mode == QQmode) | |
3631 | return unsignedp ? sat_qq_type_node : qq_type_node; | |
3632 | if (mode == HQmode) | |
3633 | return unsignedp ? sat_hq_type_node : hq_type_node; | |
3634 | if (mode == SQmode) | |
3635 | return unsignedp ? sat_sq_type_node : sq_type_node; | |
3636 | if (mode == DQmode) | |
3637 | return unsignedp ? sat_dq_type_node : dq_type_node; | |
3638 | if (mode == TQmode) | |
3639 | return unsignedp ? sat_tq_type_node : tq_type_node; | |
3640 | ||
3641 | if (mode == UQQmode) | |
3642 | return unsignedp ? sat_uqq_type_node : uqq_type_node; | |
3643 | if (mode == UHQmode) | |
3644 | return unsignedp ? sat_uhq_type_node : uhq_type_node; | |
3645 | if (mode == USQmode) | |
3646 | return unsignedp ? sat_usq_type_node : usq_type_node; | |
3647 | if (mode == UDQmode) | |
3648 | return unsignedp ? sat_udq_type_node : udq_type_node; | |
3649 | if (mode == UTQmode) | |
3650 | return unsignedp ? sat_utq_type_node : utq_type_node; | |
3651 | ||
3652 | if (mode == HAmode) | |
3653 | return unsignedp ? sat_ha_type_node : ha_type_node; | |
3654 | if (mode == SAmode) | |
3655 | return unsignedp ? sat_sa_type_node : sa_type_node; | |
3656 | if (mode == DAmode) | |
3657 | return unsignedp ? sat_da_type_node : da_type_node; | |
3658 | if (mode == TAmode) | |
3659 | return unsignedp ? sat_ta_type_node : ta_type_node; | |
3660 | ||
3661 | if (mode == UHAmode) | |
3662 | return unsignedp ? sat_uha_type_node : uha_type_node; | |
3663 | if (mode == USAmode) | |
3664 | return unsignedp ? sat_usa_type_node : usa_type_node; | |
3665 | if (mode == UDAmode) | |
3666 | return unsignedp ? sat_uda_type_node : uda_type_node; | |
3667 | if (mode == UTAmode) | |
3668 | return unsignedp ? sat_uta_type_node : uta_type_node; | |
3669 | } | |
3670 | ||
5b247e9f | 3671 | for (t = registered_builtin_types; t; t = TREE_CHAIN (t)) |
baec58e1 | 3672 | if (TYPE_MODE (TREE_VALUE (t)) == mode |
3673 | && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t))) | |
5b247e9f | 3674 | return TREE_VALUE (t); |
3675 | ||
b0fc3e72 | 3676 | return 0; |
3677 | } | |
20d39783 | 3678 | |
11773141 | 3679 | tree |
3680 | c_common_unsigned_type (tree type) | |
3681 | { | |
3682 | return c_common_signed_or_unsigned_type (1, type); | |
3683 | } | |
3684 | ||
20d39783 | 3685 | /* Return a signed type the same as TYPE in other respects. */ |
3686 | ||
3687 | tree | |
1cae46be | 3688 | c_common_signed_type (tree type) |
20d39783 | 3689 | { |
4070745f | 3690 | return c_common_signed_or_unsigned_type (0, type); |
20d39783 | 3691 | } |
3692 | ||
3693 | /* Return a type the same as TYPE except unsigned or | |
3694 | signed according to UNSIGNEDP. */ | |
3695 | ||
3696 | tree | |
1cae46be | 3697 | c_common_signed_or_unsigned_type (int unsignedp, tree type) |
20d39783 | 3698 | { |
7a91101f | 3699 | tree type1; |
20d39783 | 3700 | |
7a91101f | 3701 | /* This block of code emulates the behavior of the old |
3702 | c_common_unsigned_type. In particular, it returns | |
3703 | long_unsigned_type_node if passed a long, even when a int would | |
3704 | have the same size. This is necessary for warnings to work | |
3705 | correctly in archs where sizeof(int) == sizeof(long) */ | |
3706 | ||
3707 | type1 = TYPE_MAIN_VARIANT (type); | |
3708 | if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node) | |
3709 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3710 | if (type1 == integer_type_node || type1 == unsigned_type_node) | |
3711 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3712 | if (type1 == short_integer_type_node || type1 == short_unsigned_type_node) | |
3713 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3714 | if (type1 == long_integer_type_node || type1 == long_unsigned_type_node) | |
3715 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3716 | if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node) | |
3717 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
6388cfe2 | 3718 | if (int128_integer_type_node |
3719 | && (type1 == int128_integer_type_node | |
3720 | || type1 == int128_unsigned_type_node)) | |
3721 | return unsignedp ? int128_unsigned_type_node : int128_integer_type_node; | |
7a91101f | 3722 | if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node) |
3723 | return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node; | |
3724 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
3725 | if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node) | |
3726 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
3727 | #endif | |
3728 | if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node) | |
3729 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3730 | if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node) | |
3731 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3732 | if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node) | |
3733 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3734 | if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node) | |
3735 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3736 | ||
9f92e1a2 | 3737 | #define C_COMMON_FIXED_TYPES(NAME) \ |
3738 | if (type1 == short_ ## NAME ## _type_node \ | |
3739 | || type1 == unsigned_short_ ## NAME ## _type_node) \ | |
3740 | return unsignedp ? unsigned_short_ ## NAME ## _type_node \ | |
3741 | : short_ ## NAME ## _type_node; \ | |
3742 | if (type1 == NAME ## _type_node \ | |
3743 | || type1 == unsigned_ ## NAME ## _type_node) \ | |
3744 | return unsignedp ? unsigned_ ## NAME ## _type_node \ | |
3745 | : NAME ## _type_node; \ | |
3746 | if (type1 == long_ ## NAME ## _type_node \ | |
3747 | || type1 == unsigned_long_ ## NAME ## _type_node) \ | |
3748 | return unsignedp ? unsigned_long_ ## NAME ## _type_node \ | |
3749 | : long_ ## NAME ## _type_node; \ | |
3750 | if (type1 == long_long_ ## NAME ## _type_node \ | |
3751 | || type1 == unsigned_long_long_ ## NAME ## _type_node) \ | |
3752 | return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \ | |
3753 | : long_long_ ## NAME ## _type_node; | |
3754 | ||
3755 | #define C_COMMON_FIXED_MODE_TYPES(NAME) \ | |
3756 | if (type1 == NAME ## _type_node \ | |
3757 | || type1 == u ## NAME ## _type_node) \ | |
3758 | return unsignedp ? u ## NAME ## _type_node \ | |
3759 | : NAME ## _type_node; | |
3760 | ||
3761 | #define C_COMMON_FIXED_TYPES_SAT(NAME) \ | |
3762 | if (type1 == sat_ ## short_ ## NAME ## _type_node \ | |
3763 | || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \ | |
3764 | return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \ | |
3765 | : sat_ ## short_ ## NAME ## _type_node; \ | |
3766 | if (type1 == sat_ ## NAME ## _type_node \ | |
3767 | || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \ | |
3768 | return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \ | |
3769 | : sat_ ## NAME ## _type_node; \ | |
3770 | if (type1 == sat_ ## long_ ## NAME ## _type_node \ | |
3771 | || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \ | |
3772 | return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \ | |
3773 | : sat_ ## long_ ## NAME ## _type_node; \ | |
3774 | if (type1 == sat_ ## long_long_ ## NAME ## _type_node \ | |
3775 | || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \ | |
3776 | return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \ | |
3777 | : sat_ ## long_long_ ## NAME ## _type_node; | |
3778 | ||
3779 | #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \ | |
3780 | if (type1 == sat_ ## NAME ## _type_node \ | |
3781 | || type1 == sat_ ## u ## NAME ## _type_node) \ | |
3782 | return unsignedp ? sat_ ## u ## NAME ## _type_node \ | |
3783 | : sat_ ## NAME ## _type_node; | |
3784 | ||
3785 | C_COMMON_FIXED_TYPES (fract); | |
3786 | C_COMMON_FIXED_TYPES_SAT (fract); | |
3787 | C_COMMON_FIXED_TYPES (accum); | |
3788 | C_COMMON_FIXED_TYPES_SAT (accum); | |
3789 | ||
3790 | C_COMMON_FIXED_MODE_TYPES (qq); | |
3791 | C_COMMON_FIXED_MODE_TYPES (hq); | |
3792 | C_COMMON_FIXED_MODE_TYPES (sq); | |
3793 | C_COMMON_FIXED_MODE_TYPES (dq); | |
3794 | C_COMMON_FIXED_MODE_TYPES (tq); | |
3795 | C_COMMON_FIXED_MODE_TYPES_SAT (qq); | |
3796 | C_COMMON_FIXED_MODE_TYPES_SAT (hq); | |
3797 | C_COMMON_FIXED_MODE_TYPES_SAT (sq); | |
3798 | C_COMMON_FIXED_MODE_TYPES_SAT (dq); | |
3799 | C_COMMON_FIXED_MODE_TYPES_SAT (tq); | |
3800 | C_COMMON_FIXED_MODE_TYPES (ha); | |
3801 | C_COMMON_FIXED_MODE_TYPES (sa); | |
3802 | C_COMMON_FIXED_MODE_TYPES (da); | |
3803 | C_COMMON_FIXED_MODE_TYPES (ta); | |
3804 | C_COMMON_FIXED_MODE_TYPES_SAT (ha); | |
3805 | C_COMMON_FIXED_MODE_TYPES_SAT (sa); | |
3806 | C_COMMON_FIXED_MODE_TYPES_SAT (da); | |
3807 | C_COMMON_FIXED_MODE_TYPES_SAT (ta); | |
9421ebb9 | 3808 | |
4f7f7efd | 3809 | /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not |
3810 | the precision; they have precision set to match their range, but | |
3811 | may use a wider mode to match an ABI. If we change modes, we may | |
3812 | wind up with bad conversions. For INTEGER_TYPEs in C, must check | |
3813 | the precision as well, so as to yield correct results for | |
3814 | bit-field types. C++ does not have these separate bit-field | |
3815 | types, and producing a signed or unsigned variant of an | |
3816 | ENUMERAL_TYPE may cause other problems as well. */ | |
3817 | ||
ac265864 | 3818 | if (!INTEGRAL_TYPE_P (type) |
3819 | || TYPE_UNSIGNED (type) == unsignedp) | |
3820 | return type; | |
3821 | ||
4f7f7efd | 3822 | #define TYPE_OK(node) \ |
3823 | (TYPE_MODE (type) == TYPE_MODE (node) \ | |
0c4abe5b | 3824 | && TYPE_PRECISION (type) == TYPE_PRECISION (node)) |
4f7f7efd | 3825 | if (TYPE_OK (signed_char_type_node)) |
20d39783 | 3826 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
4f7f7efd | 3827 | if (TYPE_OK (integer_type_node)) |
20d39783 | 3828 | return unsignedp ? unsigned_type_node : integer_type_node; |
4f7f7efd | 3829 | if (TYPE_OK (short_integer_type_node)) |
20d39783 | 3830 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
4f7f7efd | 3831 | if (TYPE_OK (long_integer_type_node)) |
20d39783 | 3832 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
4f7f7efd | 3833 | if (TYPE_OK (long_long_integer_type_node)) |
20d39783 | 3834 | return (unsignedp ? long_long_unsigned_type_node |
3835 | : long_long_integer_type_node); | |
6388cfe2 | 3836 | if (int128_integer_type_node && TYPE_OK (int128_integer_type_node)) |
3837 | return (unsignedp ? int128_unsigned_type_node | |
3838 | : int128_integer_type_node); | |
4f7f7efd | 3839 | if (TYPE_OK (widest_integer_literal_type_node)) |
20d39783 | 3840 | return (unsignedp ? widest_unsigned_literal_type_node |
3841 | : widest_integer_literal_type_node); | |
ef11801e | 3842 | |
3843 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
4f7f7efd | 3844 | if (TYPE_OK (intTI_type_node)) |
ef11801e | 3845 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; |
3846 | #endif | |
4f7f7efd | 3847 | if (TYPE_OK (intDI_type_node)) |
ef11801e | 3848 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
4f7f7efd | 3849 | if (TYPE_OK (intSI_type_node)) |
ef11801e | 3850 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
4f7f7efd | 3851 | if (TYPE_OK (intHI_type_node)) |
ef11801e | 3852 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
4f7f7efd | 3853 | if (TYPE_OK (intQI_type_node)) |
ef11801e | 3854 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
4f7f7efd | 3855 | #undef TYPE_OK |
ef11801e | 3856 | |
0c4abe5b | 3857 | return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp); |
20d39783 | 3858 | } |
b268e47e | 3859 | |
c0e47fd4 | 3860 | /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */ |
3861 | ||
3862 | tree | |
3863 | c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp) | |
3864 | { | |
3865 | /* Extended integer types of the same width as a standard type have | |
3866 | lesser rank, so those of the same width as int promote to int or | |
3867 | unsigned int and are valid for printf formats expecting int or | |
3868 | unsigned int. To avoid such special cases, avoid creating | |
3869 | extended integer types for bit-fields if a standard integer type | |
3870 | is available. */ | |
3871 | if (width == TYPE_PRECISION (integer_type_node)) | |
3872 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3873 | if (width == TYPE_PRECISION (signed_char_type_node)) | |
3874 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3875 | if (width == TYPE_PRECISION (short_integer_type_node)) | |
3876 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3877 | if (width == TYPE_PRECISION (long_integer_type_node)) | |
3878 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3879 | if (width == TYPE_PRECISION (long_long_integer_type_node)) | |
3880 | return (unsignedp ? long_long_unsigned_type_node | |
3881 | : long_long_integer_type_node); | |
6388cfe2 | 3882 | if (int128_integer_type_node |
3883 | && width == TYPE_PRECISION (int128_integer_type_node)) | |
3884 | return (unsignedp ? int128_unsigned_type_node | |
3885 | : int128_integer_type_node); | |
c0e47fd4 | 3886 | return build_nonstandard_integer_type (width, unsignedp); |
3887 | } | |
3888 | ||
b268e47e | 3889 | /* The C version of the register_builtin_type langhook. */ |
3890 | ||
3891 | void | |
3892 | c_register_builtin_type (tree type, const char* name) | |
3893 | { | |
3894 | tree decl; | |
3895 | ||
e60a6f7b | 3896 | decl = build_decl (UNKNOWN_LOCATION, |
3897 | TYPE_DECL, get_identifier (name), type); | |
b268e47e | 3898 | DECL_ARTIFICIAL (decl) = 1; |
3899 | if (!TYPE_NAME (type)) | |
3900 | TYPE_NAME (type) = decl; | |
3901 | pushdecl (decl); | |
5b247e9f | 3902 | |
3903 | registered_builtin_types = tree_cons (0, type, registered_builtin_types); | |
b268e47e | 3904 | } |
a9b9d10c | 3905 | \f |
aff9e656 | 3906 | /* Print an error message for invalid operands to arith operation |
8e70fb09 | 3907 | CODE with TYPE0 for operand 0, and TYPE1 for operand 1. |
3908 | LOCATION is the location of the message. */ | |
b0fc3e72 | 3909 | |
3910 | void | |
8e70fb09 | 3911 | binary_op_error (location_t location, enum tree_code code, |
3912 | tree type0, tree type1) | |
b0fc3e72 | 3913 | { |
19cb6b50 | 3914 | const char *opname; |
f03946e4 | 3915 | |
b0fc3e72 | 3916 | switch (code) |
3917 | { | |
b0fc3e72 | 3918 | case PLUS_EXPR: |
3919 | opname = "+"; break; | |
3920 | case MINUS_EXPR: | |
3921 | opname = "-"; break; | |
3922 | case MULT_EXPR: | |
3923 | opname = "*"; break; | |
3924 | case MAX_EXPR: | |
3925 | opname = "max"; break; | |
3926 | case MIN_EXPR: | |
3927 | opname = "min"; break; | |
3928 | case EQ_EXPR: | |
3929 | opname = "=="; break; | |
3930 | case NE_EXPR: | |
3931 | opname = "!="; break; | |
3932 | case LE_EXPR: | |
3933 | opname = "<="; break; | |
3934 | case GE_EXPR: | |
3935 | opname = ">="; break; | |
3936 | case LT_EXPR: | |
3937 | opname = "<"; break; | |
3938 | case GT_EXPR: | |
3939 | opname = ">"; break; | |
3940 | case LSHIFT_EXPR: | |
3941 | opname = "<<"; break; | |
3942 | case RSHIFT_EXPR: | |
3943 | opname = ">>"; break; | |
3944 | case TRUNC_MOD_EXPR: | |
66618a1e | 3945 | case FLOOR_MOD_EXPR: |
b0fc3e72 | 3946 | opname = "%"; break; |
3947 | case TRUNC_DIV_EXPR: | |
66618a1e | 3948 | case FLOOR_DIV_EXPR: |
b0fc3e72 | 3949 | opname = "/"; break; |
3950 | case BIT_AND_EXPR: | |
3951 | opname = "&"; break; | |
3952 | case BIT_IOR_EXPR: | |
3953 | opname = "|"; break; | |
3954 | case TRUTH_ANDIF_EXPR: | |
3955 | opname = "&&"; break; | |
3956 | case TRUTH_ORIF_EXPR: | |
3957 | opname = "||"; break; | |
3958 | case BIT_XOR_EXPR: | |
3959 | opname = "^"; break; | |
31f820d2 | 3960 | default: |
315ba355 | 3961 | gcc_unreachable (); |
b0fc3e72 | 3962 | } |
8e70fb09 | 3963 | error_at (location, |
3964 | "invalid operands to binary %s (have %qT and %qT)", opname, | |
3965 | type0, type1); | |
b0fc3e72 | 3966 | } |
3967 | \f | |
03fe1dc2 | 3968 | /* Given an expression as a tree, return its original type. Do this |
3969 | by stripping any conversion that preserves the sign and precision. */ | |
3970 | static tree | |
3971 | expr_original_type (tree expr) | |
3972 | { | |
3973 | STRIP_SIGN_NOPS (expr); | |
3974 | return TREE_TYPE (expr); | |
3975 | } | |
3976 | ||
b0fc3e72 | 3977 | /* Subroutine of build_binary_op, used for comparison operations. |
3978 | See if the operands have both been converted from subword integer types | |
3979 | and, if so, perhaps change them both back to their original type. | |
5b511807 | 3980 | This function is also responsible for converting the two operands |
3981 | to the proper common type for comparison. | |
b0fc3e72 | 3982 | |
3983 | The arguments of this function are all pointers to local variables | |
3984 | of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, | |
3985 | RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. | |
3986 | ||
2623625f | 3987 | LOC is the location of the comparison. |
3988 | ||
b0fc3e72 | 3989 | If this function returns nonzero, it means that the comparison has |
3990 | a constant value. What this function returns is an expression for | |
3991 | that value. */ | |
3992 | ||
3993 | tree | |
2623625f | 3994 | shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr, |
3995 | tree *restype_ptr, enum tree_code *rescode_ptr) | |
b0fc3e72 | 3996 | { |
19cb6b50 | 3997 | tree type; |
b0fc3e72 | 3998 | tree op0 = *op0_ptr; |
3999 | tree op1 = *op1_ptr; | |
4000 | int unsignedp0, unsignedp1; | |
4001 | int real1, real2; | |
4002 | tree primop0, primop1; | |
4003 | enum tree_code code = *rescode_ptr; | |
4004 | ||
4005 | /* Throw away any conversions to wider types | |
4006 | already present in the operands. */ | |
4007 | ||
7f506bca | 4008 | primop0 = c_common_get_narrower (op0, &unsignedp0); |
4009 | primop1 = c_common_get_narrower (op1, &unsignedp1); | |
b0fc3e72 | 4010 | |
119d06b2 | 4011 | /* If primopN is first sign-extended from primopN's precision to opN's |
4012 | precision, then zero-extended from opN's precision to | |
4013 | *restype_ptr precision, shortenings might be invalid. */ | |
4014 | if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0)) | |
4015 | && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr) | |
4016 | && !unsignedp0 | |
4017 | && TYPE_UNSIGNED (TREE_TYPE (op0))) | |
4018 | primop0 = op0; | |
4019 | if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1)) | |
4020 | && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr) | |
4021 | && !unsignedp1 | |
4022 | && TYPE_UNSIGNED (TREE_TYPE (op1))) | |
4023 | primop1 = op1; | |
4024 | ||
b0fc3e72 | 4025 | /* Handle the case that OP0 does not *contain* a conversion |
4026 | but it *requires* conversion to FINAL_TYPE. */ | |
4027 | ||
4028 | if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) | |
78a8ed03 | 4029 | unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
b0fc3e72 | 4030 | if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) |
78a8ed03 | 4031 | unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1)); |
b0fc3e72 | 4032 | |
4033 | /* If one of the operands must be floated, we cannot optimize. */ | |
4034 | real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; | |
4035 | real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; | |
4036 | ||
4037 | /* If first arg is constant, swap the args (changing operation | |
2bd278cc | 4038 | so value is preserved), for canonicalization. Don't do this if |
4039 | the second arg is 0. */ | |
b0fc3e72 | 4040 | |
2bd278cc | 4041 | if (TREE_CONSTANT (primop0) |
9421ebb9 | 4042 | && !integer_zerop (primop1) && !real_zerop (primop1) |
4043 | && !fixed_zerop (primop1)) | |
b0fc3e72 | 4044 | { |
19cb6b50 | 4045 | tree tem = primop0; |
4046 | int temi = unsignedp0; | |
b0fc3e72 | 4047 | primop0 = primop1; |
4048 | primop1 = tem; | |
4049 | tem = op0; | |
4050 | op0 = op1; | |
4051 | op1 = tem; | |
4052 | *op0_ptr = op0; | |
4053 | *op1_ptr = op1; | |
4054 | unsignedp0 = unsignedp1; | |
4055 | unsignedp1 = temi; | |
4056 | temi = real1; | |
4057 | real1 = real2; | |
4058 | real2 = temi; | |
4059 | ||
4060 | switch (code) | |
4061 | { | |
4062 | case LT_EXPR: | |
4063 | code = GT_EXPR; | |
4064 | break; | |
4065 | case GT_EXPR: | |
4066 | code = LT_EXPR; | |
4067 | break; | |
4068 | case LE_EXPR: | |
4069 | code = GE_EXPR; | |
4070 | break; | |
4071 | case GE_EXPR: | |
4072 | code = LE_EXPR; | |
4073 | break; | |
31f820d2 | 4074 | default: |
4075 | break; | |
b0fc3e72 | 4076 | } |
4077 | *rescode_ptr = code; | |
4078 | } | |
4079 | ||
4080 | /* If comparing an integer against a constant more bits wide, | |
4081 | maybe we can deduce a value of 1 or 0 independent of the data. | |
4082 | Or else truncate the constant now | |
4083 | rather than extend the variable at run time. | |
4084 | ||
4085 | This is only interesting if the constant is the wider arg. | |
4086 | Also, it is not safe if the constant is unsigned and the | |
4087 | variable arg is signed, since in this case the variable | |
4088 | would be sign-extended and then regarded as unsigned. | |
4089 | Our technique fails in this case because the lowest/highest | |
4090 | possible unsigned results don't follow naturally from the | |
4091 | lowest/highest possible values of the variable operand. | |
4092 | For just EQ_EXPR and NE_EXPR there is another technique that | |
4093 | could be used: see if the constant can be faithfully represented | |
4094 | in the other operand's type, by truncating it and reextending it | |
4095 | and see if that preserves the constant's value. */ | |
4096 | ||
4097 | if (!real1 && !real2 | |
9421ebb9 | 4098 | && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE |
b0fc3e72 | 4099 | && TREE_CODE (primop1) == INTEGER_CST |
4100 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) | |
4101 | { | |
4102 | int min_gt, max_gt, min_lt, max_lt; | |
4103 | tree maxval, minval; | |
4104 | /* 1 if comparison is nominally unsigned. */ | |
78a8ed03 | 4105 | int unsignedp = TYPE_UNSIGNED (*restype_ptr); |
b0fc3e72 | 4106 | tree val; |
4107 | ||
4070745f | 4108 | type = c_common_signed_or_unsigned_type (unsignedp0, |
4109 | TREE_TYPE (primop0)); | |
cda09c61 | 4110 | |
b0fc3e72 | 4111 | maxval = TYPE_MAX_VALUE (type); |
4112 | minval = TYPE_MIN_VALUE (type); | |
4113 | ||
4114 | if (unsignedp && !unsignedp0) | |
4070745f | 4115 | *restype_ptr = c_common_signed_type (*restype_ptr); |
b0fc3e72 | 4116 | |
4117 | if (TREE_TYPE (primop1) != *restype_ptr) | |
18dbec6f | 4118 | { |
9a5e8086 | 4119 | /* Convert primop1 to target type, but do not introduce |
4120 | additional overflow. We know primop1 is an int_cst. */ | |
c8110c8f | 4121 | primop1 = force_fit_type_double (*restype_ptr, |
d3237426 | 4122 | tree_to_double_int (primop1), |
4123 | 0, TREE_OVERFLOW (primop1)); | |
18dbec6f | 4124 | } |
b0fc3e72 | 4125 | if (type != *restype_ptr) |
4126 | { | |
4127 | minval = convert (*restype_ptr, minval); | |
4128 | maxval = convert (*restype_ptr, maxval); | |
4129 | } | |
4130 | ||
4131 | if (unsignedp && unsignedp0) | |
4132 | { | |
4133 | min_gt = INT_CST_LT_UNSIGNED (primop1, minval); | |
4134 | max_gt = INT_CST_LT_UNSIGNED (primop1, maxval); | |
4135 | min_lt = INT_CST_LT_UNSIGNED (minval, primop1); | |
4136 | max_lt = INT_CST_LT_UNSIGNED (maxval, primop1); | |
4137 | } | |
4138 | else | |
4139 | { | |
4140 | min_gt = INT_CST_LT (primop1, minval); | |
4141 | max_gt = INT_CST_LT (primop1, maxval); | |
4142 | min_lt = INT_CST_LT (minval, primop1); | |
4143 | max_lt = INT_CST_LT (maxval, primop1); | |
4144 | } | |
4145 | ||
4146 | val = 0; | |
4147 | /* This used to be a switch, but Genix compiler can't handle that. */ | |
4148 | if (code == NE_EXPR) | |
4149 | { | |
4150 | if (max_lt || min_gt) | |
3c2239cf | 4151 | val = truthvalue_true_node; |
b0fc3e72 | 4152 | } |
4153 | else if (code == EQ_EXPR) | |
4154 | { | |
4155 | if (max_lt || min_gt) | |
3c2239cf | 4156 | val = truthvalue_false_node; |
b0fc3e72 | 4157 | } |
4158 | else if (code == LT_EXPR) | |
4159 | { | |
4160 | if (max_lt) | |
3c2239cf | 4161 | val = truthvalue_true_node; |
b0fc3e72 | 4162 | if (!min_lt) |
3c2239cf | 4163 | val = truthvalue_false_node; |
b0fc3e72 | 4164 | } |
4165 | else if (code == GT_EXPR) | |
4166 | { | |
4167 | if (min_gt) | |
3c2239cf | 4168 | val = truthvalue_true_node; |
b0fc3e72 | 4169 | if (!max_gt) |
3c2239cf | 4170 | val = truthvalue_false_node; |
b0fc3e72 | 4171 | } |
4172 | else if (code == LE_EXPR) | |
4173 | { | |
4174 | if (!max_gt) | |
3c2239cf | 4175 | val = truthvalue_true_node; |
b0fc3e72 | 4176 | if (min_gt) |
3c2239cf | 4177 | val = truthvalue_false_node; |
b0fc3e72 | 4178 | } |
4179 | else if (code == GE_EXPR) | |
4180 | { | |
4181 | if (!min_lt) | |
3c2239cf | 4182 | val = truthvalue_true_node; |
b0fc3e72 | 4183 | if (max_lt) |
3c2239cf | 4184 | val = truthvalue_false_node; |
b0fc3e72 | 4185 | } |
4186 | ||
4187 | /* If primop0 was sign-extended and unsigned comparison specd, | |
4188 | we did a signed comparison above using the signed type bounds. | |
4189 | But the comparison we output must be unsigned. | |
4190 | ||
4191 | Also, for inequalities, VAL is no good; but if the signed | |
4192 | comparison had *any* fixed result, it follows that the | |
4193 | unsigned comparison just tests the sign in reverse | |
4194 | (positive values are LE, negative ones GE). | |
4195 | So we can generate an unsigned comparison | |
4196 | against an extreme value of the signed type. */ | |
4197 | ||
4198 | if (unsignedp && !unsignedp0) | |
4199 | { | |
4200 | if (val != 0) | |
4201 | switch (code) | |
4202 | { | |
4203 | case LT_EXPR: | |
4204 | case GE_EXPR: | |
4205 | primop1 = TYPE_MIN_VALUE (type); | |
4206 | val = 0; | |
4207 | break; | |
4208 | ||
4209 | case LE_EXPR: | |
4210 | case GT_EXPR: | |
4211 | primop1 = TYPE_MAX_VALUE (type); | |
4212 | val = 0; | |
4213 | break; | |
31f820d2 | 4214 | |
4215 | default: | |
4216 | break; | |
b0fc3e72 | 4217 | } |
11773141 | 4218 | type = c_common_unsigned_type (type); |
b0fc3e72 | 4219 | } |
4220 | ||
734ec290 | 4221 | if (TREE_CODE (primop0) != INTEGER_CST) |
b0fc3e72 | 4222 | { |
3c2239cf | 4223 | if (val == truthvalue_false_node) |
03fe1dc2 | 4224 | warning_at (loc, OPT_Wtype_limits, |
4225 | "comparison is always false due to limited range of data type"); | |
3c2239cf | 4226 | if (val == truthvalue_true_node) |
03fe1dc2 | 4227 | warning_at (loc, OPT_Wtype_limits, |
4228 | "comparison is always true due to limited range of data type"); | |
b0fc3e72 | 4229 | } |
4230 | ||
4231 | if (val != 0) | |
4232 | { | |
4233 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4234 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 4235 | return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); |
b0fc3e72 | 4236 | return val; |
4237 | } | |
4238 | ||
4239 | /* Value is not predetermined, but do the comparison | |
4240 | in the type of the operand that is not constant. | |
4241 | TYPE is already properly set. */ | |
4242 | } | |
c4503c0a | 4243 | |
4244 | /* If either arg is decimal float and the other is float, find the | |
4245 | proper common type to use for comparison. */ | |
4246 | else if (real1 && real2 | |
4247 | && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0))) | |
4248 | || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))) | |
4249 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4250 | ||
b0fc3e72 | 4251 | else if (real1 && real2 |
2203bd5c | 4252 | && (TYPE_PRECISION (TREE_TYPE (primop0)) |
4253 | == TYPE_PRECISION (TREE_TYPE (primop1)))) | |
b0fc3e72 | 4254 | type = TREE_TYPE (primop0); |
4255 | ||
4256 | /* If args' natural types are both narrower than nominal type | |
4257 | and both extend in the same manner, compare them | |
4258 | in the type of the wider arg. | |
4259 | Otherwise must actually extend both to the nominal | |
4260 | common type lest different ways of extending | |
4261 | alter the result. | |
4262 | (eg, (short)-1 == (unsigned short)-1 should be 0.) */ | |
4263 | ||
4264 | else if (unsignedp0 == unsignedp1 && real1 == real2 | |
4265 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) | |
4266 | && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) | |
4267 | { | |
4268 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4070745f | 4269 | type = c_common_signed_or_unsigned_type (unsignedp0 |
78a8ed03 | 4270 | || TYPE_UNSIGNED (*restype_ptr), |
4070745f | 4271 | type); |
b0fc3e72 | 4272 | /* Make sure shorter operand is extended the right way |
4273 | to match the longer operand. */ | |
4070745f | 4274 | primop0 |
4275 | = convert (c_common_signed_or_unsigned_type (unsignedp0, | |
4276 | TREE_TYPE (primop0)), | |
4277 | primop0); | |
4278 | primop1 | |
4279 | = convert (c_common_signed_or_unsigned_type (unsignedp1, | |
4280 | TREE_TYPE (primop1)), | |
4281 | primop1); | |
b0fc3e72 | 4282 | } |
4283 | else | |
4284 | { | |
4285 | /* Here we must do the comparison on the nominal type | |
4286 | using the args exactly as we received them. */ | |
4287 | type = *restype_ptr; | |
4288 | primop0 = op0; | |
4289 | primop1 = op1; | |
4290 | ||
4291 | if (!real1 && !real2 && integer_zerop (primop1) | |
78a8ed03 | 4292 | && TYPE_UNSIGNED (*restype_ptr)) |
b0fc3e72 | 4293 | { |
4294 | tree value = 0; | |
03fe1dc2 | 4295 | /* All unsigned values are >= 0, so we warn. However, |
4296 | if OP0 is a constant that is >= 0, the signedness of | |
4297 | the comparison isn't an issue, so suppress the | |
4298 | warning. */ | |
4299 | bool warn = | |
3df42822 | 4300 | warn_type_limits && !in_system_header_at (loc) |
03fe1dc2 | 4301 | && !(TREE_CODE (primop0) == INTEGER_CST |
4302 | && !TREE_OVERFLOW (convert (c_common_signed_type (type), | |
4303 | primop0))) | |
4304 | /* Do not warn for enumeration types. */ | |
4305 | && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE); | |
4306 | ||
b0fc3e72 | 4307 | switch (code) |
4308 | { | |
4309 | case GE_EXPR: | |
03fe1dc2 | 4310 | if (warn) |
4311 | warning_at (loc, OPT_Wtype_limits, | |
4312 | "comparison of unsigned expression >= 0 is always true"); | |
3c2239cf | 4313 | value = truthvalue_true_node; |
b0fc3e72 | 4314 | break; |
4315 | ||
4316 | case LT_EXPR: | |
03fe1dc2 | 4317 | if (warn) |
4318 | warning_at (loc, OPT_Wtype_limits, | |
4319 | "comparison of unsigned expression < 0 is always false"); | |
3c2239cf | 4320 | value = truthvalue_false_node; |
31f820d2 | 4321 | break; |
4322 | ||
4323 | default: | |
4324 | break; | |
b0fc3e72 | 4325 | } |
4326 | ||
4327 | if (value != 0) | |
4328 | { | |
4329 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4330 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 4331 | return build2 (COMPOUND_EXPR, TREE_TYPE (value), |
4332 | primop0, value); | |
b0fc3e72 | 4333 | return value; |
4334 | } | |
4335 | } | |
4336 | } | |
4337 | ||
4338 | *op0_ptr = convert (type, primop0); | |
4339 | *op1_ptr = convert (type, primop1); | |
4340 | ||
3c2239cf | 4341 | *restype_ptr = truthvalue_type_node; |
b0fc3e72 | 4342 | |
4343 | return 0; | |
4344 | } | |
4345 | \f | |
1c26100f | 4346 | /* Return a tree for the sum or difference (RESULTCODE says which) |
4347 | of pointer PTROP and integer INTOP. */ | |
4348 | ||
4349 | tree | |
389dd41b | 4350 | pointer_int_sum (location_t loc, enum tree_code resultcode, |
42f9a786 | 4351 | tree ptrop, tree intop, bool complain) |
1c26100f | 4352 | { |
add6ee5e | 4353 | tree size_exp, ret; |
1c26100f | 4354 | |
1c26100f | 4355 | /* The result is a pointer of the same type that is being added. */ |
1c26100f | 4356 | tree result_type = TREE_TYPE (ptrop); |
4357 | ||
4358 | if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) | |
4359 | { | |
42f9a786 | 4360 | if (complain && warn_pointer_arith) |
4361 | pedwarn (loc, OPT_Wpointer_arith, | |
4362 | "pointer of type %<void *%> used in arithmetic"); | |
4363 | else if (!complain) | |
4364 | return error_mark_node; | |
1c26100f | 4365 | size_exp = integer_one_node; |
4366 | } | |
4367 | else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) | |
4368 | { | |
42f9a786 | 4369 | if (complain && warn_pointer_arith) |
4370 | pedwarn (loc, OPT_Wpointer_arith, | |
4371 | "pointer to a function used in arithmetic"); | |
4372 | else if (!complain) | |
4373 | return error_mark_node; | |
1c26100f | 4374 | size_exp = integer_one_node; |
4375 | } | |
1c26100f | 4376 | else |
4377 | size_exp = size_in_bytes (TREE_TYPE (result_type)); | |
4378 | ||
add6ee5e | 4379 | /* We are manipulating pointer values, so we don't need to warn |
4380 | about relying on undefined signed overflow. We disable the | |
4381 | warning here because we use integer types so fold won't know that | |
4382 | they are really pointers. */ | |
4383 | fold_defer_overflow_warnings (); | |
4384 | ||
1c26100f | 4385 | /* If what we are about to multiply by the size of the elements |
4386 | contains a constant term, apply distributive law | |
4387 | and multiply that constant term separately. | |
4388 | This helps produce common subexpressions. */ | |
1c26100f | 4389 | if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) |
84166705 | 4390 | && !TREE_CONSTANT (intop) |
1c26100f | 4391 | && TREE_CONSTANT (TREE_OPERAND (intop, 1)) |
4392 | && TREE_CONSTANT (size_exp) | |
4393 | /* If the constant comes from pointer subtraction, | |
4394 | skip this optimization--it would cause an error. */ | |
4395 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE | |
4396 | /* If the constant is unsigned, and smaller than the pointer size, | |
4397 | then we must skip this optimization. This is because it could cause | |
4398 | an overflow error if the constant is negative but INTOP is not. */ | |
84166705 | 4399 | && (!TYPE_UNSIGNED (TREE_TYPE (intop)) |
1c26100f | 4400 | || (TYPE_PRECISION (TREE_TYPE (intop)) |
4401 | == TYPE_PRECISION (TREE_TYPE (ptrop))))) | |
4402 | { | |
4403 | enum tree_code subcode = resultcode; | |
4404 | tree int_type = TREE_TYPE (intop); | |
4405 | if (TREE_CODE (intop) == MINUS_EXPR) | |
4406 | subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); | |
4407 | /* Convert both subexpression types to the type of intop, | |
4408 | because weird cases involving pointer arithmetic | |
4409 | can result in a sum or difference with different type args. */ | |
8e70fb09 | 4410 | ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)), |
4411 | subcode, ptrop, | |
1c26100f | 4412 | convert (int_type, TREE_OPERAND (intop, 1)), 1); |
4413 | intop = convert (int_type, TREE_OPERAND (intop, 0)); | |
4414 | } | |
4415 | ||
4416 | /* Convert the integer argument to a type the same size as sizetype | |
4417 | so the multiply won't overflow spuriously. */ | |
1c26100f | 4418 | if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) |
78a8ed03 | 4419 | || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype)) |
1cae46be | 4420 | intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype), |
78a8ed03 | 4421 | TYPE_UNSIGNED (sizetype)), intop); |
1c26100f | 4422 | |
4423 | /* Replace the integer argument with a suitable product by the object size. | |
c6feb9f1 | 4424 | Do this multiplication as signed, then convert to the appropriate type |
8032877c | 4425 | for the pointer operation and disregard an overflow that occurred only |
c6feb9f1 | 4426 | because of the sign-extension change in the latter conversion. */ |
4427 | { | |
4428 | tree t = build_binary_op (loc, | |
4429 | MULT_EXPR, intop, | |
4430 | convert (TREE_TYPE (intop), size_exp), 1); | |
4431 | intop = convert (sizetype, t); | |
4432 | if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t)) | |
4433 | intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop), | |
4434 | TREE_INT_CST_HIGH (intop)); | |
4435 | } | |
0de36bdb | 4436 | |
499e523f | 4437 | /* Create the sum or difference. */ |
0de36bdb | 4438 | if (resultcode == MINUS_EXPR) |
389dd41b | 4439 | intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop); |
1c26100f | 4440 | |
2cc66f2a | 4441 | ret = fold_build_pointer_plus_loc (loc, ptrop, intop); |
add6ee5e | 4442 | |
4443 | fold_undefer_and_ignore_overflow_warnings (); | |
4444 | ||
4445 | return ret; | |
1c26100f | 4446 | } |
4447 | \f | |
f59e3889 | 4448 | /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded |
4449 | and if NON_CONST is known not to be permitted in an evaluated part | |
4450 | of a constant expression. */ | |
4451 | ||
4452 | tree | |
4453 | c_wrap_maybe_const (tree expr, bool non_const) | |
4454 | { | |
4455 | bool nowarning = TREE_NO_WARNING (expr); | |
4456 | location_t loc = EXPR_LOCATION (expr); | |
4457 | ||
4458 | /* This should never be called for C++. */ | |
4459 | if (c_dialect_cxx ()) | |
4460 | gcc_unreachable (); | |
4461 | ||
4462 | /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */ | |
4463 | STRIP_TYPE_NOPS (expr); | |
4464 | expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr); | |
4465 | C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const; | |
4466 | if (nowarning) | |
4467 | TREE_NO_WARNING (expr) = 1; | |
4468 | protected_set_expr_location (expr, loc); | |
4469 | ||
4470 | return expr; | |
4471 | } | |
4472 | ||
a75b1c71 | 4473 | /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but |
4474 | for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR | |
4475 | around the SAVE_EXPR if needed so that c_fully_fold does not need | |
4476 | to look inside SAVE_EXPRs. */ | |
4477 | ||
4478 | tree | |
4479 | c_save_expr (tree expr) | |
4480 | { | |
4481 | bool maybe_const = true; | |
4482 | if (c_dialect_cxx ()) | |
4483 | return save_expr (expr); | |
4484 | expr = c_fully_fold (expr, false, &maybe_const); | |
4485 | expr = save_expr (expr); | |
4486 | if (!maybe_const) | |
f59e3889 | 4487 | expr = c_wrap_maybe_const (expr, true); |
a75b1c71 | 4488 | return expr; |
4489 | } | |
4490 | ||
6b68e71a | 4491 | /* Return whether EXPR is a declaration whose address can never be |
4492 | NULL. */ | |
4493 | ||
4494 | bool | |
9f627b1a | 4495 | decl_with_nonnull_addr_p (const_tree expr) |
6b68e71a | 4496 | { |
4497 | return (DECL_P (expr) | |
4498 | && (TREE_CODE (expr) == PARM_DECL | |
4499 | || TREE_CODE (expr) == LABEL_DECL | |
4500 | || !DECL_WEAK (expr))); | |
4501 | } | |
4502 | ||
b0fc3e72 | 4503 | /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, |
45a78cc0 | 4504 | or for an `if' or `while' statement or ?..: exp. It should already |
4505 | have been validated to be of suitable type; otherwise, a bad | |
4506 | diagnostic may result. | |
b0fc3e72 | 4507 | |
8e70fb09 | 4508 | The EXPR is located at LOCATION. |
4509 | ||
b0fc3e72 | 4510 | This preparation consists of taking the ordinary |
4511 | representation of an expression expr and producing a valid tree | |
4512 | boolean expression describing whether expr is nonzero. We could | |
3c2239cf | 4513 | simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1), |
b0fc3e72 | 4514 | but we optimize comparisons, &&, ||, and !. |
4515 | ||
3c2239cf | 4516 | The resulting type should always be `truthvalue_type_node'. */ |
b0fc3e72 | 4517 | |
4518 | tree | |
8e70fb09 | 4519 | c_common_truthvalue_conversion (location_t location, tree expr) |
b0fc3e72 | 4520 | { |
b0fc3e72 | 4521 | switch (TREE_CODE (expr)) |
4522 | { | |
318a728f | 4523 | case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR: |
e1232ce2 | 4524 | case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: |
4525 | case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR: | |
4526 | case ORDERED_EXPR: case UNORDERED_EXPR: | |
ce04dcdc | 4527 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4528 | return expr; | |
e60a6f7b | 4529 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
ce04dcdc | 4530 | TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); |
e60a6f7b | 4531 | goto ret; |
ce04dcdc | 4532 | |
b0fc3e72 | 4533 | case TRUTH_ANDIF_EXPR: |
4534 | case TRUTH_ORIF_EXPR: | |
4535 | case TRUTH_AND_EXPR: | |
4536 | case TRUTH_OR_EXPR: | |
31f6e93c | 4537 | case TRUTH_XOR_EXPR: |
ce04dcdc | 4538 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4539 | return expr; | |
e60a6f7b | 4540 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
48e1416a | 4541 | c_common_truthvalue_conversion (location, |
e60a6f7b | 4542 | TREE_OPERAND (expr, 0)), |
4543 | c_common_truthvalue_conversion (location, | |
4544 | TREE_OPERAND (expr, 1))); | |
4545 | goto ret; | |
3e851b85 | 4546 | |
f6e28f72 | 4547 | case TRUTH_NOT_EXPR: |
ce04dcdc | 4548 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4549 | return expr; | |
e60a6f7b | 4550 | expr = build1 (TREE_CODE (expr), truthvalue_type_node, |
4551 | c_common_truthvalue_conversion (location, | |
4552 | TREE_OPERAND (expr, 0))); | |
4553 | goto ret; | |
f6e28f72 | 4554 | |
b0fc3e72 | 4555 | case ERROR_MARK: |
4556 | return expr; | |
4557 | ||
4558 | case INTEGER_CST: | |
eddad94a | 4559 | return integer_zerop (expr) ? truthvalue_false_node |
4560 | : truthvalue_true_node; | |
b0fc3e72 | 4561 | |
4562 | case REAL_CST: | |
5000e21c | 4563 | return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0) |
4564 | ? truthvalue_true_node | |
4565 | : truthvalue_false_node; | |
b0fc3e72 | 4566 | |
9421ebb9 | 4567 | case FIXED_CST: |
4568 | return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr), | |
4569 | &FCONST0 (TYPE_MODE (TREE_TYPE (expr)))) | |
4570 | ? truthvalue_true_node | |
4571 | : truthvalue_false_node; | |
4572 | ||
ce04dcdc | 4573 | case FUNCTION_DECL: |
b6889cb0 | 4574 | expr = build_unary_op (location, ADDR_EXPR, expr, 0); |
ce04dcdc | 4575 | /* Fall through. */ |
4576 | ||
b0fc3e72 | 4577 | case ADDR_EXPR: |
ce871053 | 4578 | { |
ee7d4d6a | 4579 | tree inner = TREE_OPERAND (expr, 0); |
6b68e71a | 4580 | if (decl_with_nonnull_addr_p (inner)) |
ce871053 | 4581 | { |
6b68e71a | 4582 | /* Common Ada/Pascal programmer's mistake. */ |
8e70fb09 | 4583 | warning_at (location, |
4584 | OPT_Waddress, | |
4585 | "the address of %qD will always evaluate as %<true%>", | |
4586 | inner); | |
ce871053 | 4587 | return truthvalue_true_node; |
4588 | } | |
d473d901 | 4589 | break; |
ce871053 | 4590 | } |
b0fc3e72 | 4591 | |
2203bd5c | 4592 | case COMPLEX_EXPR: |
e60a6f7b | 4593 | expr = build_binary_op (EXPR_LOCATION (expr), |
8e70fb09 | 4594 | (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) |
95809de4 | 4595 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
8e70fb09 | 4596 | c_common_truthvalue_conversion (location, |
4597 | TREE_OPERAND (expr, 0)), | |
4598 | c_common_truthvalue_conversion (location, | |
4599 | TREE_OPERAND (expr, 1)), | |
2203bd5c | 4600 | 0); |
e60a6f7b | 4601 | goto ret; |
2203bd5c | 4602 | |
b0fc3e72 | 4603 | case NEGATE_EXPR: |
4604 | case ABS_EXPR: | |
4605 | case FLOAT_EXPR: | |
c6418a4e | 4606 | case EXCESS_PRECISION_EXPR: |
d10cfa8d | 4607 | /* These don't change whether an object is nonzero or zero. */ |
8e70fb09 | 4608 | return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); |
b0fc3e72 | 4609 | |
4610 | case LROTATE_EXPR: | |
4611 | case RROTATE_EXPR: | |
d10cfa8d | 4612 | /* These don't change whether an object is zero or nonzero, but |
b0fc3e72 | 4613 | we can't ignore them if their second arg has side-effects. */ |
4614 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) | |
e60a6f7b | 4615 | { |
4616 | expr = build2 (COMPOUND_EXPR, truthvalue_type_node, | |
4617 | TREE_OPERAND (expr, 1), | |
48e1416a | 4618 | c_common_truthvalue_conversion |
e60a6f7b | 4619 | (location, TREE_OPERAND (expr, 0))); |
4620 | goto ret; | |
4621 | } | |
b0fc3e72 | 4622 | else |
8e70fb09 | 4623 | return c_common_truthvalue_conversion (location, |
4624 | TREE_OPERAND (expr, 0)); | |
73be5127 | 4625 | |
b0fc3e72 | 4626 | case COND_EXPR: |
4627 | /* Distribute the conversion into the arms of a COND_EXPR. */ | |
a75b1c71 | 4628 | if (c_dialect_cxx ()) |
e60a6f7b | 4629 | { |
d0389adc | 4630 | tree op1 = TREE_OPERAND (expr, 1); |
4631 | tree op2 = TREE_OPERAND (expr, 2); | |
4632 | /* In C++ one of the arms might have void type if it is throw. */ | |
4633 | if (!VOID_TYPE_P (TREE_TYPE (op1))) | |
4634 | op1 = c_common_truthvalue_conversion (location, op1); | |
4635 | if (!VOID_TYPE_P (TREE_TYPE (op2))) | |
4636 | op2 = c_common_truthvalue_conversion (location, op2); | |
389dd41b | 4637 | expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node, |
d0389adc | 4638 | TREE_OPERAND (expr, 0), op1, op2); |
e60a6f7b | 4639 | goto ret; |
4640 | } | |
a75b1c71 | 4641 | else |
e60a6f7b | 4642 | { |
4643 | /* Folding will happen later for C. */ | |
4644 | expr = build3 (COND_EXPR, truthvalue_type_node, | |
4645 | TREE_OPERAND (expr, 0), | |
4646 | c_common_truthvalue_conversion (location, | |
4647 | TREE_OPERAND (expr, 1)), | |
4648 | c_common_truthvalue_conversion (location, | |
4649 | TREE_OPERAND (expr, 2))); | |
4650 | goto ret; | |
4651 | } | |
b0fc3e72 | 4652 | |
72dd6141 | 4653 | CASE_CONVERT: |
f8913d47 | 4654 | { |
4655 | tree totype = TREE_TYPE (expr); | |
4656 | tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0)); | |
4657 | ||
4658 | /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, | |
4659 | since that affects how `default_conversion' will behave. */ | |
4660 | if (TREE_CODE (totype) == REFERENCE_TYPE | |
4661 | || TREE_CODE (fromtype) == REFERENCE_TYPE) | |
4662 | break; | |
4663 | /* Don't strip a conversion from C++0x scoped enum, since they | |
4664 | don't implicitly convert to other types. */ | |
4665 | if (TREE_CODE (fromtype) == ENUMERAL_TYPE | |
4666 | && ENUM_IS_SCOPED (fromtype)) | |
4667 | break; | |
4668 | /* If this isn't narrowing the argument, we can ignore it. */ | |
4669 | if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype)) | |
4670 | return c_common_truthvalue_conversion (location, | |
4671 | TREE_OPERAND (expr, 0)); | |
4672 | } | |
b0fc3e72 | 4673 | break; |
4674 | ||
16837b18 | 4675 | case MODIFY_EXPR: |
60a0513e | 4676 | if (!TREE_NO_WARNING (expr) |
4677 | && warn_parentheses) | |
4678 | { | |
4679 | warning (OPT_Wparentheses, | |
4680 | "suggest parentheses around assignment used as truth value"); | |
4681 | TREE_NO_WARNING (expr) = 1; | |
4682 | } | |
16837b18 | 4683 | break; |
73be5127 | 4684 | |
31f820d2 | 4685 | default: |
4686 | break; | |
b0fc3e72 | 4687 | } |
4688 | ||
2ba726d2 | 4689 | if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) |
a0748b7d | 4690 | { |
93be21c0 | 4691 | tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr)); |
e60a6f7b | 4692 | expr = (build_binary_op |
8e70fb09 | 4693 | (EXPR_LOCATION (expr), |
4694 | (TREE_SIDE_EFFECTS (expr) | |
a0748b7d | 4695 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
b6889cb0 | 4696 | c_common_truthvalue_conversion |
4697 | (location, | |
4698 | build_unary_op (location, REALPART_EXPR, t, 0)), | |
4699 | c_common_truthvalue_conversion | |
4700 | (location, | |
4701 | build_unary_op (location, IMAGPART_EXPR, t, 0)), | |
a0748b7d | 4702 | 0)); |
e60a6f7b | 4703 | goto ret; |
a0748b7d | 4704 | } |
2ba726d2 | 4705 | |
9421ebb9 | 4706 | if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE) |
4707 | { | |
4708 | tree fixed_zero_node = build_fixed (TREE_TYPE (expr), | |
4709 | FCONST0 (TYPE_MODE | |
4710 | (TREE_TYPE (expr)))); | |
43158006 | 4711 | return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1); |
9421ebb9 | 4712 | } |
e60a6f7b | 4713 | else |
4714 | return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1); | |
9421ebb9 | 4715 | |
e60a6f7b | 4716 | ret: |
4717 | protected_set_expr_location (expr, location); | |
4718 | return expr; | |
b0fc3e72 | 4719 | } |
4720 | \f | |
3237155d | 4721 | static void def_builtin_1 (enum built_in_function fncode, |
4722 | const char *name, | |
4723 | enum built_in_class fnclass, | |
4724 | tree fntype, tree libtype, | |
4725 | bool both_p, bool fallback_p, bool nonansi_p, | |
4726 | tree fnattrs, bool implicit_p); | |
0d4238dc | 4727 | |
a5b1863e | 4728 | |
4729 | /* Apply the TYPE_QUALS to the new DECL. */ | |
4730 | ||
4731 | void | |
1cae46be | 4732 | c_apply_type_quals_to_decl (int type_quals, tree decl) |
a5b1863e | 4733 | { |
adfb367f | 4734 | tree type = TREE_TYPE (decl); |
b27ac6b5 | 4735 | |
e4eabbe4 | 4736 | if (type == error_mark_node) |
4737 | return; | |
adfb367f | 4738 | |
98a33d9f | 4739 | if ((type_quals & TYPE_QUAL_CONST) |
4740 | || (type && TREE_CODE (type) == REFERENCE_TYPE)) | |
4741 | /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr | |
4742 | constructor can produce constant init, so rely on cp_finish_decl to | |
4743 | clear TREE_READONLY if the variable has non-constant init. */ | |
a5b1863e | 4744 | TREE_READONLY (decl) = 1; |
4745 | if (type_quals & TYPE_QUAL_VOLATILE) | |
4746 | { | |
4747 | TREE_SIDE_EFFECTS (decl) = 1; | |
4748 | TREE_THIS_VOLATILE (decl) = 1; | |
4749 | } | |
d91a20bc | 4750 | if (type_quals & TYPE_QUAL_RESTRICT) |
a5b1863e | 4751 | { |
adfb367f | 4752 | while (type && TREE_CODE (type) == ARRAY_TYPE) |
4753 | /* Allow 'restrict' on arrays of pointers. | |
4754 | FIXME currently we just ignore it. */ | |
4755 | type = TREE_TYPE (type); | |
4756 | if (!type | |
4757 | || !POINTER_TYPE_P (type) | |
4758 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))) | |
b0b1af64 | 4759 | error ("invalid use of %<restrict%>"); |
a5b1863e | 4760 | } |
4761 | } | |
4762 | ||
4ee9c684 | 4763 | /* Hash function for the problem of multiple type definitions in |
4764 | different files. This must hash all types that will compare | |
4765 | equal via comptypes to the same value. In practice it hashes | |
0bed3869 | 4766 | on some of the simple stuff and leaves the details to comptypes. */ |
4ee9c684 | 4767 | |
4768 | static hashval_t | |
4769 | c_type_hash (const void *p) | |
4770 | { | |
ecf2703d | 4771 | int n_elements; |
4ee9c684 | 4772 | int shift, size; |
aae87fc3 | 4773 | const_tree const t = (const_tree) p; |
4ee9c684 | 4774 | tree t2; |
4775 | switch (TREE_CODE (t)) | |
4776 | { | |
fbf0afd1 | 4777 | /* For pointers, hash on pointee type plus some swizzling. */ |
2363ef00 | 4778 | case POINTER_TYPE: |
4779 | return c_type_hash (TREE_TYPE (t)) ^ 0x3003003; | |
4780 | /* Hash on number of elements and total size. */ | |
4781 | case ENUMERAL_TYPE: | |
4782 | shift = 3; | |
4783 | t2 = TYPE_VALUES (t); | |
4784 | break; | |
4785 | case RECORD_TYPE: | |
4786 | shift = 0; | |
4787 | t2 = TYPE_FIELDS (t); | |
4788 | break; | |
4789 | case QUAL_UNION_TYPE: | |
4790 | shift = 1; | |
4791 | t2 = TYPE_FIELDS (t); | |
4792 | break; | |
4793 | case UNION_TYPE: | |
4794 | shift = 2; | |
4795 | t2 = TYPE_FIELDS (t); | |
4796 | break; | |
4797 | default: | |
231bd014 | 4798 | gcc_unreachable (); |
4ee9c684 | 4799 | } |
ecf2703d | 4800 | /* FIXME: We want to use a DECL_CHAIN iteration method here, but |
4801 | TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */ | |
4802 | n_elements = list_length (t2); | |
78c2e180 | 4803 | /* We might have a VLA here. */ |
4804 | if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST) | |
4805 | size = 0; | |
4806 | else | |
4807 | size = TREE_INT_CST_LOW (TYPE_SIZE (t)); | |
ecf2703d | 4808 | return ((size << 24) | (n_elements << shift)); |
4ee9c684 | 4809 | } |
4810 | ||
1ecd4018 | 4811 | static GTY((param_is (union tree_node))) htab_t type_hash_table; |
4812 | ||
b5ba9f3a | 4813 | /* Return the typed-based alias set for T, which may be an expression |
f7c44134 | 4814 | or a type. Return -1 if we don't do anything special. */ |
b5ba9f3a | 4815 | |
32c2fdea | 4816 | alias_set_type |
1cae46be | 4817 | c_common_get_alias_set (tree t) |
b5ba9f3a | 4818 | { |
be4f2de7 | 4819 | tree u; |
4ee9c684 | 4820 | PTR *slot; |
1cae46be | 4821 | |
e58c17e7 | 4822 | /* For VLAs, use the alias set of the element type rather than the |
4823 | default of alias set 0 for types compared structurally. */ | |
4824 | if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t)) | |
4825 | { | |
4826 | if (TREE_CODE (t) == ARRAY_TYPE) | |
4827 | return get_alias_set (TREE_TYPE (t)); | |
4828 | return -1; | |
4829 | } | |
4830 | ||
be4f2de7 | 4831 | /* Permit type-punning when accessing a union, provided the access |
4832 | is directly through the union. For example, this code does not | |
4833 | permit taking the address of a union member and then storing | |
4834 | through it. Even the type-punning allowed here is a GCC | |
4835 | extension, albeit a common and useful one; the C standard says | |
4836 | that such accesses have implementation-defined behavior. */ | |
4837 | for (u = t; | |
4838 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
4839 | u = TREE_OPERAND (u, 0)) | |
4840 | if (TREE_CODE (u) == COMPONENT_REF | |
4841 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
4842 | return 0; | |
1e2513d9 | 4843 | |
9fcc3e54 | 4844 | /* That's all the expressions we handle specially. */ |
84166705 | 4845 | if (!TYPE_P (t)) |
9fcc3e54 | 4846 | return -1; |
4847 | ||
d716ce75 | 4848 | /* The C standard guarantees that any object may be accessed via an |
9fcc3e54 | 4849 | lvalue that has character type. */ |
4850 | if (t == char_type_node | |
4851 | || t == signed_char_type_node | |
4852 | || t == unsigned_char_type_node) | |
f7c44134 | 4853 | return 0; |
a5b1863e | 4854 | |
1607663f | 4855 | /* The C standard specifically allows aliasing between signed and |
4856 | unsigned variants of the same type. We treat the signed | |
4857 | variant as canonical. */ | |
78a8ed03 | 4858 | if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t)) |
a8868e19 | 4859 | { |
4070745f | 4860 | tree t1 = c_common_signed_type (t); |
1607663f | 4861 | |
a8868e19 | 4862 | /* t1 == t can happen for boolean nodes which are always unsigned. */ |
4863 | if (t1 != t) | |
4864 | return get_alias_set (t1); | |
4865 | } | |
1e2513d9 | 4866 | |
4ee9c684 | 4867 | /* Handle the case of multiple type nodes referring to "the same" type, |
4868 | which occurs with IMA. These share an alias set. FIXME: Currently only | |
4869 | C90 is handled. (In C99 type compatibility is not transitive, which | |
4870 | complicates things mightily. The alias set splay trees can theoretically | |
4871 | represent this, but insertion is tricky when you consider all the | |
4872 | different orders things might arrive in.) */ | |
4873 | ||
4874 | if (c_language != clk_c || flag_isoc99) | |
4875 | return -1; | |
4876 | ||
0bed3869 | 4877 | /* Save time if there's only one input file. */ |
e08bd2f4 | 4878 | if (num_in_fnames == 1) |
4ee9c684 | 4879 | return -1; |
4880 | ||
4881 | /* Pointers need special handling if they point to any type that | |
4882 | needs special handling (below). */ | |
4883 | if (TREE_CODE (t) == POINTER_TYPE) | |
4884 | { | |
4885 | tree t2; | |
4886 | /* Find bottom type under any nested POINTERs. */ | |
b27ac6b5 | 4887 | for (t2 = TREE_TYPE (t); |
af592f67 | 4888 | TREE_CODE (t2) == POINTER_TYPE; |
4889 | t2 = TREE_TYPE (t2)) | |
4890 | ; | |
b27ac6b5 | 4891 | if (TREE_CODE (t2) != RECORD_TYPE |
af592f67 | 4892 | && TREE_CODE (t2) != ENUMERAL_TYPE |
4893 | && TREE_CODE (t2) != QUAL_UNION_TYPE | |
4894 | && TREE_CODE (t2) != UNION_TYPE) | |
4895 | return -1; | |
4ee9c684 | 4896 | if (TYPE_SIZE (t2) == 0) |
af592f67 | 4897 | return -1; |
4ee9c684 | 4898 | } |
4899 | /* These are the only cases that need special handling. */ | |
b27ac6b5 | 4900 | if (TREE_CODE (t) != RECORD_TYPE |
4ee9c684 | 4901 | && TREE_CODE (t) != ENUMERAL_TYPE |
4902 | && TREE_CODE (t) != QUAL_UNION_TYPE | |
4903 | && TREE_CODE (t) != UNION_TYPE | |
4904 | && TREE_CODE (t) != POINTER_TYPE) | |
4905 | return -1; | |
4906 | /* Undefined? */ | |
4907 | if (TYPE_SIZE (t) == 0) | |
4908 | return -1; | |
4909 | ||
b27ac6b5 | 4910 | /* Look up t in hash table. Only one of the compatible types within each |
4ee9c684 | 4911 | alias set is recorded in the table. */ |
4912 | if (!type_hash_table) | |
1ecd4018 | 4913 | type_hash_table = htab_create_ggc (1021, c_type_hash, |
4ee9c684 | 4914 | (htab_eq) lang_hooks.types_compatible_p, |
4915 | NULL); | |
4916 | slot = htab_find_slot (type_hash_table, t, INSERT); | |
4917 | if (*slot != NULL) | |
ad16cb2c | 4918 | { |
4919 | TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot); | |
4920 | return TYPE_ALIAS_SET ((tree)*slot); | |
4921 | } | |
4ee9c684 | 4922 | else |
4923 | /* Our caller will assign and record (in t) a new alias set; all we need | |
4924 | to do is remember t in the hash table. */ | |
4925 | *slot = t; | |
4926 | ||
f7c44134 | 4927 | return -1; |
b5ba9f3a | 4928 | } |
902b4e01 | 4929 | \f |
e60a6f7b | 4930 | /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where |
a179a7dc | 4931 | the IS_SIZEOF parameter indicates which operator is being applied. |
e60a6f7b | 4932 | The COMPLAIN flag controls whether we should diagnose possibly |
4933 | ill-formed constructs or not. LOC is the location of the SIZEOF or | |
a179a7dc | 4934 | TYPEOF operator. If MIN_ALIGNOF, the least alignment required for |
4935 | a type in any context should be returned, rather than the normal | |
4936 | alignment for that type. */ | |
908c697e | 4937 | |
902b4e01 | 4938 | tree |
e60a6f7b | 4939 | c_sizeof_or_alignof_type (location_t loc, |
a179a7dc | 4940 | tree type, bool is_sizeof, bool min_alignof, |
4941 | int complain) | |
902b4e01 | 4942 | { |
d4c4d95c | 4943 | const char *op_name; |
4944 | tree value = NULL; | |
4945 | enum tree_code type_code = TREE_CODE (type); | |
1cae46be | 4946 | |
908c697e | 4947 | op_name = is_sizeof ? "sizeof" : "__alignof__"; |
1cae46be | 4948 | |
d4c4d95c | 4949 | if (type_code == FUNCTION_TYPE) |
902b4e01 | 4950 | { |
908c697e | 4951 | if (is_sizeof) |
d4c4d95c | 4952 | { |
9205a6cc | 4953 | if (complain && warn_pointer_arith) |
4954 | pedwarn (loc, OPT_Wpointer_arith, | |
8864917d | 4955 | "invalid application of %<sizeof%> to a function type"); |
ebd21de4 | 4956 | else if (!complain) |
4957 | return error_mark_node; | |
d4c4d95c | 4958 | value = size_one_node; |
4959 | } | |
4960 | else | |
83e25171 | 4961 | { |
4962 | if (complain) | |
4963 | { | |
4964 | if (c_dialect_cxx ()) | |
29438999 | 4965 | pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit " |
83e25171 | 4966 | "%<alignof%> applied to a function type"); |
4967 | else | |
29438999 | 4968 | pedwarn (loc, OPT_Wpedantic, "ISO C does not permit " |
83e25171 | 4969 | "%<_Alignof%> applied to a function type"); |
4970 | } | |
4971 | value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); | |
4972 | } | |
d4c4d95c | 4973 | } |
4974 | else if (type_code == VOID_TYPE || type_code == ERROR_MARK) | |
4975 | { | |
1cae46be | 4976 | if (type_code == VOID_TYPE |
9205a6cc | 4977 | && complain && warn_pointer_arith) |
4978 | pedwarn (loc, OPT_Wpointer_arith, | |
8864917d | 4979 | "invalid application of %qs to a void type", op_name); |
ebd21de4 | 4980 | else if (!complain) |
4981 | return error_mark_node; | |
d4c4d95c | 4982 | value = size_one_node; |
902b4e01 | 4983 | } |
3df19e1b | 4984 | else if (!COMPLETE_TYPE_P (type) |
4985 | && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE)) | |
902b4e01 | 4986 | { |
d3a4d008 | 4987 | if (complain) |
3df19e1b | 4988 | error_at (loc, "invalid application of %qs to incomplete type %qT", |
e60a6f7b | 4989 | op_name, type); |
9c719c74 | 4990 | return error_mark_node; |
902b4e01 | 4991 | } |
3df19e1b | 4992 | else if (c_dialect_cxx () && type_code == ARRAY_TYPE |
4993 | && !COMPLETE_TYPE_P (TREE_TYPE (type))) | |
4994 | { | |
4995 | if (complain) | |
4996 | error_at (loc, "invalid application of %qs to array type %qT of " | |
4997 | "incomplete element type", op_name, type); | |
4998 | return error_mark_node; | |
4999 | } | |
902b4e01 | 5000 | else |
d4c4d95c | 5001 | { |
908c697e | 5002 | if (is_sizeof) |
d4c4d95c | 5003 | /* Convert in case a char is more than one unit. */ |
389dd41b | 5004 | value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
5005 | size_int (TYPE_PRECISION (char_type_node) | |
5006 | / BITS_PER_UNIT)); | |
a179a7dc | 5007 | else if (min_alignof) |
5008 | { | |
5009 | unsigned int align = TYPE_ALIGN (type); | |
5010 | align = MIN (align, BIGGEST_ALIGNMENT); | |
5011 | #ifdef BIGGEST_FIELD_ALIGNMENT | |
5012 | align = MIN (align, BIGGEST_FIELD_ALIGNMENT); | |
5013 | #endif | |
a179a7dc | 5014 | unsigned int field_align = align; |
5015 | #ifdef ADJUST_FIELD_ALIGN | |
0b26ec77 | 5016 | tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, |
5017 | type); | |
a179a7dc | 5018 | field_align = ADJUST_FIELD_ALIGN (field, field_align); |
5019 | #endif | |
5020 | align = MIN (align, field_align); | |
5021 | value = size_int (align / BITS_PER_UNIT); | |
5022 | } | |
d4c4d95c | 5023 | else |
d37625c0 | 5024 | value = size_int (TYPE_ALIGN_UNIT (type)); |
d4c4d95c | 5025 | } |
902b4e01 | 5026 | |
5a1fe2db | 5027 | /* VALUE will have the middle-end integer type sizetype. |
5028 | However, we should really return a value of type `size_t', | |
5029 | which is just a typedef for an ordinary integer type. */ | |
389dd41b | 5030 | value = fold_convert_loc (loc, size_type_node, value); |
1cae46be | 5031 | |
d4c4d95c | 5032 | return value; |
902b4e01 | 5033 | } |
5034 | ||
5035 | /* Implement the __alignof keyword: Return the minimum required | |
097b5c8b | 5036 | alignment of EXPR, measured in bytes. For VAR_DECLs, |
5037 | FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set | |
e60a6f7b | 5038 | from an "aligned" __attribute__ specification). LOC is the |
5039 | location of the ALIGNOF operator. */ | |
72040e7e | 5040 | |
902b4e01 | 5041 | tree |
e60a6f7b | 5042 | c_alignof_expr (location_t loc, tree expr) |
902b4e01 | 5043 | { |
5044 | tree t; | |
5045 | ||
097b5c8b | 5046 | if (VAR_OR_FUNCTION_DECL_P (expr)) |
d37625c0 | 5047 | t = size_int (DECL_ALIGN_UNIT (expr)); |
1cae46be | 5048 | |
902b4e01 | 5049 | else if (TREE_CODE (expr) == COMPONENT_REF |
5050 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
5051 | { | |
e60a6f7b | 5052 | error_at (loc, "%<__alignof%> applied to a bit-field"); |
902b4e01 | 5053 | t = size_one_node; |
5054 | } | |
5055 | else if (TREE_CODE (expr) == COMPONENT_REF | |
7cc7e163 | 5056 | && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) |
d37625c0 | 5057 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1))); |
1cae46be | 5058 | |
902b4e01 | 5059 | else if (TREE_CODE (expr) == INDIRECT_REF) |
5060 | { | |
5061 | tree t = TREE_OPERAND (expr, 0); | |
5062 | tree best = t; | |
5063 | int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
1cae46be | 5064 | |
72dd6141 | 5065 | while (CONVERT_EXPR_P (t) |
7cc7e163 | 5066 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) |
902b4e01 | 5067 | { |
5068 | int thisalign; | |
5069 | ||
5070 | t = TREE_OPERAND (t, 0); | |
5071 | thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
5072 | if (thisalign > bestalign) | |
5073 | best = t, bestalign = thisalign; | |
5074 | } | |
e60a6f7b | 5075 | return c_alignof (loc, TREE_TYPE (TREE_TYPE (best))); |
902b4e01 | 5076 | } |
5077 | else | |
e60a6f7b | 5078 | return c_alignof (loc, TREE_TYPE (expr)); |
902b4e01 | 5079 | |
389dd41b | 5080 | return fold_convert_loc (loc, size_type_node, t); |
902b4e01 | 5081 | } |
5082 | \f | |
8fe4a266 | 5083 | /* Handle C and C++ default attributes. */ |
5084 | ||
5085 | enum built_in_attribute | |
5086 | { | |
5087 | #define DEF_ATTR_NULL_TREE(ENUM) ENUM, | |
5088 | #define DEF_ATTR_INT(ENUM, VALUE) ENUM, | |
c8010b80 | 5089 | #define DEF_ATTR_STRING(ENUM, VALUE) ENUM, |
8fe4a266 | 5090 | #define DEF_ATTR_IDENT(ENUM, STRING) ENUM, |
5091 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM, | |
8fe4a266 | 5092 | #include "builtin-attrs.def" |
5093 | #undef DEF_ATTR_NULL_TREE | |
5094 | #undef DEF_ATTR_INT | |
c8010b80 | 5095 | #undef DEF_ATTR_STRING |
8fe4a266 | 5096 | #undef DEF_ATTR_IDENT |
5097 | #undef DEF_ATTR_TREE_LIST | |
8fe4a266 | 5098 | ATTR_LAST |
5099 | }; | |
5100 | ||
5101 | static GTY(()) tree built_in_attributes[(int) ATTR_LAST]; | |
5102 | ||
1cae46be | 5103 | static void c_init_attributes (void); |
8fe4a266 | 5104 | |
27213ba3 | 5105 | enum c_builtin_type |
72040e7e | 5106 | { |
d2d4bdde | 5107 | #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME, |
5108 | #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME, | |
5109 | #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME, | |
5110 | #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME, | |
5111 | #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, | |
5112 | #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, | |
0a39fd54 | 5113 | #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME, |
27213ba3 | 5114 | #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME, |
5115 | #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME, | |
bc7bff74 | 5116 | #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME, |
d2d4bdde | 5117 | #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME, |
5118 | #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME, | |
5119 | #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME, | |
03901330 | 5120 | #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, |
0a39fd54 | 5121 | #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, |
5122 | #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \ | |
5123 | NAME, | |
d2d4bdde | 5124 | #define DEF_POINTER_TYPE(NAME, TYPE) NAME, |
5125 | #include "builtin-types.def" | |
5126 | #undef DEF_PRIMITIVE_TYPE | |
5127 | #undef DEF_FUNCTION_TYPE_0 | |
5128 | #undef DEF_FUNCTION_TYPE_1 | |
5129 | #undef DEF_FUNCTION_TYPE_2 | |
5130 | #undef DEF_FUNCTION_TYPE_3 | |
5131 | #undef DEF_FUNCTION_TYPE_4 | |
0a39fd54 | 5132 | #undef DEF_FUNCTION_TYPE_5 |
5133 | #undef DEF_FUNCTION_TYPE_6 | |
27213ba3 | 5134 | #undef DEF_FUNCTION_TYPE_7 |
bc7bff74 | 5135 | #undef DEF_FUNCTION_TYPE_8 |
d2d4bdde | 5136 | #undef DEF_FUNCTION_TYPE_VAR_0 |
5137 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
5138 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
03901330 | 5139 | #undef DEF_FUNCTION_TYPE_VAR_3 |
0a39fd54 | 5140 | #undef DEF_FUNCTION_TYPE_VAR_4 |
5141 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
d2d4bdde | 5142 | #undef DEF_POINTER_TYPE |
27213ba3 | 5143 | BT_LAST |
5144 | }; | |
5145 | ||
5146 | typedef enum c_builtin_type builtin_type; | |
d2d4bdde | 5147 | |
27213ba3 | 5148 | /* A temporary array for c_common_nodes_and_builtins. Used in |
5149 | communication with def_fn_type. */ | |
5150 | static tree builtin_types[(int) BT_LAST + 1]; | |
d2d4bdde | 5151 | |
27213ba3 | 5152 | /* A helper function for c_common_nodes_and_builtins. Build function type |
5153 | for DEF with return type RET and N arguments. If VAR is true, then the | |
5154 | function should be variadic after those N arguments. | |
5155 | ||
5156 | Takes special care not to ICE if any of the types involved are | |
5157 | error_mark_node, which indicates that said type is not in fact available | |
5158 | (see builtin_type_for_size). In which case the function type as a whole | |
5159 | should be error_mark_node. */ | |
5160 | ||
5161 | static void | |
5162 | def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...) | |
5163 | { | |
3a939d12 | 5164 | tree t; |
5165 | tree *args = XALLOCAVEC (tree, n); | |
27213ba3 | 5166 | va_list list; |
5167 | int i; | |
5168 | ||
5169 | va_start (list, n); | |
5170 | for (i = 0; i < n; ++i) | |
5171 | { | |
7d339f93 | 5172 | builtin_type a = (builtin_type) va_arg (list, int); |
27213ba3 | 5173 | t = builtin_types[a]; |
5174 | if (t == error_mark_node) | |
5175 | goto egress; | |
3a939d12 | 5176 | args[i] = t; |
27213ba3 | 5177 | } |
27213ba3 | 5178 | |
27213ba3 | 5179 | t = builtin_types[ret]; |
5180 | if (t == error_mark_node) | |
5181 | goto egress; | |
3a939d12 | 5182 | if (var) |
5183 | t = build_varargs_function_type_array (t, n, args); | |
5184 | else | |
5185 | t = build_function_type_array (t, n, args); | |
27213ba3 | 5186 | |
5187 | egress: | |
5188 | builtin_types[def] = t; | |
451c8e2f | 5189 | va_end (list); |
27213ba3 | 5190 | } |
5191 | ||
dce22712 | 5192 | /* Build builtin functions common to both C and C++ language |
5193 | frontends. */ | |
5194 | ||
5195 | static void | |
5196 | c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node) | |
5197 | { | |
5198 | #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ | |
5199 | builtin_types[ENUM] = VALUE; | |
5200 | #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ | |
5201 | def_fn_type (ENUM, RETURN, 0, 0); | |
5202 | #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ | |
5203 | def_fn_type (ENUM, RETURN, 0, 1, ARG1); | |
5204 | #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ | |
5205 | def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2); | |
5206 | #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
5207 | def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3); | |
5208 | #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
5209 | def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4); | |
5210 | #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
5211 | def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
5212 | #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5213 | ARG6) \ | |
5214 | def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); | |
5215 | #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5216 | ARG6, ARG7) \ | |
5217 | def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); | |
bc7bff74 | 5218 | #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5219 | ARG6, ARG7, ARG8) \ | |
5220 | def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \ | |
5221 | ARG7, ARG8); | |
dce22712 | 5222 | #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ |
5223 | def_fn_type (ENUM, RETURN, 1, 0); | |
5224 | #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ | |
5225 | def_fn_type (ENUM, RETURN, 1, 1, ARG1); | |
5226 | #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ | |
5227 | def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2); | |
5228 | #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
5229 | def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3); | |
5230 | #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
5231 | def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4); | |
5232 | #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
5233 | def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
5234 | #define DEF_POINTER_TYPE(ENUM, TYPE) \ | |
5235 | builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]); | |
5236 | ||
5237 | #include "builtin-types.def" | |
5238 | ||
5239 | #undef DEF_PRIMITIVE_TYPE | |
5240 | #undef DEF_FUNCTION_TYPE_1 | |
5241 | #undef DEF_FUNCTION_TYPE_2 | |
5242 | #undef DEF_FUNCTION_TYPE_3 | |
5243 | #undef DEF_FUNCTION_TYPE_4 | |
5244 | #undef DEF_FUNCTION_TYPE_5 | |
5245 | #undef DEF_FUNCTION_TYPE_6 | |
5246 | #undef DEF_FUNCTION_TYPE_VAR_0 | |
5247 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
5248 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
5249 | #undef DEF_FUNCTION_TYPE_VAR_3 | |
5250 | #undef DEF_FUNCTION_TYPE_VAR_4 | |
5251 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
5252 | #undef DEF_POINTER_TYPE | |
5253 | builtin_types[(int) BT_LAST] = NULL_TREE; | |
5254 | ||
5255 | c_init_attributes (); | |
5256 | ||
5257 | #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ | |
5258 | NONANSI_P, ATTRS, IMPLICIT, COND) \ | |
5259 | if (NAME && COND) \ | |
5260 | def_builtin_1 (ENUM, NAME, CLASS, \ | |
5261 | builtin_types[(int) TYPE], \ | |
5262 | builtin_types[(int) LIBTYPE], \ | |
5263 | BOTH_P, FALLBACK_P, NONANSI_P, \ | |
5264 | built_in_attributes[(int) ATTRS], IMPLICIT); | |
5265 | #include "builtins.def" | |
5266 | #undef DEF_BUILTIN | |
5267 | ||
87eb1c28 | 5268 | targetm.init_builtins (); |
5269 | ||
471eff36 | 5270 | build_common_builtin_nodes (); |
d037099f | 5271 | |
a89e6c15 | 5272 | if (flag_cilkplus) |
d037099f | 5273 | cilk_init_builtins (); |
dce22712 | 5274 | } |
5275 | ||
9e6687c8 | 5276 | /* Like get_identifier, but avoid warnings about null arguments when |
5277 | the argument may be NULL for targets where GCC lacks stdint.h type | |
5278 | information. */ | |
5279 | ||
5280 | static inline tree | |
5281 | c_get_ident (const char *id) | |
5282 | { | |
5283 | return get_identifier (id); | |
5284 | } | |
5285 | ||
27213ba3 | 5286 | /* Build tree nodes and builtin functions common to both C and C++ language |
5287 | frontends. */ | |
5288 | ||
5289 | void | |
5290 | c_common_nodes_and_builtins (void) | |
5291 | { | |
924bbf02 | 5292 | int char16_type_size; |
5293 | int char32_type_size; | |
174fcc61 | 5294 | int wchar_type_size; |
5295 | tree array_domain_type; | |
2d47cc32 | 5296 | tree va_list_ref_type_node; |
8a15c04a | 5297 | tree va_list_arg_type_node; |
a66c9326 | 5298 | |
c38a75b7 | 5299 | build_common_tree_nodes (flag_signed_char, flag_short_double); |
e593356b | 5300 | |
174fcc61 | 5301 | /* Define `int' and `char' first so that dbx will output them first. */ |
d946ea19 | 5302 | record_builtin_type (RID_INT, NULL, integer_type_node); |
174fcc61 | 5303 | record_builtin_type (RID_CHAR, "char", char_type_node); |
5304 | ||
5305 | /* `signed' is the same as `int'. FIXME: the declarations of "signed", | |
5306 | "unsigned long", "long long unsigned" and "unsigned short" were in C++ | |
5307 | but not C. Are the conditionals here needed? */ | |
c0f19401 | 5308 | if (c_dialect_cxx ()) |
d946ea19 | 5309 | record_builtin_type (RID_SIGNED, NULL, integer_type_node); |
174fcc61 | 5310 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); |
5311 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); | |
5312 | record_builtin_type (RID_MAX, "long unsigned int", | |
5313 | long_unsigned_type_node); | |
6388cfe2 | 5314 | if (int128_integer_type_node != NULL_TREE) |
5315 | { | |
5316 | record_builtin_type (RID_INT128, "__int128", | |
5317 | int128_integer_type_node); | |
5318 | record_builtin_type (RID_MAX, "__int128 unsigned", | |
5319 | int128_unsigned_type_node); | |
5320 | } | |
c0f19401 | 5321 | if (c_dialect_cxx ()) |
174fcc61 | 5322 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); |
5323 | record_builtin_type (RID_MAX, "long long int", | |
5324 | long_long_integer_type_node); | |
5325 | record_builtin_type (RID_MAX, "long long unsigned int", | |
5326 | long_long_unsigned_type_node); | |
c0f19401 | 5327 | if (c_dialect_cxx ()) |
174fcc61 | 5328 | record_builtin_type (RID_MAX, "long long unsigned", |
5329 | long_long_unsigned_type_node); | |
5330 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); | |
5331 | record_builtin_type (RID_MAX, "short unsigned int", | |
5332 | short_unsigned_type_node); | |
c0f19401 | 5333 | if (c_dialect_cxx ()) |
174fcc61 | 5334 | record_builtin_type (RID_MAX, "unsigned short", |
5335 | short_unsigned_type_node); | |
5336 | ||
5337 | /* Define both `signed char' and `unsigned char'. */ | |
5338 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); | |
5339 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); | |
5340 | ||
771d21fa | 5341 | /* These are types that c_common_type_for_size and |
5342 | c_common_type_for_mode use. */ | |
e60a6f7b | 5343 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5344 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5345 | intQI_type_node)); |
e60a6f7b | 5346 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5347 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5348 | intHI_type_node)); |
e60a6f7b | 5349 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5350 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5351 | intSI_type_node)); |
e60a6f7b | 5352 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5353 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5354 | intDI_type_node)); |
174fcc61 | 5355 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 5356 | if (targetm.scalar_mode_supported_p (TImode)) |
e60a6f7b | 5357 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5358 | TYPE_DECL, | |
f1515a39 | 5359 | get_identifier ("__int128_t"), |
5360 | intTI_type_node)); | |
174fcc61 | 5361 | #endif |
e60a6f7b | 5362 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5363 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5364 | unsigned_intQI_type_node)); |
e60a6f7b | 5365 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5366 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5367 | unsigned_intHI_type_node)); |
e60a6f7b | 5368 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5369 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5370 | unsigned_intSI_type_node)); |
e60a6f7b | 5371 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5372 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5373 | unsigned_intDI_type_node)); |
174fcc61 | 5374 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 5375 | if (targetm.scalar_mode_supported_p (TImode)) |
e60a6f7b | 5376 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5377 | TYPE_DECL, | |
f1515a39 | 5378 | get_identifier ("__uint128_t"), |
5379 | unsigned_intTI_type_node)); | |
174fcc61 | 5380 | #endif |
5381 | ||
5382 | /* Create the widest literal types. */ | |
5383 | widest_integer_literal_type_node | |
5384 | = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
e60a6f7b | 5385 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5386 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5387 | widest_integer_literal_type_node)); |
174fcc61 | 5388 | |
5389 | widest_unsigned_literal_type_node | |
5390 | = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
e60a6f7b | 5391 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5392 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5393 | widest_unsigned_literal_type_node)); |
174fcc61 | 5394 | |
654ef926 | 5395 | signed_size_type_node = c_common_signed_type (size_type_node); |
174fcc61 | 5396 | |
73673831 | 5397 | pid_type_node = |
5398 | TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE))); | |
5399 | ||
d946ea19 | 5400 | record_builtin_type (RID_FLOAT, NULL, float_type_node); |
5401 | record_builtin_type (RID_DOUBLE, NULL, double_type_node); | |
174fcc61 | 5402 | record_builtin_type (RID_MAX, "long double", long_double_type_node); |
5403 | ||
c4503c0a | 5404 | /* Only supported decimal floating point extension if the target |
5405 | actually supports underlying modes. */ | |
48e1416a | 5406 | if (targetm.scalar_mode_supported_p (SDmode) |
c4503c0a | 5407 | && targetm.scalar_mode_supported_p (DDmode) |
5408 | && targetm.scalar_mode_supported_p (TDmode)) | |
5409 | { | |
5410 | record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node); | |
5411 | record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node); | |
5412 | record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node); | |
5413 | } | |
5414 | ||
9421ebb9 | 5415 | if (targetm.fixed_point_supported_p ()) |
5416 | { | |
5417 | record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node); | |
5418 | record_builtin_type (RID_FRACT, NULL, fract_type_node); | |
5419 | record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node); | |
5420 | record_builtin_type (RID_MAX, "long long _Fract", | |
5421 | long_long_fract_type_node); | |
5422 | record_builtin_type (RID_MAX, "unsigned short _Fract", | |
5423 | unsigned_short_fract_type_node); | |
5424 | record_builtin_type (RID_MAX, "unsigned _Fract", | |
5425 | unsigned_fract_type_node); | |
5426 | record_builtin_type (RID_MAX, "unsigned long _Fract", | |
5427 | unsigned_long_fract_type_node); | |
5428 | record_builtin_type (RID_MAX, "unsigned long long _Fract", | |
5429 | unsigned_long_long_fract_type_node); | |
5430 | record_builtin_type (RID_MAX, "_Sat short _Fract", | |
5431 | sat_short_fract_type_node); | |
5432 | record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node); | |
5433 | record_builtin_type (RID_MAX, "_Sat long _Fract", | |
5434 | sat_long_fract_type_node); | |
5435 | record_builtin_type (RID_MAX, "_Sat long long _Fract", | |
5436 | sat_long_long_fract_type_node); | |
5437 | record_builtin_type (RID_MAX, "_Sat unsigned short _Fract", | |
5438 | sat_unsigned_short_fract_type_node); | |
5439 | record_builtin_type (RID_MAX, "_Sat unsigned _Fract", | |
5440 | sat_unsigned_fract_type_node); | |
5441 | record_builtin_type (RID_MAX, "_Sat unsigned long _Fract", | |
5442 | sat_unsigned_long_fract_type_node); | |
5443 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract", | |
5444 | sat_unsigned_long_long_fract_type_node); | |
5445 | record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node); | |
5446 | record_builtin_type (RID_ACCUM, NULL, accum_type_node); | |
5447 | record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node); | |
5448 | record_builtin_type (RID_MAX, "long long _Accum", | |
5449 | long_long_accum_type_node); | |
5450 | record_builtin_type (RID_MAX, "unsigned short _Accum", | |
5451 | unsigned_short_accum_type_node); | |
5452 | record_builtin_type (RID_MAX, "unsigned _Accum", | |
5453 | unsigned_accum_type_node); | |
5454 | record_builtin_type (RID_MAX, "unsigned long _Accum", | |
5455 | unsigned_long_accum_type_node); | |
5456 | record_builtin_type (RID_MAX, "unsigned long long _Accum", | |
5457 | unsigned_long_long_accum_type_node); | |
5458 | record_builtin_type (RID_MAX, "_Sat short _Accum", | |
5459 | sat_short_accum_type_node); | |
5460 | record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node); | |
5461 | record_builtin_type (RID_MAX, "_Sat long _Accum", | |
5462 | sat_long_accum_type_node); | |
5463 | record_builtin_type (RID_MAX, "_Sat long long _Accum", | |
5464 | sat_long_long_accum_type_node); | |
5465 | record_builtin_type (RID_MAX, "_Sat unsigned short _Accum", | |
5466 | sat_unsigned_short_accum_type_node); | |
5467 | record_builtin_type (RID_MAX, "_Sat unsigned _Accum", | |
5468 | sat_unsigned_accum_type_node); | |
5469 | record_builtin_type (RID_MAX, "_Sat unsigned long _Accum", | |
5470 | sat_unsigned_long_accum_type_node); | |
5471 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum", | |
5472 | sat_unsigned_long_long_accum_type_node); | |
5473 | ||
5474 | } | |
5475 | ||
e60a6f7b | 5476 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5477 | TYPE_DECL, | |
dc24ddbd | 5478 | get_identifier ("complex int"), |
5479 | complex_integer_type_node)); | |
e60a6f7b | 5480 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5481 | TYPE_DECL, | |
dc24ddbd | 5482 | get_identifier ("complex float"), |
5483 | complex_float_type_node)); | |
e60a6f7b | 5484 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5485 | TYPE_DECL, | |
dc24ddbd | 5486 | get_identifier ("complex double"), |
5487 | complex_double_type_node)); | |
5488 | lang_hooks.decls.pushdecl | |
e60a6f7b | 5489 | (build_decl (UNKNOWN_LOCATION, |
5490 | TYPE_DECL, get_identifier ("complex long double"), | |
20325f61 | 5491 | complex_long_double_type_node)); |
174fcc61 | 5492 | |
e256d445 | 5493 | if (c_dialect_cxx ()) |
5494 | /* For C++, make fileptr_type_node a distinct void * type until | |
5495 | FILE type is defined. */ | |
e086912e | 5496 | fileptr_type_node = build_variant_type_copy (ptr_type_node); |
e256d445 | 5497 | |
d946ea19 | 5498 | record_builtin_type (RID_VOID, NULL, void_type_node); |
174fcc61 | 5499 | |
6753bca0 | 5500 | /* Set the TYPE_NAME for any variants that were built before |
5501 | record_builtin_type gave names to the built-in types. */ | |
5502 | { | |
5503 | tree void_name = TYPE_NAME (void_type_node); | |
5504 | TYPE_NAME (void_type_node) = NULL_TREE; | |
5505 | TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST)) | |
5506 | = void_name; | |
5507 | TYPE_NAME (void_type_node) = void_name; | |
5508 | } | |
5509 | ||
fbf0afd1 | 5510 | /* This node must not be shared. */ |
7c446c95 | 5511 | void_zero_node = make_node (INTEGER_CST); |
d2d4bdde | 5512 | TREE_TYPE (void_zero_node) = void_type_node; |
5513 | ||
174fcc61 | 5514 | void_list_node = build_void_list_node (); |
5515 | ||
5516 | /* Make a type to be the domain of a few array types | |
5517 | whose domains don't really matter. | |
5518 | 200 is small enough that it always fits in size_t | |
5519 | and large enough that it can hold most function names for the | |
5520 | initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
5521 | array_domain_type = build_index_type (size_int (200)); | |
5522 | ||
5523 | /* Make a type for arrays of characters. | |
5524 | With luck nothing will ever really depend on the length of this | |
5525 | array type. */ | |
5526 | char_array_type_node | |
5527 | = build_array_type (char_type_node, array_domain_type); | |
5528 | ||
d2d4bdde | 5529 | string_type_node = build_pointer_type (char_type_node); |
5530 | const_string_type_node | |
5531 | = build_pointer_type (build_qualified_type | |
5532 | (char_type_node, TYPE_QUAL_CONST)); | |
5533 | ||
174fcc61 | 5534 | /* This is special for C++ so functions can be overloaded. */ |
18ef7ac2 | 5535 | wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE); |
174fcc61 | 5536 | wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); |
5537 | wchar_type_size = TYPE_PRECISION (wchar_type_node); | |
f3449a3c | 5538 | underlying_wchar_type_node = wchar_type_node; |
c0f19401 | 5539 | if (c_dialect_cxx ()) |
174fcc61 | 5540 | { |
78a8ed03 | 5541 | if (TYPE_UNSIGNED (wchar_type_node)) |
174fcc61 | 5542 | wchar_type_node = make_unsigned_type (wchar_type_size); |
5543 | else | |
5544 | wchar_type_node = make_signed_type (wchar_type_size); | |
5545 | record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); | |
5546 | } | |
174fcc61 | 5547 | |
5548 | /* This is for wide string constants. */ | |
5549 | wchar_array_type_node | |
5550 | = build_array_type (wchar_type_node, array_domain_type); | |
5551 | ||
924bbf02 | 5552 | /* Define 'char16_t'. */ |
5553 | char16_type_node = get_identifier (CHAR16_TYPE); | |
5554 | char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node)); | |
5555 | char16_type_size = TYPE_PRECISION (char16_type_node); | |
5556 | if (c_dialect_cxx ()) | |
5557 | { | |
5558 | char16_type_node = make_unsigned_type (char16_type_size); | |
5559 | ||
60777f69 | 5560 | if (cxx_dialect >= cxx11) |
924bbf02 | 5561 | record_builtin_type (RID_CHAR16, "char16_t", char16_type_node); |
5562 | } | |
5563 | ||
5564 | /* This is for UTF-16 string constants. */ | |
5565 | char16_array_type_node | |
5566 | = build_array_type (char16_type_node, array_domain_type); | |
5567 | ||
5568 | /* Define 'char32_t'. */ | |
5569 | char32_type_node = get_identifier (CHAR32_TYPE); | |
5570 | char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node)); | |
5571 | char32_type_size = TYPE_PRECISION (char32_type_node); | |
5572 | if (c_dialect_cxx ()) | |
5573 | { | |
5574 | char32_type_node = make_unsigned_type (char32_type_size); | |
5575 | ||
60777f69 | 5576 | if (cxx_dialect >= cxx11) |
924bbf02 | 5577 | record_builtin_type (RID_CHAR32, "char32_t", char32_type_node); |
5578 | } | |
5579 | ||
5580 | /* This is for UTF-32 string constants. */ | |
5581 | char32_array_type_node | |
5582 | = build_array_type (char32_type_node, array_domain_type); | |
5583 | ||
6bf5ed8d | 5584 | wint_type_node = |
5585 | TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); | |
5586 | ||
5587 | intmax_type_node = | |
5588 | TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); | |
5589 | uintmax_type_node = | |
5590 | TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); | |
5591 | ||
f3449a3c | 5592 | if (SIG_ATOMIC_TYPE) |
5593 | sig_atomic_type_node = | |
9e6687c8 | 5594 | TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE))); |
f3449a3c | 5595 | if (INT8_TYPE) |
5596 | int8_type_node = | |
9e6687c8 | 5597 | TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE))); |
f3449a3c | 5598 | if (INT16_TYPE) |
5599 | int16_type_node = | |
9e6687c8 | 5600 | TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE))); |
f3449a3c | 5601 | if (INT32_TYPE) |
5602 | int32_type_node = | |
9e6687c8 | 5603 | TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE))); |
f3449a3c | 5604 | if (INT64_TYPE) |
5605 | int64_type_node = | |
9e6687c8 | 5606 | TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE))); |
f3449a3c | 5607 | if (UINT8_TYPE) |
5608 | uint8_type_node = | |
9e6687c8 | 5609 | TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE))); |
f3449a3c | 5610 | if (UINT16_TYPE) |
d1081017 | 5611 | c_uint16_type_node = uint16_type_node = |
9e6687c8 | 5612 | TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE))); |
f3449a3c | 5613 | if (UINT32_TYPE) |
d1081017 | 5614 | c_uint32_type_node = uint32_type_node = |
9e6687c8 | 5615 | TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE))); |
f3449a3c | 5616 | if (UINT64_TYPE) |
d1081017 | 5617 | c_uint64_type_node = uint64_type_node = |
9e6687c8 | 5618 | TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE))); |
f3449a3c | 5619 | if (INT_LEAST8_TYPE) |
5620 | int_least8_type_node = | |
9e6687c8 | 5621 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE))); |
f3449a3c | 5622 | if (INT_LEAST16_TYPE) |
5623 | int_least16_type_node = | |
9e6687c8 | 5624 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE))); |
f3449a3c | 5625 | if (INT_LEAST32_TYPE) |
5626 | int_least32_type_node = | |
9e6687c8 | 5627 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE))); |
f3449a3c | 5628 | if (INT_LEAST64_TYPE) |
5629 | int_least64_type_node = | |
9e6687c8 | 5630 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE))); |
f3449a3c | 5631 | if (UINT_LEAST8_TYPE) |
5632 | uint_least8_type_node = | |
9e6687c8 | 5633 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE))); |
f3449a3c | 5634 | if (UINT_LEAST16_TYPE) |
5635 | uint_least16_type_node = | |
9e6687c8 | 5636 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE))); |
f3449a3c | 5637 | if (UINT_LEAST32_TYPE) |
5638 | uint_least32_type_node = | |
9e6687c8 | 5639 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE))); |
f3449a3c | 5640 | if (UINT_LEAST64_TYPE) |
5641 | uint_least64_type_node = | |
9e6687c8 | 5642 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE))); |
f3449a3c | 5643 | if (INT_FAST8_TYPE) |
5644 | int_fast8_type_node = | |
9e6687c8 | 5645 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE))); |
f3449a3c | 5646 | if (INT_FAST16_TYPE) |
5647 | int_fast16_type_node = | |
9e6687c8 | 5648 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE))); |
f3449a3c | 5649 | if (INT_FAST32_TYPE) |
5650 | int_fast32_type_node = | |
9e6687c8 | 5651 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE))); |
f3449a3c | 5652 | if (INT_FAST64_TYPE) |
5653 | int_fast64_type_node = | |
9e6687c8 | 5654 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE))); |
f3449a3c | 5655 | if (UINT_FAST8_TYPE) |
5656 | uint_fast8_type_node = | |
9e6687c8 | 5657 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE))); |
f3449a3c | 5658 | if (UINT_FAST16_TYPE) |
5659 | uint_fast16_type_node = | |
9e6687c8 | 5660 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE))); |
f3449a3c | 5661 | if (UINT_FAST32_TYPE) |
5662 | uint_fast32_type_node = | |
9e6687c8 | 5663 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE))); |
f3449a3c | 5664 | if (UINT_FAST64_TYPE) |
5665 | uint_fast64_type_node = | |
9e6687c8 | 5666 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE))); |
f3449a3c | 5667 | if (INTPTR_TYPE) |
5668 | intptr_type_node = | |
9e6687c8 | 5669 | TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE))); |
f3449a3c | 5670 | if (UINTPTR_TYPE) |
5671 | uintptr_type_node = | |
9e6687c8 | 5672 | TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE))); |
f3449a3c | 5673 | |
3a939d12 | 5674 | default_function_type |
5675 | = build_varargs_function_type_list (integer_type_node, NULL_TREE); | |
6bf5ed8d | 5676 | ptrdiff_type_node |
5677 | = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); | |
11773141 | 5678 | unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node); |
6bf5ed8d | 5679 | |
dc24ddbd | 5680 | lang_hooks.decls.pushdecl |
e60a6f7b | 5681 | (build_decl (UNKNOWN_LOCATION, |
5682 | TYPE_DECL, get_identifier ("__builtin_va_list"), | |
20325f61 | 5683 | va_list_type_node)); |
202d6e5f | 5684 | if (targetm.enum_va_list_p) |
acd6f472 | 5685 | { |
5686 | int l; | |
5687 | const char *pname; | |
5688 | tree ptype; | |
5f57a8b1 | 5689 | |
202d6e5f | 5690 | for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l) |
acd6f472 | 5691 | { |
5692 | lang_hooks.decls.pushdecl | |
5693 | (build_decl (UNKNOWN_LOCATION, | |
5694 | TYPE_DECL, get_identifier (pname), | |
5695 | ptype)); | |
5696 | ||
5697 | } | |
5698 | } | |
8a15c04a | 5699 | |
8a15c04a | 5700 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
2d47cc32 | 5701 | { |
5702 | va_list_arg_type_node = va_list_ref_type_node = | |
5703 | build_pointer_type (TREE_TYPE (va_list_type_node)); | |
5704 | } | |
8a15c04a | 5705 | else |
2d47cc32 | 5706 | { |
5707 | va_list_arg_type_node = va_list_type_node; | |
5708 | va_list_ref_type_node = build_reference_type (va_list_type_node); | |
5709 | } | |
1cae46be | 5710 | |
dce22712 | 5711 | if (!flag_preprocess_only) |
5712 | c_define_builtins (va_list_ref_type_node, va_list_arg_type_node); | |
ffa8918b | 5713 | |
5c62f199 | 5714 | main_identifier_node = get_identifier ("main"); |
ae84079f | 5715 | |
5716 | /* Create the built-in __null node. It is important that this is | |
5717 | not shared. */ | |
5718 | null_node = make_node (INTEGER_CST); | |
5719 | TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0); | |
27213ba3 | 5720 | |
5721 | /* Since builtin_types isn't gc'ed, don't export these nodes. */ | |
5722 | memset (builtin_types, 0, sizeof (builtin_types)); | |
72040e7e | 5723 | } |
a66c9326 | 5724 | |
79b01846 | 5725 | /* The number of named compound-literals generated thus far. */ |
5726 | static GTY(()) int compound_literal_number; | |
5727 | ||
5728 | /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */ | |
5729 | ||
5730 | void | |
5731 | set_compound_literal_name (tree decl) | |
5732 | { | |
5733 | char *name; | |
5734 | ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", | |
5735 | compound_literal_number); | |
5736 | compound_literal_number++; | |
5737 | DECL_NAME (decl) = get_identifier (name); | |
5738 | } | |
5739 | ||
a66c9326 | 5740 | tree |
e60a6f7b | 5741 | build_va_arg (location_t loc, tree expr, tree type) |
a66c9326 | 5742 | { |
e60a6f7b | 5743 | expr = build1 (VA_ARG_EXPR, type, expr); |
5744 | SET_EXPR_LOCATION (expr, loc); | |
5745 | return expr; | |
a66c9326 | 5746 | } |
0d4238dc | 5747 | |
5748 | ||
dd878098 | 5749 | /* Linked list of disabled built-in functions. */ |
5750 | ||
5751 | typedef struct disabled_builtin | |
5752 | { | |
5753 | const char *name; | |
5754 | struct disabled_builtin *next; | |
5755 | } disabled_builtin; | |
5756 | static disabled_builtin *disabled_builtins = NULL; | |
5757 | ||
1cae46be | 5758 | static bool builtin_function_disabled_p (const char *); |
dd878098 | 5759 | |
5760 | /* Disable a built-in function specified by -fno-builtin-NAME. If NAME | |
5761 | begins with "__builtin_", give an error. */ | |
5762 | ||
5763 | void | |
1cae46be | 5764 | disable_builtin_function (const char *name) |
dd878098 | 5765 | { |
5766 | if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0) | |
b0b1af64 | 5767 | error ("cannot disable built-in function %qs", name); |
dd878098 | 5768 | else |
5769 | { | |
e85905e5 | 5770 | disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin); |
5771 | new_disabled_builtin->name = name; | |
5772 | new_disabled_builtin->next = disabled_builtins; | |
5773 | disabled_builtins = new_disabled_builtin; | |
dd878098 | 5774 | } |
5775 | } | |
5776 | ||
5777 | ||
5778 | /* Return true if the built-in function NAME has been disabled, false | |
5779 | otherwise. */ | |
5780 | ||
5781 | static bool | |
1cae46be | 5782 | builtin_function_disabled_p (const char *name) |
dd878098 | 5783 | { |
5784 | disabled_builtin *p; | |
5785 | for (p = disabled_builtins; p != NULL; p = p->next) | |
5786 | { | |
5787 | if (strcmp (name, p->name) == 0) | |
5788 | return true; | |
5789 | } | |
5790 | return false; | |
5791 | } | |
5792 | ||
5793 | ||
3237155d | 5794 | /* Worker for DEF_BUILTIN. |
5795 | Possibly define a builtin function with one or two names. | |
5796 | Does not declare a non-__builtin_ function if flag_no_builtin, or if | |
5797 | nonansi_p and flag_no_nonansi_builtin. */ | |
0d4238dc | 5798 | |
3237155d | 5799 | static void |
5800 | def_builtin_1 (enum built_in_function fncode, | |
5801 | const char *name, | |
5802 | enum built_in_class fnclass, | |
5803 | tree fntype, tree libtype, | |
5804 | bool both_p, bool fallback_p, bool nonansi_p, | |
5805 | tree fnattrs, bool implicit_p) | |
0d4238dc | 5806 | { |
3237155d | 5807 | tree decl; |
5808 | const char *libname; | |
5809 | ||
27213ba3 | 5810 | if (fntype == error_mark_node) |
5811 | return; | |
5812 | ||
3237155d | 5813 | gcc_assert ((!both_p && !fallback_p) |
5814 | || !strncmp (name, "__builtin_", | |
5815 | strlen ("__builtin_"))); | |
5816 | ||
5817 | libname = name + strlen ("__builtin_"); | |
54be5d7e | 5818 | decl = add_builtin_function (name, fntype, fncode, fnclass, |
5819 | (fallback_p ? libname : NULL), | |
5820 | fnattrs); | |
b9a16870 | 5821 | |
5822 | set_builtin_decl (fncode, decl, implicit_p); | |
5823 | ||
3237155d | 5824 | if (both_p |
5825 | && !flag_no_builtin && !builtin_function_disabled_p (libname) | |
dd878098 | 5826 | && !(nonansi_p && flag_no_nonansi_builtin)) |
54be5d7e | 5827 | add_builtin_function (libname, libtype, fncode, fnclass, |
5828 | NULL, fnattrs); | |
0d4238dc | 5829 | } |
e94026da | 5830 | \f |
d7aeef06 | 5831 | /* Nonzero if the type T promotes to int. This is (nearly) the |
5832 | integral promotions defined in ISO C99 6.3.1.1/2. */ | |
5833 | ||
5834 | bool | |
9f627b1a | 5835 | c_promoting_integer_type_p (const_tree t) |
d7aeef06 | 5836 | { |
5837 | switch (TREE_CODE (t)) | |
5838 | { | |
5839 | case INTEGER_TYPE: | |
5840 | return (TYPE_MAIN_VARIANT (t) == char_type_node | |
5841 | || TYPE_MAIN_VARIANT (t) == signed_char_type_node | |
5842 | || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node | |
5843 | || TYPE_MAIN_VARIANT (t) == short_integer_type_node | |
7aa1e6eb | 5844 | || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node |
5845 | || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node)); | |
d7aeef06 | 5846 | |
5847 | case ENUMERAL_TYPE: | |
5848 | /* ??? Technically all enumerations not larger than an int | |
5849 | promote to an int. But this is used along code paths | |
5850 | that only want to notice a size change. */ | |
5851 | return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node); | |
5852 | ||
5853 | case BOOLEAN_TYPE: | |
5854 | return 1; | |
5855 | ||
5856 | default: | |
5857 | return 0; | |
5858 | } | |
5859 | } | |
5860 | ||
e94026da | 5861 | /* Return 1 if PARMS specifies a fixed number of parameters |
5862 | and none of their types is affected by default promotions. */ | |
5863 | ||
5864 | int | |
9f627b1a | 5865 | self_promoting_args_p (const_tree parms) |
e94026da | 5866 | { |
9f627b1a | 5867 | const_tree t; |
e94026da | 5868 | for (t = parms; t; t = TREE_CHAIN (t)) |
5869 | { | |
19cb6b50 | 5870 | tree type = TREE_VALUE (t); |
43f74bc4 | 5871 | |
e1d8e198 | 5872 | if (type == error_mark_node) |
5873 | continue; | |
5874 | ||
e94026da | 5875 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
5876 | return 0; | |
5877 | ||
5878 | if (type == 0) | |
5879 | return 0; | |
5880 | ||
5881 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
5882 | return 0; | |
5883 | ||
d7aeef06 | 5884 | if (c_promoting_integer_type_p (type)) |
e94026da | 5885 | return 0; |
5886 | } | |
5887 | return 1; | |
5888 | } | |
605fb01e | 5889 | |
c10de5e7 | 5890 | /* Recursively remove any '*' or '&' operator from TYPE. */ |
5891 | tree | |
5892 | strip_pointer_operator (tree t) | |
5893 | { | |
5894 | while (POINTER_TYPE_P (t)) | |
5895 | t = TREE_TYPE (t); | |
5896 | return t; | |
5897 | } | |
5898 | ||
57a0ed23 | 5899 | /* Recursively remove pointer or array type from TYPE. */ |
5900 | tree | |
5901 | strip_pointer_or_array_types (tree t) | |
5902 | { | |
5903 | while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t)) | |
5904 | t = TREE_TYPE (t); | |
5905 | return t; | |
5906 | } | |
5907 | ||
e41f0d80 | 5908 | /* Used to compare case labels. K1 and K2 are actually tree nodes |
5909 | representing case labels, or NULL_TREE for a `default' label. | |
5910 | Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after | |
5911 | K2, and 0 if K1 and K2 are equal. */ | |
5912 | ||
5913 | int | |
1cae46be | 5914 | case_compare (splay_tree_key k1, splay_tree_key k2) |
e41f0d80 | 5915 | { |
5916 | /* Consider a NULL key (such as arises with a `default' label) to be | |
5917 | smaller than anything else. */ | |
5918 | if (!k1) | |
5919 | return k2 ? -1 : 0; | |
5920 | else if (!k2) | |
5921 | return k1 ? 1 : 0; | |
5922 | ||
5923 | return tree_int_cst_compare ((tree) k1, (tree) k2); | |
5924 | } | |
5925 | ||
e60a6f7b | 5926 | /* Process a case label, located at LOC, for the range LOW_VALUE |
5927 | ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE | |
5928 | then this case label is actually a `default' label. If only | |
5929 | HIGH_VALUE is NULL_TREE, then case label was declared using the | |
5930 | usual C/C++ syntax, rather than the GNU case range extension. | |
5931 | CASES is a tree containing all the case ranges processed so far; | |
5932 | COND is the condition for the switch-statement itself. Returns the | |
5933 | CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR | |
5934 | is created. */ | |
e41f0d80 | 5935 | |
5936 | tree | |
e60a6f7b | 5937 | c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type, |
2ca392fd | 5938 | tree low_value, tree high_value) |
e41f0d80 | 5939 | { |
5940 | tree type; | |
5941 | tree label; | |
5942 | tree case_label; | |
5943 | splay_tree_node node; | |
5944 | ||
5945 | /* Create the LABEL_DECL itself. */ | |
e60a6f7b | 5946 | label = create_artificial_label (loc); |
e41f0d80 | 5947 | |
5948 | /* If there was an error processing the switch condition, bail now | |
5949 | before we get more confused. */ | |
5950 | if (!cond || cond == error_mark_node) | |
4ee9c684 | 5951 | goto error_out; |
e41f0d80 | 5952 | |
1cae46be | 5953 | if ((low_value && TREE_TYPE (low_value) |
5954 | && POINTER_TYPE_P (TREE_TYPE (low_value))) | |
e41f0d80 | 5955 | || (high_value && TREE_TYPE (high_value) |
5956 | && POINTER_TYPE_P (TREE_TYPE (high_value)))) | |
b96dc121 | 5957 | { |
e60a6f7b | 5958 | error_at (loc, "pointers are not permitted as case values"); |
b96dc121 | 5959 | goto error_out; |
5960 | } | |
e41f0d80 | 5961 | |
5962 | /* Case ranges are a GNU extension. */ | |
8864917d | 5963 | if (high_value) |
29438999 | 5964 | pedwarn (loc, OPT_Wpedantic, |
8864917d | 5965 | "range expressions in switch statements are non-standard"); |
e41f0d80 | 5966 | |
5967 | type = TREE_TYPE (cond); | |
5968 | if (low_value) | |
5969 | { | |
5970 | low_value = check_case_value (low_value); | |
22a75734 | 5971 | low_value = convert_and_check (loc, type, low_value); |
96722196 | 5972 | if (low_value == error_mark_node) |
5973 | goto error_out; | |
e41f0d80 | 5974 | } |
5975 | if (high_value) | |
5976 | { | |
5977 | high_value = check_case_value (high_value); | |
22a75734 | 5978 | high_value = convert_and_check (loc, type, high_value); |
96722196 | 5979 | if (high_value == error_mark_node) |
5980 | goto error_out; | |
e41f0d80 | 5981 | } |
5982 | ||
96722196 | 5983 | if (low_value && high_value) |
5984 | { | |
5985 | /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't | |
a0c938f0 | 5986 | really a case range, even though it was written that way. |
5987 | Remove the HIGH_VALUE to simplify later processing. */ | |
96722196 | 5988 | if (tree_int_cst_equal (low_value, high_value)) |
5989 | high_value = NULL_TREE; | |
5990 | else if (!tree_int_cst_lt (low_value, high_value)) | |
e60a6f7b | 5991 | warning_at (loc, 0, "empty range specified"); |
96722196 | 5992 | } |
e41f0d80 | 5993 | |
2ca392fd | 5994 | /* See if the case is in range of the type of the original testing |
5995 | expression. If both low_value and high_value are out of range, | |
5996 | don't insert the case label and return NULL_TREE. */ | |
5997 | if (low_value | |
84166705 | 5998 | && !check_case_bounds (type, orig_type, |
5999 | &low_value, high_value ? &high_value : NULL)) | |
2ca392fd | 6000 | return NULL_TREE; |
6001 | ||
e41f0d80 | 6002 | /* Look up the LOW_VALUE in the table of case labels we already |
6003 | have. */ | |
6004 | node = splay_tree_lookup (cases, (splay_tree_key) low_value); | |
6005 | /* If there was not an exact match, check for overlapping ranges. | |
6006 | There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; | |
6007 | that's a `default' label and the only overlap is an exact match. */ | |
6008 | if (!node && (low_value || high_value)) | |
6009 | { | |
6010 | splay_tree_node low_bound; | |
6011 | splay_tree_node high_bound; | |
6012 | ||
6013 | /* Even though there wasn't an exact match, there might be an | |
6014 | overlap between this case range and another case range. | |
6015 | Since we've (inductively) not allowed any overlapping case | |
6016 | ranges, we simply need to find the greatest low case label | |
6017 | that is smaller that LOW_VALUE, and the smallest low case | |
6018 | label that is greater than LOW_VALUE. If there is an overlap | |
6019 | it will occur in one of these two ranges. */ | |
6020 | low_bound = splay_tree_predecessor (cases, | |
6021 | (splay_tree_key) low_value); | |
6022 | high_bound = splay_tree_successor (cases, | |
6023 | (splay_tree_key) low_value); | |
6024 | ||
6025 | /* Check to see if the LOW_BOUND overlaps. It is smaller than | |
6026 | the LOW_VALUE, so there is no need to check unless the | |
6027 | LOW_BOUND is in fact itself a case range. */ | |
6028 | if (low_bound | |
6029 | && CASE_HIGH ((tree) low_bound->value) | |
6030 | && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), | |
6031 | low_value) >= 0) | |
6032 | node = low_bound; | |
6033 | /* Check to see if the HIGH_BOUND overlaps. The low end of that | |
6034 | range is bigger than the low end of the current range, so we | |
6035 | are only interested if the current range is a real range, and | |
6036 | not an ordinary case label. */ | |
1cae46be | 6037 | else if (high_bound |
e41f0d80 | 6038 | && high_value |
6039 | && (tree_int_cst_compare ((tree) high_bound->key, | |
6040 | high_value) | |
6041 | <= 0)) | |
6042 | node = high_bound; | |
6043 | } | |
6044 | /* If there was an overlap, issue an error. */ | |
6045 | if (node) | |
6046 | { | |
eaae3b75 | 6047 | tree duplicate = CASE_LABEL ((tree) node->value); |
e41f0d80 | 6048 | |
6049 | if (high_value) | |
6050 | { | |
e60a6f7b | 6051 | error_at (loc, "duplicate (or overlapping) case value"); |
6052 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
6053 | "this is the first entry overlapping that value"); | |
e41f0d80 | 6054 | } |
6055 | else if (low_value) | |
6056 | { | |
e60a6f7b | 6057 | error_at (loc, "duplicate case value") ; |
6058 | error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here"); | |
e41f0d80 | 6059 | } |
6060 | else | |
6061 | { | |
e60a6f7b | 6062 | error_at (loc, "multiple default labels in one switch"); |
6063 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
6064 | "this is the first default label"); | |
e41f0d80 | 6065 | } |
4ee9c684 | 6066 | goto error_out; |
e41f0d80 | 6067 | } |
6068 | ||
6069 | /* Add a CASE_LABEL to the statement-tree. */ | |
b6e3dd65 | 6070 | case_label = add_stmt (build_case_label (low_value, high_value, label)); |
e41f0d80 | 6071 | /* Register this case label in the splay tree. */ |
1cae46be | 6072 | splay_tree_insert (cases, |
e41f0d80 | 6073 | (splay_tree_key) low_value, |
6074 | (splay_tree_value) case_label); | |
6075 | ||
6076 | return case_label; | |
4ee9c684 | 6077 | |
6078 | error_out: | |
daf6dff5 | 6079 | /* Add a label so that the back-end doesn't think that the beginning of |
4ee9c684 | 6080 | the switch is unreachable. Note that we do not add a case label, as |
a53ff4c1 | 6081 | that just leads to duplicates and thence to failure later on. */ |
4ee9c684 | 6082 | if (!cases->root) |
6083 | { | |
e60a6f7b | 6084 | tree t = create_artificial_label (loc); |
6085 | add_stmt (build_stmt (loc, LABEL_EXPR, t)); | |
4ee9c684 | 6086 | } |
6087 | return error_mark_node; | |
6088 | } | |
6089 | ||
6090 | /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. | |
6091 | Used to verify that case values match up with enumerator values. */ | |
6092 | ||
6093 | static void | |
6094 | match_case_to_enum_1 (tree key, tree type, tree label) | |
6095 | { | |
6096 | char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1]; | |
6097 | ||
6098 | /* ??? Not working too hard to print the double-word value. | |
6099 | Should perhaps be done with %lwd in the diagnostic routines? */ | |
6100 | if (TREE_INT_CST_HIGH (key) == 0) | |
6101 | snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED, | |
6102 | TREE_INT_CST_LOW (key)); | |
6103 | else if (!TYPE_UNSIGNED (type) | |
6104 | && TREE_INT_CST_HIGH (key) == -1 | |
6105 | && TREE_INT_CST_LOW (key) != 0) | |
6106 | snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED, | |
6107 | -TREE_INT_CST_LOW (key)); | |
6108 | else | |
6109 | snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX, | |
7df7561b | 6110 | (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key), |
6111 | (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key)); | |
4ee9c684 | 6112 | |
6113 | if (TYPE_NAME (type) == 0) | |
712d2297 | 6114 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
6115 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
6116 | "case value %qs not in enumerated type", | |
6117 | buf); | |
4ee9c684 | 6118 | else |
712d2297 | 6119 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
6120 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
6121 | "case value %qs not in enumerated type %qT", | |
6122 | buf, type); | |
4ee9c684 | 6123 | } |
6124 | ||
359d87c6 | 6125 | /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. |
6126 | Used to verify that case values match up with enumerator values. */ | |
6127 | ||
4ee9c684 | 6128 | static int |
6129 | match_case_to_enum (splay_tree_node node, void *data) | |
6130 | { | |
6131 | tree label = (tree) node->value; | |
4fd61bc6 | 6132 | tree type = (tree) data; |
4ee9c684 | 6133 | |
6134 | /* Skip default case. */ | |
6135 | if (!CASE_LOW (label)) | |
6136 | return 0; | |
6137 | ||
359d87c6 | 6138 | /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear |
4ee9c684 | 6139 | when we did our enum->case scan. Reset our scratch bit after. */ |
359d87c6 | 6140 | if (!CASE_LOW_SEEN (label)) |
4ee9c684 | 6141 | match_case_to_enum_1 (CASE_LOW (label), type, label); |
6142 | else | |
359d87c6 | 6143 | CASE_LOW_SEEN (label) = 0; |
4ee9c684 | 6144 | |
359d87c6 | 6145 | /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is |
6146 | not set, that means that CASE_HIGH did not appear when we did our | |
6147 | enum->case scan. Reset our scratch bit after. */ | |
4ee9c684 | 6148 | if (CASE_HIGH (label)) |
6149 | { | |
359d87c6 | 6150 | if (!CASE_HIGH_SEEN (label)) |
6151 | match_case_to_enum_1 (CASE_HIGH (label), type, label); | |
6152 | else | |
6153 | CASE_HIGH_SEEN (label) = 0; | |
4ee9c684 | 6154 | } |
6155 | ||
6156 | return 0; | |
6157 | } | |
6158 | ||
e7911019 | 6159 | /* Handle -Wswitch*. Called from the front end after parsing the |
6160 | switch construct. */ | |
6161 | /* ??? Should probably be somewhere generic, since other languages | |
6162 | besides C and C++ would want this. At the moment, however, C/C++ | |
6163 | are the only tree-ssa languages that support enumerations at all, | |
6164 | so the point is moot. */ | |
4ee9c684 | 6165 | |
e7911019 | 6166 | void |
6167 | c_do_switch_warnings (splay_tree cases, location_t switch_location, | |
6168 | tree type, tree cond) | |
4ee9c684 | 6169 | { |
b27ac6b5 | 6170 | splay_tree_node default_node; |
359d87c6 | 6171 | splay_tree_node node; |
6172 | tree chain; | |
4ee9c684 | 6173 | |
6174 | if (!warn_switch && !warn_switch_enum && !warn_switch_default) | |
6175 | return; | |
6176 | ||
4ee9c684 | 6177 | default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); |
8b6866af | 6178 | if (!default_node) |
5fb6a912 | 6179 | warning_at (switch_location, OPT_Wswitch_default, |
6180 | "switch missing default case"); | |
4ee9c684 | 6181 | |
359d87c6 | 6182 | /* From here on, we only care about about enumerated types. */ |
6183 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
6184 | return; | |
6185 | ||
561017b5 | 6186 | /* From here on, we only care about -Wswitch and -Wswitch-enum. */ |
6187 | if (!warn_switch_enum && !warn_switch) | |
359d87c6 | 6188 | return; |
6189 | ||
561017b5 | 6190 | /* Check the cases. Warn about case values which are not members of |
6191 | the enumerated type. For -Wswitch-enum, or for -Wswitch when | |
6192 | there is no default case, check that exactly all enumeration | |
6193 | literals are covered by the cases. */ | |
6194 | ||
359d87c6 | 6195 | /* Clearing COND if it is not an integer constant simplifies |
6196 | the tests inside the loop below. */ | |
6197 | if (TREE_CODE (cond) != INTEGER_CST) | |
6198 | cond = NULL_TREE; | |
6199 | ||
6200 | /* The time complexity here is O(N*lg(N)) worst case, but for the | |
6201 | common case of monotonically increasing enumerators, it is | |
6202 | O(N), since the nature of the splay tree will keep the next | |
6203 | element adjacent to the root at all times. */ | |
4ee9c684 | 6204 | |
359d87c6 | 6205 | for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) |
6206 | { | |
6207 | tree value = TREE_VALUE (chain); | |
3f00a6c0 | 6208 | if (TREE_CODE (value) == CONST_DECL) |
6209 | value = DECL_INITIAL (value); | |
359d87c6 | 6210 | node = splay_tree_lookup (cases, (splay_tree_key) value); |
6211 | if (node) | |
4ee9c684 | 6212 | { |
359d87c6 | 6213 | /* Mark the CASE_LOW part of the case entry as seen. */ |
6214 | tree label = (tree) node->value; | |
6215 | CASE_LOW_SEEN (label) = 1; | |
6216 | continue; | |
6217 | } | |
6218 | ||
6219 | /* Even though there wasn't an exact match, there might be a | |
f0b5f617 | 6220 | case range which includes the enumerator's value. */ |
359d87c6 | 6221 | node = splay_tree_predecessor (cases, (splay_tree_key) value); |
6222 | if (node && CASE_HIGH ((tree) node->value)) | |
6223 | { | |
6224 | tree label = (tree) node->value; | |
6225 | int cmp = tree_int_cst_compare (CASE_HIGH (label), value); | |
6226 | if (cmp >= 0) | |
4ee9c684 | 6227 | { |
359d87c6 | 6228 | /* If we match the upper bound exactly, mark the CASE_HIGH |
6229 | part of the case entry as seen. */ | |
6230 | if (cmp == 0) | |
6231 | CASE_HIGH_SEEN (label) = 1; | |
6232 | continue; | |
4ee9c684 | 6233 | } |
6234 | } | |
6235 | ||
359d87c6 | 6236 | /* We've now determined that this enumerated literal isn't |
6237 | handled by the case labels of the switch statement. */ | |
4ee9c684 | 6238 | |
359d87c6 | 6239 | /* If the switch expression is a constant, we only really care |
6240 | about whether that constant is handled by the switch. */ | |
6241 | if (cond && tree_int_cst_compare (cond, value)) | |
6242 | continue; | |
4ee9c684 | 6243 | |
6cbbbc89 | 6244 | /* If there is a default_node, the only relevant option is |
561017b5 | 6245 | Wswitch-enum. Otherwise, if both are enabled then we prefer |
6cbbbc89 | 6246 | to warn using -Wswitch because -Wswitch is enabled by -Wall |
6247 | while -Wswitch-enum is explicit. */ | |
561017b5 | 6248 | warning_at (switch_location, |
6249 | (default_node || !warn_switch | |
6250 | ? OPT_Wswitch_enum | |
6251 | : OPT_Wswitch), | |
6252 | "enumeration value %qE not handled in switch", | |
6253 | TREE_PURPOSE (chain)); | |
4ee9c684 | 6254 | } |
359d87c6 | 6255 | |
6256 | /* Warn if there are case expressions that don't correspond to | |
6257 | enumerators. This can occur since C and C++ don't enforce | |
6258 | type-checking of assignments to enumeration variables. | |
6259 | ||
6260 | The time complexity here is now always O(N) worst case, since | |
6261 | we should have marked both the lower bound and upper bound of | |
6262 | every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN | |
6263 | above. This scan also resets those fields. */ | |
6cbbbc89 | 6264 | |
359d87c6 | 6265 | splay_tree_foreach (cases, match_case_to_enum, type); |
e41f0d80 | 6266 | } |
6267 | ||
9dd48740 | 6268 | /* Finish an expression taking the address of LABEL (an |
dda49785 | 6269 | IDENTIFIER_NODE). Returns an expression for the address. |
6270 | ||
6271 | LOC is the location for the expression returned. */ | |
d0a47c8d | 6272 | |
1cae46be | 6273 | tree |
dda49785 | 6274 | finish_label_address_expr (tree label, location_t loc) |
d0a47c8d | 6275 | { |
6276 | tree result; | |
6277 | ||
29438999 | 6278 | pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard"); |
d0a47c8d | 6279 | |
9dd48740 | 6280 | if (label == error_mark_node) |
6281 | return error_mark_node; | |
6282 | ||
d0a47c8d | 6283 | label = lookup_label (label); |
6284 | if (label == NULL_TREE) | |
6285 | result = null_pointer_node; | |
6286 | else | |
6287 | { | |
6288 | TREE_USED (label) = 1; | |
6289 | result = build1 (ADDR_EXPR, ptr_type_node, label); | |
344cd9b2 | 6290 | /* The current function is not necessarily uninlinable. |
d0a47c8d | 6291 | Computed gotos are incompatible with inlining, but the value |
6292 | here could be used only in a diagnostic, for example. */ | |
dda49785 | 6293 | protected_set_expr_location (result, loc); |
d0a47c8d | 6294 | } |
6295 | ||
6296 | return result; | |
6297 | } | |
4f9a1c9b | 6298 | \f |
6299 | ||
6300 | /* Given a boolean expression ARG, return a tree representing an increment | |
6301 | or decrement (as indicated by CODE) of ARG. The front end must check for | |
6302 | invalid cases (e.g., decrement in C++). */ | |
6303 | tree | |
1cae46be | 6304 | boolean_increment (enum tree_code code, tree arg) |
4f9a1c9b | 6305 | { |
6306 | tree val; | |
69db191c | 6307 | tree true_res = build_int_cst (TREE_TYPE (arg), 1); |
c0f19401 | 6308 | |
4f9a1c9b | 6309 | arg = stabilize_reference (arg); |
6310 | switch (code) | |
6311 | { | |
6312 | case PREINCREMENT_EXPR: | |
14ae0310 | 6313 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 6314 | break; |
6315 | case POSTINCREMENT_EXPR: | |
14ae0310 | 6316 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 6317 | arg = save_expr (arg); |
14ae0310 | 6318 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
6319 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 6320 | break; |
6321 | case PREDECREMENT_EXPR: | |
14ae0310 | 6322 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
389dd41b | 6323 | invert_truthvalue_loc (input_location, arg)); |
4f9a1c9b | 6324 | break; |
6325 | case POSTDECREMENT_EXPR: | |
14ae0310 | 6326 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
389dd41b | 6327 | invert_truthvalue_loc (input_location, arg)); |
4f9a1c9b | 6328 | arg = save_expr (arg); |
14ae0310 | 6329 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
6330 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 6331 | break; |
6332 | default: | |
231bd014 | 6333 | gcc_unreachable (); |
4f9a1c9b | 6334 | } |
6335 | TREE_SIDE_EFFECTS (val) = 1; | |
6336 | return val; | |
6337 | } | |
76a6e674 | 6338 | \f |
f3449a3c | 6339 | /* Built-in macros for stddef.h and stdint.h, that require macros |
6340 | defined in this file. */ | |
79cf3ec1 | 6341 | void |
1cae46be | 6342 | c_stddef_cpp_builtins(void) |
1ed9d5f5 | 6343 | { |
63994318 | 6344 | builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0); |
6345 | builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0); | |
6346 | builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0); | |
6347 | builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0); | |
36bccbfc | 6348 | builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0); |
6349 | builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0); | |
b0726616 | 6350 | builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0); |
6351 | builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0); | |
f3449a3c | 6352 | if (SIG_ATOMIC_TYPE) |
6353 | builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0); | |
6354 | if (INT8_TYPE) | |
6355 | builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0); | |
6356 | if (INT16_TYPE) | |
6357 | builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0); | |
6358 | if (INT32_TYPE) | |
6359 | builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0); | |
6360 | if (INT64_TYPE) | |
6361 | builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0); | |
6362 | if (UINT8_TYPE) | |
6363 | builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0); | |
6364 | if (UINT16_TYPE) | |
6365 | builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0); | |
6366 | if (UINT32_TYPE) | |
6367 | builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0); | |
6368 | if (UINT64_TYPE) | |
6369 | builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0); | |
6370 | if (INT_LEAST8_TYPE) | |
6371 | builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0); | |
6372 | if (INT_LEAST16_TYPE) | |
6373 | builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0); | |
6374 | if (INT_LEAST32_TYPE) | |
6375 | builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0); | |
6376 | if (INT_LEAST64_TYPE) | |
6377 | builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0); | |
6378 | if (UINT_LEAST8_TYPE) | |
6379 | builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0); | |
6380 | if (UINT_LEAST16_TYPE) | |
6381 | builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0); | |
6382 | if (UINT_LEAST32_TYPE) | |
6383 | builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0); | |
6384 | if (UINT_LEAST64_TYPE) | |
6385 | builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0); | |
6386 | if (INT_FAST8_TYPE) | |
6387 | builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0); | |
6388 | if (INT_FAST16_TYPE) | |
6389 | builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0); | |
6390 | if (INT_FAST32_TYPE) | |
6391 | builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0); | |
6392 | if (INT_FAST64_TYPE) | |
6393 | builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0); | |
6394 | if (UINT_FAST8_TYPE) | |
6395 | builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0); | |
6396 | if (UINT_FAST16_TYPE) | |
6397 | builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0); | |
6398 | if (UINT_FAST32_TYPE) | |
6399 | builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0); | |
6400 | if (UINT_FAST64_TYPE) | |
6401 | builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0); | |
6402 | if (INTPTR_TYPE) | |
6403 | builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0); | |
6404 | if (UINTPTR_TYPE) | |
6405 | builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0); | |
574006c3 | 6406 | } |
6407 | ||
7d3b509a | 6408 | static void |
1cae46be | 6409 | c_init_attributes (void) |
7d3b509a | 6410 | { |
6411 | /* Fill in the built_in_attributes array. */ | |
7c446c95 | 6412 | #define DEF_ATTR_NULL_TREE(ENUM) \ |
7d3b509a | 6413 | built_in_attributes[(int) ENUM] = NULL_TREE; |
7c446c95 | 6414 | #define DEF_ATTR_INT(ENUM, VALUE) \ |
ceb7b692 | 6415 | built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE); |
c8010b80 | 6416 | #define DEF_ATTR_STRING(ENUM, VALUE) \ |
6417 | built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE); | |
7d3b509a | 6418 | #define DEF_ATTR_IDENT(ENUM, STRING) \ |
6419 | built_in_attributes[(int) ENUM] = get_identifier (STRING); | |
6420 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \ | |
6421 | built_in_attributes[(int) ENUM] \ | |
6422 | = tree_cons (built_in_attributes[(int) PURPOSE], \ | |
6423 | built_in_attributes[(int) VALUE], \ | |
6424 | built_in_attributes[(int) CHAIN]); | |
7d3b509a | 6425 | #include "builtin-attrs.def" |
6426 | #undef DEF_ATTR_NULL_TREE | |
6427 | #undef DEF_ATTR_INT | |
6428 | #undef DEF_ATTR_IDENT | |
6429 | #undef DEF_ATTR_TREE_LIST | |
76a6e674 | 6430 | } |
5f3cead1 | 6431 | |
33199a81 | 6432 | /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain |
6433 | identifier as an argument, so the front end shouldn't look it up. */ | |
6434 | ||
6435 | bool | |
47b19b94 | 6436 | attribute_takes_identifier_p (const_tree attr_id) |
33199a81 | 6437 | { |
9bf1c74e | 6438 | const struct attribute_spec *spec = lookup_attribute_spec (attr_id); |
02cb1060 | 6439 | if (spec == NULL) |
6440 | /* Unknown attribute that we'll end up ignoring, return true so we | |
6441 | don't complain about an identifier argument. */ | |
6442 | return true; | |
6443 | else if (!strcmp ("mode", spec->name) | |
6444 | || !strcmp ("format", spec->name) | |
6445 | || !strcmp ("cleanup", spec->name)) | |
47b19b94 | 6446 | return true; |
6447 | else | |
6448 | return targetm.attribute_takes_identifier_p (attr_id); | |
33199a81 | 6449 | } |
6450 | ||
f8e93a2e | 6451 | /* Attribute handlers common to C front ends. */ |
6452 | ||
6453 | /* Handle a "packed" attribute; arguments as in | |
6454 | struct attribute_spec.handler. */ | |
6455 | ||
6456 | static tree | |
9a03a746 | 6457 | handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
09347743 | 6458 | int flags, bool *no_add_attrs) |
f8e93a2e | 6459 | { |
f40175cb | 6460 | if (TYPE_P (*node)) |
f8e93a2e | 6461 | { |
6462 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 6463 | *node = build_variant_type_copy (*node); |
f40175cb | 6464 | TYPE_PACKED (*node) = 1; |
f8e93a2e | 6465 | } |
6466 | else if (TREE_CODE (*node) == FIELD_DECL) | |
c2ab04f9 | 6467 | { |
9fd767c5 | 6468 | if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT |
6469 | /* Still pack bitfields. */ | |
6470 | && ! DECL_INITIAL (*node)) | |
c2ab04f9 | 6471 | warning (OPT_Wattributes, |
6472 | "%qE attribute ignored for field of type %qT", | |
6473 | name, TREE_TYPE (*node)); | |
6474 | else | |
6475 | DECL_PACKED (*node) = 1; | |
6476 | } | |
f8e93a2e | 6477 | /* We can't set DECL_PACKED for a VAR_DECL, because the bit is |
f40175cb | 6478 | used for DECL_REGISTER. It wouldn't mean anything anyway. |
6479 | We can't set DECL_PACKED on the type of a TYPE_DECL, because | |
6480 | that changes what the typedef is typing. */ | |
f8e93a2e | 6481 | else |
6482 | { | |
9b2d6d13 | 6483 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6484 | *no_add_attrs = true; |
6485 | } | |
6486 | ||
6487 | return NULL_TREE; | |
6488 | } | |
6489 | ||
6490 | /* Handle a "nocommon" attribute; arguments as in | |
6491 | struct attribute_spec.handler. */ | |
6492 | ||
6493 | static tree | |
1cae46be | 6494 | handle_nocommon_attribute (tree *node, tree name, |
9a03a746 | 6495 | tree ARG_UNUSED (args), |
6496 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6497 | { |
6498 | if (TREE_CODE (*node) == VAR_DECL) | |
6499 | DECL_COMMON (*node) = 0; | |
6500 | else | |
6501 | { | |
9b2d6d13 | 6502 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6503 | *no_add_attrs = true; |
6504 | } | |
6505 | ||
6506 | return NULL_TREE; | |
6507 | } | |
6508 | ||
6509 | /* Handle a "common" attribute; arguments as in | |
6510 | struct attribute_spec.handler. */ | |
6511 | ||
6512 | static tree | |
9a03a746 | 6513 | handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6514 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6515 | { |
6516 | if (TREE_CODE (*node) == VAR_DECL) | |
6517 | DECL_COMMON (*node) = 1; | |
6518 | else | |
6519 | { | |
9b2d6d13 | 6520 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6521 | *no_add_attrs = true; |
6522 | } | |
6523 | ||
6524 | return NULL_TREE; | |
6525 | } | |
6526 | ||
6527 | /* Handle a "noreturn" attribute; arguments as in | |
6528 | struct attribute_spec.handler. */ | |
6529 | ||
6530 | static tree | |
9a03a746 | 6531 | handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6532 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6533 | { |
6534 | tree type = TREE_TYPE (*node); | |
6535 | ||
6536 | /* See FIXME comment in c_common_attribute_table. */ | |
8c582e4f | 6537 | if (TREE_CODE (*node) == FUNCTION_DECL |
6538 | || objc_method_decl (TREE_CODE (*node))) | |
f8e93a2e | 6539 | TREE_THIS_VOLATILE (*node) = 1; |
6540 | else if (TREE_CODE (type) == POINTER_TYPE | |
6541 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
6542 | TREE_TYPE (*node) | |
6543 | = build_pointer_type | |
6544 | (build_type_variant (TREE_TYPE (type), | |
71bda45d | 6545 | TYPE_READONLY (TREE_TYPE (type)), 1)); |
f8e93a2e | 6546 | else |
6547 | { | |
9b2d6d13 | 6548 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6549 | *no_add_attrs = true; |
6550 | } | |
6551 | ||
6552 | return NULL_TREE; | |
6553 | } | |
6554 | ||
5de92639 | 6555 | /* Handle a "hot" and attribute; arguments as in |
6556 | struct attribute_spec.handler. */ | |
6557 | ||
6558 | static tree | |
6559 | handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
46f8e3b0 | 6560 | int ARG_UNUSED (flags), bool *no_add_attrs) |
5de92639 | 6561 | { |
758a38ab | 6562 | if (TREE_CODE (*node) == FUNCTION_DECL |
6563 | || TREE_CODE (*node) == LABEL_DECL) | |
5de92639 | 6564 | { |
6565 | if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL) | |
6566 | { | |
6567 | warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", | |
6568 | name, "cold"); | |
6569 | *no_add_attrs = true; | |
6570 | } | |
24470055 | 6571 | /* Most of the rest of the hot processing is done later with |
6572 | lookup_attribute. */ | |
5de92639 | 6573 | } |
6574 | else | |
6575 | { | |
6576 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6577 | *no_add_attrs = true; | |
6578 | } | |
6579 | ||
6580 | return NULL_TREE; | |
6581 | } | |
758a38ab | 6582 | |
5de92639 | 6583 | /* Handle a "cold" and attribute; arguments as in |
6584 | struct attribute_spec.handler. */ | |
6585 | ||
6586 | static tree | |
6587 | handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
6588 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6589 | { | |
758a38ab | 6590 | if (TREE_CODE (*node) == FUNCTION_DECL |
6591 | || TREE_CODE (*node) == LABEL_DECL) | |
5de92639 | 6592 | { |
6593 | if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL) | |
6594 | { | |
6595 | warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", | |
6596 | name, "hot"); | |
6597 | *no_add_attrs = true; | |
6598 | } | |
24470055 | 6599 | /* Most of the rest of the cold processing is done later with |
6600 | lookup_attribute. */ | |
5de92639 | 6601 | } |
6602 | else | |
6603 | { | |
6604 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6605 | *no_add_attrs = true; | |
6606 | } | |
6607 | ||
6608 | return NULL_TREE; | |
6609 | } | |
6610 | ||
a9196da9 | 6611 | /* Handle a "no_sanitize_address" attribute; arguments as in |
d413ffdd | 6612 | struct attribute_spec.handler. */ |
6613 | ||
6614 | static tree | |
a9196da9 | 6615 | handle_no_sanitize_address_attribute (tree *node, tree name, tree, int, |
6616 | bool *no_add_attrs) | |
d413ffdd | 6617 | { |
6618 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6619 | { | |
6620 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6621 | *no_add_attrs = true; | |
6622 | } | |
6623 | ||
6624 | return NULL_TREE; | |
6625 | } | |
6626 | ||
a9196da9 | 6627 | /* Handle a "no_address_safety_analysis" attribute; arguments as in |
6628 | struct attribute_spec.handler. */ | |
6629 | ||
6630 | static tree | |
6631 | handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int, | |
6632 | bool *no_add_attrs) | |
6633 | { | |
6634 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6635 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6636 | else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node))) | |
6637 | DECL_ATTRIBUTES (*node) | |
6638 | = tree_cons (get_identifier ("no_sanitize_address"), | |
6639 | NULL_TREE, DECL_ATTRIBUTES (*node)); | |
6640 | *no_add_attrs = true; | |
6641 | return NULL_TREE; | |
6642 | } | |
6643 | ||
05f893e1 | 6644 | /* Handle a "no_sanitize_undefined" attribute; arguments as in |
6645 | struct attribute_spec.handler. */ | |
6646 | ||
6647 | static tree | |
6648 | handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int, | |
6649 | bool *no_add_attrs) | |
6650 | { | |
6651 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6652 | { | |
6653 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6654 | *no_add_attrs = true; | |
6655 | } | |
6656 | ||
6657 | return NULL_TREE; | |
6658 | } | |
6659 | ||
f8e93a2e | 6660 | /* Handle a "noinline" attribute; arguments as in |
6661 | struct attribute_spec.handler. */ | |
6662 | ||
6663 | static tree | |
1cae46be | 6664 | handle_noinline_attribute (tree *node, tree name, |
9a03a746 | 6665 | tree ARG_UNUSED (args), |
6666 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6667 | { |
6668 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6669 | DECL_UNINLINABLE (*node) = 1; | |
6670 | else | |
6671 | { | |
9b2d6d13 | 6672 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6673 | *no_add_attrs = true; |
6674 | } | |
6675 | ||
6676 | return NULL_TREE; | |
6677 | } | |
6678 | ||
bdb1f0d1 | 6679 | /* Handle a "noclone" attribute; arguments as in |
6680 | struct attribute_spec.handler. */ | |
6681 | ||
6682 | static tree | |
6683 | handle_noclone_attribute (tree *node, tree name, | |
6684 | tree ARG_UNUSED (args), | |
6685 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6686 | { | |
6687 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6688 | { | |
6689 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6690 | *no_add_attrs = true; | |
6691 | } | |
6692 | ||
6693 | return NULL_TREE; | |
6694 | } | |
6695 | ||
f8e93a2e | 6696 | /* Handle a "always_inline" attribute; arguments as in |
6697 | struct attribute_spec.handler. */ | |
6698 | ||
6699 | static tree | |
1cae46be | 6700 | handle_always_inline_attribute (tree *node, tree name, |
9a03a746 | 6701 | tree ARG_UNUSED (args), |
6702 | int ARG_UNUSED (flags), | |
09347743 | 6703 | bool *no_add_attrs) |
f8e93a2e | 6704 | { |
6705 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6706 | { | |
ebb7d626 | 6707 | /* Set the attribute and mark it for disregarding inline |
6708 | limits. */ | |
6709 | DECL_DISREGARD_INLINE_LIMITS (*node) = 1; | |
f8e93a2e | 6710 | } |
6711 | else | |
6712 | { | |
9b2d6d13 | 6713 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6714 | *no_add_attrs = true; |
541e4101 | 6715 | } |
6716 | ||
6717 | return NULL_TREE; | |
6718 | } | |
6719 | ||
6720 | /* Handle a "gnu_inline" attribute; arguments as in | |
6721 | struct attribute_spec.handler. */ | |
6722 | ||
6723 | static tree | |
6724 | handle_gnu_inline_attribute (tree *node, tree name, | |
6725 | tree ARG_UNUSED (args), | |
6726 | int ARG_UNUSED (flags), | |
6727 | bool *no_add_attrs) | |
6728 | { | |
6729 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6730 | { | |
6731 | /* Do nothing else, just set the attribute. We'll get at | |
6732 | it later with lookup_attribute. */ | |
6733 | } | |
6734 | else | |
6735 | { | |
6736 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6737 | *no_add_attrs = true; | |
7bd95dfd | 6738 | } |
6739 | ||
6740 | return NULL_TREE; | |
6741 | } | |
6742 | ||
6743 | /* Handle a "leaf" attribute; arguments as in | |
6744 | struct attribute_spec.handler. */ | |
6745 | ||
6746 | static tree | |
6747 | handle_leaf_attribute (tree *node, tree name, | |
6748 | tree ARG_UNUSED (args), | |
6749 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6750 | { | |
6751 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6752 | { | |
6753 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6754 | *no_add_attrs = true; | |
6755 | } | |
6756 | if (!TREE_PUBLIC (*node)) | |
6757 | { | |
6758 | warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name); | |
6759 | *no_add_attrs = true; | |
f8e93a2e | 6760 | } |
6761 | ||
6762 | return NULL_TREE; | |
6763 | } | |
6764 | ||
1b16fc45 | 6765 | /* Handle an "artificial" attribute; arguments as in |
6766 | struct attribute_spec.handler. */ | |
6767 | ||
6768 | static tree | |
6769 | handle_artificial_attribute (tree *node, tree name, | |
6770 | tree ARG_UNUSED (args), | |
6771 | int ARG_UNUSED (flags), | |
6772 | bool *no_add_attrs) | |
6773 | { | |
6774 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6775 | { | |
6776 | /* Do nothing else, just set the attribute. We'll get at | |
6777 | it later with lookup_attribute. */ | |
6778 | } | |
6779 | else | |
6780 | { | |
6781 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6782 | *no_add_attrs = true; | |
6783 | } | |
6784 | ||
6785 | return NULL_TREE; | |
6786 | } | |
6787 | ||
0cdd9887 | 6788 | /* Handle a "flatten" attribute; arguments as in |
6789 | struct attribute_spec.handler. */ | |
6790 | ||
6791 | static tree | |
6792 | handle_flatten_attribute (tree *node, tree name, | |
a0c938f0 | 6793 | tree args ATTRIBUTE_UNUSED, |
6794 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
0cdd9887 | 6795 | { |
6796 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6797 | /* Do nothing else, just set the attribute. We'll get at | |
6798 | it later with lookup_attribute. */ | |
6799 | ; | |
6800 | else | |
6801 | { | |
6802 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6803 | *no_add_attrs = true; | |
6804 | } | |
6805 | ||
6806 | return NULL_TREE; | |
6807 | } | |
6808 | ||
10fc867f | 6809 | /* Handle a "warning" or "error" attribute; arguments as in |
6810 | struct attribute_spec.handler. */ | |
6811 | ||
6812 | static tree | |
6813 | handle_error_attribute (tree *node, tree name, tree args, | |
6814 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6815 | { | |
6816 | if (TREE_CODE (*node) == FUNCTION_DECL | |
df936998 | 6817 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) |
10fc867f | 6818 | /* Do nothing else, just set the attribute. We'll get at |
6819 | it later with lookup_attribute. */ | |
6820 | ; | |
6821 | else | |
6822 | { | |
6823 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6824 | *no_add_attrs = true; | |
6825 | } | |
6826 | ||
6827 | return NULL_TREE; | |
6828 | } | |
0cdd9887 | 6829 | |
f8e93a2e | 6830 | /* Handle a "used" attribute; arguments as in |
6831 | struct attribute_spec.handler. */ | |
6832 | ||
6833 | static tree | |
9a03a746 | 6834 | handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args), |
6835 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6836 | { |
d0a31bd8 | 6837 | tree node = *pnode; |
6838 | ||
6839 | if (TREE_CODE (node) == FUNCTION_DECL | |
a4e3ffad | 6840 | || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)) |
6841 | || (TREE_CODE (node) == TYPE_DECL)) | |
f54ed8bc | 6842 | { |
f54ed8bc | 6843 | TREE_USED (node) = 1; |
9423c9b7 | 6844 | DECL_PRESERVE_P (node) = 1; |
abc6c64f | 6845 | if (TREE_CODE (node) == VAR_DECL) |
6846 | DECL_READ_P (node) = 1; | |
f54ed8bc | 6847 | } |
f8e93a2e | 6848 | else |
6849 | { | |
9b2d6d13 | 6850 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6851 | *no_add_attrs = true; |
6852 | } | |
6853 | ||
6854 | return NULL_TREE; | |
6855 | } | |
6856 | ||
6857 | /* Handle a "unused" attribute; arguments as in | |
6858 | struct attribute_spec.handler. */ | |
6859 | ||
6860 | static tree | |
9a03a746 | 6861 | handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6862 | int flags, bool *no_add_attrs) | |
f8e93a2e | 6863 | { |
6864 | if (DECL_P (*node)) | |
6865 | { | |
6866 | tree decl = *node; | |
6867 | ||
6868 | if (TREE_CODE (decl) == PARM_DECL | |
6869 | || TREE_CODE (decl) == VAR_DECL | |
6870 | || TREE_CODE (decl) == FUNCTION_DECL | |
6871 | || TREE_CODE (decl) == LABEL_DECL | |
6872 | || TREE_CODE (decl) == TYPE_DECL) | |
abc6c64f | 6873 | { |
6874 | TREE_USED (decl) = 1; | |
6875 | if (TREE_CODE (decl) == VAR_DECL | |
6876 | || TREE_CODE (decl) == PARM_DECL) | |
6877 | DECL_READ_P (decl) = 1; | |
6878 | } | |
f8e93a2e | 6879 | else |
6880 | { | |
9b2d6d13 | 6881 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6882 | *no_add_attrs = true; |
6883 | } | |
6884 | } | |
6885 | else | |
6886 | { | |
6887 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 6888 | *node = build_variant_type_copy (*node); |
f8e93a2e | 6889 | TREE_USED (*node) = 1; |
6890 | } | |
6891 | ||
6892 | return NULL_TREE; | |
6893 | } | |
6894 | ||
62eec3b4 | 6895 | /* Handle a "externally_visible" attribute; arguments as in |
6896 | struct attribute_spec.handler. */ | |
6897 | ||
6898 | static tree | |
6899 | handle_externally_visible_attribute (tree *pnode, tree name, | |
6900 | tree ARG_UNUSED (args), | |
6901 | int ARG_UNUSED (flags), | |
6902 | bool *no_add_attrs) | |
6903 | { | |
6904 | tree node = *pnode; | |
6905 | ||
ba12ea31 | 6906 | if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL) |
62eec3b4 | 6907 | { |
ba12ea31 | 6908 | if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL |
6909 | && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node)) | |
6910 | { | |
6911 | warning (OPT_Wattributes, | |
6912 | "%qE attribute have effect only on public objects", name); | |
6913 | *no_add_attrs = true; | |
6914 | } | |
62eec3b4 | 6915 | } |
62eec3b4 | 6916 | else |
6917 | { | |
6918 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6919 | *no_add_attrs = true; | |
6920 | } | |
6921 | ||
6922 | return NULL_TREE; | |
6923 | } | |
6924 | ||
f8e93a2e | 6925 | /* Handle a "const" attribute; arguments as in |
6926 | struct attribute_spec.handler. */ | |
6927 | ||
6928 | static tree | |
9a03a746 | 6929 | handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6930 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6931 | { |
6932 | tree type = TREE_TYPE (*node); | |
6933 | ||
6934 | /* See FIXME comment on noreturn in c_common_attribute_table. */ | |
6935 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6936 | TREE_READONLY (*node) = 1; | |
6937 | else if (TREE_CODE (type) == POINTER_TYPE | |
6938 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
6939 | TREE_TYPE (*node) | |
6940 | = build_pointer_type | |
6941 | (build_type_variant (TREE_TYPE (type), 1, | |
6942 | TREE_THIS_VOLATILE (TREE_TYPE (type)))); | |
6943 | else | |
6944 | { | |
9b2d6d13 | 6945 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6946 | *no_add_attrs = true; |
6947 | } | |
6948 | ||
6949 | return NULL_TREE; | |
6950 | } | |
6951 | ||
6952 | /* Handle a "transparent_union" attribute; arguments as in | |
6953 | struct attribute_spec.handler. */ | |
6954 | ||
6955 | static tree | |
1cae46be | 6956 | handle_transparent_union_attribute (tree *node, tree name, |
9a03a746 | 6957 | tree ARG_UNUSED (args), int flags, |
09347743 | 6958 | bool *no_add_attrs) |
f8e93a2e | 6959 | { |
881eb642 | 6960 | tree type; |
03908818 | 6961 | |
6962 | *no_add_attrs = true; | |
f8e93a2e | 6963 | |
ffcdbf9c | 6964 | |
6965 | if (TREE_CODE (*node) == TYPE_DECL | |
6966 | && ! (flags & ATTR_FLAG_CXX11)) | |
881eb642 | 6967 | node = &TREE_TYPE (*node); |
6968 | type = *node; | |
f8e93a2e | 6969 | |
03908818 | 6970 | if (TREE_CODE (type) == UNION_TYPE) |
f8e93a2e | 6971 | { |
fca86134 | 6972 | /* Make sure that the first field will work for a transparent union. |
6973 | If the type isn't complete yet, leave the check to the code in | |
6974 | finish_struct. */ | |
6975 | if (TYPE_SIZE (type)) | |
6976 | { | |
6977 | tree first = first_field (type); | |
6978 | if (first == NULL_TREE | |
6979 | || DECL_ARTIFICIAL (first) | |
6980 | || TYPE_MODE (type) != DECL_MODE (first)) | |
6981 | goto ignored; | |
6982 | } | |
6983 | ||
f8e93a2e | 6984 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
03908818 | 6985 | { |
fca86134 | 6986 | /* If the type isn't complete yet, setting the flag |
6987 | on a variant wouldn't ever be checked. */ | |
6988 | if (!TYPE_SIZE (type)) | |
6989 | goto ignored; | |
6990 | ||
6991 | /* build_duplicate_type doesn't work for C++. */ | |
6992 | if (c_dialect_cxx ()) | |
03908818 | 6993 | goto ignored; |
6994 | ||
6995 | /* A type variant isn't good enough, since we don't a cast | |
6996 | to such a type removed as a no-op. */ | |
6997 | *node = type = build_duplicate_type (type); | |
6998 | } | |
6999 | ||
8df5a43d | 7000 | TYPE_TRANSPARENT_AGGR (type) = 1; |
03908818 | 7001 | return NULL_TREE; |
f8e93a2e | 7002 | } |
7003 | ||
03908818 | 7004 | ignored: |
7005 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 7006 | return NULL_TREE; |
7007 | } | |
7008 | ||
9af7fd5b | 7009 | /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to |
7010 | get the requested priority for a constructor or destructor, | |
7011 | possibly issuing diagnostics for invalid or reserved | |
7012 | priorities. */ | |
7013 | ||
7014 | static priority_type | |
7015 | get_priority (tree args, bool is_destructor) | |
7016 | { | |
7017 | HOST_WIDE_INT pri; | |
6c181a06 | 7018 | tree arg; |
9af7fd5b | 7019 | |
7020 | if (!args) | |
7021 | return DEFAULT_INIT_PRIORITY; | |
48e1416a | 7022 | |
28fbc04f | 7023 | if (!SUPPORTS_INIT_PRIORITY) |
7024 | { | |
7025 | if (is_destructor) | |
7026 | error ("destructor priorities are not supported"); | |
7027 | else | |
7028 | error ("constructor priorities are not supported"); | |
7029 | return DEFAULT_INIT_PRIORITY; | |
7030 | } | |
7031 | ||
6c181a06 | 7032 | arg = TREE_VALUE (args); |
253e1cae | 7033 | if (TREE_CODE (arg) == IDENTIFIER_NODE) |
7034 | goto invalid; | |
7035 | if (arg == error_mark_node) | |
7036 | return DEFAULT_INIT_PRIORITY; | |
c28ddc97 | 7037 | arg = default_conversion (arg); |
35ec552a | 7038 | if (!tree_fits_shwi_p (arg) |
6c181a06 | 7039 | || !INTEGRAL_TYPE_P (TREE_TYPE (arg))) |
9af7fd5b | 7040 | goto invalid; |
7041 | ||
fcb97e84 | 7042 | pri = tree_to_shwi (arg); |
9af7fd5b | 7043 | if (pri < 0 || pri > MAX_INIT_PRIORITY) |
7044 | goto invalid; | |
7045 | ||
7046 | if (pri <= MAX_RESERVED_INIT_PRIORITY) | |
7047 | { | |
7048 | if (is_destructor) | |
7049 | warning (0, | |
7050 | "destructor priorities from 0 to %d are reserved " | |
48e1416a | 7051 | "for the implementation", |
9af7fd5b | 7052 | MAX_RESERVED_INIT_PRIORITY); |
7053 | else | |
7054 | warning (0, | |
7055 | "constructor priorities from 0 to %d are reserved " | |
48e1416a | 7056 | "for the implementation", |
9af7fd5b | 7057 | MAX_RESERVED_INIT_PRIORITY); |
7058 | } | |
7059 | return pri; | |
7060 | ||
7061 | invalid: | |
7062 | if (is_destructor) | |
7063 | error ("destructor priorities must be integers from 0 to %d inclusive", | |
7064 | MAX_INIT_PRIORITY); | |
7065 | else | |
7066 | error ("constructor priorities must be integers from 0 to %d inclusive", | |
7067 | MAX_INIT_PRIORITY); | |
7068 | return DEFAULT_INIT_PRIORITY; | |
7069 | } | |
7070 | ||
f8e93a2e | 7071 | /* Handle a "constructor" attribute; arguments as in |
7072 | struct attribute_spec.handler. */ | |
7073 | ||
7074 | static tree | |
9af7fd5b | 7075 | handle_constructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 7076 | int ARG_UNUSED (flags), |
09347743 | 7077 | bool *no_add_attrs) |
f8e93a2e | 7078 | { |
7079 | tree decl = *node; | |
7080 | tree type = TREE_TYPE (decl); | |
7081 | ||
7082 | if (TREE_CODE (decl) == FUNCTION_DECL | |
7083 | && TREE_CODE (type) == FUNCTION_TYPE | |
7084 | && decl_function_context (decl) == 0) | |
7085 | { | |
9af7fd5b | 7086 | priority_type priority; |
f8e93a2e | 7087 | DECL_STATIC_CONSTRUCTOR (decl) = 1; |
9af7fd5b | 7088 | priority = get_priority (args, /*is_destructor=*/false); |
7089 | SET_DECL_INIT_PRIORITY (decl, priority); | |
f8e93a2e | 7090 | TREE_USED (decl) = 1; |
7091 | } | |
7092 | else | |
7093 | { | |
9b2d6d13 | 7094 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7095 | *no_add_attrs = true; |
7096 | } | |
7097 | ||
7098 | return NULL_TREE; | |
7099 | } | |
7100 | ||
7101 | /* Handle a "destructor" attribute; arguments as in | |
7102 | struct attribute_spec.handler. */ | |
7103 | ||
7104 | static tree | |
9af7fd5b | 7105 | handle_destructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 7106 | int ARG_UNUSED (flags), |
09347743 | 7107 | bool *no_add_attrs) |
f8e93a2e | 7108 | { |
7109 | tree decl = *node; | |
7110 | tree type = TREE_TYPE (decl); | |
7111 | ||
7112 | if (TREE_CODE (decl) == FUNCTION_DECL | |
7113 | && TREE_CODE (type) == FUNCTION_TYPE | |
7114 | && decl_function_context (decl) == 0) | |
7115 | { | |
9af7fd5b | 7116 | priority_type priority; |
f8e93a2e | 7117 | DECL_STATIC_DESTRUCTOR (decl) = 1; |
9af7fd5b | 7118 | priority = get_priority (args, /*is_destructor=*/true); |
7119 | SET_DECL_FINI_PRIORITY (decl, priority); | |
f8e93a2e | 7120 | TREE_USED (decl) = 1; |
7121 | } | |
7122 | else | |
7123 | { | |
9b2d6d13 | 7124 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7125 | *no_add_attrs = true; |
7126 | } | |
7127 | ||
7128 | return NULL_TREE; | |
7129 | } | |
7130 | ||
1c58e3f1 | 7131 | /* Nonzero if the mode is a valid vector mode for this architecture. |
7132 | This returns nonzero even if there is no hardware support for the | |
7133 | vector mode, but we can emulate with narrower modes. */ | |
7134 | ||
7135 | static int | |
7136 | vector_mode_valid_p (enum machine_mode mode) | |
7137 | { | |
7138 | enum mode_class mclass = GET_MODE_CLASS (mode); | |
7139 | enum machine_mode innermode; | |
7140 | ||
7141 | /* Doh! What's going on? */ | |
7142 | if (mclass != MODE_VECTOR_INT | |
7143 | && mclass != MODE_VECTOR_FLOAT | |
7144 | && mclass != MODE_VECTOR_FRACT | |
7145 | && mclass != MODE_VECTOR_UFRACT | |
7146 | && mclass != MODE_VECTOR_ACCUM | |
7147 | && mclass != MODE_VECTOR_UACCUM) | |
7148 | return 0; | |
7149 | ||
7150 | /* Hardware support. Woo hoo! */ | |
7151 | if (targetm.vector_mode_supported_p (mode)) | |
7152 | return 1; | |
7153 | ||
7154 | innermode = GET_MODE_INNER (mode); | |
7155 | ||
7156 | /* We should probably return 1 if requesting V4DI and we have no DI, | |
7157 | but we have V2DI, but this is probably very unlikely. */ | |
7158 | ||
7159 | /* If we have support for the inner mode, we can safely emulate it. | |
7160 | We may not have V2DI, but me can emulate with a pair of DIs. */ | |
7161 | return targetm.scalar_mode_supported_p (innermode); | |
7162 | } | |
7163 | ||
7164 | ||
f8e93a2e | 7165 | /* Handle a "mode" attribute; arguments as in |
7166 | struct attribute_spec.handler. */ | |
7167 | ||
7168 | static tree | |
9a03a746 | 7169 | handle_mode_attribute (tree *node, tree name, tree args, |
7170 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7171 | { |
7172 | tree type = *node; | |
d1dd9ac0 | 7173 | tree ident = TREE_VALUE (args); |
f8e93a2e | 7174 | |
7175 | *no_add_attrs = true; | |
7176 | ||
d1dd9ac0 | 7177 | if (TREE_CODE (ident) != IDENTIFIER_NODE) |
9b2d6d13 | 7178 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7179 | else |
7180 | { | |
7181 | int j; | |
d1dd9ac0 | 7182 | const char *p = IDENTIFIER_POINTER (ident); |
f8e93a2e | 7183 | int len = strlen (p); |
7184 | enum machine_mode mode = VOIDmode; | |
7185 | tree typefm; | |
b2aef146 | 7186 | bool valid_mode; |
f8e93a2e | 7187 | |
7188 | if (len > 4 && p[0] == '_' && p[1] == '_' | |
7189 | && p[len - 1] == '_' && p[len - 2] == '_') | |
7190 | { | |
4fd61bc6 | 7191 | char *newp = (char *) alloca (len - 1); |
f8e93a2e | 7192 | |
7193 | strcpy (newp, &p[2]); | |
7194 | newp[len - 4] = '\0'; | |
7195 | p = newp; | |
7196 | } | |
7197 | ||
7198 | /* Change this type to have a type with the specified mode. | |
7199 | First check for the special modes. */ | |
84166705 | 7200 | if (!strcmp (p, "byte")) |
f8e93a2e | 7201 | mode = byte_mode; |
7202 | else if (!strcmp (p, "word")) | |
7203 | mode = word_mode; | |
84166705 | 7204 | else if (!strcmp (p, "pointer")) |
f8e93a2e | 7205 | mode = ptr_mode; |
0ef89dfd | 7206 | else if (!strcmp (p, "libgcc_cmp_return")) |
7207 | mode = targetm.libgcc_cmp_return_mode (); | |
7208 | else if (!strcmp (p, "libgcc_shift_count")) | |
7209 | mode = targetm.libgcc_shift_count_mode (); | |
1bd43494 | 7210 | else if (!strcmp (p, "unwind_word")) |
7211 | mode = targetm.unwind_word_mode (); | |
f8e93a2e | 7212 | else |
7213 | for (j = 0; j < NUM_MACHINE_MODES; j++) | |
7214 | if (!strcmp (p, GET_MODE_NAME (j))) | |
743a6f47 | 7215 | { |
7216 | mode = (enum machine_mode) j; | |
7217 | break; | |
7218 | } | |
f8e93a2e | 7219 | |
7220 | if (mode == VOIDmode) | |
4917c376 | 7221 | { |
d1dd9ac0 | 7222 | error ("unknown machine mode %qE", ident); |
4917c376 | 7223 | return NULL_TREE; |
7224 | } | |
7225 | ||
b2aef146 | 7226 | valid_mode = false; |
7227 | switch (GET_MODE_CLASS (mode)) | |
4917c376 | 7228 | { |
b2aef146 | 7229 | case MODE_INT: |
7230 | case MODE_PARTIAL_INT: | |
7231 | case MODE_FLOAT: | |
c4503c0a | 7232 | case MODE_DECIMAL_FLOAT: |
9421ebb9 | 7233 | case MODE_FRACT: |
7234 | case MODE_UFRACT: | |
7235 | case MODE_ACCUM: | |
7236 | case MODE_UACCUM: | |
b2aef146 | 7237 | valid_mode = targetm.scalar_mode_supported_p (mode); |
7238 | break; | |
7239 | ||
7240 | case MODE_COMPLEX_INT: | |
7241 | case MODE_COMPLEX_FLOAT: | |
7242 | valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode)); | |
7243 | break; | |
7244 | ||
7245 | case MODE_VECTOR_INT: | |
7246 | case MODE_VECTOR_FLOAT: | |
9421ebb9 | 7247 | case MODE_VECTOR_FRACT: |
7248 | case MODE_VECTOR_UFRACT: | |
7249 | case MODE_VECTOR_ACCUM: | |
7250 | case MODE_VECTOR_UACCUM: | |
9b2d6d13 | 7251 | warning (OPT_Wattributes, "specifying vector types with " |
7252 | "__attribute__ ((mode)) is deprecated"); | |
7253 | warning (OPT_Wattributes, | |
7254 | "use __attribute__ ((vector_size)) instead"); | |
b2aef146 | 7255 | valid_mode = vector_mode_valid_p (mode); |
7256 | break; | |
4917c376 | 7257 | |
b2aef146 | 7258 | default: |
7259 | break; | |
7260 | } | |
7261 | if (!valid_mode) | |
7262 | { | |
1e5fcbe2 | 7263 | error ("unable to emulate %qs", p); |
b2aef146 | 7264 | return NULL_TREE; |
7265 | } | |
4917c376 | 7266 | |
b2aef146 | 7267 | if (POINTER_TYPE_P (type)) |
ead34f59 | 7268 | { |
6d5d708e | 7269 | addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type)); |
b2aef146 | 7270 | tree (*fn)(tree, enum machine_mode, bool); |
7271 | ||
6d5d708e | 7272 | if (!targetm.addr_space.valid_pointer_mode (mode, as)) |
ead34f59 | 7273 | { |
1e5fcbe2 | 7274 | error ("invalid pointer mode %qs", p); |
ead34f59 | 7275 | return NULL_TREE; |
7276 | } | |
7277 | ||
a0c938f0 | 7278 | if (TREE_CODE (type) == POINTER_TYPE) |
b2aef146 | 7279 | fn = build_pointer_type_for_mode; |
805e22b2 | 7280 | else |
b2aef146 | 7281 | fn = build_reference_type_for_mode; |
7282 | typefm = fn (TREE_TYPE (type), mode, false); | |
ead34f59 | 7283 | } |
b2aef146 | 7284 | else |
9421ebb9 | 7285 | { |
7286 | /* For fixed-point modes, we need to test if the signness of type | |
7287 | and the machine mode are consistent. */ | |
7288 | if (ALL_FIXED_POINT_MODE_P (mode) | |
7289 | && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode)) | |
7290 | { | |
bf776685 | 7291 | error ("signedness of type and machine mode %qs don%'t match", p); |
9421ebb9 | 7292 | return NULL_TREE; |
7293 | } | |
7294 | /* For fixed-point modes, we need to pass saturating info. */ | |
7295 | typefm = lang_hooks.types.type_for_mode (mode, | |
7296 | ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type) | |
7297 | : TYPE_UNSIGNED (type)); | |
7298 | } | |
3a648ab9 | 7299 | |
b2aef146 | 7300 | if (typefm == NULL_TREE) |
7301 | { | |
743a6f47 | 7302 | error ("no data type for mode %qs", p); |
b2aef146 | 7303 | return NULL_TREE; |
7304 | } | |
3a648ab9 | 7305 | else if (TREE_CODE (type) == ENUMERAL_TYPE) |
7306 | { | |
7307 | /* For enumeral types, copy the precision from the integer | |
7308 | type returned above. If not an INTEGER_TYPE, we can't use | |
7309 | this mode for this type. */ | |
7310 | if (TREE_CODE (typefm) != INTEGER_TYPE) | |
7311 | { | |
743a6f47 | 7312 | error ("cannot use mode %qs for enumeral types", p); |
3a648ab9 | 7313 | return NULL_TREE; |
7314 | } | |
7315 | ||
10080eac | 7316 | if (flags & ATTR_FLAG_TYPE_IN_PLACE) |
7317 | { | |
7318 | TYPE_PRECISION (type) = TYPE_PRECISION (typefm); | |
7319 | typefm = type; | |
7320 | } | |
7321 | else | |
7322 | { | |
7323 | /* We cannot build a type variant, as there's code that assumes | |
7324 | that TYPE_MAIN_VARIANT has the same mode. This includes the | |
7325 | debug generators. Instead, create a subrange type. This | |
7326 | results in all of the enumeral values being emitted only once | |
7327 | in the original, and the subtype gets them by reference. */ | |
7328 | if (TYPE_UNSIGNED (type)) | |
7329 | typefm = make_unsigned_type (TYPE_PRECISION (typefm)); | |
7330 | else | |
7331 | typefm = make_signed_type (TYPE_PRECISION (typefm)); | |
7332 | TREE_TYPE (typefm) = type; | |
7333 | } | |
3a648ab9 | 7334 | } |
4bf450a1 | 7335 | else if (VECTOR_MODE_P (mode) |
7336 | ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm)) | |
7337 | : TREE_CODE (type) != TREE_CODE (typefm)) | |
743a6f47 | 7338 | { |
7339 | error ("mode %qs applied to inappropriate type", p); | |
7340 | return NULL_TREE; | |
7341 | } | |
7342 | ||
b2aef146 | 7343 | *node = typefm; |
f8e93a2e | 7344 | } |
7345 | ||
7346 | return NULL_TREE; | |
7347 | } | |
7348 | ||
7349 | /* Handle a "section" attribute; arguments as in | |
7350 | struct attribute_spec.handler. */ | |
7351 | ||
7352 | static tree | |
9a03a746 | 7353 | handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
7354 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7355 | { |
7356 | tree decl = *node; | |
7357 | ||
218e3e4e | 7358 | if (targetm_common.have_named_sections) |
f8e93a2e | 7359 | { |
065efcb1 | 7360 | user_defined_section_attribute = true; |
7361 | ||
f8e93a2e | 7362 | if ((TREE_CODE (decl) == FUNCTION_DECL |
7363 | || TREE_CODE (decl) == VAR_DECL) | |
7364 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
7365 | { | |
7366 | if (TREE_CODE (decl) == VAR_DECL | |
7367 | && current_function_decl != NULL_TREE | |
84166705 | 7368 | && !TREE_STATIC (decl)) |
f8e93a2e | 7369 | { |
48e1416a | 7370 | error_at (DECL_SOURCE_LOCATION (decl), |
712d2297 | 7371 | "section attribute cannot be specified for " |
7372 | "local variables"); | |
f8e93a2e | 7373 | *no_add_attrs = true; |
7374 | } | |
7375 | ||
7376 | /* The decl may have already been given a section attribute | |
7377 | from a previous declaration. Ensure they match. */ | |
7378 | else if (DECL_SECTION_NAME (decl) != NULL_TREE | |
7379 | && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), | |
7380 | TREE_STRING_POINTER (TREE_VALUE (args))) != 0) | |
7381 | { | |
3cf8b391 | 7382 | error ("section of %q+D conflicts with previous declaration", |
7383 | *node); | |
f8e93a2e | 7384 | *no_add_attrs = true; |
7385 | } | |
38475469 | 7386 | else if (TREE_CODE (decl) == VAR_DECL |
7387 | && !targetm.have_tls && targetm.emutls.tmpl_section | |
7388 | && DECL_THREAD_LOCAL_P (decl)) | |
7389 | { | |
7390 | error ("section of %q+D cannot be overridden", *node); | |
7391 | *no_add_attrs = true; | |
7392 | } | |
f8e93a2e | 7393 | else |
7394 | DECL_SECTION_NAME (decl) = TREE_VALUE (args); | |
7395 | } | |
7396 | else | |
7397 | { | |
3cf8b391 | 7398 | error ("section attribute not allowed for %q+D", *node); |
f8e93a2e | 7399 | *no_add_attrs = true; |
7400 | } | |
7401 | } | |
7402 | else | |
7403 | { | |
712d2297 | 7404 | error_at (DECL_SOURCE_LOCATION (*node), |
7405 | "section attributes are not supported for this target"); | |
f8e93a2e | 7406 | *no_add_attrs = true; |
7407 | } | |
7408 | ||
7409 | return NULL_TREE; | |
7410 | } | |
7411 | ||
83e25171 | 7412 | /* Check whether ALIGN is a valid user-specified alignment. If so, |
7413 | return its base-2 log; if not, output an error and return -1. If | |
7414 | ALLOW_ZERO then 0 is valid and should result in a return of -1 with | |
7415 | no error. */ | |
7416 | int | |
7417 | check_user_alignment (const_tree align, bool allow_zero) | |
7418 | { | |
7419 | int i; | |
7420 | ||
5abaa10a | 7421 | if (TREE_CODE (align) != INTEGER_CST |
7422 | || !INTEGRAL_TYPE_P (TREE_TYPE (align))) | |
83e25171 | 7423 | { |
7424 | error ("requested alignment is not an integer constant"); | |
7425 | return -1; | |
7426 | } | |
7427 | else if (allow_zero && integer_zerop (align)) | |
7428 | return -1; | |
1a087624 | 7429 | else if (tree_int_cst_sgn (align) == -1 |
7430 | || (i = tree_log2 (align)) == -1) | |
83e25171 | 7431 | { |
1a087624 | 7432 | error ("requested alignment is not a positive power of 2"); |
83e25171 | 7433 | return -1; |
7434 | } | |
7435 | else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG) | |
7436 | { | |
7437 | error ("requested alignment is too large"); | |
7438 | return -1; | |
7439 | } | |
7440 | return i; | |
7441 | } | |
7442 | ||
ffcdbf9c | 7443 | /* |
7444 | If in c++-11, check if the c++-11 alignment constraint with respect | |
7445 | to fundamental alignment (in [dcl.align]) are satisfied. If not in | |
7446 | c++-11 mode, does nothing. | |
7447 | ||
7448 | [dcl.align]2/ says: | |
7449 | ||
7450 | [* if the constant expression evaluates to a fundamental alignment, | |
7451 | the alignment requirement of the declared entity shall be the | |
7452 | specified fundamental alignment. | |
7453 | ||
7454 | * if the constant expression evaluates to an extended alignment | |
7455 | and the implementation supports that alignment in the context | |
7456 | of the declaration, the alignment of the declared entity shall | |
7457 | be that alignment | |
7458 | ||
7459 | * if the constant expression evaluates to an extended alignment | |
7460 | and the implementation does not support that alignment in the | |
7461 | context of the declaration, the program is ill-formed]. */ | |
7462 | ||
7463 | static bool | |
7464 | check_cxx_fundamental_alignment_constraints (tree node, | |
7465 | unsigned align_log, | |
7466 | int flags) | |
7467 | { | |
7468 | bool alignment_too_large_p = false; | |
7469 | unsigned requested_alignment = 1U << align_log; | |
7470 | unsigned max_align = 0; | |
7471 | ||
7472 | if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat) | |
7473 | || (node == NULL_TREE || node == error_mark_node)) | |
7474 | return true; | |
7475 | ||
7476 | if (cxx_fundamental_alignment_p (requested_alignment)) | |
7477 | return true; | |
7478 | ||
7479 | if (DECL_P (node)) | |
7480 | { | |
7481 | if (TREE_STATIC (node)) | |
7482 | { | |
7483 | /* For file scope variables and static members, the target | |
7484 | supports alignments that are at most | |
7485 | MAX_OFILE_ALIGNMENT. */ | |
7486 | if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT)) | |
7487 | alignment_too_large_p = true; | |
7488 | } | |
7489 | else | |
7490 | { | |
7491 | #ifdef BIGGEST_FIELD_ALIGNMENT | |
7492 | #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT | |
7493 | #else | |
7494 | #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT | |
7495 | #endif | |
7496 | /* For non-static members, the target supports either | |
7497 | alignments that at most either BIGGEST_FIELD_ALIGNMENT | |
7498 | if it is defined or BIGGEST_ALIGNMENT. */ | |
7499 | max_align = MAX_TARGET_FIELD_ALIGNMENT; | |
7500 | if (TREE_CODE (node) == FIELD_DECL | |
7501 | && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT)) | |
7502 | alignment_too_large_p = true; | |
7503 | #undef MAX_TARGET_FIELD_ALIGNMENT | |
7504 | /* For stack variables, the target supports at most | |
7505 | MAX_STACK_ALIGNMENT. */ | |
7506 | else if (decl_function_context (node) != NULL | |
7507 | && requested_alignment > (max_align = MAX_STACK_ALIGNMENT)) | |
7508 | alignment_too_large_p = true; | |
7509 | } | |
7510 | } | |
7511 | else if (TYPE_P (node)) | |
7512 | { | |
7513 | /* Let's be liberal for types. */ | |
7514 | if (requested_alignment > (max_align = BIGGEST_ALIGNMENT)) | |
7515 | alignment_too_large_p = true; | |
7516 | } | |
7517 | ||
7518 | if (alignment_too_large_p) | |
7519 | pedwarn (input_location, OPT_Wattributes, | |
7520 | "requested alignment %d is larger than %d", | |
7521 | requested_alignment, max_align); | |
7522 | ||
7523 | return !alignment_too_large_p; | |
7524 | } | |
7525 | ||
f8e93a2e | 7526 | /* Handle a "aligned" attribute; arguments as in |
7527 | struct attribute_spec.handler. */ | |
7528 | ||
7529 | static tree | |
9a03a746 | 7530 | handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
09347743 | 7531 | int flags, bool *no_add_attrs) |
f8e93a2e | 7532 | { |
7533 | tree decl = NULL_TREE; | |
7534 | tree *type = NULL; | |
7535 | int is_type = 0; | |
caf62483 | 7536 | tree align_expr; |
f8e93a2e | 7537 | int i; |
7538 | ||
caf62483 | 7539 | if (args) |
7540 | { | |
7541 | align_expr = TREE_VALUE (args); | |
7542 | if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE) | |
7543 | align_expr = default_conversion (align_expr); | |
7544 | } | |
7545 | else | |
7546 | align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT); | |
7547 | ||
f8e93a2e | 7548 | if (DECL_P (*node)) |
7549 | { | |
7550 | decl = *node; | |
7551 | type = &TREE_TYPE (decl); | |
7552 | is_type = TREE_CODE (*node) == TYPE_DECL; | |
7553 | } | |
7554 | else if (TYPE_P (*node)) | |
7555 | type = node, is_type = 1; | |
7556 | ||
ffcdbf9c | 7557 | if ((i = check_user_alignment (align_expr, false)) == -1 |
7558 | || !check_cxx_fundamental_alignment_constraints (*node, i, flags)) | |
83e25171 | 7559 | *no_add_attrs = true; |
f8e93a2e | 7560 | else if (is_type) |
7561 | { | |
2ec3af9c | 7562 | if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
7563 | /* OK, modify the type in place. */; | |
f8e93a2e | 7564 | /* If we have a TYPE_DECL, then copy the type, so that we |
7565 | don't accidentally modify a builtin type. See pushdecl. */ | |
2ec3af9c | 7566 | else if (decl && TREE_TYPE (decl) != error_mark_node |
7567 | && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) | |
f8e93a2e | 7568 | { |
7569 | tree tt = TREE_TYPE (decl); | |
e086912e | 7570 | *type = build_variant_type_copy (*type); |
f8e93a2e | 7571 | DECL_ORIGINAL_TYPE (decl) = tt; |
7572 | TYPE_NAME (*type) = decl; | |
7573 | TREE_USED (*type) = TREE_USED (decl); | |
7574 | TREE_TYPE (decl) = *type; | |
7575 | } | |
2ec3af9c | 7576 | else |
e086912e | 7577 | *type = build_variant_type_copy (*type); |
f8e93a2e | 7578 | |
7cfdc2f0 | 7579 | TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT; |
f8e93a2e | 7580 | TYPE_USER_ALIGN (*type) = 1; |
7581 | } | |
097b5c8b | 7582 | else if (! VAR_OR_FUNCTION_DECL_P (decl) |
f8e93a2e | 7583 | && TREE_CODE (decl) != FIELD_DECL) |
7584 | { | |
3cf8b391 | 7585 | error ("alignment may not be specified for %q+D", decl); |
f8e93a2e | 7586 | *no_add_attrs = true; |
7587 | } | |
ffcdbf9c | 7588 | else if (DECL_USER_ALIGN (decl) |
7589 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) | |
7590 | /* C++-11 [dcl.align/4]: | |
7591 | ||
7592 | When multiple alignment-specifiers are specified for an | |
7593 | entity, the alignment requirement shall be set to the | |
7594 | strictest specified alignment. | |
7595 | ||
7596 | This formally comes from the c++11 specification but we are | |
7597 | doing it for the GNU attribute syntax as well. */ | |
7598 | *no_add_attrs = true; | |
097b5c8b | 7599 | else if (TREE_CODE (decl) == FUNCTION_DECL |
7cfdc2f0 | 7600 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) |
097b5c8b | 7601 | { |
7602 | if (DECL_USER_ALIGN (decl)) | |
7603 | error ("alignment for %q+D was previously specified as %d " | |
7604 | "and may not be decreased", decl, | |
7605 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
7606 | else | |
7607 | error ("alignment for %q+D must be at least %d", decl, | |
7608 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
7609 | *no_add_attrs = true; | |
7610 | } | |
f8e93a2e | 7611 | else |
7612 | { | |
7cfdc2f0 | 7613 | DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT; |
f8e93a2e | 7614 | DECL_USER_ALIGN (decl) = 1; |
7615 | } | |
7616 | ||
7617 | return NULL_TREE; | |
7618 | } | |
7619 | ||
7620 | /* Handle a "weak" attribute; arguments as in | |
7621 | struct attribute_spec.handler. */ | |
7622 | ||
7623 | static tree | |
f948b309 | 7624 | handle_weak_attribute (tree *node, tree name, |
9a03a746 | 7625 | tree ARG_UNUSED (args), |
7626 | int ARG_UNUSED (flags), | |
7627 | bool * ARG_UNUSED (no_add_attrs)) | |
f8e93a2e | 7628 | { |
f948b309 | 7629 | if (TREE_CODE (*node) == FUNCTION_DECL |
059a60f3 | 7630 | && DECL_DECLARED_INLINE_P (*node)) |
7631 | { | |
0725e25c | 7632 | warning (OPT_Wattributes, "inline function %q+D declared weak", *node); |
059a60f3 | 7633 | *no_add_attrs = true; |
7634 | } | |
85c0a25c | 7635 | else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
7636 | { | |
7637 | error ("indirect function %q+D cannot be declared weak", *node); | |
7638 | *no_add_attrs = true; | |
7639 | return NULL_TREE; | |
7640 | } | |
059a60f3 | 7641 | else if (TREE_CODE (*node) == FUNCTION_DECL |
7642 | || TREE_CODE (*node) == VAR_DECL) | |
f948b309 | 7643 | declare_weak (*node); |
7644 | else | |
7645 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 7646 | |
7647 | return NULL_TREE; | |
7648 | } | |
7649 | ||
85c0a25c | 7650 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
7651 | struct attribute_spec.handler, except that IS_ALIAS tells us | |
7652 | whether this is an alias as opposed to ifunc attribute. */ | |
f8e93a2e | 7653 | |
7654 | static tree | |
85c0a25c | 7655 | handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args, |
7656 | bool *no_add_attrs) | |
f8e93a2e | 7657 | { |
7658 | tree decl = *node; | |
7659 | ||
85c0a25c | 7660 | if (TREE_CODE (decl) != FUNCTION_DECL |
7661 | && (!is_alias || TREE_CODE (decl) != VAR_DECL)) | |
9e830260 | 7662 | { |
7663 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7664 | *no_add_attrs = true; | |
7665 | } | |
7666 | else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) | |
48e1416a | 7667 | || (TREE_CODE (decl) != FUNCTION_DECL |
0a3ecdc1 | 7668 | && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) |
7669 | /* A static variable declaration is always a tentative definition, | |
7670 | but the alias is a non-tentative definition which overrides. */ | |
48e1416a | 7671 | || (TREE_CODE (decl) != FUNCTION_DECL |
0a3ecdc1 | 7672 | && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl))) |
f8e93a2e | 7673 | { |
85c0a25c | 7674 | error ("%q+D defined both normally and as %qE attribute", decl, name); |
f8e93a2e | 7675 | *no_add_attrs = true; |
85c0a25c | 7676 | return NULL_TREE; |
f8e93a2e | 7677 | } |
85c0a25c | 7678 | else if (!is_alias |
7679 | && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl)) | |
7680 | || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))) | |
7681 | { | |
7682 | error ("weak %q+D cannot be defined %qE", decl, name); | |
7683 | *no_add_attrs = true; | |
7684 | return NULL_TREE; | |
7685 | } | |
8c42f0d9 | 7686 | |
7687 | /* Note that the very first time we process a nested declaration, | |
7688 | decl_function_context will not be set. Indeed, *would* never | |
7689 | be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that | |
7690 | we do below. After such frobbery, pushdecl would set the context. | |
7691 | In any case, this is never what we want. */ | |
7692 | else if (decl_function_context (decl) == 0 && current_function_decl == NULL) | |
f8e93a2e | 7693 | { |
7694 | tree id; | |
7695 | ||
7696 | id = TREE_VALUE (args); | |
7697 | if (TREE_CODE (id) != STRING_CST) | |
7698 | { | |
85c0a25c | 7699 | error ("attribute %qE argument not a string", name); |
f8e93a2e | 7700 | *no_add_attrs = true; |
7701 | return NULL_TREE; | |
7702 | } | |
7703 | id = get_identifier (TREE_STRING_POINTER (id)); | |
7704 | /* This counts as a use of the object pointed to. */ | |
7705 | TREE_USED (id) = 1; | |
7706 | ||
7707 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
7708 | DECL_INITIAL (decl) = error_mark_node; | |
7709 | else | |
f2526cce | 7710 | TREE_STATIC (decl) = 1; |
85c0a25c | 7711 | |
7712 | if (!is_alias) | |
7713 | /* ifuncs are also aliases, so set that attribute too. */ | |
7714 | DECL_ATTRIBUTES (decl) | |
7715 | = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl)); | |
f8e93a2e | 7716 | } |
7717 | else | |
7718 | { | |
9b2d6d13 | 7719 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7720 | *no_add_attrs = true; |
7721 | } | |
7722 | ||
7723 | return NULL_TREE; | |
7724 | } | |
7725 | ||
85c0a25c | 7726 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
7727 | struct attribute_spec.handler. */ | |
7728 | ||
7729 | static tree | |
7730 | handle_ifunc_attribute (tree *node, tree name, tree args, | |
7731 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7732 | { | |
7733 | return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs); | |
7734 | } | |
7735 | ||
7736 | /* Handle an "alias" or "ifunc" attribute; arguments as in | |
7737 | struct attribute_spec.handler. */ | |
7738 | ||
7739 | static tree | |
7740 | handle_alias_attribute (tree *node, tree name, tree args, | |
7741 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7742 | { | |
7743 | return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs); | |
7744 | } | |
7745 | ||
f4a30bd7 | 7746 | /* Handle a "weakref" attribute; arguments as in struct |
7747 | attribute_spec.handler. */ | |
7748 | ||
7749 | static tree | |
7750 | handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
7751 | int flags, bool *no_add_attrs) | |
7752 | { | |
7753 | tree attr = NULL_TREE; | |
7754 | ||
83852912 | 7755 | /* We must ignore the attribute when it is associated with |
7756 | local-scoped decls, since attribute alias is ignored and many | |
7757 | such symbols do not even have a DECL_WEAK field. */ | |
64a7bd81 | 7758 | if (decl_function_context (*node) |
7759 | || current_function_decl | |
7760 | || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL)) | |
83852912 | 7761 | { |
7762 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7763 | *no_add_attrs = true; | |
7764 | return NULL_TREE; | |
7765 | } | |
7766 | ||
85c0a25c | 7767 | if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
7768 | { | |
7769 | error ("indirect function %q+D cannot be declared weakref", *node); | |
7770 | *no_add_attrs = true; | |
7771 | return NULL_TREE; | |
7772 | } | |
7773 | ||
f4a30bd7 | 7774 | /* The idea here is that `weakref("name")' mutates into `weakref, |
7775 | alias("name")', and weakref without arguments, in turn, | |
7776 | implicitly adds weak. */ | |
7777 | ||
7778 | if (args) | |
7779 | { | |
7780 | attr = tree_cons (get_identifier ("alias"), args, attr); | |
7781 | attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr); | |
7782 | ||
7783 | *no_add_attrs = true; | |
0a3ecdc1 | 7784 | |
7785 | decl_attributes (node, attr, flags); | |
f4a30bd7 | 7786 | } |
7787 | else | |
7788 | { | |
7789 | if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node))) | |
712d2297 | 7790 | error_at (DECL_SOURCE_LOCATION (*node), |
7791 | "weakref attribute must appear before alias attribute"); | |
f4a30bd7 | 7792 | |
0a3ecdc1 | 7793 | /* Can't call declare_weak because it wants this to be TREE_PUBLIC, |
7794 | and that isn't supported; and because it wants to add it to | |
7795 | the list of weak decls, which isn't helpful. */ | |
7796 | DECL_WEAK (*node) = 1; | |
f4a30bd7 | 7797 | } |
7798 | ||
f4a30bd7 | 7799 | return NULL_TREE; |
7800 | } | |
7801 | ||
f8e93a2e | 7802 | /* Handle an "visibility" attribute; arguments as in |
7803 | struct attribute_spec.handler. */ | |
7804 | ||
7805 | static tree | |
1cae46be | 7806 | handle_visibility_attribute (tree *node, tree name, tree args, |
9a03a746 | 7807 | int ARG_UNUSED (flags), |
4a2849cb | 7808 | bool *ARG_UNUSED (no_add_attrs)) |
f8e93a2e | 7809 | { |
7810 | tree decl = *node; | |
9c40570a | 7811 | tree id = TREE_VALUE (args); |
4a2849cb | 7812 | enum symbol_visibility vis; |
f8e93a2e | 7813 | |
b212f378 | 7814 | if (TYPE_P (*node)) |
7815 | { | |
4a2849cb | 7816 | if (TREE_CODE (*node) == ENUMERAL_TYPE) |
7817 | /* OK */; | |
7818 | else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE) | |
7819 | { | |
7820 | warning (OPT_Wattributes, "%qE attribute ignored on non-class types", | |
7821 | name); | |
7822 | return NULL_TREE; | |
7823 | } | |
7824 | else if (TYPE_FIELDS (*node)) | |
7825 | { | |
7826 | error ("%qE attribute ignored because %qT is already defined", | |
7827 | name, *node); | |
7828 | return NULL_TREE; | |
7829 | } | |
b212f378 | 7830 | } |
84166705 | 7831 | else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl)) |
f8e93a2e | 7832 | { |
9b2d6d13 | 7833 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
9c40570a | 7834 | return NULL_TREE; |
f8e93a2e | 7835 | } |
f8e93a2e | 7836 | |
9c40570a | 7837 | if (TREE_CODE (id) != STRING_CST) |
7838 | { | |
07e3a3d2 | 7839 | error ("visibility argument not a string"); |
9c40570a | 7840 | return NULL_TREE; |
f8e93a2e | 7841 | } |
b27ac6b5 | 7842 | |
b212f378 | 7843 | /* If this is a type, set the visibility on the type decl. */ |
7844 | if (TYPE_P (decl)) | |
7845 | { | |
7846 | decl = TYPE_NAME (decl); | |
84166705 | 7847 | if (!decl) |
a0c938f0 | 7848 | return NULL_TREE; |
e147aab3 | 7849 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
7850 | { | |
9b2d6d13 | 7851 | warning (OPT_Wattributes, "%qE attribute ignored on types", |
e147aab3 | 7852 | name); |
7853 | return NULL_TREE; | |
7854 | } | |
b212f378 | 7855 | } |
f8e93a2e | 7856 | |
9c40570a | 7857 | if (strcmp (TREE_STRING_POINTER (id), "default") == 0) |
4a2849cb | 7858 | vis = VISIBILITY_DEFAULT; |
9c40570a | 7859 | else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0) |
4a2849cb | 7860 | vis = VISIBILITY_INTERNAL; |
9c40570a | 7861 | else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0) |
4a2849cb | 7862 | vis = VISIBILITY_HIDDEN; |
9c40570a | 7863 | else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0) |
4a2849cb | 7864 | vis = VISIBILITY_PROTECTED; |
9c40570a | 7865 | else |
4a2849cb | 7866 | { |
7867 | error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\""); | |
7868 | vis = VISIBILITY_DEFAULT; | |
7869 | } | |
7870 | ||
7871 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
098a01e7 | 7872 | && vis != DECL_VISIBILITY (decl)) |
7873 | { | |
7874 | tree attributes = (TYPE_P (*node) | |
7875 | ? TYPE_ATTRIBUTES (*node) | |
7876 | : DECL_ATTRIBUTES (decl)); | |
7877 | if (lookup_attribute ("visibility", attributes)) | |
7878 | error ("%qD redeclared with different visibility", decl); | |
7879 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
7880 | && lookup_attribute ("dllimport", attributes)) | |
7881 | error ("%qD was declared %qs which implies default visibility", | |
7882 | decl, "dllimport"); | |
7883 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
7884 | && lookup_attribute ("dllexport", attributes)) | |
7885 | error ("%qD was declared %qs which implies default visibility", | |
7886 | decl, "dllexport"); | |
7887 | } | |
4a2849cb | 7888 | |
7889 | DECL_VISIBILITY (decl) = vis; | |
b212f378 | 7890 | DECL_VISIBILITY_SPECIFIED (decl) = 1; |
7891 | ||
4a2849cb | 7892 | /* Go ahead and attach the attribute to the node as well. This is needed |
7893 | so we can determine whether we have VISIBILITY_DEFAULT because the | |
7894 | visibility was not specified, or because it was explicitly overridden | |
7895 | from the containing scope. */ | |
9c40570a | 7896 | |
f8e93a2e | 7897 | return NULL_TREE; |
7898 | } | |
7899 | ||
3aa0c315 | 7900 | /* Determine the ELF symbol visibility for DECL, which is either a |
7901 | variable or a function. It is an error to use this function if a | |
7902 | definition of DECL is not available in this translation unit. | |
7903 | Returns true if the final visibility has been determined by this | |
7904 | function; false if the caller is free to make additional | |
7905 | modifications. */ | |
7906 | ||
7907 | bool | |
7908 | c_determine_visibility (tree decl) | |
7909 | { | |
1d8fc210 | 7910 | gcc_assert (TREE_CODE (decl) == VAR_DECL |
7911 | || TREE_CODE (decl) == FUNCTION_DECL); | |
3aa0c315 | 7912 | |
7913 | /* If the user explicitly specified the visibility with an | |
7914 | attribute, honor that. DECL_VISIBILITY will have been set during | |
920f5a70 | 7915 | the processing of the attribute. We check for an explicit |
7916 | attribute, rather than just checking DECL_VISIBILITY_SPECIFIED, | |
7917 | to distinguish the use of an attribute from the use of a "#pragma | |
7918 | GCC visibility push(...)"; in the latter case we still want other | |
7919 | considerations to be able to overrule the #pragma. */ | |
7920 | if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)) | |
7921 | || (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
7922 | && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) | |
7923 | || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))))) | |
3aa0c315 | 7924 | return true; |
7925 | ||
4a2849cb | 7926 | /* Set default visibility to whatever the user supplied with |
7927 | visibility_specified depending on #pragma GCC visibility. */ | |
7928 | if (!DECL_VISIBILITY_SPECIFIED (decl)) | |
7929 | { | |
2d9d8740 | 7930 | if (visibility_options.inpragma |
7931 | || DECL_VISIBILITY (decl) != default_visibility) | |
7932 | { | |
7933 | DECL_VISIBILITY (decl) = default_visibility; | |
7934 | DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; | |
7935 | /* If visibility changed and DECL already has DECL_RTL, ensure | |
7936 | symbol flags are updated. */ | |
7937 | if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)) | |
7938 | || TREE_CODE (decl) == FUNCTION_DECL) | |
7939 | && DECL_RTL_SET_P (decl)) | |
7940 | make_decl_rtl (decl); | |
7941 | } | |
4a2849cb | 7942 | } |
3aa0c315 | 7943 | return false; |
7944 | } | |
7945 | ||
24dfead4 | 7946 | /* Handle an "tls_model" attribute; arguments as in |
7947 | struct attribute_spec.handler. */ | |
7948 | ||
7949 | static tree | |
1cae46be | 7950 | handle_tls_model_attribute (tree *node, tree name, tree args, |
9a03a746 | 7951 | int ARG_UNUSED (flags), bool *no_add_attrs) |
24dfead4 | 7952 | { |
1b53eb20 | 7953 | tree id; |
24dfead4 | 7954 | tree decl = *node; |
1b53eb20 | 7955 | enum tls_model kind; |
24dfead4 | 7956 | |
1b53eb20 | 7957 | *no_add_attrs = true; |
7958 | ||
c34f8a78 | 7959 | if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl)) |
24dfead4 | 7960 | { |
9b2d6d13 | 7961 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
1b53eb20 | 7962 | return NULL_TREE; |
24dfead4 | 7963 | } |
24dfead4 | 7964 | |
1b53eb20 | 7965 | kind = DECL_TLS_MODEL (decl); |
7966 | id = TREE_VALUE (args); | |
7967 | if (TREE_CODE (id) != STRING_CST) | |
7968 | { | |
7969 | error ("tls_model argument not a string"); | |
7970 | return NULL_TREE; | |
24dfead4 | 7971 | } |
7972 | ||
1b53eb20 | 7973 | if (!strcmp (TREE_STRING_POINTER (id), "local-exec")) |
7974 | kind = TLS_MODEL_LOCAL_EXEC; | |
7975 | else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec")) | |
7976 | kind = TLS_MODEL_INITIAL_EXEC; | |
7977 | else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic")) | |
7978 | kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC; | |
7979 | else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic")) | |
7980 | kind = TLS_MODEL_GLOBAL_DYNAMIC; | |
7981 | else | |
7982 | error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\""); | |
7983 | ||
7984 | DECL_TLS_MODEL (decl) = kind; | |
24dfead4 | 7985 | return NULL_TREE; |
7986 | } | |
7987 | ||
f8e93a2e | 7988 | /* Handle a "no_instrument_function" attribute; arguments as in |
7989 | struct attribute_spec.handler. */ | |
7990 | ||
7991 | static tree | |
1cae46be | 7992 | handle_no_instrument_function_attribute (tree *node, tree name, |
9a03a746 | 7993 | tree ARG_UNUSED (args), |
7994 | int ARG_UNUSED (flags), | |
09347743 | 7995 | bool *no_add_attrs) |
f8e93a2e | 7996 | { |
7997 | tree decl = *node; | |
7998 | ||
7999 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8000 | { | |
712d2297 | 8001 | error_at (DECL_SOURCE_LOCATION (decl), |
8002 | "%qE attribute applies only to functions", name); | |
f8e93a2e | 8003 | *no_add_attrs = true; |
8004 | } | |
f8e93a2e | 8005 | else |
8006 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
8007 | ||
8008 | return NULL_TREE; | |
8009 | } | |
8010 | ||
8011 | /* Handle a "malloc" attribute; arguments as in | |
8012 | struct attribute_spec.handler. */ | |
8013 | ||
8014 | static tree | |
9a03a746 | 8015 | handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8016 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 8017 | { |
a5147fca | 8018 | if (TREE_CODE (*node) == FUNCTION_DECL |
8019 | && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) | |
f8e93a2e | 8020 | DECL_IS_MALLOC (*node) = 1; |
f8e93a2e | 8021 | else |
8022 | { | |
9b2d6d13 | 8023 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8024 | *no_add_attrs = true; |
8025 | } | |
8026 | ||
8027 | return NULL_TREE; | |
8028 | } | |
8029 | ||
4a29c97c | 8030 | /* Handle a "alloc_size" attribute; arguments as in |
8031 | struct attribute_spec.handler. */ | |
8032 | ||
8033 | static tree | |
8034 | handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
8035 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8036 | { | |
2802826e | 8037 | unsigned arg_count = type_num_arguments (*node); |
4a29c97c | 8038 | for (; args; args = TREE_CHAIN (args)) |
8039 | { | |
8040 | tree position = TREE_VALUE (args); | |
caf62483 | 8041 | if (position && TREE_CODE (position) != IDENTIFIER_NODE |
8042 | && TREE_CODE (position) != FUNCTION_DECL) | |
8043 | position = default_conversion (position); | |
4a29c97c | 8044 | |
237e78b1 | 8045 | if (!tree_fits_uhwi_p (position) |
8046 | || !arg_count | |
8047 | || !IN_RANGE (tree_to_uhwi (position), 1, arg_count)) | |
4a29c97c | 8048 | { |
48e1416a | 8049 | warning (OPT_Wattributes, |
4a29c97c | 8050 | "alloc_size parameter outside range"); |
8051 | *no_add_attrs = true; | |
8052 | return NULL_TREE; | |
8053 | } | |
8054 | } | |
8055 | return NULL_TREE; | |
8056 | } | |
8057 | ||
237e78b1 | 8058 | /* Handle a "alloc_align" attribute; arguments as in |
8059 | struct attribute_spec.handler. */ | |
8060 | ||
8061 | static tree | |
8062 | handle_alloc_align_attribute (tree *node, tree, tree args, int, | |
8063 | bool *no_add_attrs) | |
8064 | { | |
8065 | unsigned arg_count = type_num_arguments (*node); | |
8066 | tree position = TREE_VALUE (args); | |
8067 | if (position && TREE_CODE (position) != IDENTIFIER_NODE) | |
8068 | position = default_conversion (position); | |
8069 | ||
8070 | if (!tree_fits_uhwi_p (position) | |
8071 | || !arg_count | |
8072 | || !IN_RANGE (tree_to_uhwi (position), 1, arg_count)) | |
8073 | { | |
8074 | warning (OPT_Wattributes, | |
8075 | "alloc_align parameter outside range"); | |
8076 | *no_add_attrs = true; | |
8077 | return NULL_TREE; | |
8078 | } | |
8079 | return NULL_TREE; | |
8080 | } | |
8081 | ||
8082 | /* Handle a "assume_aligned" attribute; arguments as in | |
8083 | struct attribute_spec.handler. */ | |
8084 | ||
8085 | static tree | |
8086 | handle_assume_aligned_attribute (tree *, tree, tree args, int, | |
8087 | bool *no_add_attrs) | |
8088 | { | |
8089 | for (; args; args = TREE_CHAIN (args)) | |
8090 | { | |
8091 | tree position = TREE_VALUE (args); | |
8092 | if (position && TREE_CODE (position) != IDENTIFIER_NODE | |
8093 | && TREE_CODE (position) != FUNCTION_DECL) | |
8094 | position = default_conversion (position); | |
8095 | ||
8096 | if (TREE_CODE (position) != INTEGER_CST) | |
8097 | { | |
8098 | warning (OPT_Wattributes, | |
8099 | "assume_aligned parameter not integer constant"); | |
8100 | *no_add_attrs = true; | |
8101 | return NULL_TREE; | |
8102 | } | |
8103 | } | |
8104 | return NULL_TREE; | |
8105 | } | |
8106 | ||
8ce86007 | 8107 | /* Handle a "fn spec" attribute; arguments as in |
8108 | struct attribute_spec.handler. */ | |
8109 | ||
8110 | static tree | |
8111 | handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name), | |
8112 | tree args, int ARG_UNUSED (flags), | |
8113 | bool *no_add_attrs ATTRIBUTE_UNUSED) | |
8114 | { | |
8115 | gcc_assert (args | |
8116 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST | |
8117 | && !TREE_CHAIN (args)); | |
8118 | return NULL_TREE; | |
8119 | } | |
8120 | ||
a96c3cc1 | 8121 | /* Handle a "warn_unused" attribute; arguments as in |
8122 | struct attribute_spec.handler. */ | |
8123 | ||
8124 | static tree | |
8125 | handle_warn_unused_attribute (tree *node, tree name, | |
8126 | tree args ATTRIBUTE_UNUSED, | |
8127 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
8128 | { | |
8129 | if (TYPE_P (*node)) | |
8130 | /* Do nothing else, just set the attribute. We'll get at | |
8131 | it later with lookup_attribute. */ | |
8132 | ; | |
8133 | else | |
8134 | { | |
8135 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8136 | *no_add_attrs = true; | |
8137 | } | |
8138 | ||
8139 | return NULL_TREE; | |
8140 | } | |
8141 | ||
bc7bff74 | 8142 | /* Handle an "omp declare simd" attribute; arguments as in |
8143 | struct attribute_spec.handler. */ | |
8144 | ||
8145 | static tree | |
8146 | handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *) | |
8147 | { | |
8148 | return NULL_TREE; | |
8149 | } | |
8150 | ||
8151 | /* Handle an "omp declare target" attribute; arguments as in | |
8152 | struct attribute_spec.handler. */ | |
8153 | ||
8154 | static tree | |
8155 | handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *) | |
8156 | { | |
8157 | return NULL_TREE; | |
8158 | } | |
8159 | ||
26d1c5ff | 8160 | /* Handle a "returns_twice" attribute; arguments as in |
8161 | struct attribute_spec.handler. */ | |
8162 | ||
8163 | static tree | |
8164 | handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
8165 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8166 | { | |
8167 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
8168 | DECL_IS_RETURNS_TWICE (*node) = 1; | |
8169 | else | |
8170 | { | |
9b2d6d13 | 8171 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
26d1c5ff | 8172 | *no_add_attrs = true; |
8173 | } | |
8174 | ||
8175 | return NULL_TREE; | |
8176 | } | |
8177 | ||
f8e93a2e | 8178 | /* Handle a "no_limit_stack" attribute; arguments as in |
8179 | struct attribute_spec.handler. */ | |
8180 | ||
8181 | static tree | |
1cae46be | 8182 | handle_no_limit_stack_attribute (tree *node, tree name, |
9a03a746 | 8183 | tree ARG_UNUSED (args), |
8184 | int ARG_UNUSED (flags), | |
09347743 | 8185 | bool *no_add_attrs) |
f8e93a2e | 8186 | { |
8187 | tree decl = *node; | |
8188 | ||
8189 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8190 | { | |
712d2297 | 8191 | error_at (DECL_SOURCE_LOCATION (decl), |
8192 | "%qE attribute applies only to functions", name); | |
f8e93a2e | 8193 | *no_add_attrs = true; |
8194 | } | |
8195 | else if (DECL_INITIAL (decl)) | |
8196 | { | |
712d2297 | 8197 | error_at (DECL_SOURCE_LOCATION (decl), |
8198 | "can%'t set %qE attribute after definition", name); | |
f8e93a2e | 8199 | *no_add_attrs = true; |
8200 | } | |
8201 | else | |
8202 | DECL_NO_LIMIT_STACK (decl) = 1; | |
8203 | ||
8204 | return NULL_TREE; | |
8205 | } | |
8206 | ||
8207 | /* Handle a "pure" attribute; arguments as in | |
8208 | struct attribute_spec.handler. */ | |
8209 | ||
8210 | static tree | |
9a03a746 | 8211 | handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8212 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 8213 | { |
8214 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
9c2a0c05 | 8215 | DECL_PURE_P (*node) = 1; |
f8e93a2e | 8216 | /* ??? TODO: Support types. */ |
8217 | else | |
8218 | { | |
9b2d6d13 | 8219 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8220 | *no_add_attrs = true; |
8221 | } | |
8222 | ||
8223 | return NULL_TREE; | |
8224 | } | |
8225 | ||
4c0315d0 | 8226 | /* Digest an attribute list destined for a transactional memory statement. |
8227 | ALLOWED is the set of attributes that are allowed for this statement; | |
8228 | return the attribute we parsed. Multiple attributes are never allowed. */ | |
8229 | ||
8230 | int | |
8231 | parse_tm_stmt_attr (tree attrs, int allowed) | |
8232 | { | |
8233 | tree a_seen = NULL; | |
8234 | int m_seen = 0; | |
8235 | ||
8236 | for ( ; attrs ; attrs = TREE_CHAIN (attrs)) | |
8237 | { | |
8238 | tree a = TREE_PURPOSE (attrs); | |
8239 | int m = 0; | |
8240 | ||
8241 | if (is_attribute_p ("outer", a)) | |
8242 | m = TM_STMT_ATTR_OUTER; | |
8243 | ||
8244 | if ((m & allowed) == 0) | |
8245 | { | |
8246 | warning (OPT_Wattributes, "%qE attribute directive ignored", a); | |
8247 | continue; | |
8248 | } | |
8249 | ||
8250 | if (m_seen == 0) | |
8251 | { | |
8252 | a_seen = a; | |
8253 | m_seen = m; | |
8254 | } | |
8255 | else if (m_seen == m) | |
8256 | warning (OPT_Wattributes, "%qE attribute duplicated", a); | |
8257 | else | |
8258 | warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen); | |
8259 | } | |
8260 | ||
8261 | return m_seen; | |
8262 | } | |
8263 | ||
8264 | /* Transform a TM attribute name into a maskable integer and back. | |
8265 | Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding | |
8266 | to how the lack of an attribute is treated. */ | |
8267 | ||
8268 | int | |
8269 | tm_attr_to_mask (tree attr) | |
8270 | { | |
8271 | if (attr == NULL) | |
8272 | return 0; | |
8273 | if (is_attribute_p ("transaction_safe", attr)) | |
8274 | return TM_ATTR_SAFE; | |
8275 | if (is_attribute_p ("transaction_callable", attr)) | |
8276 | return TM_ATTR_CALLABLE; | |
8277 | if (is_attribute_p ("transaction_pure", attr)) | |
8278 | return TM_ATTR_PURE; | |
8279 | if (is_attribute_p ("transaction_unsafe", attr)) | |
8280 | return TM_ATTR_IRREVOCABLE; | |
8281 | if (is_attribute_p ("transaction_may_cancel_outer", attr)) | |
8282 | return TM_ATTR_MAY_CANCEL_OUTER; | |
8283 | return 0; | |
8284 | } | |
8285 | ||
8286 | tree | |
8287 | tm_mask_to_attr (int mask) | |
8288 | { | |
8289 | const char *str; | |
8290 | switch (mask) | |
8291 | { | |
8292 | case TM_ATTR_SAFE: | |
8293 | str = "transaction_safe"; | |
8294 | break; | |
8295 | case TM_ATTR_CALLABLE: | |
8296 | str = "transaction_callable"; | |
8297 | break; | |
8298 | case TM_ATTR_PURE: | |
8299 | str = "transaction_pure"; | |
8300 | break; | |
8301 | case TM_ATTR_IRREVOCABLE: | |
8302 | str = "transaction_unsafe"; | |
8303 | break; | |
8304 | case TM_ATTR_MAY_CANCEL_OUTER: | |
8305 | str = "transaction_may_cancel_outer"; | |
8306 | break; | |
8307 | default: | |
8308 | gcc_unreachable (); | |
8309 | } | |
8310 | return get_identifier (str); | |
8311 | } | |
8312 | ||
8313 | /* Return the first TM attribute seen in LIST. */ | |
8314 | ||
8315 | tree | |
8316 | find_tm_attribute (tree list) | |
8317 | { | |
8318 | for (; list ; list = TREE_CHAIN (list)) | |
8319 | { | |
8320 | tree name = TREE_PURPOSE (list); | |
8321 | if (tm_attr_to_mask (name) != 0) | |
8322 | return name; | |
8323 | } | |
8324 | return NULL_TREE; | |
8325 | } | |
8326 | ||
8327 | /* Handle the TM attributes; arguments as in struct attribute_spec.handler. | |
8328 | Here we accept only function types, and verify that none of the other | |
8329 | function TM attributes are also applied. */ | |
8330 | /* ??? We need to accept class types for C++, but not C. This greatly | |
8331 | complicates this function, since we can no longer rely on the extra | |
8332 | processing given by function_type_required. */ | |
8333 | ||
8334 | static tree | |
8335 | handle_tm_attribute (tree *node, tree name, tree args, | |
8336 | int flags, bool *no_add_attrs) | |
8337 | { | |
8338 | /* Only one path adds the attribute; others don't. */ | |
8339 | *no_add_attrs = true; | |
8340 | ||
8341 | switch (TREE_CODE (*node)) | |
8342 | { | |
8343 | case RECORD_TYPE: | |
8344 | case UNION_TYPE: | |
8345 | /* Only tm_callable and tm_safe apply to classes. */ | |
8346 | if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE)) | |
8347 | goto ignored; | |
8348 | /* FALLTHRU */ | |
8349 | ||
8350 | case FUNCTION_TYPE: | |
8351 | case METHOD_TYPE: | |
8352 | { | |
8353 | tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node)); | |
8354 | if (old_name == name) | |
8355 | ; | |
8356 | else if (old_name != NULL_TREE) | |
8357 | error ("type was previously declared %qE", old_name); | |
8358 | else | |
8359 | *no_add_attrs = false; | |
8360 | } | |
8361 | break; | |
8362 | ||
8363 | case POINTER_TYPE: | |
8364 | { | |
8365 | enum tree_code subcode = TREE_CODE (TREE_TYPE (*node)); | |
8366 | if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE) | |
8367 | { | |
8368 | tree fn_tmp = TREE_TYPE (*node); | |
8369 | decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0); | |
8370 | *node = build_pointer_type (fn_tmp); | |
8371 | break; | |
8372 | } | |
8373 | } | |
8374 | /* FALLTHRU */ | |
8375 | ||
8376 | default: | |
8377 | /* If a function is next, pass it on to be tried next. */ | |
8378 | if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) | |
8379 | return tree_cons (name, args, NULL); | |
8380 | ||
8381 | ignored: | |
8382 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8383 | break; | |
8384 | } | |
8385 | ||
8386 | return NULL_TREE; | |
8387 | } | |
8388 | ||
8389 | /* Handle the TM_WRAP attribute; arguments as in | |
8390 | struct attribute_spec.handler. */ | |
8391 | ||
8392 | static tree | |
8393 | handle_tm_wrap_attribute (tree *node, tree name, tree args, | |
8394 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8395 | { | |
8396 | tree decl = *node; | |
8397 | ||
8398 | /* We don't need the attribute even on success, since we | |
8399 | record the entry in an external table. */ | |
8400 | *no_add_attrs = true; | |
8401 | ||
8402 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8403 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8404 | else | |
8405 | { | |
8406 | tree wrap_decl = TREE_VALUE (args); | |
8407 | if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE | |
8408 | && TREE_CODE (wrap_decl) != VAR_DECL | |
8409 | && TREE_CODE (wrap_decl) != FUNCTION_DECL) | |
8410 | error ("%qE argument not an identifier", name); | |
8411 | else | |
8412 | { | |
8413 | if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE) | |
8414 | wrap_decl = lookup_name (wrap_decl); | |
8415 | if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL) | |
8416 | { | |
8417 | if (lang_hooks.types_compatible_p (TREE_TYPE (decl), | |
8418 | TREE_TYPE (wrap_decl))) | |
8419 | record_tm_replacement (wrap_decl, decl); | |
8420 | else | |
8421 | error ("%qD is not compatible with %qD", wrap_decl, decl); | |
8422 | } | |
8423 | else | |
8424 | error ("transaction_wrap argument is not a function"); | |
8425 | } | |
8426 | } | |
8427 | ||
8428 | return NULL_TREE; | |
8429 | } | |
8430 | ||
8431 | /* Ignore the given attribute. Used when this attribute may be usefully | |
8432 | overridden by the target, but is not used generically. */ | |
8433 | ||
8434 | static tree | |
8435 | ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name), | |
8436 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8437 | bool *no_add_attrs) | |
8438 | { | |
8439 | *no_add_attrs = true; | |
8440 | return NULL_TREE; | |
8441 | } | |
8442 | ||
fc09b200 | 8443 | /* Handle a "no vops" attribute; arguments as in |
8444 | struct attribute_spec.handler. */ | |
8445 | ||
8446 | static tree | |
8447 | handle_novops_attribute (tree *node, tree ARG_UNUSED (name), | |
8448 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8449 | bool *ARG_UNUSED (no_add_attrs)) | |
8450 | { | |
8451 | gcc_assert (TREE_CODE (*node) == FUNCTION_DECL); | |
8452 | DECL_IS_NOVOPS (*node) = 1; | |
8453 | return NULL_TREE; | |
8454 | } | |
8455 | ||
f8e93a2e | 8456 | /* Handle a "deprecated" attribute; arguments as in |
8457 | struct attribute_spec.handler. */ | |
1cae46be | 8458 | |
f8e93a2e | 8459 | static tree |
1cae46be | 8460 | handle_deprecated_attribute (tree *node, tree name, |
45c4e798 | 8461 | tree args, int flags, |
09347743 | 8462 | bool *no_add_attrs) |
f8e93a2e | 8463 | { |
8464 | tree type = NULL_TREE; | |
8465 | int warn = 0; | |
782858b8 | 8466 | tree what = NULL_TREE; |
1cae46be | 8467 | |
45c4e798 | 8468 | if (!args) |
8469 | *no_add_attrs = true; | |
8470 | else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST) | |
8471 | { | |
8472 | error ("deprecated message is not a string"); | |
8473 | *no_add_attrs = true; | |
8474 | } | |
8475 | ||
f8e93a2e | 8476 | if (DECL_P (*node)) |
8477 | { | |
8478 | tree decl = *node; | |
8479 | type = TREE_TYPE (decl); | |
1cae46be | 8480 | |
f8e93a2e | 8481 | if (TREE_CODE (decl) == TYPE_DECL |
8482 | || TREE_CODE (decl) == PARM_DECL | |
8483 | || TREE_CODE (decl) == VAR_DECL | |
8484 | || TREE_CODE (decl) == FUNCTION_DECL | |
40c8d1dd | 8485 | || TREE_CODE (decl) == FIELD_DECL |
8486 | || objc_method_decl (TREE_CODE (decl))) | |
f8e93a2e | 8487 | TREE_DEPRECATED (decl) = 1; |
8488 | else | |
8489 | warn = 1; | |
8490 | } | |
8491 | else if (TYPE_P (*node)) | |
8492 | { | |
8493 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 8494 | *node = build_variant_type_copy (*node); |
f8e93a2e | 8495 | TREE_DEPRECATED (*node) = 1; |
8496 | type = *node; | |
8497 | } | |
8498 | else | |
8499 | warn = 1; | |
1cae46be | 8500 | |
f8e93a2e | 8501 | if (warn) |
8502 | { | |
8503 | *no_add_attrs = true; | |
8504 | if (type && TYPE_NAME (type)) | |
8505 | { | |
8506 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
782858b8 | 8507 | what = TYPE_NAME (*node); |
f8e93a2e | 8508 | else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL |
8509 | && DECL_NAME (TYPE_NAME (type))) | |
782858b8 | 8510 | what = DECL_NAME (TYPE_NAME (type)); |
f8e93a2e | 8511 | } |
8512 | if (what) | |
9b2d6d13 | 8513 | warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what); |
f8e93a2e | 8514 | else |
9b2d6d13 | 8515 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8516 | } |
8517 | ||
8518 | return NULL_TREE; | |
8519 | } | |
8520 | ||
f8e93a2e | 8521 | /* Handle a "vector_size" attribute; arguments as in |
8522 | struct attribute_spec.handler. */ | |
8523 | ||
8524 | static tree | |
1cae46be | 8525 | handle_vector_size_attribute (tree *node, tree name, tree args, |
9a03a746 | 8526 | int ARG_UNUSED (flags), |
09347743 | 8527 | bool *no_add_attrs) |
f8e93a2e | 8528 | { |
8529 | unsigned HOST_WIDE_INT vecsize, nunits; | |
83e2a11b | 8530 | enum machine_mode orig_mode; |
4917c376 | 8531 | tree type = *node, new_type, size; |
f8e93a2e | 8532 | |
8533 | *no_add_attrs = true; | |
8534 | ||
4917c376 | 8535 | size = TREE_VALUE (args); |
caf62483 | 8536 | if (size && TREE_CODE (size) != IDENTIFIER_NODE) |
8537 | size = default_conversion (size); | |
4917c376 | 8538 | |
cd4547bf | 8539 | if (!tree_fits_uhwi_p (size)) |
f8e93a2e | 8540 | { |
9b2d6d13 | 8541 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8542 | return NULL_TREE; |
8543 | } | |
8544 | ||
8545 | /* Get the vector size (in bytes). */ | |
6a0712d4 | 8546 | vecsize = tree_to_uhwi (size); |
f8e93a2e | 8547 | |
8548 | /* We need to provide for vector pointers, vector arrays, and | |
8549 | functions returning vectors. For example: | |
8550 | ||
8551 | __attribute__((vector_size(16))) short *foo; | |
8552 | ||
8553 | In this case, the mode is SI, but the type being modified is | |
8554 | HI, so we need to look further. */ | |
8555 | ||
8556 | while (POINTER_TYPE_P (type) | |
8557 | || TREE_CODE (type) == FUNCTION_TYPE | |
5bfb0742 | 8558 | || TREE_CODE (type) == METHOD_TYPE |
2cb0e5d9 | 8559 | || TREE_CODE (type) == ARRAY_TYPE |
8560 | || TREE_CODE (type) == OFFSET_TYPE) | |
f8e93a2e | 8561 | type = TREE_TYPE (type); |
8562 | ||
8563 | /* Get the mode of the type being modified. */ | |
8564 | orig_mode = TYPE_MODE (type); | |
8565 | ||
2cb0e5d9 | 8566 | if ((!INTEGRAL_TYPE_P (type) |
8567 | && !SCALAR_FLOAT_TYPE_P (type) | |
8568 | && !FIXED_POINT_TYPE_P (type)) | |
cee7491d | 8569 | || (!SCALAR_FLOAT_MODE_P (orig_mode) |
9421ebb9 | 8570 | && GET_MODE_CLASS (orig_mode) != MODE_INT |
8571 | && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode)) | |
cd4547bf | 8572 | || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)) |
7ec31215 | 8573 | || TREE_CODE (type) == BOOLEAN_TYPE) |
f8e93a2e | 8574 | { |
782858b8 | 8575 | error ("invalid vector type for attribute %qE", name); |
f8e93a2e | 8576 | return NULL_TREE; |
8577 | } | |
8578 | ||
6a0712d4 | 8579 | if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type))) |
39cc3e6d | 8580 | { |
8581 | error ("vector size not an integral multiple of component size"); | |
8582 | return NULL; | |
8583 | } | |
8584 | ||
8585 | if (vecsize == 0) | |
8586 | { | |
8587 | error ("zero vector size"); | |
8588 | return NULL; | |
8589 | } | |
8590 | ||
f8e93a2e | 8591 | /* Calculate how many units fit in the vector. */ |
6a0712d4 | 8592 | nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
83e2a11b | 8593 | if (nunits & (nunits - 1)) |
f8e93a2e | 8594 | { |
83e2a11b | 8595 | error ("number of components of the vector not a power of two"); |
f8e93a2e | 8596 | return NULL_TREE; |
8597 | } | |
8598 | ||
83e2a11b | 8599 | new_type = build_vector_type (type, nunits); |
f8e93a2e | 8600 | |
8601 | /* Build back pointers if needed. */ | |
d991e6e8 | 8602 | *node = lang_hooks.types.reconstruct_complex_type (*node, new_type); |
f8e93a2e | 8603 | |
8604 | return NULL_TREE; | |
8605 | } | |
8606 | ||
dbf6c367 | 8607 | /* Handle the "nonnull" attribute. */ |
8608 | static tree | |
9a03a746 | 8609 | handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), |
8610 | tree args, int ARG_UNUSED (flags), | |
09347743 | 8611 | bool *no_add_attrs) |
dbf6c367 | 8612 | { |
8613 | tree type = *node; | |
8614 | unsigned HOST_WIDE_INT attr_arg_num; | |
8615 | ||
8616 | /* If no arguments are specified, all pointer arguments should be | |
d716ce75 | 8617 | non-null. Verify a full prototype is given so that the arguments |
dbf6c367 | 8618 | will have the correct types when we actually check them later. */ |
84166705 | 8619 | if (!args) |
dbf6c367 | 8620 | { |
a36cf284 | 8621 | if (!prototype_p (type)) |
dbf6c367 | 8622 | { |
8623 | error ("nonnull attribute without arguments on a non-prototype"); | |
4ee9c684 | 8624 | *no_add_attrs = true; |
dbf6c367 | 8625 | } |
8626 | return NULL_TREE; | |
8627 | } | |
8628 | ||
8629 | /* Argument list specified. Verify that each argument number references | |
8630 | a pointer argument. */ | |
caf62483 | 8631 | for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args)) |
dbf6c367 | 8632 | { |
4ee9c684 | 8633 | unsigned HOST_WIDE_INT arg_num = 0, ck_num; |
dbf6c367 | 8634 | |
caf62483 | 8635 | tree arg = TREE_VALUE (args); |
8636 | if (arg && TREE_CODE (arg) != IDENTIFIER_NODE | |
8637 | && TREE_CODE (arg) != FUNCTION_DECL) | |
8638 | arg = default_conversion (arg); | |
8639 | ||
8640 | if (!get_nonnull_operand (arg, &arg_num)) | |
dbf6c367 | 8641 | { |
07e3a3d2 | 8642 | error ("nonnull argument has invalid operand number (argument %lu)", |
dbf6c367 | 8643 | (unsigned long) attr_arg_num); |
8644 | *no_add_attrs = true; | |
8645 | return NULL_TREE; | |
8646 | } | |
8647 | ||
d0af78c5 | 8648 | if (prototype_p (type)) |
dbf6c367 | 8649 | { |
d0af78c5 | 8650 | function_args_iterator iter; |
8651 | tree argument; | |
8652 | ||
8653 | function_args_iter_init (&iter, type); | |
8654 | for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter)) | |
dbf6c367 | 8655 | { |
d0af78c5 | 8656 | argument = function_args_iter_cond (&iter); |
8657 | if (argument == NULL_TREE || ck_num == arg_num) | |
dbf6c367 | 8658 | break; |
dbf6c367 | 8659 | } |
8660 | ||
84166705 | 8661 | if (!argument |
d0af78c5 | 8662 | || TREE_CODE (argument) == VOID_TYPE) |
dbf6c367 | 8663 | { |
07e3a3d2 | 8664 | error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)", |
dbf6c367 | 8665 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
8666 | *no_add_attrs = true; | |
8667 | return NULL_TREE; | |
8668 | } | |
8669 | ||
d0af78c5 | 8670 | if (TREE_CODE (argument) != POINTER_TYPE) |
dbf6c367 | 8671 | { |
07e3a3d2 | 8672 | error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)", |
dbf6c367 | 8673 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
8674 | *no_add_attrs = true; | |
8675 | return NULL_TREE; | |
8676 | } | |
8677 | } | |
8678 | } | |
8679 | ||
8680 | return NULL_TREE; | |
8681 | } | |
8682 | ||
8683 | /* Check the argument list of a function call for null in argument slots | |
d01f58f9 | 8684 | that are marked as requiring a non-null pointer argument. The NARGS |
8685 | arguments are passed in the array ARGARRAY. | |
8686 | */ | |
dbf6c367 | 8687 | |
8688 | static void | |
d01f58f9 | 8689 | check_function_nonnull (tree attrs, int nargs, tree *argarray) |
dbf6c367 | 8690 | { |
9ca77b08 | 8691 | tree a; |
d01f58f9 | 8692 | int i; |
dbf6c367 | 8693 | |
9ca77b08 | 8694 | attrs = lookup_attribute ("nonnull", attrs); |
8695 | if (attrs == NULL_TREE) | |
8696 | return; | |
8697 | ||
8698 | a = attrs; | |
8699 | /* See if any of the nonnull attributes has no arguments. If so, | |
8700 | then every pointer argument is checked (in which case the check | |
8701 | for pointer type is done in check_nonnull_arg). */ | |
8702 | if (TREE_VALUE (a) != NULL_TREE) | |
8703 | do | |
8704 | a = lookup_attribute ("nonnull", TREE_CHAIN (a)); | |
8705 | while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE); | |
8706 | ||
8707 | if (a != NULL_TREE) | |
8708 | for (i = 0; i < nargs; i++) | |
8709 | check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i], | |
8710 | i + 1); | |
8711 | else | |
dbf6c367 | 8712 | { |
9ca77b08 | 8713 | /* Walk the argument list. If we encounter an argument number we |
8714 | should check for non-null, do it. */ | |
8715 | for (i = 0; i < nargs; i++) | |
dbf6c367 | 8716 | { |
9ca77b08 | 8717 | for (a = attrs; ; a = TREE_CHAIN (a)) |
4ee9c684 | 8718 | { |
9ca77b08 | 8719 | a = lookup_attribute ("nonnull", a); |
8720 | if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1)) | |
8721 | break; | |
4ee9c684 | 8722 | } |
9ca77b08 | 8723 | |
8724 | if (a != NULL_TREE) | |
8725 | check_function_arguments_recurse (check_nonnull_arg, NULL, | |
8726 | argarray[i], i + 1); | |
dbf6c367 | 8727 | } |
8728 | } | |
8729 | } | |
8730 | ||
50ca527f | 8731 | /* Check that the Nth argument of a function call (counting backwards |
d01f58f9 | 8732 | from the end) is a (pointer)0. The NARGS arguments are passed in the |
8733 | array ARGARRAY. */ | |
bf6c8de0 | 8734 | |
8735 | static void | |
774e9d58 | 8736 | check_function_sentinel (const_tree fntype, int nargs, tree *argarray) |
bf6c8de0 | 8737 | { |
774e9d58 | 8738 | tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype)); |
bf6c8de0 | 8739 | |
8740 | if (attr) | |
8741 | { | |
d01f58f9 | 8742 | int len = 0; |
8743 | int pos = 0; | |
8744 | tree sentinel; | |
774e9d58 | 8745 | function_args_iterator iter; |
8746 | tree t; | |
a0c938f0 | 8747 | |
d01f58f9 | 8748 | /* Skip over the named arguments. */ |
774e9d58 | 8749 | FOREACH_FUNCTION_ARGS (fntype, t, iter) |
a0c938f0 | 8750 | { |
774e9d58 | 8751 | if (len == nargs) |
8752 | break; | |
d01f58f9 | 8753 | len++; |
8754 | } | |
50ca527f | 8755 | |
d01f58f9 | 8756 | if (TREE_VALUE (attr)) |
8757 | { | |
8758 | tree p = TREE_VALUE (TREE_VALUE (attr)); | |
8759 | pos = TREE_INT_CST_LOW (p); | |
8760 | } | |
50ca527f | 8761 | |
d01f58f9 | 8762 | /* The sentinel must be one of the varargs, i.e. |
8763 | in position >= the number of fixed arguments. */ | |
8764 | if ((nargs - 1 - pos) < len) | |
8765 | { | |
77a357e3 | 8766 | warning (OPT_Wformat_, |
d01f58f9 | 8767 | "not enough variable arguments to fit a sentinel"); |
8768 | return; | |
bf6c8de0 | 8769 | } |
d01f58f9 | 8770 | |
8771 | /* Validate the sentinel. */ | |
8772 | sentinel = argarray[nargs - 1 - pos]; | |
8773 | if ((!POINTER_TYPE_P (TREE_TYPE (sentinel)) | |
8774 | || !integer_zerop (sentinel)) | |
8775 | /* Although __null (in C++) is only an integer we allow it | |
8776 | nevertheless, as we are guaranteed that it's exactly | |
8777 | as wide as a pointer, and we don't want to force | |
8778 | users to cast the NULL they have written there. | |
8779 | We warn with -Wstrict-null-sentinel, though. */ | |
8780 | && (warn_strict_null_sentinel || null_node != sentinel)) | |
77a357e3 | 8781 | warning (OPT_Wformat_, "missing sentinel in function call"); |
bf6c8de0 | 8782 | } |
8783 | } | |
8784 | ||
dbf6c367 | 8785 | /* Helper for check_function_nonnull; given a list of operands which |
8786 | must be non-null in ARGS, determine if operand PARAM_NUM should be | |
8787 | checked. */ | |
8788 | ||
8789 | static bool | |
1cae46be | 8790 | nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 8791 | { |
4ee9c684 | 8792 | unsigned HOST_WIDE_INT arg_num = 0; |
dbf6c367 | 8793 | |
8794 | for (; args; args = TREE_CHAIN (args)) | |
8795 | { | |
231bd014 | 8796 | bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num); |
8797 | ||
8798 | gcc_assert (found); | |
dbf6c367 | 8799 | |
8800 | if (arg_num == param_num) | |
8801 | return true; | |
8802 | } | |
8803 | return false; | |
8804 | } | |
8805 | ||
8806 | /* Check that the function argument PARAM (which is operand number | |
8807 | PARAM_NUM) is non-null. This is called by check_function_nonnull | |
8808 | via check_function_arguments_recurse. */ | |
8809 | ||
8810 | static void | |
9a03a746 | 8811 | check_nonnull_arg (void * ARG_UNUSED (ctx), tree param, |
1cae46be | 8812 | unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 8813 | { |
8814 | /* Just skip checking the argument if it's not a pointer. This can | |
8815 | happen if the "nonnull" attribute was given without an operand | |
8816 | list (which means to check every pointer argument). */ | |
8817 | ||
8818 | if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) | |
8819 | return; | |
8820 | ||
8821 | if (integer_zerop (param)) | |
155b601b | 8822 | warning (OPT_Wnonnull, "null argument where non-null required " |
8823 | "(argument %lu)", (unsigned long) param_num); | |
dbf6c367 | 8824 | } |
8825 | ||
8826 | /* Helper for nonnull attribute handling; fetch the operand number | |
8827 | from the attribute argument list. */ | |
8828 | ||
8829 | static bool | |
1cae46be | 8830 | get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) |
dbf6c367 | 8831 | { |
67409385 | 8832 | /* Verify the arg number is a constant. */ |
dbf6c367 | 8833 | if (TREE_CODE (arg_num_expr) != INTEGER_CST |
8834 | || TREE_INT_CST_HIGH (arg_num_expr) != 0) | |
8835 | return false; | |
8836 | ||
8837 | *valp = TREE_INT_CST_LOW (arg_num_expr); | |
8838 | return true; | |
8839 | } | |
fa987697 | 8840 | |
8841 | /* Handle a "nothrow" attribute; arguments as in | |
8842 | struct attribute_spec.handler. */ | |
8843 | ||
8844 | static tree | |
9a03a746 | 8845 | handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8846 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
fa987697 | 8847 | { |
8848 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
8849 | TREE_NOTHROW (*node) = 1; | |
8850 | /* ??? TODO: Support types. */ | |
8851 | else | |
8852 | { | |
9b2d6d13 | 8853 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
fa987697 | 8854 | *no_add_attrs = true; |
8855 | } | |
8856 | ||
8857 | return NULL_TREE; | |
8858 | } | |
7acb29a3 | 8859 | |
8860 | /* Handle a "cleanup" attribute; arguments as in | |
8861 | struct attribute_spec.handler. */ | |
8862 | ||
8863 | static tree | |
1cae46be | 8864 | handle_cleanup_attribute (tree *node, tree name, tree args, |
9a03a746 | 8865 | int ARG_UNUSED (flags), bool *no_add_attrs) |
7acb29a3 | 8866 | { |
8867 | tree decl = *node; | |
8868 | tree cleanup_id, cleanup_decl; | |
8869 | ||
8870 | /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do | |
8871 | for global destructors in C++. This requires infrastructure that | |
8872 | we don't have generically at the moment. It's also not a feature | |
8873 | we'd be missing too much, since we do have attribute constructor. */ | |
8874 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
8875 | { | |
9b2d6d13 | 8876 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
7acb29a3 | 8877 | *no_add_attrs = true; |
8878 | return NULL_TREE; | |
8879 | } | |
8880 | ||
8881 | /* Verify that the argument is a function in scope. */ | |
8882 | /* ??? We could support pointers to functions here as well, if | |
8883 | that was considered desirable. */ | |
8884 | cleanup_id = TREE_VALUE (args); | |
8885 | if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE) | |
8886 | { | |
07e3a3d2 | 8887 | error ("cleanup argument not an identifier"); |
7acb29a3 | 8888 | *no_add_attrs = true; |
8889 | return NULL_TREE; | |
8890 | } | |
d1c41717 | 8891 | cleanup_decl = lookup_name (cleanup_id); |
7acb29a3 | 8892 | if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL) |
8893 | { | |
07e3a3d2 | 8894 | error ("cleanup argument not a function"); |
7acb29a3 | 8895 | *no_add_attrs = true; |
8896 | return NULL_TREE; | |
8897 | } | |
8898 | ||
1cae46be | 8899 | /* That the function has proper type is checked with the |
7acb29a3 | 8900 | eventual call to build_function_call. */ |
8901 | ||
8902 | return NULL_TREE; | |
8903 | } | |
8a8cdb8d | 8904 | |
8905 | /* Handle a "warn_unused_result" attribute. No special handling. */ | |
8906 | ||
8907 | static tree | |
8908 | handle_warn_unused_result_attribute (tree *node, tree name, | |
9a03a746 | 8909 | tree ARG_UNUSED (args), |
8910 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8a8cdb8d | 8911 | { |
8912 | /* Ignore the attribute for functions not returning any value. */ | |
8913 | if (VOID_TYPE_P (TREE_TYPE (*node))) | |
8914 | { | |
9b2d6d13 | 8915 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
8a8cdb8d | 8916 | *no_add_attrs = true; |
8917 | } | |
8918 | ||
8919 | return NULL_TREE; | |
8920 | } | |
bf6c8de0 | 8921 | |
8922 | /* Handle a "sentinel" attribute. */ | |
8923 | ||
8924 | static tree | |
50ca527f | 8925 | handle_sentinel_attribute (tree *node, tree name, tree args, |
bf6c8de0 | 8926 | int ARG_UNUSED (flags), bool *no_add_attrs) |
8927 | { | |
a36cf284 | 8928 | if (!prototype_p (*node)) |
bf6c8de0 | 8929 | { |
9b2d6d13 | 8930 | warning (OPT_Wattributes, |
8931 | "%qE attribute requires prototypes with named arguments", name); | |
bf6c8de0 | 8932 | *no_add_attrs = true; |
bf6c8de0 | 8933 | } |
50ca527f | 8934 | else |
8935 | { | |
c33080b9 | 8936 | if (!stdarg_p (*node)) |
a0c938f0 | 8937 | { |
9b2d6d13 | 8938 | warning (OPT_Wattributes, |
8939 | "%qE attribute only applies to variadic functions", name); | |
50ca527f | 8940 | *no_add_attrs = true; |
8941 | } | |
8942 | } | |
a0c938f0 | 8943 | |
50ca527f | 8944 | if (args) |
bf6c8de0 | 8945 | { |
50ca527f | 8946 | tree position = TREE_VALUE (args); |
8947 | ||
50ca527f | 8948 | if (TREE_CODE (position) != INTEGER_CST) |
a0c938f0 | 8949 | { |
48e1416a | 8950 | warning (OPT_Wattributes, |
01b54db5 | 8951 | "requested position is not an integer constant"); |
50ca527f | 8952 | *no_add_attrs = true; |
8953 | } | |
8954 | else | |
a0c938f0 | 8955 | { |
50ca527f | 8956 | if (tree_int_cst_lt (position, integer_zero_node)) |
8957 | { | |
01b54db5 | 8958 | warning (OPT_Wattributes, |
8959 | "requested position is less than zero"); | |
50ca527f | 8960 | *no_add_attrs = true; |
8961 | } | |
8962 | } | |
bf6c8de0 | 8963 | } |
a0c938f0 | 8964 | |
bf6c8de0 | 8965 | return NULL_TREE; |
8966 | } | |
b5c26b42 | 8967 | |
8968 | /* Handle a "type_generic" attribute. */ | |
8969 | ||
8970 | static tree | |
8971 | handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name), | |
8972 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8973 | bool * ARG_UNUSED (no_add_attrs)) | |
8974 | { | |
19fbe3a4 | 8975 | /* Ensure we have a function type. */ |
8976 | gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE); | |
48e1416a | 8977 | |
19fbe3a4 | 8978 | /* Ensure we have a variadic function. */ |
c33080b9 | 8979 | gcc_assert (!prototype_p (*node) || stdarg_p (*node)); |
b5c26b42 | 8980 | |
8981 | return NULL_TREE; | |
8982 | } | |
46f8e3b0 | 8983 | |
24470055 | 8984 | /* Handle a "target" attribute. */ |
46f8e3b0 | 8985 | |
8986 | static tree | |
24470055 | 8987 | handle_target_attribute (tree *node, tree name, tree args, int flags, |
46f8e3b0 | 8988 | bool *no_add_attrs) |
8989 | { | |
8990 | /* Ensure we have a function type. */ | |
8991 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
8992 | { | |
8993 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8994 | *no_add_attrs = true; | |
8995 | } | |
46f8e3b0 | 8996 | else if (! targetm.target_option.valid_attribute_p (*node, name, args, |
ae0c3984 | 8997 | flags)) |
46f8e3b0 | 8998 | *no_add_attrs = true; |
8999 | ||
9000 | return NULL_TREE; | |
9001 | } | |
9002 | ||
9003 | /* Arguments being collected for optimization. */ | |
9004 | typedef const char *const_char_p; /* For DEF_VEC_P. */ | |
f1f41a6c | 9005 | static GTY(()) vec<const_char_p, va_gc> *optimize_args; |
46f8e3b0 | 9006 | |
9007 | ||
9008 | /* Inner function to convert a TREE_LIST to argv string to parse the optimize | |
9009 | options in ARGS. ATTR_P is true if this is for attribute(optimize), and | |
9010 | false for #pragma GCC optimize. */ | |
9011 | ||
9012 | bool | |
9013 | parse_optimize_options (tree args, bool attr_p) | |
9014 | { | |
9015 | bool ret = true; | |
9016 | unsigned opt_argc; | |
9017 | unsigned i; | |
2becf397 | 9018 | int saved_flag_strict_aliasing; |
46f8e3b0 | 9019 | const char **opt_argv; |
615ef0bb | 9020 | struct cl_decoded_option *decoded_options; |
9021 | unsigned int decoded_options_count; | |
46f8e3b0 | 9022 | tree ap; |
9023 | ||
9024 | /* Build up argv vector. Just in case the string is stored away, use garbage | |
9025 | collected strings. */ | |
f1f41a6c | 9026 | vec_safe_truncate (optimize_args, 0); |
9027 | vec_safe_push (optimize_args, (const char *) NULL); | |
46f8e3b0 | 9028 | |
9029 | for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap)) | |
9030 | { | |
9031 | tree value = TREE_VALUE (ap); | |
9032 | ||
9033 | if (TREE_CODE (value) == INTEGER_CST) | |
9034 | { | |
9035 | char buffer[20]; | |
9036 | sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value)); | |
f1f41a6c | 9037 | vec_safe_push (optimize_args, ggc_strdup (buffer)); |
46f8e3b0 | 9038 | } |
9039 | ||
9040 | else if (TREE_CODE (value) == STRING_CST) | |
9041 | { | |
9042 | /* Split string into multiple substrings. */ | |
9043 | size_t len = TREE_STRING_LENGTH (value); | |
9044 | char *p = ASTRDUP (TREE_STRING_POINTER (value)); | |
9045 | char *end = p + len; | |
9046 | char *comma; | |
9047 | char *next_p = p; | |
9048 | ||
9049 | while (next_p != NULL) | |
9050 | { | |
9051 | size_t len2; | |
9052 | char *q, *r; | |
9053 | ||
9054 | p = next_p; | |
9055 | comma = strchr (p, ','); | |
9056 | if (comma) | |
9057 | { | |
9058 | len2 = comma - p; | |
9059 | *comma = '\0'; | |
9060 | next_p = comma+1; | |
9061 | } | |
9062 | else | |
9063 | { | |
9064 | len2 = end - p; | |
9065 | next_p = NULL; | |
9066 | } | |
9067 | ||
ba72912a | 9068 | r = q = (char *) ggc_alloc_atomic (len2 + 3); |
46f8e3b0 | 9069 | |
9070 | /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx | |
9071 | options. */ | |
9072 | if (*p == '-' && p[1] != 'O' && p[1] != 'f') | |
9073 | { | |
9074 | ret = false; | |
9075 | if (attr_p) | |
9076 | warning (OPT_Wattributes, | |
ebd7c4c1 | 9077 | "bad option %s to optimize attribute", p); |
46f8e3b0 | 9078 | else |
9079 | warning (OPT_Wpragmas, | |
e44b0a1f | 9080 | "bad option %s to pragma attribute", p); |
46f8e3b0 | 9081 | continue; |
9082 | } | |
9083 | ||
9084 | if (*p != '-') | |
9085 | { | |
9086 | *r++ = '-'; | |
9087 | ||
9088 | /* Assume that Ox is -Ox, a numeric value is -Ox, a s by | |
9089 | itself is -Os, and any other switch begins with a -f. */ | |
9090 | if ((*p >= '0' && *p <= '9') | |
9091 | || (p[0] == 's' && p[1] == '\0')) | |
9092 | *r++ = 'O'; | |
9093 | else if (*p != 'O') | |
9094 | *r++ = 'f'; | |
9095 | } | |
9096 | ||
9097 | memcpy (r, p, len2); | |
9098 | r[len2] = '\0'; | |
f1f41a6c | 9099 | vec_safe_push (optimize_args, (const char *) q); |
46f8e3b0 | 9100 | } |
9101 | ||
9102 | } | |
9103 | } | |
9104 | ||
f1f41a6c | 9105 | opt_argc = optimize_args->length (); |
46f8e3b0 | 9106 | opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1)); |
9107 | ||
9108 | for (i = 1; i < opt_argc; i++) | |
f1f41a6c | 9109 | opt_argv[i] = (*optimize_args)[i]; |
46f8e3b0 | 9110 | |
2becf397 | 9111 | saved_flag_strict_aliasing = flag_strict_aliasing; |
9112 | ||
46f8e3b0 | 9113 | /* Now parse the options. */ |
f3f006ad | 9114 | decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv, |
9115 | &decoded_options, | |
9116 | &decoded_options_count); | |
9117 | decode_options (&global_options, &global_options_set, | |
3c6c0e40 | 9118 | decoded_options, decoded_options_count, |
9119 | input_location, global_dc); | |
46f8e3b0 | 9120 | |
4bec06b3 | 9121 | targetm.override_options_after_change(); |
9122 | ||
2becf397 | 9123 | /* Don't allow changing -fstrict-aliasing. */ |
9124 | flag_strict_aliasing = saved_flag_strict_aliasing; | |
9125 | ||
f1f41a6c | 9126 | optimize_args->truncate (0); |
46f8e3b0 | 9127 | return ret; |
9128 | } | |
9129 | ||
9130 | /* For handling "optimize" attribute. arguments as in | |
9131 | struct attribute_spec.handler. */ | |
9132 | ||
9133 | static tree | |
9134 | handle_optimize_attribute (tree *node, tree name, tree args, | |
9135 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
9136 | { | |
9137 | /* Ensure we have a function type. */ | |
9138 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
9139 | { | |
9140 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
9141 | *no_add_attrs = true; | |
9142 | } | |
9143 | else | |
9144 | { | |
9145 | struct cl_optimization cur_opts; | |
9146 | tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node); | |
9147 | ||
9148 | /* Save current options. */ | |
2c5d2e39 | 9149 | cl_optimization_save (&cur_opts, &global_options); |
46f8e3b0 | 9150 | |
9151 | /* If we previously had some optimization options, use them as the | |
9152 | default. */ | |
9153 | if (old_opts) | |
2c5d2e39 | 9154 | cl_optimization_restore (&global_options, |
9155 | TREE_OPTIMIZATION (old_opts)); | |
46f8e3b0 | 9156 | |
9157 | /* Parse options, and update the vector. */ | |
9158 | parse_optimize_options (args, true); | |
9159 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) | |
55310327 | 9160 | = build_optimization_node (&global_options); |
46f8e3b0 | 9161 | |
9162 | /* Restore current options. */ | |
2c5d2e39 | 9163 | cl_optimization_restore (&global_options, &cur_opts); |
46f8e3b0 | 9164 | } |
9165 | ||
9166 | return NULL_TREE; | |
9167 | } | |
48b14f50 | 9168 | |
9169 | /* Handle a "no_split_stack" attribute. */ | |
9170 | ||
9171 | static tree | |
9172 | handle_no_split_stack_attribute (tree *node, tree name, | |
9173 | tree ARG_UNUSED (args), | |
9174 | int ARG_UNUSED (flags), | |
9175 | bool *no_add_attrs) | |
9176 | { | |
9177 | tree decl = *node; | |
9178 | ||
9179 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
9180 | { | |
9181 | error_at (DECL_SOURCE_LOCATION (decl), | |
9182 | "%qE attribute applies only to functions", name); | |
9183 | *no_add_attrs = true; | |
9184 | } | |
9185 | else if (DECL_INITIAL (decl)) | |
9186 | { | |
9187 | error_at (DECL_SOURCE_LOCATION (decl), | |
9188 | "can%'t set %qE attribute after definition", name); | |
9189 | *no_add_attrs = true; | |
9190 | } | |
9191 | ||
9192 | return NULL_TREE; | |
9193 | } | |
d7dcba40 | 9194 | |
9195 | /* Handle a "returns_nonnull" attribute; arguments as in | |
9196 | struct attribute_spec.handler. */ | |
9197 | ||
9198 | static tree | |
9199 | handle_returns_nonnull_attribute (tree *node, tree, tree, int, | |
9200 | bool *no_add_attrs) | |
9201 | { | |
9202 | // Even without a prototype we still have a return type we can check. | |
9203 | if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE) | |
9204 | { | |
9205 | error ("returns_nonnull attribute on a function not returning a pointer"); | |
9206 | *no_add_attrs = true; | |
9207 | } | |
9208 | return NULL_TREE; | |
9209 | } | |
9210 | ||
dbf6c367 | 9211 | \f |
774e9d58 | 9212 | /* Check for valid arguments being passed to a function with FNTYPE. |
9213 | There are NARGS arguments in the array ARGARRAY. */ | |
dbf6c367 | 9214 | void |
774e9d58 | 9215 | check_function_arguments (const_tree fntype, int nargs, tree *argarray) |
dbf6c367 | 9216 | { |
9217 | /* Check for null being passed in a pointer argument that must be | |
9218 | non-null. We also need to do this if format checking is enabled. */ | |
9219 | ||
9220 | if (warn_nonnull) | |
774e9d58 | 9221 | check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
dbf6c367 | 9222 | |
9223 | /* Check for errors in format strings. */ | |
9224 | ||
068bea1e | 9225 | if (warn_format || warn_suggest_attribute_format) |
774e9d58 | 9226 | check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
95c90e04 | 9227 | |
9228 | if (warn_format) | |
774e9d58 | 9229 | check_function_sentinel (fntype, nargs, argarray); |
dbf6c367 | 9230 | } |
9231 | ||
9232 | /* Generic argument checking recursion routine. PARAM is the argument to | |
9233 | be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked | |
9234 | once the argument is resolved. CTX is context for the callback. */ | |
9235 | void | |
1cae46be | 9236 | check_function_arguments_recurse (void (*callback) |
9237 | (void *, tree, unsigned HOST_WIDE_INT), | |
9238 | void *ctx, tree param, | |
9239 | unsigned HOST_WIDE_INT param_num) | |
dbf6c367 | 9240 | { |
72dd6141 | 9241 | if (CONVERT_EXPR_P (param) |
c44afe23 | 9242 | && (TYPE_PRECISION (TREE_TYPE (param)) |
9243 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0))))) | |
dbf6c367 | 9244 | { |
9245 | /* Strip coercion. */ | |
9246 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 9247 | TREE_OPERAND (param, 0), param_num); |
dbf6c367 | 9248 | return; |
9249 | } | |
9250 | ||
9251 | if (TREE_CODE (param) == CALL_EXPR) | |
9252 | { | |
c2f47e15 | 9253 | tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param))); |
dbf6c367 | 9254 | tree attrs; |
9255 | bool found_format_arg = false; | |
9256 | ||
9257 | /* See if this is a call to a known internationalization function | |
9258 | that modifies a format arg. Such a function may have multiple | |
9259 | format_arg attributes (for example, ngettext). */ | |
9260 | ||
9261 | for (attrs = TYPE_ATTRIBUTES (type); | |
9262 | attrs; | |
9263 | attrs = TREE_CHAIN (attrs)) | |
9264 | if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs))) | |
9265 | { | |
c2f47e15 | 9266 | tree inner_arg; |
dbf6c367 | 9267 | tree format_num_expr; |
9268 | int format_num; | |
9269 | int i; | |
c2f47e15 | 9270 | call_expr_arg_iterator iter; |
dbf6c367 | 9271 | |
9272 | /* Extract the argument number, which was previously checked | |
9273 | to be valid. */ | |
9274 | format_num_expr = TREE_VALUE (TREE_VALUE (attrs)); | |
dbf6c367 | 9275 | |
5200ef07 | 9276 | format_num = tree_to_uhwi (format_num_expr); |
dbf6c367 | 9277 | |
c2f47e15 | 9278 | for (inner_arg = first_call_expr_arg (param, &iter), i = 1; |
9279 | inner_arg != 0; | |
9280 | inner_arg = next_call_expr_arg (&iter), i++) | |
dbf6c367 | 9281 | if (i == format_num) |
9282 | { | |
9283 | check_function_arguments_recurse (callback, ctx, | |
c2f47e15 | 9284 | inner_arg, param_num); |
dbf6c367 | 9285 | found_format_arg = true; |
9286 | break; | |
9287 | } | |
9288 | } | |
9289 | ||
9290 | /* If we found a format_arg attribute and did a recursive check, | |
9291 | we are done with checking this argument. Otherwise, we continue | |
9292 | and this will be considered a non-literal. */ | |
9293 | if (found_format_arg) | |
9294 | return; | |
9295 | } | |
9296 | ||
9297 | if (TREE_CODE (param) == COND_EXPR) | |
9298 | { | |
9299 | /* Check both halves of the conditional expression. */ | |
9300 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 9301 | TREE_OPERAND (param, 1), param_num); |
dbf6c367 | 9302 | check_function_arguments_recurse (callback, ctx, |
4ee9c684 | 9303 | TREE_OPERAND (param, 2), param_num); |
dbf6c367 | 9304 | return; |
9305 | } | |
9306 | ||
9307 | (*callback) (ctx, param, param_num); | |
9308 | } | |
1f3233d1 | 9309 | |
60cce472 | 9310 | /* Checks for a builtin function FNDECL that the number of arguments |
9311 | NARGS against the required number REQUIRED and issues an error if | |
9312 | there is a mismatch. Returns true if the number of arguments is | |
9313 | correct, otherwise false. */ | |
d43cee80 | 9314 | |
9315 | static bool | |
60cce472 | 9316 | builtin_function_validate_nargs (tree fndecl, int nargs, int required) |
d43cee80 | 9317 | { |
9318 | if (nargs < required) | |
9319 | { | |
60cce472 | 9320 | error_at (input_location, |
9321 | "not enough arguments to function %qE", fndecl); | |
d43cee80 | 9322 | return false; |
9323 | } | |
9324 | else if (nargs > required) | |
9325 | { | |
60cce472 | 9326 | error_at (input_location, |
9327 | "too many arguments to function %qE", fndecl); | |
d43cee80 | 9328 | return false; |
9329 | } | |
9330 | return true; | |
9331 | } | |
9332 | ||
9333 | /* Verifies the NARGS arguments ARGS to the builtin function FNDECL. | |
9334 | Returns false if there was an error, otherwise true. */ | |
9335 | ||
9336 | bool | |
9337 | check_builtin_function_arguments (tree fndecl, int nargs, tree *args) | |
9338 | { | |
9339 | if (!DECL_BUILT_IN (fndecl) | |
9340 | || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) | |
9341 | return true; | |
9342 | ||
9343 | switch (DECL_FUNCTION_CODE (fndecl)) | |
9344 | { | |
9345 | case BUILT_IN_CONSTANT_P: | |
60cce472 | 9346 | return builtin_function_validate_nargs (fndecl, nargs, 1); |
d43cee80 | 9347 | |
9348 | case BUILT_IN_ISFINITE: | |
9349 | case BUILT_IN_ISINF: | |
c319d56a | 9350 | case BUILT_IN_ISINF_SIGN: |
d43cee80 | 9351 | case BUILT_IN_ISNAN: |
9352 | case BUILT_IN_ISNORMAL: | |
60cce472 | 9353 | if (builtin_function_validate_nargs (fndecl, nargs, 1)) |
d43cee80 | 9354 | { |
9355 | if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE) | |
9356 | { | |
9357 | error ("non-floating-point argument in call to " | |
9358 | "function %qE", fndecl); | |
9359 | return false; | |
9360 | } | |
9361 | return true; | |
9362 | } | |
9363 | return false; | |
9364 | ||
9365 | case BUILT_IN_ISGREATER: | |
9366 | case BUILT_IN_ISGREATEREQUAL: | |
9367 | case BUILT_IN_ISLESS: | |
9368 | case BUILT_IN_ISLESSEQUAL: | |
9369 | case BUILT_IN_ISLESSGREATER: | |
9370 | case BUILT_IN_ISUNORDERED: | |
60cce472 | 9371 | if (builtin_function_validate_nargs (fndecl, nargs, 2)) |
d43cee80 | 9372 | { |
9373 | enum tree_code code0, code1; | |
9374 | code0 = TREE_CODE (TREE_TYPE (args[0])); | |
9375 | code1 = TREE_CODE (TREE_TYPE (args[1])); | |
9376 | if (!((code0 == REAL_TYPE && code1 == REAL_TYPE) | |
9377 | || (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
9378 | || (code0 == INTEGER_TYPE && code1 == REAL_TYPE))) | |
9379 | { | |
9380 | error ("non-floating-point arguments in call to " | |
9381 | "function %qE", fndecl); | |
9382 | return false; | |
9383 | } | |
9384 | return true; | |
9385 | } | |
9386 | return false; | |
9387 | ||
19fbe3a4 | 9388 | case BUILT_IN_FPCLASSIFY: |
60cce472 | 9389 | if (builtin_function_validate_nargs (fndecl, nargs, 6)) |
19fbe3a4 | 9390 | { |
9391 | unsigned i; | |
48e1416a | 9392 | |
19fbe3a4 | 9393 | for (i=0; i<5; i++) |
9394 | if (TREE_CODE (args[i]) != INTEGER_CST) | |
9395 | { | |
9396 | error ("non-const integer argument %u in call to function %qE", | |
9397 | i+1, fndecl); | |
9398 | return false; | |
9399 | } | |
9400 | ||
9401 | if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE) | |
9402 | { | |
9403 | error ("non-floating-point argument in call to function %qE", | |
9404 | fndecl); | |
9405 | return false; | |
9406 | } | |
9407 | return true; | |
9408 | } | |
9409 | return false; | |
9410 | ||
fca0886c | 9411 | case BUILT_IN_ASSUME_ALIGNED: |
9412 | if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2))) | |
9413 | { | |
9414 | if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE) | |
9415 | { | |
9416 | error ("non-integer argument 3 in call to function %qE", fndecl); | |
9417 | return false; | |
9418 | } | |
9419 | return true; | |
9420 | } | |
9421 | return false; | |
9422 | ||
d43cee80 | 9423 | default: |
9424 | return true; | |
9425 | } | |
9426 | } | |
9427 | ||
860251be | 9428 | /* Function to help qsort sort FIELD_DECLs by name order. */ |
9429 | ||
9430 | int | |
9431 | field_decl_cmp (const void *x_p, const void *y_p) | |
9432 | { | |
4fd61bc6 | 9433 | const tree *const x = (const tree *const) x_p; |
9434 | const tree *const y = (const tree *const) y_p; | |
9435 | ||
860251be | 9436 | if (DECL_NAME (*x) == DECL_NAME (*y)) |
9437 | /* A nontype is "greater" than a type. */ | |
9438 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
9439 | if (DECL_NAME (*x) == NULL_TREE) | |
9440 | return -1; | |
9441 | if (DECL_NAME (*y) == NULL_TREE) | |
9442 | return 1; | |
9443 | if (DECL_NAME (*x) < DECL_NAME (*y)) | |
9444 | return -1; | |
9445 | return 1; | |
9446 | } | |
9447 | ||
9448 | static struct { | |
9449 | gt_pointer_operator new_value; | |
9450 | void *cookie; | |
9451 | } resort_data; | |
9452 | ||
9453 | /* This routine compares two fields like field_decl_cmp but using the | |
9454 | pointer operator in resort_data. */ | |
9455 | ||
9456 | static int | |
9457 | resort_field_decl_cmp (const void *x_p, const void *y_p) | |
9458 | { | |
4fd61bc6 | 9459 | const tree *const x = (const tree *const) x_p; |
9460 | const tree *const y = (const tree *const) y_p; | |
860251be | 9461 | |
9462 | if (DECL_NAME (*x) == DECL_NAME (*y)) | |
9463 | /* A nontype is "greater" than a type. */ | |
9464 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
9465 | if (DECL_NAME (*x) == NULL_TREE) | |
9466 | return -1; | |
9467 | if (DECL_NAME (*y) == NULL_TREE) | |
9468 | return 1; | |
9469 | { | |
9470 | tree d1 = DECL_NAME (*x); | |
9471 | tree d2 = DECL_NAME (*y); | |
9472 | resort_data.new_value (&d1, resort_data.cookie); | |
9473 | resort_data.new_value (&d2, resort_data.cookie); | |
9474 | if (d1 < d2) | |
9475 | return -1; | |
9476 | } | |
9477 | return 1; | |
9478 | } | |
9479 | ||
9480 | /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ | |
9481 | ||
9482 | void | |
9483 | resort_sorted_fields (void *obj, | |
9a03a746 | 9484 | void * ARG_UNUSED (orig_obj), |
4ee9c684 | 9485 | gt_pointer_operator new_value, |
9486 | void *cookie) | |
860251be | 9487 | { |
9a03a746 | 9488 | struct sorted_fields_type *sf = (struct sorted_fields_type *) obj; |
860251be | 9489 | resort_data.new_value = new_value; |
9490 | resort_data.cookie = cookie; | |
9491 | qsort (&sf->elts[0], sf->len, sizeof (tree), | |
4ee9c684 | 9492 | resort_field_decl_cmp); |
860251be | 9493 | } |
9494 | ||
209c9752 | 9495 | /* Subroutine of c_parse_error. |
9496 | Return the result of concatenating LHS and RHS. RHS is really | |
9497 | a string literal, its first character is indicated by RHS_START and | |
cfee01e3 | 9498 | RHS_SIZE is its length (including the terminating NUL character). |
209c9752 | 9499 | |
9500 | The caller is responsible for deleting the returned pointer. */ | |
9501 | ||
9502 | static char * | |
9503 | catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) | |
9504 | { | |
9505 | const int lhs_size = strlen (lhs); | |
9506 | char *result = XNEWVEC (char, lhs_size + rhs_size); | |
9507 | strncpy (result, lhs, lhs_size); | |
9508 | strncpy (result + lhs_size, rhs_start, rhs_size); | |
9509 | return result; | |
9510 | } | |
9511 | ||
380c6697 | 9512 | /* Issue the error given by GMSGID, indicating that it occurred before |
92b128ed | 9513 | TOKEN, which had the associated VALUE. */ |
9514 | ||
9515 | void | |
48e1416a | 9516 | c_parse_error (const char *gmsgid, enum cpp_ttype token_type, |
ba99525e | 9517 | tree value, unsigned char token_flags) |
92b128ed | 9518 | { |
209c9752 | 9519 | #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2)) |
9520 | ||
9521 | char *message = NULL; | |
92b128ed | 9522 | |
ba99525e | 9523 | if (token_type == CPP_EOF) |
380c6697 | 9524 | message = catenate_messages (gmsgid, " at end of input"); |
48e1416a | 9525 | else if (token_type == CPP_CHAR |
9526 | || token_type == CPP_WCHAR | |
ba99525e | 9527 | || token_type == CPP_CHAR16 |
9528 | || token_type == CPP_CHAR32) | |
92b128ed | 9529 | { |
9530 | unsigned int val = TREE_INT_CST_LOW (value); | |
924bbf02 | 9531 | const char *prefix; |
9532 | ||
ba99525e | 9533 | switch (token_type) |
924bbf02 | 9534 | { |
9535 | default: | |
9536 | prefix = ""; | |
9537 | break; | |
9538 | case CPP_WCHAR: | |
9539 | prefix = "L"; | |
9540 | break; | |
9541 | case CPP_CHAR16: | |
9542 | prefix = "u"; | |
9543 | break; | |
9544 | case CPP_CHAR32: | |
9545 | prefix = "U"; | |
9546 | break; | |
9547 | } | |
9548 | ||
92b128ed | 9549 | if (val <= UCHAR_MAX && ISGRAPH (val)) |
a0c938f0 | 9550 | message = catenate_messages (gmsgid, " before %s'%c'"); |
92b128ed | 9551 | else |
a0c938f0 | 9552 | message = catenate_messages (gmsgid, " before %s'\\x%x'"); |
209c9752 | 9553 | |
924bbf02 | 9554 | error (message, prefix, val); |
209c9752 | 9555 | free (message); |
9556 | message = NULL; | |
92b128ed | 9557 | } |
1898176c | 9558 | else if (token_type == CPP_CHAR_USERDEF |
9559 | || token_type == CPP_WCHAR_USERDEF | |
9560 | || token_type == CPP_CHAR16_USERDEF | |
9561 | || token_type == CPP_CHAR32_USERDEF) | |
9562 | message = catenate_messages (gmsgid, | |
9563 | " before user-defined character literal"); | |
9564 | else if (token_type == CPP_STRING_USERDEF | |
9565 | || token_type == CPP_WSTRING_USERDEF | |
9566 | || token_type == CPP_STRING16_USERDEF | |
9567 | || token_type == CPP_STRING32_USERDEF | |
9568 | || token_type == CPP_UTF8STRING_USERDEF) | |
9569 | message = catenate_messages (gmsgid, " before user-defined string literal"); | |
48e1416a | 9570 | else if (token_type == CPP_STRING |
9571 | || token_type == CPP_WSTRING | |
ba99525e | 9572 | || token_type == CPP_STRING16 |
538ba11a | 9573 | || token_type == CPP_STRING32 |
9574 | || token_type == CPP_UTF8STRING) | |
380c6697 | 9575 | message = catenate_messages (gmsgid, " before string constant"); |
ba99525e | 9576 | else if (token_type == CPP_NUMBER) |
380c6697 | 9577 | message = catenate_messages (gmsgid, " before numeric constant"); |
ba99525e | 9578 | else if (token_type == CPP_NAME) |
209c9752 | 9579 | { |
380c6697 | 9580 | message = catenate_messages (gmsgid, " before %qE"); |
782858b8 | 9581 | error (message, value); |
209c9752 | 9582 | free (message); |
9583 | message = NULL; | |
9584 | } | |
ba99525e | 9585 | else if (token_type == CPP_PRAGMA) |
b75b98aa | 9586 | message = catenate_messages (gmsgid, " before %<#pragma%>"); |
ba99525e | 9587 | else if (token_type == CPP_PRAGMA_EOL) |
b75b98aa | 9588 | message = catenate_messages (gmsgid, " before end of line"); |
07b8f133 | 9589 | else if (token_type == CPP_DECLTYPE) |
9590 | message = catenate_messages (gmsgid, " before %<decltype%>"); | |
ba99525e | 9591 | else if (token_type < N_TTYPES) |
209c9752 | 9592 | { |
380c6697 | 9593 | message = catenate_messages (gmsgid, " before %qs token"); |
ba99525e | 9594 | error (message, cpp_type2name (token_type, token_flags)); |
209c9752 | 9595 | free (message); |
9596 | message = NULL; | |
9597 | } | |
92b128ed | 9598 | else |
380c6697 | 9599 | error (gmsgid); |
209c9752 | 9600 | |
9601 | if (message) | |
9602 | { | |
9603 | error (message); | |
9604 | free (message); | |
9605 | } | |
a0c938f0 | 9606 | #undef catenate_messages |
92b128ed | 9607 | } |
9608 | ||
3a79f5da | 9609 | /* Mapping for cpp message reasons to the options that enable them. */ |
9610 | ||
9611 | struct reason_option_codes_t | |
9612 | { | |
9613 | const int reason; /* cpplib message reason. */ | |
9614 | const int option_code; /* gcc option that controls this message. */ | |
9615 | }; | |
9616 | ||
9617 | static const struct reason_option_codes_t option_codes[] = { | |
9618 | {CPP_W_DEPRECATED, OPT_Wdeprecated}, | |
67089c6b | 9619 | {CPP_W_COMMENTS, OPT_Wcomment}, |
3a79f5da | 9620 | {CPP_W_TRIGRAPHS, OPT_Wtrigraphs}, |
9621 | {CPP_W_MULTICHAR, OPT_Wmultichar}, | |
9622 | {CPP_W_TRADITIONAL, OPT_Wtraditional}, | |
9623 | {CPP_W_LONG_LONG, OPT_Wlong_long}, | |
9624 | {CPP_W_ENDIF_LABELS, OPT_Wendif_labels}, | |
9625 | {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros}, | |
9626 | {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined}, | |
9627 | {CPP_W_UNDEF, OPT_Wundef}, | |
9628 | {CPP_W_UNUSED_MACROS, OPT_Wunused_macros}, | |
9629 | {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat}, | |
9630 | {CPP_W_NORMALIZE, OPT_Wnormalized_}, | |
9631 | {CPP_W_INVALID_PCH, OPT_Winvalid_pch}, | |
9632 | {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp}, | |
76d340ac | 9633 | {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix}, |
5b1a0622 | 9634 | {CPP_W_DATE_TIME, OPT_Wdate_time}, |
3a79f5da | 9635 | {CPP_W_NONE, 0} |
9636 | }; | |
9637 | ||
9638 | /* Return the gcc option code associated with the reason for a cpp | |
9639 | message, or 0 if none. */ | |
9640 | ||
9641 | static int | |
9642 | c_option_controlling_cpp_error (int reason) | |
9643 | { | |
9644 | const struct reason_option_codes_t *entry; | |
9645 | ||
9646 | for (entry = option_codes; entry->reason != CPP_W_NONE; entry++) | |
9647 | { | |
9648 | if (entry->reason == reason) | |
9649 | return entry->option_code; | |
9650 | } | |
9651 | return 0; | |
9652 | } | |
9653 | ||
7f5f3953 | 9654 | /* Callback from cpp_error for PFILE to print diagnostics from the |
3a79f5da | 9655 | preprocessor. The diagnostic is of type LEVEL, with REASON set |
9656 | to the reason code if LEVEL is represents a warning, at location | |
7f5f3953 | 9657 | LOCATION unless this is after lexing and the compiler's location |
9658 | should be used instead, with column number possibly overridden by | |
9659 | COLUMN_OVERRIDE if not zero; MSG is the translated message and AP | |
9660 | the arguments. Returns true if a diagnostic was emitted, false | |
9661 | otherwise. */ | |
9662 | ||
9663 | bool | |
3a79f5da | 9664 | c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason, |
7f5f3953 | 9665 | location_t location, unsigned int column_override, |
9666 | const char *msg, va_list *ap) | |
9667 | { | |
9668 | diagnostic_info diagnostic; | |
9669 | diagnostic_t dlevel; | |
5ae82d58 | 9670 | bool save_warn_system_headers = global_dc->dc_warn_system_headers; |
7f5f3953 | 9671 | bool ret; |
9672 | ||
9673 | switch (level) | |
9674 | { | |
9675 | case CPP_DL_WARNING_SYSHDR: | |
9676 | if (flag_no_output) | |
9677 | return false; | |
5ae82d58 | 9678 | global_dc->dc_warn_system_headers = 1; |
7f5f3953 | 9679 | /* Fall through. */ |
9680 | case CPP_DL_WARNING: | |
9681 | if (flag_no_output) | |
9682 | return false; | |
9683 | dlevel = DK_WARNING; | |
9684 | break; | |
9685 | case CPP_DL_PEDWARN: | |
9686 | if (flag_no_output && !flag_pedantic_errors) | |
9687 | return false; | |
9688 | dlevel = DK_PEDWARN; | |
9689 | break; | |
9690 | case CPP_DL_ERROR: | |
9691 | dlevel = DK_ERROR; | |
9692 | break; | |
9693 | case CPP_DL_ICE: | |
9694 | dlevel = DK_ICE; | |
9695 | break; | |
9696 | case CPP_DL_NOTE: | |
9697 | dlevel = DK_NOTE; | |
9698 | break; | |
ff903809 | 9699 | case CPP_DL_FATAL: |
9700 | dlevel = DK_FATAL; | |
9701 | break; | |
7f5f3953 | 9702 | default: |
9703 | gcc_unreachable (); | |
9704 | } | |
9705 | if (done_lexing) | |
9706 | location = input_location; | |
9707 | diagnostic_set_info_translated (&diagnostic, msg, ap, | |
9708 | location, dlevel); | |
9709 | if (column_override) | |
9710 | diagnostic_override_column (&diagnostic, column_override); | |
3a79f5da | 9711 | diagnostic_override_option_index (&diagnostic, |
9712 | c_option_controlling_cpp_error (reason)); | |
7f5f3953 | 9713 | ret = report_diagnostic (&diagnostic); |
9714 | if (level == CPP_DL_WARNING_SYSHDR) | |
5ae82d58 | 9715 | global_dc->dc_warn_system_headers = save_warn_system_headers; |
7f5f3953 | 9716 | return ret; |
9717 | } | |
9718 | ||
624d37a6 | 9719 | /* Convert a character from the host to the target execution character |
9720 | set. cpplib handles this, mostly. */ | |
9721 | ||
9722 | HOST_WIDE_INT | |
9723 | c_common_to_target_charset (HOST_WIDE_INT c) | |
9724 | { | |
9725 | /* Character constants in GCC proper are sign-extended under -fsigned-char, | |
9726 | zero-extended under -fno-signed-char. cpplib insists that characters | |
9727 | and character constants are always unsigned. Hence we must convert | |
9728 | back and forth. */ | |
9729 | cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1); | |
9730 | ||
9731 | uc = cpp_host_to_exec_charset (parse_in, uc); | |
9732 | ||
9733 | if (flag_signed_char) | |
9734 | return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE) | |
9735 | >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE); | |
9736 | else | |
9737 | return uc; | |
9738 | } | |
9739 | ||
7549df0d | 9740 | /* Fold an offsetof-like expression. EXPR is a nested sequence of component |
9741 | references with an INDIRECT_REF of a constant at the bottom; much like the | |
9742 | traditional rendering of offsetof as a macro. Return the folded result. */ | |
af28855b | 9743 | |
7549df0d | 9744 | tree |
9745 | fold_offsetof_1 (tree expr) | |
af28855b | 9746 | { |
af28855b | 9747 | tree base, off, t; |
9748 | ||
9749 | switch (TREE_CODE (expr)) | |
9750 | { | |
9751 | case ERROR_MARK: | |
9752 | return expr; | |
9753 | ||
6b11d2e3 | 9754 | case VAR_DECL: |
9755 | error ("cannot apply %<offsetof%> to static data member %qD", expr); | |
9756 | return error_mark_node; | |
9757 | ||
d897f7c2 | 9758 | case CALL_EXPR: |
cf1a89a3 | 9759 | case TARGET_EXPR: |
d897f7c2 | 9760 | error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded"); |
9761 | return error_mark_node; | |
9762 | ||
d897f7c2 | 9763 | case NOP_EXPR: |
9764 | case INDIRECT_REF: | |
7549df0d | 9765 | if (!TREE_CONSTANT (TREE_OPERAND (expr, 0))) |
64ed018c | 9766 | { |
9767 | error ("cannot apply %<offsetof%> to a non constant address"); | |
9768 | return error_mark_node; | |
9769 | } | |
7549df0d | 9770 | return TREE_OPERAND (expr, 0); |
d897f7c2 | 9771 | |
af28855b | 9772 | case COMPONENT_REF: |
7549df0d | 9773 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); |
af28855b | 9774 | if (base == error_mark_node) |
9775 | return base; | |
9776 | ||
9777 | t = TREE_OPERAND (expr, 1); | |
9778 | if (DECL_C_BIT_FIELD (t)) | |
9779 | { | |
9780 | error ("attempt to take address of bit-field structure " | |
782858b8 | 9781 | "member %qD", t); |
af28855b | 9782 | return error_mark_node; |
9783 | } | |
389dd41b | 9784 | off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t), |
08f817b3 | 9785 | size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t)) |
389dd41b | 9786 | / BITS_PER_UNIT)); |
af28855b | 9787 | break; |
9788 | ||
9789 | case ARRAY_REF: | |
7549df0d | 9790 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); |
af28855b | 9791 | if (base == error_mark_node) |
9792 | return base; | |
9793 | ||
9794 | t = TREE_OPERAND (expr, 1); | |
64ed018c | 9795 | |
9796 | /* Check if the offset goes beyond the upper bound of the array. */ | |
7549df0d | 9797 | if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0) |
e0559d69 | 9798 | { |
9799 | tree upbound = array_ref_up_bound (expr); | |
9800 | if (upbound != NULL_TREE | |
9801 | && TREE_CODE (upbound) == INTEGER_CST | |
9802 | && !tree_int_cst_equal (upbound, | |
9803 | TYPE_MAX_VALUE (TREE_TYPE (upbound)))) | |
9804 | { | |
9805 | upbound = size_binop (PLUS_EXPR, upbound, | |
9806 | build_int_cst (TREE_TYPE (upbound), 1)); | |
9807 | if (tree_int_cst_lt (upbound, t)) | |
9808 | { | |
9809 | tree v; | |
9810 | ||
9811 | for (v = TREE_OPERAND (expr, 0); | |
9812 | TREE_CODE (v) == COMPONENT_REF; | |
9813 | v = TREE_OPERAND (v, 0)) | |
9814 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) | |
9815 | == RECORD_TYPE) | |
9816 | { | |
1767a056 | 9817 | tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); |
9818 | for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) | |
e0559d69 | 9819 | if (TREE_CODE (fld_chain) == FIELD_DECL) |
9820 | break; | |
9821 | ||
9822 | if (fld_chain) | |
9823 | break; | |
9824 | } | |
9825 | /* Don't warn if the array might be considered a poor | |
9826 | man's flexible array member with a very permissive | |
9827 | definition thereof. */ | |
9828 | if (TREE_CODE (v) == ARRAY_REF | |
9829 | || TREE_CODE (v) == COMPONENT_REF) | |
9830 | warning (OPT_Warray_bounds, | |
9831 | "index %E denotes an offset " | |
9832 | "greater than size of %qT", | |
9833 | t, TREE_TYPE (TREE_OPERAND (expr, 0))); | |
9834 | } | |
9835 | } | |
9836 | } | |
7549df0d | 9837 | |
9838 | t = convert (sizetype, t); | |
9839 | off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); | |
af28855b | 9840 | break; |
9841 | ||
ede90cc2 | 9842 | case COMPOUND_EXPR: |
9843 | /* Handle static members of volatile structs. */ | |
9844 | t = TREE_OPERAND (expr, 1); | |
9845 | gcc_assert (TREE_CODE (t) == VAR_DECL); | |
7549df0d | 9846 | return fold_offsetof_1 (t); |
ede90cc2 | 9847 | |
af28855b | 9848 | default: |
231bd014 | 9849 | gcc_unreachable (); |
af28855b | 9850 | } |
9851 | ||
7549df0d | 9852 | return fold_build_pointer_plus (base, off); |
af28855b | 9853 | } |
9854 | ||
7549df0d | 9855 | /* Likewise, but convert it to the return type of offsetof. */ |
9856 | ||
af28855b | 9857 | tree |
7549df0d | 9858 | fold_offsetof (tree expr) |
af28855b | 9859 | { |
7549df0d | 9860 | return convert (size_type_node, fold_offsetof_1 (expr)); |
af28855b | 9861 | } |
9862 | ||
b9bdfa0b | 9863 | /* Warn for A ?: C expressions (with B omitted) where A is a boolean |
9864 | expression, because B will always be true. */ | |
9865 | ||
9866 | void | |
9867 | warn_for_omitted_condop (location_t location, tree cond) | |
9868 | { | |
9869 | if (truth_value_p (TREE_CODE (cond))) | |
9870 | warning_at (location, OPT_Wparentheses, | |
9871 | "the omitted middle operand in ?: will always be %<true%>, " | |
9872 | "suggest explicit middle operand"); | |
9873 | } | |
9874 | ||
a1f90215 | 9875 | /* Give an error for storing into ARG, which is 'const'. USE indicates |
9876 | how ARG was being used. */ | |
9877 | ||
9878 | void | |
f2697631 | 9879 | readonly_error (location_t loc, tree arg, enum lvalue_use use) |
a1f90215 | 9880 | { |
9881 | gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement | |
9882 | || use == lv_asm); | |
9883 | /* Using this macro rather than (for example) arrays of messages | |
9884 | ensures that all the format strings are checked at compile | |
9885 | time. */ | |
9886 | #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ | |
9887 | : (use == lv_increment ? (I) \ | |
9888 | : (use == lv_decrement ? (D) : (AS)))) | |
9889 | if (TREE_CODE (arg) == COMPONENT_REF) | |
9890 | { | |
9891 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
f2697631 | 9892 | error_at (loc, READONLY_MSG (G_("assignment of member " |
9893 | "%qD in read-only object"), | |
9894 | G_("increment of member " | |
9895 | "%qD in read-only object"), | |
9896 | G_("decrement of member " | |
9897 | "%qD in read-only object"), | |
9898 | G_("member %qD in read-only object " | |
9899 | "used as %<asm%> output")), | |
9900 | TREE_OPERAND (arg, 1)); | |
a1f90215 | 9901 | else |
f2697631 | 9902 | error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"), |
9903 | G_("increment of read-only member %qD"), | |
9904 | G_("decrement of read-only member %qD"), | |
9905 | G_("read-only member %qD used as %<asm%> output")), | |
9906 | TREE_OPERAND (arg, 1)); | |
a1f90215 | 9907 | } |
9908 | else if (TREE_CODE (arg) == VAR_DECL) | |
f2697631 | 9909 | error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"), |
9910 | G_("increment of read-only variable %qD"), | |
9911 | G_("decrement of read-only variable %qD"), | |
9912 | G_("read-only variable %qD used as %<asm%> output")), | |
9913 | arg); | |
a1f90215 | 9914 | else if (TREE_CODE (arg) == PARM_DECL) |
f2697631 | 9915 | error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"), |
9916 | G_("increment of read-only parameter %qD"), | |
9917 | G_("decrement of read-only parameter %qD"), | |
9918 | G_("read-only parameter %qD use as %<asm%> output")), | |
9919 | arg); | |
a1f90215 | 9920 | else if (TREE_CODE (arg) == RESULT_DECL) |
9921 | { | |
9922 | gcc_assert (c_dialect_cxx ()); | |
f2697631 | 9923 | error_at (loc, READONLY_MSG (G_("assignment of " |
9924 | "read-only named return value %qD"), | |
9925 | G_("increment of " | |
9926 | "read-only named return value %qD"), | |
9927 | G_("decrement of " | |
9928 | "read-only named return value %qD"), | |
9929 | G_("read-only named return value %qD " | |
9930 | "used as %<asm%>output")), | |
9931 | arg); | |
a1f90215 | 9932 | } |
9933 | else if (TREE_CODE (arg) == FUNCTION_DECL) | |
f2697631 | 9934 | error_at (loc, READONLY_MSG (G_("assignment of function %qD"), |
9935 | G_("increment of function %qD"), | |
9936 | G_("decrement of function %qD"), | |
9937 | G_("function %qD used as %<asm%> output")), | |
9938 | arg); | |
a1f90215 | 9939 | else |
f2697631 | 9940 | error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"), |
9941 | G_("increment of read-only location %qE"), | |
9942 | G_("decrement of read-only location %qE"), | |
9943 | G_("read-only location %qE used as %<asm%> output")), | |
9944 | arg); | |
a1f90215 | 9945 | } |
9946 | ||
e35976b1 | 9947 | /* Print an error message for an invalid lvalue. USE says |
fdd84b77 | 9948 | how the lvalue is being used and so selects the error message. LOC |
9949 | is the location for the error. */ | |
ab6bb714 | 9950 | |
e35976b1 | 9951 | void |
fdd84b77 | 9952 | lvalue_error (location_t loc, enum lvalue_use use) |
ab6bb714 | 9953 | { |
e35976b1 | 9954 | switch (use) |
ab6bb714 | 9955 | { |
e35976b1 | 9956 | case lv_assign: |
fdd84b77 | 9957 | error_at (loc, "lvalue required as left operand of assignment"); |
e35976b1 | 9958 | break; |
9959 | case lv_increment: | |
fdd84b77 | 9960 | error_at (loc, "lvalue required as increment operand"); |
e35976b1 | 9961 | break; |
9962 | case lv_decrement: | |
fdd84b77 | 9963 | error_at (loc, "lvalue required as decrement operand"); |
e35976b1 | 9964 | break; |
9965 | case lv_addressof: | |
fdd84b77 | 9966 | error_at (loc, "lvalue required as unary %<&%> operand"); |
e35976b1 | 9967 | break; |
9968 | case lv_asm: | |
fdd84b77 | 9969 | error_at (loc, "lvalue required in asm statement"); |
e35976b1 | 9970 | break; |
9971 | default: | |
9972 | gcc_unreachable (); | |
ab6bb714 | 9973 | } |
ab6bb714 | 9974 | } |
b1bbc8e5 | 9975 | |
9976 | /* Print an error message for an invalid indirection of type TYPE. | |
9977 | ERRSTRING is the name of the operator for the indirection. */ | |
9978 | ||
9979 | void | |
9980 | invalid_indirection_error (location_t loc, tree type, ref_operator errstring) | |
9981 | { | |
9982 | switch (errstring) | |
9983 | { | |
9984 | case RO_NULL: | |
9985 | gcc_assert (c_dialect_cxx ()); | |
9986 | error_at (loc, "invalid type argument (have %qT)", type); | |
9987 | break; | |
9988 | case RO_ARRAY_INDEXING: | |
9989 | error_at (loc, | |
9990 | "invalid type argument of array indexing (have %qT)", | |
9991 | type); | |
9992 | break; | |
9993 | case RO_UNARY_STAR: | |
9994 | error_at (loc, | |
9995 | "invalid type argument of unary %<*%> (have %qT)", | |
9996 | type); | |
9997 | break; | |
9998 | case RO_ARROW: | |
9999 | error_at (loc, | |
10000 | "invalid type argument of %<->%> (have %qT)", | |
10001 | type); | |
10002 | break; | |
7354a89b | 10003 | case RO_ARROW_STAR: |
10004 | error_at (loc, | |
10005 | "invalid type argument of %<->*%> (have %qT)", | |
10006 | type); | |
10007 | break; | |
b1bbc8e5 | 10008 | case RO_IMPLICIT_CONVERSION: |
10009 | error_at (loc, | |
10010 | "invalid type argument of implicit conversion (have %qT)", | |
10011 | type); | |
10012 | break; | |
10013 | default: | |
10014 | gcc_unreachable (); | |
10015 | } | |
10016 | } | |
c271bdb2 | 10017 | \f |
10018 | /* *PTYPE is an incomplete array. Complete it with a domain based on | |
10019 | INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT | |
10020 | is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
10021 | 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ | |
10022 | ||
10023 | int | |
10024 | complete_array_type (tree *ptype, tree initial_value, bool do_default) | |
10025 | { | |
10026 | tree maxindex, type, main_type, elt, unqual_elt; | |
10027 | int failure = 0, quals; | |
6753bca0 | 10028 | hashval_t hashcode = 0; |
f5298614 | 10029 | bool overflow_p = false; |
c271bdb2 | 10030 | |
10031 | maxindex = size_zero_node; | |
10032 | if (initial_value) | |
10033 | { | |
10034 | if (TREE_CODE (initial_value) == STRING_CST) | |
10035 | { | |
10036 | int eltsize | |
10037 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
10038 | maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1); | |
10039 | } | |
10040 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) | |
10041 | { | |
f1f41a6c | 10042 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value); |
c271bdb2 | 10043 | |
f1f41a6c | 10044 | if (vec_safe_is_empty (v)) |
c271bdb2 | 10045 | { |
10046 | if (pedantic) | |
10047 | failure = 3; | |
7542c3b4 | 10048 | maxindex = ssize_int (-1); |
c271bdb2 | 10049 | } |
10050 | else | |
10051 | { | |
10052 | tree curindex; | |
c75b4594 | 10053 | unsigned HOST_WIDE_INT cnt; |
10054 | constructor_elt *ce; | |
cee43f7e | 10055 | bool fold_p = false; |
c271bdb2 | 10056 | |
f1f41a6c | 10057 | if ((*v)[0].index) |
f5298614 | 10058 | maxindex = (*v)[0].index, fold_p = true; |
10059 | ||
c271bdb2 | 10060 | curindex = maxindex; |
10061 | ||
f1f41a6c | 10062 | for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++) |
c271bdb2 | 10063 | { |
cee43f7e | 10064 | bool curfold_p = false; |
c75b4594 | 10065 | if (ce->index) |
cee43f7e | 10066 | curindex = ce->index, curfold_p = true; |
c271bdb2 | 10067 | else |
cee43f7e | 10068 | { |
10069 | if (fold_p) | |
f5298614 | 10070 | { |
10071 | /* Since we treat size types now as ordinary | |
10072 | unsigned types, we need an explicit overflow | |
10073 | check. */ | |
10074 | tree orig = curindex; | |
10075 | curindex = fold_convert (sizetype, curindex); | |
10076 | overflow_p |= tree_int_cst_lt (curindex, orig); | |
10077 | } | |
389dd41b | 10078 | curindex = size_binop (PLUS_EXPR, curindex, |
10079 | size_one_node); | |
cee43f7e | 10080 | } |
c271bdb2 | 10081 | if (tree_int_cst_lt (maxindex, curindex)) |
cee43f7e | 10082 | maxindex = curindex, fold_p = curfold_p; |
c271bdb2 | 10083 | } |
f5298614 | 10084 | if (fold_p) |
10085 | { | |
10086 | tree orig = maxindex; | |
10087 | maxindex = fold_convert (sizetype, maxindex); | |
10088 | overflow_p |= tree_int_cst_lt (maxindex, orig); | |
10089 | } | |
c271bdb2 | 10090 | } |
10091 | } | |
10092 | else | |
10093 | { | |
10094 | /* Make an error message unless that happened already. */ | |
10095 | if (initial_value != error_mark_node) | |
10096 | failure = 1; | |
10097 | } | |
10098 | } | |
10099 | else | |
10100 | { | |
10101 | failure = 2; | |
10102 | if (!do_default) | |
10103 | return failure; | |
10104 | } | |
10105 | ||
10106 | type = *ptype; | |
10107 | elt = TREE_TYPE (type); | |
10108 | quals = TYPE_QUALS (strip_array_types (elt)); | |
10109 | if (quals == 0) | |
10110 | unqual_elt = elt; | |
10111 | else | |
6d5d708e | 10112 | unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals)); |
c271bdb2 | 10113 | |
10114 | /* Using build_distinct_type_copy and modifying things afterward instead | |
10115 | of using build_array_type to create a new type preserves all of the | |
10116 | TYPE_LANG_FLAG_? bits that the front end may have set. */ | |
10117 | main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); | |
10118 | TREE_TYPE (main_type) = unqual_elt; | |
783bb57e | 10119 | TYPE_DOMAIN (main_type) |
10120 | = build_range_type (TREE_TYPE (maxindex), | |
10121 | build_int_cst (TREE_TYPE (maxindex), 0), maxindex); | |
c271bdb2 | 10122 | layout_type (main_type); |
10123 | ||
6753bca0 | 10124 | /* Make sure we have the canonical MAIN_TYPE. */ |
10125 | hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode); | |
48e1416a | 10126 | hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)), |
6753bca0 | 10127 | hashcode); |
10128 | main_type = type_hash_canon (hashcode, main_type); | |
10129 | ||
796735dc | 10130 | /* Fix the canonical type. */ |
10131 | if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type)) | |
10132 | || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type))) | |
10133 | SET_TYPE_STRUCTURAL_EQUALITY (main_type); | |
10134 | else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type) | |
10135 | || (TYPE_CANONICAL (TYPE_DOMAIN (main_type)) | |
10136 | != TYPE_DOMAIN (main_type))) | |
48e1416a | 10137 | TYPE_CANONICAL (main_type) |
796735dc | 10138 | = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)), |
10139 | TYPE_CANONICAL (TYPE_DOMAIN (main_type))); | |
10140 | else | |
10141 | TYPE_CANONICAL (main_type) = main_type; | |
10142 | ||
c271bdb2 | 10143 | if (quals == 0) |
10144 | type = main_type; | |
10145 | else | |
10146 | type = c_build_qualified_type (main_type, quals); | |
10147 | ||
4f5b8f2a | 10148 | if (COMPLETE_TYPE_P (type) |
10149 | && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST | |
f5298614 | 10150 | && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))) |
4f5b8f2a | 10151 | { |
10152 | error ("size of array is too large"); | |
10153 | /* If we proceed with the array type as it is, we'll eventually | |
08f817b3 | 10154 | crash in tree_to_[su]hwi(). */ |
4f5b8f2a | 10155 | type = error_mark_node; |
10156 | } | |
10157 | ||
c271bdb2 | 10158 | *ptype = type; |
10159 | return failure; | |
10160 | } | |
ab6bb714 | 10161 | |
93426222 | 10162 | /* Like c_mark_addressable but don't check register qualifier. */ |
10163 | void | |
10164 | c_common_mark_addressable_vec (tree t) | |
10165 | { | |
10166 | while (handled_component_p (t)) | |
10167 | t = TREE_OPERAND (t, 0); | |
10168 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10169 | return; | |
10170 | TREE_ADDRESSABLE (t) = 1; | |
10171 | } | |
10172 | ||
10173 | ||
b6a5fc45 | 10174 | \f |
10175 | /* Used to help initialize the builtin-types.def table. When a type of | |
10176 | the correct size doesn't exist, use error_mark_node instead of NULL. | |
10177 | The later results in segfaults even when a decl using the type doesn't | |
10178 | get invoked. */ | |
10179 | ||
10180 | tree | |
10181 | builtin_type_for_size (int size, bool unsignedp) | |
10182 | { | |
a51edb4c | 10183 | tree type = c_common_type_for_size (size, unsignedp); |
b6a5fc45 | 10184 | return type ? type : error_mark_node; |
10185 | } | |
10186 | ||
10187 | /* A helper function for resolve_overloaded_builtin in resolving the | |
10188 | overloaded __sync_ builtins. Returns a positive power of 2 if the | |
10189 | first operand of PARAMS is a pointer to a supported data type. | |
10190 | Returns 0 if an error is encountered. */ | |
10191 | ||
10192 | static int | |
f1f41a6c | 10193 | sync_resolve_size (tree function, vec<tree, va_gc> *params) |
b6a5fc45 | 10194 | { |
10195 | tree type; | |
10196 | int size; | |
10197 | ||
f1f41a6c | 10198 | if (!params) |
b6a5fc45 | 10199 | { |
10200 | error ("too few arguments to function %qE", function); | |
10201 | return 0; | |
10202 | } | |
10203 | ||
f1f41a6c | 10204 | type = TREE_TYPE ((*params)[0]); |
0d284870 | 10205 | if (TREE_CODE (type) == ARRAY_TYPE) |
10206 | { | |
10207 | /* Force array-to-pointer decay for C++. */ | |
10208 | gcc_assert (c_dialect_cxx()); | |
10209 | (*params)[0] = default_conversion ((*params)[0]); | |
10210 | type = TREE_TYPE ((*params)[0]); | |
10211 | } | |
b6a5fc45 | 10212 | if (TREE_CODE (type) != POINTER_TYPE) |
10213 | goto incompatible; | |
10214 | ||
10215 | type = TREE_TYPE (type); | |
10216 | if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) | |
10217 | goto incompatible; | |
10218 | ||
6a0712d4 | 10219 | size = tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
27213ba3 | 10220 | if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16) |
b6a5fc45 | 10221 | return size; |
10222 | ||
10223 | incompatible: | |
10224 | error ("incompatible type for argument %d of %qE", 1, function); | |
10225 | return 0; | |
10226 | } | |
10227 | ||
a0c938f0 | 10228 | /* A helper function for resolve_overloaded_builtin. Adds casts to |
b6a5fc45 | 10229 | PARAMS to make arguments match up with those of FUNCTION. Drops |
10230 | the variadic arguments at the end. Returns false if some error | |
10231 | was encountered; true on success. */ | |
10232 | ||
10233 | static bool | |
1cd6e20d | 10234 | sync_resolve_params (location_t loc, tree orig_function, tree function, |
f1f41a6c | 10235 | vec<tree, va_gc> *params, bool orig_format) |
b6a5fc45 | 10236 | { |
d0af78c5 | 10237 | function_args_iterator iter; |
b6a5fc45 | 10238 | tree ptype; |
b9c74b4d | 10239 | unsigned int parmnum; |
b6a5fc45 | 10240 | |
d0af78c5 | 10241 | function_args_iter_init (&iter, TREE_TYPE (function)); |
b6a5fc45 | 10242 | /* We've declared the implementation functions to use "volatile void *" |
10243 | as the pointer parameter, so we shouldn't get any complaints from the | |
10244 | call to check_function_arguments what ever type the user used. */ | |
d0af78c5 | 10245 | function_args_iter_next (&iter); |
f1f41a6c | 10246 | ptype = TREE_TYPE (TREE_TYPE ((*params)[0])); |
b560fabd | 10247 | ptype = TYPE_MAIN_VARIANT (ptype); |
b6a5fc45 | 10248 | |
10249 | /* For the rest of the values, we need to cast these to FTYPE, so that we | |
10250 | don't get warnings for passing pointer types, etc. */ | |
b9c74b4d | 10251 | parmnum = 0; |
d0af78c5 | 10252 | while (1) |
b6a5fc45 | 10253 | { |
d0af78c5 | 10254 | tree val, arg_type; |
10255 | ||
10256 | arg_type = function_args_iter_cond (&iter); | |
10257 | /* XXX void_type_node belies the abstraction. */ | |
10258 | if (arg_type == void_type_node) | |
10259 | break; | |
b6a5fc45 | 10260 | |
b9c74b4d | 10261 | ++parmnum; |
f1f41a6c | 10262 | if (params->length () <= parmnum) |
b6a5fc45 | 10263 | { |
1cd6e20d | 10264 | error_at (loc, "too few arguments to function %qE", orig_function); |
b6a5fc45 | 10265 | return false; |
10266 | } | |
10267 | ||
0f6a7cb7 | 10268 | /* Only convert parameters if arg_type is unsigned integer type with |
10269 | new format sync routines, i.e. don't attempt to convert pointer | |
10270 | arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n), | |
10271 | bool arguments (e.g. WEAK argument) or signed int arguments (memmodel | |
10272 | kinds). */ | |
10273 | if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type)) | |
1cd6e20d | 10274 | { |
10275 | /* Ideally for the first conversion we'd use convert_for_assignment | |
10276 | so that we get warnings for anything that doesn't match the pointer | |
10277 | type. This isn't portable across the C and C++ front ends atm. */ | |
f1f41a6c | 10278 | val = (*params)[parmnum]; |
1cd6e20d | 10279 | val = convert (ptype, val); |
10280 | val = convert (arg_type, val); | |
f1f41a6c | 10281 | (*params)[parmnum] = val; |
1cd6e20d | 10282 | } |
b6a5fc45 | 10283 | |
d0af78c5 | 10284 | function_args_iter_next (&iter); |
b6a5fc45 | 10285 | } |
10286 | ||
1cd6e20d | 10287 | /* __atomic routines are not variadic. */ |
f1f41a6c | 10288 | if (!orig_format && params->length () != parmnum + 1) |
1cd6e20d | 10289 | { |
10290 | error_at (loc, "too many arguments to function %qE", orig_function); | |
10291 | return false; | |
10292 | } | |
10293 | ||
b6a5fc45 | 10294 | /* The definition of these primitives is variadic, with the remaining |
10295 | being "an optional list of variables protected by the memory barrier". | |
10296 | No clue what that's supposed to mean, precisely, but we consider all | |
10297 | call-clobbered variables to be protected so we're safe. */ | |
f1f41a6c | 10298 | params->truncate (parmnum + 1); |
b6a5fc45 | 10299 | |
10300 | return true; | |
10301 | } | |
10302 | ||
a0c938f0 | 10303 | /* A helper function for resolve_overloaded_builtin. Adds a cast to |
b6a5fc45 | 10304 | RESULT to make it match the type of the first pointer argument in |
10305 | PARAMS. */ | |
10306 | ||
10307 | static tree | |
1cd6e20d | 10308 | sync_resolve_return (tree first_param, tree result, bool orig_format) |
b6a5fc45 | 10309 | { |
b9c74b4d | 10310 | tree ptype = TREE_TYPE (TREE_TYPE (first_param)); |
1cd6e20d | 10311 | tree rtype = TREE_TYPE (result); |
10080eac | 10312 | ptype = TYPE_MAIN_VARIANT (ptype); |
1cd6e20d | 10313 | |
10314 | /* New format doesn't require casting unless the types are the same size. */ | |
10315 | if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype))) | |
10316 | return convert (ptype, result); | |
10317 | else | |
10318 | return result; | |
10319 | } | |
10320 | ||
10321 | /* This function verifies the PARAMS to generic atomic FUNCTION. | |
10322 | It returns the size if all the parameters are the same size, otherwise | |
10323 | 0 is returned if the parameters are invalid. */ | |
10324 | ||
10325 | static int | |
f1f41a6c | 10326 | get_atomic_generic_size (location_t loc, tree function, |
10327 | vec<tree, va_gc> *params) | |
1cd6e20d | 10328 | { |
10329 | unsigned int n_param; | |
10330 | unsigned int n_model; | |
10331 | unsigned int x; | |
10332 | int size_0; | |
10333 | tree type_0; | |
10334 | ||
10335 | /* Determine the parameter makeup. */ | |
10336 | switch (DECL_FUNCTION_CODE (function)) | |
10337 | { | |
10338 | case BUILT_IN_ATOMIC_EXCHANGE: | |
10339 | n_param = 4; | |
10340 | n_model = 1; | |
10341 | break; | |
10342 | case BUILT_IN_ATOMIC_LOAD: | |
10343 | case BUILT_IN_ATOMIC_STORE: | |
10344 | n_param = 3; | |
10345 | n_model = 1; | |
10346 | break; | |
10347 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
10348 | n_param = 6; | |
10349 | n_model = 2; | |
10350 | break; | |
10351 | default: | |
1d581089 | 10352 | gcc_unreachable (); |
1cd6e20d | 10353 | } |
10354 | ||
f1f41a6c | 10355 | if (vec_safe_length (params) != n_param) |
1cd6e20d | 10356 | { |
10357 | error_at (loc, "incorrect number of arguments to function %qE", function); | |
10358 | return 0; | |
10359 | } | |
10360 | ||
10361 | /* Get type of first parameter, and determine its size. */ | |
f1f41a6c | 10362 | type_0 = TREE_TYPE ((*params)[0]); |
0d284870 | 10363 | if (TREE_CODE (type_0) == ARRAY_TYPE) |
10364 | { | |
10365 | /* Force array-to-pointer decay for C++. */ | |
10366 | gcc_assert (c_dialect_cxx()); | |
10367 | (*params)[0] = default_conversion ((*params)[0]); | |
10368 | type_0 = TREE_TYPE ((*params)[0]); | |
10369 | } | |
1d581089 | 10370 | if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0))) |
10371 | { | |
10372 | error_at (loc, "argument 1 of %qE must be a non-void pointer type", | |
10373 | function); | |
10374 | return 0; | |
10375 | } | |
10376 | ||
10377 | /* Types must be compile time constant sizes. */ | |
10378 | if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST) | |
1cd6e20d | 10379 | { |
1d581089 | 10380 | error_at (loc, |
10381 | "argument 1 of %qE must be a pointer to a constant size type", | |
10382 | function); | |
1cd6e20d | 10383 | return 0; |
10384 | } | |
1d581089 | 10385 | |
6a0712d4 | 10386 | size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0))); |
1cd6e20d | 10387 | |
1d581089 | 10388 | /* Zero size objects are not allowed. */ |
10389 | if (size_0 == 0) | |
10390 | { | |
10391 | error_at (loc, | |
10392 | "argument 1 of %qE must be a pointer to a nonzero size object", | |
10393 | function); | |
10394 | return 0; | |
10395 | } | |
10396 | ||
1cd6e20d | 10397 | /* Check each other parameter is a pointer and the same size. */ |
10398 | for (x = 0; x < n_param - n_model; x++) | |
10399 | { | |
10400 | int size; | |
f1f41a6c | 10401 | tree type = TREE_TYPE ((*params)[x]); |
a04e8d62 | 10402 | /* __atomic_compare_exchange has a bool in the 4th position, skip it. */ |
1cd6e20d | 10403 | if (n_param == 6 && x == 3) |
10404 | continue; | |
10405 | if (!POINTER_TYPE_P (type)) | |
10406 | { | |
10407 | error_at (loc, "argument %d of %qE must be a pointer type", x + 1, | |
10408 | function); | |
10409 | return 0; | |
10410 | } | |
6a0712d4 | 10411 | size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type))); |
1cd6e20d | 10412 | if (size != size_0) |
10413 | { | |
10414 | error_at (loc, "size mismatch in argument %d of %qE", x + 1, | |
10415 | function); | |
10416 | return 0; | |
10417 | } | |
10418 | } | |
10419 | ||
10420 | /* Check memory model parameters for validity. */ | |
10421 | for (x = n_param - n_model ; x < n_param; x++) | |
10422 | { | |
f1f41a6c | 10423 | tree p = (*params)[x]; |
1cd6e20d | 10424 | if (TREE_CODE (p) == INTEGER_CST) |
10425 | { | |
6a0712d4 | 10426 | int i = tree_to_uhwi (p); |
72d65da9 | 10427 | if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST) |
1cd6e20d | 10428 | { |
10429 | warning_at (loc, OPT_Winvalid_memory_model, | |
10430 | "invalid memory model argument %d of %qE", x + 1, | |
10431 | function); | |
1cd6e20d | 10432 | } |
10433 | } | |
10434 | else | |
10435 | if (!INTEGRAL_TYPE_P (TREE_TYPE (p))) | |
10436 | { | |
10437 | error_at (loc, "non-integer memory model argument %d of %qE", x + 1, | |
10438 | function); | |
10439 | return 0; | |
10440 | } | |
10441 | } | |
10442 | ||
10443 | return size_0; | |
10444 | } | |
10445 | ||
10446 | ||
10447 | /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N | |
10448 | at the beginning of the parameter list PARAMS representing the size of the | |
10449 | objects. This is to match the library ABI requirement. LOC is the location | |
10450 | of the function call. | |
10451 | The new function is returned if it needed rebuilding, otherwise NULL_TREE is | |
10452 | returned to allow the external call to be constructed. */ | |
10453 | ||
10454 | static tree | |
10455 | add_atomic_size_parameter (unsigned n, location_t loc, tree function, | |
f1f41a6c | 10456 | vec<tree, va_gc> *params) |
1cd6e20d | 10457 | { |
10458 | tree size_node; | |
10459 | ||
10460 | /* Insert a SIZE_T parameter as the first param. If there isn't | |
10461 | enough space, allocate a new vector and recursively re-build with that. */ | |
f1f41a6c | 10462 | if (!params->space (1)) |
1cd6e20d | 10463 | { |
10464 | unsigned int z, len; | |
f1f41a6c | 10465 | vec<tree, va_gc> *v; |
1cd6e20d | 10466 | tree f; |
10467 | ||
f1f41a6c | 10468 | len = params->length (); |
10469 | vec_alloc (v, len + 1); | |
5a672e62 | 10470 | v->quick_push (build_int_cst (size_type_node, n)); |
1cd6e20d | 10471 | for (z = 0; z < len; z++) |
f1f41a6c | 10472 | v->quick_push ((*params)[z]); |
ec761d5a | 10473 | f = build_function_call_vec (loc, vNULL, function, v, NULL); |
f1f41a6c | 10474 | vec_free (v); |
1cd6e20d | 10475 | return f; |
10476 | } | |
10477 | ||
10478 | /* Add the size parameter and leave as a function call for processing. */ | |
10479 | size_node = build_int_cst (size_type_node, n); | |
f1f41a6c | 10480 | params->quick_insert (0, size_node); |
1cd6e20d | 10481 | return NULL_TREE; |
10482 | } | |
10483 | ||
10484 | ||
a056826c | 10485 | /* Return whether atomic operations for naturally aligned N-byte |
10486 | arguments are supported, whether inline or through libatomic. */ | |
10487 | static bool | |
10488 | atomic_size_supported_p (int n) | |
10489 | { | |
10490 | switch (n) | |
10491 | { | |
10492 | case 1: | |
10493 | case 2: | |
10494 | case 4: | |
10495 | case 8: | |
10496 | return true; | |
10497 | ||
10498 | case 16: | |
10499 | return targetm.scalar_mode_supported_p (TImode); | |
10500 | ||
10501 | default: | |
10502 | return false; | |
10503 | } | |
10504 | } | |
10505 | ||
1cd6e20d | 10506 | /* This will process an __atomic_exchange function call, determine whether it |
10507 | needs to be mapped to the _N variation, or turned into a library call. | |
10508 | LOC is the location of the builtin call. | |
10509 | FUNCTION is the DECL that has been invoked; | |
10510 | PARAMS is the argument list for the call. The return value is non-null | |
10511 | TRUE is returned if it is translated into the proper format for a call to the | |
10512 | external library, and NEW_RETURN is set the tree for that function. | |
10513 | FALSE is returned if processing for the _N variation is required, and | |
10514 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10515 | static bool | |
10516 | resolve_overloaded_atomic_exchange (location_t loc, tree function, | |
f1f41a6c | 10517 | vec<tree, va_gc> *params, tree *new_return) |
1cd6e20d | 10518 | { |
10519 | tree p0, p1, p2, p3; | |
10520 | tree I_type, I_type_ptr; | |
10521 | int n = get_atomic_generic_size (loc, function, params); | |
10522 | ||
1d581089 | 10523 | /* Size of 0 is an error condition. */ |
10524 | if (n == 0) | |
10525 | { | |
10526 | *new_return = error_mark_node; | |
10527 | return true; | |
10528 | } | |
10529 | ||
1cd6e20d | 10530 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 10531 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 10532 | { |
10533 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10534 | return true; | |
10535 | } | |
10536 | ||
10537 | /* Otherwise there is a lockfree match, transform the call from: | |
10538 | void fn(T* mem, T* desired, T* return, model) | |
10539 | into | |
10540 | *return = (T) (fn (In* mem, (In) *desired, model)) */ | |
10541 | ||
f1f41a6c | 10542 | p0 = (*params)[0]; |
10543 | p1 = (*params)[1]; | |
10544 | p2 = (*params)[2]; | |
10545 | p3 = (*params)[3]; | |
1cd6e20d | 10546 | |
10547 | /* Create pointer to appropriate size. */ | |
10548 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10549 | I_type_ptr = build_pointer_type (I_type); | |
10550 | ||
10551 | /* Convert object pointer to required type. */ | |
10552 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 10553 | (*params)[0] = p0; |
1cd6e20d | 10554 | /* Convert new value to required type, and dereference it. */ |
10555 | p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10556 | p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); | |
f1f41a6c | 10557 | (*params)[1] = p1; |
1cd6e20d | 10558 | |
10559 | /* Move memory model to the 3rd position, and end param list. */ | |
f1f41a6c | 10560 | (*params)[2] = p3; |
10561 | params->truncate (3); | |
1cd6e20d | 10562 | |
10563 | /* Convert return pointer and dereference it for later assignment. */ | |
10564 | *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR); | |
10565 | ||
10566 | return false; | |
b6a5fc45 | 10567 | } |
10568 | ||
1cd6e20d | 10569 | |
10570 | /* This will process an __atomic_compare_exchange function call, determine | |
10571 | whether it needs to be mapped to the _N variation, or turned into a lib call. | |
10572 | LOC is the location of the builtin call. | |
10573 | FUNCTION is the DECL that has been invoked; | |
10574 | PARAMS is the argument list for the call. The return value is non-null | |
10575 | TRUE is returned if it is translated into the proper format for a call to the | |
10576 | external library, and NEW_RETURN is set the tree for that function. | |
10577 | FALSE is returned if processing for the _N variation is required. */ | |
10578 | ||
10579 | static bool | |
10580 | resolve_overloaded_atomic_compare_exchange (location_t loc, tree function, | |
f1f41a6c | 10581 | vec<tree, va_gc> *params, |
1cd6e20d | 10582 | tree *new_return) |
10583 | { | |
10584 | tree p0, p1, p2; | |
10585 | tree I_type, I_type_ptr; | |
10586 | int n = get_atomic_generic_size (loc, function, params); | |
10587 | ||
1d581089 | 10588 | /* Size of 0 is an error condition. */ |
10589 | if (n == 0) | |
10590 | { | |
10591 | *new_return = error_mark_node; | |
10592 | return true; | |
10593 | } | |
10594 | ||
1cd6e20d | 10595 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 10596 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 10597 | { |
10598 | /* The library generic format does not have the weak parameter, so | |
10599 | remove it from the param list. Since a parameter has been removed, | |
10600 | we can be sure that there is room for the SIZE_T parameter, meaning | |
10601 | there will not be a recursive rebuilding of the parameter list, so | |
10602 | there is no danger this will be done twice. */ | |
10603 | if (n > 0) | |
10604 | { | |
f1f41a6c | 10605 | (*params)[3] = (*params)[4]; |
10606 | (*params)[4] = (*params)[5]; | |
10607 | params->truncate (5); | |
1cd6e20d | 10608 | } |
10609 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10610 | return true; | |
10611 | } | |
10612 | ||
10613 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10614 | bool fn(T* mem, T* desired, T* return, weak, success, failure) | |
10615 | into | |
10616 | bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */ | |
10617 | ||
f1f41a6c | 10618 | p0 = (*params)[0]; |
10619 | p1 = (*params)[1]; | |
10620 | p2 = (*params)[2]; | |
1cd6e20d | 10621 | |
10622 | /* Create pointer to appropriate size. */ | |
10623 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10624 | I_type_ptr = build_pointer_type (I_type); | |
10625 | ||
10626 | /* Convert object pointer to required type. */ | |
10627 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 10628 | (*params)[0] = p0; |
1cd6e20d | 10629 | |
10630 | /* Convert expected pointer to required type. */ | |
10631 | p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1); | |
f1f41a6c | 10632 | (*params)[1] = p1; |
1cd6e20d | 10633 | |
10634 | /* Convert desired value to required type, and dereference it. */ | |
10635 | p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR); | |
10636 | p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2); | |
f1f41a6c | 10637 | (*params)[2] = p2; |
1cd6e20d | 10638 | |
10639 | /* The rest of the parameters are fine. NULL means no special return value | |
10640 | processing.*/ | |
10641 | *new_return = NULL; | |
10642 | return false; | |
10643 | } | |
10644 | ||
10645 | ||
10646 | /* This will process an __atomic_load function call, determine whether it | |
10647 | needs to be mapped to the _N variation, or turned into a library call. | |
10648 | LOC is the location of the builtin call. | |
10649 | FUNCTION is the DECL that has been invoked; | |
10650 | PARAMS is the argument list for the call. The return value is non-null | |
10651 | TRUE is returned if it is translated into the proper format for a call to the | |
10652 | external library, and NEW_RETURN is set the tree for that function. | |
10653 | FALSE is returned if processing for the _N variation is required, and | |
10654 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10655 | ||
10656 | static bool | |
10657 | resolve_overloaded_atomic_load (location_t loc, tree function, | |
f1f41a6c | 10658 | vec<tree, va_gc> *params, tree *new_return) |
1cd6e20d | 10659 | { |
10660 | tree p0, p1, p2; | |
10661 | tree I_type, I_type_ptr; | |
10662 | int n = get_atomic_generic_size (loc, function, params); | |
10663 | ||
1d581089 | 10664 | /* Size of 0 is an error condition. */ |
10665 | if (n == 0) | |
10666 | { | |
10667 | *new_return = error_mark_node; | |
10668 | return true; | |
10669 | } | |
10670 | ||
1cd6e20d | 10671 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 10672 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 10673 | { |
10674 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10675 | return true; | |
10676 | } | |
10677 | ||
10678 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10679 | void fn(T* mem, T* return, model) | |
10680 | into | |
10681 | *return = (T) (fn ((In *) mem, model)) */ | |
10682 | ||
f1f41a6c | 10683 | p0 = (*params)[0]; |
10684 | p1 = (*params)[1]; | |
10685 | p2 = (*params)[2]; | |
1cd6e20d | 10686 | |
10687 | /* Create pointer to appropriate size. */ | |
10688 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10689 | I_type_ptr = build_pointer_type (I_type); | |
10690 | ||
10691 | /* Convert object pointer to required type. */ | |
10692 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 10693 | (*params)[0] = p0; |
1cd6e20d | 10694 | |
10695 | /* Move memory model to the 2nd position, and end param list. */ | |
f1f41a6c | 10696 | (*params)[1] = p2; |
10697 | params->truncate (2); | |
1cd6e20d | 10698 | |
10699 | /* Convert return pointer and dereference it for later assignment. */ | |
10700 | *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10701 | ||
10702 | return false; | |
10703 | } | |
10704 | ||
10705 | ||
10706 | /* This will process an __atomic_store function call, determine whether it | |
10707 | needs to be mapped to the _N variation, or turned into a library call. | |
10708 | LOC is the location of the builtin call. | |
10709 | FUNCTION is the DECL that has been invoked; | |
10710 | PARAMS is the argument list for the call. The return value is non-null | |
10711 | TRUE is returned if it is translated into the proper format for a call to the | |
10712 | external library, and NEW_RETURN is set the tree for that function. | |
10713 | FALSE is returned if processing for the _N variation is required, and | |
10714 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10715 | ||
10716 | static bool | |
10717 | resolve_overloaded_atomic_store (location_t loc, tree function, | |
f1f41a6c | 10718 | vec<tree, va_gc> *params, tree *new_return) |
1cd6e20d | 10719 | { |
10720 | tree p0, p1; | |
10721 | tree I_type, I_type_ptr; | |
10722 | int n = get_atomic_generic_size (loc, function, params); | |
10723 | ||
1d581089 | 10724 | /* Size of 0 is an error condition. */ |
10725 | if (n == 0) | |
10726 | { | |
10727 | *new_return = error_mark_node; | |
10728 | return true; | |
10729 | } | |
10730 | ||
1cd6e20d | 10731 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 10732 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 10733 | { |
10734 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10735 | return true; | |
10736 | } | |
10737 | ||
10738 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10739 | void fn(T* mem, T* value, model) | |
10740 | into | |
10741 | fn ((In *) mem, (In) *value, model) */ | |
10742 | ||
f1f41a6c | 10743 | p0 = (*params)[0]; |
10744 | p1 = (*params)[1]; | |
1cd6e20d | 10745 | |
10746 | /* Create pointer to appropriate size. */ | |
10747 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10748 | I_type_ptr = build_pointer_type (I_type); | |
10749 | ||
10750 | /* Convert object pointer to required type. */ | |
10751 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 10752 | (*params)[0] = p0; |
1cd6e20d | 10753 | |
10754 | /* Convert new value to required type, and dereference it. */ | |
10755 | p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10756 | p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); | |
f1f41a6c | 10757 | (*params)[1] = p1; |
1cd6e20d | 10758 | |
10759 | /* The memory model is in the right spot already. Return is void. */ | |
10760 | *new_return = NULL_TREE; | |
10761 | ||
10762 | return false; | |
10763 | } | |
10764 | ||
10765 | ||
b6a5fc45 | 10766 | /* Some builtin functions are placeholders for other expressions. This |
10767 | function should be called immediately after parsing the call expression | |
10768 | before surrounding code has committed to the type of the expression. | |
10769 | ||
e60a6f7b | 10770 | LOC is the location of the builtin call. |
10771 | ||
b6a5fc45 | 10772 | FUNCTION is the DECL that has been invoked; it is known to be a builtin. |
10773 | PARAMS is the argument list for the call. The return value is non-null | |
10774 | when expansion is complete, and null if normal processing should | |
10775 | continue. */ | |
10776 | ||
10777 | tree | |
f1f41a6c | 10778 | resolve_overloaded_builtin (location_t loc, tree function, |
10779 | vec<tree, va_gc> *params) | |
b6a5fc45 | 10780 | { |
10781 | enum built_in_function orig_code = DECL_FUNCTION_CODE (function); | |
1cd6e20d | 10782 | bool orig_format = true; |
10783 | tree new_return = NULL_TREE; | |
10784 | ||
65441f6f | 10785 | switch (DECL_BUILT_IN_CLASS (function)) |
10786 | { | |
10787 | case BUILT_IN_NORMAL: | |
10788 | break; | |
10789 | case BUILT_IN_MD: | |
10790 | if (targetm.resolve_overloaded_builtin) | |
e60a6f7b | 10791 | return targetm.resolve_overloaded_builtin (loc, function, params); |
65441f6f | 10792 | else |
a0c938f0 | 10793 | return NULL_TREE; |
65441f6f | 10794 | default: |
10795 | return NULL_TREE; | |
10796 | } | |
a0c938f0 | 10797 | |
65441f6f | 10798 | /* Handle BUILT_IN_NORMAL here. */ |
b6a5fc45 | 10799 | switch (orig_code) |
10800 | { | |
1cd6e20d | 10801 | case BUILT_IN_ATOMIC_EXCHANGE: |
10802 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
10803 | case BUILT_IN_ATOMIC_LOAD: | |
10804 | case BUILT_IN_ATOMIC_STORE: | |
10805 | { | |
10806 | /* Handle these 4 together so that they can fall through to the next | |
10807 | case if the call is transformed to an _N variant. */ | |
10808 | switch (orig_code) | |
10809 | { | |
10810 | case BUILT_IN_ATOMIC_EXCHANGE: | |
10811 | { | |
10812 | if (resolve_overloaded_atomic_exchange (loc, function, params, | |
10813 | &new_return)) | |
10814 | return new_return; | |
10815 | /* Change to the _N variant. */ | |
10816 | orig_code = BUILT_IN_ATOMIC_EXCHANGE_N; | |
10817 | break; | |
10818 | } | |
10819 | ||
10820 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
10821 | { | |
10822 | if (resolve_overloaded_atomic_compare_exchange (loc, function, | |
10823 | params, | |
10824 | &new_return)) | |
10825 | return new_return; | |
10826 | /* Change to the _N variant. */ | |
10827 | orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N; | |
10828 | break; | |
10829 | } | |
10830 | case BUILT_IN_ATOMIC_LOAD: | |
10831 | { | |
10832 | if (resolve_overloaded_atomic_load (loc, function, params, | |
10833 | &new_return)) | |
10834 | return new_return; | |
10835 | /* Change to the _N variant. */ | |
10836 | orig_code = BUILT_IN_ATOMIC_LOAD_N; | |
10837 | break; | |
10838 | } | |
10839 | case BUILT_IN_ATOMIC_STORE: | |
10840 | { | |
10841 | if (resolve_overloaded_atomic_store (loc, function, params, | |
10842 | &new_return)) | |
10843 | return new_return; | |
10844 | /* Change to the _N variant. */ | |
10845 | orig_code = BUILT_IN_ATOMIC_STORE_N; | |
10846 | break; | |
10847 | } | |
10848 | default: | |
10849 | gcc_unreachable (); | |
10850 | } | |
10851 | /* Fallthrough to the normal processing. */ | |
10852 | } | |
10853 | case BUILT_IN_ATOMIC_EXCHANGE_N: | |
10854 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N: | |
10855 | case BUILT_IN_ATOMIC_LOAD_N: | |
10856 | case BUILT_IN_ATOMIC_STORE_N: | |
10857 | case BUILT_IN_ATOMIC_ADD_FETCH_N: | |
10858 | case BUILT_IN_ATOMIC_SUB_FETCH_N: | |
10859 | case BUILT_IN_ATOMIC_AND_FETCH_N: | |
10860 | case BUILT_IN_ATOMIC_NAND_FETCH_N: | |
10861 | case BUILT_IN_ATOMIC_XOR_FETCH_N: | |
10862 | case BUILT_IN_ATOMIC_OR_FETCH_N: | |
10863 | case BUILT_IN_ATOMIC_FETCH_ADD_N: | |
10864 | case BUILT_IN_ATOMIC_FETCH_SUB_N: | |
10865 | case BUILT_IN_ATOMIC_FETCH_AND_N: | |
10866 | case BUILT_IN_ATOMIC_FETCH_NAND_N: | |
10867 | case BUILT_IN_ATOMIC_FETCH_XOR_N: | |
10868 | case BUILT_IN_ATOMIC_FETCH_OR_N: | |
10869 | { | |
10870 | orig_format = false; | |
10871 | /* Fallthru for parameter processing. */ | |
10872 | } | |
2797f13a | 10873 | case BUILT_IN_SYNC_FETCH_AND_ADD_N: |
10874 | case BUILT_IN_SYNC_FETCH_AND_SUB_N: | |
10875 | case BUILT_IN_SYNC_FETCH_AND_OR_N: | |
10876 | case BUILT_IN_SYNC_FETCH_AND_AND_N: | |
10877 | case BUILT_IN_SYNC_FETCH_AND_XOR_N: | |
10878 | case BUILT_IN_SYNC_FETCH_AND_NAND_N: | |
10879 | case BUILT_IN_SYNC_ADD_AND_FETCH_N: | |
10880 | case BUILT_IN_SYNC_SUB_AND_FETCH_N: | |
10881 | case BUILT_IN_SYNC_OR_AND_FETCH_N: | |
10882 | case BUILT_IN_SYNC_AND_AND_FETCH_N: | |
10883 | case BUILT_IN_SYNC_XOR_AND_FETCH_N: | |
10884 | case BUILT_IN_SYNC_NAND_AND_FETCH_N: | |
10885 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N: | |
10886 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N: | |
10887 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N: | |
10888 | case BUILT_IN_SYNC_LOCK_RELEASE_N: | |
b6a5fc45 | 10889 | { |
10890 | int n = sync_resolve_size (function, params); | |
b9c74b4d | 10891 | tree new_function, first_param, result; |
b9a16870 | 10892 | enum built_in_function fncode; |
b6a5fc45 | 10893 | |
10894 | if (n == 0) | |
10895 | return error_mark_node; | |
10896 | ||
b9a16870 | 10897 | fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1); |
10898 | new_function = builtin_decl_explicit (fncode); | |
1cd6e20d | 10899 | if (!sync_resolve_params (loc, function, new_function, params, |
10900 | orig_format)) | |
b6a5fc45 | 10901 | return error_mark_node; |
10902 | ||
f1f41a6c | 10903 | first_param = (*params)[0]; |
ec761d5a | 10904 | result = build_function_call_vec (loc, vNULL, new_function, params, |
10905 | NULL); | |
1cd6e20d | 10906 | if (result == error_mark_node) |
10907 | return result; | |
2797f13a | 10908 | if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N |
1cd6e20d | 10909 | && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N |
10910 | && orig_code != BUILT_IN_ATOMIC_STORE_N) | |
10911 | result = sync_resolve_return (first_param, result, orig_format); | |
b6a5fc45 | 10912 | |
1cd6e20d | 10913 | /* If new_return is set, assign function to that expr and cast the |
10914 | result to void since the generic interface returned void. */ | |
10915 | if (new_return) | |
10916 | { | |
10917 | /* Cast function result from I{1,2,4,8,16} to the required type. */ | |
10918 | result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result); | |
10919 | result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return, | |
10920 | result); | |
10921 | TREE_SIDE_EFFECTS (result) = 1; | |
10922 | protected_set_expr_location (result, loc); | |
10923 | result = convert (void_type_node, result); | |
10924 | } | |
b6a5fc45 | 10925 | return result; |
10926 | } | |
10927 | ||
10928 | default: | |
65441f6f | 10929 | return NULL_TREE; |
b6a5fc45 | 10930 | } |
10931 | } | |
10932 | ||
73437615 | 10933 | /* vector_types_compatible_elements_p is used in type checks of vectors |
10934 | values used as operands of binary operators. Where it returns true, and | |
10935 | the other checks of the caller succeed (being vector types in he first | |
10936 | place, and matching number of elements), we can just treat the types | |
10937 | as essentially the same. | |
10938 | Contrast with vector_targets_convertible_p, which is used for vector | |
10939 | pointer types, and vector_types_convertible_p, which will allow | |
10940 | language-specific matches under the control of flag_lax_vector_conversions, | |
10941 | and might still require a conversion. */ | |
10942 | /* True if vector types T1 and T2 can be inputs to the same binary | |
10943 | operator without conversion. | |
10944 | We don't check the overall vector size here because some of our callers | |
10945 | want to give different error messages when the vectors are compatible | |
10946 | except for the element count. */ | |
10947 | ||
491255f5 | 10948 | bool |
73437615 | 10949 | vector_types_compatible_elements_p (tree t1, tree t2) |
491255f5 | 10950 | { |
73437615 | 10951 | bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2); |
10952 | t1 = TREE_TYPE (t1); | |
10953 | t2 = TREE_TYPE (t2); | |
10954 | ||
491255f5 | 10955 | enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); |
10956 | ||
9421ebb9 | 10957 | gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE) |
10958 | && (c2 == INTEGER_TYPE || c2 == REAL_TYPE | |
10959 | || c2 == FIXED_POINT_TYPE)); | |
491255f5 | 10960 | |
73437615 | 10961 | t1 = c_common_signed_type (t1); |
10962 | t2 = c_common_signed_type (t2); | |
491255f5 | 10963 | /* Equality works here because c_common_signed_type uses |
10964 | TYPE_MAIN_VARIANT. */ | |
73437615 | 10965 | if (t1 == t2) |
10966 | return true; | |
10967 | if (opaque && c1 == c2 | |
10968 | && (c1 == INTEGER_TYPE || c1 == REAL_TYPE) | |
10969 | && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) | |
10970 | return true; | |
10971 | return false; | |
491255f5 | 10972 | } |
10973 | ||
be7350e7 | 10974 | /* Check for missing format attributes on function pointers. LTYPE is |
10975 | the new type or left-hand side type. RTYPE is the old type or | |
10976 | right-hand side type. Returns TRUE if LTYPE is missing the desired | |
10977 | attribute. */ | |
10978 | ||
10979 | bool | |
10980 | check_missing_format_attribute (tree ltype, tree rtype) | |
10981 | { | |
10982 | tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype); | |
10983 | tree ra; | |
10984 | ||
10985 | for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra)) | |
10986 | if (is_attribute_p ("format", TREE_PURPOSE (ra))) | |
10987 | break; | |
10988 | if (ra) | |
10989 | { | |
10990 | tree la; | |
10991 | for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la)) | |
10992 | if (is_attribute_p ("format", TREE_PURPOSE (la))) | |
10993 | break; | |
10994 | return !la; | |
10995 | } | |
10996 | else | |
10997 | return false; | |
10998 | } | |
10999 | ||
2840aae4 | 11000 | /* Subscripting with type char is likely to lose on a machine where |
11001 | chars are signed. So warn on any machine, but optionally. Don't | |
11002 | warn for unsigned char since that type is safe. Don't warn for | |
11003 | signed char because anyone who uses that must have done so | |
11004 | deliberately. Furthermore, we reduce the false positive load by | |
11005 | warning only for non-constant value of type char. */ | |
11006 | ||
11007 | void | |
11008 | warn_array_subscript_with_type_char (tree index) | |
11009 | { | |
11010 | if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node | |
11011 | && TREE_CODE (index) != INTEGER_CST) | |
11012 | warning (OPT_Wchar_subscripts, "array subscript has type %<char%>"); | |
11013 | } | |
11014 | ||
e534436e | 11015 | /* Implement -Wparentheses for the unexpected C precedence rules, to |
11016 | cover cases like x + y << z which readers are likely to | |
11017 | misinterpret. We have seen an expression in which CODE is a binary | |
82012ffe | 11018 | operator used to combine expressions ARG_LEFT and ARG_RIGHT, which |
11019 | before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and | |
11020 | CODE_RIGHT may be ERROR_MARK, which means that that side of the | |
11021 | expression was not formed using a binary or unary operator, or it | |
11022 | was enclosed in parentheses. */ | |
e534436e | 11023 | |
11024 | void | |
b0e7825e | 11025 | warn_about_parentheses (location_t loc, enum tree_code code, |
269f7979 | 11026 | enum tree_code code_left, tree arg_left, |
82012ffe | 11027 | enum tree_code code_right, tree arg_right) |
e534436e | 11028 | { |
11029 | if (!warn_parentheses) | |
11030 | return; | |
11031 | ||
82012ffe | 11032 | /* This macro tests that the expression ARG with original tree code |
11033 | CODE appears to be a boolean expression. or the result of folding a | |
11034 | boolean expression. */ | |
11035 | #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ | |
11036 | (truth_value_p (TREE_CODE (ARG)) \ | |
11037 | || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ | |
11038 | /* Folding may create 0 or 1 integers from other expressions. */ \ | |
11039 | || ((CODE) != INTEGER_CST \ | |
11040 | && (integer_onep (ARG) || integer_zerop (ARG)))) | |
11041 | ||
48e1416a | 11042 | switch (code) |
e534436e | 11043 | { |
82012ffe | 11044 | case LSHIFT_EXPR: |
b0e7825e | 11045 | if (code_left == PLUS_EXPR) |
11046 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11047 | "suggest parentheses around %<+%> inside %<<<%>"); | |
11048 | else if (code_right == PLUS_EXPR) | |
11049 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11050 | "suggest parentheses around %<+%> inside %<<<%>"); | |
11051 | else if (code_left == MINUS_EXPR) | |
11052 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11053 | "suggest parentheses around %<-%> inside %<<<%>"); | |
11054 | else if (code_right == MINUS_EXPR) | |
11055 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11056 | "suggest parentheses around %<-%> inside %<<<%>"); | |
82012ffe | 11057 | return; |
e534436e | 11058 | |
82012ffe | 11059 | case RSHIFT_EXPR: |
b0e7825e | 11060 | if (code_left == PLUS_EXPR) |
11061 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11062 | "suggest parentheses around %<+%> inside %<>>%>"); | |
11063 | else if (code_right == PLUS_EXPR) | |
11064 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11065 | "suggest parentheses around %<+%> inside %<>>%>"); | |
11066 | else if (code_left == MINUS_EXPR) | |
11067 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11068 | "suggest parentheses around %<-%> inside %<>>%>"); | |
11069 | else if (code_right == MINUS_EXPR) | |
11070 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11071 | "suggest parentheses around %<-%> inside %<>>%>"); | |
82012ffe | 11072 | return; |
e534436e | 11073 | |
82012ffe | 11074 | case TRUTH_ORIF_EXPR: |
b0e7825e | 11075 | if (code_left == TRUTH_ANDIF_EXPR) |
11076 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11077 | "suggest parentheses around %<&&%> within %<||%>"); | |
11078 | else if (code_right == TRUTH_ANDIF_EXPR) | |
11079 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11080 | "suggest parentheses around %<&&%> within %<||%>"); | |
82012ffe | 11081 | return; |
11082 | ||
11083 | case BIT_IOR_EXPR: | |
e534436e | 11084 | if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR |
b0e7825e | 11085 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
11086 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11087 | "suggest parentheses around arithmetic in operand of %<|%>"); | |
11088 | else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR | |
11089 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
11090 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11091 | "suggest parentheses around arithmetic in operand of %<|%>"); |
e534436e | 11092 | /* Check cases like x|y==z */ |
b0e7825e | 11093 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11094 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11095 | "suggest parentheses around comparison in operand of %<|%>"); | |
11096 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11097 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11098 | "suggest parentheses around comparison in operand of %<|%>"); |
11099 | /* Check cases like !x | y */ | |
11100 | else if (code_left == TRUTH_NOT_EXPR | |
11101 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
b0e7825e | 11102 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11103 | "suggest parentheses around operand of " | |
11104 | "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>"); | |
82012ffe | 11105 | return; |
e534436e | 11106 | |
82012ffe | 11107 | case BIT_XOR_EXPR: |
e534436e | 11108 | if (code_left == BIT_AND_EXPR |
b0e7825e | 11109 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
11110 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11111 | "suggest parentheses around arithmetic in operand of %<^%>"); | |
11112 | else if (code_right == BIT_AND_EXPR | |
11113 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
11114 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11115 | "suggest parentheses around arithmetic in operand of %<^%>"); |
e534436e | 11116 | /* Check cases like x^y==z */ |
b0e7825e | 11117 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11118 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11119 | "suggest parentheses around comparison in operand of %<^%>"); | |
11120 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11121 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11122 | "suggest parentheses around comparison in operand of %<^%>"); |
11123 | return; | |
e534436e | 11124 | |
82012ffe | 11125 | case BIT_AND_EXPR: |
b0e7825e | 11126 | if (code_left == PLUS_EXPR) |
11127 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11128 | "suggest parentheses around %<+%> in operand of %<&%>"); | |
11129 | else if (code_right == PLUS_EXPR) | |
11130 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11131 | "suggest parentheses around %<+%> in operand of %<&%>"); |
b0e7825e | 11132 | else if (code_left == MINUS_EXPR) |
11133 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11134 | "suggest parentheses around %<-%> in operand of %<&%>"); | |
11135 | else if (code_right == MINUS_EXPR) | |
11136 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11137 | "suggest parentheses around %<-%> in operand of %<&%>"); |
e534436e | 11138 | /* Check cases like x&y==z */ |
b0e7825e | 11139 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11140 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11141 | "suggest parentheses around comparison in operand of %<&%>"); | |
11142 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11143 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11144 | "suggest parentheses around comparison in operand of %<&%>"); |
11145 | /* Check cases like !x & y */ | |
11146 | else if (code_left == TRUTH_NOT_EXPR | |
11147 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
b0e7825e | 11148 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11149 | "suggest parentheses around operand of " | |
11150 | "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>"); | |
82012ffe | 11151 | return; |
e534436e | 11152 | |
82012ffe | 11153 | case EQ_EXPR: |
b0e7825e | 11154 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11155 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11156 | "suggest parentheses around comparison in operand of %<==%>"); | |
11157 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11158 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11159 | "suggest parentheses around comparison in operand of %<==%>"); |
11160 | return; | |
11161 | case NE_EXPR: | |
b0e7825e | 11162 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11163 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11164 | "suggest parentheses around comparison in operand of %<!=%>"); | |
11165 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11166 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11167 | "suggest parentheses around comparison in operand of %<!=%>"); |
11168 | return; | |
11169 | ||
11170 | default: | |
b0e7825e | 11171 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
11172 | { | |
11173 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
269f7979 | 11174 | && code_left != NE_EXPR && code_left != EQ_EXPR |
11175 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) | |
b0e7825e | 11176 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11177 | "comparisons like %<X<=Y<=Z%> do not " | |
11178 | "have their mathematical meaning"); | |
11179 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison | |
269f7979 | 11180 | && code_right != NE_EXPR && code_right != EQ_EXPR |
b0e7825e | 11181 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))) |
11182 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11183 | "comparisons like %<X<=Y<=Z%> do not " | |
11184 | "have their mathematical meaning"); | |
11185 | } | |
82012ffe | 11186 | return; |
6ce0c450 | 11187 | } |
82012ffe | 11188 | #undef NOT_A_BOOLEAN_EXPR_P |
e534436e | 11189 | } |
11190 | ||
92fccaaa | 11191 | /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ |
11192 | ||
11193 | void | |
11194 | warn_for_unused_label (tree label) | |
11195 | { | |
11196 | if (!TREE_USED (label)) | |
11197 | { | |
11198 | if (DECL_INITIAL (label)) | |
11199 | warning (OPT_Wunused_label, "label %q+D defined but not used", label); | |
11200 | else | |
11201 | warning (OPT_Wunused_label, "label %q+D declared but not defined", label); | |
11202 | } | |
11203 | } | |
2840aae4 | 11204 | |
b6889cb0 | 11205 | /* Warn for division by zero according to the value of DIVISOR. LOC |
11206 | is the location of the division operator. */ | |
f092582b | 11207 | |
11208 | void | |
b6889cb0 | 11209 | warn_for_div_by_zero (location_t loc, tree divisor) |
f092582b | 11210 | { |
9421ebb9 | 11211 | /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning |
11212 | about division by zero. Do not issue a warning if DIVISOR has a | |
f092582b | 11213 | floating-point type, since we consider 0.0/0.0 a valid way of |
11214 | generating a NaN. */ | |
48d94ede | 11215 | if (c_inhibit_evaluation_warnings == 0 |
9421ebb9 | 11216 | && (integer_zerop (divisor) || fixed_zerop (divisor))) |
b6889cb0 | 11217 | warning_at (loc, OPT_Wdiv_by_zero, "division by zero"); |
f092582b | 11218 | } |
11219 | ||
13869a99 | 11220 | /* Subroutine of build_binary_op. Give warnings for comparisons |
11221 | between signed and unsigned quantities that may fail. Do the | |
11222 | checking based on the original operand trees ORIG_OP0 and ORIG_OP1, | |
11223 | so that casts will be considered, but default promotions won't | |
8e70fb09 | 11224 | be. |
11225 | ||
11226 | LOCATION is the location of the comparison operator. | |
13869a99 | 11227 | |
11228 | The arguments of this function map directly to local variables | |
11229 | of build_binary_op. */ | |
11230 | ||
48e1416a | 11231 | void |
8e70fb09 | 11232 | warn_for_sign_compare (location_t location, |
48e1416a | 11233 | tree orig_op0, tree orig_op1, |
11234 | tree op0, tree op1, | |
13869a99 | 11235 | tree result_type, enum tree_code resultcode) |
11236 | { | |
11237 | int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); | |
11238 | int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); | |
11239 | int unsignedp0, unsignedp1; | |
48e1416a | 11240 | |
13869a99 | 11241 | /* In C++, check for comparison of different enum types. */ |
11242 | if (c_dialect_cxx() | |
11243 | && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE | |
11244 | && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE | |
11245 | && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) | |
895b662f | 11246 | != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) |
13869a99 | 11247 | { |
8e70fb09 | 11248 | warning_at (location, |
11249 | OPT_Wsign_compare, "comparison between types %qT and %qT", | |
11250 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); | |
13869a99 | 11251 | } |
11252 | ||
11253 | /* Do not warn if the comparison is being done in a signed type, | |
11254 | since the signed type will only be chosen if it can represent | |
11255 | all the values of the unsigned type. */ | |
11256 | if (!TYPE_UNSIGNED (result_type)) | |
11257 | /* OK */; | |
11258 | /* Do not warn if both operands are unsigned. */ | |
11259 | else if (op0_signed == op1_signed) | |
11260 | /* OK */; | |
11261 | else | |
11262 | { | |
895b662f | 11263 | tree sop, uop, base_type; |
13869a99 | 11264 | bool ovf; |
895b662f | 11265 | |
13869a99 | 11266 | if (op0_signed) |
11267 | sop = orig_op0, uop = orig_op1; | |
48e1416a | 11268 | else |
13869a99 | 11269 | sop = orig_op1, uop = orig_op0; |
11270 | ||
48e1416a | 11271 | STRIP_TYPE_NOPS (sop); |
13869a99 | 11272 | STRIP_TYPE_NOPS (uop); |
895b662f | 11273 | base_type = (TREE_CODE (result_type) == COMPLEX_TYPE |
11274 | ? TREE_TYPE (result_type) : result_type); | |
13869a99 | 11275 | |
11276 | /* Do not warn if the signed quantity is an unsuffixed integer | |
11277 | literal (or some static constant expression involving such | |
11278 | literals or a conditional expression involving such literals) | |
11279 | and it is non-negative. */ | |
11280 | if (tree_expr_nonnegative_warnv_p (sop, &ovf)) | |
11281 | /* OK */; | |
11282 | /* Do not warn if the comparison is an equality operation, the | |
11283 | unsigned quantity is an integral constant, and it would fit | |
11284 | in the result if the result were signed. */ | |
11285 | else if (TREE_CODE (uop) == INTEGER_CST | |
11286 | && (resultcode == EQ_EXPR || resultcode == NE_EXPR) | |
895b662f | 11287 | && int_fits_type_p (uop, c_common_signed_type (base_type))) |
13869a99 | 11288 | /* OK */; |
11289 | /* In C, do not warn if the unsigned quantity is an enumeration | |
11290 | constant and its maximum value would fit in the result if the | |
11291 | result were signed. */ | |
11292 | else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST | |
11293 | && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE | |
11294 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), | |
895b662f | 11295 | c_common_signed_type (base_type))) |
13869a99 | 11296 | /* OK */; |
48e1416a | 11297 | else |
8e70fb09 | 11298 | warning_at (location, |
48e1416a | 11299 | OPT_Wsign_compare, |
8e70fb09 | 11300 | "comparison between signed and unsigned integer expressions"); |
13869a99 | 11301 | } |
48e1416a | 11302 | |
13869a99 | 11303 | /* Warn if two unsigned values are being compared in a size larger |
11304 | than their original size, and one (and only one) is the result of | |
11305 | a `~' operator. This comparison will always fail. | |
48e1416a | 11306 | |
13869a99 | 11307 | Also warn if one operand is a constant, and the constant does not |
11308 | have all bits set that are set in the ~ operand when it is | |
11309 | extended. */ | |
11310 | ||
7f506bca | 11311 | op0 = c_common_get_narrower (op0, &unsignedp0); |
11312 | op1 = c_common_get_narrower (op1, &unsignedp1); | |
48e1416a | 11313 | |
13869a99 | 11314 | if ((TREE_CODE (op0) == BIT_NOT_EXPR) |
11315 | ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) | |
11316 | { | |
11317 | if (TREE_CODE (op0) == BIT_NOT_EXPR) | |
7f506bca | 11318 | op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); |
13869a99 | 11319 | if (TREE_CODE (op1) == BIT_NOT_EXPR) |
7f506bca | 11320 | op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); |
13869a99 | 11321 | |
35ec552a | 11322 | if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1)) |
13869a99 | 11323 | { |
11324 | tree primop; | |
11325 | HOST_WIDE_INT constant, mask; | |
11326 | int unsignedp; | |
11327 | unsigned int bits; | |
48e1416a | 11328 | |
35ec552a | 11329 | if (tree_fits_shwi_p (op0)) |
13869a99 | 11330 | { |
11331 | primop = op1; | |
11332 | unsignedp = unsignedp1; | |
fcb97e84 | 11333 | constant = tree_to_shwi (op0); |
13869a99 | 11334 | } |
11335 | else | |
11336 | { | |
11337 | primop = op0; | |
11338 | unsignedp = unsignedp0; | |
fcb97e84 | 11339 | constant = tree_to_shwi (op1); |
13869a99 | 11340 | } |
48e1416a | 11341 | |
13869a99 | 11342 | bits = TYPE_PRECISION (TREE_TYPE (primop)); |
11343 | if (bits < TYPE_PRECISION (result_type) | |
11344 | && bits < HOST_BITS_PER_LONG && unsignedp) | |
11345 | { | |
11346 | mask = (~ (HOST_WIDE_INT) 0) << bits; | |
11347 | if ((mask & constant) != mask) | |
11348 | { | |
11349 | if (constant == 0) | |
76fdceeb | 11350 | warning_at (location, OPT_Wsign_compare, |
11351 | "promoted ~unsigned is always non-zero"); | |
13869a99 | 11352 | else |
48e1416a | 11353 | warning_at (location, OPT_Wsign_compare, |
8e70fb09 | 11354 | "comparison of promoted ~unsigned with constant"); |
13869a99 | 11355 | } |
11356 | } | |
11357 | } | |
11358 | else if (unsignedp0 && unsignedp1 | |
11359 | && (TYPE_PRECISION (TREE_TYPE (op0)) | |
11360 | < TYPE_PRECISION (result_type)) | |
11361 | && (TYPE_PRECISION (TREE_TYPE (op1)) | |
11362 | < TYPE_PRECISION (result_type))) | |
8e70fb09 | 11363 | warning_at (location, OPT_Wsign_compare, |
13869a99 | 11364 | "comparison of promoted ~unsigned with unsigned"); |
11365 | } | |
11366 | } | |
11367 | ||
5ba33bf4 | 11368 | /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common |
11369 | type via c_common_type. If -Wdouble-promotion is in use, and the | |
11370 | conditions for warning have been met, issue a warning. GMSGID is | |
11371 | the warning message. It must have two %T specifiers for the type | |
11372 | that was converted (generally "float") and the type to which it was | |
11373 | converted (generally "double), respectively. LOC is the location | |
11374 | to which the awrning should refer. */ | |
11375 | ||
11376 | void | |
11377 | do_warn_double_promotion (tree result_type, tree type1, tree type2, | |
11378 | const char *gmsgid, location_t loc) | |
11379 | { | |
11380 | tree source_type; | |
11381 | ||
11382 | if (!warn_double_promotion) | |
11383 | return; | |
11384 | /* If the conversion will not occur at run-time, there is no need to | |
11385 | warn about it. */ | |
11386 | if (c_inhibit_evaluation_warnings) | |
11387 | return; | |
11388 | if (TYPE_MAIN_VARIANT (result_type) != double_type_node | |
11389 | && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) | |
11390 | return; | |
11391 | if (TYPE_MAIN_VARIANT (type1) == float_type_node | |
11392 | || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) | |
11393 | source_type = type1; | |
11394 | else if (TYPE_MAIN_VARIANT (type2) == float_type_node | |
11395 | || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) | |
11396 | source_type = type2; | |
11397 | else | |
11398 | return; | |
11399 | warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); | |
11400 | } | |
11401 | ||
41771881 | 11402 | /* Setup a TYPE_DECL node as a typedef representation. |
11403 | ||
11404 | X is a TYPE_DECL for a typedef statement. Create a brand new | |
11405 | ..._TYPE node (which will be just a variant of the existing | |
11406 | ..._TYPE node with identical properties) and then install X | |
11407 | as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. | |
11408 | ||
11409 | The whole point here is to end up with a situation where each | |
11410 | and every ..._TYPE node the compiler creates will be uniquely | |
11411 | associated with AT MOST one node representing a typedef name. | |
11412 | This way, even though the compiler substitutes corresponding | |
11413 | ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very | |
11414 | early on, later parts of the compiler can always do the reverse | |
11415 | translation and get back the corresponding typedef name. For | |
11416 | example, given: | |
11417 | ||
11418 | typedef struct S MY_TYPE; | |
11419 | MY_TYPE object; | |
11420 | ||
11421 | Later parts of the compiler might only know that `object' was of | |
11422 | type `struct S' if it were not for code just below. With this | |
11423 | code however, later parts of the compiler see something like: | |
11424 | ||
11425 | struct S' == struct S | |
11426 | typedef struct S' MY_TYPE; | |
11427 | struct S' object; | |
11428 | ||
11429 | And they can then deduce (from the node for type struct S') that | |
11430 | the original object declaration was: | |
11431 | ||
11432 | MY_TYPE object; | |
11433 | ||
11434 | Being able to do this is important for proper support of protoize, | |
11435 | and also for generating precise symbolic debugging information | |
11436 | which takes full account of the programmer's (typedef) vocabulary. | |
11437 | ||
11438 | Obviously, we don't want to generate a duplicate ..._TYPE node if | |
11439 | the TYPE_DECL node that we are now processing really represents a | |
11440 | standard built-in type. */ | |
11441 | ||
11442 | void | |
11443 | set_underlying_type (tree x) | |
11444 | { | |
11445 | if (x == error_mark_node) | |
11446 | return; | |
11447 | if (DECL_IS_BUILTIN (x)) | |
11448 | { | |
11449 | if (TYPE_NAME (TREE_TYPE (x)) == 0) | |
11450 | TYPE_NAME (TREE_TYPE (x)) = x; | |
11451 | } | |
11452 | else if (TREE_TYPE (x) != error_mark_node | |
11453 | && DECL_ORIGINAL_TYPE (x) == NULL_TREE) | |
11454 | { | |
11455 | tree tt = TREE_TYPE (x); | |
11456 | DECL_ORIGINAL_TYPE (x) = tt; | |
11457 | tt = build_variant_type_copy (tt); | |
11458 | TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
11459 | TYPE_NAME (tt) = x; | |
11460 | TREE_USED (tt) = TREE_USED (x); | |
11461 | TREE_TYPE (x) = tt; | |
11462 | } | |
11463 | } | |
11464 | ||
1a4c44c5 | 11465 | /* Record the types used by the current global variable declaration |
11466 | being parsed, so that we can decide later to emit their debug info. | |
11467 | Those types are in types_used_by_cur_var_decl, and we are going to | |
11468 | store them in the types_used_by_vars_hash hash table. | |
11469 | DECL is the declaration of the global variable that has been parsed. */ | |
11470 | ||
11471 | void | |
11472 | record_types_used_by_current_var_decl (tree decl) | |
11473 | { | |
11474 | gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl)); | |
11475 | ||
f1f41a6c | 11476 | while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ()) |
1a4c44c5 | 11477 | { |
f1f41a6c | 11478 | tree type = types_used_by_cur_var_decl->pop (); |
aef48c9a | 11479 | types_used_by_var_decl_insert (type, decl); |
1a4c44c5 | 11480 | } |
11481 | } | |
11482 | ||
a4e3ffad | 11483 | /* If DECL is a typedef that is declared in the current function, |
11484 | record it for the purpose of -Wunused-local-typedefs. */ | |
11485 | ||
11486 | void | |
11487 | record_locally_defined_typedef (tree decl) | |
11488 | { | |
11489 | struct c_language_function *l; | |
11490 | ||
11491 | if (!warn_unused_local_typedefs | |
11492 | || cfun == NULL | |
11493 | /* if this is not a locally defined typedef then we are not | |
11494 | interested. */ | |
11495 | || !is_typedef_decl (decl) | |
11496 | || !decl_function_context (decl)) | |
11497 | return; | |
11498 | ||
11499 | l = (struct c_language_function *) cfun->language; | |
f1f41a6c | 11500 | vec_safe_push (l->local_typedefs, decl); |
a4e3ffad | 11501 | } |
11502 | ||
11503 | /* If T is a TYPE_DECL declared locally, mark it as used. */ | |
11504 | ||
11505 | void | |
11506 | maybe_record_typedef_use (tree t) | |
11507 | { | |
11508 | if (!is_typedef_decl (t)) | |
11509 | return; | |
11510 | ||
11511 | TREE_USED (t) = true; | |
11512 | } | |
11513 | ||
11514 | /* Warn if there are some unused locally defined typedefs in the | |
11515 | current function. */ | |
11516 | ||
11517 | void | |
11518 | maybe_warn_unused_local_typedefs (void) | |
11519 | { | |
11520 | int i; | |
11521 | tree decl; | |
11522 | /* The number of times we have emitted -Wunused-local-typedefs | |
11523 | warnings. If this is different from errorcount, that means some | |
11524 | unrelated errors have been issued. In which case, we'll avoid | |
11525 | emitting "unused-local-typedefs" warnings. */ | |
11526 | static int unused_local_typedefs_warn_count; | |
11527 | struct c_language_function *l; | |
11528 | ||
11529 | if (cfun == NULL) | |
11530 | return; | |
11531 | ||
11532 | if ((l = (struct c_language_function *) cfun->language) == NULL) | |
11533 | return; | |
11534 | ||
11535 | if (warn_unused_local_typedefs | |
11536 | && errorcount == unused_local_typedefs_warn_count) | |
11537 | { | |
f1f41a6c | 11538 | FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl) |
a4e3ffad | 11539 | if (!TREE_USED (decl)) |
11540 | warning_at (DECL_SOURCE_LOCATION (decl), | |
11541 | OPT_Wunused_local_typedefs, | |
11542 | "typedef %qD locally defined but not used", decl); | |
11543 | unused_local_typedefs_warn_count = errorcount; | |
11544 | } | |
11545 | ||
f1f41a6c | 11546 | vec_free (l->local_typedefs); |
a4e3ffad | 11547 | } |
11548 | ||
f352a3fb | 11549 | /* The C and C++ parsers both use vectors to hold function arguments. |
11550 | For efficiency, we keep a cache of unused vectors. This is the | |
11551 | cache. */ | |
11552 | ||
f1f41a6c | 11553 | typedef vec<tree, va_gc> *tree_gc_vec; |
11554 | static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache; | |
f352a3fb | 11555 | |
11556 | /* Return a new vector from the cache. If the cache is empty, | |
11557 | allocate a new vector. These vectors are GC'ed, so it is OK if the | |
11558 | pointer is not released.. */ | |
11559 | ||
f1f41a6c | 11560 | vec<tree, va_gc> * |
f352a3fb | 11561 | make_tree_vector (void) |
11562 | { | |
f1f41a6c | 11563 | if (tree_vector_cache && !tree_vector_cache->is_empty ()) |
11564 | return tree_vector_cache->pop (); | |
f352a3fb | 11565 | else |
11566 | { | |
f1f41a6c | 11567 | /* Passing 0 to vec::alloc returns NULL, and our callers require |
f352a3fb | 11568 | that we always return a non-NULL value. The vector code uses |
11569 | 4 when growing a NULL vector, so we do too. */ | |
f1f41a6c | 11570 | vec<tree, va_gc> *v; |
11571 | vec_alloc (v, 4); | |
11572 | return v; | |
f352a3fb | 11573 | } |
11574 | } | |
11575 | ||
11576 | /* Release a vector of trees back to the cache. */ | |
11577 | ||
11578 | void | |
f1f41a6c | 11579 | release_tree_vector (vec<tree, va_gc> *vec) |
f352a3fb | 11580 | { |
11581 | if (vec != NULL) | |
11582 | { | |
f1f41a6c | 11583 | vec->truncate (0); |
11584 | vec_safe_push (tree_vector_cache, vec); | |
f352a3fb | 11585 | } |
11586 | } | |
11587 | ||
11588 | /* Get a new tree vector holding a single tree. */ | |
11589 | ||
f1f41a6c | 11590 | vec<tree, va_gc> * |
f352a3fb | 11591 | make_tree_vector_single (tree t) |
11592 | { | |
f1f41a6c | 11593 | vec<tree, va_gc> *ret = make_tree_vector (); |
11594 | ret->quick_push (t); | |
f352a3fb | 11595 | return ret; |
11596 | } | |
11597 | ||
c66c81be | 11598 | /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */ |
11599 | ||
f1f41a6c | 11600 | vec<tree, va_gc> * |
c66c81be | 11601 | make_tree_vector_from_list (tree list) |
11602 | { | |
f1f41a6c | 11603 | vec<tree, va_gc> *ret = make_tree_vector (); |
c66c81be | 11604 | for (; list; list = TREE_CHAIN (list)) |
f1f41a6c | 11605 | vec_safe_push (ret, TREE_VALUE (list)); |
c66c81be | 11606 | return ret; |
11607 | } | |
11608 | ||
f352a3fb | 11609 | /* Get a new tree vector which is a copy of an existing one. */ |
11610 | ||
f1f41a6c | 11611 | vec<tree, va_gc> * |
11612 | make_tree_vector_copy (const vec<tree, va_gc> *orig) | |
f352a3fb | 11613 | { |
f1f41a6c | 11614 | vec<tree, va_gc> *ret; |
f352a3fb | 11615 | unsigned int ix; |
11616 | tree t; | |
11617 | ||
11618 | ret = make_tree_vector (); | |
f1f41a6c | 11619 | vec_safe_reserve (ret, vec_safe_length (orig)); |
11620 | FOR_EACH_VEC_SAFE_ELT (orig, ix, t) | |
11621 | ret->quick_push (t); | |
f352a3fb | 11622 | return ret; |
11623 | } | |
11624 | ||
a9ffdd35 | 11625 | /* Return true if KEYWORD starts a type specifier. */ |
11626 | ||
11627 | bool | |
11628 | keyword_begins_type_specifier (enum rid keyword) | |
11629 | { | |
11630 | switch (keyword) | |
11631 | { | |
4fba5eb9 | 11632 | case RID_AUTO_TYPE: |
a9ffdd35 | 11633 | case RID_INT: |
11634 | case RID_CHAR: | |
11635 | case RID_FLOAT: | |
11636 | case RID_DOUBLE: | |
11637 | case RID_VOID: | |
11638 | case RID_INT128: | |
11639 | case RID_UNSIGNED: | |
11640 | case RID_LONG: | |
11641 | case RID_SHORT: | |
11642 | case RID_SIGNED: | |
11643 | case RID_DFLOAT32: | |
11644 | case RID_DFLOAT64: | |
11645 | case RID_DFLOAT128: | |
11646 | case RID_FRACT: | |
11647 | case RID_ACCUM: | |
11648 | case RID_BOOL: | |
11649 | case RID_WCHAR: | |
11650 | case RID_CHAR16: | |
11651 | case RID_CHAR32: | |
11652 | case RID_SAT: | |
11653 | case RID_COMPLEX: | |
11654 | case RID_TYPEOF: | |
11655 | case RID_STRUCT: | |
11656 | case RID_CLASS: | |
11657 | case RID_UNION: | |
11658 | case RID_ENUM: | |
11659 | return true; | |
11660 | default: | |
11661 | return false; | |
11662 | } | |
11663 | } | |
11664 | ||
11665 | /* Return true if KEYWORD names a type qualifier. */ | |
11666 | ||
11667 | bool | |
11668 | keyword_is_type_qualifier (enum rid keyword) | |
11669 | { | |
11670 | switch (keyword) | |
11671 | { | |
11672 | case RID_CONST: | |
11673 | case RID_VOLATILE: | |
11674 | case RID_RESTRICT: | |
b560fabd | 11675 | case RID_ATOMIC: |
a9ffdd35 | 11676 | return true; |
11677 | default: | |
11678 | return false; | |
11679 | } | |
11680 | } | |
11681 | ||
11682 | /* Return true if KEYWORD names a storage class specifier. | |
11683 | ||
11684 | RID_TYPEDEF is not included in this list despite `typedef' being | |
11685 | listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as | |
11686 | such for syntactic convenience only. */ | |
11687 | ||
11688 | bool | |
11689 | keyword_is_storage_class_specifier (enum rid keyword) | |
11690 | { | |
11691 | switch (keyword) | |
11692 | { | |
11693 | case RID_STATIC: | |
11694 | case RID_EXTERN: | |
11695 | case RID_REGISTER: | |
11696 | case RID_AUTO: | |
11697 | case RID_MUTABLE: | |
11698 | case RID_THREAD: | |
11699 | return true; | |
11700 | default: | |
11701 | return false; | |
11702 | } | |
11703 | } | |
11704 | ||
fad3f658 | 11705 | /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */ |
11706 | ||
11707 | static bool | |
11708 | keyword_is_function_specifier (enum rid keyword) | |
11709 | { | |
11710 | switch (keyword) | |
11711 | { | |
11712 | case RID_INLINE: | |
985c6e3a | 11713 | case RID_NORETURN: |
fad3f658 | 11714 | case RID_VIRTUAL: |
11715 | case RID_EXPLICIT: | |
11716 | return true; | |
11717 | default: | |
11718 | return false; | |
11719 | } | |
11720 | } | |
11721 | ||
11722 | /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a | |
11723 | declaration-specifier (C99 6.7). */ | |
11724 | ||
11725 | bool | |
11726 | keyword_is_decl_specifier (enum rid keyword) | |
11727 | { | |
11728 | if (keyword_is_storage_class_specifier (keyword) | |
11729 | || keyword_is_type_qualifier (keyword) | |
11730 | || keyword_is_function_specifier (keyword)) | |
11731 | return true; | |
11732 | ||
11733 | switch (keyword) | |
11734 | { | |
11735 | case RID_TYPEDEF: | |
11736 | case RID_FRIEND: | |
11737 | case RID_CONSTEXPR: | |
11738 | return true; | |
11739 | default: | |
11740 | return false; | |
11741 | } | |
11742 | } | |
11743 | ||
9b88d08d | 11744 | /* Initialize language-specific-bits of tree_contains_struct. */ |
11745 | ||
11746 | void | |
11747 | c_common_init_ts (void) | |
11748 | { | |
11749 | MARK_TS_TYPED (C_MAYBE_CONST_EXPR); | |
11750 | MARK_TS_TYPED (EXCESS_PRECISION_EXPR); | |
3c6d4197 | 11751 | MARK_TS_TYPED (ARRAY_NOTATION_REF); |
9b88d08d | 11752 | } |
11753 | ||
244db24d | 11754 | /* Build a user-defined numeric literal out of an integer constant type VALUE |
11755 | with identifier SUFFIX. */ | |
11756 | ||
11757 | tree | |
324ca377 | 11758 | build_userdef_literal (tree suffix_id, tree value, |
11759 | enum overflow_type overflow, tree num_string) | |
244db24d | 11760 | { |
11761 | tree literal = make_node (USERDEF_LITERAL); | |
11762 | USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id; | |
11763 | USERDEF_LITERAL_VALUE (literal) = value; | |
324ca377 | 11764 | USERDEF_LITERAL_OVERFLOW (literal) = overflow; |
244db24d | 11765 | USERDEF_LITERAL_NUM_STRING (literal) = num_string; |
11766 | return literal; | |
11767 | } | |
11768 | ||
7059d45d | 11769 | /* For vector[index], convert the vector to a |
11770 | pointer of the underlying type. */ | |
11771 | void | |
11772 | convert_vector_to_pointer_for_subscript (location_t loc, | |
11773 | tree* vecp, tree index) | |
11774 | { | |
11775 | if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE) | |
11776 | { | |
11777 | tree type = TREE_TYPE (*vecp); | |
11778 | tree type1; | |
11779 | ||
11780 | if (TREE_CODE (index) == INTEGER_CST) | |
cd4547bf | 11781 | if (!tree_fits_uhwi_p (index) |
aa59f000 | 11782 | || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type)) |
7059d45d | 11783 | warning_at (loc, OPT_Warray_bounds, "index value is out of bound"); |
11784 | ||
11785 | c_common_mark_addressable_vec (*vecp); | |
11786 | type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); | |
11787 | type = build_pointer_type (type); | |
11788 | type1 = build_pointer_type (TREE_TYPE (*vecp)); | |
11789 | *vecp = build1 (ADDR_EXPR, type1, *vecp); | |
11790 | *vecp = convert (type, *vecp); | |
11791 | } | |
11792 | } | |
11793 | ||
41ed701a | 11794 | /* Determine which of the operands, if any, is a scalar that needs to be |
11795 | converted to a vector, for the range of operations. */ | |
11796 | enum stv_conv | |
11797 | scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1, | |
11798 | bool complain) | |
11799 | { | |
11800 | tree type0 = TREE_TYPE (op0); | |
11801 | tree type1 = TREE_TYPE (op1); | |
11802 | bool integer_only_op = false; | |
11803 | enum stv_conv ret = stv_firstarg; | |
11804 | ||
11805 | gcc_assert (TREE_CODE (type0) == VECTOR_TYPE | |
11806 | || TREE_CODE (type1) == VECTOR_TYPE); | |
11807 | switch (code) | |
11808 | { | |
11809 | /* Most GENERIC binary expressions require homogeneous arguments. | |
11810 | LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first | |
11811 | argument that is a vector and a second one that is a scalar, so | |
11812 | we never return stv_secondarg for them. */ | |
11813 | case RSHIFT_EXPR: | |
11814 | case LSHIFT_EXPR: | |
11815 | if (TREE_CODE (type0) == INTEGER_TYPE | |
11816 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
11817 | { | |
22a75734 | 11818 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
41ed701a | 11819 | { |
11820 | if (complain) | |
11821 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
11822 | "involves truncation", type0, type1); | |
11823 | return stv_error; | |
11824 | } | |
11825 | else | |
11826 | return stv_firstarg; | |
11827 | } | |
11828 | break; | |
11829 | ||
11830 | case BIT_IOR_EXPR: | |
11831 | case BIT_XOR_EXPR: | |
11832 | case BIT_AND_EXPR: | |
11833 | integer_only_op = true; | |
11834 | /* ... fall through ... */ | |
11835 | ||
7b463b19 | 11836 | case VEC_COND_EXPR: |
11837 | ||
41ed701a | 11838 | case PLUS_EXPR: |
11839 | case MINUS_EXPR: | |
11840 | case MULT_EXPR: | |
11841 | case TRUNC_DIV_EXPR: | |
11842 | case CEIL_DIV_EXPR: | |
11843 | case FLOOR_DIV_EXPR: | |
11844 | case ROUND_DIV_EXPR: | |
11845 | case EXACT_DIV_EXPR: | |
11846 | case TRUNC_MOD_EXPR: | |
11847 | case FLOOR_MOD_EXPR: | |
11848 | case RDIV_EXPR: | |
11849 | case EQ_EXPR: | |
11850 | case NE_EXPR: | |
11851 | case LE_EXPR: | |
11852 | case GE_EXPR: | |
11853 | case LT_EXPR: | |
11854 | case GT_EXPR: | |
11855 | /* What about UNLT_EXPR? */ | |
11856 | if (TREE_CODE (type0) == VECTOR_TYPE) | |
11857 | { | |
11858 | tree tmp; | |
11859 | ret = stv_secondarg; | |
11860 | /* Swap TYPE0 with TYPE1 and OP0 with OP1 */ | |
11861 | tmp = type0; type0 = type1; type1 = tmp; | |
11862 | tmp = op0; op0 = op1; op1 = tmp; | |
11863 | } | |
11864 | ||
11865 | if (TREE_CODE (type0) == INTEGER_TYPE | |
11866 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
11867 | { | |
22a75734 | 11868 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
41ed701a | 11869 | { |
11870 | if (complain) | |
11871 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
11872 | "involves truncation", type0, type1); | |
11873 | return stv_error; | |
11874 | } | |
11875 | return ret; | |
11876 | } | |
11877 | else if (!integer_only_op | |
11878 | /* Allow integer --> real conversion if safe. */ | |
11879 | && (TREE_CODE (type0) == REAL_TYPE | |
11880 | || TREE_CODE (type0) == INTEGER_TYPE) | |
11881 | && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1))) | |
11882 | { | |
22a75734 | 11883 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
41ed701a | 11884 | { |
11885 | if (complain) | |
11886 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
11887 | "involves truncation", type0, type1); | |
11888 | return stv_error; | |
11889 | } | |
11890 | return ret; | |
11891 | } | |
11892 | default: | |
11893 | break; | |
11894 | } | |
11895 | ||
11896 | return stv_nothing; | |
11897 | } | |
11898 | ||
ffcdbf9c | 11899 | /* Return true iff ALIGN is an integral constant that is a fundamental |
11900 | alignment, as defined by [basic.align] in the c++-11 | |
11901 | specifications. | |
11902 | ||
11903 | That is: | |
11904 | ||
11905 | [A fundamental alignment is represented by an alignment less than or | |
11906 | equal to the greatest alignment supported by the implementation | |
11907 | in all contexts, which is equal to | |
11908 | alignof(max_align_t)]. */ | |
11909 | ||
11910 | bool | |
11911 | cxx_fundamental_alignment_p (unsigned align) | |
11912 | { | |
11913 | return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node), | |
11914 | TYPE_ALIGN (long_double_type_node))); | |
11915 | } | |
11916 | ||
46da3601 | 11917 | /* Return true if T is a pointer to a zero-sized aggregate. */ |
11918 | ||
11919 | bool | |
11920 | pointer_to_zero_sized_aggr_p (tree t) | |
11921 | { | |
11922 | if (!POINTER_TYPE_P (t)) | |
11923 | return false; | |
11924 | t = TREE_TYPE (t); | |
11925 | return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t))); | |
11926 | } | |
11927 | ||
7bedc3a0 | 11928 | #include "gt-c-family-c-common.h" |