]>
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 *); |
24470055 | 369 | static tree handle_target_attribute (tree *, tree, tree, int, bool *); |
46f8e3b0 | 370 | static tree handle_optimize_attribute (tree *, tree, tree, int, bool *); |
4c0315d0 | 371 | static tree ignore_attribute (tree *, tree, tree, int, bool *); |
48b14f50 | 372 | static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *); |
8ce86007 | 373 | static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *); |
a96c3cc1 | 374 | static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *); |
d7dcba40 | 375 | static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *); |
bc7bff74 | 376 | static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int, |
377 | bool *); | |
378 | static tree handle_omp_declare_target_attribute (tree *, tree, tree, int, | |
379 | bool *); | |
1cae46be | 380 | |
d01f58f9 | 381 | static void check_function_nonnull (tree, int, tree *); |
1cae46be | 382 | static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); |
383 | static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT); | |
384 | static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); | |
860251be | 385 | static int resort_field_decl_cmp (const void *, const void *); |
dbf6c367 | 386 | |
5c6e5756 | 387 | /* Reserved words. The third field is a mask: keywords are disabled |
388 | if they match the mask. | |
389 | ||
390 | Masks for languages: | |
391 | C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC | |
392 | C --std=c99: D_CXXONLY | D_OBJC | |
393 | ObjC is like C except that D_OBJC and D_CXX_OBJC are not set | |
394 | C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | |
395 | C++ --std=c0x: D_CONLY | D_OBJC | |
396 | ObjC++ is like C++ except that D_OBJC is not set | |
397 | ||
398 | If -fno-asm is used, D_ASM is added to the mask. If | |
399 | -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in | |
400 | C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords. | |
b27e241e | 401 | In C with -Wc++-compat, we warn if D_CXXWARN is set. |
5c6e5756 | 402 | |
b27e241e | 403 | Note the complication of the D_CXX_OBJC keywords. These are |
404 | reserved words such as 'class'. In C++, 'class' is a reserved | |
405 | word. In Objective-C++ it is too. In Objective-C, it is a | |
406 | reserved word too, but only if it follows an '@' sign. | |
407 | */ | |
5c6e5756 | 408 | const struct c_common_resword c_common_reswords[] = |
409 | { | |
83e25171 | 410 | { "_Alignas", RID_ALIGNAS, D_CONLY }, |
411 | { "_Alignof", RID_ALIGNOF, D_CONLY }, | |
b560fabd | 412 | { "_Atomic", RID_ATOMIC, D_CONLY }, |
5c6e5756 | 413 | { "_Bool", RID_BOOL, D_CONLY }, |
414 | { "_Complex", RID_COMPLEX, 0 }, | |
d037099f | 415 | { "_Cilk_spawn", RID_CILK_SPAWN, 0 }, |
416 | { "_Cilk_sync", RID_CILK_SYNC, 0 }, | |
c1800156 | 417 | { "_Imaginary", RID_IMAGINARY, D_CONLY }, |
5c6e5756 | 418 | { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT }, |
419 | { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT }, | |
420 | { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT }, | |
421 | { "_Fract", RID_FRACT, D_CONLY | D_EXT }, | |
422 | { "_Accum", RID_ACCUM, D_CONLY | D_EXT }, | |
423 | { "_Sat", RID_SAT, D_CONLY | D_EXT }, | |
f80e7755 | 424 | { "_Static_assert", RID_STATIC_ASSERT, D_CONLY }, |
985c6e3a | 425 | { "_Noreturn", RID_NORETURN, D_CONLY }, |
7aa04c8d | 426 | { "_Generic", RID_GENERIC, D_CONLY }, |
d184e0c0 | 427 | { "_Thread_local", RID_THREAD, D_CONLY }, |
5c6e5756 | 428 | { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, |
429 | { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, | |
430 | { "__alignof", RID_ALIGNOF, 0 }, | |
431 | { "__alignof__", RID_ALIGNOF, 0 }, | |
432 | { "__asm", RID_ASM, 0 }, | |
433 | { "__asm__", RID_ASM, 0 }, | |
434 | { "__attribute", RID_ATTRIBUTE, 0 }, | |
435 | { "__attribute__", RID_ATTRIBUTE, 0 }, | |
4fba5eb9 | 436 | { "__auto_type", RID_AUTO_TYPE, D_CONLY }, |
e6014a82 | 437 | { "__bases", RID_BASES, D_CXXONLY }, |
5c6e5756 | 438 | { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, |
bff4ad11 | 439 | { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY }, |
bf0cb017 | 440 | { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 }, |
5c6e5756 | 441 | { "__builtin_offsetof", RID_OFFSETOF, 0 }, |
442 | { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, | |
443 | { "__builtin_va_arg", RID_VA_ARG, 0 }, | |
444 | { "__complex", RID_COMPLEX, 0 }, | |
445 | { "__complex__", RID_COMPLEX, 0 }, | |
446 | { "__const", RID_CONST, 0 }, | |
447 | { "__const__", RID_CONST, 0 }, | |
448 | { "__decltype", RID_DECLTYPE, D_CXXONLY }, | |
e6014a82 | 449 | { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY }, |
5c6e5756 | 450 | { "__extension__", RID_EXTENSION, 0 }, |
451 | { "__func__", RID_C99_FUNCTION_NAME, 0 }, | |
452 | { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY }, | |
453 | { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY }, | |
454 | { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY }, | |
455 | { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY }, | |
456 | { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY }, | |
457 | { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY }, | |
458 | { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY }, | |
459 | { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY }, | |
23407dc9 | 460 | { "__imag", RID_IMAGPART, 0 }, |
461 | { "__imag__", RID_IMAGPART, 0 }, | |
462 | { "__inline", RID_INLINE, 0 }, | |
463 | { "__inline__", RID_INLINE, 0 }, | |
6388cfe2 | 464 | { "__int128", RID_INT128, 0 }, |
5c6e5756 | 465 | { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY }, |
466 | { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY }, | |
467 | { "__is_class", RID_IS_CLASS, D_CXXONLY }, | |
468 | { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY }, | |
469 | { "__is_empty", RID_IS_EMPTY, D_CXXONLY }, | |
470 | { "__is_enum", RID_IS_ENUM, D_CXXONLY }, | |
aa4313eb | 471 | { "__is_final", RID_IS_FINAL, D_CXXONLY }, |
23407dc9 | 472 | { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY }, |
5c6e5756 | 473 | { "__is_pod", RID_IS_POD, D_CXXONLY }, |
474 | { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY }, | |
c1c67b4f | 475 | { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY }, |
476 | { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY }, | |
5c6e5756 | 477 | { "__is_union", RID_IS_UNION, D_CXXONLY }, |
5c6e5756 | 478 | { "__label__", RID_LABEL, 0 }, |
479 | { "__null", RID_NULL, 0 }, | |
480 | { "__real", RID_REALPART, 0 }, | |
481 | { "__real__", RID_REALPART, 0 }, | |
482 | { "__restrict", RID_RESTRICT, 0 }, | |
483 | { "__restrict__", RID_RESTRICT, 0 }, | |
484 | { "__signed", RID_SIGNED, 0 }, | |
485 | { "__signed__", RID_SIGNED, 0 }, | |
486 | { "__thread", RID_THREAD, 0 }, | |
4c0315d0 | 487 | { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 }, |
488 | { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 }, | |
489 | { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 }, | |
5c6e5756 | 490 | { "__typeof", RID_TYPEOF, 0 }, |
491 | { "__typeof__", RID_TYPEOF, 0 }, | |
23407dc9 | 492 | { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY }, |
5c6e5756 | 493 | { "__volatile", RID_VOLATILE, 0 }, |
494 | { "__volatile__", RID_VOLATILE, 0 }, | |
ffcdbf9c | 495 | { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
e463efd7 | 496 | { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 497 | { "asm", RID_ASM, D_ASM }, |
498 | { "auto", RID_AUTO, 0 }, | |
dbd982c9 | 499 | { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 500 | { "break", RID_BREAK, 0 }, |
501 | { "case", RID_CASE, 0 }, | |
51030405 | 502 | { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN }, |
5c6e5756 | 503 | { "char", RID_CHAR, 0 }, |
51030405 | 504 | { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
505 | { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
506 | { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 507 | { "const", RID_CONST, 0 }, |
17814aca | 508 | { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 509 | { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN }, |
510 | { "continue", RID_CONTINUE, 0 }, | |
51030405 | 511 | { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 512 | { "default", RID_DEFAULT, 0 }, |
51030405 | 513 | { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 514 | { "do", RID_DO, 0 }, |
515 | { "double", RID_DOUBLE, 0 }, | |
516 | { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN }, | |
517 | { "else", RID_ELSE, 0 }, | |
518 | { "enum", RID_ENUM, 0 }, | |
51030405 | 519 | { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN }, |
520 | { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 521 | { "extern", RID_EXTERN, 0 }, |
51030405 | 522 | { "false", RID_FALSE, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 523 | { "float", RID_FLOAT, 0 }, |
524 | { "for", RID_FOR, 0 }, | |
51030405 | 525 | { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 526 | { "goto", RID_GOTO, 0 }, |
527 | { "if", RID_IF, 0 }, | |
528 | { "inline", RID_INLINE, D_EXT89 }, | |
529 | { "int", RID_INT, 0 }, | |
530 | { "long", RID_LONG, 0 }, | |
531 | { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN }, | |
51030405 | 532 | { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN }, |
533 | { "new", RID_NEW, D_CXXONLY | D_CXXWARN }, | |
98fe9664 | 534 | { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
6fe11077 | 535 | { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
51030405 | 536 | { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN }, |
537 | { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN }, | |
538 | { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN }, | |
539 | { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 540 | { "register", RID_REGISTER, 0 }, |
541 | { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN }, | |
542 | { "restrict", RID_RESTRICT, D_CONLY | D_C99 }, | |
543 | { "return", RID_RETURN, 0 }, | |
544 | { "short", RID_SHORT, 0 }, | |
545 | { "signed", RID_SIGNED, 0 }, | |
546 | { "sizeof", RID_SIZEOF, 0 }, | |
547 | { "static", RID_STATIC, 0 }, | |
548 | { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
549 | { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN }, | |
550 | { "struct", RID_STRUCT, 0 }, | |
551 | { "switch", RID_SWITCH, 0 }, | |
51030405 | 552 | { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN }, |
553 | { "this", RID_THIS, D_CXXONLY | D_CXXWARN }, | |
3740094c | 554 | { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
51030405 | 555 | { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN }, |
556 | { "true", RID_TRUE, D_CXXONLY | D_CXXWARN }, | |
557 | { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 558 | { "typedef", RID_TYPEDEF, 0 }, |
51030405 | 559 | { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN }, |
560 | { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 561 | { "typeof", RID_TYPEOF, D_ASM | D_EXT }, |
562 | { "union", RID_UNION, 0 }, | |
563 | { "unsigned", RID_UNSIGNED, 0 }, | |
51030405 | 564 | { "using", RID_USING, D_CXXONLY | D_CXXWARN }, |
565 | { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 566 | { "void", RID_VOID, 0 }, |
567 | { "volatile", RID_VOLATILE, 0 }, | |
568 | { "wchar_t", RID_WCHAR, D_CXXONLY }, | |
569 | { "while", RID_WHILE, 0 }, | |
570 | /* These Objective-C keywords are recognized only immediately after | |
571 | an '@'. */ | |
572 | { "compatibility_alias", RID_AT_ALIAS, D_OBJC }, | |
573 | { "defs", RID_AT_DEFS, D_OBJC }, | |
574 | { "encode", RID_AT_ENCODE, D_OBJC }, | |
575 | { "end", RID_AT_END, D_OBJC }, | |
576 | { "implementation", RID_AT_IMPLEMENTATION, D_OBJC }, | |
577 | { "interface", RID_AT_INTERFACE, D_OBJC }, | |
578 | { "protocol", RID_AT_PROTOCOL, D_OBJC }, | |
579 | { "selector", RID_AT_SELECTOR, D_OBJC }, | |
580 | { "finally", RID_AT_FINALLY, D_OBJC }, | |
581 | { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC }, | |
069761fb | 582 | { "optional", RID_AT_OPTIONAL, D_OBJC }, |
583 | { "required", RID_AT_REQUIRED, D_OBJC }, | |
86c110ac | 584 | { "property", RID_AT_PROPERTY, D_OBJC }, |
4a8875ed | 585 | { "package", RID_AT_PACKAGE, D_OBJC }, |
e1f293c0 | 586 | { "synthesize", RID_AT_SYNTHESIZE, D_OBJC }, |
587 | { "dynamic", RID_AT_DYNAMIC, D_OBJC }, | |
5c6e5756 | 588 | /* These are recognized only in protocol-qualifier context |
589 | (see above) */ | |
590 | { "bycopy", RID_BYCOPY, D_OBJC }, | |
591 | { "byref", RID_BYREF, D_OBJC }, | |
592 | { "in", RID_IN, D_OBJC }, | |
593 | { "inout", RID_INOUT, D_OBJC }, | |
594 | { "oneway", RID_ONEWAY, D_OBJC }, | |
595 | { "out", RID_OUT, D_OBJC }, | |
86c110ac | 596 | /* These are recognized inside a property attribute list */ |
7590f0e5 | 597 | { "assign", RID_ASSIGN, D_OBJC }, |
598 | { "copy", RID_COPY, D_OBJC }, | |
86c110ac | 599 | { "getter", RID_GETTER, D_OBJC }, |
7590f0e5 | 600 | { "nonatomic", RID_NONATOMIC, D_OBJC }, |
601 | { "readonly", RID_READONLY, D_OBJC }, | |
602 | { "readwrite", RID_READWRITE, D_OBJC }, | |
603 | { "retain", RID_RETAIN, D_OBJC }, | |
86c110ac | 604 | { "setter", RID_SETTER, D_OBJC }, |
5c6e5756 | 605 | }; |
606 | ||
607 | const unsigned int num_c_common_reswords = | |
608 | sizeof c_common_reswords / sizeof (struct c_common_resword); | |
609 | ||
f8e93a2e | 610 | /* Table of machine-independent attributes common to all C-like languages. */ |
611 | const struct attribute_spec c_common_attribute_table[] = | |
612 | { | |
ac86af5d | 613 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
614 | affects_type_identity } */ | |
f8e93a2e | 615 | { "packed", 0, 0, false, false, false, |
ac86af5d | 616 | handle_packed_attribute , false}, |
f8e93a2e | 617 | { "nocommon", 0, 0, true, false, false, |
ac86af5d | 618 | handle_nocommon_attribute, false}, |
f8e93a2e | 619 | { "common", 0, 0, true, false, false, |
ac86af5d | 620 | handle_common_attribute, false }, |
f8e93a2e | 621 | /* FIXME: logically, noreturn attributes should be listed as |
622 | "false, true, true" and apply to function types. But implementing this | |
623 | would require all the places in the compiler that use TREE_THIS_VOLATILE | |
624 | on a decl to identify non-returning functions to be located and fixed | |
625 | to check the function type instead. */ | |
626 | { "noreturn", 0, 0, true, false, false, | |
ac86af5d | 627 | handle_noreturn_attribute, false }, |
f8e93a2e | 628 | { "volatile", 0, 0, true, false, false, |
ac86af5d | 629 | handle_noreturn_attribute, false }, |
f8e93a2e | 630 | { "noinline", 0, 0, true, false, false, |
ac86af5d | 631 | handle_noinline_attribute, false }, |
bdb1f0d1 | 632 | { "noclone", 0, 0, true, false, false, |
ac86af5d | 633 | handle_noclone_attribute, false }, |
7bd95dfd | 634 | { "leaf", 0, 0, true, false, false, |
ac86af5d | 635 | handle_leaf_attribute, false }, |
f8e93a2e | 636 | { "always_inline", 0, 0, true, false, false, |
ac86af5d | 637 | handle_always_inline_attribute, false }, |
541e4101 | 638 | { "gnu_inline", 0, 0, true, false, false, |
ac86af5d | 639 | handle_gnu_inline_attribute, false }, |
1b16fc45 | 640 | { "artificial", 0, 0, true, false, false, |
ac86af5d | 641 | handle_artificial_attribute, false }, |
0cdd9887 | 642 | { "flatten", 0, 0, true, false, false, |
ac86af5d | 643 | handle_flatten_attribute, false }, |
f8e93a2e | 644 | { "used", 0, 0, true, false, false, |
ac86af5d | 645 | handle_used_attribute, false }, |
f8e93a2e | 646 | { "unused", 0, 0, false, false, false, |
ac86af5d | 647 | handle_unused_attribute, false }, |
62eec3b4 | 648 | { "externally_visible", 0, 0, true, false, false, |
ac86af5d | 649 | handle_externally_visible_attribute, false }, |
f8e93a2e | 650 | /* The same comments as for noreturn attributes apply to const ones. */ |
651 | { "const", 0, 0, true, false, false, | |
ac86af5d | 652 | handle_const_attribute, false }, |
f8e93a2e | 653 | { "transparent_union", 0, 0, false, false, false, |
ac86af5d | 654 | handle_transparent_union_attribute, false }, |
9af7fd5b | 655 | { "constructor", 0, 1, true, false, false, |
ac86af5d | 656 | handle_constructor_attribute, false }, |
9af7fd5b | 657 | { "destructor", 0, 1, true, false, false, |
ac86af5d | 658 | handle_destructor_attribute, false }, |
f8e93a2e | 659 | { "mode", 1, 1, false, true, false, |
ac86af5d | 660 | handle_mode_attribute, false }, |
f8e93a2e | 661 | { "section", 1, 1, true, false, false, |
ac86af5d | 662 | handle_section_attribute, false }, |
f8e93a2e | 663 | { "aligned", 0, 1, false, false, false, |
ac86af5d | 664 | handle_aligned_attribute, false }, |
f8e93a2e | 665 | { "weak", 0, 0, true, false, false, |
ac86af5d | 666 | handle_weak_attribute, false }, |
85c0a25c | 667 | { "ifunc", 1, 1, true, false, false, |
ac86af5d | 668 | handle_ifunc_attribute, false }, |
f8e93a2e | 669 | { "alias", 1, 1, true, false, false, |
ac86af5d | 670 | handle_alias_attribute, false }, |
f4a30bd7 | 671 | { "weakref", 0, 1, true, false, false, |
ac86af5d | 672 | handle_weakref_attribute, false }, |
f8e93a2e | 673 | { "no_instrument_function", 0, 0, true, false, false, |
ac86af5d | 674 | handle_no_instrument_function_attribute, |
675 | false }, | |
f8e93a2e | 676 | { "malloc", 0, 0, true, false, false, |
ac86af5d | 677 | handle_malloc_attribute, false }, |
26d1c5ff | 678 | { "returns_twice", 0, 0, true, false, false, |
ac86af5d | 679 | handle_returns_twice_attribute, false }, |
f8e93a2e | 680 | { "no_stack_limit", 0, 0, true, false, false, |
ac86af5d | 681 | handle_no_limit_stack_attribute, false }, |
f8e93a2e | 682 | { "pure", 0, 0, true, false, false, |
ac86af5d | 683 | handle_pure_attribute, false }, |
4c0315d0 | 684 | { "transaction_callable", 0, 0, false, true, false, |
685 | handle_tm_attribute, false }, | |
686 | { "transaction_unsafe", 0, 0, false, true, false, | |
687 | handle_tm_attribute, false }, | |
688 | { "transaction_safe", 0, 0, false, true, false, | |
689 | handle_tm_attribute, false }, | |
690 | { "transaction_may_cancel_outer", 0, 0, false, true, false, | |
691 | handle_tm_attribute, false }, | |
692 | /* ??? These two attributes didn't make the transition from the | |
693 | Intel language document to the multi-vendor language document. */ | |
694 | { "transaction_pure", 0, 0, false, true, false, | |
695 | handle_tm_attribute, false }, | |
696 | { "transaction_wrap", 1, 1, true, false, false, | |
697 | handle_tm_wrap_attribute, false }, | |
fc09b200 | 698 | /* For internal use (marking of builtins) only. The name contains space |
699 | to prevent its usage in source code. */ | |
700 | { "no vops", 0, 0, true, false, false, | |
ac86af5d | 701 | handle_novops_attribute, false }, |
45c4e798 | 702 | { "deprecated", 0, 1, false, false, false, |
ac86af5d | 703 | handle_deprecated_attribute, false }, |
f8e93a2e | 704 | { "vector_size", 1, 1, false, true, false, |
ac86af5d | 705 | handle_vector_size_attribute, false }, |
b212f378 | 706 | { "visibility", 1, 1, false, false, false, |
ac86af5d | 707 | handle_visibility_attribute, false }, |
24dfead4 | 708 | { "tls_model", 1, 1, true, false, false, |
ac86af5d | 709 | handle_tls_model_attribute, false }, |
dbf6c367 | 710 | { "nonnull", 0, -1, false, true, true, |
ac86af5d | 711 | handle_nonnull_attribute, false }, |
fa987697 | 712 | { "nothrow", 0, 0, true, false, false, |
ac86af5d | 713 | handle_nothrow_attribute, false }, |
714 | { "may_alias", 0, 0, false, true, false, NULL, false }, | |
7acb29a3 | 715 | { "cleanup", 1, 1, true, false, false, |
ac86af5d | 716 | handle_cleanup_attribute, false }, |
8a8cdb8d | 717 | { "warn_unused_result", 0, 0, false, true, true, |
ac86af5d | 718 | handle_warn_unused_result_attribute, false }, |
50ca527f | 719 | { "sentinel", 0, 1, false, true, true, |
ac86af5d | 720 | handle_sentinel_attribute, false }, |
b5c26b42 | 721 | /* For internal use (marking of builtins) only. The name contains space |
722 | to prevent its usage in source code. */ | |
723 | { "type generic", 0, 0, false, true, true, | |
ac86af5d | 724 | handle_type_generic_attribute, false }, |
4a29c97c | 725 | { "alloc_size", 1, 2, false, true, true, |
ac86af5d | 726 | handle_alloc_size_attribute, false }, |
5de92639 | 727 | { "cold", 0, 0, true, false, false, |
ac86af5d | 728 | handle_cold_attribute, false }, |
5de92639 | 729 | { "hot", 0, 0, true, false, false, |
ac86af5d | 730 | handle_hot_attribute, false }, |
d413ffdd | 731 | { "no_address_safety_analysis", |
732 | 0, 0, true, false, false, | |
733 | handle_no_address_safety_analysis_attribute, | |
734 | false }, | |
a9196da9 | 735 | { "no_sanitize_address", 0, 0, true, false, false, |
736 | handle_no_sanitize_address_attribute, | |
737 | false }, | |
05f893e1 | 738 | { "no_sanitize_undefined", 0, 0, true, false, false, |
739 | handle_no_sanitize_undefined_attribute, | |
740 | false }, | |
10fc867f | 741 | { "warning", 1, 1, true, false, false, |
ac86af5d | 742 | handle_error_attribute, false }, |
10fc867f | 743 | { "error", 1, 1, true, false, false, |
ac86af5d | 744 | handle_error_attribute, false }, |
24470055 | 745 | { "target", 1, -1, true, false, false, |
ac86af5d | 746 | handle_target_attribute, false }, |
46f8e3b0 | 747 | { "optimize", 1, -1, true, false, false, |
ac86af5d | 748 | handle_optimize_attribute, false }, |
4c0315d0 | 749 | /* For internal use only. The leading '*' both prevents its usage in |
750 | source code and signals that it may be overridden by machine tables. */ | |
751 | { "*tm regparm", 0, 0, false, true, true, | |
752 | ignore_attribute, false }, | |
48b14f50 | 753 | { "no_split_stack", 0, 0, true, false, false, |
ac86af5d | 754 | handle_no_split_stack_attribute, false }, |
8ce86007 | 755 | /* For internal use (marking of builtins and runtime functions) only. |
756 | The name contains space to prevent its usage in source code. */ | |
757 | { "fn spec", 1, 1, false, true, true, | |
ac86af5d | 758 | handle_fnspec_attribute, false }, |
a96c3cc1 | 759 | { "warn_unused", 0, 0, false, false, false, |
760 | handle_warn_unused_attribute, false }, | |
d7dcba40 | 761 | { "returns_nonnull", 0, 0, false, true, true, |
762 | handle_returns_nonnull_attribute, false }, | |
bc7bff74 | 763 | { "omp declare simd", 0, -1, true, false, false, |
764 | handle_omp_declare_simd_attribute, false }, | |
74acc703 | 765 | { "cilk simd function", 0, -1, true, false, false, |
766 | handle_omp_declare_simd_attribute, false }, | |
bc7bff74 | 767 | { "omp declare target", 0, 0, true, false, false, |
768 | handle_omp_declare_target_attribute, false }, | |
ac86af5d | 769 | { NULL, 0, 0, false, false, false, NULL, false } |
f8e93a2e | 770 | }; |
771 | ||
772 | /* Give the specifications for the format attributes, used by C and all | |
d716ce75 | 773 | descendants. */ |
f8e93a2e | 774 | |
775 | const struct attribute_spec c_common_format_attribute_table[] = | |
776 | { | |
ac86af5d | 777 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
778 | affects_type_identity } */ | |
f8e93a2e | 779 | { "format", 3, 3, false, true, true, |
ac86af5d | 780 | handle_format_attribute, false }, |
f8e93a2e | 781 | { "format_arg", 1, 1, false, true, true, |
ac86af5d | 782 | handle_format_arg_attribute, false }, |
783 | { NULL, 0, 0, false, false, false, NULL, false } | |
f8e93a2e | 784 | }; |
785 | ||
6d5d708e | 786 | /* Return identifier for address space AS. */ |
34208e18 | 787 | |
6d5d708e | 788 | const char * |
789 | c_addr_space_name (addr_space_t as) | |
790 | { | |
34208e18 | 791 | int rid = RID_FIRST_ADDR_SPACE + as; |
792 | gcc_assert (ridpointers [rid]); | |
793 | return IDENTIFIER_POINTER (ridpointers [rid]); | |
6d5d708e | 794 | } |
795 | ||
2c0e001b | 796 | /* Push current bindings for the function name VAR_DECLS. */ |
f4e3c278 | 797 | |
798 | void | |
1cae46be | 799 | start_fname_decls (void) |
f4e3c278 | 800 | { |
65b7f83f | 801 | unsigned ix; |
802 | tree saved = NULL_TREE; | |
1cae46be | 803 | |
65b7f83f | 804 | for (ix = 0; fname_vars[ix].decl; ix++) |
805 | { | |
806 | tree decl = *fname_vars[ix].decl; | |
f4e3c278 | 807 | |
65b7f83f | 808 | if (decl) |
809 | { | |
ceb7b692 | 810 | saved = tree_cons (decl, build_int_cst (integer_type_node, ix), |
811 | saved); | |
65b7f83f | 812 | *fname_vars[ix].decl = NULL_TREE; |
813 | } | |
814 | } | |
815 | if (saved || saved_function_name_decls) | |
816 | /* Normally they'll have been NULL, so only push if we've got a | |
817 | stack, or they are non-NULL. */ | |
818 | saved_function_name_decls = tree_cons (saved, NULL_TREE, | |
819 | saved_function_name_decls); | |
820 | } | |
821 | ||
2363ef00 | 822 | /* Finish up the current bindings, adding them into the current function's |
823 | statement tree. This must be done _before_ finish_stmt_tree is called. | |
824 | If there is no current function, we must be at file scope and no statements | |
825 | are involved. Pop the previous bindings. */ | |
65b7f83f | 826 | |
827 | void | |
1cae46be | 828 | finish_fname_decls (void) |
65b7f83f | 829 | { |
830 | unsigned ix; | |
2363ef00 | 831 | tree stmts = NULL_TREE; |
65b7f83f | 832 | tree stack = saved_function_name_decls; |
833 | ||
834 | for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack)) | |
2363ef00 | 835 | append_to_statement_list (TREE_VALUE (stack), &stmts); |
1cae46be | 836 | |
2363ef00 | 837 | if (stmts) |
65b7f83f | 838 | { |
2363ef00 | 839 | tree *bodyp = &DECL_SAVED_TREE (current_function_decl); |
5c423bd6 | 840 | |
2363ef00 | 841 | if (TREE_CODE (*bodyp) == BIND_EXPR) |
842 | bodyp = &BIND_EXPR_BODY (*bodyp); | |
81010c97 | 843 | |
bc2b76e0 | 844 | append_to_statement_list_force (*bodyp, &stmts); |
2363ef00 | 845 | *bodyp = stmts; |
65b7f83f | 846 | } |
1cae46be | 847 | |
65b7f83f | 848 | for (ix = 0; fname_vars[ix].decl; ix++) |
849 | *fname_vars[ix].decl = NULL_TREE; | |
1cae46be | 850 | |
65b7f83f | 851 | if (stack) |
f4e3c278 | 852 | { |
2c0e001b | 853 | /* We had saved values, restore them. */ |
65b7f83f | 854 | tree saved; |
855 | ||
856 | for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved)) | |
857 | { | |
858 | tree decl = TREE_PURPOSE (saved); | |
859 | unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved)); | |
1cae46be | 860 | |
65b7f83f | 861 | *fname_vars[ix].decl = decl; |
862 | } | |
863 | stack = TREE_CHAIN (stack); | |
f4e3c278 | 864 | } |
65b7f83f | 865 | saved_function_name_decls = stack; |
866 | } | |
867 | ||
81010c97 | 868 | /* Return the text name of the current function, suitably prettified |
5fc7fa69 | 869 | by PRETTY_P. Return string must be freed by caller. */ |
65b7f83f | 870 | |
871 | const char * | |
1cae46be | 872 | fname_as_string (int pretty_p) |
65b7f83f | 873 | { |
9ad4bb1e | 874 | const char *name = "top level"; |
5fc7fa69 | 875 | char *namep; |
8115b8be | 876 | int vrb = 2, len; |
877 | cpp_string cstr = { 0, 0 }, strname; | |
9ad4bb1e | 878 | |
84166705 | 879 | if (!pretty_p) |
9ad4bb1e | 880 | { |
881 | name = ""; | |
882 | vrb = 0; | |
883 | } | |
884 | ||
885 | if (current_function_decl) | |
dc24ddbd | 886 | name = lang_hooks.decl_printable_name (current_function_decl, vrb); |
9ad4bb1e | 887 | |
8115b8be | 888 | len = strlen (name) + 3; /* Two for '"'s. One for NULL. */ |
5fc7fa69 | 889 | |
8115b8be | 890 | namep = XNEWVEC (char, len); |
891 | snprintf (namep, len, "\"%s\"", name); | |
892 | strname.text = (unsigned char *) namep; | |
893 | strname.len = len - 1; | |
5fc7fa69 | 894 | |
924bbf02 | 895 | if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING)) |
8115b8be | 896 | { |
897 | XDELETEVEC (namep); | |
898 | return (const char *) cstr.text; | |
5fc7fa69 | 899 | } |
5fc7fa69 | 900 | |
901 | return namep; | |
65b7f83f | 902 | } |
903 | ||
65b7f83f | 904 | /* Return the VAR_DECL for a const char array naming the current |
905 | function. If the VAR_DECL has not yet been created, create it | |
906 | now. RID indicates how it should be formatted and IDENTIFIER_NODE | |
907 | ID is its name (unfortunately C and C++ hold the RID values of | |
908 | keywords in different places, so we can't derive RID from ID in | |
e3b80d49 | 909 | this language independent code. LOC is the location of the |
910 | function. */ | |
65b7f83f | 911 | |
912 | tree | |
e3b80d49 | 913 | fname_decl (location_t loc, unsigned int rid, tree id) |
65b7f83f | 914 | { |
915 | unsigned ix; | |
916 | tree decl = NULL_TREE; | |
917 | ||
918 | for (ix = 0; fname_vars[ix].decl; ix++) | |
919 | if (fname_vars[ix].rid == rid) | |
920 | break; | |
921 | ||
922 | decl = *fname_vars[ix].decl; | |
923 | if (!decl) | |
f4e3c278 | 924 | { |
2222b3c6 | 925 | /* If a tree is built here, it would normally have the lineno of |
926 | the current statement. Later this tree will be moved to the | |
927 | beginning of the function and this line number will be wrong. | |
928 | To avoid this problem set the lineno to 0 here; that prevents | |
7299020b | 929 | it from appearing in the RTL. */ |
2363ef00 | 930 | tree stmts; |
9a6486a6 | 931 | location_t saved_location = input_location; |
9a6486a6 | 932 | input_location = UNKNOWN_LOCATION; |
1cae46be | 933 | |
2363ef00 | 934 | stmts = push_stmt_list (); |
e60a6f7b | 935 | decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty); |
2363ef00 | 936 | stmts = pop_stmt_list (stmts); |
937 | if (!IS_EMPTY_STMT (stmts)) | |
938 | saved_function_name_decls | |
939 | = tree_cons (decl, stmts, saved_function_name_decls); | |
65b7f83f | 940 | *fname_vars[ix].decl = decl; |
9a6486a6 | 941 | input_location = saved_location; |
f4e3c278 | 942 | } |
65b7f83f | 943 | if (!ix && !current_function_decl) |
e3b80d49 | 944 | pedwarn (loc, 0, "%qD is not defined outside of function scope", decl); |
81010c97 | 945 | |
65b7f83f | 946 | return decl; |
f4e3c278 | 947 | } |
948 | ||
070236f0 | 949 | /* Given a STRING_CST, give it a suitable array-of-chars data type. */ |
b0fc3e72 | 950 | |
951 | tree | |
1cae46be | 952 | fix_string_type (tree value) |
b0fc3e72 | 953 | { |
070236f0 | 954 | int length = TREE_STRING_LENGTH (value); |
955 | int nchars; | |
00d26680 | 956 | tree e_type, i_type, a_type; |
957 | ||
73be5127 | 958 | /* Compute the number of elements, for the array type. */ |
924bbf02 | 959 | if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value)) |
960 | { | |
961 | nchars = length; | |
962 | e_type = char_type_node; | |
963 | } | |
964 | else if (TREE_TYPE (value) == char16_array_type_node) | |
965 | { | |
966 | nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT); | |
967 | e_type = char16_type_node; | |
968 | } | |
969 | else if (TREE_TYPE (value) == char32_array_type_node) | |
970 | { | |
971 | nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT); | |
972 | e_type = char32_type_node; | |
973 | } | |
974 | else | |
975 | { | |
976 | nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT); | |
977 | e_type = wchar_type_node; | |
978 | } | |
b0fc3e72 | 979 | |
1d752508 | 980 | /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous |
981 | limit in C++98 Annex B is very large (65536) and is not normative, | |
982 | so we do not diagnose it (warn_overlength_strings is forced off | |
983 | in c_common_post_options). */ | |
984 | if (warn_overlength_strings) | |
985 | { | |
986 | const int nchars_max = flag_isoc99 ? 4095 : 509; | |
987 | const int relevant_std = flag_isoc99 ? 99 : 90; | |
988 | if (nchars - 1 > nchars_max) | |
989 | /* Translators: The %d after 'ISO C' will be 90 or 99. Do not | |
990 | separate the %d from the 'C'. 'ISO' should not be | |
991 | translated, but it may be moved after 'C%d' in languages | |
992 | where modifiers follow nouns. */ | |
21ca8540 | 993 | pedwarn (input_location, OPT_Woverlength_strings, |
8864917d | 994 | "string length %qd is greater than the length %qd " |
1d752508 | 995 | "ISO C%d compilers are required to support", |
996 | nchars - 1, nchars_max, relevant_std); | |
997 | } | |
82cfc7f7 | 998 | |
390be14e | 999 | /* Create the array type for the string constant. The ISO C++ |
1000 | standard says that a string literal has type `const char[N]' or | |
1001 | `const wchar_t[N]'. We use the same logic when invoked as a C | |
1002 | front-end with -Wwrite-strings. | |
1003 | ??? We should change the type of an expression depending on the | |
1004 | state of a warning flag. We should just be warning -- see how | |
1005 | this is handled in the C++ front-end for the deprecated implicit | |
1006 | conversion from string literals to `char*' or `wchar_t*'. | |
00d26680 | 1007 | |
1008 | The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified | |
1009 | array type being the unqualified version of that type. | |
1010 | Therefore, if we are constructing an array of const char, we must | |
1011 | construct the matching unqualified array type first. The C front | |
1012 | end does not require this, but it does no harm, so we do it | |
1013 | unconditionally. */ | |
ceb7b692 | 1014 | i_type = build_index_type (size_int (nchars - 1)); |
00d26680 | 1015 | a_type = build_array_type (e_type, i_type); |
390be14e | 1016 | if (c_dialect_cxx() || warn_write_strings) |
aebc8537 | 1017 | a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST); |
3a10ba35 | 1018 | |
00d26680 | 1019 | TREE_TYPE (value) = a_type; |
b8e3b7ad | 1020 | TREE_CONSTANT (value) = 1; |
a814bad5 | 1021 | TREE_READONLY (value) = 1; |
b0fc3e72 | 1022 | TREE_STATIC (value) = 1; |
1023 | return value; | |
1024 | } | |
1025 | \f | |
fc501191 | 1026 | /* If DISABLE is true, stop issuing warnings. This is used when |
1027 | parsing code that we know will not be executed. This function may | |
1028 | be called multiple times, and works as a stack. */ | |
1029 | ||
1030 | static void | |
1031 | c_disable_warnings (bool disable) | |
1032 | { | |
1033 | if (disable) | |
1034 | { | |
1035 | ++c_inhibit_evaluation_warnings; | |
1036 | fold_defer_overflow_warnings (); | |
1037 | } | |
1038 | } | |
1039 | ||
1040 | /* If ENABLE is true, reenable issuing warnings. */ | |
1041 | ||
1042 | static void | |
1043 | c_enable_warnings (bool enable) | |
1044 | { | |
1045 | if (enable) | |
1046 | { | |
1047 | --c_inhibit_evaluation_warnings; | |
1048 | fold_undefer_and_ignore_overflow_warnings (); | |
1049 | } | |
1050 | } | |
1051 | ||
a75b1c71 | 1052 | /* Fully fold EXPR, an expression that was not folded (beyond integer |
1053 | constant expressions and null pointer constants) when being built | |
1054 | up. If IN_INIT, this is in a static initializer and certain | |
1055 | changes are made to the folding done. Clear *MAYBE_CONST if | |
1056 | MAYBE_CONST is not NULL and EXPR is definitely not a constant | |
1057 | expression because it contains an evaluated operator (in C99) or an | |
1058 | operator outside of sizeof returning an integer constant (in C90) | |
1059 | not permitted in constant expressions, or because it contains an | |
1060 | evaluated arithmetic overflow. (*MAYBE_CONST should typically be | |
1061 | set to true by callers before calling this function.) Return the | |
1062 | folded expression. Function arguments have already been folded | |
1063 | before calling this function, as have the contents of SAVE_EXPR, | |
1064 | TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and | |
1065 | C_MAYBE_CONST_EXPR. */ | |
1066 | ||
1067 | tree | |
1068 | c_fully_fold (tree expr, bool in_init, bool *maybe_const) | |
1069 | { | |
1070 | tree ret; | |
c6418a4e | 1071 | tree eptype = NULL_TREE; |
a75b1c71 | 1072 | bool dummy = true; |
1073 | bool maybe_const_itself = true; | |
389dd41b | 1074 | location_t loc = EXPR_LOCATION (expr); |
a75b1c71 | 1075 | |
1076 | /* This function is not relevant to C++ because C++ folds while | |
1077 | parsing, and may need changes to be correct for C++ when C++ | |
1078 | stops folding while parsing. */ | |
1079 | if (c_dialect_cxx ()) | |
1080 | gcc_unreachable (); | |
1081 | ||
1082 | if (!maybe_const) | |
1083 | maybe_const = &dummy; | |
c6418a4e | 1084 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) |
1085 | { | |
1086 | eptype = TREE_TYPE (expr); | |
1087 | expr = TREE_OPERAND (expr, 0); | |
1088 | } | |
a75b1c71 | 1089 | ret = c_fully_fold_internal (expr, in_init, maybe_const, |
1090 | &maybe_const_itself); | |
c6418a4e | 1091 | if (eptype) |
389dd41b | 1092 | ret = fold_convert_loc (loc, eptype, ret); |
a75b1c71 | 1093 | *maybe_const &= maybe_const_itself; |
1094 | return ret; | |
1095 | } | |
1096 | ||
1097 | /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for | |
1098 | c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands | |
1099 | not permitted, while *MAYBE_CONST_ITSELF is cleared because of | |
1100 | arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from | |
1101 | both evaluated and unevaluated subexpressions while | |
1102 | *MAYBE_CONST_ITSELF is carried from only evaluated | |
1103 | subexpressions). */ | |
1104 | ||
1105 | static tree | |
1106 | c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands, | |
1107 | bool *maybe_const_itself) | |
1108 | { | |
1109 | tree ret = expr; | |
1110 | enum tree_code code = TREE_CODE (expr); | |
1111 | enum tree_code_class kind = TREE_CODE_CLASS (code); | |
1112 | location_t loc = EXPR_LOCATION (expr); | |
1113 | tree op0, op1, op2, op3; | |
1114 | tree orig_op0, orig_op1, orig_op2; | |
1115 | bool op0_const = true, op1_const = true, op2_const = true; | |
1116 | bool op0_const_self = true, op1_const_self = true, op2_const_self = true; | |
1117 | bool nowarning = TREE_NO_WARNING (expr); | |
fc501191 | 1118 | bool unused_p; |
a75b1c71 | 1119 | |
1120 | /* This function is not relevant to C++ because C++ folds while | |
1121 | parsing, and may need changes to be correct for C++ when C++ | |
1122 | stops folding while parsing. */ | |
1123 | if (c_dialect_cxx ()) | |
1124 | gcc_unreachable (); | |
1125 | ||
1126 | /* Constants, declarations, statements, errors, SAVE_EXPRs and | |
1127 | anything else not counted as an expression cannot usefully be | |
1128 | folded further at this point. */ | |
1129 | if (!IS_EXPR_CODE_CLASS (kind) | |
1130 | || kind == tcc_statement | |
1131 | || code == SAVE_EXPR) | |
1132 | return expr; | |
1133 | ||
1134 | /* Operands of variable-length expressions (function calls) have | |
1135 | already been folded, as have __builtin_* function calls, and such | |
1136 | expressions cannot occur in constant expressions. */ | |
1137 | if (kind == tcc_vl_exp) | |
1138 | { | |
1139 | *maybe_const_operands = false; | |
1140 | ret = fold (expr); | |
1141 | goto out; | |
1142 | } | |
1143 | ||
1144 | if (code == C_MAYBE_CONST_EXPR) | |
1145 | { | |
1146 | tree pre = C_MAYBE_CONST_EXPR_PRE (expr); | |
1147 | tree inner = C_MAYBE_CONST_EXPR_EXPR (expr); | |
1148 | if (C_MAYBE_CONST_EXPR_NON_CONST (expr)) | |
1149 | *maybe_const_operands = false; | |
1150 | if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr)) | |
1151 | *maybe_const_itself = false; | |
1152 | if (pre && !in_init) | |
1153 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner); | |
1154 | else | |
1155 | ret = inner; | |
1156 | goto out; | |
1157 | } | |
1158 | ||
1159 | /* Assignment, increment, decrement, function call and comma | |
1160 | operators, and statement expressions, cannot occur in constant | |
1161 | expressions if evaluated / outside of sizeof. (Function calls | |
1162 | were handled above, though VA_ARG_EXPR is treated like a function | |
1163 | call here, and statement expressions are handled through | |
1164 | C_MAYBE_CONST_EXPR to avoid folding inside them.) */ | |
1165 | switch (code) | |
1166 | { | |
1167 | case MODIFY_EXPR: | |
1168 | case PREDECREMENT_EXPR: | |
1169 | case PREINCREMENT_EXPR: | |
1170 | case POSTDECREMENT_EXPR: | |
1171 | case POSTINCREMENT_EXPR: | |
1172 | case COMPOUND_EXPR: | |
1173 | *maybe_const_operands = false; | |
1174 | break; | |
1175 | ||
1176 | case VA_ARG_EXPR: | |
1177 | case TARGET_EXPR: | |
1178 | case BIND_EXPR: | |
1179 | case OBJ_TYPE_REF: | |
1180 | *maybe_const_operands = false; | |
1181 | ret = fold (expr); | |
1182 | goto out; | |
1183 | ||
1184 | default: | |
1185 | break; | |
1186 | } | |
1187 | ||
1188 | /* Fold individual tree codes as appropriate. */ | |
1189 | switch (code) | |
1190 | { | |
1191 | case COMPOUND_LITERAL_EXPR: | |
1192 | /* Any non-constancy will have been marked in a containing | |
1193 | C_MAYBE_CONST_EXPR; there is no more folding to do here. */ | |
1194 | goto out; | |
1195 | ||
1196 | case COMPONENT_REF: | |
1197 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1198 | op1 = TREE_OPERAND (expr, 1); | |
1199 | op2 = TREE_OPERAND (expr, 2); | |
1200 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1201 | maybe_const_itself); | |
f59e3889 | 1202 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1203 | if (op0 != orig_op0) |
1204 | ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2); | |
1205 | if (ret != expr) | |
1206 | { | |
1207 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1208 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1209 | } | |
1210 | goto out; | |
1211 | ||
1212 | case ARRAY_REF: | |
1213 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1214 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1215 | op2 = TREE_OPERAND (expr, 2); | |
1216 | op3 = TREE_OPERAND (expr, 3); | |
1217 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1218 | maybe_const_itself); | |
f59e3889 | 1219 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1220 | op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, |
1221 | maybe_const_itself); | |
f59e3889 | 1222 | STRIP_TYPE_NOPS (op1); |
a75b1c71 | 1223 | op1 = decl_constant_value_for_optimization (op1); |
1224 | if (op0 != orig_op0 || op1 != orig_op1) | |
1225 | ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3); | |
1226 | if (ret != expr) | |
1227 | { | |
1228 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1229 | TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); | |
1230 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1231 | } | |
1232 | ret = fold (ret); | |
1233 | goto out; | |
1234 | ||
1235 | case COMPOUND_EXPR: | |
1236 | case MODIFY_EXPR: | |
1237 | case PREDECREMENT_EXPR: | |
1238 | case PREINCREMENT_EXPR: | |
1239 | case POSTDECREMENT_EXPR: | |
1240 | case POSTINCREMENT_EXPR: | |
1241 | case PLUS_EXPR: | |
1242 | case MINUS_EXPR: | |
1243 | case MULT_EXPR: | |
1244 | case POINTER_PLUS_EXPR: | |
1245 | case TRUNC_DIV_EXPR: | |
1246 | case CEIL_DIV_EXPR: | |
1247 | case FLOOR_DIV_EXPR: | |
1248 | case TRUNC_MOD_EXPR: | |
1249 | case RDIV_EXPR: | |
1250 | case EXACT_DIV_EXPR: | |
1251 | case LSHIFT_EXPR: | |
1252 | case RSHIFT_EXPR: | |
1253 | case BIT_IOR_EXPR: | |
1254 | case BIT_XOR_EXPR: | |
1255 | case BIT_AND_EXPR: | |
1256 | case LT_EXPR: | |
1257 | case LE_EXPR: | |
1258 | case GT_EXPR: | |
1259 | case GE_EXPR: | |
1260 | case EQ_EXPR: | |
1261 | case NE_EXPR: | |
1262 | case COMPLEX_EXPR: | |
1263 | case TRUTH_AND_EXPR: | |
1264 | case TRUTH_OR_EXPR: | |
1265 | case TRUTH_XOR_EXPR: | |
1266 | case UNORDERED_EXPR: | |
1267 | case ORDERED_EXPR: | |
1268 | case UNLT_EXPR: | |
1269 | case UNLE_EXPR: | |
1270 | case UNGT_EXPR: | |
1271 | case UNGE_EXPR: | |
1272 | case UNEQ_EXPR: | |
1273 | /* Binary operations evaluating both arguments (increment and | |
1274 | decrement are binary internally in GCC). */ | |
1275 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1276 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1277 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1278 | maybe_const_itself); | |
f59e3889 | 1279 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1280 | if (code != MODIFY_EXPR |
1281 | && code != PREDECREMENT_EXPR | |
1282 | && code != PREINCREMENT_EXPR | |
1283 | && code != POSTDECREMENT_EXPR | |
1284 | && code != POSTINCREMENT_EXPR) | |
1285 | op0 = decl_constant_value_for_optimization (op0); | |
1286 | /* The RHS of a MODIFY_EXPR was fully folded when building that | |
1287 | expression for the sake of conversion warnings. */ | |
1288 | if (code != MODIFY_EXPR) | |
1289 | op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, | |
1290 | maybe_const_itself); | |
f59e3889 | 1291 | STRIP_TYPE_NOPS (op1); |
a75b1c71 | 1292 | op1 = decl_constant_value_for_optimization (op1); |
1293 | if (op0 != orig_op0 || op1 != orig_op1 || in_init) | |
1294 | ret = in_init | |
389dd41b | 1295 | ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) |
1296 | : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); | |
a75b1c71 | 1297 | else |
1298 | ret = fold (expr); | |
672d914b | 1299 | if (TREE_OVERFLOW_P (ret) |
1300 | && !TREE_OVERFLOW_P (op0) | |
1301 | && !TREE_OVERFLOW_P (op1)) | |
1302 | overflow_warning (EXPR_LOCATION (expr), ret); | |
7c834436 | 1303 | if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR) |
1304 | && TREE_CODE (orig_op1) != INTEGER_CST | |
1305 | && TREE_CODE (op1) == INTEGER_CST | |
1306 | && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE | |
1307 | || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE) | |
1308 | && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE | |
1309 | && c_inhibit_evaluation_warnings == 0) | |
1310 | { | |
1311 | if (tree_int_cst_sgn (op1) < 0) | |
1312 | warning_at (loc, 0, (code == LSHIFT_EXPR | |
1313 | ? G_("left shift count is negative") | |
1314 | : G_("right shift count is negative"))); | |
1315 | else if (compare_tree_int (op1, | |
1316 | TYPE_PRECISION (TREE_TYPE (orig_op0))) | |
1317 | >= 0) | |
1318 | warning_at (loc, 0, (code == LSHIFT_EXPR | |
1319 | ? G_("left shift count >= width of type") | |
1320 | : G_("right shift count >= width of type"))); | |
1321 | } | |
a75b1c71 | 1322 | goto out; |
1323 | ||
1324 | case INDIRECT_REF: | |
1325 | case FIX_TRUNC_EXPR: | |
1326 | case FLOAT_EXPR: | |
1327 | CASE_CONVERT: | |
e66325ea | 1328 | case ADDR_SPACE_CONVERT_EXPR: |
2b19dfe4 | 1329 | case VIEW_CONVERT_EXPR: |
a75b1c71 | 1330 | case NON_LVALUE_EXPR: |
1331 | case NEGATE_EXPR: | |
1332 | case BIT_NOT_EXPR: | |
1333 | case TRUTH_NOT_EXPR: | |
1334 | case ADDR_EXPR: | |
1335 | case CONJ_EXPR: | |
1336 | case REALPART_EXPR: | |
1337 | case IMAGPART_EXPR: | |
1338 | /* Unary operations. */ | |
1339 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1340 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1341 | maybe_const_itself); | |
f59e3889 | 1342 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1343 | if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR) |
1344 | op0 = decl_constant_value_for_optimization (op0); | |
737a23cc | 1345 | /* ??? Cope with user tricks that amount to offsetof. The middle-end is |
1346 | not prepared to deal with them if they occur in initializers. */ | |
1347 | if (op0 != orig_op0 | |
1348 | && code == ADDR_EXPR | |
1349 | && (op1 = get_base_address (op0)) != NULL_TREE | |
1350 | && TREE_CODE (op1) == INDIRECT_REF | |
1351 | && TREE_CONSTANT (TREE_OPERAND (op1, 0))) | |
7549df0d | 1352 | ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0)); |
737a23cc | 1353 | else if (op0 != orig_op0 || in_init) |
a75b1c71 | 1354 | ret = in_init |
389dd41b | 1355 | ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0) |
1356 | : fold_build1_loc (loc, code, TREE_TYPE (expr), op0); | |
a75b1c71 | 1357 | else |
1358 | ret = fold (expr); | |
1359 | if (code == INDIRECT_REF | |
1360 | && ret != expr | |
1361 | && TREE_CODE (ret) == INDIRECT_REF) | |
1362 | { | |
1363 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1364 | TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); | |
1365 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1366 | } | |
672d914b | 1367 | switch (code) |
1368 | { | |
1369 | case FIX_TRUNC_EXPR: | |
1370 | case FLOAT_EXPR: | |
1371 | CASE_CONVERT: | |
1372 | /* Don't warn about explicit conversions. We will already | |
1373 | have warned about suspect implicit conversions. */ | |
1374 | break; | |
1375 | ||
1376 | default: | |
1377 | if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0)) | |
1378 | overflow_warning (EXPR_LOCATION (expr), ret); | |
1379 | break; | |
1380 | } | |
a75b1c71 | 1381 | goto out; |
1382 | ||
1383 | case TRUTH_ANDIF_EXPR: | |
1384 | case TRUTH_ORIF_EXPR: | |
1385 | /* Binary operations not necessarily evaluating both | |
1386 | arguments. */ | |
1387 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1388 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1389 | op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); | |
f59e3889 | 1390 | STRIP_TYPE_NOPS (op0); |
672d914b | 1391 | |
1392 | unused_p = (op0 == (code == TRUTH_ANDIF_EXPR | |
1393 | ? truthvalue_false_node | |
1394 | : truthvalue_true_node)); | |
fc501191 | 1395 | c_disable_warnings (unused_p); |
a75b1c71 | 1396 | op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); |
f59e3889 | 1397 | STRIP_TYPE_NOPS (op1); |
fc501191 | 1398 | c_enable_warnings (unused_p); |
672d914b | 1399 | |
a75b1c71 | 1400 | if (op0 != orig_op0 || op1 != orig_op1 || in_init) |
1401 | ret = in_init | |
389dd41b | 1402 | ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) |
1403 | : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); | |
a75b1c71 | 1404 | else |
1405 | ret = fold (expr); | |
1406 | *maybe_const_operands &= op0_const; | |
1407 | *maybe_const_itself &= op0_const_self; | |
1408 | if (!(flag_isoc99 | |
1409 | && op0_const | |
1410 | && op0_const_self | |
1411 | && (code == TRUTH_ANDIF_EXPR | |
1412 | ? op0 == truthvalue_false_node | |
1413 | : op0 == truthvalue_true_node))) | |
1414 | *maybe_const_operands &= op1_const; | |
1415 | if (!(op0_const | |
1416 | && op0_const_self | |
1417 | && (code == TRUTH_ANDIF_EXPR | |
1418 | ? op0 == truthvalue_false_node | |
1419 | : op0 == truthvalue_true_node))) | |
1420 | *maybe_const_itself &= op1_const_self; | |
1421 | goto out; | |
1422 | ||
1423 | case COND_EXPR: | |
1424 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1425 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1426 | orig_op2 = op2 = TREE_OPERAND (expr, 2); | |
1427 | op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); | |
672d914b | 1428 | |
f59e3889 | 1429 | STRIP_TYPE_NOPS (op0); |
fc501191 | 1430 | c_disable_warnings (op0 == truthvalue_false_node); |
a75b1c71 | 1431 | op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); |
f59e3889 | 1432 | STRIP_TYPE_NOPS (op1); |
fc501191 | 1433 | c_enable_warnings (op0 == truthvalue_false_node); |
672d914b | 1434 | |
fc501191 | 1435 | c_disable_warnings (op0 == truthvalue_true_node); |
a75b1c71 | 1436 | op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self); |
f59e3889 | 1437 | STRIP_TYPE_NOPS (op2); |
fc501191 | 1438 | c_enable_warnings (op0 == truthvalue_true_node); |
672d914b | 1439 | |
a75b1c71 | 1440 | if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2) |
389dd41b | 1441 | ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2); |
a75b1c71 | 1442 | else |
1443 | ret = fold (expr); | |
1444 | *maybe_const_operands &= op0_const; | |
1445 | *maybe_const_itself &= op0_const_self; | |
1446 | if (!(flag_isoc99 | |
1447 | && op0_const | |
1448 | && op0_const_self | |
1449 | && op0 == truthvalue_false_node)) | |
1450 | *maybe_const_operands &= op1_const; | |
1451 | if (!(op0_const | |
1452 | && op0_const_self | |
1453 | && op0 == truthvalue_false_node)) | |
1454 | *maybe_const_itself &= op1_const_self; | |
1455 | if (!(flag_isoc99 | |
1456 | && op0_const | |
1457 | && op0_const_self | |
1458 | && op0 == truthvalue_true_node)) | |
1459 | *maybe_const_operands &= op2_const; | |
1460 | if (!(op0_const | |
1461 | && op0_const_self | |
1462 | && op0 == truthvalue_true_node)) | |
1463 | *maybe_const_itself &= op2_const_self; | |
1464 | goto out; | |
1465 | ||
c6418a4e | 1466 | case EXCESS_PRECISION_EXPR: |
1467 | /* Each case where an operand with excess precision may be | |
1468 | encountered must remove the EXCESS_PRECISION_EXPR around | |
1469 | inner operands and possibly put one around the whole | |
1470 | expression or possibly convert to the semantic type (which | |
1471 | c_fully_fold does); we cannot tell at this stage which is | |
1472 | appropriate in any particular case. */ | |
1473 | gcc_unreachable (); | |
1474 | ||
a75b1c71 | 1475 | default: |
1476 | /* Various codes may appear through folding built-in functions | |
1477 | and their arguments. */ | |
1478 | goto out; | |
1479 | } | |
1480 | ||
1481 | out: | |
1482 | /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks | |
1483 | have been done by this point, so remove them again. */ | |
1484 | nowarning |= TREE_NO_WARNING (ret); | |
1485 | STRIP_TYPE_NOPS (ret); | |
1486 | if (nowarning && !TREE_NO_WARNING (ret)) | |
1487 | { | |
1488 | if (!CAN_HAVE_LOCATION_P (ret)) | |
1489 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
1490 | TREE_NO_WARNING (ret) = 1; | |
1491 | } | |
1492 | if (ret != expr) | |
1493 | protected_set_expr_location (ret, loc); | |
1494 | return ret; | |
1495 | } | |
1496 | ||
1497 | /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type, | |
1498 | return EXP. Otherwise, return either EXP or its known constant | |
1499 | value (if it has one), but return EXP if EXP has mode BLKmode. ??? | |
1500 | Is the BLKmode test appropriate? */ | |
1501 | ||
1502 | tree | |
1503 | decl_constant_value_for_optimization (tree exp) | |
1504 | { | |
1505 | tree ret; | |
1506 | ||
1507 | /* This function is only used by C, for c_fully_fold and other | |
1508 | optimization, and may not be correct for C++. */ | |
1509 | if (c_dialect_cxx ()) | |
1510 | gcc_unreachable (); | |
1511 | ||
1512 | if (!optimize | |
1513 | || TREE_CODE (exp) != VAR_DECL | |
1514 | || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE | |
1515 | || DECL_MODE (exp) == BLKmode) | |
1516 | return exp; | |
1517 | ||
1518 | ret = decl_constant_value (exp); | |
1519 | /* Avoid unwanted tree sharing between the initializer and current | |
1520 | function's body where the tree can be modified e.g. by the | |
1521 | gimplifier. */ | |
1522 | if (ret != exp && TREE_STATIC (exp)) | |
1523 | ret = unshare_expr (ret); | |
1524 | return ret; | |
1525 | } | |
1526 | ||
2a1736ed | 1527 | /* Print a warning if a constant expression had overflow in folding. |
1528 | Invoke this function on every expression that the language | |
1529 | requires to be a constant expression. | |
1530 | Note the ANSI C standard says it is erroneous for a | |
1531 | constant expression to overflow. */ | |
b2806639 | 1532 | |
1533 | void | |
1cae46be | 1534 | constant_expression_warning (tree value) |
07317e69 | 1535 | { |
48e1416a | 1536 | if (warn_overflow && pedantic |
07317e69 | 1537 | && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
1538 | || TREE_CODE (value) == FIXED_CST | |
1539 | || TREE_CODE (value) == VECTOR_CST | |
1540 | || TREE_CODE (value) == COMPLEX_CST) | |
1541 | && TREE_OVERFLOW (value)) | |
21ca8540 | 1542 | pedwarn (input_location, OPT_Woverflow, "overflow in constant expression"); |
07317e69 | 1543 | } |
1544 | ||
1545 | /* The same as above but print an unconditional error. */ | |
1546 | void | |
1547 | constant_expression_error (tree value) | |
b2806639 | 1548 | { |
837e1122 | 1549 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
9421ebb9 | 1550 | || TREE_CODE (value) == FIXED_CST |
886cfd4f | 1551 | || TREE_CODE (value) == VECTOR_CST |
837e1122 | 1552 | || TREE_CODE (value) == COMPLEX_CST) |
07317e69 | 1553 | && TREE_OVERFLOW (value)) |
1554 | error ("overflow in constant expression"); | |
2a1736ed | 1555 | } |
1556 | ||
f170d67f | 1557 | /* Print a warning if an expression had overflow in folding and its |
1558 | operands hadn't. | |
1559 | ||
2a1736ed | 1560 | Invoke this function on every expression that |
1561 | (1) appears in the source code, and | |
f170d67f | 1562 | (2) is a constant expression that overflowed, and |
2a1736ed | 1563 | (3) is not already checked by convert_and_check; |
f170d67f | 1564 | however, do not invoke this function on operands of explicit casts |
1565 | or when the expression is the result of an operator and any operand | |
1566 | already overflowed. */ | |
2a1736ed | 1567 | |
1568 | void | |
e60a6f7b | 1569 | overflow_warning (location_t loc, tree value) |
2a1736ed | 1570 | { |
48d94ede | 1571 | if (c_inhibit_evaluation_warnings != 0) |
1572 | return; | |
f170d67f | 1573 | |
1574 | switch (TREE_CODE (value)) | |
886cfd4f | 1575 | { |
f170d67f | 1576 | case INTEGER_CST: |
e60a6f7b | 1577 | warning_at (loc, OPT_Woverflow, "integer overflow in expression"); |
f170d67f | 1578 | break; |
48e1416a | 1579 | |
f170d67f | 1580 | case REAL_CST: |
e60a6f7b | 1581 | warning_at (loc, OPT_Woverflow, |
1582 | "floating point overflow in expression"); | |
f170d67f | 1583 | break; |
48e1416a | 1584 | |
9421ebb9 | 1585 | case FIXED_CST: |
e60a6f7b | 1586 | warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression"); |
9421ebb9 | 1587 | break; |
1588 | ||
f170d67f | 1589 | case VECTOR_CST: |
e60a6f7b | 1590 | warning_at (loc, OPT_Woverflow, "vector overflow in expression"); |
f170d67f | 1591 | break; |
48e1416a | 1592 | |
f170d67f | 1593 | case COMPLEX_CST: |
1594 | if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) | |
e60a6f7b | 1595 | warning_at (loc, OPT_Woverflow, |
1596 | "complex integer overflow in expression"); | |
f170d67f | 1597 | else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) |
e60a6f7b | 1598 | warning_at (loc, OPT_Woverflow, |
1599 | "complex floating point overflow in expression"); | |
f170d67f | 1600 | break; |
1601 | ||
1602 | default: | |
1603 | break; | |
886cfd4f | 1604 | } |
2a1736ed | 1605 | } |
1606 | ||
03033af4 | 1607 | /* Warn about uses of logical || / && operator in a context where it |
1608 | is likely that the bitwise equivalent was intended by the | |
1609 | programmer. We have seen an expression in which CODE is a binary | |
9c20c4fc | 1610 | operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding |
1611 | had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ | |
b13d1547 | 1612 | void |
9c20c4fc | 1613 | warn_logical_operator (location_t location, enum tree_code code, tree type, |
48e1416a | 1614 | enum tree_code code_left, tree op_left, |
03033af4 | 1615 | enum tree_code ARG_UNUSED (code_right), tree op_right) |
b13d1547 | 1616 | { |
9c20c4fc | 1617 | int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); |
1618 | int in0_p, in1_p, in_p; | |
1619 | tree low0, low1, low, high0, high1, high, lhs, rhs, tem; | |
1620 | bool strict_overflow_p = false; | |
1621 | ||
03033af4 | 1622 | if (code != TRUTH_ANDIF_EXPR |
1623 | && code != TRUTH_AND_EXPR | |
1624 | && code != TRUTH_ORIF_EXPR | |
1625 | && code != TRUTH_OR_EXPR) | |
1626 | return; | |
1627 | ||
1628 | /* Warn if &&/|| are being used in a context where it is | |
1629 | likely that the bitwise equivalent was intended by the | |
1630 | programmer. That is, an expression such as op && MASK | |
1631 | where op should not be any boolean expression, nor a | |
1632 | constant, and mask seems to be a non-boolean integer constant. */ | |
1633 | if (!truth_value_p (code_left) | |
1634 | && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) | |
1635 | && !CONSTANT_CLASS_P (op_left) | |
1636 | && !TREE_NO_WARNING (op_left) | |
1637 | && TREE_CODE (op_right) == INTEGER_CST | |
1638 | && !integer_zerop (op_right) | |
1639 | && !integer_onep (op_right)) | |
b13d1547 | 1640 | { |
9c20c4fc | 1641 | if (or_op) |
03033af4 | 1642 | warning_at (location, OPT_Wlogical_op, "logical %<or%>" |
1643 | " applied to non-boolean constant"); | |
1644 | else | |
1645 | warning_at (location, OPT_Wlogical_op, "logical %<and%>" | |
1646 | " applied to non-boolean constant"); | |
1647 | TREE_NO_WARNING (op_left) = true; | |
9c20c4fc | 1648 | return; |
1649 | } | |
1650 | ||
1651 | /* We do not warn for constants because they are typical of macro | |
1652 | expansions that test for features. */ | |
1653 | if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right)) | |
1654 | return; | |
1655 | ||
1656 | /* This warning only makes sense with logical operands. */ | |
1657 | if (!(truth_value_p (TREE_CODE (op_left)) | |
1658 | || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) | |
1659 | || !(truth_value_p (TREE_CODE (op_right)) | |
1660 | || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) | |
1661 | return; | |
1662 | ||
9c20c4fc | 1663 | |
686369e8 | 1664 | /* We first test whether either side separately is trivially true |
1665 | (with OR) or trivially false (with AND). If so, do not warn. | |
1666 | This is a common idiom for testing ranges of data types in | |
1667 | portable code. */ | |
1668 | lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); | |
1669 | if (!lhs) | |
1670 | return; | |
1671 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) | |
9c20c4fc | 1672 | lhs = C_MAYBE_CONST_EXPR_EXPR (lhs); |
1673 | ||
686369e8 | 1674 | /* If this is an OR operation, invert both sides; now, the result |
1675 | should be always false to get a warning. */ | |
1676 | if (or_op) | |
1677 | in0_p = !in0_p; | |
1678 | ||
1679 | tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); | |
d42e7c5a | 1680 | if (tem && integer_zerop (tem)) |
686369e8 | 1681 | return; |
1682 | ||
1683 | rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); | |
1684 | if (!rhs) | |
1685 | return; | |
1686 | if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) | |
9c20c4fc | 1687 | rhs = C_MAYBE_CONST_EXPR_EXPR (rhs); |
48e1416a | 1688 | |
686369e8 | 1689 | /* If this is an OR operation, invert both sides; now, the result |
1690 | should be always false to get a warning. */ | |
9c20c4fc | 1691 | if (or_op) |
686369e8 | 1692 | in1_p = !in1_p; |
48e1416a | 1693 | |
686369e8 | 1694 | tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); |
d42e7c5a | 1695 | if (tem && integer_zerop (tem)) |
686369e8 | 1696 | return; |
1697 | ||
1698 | /* If both expressions have the same operand, if we can merge the | |
1699 | ranges, and if the range test is always false, then warn. */ | |
1700 | if (operand_equal_p (lhs, rhs, 0) | |
9c20c4fc | 1701 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, |
1702 | in1_p, low1, high1) | |
389dd41b | 1703 | && 0 != (tem = build_range_check (UNKNOWN_LOCATION, |
686369e8 | 1704 | type, lhs, in_p, low, high)) |
1705 | && integer_zerop (tem)) | |
9c20c4fc | 1706 | { |
9c20c4fc | 1707 | if (or_op) |
1708 | warning_at (location, OPT_Wlogical_op, | |
1709 | "logical %<or%> " | |
1710 | "of collectively exhaustive tests is always true"); | |
1711 | else | |
1712 | warning_at (location, OPT_Wlogical_op, | |
1713 | "logical %<and%> " | |
1714 | "of mutually exclusive tests is always false"); | |
b13d1547 | 1715 | } |
1716 | } | |
1717 | ||
1718 | ||
3d177e8c | 1719 | /* Warn if EXP contains any computations whose results are not used. |
1720 | Return true if a warning is printed; false otherwise. LOCUS is the | |
1721 | (potential) location of the expression. */ | |
1722 | ||
1723 | bool | |
1724 | warn_if_unused_value (const_tree exp, location_t locus) | |
1725 | { | |
1726 | restart: | |
1727 | if (TREE_USED (exp) || TREE_NO_WARNING (exp)) | |
1728 | return false; | |
1729 | ||
1730 | /* Don't warn about void constructs. This includes casting to void, | |
1731 | void function calls, and statement expressions with a final cast | |
1732 | to void. */ | |
1733 | if (VOID_TYPE_P (TREE_TYPE (exp))) | |
1734 | return false; | |
1735 | ||
1736 | if (EXPR_HAS_LOCATION (exp)) | |
1737 | locus = EXPR_LOCATION (exp); | |
1738 | ||
1739 | switch (TREE_CODE (exp)) | |
1740 | { | |
1741 | case PREINCREMENT_EXPR: | |
1742 | case POSTINCREMENT_EXPR: | |
1743 | case PREDECREMENT_EXPR: | |
1744 | case POSTDECREMENT_EXPR: | |
1745 | case MODIFY_EXPR: | |
1746 | case INIT_EXPR: | |
1747 | case TARGET_EXPR: | |
1748 | case CALL_EXPR: | |
1749 | case TRY_CATCH_EXPR: | |
1750 | case WITH_CLEANUP_EXPR: | |
1751 | case EXIT_EXPR: | |
1752 | case VA_ARG_EXPR: | |
1753 | return false; | |
1754 | ||
1755 | case BIND_EXPR: | |
1756 | /* For a binding, warn if no side effect within it. */ | |
1757 | exp = BIND_EXPR_BODY (exp); | |
1758 | goto restart; | |
1759 | ||
1760 | case SAVE_EXPR: | |
1761 | case NON_LVALUE_EXPR: | |
d85dbdb3 | 1762 | case NOP_EXPR: |
3d177e8c | 1763 | exp = TREE_OPERAND (exp, 0); |
1764 | goto restart; | |
1765 | ||
1766 | case TRUTH_ORIF_EXPR: | |
1767 | case TRUTH_ANDIF_EXPR: | |
1768 | /* In && or ||, warn if 2nd operand has no side effect. */ | |
1769 | exp = TREE_OPERAND (exp, 1); | |
1770 | goto restart; | |
1771 | ||
1772 | case COMPOUND_EXPR: | |
1773 | if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) | |
1774 | return true; | |
1775 | /* Let people do `(foo (), 0)' without a warning. */ | |
1776 | if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) | |
1777 | return false; | |
1778 | exp = TREE_OPERAND (exp, 1); | |
1779 | goto restart; | |
1780 | ||
1781 | case COND_EXPR: | |
1782 | /* If this is an expression with side effects, don't warn; this | |
1783 | case commonly appears in macro expansions. */ | |
1784 | if (TREE_SIDE_EFFECTS (exp)) | |
1785 | return false; | |
1786 | goto warn; | |
1787 | ||
1788 | case INDIRECT_REF: | |
1789 | /* Don't warn about automatic dereferencing of references, since | |
1790 | the user cannot control it. */ | |
1791 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE) | |
1792 | { | |
1793 | exp = TREE_OPERAND (exp, 0); | |
1794 | goto restart; | |
1795 | } | |
1796 | /* Fall through. */ | |
1797 | ||
1798 | default: | |
1799 | /* Referencing a volatile value is a side effect, so don't warn. */ | |
1800 | if ((DECL_P (exp) || REFERENCE_CLASS_P (exp)) | |
1801 | && TREE_THIS_VOLATILE (exp)) | |
1802 | return false; | |
1803 | ||
1804 | /* If this is an expression which has no operands, there is no value | |
1805 | to be unused. There are no such language-independent codes, | |
1806 | but front ends may define such. */ | |
1807 | if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0) | |
1808 | return false; | |
1809 | ||
1810 | warn: | |
1811 | return warning_at (locus, OPT_Wunused_value, "value computed is not used"); | |
1812 | } | |
1813 | } | |
1814 | ||
1815 | ||
bcf22371 | 1816 | /* Print a warning about casts that might indicate violation |
1817 | of strict aliasing rules if -Wstrict-aliasing is used and | |
1e31ff37 | 1818 | strict aliasing mode is in effect. OTYPE is the original |
1819 | TREE_TYPE of EXPR, and TYPE the type we're casting to. */ | |
bcf22371 | 1820 | |
e6fa0ea6 | 1821 | bool |
1e31ff37 | 1822 | strict_aliasing_warning (tree otype, tree type, tree expr) |
bcf22371 | 1823 | { |
45bb3afb | 1824 | /* Strip pointer conversion chains and get to the correct original type. */ |
1825 | STRIP_NOPS (expr); | |
1826 | otype = TREE_TYPE (expr); | |
1827 | ||
f06537f2 | 1828 | if (!(flag_strict_aliasing |
1829 | && POINTER_TYPE_P (type) | |
1830 | && POINTER_TYPE_P (otype) | |
1831 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1832 | /* If the type we are casting to is a ref-all pointer | |
1833 | dereferencing it is always valid. */ | |
1834 | || TYPE_REF_CAN_ALIAS_ALL (type)) | |
e6fa0ea6 | 1835 | return false; |
1836 | ||
1837 | if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR | |
bcf22371 | 1838 | && (DECL_P (TREE_OPERAND (expr, 0)) |
e6fa0ea6 | 1839 | || handled_component_p (TREE_OPERAND (expr, 0)))) |
bcf22371 | 1840 | { |
1841 | /* Casting the address of an object to non void pointer. Warn | |
1842 | if the cast breaks type based aliasing. */ | |
e6fa0ea6 | 1843 | if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) |
1844 | { | |
1845 | warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " | |
1846 | "might break strict-aliasing rules"); | |
1847 | return true; | |
1848 | } | |
bcf22371 | 1849 | else |
1850 | { | |
48e1416a | 1851 | /* warn_strict_aliasing >= 3. This includes the default (3). |
e6fa0ea6 | 1852 | Only warn if the cast is dereferenced immediately. */ |
32c2fdea | 1853 | alias_set_type set1 = |
e6fa0ea6 | 1854 | get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); |
32c2fdea | 1855 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); |
bcf22371 | 1856 | |
62d823d0 | 1857 | if (set1 != set2 && set2 != 0 |
1858 | && (set1 == 0 || !alias_sets_conflict_p (set1, set2))) | |
e6fa0ea6 | 1859 | { |
1860 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1861 | "pointer will break strict-aliasing rules"); | |
1862 | return true; | |
1863 | } | |
1864 | else if (warn_strict_aliasing == 2 | |
879f881c | 1865 | && !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1866 | { |
1867 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1868 | "pointer might break strict-aliasing rules"); | |
1869 | return true; | |
1870 | } | |
bcf22371 | 1871 | } |
1872 | } | |
e6fa0ea6 | 1873 | else |
1874 | if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) | |
1875 | { | |
1876 | /* At this level, warn for any conversions, even if an address is | |
1877 | not taken in the same statement. This will likely produce many | |
1878 | false positives, but could be useful to pinpoint problems that | |
1879 | are not revealed at higher levels. */ | |
32c2fdea | 1880 | alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); |
1881 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); | |
1882 | if (!COMPLETE_TYPE_P (type) | |
879f881c | 1883 | || !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1884 | { |
1885 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1886 | "pointer might break strict-aliasing rules"); | |
1887 | return true; | |
1888 | } | |
1889 | } | |
1890 | ||
1891 | return false; | |
bcf22371 | 1892 | } |
1893 | ||
f003f5dc | 1894 | /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with |
1895 | sizeof as last operand of certain builtins. */ | |
1896 | ||
1897 | void | |
57f872a2 | 1898 | sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee, |
f1f41a6c | 1899 | vec<tree, va_gc> *params, tree *sizeof_arg, |
f003f5dc | 1900 | bool (*comp_types) (tree, tree)) |
1901 | { | |
1902 | tree type, dest = NULL_TREE, src = NULL_TREE, tem; | |
57f872a2 | 1903 | bool strop = false, cmp = false; |
1904 | unsigned int idx = ~0; | |
1905 | location_t loc; | |
f003f5dc | 1906 | |
1907 | if (TREE_CODE (callee) != FUNCTION_DECL | |
1908 | || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL | |
f1f41a6c | 1909 | || vec_safe_length (params) <= 1) |
f003f5dc | 1910 | return; |
1911 | ||
f003f5dc | 1912 | switch (DECL_FUNCTION_CODE (callee)) |
1913 | { | |
1914 | case BUILT_IN_STRNCMP: | |
1915 | case BUILT_IN_STRNCASECMP: | |
57f872a2 | 1916 | cmp = true; |
1917 | /* FALLTHRU */ | |
f003f5dc | 1918 | case BUILT_IN_STRNCPY: |
57f872a2 | 1919 | case BUILT_IN_STRNCPY_CHK: |
f003f5dc | 1920 | case BUILT_IN_STRNCAT: |
57f872a2 | 1921 | case BUILT_IN_STRNCAT_CHK: |
1922 | case BUILT_IN_STPNCPY: | |
1923 | case BUILT_IN_STPNCPY_CHK: | |
f003f5dc | 1924 | strop = true; |
1925 | /* FALLTHRU */ | |
1926 | case BUILT_IN_MEMCPY: | |
57f872a2 | 1927 | case BUILT_IN_MEMCPY_CHK: |
f003f5dc | 1928 | case BUILT_IN_MEMMOVE: |
57f872a2 | 1929 | case BUILT_IN_MEMMOVE_CHK: |
f1f41a6c | 1930 | if (params->length () < 3) |
57f872a2 | 1931 | return; |
f1f41a6c | 1932 | src = (*params)[1]; |
1933 | dest = (*params)[0]; | |
57f872a2 | 1934 | idx = 2; |
1935 | break; | |
1936 | case BUILT_IN_BCOPY: | |
f1f41a6c | 1937 | if (params->length () < 3) |
57f872a2 | 1938 | return; |
f1f41a6c | 1939 | src = (*params)[0]; |
1940 | dest = (*params)[1]; | |
57f872a2 | 1941 | idx = 2; |
1942 | break; | |
f003f5dc | 1943 | case BUILT_IN_MEMCMP: |
57f872a2 | 1944 | case BUILT_IN_BCMP: |
f1f41a6c | 1945 | if (params->length () < 3) |
f003f5dc | 1946 | return; |
f1f41a6c | 1947 | src = (*params)[1]; |
1948 | dest = (*params)[0]; | |
57f872a2 | 1949 | idx = 2; |
1950 | cmp = true; | |
f003f5dc | 1951 | break; |
1952 | case BUILT_IN_MEMSET: | |
57f872a2 | 1953 | case BUILT_IN_MEMSET_CHK: |
f1f41a6c | 1954 | if (params->length () < 3) |
f003f5dc | 1955 | return; |
f1f41a6c | 1956 | dest = (*params)[0]; |
57f872a2 | 1957 | idx = 2; |
1958 | break; | |
1959 | case BUILT_IN_BZERO: | |
f1f41a6c | 1960 | dest = (*params)[0]; |
57f872a2 | 1961 | idx = 1; |
f003f5dc | 1962 | break; |
1963 | case BUILT_IN_STRNDUP: | |
f1f41a6c | 1964 | src = (*params)[0]; |
f003f5dc | 1965 | strop = true; |
57f872a2 | 1966 | idx = 1; |
1967 | break; | |
1968 | case BUILT_IN_MEMCHR: | |
f1f41a6c | 1969 | if (params->length () < 3) |
57f872a2 | 1970 | return; |
f1f41a6c | 1971 | src = (*params)[0]; |
57f872a2 | 1972 | idx = 2; |
1973 | break; | |
1974 | case BUILT_IN_SNPRINTF: | |
1975 | case BUILT_IN_SNPRINTF_CHK: | |
1976 | case BUILT_IN_VSNPRINTF: | |
1977 | case BUILT_IN_VSNPRINTF_CHK: | |
f1f41a6c | 1978 | dest = (*params)[0]; |
57f872a2 | 1979 | idx = 1; |
1980 | strop = true; | |
f003f5dc | 1981 | break; |
1982 | default: | |
1983 | break; | |
1984 | } | |
1985 | ||
57f872a2 | 1986 | if (idx >= 3) |
1987 | return; | |
1988 | ||
1989 | if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node) | |
1990 | return; | |
1991 | ||
1992 | type = TYPE_P (sizeof_arg[idx]) | |
1993 | ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]); | |
1994 | if (!POINTER_TYPE_P (type)) | |
1995 | return; | |
1996 | ||
f003f5dc | 1997 | if (dest |
1998 | && (tem = tree_strip_nop_conversions (dest)) | |
1999 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
2000 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
2001 | return; | |
2002 | ||
2003 | if (src | |
2004 | && (tem = tree_strip_nop_conversions (src)) | |
2005 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
2006 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
2007 | return; | |
2008 | ||
57f872a2 | 2009 | loc = sizeof_arg_loc[idx]; |
2010 | ||
2011 | if (dest && !cmp) | |
f003f5dc | 2012 | { |
57f872a2 | 2013 | if (!TYPE_P (sizeof_arg[idx]) |
2014 | && operand_equal_p (dest, sizeof_arg[idx], 0) | |
f003f5dc | 2015 | && comp_types (TREE_TYPE (dest), type)) |
2016 | { | |
57f872a2 | 2017 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
f003f5dc | 2018 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
2019 | "argument to %<sizeof%> in %qD call is the same " | |
2020 | "expression as the destination; did you mean to " | |
2021 | "remove the addressof?", callee); | |
2022 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2023 | == TYPE_PRECISION (char_type_node)) | |
2024 | || strop) | |
2025 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2026 | "argument to %<sizeof%> in %qD call is the same " | |
2027 | "expression as the destination; did you mean to " | |
2028 | "provide an explicit length?", callee); | |
2029 | else | |
2030 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2031 | "argument to %<sizeof%> in %qD call is the same " | |
2032 | "expression as the destination; did you mean to " | |
2033 | "dereference it?", callee); | |
2034 | return; | |
2035 | } | |
2036 | ||
2037 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
2038 | && !strop | |
2039 | && comp_types (TREE_TYPE (dest), type) | |
2040 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2041 | { | |
2042 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2043 | "argument to %<sizeof%> in %qD call is the same " | |
2044 | "pointer type %qT as the destination; expected %qT " | |
2045 | "or an explicit length", callee, TREE_TYPE (dest), | |
2046 | TREE_TYPE (TREE_TYPE (dest))); | |
2047 | return; | |
2048 | } | |
2049 | } | |
2050 | ||
57f872a2 | 2051 | if (src && !cmp) |
f003f5dc | 2052 | { |
57f872a2 | 2053 | if (!TYPE_P (sizeof_arg[idx]) |
2054 | && operand_equal_p (src, sizeof_arg[idx], 0) | |
f003f5dc | 2055 | && comp_types (TREE_TYPE (src), type)) |
2056 | { | |
57f872a2 | 2057 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
f003f5dc | 2058 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
2059 | "argument to %<sizeof%> in %qD call is the same " | |
2060 | "expression as the source; did you mean to " | |
2061 | "remove the addressof?", callee); | |
2062 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2063 | == TYPE_PRECISION (char_type_node)) | |
2064 | || strop) | |
2065 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2066 | "argument to %<sizeof%> in %qD call is the same " | |
2067 | "expression as the source; did you mean to " | |
2068 | "provide an explicit length?", callee); | |
2069 | else | |
2070 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2071 | "argument to %<sizeof%> in %qD call is the same " | |
2072 | "expression as the source; did you mean to " | |
2073 | "dereference it?", callee); | |
2074 | return; | |
2075 | } | |
2076 | ||
2077 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
2078 | && !strop | |
2079 | && comp_types (TREE_TYPE (src), type) | |
2080 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2081 | { | |
2082 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2083 | "argument to %<sizeof%> in %qD call is the same " | |
2084 | "pointer type %qT as the source; expected %qT " | |
2085 | "or an explicit length", callee, TREE_TYPE (src), | |
2086 | TREE_TYPE (TREE_TYPE (src))); | |
2087 | return; | |
2088 | } | |
2089 | } | |
57f872a2 | 2090 | |
2091 | if (dest) | |
2092 | { | |
2093 | if (!TYPE_P (sizeof_arg[idx]) | |
2094 | && operand_equal_p (dest, sizeof_arg[idx], 0) | |
2095 | && comp_types (TREE_TYPE (dest), type)) | |
2096 | { | |
2097 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
2098 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2099 | "argument to %<sizeof%> in %qD call is the same " | |
2100 | "expression as the first source; did you mean to " | |
2101 | "remove the addressof?", callee); | |
2102 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2103 | == TYPE_PRECISION (char_type_node)) | |
2104 | || strop) | |
2105 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2106 | "argument to %<sizeof%> in %qD call is the same " | |
2107 | "expression as the first source; did you mean to " | |
2108 | "provide an explicit length?", callee); | |
2109 | else | |
2110 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2111 | "argument to %<sizeof%> in %qD call is the same " | |
2112 | "expression as the first source; did you mean to " | |
2113 | "dereference it?", callee); | |
2114 | return; | |
2115 | } | |
2116 | ||
2117 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
2118 | && !strop | |
2119 | && comp_types (TREE_TYPE (dest), type) | |
2120 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2121 | { | |
2122 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2123 | "argument to %<sizeof%> in %qD call is the same " | |
2124 | "pointer type %qT as the first source; expected %qT " | |
2125 | "or an explicit length", callee, TREE_TYPE (dest), | |
2126 | TREE_TYPE (TREE_TYPE (dest))); | |
2127 | return; | |
2128 | } | |
2129 | } | |
2130 | ||
2131 | if (src) | |
2132 | { | |
2133 | if (!TYPE_P (sizeof_arg[idx]) | |
2134 | && operand_equal_p (src, sizeof_arg[idx], 0) | |
2135 | && comp_types (TREE_TYPE (src), type)) | |
2136 | { | |
2137 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
2138 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2139 | "argument to %<sizeof%> in %qD call is the same " | |
2140 | "expression as the second source; did you mean to " | |
2141 | "remove the addressof?", callee); | |
2142 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2143 | == TYPE_PRECISION (char_type_node)) | |
2144 | || strop) | |
2145 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2146 | "argument to %<sizeof%> in %qD call is the same " | |
2147 | "expression as the second source; did you mean to " | |
2148 | "provide an explicit length?", callee); | |
2149 | else | |
2150 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2151 | "argument to %<sizeof%> in %qD call is the same " | |
2152 | "expression as the second source; did you mean to " | |
2153 | "dereference it?", callee); | |
2154 | return; | |
2155 | } | |
2156 | ||
2157 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
2158 | && !strop | |
2159 | && comp_types (TREE_TYPE (src), type) | |
2160 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2161 | { | |
2162 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2163 | "argument to %<sizeof%> in %qD call is the same " | |
2164 | "pointer type %qT as the second source; expected %qT " | |
2165 | "or an explicit length", callee, TREE_TYPE (src), | |
2166 | TREE_TYPE (TREE_TYPE (src))); | |
2167 | return; | |
2168 | } | |
2169 | } | |
2170 | ||
f003f5dc | 2171 | } |
2172 | ||
3f08e399 | 2173 | /* Warn for unlikely, improbable, or stupid DECL declarations |
2174 | of `main'. */ | |
2175 | ||
2176 | void | |
2177 | check_main_parameter_types (tree decl) | |
2178 | { | |
d0af78c5 | 2179 | function_args_iterator iter; |
2180 | tree type; | |
3f08e399 | 2181 | int argct = 0; |
2182 | ||
d0af78c5 | 2183 | FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) |
2184 | { | |
2185 | /* XXX void_type_node belies the abstraction. */ | |
2186 | if (type == void_type_node || type == error_mark_node ) | |
2187 | break; | |
2188 | ||
2189 | ++argct; | |
2190 | switch (argct) | |
2191 | { | |
2192 | case 1: | |
2193 | if (TYPE_MAIN_VARIANT (type) != integer_type_node) | |
2194 | pedwarn (input_location, OPT_Wmain, | |
2195 | "first argument of %q+D should be %<int%>", decl); | |
2196 | break; | |
2197 | ||
2198 | case 2: | |
2199 | if (TREE_CODE (type) != POINTER_TYPE | |
2200 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
2201 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
2202 | != char_type_node)) | |
2203 | pedwarn (input_location, OPT_Wmain, | |
2204 | "second argument of %q+D should be %<char **%>", decl); | |
2205 | break; | |
2206 | ||
2207 | case 3: | |
2208 | if (TREE_CODE (type) != POINTER_TYPE | |
2209 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
2210 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
2211 | != char_type_node)) | |
2212 | pedwarn (input_location, OPT_Wmain, | |
2213 | "third argument of %q+D should probably be " | |
2214 | "%<char **%>", decl); | |
2215 | break; | |
2216 | } | |
2217 | } | |
3f08e399 | 2218 | |
2219 | /* It is intentional that this message does not mention the third | |
2220 | argument because it's only mentioned in an appendix of the | |
2221 | standard. */ | |
2222 | if (argct > 0 && (argct < 2 || argct > 3)) | |
d0af78c5 | 2223 | pedwarn (input_location, OPT_Wmain, |
2224 | "%q+D takes only zero or two arguments", decl); | |
3f08e399 | 2225 | } |
2226 | ||
73437615 | 2227 | /* vector_targets_convertible_p is used for vector pointer types. The |
2228 | callers perform various checks that the qualifiers are satisfactory, | |
2229 | while OTOH vector_targets_convertible_p ignores the number of elements | |
2230 | in the vectors. That's fine with vector pointers as we can consider, | |
2231 | say, a vector of 8 elements as two consecutive vectors of 4 elements, | |
2232 | and that does not require and conversion of the pointer values. | |
2233 | In contrast, vector_types_convertible_p and | |
2234 | vector_types_compatible_elements_p are used for vector value types. */ | |
ed7c4e62 | 2235 | /* True if pointers to distinct types T1 and T2 can be converted to |
2236 | each other without an explicit cast. Only returns true for opaque | |
2237 | vector types. */ | |
2238 | bool | |
2239 | vector_targets_convertible_p (const_tree t1, const_tree t2) | |
2240 | { | |
2241 | if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE | |
8d125f7d | 2242 | && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
ed7c4e62 | 2243 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
2244 | return true; | |
2245 | ||
2246 | return false; | |
2247 | } | |
2248 | ||
73437615 | 2249 | /* vector_types_convertible_p is used for vector value types. |
2250 | It could in principle call vector_targets_convertible_p as a subroutine, | |
2251 | but then the check for vector type would be duplicated with its callers, | |
2252 | and also the purpose of vector_targets_convertible_p would become | |
2253 | muddled. | |
2254 | Where vector_types_convertible_p returns true, a conversion might still be | |
2255 | needed to make the types match. | |
2256 | In contrast, vector_targets_convertible_p is used for vector pointer | |
2257 | values, and vector_types_compatible_elements_p is used specifically | |
2258 | in the context for binary operators, as a check if use is possible without | |
2259 | conversion. */ | |
546c4794 | 2260 | /* True if vector types T1 and T2 can be converted to each other |
2261 | without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2 | |
2262 | can only be converted with -flax-vector-conversions yet that is not | |
2263 | in effect, emit a note telling the user about that option if such | |
2264 | a note has not previously been emitted. */ | |
2265 | bool | |
9f627b1a | 2266 | vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note) |
8b4b9810 | 2267 | { |
546c4794 | 2268 | static bool emitted_lax_note = false; |
ae6db8ab | 2269 | bool convertible_lax; |
2270 | ||
8d125f7d | 2271 | if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
ae6db8ab | 2272 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
2273 | return true; | |
2274 | ||
2275 | convertible_lax = | |
2276 | (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) | |
2277 | && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || | |
432dd330 | 2278 | TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)) |
ae6db8ab | 2279 | && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) |
2280 | == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); | |
546c4794 | 2281 | |
2282 | if (!convertible_lax || flag_lax_vector_conversions) | |
2283 | return convertible_lax; | |
2284 | ||
2285 | if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) | |
29f51994 | 2286 | && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
546c4794 | 2287 | return true; |
2288 | ||
2289 | if (emit_lax_note && !emitted_lax_note) | |
2290 | { | |
2291 | emitted_lax_note = true; | |
5bcc316e | 2292 | inform (input_location, "use -flax-vector-conversions to permit " |
546c4794 | 2293 | "conversions between vectors with differing " |
2294 | "element types or numbers of subparts"); | |
2295 | } | |
2296 | ||
2297 | return false; | |
8b4b9810 | 2298 | } |
2299 | ||
bf0cb017 | 2300 | /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes |
2301 | and have vector types, V0 has the same type as V1, and the number of | |
2302 | elements of V0, V1, MASK is the same. | |
2303 | ||
2304 | In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was | |
2305 | called with two arguments. In this case implementation passes the | |
2306 | first argument twice in order to share the same tree code. This fact | |
2307 | could enable the mask-values being twice the vector length. This is | |
2308 | an implementation accident and this semantics is not guaranteed to | |
2309 | the user. */ | |
2310 | tree | |
68ea4406 | 2311 | c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask, |
2312 | bool complain) | |
bf0cb017 | 2313 | { |
2314 | tree ret; | |
2315 | bool wrap = true; | |
2316 | bool maybe_const = false; | |
2317 | bool two_arguments = false; | |
2318 | ||
2319 | if (v1 == NULL_TREE) | |
2320 | { | |
2321 | two_arguments = true; | |
2322 | v1 = v0; | |
2323 | } | |
2324 | ||
2325 | if (v0 == error_mark_node || v1 == error_mark_node | |
2326 | || mask == error_mark_node) | |
2327 | return error_mark_node; | |
2328 | ||
2329 | if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE | |
2330 | || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE) | |
2331 | { | |
68ea4406 | 2332 | if (complain) |
2333 | error_at (loc, "__builtin_shuffle last argument must " | |
2334 | "be an integer vector"); | |
bf0cb017 | 2335 | return error_mark_node; |
2336 | } | |
2337 | ||
2338 | if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE | |
2339 | || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE) | |
2340 | { | |
68ea4406 | 2341 | if (complain) |
2342 | error_at (loc, "__builtin_shuffle arguments must be vectors"); | |
bf0cb017 | 2343 | return error_mark_node; |
2344 | } | |
2345 | ||
2346 | if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1))) | |
2347 | { | |
68ea4406 | 2348 | if (complain) |
2349 | error_at (loc, "__builtin_shuffle argument vectors must be of " | |
2350 | "the same type"); | |
bf0cb017 | 2351 | return error_mark_node; |
2352 | } | |
2353 | ||
2354 | if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)) | |
2355 | != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)) | |
2356 | && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)) | |
2357 | != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))) | |
2358 | { | |
68ea4406 | 2359 | if (complain) |
2360 | error_at (loc, "__builtin_shuffle number of elements of the " | |
2361 | "argument vector(s) and the mask vector should " | |
2362 | "be the same"); | |
bf0cb017 | 2363 | return error_mark_node; |
2364 | } | |
2365 | ||
2366 | if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0)))) | |
2367 | != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask))))) | |
2368 | { | |
68ea4406 | 2369 | if (complain) |
2370 | error_at (loc, "__builtin_shuffle argument vector(s) inner type " | |
2371 | "must have the same size as inner type of the mask"); | |
bf0cb017 | 2372 | return error_mark_node; |
2373 | } | |
2374 | ||
2375 | if (!c_dialect_cxx ()) | |
2376 | { | |
2377 | /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */ | |
2378 | v0 = c_fully_fold (v0, false, &maybe_const); | |
2379 | wrap &= maybe_const; | |
2380 | ||
2381 | if (two_arguments) | |
2382 | v1 = v0 = save_expr (v0); | |
2383 | else | |
2384 | { | |
2385 | v1 = c_fully_fold (v1, false, &maybe_const); | |
2386 | wrap &= maybe_const; | |
2387 | } | |
2388 | ||
2389 | mask = c_fully_fold (mask, false, &maybe_const); | |
2390 | wrap &= maybe_const; | |
2391 | } | |
68ea4406 | 2392 | else if (two_arguments) |
2393 | v1 = v0 = save_expr (v0); | |
bf0cb017 | 2394 | |
2395 | ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask); | |
2396 | ||
2397 | if (!c_dialect_cxx () && !wrap) | |
2398 | ret = c_wrap_maybe_const (ret, true); | |
2399 | ||
2400 | return ret; | |
2401 | } | |
2402 | ||
7f506bca | 2403 | /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum |
2404 | to integral type. */ | |
2405 | ||
2406 | static tree | |
2407 | c_common_get_narrower (tree op, int *unsignedp_ptr) | |
2408 | { | |
2409 | op = get_narrower (op, unsignedp_ptr); | |
2410 | ||
2411 | if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE | |
2412 | && ENUM_IS_SCOPED (TREE_TYPE (op))) | |
2413 | { | |
2414 | /* C++0x scoped enumerations don't implicitly convert to integral | |
2415 | type; if we stripped an explicit conversion to a larger type we | |
2416 | need to replace it so common_type will still work. */ | |
a51edb4c | 2417 | tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)), |
2418 | TYPE_UNSIGNED (TREE_TYPE (op))); | |
7f506bca | 2419 | op = fold_convert (type, op); |
2420 | } | |
2421 | return op; | |
2422 | } | |
2423 | ||
2561cea2 | 2424 | /* This is a helper function of build_binary_op. |
2425 | ||
2426 | For certain operations if both args were extended from the same | |
2427 | smaller type, do the arithmetic in that type and then extend. | |
2428 | ||
2429 | BITWISE indicates a bitwise operation. | |
2430 | For them, this optimization is safe only if | |
2431 | both args are zero-extended or both are sign-extended. | |
2432 | Otherwise, we might change the result. | |
2433 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
48e1416a | 2434 | but calculated in (unsigned short) it would be (unsigned short)-1. |
2561cea2 | 2435 | */ |
7f506bca | 2436 | tree |
2437 | shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise) | |
2561cea2 | 2438 | { |
2439 | int unsigned0, unsigned1; | |
2440 | tree arg0, arg1; | |
2441 | int uns; | |
2442 | tree type; | |
2443 | ||
2444 | /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents | |
2445 | excessive narrowing when we call get_narrower below. For | |
2446 | example, suppose that OP0 is of unsigned int extended | |
2447 | from signed char and that RESULT_TYPE is long long int. | |
2448 | If we explicitly cast OP0 to RESULT_TYPE, OP0 would look | |
2449 | like | |
48e1416a | 2450 | |
2561cea2 | 2451 | (long long int) (unsigned int) signed_char |
2452 | ||
2453 | which get_narrower would narrow down to | |
48e1416a | 2454 | |
2561cea2 | 2455 | (unsigned int) signed char |
48e1416a | 2456 | |
2561cea2 | 2457 | If we do not cast OP0 first, get_narrower would return |
2458 | signed_char, which is inconsistent with the case of the | |
2459 | explicit cast. */ | |
2460 | op0 = convert (result_type, op0); | |
2461 | op1 = convert (result_type, op1); | |
2462 | ||
7f506bca | 2463 | arg0 = c_common_get_narrower (op0, &unsigned0); |
2464 | arg1 = c_common_get_narrower (op1, &unsigned1); | |
2561cea2 | 2465 | |
2466 | /* UNS is 1 if the operation to be done is an unsigned one. */ | |
2467 | uns = TYPE_UNSIGNED (result_type); | |
2468 | ||
2469 | /* Handle the case that OP0 (or OP1) does not *contain* a conversion | |
2470 | but it *requires* conversion to FINAL_TYPE. */ | |
48e1416a | 2471 | |
2561cea2 | 2472 | if ((TYPE_PRECISION (TREE_TYPE (op0)) |
2473 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2474 | && TREE_TYPE (op0) != result_type) | |
2475 | unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); | |
2476 | if ((TYPE_PRECISION (TREE_TYPE (op1)) | |
2477 | == TYPE_PRECISION (TREE_TYPE (arg1))) | |
2478 | && TREE_TYPE (op1) != result_type) | |
2479 | unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
48e1416a | 2480 | |
2561cea2 | 2481 | /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ |
48e1416a | 2482 | |
2561cea2 | 2483 | /* For bitwise operations, signedness of nominal type |
2484 | does not matter. Consider only how operands were extended. */ | |
2485 | if (bitwise) | |
2486 | uns = unsigned0; | |
48e1416a | 2487 | |
2561cea2 | 2488 | /* Note that in all three cases below we refrain from optimizing |
2489 | an unsigned operation on sign-extended args. | |
2490 | That would not be valid. */ | |
48e1416a | 2491 | |
2561cea2 | 2492 | /* Both args variable: if both extended in same way |
2493 | from same width, do it in that width. | |
2494 | Do it unsigned if args were zero-extended. */ | |
2495 | if ((TYPE_PRECISION (TREE_TYPE (arg0)) | |
2496 | < TYPE_PRECISION (result_type)) | |
2497 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2498 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2499 | && unsigned0 == unsigned1 | |
2500 | && (unsigned0 || !uns)) | |
2501 | return c_common_signed_or_unsigned_type | |
2502 | (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); | |
2503 | ||
2504 | else if (TREE_CODE (arg0) == INTEGER_CST | |
2505 | && (unsigned1 || !uns) | |
2506 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2507 | < TYPE_PRECISION (result_type)) | |
2508 | && (type | |
2509 | = c_common_signed_or_unsigned_type (unsigned1, | |
2510 | TREE_TYPE (arg1))) | |
2511 | && !POINTER_TYPE_P (type) | |
2512 | && int_fits_type_p (arg0, type)) | |
2513 | return type; | |
2514 | ||
2515 | else if (TREE_CODE (arg1) == INTEGER_CST | |
2516 | && (unsigned0 || !uns) | |
2517 | && (TYPE_PRECISION (TREE_TYPE (arg0)) | |
2518 | < TYPE_PRECISION (result_type)) | |
2519 | && (type | |
2520 | = c_common_signed_or_unsigned_type (unsigned0, | |
2521 | TREE_TYPE (arg0))) | |
2522 | && !POINTER_TYPE_P (type) | |
2523 | && int_fits_type_p (arg1, type)) | |
2524 | return type; | |
2525 | ||
2526 | return result_type; | |
2527 | } | |
2528 | ||
22a75734 | 2529 | /* Checks if expression EXPR of real/integer type cannot be converted |
ca9d7d74 | 2530 | to the real/integer type TYPE. Function returns non-zero when: |
22a75734 | 2531 | * EXPR is a constant which cannot be exactly converted to TYPE. |
2532 | * EXPR is not a constant and size of EXPR's type > than size of TYPE, | |
7dfa155b | 2533 | for EXPR type and TYPE being both integers or both real. |
22a75734 | 2534 | * EXPR is not a constant of real type and TYPE is an integer. |
2535 | * EXPR is not a constant of integer type which cannot be | |
2536 | exactly converted to real type. | |
7dfa155b | 2537 | Function allows conversions between types of different signedness and |
ca9d7d74 | 2538 | can return SAFE_CONVERSION (zero) in that case. Function can produce |
2539 | signedness warnings if PRODUCE_WARNS is true. */ | |
22a75734 | 2540 | |
ca9d7d74 | 2541 | enum conversion_safety |
22a75734 | 2542 | unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns) |
d31d55f0 | 2543 | { |
ca9d7d74 | 2544 | enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */ |
27259707 | 2545 | tree expr_type = TREE_TYPE (expr); |
22a75734 | 2546 | loc = expansion_point_location_if_in_system_header (loc); |
d31d55f0 | 2547 | |
7dfa155b | 2548 | if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST) |
5b16c152 | 2549 | { |
d31d55f0 | 2550 | /* Warn for real constant that is not an exact integer converted |
7dfa155b | 2551 | to integer type. */ |
27259707 | 2552 | if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2553 | && TREE_CODE (type) == INTEGER_TYPE) |
2554 | { | |
2555 | if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) | |
ca9d7d74 | 2556 | give_warning = UNSAFE_REAL; |
7dfa155b | 2557 | } |
da1fb07b | 2558 | /* Warn for an integer constant that does not fit into integer type. */ |
27259707 | 2559 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2560 | && TREE_CODE (type) == INTEGER_TYPE |
2561 | && !int_fits_type_p (expr, type)) | |
2562 | { | |
2563 | if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type) | |
27259707 | 2564 | && tree_int_cst_sgn (expr) < 0) |
7dfa155b | 2565 | { |
2566 | if (produce_warns) | |
2567 | warning_at (loc, OPT_Wsign_conversion, "negative integer" | |
2568 | " implicitly converted to unsigned type"); | |
2569 | } | |
2570 | else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type)) | |
2571 | { | |
2572 | if (produce_warns) | |
2573 | warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned" | |
2574 | " constant value to negative integer"); | |
2575 | } | |
7ee0d227 | 2576 | else |
ca9d7d74 | 2577 | give_warning = UNSAFE_OTHER; |
7dfa155b | 2578 | } |
d31d55f0 | 2579 | else if (TREE_CODE (type) == REAL_TYPE) |
7dfa155b | 2580 | { |
2581 | /* Warn for an integer constant that does not fit into real type. */ | |
2582 | if (TREE_CODE (expr_type) == INTEGER_TYPE) | |
2583 | { | |
2584 | REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); | |
2585 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
ca9d7d74 | 2586 | give_warning = UNSAFE_REAL; |
7dfa155b | 2587 | } |
2588 | /* Warn for a real constant that does not fit into a smaller | |
2589 | real type. */ | |
2590 | else if (TREE_CODE (expr_type) == REAL_TYPE | |
2591 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
2592 | { | |
2593 | REAL_VALUE_TYPE a = TREE_REAL_CST (expr); | |
2594 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
ca9d7d74 | 2595 | give_warning = UNSAFE_REAL; |
7dfa155b | 2596 | } |
2597 | } | |
2598 | } | |
2599 | else | |
2600 | { | |
d31d55f0 | 2601 | /* Warn for real types converted to integer types. */ |
2561cea2 | 2602 | if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2603 | && TREE_CODE (type) == INTEGER_TYPE) |
ca9d7d74 | 2604 | give_warning = UNSAFE_REAL; |
d31d55f0 | 2605 | |
2561cea2 | 2606 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2607 | && TREE_CODE (type) == INTEGER_TYPE) |
2608 | { | |
69609004 | 2609 | /* Don't warn about unsigned char y = 0xff, x = (int) y; */ |
f9d856a4 | 2610 | expr = get_unwidened (expr, 0); |
2561cea2 | 2611 | expr_type = TREE_TYPE (expr); |
69609004 | 2612 | |
2561cea2 | 2613 | /* Don't warn for short y; short x = ((int)y & 0xff); */ |
48e1416a | 2614 | if (TREE_CODE (expr) == BIT_AND_EXPR |
7dfa155b | 2615 | || TREE_CODE (expr) == BIT_IOR_EXPR |
2561cea2 | 2616 | || TREE_CODE (expr) == BIT_XOR_EXPR) |
2617 | { | |
27259707 | 2618 | /* If both args were extended from a shortest type, |
2619 | use that type if that is safe. */ | |
48e1416a | 2620 | expr_type = shorten_binary_op (expr_type, |
2621 | TREE_OPERAND (expr, 0), | |
2622 | TREE_OPERAND (expr, 1), | |
2561cea2 | 2623 | /* bitwise */1); |
2624 | ||
2561cea2 | 2625 | if (TREE_CODE (expr) == BIT_AND_EXPR) |
2626 | { | |
2627 | tree op0 = TREE_OPERAND (expr, 0); | |
2628 | tree op1 = TREE_OPERAND (expr, 1); | |
30de145b | 2629 | bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
2630 | bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
2631 | ||
2632 | /* If one of the operands is a non-negative constant | |
2633 | that fits in the target type, then the type of the | |
2634 | other operand does not matter. */ | |
2561cea2 | 2635 | if ((TREE_CODE (op0) == INTEGER_CST |
2636 | && int_fits_type_p (op0, c_common_signed_type (type)) | |
2637 | && int_fits_type_p (op0, c_common_unsigned_type (type))) | |
2638 | || (TREE_CODE (op1) == INTEGER_CST | |
27259707 | 2639 | && int_fits_type_p (op1, c_common_signed_type (type)) |
48e1416a | 2640 | && int_fits_type_p (op1, |
27259707 | 2641 | c_common_unsigned_type (type)))) |
ca9d7d74 | 2642 | return SAFE_CONVERSION; |
30de145b | 2643 | /* If constant is unsigned and fits in the target |
2644 | type, then the result will also fit. */ | |
2645 | else if ((TREE_CODE (op0) == INTEGER_CST | |
48e1416a | 2646 | && unsigned0 |
30de145b | 2647 | && int_fits_type_p (op0, type)) |
2648 | || (TREE_CODE (op1) == INTEGER_CST | |
2649 | && unsigned1 | |
2650 | && int_fits_type_p (op1, type))) | |
ca9d7d74 | 2651 | return SAFE_CONVERSION; |
2561cea2 | 2652 | } |
2653 | } | |
7dfa155b | 2654 | /* Warn for integer types converted to smaller integer types. */ |
48e1416a | 2655 | if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
ca9d7d74 | 2656 | give_warning = UNSAFE_OTHER; |
7ee0d227 | 2657 | |
2658 | /* When they are the same width but different signedness, | |
2659 | then the value may change. */ | |
7dfa155b | 2660 | else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type) |
2561cea2 | 2661 | && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type)) |
7ee0d227 | 2662 | /* Even when converted to a bigger type, if the type is |
2663 | unsigned but expr is signed, then negative values | |
2664 | will be changed. */ | |
7dfa155b | 2665 | || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type))) |
2666 | && produce_warns) | |
200dd99c | 2667 | warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT " |
2668 | "may change the sign of the result", | |
2669 | type, expr_type); | |
7dfa155b | 2670 | } |
d31d55f0 | 2671 | |
2672 | /* Warn for integer types converted to real types if and only if | |
7dfa155b | 2673 | all the range of values of the integer type cannot be |
2674 | represented by the real type. */ | |
2561cea2 | 2675 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2676 | && TREE_CODE (type) == REAL_TYPE) |
2677 | { | |
4c2cfa81 | 2678 | tree type_low_bound, type_high_bound; |
7dfa155b | 2679 | REAL_VALUE_TYPE real_low_bound, real_high_bound; |
4c2cfa81 | 2680 | |
2681 | /* Don't warn about char y = 0xff; float x = (int) y; */ | |
2682 | expr = get_unwidened (expr, 0); | |
2683 | expr_type = TREE_TYPE (expr); | |
2684 | ||
7dfa155b | 2685 | type_low_bound = TYPE_MIN_VALUE (expr_type); |
2686 | type_high_bound = TYPE_MAX_VALUE (expr_type); | |
2687 | real_low_bound = real_value_from_int_cst (0, type_low_bound); | |
2688 | real_high_bound = real_value_from_int_cst (0, type_high_bound); | |
d31d55f0 | 2689 | |
7dfa155b | 2690 | if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound) |
2691 | || !exact_real_truncate (TYPE_MODE (type), &real_high_bound)) | |
ca9d7d74 | 2692 | give_warning = UNSAFE_OTHER; |
7dfa155b | 2693 | } |
d31d55f0 | 2694 | |
2695 | /* Warn for real types converted to smaller real types. */ | |
2561cea2 | 2696 | else if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2697 | && TREE_CODE (type) == REAL_TYPE |
2698 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
ca9d7d74 | 2699 | give_warning = UNSAFE_REAL; |
7dfa155b | 2700 | } |
2701 | ||
2702 | return give_warning; | |
2703 | } | |
2704 | ||
2705 | /* Warns if the conversion of EXPR to TYPE may alter a value. | |
2706 | This is a helper function for warnings_for_convert_and_check. */ | |
2707 | ||
2708 | static void | |
22a75734 | 2709 | conversion_warning (location_t loc, tree type, tree expr) |
7dfa155b | 2710 | { |
7dfa155b | 2711 | tree expr_type = TREE_TYPE (expr); |
ca9d7d74 | 2712 | enum conversion_safety conversion_kind; |
d31d55f0 | 2713 | |
ca9d7d74 | 2714 | if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion) |
7dfa155b | 2715 | return; |
d31d55f0 | 2716 | |
7dfa155b | 2717 | switch (TREE_CODE (expr)) |
2718 | { | |
2719 | case EQ_EXPR: | |
2720 | case NE_EXPR: | |
2721 | case LE_EXPR: | |
2722 | case GE_EXPR: | |
2723 | case LT_EXPR: | |
2724 | case GT_EXPR: | |
2725 | case TRUTH_ANDIF_EXPR: | |
2726 | case TRUTH_ORIF_EXPR: | |
2727 | case TRUTH_AND_EXPR: | |
2728 | case TRUTH_OR_EXPR: | |
2729 | case TRUTH_XOR_EXPR: | |
2730 | case TRUTH_NOT_EXPR: | |
2731 | /* Conversion from boolean to a signed:1 bit-field (which only | |
2732 | can hold the values 0 and -1) doesn't lose information - but | |
2733 | it does change the value. */ | |
2734 | if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) | |
2735 | warning_at (loc, OPT_Wconversion, | |
2736 | "conversion to %qT from boolean expression", type); | |
2737 | return; | |
2738 | ||
2739 | case REAL_CST: | |
2740 | case INTEGER_CST: | |
22a75734 | 2741 | conversion_kind = unsafe_conversion_p (loc, type, expr, true); |
ca9d7d74 | 2742 | if (conversion_kind == UNSAFE_REAL) |
2743 | warning_at (loc, OPT_Wfloat_conversion, | |
2744 | "conversion to %qT alters %qT constant value", | |
2745 | type, expr_type); | |
2746 | else if (conversion_kind) | |
7dfa155b | 2747 | warning_at (loc, OPT_Wconversion, |
2748 | "conversion to %qT alters %qT constant value", | |
2749 | type, expr_type); | |
2750 | return; | |
2751 | ||
2752 | case COND_EXPR: | |
2753 | { | |
0e4e775a | 2754 | /* In case of COND_EXPR, we do not care about the type of |
2755 | COND_EXPR, only about the conversion of each operand. */ | |
2756 | tree op1 = TREE_OPERAND (expr, 1); | |
2757 | tree op2 = TREE_OPERAND (expr, 2); | |
2758 | ||
22a75734 | 2759 | conversion_warning (loc, type, op1); |
2760 | conversion_warning (loc, type, op2); | |
0e4e775a | 2761 | return; |
7dfa155b | 2762 | } |
2763 | ||
2764 | default: /* 'expr' is not a constant. */ | |
22a75734 | 2765 | conversion_kind = unsafe_conversion_p (loc, type, expr, true); |
ca9d7d74 | 2766 | if (conversion_kind == UNSAFE_REAL) |
2767 | warning_at (loc, OPT_Wfloat_conversion, | |
2768 | "conversion to %qT from %qT may alter its value", | |
2769 | type, expr_type); | |
2770 | else if (conversion_kind) | |
7dfa155b | 2771 | warning_at (loc, OPT_Wconversion, |
200dd99c | 2772 | "conversion to %qT from %qT may alter its value", |
2773 | type, expr_type); | |
d31d55f0 | 2774 | } |
2775 | } | |
2776 | ||
59dd8856 | 2777 | /* Produce warnings after a conversion. RESULT is the result of |
2778 | converting EXPR to TYPE. This is a helper function for | |
2779 | convert_and_check and cp_convert_and_check. */ | |
2a1736ed | 2780 | |
59dd8856 | 2781 | void |
22a75734 | 2782 | warnings_for_convert_and_check (location_t loc, tree type, tree expr, |
2783 | tree result) | |
2a1736ed | 2784 | { |
22a75734 | 2785 | loc = expansion_point_location_if_in_system_header (loc); |
61f69bc9 | 2786 | |
da1fb07b | 2787 | if (TREE_CODE (expr) == INTEGER_CST |
2788 | && (TREE_CODE (type) == INTEGER_TYPE | |
2789 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
2790 | && !int_fits_type_p (expr, type)) | |
2791 | { | |
d31d55f0 | 2792 | /* Do not diagnose overflow in a constant expression merely |
2793 | because a conversion overflowed. */ | |
da1fb07b | 2794 | if (TREE_OVERFLOW (result)) |
eddad94a | 2795 | TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); |
2796 | ||
da1fb07b | 2797 | if (TYPE_UNSIGNED (type)) |
d31d55f0 | 2798 | { |
da1fb07b | 2799 | /* This detects cases like converting -129 or 256 to |
2800 | unsigned char. */ | |
2801 | if (!int_fits_type_p (expr, c_common_signed_type (type))) | |
61f69bc9 | 2802 | warning_at (loc, OPT_Woverflow, |
2803 | "large integer implicitly truncated to unsigned type"); | |
7ee0d227 | 2804 | else |
22a75734 | 2805 | conversion_warning (loc, type, expr); |
da1fb07b | 2806 | } |
48e1416a | 2807 | else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) |
22a75734 | 2808 | warning_at (loc, OPT_Woverflow, |
e0913805 | 2809 | "overflow in implicit constant conversion"); |
2810 | /* No warning for converting 0x80000000 to int. */ | |
2811 | else if (pedantic | |
2812 | && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE | |
2813 | || TYPE_PRECISION (TREE_TYPE (expr)) | |
2814 | != TYPE_PRECISION (type))) | |
61f69bc9 | 2815 | warning_at (loc, OPT_Woverflow, |
2816 | "overflow in implicit constant conversion"); | |
e0913805 | 2817 | |
7ee0d227 | 2818 | else |
22a75734 | 2819 | conversion_warning (loc, type, expr); |
2a1736ed | 2820 | } |
9421ebb9 | 2821 | else if ((TREE_CODE (result) == INTEGER_CST |
2822 | || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) | |
61f69bc9 | 2823 | warning_at (loc, OPT_Woverflow, |
2824 | "overflow in implicit constant conversion"); | |
7ee0d227 | 2825 | else |
22a75734 | 2826 | conversion_warning (loc, type, expr); |
59dd8856 | 2827 | } |
2828 | ||
2829 | ||
2830 | /* Convert EXPR to TYPE, warning about conversion problems with constants. | |
2831 | Invoke this function on every expression that is converted implicitly, | |
2832 | i.e. because of language rules and not because of an explicit cast. */ | |
2833 | ||
2834 | tree | |
22a75734 | 2835 | convert_and_check (location_t loc, tree type, tree expr) |
59dd8856 | 2836 | { |
2837 | tree result; | |
c6418a4e | 2838 | tree expr_for_warning; |
2839 | ||
2840 | /* Convert from a value with possible excess precision rather than | |
2841 | via the semantic type, but do not warn about values not fitting | |
2842 | exactly in the semantic type. */ | |
2843 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
2844 | { | |
2845 | tree orig_type = TREE_TYPE (expr); | |
2846 | expr = TREE_OPERAND (expr, 0); | |
2847 | expr_for_warning = convert (orig_type, expr); | |
2848 | if (orig_type == type) | |
2849 | return expr_for_warning; | |
2850 | } | |
2851 | else | |
2852 | expr_for_warning = expr; | |
59dd8856 | 2853 | |
2854 | if (TREE_TYPE (expr) == type) | |
2855 | return expr; | |
48e1416a | 2856 | |
59dd8856 | 2857 | result = convert (type, expr); |
2858 | ||
48d94ede | 2859 | if (c_inhibit_evaluation_warnings == 0 |
2860 | && !TREE_OVERFLOW_P (expr) | |
2861 | && result != error_mark_node) | |
22a75734 | 2862 | warnings_for_convert_and_check (loc, type, expr_for_warning, result); |
59dd8856 | 2863 | |
da1fb07b | 2864 | return result; |
b2806639 | 2865 | } |
2866 | \f | |
4e91a871 | 2867 | /* A node in a list that describes references to variables (EXPR), which are |
2868 | either read accesses if WRITER is zero, or write accesses, in which case | |
2869 | WRITER is the parent of EXPR. */ | |
2870 | struct tlist | |
2871 | { | |
2872 | struct tlist *next; | |
2873 | tree expr, writer; | |
2874 | }; | |
2875 | ||
2876 | /* Used to implement a cache the results of a call to verify_tree. We only | |
2877 | use this for SAVE_EXPRs. */ | |
2878 | struct tlist_cache | |
2879 | { | |
2880 | struct tlist_cache *next; | |
2881 | struct tlist *cache_before_sp; | |
2882 | struct tlist *cache_after_sp; | |
2883 | tree expr; | |
481c6ce6 | 2884 | }; |
2885 | ||
4e91a871 | 2886 | /* Obstack to use when allocating tlist structures, and corresponding |
2887 | firstobj. */ | |
2888 | static struct obstack tlist_obstack; | |
2889 | static char *tlist_firstobj = 0; | |
2890 | ||
2891 | /* Keep track of the identifiers we've warned about, so we can avoid duplicate | |
2892 | warnings. */ | |
2893 | static struct tlist *warned_ids; | |
2894 | /* SAVE_EXPRs need special treatment. We process them only once and then | |
2895 | cache the results. */ | |
2896 | static struct tlist_cache *save_expr_cache; | |
2897 | ||
1cae46be | 2898 | static void add_tlist (struct tlist **, struct tlist *, tree, int); |
2899 | static void merge_tlist (struct tlist **, struct tlist *, int); | |
2900 | static void verify_tree (tree, struct tlist **, struct tlist **, tree); | |
2901 | static int warning_candidate_p (tree); | |
79973b57 | 2902 | static bool candidate_equal_p (const_tree, const_tree); |
1cae46be | 2903 | static void warn_for_collisions (struct tlist *); |
2904 | static void warn_for_collisions_1 (tree, tree, struct tlist *, int); | |
2905 | static struct tlist *new_tlist (struct tlist *, tree, tree); | |
481c6ce6 | 2906 | |
4e91a871 | 2907 | /* Create a new struct tlist and fill in its fields. */ |
2908 | static struct tlist * | |
1cae46be | 2909 | new_tlist (struct tlist *next, tree t, tree writer) |
4e91a871 | 2910 | { |
2911 | struct tlist *l; | |
9318f22c | 2912 | l = XOBNEW (&tlist_obstack, struct tlist); |
4e91a871 | 2913 | l->next = next; |
2914 | l->expr = t; | |
2915 | l->writer = writer; | |
2916 | return l; | |
2917 | } | |
2918 | ||
2919 | /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER | |
2920 | is nonnull, we ignore any node we find which has a writer equal to it. */ | |
2921 | ||
2922 | static void | |
1cae46be | 2923 | add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy) |
4e91a871 | 2924 | { |
2925 | while (add) | |
2926 | { | |
2927 | struct tlist *next = add->next; | |
84166705 | 2928 | if (!copy) |
4e91a871 | 2929 | add->next = *to; |
79973b57 | 2930 | if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer)) |
4e91a871 | 2931 | *to = copy ? new_tlist (*to, add->expr, add->writer) : add; |
2932 | add = next; | |
2933 | } | |
2934 | } | |
2935 | ||
2936 | /* Merge the nodes of ADD into TO. This merging process is done so that for | |
2937 | each variable that already exists in TO, no new node is added; however if | |
2938 | there is a write access recorded in ADD, and an occurrence on TO is only | |
2939 | a read access, then the occurrence in TO will be modified to record the | |
2940 | write. */ | |
481c6ce6 | 2941 | |
2942 | static void | |
1cae46be | 2943 | merge_tlist (struct tlist **to, struct tlist *add, int copy) |
4e91a871 | 2944 | { |
2945 | struct tlist **end = to; | |
2946 | ||
2947 | while (*end) | |
2948 | end = &(*end)->next; | |
2949 | ||
2950 | while (add) | |
2951 | { | |
2952 | int found = 0; | |
2953 | struct tlist *tmp2; | |
2954 | struct tlist *next = add->next; | |
2955 | ||
2956 | for (tmp2 = *to; tmp2; tmp2 = tmp2->next) | |
79973b57 | 2957 | if (candidate_equal_p (tmp2->expr, add->expr)) |
4e91a871 | 2958 | { |
2959 | found = 1; | |
84166705 | 2960 | if (!tmp2->writer) |
4e91a871 | 2961 | tmp2->writer = add->writer; |
2962 | } | |
84166705 | 2963 | if (!found) |
4e91a871 | 2964 | { |
2965 | *end = copy ? add : new_tlist (NULL, add->expr, add->writer); | |
2966 | end = &(*end)->next; | |
2967 | *end = 0; | |
2968 | } | |
2969 | add = next; | |
2970 | } | |
2971 | } | |
2972 | ||
2973 | /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable | |
2974 | references in list LIST conflict with it, excluding reads if ONLY writers | |
2975 | is nonzero. */ | |
2976 | ||
2977 | static void | |
1cae46be | 2978 | warn_for_collisions_1 (tree written, tree writer, struct tlist *list, |
2979 | int only_writes) | |
4e91a871 | 2980 | { |
2981 | struct tlist *tmp; | |
2982 | ||
2983 | /* Avoid duplicate warnings. */ | |
2984 | for (tmp = warned_ids; tmp; tmp = tmp->next) | |
79973b57 | 2985 | if (candidate_equal_p (tmp->expr, written)) |
4e91a871 | 2986 | return; |
2987 | ||
2988 | while (list) | |
2989 | { | |
79973b57 | 2990 | if (candidate_equal_p (list->expr, written) |
2991 | && !candidate_equal_p (list->writer, writer) | |
2992 | && (!only_writes || list->writer)) | |
4e91a871 | 2993 | { |
2994 | warned_ids = new_tlist (warned_ids, written, NULL_TREE); | |
3df42822 | 2995 | warning_at (EXPR_LOC_OR_LOC (writer, input_location), |
6513b50d | 2996 | OPT_Wsequence_point, "operation on %qE may be undefined", |
2997 | list->expr); | |
4e91a871 | 2998 | } |
2999 | list = list->next; | |
3000 | } | |
3001 | } | |
3002 | ||
3003 | /* Given a list LIST of references to variables, find whether any of these | |
3004 | can cause conflicts due to missing sequence points. */ | |
3005 | ||
3006 | static void | |
1cae46be | 3007 | warn_for_collisions (struct tlist *list) |
4e91a871 | 3008 | { |
3009 | struct tlist *tmp; | |
1cae46be | 3010 | |
4e91a871 | 3011 | for (tmp = list; tmp; tmp = tmp->next) |
3012 | { | |
3013 | if (tmp->writer) | |
3014 | warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); | |
3015 | } | |
3016 | } | |
3017 | ||
734c98be | 3018 | /* Return nonzero if X is a tree that can be verified by the sequence point |
4e91a871 | 3019 | warnings. */ |
3020 | static int | |
1cae46be | 3021 | warning_candidate_p (tree x) |
481c6ce6 | 3022 | { |
6ef8d12f | 3023 | if (DECL_P (x) && DECL_ARTIFICIAL (x)) |
3024 | return 0; | |
3025 | ||
027fc6ef | 3026 | if (TREE_CODE (x) == BLOCK) |
3027 | return 0; | |
3028 | ||
6ef8d12f | 3029 | /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c |
79973b57 | 3030 | (lvalue_p) crash on TRY/CATCH. */ |
6ef8d12f | 3031 | if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x))) |
3032 | return 0; | |
3033 | ||
3034 | if (!lvalue_p (x)) | |
3035 | return 0; | |
3036 | ||
3037 | /* No point to track non-const calls, they will never satisfy | |
3038 | operand_equal_p. */ | |
3039 | if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0) | |
3040 | return 0; | |
3041 | ||
3042 | if (TREE_CODE (x) == STRING_CST) | |
3043 | return 0; | |
3044 | ||
3045 | return 1; | |
79973b57 | 3046 | } |
3047 | ||
3048 | /* Return nonzero if X and Y appear to be the same candidate (or NULL) */ | |
3049 | static bool | |
3050 | candidate_equal_p (const_tree x, const_tree y) | |
3051 | { | |
3052 | return (x == y) || (x && y && operand_equal_p (x, y, 0)); | |
4e91a871 | 3053 | } |
481c6ce6 | 3054 | |
4e91a871 | 3055 | /* Walk the tree X, and record accesses to variables. If X is written by the |
3056 | parent tree, WRITER is the parent. | |
3057 | We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this | |
3058 | expression or its only operand forces a sequence point, then everything up | |
3059 | to the sequence point is stored in PBEFORE_SP. Everything else gets stored | |
3060 | in PNO_SP. | |
3061 | Once we return, we will have emitted warnings if any subexpression before | |
3062 | such a sequence point could be undefined. On a higher level, however, the | |
3063 | sequence point may not be relevant, and we'll merge the two lists. | |
3064 | ||
3065 | Example: (b++, a) + b; | |
3066 | The call that processes the COMPOUND_EXPR will store the increment of B | |
3067 | in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that | |
3068 | processes the PLUS_EXPR will need to merge the two lists so that | |
3069 | eventually, all accesses end up on the same list (and we'll warn about the | |
3070 | unordered subexpressions b++ and b. | |
3071 | ||
3072 | A note on merging. If we modify the former example so that our expression | |
3073 | becomes | |
3074 | (b++, b) + a | |
3075 | care must be taken not simply to add all three expressions into the final | |
3076 | PNO_SP list. The function merge_tlist takes care of that by merging the | |
3077 | before-SP list of the COMPOUND_EXPR into its after-SP list in a special | |
3078 | way, so that no more than one access to B is recorded. */ | |
481c6ce6 | 3079 | |
4e91a871 | 3080 | static void |
1cae46be | 3081 | verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp, |
3082 | tree writer) | |
4e91a871 | 3083 | { |
3084 | struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; | |
3085 | enum tree_code code; | |
ce45a448 | 3086 | enum tree_code_class cl; |
481c6ce6 | 3087 | |
e5b75768 | 3088 | /* X may be NULL if it is the operand of an empty statement expression |
3089 | ({ }). */ | |
3090 | if (x == NULL) | |
3091 | return; | |
3092 | ||
4e91a871 | 3093 | restart: |
3094 | code = TREE_CODE (x); | |
e916c70c | 3095 | cl = TREE_CODE_CLASS (code); |
481c6ce6 | 3096 | |
4e91a871 | 3097 | if (warning_candidate_p (x)) |
79973b57 | 3098 | *pno_sp = new_tlist (*pno_sp, x, writer); |
4e91a871 | 3099 | |
3100 | switch (code) | |
3101 | { | |
67b28e3e | 3102 | case CONSTRUCTOR: |
8e71dad2 | 3103 | case SIZEOF_EXPR: |
67b28e3e | 3104 | return; |
3105 | ||
4e91a871 | 3106 | case COMPOUND_EXPR: |
3107 | case TRUTH_ANDIF_EXPR: | |
3108 | case TRUTH_ORIF_EXPR: | |
3109 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
3110 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3111 | warn_for_collisions (tmp_nosp); | |
3112 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3113 | merge_tlist (pbefore_sp, tmp_nosp, 0); | |
3114 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); | |
3115 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3116 | return; | |
3117 | ||
3118 | case COND_EXPR: | |
3119 | tmp_before = tmp_list2 = 0; | |
3120 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); | |
3121 | warn_for_collisions (tmp_list2); | |
3122 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3123 | merge_tlist (pbefore_sp, tmp_list2, 1); | |
3124 | ||
3125 | tmp_list3 = tmp_nosp = 0; | |
3126 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); | |
3127 | warn_for_collisions (tmp_nosp); | |
3128 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3129 | ||
3130 | tmp_list3 = tmp_list2 = 0; | |
3131 | verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); | |
3132 | warn_for_collisions (tmp_list2); | |
3133 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3134 | /* Rather than add both tmp_nosp and tmp_list2, we have to merge the | |
3135 | two first, to avoid warning for (a ? b++ : b++). */ | |
3136 | merge_tlist (&tmp_nosp, tmp_list2, 0); | |
3137 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
3138 | return; | |
3139 | ||
481c6ce6 | 3140 | case PREDECREMENT_EXPR: |
3141 | case PREINCREMENT_EXPR: | |
3142 | case POSTDECREMENT_EXPR: | |
3143 | case POSTINCREMENT_EXPR: | |
4e91a871 | 3144 | verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); |
3145 | return; | |
3146 | ||
3147 | case MODIFY_EXPR: | |
3148 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
3149 | verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); | |
3150 | verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); | |
3151 | /* Expressions inside the LHS are not ordered wrt. the sequence points | |
3152 | in the RHS. Example: | |
3153 | *a = (a++, 2) | |
3154 | Despite the fact that the modification of "a" is in the before_sp | |
3155 | list (tmp_before), it conflicts with the use of "a" in the LHS. | |
3156 | We can handle this by adding the contents of tmp_list3 | |
3157 | to those of tmp_before, and redoing the collision warnings for that | |
3158 | list. */ | |
3159 | add_tlist (&tmp_before, tmp_list3, x, 1); | |
3160 | warn_for_collisions (tmp_before); | |
3161 | /* Exclude the LHS itself here; we first have to merge it into the | |
3162 | tmp_nosp list. This is done to avoid warning for "a = a"; if we | |
3163 | didn't exclude the LHS, we'd get it twice, once as a read and once | |
3164 | as a write. */ | |
3165 | add_tlist (pno_sp, tmp_list3, x, 0); | |
3166 | warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); | |
3167 | ||
3168 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3169 | if (warning_candidate_p (TREE_OPERAND (x, 0))) | |
3170 | merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); | |
3171 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); | |
3172 | return; | |
481c6ce6 | 3173 | |
3174 | case CALL_EXPR: | |
4e91a871 | 3175 | /* We need to warn about conflicts among arguments and conflicts between |
3176 | args and the function address. Side effects of the function address, | |
3177 | however, are not ordered by the sequence point of the call. */ | |
c2f47e15 | 3178 | { |
3179 | call_expr_arg_iterator iter; | |
3180 | tree arg; | |
48e1416a | 3181 | tmp_before = tmp_nosp = 0; |
c2f47e15 | 3182 | verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE); |
3183 | FOR_EACH_CALL_EXPR_ARG (arg, iter, x) | |
3184 | { | |
3185 | tmp_list2 = tmp_list3 = 0; | |
3186 | verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE); | |
3187 | merge_tlist (&tmp_list3, tmp_list2, 0); | |
3188 | add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); | |
3189 | } | |
3190 | add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); | |
3191 | warn_for_collisions (tmp_before); | |
3192 | add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); | |
3193 | return; | |
3194 | } | |
481c6ce6 | 3195 | |
3196 | case TREE_LIST: | |
3197 | /* Scan all the list, e.g. indices of multi dimensional array. */ | |
3198 | while (x) | |
3199 | { | |
4e91a871 | 3200 | tmp_before = tmp_nosp = 0; |
3201 | verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
3202 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
3203 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
481c6ce6 | 3204 | x = TREE_CHAIN (x); |
3205 | } | |
4e91a871 | 3206 | return; |
481c6ce6 | 3207 | |
4e91a871 | 3208 | case SAVE_EXPR: |
3209 | { | |
3210 | struct tlist_cache *t; | |
3211 | for (t = save_expr_cache; t; t = t->next) | |
79973b57 | 3212 | if (candidate_equal_p (t->expr, x)) |
4e91a871 | 3213 | break; |
481c6ce6 | 3214 | |
84166705 | 3215 | if (!t) |
481c6ce6 | 3216 | { |
9318f22c | 3217 | t = XOBNEW (&tlist_obstack, struct tlist_cache); |
4e91a871 | 3218 | t->next = save_expr_cache; |
3219 | t->expr = x; | |
3220 | save_expr_cache = t; | |
3221 | ||
3222 | tmp_before = tmp_nosp = 0; | |
3223 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3224 | warn_for_collisions (tmp_nosp); | |
3225 | ||
3226 | tmp_list3 = 0; | |
3227 | while (tmp_nosp) | |
3228 | { | |
3229 | struct tlist *t = tmp_nosp; | |
3230 | tmp_nosp = t->next; | |
3231 | merge_tlist (&tmp_list3, t, 0); | |
3232 | } | |
3233 | t->cache_before_sp = tmp_before; | |
3234 | t->cache_after_sp = tmp_list3; | |
481c6ce6 | 3235 | } |
4e91a871 | 3236 | merge_tlist (pbefore_sp, t->cache_before_sp, 1); |
3237 | add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); | |
3238 | return; | |
3239 | } | |
481c6ce6 | 3240 | |
012916cb | 3241 | case ADDR_EXPR: |
3242 | x = TREE_OPERAND (x, 0); | |
3243 | if (DECL_P (x)) | |
3244 | return; | |
3245 | writer = 0; | |
3246 | goto restart; | |
3247 | ||
ce45a448 | 3248 | default: |
3249 | /* For other expressions, simply recurse on their operands. | |
a0c938f0 | 3250 | Manual tail recursion for unary expressions. |
ce45a448 | 3251 | Other non-expressions need not be processed. */ |
3252 | if (cl == tcc_unary) | |
3253 | { | |
ce45a448 | 3254 | x = TREE_OPERAND (x, 0); |
3255 | writer = 0; | |
3256 | goto restart; | |
3257 | } | |
3258 | else if (IS_EXPR_CODE_CLASS (cl)) | |
3259 | { | |
3260 | int lp; | |
c2f47e15 | 3261 | int max = TREE_OPERAND_LENGTH (x); |
ce45a448 | 3262 | for (lp = 0; lp < max; lp++) |
3263 | { | |
3264 | tmp_before = tmp_nosp = 0; | |
3265 | verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0); | |
3266 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
3267 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
3268 | } | |
3269 | } | |
3270 | return; | |
481c6ce6 | 3271 | } |
481c6ce6 | 3272 | } |
3273 | ||
974e2c0c | 3274 | /* Try to warn for undefined behavior in EXPR due to missing sequence |
481c6ce6 | 3275 | points. */ |
3276 | ||
4b987fac | 3277 | DEBUG_FUNCTION void |
1cae46be | 3278 | verify_sequence_points (tree expr) |
481c6ce6 | 3279 | { |
4e91a871 | 3280 | struct tlist *before_sp = 0, *after_sp = 0; |
481c6ce6 | 3281 | |
4e91a871 | 3282 | warned_ids = 0; |
3283 | save_expr_cache = 0; | |
3284 | if (tlist_firstobj == 0) | |
481c6ce6 | 3285 | { |
4e91a871 | 3286 | gcc_obstack_init (&tlist_obstack); |
4fd61bc6 | 3287 | tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0); |
481c6ce6 | 3288 | } |
3289 | ||
4e91a871 | 3290 | verify_tree (expr, &before_sp, &after_sp, 0); |
3291 | warn_for_collisions (after_sp); | |
3292 | obstack_free (&tlist_obstack, tlist_firstobj); | |
481c6ce6 | 3293 | } |
b0fc3e72 | 3294 | \f |
3295 | /* Validate the expression after `case' and apply default promotions. */ | |
3296 | ||
2ca392fd | 3297 | static tree |
1cae46be | 3298 | check_case_value (tree value) |
b0fc3e72 | 3299 | { |
3300 | if (value == NULL_TREE) | |
3301 | return value; | |
3302 | ||
b96dc121 | 3303 | if (TREE_CODE (value) == INTEGER_CST) |
3304 | /* Promote char or short to int. */ | |
3305 | value = perform_integral_promotions (value); | |
3306 | else if (value != error_mark_node) | |
b0fc3e72 | 3307 | { |
3308 | error ("case label does not reduce to an integer constant"); | |
3309 | value = error_mark_node; | |
3310 | } | |
b0fc3e72 | 3311 | |
6433f1c2 | 3312 | constant_expression_warning (value); |
3313 | ||
b0fc3e72 | 3314 | return value; |
3315 | } | |
3316 | \f | |
2ca392fd | 3317 | /* See if the case values LOW and HIGH are in the range of the original |
5c9dae64 | 3318 | type (i.e. before the default conversion to int) of the switch testing |
2ca392fd | 3319 | expression. |
3320 | TYPE is the promoted type of the testing expression, and ORIG_TYPE is | |
91275768 | 3321 | the type before promoting it. CASE_LOW_P is a pointer to the lower |
2ca392fd | 3322 | bound of the case label, and CASE_HIGH_P is the upper bound or NULL |
3323 | if the case is not a case range. | |
3324 | The caller has to make sure that we are not called with NULL for | |
5c9dae64 | 3325 | CASE_LOW_P (i.e. the default case). |
442e3cb9 | 3326 | Returns true if the case label is in range of ORIG_TYPE (saturated or |
2ca392fd | 3327 | untouched) or false if the label is out of range. */ |
3328 | ||
3329 | static bool | |
3330 | check_case_bounds (tree type, tree orig_type, | |
3331 | tree *case_low_p, tree *case_high_p) | |
3332 | { | |
3333 | tree min_value, max_value; | |
3334 | tree case_low = *case_low_p; | |
3335 | tree case_high = case_high_p ? *case_high_p : case_low; | |
3336 | ||
3337 | /* If there was a problem with the original type, do nothing. */ | |
3338 | if (orig_type == error_mark_node) | |
3339 | return true; | |
3340 | ||
3341 | min_value = TYPE_MIN_VALUE (orig_type); | |
3342 | max_value = TYPE_MAX_VALUE (orig_type); | |
3343 | ||
3344 | /* Case label is less than minimum for type. */ | |
3345 | if (tree_int_cst_compare (case_low, min_value) < 0 | |
3346 | && tree_int_cst_compare (case_high, min_value) < 0) | |
3347 | { | |
c3ceba8e | 3348 | warning (0, "case label value is less than minimum value for type"); |
2ca392fd | 3349 | return false; |
3350 | } | |
b27ac6b5 | 3351 | |
2ca392fd | 3352 | /* Case value is greater than maximum for type. */ |
3353 | if (tree_int_cst_compare (case_low, max_value) > 0 | |
3354 | && tree_int_cst_compare (case_high, max_value) > 0) | |
3355 | { | |
c3ceba8e | 3356 | warning (0, "case label value exceeds maximum value for type"); |
2ca392fd | 3357 | return false; |
3358 | } | |
3359 | ||
3360 | /* Saturate lower case label value to minimum. */ | |
3361 | if (tree_int_cst_compare (case_high, min_value) >= 0 | |
3362 | && tree_int_cst_compare (case_low, min_value) < 0) | |
3363 | { | |
c3ceba8e | 3364 | warning (0, "lower value in case label range" |
2ca392fd | 3365 | " less than minimum value for type"); |
3366 | case_low = min_value; | |
3367 | } | |
b27ac6b5 | 3368 | |
2ca392fd | 3369 | /* Saturate upper case label value to maximum. */ |
3370 | if (tree_int_cst_compare (case_low, max_value) <= 0 | |
3371 | && tree_int_cst_compare (case_high, max_value) > 0) | |
3372 | { | |
c3ceba8e | 3373 | warning (0, "upper value in case label range" |
2ca392fd | 3374 | " exceeds maximum value for type"); |
3375 | case_high = max_value; | |
3376 | } | |
3377 | ||
3378 | if (*case_low_p != case_low) | |
3379 | *case_low_p = convert (type, case_low); | |
3380 | if (case_high_p && *case_high_p != case_high) | |
3381 | *case_high_p = convert (type, case_high); | |
3382 | ||
3383 | return true; | |
3384 | } | |
3385 | \f | |
b0fc3e72 | 3386 | /* Return an integer type with BITS bits of precision, |
3387 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
3388 | ||
3389 | tree | |
1cae46be | 3390 | c_common_type_for_size (unsigned int bits, int unsignedp) |
b0fc3e72 | 3391 | { |
46375237 | 3392 | if (bits == TYPE_PRECISION (integer_type_node)) |
3393 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3394 | ||
bacde65a | 3395 | if (bits == TYPE_PRECISION (signed_char_type_node)) |
b0fc3e72 | 3396 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
3397 | ||
bacde65a | 3398 | if (bits == TYPE_PRECISION (short_integer_type_node)) |
b0fc3e72 | 3399 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
3400 | ||
bacde65a | 3401 | if (bits == TYPE_PRECISION (long_integer_type_node)) |
b0fc3e72 | 3402 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
3403 | ||
bacde65a | 3404 | if (bits == TYPE_PRECISION (long_long_integer_type_node)) |
b0fc3e72 | 3405 | return (unsignedp ? long_long_unsigned_type_node |
3406 | : long_long_integer_type_node); | |
3407 | ||
6388cfe2 | 3408 | if (int128_integer_type_node |
3409 | && bits == TYPE_PRECISION (int128_integer_type_node)) | |
3410 | return (unsignedp ? int128_unsigned_type_node | |
3411 | : int128_integer_type_node); | |
3412 | ||
f57fa2ea | 3413 | if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) |
3414 | return (unsignedp ? widest_unsigned_literal_type_node | |
3415 | : widest_integer_literal_type_node); | |
3416 | ||
bacde65a | 3417 | if (bits <= TYPE_PRECISION (intQI_type_node)) |
3418 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3419 | ||
3420 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
3421 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3422 | ||
3423 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
3424 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3425 | ||
3426 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
3427 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3428 | ||
b0fc3e72 | 3429 | return 0; |
3430 | } | |
3431 | ||
9421ebb9 | 3432 | /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits |
3433 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed; | |
3434 | and saturating if SATP is nonzero, otherwise not saturating. */ | |
3435 | ||
3436 | tree | |
3437 | c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit, | |
3438 | int unsignedp, int satp) | |
3439 | { | |
3440 | enum machine_mode mode; | |
3441 | if (ibit == 0) | |
3442 | mode = unsignedp ? UQQmode : QQmode; | |
3443 | else | |
3444 | mode = unsignedp ? UHAmode : HAmode; | |
3445 | ||
3446 | for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode)) | |
3447 | if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit) | |
3448 | break; | |
3449 | ||
3450 | if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode)) | |
3451 | { | |
3452 | sorry ("GCC cannot support operators with integer types and " | |
3453 | "fixed-point types that have too many integral and " | |
3454 | "fractional bits together"); | |
3455 | return 0; | |
3456 | } | |
3457 | ||
3458 | return c_common_type_for_mode (mode, satp); | |
3459 | } | |
3460 | ||
5b247e9f | 3461 | /* Used for communication between c_common_type_for_mode and |
3462 | c_register_builtin_type. */ | |
3463 | static GTY(()) tree registered_builtin_types; | |
3464 | ||
b0fc3e72 | 3465 | /* Return a data type that has machine mode MODE. |
3466 | If the mode is an integer, | |
9421ebb9 | 3467 | then UNSIGNEDP selects between signed and unsigned types. |
3468 | If the mode is a fixed-point mode, | |
3469 | then UNSIGNEDP selects between saturating and nonsaturating types. */ | |
b0fc3e72 | 3470 | |
3471 | tree | |
1cae46be | 3472 | c_common_type_for_mode (enum machine_mode mode, int unsignedp) |
b0fc3e72 | 3473 | { |
5b247e9f | 3474 | tree t; |
3475 | ||
46375237 | 3476 | if (mode == TYPE_MODE (integer_type_node)) |
3477 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3478 | ||
b0fc3e72 | 3479 | if (mode == TYPE_MODE (signed_char_type_node)) |
3480 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3481 | ||
3482 | if (mode == TYPE_MODE (short_integer_type_node)) | |
3483 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3484 | ||
b0fc3e72 | 3485 | if (mode == TYPE_MODE (long_integer_type_node)) |
3486 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3487 | ||
3488 | if (mode == TYPE_MODE (long_long_integer_type_node)) | |
3489 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
3490 | ||
6388cfe2 | 3491 | if (int128_integer_type_node |
3492 | && mode == TYPE_MODE (int128_integer_type_node)) | |
3493 | return unsignedp ? int128_unsigned_type_node : int128_integer_type_node; | |
3494 | ||
f57fa2ea | 3495 | if (mode == TYPE_MODE (widest_integer_literal_type_node)) |
44e9fa65 | 3496 | return unsignedp ? widest_unsigned_literal_type_node |
4ee9c684 | 3497 | : widest_integer_literal_type_node; |
f57fa2ea | 3498 | |
88ae7f04 | 3499 | if (mode == QImode) |
bacde65a | 3500 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
3501 | ||
88ae7f04 | 3502 | if (mode == HImode) |
bacde65a | 3503 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
3504 | ||
88ae7f04 | 3505 | if (mode == SImode) |
bacde65a | 3506 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
3507 | ||
88ae7f04 | 3508 | if (mode == DImode) |
bacde65a | 3509 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
3510 | ||
cc1cc1c7 | 3511 | #if HOST_BITS_PER_WIDE_INT >= 64 |
6274009c | 3512 | if (mode == TYPE_MODE (intTI_type_node)) |
3513 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
cc1cc1c7 | 3514 | #endif |
6274009c | 3515 | |
b0fc3e72 | 3516 | if (mode == TYPE_MODE (float_type_node)) |
3517 | return float_type_node; | |
3518 | ||
3519 | if (mode == TYPE_MODE (double_type_node)) | |
3520 | return double_type_node; | |
3521 | ||
3522 | if (mode == TYPE_MODE (long_double_type_node)) | |
3523 | return long_double_type_node; | |
3524 | ||
545c2bde | 3525 | if (mode == TYPE_MODE (void_type_node)) |
3526 | return void_type_node; | |
b27ac6b5 | 3527 | |
b0fc3e72 | 3528 | if (mode == TYPE_MODE (build_pointer_type (char_type_node))) |
61b9b73c | 3529 | return (unsignedp |
3530 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
3531 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 3532 | |
3533 | if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) | |
61b9b73c | 3534 | return (unsignedp |
3535 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
3536 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 3537 | |
0dfc45b5 | 3538 | if (COMPLEX_MODE_P (mode)) |
3539 | { | |
3540 | enum machine_mode inner_mode; | |
3541 | tree inner_type; | |
3542 | ||
3543 | if (mode == TYPE_MODE (complex_float_type_node)) | |
3544 | return complex_float_type_node; | |
3545 | if (mode == TYPE_MODE (complex_double_type_node)) | |
3546 | return complex_double_type_node; | |
3547 | if (mode == TYPE_MODE (complex_long_double_type_node)) | |
3548 | return complex_long_double_type_node; | |
3549 | ||
3550 | if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp) | |
3551 | return complex_integer_type_node; | |
3552 | ||
3553 | inner_mode = GET_MODE_INNER (mode); | |
3554 | inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
3555 | if (inner_type != NULL_TREE) | |
3556 | return build_complex_type (inner_type); | |
3557 | } | |
3558 | else if (VECTOR_MODE_P (mode)) | |
4917c376 | 3559 | { |
3560 | enum machine_mode inner_mode = GET_MODE_INNER (mode); | |
3561 | tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
3562 | if (inner_type != NULL_TREE) | |
3563 | return build_vector_type_for_mode (inner_type, mode); | |
88ae7f04 | 3564 | } |
e2ea7e3a | 3565 | |
c4503c0a | 3566 | if (mode == TYPE_MODE (dfloat32_type_node)) |
3567 | return dfloat32_type_node; | |
3568 | if (mode == TYPE_MODE (dfloat64_type_node)) | |
3569 | return dfloat64_type_node; | |
3570 | if (mode == TYPE_MODE (dfloat128_type_node)) | |
3571 | return dfloat128_type_node; | |
3572 | ||
9421ebb9 | 3573 | if (ALL_SCALAR_FIXED_POINT_MODE_P (mode)) |
3574 | { | |
3575 | if (mode == TYPE_MODE (short_fract_type_node)) | |
3576 | return unsignedp ? sat_short_fract_type_node : short_fract_type_node; | |
3577 | if (mode == TYPE_MODE (fract_type_node)) | |
3578 | return unsignedp ? sat_fract_type_node : fract_type_node; | |
3579 | if (mode == TYPE_MODE (long_fract_type_node)) | |
3580 | return unsignedp ? sat_long_fract_type_node : long_fract_type_node; | |
3581 | if (mode == TYPE_MODE (long_long_fract_type_node)) | |
3582 | return unsignedp ? sat_long_long_fract_type_node | |
3583 | : long_long_fract_type_node; | |
3584 | ||
3585 | if (mode == TYPE_MODE (unsigned_short_fract_type_node)) | |
3586 | return unsignedp ? sat_unsigned_short_fract_type_node | |
3587 | : unsigned_short_fract_type_node; | |
3588 | if (mode == TYPE_MODE (unsigned_fract_type_node)) | |
3589 | return unsignedp ? sat_unsigned_fract_type_node | |
3590 | : unsigned_fract_type_node; | |
3591 | if (mode == TYPE_MODE (unsigned_long_fract_type_node)) | |
3592 | return unsignedp ? sat_unsigned_long_fract_type_node | |
3593 | : unsigned_long_fract_type_node; | |
3594 | if (mode == TYPE_MODE (unsigned_long_long_fract_type_node)) | |
3595 | return unsignedp ? sat_unsigned_long_long_fract_type_node | |
3596 | : unsigned_long_long_fract_type_node; | |
3597 | ||
3598 | if (mode == TYPE_MODE (short_accum_type_node)) | |
3599 | return unsignedp ? sat_short_accum_type_node : short_accum_type_node; | |
3600 | if (mode == TYPE_MODE (accum_type_node)) | |
3601 | return unsignedp ? sat_accum_type_node : accum_type_node; | |
3602 | if (mode == TYPE_MODE (long_accum_type_node)) | |
3603 | return unsignedp ? sat_long_accum_type_node : long_accum_type_node; | |
3604 | if (mode == TYPE_MODE (long_long_accum_type_node)) | |
3605 | return unsignedp ? sat_long_long_accum_type_node | |
3606 | : long_long_accum_type_node; | |
3607 | ||
3608 | if (mode == TYPE_MODE (unsigned_short_accum_type_node)) | |
3609 | return unsignedp ? sat_unsigned_short_accum_type_node | |
3610 | : unsigned_short_accum_type_node; | |
3611 | if (mode == TYPE_MODE (unsigned_accum_type_node)) | |
3612 | return unsignedp ? sat_unsigned_accum_type_node | |
3613 | : unsigned_accum_type_node; | |
3614 | if (mode == TYPE_MODE (unsigned_long_accum_type_node)) | |
3615 | return unsignedp ? sat_unsigned_long_accum_type_node | |
3616 | : unsigned_long_accum_type_node; | |
3617 | if (mode == TYPE_MODE (unsigned_long_long_accum_type_node)) | |
3618 | return unsignedp ? sat_unsigned_long_long_accum_type_node | |
3619 | : unsigned_long_long_accum_type_node; | |
3620 | ||
3621 | if (mode == QQmode) | |
3622 | return unsignedp ? sat_qq_type_node : qq_type_node; | |
3623 | if (mode == HQmode) | |
3624 | return unsignedp ? sat_hq_type_node : hq_type_node; | |
3625 | if (mode == SQmode) | |
3626 | return unsignedp ? sat_sq_type_node : sq_type_node; | |
3627 | if (mode == DQmode) | |
3628 | return unsignedp ? sat_dq_type_node : dq_type_node; | |
3629 | if (mode == TQmode) | |
3630 | return unsignedp ? sat_tq_type_node : tq_type_node; | |
3631 | ||
3632 | if (mode == UQQmode) | |
3633 | return unsignedp ? sat_uqq_type_node : uqq_type_node; | |
3634 | if (mode == UHQmode) | |
3635 | return unsignedp ? sat_uhq_type_node : uhq_type_node; | |
3636 | if (mode == USQmode) | |
3637 | return unsignedp ? sat_usq_type_node : usq_type_node; | |
3638 | if (mode == UDQmode) | |
3639 | return unsignedp ? sat_udq_type_node : udq_type_node; | |
3640 | if (mode == UTQmode) | |
3641 | return unsignedp ? sat_utq_type_node : utq_type_node; | |
3642 | ||
3643 | if (mode == HAmode) | |
3644 | return unsignedp ? sat_ha_type_node : ha_type_node; | |
3645 | if (mode == SAmode) | |
3646 | return unsignedp ? sat_sa_type_node : sa_type_node; | |
3647 | if (mode == DAmode) | |
3648 | return unsignedp ? sat_da_type_node : da_type_node; | |
3649 | if (mode == TAmode) | |
3650 | return unsignedp ? sat_ta_type_node : ta_type_node; | |
3651 | ||
3652 | if (mode == UHAmode) | |
3653 | return unsignedp ? sat_uha_type_node : uha_type_node; | |
3654 | if (mode == USAmode) | |
3655 | return unsignedp ? sat_usa_type_node : usa_type_node; | |
3656 | if (mode == UDAmode) | |
3657 | return unsignedp ? sat_uda_type_node : uda_type_node; | |
3658 | if (mode == UTAmode) | |
3659 | return unsignedp ? sat_uta_type_node : uta_type_node; | |
3660 | } | |
3661 | ||
5b247e9f | 3662 | for (t = registered_builtin_types; t; t = TREE_CHAIN (t)) |
baec58e1 | 3663 | if (TYPE_MODE (TREE_VALUE (t)) == mode |
3664 | && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t))) | |
5b247e9f | 3665 | return TREE_VALUE (t); |
3666 | ||
b0fc3e72 | 3667 | return 0; |
3668 | } | |
20d39783 | 3669 | |
11773141 | 3670 | tree |
3671 | c_common_unsigned_type (tree type) | |
3672 | { | |
3673 | return c_common_signed_or_unsigned_type (1, type); | |
3674 | } | |
3675 | ||
20d39783 | 3676 | /* Return a signed type the same as TYPE in other respects. */ |
3677 | ||
3678 | tree | |
1cae46be | 3679 | c_common_signed_type (tree type) |
20d39783 | 3680 | { |
4070745f | 3681 | return c_common_signed_or_unsigned_type (0, type); |
20d39783 | 3682 | } |
3683 | ||
3684 | /* Return a type the same as TYPE except unsigned or | |
3685 | signed according to UNSIGNEDP. */ | |
3686 | ||
3687 | tree | |
1cae46be | 3688 | c_common_signed_or_unsigned_type (int unsignedp, tree type) |
20d39783 | 3689 | { |
7a91101f | 3690 | tree type1; |
20d39783 | 3691 | |
7a91101f | 3692 | /* This block of code emulates the behavior of the old |
3693 | c_common_unsigned_type. In particular, it returns | |
3694 | long_unsigned_type_node if passed a long, even when a int would | |
3695 | have the same size. This is necessary for warnings to work | |
3696 | correctly in archs where sizeof(int) == sizeof(long) */ | |
3697 | ||
3698 | type1 = TYPE_MAIN_VARIANT (type); | |
3699 | if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node) | |
3700 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3701 | if (type1 == integer_type_node || type1 == unsigned_type_node) | |
3702 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3703 | if (type1 == short_integer_type_node || type1 == short_unsigned_type_node) | |
3704 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3705 | if (type1 == long_integer_type_node || type1 == long_unsigned_type_node) | |
3706 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3707 | if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node) | |
3708 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
6388cfe2 | 3709 | if (int128_integer_type_node |
3710 | && (type1 == int128_integer_type_node | |
3711 | || type1 == int128_unsigned_type_node)) | |
3712 | return unsignedp ? int128_unsigned_type_node : int128_integer_type_node; | |
7a91101f | 3713 | if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node) |
3714 | return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node; | |
3715 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
3716 | if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node) | |
3717 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
3718 | #endif | |
3719 | if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node) | |
3720 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3721 | if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node) | |
3722 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3723 | if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node) | |
3724 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3725 | if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node) | |
3726 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3727 | ||
9f92e1a2 | 3728 | #define C_COMMON_FIXED_TYPES(NAME) \ |
3729 | if (type1 == short_ ## NAME ## _type_node \ | |
3730 | || type1 == unsigned_short_ ## NAME ## _type_node) \ | |
3731 | return unsignedp ? unsigned_short_ ## NAME ## _type_node \ | |
3732 | : short_ ## NAME ## _type_node; \ | |
3733 | if (type1 == NAME ## _type_node \ | |
3734 | || type1 == unsigned_ ## NAME ## _type_node) \ | |
3735 | return unsignedp ? unsigned_ ## NAME ## _type_node \ | |
3736 | : NAME ## _type_node; \ | |
3737 | if (type1 == long_ ## NAME ## _type_node \ | |
3738 | || type1 == unsigned_long_ ## NAME ## _type_node) \ | |
3739 | return unsignedp ? unsigned_long_ ## NAME ## _type_node \ | |
3740 | : long_ ## NAME ## _type_node; \ | |
3741 | if (type1 == long_long_ ## NAME ## _type_node \ | |
3742 | || type1 == unsigned_long_long_ ## NAME ## _type_node) \ | |
3743 | return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \ | |
3744 | : long_long_ ## NAME ## _type_node; | |
3745 | ||
3746 | #define C_COMMON_FIXED_MODE_TYPES(NAME) \ | |
3747 | if (type1 == NAME ## _type_node \ | |
3748 | || type1 == u ## NAME ## _type_node) \ | |
3749 | return unsignedp ? u ## NAME ## _type_node \ | |
3750 | : NAME ## _type_node; | |
3751 | ||
3752 | #define C_COMMON_FIXED_TYPES_SAT(NAME) \ | |
3753 | if (type1 == sat_ ## short_ ## NAME ## _type_node \ | |
3754 | || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \ | |
3755 | return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \ | |
3756 | : sat_ ## short_ ## NAME ## _type_node; \ | |
3757 | if (type1 == sat_ ## NAME ## _type_node \ | |
3758 | || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \ | |
3759 | return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \ | |
3760 | : sat_ ## NAME ## _type_node; \ | |
3761 | if (type1 == sat_ ## long_ ## NAME ## _type_node \ | |
3762 | || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \ | |
3763 | return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \ | |
3764 | : sat_ ## long_ ## NAME ## _type_node; \ | |
3765 | if (type1 == sat_ ## long_long_ ## NAME ## _type_node \ | |
3766 | || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \ | |
3767 | return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \ | |
3768 | : sat_ ## long_long_ ## NAME ## _type_node; | |
3769 | ||
3770 | #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \ | |
3771 | if (type1 == sat_ ## NAME ## _type_node \ | |
3772 | || type1 == sat_ ## u ## NAME ## _type_node) \ | |
3773 | return unsignedp ? sat_ ## u ## NAME ## _type_node \ | |
3774 | : sat_ ## NAME ## _type_node; | |
3775 | ||
3776 | C_COMMON_FIXED_TYPES (fract); | |
3777 | C_COMMON_FIXED_TYPES_SAT (fract); | |
3778 | C_COMMON_FIXED_TYPES (accum); | |
3779 | C_COMMON_FIXED_TYPES_SAT (accum); | |
3780 | ||
3781 | C_COMMON_FIXED_MODE_TYPES (qq); | |
3782 | C_COMMON_FIXED_MODE_TYPES (hq); | |
3783 | C_COMMON_FIXED_MODE_TYPES (sq); | |
3784 | C_COMMON_FIXED_MODE_TYPES (dq); | |
3785 | C_COMMON_FIXED_MODE_TYPES (tq); | |
3786 | C_COMMON_FIXED_MODE_TYPES_SAT (qq); | |
3787 | C_COMMON_FIXED_MODE_TYPES_SAT (hq); | |
3788 | C_COMMON_FIXED_MODE_TYPES_SAT (sq); | |
3789 | C_COMMON_FIXED_MODE_TYPES_SAT (dq); | |
3790 | C_COMMON_FIXED_MODE_TYPES_SAT (tq); | |
3791 | C_COMMON_FIXED_MODE_TYPES (ha); | |
3792 | C_COMMON_FIXED_MODE_TYPES (sa); | |
3793 | C_COMMON_FIXED_MODE_TYPES (da); | |
3794 | C_COMMON_FIXED_MODE_TYPES (ta); | |
3795 | C_COMMON_FIXED_MODE_TYPES_SAT (ha); | |
3796 | C_COMMON_FIXED_MODE_TYPES_SAT (sa); | |
3797 | C_COMMON_FIXED_MODE_TYPES_SAT (da); | |
3798 | C_COMMON_FIXED_MODE_TYPES_SAT (ta); | |
9421ebb9 | 3799 | |
4f7f7efd | 3800 | /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not |
3801 | the precision; they have precision set to match their range, but | |
3802 | may use a wider mode to match an ABI. If we change modes, we may | |
3803 | wind up with bad conversions. For INTEGER_TYPEs in C, must check | |
3804 | the precision as well, so as to yield correct results for | |
3805 | bit-field types. C++ does not have these separate bit-field | |
3806 | types, and producing a signed or unsigned variant of an | |
3807 | ENUMERAL_TYPE may cause other problems as well. */ | |
3808 | ||
ac265864 | 3809 | if (!INTEGRAL_TYPE_P (type) |
3810 | || TYPE_UNSIGNED (type) == unsignedp) | |
3811 | return type; | |
3812 | ||
4f7f7efd | 3813 | #define TYPE_OK(node) \ |
3814 | (TYPE_MODE (type) == TYPE_MODE (node) \ | |
0c4abe5b | 3815 | && TYPE_PRECISION (type) == TYPE_PRECISION (node)) |
4f7f7efd | 3816 | if (TYPE_OK (signed_char_type_node)) |
20d39783 | 3817 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
4f7f7efd | 3818 | if (TYPE_OK (integer_type_node)) |
20d39783 | 3819 | return unsignedp ? unsigned_type_node : integer_type_node; |
4f7f7efd | 3820 | if (TYPE_OK (short_integer_type_node)) |
20d39783 | 3821 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
4f7f7efd | 3822 | if (TYPE_OK (long_integer_type_node)) |
20d39783 | 3823 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
4f7f7efd | 3824 | if (TYPE_OK (long_long_integer_type_node)) |
20d39783 | 3825 | return (unsignedp ? long_long_unsigned_type_node |
3826 | : long_long_integer_type_node); | |
6388cfe2 | 3827 | if (int128_integer_type_node && TYPE_OK (int128_integer_type_node)) |
3828 | return (unsignedp ? int128_unsigned_type_node | |
3829 | : int128_integer_type_node); | |
4f7f7efd | 3830 | if (TYPE_OK (widest_integer_literal_type_node)) |
20d39783 | 3831 | return (unsignedp ? widest_unsigned_literal_type_node |
3832 | : widest_integer_literal_type_node); | |
ef11801e | 3833 | |
3834 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
4f7f7efd | 3835 | if (TYPE_OK (intTI_type_node)) |
ef11801e | 3836 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; |
3837 | #endif | |
4f7f7efd | 3838 | if (TYPE_OK (intDI_type_node)) |
ef11801e | 3839 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
4f7f7efd | 3840 | if (TYPE_OK (intSI_type_node)) |
ef11801e | 3841 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
4f7f7efd | 3842 | if (TYPE_OK (intHI_type_node)) |
ef11801e | 3843 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
4f7f7efd | 3844 | if (TYPE_OK (intQI_type_node)) |
ef11801e | 3845 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
4f7f7efd | 3846 | #undef TYPE_OK |
ef11801e | 3847 | |
0c4abe5b | 3848 | return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp); |
20d39783 | 3849 | } |
b268e47e | 3850 | |
c0e47fd4 | 3851 | /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */ |
3852 | ||
3853 | tree | |
3854 | c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp) | |
3855 | { | |
3856 | /* Extended integer types of the same width as a standard type have | |
3857 | lesser rank, so those of the same width as int promote to int or | |
3858 | unsigned int and are valid for printf formats expecting int or | |
3859 | unsigned int. To avoid such special cases, avoid creating | |
3860 | extended integer types for bit-fields if a standard integer type | |
3861 | is available. */ | |
3862 | if (width == TYPE_PRECISION (integer_type_node)) | |
3863 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3864 | if (width == TYPE_PRECISION (signed_char_type_node)) | |
3865 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3866 | if (width == TYPE_PRECISION (short_integer_type_node)) | |
3867 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3868 | if (width == TYPE_PRECISION (long_integer_type_node)) | |
3869 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3870 | if (width == TYPE_PRECISION (long_long_integer_type_node)) | |
3871 | return (unsignedp ? long_long_unsigned_type_node | |
3872 | : long_long_integer_type_node); | |
6388cfe2 | 3873 | if (int128_integer_type_node |
3874 | && width == TYPE_PRECISION (int128_integer_type_node)) | |
3875 | return (unsignedp ? int128_unsigned_type_node | |
3876 | : int128_integer_type_node); | |
c0e47fd4 | 3877 | return build_nonstandard_integer_type (width, unsignedp); |
3878 | } | |
3879 | ||
b268e47e | 3880 | /* The C version of the register_builtin_type langhook. */ |
3881 | ||
3882 | void | |
3883 | c_register_builtin_type (tree type, const char* name) | |
3884 | { | |
3885 | tree decl; | |
3886 | ||
e60a6f7b | 3887 | decl = build_decl (UNKNOWN_LOCATION, |
3888 | TYPE_DECL, get_identifier (name), type); | |
b268e47e | 3889 | DECL_ARTIFICIAL (decl) = 1; |
3890 | if (!TYPE_NAME (type)) | |
3891 | TYPE_NAME (type) = decl; | |
3892 | pushdecl (decl); | |
5b247e9f | 3893 | |
3894 | registered_builtin_types = tree_cons (0, type, registered_builtin_types); | |
b268e47e | 3895 | } |
a9b9d10c | 3896 | \f |
aff9e656 | 3897 | /* Print an error message for invalid operands to arith operation |
8e70fb09 | 3898 | CODE with TYPE0 for operand 0, and TYPE1 for operand 1. |
3899 | LOCATION is the location of the message. */ | |
b0fc3e72 | 3900 | |
3901 | void | |
8e70fb09 | 3902 | binary_op_error (location_t location, enum tree_code code, |
3903 | tree type0, tree type1) | |
b0fc3e72 | 3904 | { |
19cb6b50 | 3905 | const char *opname; |
f03946e4 | 3906 | |
b0fc3e72 | 3907 | switch (code) |
3908 | { | |
b0fc3e72 | 3909 | case PLUS_EXPR: |
3910 | opname = "+"; break; | |
3911 | case MINUS_EXPR: | |
3912 | opname = "-"; break; | |
3913 | case MULT_EXPR: | |
3914 | opname = "*"; break; | |
3915 | case MAX_EXPR: | |
3916 | opname = "max"; break; | |
3917 | case MIN_EXPR: | |
3918 | opname = "min"; break; | |
3919 | case EQ_EXPR: | |
3920 | opname = "=="; break; | |
3921 | case NE_EXPR: | |
3922 | opname = "!="; break; | |
3923 | case LE_EXPR: | |
3924 | opname = "<="; break; | |
3925 | case GE_EXPR: | |
3926 | opname = ">="; break; | |
3927 | case LT_EXPR: | |
3928 | opname = "<"; break; | |
3929 | case GT_EXPR: | |
3930 | opname = ">"; break; | |
3931 | case LSHIFT_EXPR: | |
3932 | opname = "<<"; break; | |
3933 | case RSHIFT_EXPR: | |
3934 | opname = ">>"; break; | |
3935 | case TRUNC_MOD_EXPR: | |
66618a1e | 3936 | case FLOOR_MOD_EXPR: |
b0fc3e72 | 3937 | opname = "%"; break; |
3938 | case TRUNC_DIV_EXPR: | |
66618a1e | 3939 | case FLOOR_DIV_EXPR: |
b0fc3e72 | 3940 | opname = "/"; break; |
3941 | case BIT_AND_EXPR: | |
3942 | opname = "&"; break; | |
3943 | case BIT_IOR_EXPR: | |
3944 | opname = "|"; break; | |
3945 | case TRUTH_ANDIF_EXPR: | |
3946 | opname = "&&"; break; | |
3947 | case TRUTH_ORIF_EXPR: | |
3948 | opname = "||"; break; | |
3949 | case BIT_XOR_EXPR: | |
3950 | opname = "^"; break; | |
31f820d2 | 3951 | default: |
315ba355 | 3952 | gcc_unreachable (); |
b0fc3e72 | 3953 | } |
8e70fb09 | 3954 | error_at (location, |
3955 | "invalid operands to binary %s (have %qT and %qT)", opname, | |
3956 | type0, type1); | |
b0fc3e72 | 3957 | } |
3958 | \f | |
03fe1dc2 | 3959 | /* Given an expression as a tree, return its original type. Do this |
3960 | by stripping any conversion that preserves the sign and precision. */ | |
3961 | static tree | |
3962 | expr_original_type (tree expr) | |
3963 | { | |
3964 | STRIP_SIGN_NOPS (expr); | |
3965 | return TREE_TYPE (expr); | |
3966 | } | |
3967 | ||
b0fc3e72 | 3968 | /* Subroutine of build_binary_op, used for comparison operations. |
3969 | See if the operands have both been converted from subword integer types | |
3970 | and, if so, perhaps change them both back to their original type. | |
5b511807 | 3971 | This function is also responsible for converting the two operands |
3972 | to the proper common type for comparison. | |
b0fc3e72 | 3973 | |
3974 | The arguments of this function are all pointers to local variables | |
3975 | of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, | |
3976 | RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. | |
3977 | ||
2623625f | 3978 | LOC is the location of the comparison. |
3979 | ||
b0fc3e72 | 3980 | If this function returns nonzero, it means that the comparison has |
3981 | a constant value. What this function returns is an expression for | |
3982 | that value. */ | |
3983 | ||
3984 | tree | |
2623625f | 3985 | shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr, |
3986 | tree *restype_ptr, enum tree_code *rescode_ptr) | |
b0fc3e72 | 3987 | { |
19cb6b50 | 3988 | tree type; |
b0fc3e72 | 3989 | tree op0 = *op0_ptr; |
3990 | tree op1 = *op1_ptr; | |
3991 | int unsignedp0, unsignedp1; | |
3992 | int real1, real2; | |
3993 | tree primop0, primop1; | |
3994 | enum tree_code code = *rescode_ptr; | |
3995 | ||
3996 | /* Throw away any conversions to wider types | |
3997 | already present in the operands. */ | |
3998 | ||
7f506bca | 3999 | primop0 = c_common_get_narrower (op0, &unsignedp0); |
4000 | primop1 = c_common_get_narrower (op1, &unsignedp1); | |
b0fc3e72 | 4001 | |
119d06b2 | 4002 | /* If primopN is first sign-extended from primopN's precision to opN's |
4003 | precision, then zero-extended from opN's precision to | |
4004 | *restype_ptr precision, shortenings might be invalid. */ | |
4005 | if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0)) | |
4006 | && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr) | |
4007 | && !unsignedp0 | |
4008 | && TYPE_UNSIGNED (TREE_TYPE (op0))) | |
4009 | primop0 = op0; | |
4010 | if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1)) | |
4011 | && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr) | |
4012 | && !unsignedp1 | |
4013 | && TYPE_UNSIGNED (TREE_TYPE (op1))) | |
4014 | primop1 = op1; | |
4015 | ||
b0fc3e72 | 4016 | /* Handle the case that OP0 does not *contain* a conversion |
4017 | but it *requires* conversion to FINAL_TYPE. */ | |
4018 | ||
4019 | if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) | |
78a8ed03 | 4020 | unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
b0fc3e72 | 4021 | if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) |
78a8ed03 | 4022 | unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1)); |
b0fc3e72 | 4023 | |
4024 | /* If one of the operands must be floated, we cannot optimize. */ | |
4025 | real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; | |
4026 | real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; | |
4027 | ||
4028 | /* If first arg is constant, swap the args (changing operation | |
2bd278cc | 4029 | so value is preserved), for canonicalization. Don't do this if |
4030 | the second arg is 0. */ | |
b0fc3e72 | 4031 | |
2bd278cc | 4032 | if (TREE_CONSTANT (primop0) |
9421ebb9 | 4033 | && !integer_zerop (primop1) && !real_zerop (primop1) |
4034 | && !fixed_zerop (primop1)) | |
b0fc3e72 | 4035 | { |
19cb6b50 | 4036 | tree tem = primop0; |
4037 | int temi = unsignedp0; | |
b0fc3e72 | 4038 | primop0 = primop1; |
4039 | primop1 = tem; | |
4040 | tem = op0; | |
4041 | op0 = op1; | |
4042 | op1 = tem; | |
4043 | *op0_ptr = op0; | |
4044 | *op1_ptr = op1; | |
4045 | unsignedp0 = unsignedp1; | |
4046 | unsignedp1 = temi; | |
4047 | temi = real1; | |
4048 | real1 = real2; | |
4049 | real2 = temi; | |
4050 | ||
4051 | switch (code) | |
4052 | { | |
4053 | case LT_EXPR: | |
4054 | code = GT_EXPR; | |
4055 | break; | |
4056 | case GT_EXPR: | |
4057 | code = LT_EXPR; | |
4058 | break; | |
4059 | case LE_EXPR: | |
4060 | code = GE_EXPR; | |
4061 | break; | |
4062 | case GE_EXPR: | |
4063 | code = LE_EXPR; | |
4064 | break; | |
31f820d2 | 4065 | default: |
4066 | break; | |
b0fc3e72 | 4067 | } |
4068 | *rescode_ptr = code; | |
4069 | } | |
4070 | ||
4071 | /* If comparing an integer against a constant more bits wide, | |
4072 | maybe we can deduce a value of 1 or 0 independent of the data. | |
4073 | Or else truncate the constant now | |
4074 | rather than extend the variable at run time. | |
4075 | ||
4076 | This is only interesting if the constant is the wider arg. | |
4077 | Also, it is not safe if the constant is unsigned and the | |
4078 | variable arg is signed, since in this case the variable | |
4079 | would be sign-extended and then regarded as unsigned. | |
4080 | Our technique fails in this case because the lowest/highest | |
4081 | possible unsigned results don't follow naturally from the | |
4082 | lowest/highest possible values of the variable operand. | |
4083 | For just EQ_EXPR and NE_EXPR there is another technique that | |
4084 | could be used: see if the constant can be faithfully represented | |
4085 | in the other operand's type, by truncating it and reextending it | |
4086 | and see if that preserves the constant's value. */ | |
4087 | ||
4088 | if (!real1 && !real2 | |
9421ebb9 | 4089 | && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE |
b0fc3e72 | 4090 | && TREE_CODE (primop1) == INTEGER_CST |
4091 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) | |
4092 | { | |
4093 | int min_gt, max_gt, min_lt, max_lt; | |
4094 | tree maxval, minval; | |
4095 | /* 1 if comparison is nominally unsigned. */ | |
78a8ed03 | 4096 | int unsignedp = TYPE_UNSIGNED (*restype_ptr); |
b0fc3e72 | 4097 | tree val; |
4098 | ||
4070745f | 4099 | type = c_common_signed_or_unsigned_type (unsignedp0, |
4100 | TREE_TYPE (primop0)); | |
cda09c61 | 4101 | |
b0fc3e72 | 4102 | maxval = TYPE_MAX_VALUE (type); |
4103 | minval = TYPE_MIN_VALUE (type); | |
4104 | ||
4105 | if (unsignedp && !unsignedp0) | |
4070745f | 4106 | *restype_ptr = c_common_signed_type (*restype_ptr); |
b0fc3e72 | 4107 | |
4108 | if (TREE_TYPE (primop1) != *restype_ptr) | |
18dbec6f | 4109 | { |
9a5e8086 | 4110 | /* Convert primop1 to target type, but do not introduce |
4111 | additional overflow. We know primop1 is an int_cst. */ | |
c8110c8f | 4112 | primop1 = force_fit_type_double (*restype_ptr, |
d3237426 | 4113 | tree_to_double_int (primop1), |
4114 | 0, TREE_OVERFLOW (primop1)); | |
18dbec6f | 4115 | } |
b0fc3e72 | 4116 | if (type != *restype_ptr) |
4117 | { | |
4118 | minval = convert (*restype_ptr, minval); | |
4119 | maxval = convert (*restype_ptr, maxval); | |
4120 | } | |
4121 | ||
4122 | if (unsignedp && unsignedp0) | |
4123 | { | |
4124 | min_gt = INT_CST_LT_UNSIGNED (primop1, minval); | |
4125 | max_gt = INT_CST_LT_UNSIGNED (primop1, maxval); | |
4126 | min_lt = INT_CST_LT_UNSIGNED (minval, primop1); | |
4127 | max_lt = INT_CST_LT_UNSIGNED (maxval, primop1); | |
4128 | } | |
4129 | else | |
4130 | { | |
4131 | min_gt = INT_CST_LT (primop1, minval); | |
4132 | max_gt = INT_CST_LT (primop1, maxval); | |
4133 | min_lt = INT_CST_LT (minval, primop1); | |
4134 | max_lt = INT_CST_LT (maxval, primop1); | |
4135 | } | |
4136 | ||
4137 | val = 0; | |
4138 | /* This used to be a switch, but Genix compiler can't handle that. */ | |
4139 | if (code == NE_EXPR) | |
4140 | { | |
4141 | if (max_lt || min_gt) | |
3c2239cf | 4142 | val = truthvalue_true_node; |
b0fc3e72 | 4143 | } |
4144 | else if (code == EQ_EXPR) | |
4145 | { | |
4146 | if (max_lt || min_gt) | |
3c2239cf | 4147 | val = truthvalue_false_node; |
b0fc3e72 | 4148 | } |
4149 | else if (code == LT_EXPR) | |
4150 | { | |
4151 | if (max_lt) | |
3c2239cf | 4152 | val = truthvalue_true_node; |
b0fc3e72 | 4153 | if (!min_lt) |
3c2239cf | 4154 | val = truthvalue_false_node; |
b0fc3e72 | 4155 | } |
4156 | else if (code == GT_EXPR) | |
4157 | { | |
4158 | if (min_gt) | |
3c2239cf | 4159 | val = truthvalue_true_node; |
b0fc3e72 | 4160 | if (!max_gt) |
3c2239cf | 4161 | val = truthvalue_false_node; |
b0fc3e72 | 4162 | } |
4163 | else if (code == LE_EXPR) | |
4164 | { | |
4165 | if (!max_gt) | |
3c2239cf | 4166 | val = truthvalue_true_node; |
b0fc3e72 | 4167 | if (min_gt) |
3c2239cf | 4168 | val = truthvalue_false_node; |
b0fc3e72 | 4169 | } |
4170 | else if (code == GE_EXPR) | |
4171 | { | |
4172 | if (!min_lt) | |
3c2239cf | 4173 | val = truthvalue_true_node; |
b0fc3e72 | 4174 | if (max_lt) |
3c2239cf | 4175 | val = truthvalue_false_node; |
b0fc3e72 | 4176 | } |
4177 | ||
4178 | /* If primop0 was sign-extended and unsigned comparison specd, | |
4179 | we did a signed comparison above using the signed type bounds. | |
4180 | But the comparison we output must be unsigned. | |
4181 | ||
4182 | Also, for inequalities, VAL is no good; but if the signed | |
4183 | comparison had *any* fixed result, it follows that the | |
4184 | unsigned comparison just tests the sign in reverse | |
4185 | (positive values are LE, negative ones GE). | |
4186 | So we can generate an unsigned comparison | |
4187 | against an extreme value of the signed type. */ | |
4188 | ||
4189 | if (unsignedp && !unsignedp0) | |
4190 | { | |
4191 | if (val != 0) | |
4192 | switch (code) | |
4193 | { | |
4194 | case LT_EXPR: | |
4195 | case GE_EXPR: | |
4196 | primop1 = TYPE_MIN_VALUE (type); | |
4197 | val = 0; | |
4198 | break; | |
4199 | ||
4200 | case LE_EXPR: | |
4201 | case GT_EXPR: | |
4202 | primop1 = TYPE_MAX_VALUE (type); | |
4203 | val = 0; | |
4204 | break; | |
31f820d2 | 4205 | |
4206 | default: | |
4207 | break; | |
b0fc3e72 | 4208 | } |
11773141 | 4209 | type = c_common_unsigned_type (type); |
b0fc3e72 | 4210 | } |
4211 | ||
82e6ef7c | 4212 | if (TREE_CODE (primop0) != INTEGER_CST |
4213 | && c_inhibit_evaluation_warnings == 0) | |
b0fc3e72 | 4214 | { |
3c2239cf | 4215 | if (val == truthvalue_false_node) |
03fe1dc2 | 4216 | warning_at (loc, OPT_Wtype_limits, |
4217 | "comparison is always false due to limited range of data type"); | |
3c2239cf | 4218 | if (val == truthvalue_true_node) |
03fe1dc2 | 4219 | warning_at (loc, OPT_Wtype_limits, |
4220 | "comparison is always true due to limited range of data type"); | |
b0fc3e72 | 4221 | } |
4222 | ||
4223 | if (val != 0) | |
4224 | { | |
4225 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4226 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 4227 | return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); |
b0fc3e72 | 4228 | return val; |
4229 | } | |
4230 | ||
4231 | /* Value is not predetermined, but do the comparison | |
4232 | in the type of the operand that is not constant. | |
4233 | TYPE is already properly set. */ | |
4234 | } | |
c4503c0a | 4235 | |
4236 | /* If either arg is decimal float and the other is float, find the | |
4237 | proper common type to use for comparison. */ | |
4238 | else if (real1 && real2 | |
4239 | && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0))) | |
4240 | || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))) | |
4241 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4242 | ||
b0fc3e72 | 4243 | else if (real1 && real2 |
2203bd5c | 4244 | && (TYPE_PRECISION (TREE_TYPE (primop0)) |
4245 | == TYPE_PRECISION (TREE_TYPE (primop1)))) | |
b0fc3e72 | 4246 | type = TREE_TYPE (primop0); |
4247 | ||
4248 | /* If args' natural types are both narrower than nominal type | |
4249 | and both extend in the same manner, compare them | |
4250 | in the type of the wider arg. | |
4251 | Otherwise must actually extend both to the nominal | |
4252 | common type lest different ways of extending | |
4253 | alter the result. | |
4254 | (eg, (short)-1 == (unsigned short)-1 should be 0.) */ | |
4255 | ||
4256 | else if (unsignedp0 == unsignedp1 && real1 == real2 | |
4257 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) | |
4258 | && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) | |
4259 | { | |
4260 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4070745f | 4261 | type = c_common_signed_or_unsigned_type (unsignedp0 |
78a8ed03 | 4262 | || TYPE_UNSIGNED (*restype_ptr), |
4070745f | 4263 | type); |
b0fc3e72 | 4264 | /* Make sure shorter operand is extended the right way |
4265 | to match the longer operand. */ | |
4070745f | 4266 | primop0 |
4267 | = convert (c_common_signed_or_unsigned_type (unsignedp0, | |
4268 | TREE_TYPE (primop0)), | |
4269 | primop0); | |
4270 | primop1 | |
4271 | = convert (c_common_signed_or_unsigned_type (unsignedp1, | |
4272 | TREE_TYPE (primop1)), | |
4273 | primop1); | |
b0fc3e72 | 4274 | } |
4275 | else | |
4276 | { | |
4277 | /* Here we must do the comparison on the nominal type | |
4278 | using the args exactly as we received them. */ | |
4279 | type = *restype_ptr; | |
4280 | primop0 = op0; | |
4281 | primop1 = op1; | |
4282 | ||
4283 | if (!real1 && !real2 && integer_zerop (primop1) | |
78a8ed03 | 4284 | && TYPE_UNSIGNED (*restype_ptr)) |
b0fc3e72 | 4285 | { |
4286 | tree value = 0; | |
03fe1dc2 | 4287 | /* All unsigned values are >= 0, so we warn. However, |
4288 | if OP0 is a constant that is >= 0, the signedness of | |
4289 | the comparison isn't an issue, so suppress the | |
4290 | warning. */ | |
4291 | bool warn = | |
3df42822 | 4292 | warn_type_limits && !in_system_header_at (loc) |
82e6ef7c | 4293 | && c_inhibit_evaluation_warnings == 0 |
03fe1dc2 | 4294 | && !(TREE_CODE (primop0) == INTEGER_CST |
4295 | && !TREE_OVERFLOW (convert (c_common_signed_type (type), | |
4296 | primop0))) | |
4297 | /* Do not warn for enumeration types. */ | |
4298 | && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE); | |
4299 | ||
b0fc3e72 | 4300 | switch (code) |
4301 | { | |
4302 | case GE_EXPR: | |
03fe1dc2 | 4303 | if (warn) |
4304 | warning_at (loc, OPT_Wtype_limits, | |
4305 | "comparison of unsigned expression >= 0 is always true"); | |
3c2239cf | 4306 | value = truthvalue_true_node; |
b0fc3e72 | 4307 | break; |
4308 | ||
4309 | case LT_EXPR: | |
03fe1dc2 | 4310 | if (warn) |
4311 | warning_at (loc, OPT_Wtype_limits, | |
4312 | "comparison of unsigned expression < 0 is always false"); | |
3c2239cf | 4313 | value = truthvalue_false_node; |
31f820d2 | 4314 | break; |
4315 | ||
4316 | default: | |
4317 | break; | |
b0fc3e72 | 4318 | } |
4319 | ||
4320 | if (value != 0) | |
4321 | { | |
4322 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4323 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 4324 | return build2 (COMPOUND_EXPR, TREE_TYPE (value), |
4325 | primop0, value); | |
b0fc3e72 | 4326 | return value; |
4327 | } | |
4328 | } | |
4329 | } | |
4330 | ||
4331 | *op0_ptr = convert (type, primop0); | |
4332 | *op1_ptr = convert (type, primop1); | |
4333 | ||
3c2239cf | 4334 | *restype_ptr = truthvalue_type_node; |
b0fc3e72 | 4335 | |
4336 | return 0; | |
4337 | } | |
4338 | \f | |
1c26100f | 4339 | /* Return a tree for the sum or difference (RESULTCODE says which) |
4340 | of pointer PTROP and integer INTOP. */ | |
4341 | ||
4342 | tree | |
389dd41b | 4343 | pointer_int_sum (location_t loc, enum tree_code resultcode, |
42f9a786 | 4344 | tree ptrop, tree intop, bool complain) |
1c26100f | 4345 | { |
add6ee5e | 4346 | tree size_exp, ret; |
1c26100f | 4347 | |
1c26100f | 4348 | /* The result is a pointer of the same type that is being added. */ |
1c26100f | 4349 | tree result_type = TREE_TYPE (ptrop); |
4350 | ||
4351 | if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) | |
4352 | { | |
42f9a786 | 4353 | if (complain && warn_pointer_arith) |
4354 | pedwarn (loc, OPT_Wpointer_arith, | |
4355 | "pointer of type %<void *%> used in arithmetic"); | |
4356 | else if (!complain) | |
4357 | return error_mark_node; | |
1c26100f | 4358 | size_exp = integer_one_node; |
4359 | } | |
4360 | else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) | |
4361 | { | |
42f9a786 | 4362 | if (complain && warn_pointer_arith) |
4363 | pedwarn (loc, OPT_Wpointer_arith, | |
4364 | "pointer to a function used in arithmetic"); | |
4365 | else if (!complain) | |
4366 | return error_mark_node; | |
1c26100f | 4367 | size_exp = integer_one_node; |
4368 | } | |
1c26100f | 4369 | else |
4370 | size_exp = size_in_bytes (TREE_TYPE (result_type)); | |
4371 | ||
add6ee5e | 4372 | /* We are manipulating pointer values, so we don't need to warn |
4373 | about relying on undefined signed overflow. We disable the | |
4374 | warning here because we use integer types so fold won't know that | |
4375 | they are really pointers. */ | |
4376 | fold_defer_overflow_warnings (); | |
4377 | ||
1c26100f | 4378 | /* If what we are about to multiply by the size of the elements |
4379 | contains a constant term, apply distributive law | |
4380 | and multiply that constant term separately. | |
4381 | This helps produce common subexpressions. */ | |
1c26100f | 4382 | if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) |
84166705 | 4383 | && !TREE_CONSTANT (intop) |
1c26100f | 4384 | && TREE_CONSTANT (TREE_OPERAND (intop, 1)) |
4385 | && TREE_CONSTANT (size_exp) | |
4386 | /* If the constant comes from pointer subtraction, | |
4387 | skip this optimization--it would cause an error. */ | |
4388 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE | |
4389 | /* If the constant is unsigned, and smaller than the pointer size, | |
4390 | then we must skip this optimization. This is because it could cause | |
4391 | an overflow error if the constant is negative but INTOP is not. */ | |
84166705 | 4392 | && (!TYPE_UNSIGNED (TREE_TYPE (intop)) |
1c26100f | 4393 | || (TYPE_PRECISION (TREE_TYPE (intop)) |
4394 | == TYPE_PRECISION (TREE_TYPE (ptrop))))) | |
4395 | { | |
4396 | enum tree_code subcode = resultcode; | |
4397 | tree int_type = TREE_TYPE (intop); | |
4398 | if (TREE_CODE (intop) == MINUS_EXPR) | |
4399 | subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); | |
4400 | /* Convert both subexpression types to the type of intop, | |
4401 | because weird cases involving pointer arithmetic | |
4402 | can result in a sum or difference with different type args. */ | |
8e70fb09 | 4403 | ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)), |
4404 | subcode, ptrop, | |
1c26100f | 4405 | convert (int_type, TREE_OPERAND (intop, 1)), 1); |
4406 | intop = convert (int_type, TREE_OPERAND (intop, 0)); | |
4407 | } | |
4408 | ||
4409 | /* Convert the integer argument to a type the same size as sizetype | |
4410 | so the multiply won't overflow spuriously. */ | |
1c26100f | 4411 | if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) |
78a8ed03 | 4412 | || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype)) |
1cae46be | 4413 | intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype), |
78a8ed03 | 4414 | TYPE_UNSIGNED (sizetype)), intop); |
1c26100f | 4415 | |
4416 | /* Replace the integer argument with a suitable product by the object size. | |
c6feb9f1 | 4417 | Do this multiplication as signed, then convert to the appropriate type |
8032877c | 4418 | for the pointer operation and disregard an overflow that occurred only |
c6feb9f1 | 4419 | because of the sign-extension change in the latter conversion. */ |
4420 | { | |
4421 | tree t = build_binary_op (loc, | |
4422 | MULT_EXPR, intop, | |
4423 | convert (TREE_TYPE (intop), size_exp), 1); | |
4424 | intop = convert (sizetype, t); | |
4425 | if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t)) | |
4426 | intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop), | |
4427 | TREE_INT_CST_HIGH (intop)); | |
4428 | } | |
0de36bdb | 4429 | |
499e523f | 4430 | /* Create the sum or difference. */ |
0de36bdb | 4431 | if (resultcode == MINUS_EXPR) |
389dd41b | 4432 | intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop); |
1c26100f | 4433 | |
2cc66f2a | 4434 | ret = fold_build_pointer_plus_loc (loc, ptrop, intop); |
add6ee5e | 4435 | |
4436 | fold_undefer_and_ignore_overflow_warnings (); | |
4437 | ||
4438 | return ret; | |
1c26100f | 4439 | } |
4440 | \f | |
f59e3889 | 4441 | /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded |
4442 | and if NON_CONST is known not to be permitted in an evaluated part | |
4443 | of a constant expression. */ | |
4444 | ||
4445 | tree | |
4446 | c_wrap_maybe_const (tree expr, bool non_const) | |
4447 | { | |
4448 | bool nowarning = TREE_NO_WARNING (expr); | |
4449 | location_t loc = EXPR_LOCATION (expr); | |
4450 | ||
4451 | /* This should never be called for C++. */ | |
4452 | if (c_dialect_cxx ()) | |
4453 | gcc_unreachable (); | |
4454 | ||
4455 | /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */ | |
4456 | STRIP_TYPE_NOPS (expr); | |
4457 | expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr); | |
4458 | C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const; | |
4459 | if (nowarning) | |
4460 | TREE_NO_WARNING (expr) = 1; | |
4461 | protected_set_expr_location (expr, loc); | |
4462 | ||
4463 | return expr; | |
4464 | } | |
4465 | ||
a75b1c71 | 4466 | /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but |
4467 | for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR | |
4468 | around the SAVE_EXPR if needed so that c_fully_fold does not need | |
4469 | to look inside SAVE_EXPRs. */ | |
4470 | ||
4471 | tree | |
4472 | c_save_expr (tree expr) | |
4473 | { | |
4474 | bool maybe_const = true; | |
4475 | if (c_dialect_cxx ()) | |
4476 | return save_expr (expr); | |
4477 | expr = c_fully_fold (expr, false, &maybe_const); | |
4478 | expr = save_expr (expr); | |
4479 | if (!maybe_const) | |
f59e3889 | 4480 | expr = c_wrap_maybe_const (expr, true); |
a75b1c71 | 4481 | return expr; |
4482 | } | |
4483 | ||
6b68e71a | 4484 | /* Return whether EXPR is a declaration whose address can never be |
4485 | NULL. */ | |
4486 | ||
4487 | bool | |
9f627b1a | 4488 | decl_with_nonnull_addr_p (const_tree expr) |
6b68e71a | 4489 | { |
4490 | return (DECL_P (expr) | |
4491 | && (TREE_CODE (expr) == PARM_DECL | |
4492 | || TREE_CODE (expr) == LABEL_DECL | |
4493 | || !DECL_WEAK (expr))); | |
4494 | } | |
4495 | ||
b0fc3e72 | 4496 | /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, |
45a78cc0 | 4497 | or for an `if' or `while' statement or ?..: exp. It should already |
4498 | have been validated to be of suitable type; otherwise, a bad | |
4499 | diagnostic may result. | |
b0fc3e72 | 4500 | |
8e70fb09 | 4501 | The EXPR is located at LOCATION. |
4502 | ||
b0fc3e72 | 4503 | This preparation consists of taking the ordinary |
4504 | representation of an expression expr and producing a valid tree | |
4505 | boolean expression describing whether expr is nonzero. We could | |
3c2239cf | 4506 | simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1), |
b0fc3e72 | 4507 | but we optimize comparisons, &&, ||, and !. |
4508 | ||
3c2239cf | 4509 | The resulting type should always be `truthvalue_type_node'. */ |
b0fc3e72 | 4510 | |
4511 | tree | |
8e70fb09 | 4512 | c_common_truthvalue_conversion (location_t location, tree expr) |
b0fc3e72 | 4513 | { |
b0fc3e72 | 4514 | switch (TREE_CODE (expr)) |
4515 | { | |
318a728f | 4516 | case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR: |
e1232ce2 | 4517 | case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: |
4518 | case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR: | |
4519 | case ORDERED_EXPR: case UNORDERED_EXPR: | |
ce04dcdc | 4520 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4521 | return expr; | |
e60a6f7b | 4522 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
ce04dcdc | 4523 | TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); |
e60a6f7b | 4524 | goto ret; |
ce04dcdc | 4525 | |
b0fc3e72 | 4526 | case TRUTH_ANDIF_EXPR: |
4527 | case TRUTH_ORIF_EXPR: | |
4528 | case TRUTH_AND_EXPR: | |
4529 | case TRUTH_OR_EXPR: | |
31f6e93c | 4530 | case TRUTH_XOR_EXPR: |
ce04dcdc | 4531 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4532 | return expr; | |
e60a6f7b | 4533 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
48e1416a | 4534 | c_common_truthvalue_conversion (location, |
e60a6f7b | 4535 | TREE_OPERAND (expr, 0)), |
4536 | c_common_truthvalue_conversion (location, | |
4537 | TREE_OPERAND (expr, 1))); | |
4538 | goto ret; | |
3e851b85 | 4539 | |
f6e28f72 | 4540 | case TRUTH_NOT_EXPR: |
ce04dcdc | 4541 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4542 | return expr; | |
e60a6f7b | 4543 | expr = build1 (TREE_CODE (expr), truthvalue_type_node, |
4544 | c_common_truthvalue_conversion (location, | |
4545 | TREE_OPERAND (expr, 0))); | |
4546 | goto ret; | |
f6e28f72 | 4547 | |
b0fc3e72 | 4548 | case ERROR_MARK: |
4549 | return expr; | |
4550 | ||
4551 | case INTEGER_CST: | |
eddad94a | 4552 | return integer_zerop (expr) ? truthvalue_false_node |
4553 | : truthvalue_true_node; | |
b0fc3e72 | 4554 | |
4555 | case REAL_CST: | |
5000e21c | 4556 | return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0) |
4557 | ? truthvalue_true_node | |
4558 | : truthvalue_false_node; | |
b0fc3e72 | 4559 | |
9421ebb9 | 4560 | case FIXED_CST: |
4561 | return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr), | |
4562 | &FCONST0 (TYPE_MODE (TREE_TYPE (expr)))) | |
4563 | ? truthvalue_true_node | |
4564 | : truthvalue_false_node; | |
4565 | ||
ce04dcdc | 4566 | case FUNCTION_DECL: |
b6889cb0 | 4567 | expr = build_unary_op (location, ADDR_EXPR, expr, 0); |
ce04dcdc | 4568 | /* Fall through. */ |
4569 | ||
b0fc3e72 | 4570 | case ADDR_EXPR: |
ce871053 | 4571 | { |
ee7d4d6a | 4572 | tree inner = TREE_OPERAND (expr, 0); |
6b68e71a | 4573 | if (decl_with_nonnull_addr_p (inner)) |
ce871053 | 4574 | { |
6b68e71a | 4575 | /* Common Ada/Pascal programmer's mistake. */ |
8e70fb09 | 4576 | warning_at (location, |
4577 | OPT_Waddress, | |
4578 | "the address of %qD will always evaluate as %<true%>", | |
4579 | inner); | |
ce871053 | 4580 | return truthvalue_true_node; |
4581 | } | |
d473d901 | 4582 | break; |
ce871053 | 4583 | } |
b0fc3e72 | 4584 | |
2203bd5c | 4585 | case COMPLEX_EXPR: |
e60a6f7b | 4586 | expr = build_binary_op (EXPR_LOCATION (expr), |
8e70fb09 | 4587 | (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) |
95809de4 | 4588 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
8e70fb09 | 4589 | c_common_truthvalue_conversion (location, |
4590 | TREE_OPERAND (expr, 0)), | |
4591 | c_common_truthvalue_conversion (location, | |
4592 | TREE_OPERAND (expr, 1)), | |
2203bd5c | 4593 | 0); |
e60a6f7b | 4594 | goto ret; |
2203bd5c | 4595 | |
b0fc3e72 | 4596 | case NEGATE_EXPR: |
4597 | case ABS_EXPR: | |
4598 | case FLOAT_EXPR: | |
c6418a4e | 4599 | case EXCESS_PRECISION_EXPR: |
d10cfa8d | 4600 | /* These don't change whether an object is nonzero or zero. */ |
8e70fb09 | 4601 | return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); |
b0fc3e72 | 4602 | |
4603 | case LROTATE_EXPR: | |
4604 | case RROTATE_EXPR: | |
d10cfa8d | 4605 | /* These don't change whether an object is zero or nonzero, but |
b0fc3e72 | 4606 | we can't ignore them if their second arg has side-effects. */ |
4607 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) | |
e60a6f7b | 4608 | { |
4609 | expr = build2 (COMPOUND_EXPR, truthvalue_type_node, | |
4610 | TREE_OPERAND (expr, 1), | |
48e1416a | 4611 | c_common_truthvalue_conversion |
e60a6f7b | 4612 | (location, TREE_OPERAND (expr, 0))); |
4613 | goto ret; | |
4614 | } | |
b0fc3e72 | 4615 | else |
8e70fb09 | 4616 | return c_common_truthvalue_conversion (location, |
4617 | TREE_OPERAND (expr, 0)); | |
73be5127 | 4618 | |
b0fc3e72 | 4619 | case COND_EXPR: |
4620 | /* Distribute the conversion into the arms of a COND_EXPR. */ | |
a75b1c71 | 4621 | if (c_dialect_cxx ()) |
e60a6f7b | 4622 | { |
d0389adc | 4623 | tree op1 = TREE_OPERAND (expr, 1); |
4624 | tree op2 = TREE_OPERAND (expr, 2); | |
4625 | /* In C++ one of the arms might have void type if it is throw. */ | |
4626 | if (!VOID_TYPE_P (TREE_TYPE (op1))) | |
4627 | op1 = c_common_truthvalue_conversion (location, op1); | |
4628 | if (!VOID_TYPE_P (TREE_TYPE (op2))) | |
4629 | op2 = c_common_truthvalue_conversion (location, op2); | |
389dd41b | 4630 | expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node, |
d0389adc | 4631 | TREE_OPERAND (expr, 0), op1, op2); |
e60a6f7b | 4632 | goto ret; |
4633 | } | |
a75b1c71 | 4634 | else |
e60a6f7b | 4635 | { |
4636 | /* Folding will happen later for C. */ | |
4637 | expr = build3 (COND_EXPR, truthvalue_type_node, | |
4638 | TREE_OPERAND (expr, 0), | |
4639 | c_common_truthvalue_conversion (location, | |
4640 | TREE_OPERAND (expr, 1)), | |
4641 | c_common_truthvalue_conversion (location, | |
4642 | TREE_OPERAND (expr, 2))); | |
4643 | goto ret; | |
4644 | } | |
b0fc3e72 | 4645 | |
72dd6141 | 4646 | CASE_CONVERT: |
f8913d47 | 4647 | { |
4648 | tree totype = TREE_TYPE (expr); | |
4649 | tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0)); | |
4650 | ||
4651 | /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, | |
4652 | since that affects how `default_conversion' will behave. */ | |
4653 | if (TREE_CODE (totype) == REFERENCE_TYPE | |
4654 | || TREE_CODE (fromtype) == REFERENCE_TYPE) | |
4655 | break; | |
4656 | /* Don't strip a conversion from C++0x scoped enum, since they | |
4657 | don't implicitly convert to other types. */ | |
4658 | if (TREE_CODE (fromtype) == ENUMERAL_TYPE | |
4659 | && ENUM_IS_SCOPED (fromtype)) | |
4660 | break; | |
4661 | /* If this isn't narrowing the argument, we can ignore it. */ | |
4662 | if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype)) | |
4663 | return c_common_truthvalue_conversion (location, | |
4664 | TREE_OPERAND (expr, 0)); | |
4665 | } | |
b0fc3e72 | 4666 | break; |
4667 | ||
16837b18 | 4668 | case MODIFY_EXPR: |
60a0513e | 4669 | if (!TREE_NO_WARNING (expr) |
4670 | && warn_parentheses) | |
4671 | { | |
4672 | warning (OPT_Wparentheses, | |
4673 | "suggest parentheses around assignment used as truth value"); | |
4674 | TREE_NO_WARNING (expr) = 1; | |
4675 | } | |
16837b18 | 4676 | break; |
73be5127 | 4677 | |
31f820d2 | 4678 | default: |
4679 | break; | |
b0fc3e72 | 4680 | } |
4681 | ||
2ba726d2 | 4682 | if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) |
a0748b7d | 4683 | { |
93be21c0 | 4684 | tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr)); |
e60a6f7b | 4685 | expr = (build_binary_op |
8e70fb09 | 4686 | (EXPR_LOCATION (expr), |
4687 | (TREE_SIDE_EFFECTS (expr) | |
a0748b7d | 4688 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
b6889cb0 | 4689 | c_common_truthvalue_conversion |
4690 | (location, | |
4691 | build_unary_op (location, REALPART_EXPR, t, 0)), | |
4692 | c_common_truthvalue_conversion | |
4693 | (location, | |
4694 | build_unary_op (location, IMAGPART_EXPR, t, 0)), | |
a0748b7d | 4695 | 0)); |
e60a6f7b | 4696 | goto ret; |
a0748b7d | 4697 | } |
2ba726d2 | 4698 | |
9421ebb9 | 4699 | if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE) |
4700 | { | |
4701 | tree fixed_zero_node = build_fixed (TREE_TYPE (expr), | |
4702 | FCONST0 (TYPE_MODE | |
4703 | (TREE_TYPE (expr)))); | |
43158006 | 4704 | return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1); |
9421ebb9 | 4705 | } |
e60a6f7b | 4706 | else |
4707 | return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1); | |
9421ebb9 | 4708 | |
e60a6f7b | 4709 | ret: |
4710 | protected_set_expr_location (expr, location); | |
4711 | return expr; | |
b0fc3e72 | 4712 | } |
4713 | \f | |
3237155d | 4714 | static void def_builtin_1 (enum built_in_function fncode, |
4715 | const char *name, | |
4716 | enum built_in_class fnclass, | |
4717 | tree fntype, tree libtype, | |
4718 | bool both_p, bool fallback_p, bool nonansi_p, | |
4719 | tree fnattrs, bool implicit_p); | |
0d4238dc | 4720 | |
a5b1863e | 4721 | |
4722 | /* Apply the TYPE_QUALS to the new DECL. */ | |
4723 | ||
4724 | void | |
1cae46be | 4725 | c_apply_type_quals_to_decl (int type_quals, tree decl) |
a5b1863e | 4726 | { |
adfb367f | 4727 | tree type = TREE_TYPE (decl); |
b27ac6b5 | 4728 | |
e4eabbe4 | 4729 | if (type == error_mark_node) |
4730 | return; | |
adfb367f | 4731 | |
98a33d9f | 4732 | if ((type_quals & TYPE_QUAL_CONST) |
4733 | || (type && TREE_CODE (type) == REFERENCE_TYPE)) | |
4734 | /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr | |
4735 | constructor can produce constant init, so rely on cp_finish_decl to | |
4736 | clear TREE_READONLY if the variable has non-constant init. */ | |
a5b1863e | 4737 | TREE_READONLY (decl) = 1; |
4738 | if (type_quals & TYPE_QUAL_VOLATILE) | |
4739 | { | |
4740 | TREE_SIDE_EFFECTS (decl) = 1; | |
4741 | TREE_THIS_VOLATILE (decl) = 1; | |
4742 | } | |
d91a20bc | 4743 | if (type_quals & TYPE_QUAL_RESTRICT) |
a5b1863e | 4744 | { |
adfb367f | 4745 | while (type && TREE_CODE (type) == ARRAY_TYPE) |
4746 | /* Allow 'restrict' on arrays of pointers. | |
4747 | FIXME currently we just ignore it. */ | |
4748 | type = TREE_TYPE (type); | |
4749 | if (!type | |
4750 | || !POINTER_TYPE_P (type) | |
4751 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))) | |
b0b1af64 | 4752 | error ("invalid use of %<restrict%>"); |
a5b1863e | 4753 | } |
4754 | } | |
4755 | ||
4ee9c684 | 4756 | /* Hash function for the problem of multiple type definitions in |
4757 | different files. This must hash all types that will compare | |
4758 | equal via comptypes to the same value. In practice it hashes | |
0bed3869 | 4759 | on some of the simple stuff and leaves the details to comptypes. */ |
4ee9c684 | 4760 | |
4761 | static hashval_t | |
4762 | c_type_hash (const void *p) | |
4763 | { | |
ecf2703d | 4764 | int n_elements; |
4ee9c684 | 4765 | int shift, size; |
aae87fc3 | 4766 | const_tree const t = (const_tree) p; |
4ee9c684 | 4767 | tree t2; |
4768 | switch (TREE_CODE (t)) | |
4769 | { | |
fbf0afd1 | 4770 | /* For pointers, hash on pointee type plus some swizzling. */ |
2363ef00 | 4771 | case POINTER_TYPE: |
4772 | return c_type_hash (TREE_TYPE (t)) ^ 0x3003003; | |
4773 | /* Hash on number of elements and total size. */ | |
4774 | case ENUMERAL_TYPE: | |
4775 | shift = 3; | |
4776 | t2 = TYPE_VALUES (t); | |
4777 | break; | |
4778 | case RECORD_TYPE: | |
4779 | shift = 0; | |
4780 | t2 = TYPE_FIELDS (t); | |
4781 | break; | |
4782 | case QUAL_UNION_TYPE: | |
4783 | shift = 1; | |
4784 | t2 = TYPE_FIELDS (t); | |
4785 | break; | |
4786 | case UNION_TYPE: | |
4787 | shift = 2; | |
4788 | t2 = TYPE_FIELDS (t); | |
4789 | break; | |
4790 | default: | |
231bd014 | 4791 | gcc_unreachable (); |
4ee9c684 | 4792 | } |
ecf2703d | 4793 | /* FIXME: We want to use a DECL_CHAIN iteration method here, but |
4794 | TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */ | |
4795 | n_elements = list_length (t2); | |
78c2e180 | 4796 | /* We might have a VLA here. */ |
4797 | if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST) | |
4798 | size = 0; | |
4799 | else | |
4800 | size = TREE_INT_CST_LOW (TYPE_SIZE (t)); | |
ecf2703d | 4801 | return ((size << 24) | (n_elements << shift)); |
4ee9c684 | 4802 | } |
4803 | ||
1ecd4018 | 4804 | static GTY((param_is (union tree_node))) htab_t type_hash_table; |
4805 | ||
b5ba9f3a | 4806 | /* Return the typed-based alias set for T, which may be an expression |
f7c44134 | 4807 | or a type. Return -1 if we don't do anything special. */ |
b5ba9f3a | 4808 | |
32c2fdea | 4809 | alias_set_type |
1cae46be | 4810 | c_common_get_alias_set (tree t) |
b5ba9f3a | 4811 | { |
be4f2de7 | 4812 | tree u; |
4ee9c684 | 4813 | PTR *slot; |
1cae46be | 4814 | |
e58c17e7 | 4815 | /* For VLAs, use the alias set of the element type rather than the |
4816 | default of alias set 0 for types compared structurally. */ | |
4817 | if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t)) | |
4818 | { | |
4819 | if (TREE_CODE (t) == ARRAY_TYPE) | |
4820 | return get_alias_set (TREE_TYPE (t)); | |
4821 | return -1; | |
4822 | } | |
4823 | ||
be4f2de7 | 4824 | /* Permit type-punning when accessing a union, provided the access |
4825 | is directly through the union. For example, this code does not | |
4826 | permit taking the address of a union member and then storing | |
4827 | through it. Even the type-punning allowed here is a GCC | |
4828 | extension, albeit a common and useful one; the C standard says | |
4829 | that such accesses have implementation-defined behavior. */ | |
4830 | for (u = t; | |
4831 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
4832 | u = TREE_OPERAND (u, 0)) | |
4833 | if (TREE_CODE (u) == COMPONENT_REF | |
4834 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
4835 | return 0; | |
1e2513d9 | 4836 | |
9fcc3e54 | 4837 | /* That's all the expressions we handle specially. */ |
84166705 | 4838 | if (!TYPE_P (t)) |
9fcc3e54 | 4839 | return -1; |
4840 | ||
d716ce75 | 4841 | /* The C standard guarantees that any object may be accessed via an |
9fcc3e54 | 4842 | lvalue that has character type. */ |
4843 | if (t == char_type_node | |
4844 | || t == signed_char_type_node | |
4845 | || t == unsigned_char_type_node) | |
f7c44134 | 4846 | return 0; |
a5b1863e | 4847 | |
1607663f | 4848 | /* The C standard specifically allows aliasing between signed and |
4849 | unsigned variants of the same type. We treat the signed | |
4850 | variant as canonical. */ | |
78a8ed03 | 4851 | if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t)) |
a8868e19 | 4852 | { |
4070745f | 4853 | tree t1 = c_common_signed_type (t); |
1607663f | 4854 | |
a8868e19 | 4855 | /* t1 == t can happen for boolean nodes which are always unsigned. */ |
4856 | if (t1 != t) | |
4857 | return get_alias_set (t1); | |
4858 | } | |
1e2513d9 | 4859 | |
4ee9c684 | 4860 | /* Handle the case of multiple type nodes referring to "the same" type, |
4861 | which occurs with IMA. These share an alias set. FIXME: Currently only | |
4862 | C90 is handled. (In C99 type compatibility is not transitive, which | |
4863 | complicates things mightily. The alias set splay trees can theoretically | |
4864 | represent this, but insertion is tricky when you consider all the | |
4865 | different orders things might arrive in.) */ | |
4866 | ||
4867 | if (c_language != clk_c || flag_isoc99) | |
4868 | return -1; | |
4869 | ||
0bed3869 | 4870 | /* Save time if there's only one input file. */ |
e08bd2f4 | 4871 | if (num_in_fnames == 1) |
4ee9c684 | 4872 | return -1; |
4873 | ||
4874 | /* Pointers need special handling if they point to any type that | |
4875 | needs special handling (below). */ | |
4876 | if (TREE_CODE (t) == POINTER_TYPE) | |
4877 | { | |
4878 | tree t2; | |
4879 | /* Find bottom type under any nested POINTERs. */ | |
b27ac6b5 | 4880 | for (t2 = TREE_TYPE (t); |
af592f67 | 4881 | TREE_CODE (t2) == POINTER_TYPE; |
4882 | t2 = TREE_TYPE (t2)) | |
4883 | ; | |
b27ac6b5 | 4884 | if (TREE_CODE (t2) != RECORD_TYPE |
af592f67 | 4885 | && TREE_CODE (t2) != ENUMERAL_TYPE |
4886 | && TREE_CODE (t2) != QUAL_UNION_TYPE | |
4887 | && TREE_CODE (t2) != UNION_TYPE) | |
4888 | return -1; | |
4ee9c684 | 4889 | if (TYPE_SIZE (t2) == 0) |
af592f67 | 4890 | return -1; |
4ee9c684 | 4891 | } |
4892 | /* These are the only cases that need special handling. */ | |
b27ac6b5 | 4893 | if (TREE_CODE (t) != RECORD_TYPE |
4ee9c684 | 4894 | && TREE_CODE (t) != ENUMERAL_TYPE |
4895 | && TREE_CODE (t) != QUAL_UNION_TYPE | |
4896 | && TREE_CODE (t) != UNION_TYPE | |
4897 | && TREE_CODE (t) != POINTER_TYPE) | |
4898 | return -1; | |
4899 | /* Undefined? */ | |
4900 | if (TYPE_SIZE (t) == 0) | |
4901 | return -1; | |
4902 | ||
b27ac6b5 | 4903 | /* Look up t in hash table. Only one of the compatible types within each |
4ee9c684 | 4904 | alias set is recorded in the table. */ |
4905 | if (!type_hash_table) | |
1ecd4018 | 4906 | type_hash_table = htab_create_ggc (1021, c_type_hash, |
4ee9c684 | 4907 | (htab_eq) lang_hooks.types_compatible_p, |
4908 | NULL); | |
4909 | slot = htab_find_slot (type_hash_table, t, INSERT); | |
4910 | if (*slot != NULL) | |
ad16cb2c | 4911 | { |
4912 | TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot); | |
4913 | return TYPE_ALIAS_SET ((tree)*slot); | |
4914 | } | |
4ee9c684 | 4915 | else |
4916 | /* Our caller will assign and record (in t) a new alias set; all we need | |
4917 | to do is remember t in the hash table. */ | |
4918 | *slot = t; | |
4919 | ||
f7c44134 | 4920 | return -1; |
b5ba9f3a | 4921 | } |
902b4e01 | 4922 | \f |
e60a6f7b | 4923 | /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where |
a179a7dc | 4924 | the IS_SIZEOF parameter indicates which operator is being applied. |
e60a6f7b | 4925 | The COMPLAIN flag controls whether we should diagnose possibly |
4926 | ill-formed constructs or not. LOC is the location of the SIZEOF or | |
a179a7dc | 4927 | TYPEOF operator. If MIN_ALIGNOF, the least alignment required for |
4928 | a type in any context should be returned, rather than the normal | |
4929 | alignment for that type. */ | |
908c697e | 4930 | |
902b4e01 | 4931 | tree |
e60a6f7b | 4932 | c_sizeof_or_alignof_type (location_t loc, |
a179a7dc | 4933 | tree type, bool is_sizeof, bool min_alignof, |
4934 | int complain) | |
902b4e01 | 4935 | { |
d4c4d95c | 4936 | const char *op_name; |
4937 | tree value = NULL; | |
4938 | enum tree_code type_code = TREE_CODE (type); | |
1cae46be | 4939 | |
908c697e | 4940 | op_name = is_sizeof ? "sizeof" : "__alignof__"; |
1cae46be | 4941 | |
d4c4d95c | 4942 | if (type_code == FUNCTION_TYPE) |
902b4e01 | 4943 | { |
908c697e | 4944 | if (is_sizeof) |
d4c4d95c | 4945 | { |
9205a6cc | 4946 | if (complain && warn_pointer_arith) |
4947 | pedwarn (loc, OPT_Wpointer_arith, | |
8864917d | 4948 | "invalid application of %<sizeof%> to a function type"); |
ebd21de4 | 4949 | else if (!complain) |
4950 | return error_mark_node; | |
d4c4d95c | 4951 | value = size_one_node; |
4952 | } | |
4953 | else | |
83e25171 | 4954 | { |
4955 | if (complain) | |
4956 | { | |
4957 | if (c_dialect_cxx ()) | |
29438999 | 4958 | pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit " |
83e25171 | 4959 | "%<alignof%> applied to a function type"); |
4960 | else | |
29438999 | 4961 | pedwarn (loc, OPT_Wpedantic, "ISO C does not permit " |
83e25171 | 4962 | "%<_Alignof%> applied to a function type"); |
4963 | } | |
4964 | value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); | |
4965 | } | |
d4c4d95c | 4966 | } |
4967 | else if (type_code == VOID_TYPE || type_code == ERROR_MARK) | |
4968 | { | |
1cae46be | 4969 | if (type_code == VOID_TYPE |
9205a6cc | 4970 | && complain && warn_pointer_arith) |
4971 | pedwarn (loc, OPT_Wpointer_arith, | |
8864917d | 4972 | "invalid application of %qs to a void type", op_name); |
ebd21de4 | 4973 | else if (!complain) |
4974 | return error_mark_node; | |
d4c4d95c | 4975 | value = size_one_node; |
902b4e01 | 4976 | } |
3df19e1b | 4977 | else if (!COMPLETE_TYPE_P (type) |
4978 | && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE)) | |
902b4e01 | 4979 | { |
d3a4d008 | 4980 | if (complain) |
3df19e1b | 4981 | error_at (loc, "invalid application of %qs to incomplete type %qT", |
e60a6f7b | 4982 | op_name, type); |
9c719c74 | 4983 | return error_mark_node; |
902b4e01 | 4984 | } |
3df19e1b | 4985 | else if (c_dialect_cxx () && type_code == ARRAY_TYPE |
4986 | && !COMPLETE_TYPE_P (TREE_TYPE (type))) | |
4987 | { | |
4988 | if (complain) | |
4989 | error_at (loc, "invalid application of %qs to array type %qT of " | |
4990 | "incomplete element type", op_name, type); | |
4991 | return error_mark_node; | |
4992 | } | |
902b4e01 | 4993 | else |
d4c4d95c | 4994 | { |
908c697e | 4995 | if (is_sizeof) |
d4c4d95c | 4996 | /* Convert in case a char is more than one unit. */ |
389dd41b | 4997 | value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
4998 | size_int (TYPE_PRECISION (char_type_node) | |
4999 | / BITS_PER_UNIT)); | |
a179a7dc | 5000 | else if (min_alignof) |
5001 | { | |
5002 | unsigned int align = TYPE_ALIGN (type); | |
5003 | align = MIN (align, BIGGEST_ALIGNMENT); | |
5004 | #ifdef BIGGEST_FIELD_ALIGNMENT | |
5005 | align = MIN (align, BIGGEST_FIELD_ALIGNMENT); | |
5006 | #endif | |
a179a7dc | 5007 | unsigned int field_align = align; |
5008 | #ifdef ADJUST_FIELD_ALIGN | |
0b26ec77 | 5009 | tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE, |
5010 | type); | |
a179a7dc | 5011 | field_align = ADJUST_FIELD_ALIGN (field, field_align); |
5012 | #endif | |
5013 | align = MIN (align, field_align); | |
5014 | value = size_int (align / BITS_PER_UNIT); | |
5015 | } | |
d4c4d95c | 5016 | else |
d37625c0 | 5017 | value = size_int (TYPE_ALIGN_UNIT (type)); |
d4c4d95c | 5018 | } |
902b4e01 | 5019 | |
5a1fe2db | 5020 | /* VALUE will have the middle-end integer type sizetype. |
5021 | However, we should really return a value of type `size_t', | |
5022 | which is just a typedef for an ordinary integer type. */ | |
389dd41b | 5023 | value = fold_convert_loc (loc, size_type_node, value); |
1cae46be | 5024 | |
d4c4d95c | 5025 | return value; |
902b4e01 | 5026 | } |
5027 | ||
5028 | /* Implement the __alignof keyword: Return the minimum required | |
097b5c8b | 5029 | alignment of EXPR, measured in bytes. For VAR_DECLs, |
5030 | FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set | |
e60a6f7b | 5031 | from an "aligned" __attribute__ specification). LOC is the |
5032 | location of the ALIGNOF operator. */ | |
72040e7e | 5033 | |
902b4e01 | 5034 | tree |
e60a6f7b | 5035 | c_alignof_expr (location_t loc, tree expr) |
902b4e01 | 5036 | { |
5037 | tree t; | |
5038 | ||
097b5c8b | 5039 | if (VAR_OR_FUNCTION_DECL_P (expr)) |
d37625c0 | 5040 | t = size_int (DECL_ALIGN_UNIT (expr)); |
1cae46be | 5041 | |
902b4e01 | 5042 | else if (TREE_CODE (expr) == COMPONENT_REF |
5043 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
5044 | { | |
e60a6f7b | 5045 | error_at (loc, "%<__alignof%> applied to a bit-field"); |
902b4e01 | 5046 | t = size_one_node; |
5047 | } | |
5048 | else if (TREE_CODE (expr) == COMPONENT_REF | |
7cc7e163 | 5049 | && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) |
d37625c0 | 5050 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1))); |
1cae46be | 5051 | |
902b4e01 | 5052 | else if (TREE_CODE (expr) == INDIRECT_REF) |
5053 | { | |
5054 | tree t = TREE_OPERAND (expr, 0); | |
5055 | tree best = t; | |
5056 | int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
1cae46be | 5057 | |
72dd6141 | 5058 | while (CONVERT_EXPR_P (t) |
7cc7e163 | 5059 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) |
902b4e01 | 5060 | { |
5061 | int thisalign; | |
5062 | ||
5063 | t = TREE_OPERAND (t, 0); | |
5064 | thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
5065 | if (thisalign > bestalign) | |
5066 | best = t, bestalign = thisalign; | |
5067 | } | |
e60a6f7b | 5068 | return c_alignof (loc, TREE_TYPE (TREE_TYPE (best))); |
902b4e01 | 5069 | } |
5070 | else | |
e60a6f7b | 5071 | return c_alignof (loc, TREE_TYPE (expr)); |
902b4e01 | 5072 | |
389dd41b | 5073 | return fold_convert_loc (loc, size_type_node, t); |
902b4e01 | 5074 | } |
5075 | \f | |
8fe4a266 | 5076 | /* Handle C and C++ default attributes. */ |
5077 | ||
5078 | enum built_in_attribute | |
5079 | { | |
5080 | #define DEF_ATTR_NULL_TREE(ENUM) ENUM, | |
5081 | #define DEF_ATTR_INT(ENUM, VALUE) ENUM, | |
c8010b80 | 5082 | #define DEF_ATTR_STRING(ENUM, VALUE) ENUM, |
8fe4a266 | 5083 | #define DEF_ATTR_IDENT(ENUM, STRING) ENUM, |
5084 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM, | |
8fe4a266 | 5085 | #include "builtin-attrs.def" |
5086 | #undef DEF_ATTR_NULL_TREE | |
5087 | #undef DEF_ATTR_INT | |
c8010b80 | 5088 | #undef DEF_ATTR_STRING |
8fe4a266 | 5089 | #undef DEF_ATTR_IDENT |
5090 | #undef DEF_ATTR_TREE_LIST | |
8fe4a266 | 5091 | ATTR_LAST |
5092 | }; | |
5093 | ||
5094 | static GTY(()) tree built_in_attributes[(int) ATTR_LAST]; | |
5095 | ||
1cae46be | 5096 | static void c_init_attributes (void); |
8fe4a266 | 5097 | |
27213ba3 | 5098 | enum c_builtin_type |
72040e7e | 5099 | { |
d2d4bdde | 5100 | #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME, |
5101 | #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME, | |
5102 | #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME, | |
5103 | #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME, | |
5104 | #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, | |
5105 | #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, | |
0a39fd54 | 5106 | #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME, |
27213ba3 | 5107 | #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME, |
5108 | #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME, | |
bc7bff74 | 5109 | #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME, |
d2d4bdde | 5110 | #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME, |
5111 | #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME, | |
5112 | #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME, | |
03901330 | 5113 | #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, |
0a39fd54 | 5114 | #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, |
5115 | #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \ | |
5116 | NAME, | |
d2d4bdde | 5117 | #define DEF_POINTER_TYPE(NAME, TYPE) NAME, |
5118 | #include "builtin-types.def" | |
5119 | #undef DEF_PRIMITIVE_TYPE | |
5120 | #undef DEF_FUNCTION_TYPE_0 | |
5121 | #undef DEF_FUNCTION_TYPE_1 | |
5122 | #undef DEF_FUNCTION_TYPE_2 | |
5123 | #undef DEF_FUNCTION_TYPE_3 | |
5124 | #undef DEF_FUNCTION_TYPE_4 | |
0a39fd54 | 5125 | #undef DEF_FUNCTION_TYPE_5 |
5126 | #undef DEF_FUNCTION_TYPE_6 | |
27213ba3 | 5127 | #undef DEF_FUNCTION_TYPE_7 |
bc7bff74 | 5128 | #undef DEF_FUNCTION_TYPE_8 |
d2d4bdde | 5129 | #undef DEF_FUNCTION_TYPE_VAR_0 |
5130 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
5131 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
03901330 | 5132 | #undef DEF_FUNCTION_TYPE_VAR_3 |
0a39fd54 | 5133 | #undef DEF_FUNCTION_TYPE_VAR_4 |
5134 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
d2d4bdde | 5135 | #undef DEF_POINTER_TYPE |
27213ba3 | 5136 | BT_LAST |
5137 | }; | |
5138 | ||
5139 | typedef enum c_builtin_type builtin_type; | |
d2d4bdde | 5140 | |
27213ba3 | 5141 | /* A temporary array for c_common_nodes_and_builtins. Used in |
5142 | communication with def_fn_type. */ | |
5143 | static tree builtin_types[(int) BT_LAST + 1]; | |
d2d4bdde | 5144 | |
27213ba3 | 5145 | /* A helper function for c_common_nodes_and_builtins. Build function type |
5146 | for DEF with return type RET and N arguments. If VAR is true, then the | |
5147 | function should be variadic after those N arguments. | |
5148 | ||
5149 | Takes special care not to ICE if any of the types involved are | |
5150 | error_mark_node, which indicates that said type is not in fact available | |
5151 | (see builtin_type_for_size). In which case the function type as a whole | |
5152 | should be error_mark_node. */ | |
5153 | ||
5154 | static void | |
5155 | def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...) | |
5156 | { | |
3a939d12 | 5157 | tree t; |
5158 | tree *args = XALLOCAVEC (tree, n); | |
27213ba3 | 5159 | va_list list; |
5160 | int i; | |
5161 | ||
5162 | va_start (list, n); | |
5163 | for (i = 0; i < n; ++i) | |
5164 | { | |
7d339f93 | 5165 | builtin_type a = (builtin_type) va_arg (list, int); |
27213ba3 | 5166 | t = builtin_types[a]; |
5167 | if (t == error_mark_node) | |
5168 | goto egress; | |
3a939d12 | 5169 | args[i] = t; |
27213ba3 | 5170 | } |
27213ba3 | 5171 | |
27213ba3 | 5172 | t = builtin_types[ret]; |
5173 | if (t == error_mark_node) | |
5174 | goto egress; | |
3a939d12 | 5175 | if (var) |
5176 | t = build_varargs_function_type_array (t, n, args); | |
5177 | else | |
5178 | t = build_function_type_array (t, n, args); | |
27213ba3 | 5179 | |
5180 | egress: | |
5181 | builtin_types[def] = t; | |
451c8e2f | 5182 | va_end (list); |
27213ba3 | 5183 | } |
5184 | ||
dce22712 | 5185 | /* Build builtin functions common to both C and C++ language |
5186 | frontends. */ | |
5187 | ||
5188 | static void | |
5189 | c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node) | |
5190 | { | |
5191 | #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ | |
5192 | builtin_types[ENUM] = VALUE; | |
5193 | #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ | |
5194 | def_fn_type (ENUM, RETURN, 0, 0); | |
5195 | #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ | |
5196 | def_fn_type (ENUM, RETURN, 0, 1, ARG1); | |
5197 | #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ | |
5198 | def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2); | |
5199 | #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
5200 | def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3); | |
5201 | #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
5202 | def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4); | |
5203 | #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
5204 | def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
5205 | #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5206 | ARG6) \ | |
5207 | def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); | |
5208 | #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5209 | ARG6, ARG7) \ | |
5210 | def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); | |
bc7bff74 | 5211 | #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5212 | ARG6, ARG7, ARG8) \ | |
5213 | def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \ | |
5214 | ARG7, ARG8); | |
dce22712 | 5215 | #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ |
5216 | def_fn_type (ENUM, RETURN, 1, 0); | |
5217 | #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ | |
5218 | def_fn_type (ENUM, RETURN, 1, 1, ARG1); | |
5219 | #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ | |
5220 | def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2); | |
5221 | #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
5222 | def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3); | |
5223 | #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
5224 | def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4); | |
5225 | #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
5226 | def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
5227 | #define DEF_POINTER_TYPE(ENUM, TYPE) \ | |
5228 | builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]); | |
5229 | ||
5230 | #include "builtin-types.def" | |
5231 | ||
5232 | #undef DEF_PRIMITIVE_TYPE | |
5233 | #undef DEF_FUNCTION_TYPE_1 | |
5234 | #undef DEF_FUNCTION_TYPE_2 | |
5235 | #undef DEF_FUNCTION_TYPE_3 | |
5236 | #undef DEF_FUNCTION_TYPE_4 | |
5237 | #undef DEF_FUNCTION_TYPE_5 | |
5238 | #undef DEF_FUNCTION_TYPE_6 | |
5239 | #undef DEF_FUNCTION_TYPE_VAR_0 | |
5240 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
5241 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
5242 | #undef DEF_FUNCTION_TYPE_VAR_3 | |
5243 | #undef DEF_FUNCTION_TYPE_VAR_4 | |
5244 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
5245 | #undef DEF_POINTER_TYPE | |
5246 | builtin_types[(int) BT_LAST] = NULL_TREE; | |
5247 | ||
5248 | c_init_attributes (); | |
5249 | ||
5250 | #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ | |
5251 | NONANSI_P, ATTRS, IMPLICIT, COND) \ | |
5252 | if (NAME && COND) \ | |
5253 | def_builtin_1 (ENUM, NAME, CLASS, \ | |
5254 | builtin_types[(int) TYPE], \ | |
5255 | builtin_types[(int) LIBTYPE], \ | |
5256 | BOTH_P, FALLBACK_P, NONANSI_P, \ | |
5257 | built_in_attributes[(int) ATTRS], IMPLICIT); | |
5258 | #include "builtins.def" | |
5259 | #undef DEF_BUILTIN | |
5260 | ||
87eb1c28 | 5261 | targetm.init_builtins (); |
5262 | ||
471eff36 | 5263 | build_common_builtin_nodes (); |
d037099f | 5264 | |
a89e6c15 | 5265 | if (flag_cilkplus) |
d037099f | 5266 | cilk_init_builtins (); |
dce22712 | 5267 | } |
5268 | ||
9e6687c8 | 5269 | /* Like get_identifier, but avoid warnings about null arguments when |
5270 | the argument may be NULL for targets where GCC lacks stdint.h type | |
5271 | information. */ | |
5272 | ||
5273 | static inline tree | |
5274 | c_get_ident (const char *id) | |
5275 | { | |
5276 | return get_identifier (id); | |
5277 | } | |
5278 | ||
27213ba3 | 5279 | /* Build tree nodes and builtin functions common to both C and C++ language |
5280 | frontends. */ | |
5281 | ||
5282 | void | |
5283 | c_common_nodes_and_builtins (void) | |
5284 | { | |
924bbf02 | 5285 | int char16_type_size; |
5286 | int char32_type_size; | |
174fcc61 | 5287 | int wchar_type_size; |
5288 | tree array_domain_type; | |
2d47cc32 | 5289 | tree va_list_ref_type_node; |
8a15c04a | 5290 | tree va_list_arg_type_node; |
a66c9326 | 5291 | |
c38a75b7 | 5292 | build_common_tree_nodes (flag_signed_char, flag_short_double); |
e593356b | 5293 | |
174fcc61 | 5294 | /* Define `int' and `char' first so that dbx will output them first. */ |
d946ea19 | 5295 | record_builtin_type (RID_INT, NULL, integer_type_node); |
174fcc61 | 5296 | record_builtin_type (RID_CHAR, "char", char_type_node); |
5297 | ||
5298 | /* `signed' is the same as `int'. FIXME: the declarations of "signed", | |
5299 | "unsigned long", "long long unsigned" and "unsigned short" were in C++ | |
5300 | but not C. Are the conditionals here needed? */ | |
c0f19401 | 5301 | if (c_dialect_cxx ()) |
d946ea19 | 5302 | record_builtin_type (RID_SIGNED, NULL, integer_type_node); |
174fcc61 | 5303 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); |
5304 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); | |
5305 | record_builtin_type (RID_MAX, "long unsigned int", | |
5306 | long_unsigned_type_node); | |
6388cfe2 | 5307 | if (int128_integer_type_node != NULL_TREE) |
5308 | { | |
5309 | record_builtin_type (RID_INT128, "__int128", | |
5310 | int128_integer_type_node); | |
5311 | record_builtin_type (RID_MAX, "__int128 unsigned", | |
5312 | int128_unsigned_type_node); | |
5313 | } | |
c0f19401 | 5314 | if (c_dialect_cxx ()) |
174fcc61 | 5315 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); |
5316 | record_builtin_type (RID_MAX, "long long int", | |
5317 | long_long_integer_type_node); | |
5318 | record_builtin_type (RID_MAX, "long long unsigned int", | |
5319 | long_long_unsigned_type_node); | |
c0f19401 | 5320 | if (c_dialect_cxx ()) |
174fcc61 | 5321 | record_builtin_type (RID_MAX, "long long unsigned", |
5322 | long_long_unsigned_type_node); | |
5323 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); | |
5324 | record_builtin_type (RID_MAX, "short unsigned int", | |
5325 | short_unsigned_type_node); | |
c0f19401 | 5326 | if (c_dialect_cxx ()) |
174fcc61 | 5327 | record_builtin_type (RID_MAX, "unsigned short", |
5328 | short_unsigned_type_node); | |
5329 | ||
5330 | /* Define both `signed char' and `unsigned char'. */ | |
5331 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); | |
5332 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); | |
5333 | ||
771d21fa | 5334 | /* These are types that c_common_type_for_size and |
5335 | c_common_type_for_mode use. */ | |
e60a6f7b | 5336 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5337 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5338 | intQI_type_node)); |
e60a6f7b | 5339 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5340 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5341 | intHI_type_node)); |
e60a6f7b | 5342 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5343 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5344 | intSI_type_node)); |
e60a6f7b | 5345 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5346 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5347 | intDI_type_node)); |
174fcc61 | 5348 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 5349 | if (targetm.scalar_mode_supported_p (TImode)) |
e60a6f7b | 5350 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5351 | TYPE_DECL, | |
f1515a39 | 5352 | get_identifier ("__int128_t"), |
5353 | intTI_type_node)); | |
174fcc61 | 5354 | #endif |
e60a6f7b | 5355 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5356 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5357 | unsigned_intQI_type_node)); |
e60a6f7b | 5358 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5359 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5360 | unsigned_intHI_type_node)); |
e60a6f7b | 5361 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5362 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5363 | unsigned_intSI_type_node)); |
e60a6f7b | 5364 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5365 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5366 | unsigned_intDI_type_node)); |
174fcc61 | 5367 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 5368 | if (targetm.scalar_mode_supported_p (TImode)) |
e60a6f7b | 5369 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5370 | TYPE_DECL, | |
f1515a39 | 5371 | get_identifier ("__uint128_t"), |
5372 | unsigned_intTI_type_node)); | |
174fcc61 | 5373 | #endif |
5374 | ||
5375 | /* Create the widest literal types. */ | |
5376 | widest_integer_literal_type_node | |
5377 | = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
e60a6f7b | 5378 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5379 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5380 | widest_integer_literal_type_node)); |
174fcc61 | 5381 | |
5382 | widest_unsigned_literal_type_node | |
5383 | = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
e60a6f7b | 5384 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5385 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5386 | widest_unsigned_literal_type_node)); |
174fcc61 | 5387 | |
654ef926 | 5388 | signed_size_type_node = c_common_signed_type (size_type_node); |
174fcc61 | 5389 | |
73673831 | 5390 | pid_type_node = |
5391 | TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE))); | |
5392 | ||
d946ea19 | 5393 | record_builtin_type (RID_FLOAT, NULL, float_type_node); |
5394 | record_builtin_type (RID_DOUBLE, NULL, double_type_node); | |
174fcc61 | 5395 | record_builtin_type (RID_MAX, "long double", long_double_type_node); |
5396 | ||
c4503c0a | 5397 | /* Only supported decimal floating point extension if the target |
5398 | actually supports underlying modes. */ | |
48e1416a | 5399 | if (targetm.scalar_mode_supported_p (SDmode) |
c4503c0a | 5400 | && targetm.scalar_mode_supported_p (DDmode) |
5401 | && targetm.scalar_mode_supported_p (TDmode)) | |
5402 | { | |
5403 | record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node); | |
5404 | record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node); | |
5405 | record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node); | |
5406 | } | |
5407 | ||
9421ebb9 | 5408 | if (targetm.fixed_point_supported_p ()) |
5409 | { | |
5410 | record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node); | |
5411 | record_builtin_type (RID_FRACT, NULL, fract_type_node); | |
5412 | record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node); | |
5413 | record_builtin_type (RID_MAX, "long long _Fract", | |
5414 | long_long_fract_type_node); | |
5415 | record_builtin_type (RID_MAX, "unsigned short _Fract", | |
5416 | unsigned_short_fract_type_node); | |
5417 | record_builtin_type (RID_MAX, "unsigned _Fract", | |
5418 | unsigned_fract_type_node); | |
5419 | record_builtin_type (RID_MAX, "unsigned long _Fract", | |
5420 | unsigned_long_fract_type_node); | |
5421 | record_builtin_type (RID_MAX, "unsigned long long _Fract", | |
5422 | unsigned_long_long_fract_type_node); | |
5423 | record_builtin_type (RID_MAX, "_Sat short _Fract", | |
5424 | sat_short_fract_type_node); | |
5425 | record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node); | |
5426 | record_builtin_type (RID_MAX, "_Sat long _Fract", | |
5427 | sat_long_fract_type_node); | |
5428 | record_builtin_type (RID_MAX, "_Sat long long _Fract", | |
5429 | sat_long_long_fract_type_node); | |
5430 | record_builtin_type (RID_MAX, "_Sat unsigned short _Fract", | |
5431 | sat_unsigned_short_fract_type_node); | |
5432 | record_builtin_type (RID_MAX, "_Sat unsigned _Fract", | |
5433 | sat_unsigned_fract_type_node); | |
5434 | record_builtin_type (RID_MAX, "_Sat unsigned long _Fract", | |
5435 | sat_unsigned_long_fract_type_node); | |
5436 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract", | |
5437 | sat_unsigned_long_long_fract_type_node); | |
5438 | record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node); | |
5439 | record_builtin_type (RID_ACCUM, NULL, accum_type_node); | |
5440 | record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node); | |
5441 | record_builtin_type (RID_MAX, "long long _Accum", | |
5442 | long_long_accum_type_node); | |
5443 | record_builtin_type (RID_MAX, "unsigned short _Accum", | |
5444 | unsigned_short_accum_type_node); | |
5445 | record_builtin_type (RID_MAX, "unsigned _Accum", | |
5446 | unsigned_accum_type_node); | |
5447 | record_builtin_type (RID_MAX, "unsigned long _Accum", | |
5448 | unsigned_long_accum_type_node); | |
5449 | record_builtin_type (RID_MAX, "unsigned long long _Accum", | |
5450 | unsigned_long_long_accum_type_node); | |
5451 | record_builtin_type (RID_MAX, "_Sat short _Accum", | |
5452 | sat_short_accum_type_node); | |
5453 | record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node); | |
5454 | record_builtin_type (RID_MAX, "_Sat long _Accum", | |
5455 | sat_long_accum_type_node); | |
5456 | record_builtin_type (RID_MAX, "_Sat long long _Accum", | |
5457 | sat_long_long_accum_type_node); | |
5458 | record_builtin_type (RID_MAX, "_Sat unsigned short _Accum", | |
5459 | sat_unsigned_short_accum_type_node); | |
5460 | record_builtin_type (RID_MAX, "_Sat unsigned _Accum", | |
5461 | sat_unsigned_accum_type_node); | |
5462 | record_builtin_type (RID_MAX, "_Sat unsigned long _Accum", | |
5463 | sat_unsigned_long_accum_type_node); | |
5464 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum", | |
5465 | sat_unsigned_long_long_accum_type_node); | |
5466 | ||
5467 | } | |
5468 | ||
e60a6f7b | 5469 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5470 | TYPE_DECL, | |
dc24ddbd | 5471 | get_identifier ("complex int"), |
5472 | complex_integer_type_node)); | |
e60a6f7b | 5473 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5474 | TYPE_DECL, | |
dc24ddbd | 5475 | get_identifier ("complex float"), |
5476 | complex_float_type_node)); | |
e60a6f7b | 5477 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5478 | TYPE_DECL, | |
dc24ddbd | 5479 | get_identifier ("complex double"), |
5480 | complex_double_type_node)); | |
5481 | lang_hooks.decls.pushdecl | |
e60a6f7b | 5482 | (build_decl (UNKNOWN_LOCATION, |
5483 | TYPE_DECL, get_identifier ("complex long double"), | |
20325f61 | 5484 | complex_long_double_type_node)); |
174fcc61 | 5485 | |
e256d445 | 5486 | if (c_dialect_cxx ()) |
5487 | /* For C++, make fileptr_type_node a distinct void * type until | |
5488 | FILE type is defined. */ | |
e086912e | 5489 | fileptr_type_node = build_variant_type_copy (ptr_type_node); |
e256d445 | 5490 | |
d946ea19 | 5491 | record_builtin_type (RID_VOID, NULL, void_type_node); |
174fcc61 | 5492 | |
6753bca0 | 5493 | /* Set the TYPE_NAME for any variants that were built before |
5494 | record_builtin_type gave names to the built-in types. */ | |
5495 | { | |
5496 | tree void_name = TYPE_NAME (void_type_node); | |
5497 | TYPE_NAME (void_type_node) = NULL_TREE; | |
5498 | TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST)) | |
5499 | = void_name; | |
5500 | TYPE_NAME (void_type_node) = void_name; | |
5501 | } | |
5502 | ||
fbf0afd1 | 5503 | /* This node must not be shared. */ |
7c446c95 | 5504 | void_zero_node = make_node (INTEGER_CST); |
d2d4bdde | 5505 | TREE_TYPE (void_zero_node) = void_type_node; |
5506 | ||
174fcc61 | 5507 | void_list_node = build_void_list_node (); |
5508 | ||
5509 | /* Make a type to be the domain of a few array types | |
5510 | whose domains don't really matter. | |
5511 | 200 is small enough that it always fits in size_t | |
5512 | and large enough that it can hold most function names for the | |
5513 | initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
5514 | array_domain_type = build_index_type (size_int (200)); | |
5515 | ||
5516 | /* Make a type for arrays of characters. | |
5517 | With luck nothing will ever really depend on the length of this | |
5518 | array type. */ | |
5519 | char_array_type_node | |
5520 | = build_array_type (char_type_node, array_domain_type); | |
5521 | ||
d2d4bdde | 5522 | string_type_node = build_pointer_type (char_type_node); |
5523 | const_string_type_node | |
5524 | = build_pointer_type (build_qualified_type | |
5525 | (char_type_node, TYPE_QUAL_CONST)); | |
5526 | ||
174fcc61 | 5527 | /* This is special for C++ so functions can be overloaded. */ |
18ef7ac2 | 5528 | wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE); |
174fcc61 | 5529 | wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); |
5530 | wchar_type_size = TYPE_PRECISION (wchar_type_node); | |
f3449a3c | 5531 | underlying_wchar_type_node = wchar_type_node; |
c0f19401 | 5532 | if (c_dialect_cxx ()) |
174fcc61 | 5533 | { |
78a8ed03 | 5534 | if (TYPE_UNSIGNED (wchar_type_node)) |
174fcc61 | 5535 | wchar_type_node = make_unsigned_type (wchar_type_size); |
5536 | else | |
5537 | wchar_type_node = make_signed_type (wchar_type_size); | |
5538 | record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); | |
5539 | } | |
174fcc61 | 5540 | |
5541 | /* This is for wide string constants. */ | |
5542 | wchar_array_type_node | |
5543 | = build_array_type (wchar_type_node, array_domain_type); | |
5544 | ||
924bbf02 | 5545 | /* Define 'char16_t'. */ |
5546 | char16_type_node = get_identifier (CHAR16_TYPE); | |
5547 | char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node)); | |
5548 | char16_type_size = TYPE_PRECISION (char16_type_node); | |
5549 | if (c_dialect_cxx ()) | |
5550 | { | |
5551 | char16_type_node = make_unsigned_type (char16_type_size); | |
5552 | ||
60777f69 | 5553 | if (cxx_dialect >= cxx11) |
924bbf02 | 5554 | record_builtin_type (RID_CHAR16, "char16_t", char16_type_node); |
5555 | } | |
5556 | ||
5557 | /* This is for UTF-16 string constants. */ | |
5558 | char16_array_type_node | |
5559 | = build_array_type (char16_type_node, array_domain_type); | |
5560 | ||
5561 | /* Define 'char32_t'. */ | |
5562 | char32_type_node = get_identifier (CHAR32_TYPE); | |
5563 | char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node)); | |
5564 | char32_type_size = TYPE_PRECISION (char32_type_node); | |
5565 | if (c_dialect_cxx ()) | |
5566 | { | |
5567 | char32_type_node = make_unsigned_type (char32_type_size); | |
5568 | ||
60777f69 | 5569 | if (cxx_dialect >= cxx11) |
924bbf02 | 5570 | record_builtin_type (RID_CHAR32, "char32_t", char32_type_node); |
5571 | } | |
5572 | ||
5573 | /* This is for UTF-32 string constants. */ | |
5574 | char32_array_type_node | |
5575 | = build_array_type (char32_type_node, array_domain_type); | |
5576 | ||
6bf5ed8d | 5577 | wint_type_node = |
5578 | TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); | |
5579 | ||
5580 | intmax_type_node = | |
5581 | TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); | |
5582 | uintmax_type_node = | |
5583 | TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); | |
5584 | ||
f3449a3c | 5585 | if (SIG_ATOMIC_TYPE) |
5586 | sig_atomic_type_node = | |
9e6687c8 | 5587 | TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE))); |
f3449a3c | 5588 | if (INT8_TYPE) |
5589 | int8_type_node = | |
9e6687c8 | 5590 | TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE))); |
f3449a3c | 5591 | if (INT16_TYPE) |
5592 | int16_type_node = | |
9e6687c8 | 5593 | TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE))); |
f3449a3c | 5594 | if (INT32_TYPE) |
5595 | int32_type_node = | |
9e6687c8 | 5596 | TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE))); |
f3449a3c | 5597 | if (INT64_TYPE) |
5598 | int64_type_node = | |
9e6687c8 | 5599 | TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE))); |
f3449a3c | 5600 | if (UINT8_TYPE) |
5601 | uint8_type_node = | |
9e6687c8 | 5602 | TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE))); |
f3449a3c | 5603 | if (UINT16_TYPE) |
74bdbe96 | 5604 | c_uint16_type_node = |
9e6687c8 | 5605 | TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE))); |
f3449a3c | 5606 | if (UINT32_TYPE) |
5607 | c_uint32_type_node = | |
9e6687c8 | 5608 | TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE))); |
f3449a3c | 5609 | if (UINT64_TYPE) |
5610 | c_uint64_type_node = | |
9e6687c8 | 5611 | TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE))); |
f3449a3c | 5612 | if (INT_LEAST8_TYPE) |
5613 | int_least8_type_node = | |
9e6687c8 | 5614 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE))); |
f3449a3c | 5615 | if (INT_LEAST16_TYPE) |
5616 | int_least16_type_node = | |
9e6687c8 | 5617 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE))); |
f3449a3c | 5618 | if (INT_LEAST32_TYPE) |
5619 | int_least32_type_node = | |
9e6687c8 | 5620 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE))); |
f3449a3c | 5621 | if (INT_LEAST64_TYPE) |
5622 | int_least64_type_node = | |
9e6687c8 | 5623 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE))); |
f3449a3c | 5624 | if (UINT_LEAST8_TYPE) |
5625 | uint_least8_type_node = | |
9e6687c8 | 5626 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE))); |
f3449a3c | 5627 | if (UINT_LEAST16_TYPE) |
5628 | uint_least16_type_node = | |
9e6687c8 | 5629 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE))); |
f3449a3c | 5630 | if (UINT_LEAST32_TYPE) |
5631 | uint_least32_type_node = | |
9e6687c8 | 5632 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE))); |
f3449a3c | 5633 | if (UINT_LEAST64_TYPE) |
5634 | uint_least64_type_node = | |
9e6687c8 | 5635 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE))); |
f3449a3c | 5636 | if (INT_FAST8_TYPE) |
5637 | int_fast8_type_node = | |
9e6687c8 | 5638 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE))); |
f3449a3c | 5639 | if (INT_FAST16_TYPE) |
5640 | int_fast16_type_node = | |
9e6687c8 | 5641 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE))); |
f3449a3c | 5642 | if (INT_FAST32_TYPE) |
5643 | int_fast32_type_node = | |
9e6687c8 | 5644 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE))); |
f3449a3c | 5645 | if (INT_FAST64_TYPE) |
5646 | int_fast64_type_node = | |
9e6687c8 | 5647 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE))); |
f3449a3c | 5648 | if (UINT_FAST8_TYPE) |
5649 | uint_fast8_type_node = | |
9e6687c8 | 5650 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE))); |
f3449a3c | 5651 | if (UINT_FAST16_TYPE) |
5652 | uint_fast16_type_node = | |
9e6687c8 | 5653 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE))); |
f3449a3c | 5654 | if (UINT_FAST32_TYPE) |
5655 | uint_fast32_type_node = | |
9e6687c8 | 5656 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE))); |
f3449a3c | 5657 | if (UINT_FAST64_TYPE) |
5658 | uint_fast64_type_node = | |
9e6687c8 | 5659 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE))); |
f3449a3c | 5660 | if (INTPTR_TYPE) |
5661 | intptr_type_node = | |
9e6687c8 | 5662 | TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE))); |
f3449a3c | 5663 | if (UINTPTR_TYPE) |
5664 | uintptr_type_node = | |
9e6687c8 | 5665 | TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE))); |
f3449a3c | 5666 | |
3a939d12 | 5667 | default_function_type |
5668 | = build_varargs_function_type_list (integer_type_node, NULL_TREE); | |
6bf5ed8d | 5669 | ptrdiff_type_node |
5670 | = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); | |
11773141 | 5671 | unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node); |
6bf5ed8d | 5672 | |
dc24ddbd | 5673 | lang_hooks.decls.pushdecl |
e60a6f7b | 5674 | (build_decl (UNKNOWN_LOCATION, |
5675 | TYPE_DECL, get_identifier ("__builtin_va_list"), | |
20325f61 | 5676 | va_list_type_node)); |
202d6e5f | 5677 | if (targetm.enum_va_list_p) |
acd6f472 | 5678 | { |
5679 | int l; | |
5680 | const char *pname; | |
5681 | tree ptype; | |
5f57a8b1 | 5682 | |
202d6e5f | 5683 | for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l) |
acd6f472 | 5684 | { |
5685 | lang_hooks.decls.pushdecl | |
5686 | (build_decl (UNKNOWN_LOCATION, | |
5687 | TYPE_DECL, get_identifier (pname), | |
5688 | ptype)); | |
5689 | ||
5690 | } | |
5691 | } | |
8a15c04a | 5692 | |
8a15c04a | 5693 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
2d47cc32 | 5694 | { |
5695 | va_list_arg_type_node = va_list_ref_type_node = | |
5696 | build_pointer_type (TREE_TYPE (va_list_type_node)); | |
5697 | } | |
8a15c04a | 5698 | else |
2d47cc32 | 5699 | { |
5700 | va_list_arg_type_node = va_list_type_node; | |
5701 | va_list_ref_type_node = build_reference_type (va_list_type_node); | |
5702 | } | |
1cae46be | 5703 | |
dce22712 | 5704 | if (!flag_preprocess_only) |
5705 | c_define_builtins (va_list_ref_type_node, va_list_arg_type_node); | |
ffa8918b | 5706 | |
5c62f199 | 5707 | main_identifier_node = get_identifier ("main"); |
ae84079f | 5708 | |
5709 | /* Create the built-in __null node. It is important that this is | |
5710 | not shared. */ | |
5711 | null_node = make_node (INTEGER_CST); | |
5712 | TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0); | |
27213ba3 | 5713 | |
5714 | /* Since builtin_types isn't gc'ed, don't export these nodes. */ | |
5715 | memset (builtin_types, 0, sizeof (builtin_types)); | |
72040e7e | 5716 | } |
a66c9326 | 5717 | |
79b01846 | 5718 | /* The number of named compound-literals generated thus far. */ |
5719 | static GTY(()) int compound_literal_number; | |
5720 | ||
5721 | /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */ | |
5722 | ||
5723 | void | |
5724 | set_compound_literal_name (tree decl) | |
5725 | { | |
5726 | char *name; | |
5727 | ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", | |
5728 | compound_literal_number); | |
5729 | compound_literal_number++; | |
5730 | DECL_NAME (decl) = get_identifier (name); | |
5731 | } | |
5732 | ||
a66c9326 | 5733 | tree |
e60a6f7b | 5734 | build_va_arg (location_t loc, tree expr, tree type) |
a66c9326 | 5735 | { |
e60a6f7b | 5736 | expr = build1 (VA_ARG_EXPR, type, expr); |
5737 | SET_EXPR_LOCATION (expr, loc); | |
5738 | return expr; | |
a66c9326 | 5739 | } |
0d4238dc | 5740 | |
5741 | ||
dd878098 | 5742 | /* Linked list of disabled built-in functions. */ |
5743 | ||
5744 | typedef struct disabled_builtin | |
5745 | { | |
5746 | const char *name; | |
5747 | struct disabled_builtin *next; | |
5748 | } disabled_builtin; | |
5749 | static disabled_builtin *disabled_builtins = NULL; | |
5750 | ||
1cae46be | 5751 | static bool builtin_function_disabled_p (const char *); |
dd878098 | 5752 | |
5753 | /* Disable a built-in function specified by -fno-builtin-NAME. If NAME | |
5754 | begins with "__builtin_", give an error. */ | |
5755 | ||
5756 | void | |
1cae46be | 5757 | disable_builtin_function (const char *name) |
dd878098 | 5758 | { |
5759 | if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0) | |
b0b1af64 | 5760 | error ("cannot disable built-in function %qs", name); |
dd878098 | 5761 | else |
5762 | { | |
e85905e5 | 5763 | disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin); |
5764 | new_disabled_builtin->name = name; | |
5765 | new_disabled_builtin->next = disabled_builtins; | |
5766 | disabled_builtins = new_disabled_builtin; | |
dd878098 | 5767 | } |
5768 | } | |
5769 | ||
5770 | ||
5771 | /* Return true if the built-in function NAME has been disabled, false | |
5772 | otherwise. */ | |
5773 | ||
5774 | static bool | |
1cae46be | 5775 | builtin_function_disabled_p (const char *name) |
dd878098 | 5776 | { |
5777 | disabled_builtin *p; | |
5778 | for (p = disabled_builtins; p != NULL; p = p->next) | |
5779 | { | |
5780 | if (strcmp (name, p->name) == 0) | |
5781 | return true; | |
5782 | } | |
5783 | return false; | |
5784 | } | |
5785 | ||
5786 | ||
3237155d | 5787 | /* Worker for DEF_BUILTIN. |
5788 | Possibly define a builtin function with one or two names. | |
5789 | Does not declare a non-__builtin_ function if flag_no_builtin, or if | |
5790 | nonansi_p and flag_no_nonansi_builtin. */ | |
0d4238dc | 5791 | |
3237155d | 5792 | static void |
5793 | def_builtin_1 (enum built_in_function fncode, | |
5794 | const char *name, | |
5795 | enum built_in_class fnclass, | |
5796 | tree fntype, tree libtype, | |
5797 | bool both_p, bool fallback_p, bool nonansi_p, | |
5798 | tree fnattrs, bool implicit_p) | |
0d4238dc | 5799 | { |
3237155d | 5800 | tree decl; |
5801 | const char *libname; | |
5802 | ||
27213ba3 | 5803 | if (fntype == error_mark_node) |
5804 | return; | |
5805 | ||
3237155d | 5806 | gcc_assert ((!both_p && !fallback_p) |
5807 | || !strncmp (name, "__builtin_", | |
5808 | strlen ("__builtin_"))); | |
5809 | ||
5810 | libname = name + strlen ("__builtin_"); | |
54be5d7e | 5811 | decl = add_builtin_function (name, fntype, fncode, fnclass, |
5812 | (fallback_p ? libname : NULL), | |
5813 | fnattrs); | |
b9a16870 | 5814 | |
5815 | set_builtin_decl (fncode, decl, implicit_p); | |
5816 | ||
3237155d | 5817 | if (both_p |
5818 | && !flag_no_builtin && !builtin_function_disabled_p (libname) | |
dd878098 | 5819 | && !(nonansi_p && flag_no_nonansi_builtin)) |
54be5d7e | 5820 | add_builtin_function (libname, libtype, fncode, fnclass, |
5821 | NULL, fnattrs); | |
0d4238dc | 5822 | } |
e94026da | 5823 | \f |
d7aeef06 | 5824 | /* Nonzero if the type T promotes to int. This is (nearly) the |
5825 | integral promotions defined in ISO C99 6.3.1.1/2. */ | |
5826 | ||
5827 | bool | |
9f627b1a | 5828 | c_promoting_integer_type_p (const_tree t) |
d7aeef06 | 5829 | { |
5830 | switch (TREE_CODE (t)) | |
5831 | { | |
5832 | case INTEGER_TYPE: | |
5833 | return (TYPE_MAIN_VARIANT (t) == char_type_node | |
5834 | || TYPE_MAIN_VARIANT (t) == signed_char_type_node | |
5835 | || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node | |
5836 | || TYPE_MAIN_VARIANT (t) == short_integer_type_node | |
7aa1e6eb | 5837 | || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node |
5838 | || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node)); | |
d7aeef06 | 5839 | |
5840 | case ENUMERAL_TYPE: | |
5841 | /* ??? Technically all enumerations not larger than an int | |
5842 | promote to an int. But this is used along code paths | |
5843 | that only want to notice a size change. */ | |
5844 | return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node); | |
5845 | ||
5846 | case BOOLEAN_TYPE: | |
5847 | return 1; | |
5848 | ||
5849 | default: | |
5850 | return 0; | |
5851 | } | |
5852 | } | |
5853 | ||
e94026da | 5854 | /* Return 1 if PARMS specifies a fixed number of parameters |
5855 | and none of their types is affected by default promotions. */ | |
5856 | ||
5857 | int | |
9f627b1a | 5858 | self_promoting_args_p (const_tree parms) |
e94026da | 5859 | { |
9f627b1a | 5860 | const_tree t; |
e94026da | 5861 | for (t = parms; t; t = TREE_CHAIN (t)) |
5862 | { | |
19cb6b50 | 5863 | tree type = TREE_VALUE (t); |
43f74bc4 | 5864 | |
e1d8e198 | 5865 | if (type == error_mark_node) |
5866 | continue; | |
5867 | ||
e94026da | 5868 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
5869 | return 0; | |
5870 | ||
5871 | if (type == 0) | |
5872 | return 0; | |
5873 | ||
5874 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
5875 | return 0; | |
5876 | ||
d7aeef06 | 5877 | if (c_promoting_integer_type_p (type)) |
e94026da | 5878 | return 0; |
5879 | } | |
5880 | return 1; | |
5881 | } | |
605fb01e | 5882 | |
c10de5e7 | 5883 | /* Recursively remove any '*' or '&' operator from TYPE. */ |
5884 | tree | |
5885 | strip_pointer_operator (tree t) | |
5886 | { | |
5887 | while (POINTER_TYPE_P (t)) | |
5888 | t = TREE_TYPE (t); | |
5889 | return t; | |
5890 | } | |
5891 | ||
57a0ed23 | 5892 | /* Recursively remove pointer or array type from TYPE. */ |
5893 | tree | |
5894 | strip_pointer_or_array_types (tree t) | |
5895 | { | |
5896 | while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t)) | |
5897 | t = TREE_TYPE (t); | |
5898 | return t; | |
5899 | } | |
5900 | ||
e41f0d80 | 5901 | /* Used to compare case labels. K1 and K2 are actually tree nodes |
5902 | representing case labels, or NULL_TREE for a `default' label. | |
5903 | Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after | |
5904 | K2, and 0 if K1 and K2 are equal. */ | |
5905 | ||
5906 | int | |
1cae46be | 5907 | case_compare (splay_tree_key k1, splay_tree_key k2) |
e41f0d80 | 5908 | { |
5909 | /* Consider a NULL key (such as arises with a `default' label) to be | |
5910 | smaller than anything else. */ | |
5911 | if (!k1) | |
5912 | return k2 ? -1 : 0; | |
5913 | else if (!k2) | |
5914 | return k1 ? 1 : 0; | |
5915 | ||
5916 | return tree_int_cst_compare ((tree) k1, (tree) k2); | |
5917 | } | |
5918 | ||
e60a6f7b | 5919 | /* Process a case label, located at LOC, for the range LOW_VALUE |
5920 | ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE | |
5921 | then this case label is actually a `default' label. If only | |
5922 | HIGH_VALUE is NULL_TREE, then case label was declared using the | |
5923 | usual C/C++ syntax, rather than the GNU case range extension. | |
5924 | CASES is a tree containing all the case ranges processed so far; | |
5925 | COND is the condition for the switch-statement itself. Returns the | |
5926 | CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR | |
5927 | is created. */ | |
e41f0d80 | 5928 | |
5929 | tree | |
e60a6f7b | 5930 | c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type, |
2ca392fd | 5931 | tree low_value, tree high_value) |
e41f0d80 | 5932 | { |
5933 | tree type; | |
5934 | tree label; | |
5935 | tree case_label; | |
5936 | splay_tree_node node; | |
5937 | ||
5938 | /* Create the LABEL_DECL itself. */ | |
e60a6f7b | 5939 | label = create_artificial_label (loc); |
e41f0d80 | 5940 | |
5941 | /* If there was an error processing the switch condition, bail now | |
5942 | before we get more confused. */ | |
5943 | if (!cond || cond == error_mark_node) | |
4ee9c684 | 5944 | goto error_out; |
e41f0d80 | 5945 | |
1cae46be | 5946 | if ((low_value && TREE_TYPE (low_value) |
5947 | && POINTER_TYPE_P (TREE_TYPE (low_value))) | |
e41f0d80 | 5948 | || (high_value && TREE_TYPE (high_value) |
5949 | && POINTER_TYPE_P (TREE_TYPE (high_value)))) | |
b96dc121 | 5950 | { |
e60a6f7b | 5951 | error_at (loc, "pointers are not permitted as case values"); |
b96dc121 | 5952 | goto error_out; |
5953 | } | |
e41f0d80 | 5954 | |
5955 | /* Case ranges are a GNU extension. */ | |
8864917d | 5956 | if (high_value) |
29438999 | 5957 | pedwarn (loc, OPT_Wpedantic, |
8864917d | 5958 | "range expressions in switch statements are non-standard"); |
e41f0d80 | 5959 | |
5960 | type = TREE_TYPE (cond); | |
5961 | if (low_value) | |
5962 | { | |
5963 | low_value = check_case_value (low_value); | |
22a75734 | 5964 | low_value = convert_and_check (loc, type, low_value); |
96722196 | 5965 | if (low_value == error_mark_node) |
5966 | goto error_out; | |
e41f0d80 | 5967 | } |
5968 | if (high_value) | |
5969 | { | |
5970 | high_value = check_case_value (high_value); | |
22a75734 | 5971 | high_value = convert_and_check (loc, type, high_value); |
96722196 | 5972 | if (high_value == error_mark_node) |
5973 | goto error_out; | |
e41f0d80 | 5974 | } |
5975 | ||
96722196 | 5976 | if (low_value && high_value) |
5977 | { | |
5978 | /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't | |
a0c938f0 | 5979 | really a case range, even though it was written that way. |
5980 | Remove the HIGH_VALUE to simplify later processing. */ | |
96722196 | 5981 | if (tree_int_cst_equal (low_value, high_value)) |
5982 | high_value = NULL_TREE; | |
5983 | else if (!tree_int_cst_lt (low_value, high_value)) | |
e60a6f7b | 5984 | warning_at (loc, 0, "empty range specified"); |
96722196 | 5985 | } |
e41f0d80 | 5986 | |
2ca392fd | 5987 | /* See if the case is in range of the type of the original testing |
5988 | expression. If both low_value and high_value are out of range, | |
5989 | don't insert the case label and return NULL_TREE. */ | |
5990 | if (low_value | |
84166705 | 5991 | && !check_case_bounds (type, orig_type, |
5992 | &low_value, high_value ? &high_value : NULL)) | |
2ca392fd | 5993 | return NULL_TREE; |
5994 | ||
e41f0d80 | 5995 | /* Look up the LOW_VALUE in the table of case labels we already |
5996 | have. */ | |
5997 | node = splay_tree_lookup (cases, (splay_tree_key) low_value); | |
5998 | /* If there was not an exact match, check for overlapping ranges. | |
5999 | There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; | |
6000 | that's a `default' label and the only overlap is an exact match. */ | |
6001 | if (!node && (low_value || high_value)) | |
6002 | { | |
6003 | splay_tree_node low_bound; | |
6004 | splay_tree_node high_bound; | |
6005 | ||
6006 | /* Even though there wasn't an exact match, there might be an | |
6007 | overlap between this case range and another case range. | |
6008 | Since we've (inductively) not allowed any overlapping case | |
6009 | ranges, we simply need to find the greatest low case label | |
6010 | that is smaller that LOW_VALUE, and the smallest low case | |
6011 | label that is greater than LOW_VALUE. If there is an overlap | |
6012 | it will occur in one of these two ranges. */ | |
6013 | low_bound = splay_tree_predecessor (cases, | |
6014 | (splay_tree_key) low_value); | |
6015 | high_bound = splay_tree_successor (cases, | |
6016 | (splay_tree_key) low_value); | |
6017 | ||
6018 | /* Check to see if the LOW_BOUND overlaps. It is smaller than | |
6019 | the LOW_VALUE, so there is no need to check unless the | |
6020 | LOW_BOUND is in fact itself a case range. */ | |
6021 | if (low_bound | |
6022 | && CASE_HIGH ((tree) low_bound->value) | |
6023 | && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), | |
6024 | low_value) >= 0) | |
6025 | node = low_bound; | |
6026 | /* Check to see if the HIGH_BOUND overlaps. The low end of that | |
6027 | range is bigger than the low end of the current range, so we | |
6028 | are only interested if the current range is a real range, and | |
6029 | not an ordinary case label. */ | |
1cae46be | 6030 | else if (high_bound |
e41f0d80 | 6031 | && high_value |
6032 | && (tree_int_cst_compare ((tree) high_bound->key, | |
6033 | high_value) | |
6034 | <= 0)) | |
6035 | node = high_bound; | |
6036 | } | |
6037 | /* If there was an overlap, issue an error. */ | |
6038 | if (node) | |
6039 | { | |
eaae3b75 | 6040 | tree duplicate = CASE_LABEL ((tree) node->value); |
e41f0d80 | 6041 | |
6042 | if (high_value) | |
6043 | { | |
e60a6f7b | 6044 | error_at (loc, "duplicate (or overlapping) case value"); |
6045 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
6046 | "this is the first entry overlapping that value"); | |
e41f0d80 | 6047 | } |
6048 | else if (low_value) | |
6049 | { | |
e60a6f7b | 6050 | error_at (loc, "duplicate case value") ; |
6051 | error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here"); | |
e41f0d80 | 6052 | } |
6053 | else | |
6054 | { | |
e60a6f7b | 6055 | error_at (loc, "multiple default labels in one switch"); |
6056 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
6057 | "this is the first default label"); | |
e41f0d80 | 6058 | } |
4ee9c684 | 6059 | goto error_out; |
e41f0d80 | 6060 | } |
6061 | ||
6062 | /* Add a CASE_LABEL to the statement-tree. */ | |
b6e3dd65 | 6063 | case_label = add_stmt (build_case_label (low_value, high_value, label)); |
e41f0d80 | 6064 | /* Register this case label in the splay tree. */ |
1cae46be | 6065 | splay_tree_insert (cases, |
e41f0d80 | 6066 | (splay_tree_key) low_value, |
6067 | (splay_tree_value) case_label); | |
6068 | ||
6069 | return case_label; | |
4ee9c684 | 6070 | |
6071 | error_out: | |
daf6dff5 | 6072 | /* Add a label so that the back-end doesn't think that the beginning of |
4ee9c684 | 6073 | the switch is unreachable. Note that we do not add a case label, as |
a53ff4c1 | 6074 | that just leads to duplicates and thence to failure later on. */ |
4ee9c684 | 6075 | if (!cases->root) |
6076 | { | |
e60a6f7b | 6077 | tree t = create_artificial_label (loc); |
6078 | add_stmt (build_stmt (loc, LABEL_EXPR, t)); | |
4ee9c684 | 6079 | } |
6080 | return error_mark_node; | |
6081 | } | |
6082 | ||
6083 | /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. | |
6084 | Used to verify that case values match up with enumerator values. */ | |
6085 | ||
6086 | static void | |
6087 | match_case_to_enum_1 (tree key, tree type, tree label) | |
6088 | { | |
6089 | char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1]; | |
6090 | ||
6091 | /* ??? Not working too hard to print the double-word value. | |
6092 | Should perhaps be done with %lwd in the diagnostic routines? */ | |
6093 | if (TREE_INT_CST_HIGH (key) == 0) | |
6094 | snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED, | |
6095 | TREE_INT_CST_LOW (key)); | |
6096 | else if (!TYPE_UNSIGNED (type) | |
6097 | && TREE_INT_CST_HIGH (key) == -1 | |
6098 | && TREE_INT_CST_LOW (key) != 0) | |
6099 | snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED, | |
6100 | -TREE_INT_CST_LOW (key)); | |
6101 | else | |
6102 | snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX, | |
7df7561b | 6103 | (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key), |
6104 | (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key)); | |
4ee9c684 | 6105 | |
6106 | if (TYPE_NAME (type) == 0) | |
712d2297 | 6107 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
6108 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
6109 | "case value %qs not in enumerated type", | |
6110 | buf); | |
4ee9c684 | 6111 | else |
712d2297 | 6112 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
6113 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
6114 | "case value %qs not in enumerated type %qT", | |
6115 | buf, type); | |
4ee9c684 | 6116 | } |
6117 | ||
359d87c6 | 6118 | /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. |
6119 | Used to verify that case values match up with enumerator values. */ | |
6120 | ||
4ee9c684 | 6121 | static int |
6122 | match_case_to_enum (splay_tree_node node, void *data) | |
6123 | { | |
6124 | tree label = (tree) node->value; | |
4fd61bc6 | 6125 | tree type = (tree) data; |
4ee9c684 | 6126 | |
6127 | /* Skip default case. */ | |
6128 | if (!CASE_LOW (label)) | |
6129 | return 0; | |
6130 | ||
359d87c6 | 6131 | /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear |
4ee9c684 | 6132 | when we did our enum->case scan. Reset our scratch bit after. */ |
359d87c6 | 6133 | if (!CASE_LOW_SEEN (label)) |
4ee9c684 | 6134 | match_case_to_enum_1 (CASE_LOW (label), type, label); |
6135 | else | |
359d87c6 | 6136 | CASE_LOW_SEEN (label) = 0; |
4ee9c684 | 6137 | |
359d87c6 | 6138 | /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is |
6139 | not set, that means that CASE_HIGH did not appear when we did our | |
6140 | enum->case scan. Reset our scratch bit after. */ | |
4ee9c684 | 6141 | if (CASE_HIGH (label)) |
6142 | { | |
359d87c6 | 6143 | if (!CASE_HIGH_SEEN (label)) |
6144 | match_case_to_enum_1 (CASE_HIGH (label), type, label); | |
6145 | else | |
6146 | CASE_HIGH_SEEN (label) = 0; | |
4ee9c684 | 6147 | } |
6148 | ||
6149 | return 0; | |
6150 | } | |
6151 | ||
e7911019 | 6152 | /* Handle -Wswitch*. Called from the front end after parsing the |
6153 | switch construct. */ | |
6154 | /* ??? Should probably be somewhere generic, since other languages | |
6155 | besides C and C++ would want this. At the moment, however, C/C++ | |
6156 | are the only tree-ssa languages that support enumerations at all, | |
6157 | so the point is moot. */ | |
4ee9c684 | 6158 | |
e7911019 | 6159 | void |
6160 | c_do_switch_warnings (splay_tree cases, location_t switch_location, | |
6161 | tree type, tree cond) | |
4ee9c684 | 6162 | { |
b27ac6b5 | 6163 | splay_tree_node default_node; |
359d87c6 | 6164 | splay_tree_node node; |
6165 | tree chain; | |
4ee9c684 | 6166 | |
6167 | if (!warn_switch && !warn_switch_enum && !warn_switch_default) | |
6168 | return; | |
6169 | ||
4ee9c684 | 6170 | default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); |
8b6866af | 6171 | if (!default_node) |
5fb6a912 | 6172 | warning_at (switch_location, OPT_Wswitch_default, |
6173 | "switch missing default case"); | |
4ee9c684 | 6174 | |
359d87c6 | 6175 | /* From here on, we only care about about enumerated types. */ |
6176 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
6177 | return; | |
6178 | ||
561017b5 | 6179 | /* From here on, we only care about -Wswitch and -Wswitch-enum. */ |
6180 | if (!warn_switch_enum && !warn_switch) | |
359d87c6 | 6181 | return; |
6182 | ||
561017b5 | 6183 | /* Check the cases. Warn about case values which are not members of |
6184 | the enumerated type. For -Wswitch-enum, or for -Wswitch when | |
6185 | there is no default case, check that exactly all enumeration | |
6186 | literals are covered by the cases. */ | |
6187 | ||
359d87c6 | 6188 | /* Clearing COND if it is not an integer constant simplifies |
6189 | the tests inside the loop below. */ | |
6190 | if (TREE_CODE (cond) != INTEGER_CST) | |
6191 | cond = NULL_TREE; | |
6192 | ||
6193 | /* The time complexity here is O(N*lg(N)) worst case, but for the | |
6194 | common case of monotonically increasing enumerators, it is | |
6195 | O(N), since the nature of the splay tree will keep the next | |
6196 | element adjacent to the root at all times. */ | |
4ee9c684 | 6197 | |
359d87c6 | 6198 | for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) |
6199 | { | |
6200 | tree value = TREE_VALUE (chain); | |
3f00a6c0 | 6201 | if (TREE_CODE (value) == CONST_DECL) |
6202 | value = DECL_INITIAL (value); | |
359d87c6 | 6203 | node = splay_tree_lookup (cases, (splay_tree_key) value); |
6204 | if (node) | |
4ee9c684 | 6205 | { |
359d87c6 | 6206 | /* Mark the CASE_LOW part of the case entry as seen. */ |
6207 | tree label = (tree) node->value; | |
6208 | CASE_LOW_SEEN (label) = 1; | |
6209 | continue; | |
6210 | } | |
6211 | ||
6212 | /* Even though there wasn't an exact match, there might be a | |
f0b5f617 | 6213 | case range which includes the enumerator's value. */ |
359d87c6 | 6214 | node = splay_tree_predecessor (cases, (splay_tree_key) value); |
6215 | if (node && CASE_HIGH ((tree) node->value)) | |
6216 | { | |
6217 | tree label = (tree) node->value; | |
6218 | int cmp = tree_int_cst_compare (CASE_HIGH (label), value); | |
6219 | if (cmp >= 0) | |
4ee9c684 | 6220 | { |
359d87c6 | 6221 | /* If we match the upper bound exactly, mark the CASE_HIGH |
6222 | part of the case entry as seen. */ | |
6223 | if (cmp == 0) | |
6224 | CASE_HIGH_SEEN (label) = 1; | |
6225 | continue; | |
4ee9c684 | 6226 | } |
6227 | } | |
6228 | ||
359d87c6 | 6229 | /* We've now determined that this enumerated literal isn't |
6230 | handled by the case labels of the switch statement. */ | |
4ee9c684 | 6231 | |
359d87c6 | 6232 | /* If the switch expression is a constant, we only really care |
6233 | about whether that constant is handled by the switch. */ | |
6234 | if (cond && tree_int_cst_compare (cond, value)) | |
6235 | continue; | |
4ee9c684 | 6236 | |
6cbbbc89 | 6237 | /* If there is a default_node, the only relevant option is |
561017b5 | 6238 | Wswitch-enum. Otherwise, if both are enabled then we prefer |
6cbbbc89 | 6239 | to warn using -Wswitch because -Wswitch is enabled by -Wall |
6240 | while -Wswitch-enum is explicit. */ | |
561017b5 | 6241 | warning_at (switch_location, |
6242 | (default_node || !warn_switch | |
6243 | ? OPT_Wswitch_enum | |
6244 | : OPT_Wswitch), | |
6245 | "enumeration value %qE not handled in switch", | |
6246 | TREE_PURPOSE (chain)); | |
4ee9c684 | 6247 | } |
359d87c6 | 6248 | |
6249 | /* Warn if there are case expressions that don't correspond to | |
6250 | enumerators. This can occur since C and C++ don't enforce | |
6251 | type-checking of assignments to enumeration variables. | |
6252 | ||
6253 | The time complexity here is now always O(N) worst case, since | |
6254 | we should have marked both the lower bound and upper bound of | |
6255 | every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN | |
6256 | above. This scan also resets those fields. */ | |
6cbbbc89 | 6257 | |
359d87c6 | 6258 | splay_tree_foreach (cases, match_case_to_enum, type); |
e41f0d80 | 6259 | } |
6260 | ||
9dd48740 | 6261 | /* Finish an expression taking the address of LABEL (an |
dda49785 | 6262 | IDENTIFIER_NODE). Returns an expression for the address. |
6263 | ||
6264 | LOC is the location for the expression returned. */ | |
d0a47c8d | 6265 | |
1cae46be | 6266 | tree |
dda49785 | 6267 | finish_label_address_expr (tree label, location_t loc) |
d0a47c8d | 6268 | { |
6269 | tree result; | |
6270 | ||
29438999 | 6271 | pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard"); |
d0a47c8d | 6272 | |
9dd48740 | 6273 | if (label == error_mark_node) |
6274 | return error_mark_node; | |
6275 | ||
d0a47c8d | 6276 | label = lookup_label (label); |
6277 | if (label == NULL_TREE) | |
6278 | result = null_pointer_node; | |
6279 | else | |
6280 | { | |
6281 | TREE_USED (label) = 1; | |
6282 | result = build1 (ADDR_EXPR, ptr_type_node, label); | |
344cd9b2 | 6283 | /* The current function is not necessarily uninlinable. |
d0a47c8d | 6284 | Computed gotos are incompatible with inlining, but the value |
6285 | here could be used only in a diagnostic, for example. */ | |
dda49785 | 6286 | protected_set_expr_location (result, loc); |
d0a47c8d | 6287 | } |
6288 | ||
6289 | return result; | |
6290 | } | |
4f9a1c9b | 6291 | \f |
6292 | ||
6293 | /* Given a boolean expression ARG, return a tree representing an increment | |
6294 | or decrement (as indicated by CODE) of ARG. The front end must check for | |
6295 | invalid cases (e.g., decrement in C++). */ | |
6296 | tree | |
1cae46be | 6297 | boolean_increment (enum tree_code code, tree arg) |
4f9a1c9b | 6298 | { |
6299 | tree val; | |
69db191c | 6300 | tree true_res = build_int_cst (TREE_TYPE (arg), 1); |
c0f19401 | 6301 | |
4f9a1c9b | 6302 | arg = stabilize_reference (arg); |
6303 | switch (code) | |
6304 | { | |
6305 | case PREINCREMENT_EXPR: | |
14ae0310 | 6306 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 6307 | break; |
6308 | case POSTINCREMENT_EXPR: | |
14ae0310 | 6309 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 6310 | arg = save_expr (arg); |
14ae0310 | 6311 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
6312 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 6313 | break; |
6314 | case PREDECREMENT_EXPR: | |
14ae0310 | 6315 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
389dd41b | 6316 | invert_truthvalue_loc (input_location, arg)); |
4f9a1c9b | 6317 | break; |
6318 | case POSTDECREMENT_EXPR: | |
14ae0310 | 6319 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
389dd41b | 6320 | invert_truthvalue_loc (input_location, arg)); |
4f9a1c9b | 6321 | arg = save_expr (arg); |
14ae0310 | 6322 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
6323 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 6324 | break; |
6325 | default: | |
231bd014 | 6326 | gcc_unreachable (); |
4f9a1c9b | 6327 | } |
6328 | TREE_SIDE_EFFECTS (val) = 1; | |
6329 | return val; | |
6330 | } | |
76a6e674 | 6331 | \f |
f3449a3c | 6332 | /* Built-in macros for stddef.h and stdint.h, that require macros |
6333 | defined in this file. */ | |
79cf3ec1 | 6334 | void |
1cae46be | 6335 | c_stddef_cpp_builtins(void) |
1ed9d5f5 | 6336 | { |
63994318 | 6337 | builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0); |
6338 | builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0); | |
6339 | builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0); | |
6340 | builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0); | |
36bccbfc | 6341 | builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0); |
6342 | builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0); | |
b0726616 | 6343 | builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0); |
6344 | builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0); | |
f3449a3c | 6345 | if (SIG_ATOMIC_TYPE) |
6346 | builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0); | |
6347 | if (INT8_TYPE) | |
6348 | builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0); | |
6349 | if (INT16_TYPE) | |
6350 | builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0); | |
6351 | if (INT32_TYPE) | |
6352 | builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0); | |
6353 | if (INT64_TYPE) | |
6354 | builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0); | |
6355 | if (UINT8_TYPE) | |
6356 | builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0); | |
6357 | if (UINT16_TYPE) | |
6358 | builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0); | |
6359 | if (UINT32_TYPE) | |
6360 | builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0); | |
6361 | if (UINT64_TYPE) | |
6362 | builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0); | |
6363 | if (INT_LEAST8_TYPE) | |
6364 | builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0); | |
6365 | if (INT_LEAST16_TYPE) | |
6366 | builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0); | |
6367 | if (INT_LEAST32_TYPE) | |
6368 | builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0); | |
6369 | if (INT_LEAST64_TYPE) | |
6370 | builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0); | |
6371 | if (UINT_LEAST8_TYPE) | |
6372 | builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0); | |
6373 | if (UINT_LEAST16_TYPE) | |
6374 | builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0); | |
6375 | if (UINT_LEAST32_TYPE) | |
6376 | builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0); | |
6377 | if (UINT_LEAST64_TYPE) | |
6378 | builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0); | |
6379 | if (INT_FAST8_TYPE) | |
6380 | builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0); | |
6381 | if (INT_FAST16_TYPE) | |
6382 | builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0); | |
6383 | if (INT_FAST32_TYPE) | |
6384 | builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0); | |
6385 | if (INT_FAST64_TYPE) | |
6386 | builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0); | |
6387 | if (UINT_FAST8_TYPE) | |
6388 | builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0); | |
6389 | if (UINT_FAST16_TYPE) | |
6390 | builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0); | |
6391 | if (UINT_FAST32_TYPE) | |
6392 | builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0); | |
6393 | if (UINT_FAST64_TYPE) | |
6394 | builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0); | |
6395 | if (INTPTR_TYPE) | |
6396 | builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0); | |
6397 | if (UINTPTR_TYPE) | |
6398 | builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0); | |
574006c3 | 6399 | } |
6400 | ||
7d3b509a | 6401 | static void |
1cae46be | 6402 | c_init_attributes (void) |
7d3b509a | 6403 | { |
6404 | /* Fill in the built_in_attributes array. */ | |
7c446c95 | 6405 | #define DEF_ATTR_NULL_TREE(ENUM) \ |
7d3b509a | 6406 | built_in_attributes[(int) ENUM] = NULL_TREE; |
7c446c95 | 6407 | #define DEF_ATTR_INT(ENUM, VALUE) \ |
ceb7b692 | 6408 | built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE); |
c8010b80 | 6409 | #define DEF_ATTR_STRING(ENUM, VALUE) \ |
6410 | built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE); | |
7d3b509a | 6411 | #define DEF_ATTR_IDENT(ENUM, STRING) \ |
6412 | built_in_attributes[(int) ENUM] = get_identifier (STRING); | |
6413 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \ | |
6414 | built_in_attributes[(int) ENUM] \ | |
6415 | = tree_cons (built_in_attributes[(int) PURPOSE], \ | |
6416 | built_in_attributes[(int) VALUE], \ | |
6417 | built_in_attributes[(int) CHAIN]); | |
7d3b509a | 6418 | #include "builtin-attrs.def" |
6419 | #undef DEF_ATTR_NULL_TREE | |
6420 | #undef DEF_ATTR_INT | |
6421 | #undef DEF_ATTR_IDENT | |
6422 | #undef DEF_ATTR_TREE_LIST | |
76a6e674 | 6423 | } |
5f3cead1 | 6424 | |
33199a81 | 6425 | /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain |
6426 | identifier as an argument, so the front end shouldn't look it up. */ | |
6427 | ||
6428 | bool | |
47b19b94 | 6429 | attribute_takes_identifier_p (const_tree attr_id) |
33199a81 | 6430 | { |
9bf1c74e | 6431 | const struct attribute_spec *spec = lookup_attribute_spec (attr_id); |
02cb1060 | 6432 | if (spec == NULL) |
6433 | /* Unknown attribute that we'll end up ignoring, return true so we | |
6434 | don't complain about an identifier argument. */ | |
6435 | return true; | |
6436 | else if (!strcmp ("mode", spec->name) | |
6437 | || !strcmp ("format", spec->name) | |
6438 | || !strcmp ("cleanup", spec->name)) | |
47b19b94 | 6439 | return true; |
6440 | else | |
6441 | return targetm.attribute_takes_identifier_p (attr_id); | |
33199a81 | 6442 | } |
6443 | ||
f8e93a2e | 6444 | /* Attribute handlers common to C front ends. */ |
6445 | ||
6446 | /* Handle a "packed" attribute; arguments as in | |
6447 | struct attribute_spec.handler. */ | |
6448 | ||
6449 | static tree | |
9a03a746 | 6450 | handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
09347743 | 6451 | int flags, bool *no_add_attrs) |
f8e93a2e | 6452 | { |
f40175cb | 6453 | if (TYPE_P (*node)) |
f8e93a2e | 6454 | { |
6455 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 6456 | *node = build_variant_type_copy (*node); |
f40175cb | 6457 | TYPE_PACKED (*node) = 1; |
f8e93a2e | 6458 | } |
6459 | else if (TREE_CODE (*node) == FIELD_DECL) | |
c2ab04f9 | 6460 | { |
9fd767c5 | 6461 | if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT |
6462 | /* Still pack bitfields. */ | |
6463 | && ! DECL_INITIAL (*node)) | |
c2ab04f9 | 6464 | warning (OPT_Wattributes, |
6465 | "%qE attribute ignored for field of type %qT", | |
6466 | name, TREE_TYPE (*node)); | |
6467 | else | |
6468 | DECL_PACKED (*node) = 1; | |
6469 | } | |
f8e93a2e | 6470 | /* We can't set DECL_PACKED for a VAR_DECL, because the bit is |
f40175cb | 6471 | used for DECL_REGISTER. It wouldn't mean anything anyway. |
6472 | We can't set DECL_PACKED on the type of a TYPE_DECL, because | |
6473 | that changes what the typedef is typing. */ | |
f8e93a2e | 6474 | else |
6475 | { | |
9b2d6d13 | 6476 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6477 | *no_add_attrs = true; |
6478 | } | |
6479 | ||
6480 | return NULL_TREE; | |
6481 | } | |
6482 | ||
6483 | /* Handle a "nocommon" attribute; arguments as in | |
6484 | struct attribute_spec.handler. */ | |
6485 | ||
6486 | static tree | |
1cae46be | 6487 | handle_nocommon_attribute (tree *node, tree name, |
9a03a746 | 6488 | tree ARG_UNUSED (args), |
6489 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6490 | { |
6491 | if (TREE_CODE (*node) == VAR_DECL) | |
6492 | DECL_COMMON (*node) = 0; | |
6493 | else | |
6494 | { | |
9b2d6d13 | 6495 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6496 | *no_add_attrs = true; |
6497 | } | |
6498 | ||
6499 | return NULL_TREE; | |
6500 | } | |
6501 | ||
6502 | /* Handle a "common" attribute; arguments as in | |
6503 | struct attribute_spec.handler. */ | |
6504 | ||
6505 | static tree | |
9a03a746 | 6506 | handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6507 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6508 | { |
6509 | if (TREE_CODE (*node) == VAR_DECL) | |
6510 | DECL_COMMON (*node) = 1; | |
6511 | else | |
6512 | { | |
9b2d6d13 | 6513 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6514 | *no_add_attrs = true; |
6515 | } | |
6516 | ||
6517 | return NULL_TREE; | |
6518 | } | |
6519 | ||
6520 | /* Handle a "noreturn" attribute; arguments as in | |
6521 | struct attribute_spec.handler. */ | |
6522 | ||
6523 | static tree | |
9a03a746 | 6524 | handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6525 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6526 | { |
6527 | tree type = TREE_TYPE (*node); | |
6528 | ||
6529 | /* See FIXME comment in c_common_attribute_table. */ | |
8c582e4f | 6530 | if (TREE_CODE (*node) == FUNCTION_DECL |
6531 | || objc_method_decl (TREE_CODE (*node))) | |
f8e93a2e | 6532 | TREE_THIS_VOLATILE (*node) = 1; |
6533 | else if (TREE_CODE (type) == POINTER_TYPE | |
6534 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
6535 | TREE_TYPE (*node) | |
6536 | = build_pointer_type | |
6537 | (build_type_variant (TREE_TYPE (type), | |
71bda45d | 6538 | TYPE_READONLY (TREE_TYPE (type)), 1)); |
f8e93a2e | 6539 | else |
6540 | { | |
9b2d6d13 | 6541 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6542 | *no_add_attrs = true; |
6543 | } | |
6544 | ||
6545 | return NULL_TREE; | |
6546 | } | |
6547 | ||
5de92639 | 6548 | /* Handle a "hot" and attribute; arguments as in |
6549 | struct attribute_spec.handler. */ | |
6550 | ||
6551 | static tree | |
6552 | handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
46f8e3b0 | 6553 | int ARG_UNUSED (flags), bool *no_add_attrs) |
5de92639 | 6554 | { |
758a38ab | 6555 | if (TREE_CODE (*node) == FUNCTION_DECL |
6556 | || TREE_CODE (*node) == LABEL_DECL) | |
5de92639 | 6557 | { |
6558 | if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL) | |
6559 | { | |
6560 | warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", | |
6561 | name, "cold"); | |
6562 | *no_add_attrs = true; | |
6563 | } | |
24470055 | 6564 | /* Most of the rest of the hot processing is done later with |
6565 | lookup_attribute. */ | |
5de92639 | 6566 | } |
6567 | else | |
6568 | { | |
6569 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6570 | *no_add_attrs = true; | |
6571 | } | |
6572 | ||
6573 | return NULL_TREE; | |
6574 | } | |
758a38ab | 6575 | |
5de92639 | 6576 | /* Handle a "cold" and attribute; arguments as in |
6577 | struct attribute_spec.handler. */ | |
6578 | ||
6579 | static tree | |
6580 | handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
6581 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6582 | { | |
758a38ab | 6583 | if (TREE_CODE (*node) == FUNCTION_DECL |
6584 | || TREE_CODE (*node) == LABEL_DECL) | |
5de92639 | 6585 | { |
6586 | if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL) | |
6587 | { | |
6588 | warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", | |
6589 | name, "hot"); | |
6590 | *no_add_attrs = true; | |
6591 | } | |
24470055 | 6592 | /* Most of the rest of the cold processing is done later with |
6593 | lookup_attribute. */ | |
5de92639 | 6594 | } |
6595 | else | |
6596 | { | |
6597 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6598 | *no_add_attrs = true; | |
6599 | } | |
6600 | ||
6601 | return NULL_TREE; | |
6602 | } | |
6603 | ||
a9196da9 | 6604 | /* Handle a "no_sanitize_address" attribute; arguments as in |
d413ffdd | 6605 | struct attribute_spec.handler. */ |
6606 | ||
6607 | static tree | |
a9196da9 | 6608 | handle_no_sanitize_address_attribute (tree *node, tree name, tree, int, |
6609 | bool *no_add_attrs) | |
d413ffdd | 6610 | { |
6611 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6612 | { | |
6613 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6614 | *no_add_attrs = true; | |
6615 | } | |
6616 | ||
6617 | return NULL_TREE; | |
6618 | } | |
6619 | ||
a9196da9 | 6620 | /* Handle a "no_address_safety_analysis" attribute; arguments as in |
6621 | struct attribute_spec.handler. */ | |
6622 | ||
6623 | static tree | |
6624 | handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int, | |
6625 | bool *no_add_attrs) | |
6626 | { | |
6627 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6628 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6629 | else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node))) | |
6630 | DECL_ATTRIBUTES (*node) | |
6631 | = tree_cons (get_identifier ("no_sanitize_address"), | |
6632 | NULL_TREE, DECL_ATTRIBUTES (*node)); | |
6633 | *no_add_attrs = true; | |
6634 | return NULL_TREE; | |
6635 | } | |
6636 | ||
05f893e1 | 6637 | /* Handle a "no_sanitize_undefined" attribute; arguments as in |
6638 | struct attribute_spec.handler. */ | |
6639 | ||
6640 | static tree | |
6641 | handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int, | |
6642 | bool *no_add_attrs) | |
6643 | { | |
6644 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6645 | { | |
6646 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6647 | *no_add_attrs = true; | |
6648 | } | |
6649 | ||
6650 | return NULL_TREE; | |
6651 | } | |
6652 | ||
f8e93a2e | 6653 | /* Handle a "noinline" attribute; arguments as in |
6654 | struct attribute_spec.handler. */ | |
6655 | ||
6656 | static tree | |
1cae46be | 6657 | handle_noinline_attribute (tree *node, tree name, |
9a03a746 | 6658 | tree ARG_UNUSED (args), |
6659 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6660 | { |
6661 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6662 | DECL_UNINLINABLE (*node) = 1; | |
6663 | else | |
6664 | { | |
9b2d6d13 | 6665 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6666 | *no_add_attrs = true; |
6667 | } | |
6668 | ||
6669 | return NULL_TREE; | |
6670 | } | |
6671 | ||
bdb1f0d1 | 6672 | /* Handle a "noclone" attribute; arguments as in |
6673 | struct attribute_spec.handler. */ | |
6674 | ||
6675 | static tree | |
6676 | handle_noclone_attribute (tree *node, tree name, | |
6677 | tree ARG_UNUSED (args), | |
6678 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6679 | { | |
6680 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6681 | { | |
6682 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6683 | *no_add_attrs = true; | |
6684 | } | |
6685 | ||
6686 | return NULL_TREE; | |
6687 | } | |
6688 | ||
f8e93a2e | 6689 | /* Handle a "always_inline" attribute; arguments as in |
6690 | struct attribute_spec.handler. */ | |
6691 | ||
6692 | static tree | |
1cae46be | 6693 | handle_always_inline_attribute (tree *node, tree name, |
9a03a746 | 6694 | tree ARG_UNUSED (args), |
6695 | int ARG_UNUSED (flags), | |
09347743 | 6696 | bool *no_add_attrs) |
f8e93a2e | 6697 | { |
6698 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6699 | { | |
ebb7d626 | 6700 | /* Set the attribute and mark it for disregarding inline |
6701 | limits. */ | |
6702 | DECL_DISREGARD_INLINE_LIMITS (*node) = 1; | |
f8e93a2e | 6703 | } |
6704 | else | |
6705 | { | |
9b2d6d13 | 6706 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6707 | *no_add_attrs = true; |
541e4101 | 6708 | } |
6709 | ||
6710 | return NULL_TREE; | |
6711 | } | |
6712 | ||
6713 | /* Handle a "gnu_inline" attribute; arguments as in | |
6714 | struct attribute_spec.handler. */ | |
6715 | ||
6716 | static tree | |
6717 | handle_gnu_inline_attribute (tree *node, tree name, | |
6718 | tree ARG_UNUSED (args), | |
6719 | int ARG_UNUSED (flags), | |
6720 | bool *no_add_attrs) | |
6721 | { | |
6722 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6723 | { | |
6724 | /* Do nothing else, just set the attribute. We'll get at | |
6725 | it later with lookup_attribute. */ | |
6726 | } | |
6727 | else | |
6728 | { | |
6729 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6730 | *no_add_attrs = true; | |
7bd95dfd | 6731 | } |
6732 | ||
6733 | return NULL_TREE; | |
6734 | } | |
6735 | ||
6736 | /* Handle a "leaf" attribute; arguments as in | |
6737 | struct attribute_spec.handler. */ | |
6738 | ||
6739 | static tree | |
6740 | handle_leaf_attribute (tree *node, tree name, | |
6741 | tree ARG_UNUSED (args), | |
6742 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6743 | { | |
6744 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6745 | { | |
6746 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6747 | *no_add_attrs = true; | |
6748 | } | |
6749 | if (!TREE_PUBLIC (*node)) | |
6750 | { | |
6751 | warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name); | |
6752 | *no_add_attrs = true; | |
f8e93a2e | 6753 | } |
6754 | ||
6755 | return NULL_TREE; | |
6756 | } | |
6757 | ||
1b16fc45 | 6758 | /* Handle an "artificial" attribute; arguments as in |
6759 | struct attribute_spec.handler. */ | |
6760 | ||
6761 | static tree | |
6762 | handle_artificial_attribute (tree *node, tree name, | |
6763 | tree ARG_UNUSED (args), | |
6764 | int ARG_UNUSED (flags), | |
6765 | bool *no_add_attrs) | |
6766 | { | |
6767 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6768 | { | |
6769 | /* Do nothing else, just set the attribute. We'll get at | |
6770 | it later with lookup_attribute. */ | |
6771 | } | |
6772 | else | |
6773 | { | |
6774 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6775 | *no_add_attrs = true; | |
6776 | } | |
6777 | ||
6778 | return NULL_TREE; | |
6779 | } | |
6780 | ||
0cdd9887 | 6781 | /* Handle a "flatten" attribute; arguments as in |
6782 | struct attribute_spec.handler. */ | |
6783 | ||
6784 | static tree | |
6785 | handle_flatten_attribute (tree *node, tree name, | |
a0c938f0 | 6786 | tree args ATTRIBUTE_UNUSED, |
6787 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
0cdd9887 | 6788 | { |
6789 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6790 | /* Do nothing else, just set the attribute. We'll get at | |
6791 | it later with lookup_attribute. */ | |
6792 | ; | |
6793 | else | |
6794 | { | |
6795 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6796 | *no_add_attrs = true; | |
6797 | } | |
6798 | ||
6799 | return NULL_TREE; | |
6800 | } | |
6801 | ||
10fc867f | 6802 | /* Handle a "warning" or "error" attribute; arguments as in |
6803 | struct attribute_spec.handler. */ | |
6804 | ||
6805 | static tree | |
6806 | handle_error_attribute (tree *node, tree name, tree args, | |
6807 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6808 | { | |
6809 | if (TREE_CODE (*node) == FUNCTION_DECL | |
df936998 | 6810 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) |
10fc867f | 6811 | /* Do nothing else, just set the attribute. We'll get at |
6812 | it later with lookup_attribute. */ | |
6813 | ; | |
6814 | else | |
6815 | { | |
6816 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6817 | *no_add_attrs = true; | |
6818 | } | |
6819 | ||
6820 | return NULL_TREE; | |
6821 | } | |
0cdd9887 | 6822 | |
f8e93a2e | 6823 | /* Handle a "used" attribute; arguments as in |
6824 | struct attribute_spec.handler. */ | |
6825 | ||
6826 | static tree | |
9a03a746 | 6827 | handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args), |
6828 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6829 | { |
d0a31bd8 | 6830 | tree node = *pnode; |
6831 | ||
6832 | if (TREE_CODE (node) == FUNCTION_DECL | |
a4e3ffad | 6833 | || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)) |
6834 | || (TREE_CODE (node) == TYPE_DECL)) | |
f54ed8bc | 6835 | { |
f54ed8bc | 6836 | TREE_USED (node) = 1; |
9423c9b7 | 6837 | DECL_PRESERVE_P (node) = 1; |
abc6c64f | 6838 | if (TREE_CODE (node) == VAR_DECL) |
6839 | DECL_READ_P (node) = 1; | |
f54ed8bc | 6840 | } |
f8e93a2e | 6841 | else |
6842 | { | |
9b2d6d13 | 6843 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6844 | *no_add_attrs = true; |
6845 | } | |
6846 | ||
6847 | return NULL_TREE; | |
6848 | } | |
6849 | ||
6850 | /* Handle a "unused" attribute; arguments as in | |
6851 | struct attribute_spec.handler. */ | |
6852 | ||
6853 | static tree | |
9a03a746 | 6854 | handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6855 | int flags, bool *no_add_attrs) | |
f8e93a2e | 6856 | { |
6857 | if (DECL_P (*node)) | |
6858 | { | |
6859 | tree decl = *node; | |
6860 | ||
6861 | if (TREE_CODE (decl) == PARM_DECL | |
6862 | || TREE_CODE (decl) == VAR_DECL | |
6863 | || TREE_CODE (decl) == FUNCTION_DECL | |
6864 | || TREE_CODE (decl) == LABEL_DECL | |
6865 | || TREE_CODE (decl) == TYPE_DECL) | |
abc6c64f | 6866 | { |
6867 | TREE_USED (decl) = 1; | |
6868 | if (TREE_CODE (decl) == VAR_DECL | |
6869 | || TREE_CODE (decl) == PARM_DECL) | |
6870 | DECL_READ_P (decl) = 1; | |
6871 | } | |
f8e93a2e | 6872 | else |
6873 | { | |
9b2d6d13 | 6874 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6875 | *no_add_attrs = true; |
6876 | } | |
6877 | } | |
6878 | else | |
6879 | { | |
6880 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 6881 | *node = build_variant_type_copy (*node); |
f8e93a2e | 6882 | TREE_USED (*node) = 1; |
6883 | } | |
6884 | ||
6885 | return NULL_TREE; | |
6886 | } | |
6887 | ||
62eec3b4 | 6888 | /* Handle a "externally_visible" attribute; arguments as in |
6889 | struct attribute_spec.handler. */ | |
6890 | ||
6891 | static tree | |
6892 | handle_externally_visible_attribute (tree *pnode, tree name, | |
6893 | tree ARG_UNUSED (args), | |
6894 | int ARG_UNUSED (flags), | |
6895 | bool *no_add_attrs) | |
6896 | { | |
6897 | tree node = *pnode; | |
6898 | ||
ba12ea31 | 6899 | if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL) |
62eec3b4 | 6900 | { |
ba12ea31 | 6901 | if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL |
6902 | && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node)) | |
6903 | { | |
6904 | warning (OPT_Wattributes, | |
6905 | "%qE attribute have effect only on public objects", name); | |
6906 | *no_add_attrs = true; | |
6907 | } | |
62eec3b4 | 6908 | } |
62eec3b4 | 6909 | else |
6910 | { | |
6911 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6912 | *no_add_attrs = true; | |
6913 | } | |
6914 | ||
6915 | return NULL_TREE; | |
6916 | } | |
6917 | ||
f8e93a2e | 6918 | /* Handle a "const" attribute; arguments as in |
6919 | struct attribute_spec.handler. */ | |
6920 | ||
6921 | static tree | |
9a03a746 | 6922 | handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6923 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6924 | { |
6925 | tree type = TREE_TYPE (*node); | |
6926 | ||
6927 | /* See FIXME comment on noreturn in c_common_attribute_table. */ | |
6928 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6929 | TREE_READONLY (*node) = 1; | |
6930 | else if (TREE_CODE (type) == POINTER_TYPE | |
6931 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
6932 | TREE_TYPE (*node) | |
6933 | = build_pointer_type | |
6934 | (build_type_variant (TREE_TYPE (type), 1, | |
6935 | TREE_THIS_VOLATILE (TREE_TYPE (type)))); | |
6936 | else | |
6937 | { | |
9b2d6d13 | 6938 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6939 | *no_add_attrs = true; |
6940 | } | |
6941 | ||
6942 | return NULL_TREE; | |
6943 | } | |
6944 | ||
6945 | /* Handle a "transparent_union" attribute; arguments as in | |
6946 | struct attribute_spec.handler. */ | |
6947 | ||
6948 | static tree | |
1cae46be | 6949 | handle_transparent_union_attribute (tree *node, tree name, |
9a03a746 | 6950 | tree ARG_UNUSED (args), int flags, |
09347743 | 6951 | bool *no_add_attrs) |
f8e93a2e | 6952 | { |
881eb642 | 6953 | tree type; |
03908818 | 6954 | |
6955 | *no_add_attrs = true; | |
f8e93a2e | 6956 | |
ffcdbf9c | 6957 | |
6958 | if (TREE_CODE (*node) == TYPE_DECL | |
6959 | && ! (flags & ATTR_FLAG_CXX11)) | |
881eb642 | 6960 | node = &TREE_TYPE (*node); |
6961 | type = *node; | |
f8e93a2e | 6962 | |
03908818 | 6963 | if (TREE_CODE (type) == UNION_TYPE) |
f8e93a2e | 6964 | { |
fca86134 | 6965 | /* Make sure that the first field will work for a transparent union. |
6966 | If the type isn't complete yet, leave the check to the code in | |
6967 | finish_struct. */ | |
6968 | if (TYPE_SIZE (type)) | |
6969 | { | |
6970 | tree first = first_field (type); | |
6971 | if (first == NULL_TREE | |
6972 | || DECL_ARTIFICIAL (first) | |
6973 | || TYPE_MODE (type) != DECL_MODE (first)) | |
6974 | goto ignored; | |
6975 | } | |
6976 | ||
f8e93a2e | 6977 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
03908818 | 6978 | { |
fca86134 | 6979 | /* If the type isn't complete yet, setting the flag |
6980 | on a variant wouldn't ever be checked. */ | |
6981 | if (!TYPE_SIZE (type)) | |
6982 | goto ignored; | |
6983 | ||
6984 | /* build_duplicate_type doesn't work for C++. */ | |
6985 | if (c_dialect_cxx ()) | |
03908818 | 6986 | goto ignored; |
6987 | ||
6988 | /* A type variant isn't good enough, since we don't a cast | |
6989 | to such a type removed as a no-op. */ | |
6990 | *node = type = build_duplicate_type (type); | |
6991 | } | |
6992 | ||
8df5a43d | 6993 | TYPE_TRANSPARENT_AGGR (type) = 1; |
03908818 | 6994 | return NULL_TREE; |
f8e93a2e | 6995 | } |
6996 | ||
03908818 | 6997 | ignored: |
6998 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 6999 | return NULL_TREE; |
7000 | } | |
7001 | ||
9af7fd5b | 7002 | /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to |
7003 | get the requested priority for a constructor or destructor, | |
7004 | possibly issuing diagnostics for invalid or reserved | |
7005 | priorities. */ | |
7006 | ||
7007 | static priority_type | |
7008 | get_priority (tree args, bool is_destructor) | |
7009 | { | |
7010 | HOST_WIDE_INT pri; | |
6c181a06 | 7011 | tree arg; |
9af7fd5b | 7012 | |
7013 | if (!args) | |
7014 | return DEFAULT_INIT_PRIORITY; | |
48e1416a | 7015 | |
28fbc04f | 7016 | if (!SUPPORTS_INIT_PRIORITY) |
7017 | { | |
7018 | if (is_destructor) | |
7019 | error ("destructor priorities are not supported"); | |
7020 | else | |
7021 | error ("constructor priorities are not supported"); | |
7022 | return DEFAULT_INIT_PRIORITY; | |
7023 | } | |
7024 | ||
6c181a06 | 7025 | arg = TREE_VALUE (args); |
253e1cae | 7026 | if (TREE_CODE (arg) == IDENTIFIER_NODE) |
7027 | goto invalid; | |
7028 | if (arg == error_mark_node) | |
7029 | return DEFAULT_INIT_PRIORITY; | |
c28ddc97 | 7030 | arg = default_conversion (arg); |
35ec552a | 7031 | if (!tree_fits_shwi_p (arg) |
6c181a06 | 7032 | || !INTEGRAL_TYPE_P (TREE_TYPE (arg))) |
9af7fd5b | 7033 | goto invalid; |
7034 | ||
fcb97e84 | 7035 | pri = tree_to_shwi (arg); |
9af7fd5b | 7036 | if (pri < 0 || pri > MAX_INIT_PRIORITY) |
7037 | goto invalid; | |
7038 | ||
7039 | if (pri <= MAX_RESERVED_INIT_PRIORITY) | |
7040 | { | |
7041 | if (is_destructor) | |
7042 | warning (0, | |
7043 | "destructor priorities from 0 to %d are reserved " | |
48e1416a | 7044 | "for the implementation", |
9af7fd5b | 7045 | MAX_RESERVED_INIT_PRIORITY); |
7046 | else | |
7047 | warning (0, | |
7048 | "constructor priorities from 0 to %d are reserved " | |
48e1416a | 7049 | "for the implementation", |
9af7fd5b | 7050 | MAX_RESERVED_INIT_PRIORITY); |
7051 | } | |
7052 | return pri; | |
7053 | ||
7054 | invalid: | |
7055 | if (is_destructor) | |
7056 | error ("destructor priorities must be integers from 0 to %d inclusive", | |
7057 | MAX_INIT_PRIORITY); | |
7058 | else | |
7059 | error ("constructor priorities must be integers from 0 to %d inclusive", | |
7060 | MAX_INIT_PRIORITY); | |
7061 | return DEFAULT_INIT_PRIORITY; | |
7062 | } | |
7063 | ||
f8e93a2e | 7064 | /* Handle a "constructor" attribute; arguments as in |
7065 | struct attribute_spec.handler. */ | |
7066 | ||
7067 | static tree | |
9af7fd5b | 7068 | handle_constructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 7069 | int ARG_UNUSED (flags), |
09347743 | 7070 | bool *no_add_attrs) |
f8e93a2e | 7071 | { |
7072 | tree decl = *node; | |
7073 | tree type = TREE_TYPE (decl); | |
7074 | ||
7075 | if (TREE_CODE (decl) == FUNCTION_DECL | |
7076 | && TREE_CODE (type) == FUNCTION_TYPE | |
7077 | && decl_function_context (decl) == 0) | |
7078 | { | |
9af7fd5b | 7079 | priority_type priority; |
f8e93a2e | 7080 | DECL_STATIC_CONSTRUCTOR (decl) = 1; |
9af7fd5b | 7081 | priority = get_priority (args, /*is_destructor=*/false); |
7082 | SET_DECL_INIT_PRIORITY (decl, priority); | |
f8e93a2e | 7083 | TREE_USED (decl) = 1; |
7084 | } | |
7085 | else | |
7086 | { | |
9b2d6d13 | 7087 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7088 | *no_add_attrs = true; |
7089 | } | |
7090 | ||
7091 | return NULL_TREE; | |
7092 | } | |
7093 | ||
7094 | /* Handle a "destructor" attribute; arguments as in | |
7095 | struct attribute_spec.handler. */ | |
7096 | ||
7097 | static tree | |
9af7fd5b | 7098 | handle_destructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 7099 | int ARG_UNUSED (flags), |
09347743 | 7100 | bool *no_add_attrs) |
f8e93a2e | 7101 | { |
7102 | tree decl = *node; | |
7103 | tree type = TREE_TYPE (decl); | |
7104 | ||
7105 | if (TREE_CODE (decl) == FUNCTION_DECL | |
7106 | && TREE_CODE (type) == FUNCTION_TYPE | |
7107 | && decl_function_context (decl) == 0) | |
7108 | { | |
9af7fd5b | 7109 | priority_type priority; |
f8e93a2e | 7110 | DECL_STATIC_DESTRUCTOR (decl) = 1; |
9af7fd5b | 7111 | priority = get_priority (args, /*is_destructor=*/true); |
7112 | SET_DECL_FINI_PRIORITY (decl, priority); | |
f8e93a2e | 7113 | TREE_USED (decl) = 1; |
7114 | } | |
7115 | else | |
7116 | { | |
9b2d6d13 | 7117 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7118 | *no_add_attrs = true; |
7119 | } | |
7120 | ||
7121 | return NULL_TREE; | |
7122 | } | |
7123 | ||
1c58e3f1 | 7124 | /* Nonzero if the mode is a valid vector mode for this architecture. |
7125 | This returns nonzero even if there is no hardware support for the | |
7126 | vector mode, but we can emulate with narrower modes. */ | |
7127 | ||
7128 | static int | |
7129 | vector_mode_valid_p (enum machine_mode mode) | |
7130 | { | |
7131 | enum mode_class mclass = GET_MODE_CLASS (mode); | |
7132 | enum machine_mode innermode; | |
7133 | ||
7134 | /* Doh! What's going on? */ | |
7135 | if (mclass != MODE_VECTOR_INT | |
7136 | && mclass != MODE_VECTOR_FLOAT | |
7137 | && mclass != MODE_VECTOR_FRACT | |
7138 | && mclass != MODE_VECTOR_UFRACT | |
7139 | && mclass != MODE_VECTOR_ACCUM | |
7140 | && mclass != MODE_VECTOR_UACCUM) | |
7141 | return 0; | |
7142 | ||
7143 | /* Hardware support. Woo hoo! */ | |
7144 | if (targetm.vector_mode_supported_p (mode)) | |
7145 | return 1; | |
7146 | ||
7147 | innermode = GET_MODE_INNER (mode); | |
7148 | ||
7149 | /* We should probably return 1 if requesting V4DI and we have no DI, | |
7150 | but we have V2DI, but this is probably very unlikely. */ | |
7151 | ||
7152 | /* If we have support for the inner mode, we can safely emulate it. | |
7153 | We may not have V2DI, but me can emulate with a pair of DIs. */ | |
7154 | return targetm.scalar_mode_supported_p (innermode); | |
7155 | } | |
7156 | ||
7157 | ||
f8e93a2e | 7158 | /* Handle a "mode" attribute; arguments as in |
7159 | struct attribute_spec.handler. */ | |
7160 | ||
7161 | static tree | |
9a03a746 | 7162 | handle_mode_attribute (tree *node, tree name, tree args, |
7163 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7164 | { |
7165 | tree type = *node; | |
d1dd9ac0 | 7166 | tree ident = TREE_VALUE (args); |
f8e93a2e | 7167 | |
7168 | *no_add_attrs = true; | |
7169 | ||
d1dd9ac0 | 7170 | if (TREE_CODE (ident) != IDENTIFIER_NODE) |
9b2d6d13 | 7171 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7172 | else |
7173 | { | |
7174 | int j; | |
d1dd9ac0 | 7175 | const char *p = IDENTIFIER_POINTER (ident); |
f8e93a2e | 7176 | int len = strlen (p); |
7177 | enum machine_mode mode = VOIDmode; | |
7178 | tree typefm; | |
b2aef146 | 7179 | bool valid_mode; |
f8e93a2e | 7180 | |
7181 | if (len > 4 && p[0] == '_' && p[1] == '_' | |
7182 | && p[len - 1] == '_' && p[len - 2] == '_') | |
7183 | { | |
4fd61bc6 | 7184 | char *newp = (char *) alloca (len - 1); |
f8e93a2e | 7185 | |
7186 | strcpy (newp, &p[2]); | |
7187 | newp[len - 4] = '\0'; | |
7188 | p = newp; | |
7189 | } | |
7190 | ||
7191 | /* Change this type to have a type with the specified mode. | |
7192 | First check for the special modes. */ | |
84166705 | 7193 | if (!strcmp (p, "byte")) |
f8e93a2e | 7194 | mode = byte_mode; |
7195 | else if (!strcmp (p, "word")) | |
7196 | mode = word_mode; | |
84166705 | 7197 | else if (!strcmp (p, "pointer")) |
f8e93a2e | 7198 | mode = ptr_mode; |
0ef89dfd | 7199 | else if (!strcmp (p, "libgcc_cmp_return")) |
7200 | mode = targetm.libgcc_cmp_return_mode (); | |
7201 | else if (!strcmp (p, "libgcc_shift_count")) | |
7202 | mode = targetm.libgcc_shift_count_mode (); | |
1bd43494 | 7203 | else if (!strcmp (p, "unwind_word")) |
7204 | mode = targetm.unwind_word_mode (); | |
f8e93a2e | 7205 | else |
7206 | for (j = 0; j < NUM_MACHINE_MODES; j++) | |
7207 | if (!strcmp (p, GET_MODE_NAME (j))) | |
743a6f47 | 7208 | { |
7209 | mode = (enum machine_mode) j; | |
7210 | break; | |
7211 | } | |
f8e93a2e | 7212 | |
7213 | if (mode == VOIDmode) | |
4917c376 | 7214 | { |
d1dd9ac0 | 7215 | error ("unknown machine mode %qE", ident); |
4917c376 | 7216 | return NULL_TREE; |
7217 | } | |
7218 | ||
b2aef146 | 7219 | valid_mode = false; |
7220 | switch (GET_MODE_CLASS (mode)) | |
4917c376 | 7221 | { |
b2aef146 | 7222 | case MODE_INT: |
7223 | case MODE_PARTIAL_INT: | |
7224 | case MODE_FLOAT: | |
c4503c0a | 7225 | case MODE_DECIMAL_FLOAT: |
9421ebb9 | 7226 | case MODE_FRACT: |
7227 | case MODE_UFRACT: | |
7228 | case MODE_ACCUM: | |
7229 | case MODE_UACCUM: | |
b2aef146 | 7230 | valid_mode = targetm.scalar_mode_supported_p (mode); |
7231 | break; | |
7232 | ||
7233 | case MODE_COMPLEX_INT: | |
7234 | case MODE_COMPLEX_FLOAT: | |
7235 | valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode)); | |
7236 | break; | |
7237 | ||
7238 | case MODE_VECTOR_INT: | |
7239 | case MODE_VECTOR_FLOAT: | |
9421ebb9 | 7240 | case MODE_VECTOR_FRACT: |
7241 | case MODE_VECTOR_UFRACT: | |
7242 | case MODE_VECTOR_ACCUM: | |
7243 | case MODE_VECTOR_UACCUM: | |
9b2d6d13 | 7244 | warning (OPT_Wattributes, "specifying vector types with " |
7245 | "__attribute__ ((mode)) is deprecated"); | |
7246 | warning (OPT_Wattributes, | |
7247 | "use __attribute__ ((vector_size)) instead"); | |
b2aef146 | 7248 | valid_mode = vector_mode_valid_p (mode); |
7249 | break; | |
4917c376 | 7250 | |
b2aef146 | 7251 | default: |
7252 | break; | |
7253 | } | |
7254 | if (!valid_mode) | |
7255 | { | |
1e5fcbe2 | 7256 | error ("unable to emulate %qs", p); |
b2aef146 | 7257 | return NULL_TREE; |
7258 | } | |
4917c376 | 7259 | |
b2aef146 | 7260 | if (POINTER_TYPE_P (type)) |
ead34f59 | 7261 | { |
6d5d708e | 7262 | addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type)); |
b2aef146 | 7263 | tree (*fn)(tree, enum machine_mode, bool); |
7264 | ||
6d5d708e | 7265 | if (!targetm.addr_space.valid_pointer_mode (mode, as)) |
ead34f59 | 7266 | { |
1e5fcbe2 | 7267 | error ("invalid pointer mode %qs", p); |
ead34f59 | 7268 | return NULL_TREE; |
7269 | } | |
7270 | ||
a0c938f0 | 7271 | if (TREE_CODE (type) == POINTER_TYPE) |
b2aef146 | 7272 | fn = build_pointer_type_for_mode; |
805e22b2 | 7273 | else |
b2aef146 | 7274 | fn = build_reference_type_for_mode; |
7275 | typefm = fn (TREE_TYPE (type), mode, false); | |
ead34f59 | 7276 | } |
b2aef146 | 7277 | else |
9421ebb9 | 7278 | { |
7279 | /* For fixed-point modes, we need to test if the signness of type | |
7280 | and the machine mode are consistent. */ | |
7281 | if (ALL_FIXED_POINT_MODE_P (mode) | |
7282 | && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode)) | |
7283 | { | |
bf776685 | 7284 | error ("signedness of type and machine mode %qs don%'t match", p); |
9421ebb9 | 7285 | return NULL_TREE; |
7286 | } | |
7287 | /* For fixed-point modes, we need to pass saturating info. */ | |
7288 | typefm = lang_hooks.types.type_for_mode (mode, | |
7289 | ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type) | |
7290 | : TYPE_UNSIGNED (type)); | |
7291 | } | |
3a648ab9 | 7292 | |
b2aef146 | 7293 | if (typefm == NULL_TREE) |
7294 | { | |
743a6f47 | 7295 | error ("no data type for mode %qs", p); |
b2aef146 | 7296 | return NULL_TREE; |
7297 | } | |
3a648ab9 | 7298 | else if (TREE_CODE (type) == ENUMERAL_TYPE) |
7299 | { | |
7300 | /* For enumeral types, copy the precision from the integer | |
7301 | type returned above. If not an INTEGER_TYPE, we can't use | |
7302 | this mode for this type. */ | |
7303 | if (TREE_CODE (typefm) != INTEGER_TYPE) | |
7304 | { | |
743a6f47 | 7305 | error ("cannot use mode %qs for enumeral types", p); |
3a648ab9 | 7306 | return NULL_TREE; |
7307 | } | |
7308 | ||
10080eac | 7309 | if (flags & ATTR_FLAG_TYPE_IN_PLACE) |
7310 | { | |
7311 | TYPE_PRECISION (type) = TYPE_PRECISION (typefm); | |
7312 | typefm = type; | |
7313 | } | |
7314 | else | |
7315 | { | |
7316 | /* We cannot build a type variant, as there's code that assumes | |
7317 | that TYPE_MAIN_VARIANT has the same mode. This includes the | |
7318 | debug generators. Instead, create a subrange type. This | |
7319 | results in all of the enumeral values being emitted only once | |
7320 | in the original, and the subtype gets them by reference. */ | |
7321 | if (TYPE_UNSIGNED (type)) | |
7322 | typefm = make_unsigned_type (TYPE_PRECISION (typefm)); | |
7323 | else | |
7324 | typefm = make_signed_type (TYPE_PRECISION (typefm)); | |
7325 | TREE_TYPE (typefm) = type; | |
7326 | } | |
3a648ab9 | 7327 | } |
4bf450a1 | 7328 | else if (VECTOR_MODE_P (mode) |
7329 | ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm)) | |
7330 | : TREE_CODE (type) != TREE_CODE (typefm)) | |
743a6f47 | 7331 | { |
7332 | error ("mode %qs applied to inappropriate type", p); | |
7333 | return NULL_TREE; | |
7334 | } | |
7335 | ||
b2aef146 | 7336 | *node = typefm; |
f8e93a2e | 7337 | } |
7338 | ||
7339 | return NULL_TREE; | |
7340 | } | |
7341 | ||
7342 | /* Handle a "section" attribute; arguments as in | |
7343 | struct attribute_spec.handler. */ | |
7344 | ||
7345 | static tree | |
9a03a746 | 7346 | handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
7347 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7348 | { |
7349 | tree decl = *node; | |
7350 | ||
218e3e4e | 7351 | if (targetm_common.have_named_sections) |
f8e93a2e | 7352 | { |
065efcb1 | 7353 | user_defined_section_attribute = true; |
7354 | ||
f8e93a2e | 7355 | if ((TREE_CODE (decl) == FUNCTION_DECL |
7356 | || TREE_CODE (decl) == VAR_DECL) | |
7357 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
7358 | { | |
7359 | if (TREE_CODE (decl) == VAR_DECL | |
7360 | && current_function_decl != NULL_TREE | |
84166705 | 7361 | && !TREE_STATIC (decl)) |
f8e93a2e | 7362 | { |
48e1416a | 7363 | error_at (DECL_SOURCE_LOCATION (decl), |
712d2297 | 7364 | "section attribute cannot be specified for " |
7365 | "local variables"); | |
f8e93a2e | 7366 | *no_add_attrs = true; |
7367 | } | |
7368 | ||
7369 | /* The decl may have already been given a section attribute | |
7370 | from a previous declaration. Ensure they match. */ | |
7371 | else if (DECL_SECTION_NAME (decl) != NULL_TREE | |
7372 | && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), | |
7373 | TREE_STRING_POINTER (TREE_VALUE (args))) != 0) | |
7374 | { | |
3cf8b391 | 7375 | error ("section of %q+D conflicts with previous declaration", |
7376 | *node); | |
f8e93a2e | 7377 | *no_add_attrs = true; |
7378 | } | |
38475469 | 7379 | else if (TREE_CODE (decl) == VAR_DECL |
7380 | && !targetm.have_tls && targetm.emutls.tmpl_section | |
7381 | && DECL_THREAD_LOCAL_P (decl)) | |
7382 | { | |
7383 | error ("section of %q+D cannot be overridden", *node); | |
7384 | *no_add_attrs = true; | |
7385 | } | |
f8e93a2e | 7386 | else |
7387 | DECL_SECTION_NAME (decl) = TREE_VALUE (args); | |
7388 | } | |
7389 | else | |
7390 | { | |
3cf8b391 | 7391 | error ("section attribute not allowed for %q+D", *node); |
f8e93a2e | 7392 | *no_add_attrs = true; |
7393 | } | |
7394 | } | |
7395 | else | |
7396 | { | |
712d2297 | 7397 | error_at (DECL_SOURCE_LOCATION (*node), |
7398 | "section attributes are not supported for this target"); | |
f8e93a2e | 7399 | *no_add_attrs = true; |
7400 | } | |
7401 | ||
7402 | return NULL_TREE; | |
7403 | } | |
7404 | ||
83e25171 | 7405 | /* Check whether ALIGN is a valid user-specified alignment. If so, |
7406 | return its base-2 log; if not, output an error and return -1. If | |
7407 | ALLOW_ZERO then 0 is valid and should result in a return of -1 with | |
7408 | no error. */ | |
7409 | int | |
7410 | check_user_alignment (const_tree align, bool allow_zero) | |
7411 | { | |
7412 | int i; | |
7413 | ||
5abaa10a | 7414 | if (TREE_CODE (align) != INTEGER_CST |
7415 | || !INTEGRAL_TYPE_P (TREE_TYPE (align))) | |
83e25171 | 7416 | { |
7417 | error ("requested alignment is not an integer constant"); | |
7418 | return -1; | |
7419 | } | |
7420 | else if (allow_zero && integer_zerop (align)) | |
7421 | return -1; | |
1a087624 | 7422 | else if (tree_int_cst_sgn (align) == -1 |
7423 | || (i = tree_log2 (align)) == -1) | |
83e25171 | 7424 | { |
1a087624 | 7425 | error ("requested alignment is not a positive power of 2"); |
83e25171 | 7426 | return -1; |
7427 | } | |
7428 | else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG) | |
7429 | { | |
7430 | error ("requested alignment is too large"); | |
7431 | return -1; | |
7432 | } | |
7433 | return i; | |
7434 | } | |
7435 | ||
ffcdbf9c | 7436 | /* |
7437 | If in c++-11, check if the c++-11 alignment constraint with respect | |
7438 | to fundamental alignment (in [dcl.align]) are satisfied. If not in | |
7439 | c++-11 mode, does nothing. | |
7440 | ||
7441 | [dcl.align]2/ says: | |
7442 | ||
7443 | [* if the constant expression evaluates to a fundamental alignment, | |
7444 | the alignment requirement of the declared entity shall be the | |
7445 | specified fundamental alignment. | |
7446 | ||
7447 | * if the constant expression evaluates to an extended alignment | |
7448 | and the implementation supports that alignment in the context | |
7449 | of the declaration, the alignment of the declared entity shall | |
7450 | be that alignment | |
7451 | ||
7452 | * if the constant expression evaluates to an extended alignment | |
7453 | and the implementation does not support that alignment in the | |
7454 | context of the declaration, the program is ill-formed]. */ | |
7455 | ||
7456 | static bool | |
7457 | check_cxx_fundamental_alignment_constraints (tree node, | |
7458 | unsigned align_log, | |
7459 | int flags) | |
7460 | { | |
7461 | bool alignment_too_large_p = false; | |
7462 | unsigned requested_alignment = 1U << align_log; | |
7463 | unsigned max_align = 0; | |
7464 | ||
7465 | if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat) | |
7466 | || (node == NULL_TREE || node == error_mark_node)) | |
7467 | return true; | |
7468 | ||
7469 | if (cxx_fundamental_alignment_p (requested_alignment)) | |
7470 | return true; | |
7471 | ||
7472 | if (DECL_P (node)) | |
7473 | { | |
7474 | if (TREE_STATIC (node)) | |
7475 | { | |
7476 | /* For file scope variables and static members, the target | |
7477 | supports alignments that are at most | |
7478 | MAX_OFILE_ALIGNMENT. */ | |
7479 | if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT)) | |
7480 | alignment_too_large_p = true; | |
7481 | } | |
7482 | else | |
7483 | { | |
7484 | #ifdef BIGGEST_FIELD_ALIGNMENT | |
7485 | #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT | |
7486 | #else | |
7487 | #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT | |
7488 | #endif | |
7489 | /* For non-static members, the target supports either | |
7490 | alignments that at most either BIGGEST_FIELD_ALIGNMENT | |
7491 | if it is defined or BIGGEST_ALIGNMENT. */ | |
7492 | max_align = MAX_TARGET_FIELD_ALIGNMENT; | |
7493 | if (TREE_CODE (node) == FIELD_DECL | |
7494 | && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT)) | |
7495 | alignment_too_large_p = true; | |
7496 | #undef MAX_TARGET_FIELD_ALIGNMENT | |
7497 | /* For stack variables, the target supports at most | |
7498 | MAX_STACK_ALIGNMENT. */ | |
7499 | else if (decl_function_context (node) != NULL | |
7500 | && requested_alignment > (max_align = MAX_STACK_ALIGNMENT)) | |
7501 | alignment_too_large_p = true; | |
7502 | } | |
7503 | } | |
7504 | else if (TYPE_P (node)) | |
7505 | { | |
7506 | /* Let's be liberal for types. */ | |
7507 | if (requested_alignment > (max_align = BIGGEST_ALIGNMENT)) | |
7508 | alignment_too_large_p = true; | |
7509 | } | |
7510 | ||
7511 | if (alignment_too_large_p) | |
7512 | pedwarn (input_location, OPT_Wattributes, | |
7513 | "requested alignment %d is larger than %d", | |
7514 | requested_alignment, max_align); | |
7515 | ||
7516 | return !alignment_too_large_p; | |
7517 | } | |
7518 | ||
f8e93a2e | 7519 | /* Handle a "aligned" attribute; arguments as in |
7520 | struct attribute_spec.handler. */ | |
7521 | ||
7522 | static tree | |
9a03a746 | 7523 | handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
09347743 | 7524 | int flags, bool *no_add_attrs) |
f8e93a2e | 7525 | { |
7526 | tree decl = NULL_TREE; | |
7527 | tree *type = NULL; | |
7528 | int is_type = 0; | |
7529 | tree align_expr = (args ? TREE_VALUE (args) | |
02421213 | 7530 | : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT)); |
f8e93a2e | 7531 | int i; |
7532 | ||
7533 | if (DECL_P (*node)) | |
7534 | { | |
7535 | decl = *node; | |
7536 | type = &TREE_TYPE (decl); | |
7537 | is_type = TREE_CODE (*node) == TYPE_DECL; | |
7538 | } | |
7539 | else if (TYPE_P (*node)) | |
7540 | type = node, is_type = 1; | |
7541 | ||
ffcdbf9c | 7542 | if ((i = check_user_alignment (align_expr, false)) == -1 |
7543 | || !check_cxx_fundamental_alignment_constraints (*node, i, flags)) | |
83e25171 | 7544 | *no_add_attrs = true; |
f8e93a2e | 7545 | else if (is_type) |
7546 | { | |
2ec3af9c | 7547 | if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
7548 | /* OK, modify the type in place. */; | |
f8e93a2e | 7549 | /* If we have a TYPE_DECL, then copy the type, so that we |
7550 | don't accidentally modify a builtin type. See pushdecl. */ | |
2ec3af9c | 7551 | else if (decl && TREE_TYPE (decl) != error_mark_node |
7552 | && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) | |
f8e93a2e | 7553 | { |
7554 | tree tt = TREE_TYPE (decl); | |
e086912e | 7555 | *type = build_variant_type_copy (*type); |
f8e93a2e | 7556 | DECL_ORIGINAL_TYPE (decl) = tt; |
7557 | TYPE_NAME (*type) = decl; | |
7558 | TREE_USED (*type) = TREE_USED (decl); | |
7559 | TREE_TYPE (decl) = *type; | |
7560 | } | |
2ec3af9c | 7561 | else |
e086912e | 7562 | *type = build_variant_type_copy (*type); |
f8e93a2e | 7563 | |
7cfdc2f0 | 7564 | TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT; |
f8e93a2e | 7565 | TYPE_USER_ALIGN (*type) = 1; |
7566 | } | |
097b5c8b | 7567 | else if (! VAR_OR_FUNCTION_DECL_P (decl) |
f8e93a2e | 7568 | && TREE_CODE (decl) != FIELD_DECL) |
7569 | { | |
3cf8b391 | 7570 | error ("alignment may not be specified for %q+D", decl); |
f8e93a2e | 7571 | *no_add_attrs = true; |
7572 | } | |
ffcdbf9c | 7573 | else if (DECL_USER_ALIGN (decl) |
7574 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) | |
7575 | /* C++-11 [dcl.align/4]: | |
7576 | ||
7577 | When multiple alignment-specifiers are specified for an | |
7578 | entity, the alignment requirement shall be set to the | |
7579 | strictest specified alignment. | |
7580 | ||
7581 | This formally comes from the c++11 specification but we are | |
7582 | doing it for the GNU attribute syntax as well. */ | |
7583 | *no_add_attrs = true; | |
097b5c8b | 7584 | else if (TREE_CODE (decl) == FUNCTION_DECL |
7cfdc2f0 | 7585 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) |
097b5c8b | 7586 | { |
7587 | if (DECL_USER_ALIGN (decl)) | |
7588 | error ("alignment for %q+D was previously specified as %d " | |
7589 | "and may not be decreased", decl, | |
7590 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
7591 | else | |
7592 | error ("alignment for %q+D must be at least %d", decl, | |
7593 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
7594 | *no_add_attrs = true; | |
7595 | } | |
f8e93a2e | 7596 | else |
7597 | { | |
7cfdc2f0 | 7598 | DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT; |
f8e93a2e | 7599 | DECL_USER_ALIGN (decl) = 1; |
7600 | } | |
7601 | ||
7602 | return NULL_TREE; | |
7603 | } | |
7604 | ||
7605 | /* Handle a "weak" attribute; arguments as in | |
7606 | struct attribute_spec.handler. */ | |
7607 | ||
7608 | static tree | |
f948b309 | 7609 | handle_weak_attribute (tree *node, tree name, |
9a03a746 | 7610 | tree ARG_UNUSED (args), |
7611 | int ARG_UNUSED (flags), | |
7612 | bool * ARG_UNUSED (no_add_attrs)) | |
f8e93a2e | 7613 | { |
f948b309 | 7614 | if (TREE_CODE (*node) == FUNCTION_DECL |
059a60f3 | 7615 | && DECL_DECLARED_INLINE_P (*node)) |
7616 | { | |
0725e25c | 7617 | warning (OPT_Wattributes, "inline function %q+D declared weak", *node); |
059a60f3 | 7618 | *no_add_attrs = true; |
7619 | } | |
85c0a25c | 7620 | else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
7621 | { | |
7622 | error ("indirect function %q+D cannot be declared weak", *node); | |
7623 | *no_add_attrs = true; | |
7624 | return NULL_TREE; | |
7625 | } | |
059a60f3 | 7626 | else if (TREE_CODE (*node) == FUNCTION_DECL |
7627 | || TREE_CODE (*node) == VAR_DECL) | |
f948b309 | 7628 | declare_weak (*node); |
7629 | else | |
7630 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 7631 | |
7632 | return NULL_TREE; | |
7633 | } | |
7634 | ||
85c0a25c | 7635 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
7636 | struct attribute_spec.handler, except that IS_ALIAS tells us | |
7637 | whether this is an alias as opposed to ifunc attribute. */ | |
f8e93a2e | 7638 | |
7639 | static tree | |
85c0a25c | 7640 | handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args, |
7641 | bool *no_add_attrs) | |
f8e93a2e | 7642 | { |
7643 | tree decl = *node; | |
7644 | ||
85c0a25c | 7645 | if (TREE_CODE (decl) != FUNCTION_DECL |
7646 | && (!is_alias || TREE_CODE (decl) != VAR_DECL)) | |
9e830260 | 7647 | { |
7648 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7649 | *no_add_attrs = true; | |
7650 | } | |
7651 | else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) | |
48e1416a | 7652 | || (TREE_CODE (decl) != FUNCTION_DECL |
0a3ecdc1 | 7653 | && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) |
7654 | /* A static variable declaration is always a tentative definition, | |
7655 | but the alias is a non-tentative definition which overrides. */ | |
48e1416a | 7656 | || (TREE_CODE (decl) != FUNCTION_DECL |
0a3ecdc1 | 7657 | && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl))) |
f8e93a2e | 7658 | { |
85c0a25c | 7659 | error ("%q+D defined both normally and as %qE attribute", decl, name); |
f8e93a2e | 7660 | *no_add_attrs = true; |
85c0a25c | 7661 | return NULL_TREE; |
f8e93a2e | 7662 | } |
85c0a25c | 7663 | else if (!is_alias |
7664 | && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl)) | |
7665 | || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))) | |
7666 | { | |
7667 | error ("weak %q+D cannot be defined %qE", decl, name); | |
7668 | *no_add_attrs = true; | |
7669 | return NULL_TREE; | |
7670 | } | |
8c42f0d9 | 7671 | |
7672 | /* Note that the very first time we process a nested declaration, | |
7673 | decl_function_context will not be set. Indeed, *would* never | |
7674 | be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that | |
7675 | we do below. After such frobbery, pushdecl would set the context. | |
7676 | In any case, this is never what we want. */ | |
7677 | else if (decl_function_context (decl) == 0 && current_function_decl == NULL) | |
f8e93a2e | 7678 | { |
7679 | tree id; | |
7680 | ||
7681 | id = TREE_VALUE (args); | |
7682 | if (TREE_CODE (id) != STRING_CST) | |
7683 | { | |
85c0a25c | 7684 | error ("attribute %qE argument not a string", name); |
f8e93a2e | 7685 | *no_add_attrs = true; |
7686 | return NULL_TREE; | |
7687 | } | |
7688 | id = get_identifier (TREE_STRING_POINTER (id)); | |
7689 | /* This counts as a use of the object pointed to. */ | |
7690 | TREE_USED (id) = 1; | |
7691 | ||
7692 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
7693 | DECL_INITIAL (decl) = error_mark_node; | |
7694 | else | |
f2526cce | 7695 | TREE_STATIC (decl) = 1; |
85c0a25c | 7696 | |
7697 | if (!is_alias) | |
7698 | /* ifuncs are also aliases, so set that attribute too. */ | |
7699 | DECL_ATTRIBUTES (decl) | |
7700 | = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl)); | |
f8e93a2e | 7701 | } |
7702 | else | |
7703 | { | |
9b2d6d13 | 7704 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7705 | *no_add_attrs = true; |
7706 | } | |
7707 | ||
7708 | return NULL_TREE; | |
7709 | } | |
7710 | ||
85c0a25c | 7711 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
7712 | struct attribute_spec.handler. */ | |
7713 | ||
7714 | static tree | |
7715 | handle_ifunc_attribute (tree *node, tree name, tree args, | |
7716 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7717 | { | |
7718 | return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs); | |
7719 | } | |
7720 | ||
7721 | /* Handle an "alias" or "ifunc" attribute; arguments as in | |
7722 | struct attribute_spec.handler. */ | |
7723 | ||
7724 | static tree | |
7725 | handle_alias_attribute (tree *node, tree name, tree args, | |
7726 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7727 | { | |
7728 | return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs); | |
7729 | } | |
7730 | ||
f4a30bd7 | 7731 | /* Handle a "weakref" attribute; arguments as in struct |
7732 | attribute_spec.handler. */ | |
7733 | ||
7734 | static tree | |
7735 | handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
7736 | int flags, bool *no_add_attrs) | |
7737 | { | |
7738 | tree attr = NULL_TREE; | |
7739 | ||
83852912 | 7740 | /* We must ignore the attribute when it is associated with |
7741 | local-scoped decls, since attribute alias is ignored and many | |
7742 | such symbols do not even have a DECL_WEAK field. */ | |
64a7bd81 | 7743 | if (decl_function_context (*node) |
7744 | || current_function_decl | |
7745 | || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL)) | |
83852912 | 7746 | { |
7747 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7748 | *no_add_attrs = true; | |
7749 | return NULL_TREE; | |
7750 | } | |
7751 | ||
85c0a25c | 7752 | if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
7753 | { | |
7754 | error ("indirect function %q+D cannot be declared weakref", *node); | |
7755 | *no_add_attrs = true; | |
7756 | return NULL_TREE; | |
7757 | } | |
7758 | ||
f4a30bd7 | 7759 | /* The idea here is that `weakref("name")' mutates into `weakref, |
7760 | alias("name")', and weakref without arguments, in turn, | |
7761 | implicitly adds weak. */ | |
7762 | ||
7763 | if (args) | |
7764 | { | |
7765 | attr = tree_cons (get_identifier ("alias"), args, attr); | |
7766 | attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr); | |
7767 | ||
7768 | *no_add_attrs = true; | |
0a3ecdc1 | 7769 | |
7770 | decl_attributes (node, attr, flags); | |
f4a30bd7 | 7771 | } |
7772 | else | |
7773 | { | |
7774 | if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node))) | |
712d2297 | 7775 | error_at (DECL_SOURCE_LOCATION (*node), |
7776 | "weakref attribute must appear before alias attribute"); | |
f4a30bd7 | 7777 | |
0a3ecdc1 | 7778 | /* Can't call declare_weak because it wants this to be TREE_PUBLIC, |
7779 | and that isn't supported; and because it wants to add it to | |
7780 | the list of weak decls, which isn't helpful. */ | |
7781 | DECL_WEAK (*node) = 1; | |
f4a30bd7 | 7782 | } |
7783 | ||
f4a30bd7 | 7784 | return NULL_TREE; |
7785 | } | |
7786 | ||
f8e93a2e | 7787 | /* Handle an "visibility" attribute; arguments as in |
7788 | struct attribute_spec.handler. */ | |
7789 | ||
7790 | static tree | |
1cae46be | 7791 | handle_visibility_attribute (tree *node, tree name, tree args, |
9a03a746 | 7792 | int ARG_UNUSED (flags), |
4a2849cb | 7793 | bool *ARG_UNUSED (no_add_attrs)) |
f8e93a2e | 7794 | { |
7795 | tree decl = *node; | |
9c40570a | 7796 | tree id = TREE_VALUE (args); |
4a2849cb | 7797 | enum symbol_visibility vis; |
f8e93a2e | 7798 | |
b212f378 | 7799 | if (TYPE_P (*node)) |
7800 | { | |
4a2849cb | 7801 | if (TREE_CODE (*node) == ENUMERAL_TYPE) |
7802 | /* OK */; | |
7803 | else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE) | |
7804 | { | |
7805 | warning (OPT_Wattributes, "%qE attribute ignored on non-class types", | |
7806 | name); | |
7807 | return NULL_TREE; | |
7808 | } | |
7809 | else if (TYPE_FIELDS (*node)) | |
7810 | { | |
7811 | error ("%qE attribute ignored because %qT is already defined", | |
7812 | name, *node); | |
7813 | return NULL_TREE; | |
7814 | } | |
b212f378 | 7815 | } |
84166705 | 7816 | else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl)) |
f8e93a2e | 7817 | { |
9b2d6d13 | 7818 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
9c40570a | 7819 | return NULL_TREE; |
f8e93a2e | 7820 | } |
f8e93a2e | 7821 | |
9c40570a | 7822 | if (TREE_CODE (id) != STRING_CST) |
7823 | { | |
07e3a3d2 | 7824 | error ("visibility argument not a string"); |
9c40570a | 7825 | return NULL_TREE; |
f8e93a2e | 7826 | } |
b27ac6b5 | 7827 | |
b212f378 | 7828 | /* If this is a type, set the visibility on the type decl. */ |
7829 | if (TYPE_P (decl)) | |
7830 | { | |
7831 | decl = TYPE_NAME (decl); | |
84166705 | 7832 | if (!decl) |
a0c938f0 | 7833 | return NULL_TREE; |
e147aab3 | 7834 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
7835 | { | |
9b2d6d13 | 7836 | warning (OPT_Wattributes, "%qE attribute ignored on types", |
e147aab3 | 7837 | name); |
7838 | return NULL_TREE; | |
7839 | } | |
b212f378 | 7840 | } |
f8e93a2e | 7841 | |
9c40570a | 7842 | if (strcmp (TREE_STRING_POINTER (id), "default") == 0) |
4a2849cb | 7843 | vis = VISIBILITY_DEFAULT; |
9c40570a | 7844 | else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0) |
4a2849cb | 7845 | vis = VISIBILITY_INTERNAL; |
9c40570a | 7846 | else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0) |
4a2849cb | 7847 | vis = VISIBILITY_HIDDEN; |
9c40570a | 7848 | else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0) |
4a2849cb | 7849 | vis = VISIBILITY_PROTECTED; |
9c40570a | 7850 | else |
4a2849cb | 7851 | { |
7852 | error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\""); | |
7853 | vis = VISIBILITY_DEFAULT; | |
7854 | } | |
7855 | ||
7856 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
098a01e7 | 7857 | && vis != DECL_VISIBILITY (decl)) |
7858 | { | |
7859 | tree attributes = (TYPE_P (*node) | |
7860 | ? TYPE_ATTRIBUTES (*node) | |
7861 | : DECL_ATTRIBUTES (decl)); | |
7862 | if (lookup_attribute ("visibility", attributes)) | |
7863 | error ("%qD redeclared with different visibility", decl); | |
7864 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
7865 | && lookup_attribute ("dllimport", attributes)) | |
7866 | error ("%qD was declared %qs which implies default visibility", | |
7867 | decl, "dllimport"); | |
7868 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
7869 | && lookup_attribute ("dllexport", attributes)) | |
7870 | error ("%qD was declared %qs which implies default visibility", | |
7871 | decl, "dllexport"); | |
7872 | } | |
4a2849cb | 7873 | |
7874 | DECL_VISIBILITY (decl) = vis; | |
b212f378 | 7875 | DECL_VISIBILITY_SPECIFIED (decl) = 1; |
7876 | ||
4a2849cb | 7877 | /* Go ahead and attach the attribute to the node as well. This is needed |
7878 | so we can determine whether we have VISIBILITY_DEFAULT because the | |
7879 | visibility was not specified, or because it was explicitly overridden | |
7880 | from the containing scope. */ | |
9c40570a | 7881 | |
f8e93a2e | 7882 | return NULL_TREE; |
7883 | } | |
7884 | ||
3aa0c315 | 7885 | /* Determine the ELF symbol visibility for DECL, which is either a |
7886 | variable or a function. It is an error to use this function if a | |
7887 | definition of DECL is not available in this translation unit. | |
7888 | Returns true if the final visibility has been determined by this | |
7889 | function; false if the caller is free to make additional | |
7890 | modifications. */ | |
7891 | ||
7892 | bool | |
7893 | c_determine_visibility (tree decl) | |
7894 | { | |
1d8fc210 | 7895 | gcc_assert (TREE_CODE (decl) == VAR_DECL |
7896 | || TREE_CODE (decl) == FUNCTION_DECL); | |
3aa0c315 | 7897 | |
7898 | /* If the user explicitly specified the visibility with an | |
7899 | attribute, honor that. DECL_VISIBILITY will have been set during | |
920f5a70 | 7900 | the processing of the attribute. We check for an explicit |
7901 | attribute, rather than just checking DECL_VISIBILITY_SPECIFIED, | |
7902 | to distinguish the use of an attribute from the use of a "#pragma | |
7903 | GCC visibility push(...)"; in the latter case we still want other | |
7904 | considerations to be able to overrule the #pragma. */ | |
7905 | if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)) | |
7906 | || (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
7907 | && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) | |
7908 | || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))))) | |
3aa0c315 | 7909 | return true; |
7910 | ||
4a2849cb | 7911 | /* Set default visibility to whatever the user supplied with |
7912 | visibility_specified depending on #pragma GCC visibility. */ | |
7913 | if (!DECL_VISIBILITY_SPECIFIED (decl)) | |
7914 | { | |
2d9d8740 | 7915 | if (visibility_options.inpragma |
7916 | || DECL_VISIBILITY (decl) != default_visibility) | |
7917 | { | |
7918 | DECL_VISIBILITY (decl) = default_visibility; | |
7919 | DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; | |
7920 | /* If visibility changed and DECL already has DECL_RTL, ensure | |
7921 | symbol flags are updated. */ | |
7922 | if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)) | |
7923 | || TREE_CODE (decl) == FUNCTION_DECL) | |
7924 | && DECL_RTL_SET_P (decl)) | |
7925 | make_decl_rtl (decl); | |
7926 | } | |
4a2849cb | 7927 | } |
3aa0c315 | 7928 | return false; |
7929 | } | |
7930 | ||
24dfead4 | 7931 | /* Handle an "tls_model" attribute; arguments as in |
7932 | struct attribute_spec.handler. */ | |
7933 | ||
7934 | static tree | |
1cae46be | 7935 | handle_tls_model_attribute (tree *node, tree name, tree args, |
9a03a746 | 7936 | int ARG_UNUSED (flags), bool *no_add_attrs) |
24dfead4 | 7937 | { |
1b53eb20 | 7938 | tree id; |
24dfead4 | 7939 | tree decl = *node; |
1b53eb20 | 7940 | enum tls_model kind; |
24dfead4 | 7941 | |
1b53eb20 | 7942 | *no_add_attrs = true; |
7943 | ||
c34f8a78 | 7944 | if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl)) |
24dfead4 | 7945 | { |
9b2d6d13 | 7946 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
1b53eb20 | 7947 | return NULL_TREE; |
24dfead4 | 7948 | } |
24dfead4 | 7949 | |
1b53eb20 | 7950 | kind = DECL_TLS_MODEL (decl); |
7951 | id = TREE_VALUE (args); | |
7952 | if (TREE_CODE (id) != STRING_CST) | |
7953 | { | |
7954 | error ("tls_model argument not a string"); | |
7955 | return NULL_TREE; | |
24dfead4 | 7956 | } |
7957 | ||
1b53eb20 | 7958 | if (!strcmp (TREE_STRING_POINTER (id), "local-exec")) |
7959 | kind = TLS_MODEL_LOCAL_EXEC; | |
7960 | else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec")) | |
7961 | kind = TLS_MODEL_INITIAL_EXEC; | |
7962 | else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic")) | |
7963 | kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC; | |
7964 | else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic")) | |
7965 | kind = TLS_MODEL_GLOBAL_DYNAMIC; | |
7966 | else | |
7967 | error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\""); | |
7968 | ||
7969 | DECL_TLS_MODEL (decl) = kind; | |
24dfead4 | 7970 | return NULL_TREE; |
7971 | } | |
7972 | ||
f8e93a2e | 7973 | /* Handle a "no_instrument_function" attribute; arguments as in |
7974 | struct attribute_spec.handler. */ | |
7975 | ||
7976 | static tree | |
1cae46be | 7977 | handle_no_instrument_function_attribute (tree *node, tree name, |
9a03a746 | 7978 | tree ARG_UNUSED (args), |
7979 | int ARG_UNUSED (flags), | |
09347743 | 7980 | bool *no_add_attrs) |
f8e93a2e | 7981 | { |
7982 | tree decl = *node; | |
7983 | ||
7984 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
7985 | { | |
712d2297 | 7986 | error_at (DECL_SOURCE_LOCATION (decl), |
7987 | "%qE attribute applies only to functions", name); | |
f8e93a2e | 7988 | *no_add_attrs = true; |
7989 | } | |
f8e93a2e | 7990 | else |
7991 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
7992 | ||
7993 | return NULL_TREE; | |
7994 | } | |
7995 | ||
7996 | /* Handle a "malloc" attribute; arguments as in | |
7997 | struct attribute_spec.handler. */ | |
7998 | ||
7999 | static tree | |
9a03a746 | 8000 | handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8001 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 8002 | { |
a5147fca | 8003 | if (TREE_CODE (*node) == FUNCTION_DECL |
8004 | && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) | |
f8e93a2e | 8005 | DECL_IS_MALLOC (*node) = 1; |
f8e93a2e | 8006 | else |
8007 | { | |
9b2d6d13 | 8008 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8009 | *no_add_attrs = true; |
8010 | } | |
8011 | ||
8012 | return NULL_TREE; | |
8013 | } | |
8014 | ||
4a29c97c | 8015 | /* Handle a "alloc_size" attribute; arguments as in |
8016 | struct attribute_spec.handler. */ | |
8017 | ||
8018 | static tree | |
8019 | handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
8020 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8021 | { | |
2802826e | 8022 | unsigned arg_count = type_num_arguments (*node); |
4a29c97c | 8023 | for (; args; args = TREE_CHAIN (args)) |
8024 | { | |
8025 | tree position = TREE_VALUE (args); | |
8026 | ||
8027 | if (TREE_CODE (position) != INTEGER_CST | |
48e1416a | 8028 | || TREE_INT_CST_HIGH (position) |
4a29c97c | 8029 | || TREE_INT_CST_LOW (position) < 1 |
8030 | || TREE_INT_CST_LOW (position) > arg_count ) | |
8031 | { | |
48e1416a | 8032 | warning (OPT_Wattributes, |
4a29c97c | 8033 | "alloc_size parameter outside range"); |
8034 | *no_add_attrs = true; | |
8035 | return NULL_TREE; | |
8036 | } | |
8037 | } | |
8038 | return NULL_TREE; | |
8039 | } | |
8040 | ||
8ce86007 | 8041 | /* Handle a "fn spec" attribute; arguments as in |
8042 | struct attribute_spec.handler. */ | |
8043 | ||
8044 | static tree | |
8045 | handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name), | |
8046 | tree args, int ARG_UNUSED (flags), | |
8047 | bool *no_add_attrs ATTRIBUTE_UNUSED) | |
8048 | { | |
8049 | gcc_assert (args | |
8050 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST | |
8051 | && !TREE_CHAIN (args)); | |
8052 | return NULL_TREE; | |
8053 | } | |
8054 | ||
a96c3cc1 | 8055 | /* Handle a "warn_unused" attribute; arguments as in |
8056 | struct attribute_spec.handler. */ | |
8057 | ||
8058 | static tree | |
8059 | handle_warn_unused_attribute (tree *node, tree name, | |
8060 | tree args ATTRIBUTE_UNUSED, | |
8061 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
8062 | { | |
8063 | if (TYPE_P (*node)) | |
8064 | /* Do nothing else, just set the attribute. We'll get at | |
8065 | it later with lookup_attribute. */ | |
8066 | ; | |
8067 | else | |
8068 | { | |
8069 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8070 | *no_add_attrs = true; | |
8071 | } | |
8072 | ||
8073 | return NULL_TREE; | |
8074 | } | |
8075 | ||
bc7bff74 | 8076 | /* Handle an "omp declare simd" attribute; arguments as in |
8077 | struct attribute_spec.handler. */ | |
8078 | ||
8079 | static tree | |
8080 | handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *) | |
8081 | { | |
8082 | return NULL_TREE; | |
8083 | } | |
8084 | ||
8085 | /* Handle an "omp declare target" attribute; arguments as in | |
8086 | struct attribute_spec.handler. */ | |
8087 | ||
8088 | static tree | |
8089 | handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *) | |
8090 | { | |
8091 | return NULL_TREE; | |
8092 | } | |
8093 | ||
26d1c5ff | 8094 | /* Handle a "returns_twice" attribute; arguments as in |
8095 | struct attribute_spec.handler. */ | |
8096 | ||
8097 | static tree | |
8098 | handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
8099 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8100 | { | |
8101 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
8102 | DECL_IS_RETURNS_TWICE (*node) = 1; | |
8103 | else | |
8104 | { | |
9b2d6d13 | 8105 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
26d1c5ff | 8106 | *no_add_attrs = true; |
8107 | } | |
8108 | ||
8109 | return NULL_TREE; | |
8110 | } | |
8111 | ||
f8e93a2e | 8112 | /* Handle a "no_limit_stack" attribute; arguments as in |
8113 | struct attribute_spec.handler. */ | |
8114 | ||
8115 | static tree | |
1cae46be | 8116 | handle_no_limit_stack_attribute (tree *node, tree name, |
9a03a746 | 8117 | tree ARG_UNUSED (args), |
8118 | int ARG_UNUSED (flags), | |
09347743 | 8119 | bool *no_add_attrs) |
f8e93a2e | 8120 | { |
8121 | tree decl = *node; | |
8122 | ||
8123 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8124 | { | |
712d2297 | 8125 | error_at (DECL_SOURCE_LOCATION (decl), |
8126 | "%qE attribute applies only to functions", name); | |
f8e93a2e | 8127 | *no_add_attrs = true; |
8128 | } | |
8129 | else if (DECL_INITIAL (decl)) | |
8130 | { | |
712d2297 | 8131 | error_at (DECL_SOURCE_LOCATION (decl), |
8132 | "can%'t set %qE attribute after definition", name); | |
f8e93a2e | 8133 | *no_add_attrs = true; |
8134 | } | |
8135 | else | |
8136 | DECL_NO_LIMIT_STACK (decl) = 1; | |
8137 | ||
8138 | return NULL_TREE; | |
8139 | } | |
8140 | ||
8141 | /* Handle a "pure" attribute; arguments as in | |
8142 | struct attribute_spec.handler. */ | |
8143 | ||
8144 | static tree | |
9a03a746 | 8145 | handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8146 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 8147 | { |
8148 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
9c2a0c05 | 8149 | DECL_PURE_P (*node) = 1; |
f8e93a2e | 8150 | /* ??? TODO: Support types. */ |
8151 | else | |
8152 | { | |
9b2d6d13 | 8153 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8154 | *no_add_attrs = true; |
8155 | } | |
8156 | ||
8157 | return NULL_TREE; | |
8158 | } | |
8159 | ||
4c0315d0 | 8160 | /* Digest an attribute list destined for a transactional memory statement. |
8161 | ALLOWED is the set of attributes that are allowed for this statement; | |
8162 | return the attribute we parsed. Multiple attributes are never allowed. */ | |
8163 | ||
8164 | int | |
8165 | parse_tm_stmt_attr (tree attrs, int allowed) | |
8166 | { | |
8167 | tree a_seen = NULL; | |
8168 | int m_seen = 0; | |
8169 | ||
8170 | for ( ; attrs ; attrs = TREE_CHAIN (attrs)) | |
8171 | { | |
8172 | tree a = TREE_PURPOSE (attrs); | |
8173 | int m = 0; | |
8174 | ||
8175 | if (is_attribute_p ("outer", a)) | |
8176 | m = TM_STMT_ATTR_OUTER; | |
8177 | ||
8178 | if ((m & allowed) == 0) | |
8179 | { | |
8180 | warning (OPT_Wattributes, "%qE attribute directive ignored", a); | |
8181 | continue; | |
8182 | } | |
8183 | ||
8184 | if (m_seen == 0) | |
8185 | { | |
8186 | a_seen = a; | |
8187 | m_seen = m; | |
8188 | } | |
8189 | else if (m_seen == m) | |
8190 | warning (OPT_Wattributes, "%qE attribute duplicated", a); | |
8191 | else | |
8192 | warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen); | |
8193 | } | |
8194 | ||
8195 | return m_seen; | |
8196 | } | |
8197 | ||
8198 | /* Transform a TM attribute name into a maskable integer and back. | |
8199 | Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding | |
8200 | to how the lack of an attribute is treated. */ | |
8201 | ||
8202 | int | |
8203 | tm_attr_to_mask (tree attr) | |
8204 | { | |
8205 | if (attr == NULL) | |
8206 | return 0; | |
8207 | if (is_attribute_p ("transaction_safe", attr)) | |
8208 | return TM_ATTR_SAFE; | |
8209 | if (is_attribute_p ("transaction_callable", attr)) | |
8210 | return TM_ATTR_CALLABLE; | |
8211 | if (is_attribute_p ("transaction_pure", attr)) | |
8212 | return TM_ATTR_PURE; | |
8213 | if (is_attribute_p ("transaction_unsafe", attr)) | |
8214 | return TM_ATTR_IRREVOCABLE; | |
8215 | if (is_attribute_p ("transaction_may_cancel_outer", attr)) | |
8216 | return TM_ATTR_MAY_CANCEL_OUTER; | |
8217 | return 0; | |
8218 | } | |
8219 | ||
8220 | tree | |
8221 | tm_mask_to_attr (int mask) | |
8222 | { | |
8223 | const char *str; | |
8224 | switch (mask) | |
8225 | { | |
8226 | case TM_ATTR_SAFE: | |
8227 | str = "transaction_safe"; | |
8228 | break; | |
8229 | case TM_ATTR_CALLABLE: | |
8230 | str = "transaction_callable"; | |
8231 | break; | |
8232 | case TM_ATTR_PURE: | |
8233 | str = "transaction_pure"; | |
8234 | break; | |
8235 | case TM_ATTR_IRREVOCABLE: | |
8236 | str = "transaction_unsafe"; | |
8237 | break; | |
8238 | case TM_ATTR_MAY_CANCEL_OUTER: | |
8239 | str = "transaction_may_cancel_outer"; | |
8240 | break; | |
8241 | default: | |
8242 | gcc_unreachable (); | |
8243 | } | |
8244 | return get_identifier (str); | |
8245 | } | |
8246 | ||
8247 | /* Return the first TM attribute seen in LIST. */ | |
8248 | ||
8249 | tree | |
8250 | find_tm_attribute (tree list) | |
8251 | { | |
8252 | for (; list ; list = TREE_CHAIN (list)) | |
8253 | { | |
8254 | tree name = TREE_PURPOSE (list); | |
8255 | if (tm_attr_to_mask (name) != 0) | |
8256 | return name; | |
8257 | } | |
8258 | return NULL_TREE; | |
8259 | } | |
8260 | ||
8261 | /* Handle the TM attributes; arguments as in struct attribute_spec.handler. | |
8262 | Here we accept only function types, and verify that none of the other | |
8263 | function TM attributes are also applied. */ | |
8264 | /* ??? We need to accept class types for C++, but not C. This greatly | |
8265 | complicates this function, since we can no longer rely on the extra | |
8266 | processing given by function_type_required. */ | |
8267 | ||
8268 | static tree | |
8269 | handle_tm_attribute (tree *node, tree name, tree args, | |
8270 | int flags, bool *no_add_attrs) | |
8271 | { | |
8272 | /* Only one path adds the attribute; others don't. */ | |
8273 | *no_add_attrs = true; | |
8274 | ||
8275 | switch (TREE_CODE (*node)) | |
8276 | { | |
8277 | case RECORD_TYPE: | |
8278 | case UNION_TYPE: | |
8279 | /* Only tm_callable and tm_safe apply to classes. */ | |
8280 | if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE)) | |
8281 | goto ignored; | |
8282 | /* FALLTHRU */ | |
8283 | ||
8284 | case FUNCTION_TYPE: | |
8285 | case METHOD_TYPE: | |
8286 | { | |
8287 | tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node)); | |
8288 | if (old_name == name) | |
8289 | ; | |
8290 | else if (old_name != NULL_TREE) | |
8291 | error ("type was previously declared %qE", old_name); | |
8292 | else | |
8293 | *no_add_attrs = false; | |
8294 | } | |
8295 | break; | |
8296 | ||
8297 | case POINTER_TYPE: | |
8298 | { | |
8299 | enum tree_code subcode = TREE_CODE (TREE_TYPE (*node)); | |
8300 | if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE) | |
8301 | { | |
8302 | tree fn_tmp = TREE_TYPE (*node); | |
8303 | decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0); | |
8304 | *node = build_pointer_type (fn_tmp); | |
8305 | break; | |
8306 | } | |
8307 | } | |
8308 | /* FALLTHRU */ | |
8309 | ||
8310 | default: | |
8311 | /* If a function is next, pass it on to be tried next. */ | |
8312 | if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) | |
8313 | return tree_cons (name, args, NULL); | |
8314 | ||
8315 | ignored: | |
8316 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8317 | break; | |
8318 | } | |
8319 | ||
8320 | return NULL_TREE; | |
8321 | } | |
8322 | ||
8323 | /* Handle the TM_WRAP attribute; arguments as in | |
8324 | struct attribute_spec.handler. */ | |
8325 | ||
8326 | static tree | |
8327 | handle_tm_wrap_attribute (tree *node, tree name, tree args, | |
8328 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8329 | { | |
8330 | tree decl = *node; | |
8331 | ||
8332 | /* We don't need the attribute even on success, since we | |
8333 | record the entry in an external table. */ | |
8334 | *no_add_attrs = true; | |
8335 | ||
8336 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8337 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8338 | else | |
8339 | { | |
8340 | tree wrap_decl = TREE_VALUE (args); | |
8341 | if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE | |
8342 | && TREE_CODE (wrap_decl) != VAR_DECL | |
8343 | && TREE_CODE (wrap_decl) != FUNCTION_DECL) | |
8344 | error ("%qE argument not an identifier", name); | |
8345 | else | |
8346 | { | |
8347 | if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE) | |
8348 | wrap_decl = lookup_name (wrap_decl); | |
8349 | if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL) | |
8350 | { | |
8351 | if (lang_hooks.types_compatible_p (TREE_TYPE (decl), | |
8352 | TREE_TYPE (wrap_decl))) | |
8353 | record_tm_replacement (wrap_decl, decl); | |
8354 | else | |
8355 | error ("%qD is not compatible with %qD", wrap_decl, decl); | |
8356 | } | |
8357 | else | |
8358 | error ("transaction_wrap argument is not a function"); | |
8359 | } | |
8360 | } | |
8361 | ||
8362 | return NULL_TREE; | |
8363 | } | |
8364 | ||
8365 | /* Ignore the given attribute. Used when this attribute may be usefully | |
8366 | overridden by the target, but is not used generically. */ | |
8367 | ||
8368 | static tree | |
8369 | ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name), | |
8370 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8371 | bool *no_add_attrs) | |
8372 | { | |
8373 | *no_add_attrs = true; | |
8374 | return NULL_TREE; | |
8375 | } | |
8376 | ||
fc09b200 | 8377 | /* Handle a "no vops" attribute; arguments as in |
8378 | struct attribute_spec.handler. */ | |
8379 | ||
8380 | static tree | |
8381 | handle_novops_attribute (tree *node, tree ARG_UNUSED (name), | |
8382 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8383 | bool *ARG_UNUSED (no_add_attrs)) | |
8384 | { | |
8385 | gcc_assert (TREE_CODE (*node) == FUNCTION_DECL); | |
8386 | DECL_IS_NOVOPS (*node) = 1; | |
8387 | return NULL_TREE; | |
8388 | } | |
8389 | ||
f8e93a2e | 8390 | /* Handle a "deprecated" attribute; arguments as in |
8391 | struct attribute_spec.handler. */ | |
1cae46be | 8392 | |
f8e93a2e | 8393 | static tree |
1cae46be | 8394 | handle_deprecated_attribute (tree *node, tree name, |
45c4e798 | 8395 | tree args, int flags, |
09347743 | 8396 | bool *no_add_attrs) |
f8e93a2e | 8397 | { |
8398 | tree type = NULL_TREE; | |
8399 | int warn = 0; | |
782858b8 | 8400 | tree what = NULL_TREE; |
1cae46be | 8401 | |
45c4e798 | 8402 | if (!args) |
8403 | *no_add_attrs = true; | |
8404 | else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST) | |
8405 | { | |
8406 | error ("deprecated message is not a string"); | |
8407 | *no_add_attrs = true; | |
8408 | } | |
8409 | ||
f8e93a2e | 8410 | if (DECL_P (*node)) |
8411 | { | |
8412 | tree decl = *node; | |
8413 | type = TREE_TYPE (decl); | |
1cae46be | 8414 | |
f8e93a2e | 8415 | if (TREE_CODE (decl) == TYPE_DECL |
8416 | || TREE_CODE (decl) == PARM_DECL | |
8417 | || TREE_CODE (decl) == VAR_DECL | |
8418 | || TREE_CODE (decl) == FUNCTION_DECL | |
40c8d1dd | 8419 | || TREE_CODE (decl) == FIELD_DECL |
8420 | || objc_method_decl (TREE_CODE (decl))) | |
f8e93a2e | 8421 | TREE_DEPRECATED (decl) = 1; |
8422 | else | |
8423 | warn = 1; | |
8424 | } | |
8425 | else if (TYPE_P (*node)) | |
8426 | { | |
8427 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 8428 | *node = build_variant_type_copy (*node); |
f8e93a2e | 8429 | TREE_DEPRECATED (*node) = 1; |
8430 | type = *node; | |
8431 | } | |
8432 | else | |
8433 | warn = 1; | |
1cae46be | 8434 | |
f8e93a2e | 8435 | if (warn) |
8436 | { | |
8437 | *no_add_attrs = true; | |
8438 | if (type && TYPE_NAME (type)) | |
8439 | { | |
8440 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
782858b8 | 8441 | what = TYPE_NAME (*node); |
f8e93a2e | 8442 | else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL |
8443 | && DECL_NAME (TYPE_NAME (type))) | |
782858b8 | 8444 | what = DECL_NAME (TYPE_NAME (type)); |
f8e93a2e | 8445 | } |
8446 | if (what) | |
9b2d6d13 | 8447 | warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what); |
f8e93a2e | 8448 | else |
9b2d6d13 | 8449 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8450 | } |
8451 | ||
8452 | return NULL_TREE; | |
8453 | } | |
8454 | ||
f8e93a2e | 8455 | /* Handle a "vector_size" attribute; arguments as in |
8456 | struct attribute_spec.handler. */ | |
8457 | ||
8458 | static tree | |
1cae46be | 8459 | handle_vector_size_attribute (tree *node, tree name, tree args, |
9a03a746 | 8460 | int ARG_UNUSED (flags), |
09347743 | 8461 | bool *no_add_attrs) |
f8e93a2e | 8462 | { |
8463 | unsigned HOST_WIDE_INT vecsize, nunits; | |
83e2a11b | 8464 | enum machine_mode orig_mode; |
4917c376 | 8465 | tree type = *node, new_type, size; |
f8e93a2e | 8466 | |
8467 | *no_add_attrs = true; | |
8468 | ||
4917c376 | 8469 | size = TREE_VALUE (args); |
4917c376 | 8470 | |
cd4547bf | 8471 | if (!tree_fits_uhwi_p (size)) |
f8e93a2e | 8472 | { |
9b2d6d13 | 8473 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8474 | return NULL_TREE; |
8475 | } | |
8476 | ||
8477 | /* Get the vector size (in bytes). */ | |
6a0712d4 | 8478 | vecsize = tree_to_uhwi (size); |
f8e93a2e | 8479 | |
8480 | /* We need to provide for vector pointers, vector arrays, and | |
8481 | functions returning vectors. For example: | |
8482 | ||
8483 | __attribute__((vector_size(16))) short *foo; | |
8484 | ||
8485 | In this case, the mode is SI, but the type being modified is | |
8486 | HI, so we need to look further. */ | |
8487 | ||
8488 | while (POINTER_TYPE_P (type) | |
8489 | || TREE_CODE (type) == FUNCTION_TYPE | |
5bfb0742 | 8490 | || TREE_CODE (type) == METHOD_TYPE |
2cb0e5d9 | 8491 | || TREE_CODE (type) == ARRAY_TYPE |
8492 | || TREE_CODE (type) == OFFSET_TYPE) | |
f8e93a2e | 8493 | type = TREE_TYPE (type); |
8494 | ||
8495 | /* Get the mode of the type being modified. */ | |
8496 | orig_mode = TYPE_MODE (type); | |
8497 | ||
2cb0e5d9 | 8498 | if ((!INTEGRAL_TYPE_P (type) |
8499 | && !SCALAR_FLOAT_TYPE_P (type) | |
8500 | && !FIXED_POINT_TYPE_P (type)) | |
cee7491d | 8501 | || (!SCALAR_FLOAT_MODE_P (orig_mode) |
9421ebb9 | 8502 | && GET_MODE_CLASS (orig_mode) != MODE_INT |
8503 | && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode)) | |
cd4547bf | 8504 | || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)) |
7ec31215 | 8505 | || TREE_CODE (type) == BOOLEAN_TYPE) |
f8e93a2e | 8506 | { |
782858b8 | 8507 | error ("invalid vector type for attribute %qE", name); |
f8e93a2e | 8508 | return NULL_TREE; |
8509 | } | |
8510 | ||
6a0712d4 | 8511 | if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type))) |
39cc3e6d | 8512 | { |
8513 | error ("vector size not an integral multiple of component size"); | |
8514 | return NULL; | |
8515 | } | |
8516 | ||
8517 | if (vecsize == 0) | |
8518 | { | |
8519 | error ("zero vector size"); | |
8520 | return NULL; | |
8521 | } | |
8522 | ||
f8e93a2e | 8523 | /* Calculate how many units fit in the vector. */ |
6a0712d4 | 8524 | nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
83e2a11b | 8525 | if (nunits & (nunits - 1)) |
f8e93a2e | 8526 | { |
83e2a11b | 8527 | error ("number of components of the vector not a power of two"); |
f8e93a2e | 8528 | return NULL_TREE; |
8529 | } | |
8530 | ||
83e2a11b | 8531 | new_type = build_vector_type (type, nunits); |
f8e93a2e | 8532 | |
8533 | /* Build back pointers if needed. */ | |
d991e6e8 | 8534 | *node = lang_hooks.types.reconstruct_complex_type (*node, new_type); |
f8e93a2e | 8535 | |
8536 | return NULL_TREE; | |
8537 | } | |
8538 | ||
dbf6c367 | 8539 | /* Handle the "nonnull" attribute. */ |
8540 | static tree | |
9a03a746 | 8541 | handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), |
8542 | tree args, int ARG_UNUSED (flags), | |
09347743 | 8543 | bool *no_add_attrs) |
dbf6c367 | 8544 | { |
8545 | tree type = *node; | |
8546 | unsigned HOST_WIDE_INT attr_arg_num; | |
8547 | ||
8548 | /* If no arguments are specified, all pointer arguments should be | |
d716ce75 | 8549 | non-null. Verify a full prototype is given so that the arguments |
dbf6c367 | 8550 | will have the correct types when we actually check them later. */ |
84166705 | 8551 | if (!args) |
dbf6c367 | 8552 | { |
a36cf284 | 8553 | if (!prototype_p (type)) |
dbf6c367 | 8554 | { |
8555 | error ("nonnull attribute without arguments on a non-prototype"); | |
4ee9c684 | 8556 | *no_add_attrs = true; |
dbf6c367 | 8557 | } |
8558 | return NULL_TREE; | |
8559 | } | |
8560 | ||
8561 | /* Argument list specified. Verify that each argument number references | |
8562 | a pointer argument. */ | |
8563 | for (attr_arg_num = 1; args; args = TREE_CHAIN (args)) | |
8564 | { | |
4ee9c684 | 8565 | unsigned HOST_WIDE_INT arg_num = 0, ck_num; |
dbf6c367 | 8566 | |
84166705 | 8567 | if (!get_nonnull_operand (TREE_VALUE (args), &arg_num)) |
dbf6c367 | 8568 | { |
07e3a3d2 | 8569 | error ("nonnull argument has invalid operand number (argument %lu)", |
dbf6c367 | 8570 | (unsigned long) attr_arg_num); |
8571 | *no_add_attrs = true; | |
8572 | return NULL_TREE; | |
8573 | } | |
8574 | ||
d0af78c5 | 8575 | if (prototype_p (type)) |
dbf6c367 | 8576 | { |
d0af78c5 | 8577 | function_args_iterator iter; |
8578 | tree argument; | |
8579 | ||
8580 | function_args_iter_init (&iter, type); | |
8581 | for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter)) | |
dbf6c367 | 8582 | { |
d0af78c5 | 8583 | argument = function_args_iter_cond (&iter); |
8584 | if (argument == NULL_TREE || ck_num == arg_num) | |
dbf6c367 | 8585 | break; |
dbf6c367 | 8586 | } |
8587 | ||
84166705 | 8588 | if (!argument |
d0af78c5 | 8589 | || TREE_CODE (argument) == VOID_TYPE) |
dbf6c367 | 8590 | { |
07e3a3d2 | 8591 | error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)", |
dbf6c367 | 8592 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
8593 | *no_add_attrs = true; | |
8594 | return NULL_TREE; | |
8595 | } | |
8596 | ||
d0af78c5 | 8597 | if (TREE_CODE (argument) != POINTER_TYPE) |
dbf6c367 | 8598 | { |
07e3a3d2 | 8599 | error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)", |
dbf6c367 | 8600 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
8601 | *no_add_attrs = true; | |
8602 | return NULL_TREE; | |
8603 | } | |
8604 | } | |
8605 | } | |
8606 | ||
8607 | return NULL_TREE; | |
8608 | } | |
8609 | ||
8610 | /* Check the argument list of a function call for null in argument slots | |
d01f58f9 | 8611 | that are marked as requiring a non-null pointer argument. The NARGS |
8612 | arguments are passed in the array ARGARRAY. | |
8613 | */ | |
dbf6c367 | 8614 | |
8615 | static void | |
d01f58f9 | 8616 | check_function_nonnull (tree attrs, int nargs, tree *argarray) |
dbf6c367 | 8617 | { |
9ca77b08 | 8618 | tree a; |
d01f58f9 | 8619 | int i; |
dbf6c367 | 8620 | |
9ca77b08 | 8621 | attrs = lookup_attribute ("nonnull", attrs); |
8622 | if (attrs == NULL_TREE) | |
8623 | return; | |
8624 | ||
8625 | a = attrs; | |
8626 | /* See if any of the nonnull attributes has no arguments. If so, | |
8627 | then every pointer argument is checked (in which case the check | |
8628 | for pointer type is done in check_nonnull_arg). */ | |
8629 | if (TREE_VALUE (a) != NULL_TREE) | |
8630 | do | |
8631 | a = lookup_attribute ("nonnull", TREE_CHAIN (a)); | |
8632 | while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE); | |
8633 | ||
8634 | if (a != NULL_TREE) | |
8635 | for (i = 0; i < nargs; i++) | |
8636 | check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i], | |
8637 | i + 1); | |
8638 | else | |
dbf6c367 | 8639 | { |
9ca77b08 | 8640 | /* Walk the argument list. If we encounter an argument number we |
8641 | should check for non-null, do it. */ | |
8642 | for (i = 0; i < nargs; i++) | |
dbf6c367 | 8643 | { |
9ca77b08 | 8644 | for (a = attrs; ; a = TREE_CHAIN (a)) |
4ee9c684 | 8645 | { |
9ca77b08 | 8646 | a = lookup_attribute ("nonnull", a); |
8647 | if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1)) | |
8648 | break; | |
4ee9c684 | 8649 | } |
9ca77b08 | 8650 | |
8651 | if (a != NULL_TREE) | |
8652 | check_function_arguments_recurse (check_nonnull_arg, NULL, | |
8653 | argarray[i], i + 1); | |
dbf6c367 | 8654 | } |
8655 | } | |
8656 | } | |
8657 | ||
50ca527f | 8658 | /* Check that the Nth argument of a function call (counting backwards |
d01f58f9 | 8659 | from the end) is a (pointer)0. The NARGS arguments are passed in the |
8660 | array ARGARRAY. */ | |
bf6c8de0 | 8661 | |
8662 | static void | |
774e9d58 | 8663 | check_function_sentinel (const_tree fntype, int nargs, tree *argarray) |
bf6c8de0 | 8664 | { |
774e9d58 | 8665 | tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype)); |
bf6c8de0 | 8666 | |
8667 | if (attr) | |
8668 | { | |
d01f58f9 | 8669 | int len = 0; |
8670 | int pos = 0; | |
8671 | tree sentinel; | |
774e9d58 | 8672 | function_args_iterator iter; |
8673 | tree t; | |
a0c938f0 | 8674 | |
d01f58f9 | 8675 | /* Skip over the named arguments. */ |
774e9d58 | 8676 | FOREACH_FUNCTION_ARGS (fntype, t, iter) |
a0c938f0 | 8677 | { |
774e9d58 | 8678 | if (len == nargs) |
8679 | break; | |
d01f58f9 | 8680 | len++; |
8681 | } | |
50ca527f | 8682 | |
d01f58f9 | 8683 | if (TREE_VALUE (attr)) |
8684 | { | |
8685 | tree p = TREE_VALUE (TREE_VALUE (attr)); | |
8686 | pos = TREE_INT_CST_LOW (p); | |
8687 | } | |
50ca527f | 8688 | |
d01f58f9 | 8689 | /* The sentinel must be one of the varargs, i.e. |
8690 | in position >= the number of fixed arguments. */ | |
8691 | if ((nargs - 1 - pos) < len) | |
8692 | { | |
77a357e3 | 8693 | warning (OPT_Wformat_, |
d01f58f9 | 8694 | "not enough variable arguments to fit a sentinel"); |
8695 | return; | |
bf6c8de0 | 8696 | } |
d01f58f9 | 8697 | |
8698 | /* Validate the sentinel. */ | |
8699 | sentinel = argarray[nargs - 1 - pos]; | |
8700 | if ((!POINTER_TYPE_P (TREE_TYPE (sentinel)) | |
8701 | || !integer_zerop (sentinel)) | |
8702 | /* Although __null (in C++) is only an integer we allow it | |
8703 | nevertheless, as we are guaranteed that it's exactly | |
8704 | as wide as a pointer, and we don't want to force | |
8705 | users to cast the NULL they have written there. | |
8706 | We warn with -Wstrict-null-sentinel, though. */ | |
8707 | && (warn_strict_null_sentinel || null_node != sentinel)) | |
77a357e3 | 8708 | warning (OPT_Wformat_, "missing sentinel in function call"); |
bf6c8de0 | 8709 | } |
8710 | } | |
8711 | ||
dbf6c367 | 8712 | /* Helper for check_function_nonnull; given a list of operands which |
8713 | must be non-null in ARGS, determine if operand PARAM_NUM should be | |
8714 | checked. */ | |
8715 | ||
8716 | static bool | |
1cae46be | 8717 | nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 8718 | { |
4ee9c684 | 8719 | unsigned HOST_WIDE_INT arg_num = 0; |
dbf6c367 | 8720 | |
8721 | for (; args; args = TREE_CHAIN (args)) | |
8722 | { | |
231bd014 | 8723 | bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num); |
8724 | ||
8725 | gcc_assert (found); | |
dbf6c367 | 8726 | |
8727 | if (arg_num == param_num) | |
8728 | return true; | |
8729 | } | |
8730 | return false; | |
8731 | } | |
8732 | ||
8733 | /* Check that the function argument PARAM (which is operand number | |
8734 | PARAM_NUM) is non-null. This is called by check_function_nonnull | |
8735 | via check_function_arguments_recurse. */ | |
8736 | ||
8737 | static void | |
9a03a746 | 8738 | check_nonnull_arg (void * ARG_UNUSED (ctx), tree param, |
1cae46be | 8739 | unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 8740 | { |
8741 | /* Just skip checking the argument if it's not a pointer. This can | |
8742 | happen if the "nonnull" attribute was given without an operand | |
8743 | list (which means to check every pointer argument). */ | |
8744 | ||
8745 | if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) | |
8746 | return; | |
8747 | ||
8748 | if (integer_zerop (param)) | |
155b601b | 8749 | warning (OPT_Wnonnull, "null argument where non-null required " |
8750 | "(argument %lu)", (unsigned long) param_num); | |
dbf6c367 | 8751 | } |
8752 | ||
8753 | /* Helper for nonnull attribute handling; fetch the operand number | |
8754 | from the attribute argument list. */ | |
8755 | ||
8756 | static bool | |
1cae46be | 8757 | get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) |
dbf6c367 | 8758 | { |
67409385 | 8759 | /* Verify the arg number is a constant. */ |
dbf6c367 | 8760 | if (TREE_CODE (arg_num_expr) != INTEGER_CST |
8761 | || TREE_INT_CST_HIGH (arg_num_expr) != 0) | |
8762 | return false; | |
8763 | ||
8764 | *valp = TREE_INT_CST_LOW (arg_num_expr); | |
8765 | return true; | |
8766 | } | |
fa987697 | 8767 | |
8768 | /* Handle a "nothrow" attribute; arguments as in | |
8769 | struct attribute_spec.handler. */ | |
8770 | ||
8771 | static tree | |
9a03a746 | 8772 | handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8773 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
fa987697 | 8774 | { |
8775 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
8776 | TREE_NOTHROW (*node) = 1; | |
8777 | /* ??? TODO: Support types. */ | |
8778 | else | |
8779 | { | |
9b2d6d13 | 8780 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
fa987697 | 8781 | *no_add_attrs = true; |
8782 | } | |
8783 | ||
8784 | return NULL_TREE; | |
8785 | } | |
7acb29a3 | 8786 | |
8787 | /* Handle a "cleanup" attribute; arguments as in | |
8788 | struct attribute_spec.handler. */ | |
8789 | ||
8790 | static tree | |
1cae46be | 8791 | handle_cleanup_attribute (tree *node, tree name, tree args, |
9a03a746 | 8792 | int ARG_UNUSED (flags), bool *no_add_attrs) |
7acb29a3 | 8793 | { |
8794 | tree decl = *node; | |
8795 | tree cleanup_id, cleanup_decl; | |
8796 | ||
8797 | /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do | |
8798 | for global destructors in C++. This requires infrastructure that | |
8799 | we don't have generically at the moment. It's also not a feature | |
8800 | we'd be missing too much, since we do have attribute constructor. */ | |
8801 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
8802 | { | |
9b2d6d13 | 8803 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
7acb29a3 | 8804 | *no_add_attrs = true; |
8805 | return NULL_TREE; | |
8806 | } | |
8807 | ||
8808 | /* Verify that the argument is a function in scope. */ | |
8809 | /* ??? We could support pointers to functions here as well, if | |
8810 | that was considered desirable. */ | |
8811 | cleanup_id = TREE_VALUE (args); | |
8812 | if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE) | |
8813 | { | |
07e3a3d2 | 8814 | error ("cleanup argument not an identifier"); |
7acb29a3 | 8815 | *no_add_attrs = true; |
8816 | return NULL_TREE; | |
8817 | } | |
d1c41717 | 8818 | cleanup_decl = lookup_name (cleanup_id); |
7acb29a3 | 8819 | if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL) |
8820 | { | |
07e3a3d2 | 8821 | error ("cleanup argument not a function"); |
7acb29a3 | 8822 | *no_add_attrs = true; |
8823 | return NULL_TREE; | |
8824 | } | |
8825 | ||
1cae46be | 8826 | /* That the function has proper type is checked with the |
7acb29a3 | 8827 | eventual call to build_function_call. */ |
8828 | ||
8829 | return NULL_TREE; | |
8830 | } | |
8a8cdb8d | 8831 | |
8832 | /* Handle a "warn_unused_result" attribute. No special handling. */ | |
8833 | ||
8834 | static tree | |
8835 | handle_warn_unused_result_attribute (tree *node, tree name, | |
9a03a746 | 8836 | tree ARG_UNUSED (args), |
8837 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8a8cdb8d | 8838 | { |
8839 | /* Ignore the attribute for functions not returning any value. */ | |
8840 | if (VOID_TYPE_P (TREE_TYPE (*node))) | |
8841 | { | |
9b2d6d13 | 8842 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
8a8cdb8d | 8843 | *no_add_attrs = true; |
8844 | } | |
8845 | ||
8846 | return NULL_TREE; | |
8847 | } | |
bf6c8de0 | 8848 | |
8849 | /* Handle a "sentinel" attribute. */ | |
8850 | ||
8851 | static tree | |
50ca527f | 8852 | handle_sentinel_attribute (tree *node, tree name, tree args, |
bf6c8de0 | 8853 | int ARG_UNUSED (flags), bool *no_add_attrs) |
8854 | { | |
a36cf284 | 8855 | if (!prototype_p (*node)) |
bf6c8de0 | 8856 | { |
9b2d6d13 | 8857 | warning (OPT_Wattributes, |
8858 | "%qE attribute requires prototypes with named arguments", name); | |
bf6c8de0 | 8859 | *no_add_attrs = true; |
bf6c8de0 | 8860 | } |
50ca527f | 8861 | else |
8862 | { | |
c33080b9 | 8863 | if (!stdarg_p (*node)) |
a0c938f0 | 8864 | { |
9b2d6d13 | 8865 | warning (OPT_Wattributes, |
8866 | "%qE attribute only applies to variadic functions", name); | |
50ca527f | 8867 | *no_add_attrs = true; |
8868 | } | |
8869 | } | |
a0c938f0 | 8870 | |
50ca527f | 8871 | if (args) |
bf6c8de0 | 8872 | { |
50ca527f | 8873 | tree position = TREE_VALUE (args); |
8874 | ||
50ca527f | 8875 | if (TREE_CODE (position) != INTEGER_CST) |
a0c938f0 | 8876 | { |
48e1416a | 8877 | warning (OPT_Wattributes, |
01b54db5 | 8878 | "requested position is not an integer constant"); |
50ca527f | 8879 | *no_add_attrs = true; |
8880 | } | |
8881 | else | |
a0c938f0 | 8882 | { |
50ca527f | 8883 | if (tree_int_cst_lt (position, integer_zero_node)) |
8884 | { | |
01b54db5 | 8885 | warning (OPT_Wattributes, |
8886 | "requested position is less than zero"); | |
50ca527f | 8887 | *no_add_attrs = true; |
8888 | } | |
8889 | } | |
bf6c8de0 | 8890 | } |
a0c938f0 | 8891 | |
bf6c8de0 | 8892 | return NULL_TREE; |
8893 | } | |
b5c26b42 | 8894 | |
8895 | /* Handle a "type_generic" attribute. */ | |
8896 | ||
8897 | static tree | |
8898 | handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name), | |
8899 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8900 | bool * ARG_UNUSED (no_add_attrs)) | |
8901 | { | |
19fbe3a4 | 8902 | /* Ensure we have a function type. */ |
8903 | gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE); | |
48e1416a | 8904 | |
19fbe3a4 | 8905 | /* Ensure we have a variadic function. */ |
c33080b9 | 8906 | gcc_assert (!prototype_p (*node) || stdarg_p (*node)); |
b5c26b42 | 8907 | |
8908 | return NULL_TREE; | |
8909 | } | |
46f8e3b0 | 8910 | |
24470055 | 8911 | /* Handle a "target" attribute. */ |
46f8e3b0 | 8912 | |
8913 | static tree | |
24470055 | 8914 | handle_target_attribute (tree *node, tree name, tree args, int flags, |
46f8e3b0 | 8915 | bool *no_add_attrs) |
8916 | { | |
8917 | /* Ensure we have a function type. */ | |
8918 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
8919 | { | |
8920 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8921 | *no_add_attrs = true; | |
8922 | } | |
46f8e3b0 | 8923 | else if (! targetm.target_option.valid_attribute_p (*node, name, args, |
ae0c3984 | 8924 | flags)) |
46f8e3b0 | 8925 | *no_add_attrs = true; |
8926 | ||
8927 | return NULL_TREE; | |
8928 | } | |
8929 | ||
8930 | /* Arguments being collected for optimization. */ | |
8931 | typedef const char *const_char_p; /* For DEF_VEC_P. */ | |
f1f41a6c | 8932 | static GTY(()) vec<const_char_p, va_gc> *optimize_args; |
46f8e3b0 | 8933 | |
8934 | ||
8935 | /* Inner function to convert a TREE_LIST to argv string to parse the optimize | |
8936 | options in ARGS. ATTR_P is true if this is for attribute(optimize), and | |
8937 | false for #pragma GCC optimize. */ | |
8938 | ||
8939 | bool | |
8940 | parse_optimize_options (tree args, bool attr_p) | |
8941 | { | |
8942 | bool ret = true; | |
8943 | unsigned opt_argc; | |
8944 | unsigned i; | |
2becf397 | 8945 | int saved_flag_strict_aliasing; |
46f8e3b0 | 8946 | const char **opt_argv; |
615ef0bb | 8947 | struct cl_decoded_option *decoded_options; |
8948 | unsigned int decoded_options_count; | |
46f8e3b0 | 8949 | tree ap; |
8950 | ||
8951 | /* Build up argv vector. Just in case the string is stored away, use garbage | |
8952 | collected strings. */ | |
f1f41a6c | 8953 | vec_safe_truncate (optimize_args, 0); |
8954 | vec_safe_push (optimize_args, (const char *) NULL); | |
46f8e3b0 | 8955 | |
8956 | for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap)) | |
8957 | { | |
8958 | tree value = TREE_VALUE (ap); | |
8959 | ||
8960 | if (TREE_CODE (value) == INTEGER_CST) | |
8961 | { | |
8962 | char buffer[20]; | |
8963 | sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value)); | |
f1f41a6c | 8964 | vec_safe_push (optimize_args, ggc_strdup (buffer)); |
46f8e3b0 | 8965 | } |
8966 | ||
8967 | else if (TREE_CODE (value) == STRING_CST) | |
8968 | { | |
8969 | /* Split string into multiple substrings. */ | |
8970 | size_t len = TREE_STRING_LENGTH (value); | |
8971 | char *p = ASTRDUP (TREE_STRING_POINTER (value)); | |
8972 | char *end = p + len; | |
8973 | char *comma; | |
8974 | char *next_p = p; | |
8975 | ||
8976 | while (next_p != NULL) | |
8977 | { | |
8978 | size_t len2; | |
8979 | char *q, *r; | |
8980 | ||
8981 | p = next_p; | |
8982 | comma = strchr (p, ','); | |
8983 | if (comma) | |
8984 | { | |
8985 | len2 = comma - p; | |
8986 | *comma = '\0'; | |
8987 | next_p = comma+1; | |
8988 | } | |
8989 | else | |
8990 | { | |
8991 | len2 = end - p; | |
8992 | next_p = NULL; | |
8993 | } | |
8994 | ||
ba72912a | 8995 | r = q = (char *) ggc_alloc_atomic (len2 + 3); |
46f8e3b0 | 8996 | |
8997 | /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx | |
8998 | options. */ | |
8999 | if (*p == '-' && p[1] != 'O' && p[1] != 'f') | |
9000 | { | |
9001 | ret = false; | |
9002 | if (attr_p) | |
9003 | warning (OPT_Wattributes, | |
ebd7c4c1 | 9004 | "bad option %s to optimize attribute", p); |
46f8e3b0 | 9005 | else |
9006 | warning (OPT_Wpragmas, | |
e44b0a1f | 9007 | "bad option %s to pragma attribute", p); |
46f8e3b0 | 9008 | continue; |
9009 | } | |
9010 | ||
9011 | if (*p != '-') | |
9012 | { | |
9013 | *r++ = '-'; | |
9014 | ||
9015 | /* Assume that Ox is -Ox, a numeric value is -Ox, a s by | |
9016 | itself is -Os, and any other switch begins with a -f. */ | |
9017 | if ((*p >= '0' && *p <= '9') | |
9018 | || (p[0] == 's' && p[1] == '\0')) | |
9019 | *r++ = 'O'; | |
9020 | else if (*p != 'O') | |
9021 | *r++ = 'f'; | |
9022 | } | |
9023 | ||
9024 | memcpy (r, p, len2); | |
9025 | r[len2] = '\0'; | |
f1f41a6c | 9026 | vec_safe_push (optimize_args, (const char *) q); |
46f8e3b0 | 9027 | } |
9028 | ||
9029 | } | |
9030 | } | |
9031 | ||
f1f41a6c | 9032 | opt_argc = optimize_args->length (); |
46f8e3b0 | 9033 | opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1)); |
9034 | ||
9035 | for (i = 1; i < opt_argc; i++) | |
f1f41a6c | 9036 | opt_argv[i] = (*optimize_args)[i]; |
46f8e3b0 | 9037 | |
2becf397 | 9038 | saved_flag_strict_aliasing = flag_strict_aliasing; |
9039 | ||
46f8e3b0 | 9040 | /* Now parse the options. */ |
f3f006ad | 9041 | decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv, |
9042 | &decoded_options, | |
9043 | &decoded_options_count); | |
9044 | decode_options (&global_options, &global_options_set, | |
3c6c0e40 | 9045 | decoded_options, decoded_options_count, |
9046 | input_location, global_dc); | |
46f8e3b0 | 9047 | |
4bec06b3 | 9048 | targetm.override_options_after_change(); |
9049 | ||
2becf397 | 9050 | /* Don't allow changing -fstrict-aliasing. */ |
9051 | flag_strict_aliasing = saved_flag_strict_aliasing; | |
9052 | ||
f1f41a6c | 9053 | optimize_args->truncate (0); |
46f8e3b0 | 9054 | return ret; |
9055 | } | |
9056 | ||
9057 | /* For handling "optimize" attribute. arguments as in | |
9058 | struct attribute_spec.handler. */ | |
9059 | ||
9060 | static tree | |
9061 | handle_optimize_attribute (tree *node, tree name, tree args, | |
9062 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
9063 | { | |
9064 | /* Ensure we have a function type. */ | |
9065 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
9066 | { | |
9067 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
9068 | *no_add_attrs = true; | |
9069 | } | |
9070 | else | |
9071 | { | |
9072 | struct cl_optimization cur_opts; | |
9073 | tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node); | |
9074 | ||
9075 | /* Save current options. */ | |
2c5d2e39 | 9076 | cl_optimization_save (&cur_opts, &global_options); |
46f8e3b0 | 9077 | |
9078 | /* If we previously had some optimization options, use them as the | |
9079 | default. */ | |
9080 | if (old_opts) | |
2c5d2e39 | 9081 | cl_optimization_restore (&global_options, |
9082 | TREE_OPTIMIZATION (old_opts)); | |
46f8e3b0 | 9083 | |
9084 | /* Parse options, and update the vector. */ | |
9085 | parse_optimize_options (args, true); | |
9086 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) | |
55310327 | 9087 | = build_optimization_node (&global_options); |
46f8e3b0 | 9088 | |
9089 | /* Restore current options. */ | |
2c5d2e39 | 9090 | cl_optimization_restore (&global_options, &cur_opts); |
46f8e3b0 | 9091 | } |
9092 | ||
9093 | return NULL_TREE; | |
9094 | } | |
48b14f50 | 9095 | |
9096 | /* Handle a "no_split_stack" attribute. */ | |
9097 | ||
9098 | static tree | |
9099 | handle_no_split_stack_attribute (tree *node, tree name, | |
9100 | tree ARG_UNUSED (args), | |
9101 | int ARG_UNUSED (flags), | |
9102 | bool *no_add_attrs) | |
9103 | { | |
9104 | tree decl = *node; | |
9105 | ||
9106 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
9107 | { | |
9108 | error_at (DECL_SOURCE_LOCATION (decl), | |
9109 | "%qE attribute applies only to functions", name); | |
9110 | *no_add_attrs = true; | |
9111 | } | |
9112 | else if (DECL_INITIAL (decl)) | |
9113 | { | |
9114 | error_at (DECL_SOURCE_LOCATION (decl), | |
9115 | "can%'t set %qE attribute after definition", name); | |
9116 | *no_add_attrs = true; | |
9117 | } | |
9118 | ||
9119 | return NULL_TREE; | |
9120 | } | |
d7dcba40 | 9121 | |
9122 | /* Handle a "returns_nonnull" attribute; arguments as in | |
9123 | struct attribute_spec.handler. */ | |
9124 | ||
9125 | static tree | |
9126 | handle_returns_nonnull_attribute (tree *node, tree, tree, int, | |
9127 | bool *no_add_attrs) | |
9128 | { | |
9129 | // Even without a prototype we still have a return type we can check. | |
9130 | if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE) | |
9131 | { | |
9132 | error ("returns_nonnull attribute on a function not returning a pointer"); | |
9133 | *no_add_attrs = true; | |
9134 | } | |
9135 | return NULL_TREE; | |
9136 | } | |
9137 | ||
dbf6c367 | 9138 | \f |
774e9d58 | 9139 | /* Check for valid arguments being passed to a function with FNTYPE. |
9140 | There are NARGS arguments in the array ARGARRAY. */ | |
dbf6c367 | 9141 | void |
774e9d58 | 9142 | check_function_arguments (const_tree fntype, int nargs, tree *argarray) |
dbf6c367 | 9143 | { |
9144 | /* Check for null being passed in a pointer argument that must be | |
9145 | non-null. We also need to do this if format checking is enabled. */ | |
9146 | ||
9147 | if (warn_nonnull) | |
774e9d58 | 9148 | check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
dbf6c367 | 9149 | |
9150 | /* Check for errors in format strings. */ | |
9151 | ||
068bea1e | 9152 | if (warn_format || warn_suggest_attribute_format) |
774e9d58 | 9153 | check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
95c90e04 | 9154 | |
9155 | if (warn_format) | |
774e9d58 | 9156 | check_function_sentinel (fntype, nargs, argarray); |
dbf6c367 | 9157 | } |
9158 | ||
9159 | /* Generic argument checking recursion routine. PARAM is the argument to | |
9160 | be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked | |
9161 | once the argument is resolved. CTX is context for the callback. */ | |
9162 | void | |
1cae46be | 9163 | check_function_arguments_recurse (void (*callback) |
9164 | (void *, tree, unsigned HOST_WIDE_INT), | |
9165 | void *ctx, tree param, | |
9166 | unsigned HOST_WIDE_INT param_num) | |
dbf6c367 | 9167 | { |
72dd6141 | 9168 | if (CONVERT_EXPR_P (param) |
c44afe23 | 9169 | && (TYPE_PRECISION (TREE_TYPE (param)) |
9170 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0))))) | |
dbf6c367 | 9171 | { |
9172 | /* Strip coercion. */ | |
9173 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 9174 | TREE_OPERAND (param, 0), param_num); |
dbf6c367 | 9175 | return; |
9176 | } | |
9177 | ||
9178 | if (TREE_CODE (param) == CALL_EXPR) | |
9179 | { | |
c2f47e15 | 9180 | tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param))); |
dbf6c367 | 9181 | tree attrs; |
9182 | bool found_format_arg = false; | |
9183 | ||
9184 | /* See if this is a call to a known internationalization function | |
9185 | that modifies a format arg. Such a function may have multiple | |
9186 | format_arg attributes (for example, ngettext). */ | |
9187 | ||
9188 | for (attrs = TYPE_ATTRIBUTES (type); | |
9189 | attrs; | |
9190 | attrs = TREE_CHAIN (attrs)) | |
9191 | if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs))) | |
9192 | { | |
c2f47e15 | 9193 | tree inner_arg; |
dbf6c367 | 9194 | tree format_num_expr; |
9195 | int format_num; | |
9196 | int i; | |
c2f47e15 | 9197 | call_expr_arg_iterator iter; |
dbf6c367 | 9198 | |
9199 | /* Extract the argument number, which was previously checked | |
9200 | to be valid. */ | |
9201 | format_num_expr = TREE_VALUE (TREE_VALUE (attrs)); | |
dbf6c367 | 9202 | |
5200ef07 | 9203 | format_num = tree_to_uhwi (format_num_expr); |
dbf6c367 | 9204 | |
c2f47e15 | 9205 | for (inner_arg = first_call_expr_arg (param, &iter), i = 1; |
9206 | inner_arg != 0; | |
9207 | inner_arg = next_call_expr_arg (&iter), i++) | |
dbf6c367 | 9208 | if (i == format_num) |
9209 | { | |
9210 | check_function_arguments_recurse (callback, ctx, | |
c2f47e15 | 9211 | inner_arg, param_num); |
dbf6c367 | 9212 | found_format_arg = true; |
9213 | break; | |
9214 | } | |
9215 | } | |
9216 | ||
9217 | /* If we found a format_arg attribute and did a recursive check, | |
9218 | we are done with checking this argument. Otherwise, we continue | |
9219 | and this will be considered a non-literal. */ | |
9220 | if (found_format_arg) | |
9221 | return; | |
9222 | } | |
9223 | ||
9224 | if (TREE_CODE (param) == COND_EXPR) | |
9225 | { | |
9226 | /* Check both halves of the conditional expression. */ | |
9227 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 9228 | TREE_OPERAND (param, 1), param_num); |
dbf6c367 | 9229 | check_function_arguments_recurse (callback, ctx, |
4ee9c684 | 9230 | TREE_OPERAND (param, 2), param_num); |
dbf6c367 | 9231 | return; |
9232 | } | |
9233 | ||
9234 | (*callback) (ctx, param, param_num); | |
9235 | } | |
1f3233d1 | 9236 | |
60cce472 | 9237 | /* Checks for a builtin function FNDECL that the number of arguments |
9238 | NARGS against the required number REQUIRED and issues an error if | |
9239 | there is a mismatch. Returns true if the number of arguments is | |
9240 | correct, otherwise false. */ | |
d43cee80 | 9241 | |
9242 | static bool | |
60cce472 | 9243 | builtin_function_validate_nargs (tree fndecl, int nargs, int required) |
d43cee80 | 9244 | { |
9245 | if (nargs < required) | |
9246 | { | |
60cce472 | 9247 | error_at (input_location, |
9248 | "not enough arguments to function %qE", fndecl); | |
d43cee80 | 9249 | return false; |
9250 | } | |
9251 | else if (nargs > required) | |
9252 | { | |
60cce472 | 9253 | error_at (input_location, |
9254 | "too many arguments to function %qE", fndecl); | |
d43cee80 | 9255 | return false; |
9256 | } | |
9257 | return true; | |
9258 | } | |
9259 | ||
9260 | /* Verifies the NARGS arguments ARGS to the builtin function FNDECL. | |
9261 | Returns false if there was an error, otherwise true. */ | |
9262 | ||
9263 | bool | |
9264 | check_builtin_function_arguments (tree fndecl, int nargs, tree *args) | |
9265 | { | |
9266 | if (!DECL_BUILT_IN (fndecl) | |
9267 | || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) | |
9268 | return true; | |
9269 | ||
9270 | switch (DECL_FUNCTION_CODE (fndecl)) | |
9271 | { | |
9272 | case BUILT_IN_CONSTANT_P: | |
60cce472 | 9273 | return builtin_function_validate_nargs (fndecl, nargs, 1); |
d43cee80 | 9274 | |
9275 | case BUILT_IN_ISFINITE: | |
9276 | case BUILT_IN_ISINF: | |
c319d56a | 9277 | case BUILT_IN_ISINF_SIGN: |
d43cee80 | 9278 | case BUILT_IN_ISNAN: |
9279 | case BUILT_IN_ISNORMAL: | |
60cce472 | 9280 | if (builtin_function_validate_nargs (fndecl, nargs, 1)) |
d43cee80 | 9281 | { |
9282 | if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE) | |
9283 | { | |
9284 | error ("non-floating-point argument in call to " | |
9285 | "function %qE", fndecl); | |
9286 | return false; | |
9287 | } | |
9288 | return true; | |
9289 | } | |
9290 | return false; | |
9291 | ||
9292 | case BUILT_IN_ISGREATER: | |
9293 | case BUILT_IN_ISGREATEREQUAL: | |
9294 | case BUILT_IN_ISLESS: | |
9295 | case BUILT_IN_ISLESSEQUAL: | |
9296 | case BUILT_IN_ISLESSGREATER: | |
9297 | case BUILT_IN_ISUNORDERED: | |
60cce472 | 9298 | if (builtin_function_validate_nargs (fndecl, nargs, 2)) |
d43cee80 | 9299 | { |
9300 | enum tree_code code0, code1; | |
9301 | code0 = TREE_CODE (TREE_TYPE (args[0])); | |
9302 | code1 = TREE_CODE (TREE_TYPE (args[1])); | |
9303 | if (!((code0 == REAL_TYPE && code1 == REAL_TYPE) | |
9304 | || (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
9305 | || (code0 == INTEGER_TYPE && code1 == REAL_TYPE))) | |
9306 | { | |
9307 | error ("non-floating-point arguments in call to " | |
9308 | "function %qE", fndecl); | |
9309 | return false; | |
9310 | } | |
9311 | return true; | |
9312 | } | |
9313 | return false; | |
9314 | ||
19fbe3a4 | 9315 | case BUILT_IN_FPCLASSIFY: |
60cce472 | 9316 | if (builtin_function_validate_nargs (fndecl, nargs, 6)) |
19fbe3a4 | 9317 | { |
9318 | unsigned i; | |
48e1416a | 9319 | |
19fbe3a4 | 9320 | for (i=0; i<5; i++) |
9321 | if (TREE_CODE (args[i]) != INTEGER_CST) | |
9322 | { | |
9323 | error ("non-const integer argument %u in call to function %qE", | |
9324 | i+1, fndecl); | |
9325 | return false; | |
9326 | } | |
9327 | ||
9328 | if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE) | |
9329 | { | |
9330 | error ("non-floating-point argument in call to function %qE", | |
9331 | fndecl); | |
9332 | return false; | |
9333 | } | |
9334 | return true; | |
9335 | } | |
9336 | return false; | |
9337 | ||
fca0886c | 9338 | case BUILT_IN_ASSUME_ALIGNED: |
9339 | if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2))) | |
9340 | { | |
9341 | if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE) | |
9342 | { | |
9343 | error ("non-integer argument 3 in call to function %qE", fndecl); | |
9344 | return false; | |
9345 | } | |
9346 | return true; | |
9347 | } | |
9348 | return false; | |
9349 | ||
d43cee80 | 9350 | default: |
9351 | return true; | |
9352 | } | |
9353 | } | |
9354 | ||
860251be | 9355 | /* Function to help qsort sort FIELD_DECLs by name order. */ |
9356 | ||
9357 | int | |
9358 | field_decl_cmp (const void *x_p, const void *y_p) | |
9359 | { | |
4fd61bc6 | 9360 | const tree *const x = (const tree *const) x_p; |
9361 | const tree *const y = (const tree *const) y_p; | |
9362 | ||
860251be | 9363 | if (DECL_NAME (*x) == DECL_NAME (*y)) |
9364 | /* A nontype is "greater" than a type. */ | |
9365 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
9366 | if (DECL_NAME (*x) == NULL_TREE) | |
9367 | return -1; | |
9368 | if (DECL_NAME (*y) == NULL_TREE) | |
9369 | return 1; | |
9370 | if (DECL_NAME (*x) < DECL_NAME (*y)) | |
9371 | return -1; | |
9372 | return 1; | |
9373 | } | |
9374 | ||
9375 | static struct { | |
9376 | gt_pointer_operator new_value; | |
9377 | void *cookie; | |
9378 | } resort_data; | |
9379 | ||
9380 | /* This routine compares two fields like field_decl_cmp but using the | |
9381 | pointer operator in resort_data. */ | |
9382 | ||
9383 | static int | |
9384 | resort_field_decl_cmp (const void *x_p, const void *y_p) | |
9385 | { | |
4fd61bc6 | 9386 | const tree *const x = (const tree *const) x_p; |
9387 | const tree *const y = (const tree *const) y_p; | |
860251be | 9388 | |
9389 | if (DECL_NAME (*x) == DECL_NAME (*y)) | |
9390 | /* A nontype is "greater" than a type. */ | |
9391 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
9392 | if (DECL_NAME (*x) == NULL_TREE) | |
9393 | return -1; | |
9394 | if (DECL_NAME (*y) == NULL_TREE) | |
9395 | return 1; | |
9396 | { | |
9397 | tree d1 = DECL_NAME (*x); | |
9398 | tree d2 = DECL_NAME (*y); | |
9399 | resort_data.new_value (&d1, resort_data.cookie); | |
9400 | resort_data.new_value (&d2, resort_data.cookie); | |
9401 | if (d1 < d2) | |
9402 | return -1; | |
9403 | } | |
9404 | return 1; | |
9405 | } | |
9406 | ||
9407 | /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ | |
9408 | ||
9409 | void | |
9410 | resort_sorted_fields (void *obj, | |
9a03a746 | 9411 | void * ARG_UNUSED (orig_obj), |
4ee9c684 | 9412 | gt_pointer_operator new_value, |
9413 | void *cookie) | |
860251be | 9414 | { |
9a03a746 | 9415 | struct sorted_fields_type *sf = (struct sorted_fields_type *) obj; |
860251be | 9416 | resort_data.new_value = new_value; |
9417 | resort_data.cookie = cookie; | |
9418 | qsort (&sf->elts[0], sf->len, sizeof (tree), | |
4ee9c684 | 9419 | resort_field_decl_cmp); |
860251be | 9420 | } |
9421 | ||
209c9752 | 9422 | /* Subroutine of c_parse_error. |
9423 | Return the result of concatenating LHS and RHS. RHS is really | |
9424 | a string literal, its first character is indicated by RHS_START and | |
cfee01e3 | 9425 | RHS_SIZE is its length (including the terminating NUL character). |
209c9752 | 9426 | |
9427 | The caller is responsible for deleting the returned pointer. */ | |
9428 | ||
9429 | static char * | |
9430 | catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) | |
9431 | { | |
9432 | const int lhs_size = strlen (lhs); | |
9433 | char *result = XNEWVEC (char, lhs_size + rhs_size); | |
9434 | strncpy (result, lhs, lhs_size); | |
9435 | strncpy (result + lhs_size, rhs_start, rhs_size); | |
9436 | return result; | |
9437 | } | |
9438 | ||
380c6697 | 9439 | /* Issue the error given by GMSGID, indicating that it occurred before |
92b128ed | 9440 | TOKEN, which had the associated VALUE. */ |
9441 | ||
9442 | void | |
48e1416a | 9443 | c_parse_error (const char *gmsgid, enum cpp_ttype token_type, |
ba99525e | 9444 | tree value, unsigned char token_flags) |
92b128ed | 9445 | { |
209c9752 | 9446 | #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2)) |
9447 | ||
9448 | char *message = NULL; | |
92b128ed | 9449 | |
ba99525e | 9450 | if (token_type == CPP_EOF) |
380c6697 | 9451 | message = catenate_messages (gmsgid, " at end of input"); |
48e1416a | 9452 | else if (token_type == CPP_CHAR |
9453 | || token_type == CPP_WCHAR | |
ba99525e | 9454 | || token_type == CPP_CHAR16 |
9455 | || token_type == CPP_CHAR32) | |
92b128ed | 9456 | { |
9457 | unsigned int val = TREE_INT_CST_LOW (value); | |
924bbf02 | 9458 | const char *prefix; |
9459 | ||
ba99525e | 9460 | switch (token_type) |
924bbf02 | 9461 | { |
9462 | default: | |
9463 | prefix = ""; | |
9464 | break; | |
9465 | case CPP_WCHAR: | |
9466 | prefix = "L"; | |
9467 | break; | |
9468 | case CPP_CHAR16: | |
9469 | prefix = "u"; | |
9470 | break; | |
9471 | case CPP_CHAR32: | |
9472 | prefix = "U"; | |
9473 | break; | |
9474 | } | |
9475 | ||
92b128ed | 9476 | if (val <= UCHAR_MAX && ISGRAPH (val)) |
a0c938f0 | 9477 | message = catenate_messages (gmsgid, " before %s'%c'"); |
92b128ed | 9478 | else |
a0c938f0 | 9479 | message = catenate_messages (gmsgid, " before %s'\\x%x'"); |
209c9752 | 9480 | |
924bbf02 | 9481 | error (message, prefix, val); |
209c9752 | 9482 | free (message); |
9483 | message = NULL; | |
92b128ed | 9484 | } |
1898176c | 9485 | else if (token_type == CPP_CHAR_USERDEF |
9486 | || token_type == CPP_WCHAR_USERDEF | |
9487 | || token_type == CPP_CHAR16_USERDEF | |
9488 | || token_type == CPP_CHAR32_USERDEF) | |
9489 | message = catenate_messages (gmsgid, | |
9490 | " before user-defined character literal"); | |
9491 | else if (token_type == CPP_STRING_USERDEF | |
9492 | || token_type == CPP_WSTRING_USERDEF | |
9493 | || token_type == CPP_STRING16_USERDEF | |
9494 | || token_type == CPP_STRING32_USERDEF | |
9495 | || token_type == CPP_UTF8STRING_USERDEF) | |
9496 | message = catenate_messages (gmsgid, " before user-defined string literal"); | |
48e1416a | 9497 | else if (token_type == CPP_STRING |
9498 | || token_type == CPP_WSTRING | |
ba99525e | 9499 | || token_type == CPP_STRING16 |
538ba11a | 9500 | || token_type == CPP_STRING32 |
9501 | || token_type == CPP_UTF8STRING) | |
380c6697 | 9502 | message = catenate_messages (gmsgid, " before string constant"); |
ba99525e | 9503 | else if (token_type == CPP_NUMBER) |
380c6697 | 9504 | message = catenate_messages (gmsgid, " before numeric constant"); |
ba99525e | 9505 | else if (token_type == CPP_NAME) |
209c9752 | 9506 | { |
380c6697 | 9507 | message = catenate_messages (gmsgid, " before %qE"); |
782858b8 | 9508 | error (message, value); |
209c9752 | 9509 | free (message); |
9510 | message = NULL; | |
9511 | } | |
ba99525e | 9512 | else if (token_type == CPP_PRAGMA) |
b75b98aa | 9513 | message = catenate_messages (gmsgid, " before %<#pragma%>"); |
ba99525e | 9514 | else if (token_type == CPP_PRAGMA_EOL) |
b75b98aa | 9515 | message = catenate_messages (gmsgid, " before end of line"); |
07b8f133 | 9516 | else if (token_type == CPP_DECLTYPE) |
9517 | message = catenate_messages (gmsgid, " before %<decltype%>"); | |
ba99525e | 9518 | else if (token_type < N_TTYPES) |
209c9752 | 9519 | { |
380c6697 | 9520 | message = catenate_messages (gmsgid, " before %qs token"); |
ba99525e | 9521 | error (message, cpp_type2name (token_type, token_flags)); |
209c9752 | 9522 | free (message); |
9523 | message = NULL; | |
9524 | } | |
92b128ed | 9525 | else |
380c6697 | 9526 | error (gmsgid); |
209c9752 | 9527 | |
9528 | if (message) | |
9529 | { | |
9530 | error (message); | |
9531 | free (message); | |
9532 | } | |
a0c938f0 | 9533 | #undef catenate_messages |
92b128ed | 9534 | } |
9535 | ||
3a79f5da | 9536 | /* Mapping for cpp message reasons to the options that enable them. */ |
9537 | ||
9538 | struct reason_option_codes_t | |
9539 | { | |
9540 | const int reason; /* cpplib message reason. */ | |
9541 | const int option_code; /* gcc option that controls this message. */ | |
9542 | }; | |
9543 | ||
9544 | static const struct reason_option_codes_t option_codes[] = { | |
9545 | {CPP_W_DEPRECATED, OPT_Wdeprecated}, | |
67089c6b | 9546 | {CPP_W_COMMENTS, OPT_Wcomment}, |
3a79f5da | 9547 | {CPP_W_TRIGRAPHS, OPT_Wtrigraphs}, |
9548 | {CPP_W_MULTICHAR, OPT_Wmultichar}, | |
9549 | {CPP_W_TRADITIONAL, OPT_Wtraditional}, | |
9550 | {CPP_W_LONG_LONG, OPT_Wlong_long}, | |
9551 | {CPP_W_ENDIF_LABELS, OPT_Wendif_labels}, | |
9552 | {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros}, | |
9553 | {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined}, | |
9554 | {CPP_W_UNDEF, OPT_Wundef}, | |
9555 | {CPP_W_UNUSED_MACROS, OPT_Wunused_macros}, | |
9556 | {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat}, | |
9557 | {CPP_W_NORMALIZE, OPT_Wnormalized_}, | |
9558 | {CPP_W_INVALID_PCH, OPT_Winvalid_pch}, | |
9559 | {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp}, | |
76d340ac | 9560 | {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix}, |
5b1a0622 | 9561 | {CPP_W_DATE_TIME, OPT_Wdate_time}, |
3a79f5da | 9562 | {CPP_W_NONE, 0} |
9563 | }; | |
9564 | ||
9565 | /* Return the gcc option code associated with the reason for a cpp | |
9566 | message, or 0 if none. */ | |
9567 | ||
9568 | static int | |
9569 | c_option_controlling_cpp_error (int reason) | |
9570 | { | |
9571 | const struct reason_option_codes_t *entry; | |
9572 | ||
9573 | for (entry = option_codes; entry->reason != CPP_W_NONE; entry++) | |
9574 | { | |
9575 | if (entry->reason == reason) | |
9576 | return entry->option_code; | |
9577 | } | |
9578 | return 0; | |
9579 | } | |
9580 | ||
7f5f3953 | 9581 | /* Callback from cpp_error for PFILE to print diagnostics from the |
3a79f5da | 9582 | preprocessor. The diagnostic is of type LEVEL, with REASON set |
9583 | to the reason code if LEVEL is represents a warning, at location | |
7f5f3953 | 9584 | LOCATION unless this is after lexing and the compiler's location |
9585 | should be used instead, with column number possibly overridden by | |
9586 | COLUMN_OVERRIDE if not zero; MSG is the translated message and AP | |
9587 | the arguments. Returns true if a diagnostic was emitted, false | |
9588 | otherwise. */ | |
9589 | ||
9590 | bool | |
3a79f5da | 9591 | c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason, |
7f5f3953 | 9592 | location_t location, unsigned int column_override, |
9593 | const char *msg, va_list *ap) | |
9594 | { | |
9595 | diagnostic_info diagnostic; | |
9596 | diagnostic_t dlevel; | |
5ae82d58 | 9597 | bool save_warn_system_headers = global_dc->dc_warn_system_headers; |
7f5f3953 | 9598 | bool ret; |
9599 | ||
9600 | switch (level) | |
9601 | { | |
9602 | case CPP_DL_WARNING_SYSHDR: | |
9603 | if (flag_no_output) | |
9604 | return false; | |
5ae82d58 | 9605 | global_dc->dc_warn_system_headers = 1; |
7f5f3953 | 9606 | /* Fall through. */ |
9607 | case CPP_DL_WARNING: | |
9608 | if (flag_no_output) | |
9609 | return false; | |
9610 | dlevel = DK_WARNING; | |
9611 | break; | |
9612 | case CPP_DL_PEDWARN: | |
9613 | if (flag_no_output && !flag_pedantic_errors) | |
9614 | return false; | |
9615 | dlevel = DK_PEDWARN; | |
9616 | break; | |
9617 | case CPP_DL_ERROR: | |
9618 | dlevel = DK_ERROR; | |
9619 | break; | |
9620 | case CPP_DL_ICE: | |
9621 | dlevel = DK_ICE; | |
9622 | break; | |
9623 | case CPP_DL_NOTE: | |
9624 | dlevel = DK_NOTE; | |
9625 | break; | |
ff903809 | 9626 | case CPP_DL_FATAL: |
9627 | dlevel = DK_FATAL; | |
9628 | break; | |
7f5f3953 | 9629 | default: |
9630 | gcc_unreachable (); | |
9631 | } | |
9632 | if (done_lexing) | |
9633 | location = input_location; | |
9634 | diagnostic_set_info_translated (&diagnostic, msg, ap, | |
9635 | location, dlevel); | |
9636 | if (column_override) | |
9637 | diagnostic_override_column (&diagnostic, column_override); | |
3a79f5da | 9638 | diagnostic_override_option_index (&diagnostic, |
9639 | c_option_controlling_cpp_error (reason)); | |
7f5f3953 | 9640 | ret = report_diagnostic (&diagnostic); |
9641 | if (level == CPP_DL_WARNING_SYSHDR) | |
5ae82d58 | 9642 | global_dc->dc_warn_system_headers = save_warn_system_headers; |
7f5f3953 | 9643 | return ret; |
9644 | } | |
9645 | ||
624d37a6 | 9646 | /* Convert a character from the host to the target execution character |
9647 | set. cpplib handles this, mostly. */ | |
9648 | ||
9649 | HOST_WIDE_INT | |
9650 | c_common_to_target_charset (HOST_WIDE_INT c) | |
9651 | { | |
9652 | /* Character constants in GCC proper are sign-extended under -fsigned-char, | |
9653 | zero-extended under -fno-signed-char. cpplib insists that characters | |
9654 | and character constants are always unsigned. Hence we must convert | |
9655 | back and forth. */ | |
9656 | cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1); | |
9657 | ||
9658 | uc = cpp_host_to_exec_charset (parse_in, uc); | |
9659 | ||
9660 | if (flag_signed_char) | |
9661 | return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE) | |
9662 | >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE); | |
9663 | else | |
9664 | return uc; | |
9665 | } | |
9666 | ||
7549df0d | 9667 | /* Fold an offsetof-like expression. EXPR is a nested sequence of component |
9668 | references with an INDIRECT_REF of a constant at the bottom; much like the | |
9669 | traditional rendering of offsetof as a macro. Return the folded result. */ | |
af28855b | 9670 | |
7549df0d | 9671 | tree |
9672 | fold_offsetof_1 (tree expr) | |
af28855b | 9673 | { |
af28855b | 9674 | tree base, off, t; |
9675 | ||
9676 | switch (TREE_CODE (expr)) | |
9677 | { | |
9678 | case ERROR_MARK: | |
9679 | return expr; | |
9680 | ||
6b11d2e3 | 9681 | case VAR_DECL: |
9682 | error ("cannot apply %<offsetof%> to static data member %qD", expr); | |
9683 | return error_mark_node; | |
9684 | ||
d897f7c2 | 9685 | case CALL_EXPR: |
cf1a89a3 | 9686 | case TARGET_EXPR: |
d897f7c2 | 9687 | error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded"); |
9688 | return error_mark_node; | |
9689 | ||
d897f7c2 | 9690 | case NOP_EXPR: |
9691 | case INDIRECT_REF: | |
7549df0d | 9692 | if (!TREE_CONSTANT (TREE_OPERAND (expr, 0))) |
64ed018c | 9693 | { |
9694 | error ("cannot apply %<offsetof%> to a non constant address"); | |
9695 | return error_mark_node; | |
9696 | } | |
7549df0d | 9697 | return TREE_OPERAND (expr, 0); |
d897f7c2 | 9698 | |
af28855b | 9699 | case COMPONENT_REF: |
7549df0d | 9700 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); |
af28855b | 9701 | if (base == error_mark_node) |
9702 | return base; | |
9703 | ||
9704 | t = TREE_OPERAND (expr, 1); | |
9705 | if (DECL_C_BIT_FIELD (t)) | |
9706 | { | |
9707 | error ("attempt to take address of bit-field structure " | |
782858b8 | 9708 | "member %qD", t); |
af28855b | 9709 | return error_mark_node; |
9710 | } | |
389dd41b | 9711 | off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t), |
08f817b3 | 9712 | size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t)) |
389dd41b | 9713 | / BITS_PER_UNIT)); |
af28855b | 9714 | break; |
9715 | ||
9716 | case ARRAY_REF: | |
7549df0d | 9717 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); |
af28855b | 9718 | if (base == error_mark_node) |
9719 | return base; | |
9720 | ||
9721 | t = TREE_OPERAND (expr, 1); | |
64ed018c | 9722 | |
9723 | /* Check if the offset goes beyond the upper bound of the array. */ | |
7549df0d | 9724 | if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0) |
e0559d69 | 9725 | { |
9726 | tree upbound = array_ref_up_bound (expr); | |
9727 | if (upbound != NULL_TREE | |
9728 | && TREE_CODE (upbound) == INTEGER_CST | |
9729 | && !tree_int_cst_equal (upbound, | |
9730 | TYPE_MAX_VALUE (TREE_TYPE (upbound)))) | |
9731 | { | |
9732 | upbound = size_binop (PLUS_EXPR, upbound, | |
9733 | build_int_cst (TREE_TYPE (upbound), 1)); | |
9734 | if (tree_int_cst_lt (upbound, t)) | |
9735 | { | |
9736 | tree v; | |
9737 | ||
9738 | for (v = TREE_OPERAND (expr, 0); | |
9739 | TREE_CODE (v) == COMPONENT_REF; | |
9740 | v = TREE_OPERAND (v, 0)) | |
9741 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) | |
9742 | == RECORD_TYPE) | |
9743 | { | |
1767a056 | 9744 | tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); |
9745 | for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) | |
e0559d69 | 9746 | if (TREE_CODE (fld_chain) == FIELD_DECL) |
9747 | break; | |
9748 | ||
9749 | if (fld_chain) | |
9750 | break; | |
9751 | } | |
9752 | /* Don't warn if the array might be considered a poor | |
9753 | man's flexible array member with a very permissive | |
9754 | definition thereof. */ | |
9755 | if (TREE_CODE (v) == ARRAY_REF | |
9756 | || TREE_CODE (v) == COMPONENT_REF) | |
9757 | warning (OPT_Warray_bounds, | |
9758 | "index %E denotes an offset " | |
9759 | "greater than size of %qT", | |
9760 | t, TREE_TYPE (TREE_OPERAND (expr, 0))); | |
9761 | } | |
9762 | } | |
9763 | } | |
7549df0d | 9764 | |
9765 | t = convert (sizetype, t); | |
9766 | off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); | |
af28855b | 9767 | break; |
9768 | ||
ede90cc2 | 9769 | case COMPOUND_EXPR: |
9770 | /* Handle static members of volatile structs. */ | |
9771 | t = TREE_OPERAND (expr, 1); | |
9772 | gcc_assert (TREE_CODE (t) == VAR_DECL); | |
7549df0d | 9773 | return fold_offsetof_1 (t); |
ede90cc2 | 9774 | |
af28855b | 9775 | default: |
231bd014 | 9776 | gcc_unreachable (); |
af28855b | 9777 | } |
9778 | ||
7549df0d | 9779 | return fold_build_pointer_plus (base, off); |
af28855b | 9780 | } |
9781 | ||
7549df0d | 9782 | /* Likewise, but convert it to the return type of offsetof. */ |
9783 | ||
af28855b | 9784 | tree |
7549df0d | 9785 | fold_offsetof (tree expr) |
af28855b | 9786 | { |
7549df0d | 9787 | return convert (size_type_node, fold_offsetof_1 (expr)); |
af28855b | 9788 | } |
9789 | ||
b9bdfa0b | 9790 | /* Warn for A ?: C expressions (with B omitted) where A is a boolean |
9791 | expression, because B will always be true. */ | |
9792 | ||
9793 | void | |
9794 | warn_for_omitted_condop (location_t location, tree cond) | |
9795 | { | |
9796 | if (truth_value_p (TREE_CODE (cond))) | |
9797 | warning_at (location, OPT_Wparentheses, | |
9798 | "the omitted middle operand in ?: will always be %<true%>, " | |
9799 | "suggest explicit middle operand"); | |
9800 | } | |
9801 | ||
a1f90215 | 9802 | /* Give an error for storing into ARG, which is 'const'. USE indicates |
9803 | how ARG was being used. */ | |
9804 | ||
9805 | void | |
f2697631 | 9806 | readonly_error (location_t loc, tree arg, enum lvalue_use use) |
a1f90215 | 9807 | { |
9808 | gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement | |
9809 | || use == lv_asm); | |
9810 | /* Using this macro rather than (for example) arrays of messages | |
9811 | ensures that all the format strings are checked at compile | |
9812 | time. */ | |
9813 | #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ | |
9814 | : (use == lv_increment ? (I) \ | |
9815 | : (use == lv_decrement ? (D) : (AS)))) | |
9816 | if (TREE_CODE (arg) == COMPONENT_REF) | |
9817 | { | |
9818 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
f2697631 | 9819 | error_at (loc, READONLY_MSG (G_("assignment of member " |
9820 | "%qD in read-only object"), | |
9821 | G_("increment of member " | |
9822 | "%qD in read-only object"), | |
9823 | G_("decrement of member " | |
9824 | "%qD in read-only object"), | |
9825 | G_("member %qD in read-only object " | |
9826 | "used as %<asm%> output")), | |
9827 | TREE_OPERAND (arg, 1)); | |
a1f90215 | 9828 | else |
f2697631 | 9829 | error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"), |
9830 | G_("increment of read-only member %qD"), | |
9831 | G_("decrement of read-only member %qD"), | |
9832 | G_("read-only member %qD used as %<asm%> output")), | |
9833 | TREE_OPERAND (arg, 1)); | |
a1f90215 | 9834 | } |
9835 | else if (TREE_CODE (arg) == VAR_DECL) | |
f2697631 | 9836 | error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"), |
9837 | G_("increment of read-only variable %qD"), | |
9838 | G_("decrement of read-only variable %qD"), | |
9839 | G_("read-only variable %qD used as %<asm%> output")), | |
9840 | arg); | |
a1f90215 | 9841 | else if (TREE_CODE (arg) == PARM_DECL) |
f2697631 | 9842 | error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"), |
9843 | G_("increment of read-only parameter %qD"), | |
9844 | G_("decrement of read-only parameter %qD"), | |
9845 | G_("read-only parameter %qD use as %<asm%> output")), | |
9846 | arg); | |
a1f90215 | 9847 | else if (TREE_CODE (arg) == RESULT_DECL) |
9848 | { | |
9849 | gcc_assert (c_dialect_cxx ()); | |
f2697631 | 9850 | error_at (loc, READONLY_MSG (G_("assignment of " |
9851 | "read-only named return value %qD"), | |
9852 | G_("increment of " | |
9853 | "read-only named return value %qD"), | |
9854 | G_("decrement of " | |
9855 | "read-only named return value %qD"), | |
9856 | G_("read-only named return value %qD " | |
9857 | "used as %<asm%>output")), | |
9858 | arg); | |
a1f90215 | 9859 | } |
9860 | else if (TREE_CODE (arg) == FUNCTION_DECL) | |
f2697631 | 9861 | error_at (loc, READONLY_MSG (G_("assignment of function %qD"), |
9862 | G_("increment of function %qD"), | |
9863 | G_("decrement of function %qD"), | |
9864 | G_("function %qD used as %<asm%> output")), | |
9865 | arg); | |
a1f90215 | 9866 | else |
f2697631 | 9867 | error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"), |
9868 | G_("increment of read-only location %qE"), | |
9869 | G_("decrement of read-only location %qE"), | |
9870 | G_("read-only location %qE used as %<asm%> output")), | |
9871 | arg); | |
a1f90215 | 9872 | } |
9873 | ||
e35976b1 | 9874 | /* Print an error message for an invalid lvalue. USE says |
fdd84b77 | 9875 | how the lvalue is being used and so selects the error message. LOC |
9876 | is the location for the error. */ | |
ab6bb714 | 9877 | |
e35976b1 | 9878 | void |
fdd84b77 | 9879 | lvalue_error (location_t loc, enum lvalue_use use) |
ab6bb714 | 9880 | { |
e35976b1 | 9881 | switch (use) |
ab6bb714 | 9882 | { |
e35976b1 | 9883 | case lv_assign: |
fdd84b77 | 9884 | error_at (loc, "lvalue required as left operand of assignment"); |
e35976b1 | 9885 | break; |
9886 | case lv_increment: | |
fdd84b77 | 9887 | error_at (loc, "lvalue required as increment operand"); |
e35976b1 | 9888 | break; |
9889 | case lv_decrement: | |
fdd84b77 | 9890 | error_at (loc, "lvalue required as decrement operand"); |
e35976b1 | 9891 | break; |
9892 | case lv_addressof: | |
fdd84b77 | 9893 | error_at (loc, "lvalue required as unary %<&%> operand"); |
e35976b1 | 9894 | break; |
9895 | case lv_asm: | |
fdd84b77 | 9896 | error_at (loc, "lvalue required in asm statement"); |
e35976b1 | 9897 | break; |
9898 | default: | |
9899 | gcc_unreachable (); | |
ab6bb714 | 9900 | } |
ab6bb714 | 9901 | } |
b1bbc8e5 | 9902 | |
9903 | /* Print an error message for an invalid indirection of type TYPE. | |
9904 | ERRSTRING is the name of the operator for the indirection. */ | |
9905 | ||
9906 | void | |
9907 | invalid_indirection_error (location_t loc, tree type, ref_operator errstring) | |
9908 | { | |
9909 | switch (errstring) | |
9910 | { | |
9911 | case RO_NULL: | |
9912 | gcc_assert (c_dialect_cxx ()); | |
9913 | error_at (loc, "invalid type argument (have %qT)", type); | |
9914 | break; | |
9915 | case RO_ARRAY_INDEXING: | |
9916 | error_at (loc, | |
9917 | "invalid type argument of array indexing (have %qT)", | |
9918 | type); | |
9919 | break; | |
9920 | case RO_UNARY_STAR: | |
9921 | error_at (loc, | |
9922 | "invalid type argument of unary %<*%> (have %qT)", | |
9923 | type); | |
9924 | break; | |
9925 | case RO_ARROW: | |
9926 | error_at (loc, | |
9927 | "invalid type argument of %<->%> (have %qT)", | |
9928 | type); | |
9929 | break; | |
7354a89b | 9930 | case RO_ARROW_STAR: |
9931 | error_at (loc, | |
9932 | "invalid type argument of %<->*%> (have %qT)", | |
9933 | type); | |
9934 | break; | |
b1bbc8e5 | 9935 | case RO_IMPLICIT_CONVERSION: |
9936 | error_at (loc, | |
9937 | "invalid type argument of implicit conversion (have %qT)", | |
9938 | type); | |
9939 | break; | |
9940 | default: | |
9941 | gcc_unreachable (); | |
9942 | } | |
9943 | } | |
c271bdb2 | 9944 | \f |
9945 | /* *PTYPE is an incomplete array. Complete it with a domain based on | |
9946 | INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT | |
9947 | is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
9948 | 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ | |
9949 | ||
9950 | int | |
9951 | complete_array_type (tree *ptype, tree initial_value, bool do_default) | |
9952 | { | |
9953 | tree maxindex, type, main_type, elt, unqual_elt; | |
9954 | int failure = 0, quals; | |
6753bca0 | 9955 | hashval_t hashcode = 0; |
f5298614 | 9956 | bool overflow_p = false; |
c271bdb2 | 9957 | |
9958 | maxindex = size_zero_node; | |
9959 | if (initial_value) | |
9960 | { | |
9961 | if (TREE_CODE (initial_value) == STRING_CST) | |
9962 | { | |
9963 | int eltsize | |
9964 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
9965 | maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1); | |
9966 | } | |
9967 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) | |
9968 | { | |
f1f41a6c | 9969 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value); |
c271bdb2 | 9970 | |
f1f41a6c | 9971 | if (vec_safe_is_empty (v)) |
c271bdb2 | 9972 | { |
9973 | if (pedantic) | |
9974 | failure = 3; | |
7542c3b4 | 9975 | maxindex = ssize_int (-1); |
c271bdb2 | 9976 | } |
9977 | else | |
9978 | { | |
9979 | tree curindex; | |
c75b4594 | 9980 | unsigned HOST_WIDE_INT cnt; |
9981 | constructor_elt *ce; | |
cee43f7e | 9982 | bool fold_p = false; |
c271bdb2 | 9983 | |
f1f41a6c | 9984 | if ((*v)[0].index) |
f5298614 | 9985 | maxindex = (*v)[0].index, fold_p = true; |
9986 | ||
c271bdb2 | 9987 | curindex = maxindex; |
9988 | ||
f1f41a6c | 9989 | for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++) |
c271bdb2 | 9990 | { |
cee43f7e | 9991 | bool curfold_p = false; |
c75b4594 | 9992 | if (ce->index) |
cee43f7e | 9993 | curindex = ce->index, curfold_p = true; |
c271bdb2 | 9994 | else |
cee43f7e | 9995 | { |
9996 | if (fold_p) | |
f5298614 | 9997 | { |
9998 | /* Since we treat size types now as ordinary | |
9999 | unsigned types, we need an explicit overflow | |
10000 | check. */ | |
10001 | tree orig = curindex; | |
10002 | curindex = fold_convert (sizetype, curindex); | |
10003 | overflow_p |= tree_int_cst_lt (curindex, orig); | |
10004 | } | |
389dd41b | 10005 | curindex = size_binop (PLUS_EXPR, curindex, |
10006 | size_one_node); | |
cee43f7e | 10007 | } |
c271bdb2 | 10008 | if (tree_int_cst_lt (maxindex, curindex)) |
cee43f7e | 10009 | maxindex = curindex, fold_p = curfold_p; |
c271bdb2 | 10010 | } |
f5298614 | 10011 | if (fold_p) |
10012 | { | |
10013 | tree orig = maxindex; | |
10014 | maxindex = fold_convert (sizetype, maxindex); | |
10015 | overflow_p |= tree_int_cst_lt (maxindex, orig); | |
10016 | } | |
c271bdb2 | 10017 | } |
10018 | } | |
10019 | else | |
10020 | { | |
10021 | /* Make an error message unless that happened already. */ | |
10022 | if (initial_value != error_mark_node) | |
10023 | failure = 1; | |
10024 | } | |
10025 | } | |
10026 | else | |
10027 | { | |
10028 | failure = 2; | |
10029 | if (!do_default) | |
10030 | return failure; | |
10031 | } | |
10032 | ||
10033 | type = *ptype; | |
10034 | elt = TREE_TYPE (type); | |
10035 | quals = TYPE_QUALS (strip_array_types (elt)); | |
10036 | if (quals == 0) | |
10037 | unqual_elt = elt; | |
10038 | else | |
6d5d708e | 10039 | unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals)); |
c271bdb2 | 10040 | |
10041 | /* Using build_distinct_type_copy and modifying things afterward instead | |
10042 | of using build_array_type to create a new type preserves all of the | |
10043 | TYPE_LANG_FLAG_? bits that the front end may have set. */ | |
10044 | main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); | |
10045 | TREE_TYPE (main_type) = unqual_elt; | |
783bb57e | 10046 | TYPE_DOMAIN (main_type) |
10047 | = build_range_type (TREE_TYPE (maxindex), | |
10048 | build_int_cst (TREE_TYPE (maxindex), 0), maxindex); | |
c271bdb2 | 10049 | layout_type (main_type); |
10050 | ||
6753bca0 | 10051 | /* Make sure we have the canonical MAIN_TYPE. */ |
10052 | hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode); | |
48e1416a | 10053 | hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)), |
6753bca0 | 10054 | hashcode); |
10055 | main_type = type_hash_canon (hashcode, main_type); | |
10056 | ||
796735dc | 10057 | /* Fix the canonical type. */ |
10058 | if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type)) | |
10059 | || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type))) | |
10060 | SET_TYPE_STRUCTURAL_EQUALITY (main_type); | |
10061 | else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type) | |
10062 | || (TYPE_CANONICAL (TYPE_DOMAIN (main_type)) | |
10063 | != TYPE_DOMAIN (main_type))) | |
48e1416a | 10064 | TYPE_CANONICAL (main_type) |
796735dc | 10065 | = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)), |
10066 | TYPE_CANONICAL (TYPE_DOMAIN (main_type))); | |
10067 | else | |
10068 | TYPE_CANONICAL (main_type) = main_type; | |
10069 | ||
c271bdb2 | 10070 | if (quals == 0) |
10071 | type = main_type; | |
10072 | else | |
10073 | type = c_build_qualified_type (main_type, quals); | |
10074 | ||
4f5b8f2a | 10075 | if (COMPLETE_TYPE_P (type) |
10076 | && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST | |
f5298614 | 10077 | && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))) |
4f5b8f2a | 10078 | { |
10079 | error ("size of array is too large"); | |
10080 | /* If we proceed with the array type as it is, we'll eventually | |
08f817b3 | 10081 | crash in tree_to_[su]hwi(). */ |
4f5b8f2a | 10082 | type = error_mark_node; |
10083 | } | |
10084 | ||
c271bdb2 | 10085 | *ptype = type; |
10086 | return failure; | |
10087 | } | |
ab6bb714 | 10088 | |
93426222 | 10089 | /* Like c_mark_addressable but don't check register qualifier. */ |
10090 | void | |
10091 | c_common_mark_addressable_vec (tree t) | |
10092 | { | |
10093 | while (handled_component_p (t)) | |
10094 | t = TREE_OPERAND (t, 0); | |
10095 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10096 | return; | |
10097 | TREE_ADDRESSABLE (t) = 1; | |
10098 | } | |
10099 | ||
10100 | ||
b6a5fc45 | 10101 | \f |
10102 | /* Used to help initialize the builtin-types.def table. When a type of | |
10103 | the correct size doesn't exist, use error_mark_node instead of NULL. | |
10104 | The later results in segfaults even when a decl using the type doesn't | |
10105 | get invoked. */ | |
10106 | ||
10107 | tree | |
10108 | builtin_type_for_size (int size, bool unsignedp) | |
10109 | { | |
a51edb4c | 10110 | tree type = c_common_type_for_size (size, unsignedp); |
b6a5fc45 | 10111 | return type ? type : error_mark_node; |
10112 | } | |
10113 | ||
10114 | /* A helper function for resolve_overloaded_builtin in resolving the | |
10115 | overloaded __sync_ builtins. Returns a positive power of 2 if the | |
10116 | first operand of PARAMS is a pointer to a supported data type. | |
10117 | Returns 0 if an error is encountered. */ | |
10118 | ||
10119 | static int | |
f1f41a6c | 10120 | sync_resolve_size (tree function, vec<tree, va_gc> *params) |
b6a5fc45 | 10121 | { |
10122 | tree type; | |
10123 | int size; | |
10124 | ||
f1f41a6c | 10125 | if (!params) |
b6a5fc45 | 10126 | { |
10127 | error ("too few arguments to function %qE", function); | |
10128 | return 0; | |
10129 | } | |
10130 | ||
f1f41a6c | 10131 | type = TREE_TYPE ((*params)[0]); |
b6a5fc45 | 10132 | if (TREE_CODE (type) != POINTER_TYPE) |
10133 | goto incompatible; | |
10134 | ||
10135 | type = TREE_TYPE (type); | |
10136 | if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) | |
10137 | goto incompatible; | |
10138 | ||
6a0712d4 | 10139 | size = tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
27213ba3 | 10140 | if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16) |
b6a5fc45 | 10141 | return size; |
10142 | ||
10143 | incompatible: | |
10144 | error ("incompatible type for argument %d of %qE", 1, function); | |
10145 | return 0; | |
10146 | } | |
10147 | ||
a0c938f0 | 10148 | /* A helper function for resolve_overloaded_builtin. Adds casts to |
b6a5fc45 | 10149 | PARAMS to make arguments match up with those of FUNCTION. Drops |
10150 | the variadic arguments at the end. Returns false if some error | |
10151 | was encountered; true on success. */ | |
10152 | ||
10153 | static bool | |
1cd6e20d | 10154 | sync_resolve_params (location_t loc, tree orig_function, tree function, |
f1f41a6c | 10155 | vec<tree, va_gc> *params, bool orig_format) |
b6a5fc45 | 10156 | { |
d0af78c5 | 10157 | function_args_iterator iter; |
b6a5fc45 | 10158 | tree ptype; |
b9c74b4d | 10159 | unsigned int parmnum; |
b6a5fc45 | 10160 | |
d0af78c5 | 10161 | function_args_iter_init (&iter, TREE_TYPE (function)); |
b6a5fc45 | 10162 | /* We've declared the implementation functions to use "volatile void *" |
10163 | as the pointer parameter, so we shouldn't get any complaints from the | |
10164 | call to check_function_arguments what ever type the user used. */ | |
d0af78c5 | 10165 | function_args_iter_next (&iter); |
f1f41a6c | 10166 | ptype = TREE_TYPE (TREE_TYPE ((*params)[0])); |
b560fabd | 10167 | ptype = TYPE_MAIN_VARIANT (ptype); |
b6a5fc45 | 10168 | |
10169 | /* For the rest of the values, we need to cast these to FTYPE, so that we | |
10170 | don't get warnings for passing pointer types, etc. */ | |
b9c74b4d | 10171 | parmnum = 0; |
d0af78c5 | 10172 | while (1) |
b6a5fc45 | 10173 | { |
d0af78c5 | 10174 | tree val, arg_type; |
10175 | ||
10176 | arg_type = function_args_iter_cond (&iter); | |
10177 | /* XXX void_type_node belies the abstraction. */ | |
10178 | if (arg_type == void_type_node) | |
10179 | break; | |
b6a5fc45 | 10180 | |
b9c74b4d | 10181 | ++parmnum; |
f1f41a6c | 10182 | if (params->length () <= parmnum) |
b6a5fc45 | 10183 | { |
1cd6e20d | 10184 | error_at (loc, "too few arguments to function %qE", orig_function); |
b6a5fc45 | 10185 | return false; |
10186 | } | |
10187 | ||
0f6a7cb7 | 10188 | /* Only convert parameters if arg_type is unsigned integer type with |
10189 | new format sync routines, i.e. don't attempt to convert pointer | |
10190 | arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n), | |
10191 | bool arguments (e.g. WEAK argument) or signed int arguments (memmodel | |
10192 | kinds). */ | |
10193 | if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type)) | |
1cd6e20d | 10194 | { |
10195 | /* Ideally for the first conversion we'd use convert_for_assignment | |
10196 | so that we get warnings for anything that doesn't match the pointer | |
10197 | type. This isn't portable across the C and C++ front ends atm. */ | |
f1f41a6c | 10198 | val = (*params)[parmnum]; |
1cd6e20d | 10199 | val = convert (ptype, val); |
10200 | val = convert (arg_type, val); | |
f1f41a6c | 10201 | (*params)[parmnum] = val; |
1cd6e20d | 10202 | } |
b6a5fc45 | 10203 | |
d0af78c5 | 10204 | function_args_iter_next (&iter); |
b6a5fc45 | 10205 | } |
10206 | ||
1cd6e20d | 10207 | /* __atomic routines are not variadic. */ |
f1f41a6c | 10208 | if (!orig_format && params->length () != parmnum + 1) |
1cd6e20d | 10209 | { |
10210 | error_at (loc, "too many arguments to function %qE", orig_function); | |
10211 | return false; | |
10212 | } | |
10213 | ||
b6a5fc45 | 10214 | /* The definition of these primitives is variadic, with the remaining |
10215 | being "an optional list of variables protected by the memory barrier". | |
10216 | No clue what that's supposed to mean, precisely, but we consider all | |
10217 | call-clobbered variables to be protected so we're safe. */ | |
f1f41a6c | 10218 | params->truncate (parmnum + 1); |
b6a5fc45 | 10219 | |
10220 | return true; | |
10221 | } | |
10222 | ||
a0c938f0 | 10223 | /* A helper function for resolve_overloaded_builtin. Adds a cast to |
b6a5fc45 | 10224 | RESULT to make it match the type of the first pointer argument in |
10225 | PARAMS. */ | |
10226 | ||
10227 | static tree | |
1cd6e20d | 10228 | sync_resolve_return (tree first_param, tree result, bool orig_format) |
b6a5fc45 | 10229 | { |
b9c74b4d | 10230 | tree ptype = TREE_TYPE (TREE_TYPE (first_param)); |
1cd6e20d | 10231 | tree rtype = TREE_TYPE (result); |
10080eac | 10232 | ptype = TYPE_MAIN_VARIANT (ptype); |
1cd6e20d | 10233 | |
10234 | /* New format doesn't require casting unless the types are the same size. */ | |
10235 | if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype))) | |
10236 | return convert (ptype, result); | |
10237 | else | |
10238 | return result; | |
10239 | } | |
10240 | ||
10241 | /* This function verifies the PARAMS to generic atomic FUNCTION. | |
10242 | It returns the size if all the parameters are the same size, otherwise | |
10243 | 0 is returned if the parameters are invalid. */ | |
10244 | ||
10245 | static int | |
f1f41a6c | 10246 | get_atomic_generic_size (location_t loc, tree function, |
10247 | vec<tree, va_gc> *params) | |
1cd6e20d | 10248 | { |
10249 | unsigned int n_param; | |
10250 | unsigned int n_model; | |
10251 | unsigned int x; | |
10252 | int size_0; | |
10253 | tree type_0; | |
10254 | ||
10255 | /* Determine the parameter makeup. */ | |
10256 | switch (DECL_FUNCTION_CODE (function)) | |
10257 | { | |
10258 | case BUILT_IN_ATOMIC_EXCHANGE: | |
10259 | n_param = 4; | |
10260 | n_model = 1; | |
10261 | break; | |
10262 | case BUILT_IN_ATOMIC_LOAD: | |
10263 | case BUILT_IN_ATOMIC_STORE: | |
10264 | n_param = 3; | |
10265 | n_model = 1; | |
10266 | break; | |
10267 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
10268 | n_param = 6; | |
10269 | n_model = 2; | |
10270 | break; | |
10271 | default: | |
1d581089 | 10272 | gcc_unreachable (); |
1cd6e20d | 10273 | } |
10274 | ||
f1f41a6c | 10275 | if (vec_safe_length (params) != n_param) |
1cd6e20d | 10276 | { |
10277 | error_at (loc, "incorrect number of arguments to function %qE", function); | |
10278 | return 0; | |
10279 | } | |
10280 | ||
10281 | /* Get type of first parameter, and determine its size. */ | |
f1f41a6c | 10282 | type_0 = TREE_TYPE ((*params)[0]); |
1d581089 | 10283 | if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0))) |
10284 | { | |
10285 | error_at (loc, "argument 1 of %qE must be a non-void pointer type", | |
10286 | function); | |
10287 | return 0; | |
10288 | } | |
10289 | ||
10290 | /* Types must be compile time constant sizes. */ | |
10291 | if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST) | |
1cd6e20d | 10292 | { |
1d581089 | 10293 | error_at (loc, |
10294 | "argument 1 of %qE must be a pointer to a constant size type", | |
10295 | function); | |
1cd6e20d | 10296 | return 0; |
10297 | } | |
1d581089 | 10298 | |
6a0712d4 | 10299 | size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0))); |
1cd6e20d | 10300 | |
1d581089 | 10301 | /* Zero size objects are not allowed. */ |
10302 | if (size_0 == 0) | |
10303 | { | |
10304 | error_at (loc, | |
10305 | "argument 1 of %qE must be a pointer to a nonzero size object", | |
10306 | function); | |
10307 | return 0; | |
10308 | } | |
10309 | ||
1cd6e20d | 10310 | /* Check each other parameter is a pointer and the same size. */ |
10311 | for (x = 0; x < n_param - n_model; x++) | |
10312 | { | |
10313 | int size; | |
f1f41a6c | 10314 | tree type = TREE_TYPE ((*params)[x]); |
a04e8d62 | 10315 | /* __atomic_compare_exchange has a bool in the 4th position, skip it. */ |
1cd6e20d | 10316 | if (n_param == 6 && x == 3) |
10317 | continue; | |
10318 | if (!POINTER_TYPE_P (type)) | |
10319 | { | |
10320 | error_at (loc, "argument %d of %qE must be a pointer type", x + 1, | |
10321 | function); | |
10322 | return 0; | |
10323 | } | |
6a0712d4 | 10324 | size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type))); |
1cd6e20d | 10325 | if (size != size_0) |
10326 | { | |
10327 | error_at (loc, "size mismatch in argument %d of %qE", x + 1, | |
10328 | function); | |
10329 | return 0; | |
10330 | } | |
10331 | } | |
10332 | ||
10333 | /* Check memory model parameters for validity. */ | |
10334 | for (x = n_param - n_model ; x < n_param; x++) | |
10335 | { | |
f1f41a6c | 10336 | tree p = (*params)[x]; |
1cd6e20d | 10337 | if (TREE_CODE (p) == INTEGER_CST) |
10338 | { | |
6a0712d4 | 10339 | int i = tree_to_uhwi (p); |
72d65da9 | 10340 | if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST) |
1cd6e20d | 10341 | { |
10342 | warning_at (loc, OPT_Winvalid_memory_model, | |
10343 | "invalid memory model argument %d of %qE", x + 1, | |
10344 | function); | |
1cd6e20d | 10345 | } |
10346 | } | |
10347 | else | |
10348 | if (!INTEGRAL_TYPE_P (TREE_TYPE (p))) | |
10349 | { | |
10350 | error_at (loc, "non-integer memory model argument %d of %qE", x + 1, | |
10351 | function); | |
10352 | return 0; | |
10353 | } | |
10354 | } | |
10355 | ||
10356 | return size_0; | |
10357 | } | |
10358 | ||
10359 | ||
10360 | /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N | |
10361 | at the beginning of the parameter list PARAMS representing the size of the | |
10362 | objects. This is to match the library ABI requirement. LOC is the location | |
10363 | of the function call. | |
10364 | The new function is returned if it needed rebuilding, otherwise NULL_TREE is | |
10365 | returned to allow the external call to be constructed. */ | |
10366 | ||
10367 | static tree | |
10368 | add_atomic_size_parameter (unsigned n, location_t loc, tree function, | |
f1f41a6c | 10369 | vec<tree, va_gc> *params) |
1cd6e20d | 10370 | { |
10371 | tree size_node; | |
10372 | ||
10373 | /* Insert a SIZE_T parameter as the first param. If there isn't | |
10374 | enough space, allocate a new vector and recursively re-build with that. */ | |
f1f41a6c | 10375 | if (!params->space (1)) |
1cd6e20d | 10376 | { |
10377 | unsigned int z, len; | |
f1f41a6c | 10378 | vec<tree, va_gc> *v; |
1cd6e20d | 10379 | tree f; |
10380 | ||
f1f41a6c | 10381 | len = params->length (); |
10382 | vec_alloc (v, len + 1); | |
1cd6e20d | 10383 | for (z = 0; z < len; z++) |
f1f41a6c | 10384 | v->quick_push ((*params)[z]); |
ec761d5a | 10385 | f = build_function_call_vec (loc, vNULL, function, v, NULL); |
f1f41a6c | 10386 | vec_free (v); |
1cd6e20d | 10387 | return f; |
10388 | } | |
10389 | ||
10390 | /* Add the size parameter and leave as a function call for processing. */ | |
10391 | size_node = build_int_cst (size_type_node, n); | |
f1f41a6c | 10392 | params->quick_insert (0, size_node); |
1cd6e20d | 10393 | return NULL_TREE; |
10394 | } | |
10395 | ||
10396 | ||
a056826c | 10397 | /* Return whether atomic operations for naturally aligned N-byte |
10398 | arguments are supported, whether inline or through libatomic. */ | |
10399 | static bool | |
10400 | atomic_size_supported_p (int n) | |
10401 | { | |
10402 | switch (n) | |
10403 | { | |
10404 | case 1: | |
10405 | case 2: | |
10406 | case 4: | |
10407 | case 8: | |
10408 | return true; | |
10409 | ||
10410 | case 16: | |
10411 | return targetm.scalar_mode_supported_p (TImode); | |
10412 | ||
10413 | default: | |
10414 | return false; | |
10415 | } | |
10416 | } | |
10417 | ||
1cd6e20d | 10418 | /* This will process an __atomic_exchange function call, determine whether it |
10419 | needs to be mapped to the _N variation, or turned into a library call. | |
10420 | LOC is the location of the builtin call. | |
10421 | FUNCTION is the DECL that has been invoked; | |
10422 | PARAMS is the argument list for the call. The return value is non-null | |
10423 | TRUE is returned if it is translated into the proper format for a call to the | |
10424 | external library, and NEW_RETURN is set the tree for that function. | |
10425 | FALSE is returned if processing for the _N variation is required, and | |
10426 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10427 | static bool | |
10428 | resolve_overloaded_atomic_exchange (location_t loc, tree function, | |
f1f41a6c | 10429 | vec<tree, va_gc> *params, tree *new_return) |
1cd6e20d | 10430 | { |
10431 | tree p0, p1, p2, p3; | |
10432 | tree I_type, I_type_ptr; | |
10433 | int n = get_atomic_generic_size (loc, function, params); | |
10434 | ||
1d581089 | 10435 | /* Size of 0 is an error condition. */ |
10436 | if (n == 0) | |
10437 | { | |
10438 | *new_return = error_mark_node; | |
10439 | return true; | |
10440 | } | |
10441 | ||
1cd6e20d | 10442 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 10443 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 10444 | { |
10445 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10446 | return true; | |
10447 | } | |
10448 | ||
10449 | /* Otherwise there is a lockfree match, transform the call from: | |
10450 | void fn(T* mem, T* desired, T* return, model) | |
10451 | into | |
10452 | *return = (T) (fn (In* mem, (In) *desired, model)) */ | |
10453 | ||
f1f41a6c | 10454 | p0 = (*params)[0]; |
10455 | p1 = (*params)[1]; | |
10456 | p2 = (*params)[2]; | |
10457 | p3 = (*params)[3]; | |
1cd6e20d | 10458 | |
10459 | /* Create pointer to appropriate size. */ | |
10460 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10461 | I_type_ptr = build_pointer_type (I_type); | |
10462 | ||
10463 | /* Convert object pointer to required type. */ | |
10464 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 10465 | (*params)[0] = p0; |
1cd6e20d | 10466 | /* Convert new value to required type, and dereference it. */ |
10467 | p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10468 | p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); | |
f1f41a6c | 10469 | (*params)[1] = p1; |
1cd6e20d | 10470 | |
10471 | /* Move memory model to the 3rd position, and end param list. */ | |
f1f41a6c | 10472 | (*params)[2] = p3; |
10473 | params->truncate (3); | |
1cd6e20d | 10474 | |
10475 | /* Convert return pointer and dereference it for later assignment. */ | |
10476 | *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR); | |
10477 | ||
10478 | return false; | |
b6a5fc45 | 10479 | } |
10480 | ||
1cd6e20d | 10481 | |
10482 | /* This will process an __atomic_compare_exchange function call, determine | |
10483 | whether it needs to be mapped to the _N variation, or turned into a lib call. | |
10484 | LOC is the location of the builtin call. | |
10485 | FUNCTION is the DECL that has been invoked; | |
10486 | PARAMS is the argument list for the call. The return value is non-null | |
10487 | TRUE is returned if it is translated into the proper format for a call to the | |
10488 | external library, and NEW_RETURN is set the tree for that function. | |
10489 | FALSE is returned if processing for the _N variation is required. */ | |
10490 | ||
10491 | static bool | |
10492 | resolve_overloaded_atomic_compare_exchange (location_t loc, tree function, | |
f1f41a6c | 10493 | vec<tree, va_gc> *params, |
1cd6e20d | 10494 | tree *new_return) |
10495 | { | |
10496 | tree p0, p1, p2; | |
10497 | tree I_type, I_type_ptr; | |
10498 | int n = get_atomic_generic_size (loc, function, params); | |
10499 | ||
1d581089 | 10500 | /* Size of 0 is an error condition. */ |
10501 | if (n == 0) | |
10502 | { | |
10503 | *new_return = error_mark_node; | |
10504 | return true; | |
10505 | } | |
10506 | ||
1cd6e20d | 10507 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 10508 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 10509 | { |
10510 | /* The library generic format does not have the weak parameter, so | |
10511 | remove it from the param list. Since a parameter has been removed, | |
10512 | we can be sure that there is room for the SIZE_T parameter, meaning | |
10513 | there will not be a recursive rebuilding of the parameter list, so | |
10514 | there is no danger this will be done twice. */ | |
10515 | if (n > 0) | |
10516 | { | |
f1f41a6c | 10517 | (*params)[3] = (*params)[4]; |
10518 | (*params)[4] = (*params)[5]; | |
10519 | params->truncate (5); | |
1cd6e20d | 10520 | } |
10521 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10522 | return true; | |
10523 | } | |
10524 | ||
10525 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10526 | bool fn(T* mem, T* desired, T* return, weak, success, failure) | |
10527 | into | |
10528 | bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */ | |
10529 | ||
f1f41a6c | 10530 | p0 = (*params)[0]; |
10531 | p1 = (*params)[1]; | |
10532 | p2 = (*params)[2]; | |
1cd6e20d | 10533 | |
10534 | /* Create pointer to appropriate size. */ | |
10535 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10536 | I_type_ptr = build_pointer_type (I_type); | |
10537 | ||
10538 | /* Convert object pointer to required type. */ | |
10539 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 10540 | (*params)[0] = p0; |
1cd6e20d | 10541 | |
10542 | /* Convert expected pointer to required type. */ | |
10543 | p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1); | |
f1f41a6c | 10544 | (*params)[1] = p1; |
1cd6e20d | 10545 | |
10546 | /* Convert desired value to required type, and dereference it. */ | |
10547 | p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR); | |
10548 | p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2); | |
f1f41a6c | 10549 | (*params)[2] = p2; |
1cd6e20d | 10550 | |
10551 | /* The rest of the parameters are fine. NULL means no special return value | |
10552 | processing.*/ | |
10553 | *new_return = NULL; | |
10554 | return false; | |
10555 | } | |
10556 | ||
10557 | ||
10558 | /* This will process an __atomic_load function call, determine whether it | |
10559 | needs to be mapped to the _N variation, or turned into a library call. | |
10560 | LOC is the location of the builtin call. | |
10561 | FUNCTION is the DECL that has been invoked; | |
10562 | PARAMS is the argument list for the call. The return value is non-null | |
10563 | TRUE is returned if it is translated into the proper format for a call to the | |
10564 | external library, and NEW_RETURN is set the tree for that function. | |
10565 | FALSE is returned if processing for the _N variation is required, and | |
10566 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10567 | ||
10568 | static bool | |
10569 | resolve_overloaded_atomic_load (location_t loc, tree function, | |
f1f41a6c | 10570 | vec<tree, va_gc> *params, tree *new_return) |
1cd6e20d | 10571 | { |
10572 | tree p0, p1, p2; | |
10573 | tree I_type, I_type_ptr; | |
10574 | int n = get_atomic_generic_size (loc, function, params); | |
10575 | ||
1d581089 | 10576 | /* Size of 0 is an error condition. */ |
10577 | if (n == 0) | |
10578 | { | |
10579 | *new_return = error_mark_node; | |
10580 | return true; | |
10581 | } | |
10582 | ||
1cd6e20d | 10583 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 10584 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 10585 | { |
10586 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10587 | return true; | |
10588 | } | |
10589 | ||
10590 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10591 | void fn(T* mem, T* return, model) | |
10592 | into | |
10593 | *return = (T) (fn ((In *) mem, model)) */ | |
10594 | ||
f1f41a6c | 10595 | p0 = (*params)[0]; |
10596 | p1 = (*params)[1]; | |
10597 | p2 = (*params)[2]; | |
1cd6e20d | 10598 | |
10599 | /* Create pointer to appropriate size. */ | |
10600 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10601 | I_type_ptr = build_pointer_type (I_type); | |
10602 | ||
10603 | /* Convert object pointer to required type. */ | |
10604 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 10605 | (*params)[0] = p0; |
1cd6e20d | 10606 | |
10607 | /* Move memory model to the 2nd position, and end param list. */ | |
f1f41a6c | 10608 | (*params)[1] = p2; |
10609 | params->truncate (2); | |
1cd6e20d | 10610 | |
10611 | /* Convert return pointer and dereference it for later assignment. */ | |
10612 | *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10613 | ||
10614 | return false; | |
10615 | } | |
10616 | ||
10617 | ||
10618 | /* This will process an __atomic_store function call, determine whether it | |
10619 | needs to be mapped to the _N variation, or turned into a library call. | |
10620 | LOC is the location of the builtin call. | |
10621 | FUNCTION is the DECL that has been invoked; | |
10622 | PARAMS is the argument list for the call. The return value is non-null | |
10623 | TRUE is returned if it is translated into the proper format for a call to the | |
10624 | external library, and NEW_RETURN is set the tree for that function. | |
10625 | FALSE is returned if processing for the _N variation is required, and | |
10626 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10627 | ||
10628 | static bool | |
10629 | resolve_overloaded_atomic_store (location_t loc, tree function, | |
f1f41a6c | 10630 | vec<tree, va_gc> *params, tree *new_return) |
1cd6e20d | 10631 | { |
10632 | tree p0, p1; | |
10633 | tree I_type, I_type_ptr; | |
10634 | int n = get_atomic_generic_size (loc, function, params); | |
10635 | ||
1d581089 | 10636 | /* Size of 0 is an error condition. */ |
10637 | if (n == 0) | |
10638 | { | |
10639 | *new_return = error_mark_node; | |
10640 | return true; | |
10641 | } | |
10642 | ||
1cd6e20d | 10643 | /* If not a lock-free size, change to the library generic format. */ |
a056826c | 10644 | if (!atomic_size_supported_p (n)) |
1cd6e20d | 10645 | { |
10646 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10647 | return true; | |
10648 | } | |
10649 | ||
10650 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10651 | void fn(T* mem, T* value, model) | |
10652 | into | |
10653 | fn ((In *) mem, (In) *value, model) */ | |
10654 | ||
f1f41a6c | 10655 | p0 = (*params)[0]; |
10656 | p1 = (*params)[1]; | |
1cd6e20d | 10657 | |
10658 | /* Create pointer to appropriate size. */ | |
10659 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10660 | I_type_ptr = build_pointer_type (I_type); | |
10661 | ||
10662 | /* Convert object pointer to required type. */ | |
10663 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
f1f41a6c | 10664 | (*params)[0] = p0; |
1cd6e20d | 10665 | |
10666 | /* Convert new value to required type, and dereference it. */ | |
10667 | p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10668 | p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); | |
f1f41a6c | 10669 | (*params)[1] = p1; |
1cd6e20d | 10670 | |
10671 | /* The memory model is in the right spot already. Return is void. */ | |
10672 | *new_return = NULL_TREE; | |
10673 | ||
10674 | return false; | |
10675 | } | |
10676 | ||
10677 | ||
b6a5fc45 | 10678 | /* Some builtin functions are placeholders for other expressions. This |
10679 | function should be called immediately after parsing the call expression | |
10680 | before surrounding code has committed to the type of the expression. | |
10681 | ||
e60a6f7b | 10682 | LOC is the location of the builtin call. |
10683 | ||
b6a5fc45 | 10684 | FUNCTION is the DECL that has been invoked; it is known to be a builtin. |
10685 | PARAMS is the argument list for the call. The return value is non-null | |
10686 | when expansion is complete, and null if normal processing should | |
10687 | continue. */ | |
10688 | ||
10689 | tree | |
f1f41a6c | 10690 | resolve_overloaded_builtin (location_t loc, tree function, |
10691 | vec<tree, va_gc> *params) | |
b6a5fc45 | 10692 | { |
10693 | enum built_in_function orig_code = DECL_FUNCTION_CODE (function); | |
1cd6e20d | 10694 | bool orig_format = true; |
10695 | tree new_return = NULL_TREE; | |
10696 | ||
65441f6f | 10697 | switch (DECL_BUILT_IN_CLASS (function)) |
10698 | { | |
10699 | case BUILT_IN_NORMAL: | |
10700 | break; | |
10701 | case BUILT_IN_MD: | |
10702 | if (targetm.resolve_overloaded_builtin) | |
e60a6f7b | 10703 | return targetm.resolve_overloaded_builtin (loc, function, params); |
65441f6f | 10704 | else |
a0c938f0 | 10705 | return NULL_TREE; |
65441f6f | 10706 | default: |
10707 | return NULL_TREE; | |
10708 | } | |
a0c938f0 | 10709 | |
65441f6f | 10710 | /* Handle BUILT_IN_NORMAL here. */ |
b6a5fc45 | 10711 | switch (orig_code) |
10712 | { | |
1cd6e20d | 10713 | case BUILT_IN_ATOMIC_EXCHANGE: |
10714 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
10715 | case BUILT_IN_ATOMIC_LOAD: | |
10716 | case BUILT_IN_ATOMIC_STORE: | |
10717 | { | |
10718 | /* Handle these 4 together so that they can fall through to the next | |
10719 | case if the call is transformed to an _N variant. */ | |
10720 | switch (orig_code) | |
10721 | { | |
10722 | case BUILT_IN_ATOMIC_EXCHANGE: | |
10723 | { | |
10724 | if (resolve_overloaded_atomic_exchange (loc, function, params, | |
10725 | &new_return)) | |
10726 | return new_return; | |
10727 | /* Change to the _N variant. */ | |
10728 | orig_code = BUILT_IN_ATOMIC_EXCHANGE_N; | |
10729 | break; | |
10730 | } | |
10731 | ||
10732 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
10733 | { | |
10734 | if (resolve_overloaded_atomic_compare_exchange (loc, function, | |
10735 | params, | |
10736 | &new_return)) | |
10737 | return new_return; | |
10738 | /* Change to the _N variant. */ | |
10739 | orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N; | |
10740 | break; | |
10741 | } | |
10742 | case BUILT_IN_ATOMIC_LOAD: | |
10743 | { | |
10744 | if (resolve_overloaded_atomic_load (loc, function, params, | |
10745 | &new_return)) | |
10746 | return new_return; | |
10747 | /* Change to the _N variant. */ | |
10748 | orig_code = BUILT_IN_ATOMIC_LOAD_N; | |
10749 | break; | |
10750 | } | |
10751 | case BUILT_IN_ATOMIC_STORE: | |
10752 | { | |
10753 | if (resolve_overloaded_atomic_store (loc, function, params, | |
10754 | &new_return)) | |
10755 | return new_return; | |
10756 | /* Change to the _N variant. */ | |
10757 | orig_code = BUILT_IN_ATOMIC_STORE_N; | |
10758 | break; | |
10759 | } | |
10760 | default: | |
10761 | gcc_unreachable (); | |
10762 | } | |
10763 | /* Fallthrough to the normal processing. */ | |
10764 | } | |
10765 | case BUILT_IN_ATOMIC_EXCHANGE_N: | |
10766 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N: | |
10767 | case BUILT_IN_ATOMIC_LOAD_N: | |
10768 | case BUILT_IN_ATOMIC_STORE_N: | |
10769 | case BUILT_IN_ATOMIC_ADD_FETCH_N: | |
10770 | case BUILT_IN_ATOMIC_SUB_FETCH_N: | |
10771 | case BUILT_IN_ATOMIC_AND_FETCH_N: | |
10772 | case BUILT_IN_ATOMIC_NAND_FETCH_N: | |
10773 | case BUILT_IN_ATOMIC_XOR_FETCH_N: | |
10774 | case BUILT_IN_ATOMIC_OR_FETCH_N: | |
10775 | case BUILT_IN_ATOMIC_FETCH_ADD_N: | |
10776 | case BUILT_IN_ATOMIC_FETCH_SUB_N: | |
10777 | case BUILT_IN_ATOMIC_FETCH_AND_N: | |
10778 | case BUILT_IN_ATOMIC_FETCH_NAND_N: | |
10779 | case BUILT_IN_ATOMIC_FETCH_XOR_N: | |
10780 | case BUILT_IN_ATOMIC_FETCH_OR_N: | |
10781 | { | |
10782 | orig_format = false; | |
10783 | /* Fallthru for parameter processing. */ | |
10784 | } | |
2797f13a | 10785 | case BUILT_IN_SYNC_FETCH_AND_ADD_N: |
10786 | case BUILT_IN_SYNC_FETCH_AND_SUB_N: | |
10787 | case BUILT_IN_SYNC_FETCH_AND_OR_N: | |
10788 | case BUILT_IN_SYNC_FETCH_AND_AND_N: | |
10789 | case BUILT_IN_SYNC_FETCH_AND_XOR_N: | |
10790 | case BUILT_IN_SYNC_FETCH_AND_NAND_N: | |
10791 | case BUILT_IN_SYNC_ADD_AND_FETCH_N: | |
10792 | case BUILT_IN_SYNC_SUB_AND_FETCH_N: | |
10793 | case BUILT_IN_SYNC_OR_AND_FETCH_N: | |
10794 | case BUILT_IN_SYNC_AND_AND_FETCH_N: | |
10795 | case BUILT_IN_SYNC_XOR_AND_FETCH_N: | |
10796 | case BUILT_IN_SYNC_NAND_AND_FETCH_N: | |
10797 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N: | |
10798 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N: | |
10799 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N: | |
10800 | case BUILT_IN_SYNC_LOCK_RELEASE_N: | |
b6a5fc45 | 10801 | { |
10802 | int n = sync_resolve_size (function, params); | |
b9c74b4d | 10803 | tree new_function, first_param, result; |
b9a16870 | 10804 | enum built_in_function fncode; |
b6a5fc45 | 10805 | |
10806 | if (n == 0) | |
10807 | return error_mark_node; | |
10808 | ||
b9a16870 | 10809 | fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1); |
10810 | new_function = builtin_decl_explicit (fncode); | |
1cd6e20d | 10811 | if (!sync_resolve_params (loc, function, new_function, params, |
10812 | orig_format)) | |
b6a5fc45 | 10813 | return error_mark_node; |
10814 | ||
f1f41a6c | 10815 | first_param = (*params)[0]; |
ec761d5a | 10816 | result = build_function_call_vec (loc, vNULL, new_function, params, |
10817 | NULL); | |
1cd6e20d | 10818 | if (result == error_mark_node) |
10819 | return result; | |
2797f13a | 10820 | if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N |
1cd6e20d | 10821 | && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N |
10822 | && orig_code != BUILT_IN_ATOMIC_STORE_N) | |
10823 | result = sync_resolve_return (first_param, result, orig_format); | |
b6a5fc45 | 10824 | |
1cd6e20d | 10825 | /* If new_return is set, assign function to that expr and cast the |
10826 | result to void since the generic interface returned void. */ | |
10827 | if (new_return) | |
10828 | { | |
10829 | /* Cast function result from I{1,2,4,8,16} to the required type. */ | |
10830 | result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result); | |
10831 | result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return, | |
10832 | result); | |
10833 | TREE_SIDE_EFFECTS (result) = 1; | |
10834 | protected_set_expr_location (result, loc); | |
10835 | result = convert (void_type_node, result); | |
10836 | } | |
b6a5fc45 | 10837 | return result; |
10838 | } | |
10839 | ||
10840 | default: | |
65441f6f | 10841 | return NULL_TREE; |
b6a5fc45 | 10842 | } |
10843 | } | |
10844 | ||
73437615 | 10845 | /* vector_types_compatible_elements_p is used in type checks of vectors |
10846 | values used as operands of binary operators. Where it returns true, and | |
10847 | the other checks of the caller succeed (being vector types in he first | |
10848 | place, and matching number of elements), we can just treat the types | |
10849 | as essentially the same. | |
10850 | Contrast with vector_targets_convertible_p, which is used for vector | |
10851 | pointer types, and vector_types_convertible_p, which will allow | |
10852 | language-specific matches under the control of flag_lax_vector_conversions, | |
10853 | and might still require a conversion. */ | |
10854 | /* True if vector types T1 and T2 can be inputs to the same binary | |
10855 | operator without conversion. | |
10856 | We don't check the overall vector size here because some of our callers | |
10857 | want to give different error messages when the vectors are compatible | |
10858 | except for the element count. */ | |
10859 | ||
491255f5 | 10860 | bool |
73437615 | 10861 | vector_types_compatible_elements_p (tree t1, tree t2) |
491255f5 | 10862 | { |
73437615 | 10863 | bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2); |
10864 | t1 = TREE_TYPE (t1); | |
10865 | t2 = TREE_TYPE (t2); | |
10866 | ||
491255f5 | 10867 | enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); |
10868 | ||
9421ebb9 | 10869 | gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE) |
10870 | && (c2 == INTEGER_TYPE || c2 == REAL_TYPE | |
10871 | || c2 == FIXED_POINT_TYPE)); | |
491255f5 | 10872 | |
73437615 | 10873 | t1 = c_common_signed_type (t1); |
10874 | t2 = c_common_signed_type (t2); | |
491255f5 | 10875 | /* Equality works here because c_common_signed_type uses |
10876 | TYPE_MAIN_VARIANT. */ | |
73437615 | 10877 | if (t1 == t2) |
10878 | return true; | |
10879 | if (opaque && c1 == c2 | |
10880 | && (c1 == INTEGER_TYPE || c1 == REAL_TYPE) | |
10881 | && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) | |
10882 | return true; | |
10883 | return false; | |
491255f5 | 10884 | } |
10885 | ||
be7350e7 | 10886 | /* Check for missing format attributes on function pointers. LTYPE is |
10887 | the new type or left-hand side type. RTYPE is the old type or | |
10888 | right-hand side type. Returns TRUE if LTYPE is missing the desired | |
10889 | attribute. */ | |
10890 | ||
10891 | bool | |
10892 | check_missing_format_attribute (tree ltype, tree rtype) | |
10893 | { | |
10894 | tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype); | |
10895 | tree ra; | |
10896 | ||
10897 | for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra)) | |
10898 | if (is_attribute_p ("format", TREE_PURPOSE (ra))) | |
10899 | break; | |
10900 | if (ra) | |
10901 | { | |
10902 | tree la; | |
10903 | for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la)) | |
10904 | if (is_attribute_p ("format", TREE_PURPOSE (la))) | |
10905 | break; | |
10906 | return !la; | |
10907 | } | |
10908 | else | |
10909 | return false; | |
10910 | } | |
10911 | ||
2840aae4 | 10912 | /* Subscripting with type char is likely to lose on a machine where |
10913 | chars are signed. So warn on any machine, but optionally. Don't | |
10914 | warn for unsigned char since that type is safe. Don't warn for | |
10915 | signed char because anyone who uses that must have done so | |
10916 | deliberately. Furthermore, we reduce the false positive load by | |
10917 | warning only for non-constant value of type char. */ | |
10918 | ||
10919 | void | |
10920 | warn_array_subscript_with_type_char (tree index) | |
10921 | { | |
10922 | if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node | |
10923 | && TREE_CODE (index) != INTEGER_CST) | |
10924 | warning (OPT_Wchar_subscripts, "array subscript has type %<char%>"); | |
10925 | } | |
10926 | ||
e534436e | 10927 | /* Implement -Wparentheses for the unexpected C precedence rules, to |
10928 | cover cases like x + y << z which readers are likely to | |
10929 | misinterpret. We have seen an expression in which CODE is a binary | |
82012ffe | 10930 | operator used to combine expressions ARG_LEFT and ARG_RIGHT, which |
10931 | before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and | |
10932 | CODE_RIGHT may be ERROR_MARK, which means that that side of the | |
10933 | expression was not formed using a binary or unary operator, or it | |
10934 | was enclosed in parentheses. */ | |
e534436e | 10935 | |
10936 | void | |
b0e7825e | 10937 | warn_about_parentheses (location_t loc, enum tree_code code, |
269f7979 | 10938 | enum tree_code code_left, tree arg_left, |
82012ffe | 10939 | enum tree_code code_right, tree arg_right) |
e534436e | 10940 | { |
10941 | if (!warn_parentheses) | |
10942 | return; | |
10943 | ||
82012ffe | 10944 | /* This macro tests that the expression ARG with original tree code |
10945 | CODE appears to be a boolean expression. or the result of folding a | |
10946 | boolean expression. */ | |
10947 | #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ | |
10948 | (truth_value_p (TREE_CODE (ARG)) \ | |
10949 | || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ | |
10950 | /* Folding may create 0 or 1 integers from other expressions. */ \ | |
10951 | || ((CODE) != INTEGER_CST \ | |
10952 | && (integer_onep (ARG) || integer_zerop (ARG)))) | |
10953 | ||
48e1416a | 10954 | switch (code) |
e534436e | 10955 | { |
82012ffe | 10956 | case LSHIFT_EXPR: |
b0e7825e | 10957 | if (code_left == PLUS_EXPR) |
10958 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10959 | "suggest parentheses around %<+%> inside %<<<%>"); | |
10960 | else if (code_right == PLUS_EXPR) | |
10961 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10962 | "suggest parentheses around %<+%> inside %<<<%>"); | |
10963 | else if (code_left == MINUS_EXPR) | |
10964 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10965 | "suggest parentheses around %<-%> inside %<<<%>"); | |
10966 | else if (code_right == MINUS_EXPR) | |
10967 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10968 | "suggest parentheses around %<-%> inside %<<<%>"); | |
82012ffe | 10969 | return; |
e534436e | 10970 | |
82012ffe | 10971 | case RSHIFT_EXPR: |
b0e7825e | 10972 | if (code_left == PLUS_EXPR) |
10973 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10974 | "suggest parentheses around %<+%> inside %<>>%>"); | |
10975 | else if (code_right == PLUS_EXPR) | |
10976 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10977 | "suggest parentheses around %<+%> inside %<>>%>"); | |
10978 | else if (code_left == MINUS_EXPR) | |
10979 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10980 | "suggest parentheses around %<-%> inside %<>>%>"); | |
10981 | else if (code_right == MINUS_EXPR) | |
10982 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10983 | "suggest parentheses around %<-%> inside %<>>%>"); | |
82012ffe | 10984 | return; |
e534436e | 10985 | |
82012ffe | 10986 | case TRUTH_ORIF_EXPR: |
b0e7825e | 10987 | if (code_left == TRUTH_ANDIF_EXPR) |
10988 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10989 | "suggest parentheses around %<&&%> within %<||%>"); | |
10990 | else if (code_right == TRUTH_ANDIF_EXPR) | |
10991 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10992 | "suggest parentheses around %<&&%> within %<||%>"); | |
82012ffe | 10993 | return; |
10994 | ||
10995 | case BIT_IOR_EXPR: | |
e534436e | 10996 | if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR |
b0e7825e | 10997 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
10998 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10999 | "suggest parentheses around arithmetic in operand of %<|%>"); | |
11000 | else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR | |
11001 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
11002 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11003 | "suggest parentheses around arithmetic in operand of %<|%>"); |
e534436e | 11004 | /* Check cases like x|y==z */ |
b0e7825e | 11005 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11006 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11007 | "suggest parentheses around comparison in operand of %<|%>"); | |
11008 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11009 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11010 | "suggest parentheses around comparison in operand of %<|%>"); |
11011 | /* Check cases like !x | y */ | |
11012 | else if (code_left == TRUTH_NOT_EXPR | |
11013 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
b0e7825e | 11014 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11015 | "suggest parentheses around operand of " | |
11016 | "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>"); | |
82012ffe | 11017 | return; |
e534436e | 11018 | |
82012ffe | 11019 | case BIT_XOR_EXPR: |
e534436e | 11020 | if (code_left == BIT_AND_EXPR |
b0e7825e | 11021 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
11022 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11023 | "suggest parentheses around arithmetic in operand of %<^%>"); | |
11024 | else if (code_right == BIT_AND_EXPR | |
11025 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
11026 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11027 | "suggest parentheses around arithmetic in operand of %<^%>"); |
e534436e | 11028 | /* Check cases like x^y==z */ |
b0e7825e | 11029 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11030 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11031 | "suggest parentheses around comparison in operand of %<^%>"); | |
11032 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11033 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11034 | "suggest parentheses around comparison in operand of %<^%>"); |
11035 | return; | |
e534436e | 11036 | |
82012ffe | 11037 | case BIT_AND_EXPR: |
b0e7825e | 11038 | if (code_left == PLUS_EXPR) |
11039 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11040 | "suggest parentheses around %<+%> in operand of %<&%>"); | |
11041 | else if (code_right == PLUS_EXPR) | |
11042 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11043 | "suggest parentheses around %<+%> in operand of %<&%>"); |
b0e7825e | 11044 | else if (code_left == MINUS_EXPR) |
11045 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11046 | "suggest parentheses around %<-%> in operand of %<&%>"); | |
11047 | else if (code_right == MINUS_EXPR) | |
11048 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11049 | "suggest parentheses around %<-%> in operand of %<&%>"); |
e534436e | 11050 | /* Check cases like x&y==z */ |
b0e7825e | 11051 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11052 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11053 | "suggest parentheses around comparison in operand of %<&%>"); | |
11054 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11055 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11056 | "suggest parentheses around comparison in operand of %<&%>"); |
11057 | /* Check cases like !x & y */ | |
11058 | else if (code_left == TRUTH_NOT_EXPR | |
11059 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
b0e7825e | 11060 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11061 | "suggest parentheses around operand of " | |
11062 | "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>"); | |
82012ffe | 11063 | return; |
e534436e | 11064 | |
82012ffe | 11065 | case EQ_EXPR: |
b0e7825e | 11066 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11067 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11068 | "suggest parentheses around comparison in operand of %<==%>"); | |
11069 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11070 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11071 | "suggest parentheses around comparison in operand of %<==%>"); |
11072 | return; | |
11073 | case NE_EXPR: | |
b0e7825e | 11074 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11075 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11076 | "suggest parentheses around comparison in operand of %<!=%>"); | |
11077 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11078 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 11079 | "suggest parentheses around comparison in operand of %<!=%>"); |
11080 | return; | |
11081 | ||
11082 | default: | |
b0e7825e | 11083 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
11084 | { | |
11085 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
269f7979 | 11086 | && code_left != NE_EXPR && code_left != EQ_EXPR |
11087 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) | |
b0e7825e | 11088 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11089 | "comparisons like %<X<=Y<=Z%> do not " | |
11090 | "have their mathematical meaning"); | |
11091 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison | |
269f7979 | 11092 | && code_right != NE_EXPR && code_right != EQ_EXPR |
b0e7825e | 11093 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))) |
11094 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11095 | "comparisons like %<X<=Y<=Z%> do not " | |
11096 | "have their mathematical meaning"); | |
11097 | } | |
82012ffe | 11098 | return; |
6ce0c450 | 11099 | } |
82012ffe | 11100 | #undef NOT_A_BOOLEAN_EXPR_P |
e534436e | 11101 | } |
11102 | ||
92fccaaa | 11103 | /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ |
11104 | ||
11105 | void | |
11106 | warn_for_unused_label (tree label) | |
11107 | { | |
11108 | if (!TREE_USED (label)) | |
11109 | { | |
11110 | if (DECL_INITIAL (label)) | |
11111 | warning (OPT_Wunused_label, "label %q+D defined but not used", label); | |
11112 | else | |
11113 | warning (OPT_Wunused_label, "label %q+D declared but not defined", label); | |
11114 | } | |
11115 | } | |
2840aae4 | 11116 | |
b6889cb0 | 11117 | /* Warn for division by zero according to the value of DIVISOR. LOC |
11118 | is the location of the division operator. */ | |
f092582b | 11119 | |
11120 | void | |
b6889cb0 | 11121 | warn_for_div_by_zero (location_t loc, tree divisor) |
f092582b | 11122 | { |
9421ebb9 | 11123 | /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning |
11124 | about division by zero. Do not issue a warning if DIVISOR has a | |
f092582b | 11125 | floating-point type, since we consider 0.0/0.0 a valid way of |
11126 | generating a NaN. */ | |
48d94ede | 11127 | if (c_inhibit_evaluation_warnings == 0 |
9421ebb9 | 11128 | && (integer_zerop (divisor) || fixed_zerop (divisor))) |
b6889cb0 | 11129 | warning_at (loc, OPT_Wdiv_by_zero, "division by zero"); |
f092582b | 11130 | } |
11131 | ||
13869a99 | 11132 | /* Subroutine of build_binary_op. Give warnings for comparisons |
11133 | between signed and unsigned quantities that may fail. Do the | |
11134 | checking based on the original operand trees ORIG_OP0 and ORIG_OP1, | |
11135 | so that casts will be considered, but default promotions won't | |
8e70fb09 | 11136 | be. |
11137 | ||
11138 | LOCATION is the location of the comparison operator. | |
13869a99 | 11139 | |
11140 | The arguments of this function map directly to local variables | |
11141 | of build_binary_op. */ | |
11142 | ||
48e1416a | 11143 | void |
8e70fb09 | 11144 | warn_for_sign_compare (location_t location, |
48e1416a | 11145 | tree orig_op0, tree orig_op1, |
11146 | tree op0, tree op1, | |
13869a99 | 11147 | tree result_type, enum tree_code resultcode) |
11148 | { | |
11149 | int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); | |
11150 | int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); | |
11151 | int unsignedp0, unsignedp1; | |
48e1416a | 11152 | |
13869a99 | 11153 | /* In C++, check for comparison of different enum types. */ |
11154 | if (c_dialect_cxx() | |
11155 | && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE | |
11156 | && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE | |
11157 | && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) | |
895b662f | 11158 | != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) |
13869a99 | 11159 | { |
8e70fb09 | 11160 | warning_at (location, |
11161 | OPT_Wsign_compare, "comparison between types %qT and %qT", | |
11162 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); | |
13869a99 | 11163 | } |
11164 | ||
11165 | /* Do not warn if the comparison is being done in a signed type, | |
11166 | since the signed type will only be chosen if it can represent | |
11167 | all the values of the unsigned type. */ | |
11168 | if (!TYPE_UNSIGNED (result_type)) | |
11169 | /* OK */; | |
11170 | /* Do not warn if both operands are unsigned. */ | |
11171 | else if (op0_signed == op1_signed) | |
11172 | /* OK */; | |
11173 | else | |
11174 | { | |
895b662f | 11175 | tree sop, uop, base_type; |
13869a99 | 11176 | bool ovf; |
895b662f | 11177 | |
13869a99 | 11178 | if (op0_signed) |
11179 | sop = orig_op0, uop = orig_op1; | |
48e1416a | 11180 | else |
13869a99 | 11181 | sop = orig_op1, uop = orig_op0; |
11182 | ||
48e1416a | 11183 | STRIP_TYPE_NOPS (sop); |
13869a99 | 11184 | STRIP_TYPE_NOPS (uop); |
895b662f | 11185 | base_type = (TREE_CODE (result_type) == COMPLEX_TYPE |
11186 | ? TREE_TYPE (result_type) : result_type); | |
13869a99 | 11187 | |
11188 | /* Do not warn if the signed quantity is an unsuffixed integer | |
11189 | literal (or some static constant expression involving such | |
11190 | literals or a conditional expression involving such literals) | |
11191 | and it is non-negative. */ | |
11192 | if (tree_expr_nonnegative_warnv_p (sop, &ovf)) | |
11193 | /* OK */; | |
11194 | /* Do not warn if the comparison is an equality operation, the | |
11195 | unsigned quantity is an integral constant, and it would fit | |
11196 | in the result if the result were signed. */ | |
11197 | else if (TREE_CODE (uop) == INTEGER_CST | |
11198 | && (resultcode == EQ_EXPR || resultcode == NE_EXPR) | |
895b662f | 11199 | && int_fits_type_p (uop, c_common_signed_type (base_type))) |
13869a99 | 11200 | /* OK */; |
11201 | /* In C, do not warn if the unsigned quantity is an enumeration | |
11202 | constant and its maximum value would fit in the result if the | |
11203 | result were signed. */ | |
11204 | else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST | |
11205 | && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE | |
11206 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), | |
895b662f | 11207 | c_common_signed_type (base_type))) |
13869a99 | 11208 | /* OK */; |
48e1416a | 11209 | else |
8e70fb09 | 11210 | warning_at (location, |
48e1416a | 11211 | OPT_Wsign_compare, |
8e70fb09 | 11212 | "comparison between signed and unsigned integer expressions"); |
13869a99 | 11213 | } |
48e1416a | 11214 | |
13869a99 | 11215 | /* Warn if two unsigned values are being compared in a size larger |
11216 | than their original size, and one (and only one) is the result of | |
11217 | a `~' operator. This comparison will always fail. | |
48e1416a | 11218 | |
13869a99 | 11219 | Also warn if one operand is a constant, and the constant does not |
11220 | have all bits set that are set in the ~ operand when it is | |
11221 | extended. */ | |
11222 | ||
7f506bca | 11223 | op0 = c_common_get_narrower (op0, &unsignedp0); |
11224 | op1 = c_common_get_narrower (op1, &unsignedp1); | |
48e1416a | 11225 | |
13869a99 | 11226 | if ((TREE_CODE (op0) == BIT_NOT_EXPR) |
11227 | ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) | |
11228 | { | |
11229 | if (TREE_CODE (op0) == BIT_NOT_EXPR) | |
7f506bca | 11230 | op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); |
13869a99 | 11231 | if (TREE_CODE (op1) == BIT_NOT_EXPR) |
7f506bca | 11232 | op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); |
13869a99 | 11233 | |
35ec552a | 11234 | if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1)) |
13869a99 | 11235 | { |
11236 | tree primop; | |
11237 | HOST_WIDE_INT constant, mask; | |
11238 | int unsignedp; | |
11239 | unsigned int bits; | |
48e1416a | 11240 | |
35ec552a | 11241 | if (tree_fits_shwi_p (op0)) |
13869a99 | 11242 | { |
11243 | primop = op1; | |
11244 | unsignedp = unsignedp1; | |
fcb97e84 | 11245 | constant = tree_to_shwi (op0); |
13869a99 | 11246 | } |
11247 | else | |
11248 | { | |
11249 | primop = op0; | |
11250 | unsignedp = unsignedp0; | |
fcb97e84 | 11251 | constant = tree_to_shwi (op1); |
13869a99 | 11252 | } |
48e1416a | 11253 | |
13869a99 | 11254 | bits = TYPE_PRECISION (TREE_TYPE (primop)); |
11255 | if (bits < TYPE_PRECISION (result_type) | |
11256 | && bits < HOST_BITS_PER_LONG && unsignedp) | |
11257 | { | |
11258 | mask = (~ (HOST_WIDE_INT) 0) << bits; | |
11259 | if ((mask & constant) != mask) | |
11260 | { | |
11261 | if (constant == 0) | |
48e1416a | 11262 | warning (OPT_Wsign_compare, |
13869a99 | 11263 | "promoted ~unsigned is always non-zero"); |
11264 | else | |
48e1416a | 11265 | warning_at (location, OPT_Wsign_compare, |
8e70fb09 | 11266 | "comparison of promoted ~unsigned with constant"); |
13869a99 | 11267 | } |
11268 | } | |
11269 | } | |
11270 | else if (unsignedp0 && unsignedp1 | |
11271 | && (TYPE_PRECISION (TREE_TYPE (op0)) | |
11272 | < TYPE_PRECISION (result_type)) | |
11273 | && (TYPE_PRECISION (TREE_TYPE (op1)) | |
11274 | < TYPE_PRECISION (result_type))) | |
8e70fb09 | 11275 | warning_at (location, OPT_Wsign_compare, |
13869a99 | 11276 | "comparison of promoted ~unsigned with unsigned"); |
11277 | } | |
11278 | } | |
11279 | ||
5ba33bf4 | 11280 | /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common |
11281 | type via c_common_type. If -Wdouble-promotion is in use, and the | |
11282 | conditions for warning have been met, issue a warning. GMSGID is | |
11283 | the warning message. It must have two %T specifiers for the type | |
11284 | that was converted (generally "float") and the type to which it was | |
11285 | converted (generally "double), respectively. LOC is the location | |
11286 | to which the awrning should refer. */ | |
11287 | ||
11288 | void | |
11289 | do_warn_double_promotion (tree result_type, tree type1, tree type2, | |
11290 | const char *gmsgid, location_t loc) | |
11291 | { | |
11292 | tree source_type; | |
11293 | ||
11294 | if (!warn_double_promotion) | |
11295 | return; | |
11296 | /* If the conversion will not occur at run-time, there is no need to | |
11297 | warn about it. */ | |
11298 | if (c_inhibit_evaluation_warnings) | |
11299 | return; | |
11300 | if (TYPE_MAIN_VARIANT (result_type) != double_type_node | |
11301 | && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) | |
11302 | return; | |
11303 | if (TYPE_MAIN_VARIANT (type1) == float_type_node | |
11304 | || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) | |
11305 | source_type = type1; | |
11306 | else if (TYPE_MAIN_VARIANT (type2) == float_type_node | |
11307 | || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) | |
11308 | source_type = type2; | |
11309 | else | |
11310 | return; | |
11311 | warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); | |
11312 | } | |
11313 | ||
41771881 | 11314 | /* Setup a TYPE_DECL node as a typedef representation. |
11315 | ||
11316 | X is a TYPE_DECL for a typedef statement. Create a brand new | |
11317 | ..._TYPE node (which will be just a variant of the existing | |
11318 | ..._TYPE node with identical properties) and then install X | |
11319 | as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. | |
11320 | ||
11321 | The whole point here is to end up with a situation where each | |
11322 | and every ..._TYPE node the compiler creates will be uniquely | |
11323 | associated with AT MOST one node representing a typedef name. | |
11324 | This way, even though the compiler substitutes corresponding | |
11325 | ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very | |
11326 | early on, later parts of the compiler can always do the reverse | |
11327 | translation and get back the corresponding typedef name. For | |
11328 | example, given: | |
11329 | ||
11330 | typedef struct S MY_TYPE; | |
11331 | MY_TYPE object; | |
11332 | ||
11333 | Later parts of the compiler might only know that `object' was of | |
11334 | type `struct S' if it were not for code just below. With this | |
11335 | code however, later parts of the compiler see something like: | |
11336 | ||
11337 | struct S' == struct S | |
11338 | typedef struct S' MY_TYPE; | |
11339 | struct S' object; | |
11340 | ||
11341 | And they can then deduce (from the node for type struct S') that | |
11342 | the original object declaration was: | |
11343 | ||
11344 | MY_TYPE object; | |
11345 | ||
11346 | Being able to do this is important for proper support of protoize, | |
11347 | and also for generating precise symbolic debugging information | |
11348 | which takes full account of the programmer's (typedef) vocabulary. | |
11349 | ||
11350 | Obviously, we don't want to generate a duplicate ..._TYPE node if | |
11351 | the TYPE_DECL node that we are now processing really represents a | |
11352 | standard built-in type. */ | |
11353 | ||
11354 | void | |
11355 | set_underlying_type (tree x) | |
11356 | { | |
11357 | if (x == error_mark_node) | |
11358 | return; | |
11359 | if (DECL_IS_BUILTIN (x)) | |
11360 | { | |
11361 | if (TYPE_NAME (TREE_TYPE (x)) == 0) | |
11362 | TYPE_NAME (TREE_TYPE (x)) = x; | |
11363 | } | |
11364 | else if (TREE_TYPE (x) != error_mark_node | |
11365 | && DECL_ORIGINAL_TYPE (x) == NULL_TREE) | |
11366 | { | |
11367 | tree tt = TREE_TYPE (x); | |
11368 | DECL_ORIGINAL_TYPE (x) = tt; | |
11369 | tt = build_variant_type_copy (tt); | |
11370 | TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
11371 | TYPE_NAME (tt) = x; | |
11372 | TREE_USED (tt) = TREE_USED (x); | |
11373 | TREE_TYPE (x) = tt; | |
11374 | } | |
11375 | } | |
11376 | ||
1a4c44c5 | 11377 | /* Record the types used by the current global variable declaration |
11378 | being parsed, so that we can decide later to emit their debug info. | |
11379 | Those types are in types_used_by_cur_var_decl, and we are going to | |
11380 | store them in the types_used_by_vars_hash hash table. | |
11381 | DECL is the declaration of the global variable that has been parsed. */ | |
11382 | ||
11383 | void | |
11384 | record_types_used_by_current_var_decl (tree decl) | |
11385 | { | |
11386 | gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl)); | |
11387 | ||
f1f41a6c | 11388 | while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ()) |
1a4c44c5 | 11389 | { |
f1f41a6c | 11390 | tree type = types_used_by_cur_var_decl->pop (); |
aef48c9a | 11391 | types_used_by_var_decl_insert (type, decl); |
1a4c44c5 | 11392 | } |
11393 | } | |
11394 | ||
a4e3ffad | 11395 | /* If DECL is a typedef that is declared in the current function, |
11396 | record it for the purpose of -Wunused-local-typedefs. */ | |
11397 | ||
11398 | void | |
11399 | record_locally_defined_typedef (tree decl) | |
11400 | { | |
11401 | struct c_language_function *l; | |
11402 | ||
11403 | if (!warn_unused_local_typedefs | |
11404 | || cfun == NULL | |
11405 | /* if this is not a locally defined typedef then we are not | |
11406 | interested. */ | |
11407 | || !is_typedef_decl (decl) | |
11408 | || !decl_function_context (decl)) | |
11409 | return; | |
11410 | ||
11411 | l = (struct c_language_function *) cfun->language; | |
f1f41a6c | 11412 | vec_safe_push (l->local_typedefs, decl); |
a4e3ffad | 11413 | } |
11414 | ||
11415 | /* If T is a TYPE_DECL declared locally, mark it as used. */ | |
11416 | ||
11417 | void | |
11418 | maybe_record_typedef_use (tree t) | |
11419 | { | |
11420 | if (!is_typedef_decl (t)) | |
11421 | return; | |
11422 | ||
11423 | TREE_USED (t) = true; | |
11424 | } | |
11425 | ||
11426 | /* Warn if there are some unused locally defined typedefs in the | |
11427 | current function. */ | |
11428 | ||
11429 | void | |
11430 | maybe_warn_unused_local_typedefs (void) | |
11431 | { | |
11432 | int i; | |
11433 | tree decl; | |
11434 | /* The number of times we have emitted -Wunused-local-typedefs | |
11435 | warnings. If this is different from errorcount, that means some | |
11436 | unrelated errors have been issued. In which case, we'll avoid | |
11437 | emitting "unused-local-typedefs" warnings. */ | |
11438 | static int unused_local_typedefs_warn_count; | |
11439 | struct c_language_function *l; | |
11440 | ||
11441 | if (cfun == NULL) | |
11442 | return; | |
11443 | ||
11444 | if ((l = (struct c_language_function *) cfun->language) == NULL) | |
11445 | return; | |
11446 | ||
11447 | if (warn_unused_local_typedefs | |
11448 | && errorcount == unused_local_typedefs_warn_count) | |
11449 | { | |
f1f41a6c | 11450 | FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl) |
a4e3ffad | 11451 | if (!TREE_USED (decl)) |
11452 | warning_at (DECL_SOURCE_LOCATION (decl), | |
11453 | OPT_Wunused_local_typedefs, | |
11454 | "typedef %qD locally defined but not used", decl); | |
11455 | unused_local_typedefs_warn_count = errorcount; | |
11456 | } | |
11457 | ||
f1f41a6c | 11458 | vec_free (l->local_typedefs); |
a4e3ffad | 11459 | } |
11460 | ||
f352a3fb | 11461 | /* The C and C++ parsers both use vectors to hold function arguments. |
11462 | For efficiency, we keep a cache of unused vectors. This is the | |
11463 | cache. */ | |
11464 | ||
f1f41a6c | 11465 | typedef vec<tree, va_gc> *tree_gc_vec; |
11466 | static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache; | |
f352a3fb | 11467 | |
11468 | /* Return a new vector from the cache. If the cache is empty, | |
11469 | allocate a new vector. These vectors are GC'ed, so it is OK if the | |
11470 | pointer is not released.. */ | |
11471 | ||
f1f41a6c | 11472 | vec<tree, va_gc> * |
f352a3fb | 11473 | make_tree_vector (void) |
11474 | { | |
f1f41a6c | 11475 | if (tree_vector_cache && !tree_vector_cache->is_empty ()) |
11476 | return tree_vector_cache->pop (); | |
f352a3fb | 11477 | else |
11478 | { | |
f1f41a6c | 11479 | /* Passing 0 to vec::alloc returns NULL, and our callers require |
f352a3fb | 11480 | that we always return a non-NULL value. The vector code uses |
11481 | 4 when growing a NULL vector, so we do too. */ | |
f1f41a6c | 11482 | vec<tree, va_gc> *v; |
11483 | vec_alloc (v, 4); | |
11484 | return v; | |
f352a3fb | 11485 | } |
11486 | } | |
11487 | ||
11488 | /* Release a vector of trees back to the cache. */ | |
11489 | ||
11490 | void | |
f1f41a6c | 11491 | release_tree_vector (vec<tree, va_gc> *vec) |
f352a3fb | 11492 | { |
11493 | if (vec != NULL) | |
11494 | { | |
f1f41a6c | 11495 | vec->truncate (0); |
11496 | vec_safe_push (tree_vector_cache, vec); | |
f352a3fb | 11497 | } |
11498 | } | |
11499 | ||
11500 | /* Get a new tree vector holding a single tree. */ | |
11501 | ||
f1f41a6c | 11502 | vec<tree, va_gc> * |
f352a3fb | 11503 | make_tree_vector_single (tree t) |
11504 | { | |
f1f41a6c | 11505 | vec<tree, va_gc> *ret = make_tree_vector (); |
11506 | ret->quick_push (t); | |
f352a3fb | 11507 | return ret; |
11508 | } | |
11509 | ||
c66c81be | 11510 | /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */ |
11511 | ||
f1f41a6c | 11512 | vec<tree, va_gc> * |
c66c81be | 11513 | make_tree_vector_from_list (tree list) |
11514 | { | |
f1f41a6c | 11515 | vec<tree, va_gc> *ret = make_tree_vector (); |
c66c81be | 11516 | for (; list; list = TREE_CHAIN (list)) |
f1f41a6c | 11517 | vec_safe_push (ret, TREE_VALUE (list)); |
c66c81be | 11518 | return ret; |
11519 | } | |
11520 | ||
f352a3fb | 11521 | /* Get a new tree vector which is a copy of an existing one. */ |
11522 | ||
f1f41a6c | 11523 | vec<tree, va_gc> * |
11524 | make_tree_vector_copy (const vec<tree, va_gc> *orig) | |
f352a3fb | 11525 | { |
f1f41a6c | 11526 | vec<tree, va_gc> *ret; |
f352a3fb | 11527 | unsigned int ix; |
11528 | tree t; | |
11529 | ||
11530 | ret = make_tree_vector (); | |
f1f41a6c | 11531 | vec_safe_reserve (ret, vec_safe_length (orig)); |
11532 | FOR_EACH_VEC_SAFE_ELT (orig, ix, t) | |
11533 | ret->quick_push (t); | |
f352a3fb | 11534 | return ret; |
11535 | } | |
11536 | ||
a9ffdd35 | 11537 | /* Return true if KEYWORD starts a type specifier. */ |
11538 | ||
11539 | bool | |
11540 | keyword_begins_type_specifier (enum rid keyword) | |
11541 | { | |
11542 | switch (keyword) | |
11543 | { | |
4fba5eb9 | 11544 | case RID_AUTO_TYPE: |
a9ffdd35 | 11545 | case RID_INT: |
11546 | case RID_CHAR: | |
11547 | case RID_FLOAT: | |
11548 | case RID_DOUBLE: | |
11549 | case RID_VOID: | |
11550 | case RID_INT128: | |
11551 | case RID_UNSIGNED: | |
11552 | case RID_LONG: | |
11553 | case RID_SHORT: | |
11554 | case RID_SIGNED: | |
11555 | case RID_DFLOAT32: | |
11556 | case RID_DFLOAT64: | |
11557 | case RID_DFLOAT128: | |
11558 | case RID_FRACT: | |
11559 | case RID_ACCUM: | |
11560 | case RID_BOOL: | |
11561 | case RID_WCHAR: | |
11562 | case RID_CHAR16: | |
11563 | case RID_CHAR32: | |
11564 | case RID_SAT: | |
11565 | case RID_COMPLEX: | |
11566 | case RID_TYPEOF: | |
11567 | case RID_STRUCT: | |
11568 | case RID_CLASS: | |
11569 | case RID_UNION: | |
11570 | case RID_ENUM: | |
11571 | return true; | |
11572 | default: | |
11573 | return false; | |
11574 | } | |
11575 | } | |
11576 | ||
11577 | /* Return true if KEYWORD names a type qualifier. */ | |
11578 | ||
11579 | bool | |
11580 | keyword_is_type_qualifier (enum rid keyword) | |
11581 | { | |
11582 | switch (keyword) | |
11583 | { | |
11584 | case RID_CONST: | |
11585 | case RID_VOLATILE: | |
11586 | case RID_RESTRICT: | |
b560fabd | 11587 | case RID_ATOMIC: |
a9ffdd35 | 11588 | return true; |
11589 | default: | |
11590 | return false; | |
11591 | } | |
11592 | } | |
11593 | ||
11594 | /* Return true if KEYWORD names a storage class specifier. | |
11595 | ||
11596 | RID_TYPEDEF is not included in this list despite `typedef' being | |
11597 | listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as | |
11598 | such for syntactic convenience only. */ | |
11599 | ||
11600 | bool | |
11601 | keyword_is_storage_class_specifier (enum rid keyword) | |
11602 | { | |
11603 | switch (keyword) | |
11604 | { | |
11605 | case RID_STATIC: | |
11606 | case RID_EXTERN: | |
11607 | case RID_REGISTER: | |
11608 | case RID_AUTO: | |
11609 | case RID_MUTABLE: | |
11610 | case RID_THREAD: | |
11611 | return true; | |
11612 | default: | |
11613 | return false; | |
11614 | } | |
11615 | } | |
11616 | ||
fad3f658 | 11617 | /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */ |
11618 | ||
11619 | static bool | |
11620 | keyword_is_function_specifier (enum rid keyword) | |
11621 | { | |
11622 | switch (keyword) | |
11623 | { | |
11624 | case RID_INLINE: | |
985c6e3a | 11625 | case RID_NORETURN: |
fad3f658 | 11626 | case RID_VIRTUAL: |
11627 | case RID_EXPLICIT: | |
11628 | return true; | |
11629 | default: | |
11630 | return false; | |
11631 | } | |
11632 | } | |
11633 | ||
11634 | /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a | |
11635 | declaration-specifier (C99 6.7). */ | |
11636 | ||
11637 | bool | |
11638 | keyword_is_decl_specifier (enum rid keyword) | |
11639 | { | |
11640 | if (keyword_is_storage_class_specifier (keyword) | |
11641 | || keyword_is_type_qualifier (keyword) | |
11642 | || keyword_is_function_specifier (keyword)) | |
11643 | return true; | |
11644 | ||
11645 | switch (keyword) | |
11646 | { | |
11647 | case RID_TYPEDEF: | |
11648 | case RID_FRIEND: | |
11649 | case RID_CONSTEXPR: | |
11650 | return true; | |
11651 | default: | |
11652 | return false; | |
11653 | } | |
11654 | } | |
11655 | ||
9b88d08d | 11656 | /* Initialize language-specific-bits of tree_contains_struct. */ |
11657 | ||
11658 | void | |
11659 | c_common_init_ts (void) | |
11660 | { | |
11661 | MARK_TS_TYPED (C_MAYBE_CONST_EXPR); | |
11662 | MARK_TS_TYPED (EXCESS_PRECISION_EXPR); | |
3c6d4197 | 11663 | MARK_TS_TYPED (ARRAY_NOTATION_REF); |
9b88d08d | 11664 | } |
11665 | ||
244db24d | 11666 | /* Build a user-defined numeric literal out of an integer constant type VALUE |
11667 | with identifier SUFFIX. */ | |
11668 | ||
11669 | tree | |
324ca377 | 11670 | build_userdef_literal (tree suffix_id, tree value, |
11671 | enum overflow_type overflow, tree num_string) | |
244db24d | 11672 | { |
11673 | tree literal = make_node (USERDEF_LITERAL); | |
11674 | USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id; | |
11675 | USERDEF_LITERAL_VALUE (literal) = value; | |
324ca377 | 11676 | USERDEF_LITERAL_OVERFLOW (literal) = overflow; |
244db24d | 11677 | USERDEF_LITERAL_NUM_STRING (literal) = num_string; |
11678 | return literal; | |
11679 | } | |
11680 | ||
7059d45d | 11681 | /* For vector[index], convert the vector to a |
11682 | pointer of the underlying type. */ | |
11683 | void | |
11684 | convert_vector_to_pointer_for_subscript (location_t loc, | |
11685 | tree* vecp, tree index) | |
11686 | { | |
11687 | if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE) | |
11688 | { | |
11689 | tree type = TREE_TYPE (*vecp); | |
11690 | tree type1; | |
11691 | ||
11692 | if (TREE_CODE (index) == INTEGER_CST) | |
cd4547bf | 11693 | if (!tree_fits_uhwi_p (index) |
aa59f000 | 11694 | || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type)) |
7059d45d | 11695 | warning_at (loc, OPT_Warray_bounds, "index value is out of bound"); |
11696 | ||
11697 | c_common_mark_addressable_vec (*vecp); | |
11698 | type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); | |
11699 | type = build_pointer_type (type); | |
11700 | type1 = build_pointer_type (TREE_TYPE (*vecp)); | |
11701 | *vecp = build1 (ADDR_EXPR, type1, *vecp); | |
11702 | *vecp = convert (type, *vecp); | |
11703 | } | |
11704 | } | |
11705 | ||
41ed701a | 11706 | /* Determine which of the operands, if any, is a scalar that needs to be |
11707 | converted to a vector, for the range of operations. */ | |
11708 | enum stv_conv | |
11709 | scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1, | |
11710 | bool complain) | |
11711 | { | |
11712 | tree type0 = TREE_TYPE (op0); | |
11713 | tree type1 = TREE_TYPE (op1); | |
11714 | bool integer_only_op = false; | |
11715 | enum stv_conv ret = stv_firstarg; | |
11716 | ||
11717 | gcc_assert (TREE_CODE (type0) == VECTOR_TYPE | |
11718 | || TREE_CODE (type1) == VECTOR_TYPE); | |
11719 | switch (code) | |
11720 | { | |
11721 | /* Most GENERIC binary expressions require homogeneous arguments. | |
11722 | LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first | |
11723 | argument that is a vector and a second one that is a scalar, so | |
11724 | we never return stv_secondarg for them. */ | |
11725 | case RSHIFT_EXPR: | |
11726 | case LSHIFT_EXPR: | |
11727 | if (TREE_CODE (type0) == INTEGER_TYPE | |
11728 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
11729 | { | |
22a75734 | 11730 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
41ed701a | 11731 | { |
11732 | if (complain) | |
11733 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
11734 | "involves truncation", type0, type1); | |
11735 | return stv_error; | |
11736 | } | |
11737 | else | |
11738 | return stv_firstarg; | |
11739 | } | |
11740 | break; | |
11741 | ||
11742 | case BIT_IOR_EXPR: | |
11743 | case BIT_XOR_EXPR: | |
11744 | case BIT_AND_EXPR: | |
11745 | integer_only_op = true; | |
11746 | /* ... fall through ... */ | |
11747 | ||
7b463b19 | 11748 | case VEC_COND_EXPR: |
11749 | ||
41ed701a | 11750 | case PLUS_EXPR: |
11751 | case MINUS_EXPR: | |
11752 | case MULT_EXPR: | |
11753 | case TRUNC_DIV_EXPR: | |
11754 | case CEIL_DIV_EXPR: | |
11755 | case FLOOR_DIV_EXPR: | |
11756 | case ROUND_DIV_EXPR: | |
11757 | case EXACT_DIV_EXPR: | |
11758 | case TRUNC_MOD_EXPR: | |
11759 | case FLOOR_MOD_EXPR: | |
11760 | case RDIV_EXPR: | |
11761 | case EQ_EXPR: | |
11762 | case NE_EXPR: | |
11763 | case LE_EXPR: | |
11764 | case GE_EXPR: | |
11765 | case LT_EXPR: | |
11766 | case GT_EXPR: | |
11767 | /* What about UNLT_EXPR? */ | |
11768 | if (TREE_CODE (type0) == VECTOR_TYPE) | |
11769 | { | |
11770 | tree tmp; | |
11771 | ret = stv_secondarg; | |
11772 | /* Swap TYPE0 with TYPE1 and OP0 with OP1 */ | |
11773 | tmp = type0; type0 = type1; type1 = tmp; | |
11774 | tmp = op0; op0 = op1; op1 = tmp; | |
11775 | } | |
11776 | ||
11777 | if (TREE_CODE (type0) == INTEGER_TYPE | |
11778 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
11779 | { | |
22a75734 | 11780 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
41ed701a | 11781 | { |
11782 | if (complain) | |
11783 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
11784 | "involves truncation", type0, type1); | |
11785 | return stv_error; | |
11786 | } | |
11787 | return ret; | |
11788 | } | |
11789 | else if (!integer_only_op | |
11790 | /* Allow integer --> real conversion if safe. */ | |
11791 | && (TREE_CODE (type0) == REAL_TYPE | |
11792 | || TREE_CODE (type0) == INTEGER_TYPE) | |
11793 | && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1))) | |
11794 | { | |
22a75734 | 11795 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
41ed701a | 11796 | { |
11797 | if (complain) | |
11798 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
11799 | "involves truncation", type0, type1); | |
11800 | return stv_error; | |
11801 | } | |
11802 | return ret; | |
11803 | } | |
11804 | default: | |
11805 | break; | |
11806 | } | |
11807 | ||
11808 | return stv_nothing; | |
11809 | } | |
11810 | ||
ffcdbf9c | 11811 | /* Return true iff ALIGN is an integral constant that is a fundamental |
11812 | alignment, as defined by [basic.align] in the c++-11 | |
11813 | specifications. | |
11814 | ||
11815 | That is: | |
11816 | ||
11817 | [A fundamental alignment is represented by an alignment less than or | |
11818 | equal to the greatest alignment supported by the implementation | |
11819 | in all contexts, which is equal to | |
11820 | alignof(max_align_t)]. */ | |
11821 | ||
11822 | bool | |
11823 | cxx_fundamental_alignment_p (unsigned align) | |
11824 | { | |
11825 | return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node), | |
11826 | TYPE_ALIGN (long_double_type_node))); | |
11827 | } | |
11828 | ||
46da3601 | 11829 | /* Return true if T is a pointer to a zero-sized aggregate. */ |
11830 | ||
11831 | bool | |
11832 | pointer_to_zero_sized_aggr_p (tree t) | |
11833 | { | |
11834 | if (!POINTER_TYPE_P (t)) | |
11835 | return false; | |
11836 | t = TREE_TYPE (t); | |
11837 | return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t))); | |
11838 | } | |
11839 | ||
7bedc3a0 | 11840 | #include "gt-c-family-c-common.h" |