]>
Commit | Line | Data |
---|---|---|
b30f223b | 1 | /* Subroutines shared by all languages that are variants of C. |
5624e564 | 2 | Copyright (C) 1992-2015 Free Software Foundation, Inc. |
b30f223b | 3 | |
1322177d | 4 | This file is part of GCC. |
b30f223b | 5 | |
1322177d LB |
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 | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 9 | version. |
b30f223b | 10 | |
1322177d LB |
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. | |
b30f223b RS |
15 | |
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
b30f223b RS |
19 | |
20 | #include "config.h" | |
670ee920 | 21 | #include "system.h" |
4977bab6 | 22 | #include "coretypes.h" |
b559c810 | 23 | #include "c-common.h" |
4977bab6 | 24 | #include "tm.h" |
d9b2742a | 25 | #include "intl.h" |
b30f223b | 26 | #include "tree.h" |
d8a2d370 DN |
27 | #include "fold-const.h" |
28 | #include "stor-layout.h" | |
29 | #include "calls.h" | |
30 | #include "stringpool.h" | |
31 | #include "attribs.h" | |
32 | #include "varasm.h" | |
33 | #include "trans-mem.h" | |
b30f223b | 34 | #include "flags.h" |
e2af664c | 35 | #include "c-pragma.h" |
61d3ce20 | 36 | #include "c-objc.h" |
7bdb32b9 | 37 | #include "tm_p.h" |
235cfbc4 | 38 | #include "obstack.h" |
c8724862 | 39 | #include "cpplib.h" |
12a68f1f | 40 | #include "target.h" |
677f3fa8 | 41 | #include "common/common-target.h" |
7afff7cf | 42 | #include "langhooks.h" |
d57a4b98 | 43 | #include "tree-inline.h" |
5f1989e6 | 44 | #include "toplev.h" |
148e4216 | 45 | #include "diagnostic.h" |
6de9cd9a DN |
46 | #include "tree-iterator.h" |
47 | #include "hashtab.h" | |
d974312d | 48 | #include "opts.h" |
c582198b AM |
49 | #include "hash-map.h" |
50 | #include "is-a.h" | |
51 | #include "plugin-api.h" | |
52 | #include "vec.h" | |
53 | #include "hash-set.h" | |
54 | #include "machmode.h" | |
55 | #include "hard-reg-set.h" | |
56 | #include "input.h" | |
57 | #include "function.h" | |
58 | #include "ipa-ref.h" | |
ce91e74c | 59 | #include "cgraph.h" |
1f1d5130 | 60 | #include "target-def.h" |
45b0be94 | 61 | #include "gimplify.h" |
807e902e | 62 | #include "wide-int-print.h" |
aa7da51a | 63 | #include "gimple-expr.h" |
cb60f38d | 64 | |
81a75f0f | 65 | cpp_reader *parse_in; /* Declared in c-pragma.h. */ |
c8724862 | 66 | |
7f4edbcb | 67 | /* The following symbols are subsumed in the c_global_trees array, and |
d125d268 | 68 | listed here individually for documentation purposes. |
7f4edbcb BS |
69 | |
70 | INTEGER_TYPE and REAL_TYPE nodes for the standard data types. | |
71 | ||
72 | tree short_integer_type_node; | |
73 | tree long_integer_type_node; | |
74 | tree long_long_integer_type_node; | |
75 | ||
76 | tree short_unsigned_type_node; | |
77 | tree long_unsigned_type_node; | |
78 | tree long_long_unsigned_type_node; | |
79 | ||
de7df9eb JM |
80 | tree truthvalue_type_node; |
81 | tree truthvalue_false_node; | |
82 | tree truthvalue_true_node; | |
7f4edbcb BS |
83 | |
84 | tree ptrdiff_type_node; | |
85 | ||
86 | tree unsigned_char_type_node; | |
87 | tree signed_char_type_node; | |
88 | tree wchar_type_node; | |
7f4edbcb | 89 | |
b6baa67d KVH |
90 | tree char16_type_node; |
91 | tree char32_type_node; | |
92 | ||
7f4edbcb BS |
93 | tree float_type_node; |
94 | tree double_type_node; | |
95 | tree long_double_type_node; | |
96 | ||
97 | tree complex_integer_type_node; | |
98 | tree complex_float_type_node; | |
99 | tree complex_double_type_node; | |
100 | tree complex_long_double_type_node; | |
101 | ||
9a8ce21f JG |
102 | tree dfloat32_type_node; |
103 | tree dfloat64_type_node; | |
104 | tree_dfloat128_type_node; | |
105 | ||
7f4edbcb BS |
106 | tree intQI_type_node; |
107 | tree intHI_type_node; | |
108 | tree intSI_type_node; | |
109 | tree intDI_type_node; | |
110 | tree intTI_type_node; | |
111 | ||
112 | tree unsigned_intQI_type_node; | |
113 | tree unsigned_intHI_type_node; | |
114 | tree unsigned_intSI_type_node; | |
115 | tree unsigned_intDI_type_node; | |
116 | tree unsigned_intTI_type_node; | |
117 | ||
118 | tree widest_integer_literal_type_node; | |
119 | tree widest_unsigned_literal_type_node; | |
120 | ||
121 | Nodes for types `void *' and `const void *'. | |
122 | ||
123 | tree ptr_type_node, const_ptr_type_node; | |
124 | ||
125 | Nodes for types `char *' and `const char *'. | |
126 | ||
127 | tree string_type_node, const_string_type_node; | |
128 | ||
129 | Type `char[SOMENUMBER]'. | |
130 | Used when an array of char is needed and the size is irrelevant. | |
131 | ||
132 | tree char_array_type_node; | |
133 | ||
7f4edbcb BS |
134 | Type `wchar_t[SOMENUMBER]' or something like it. |
135 | Used when a wide string literal is created. | |
136 | ||
137 | tree wchar_array_type_node; | |
138 | ||
b6baa67d KVH |
139 | Type `char16_t[SOMENUMBER]' or something like it. |
140 | Used when a UTF-16 string literal is created. | |
141 | ||
142 | tree char16_array_type_node; | |
143 | ||
144 | Type `char32_t[SOMENUMBER]' or something like it. | |
145 | Used when a UTF-32 string literal is created. | |
146 | ||
147 | tree char32_array_type_node; | |
148 | ||
7f4edbcb BS |
149 | Type `int ()' -- used for implicit declaration of functions. |
150 | ||
151 | tree default_function_type; | |
152 | ||
7f4edbcb BS |
153 | A VOID_TYPE node, packaged in a TREE_LIST. |
154 | ||
155 | tree void_list_node; | |
156 | ||
684d9f3b | 157 | The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__, |
0ba8a114 NS |
158 | and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__ |
159 | VAR_DECLS, but C++ does.) | |
63ad61ed | 160 | |
0ba8a114 | 161 | tree function_name_decl_node; |
684d9f3b | 162 | tree pretty_function_name_decl_node; |
0ba8a114 NS |
163 | tree c99_function_name_decl_node; |
164 | ||
165 | Stack of nested function name VAR_DECLs. | |
35b1a6fa | 166 | |
0ba8a114 | 167 | tree saved_function_name_decls; |
63ad61ed | 168 | |
7f4edbcb BS |
169 | */ |
170 | ||
171 | tree c_global_trees[CTI_MAX]; | |
17211ab5 | 172 | \f |
4078b403 NB |
173 | /* Switches common to the C front ends. */ |
174 | ||
63973df3 NB |
175 | /* Nonzero means don't output line number information. */ |
176 | ||
177 | char flag_no_line_commands; | |
178 | ||
179 | /* Nonzero causes -E output not to be done, but directives such as | |
180 | #define that have side effects are still obeyed. */ | |
181 | ||
182 | char flag_no_output; | |
183 | ||
184 | /* Nonzero means dump macros in some fashion. */ | |
185 | ||
186 | char flag_dump_macros; | |
187 | ||
188 | /* Nonzero means pass #include lines through to the output. */ | |
189 | ||
190 | char flag_dump_includes; | |
191 | ||
c0d578e6 GK |
192 | /* Nonzero means process PCH files while preprocessing. */ |
193 | ||
194 | bool flag_pch_preprocess; | |
195 | ||
17211ab5 GK |
196 | /* The file name to which we should write a precompiled header, or |
197 | NULL if no header will be written in this compile. */ | |
198 | ||
199 | const char *pch_file; | |
200 | ||
3df89291 NB |
201 | /* Nonzero if an ISO standard was selected. It rejects macros in the |
202 | user's namespace. */ | |
203 | int flag_iso; | |
204 | ||
4078b403 NB |
205 | /* C/ObjC language option variables. */ |
206 | ||
207 | ||
4078b403 NB |
208 | /* Nonzero means allow type mismatches in conditional expressions; |
209 | just make their values `void'. */ | |
210 | ||
211 | int flag_cond_mismatch; | |
212 | ||
213 | /* Nonzero means enable C89 Amendment 1 features. */ | |
214 | ||
215 | int flag_isoc94; | |
216 | ||
48b0b196 | 217 | /* Nonzero means use the ISO C99 (or C11) dialect of C. */ |
4078b403 NB |
218 | |
219 | int flag_isoc99; | |
220 | ||
48b0b196 | 221 | /* Nonzero means use the ISO C11 dialect of C. */ |
2778d766 | 222 | |
48b0b196 | 223 | int flag_isoc11; |
2778d766 | 224 | |
6614fd40 | 225 | /* Nonzero means that we have builtin functions, and main is an int. */ |
4078b403 NB |
226 | |
227 | int flag_hosted = 1; | |
228 | ||
4078b403 NB |
229 | |
230 | /* ObjC language option variables. */ | |
231 | ||
232 | ||
4078b403 NB |
233 | /* Tells the compiler that this is a special run. Do not perform any |
234 | compiling, instead we are to test some platform dependent features | |
235 | and output a C header file with appropriate definitions. */ | |
236 | ||
237 | int print_struct_values; | |
238 | ||
fa10beec | 239 | /* Tells the compiler what is the constant string class for ObjC. */ |
4078b403 NB |
240 | |
241 | const char *constant_string_class_name; | |
242 | ||
4078b403 NB |
243 | |
244 | /* C++ language option variables. */ | |
245 | ||
246 | ||
4078b403 NB |
247 | /* Nonzero means generate separate instantiation control files and |
248 | juggle them at link time. */ | |
249 | ||
250 | int flag_use_repository; | |
251 | ||
c1ae8be5 | 252 | /* The C++ dialect being used. C++98 is the default. */ |
966541e3 | 253 | |
c1ae8be5 | 254 | enum cxx_dialect cxx_dialect = cxx98; |
966541e3 | 255 | |
4afe7ad7 | 256 | /* Maximum template instantiation depth. This limit exists to limit the |
3ff60975 | 257 | time it takes to notice excessively recursive template instantiations. |
4078b403 | 258 | |
3ff60975 JM |
259 | The default is lower than the 1024 recommended by the C++0x standard |
260 | because G++ runs out of stack before 1024 with highly recursive template | |
261 | argument deduction substitution (g++.dg/cpp0x/enum11.C). */ | |
262 | ||
263 | int max_tinst_depth = 900; | |
4078b403 | 264 | |
f09f1de5 MM |
265 | /* The elements of `ridpointers' are identifier nodes for the reserved |
266 | type names and storage classes. It is indexed by a RID_... value. */ | |
267 | tree *ridpointers; | |
268 | ||
c2255bc4 | 269 | tree (*make_fname_decl) (location_t, tree, int); |
2ce07e2d | 270 | |
7d882b83 ILT |
271 | /* Nonzero means don't warn about problems that occur when the code is |
272 | executed. */ | |
273 | int c_inhibit_evaluation_warnings; | |
e78a3b42 | 274 | |
5386338c JM |
275 | /* Whether we are building a boolean conversion inside |
276 | convert_for_assignment, or some other late binary operation. If | |
277 | build_binary_op is called for C (from code shared by C and C++) in | |
278 | this case, then the operands have already been folded and the | |
279 | result will not be folded again, so C_MAYBE_CONST_EXPR should not | |
280 | be generated. */ | |
281 | bool in_late_binary_op; | |
282 | ||
148e4216 JM |
283 | /* Whether lexing has been completed, so subsequent preprocessor |
284 | errors should use the compiler's input_location. */ | |
285 | bool done_lexing = false; | |
286 | ||
ec5c56db | 287 | /* Information about how a function name is generated. */ |
0ba8a114 NS |
288 | struct fname_var_t |
289 | { | |
8b60264b KG |
290 | tree *const decl; /* pointer to the VAR_DECL. */ |
291 | const unsigned rid; /* RID number for the identifier. */ | |
292 | const int pretty; /* How pretty is it? */ | |
0ba8a114 NS |
293 | }; |
294 | ||
ec5c56db | 295 | /* The three ways of getting then name of the current function. */ |
0ba8a114 NS |
296 | |
297 | const struct fname_var_t fname_vars[] = | |
298 | { | |
ec5c56db | 299 | /* C99 compliant __func__, must be first. */ |
0ba8a114 | 300 | {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0}, |
ec5c56db | 301 | /* GCC __FUNCTION__ compliant. */ |
0ba8a114 | 302 | {&function_name_decl_node, RID_FUNCTION_NAME, 0}, |
ec5c56db | 303 | /* GCC __PRETTY_FUNCTION__ compliant. */ |
0ba8a114 NS |
304 | {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1}, |
305 | {NULL, 0, 0}, | |
306 | }; | |
307 | ||
c98cd5bf JM |
308 | /* Global visibility options. */ |
309 | struct visibility_flags visibility_options; | |
310 | ||
928c19bb | 311 | static tree c_fully_fold_internal (tree expr, bool, bool *, bool *); |
62e4eb35 | 312 | static tree check_case_value (location_t, tree); |
9d548dfb | 313 | static bool check_case_bounds (location_t, tree, tree, tree *, tree *); |
4724b3de | 314 | |
35b1a6fa AJ |
315 | static tree handle_packed_attribute (tree *, tree, tree, int, bool *); |
316 | static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *); | |
317 | static tree handle_common_attribute (tree *, tree, tree, int, bool *); | |
318 | static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *); | |
52bf96d2 JH |
319 | static tree handle_hot_attribute (tree *, tree, tree, int, bool *); |
320 | static tree handle_cold_attribute (tree *, tree, tree, int, bool *); | |
e664c61c KS |
321 | static tree handle_no_sanitize_address_attribute (tree *, tree, tree, |
322 | int, bool *); | |
77bc5132 JJ |
323 | static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree, |
324 | int, bool *); | |
ce6923c5 MP |
325 | static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int, |
326 | bool *); | |
35b1a6fa | 327 | static tree handle_noinline_attribute (tree *, tree, tree, int, bool *); |
86631ea3 | 328 | static tree handle_noclone_attribute (tree *, tree, tree, int, bool *); |
46a4da10 | 329 | static tree handle_leaf_attribute (tree *, tree, tree, int, bool *); |
35b1a6fa AJ |
330 | static tree handle_always_inline_attribute (tree *, tree, tree, int, |
331 | bool *); | |
d752cfdb JJ |
332 | static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *); |
333 | static tree handle_artificial_attribute (tree *, tree, tree, int, bool *); | |
0691d1d4 | 334 | static tree handle_flatten_attribute (tree *, tree, tree, int, bool *); |
d2af6a68 | 335 | static tree handle_error_attribute (tree *, tree, tree, int, bool *); |
35b1a6fa AJ |
336 | static tree handle_used_attribute (tree *, tree, tree, int, bool *); |
337 | static tree handle_unused_attribute (tree *, tree, tree, int, bool *); | |
ce91e74c JH |
338 | static tree handle_externally_visible_attribute (tree *, tree, tree, int, |
339 | bool *); | |
7861b648 AK |
340 | static tree handle_no_reorder_attribute (tree *, tree, tree, int, |
341 | bool *); | |
35b1a6fa AJ |
342 | static tree handle_const_attribute (tree *, tree, tree, int, bool *); |
343 | static tree handle_transparent_union_attribute (tree *, tree, tree, | |
344 | int, bool *); | |
345 | static tree handle_constructor_attribute (tree *, tree, tree, int, bool *); | |
346 | static tree handle_destructor_attribute (tree *, tree, tree, int, bool *); | |
347 | static tree handle_mode_attribute (tree *, tree, tree, int, bool *); | |
348 | static tree handle_section_attribute (tree *, tree, tree, int, bool *); | |
349 | static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); | |
350 | static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; | |
ba885ec5 NS |
351 | static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *); |
352 | static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *); | |
35b1a6fa | 353 | static tree handle_alias_attribute (tree *, tree, tree, int, bool *); |
a0203ca7 | 354 | static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ; |
35b1a6fa AJ |
355 | static tree handle_visibility_attribute (tree *, tree, tree, int, |
356 | bool *); | |
357 | static tree handle_tls_model_attribute (tree *, tree, tree, int, | |
358 | bool *); | |
359 | static tree handle_no_instrument_function_attribute (tree *, tree, | |
360 | tree, int, bool *); | |
361 | static tree handle_malloc_attribute (tree *, tree, tree, int, bool *); | |
6e9a3221 | 362 | static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *); |
35b1a6fa AJ |
363 | static tree handle_no_limit_stack_attribute (tree *, tree, tree, int, |
364 | bool *); | |
365 | static tree handle_pure_attribute (tree *, tree, tree, int, bool *); | |
0a35513e AH |
366 | static tree handle_tm_attribute (tree *, tree, tree, int, bool *); |
367 | static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *); | |
dcd6de6d | 368 | static tree handle_novops_attribute (tree *, tree, tree, int, bool *); |
35b1a6fa AJ |
369 | static tree handle_deprecated_attribute (tree *, tree, tree, int, |
370 | bool *); | |
371 | static tree handle_vector_size_attribute (tree *, tree, tree, int, | |
372 | bool *); | |
373 | static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *); | |
374 | static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *); | |
375 | static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *); | |
72954a4f JM |
376 | static tree handle_warn_unused_result_attribute (tree *, tree, tree, int, |
377 | bool *); | |
3d091dac | 378 | static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *); |
b5d32c25 | 379 | static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *); |
51bc54a6 | 380 | static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *); |
8fcbce72 JJ |
381 | static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *); |
382 | static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *); | |
5779e713 | 383 | static tree handle_target_attribute (tree *, tree, tree, int, bool *); |
ab442df7 | 384 | static tree handle_optimize_attribute (tree *, tree, tree, int, bool *); |
0a35513e | 385 | static tree ignore_attribute (tree *, tree, tree, int, bool *); |
7458026b | 386 | static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *); |
0b7b376d | 387 | static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *); |
2a99e5e6 | 388 | static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *); |
826cacfe | 389 | static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *); |
acf0174b JJ |
390 | static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int, |
391 | bool *); | |
392 | static tree handle_omp_declare_target_attribute (tree *, tree, tree, int, | |
393 | bool *); | |
976d5a22 | 394 | static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *); |
d5e254e1 IE |
395 | static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *); |
396 | static tree handle_bnd_legacy (tree *, tree, tree, int, bool *); | |
397 | static tree handle_bnd_instrument (tree *, tree, tree, int, bool *); | |
35b1a6fa | 398 | |
94a0dd7b | 399 | static void check_function_nonnull (tree, int, tree *); |
35b1a6fa AJ |
400 | static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); |
401 | static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT); | |
402 | static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); | |
d07605f5 | 403 | static int resort_field_decl_cmp (const void *, const void *); |
b34c7881 | 404 | |
eea1139b ILT |
405 | /* Reserved words. The third field is a mask: keywords are disabled |
406 | if they match the mask. | |
407 | ||
408 | Masks for languages: | |
409 | C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC | |
410 | C --std=c99: D_CXXONLY | D_OBJC | |
411 | ObjC is like C except that D_OBJC and D_CXX_OBJC are not set | |
412 | C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | |
413 | C++ --std=c0x: D_CONLY | D_OBJC | |
414 | ObjC++ is like C++ except that D_OBJC is not set | |
415 | ||
416 | If -fno-asm is used, D_ASM is added to the mask. If | |
417 | -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in | |
418 | C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords. | |
1973201f | 419 | In C with -Wc++-compat, we warn if D_CXXWARN is set. |
eea1139b | 420 | |
1973201f NP |
421 | Note the complication of the D_CXX_OBJC keywords. These are |
422 | reserved words such as 'class'. In C++, 'class' is a reserved | |
423 | word. In Objective-C++ it is too. In Objective-C, it is a | |
424 | reserved word too, but only if it follows an '@' sign. | |
425 | */ | |
eea1139b ILT |
426 | const struct c_common_resword c_common_reswords[] = |
427 | { | |
d19fa6b5 JM |
428 | { "_Alignas", RID_ALIGNAS, D_CONLY }, |
429 | { "_Alignof", RID_ALIGNOF, D_CONLY }, | |
267bac10 | 430 | { "_Atomic", RID_ATOMIC, D_CONLY }, |
eea1139b ILT |
431 | { "_Bool", RID_BOOL, D_CONLY }, |
432 | { "_Complex", RID_COMPLEX, 0 }, | |
939b37da BI |
433 | { "_Cilk_spawn", RID_CILK_SPAWN, 0 }, |
434 | { "_Cilk_sync", RID_CILK_SYNC, 0 }, | |
9a771876 | 435 | { "_Cilk_for", RID_CILK_FOR, 0 }, |
fa5da7de | 436 | { "_Imaginary", RID_IMAGINARY, D_CONLY }, |
eea1139b ILT |
437 | { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT }, |
438 | { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT }, | |
439 | { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT }, | |
440 | { "_Fract", RID_FRACT, D_CONLY | D_EXT }, | |
441 | { "_Accum", RID_ACCUM, D_CONLY | D_EXT }, | |
442 | { "_Sat", RID_SAT, D_CONLY | D_EXT }, | |
32912286 | 443 | { "_Static_assert", RID_STATIC_ASSERT, D_CONLY }, |
bbceee64 | 444 | { "_Noreturn", RID_NORETURN, D_CONLY }, |
433cc7b0 | 445 | { "_Generic", RID_GENERIC, D_CONLY }, |
582d9b50 | 446 | { "_Thread_local", RID_THREAD, D_CONLY }, |
eea1139b ILT |
447 | { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, |
448 | { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, | |
449 | { "__alignof", RID_ALIGNOF, 0 }, | |
450 | { "__alignof__", RID_ALIGNOF, 0 }, | |
451 | { "__asm", RID_ASM, 0 }, | |
452 | { "__asm__", RID_ASM, 0 }, | |
453 | { "__attribute", RID_ATTRIBUTE, 0 }, | |
454 | { "__attribute__", RID_ATTRIBUTE, 0 }, | |
38b7bc7f | 455 | { "__auto_type", RID_AUTO_TYPE, D_CONLY }, |
4daba884 | 456 | { "__bases", RID_BASES, D_CXXONLY }, |
74893f25 RH |
457 | { "__builtin_call_with_static_chain", |
458 | RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY }, | |
eea1139b | 459 | { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, |
d4a83c10 | 460 | { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY }, |
9e1a8dd1 | 461 | { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 }, |
eea1139b ILT |
462 | { "__builtin_offsetof", RID_OFFSETOF, 0 }, |
463 | { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, | |
464 | { "__builtin_va_arg", RID_VA_ARG, 0 }, | |
465 | { "__complex", RID_COMPLEX, 0 }, | |
466 | { "__complex__", RID_COMPLEX, 0 }, | |
467 | { "__const", RID_CONST, 0 }, | |
468 | { "__const__", RID_CONST, 0 }, | |
469 | { "__decltype", RID_DECLTYPE, D_CXXONLY }, | |
4daba884 | 470 | { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY }, |
eea1139b ILT |
471 | { "__extension__", RID_EXTENSION, 0 }, |
472 | { "__func__", RID_C99_FUNCTION_NAME, 0 }, | |
473 | { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY }, | |
474 | { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY }, | |
475 | { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY }, | |
476 | { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY }, | |
477 | { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY }, | |
478 | { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY }, | |
479 | { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY }, | |
480 | { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY }, | |
3c0d13bf PC |
481 | { "__imag", RID_IMAGPART, 0 }, |
482 | { "__imag__", RID_IMAGPART, 0 }, | |
483 | { "__inline", RID_INLINE, 0 }, | |
484 | { "__inline__", RID_INLINE, 0 }, | |
eea1139b ILT |
485 | { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY }, |
486 | { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY }, | |
487 | { "__is_class", RID_IS_CLASS, D_CXXONLY }, | |
eea1139b ILT |
488 | { "__is_empty", RID_IS_EMPTY, D_CXXONLY }, |
489 | { "__is_enum", RID_IS_ENUM, D_CXXONLY }, | |
b3908fcc | 490 | { "__is_final", RID_IS_FINAL, D_CXXONLY }, |
3c0d13bf | 491 | { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY }, |
eea1139b ILT |
492 | { "__is_pod", RID_IS_POD, D_CXXONLY }, |
493 | { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY }, | |
c32097d8 JM |
494 | { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY }, |
495 | { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY }, | |
dd5d5481 JM |
496 | { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY }, |
497 | { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY }, | |
b752325e | 498 | { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY }, |
eea1139b | 499 | { "__is_union", RID_IS_UNION, D_CXXONLY }, |
eea1139b ILT |
500 | { "__label__", RID_LABEL, 0 }, |
501 | { "__null", RID_NULL, 0 }, | |
502 | { "__real", RID_REALPART, 0 }, | |
503 | { "__real__", RID_REALPART, 0 }, | |
504 | { "__restrict", RID_RESTRICT, 0 }, | |
505 | { "__restrict__", RID_RESTRICT, 0 }, | |
506 | { "__signed", RID_SIGNED, 0 }, | |
507 | { "__signed__", RID_SIGNED, 0 }, | |
508 | { "__thread", RID_THREAD, 0 }, | |
0a35513e AH |
509 | { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 }, |
510 | { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 }, | |
511 | { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 }, | |
eea1139b ILT |
512 | { "__typeof", RID_TYPEOF, 0 }, |
513 | { "__typeof__", RID_TYPEOF, 0 }, | |
3c0d13bf | 514 | { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY }, |
eea1139b ILT |
515 | { "__volatile", RID_VOLATILE, 0 }, |
516 | { "__volatile__", RID_VOLATILE, 0 }, | |
e28d52cf | 517 | { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
f4e8a943 | 518 | { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
eea1139b ILT |
519 | { "asm", RID_ASM, D_ASM }, |
520 | { "auto", RID_AUTO, 0 }, | |
03c3034e | 521 | { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN }, |
eea1139b ILT |
522 | { "break", RID_BREAK, 0 }, |
523 | { "case", RID_CASE, 0 }, | |
2696a995 | 524 | { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN }, |
eea1139b | 525 | { "char", RID_CHAR, 0 }, |
2696a995 KG |
526 | { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
527 | { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
528 | { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN }, | |
eea1139b | 529 | { "const", RID_CONST, 0 }, |
7ecbca9d | 530 | { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
eea1139b ILT |
531 | { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN }, |
532 | { "continue", RID_CONTINUE, 0 }, | |
2696a995 | 533 | { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
eea1139b | 534 | { "default", RID_DEFAULT, 0 }, |
2696a995 | 535 | { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN }, |
eea1139b ILT |
536 | { "do", RID_DO, 0 }, |
537 | { "double", RID_DOUBLE, 0 }, | |
538 | { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN }, | |
539 | { "else", RID_ELSE, 0 }, | |
540 | { "enum", RID_ENUM, 0 }, | |
2696a995 KG |
541 | { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN }, |
542 | { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN }, | |
eea1139b | 543 | { "extern", RID_EXTERN, 0 }, |
2696a995 | 544 | { "false", RID_FALSE, D_CXXONLY | D_CXXWARN }, |
eea1139b ILT |
545 | { "float", RID_FLOAT, 0 }, |
546 | { "for", RID_FOR, 0 }, | |
2696a995 | 547 | { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN }, |
eea1139b ILT |
548 | { "goto", RID_GOTO, 0 }, |
549 | { "if", RID_IF, 0 }, | |
550 | { "inline", RID_INLINE, D_EXT89 }, | |
551 | { "int", RID_INT, 0 }, | |
552 | { "long", RID_LONG, 0 }, | |
553 | { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN }, | |
2696a995 KG |
554 | { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN }, |
555 | { "new", RID_NEW, D_CXXONLY | D_CXXWARN }, | |
0a766368 | 556 | { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
14c2101d | 557 | { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
2696a995 KG |
558 | { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN }, |
559 | { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN }, | |
560 | { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN }, | |
561 | { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN }, | |
eea1139b ILT |
562 | { "register", RID_REGISTER, 0 }, |
563 | { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN }, | |
564 | { "restrict", RID_RESTRICT, D_CONLY | D_C99 }, | |
565 | { "return", RID_RETURN, 0 }, | |
566 | { "short", RID_SHORT, 0 }, | |
567 | { "signed", RID_SIGNED, 0 }, | |
568 | { "sizeof", RID_SIZEOF, 0 }, | |
569 | { "static", RID_STATIC, 0 }, | |
570 | { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
571 | { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN }, | |
572 | { "struct", RID_STRUCT, 0 }, | |
573 | { "switch", RID_SWITCH, 0 }, | |
2696a995 KG |
574 | { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN }, |
575 | { "this", RID_THIS, D_CXXONLY | D_CXXWARN }, | |
b1db7f91 | 576 | { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
2696a995 KG |
577 | { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN }, |
578 | { "true", RID_TRUE, D_CXXONLY | D_CXXWARN }, | |
579 | { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN }, | |
eea1139b | 580 | { "typedef", RID_TYPEDEF, 0 }, |
2696a995 KG |
581 | { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN }, |
582 | { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN }, | |
eea1139b ILT |
583 | { "typeof", RID_TYPEOF, D_ASM | D_EXT }, |
584 | { "union", RID_UNION, 0 }, | |
585 | { "unsigned", RID_UNSIGNED, 0 }, | |
2696a995 KG |
586 | { "using", RID_USING, D_CXXONLY | D_CXXWARN }, |
587 | { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN }, | |
eea1139b ILT |
588 | { "void", RID_VOID, 0 }, |
589 | { "volatile", RID_VOLATILE, 0 }, | |
590 | { "wchar_t", RID_WCHAR, D_CXXONLY }, | |
591 | { "while", RID_WHILE, 0 }, | |
592 | /* These Objective-C keywords are recognized only immediately after | |
593 | an '@'. */ | |
594 | { "compatibility_alias", RID_AT_ALIAS, D_OBJC }, | |
595 | { "defs", RID_AT_DEFS, D_OBJC }, | |
596 | { "encode", RID_AT_ENCODE, D_OBJC }, | |
597 | { "end", RID_AT_END, D_OBJC }, | |
598 | { "implementation", RID_AT_IMPLEMENTATION, D_OBJC }, | |
599 | { "interface", RID_AT_INTERFACE, D_OBJC }, | |
600 | { "protocol", RID_AT_PROTOCOL, D_OBJC }, | |
601 | { "selector", RID_AT_SELECTOR, D_OBJC }, | |
602 | { "finally", RID_AT_FINALLY, D_OBJC }, | |
603 | { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC }, | |
92902b1b IS |
604 | { "optional", RID_AT_OPTIONAL, D_OBJC }, |
605 | { "required", RID_AT_REQUIRED, D_OBJC }, | |
668ea4b1 | 606 | { "property", RID_AT_PROPERTY, D_OBJC }, |
c37d8c30 | 607 | { "package", RID_AT_PACKAGE, D_OBJC }, |
da57d1b9 NP |
608 | { "synthesize", RID_AT_SYNTHESIZE, D_OBJC }, |
609 | { "dynamic", RID_AT_DYNAMIC, D_OBJC }, | |
eea1139b ILT |
610 | /* These are recognized only in protocol-qualifier context |
611 | (see above) */ | |
612 | { "bycopy", RID_BYCOPY, D_OBJC }, | |
613 | { "byref", RID_BYREF, D_OBJC }, | |
614 | { "in", RID_IN, D_OBJC }, | |
615 | { "inout", RID_INOUT, D_OBJC }, | |
616 | { "oneway", RID_ONEWAY, D_OBJC }, | |
617 | { "out", RID_OUT, D_OBJC }, | |
668ea4b1 | 618 | /* These are recognized inside a property attribute list */ |
200290f2 NP |
619 | { "assign", RID_ASSIGN, D_OBJC }, |
620 | { "copy", RID_COPY, D_OBJC }, | |
668ea4b1 | 621 | { "getter", RID_GETTER, D_OBJC }, |
200290f2 NP |
622 | { "nonatomic", RID_NONATOMIC, D_OBJC }, |
623 | { "readonly", RID_READONLY, D_OBJC }, | |
624 | { "readwrite", RID_READWRITE, D_OBJC }, | |
625 | { "retain", RID_RETAIN, D_OBJC }, | |
668ea4b1 | 626 | { "setter", RID_SETTER, D_OBJC }, |
eea1139b ILT |
627 | }; |
628 | ||
629 | const unsigned int num_c_common_reswords = | |
630 | sizeof c_common_reswords / sizeof (struct c_common_resword); | |
631 | ||
d5e254e1 IE |
632 | /* Table of machine-independent attributes common to all C-like languages. |
633 | ||
634 | All attributes referencing arguments should be additionally processed | |
635 | in chkp_copy_function_type_adding_bounds for correct instrumentation | |
636 | by Pointer Bounds Checker. | |
637 | Current list of processed common attributes: nonnull. */ | |
349ae713 NB |
638 | const struct attribute_spec c_common_attribute_table[] = |
639 | { | |
62d784f7 KT |
640 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
641 | affects_type_identity } */ | |
349ae713 | 642 | { "packed", 0, 0, false, false, false, |
62d784f7 | 643 | handle_packed_attribute , false}, |
349ae713 | 644 | { "nocommon", 0, 0, true, false, false, |
62d784f7 | 645 | handle_nocommon_attribute, false}, |
349ae713 | 646 | { "common", 0, 0, true, false, false, |
62d784f7 | 647 | handle_common_attribute, false }, |
349ae713 NB |
648 | /* FIXME: logically, noreturn attributes should be listed as |
649 | "false, true, true" and apply to function types. But implementing this | |
650 | would require all the places in the compiler that use TREE_THIS_VOLATILE | |
651 | on a decl to identify non-returning functions to be located and fixed | |
652 | to check the function type instead. */ | |
653 | { "noreturn", 0, 0, true, false, false, | |
62d784f7 | 654 | handle_noreturn_attribute, false }, |
349ae713 | 655 | { "volatile", 0, 0, true, false, false, |
62d784f7 | 656 | handle_noreturn_attribute, false }, |
349ae713 | 657 | { "noinline", 0, 0, true, false, false, |
62d784f7 | 658 | handle_noinline_attribute, false }, |
86631ea3 | 659 | { "noclone", 0, 0, true, false, false, |
62d784f7 | 660 | handle_noclone_attribute, false }, |
46a4da10 | 661 | { "leaf", 0, 0, true, false, false, |
62d784f7 | 662 | handle_leaf_attribute, false }, |
349ae713 | 663 | { "always_inline", 0, 0, true, false, false, |
62d784f7 | 664 | handle_always_inline_attribute, false }, |
4eb7fd83 | 665 | { "gnu_inline", 0, 0, true, false, false, |
62d784f7 | 666 | handle_gnu_inline_attribute, false }, |
d752cfdb | 667 | { "artificial", 0, 0, true, false, false, |
62d784f7 | 668 | handle_artificial_attribute, false }, |
0691d1d4 | 669 | { "flatten", 0, 0, true, false, false, |
62d784f7 | 670 | handle_flatten_attribute, false }, |
349ae713 | 671 | { "used", 0, 0, true, false, false, |
62d784f7 | 672 | handle_used_attribute, false }, |
349ae713 | 673 | { "unused", 0, 0, false, false, false, |
62d784f7 | 674 | handle_unused_attribute, false }, |
ce91e74c | 675 | { "externally_visible", 0, 0, true, false, false, |
62d784f7 | 676 | handle_externally_visible_attribute, false }, |
7861b648 AK |
677 | { "no_reorder", 0, 0, true, false, false, |
678 | handle_no_reorder_attribute, false }, | |
349ae713 NB |
679 | /* The same comments as for noreturn attributes apply to const ones. */ |
680 | { "const", 0, 0, true, false, false, | |
62d784f7 | 681 | handle_const_attribute, false }, |
349ae713 | 682 | { "transparent_union", 0, 0, false, false, false, |
62d784f7 | 683 | handle_transparent_union_attribute, false }, |
fc8600f9 | 684 | { "constructor", 0, 1, true, false, false, |
62d784f7 | 685 | handle_constructor_attribute, false }, |
fc8600f9 | 686 | { "destructor", 0, 1, true, false, false, |
62d784f7 | 687 | handle_destructor_attribute, false }, |
349ae713 | 688 | { "mode", 1, 1, false, true, false, |
62d784f7 | 689 | handle_mode_attribute, false }, |
349ae713 | 690 | { "section", 1, 1, true, false, false, |
62d784f7 | 691 | handle_section_attribute, false }, |
349ae713 | 692 | { "aligned", 0, 1, false, false, false, |
62d784f7 | 693 | handle_aligned_attribute, false }, |
349ae713 | 694 | { "weak", 0, 0, true, false, false, |
62d784f7 | 695 | handle_weak_attribute, false }, |
ba885ec5 | 696 | { "ifunc", 1, 1, true, false, false, |
62d784f7 | 697 | handle_ifunc_attribute, false }, |
349ae713 | 698 | { "alias", 1, 1, true, false, false, |
62d784f7 | 699 | handle_alias_attribute, false }, |
a0203ca7 | 700 | { "weakref", 0, 1, true, false, false, |
62d784f7 | 701 | handle_weakref_attribute, false }, |
349ae713 | 702 | { "no_instrument_function", 0, 0, true, false, false, |
62d784f7 KT |
703 | handle_no_instrument_function_attribute, |
704 | false }, | |
349ae713 | 705 | { "malloc", 0, 0, true, false, false, |
62d784f7 | 706 | handle_malloc_attribute, false }, |
6e9a3221 | 707 | { "returns_twice", 0, 0, true, false, false, |
62d784f7 | 708 | handle_returns_twice_attribute, false }, |
349ae713 | 709 | { "no_stack_limit", 0, 0, true, false, false, |
62d784f7 | 710 | handle_no_limit_stack_attribute, false }, |
349ae713 | 711 | { "pure", 0, 0, true, false, false, |
62d784f7 | 712 | handle_pure_attribute, false }, |
0a35513e AH |
713 | { "transaction_callable", 0, 0, false, true, false, |
714 | handle_tm_attribute, false }, | |
715 | { "transaction_unsafe", 0, 0, false, true, false, | |
716 | handle_tm_attribute, false }, | |
717 | { "transaction_safe", 0, 0, false, true, false, | |
718 | handle_tm_attribute, false }, | |
719 | { "transaction_may_cancel_outer", 0, 0, false, true, false, | |
720 | handle_tm_attribute, false }, | |
721 | /* ??? These two attributes didn't make the transition from the | |
722 | Intel language document to the multi-vendor language document. */ | |
723 | { "transaction_pure", 0, 0, false, true, false, | |
724 | handle_tm_attribute, false }, | |
725 | { "transaction_wrap", 1, 1, true, false, false, | |
726 | handle_tm_wrap_attribute, false }, | |
dcd6de6d ZD |
727 | /* For internal use (marking of builtins) only. The name contains space |
728 | to prevent its usage in source code. */ | |
729 | { "no vops", 0, 0, true, false, false, | |
62d784f7 | 730 | handle_novops_attribute, false }, |
9b86d6bb | 731 | { "deprecated", 0, 1, false, false, false, |
62d784f7 | 732 | handle_deprecated_attribute, false }, |
349ae713 | 733 | { "vector_size", 1, 1, false, true, false, |
62d784f7 | 734 | handle_vector_size_attribute, false }, |
d7afec4b | 735 | { "visibility", 1, 1, false, false, false, |
62d784f7 | 736 | handle_visibility_attribute, false }, |
dce81a1a | 737 | { "tls_model", 1, 1, true, false, false, |
62d784f7 | 738 | handle_tls_model_attribute, false }, |
b34c7881 | 739 | { "nonnull", 0, -1, false, true, true, |
62d784f7 | 740 | handle_nonnull_attribute, false }, |
39f2f3c8 | 741 | { "nothrow", 0, 0, true, false, false, |
62d784f7 KT |
742 | handle_nothrow_attribute, false }, |
743 | { "may_alias", 0, 0, false, true, false, NULL, false }, | |
0bfa5f65 | 744 | { "cleanup", 1, 1, true, false, false, |
62d784f7 | 745 | handle_cleanup_attribute, false }, |
72954a4f | 746 | { "warn_unused_result", 0, 0, false, true, true, |
62d784f7 | 747 | handle_warn_unused_result_attribute, false }, |
254986c7 | 748 | { "sentinel", 0, 1, false, true, true, |
62d784f7 | 749 | handle_sentinel_attribute, false }, |
b5d32c25 KG |
750 | /* For internal use (marking of builtins) only. The name contains space |
751 | to prevent its usage in source code. */ | |
752 | { "type generic", 0, 0, false, true, true, | |
62d784f7 | 753 | handle_type_generic_attribute, false }, |
51bc54a6 | 754 | { "alloc_size", 1, 2, false, true, true, |
62d784f7 | 755 | handle_alloc_size_attribute, false }, |
52bf96d2 | 756 | { "cold", 0, 0, true, false, false, |
62d784f7 | 757 | handle_cold_attribute, false }, |
52bf96d2 | 758 | { "hot", 0, 0, true, false, false, |
62d784f7 | 759 | handle_hot_attribute, false }, |
77bc5132 JJ |
760 | { "no_address_safety_analysis", |
761 | 0, 0, true, false, false, | |
762 | handle_no_address_safety_analysis_attribute, | |
763 | false }, | |
e664c61c KS |
764 | { "no_sanitize_address", 0, 0, true, false, false, |
765 | handle_no_sanitize_address_attribute, | |
766 | false }, | |
ce6923c5 MP |
767 | { "no_sanitize_undefined", 0, 0, true, false, false, |
768 | handle_no_sanitize_undefined_attribute, | |
769 | false }, | |
d2af6a68 | 770 | { "warning", 1, 1, true, false, false, |
62d784f7 | 771 | handle_error_attribute, false }, |
d2af6a68 | 772 | { "error", 1, 1, true, false, false, |
62d784f7 | 773 | handle_error_attribute, false }, |
5779e713 | 774 | { "target", 1, -1, true, false, false, |
62d784f7 | 775 | handle_target_attribute, false }, |
ab442df7 | 776 | { "optimize", 1, -1, true, false, false, |
62d784f7 | 777 | handle_optimize_attribute, false }, |
0a35513e AH |
778 | /* For internal use only. The leading '*' both prevents its usage in |
779 | source code and signals that it may be overridden by machine tables. */ | |
780 | { "*tm regparm", 0, 0, false, true, true, | |
781 | ignore_attribute, false }, | |
7458026b | 782 | { "no_split_stack", 0, 0, true, false, false, |
62d784f7 | 783 | handle_no_split_stack_attribute, false }, |
0b7b376d RG |
784 | /* For internal use (marking of builtins and runtime functions) only. |
785 | The name contains space to prevent its usage in source code. */ | |
786 | { "fn spec", 1, 1, false, true, true, | |
62d784f7 | 787 | handle_fnspec_attribute, false }, |
2a99e5e6 LL |
788 | { "warn_unused", 0, 0, false, false, false, |
789 | handle_warn_unused_attribute, false }, | |
826cacfe MG |
790 | { "returns_nonnull", 0, 0, false, true, true, |
791 | handle_returns_nonnull_attribute, false }, | |
acf0174b JJ |
792 | { "omp declare simd", 0, -1, true, false, false, |
793 | handle_omp_declare_simd_attribute, false }, | |
41958c28 BI |
794 | { "cilk simd function", 0, -1, true, false, false, |
795 | handle_omp_declare_simd_attribute, false }, | |
acf0174b JJ |
796 | { "omp declare target", 0, 0, true, false, false, |
797 | handle_omp_declare_target_attribute, false }, | |
8fcbce72 JJ |
798 | { "alloc_align", 1, 1, false, true, true, |
799 | handle_alloc_align_attribute, false }, | |
800 | { "assume_aligned", 1, 2, false, true, true, | |
801 | handle_assume_aligned_attribute, false }, | |
976d5a22 TT |
802 | { "designated_init", 0, 0, false, true, false, |
803 | handle_designated_init_attribute, false }, | |
d5e254e1 IE |
804 | { "bnd_variable_size", 0, 0, true, false, false, |
805 | handle_bnd_variable_size_attribute, false }, | |
806 | { "bnd_legacy", 0, 0, true, false, false, | |
807 | handle_bnd_legacy, false }, | |
808 | { "bnd_instrument", 0, 0, true, false, false, | |
809 | handle_bnd_instrument, false }, | |
62d784f7 | 810 | { NULL, 0, 0, false, false, false, NULL, false } |
349ae713 NB |
811 | }; |
812 | ||
813 | /* Give the specifications for the format attributes, used by C and all | |
d5e254e1 | 814 | descendants. |
349ae713 | 815 | |
d5e254e1 IE |
816 | All attributes referencing arguments should be additionally processed |
817 | in chkp_copy_function_type_adding_bounds for correct instrumentation | |
818 | by Pointer Bounds Checker. | |
819 | Current list of processed format attributes: format, format_arg. */ | |
349ae713 NB |
820 | const struct attribute_spec c_common_format_attribute_table[] = |
821 | { | |
62d784f7 KT |
822 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
823 | affects_type_identity } */ | |
349ae713 | 824 | { "format", 3, 3, false, true, true, |
62d784f7 | 825 | handle_format_attribute, false }, |
349ae713 | 826 | { "format_arg", 1, 1, false, true, true, |
62d784f7 KT |
827 | handle_format_arg_attribute, false }, |
828 | { NULL, 0, 0, false, false, false, NULL, false } | |
349ae713 NB |
829 | }; |
830 | ||
36c5e70a | 831 | /* Return identifier for address space AS. */ |
3ef0694c | 832 | |
36c5e70a BE |
833 | const char * |
834 | c_addr_space_name (addr_space_t as) | |
835 | { | |
3ef0694c UW |
836 | int rid = RID_FIRST_ADDR_SPACE + as; |
837 | gcc_assert (ridpointers [rid]); | |
838 | return IDENTIFIER_POINTER (ridpointers [rid]); | |
36c5e70a BE |
839 | } |
840 | ||
ec5c56db | 841 | /* Push current bindings for the function name VAR_DECLS. */ |
7da551a2 RS |
842 | |
843 | void | |
35b1a6fa | 844 | start_fname_decls (void) |
7da551a2 | 845 | { |
0ba8a114 NS |
846 | unsigned ix; |
847 | tree saved = NULL_TREE; | |
35b1a6fa | 848 | |
0ba8a114 NS |
849 | for (ix = 0; fname_vars[ix].decl; ix++) |
850 | { | |
851 | tree decl = *fname_vars[ix].decl; | |
7da551a2 | 852 | |
0ba8a114 NS |
853 | if (decl) |
854 | { | |
c62c040f RG |
855 | saved = tree_cons (decl, build_int_cst (integer_type_node, ix), |
856 | saved); | |
0ba8a114 NS |
857 | *fname_vars[ix].decl = NULL_TREE; |
858 | } | |
859 | } | |
860 | if (saved || saved_function_name_decls) | |
861 | /* Normally they'll have been NULL, so only push if we've got a | |
862 | stack, or they are non-NULL. */ | |
863 | saved_function_name_decls = tree_cons (saved, NULL_TREE, | |
864 | saved_function_name_decls); | |
865 | } | |
866 | ||
325c3691 RH |
867 | /* Finish up the current bindings, adding them into the current function's |
868 | statement tree. This must be done _before_ finish_stmt_tree is called. | |
869 | If there is no current function, we must be at file scope and no statements | |
870 | are involved. Pop the previous bindings. */ | |
0ba8a114 NS |
871 | |
872 | void | |
35b1a6fa | 873 | finish_fname_decls (void) |
0ba8a114 NS |
874 | { |
875 | unsigned ix; | |
325c3691 | 876 | tree stmts = NULL_TREE; |
0ba8a114 NS |
877 | tree stack = saved_function_name_decls; |
878 | ||
879 | for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack)) | |
325c3691 | 880 | append_to_statement_list (TREE_VALUE (stack), &stmts); |
35b1a6fa | 881 | |
325c3691 | 882 | if (stmts) |
0ba8a114 | 883 | { |
325c3691 | 884 | tree *bodyp = &DECL_SAVED_TREE (current_function_decl); |
bfaba7a9 | 885 | |
325c3691 RH |
886 | if (TREE_CODE (*bodyp) == BIND_EXPR) |
887 | bodyp = &BIND_EXPR_BODY (*bodyp); | |
6cce57b0 | 888 | |
86ad3aa9 | 889 | append_to_statement_list_force (*bodyp, &stmts); |
325c3691 | 890 | *bodyp = stmts; |
0ba8a114 | 891 | } |
35b1a6fa | 892 | |
0ba8a114 NS |
893 | for (ix = 0; fname_vars[ix].decl; ix++) |
894 | *fname_vars[ix].decl = NULL_TREE; | |
35b1a6fa | 895 | |
0ba8a114 | 896 | if (stack) |
7da551a2 | 897 | { |
ec5c56db | 898 | /* We had saved values, restore them. */ |
0ba8a114 NS |
899 | tree saved; |
900 | ||
901 | for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved)) | |
902 | { | |
903 | tree decl = TREE_PURPOSE (saved); | |
904 | unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved)); | |
35b1a6fa | 905 | |
0ba8a114 NS |
906 | *fname_vars[ix].decl = decl; |
907 | } | |
908 | stack = TREE_CHAIN (stack); | |
7da551a2 | 909 | } |
0ba8a114 NS |
910 | saved_function_name_decls = stack; |
911 | } | |
912 | ||
6cce57b0 | 913 | /* Return the text name of the current function, suitably prettified |
0d0bc036 | 914 | by PRETTY_P. Return string must be freed by caller. */ |
0ba8a114 NS |
915 | |
916 | const char * | |
35b1a6fa | 917 | fname_as_string (int pretty_p) |
0ba8a114 | 918 | { |
47ab33b2 | 919 | const char *name = "top level"; |
0d0bc036 | 920 | char *namep; |
46c2514e TT |
921 | int vrb = 2, len; |
922 | cpp_string cstr = { 0, 0 }, strname; | |
47ab33b2 | 923 | |
3f75a254 | 924 | if (!pretty_p) |
47ab33b2 MA |
925 | { |
926 | name = ""; | |
927 | vrb = 0; | |
928 | } | |
929 | ||
930 | if (current_function_decl) | |
ae2bcd98 | 931 | name = lang_hooks.decl_printable_name (current_function_decl, vrb); |
47ab33b2 | 932 | |
46c2514e | 933 | len = strlen (name) + 3; /* Two for '"'s. One for NULL. */ |
0d0bc036 | 934 | |
46c2514e TT |
935 | namep = XNEWVEC (char, len); |
936 | snprintf (namep, len, "\"%s\"", name); | |
937 | strname.text = (unsigned char *) namep; | |
938 | strname.len = len - 1; | |
0d0bc036 | 939 | |
b6baa67d | 940 | if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING)) |
46c2514e TT |
941 | { |
942 | XDELETEVEC (namep); | |
943 | return (const char *) cstr.text; | |
0d0bc036 | 944 | } |
0d0bc036 AH |
945 | |
946 | return namep; | |
0ba8a114 NS |
947 | } |
948 | ||
0ba8a114 NS |
949 | /* Return the VAR_DECL for a const char array naming the current |
950 | function. If the VAR_DECL has not yet been created, create it | |
951 | now. RID indicates how it should be formatted and IDENTIFIER_NODE | |
952 | ID is its name (unfortunately C and C++ hold the RID values of | |
953 | keywords in different places, so we can't derive RID from ID in | |
3ba09659 AH |
954 | this language independent code. LOC is the location of the |
955 | function. */ | |
0ba8a114 NS |
956 | |
957 | tree | |
3ba09659 | 958 | fname_decl (location_t loc, unsigned int rid, tree id) |
0ba8a114 NS |
959 | { |
960 | unsigned ix; | |
961 | tree decl = NULL_TREE; | |
962 | ||
963 | for (ix = 0; fname_vars[ix].decl; ix++) | |
964 | if (fname_vars[ix].rid == rid) | |
965 | break; | |
966 | ||
967 | decl = *fname_vars[ix].decl; | |
968 | if (!decl) | |
7da551a2 | 969 | { |
8d3e27d1 DJ |
970 | /* If a tree is built here, it would normally have the lineno of |
971 | the current statement. Later this tree will be moved to the | |
972 | beginning of the function and this line number will be wrong. | |
973 | To avoid this problem set the lineno to 0 here; that prevents | |
4b7e68e7 | 974 | it from appearing in the RTL. */ |
325c3691 | 975 | tree stmts; |
3c20847b | 976 | location_t saved_location = input_location; |
3c20847b | 977 | input_location = UNKNOWN_LOCATION; |
35b1a6fa | 978 | |
325c3691 | 979 | stmts = push_stmt_list (); |
c2255bc4 | 980 | decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty); |
325c3691 RH |
981 | stmts = pop_stmt_list (stmts); |
982 | if (!IS_EMPTY_STMT (stmts)) | |
983 | saved_function_name_decls | |
984 | = tree_cons (decl, stmts, saved_function_name_decls); | |
0ba8a114 | 985 | *fname_vars[ix].decl = decl; |
3c20847b | 986 | input_location = saved_location; |
7da551a2 | 987 | } |
0ba8a114 | 988 | if (!ix && !current_function_decl) |
3ba09659 | 989 | pedwarn (loc, 0, "%qD is not defined outside of function scope", decl); |
6cce57b0 | 990 | |
0ba8a114 | 991 | return decl; |
7da551a2 RS |
992 | } |
993 | ||
b84a3874 | 994 | /* Given a STRING_CST, give it a suitable array-of-chars data type. */ |
b30f223b RS |
995 | |
996 | tree | |
35b1a6fa | 997 | fix_string_type (tree value) |
b30f223b | 998 | { |
b84a3874 RH |
999 | int length = TREE_STRING_LENGTH (value); |
1000 | int nchars; | |
c162c75e MA |
1001 | tree e_type, i_type, a_type; |
1002 | ||
b57062ca | 1003 | /* Compute the number of elements, for the array type. */ |
b6baa67d KVH |
1004 | if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value)) |
1005 | { | |
1006 | nchars = length; | |
1007 | e_type = char_type_node; | |
1008 | } | |
1009 | else if (TREE_TYPE (value) == char16_array_type_node) | |
1010 | { | |
1011 | nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT); | |
1012 | e_type = char16_type_node; | |
1013 | } | |
1014 | else if (TREE_TYPE (value) == char32_array_type_node) | |
1015 | { | |
1016 | nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT); | |
1017 | e_type = char32_type_node; | |
1018 | } | |
1019 | else | |
1020 | { | |
1021 | nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT); | |
1022 | e_type = wchar_type_node; | |
1023 | } | |
b30f223b | 1024 | |
89a42ac8 ZW |
1025 | /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous |
1026 | limit in C++98 Annex B is very large (65536) and is not normative, | |
1027 | so we do not diagnose it (warn_overlength_strings is forced off | |
1028 | in c_common_post_options). */ | |
1029 | if (warn_overlength_strings) | |
1030 | { | |
1031 | const int nchars_max = flag_isoc99 ? 4095 : 509; | |
1032 | const int relevant_std = flag_isoc99 ? 99 : 90; | |
1033 | if (nchars - 1 > nchars_max) | |
1034 | /* Translators: The %d after 'ISO C' will be 90 or 99. Do not | |
1035 | separate the %d from the 'C'. 'ISO' should not be | |
1036 | translated, but it may be moved after 'C%d' in languages | |
1037 | where modifiers follow nouns. */ | |
509c9d60 | 1038 | pedwarn (input_location, OPT_Woverlength_strings, |
fcf73884 | 1039 | "string length %qd is greater than the length %qd " |
89a42ac8 ZW |
1040 | "ISO C%d compilers are required to support", |
1041 | nchars - 1, nchars_max, relevant_std); | |
1042 | } | |
1326a48b | 1043 | |
cfb10bd3 GDR |
1044 | /* Create the array type for the string constant. The ISO C++ |
1045 | standard says that a string literal has type `const char[N]' or | |
1046 | `const wchar_t[N]'. We use the same logic when invoked as a C | |
1047 | front-end with -Wwrite-strings. | |
1048 | ??? We should change the type of an expression depending on the | |
1049 | state of a warning flag. We should just be warning -- see how | |
1050 | this is handled in the C++ front-end for the deprecated implicit | |
1051 | conversion from string literals to `char*' or `wchar_t*'. | |
c162c75e MA |
1052 | |
1053 | The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified | |
1054 | array type being the unqualified version of that type. | |
1055 | Therefore, if we are constructing an array of const char, we must | |
1056 | construct the matching unqualified array type first. The C front | |
1057 | end does not require this, but it does no harm, so we do it | |
1058 | unconditionally. */ | |
c62c040f | 1059 | i_type = build_index_type (size_int (nchars - 1)); |
c162c75e | 1060 | a_type = build_array_type (e_type, i_type); |
cfb10bd3 | 1061 | if (c_dialect_cxx() || warn_write_strings) |
46df2823 | 1062 | a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST); |
d9cf7c82 | 1063 | |
c162c75e | 1064 | TREE_TYPE (value) = a_type; |
ccd4c832 | 1065 | TREE_CONSTANT (value) = 1; |
3521b33c | 1066 | TREE_READONLY (value) = 1; |
b30f223b RS |
1067 | TREE_STATIC (value) = 1; |
1068 | return value; | |
1069 | } | |
1070 | \f | |
02614448 ILT |
1071 | /* If DISABLE is true, stop issuing warnings. This is used when |
1072 | parsing code that we know will not be executed. This function may | |
1073 | be called multiple times, and works as a stack. */ | |
1074 | ||
1075 | static void | |
1076 | c_disable_warnings (bool disable) | |
1077 | { | |
1078 | if (disable) | |
1079 | { | |
1080 | ++c_inhibit_evaluation_warnings; | |
1081 | fold_defer_overflow_warnings (); | |
1082 | } | |
1083 | } | |
1084 | ||
1085 | /* If ENABLE is true, reenable issuing warnings. */ | |
1086 | ||
1087 | static void | |
1088 | c_enable_warnings (bool enable) | |
1089 | { | |
1090 | if (enable) | |
1091 | { | |
1092 | --c_inhibit_evaluation_warnings; | |
1093 | fold_undefer_and_ignore_overflow_warnings (); | |
1094 | } | |
1095 | } | |
1096 | ||
928c19bb JM |
1097 | /* Fully fold EXPR, an expression that was not folded (beyond integer |
1098 | constant expressions and null pointer constants) when being built | |
1099 | up. If IN_INIT, this is in a static initializer and certain | |
1100 | changes are made to the folding done. Clear *MAYBE_CONST if | |
1101 | MAYBE_CONST is not NULL and EXPR is definitely not a constant | |
1102 | expression because it contains an evaluated operator (in C99) or an | |
1103 | operator outside of sizeof returning an integer constant (in C90) | |
1104 | not permitted in constant expressions, or because it contains an | |
1105 | evaluated arithmetic overflow. (*MAYBE_CONST should typically be | |
1106 | set to true by callers before calling this function.) Return the | |
1107 | folded expression. Function arguments have already been folded | |
1108 | before calling this function, as have the contents of SAVE_EXPR, | |
1109 | TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and | |
1110 | C_MAYBE_CONST_EXPR. */ | |
1111 | ||
1112 | tree | |
1113 | c_fully_fold (tree expr, bool in_init, bool *maybe_const) | |
1114 | { | |
1115 | tree ret; | |
8ce94e44 | 1116 | tree eptype = NULL_TREE; |
928c19bb JM |
1117 | bool dummy = true; |
1118 | bool maybe_const_itself = true; | |
db3927fb | 1119 | location_t loc = EXPR_LOCATION (expr); |
928c19bb JM |
1120 | |
1121 | /* This function is not relevant to C++ because C++ folds while | |
1122 | parsing, and may need changes to be correct for C++ when C++ | |
1123 | stops folding while parsing. */ | |
1124 | if (c_dialect_cxx ()) | |
1125 | gcc_unreachable (); | |
1126 | ||
1127 | if (!maybe_const) | |
1128 | maybe_const = &dummy; | |
8ce94e44 JM |
1129 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) |
1130 | { | |
1131 | eptype = TREE_TYPE (expr); | |
1132 | expr = TREE_OPERAND (expr, 0); | |
1133 | } | |
928c19bb JM |
1134 | ret = c_fully_fold_internal (expr, in_init, maybe_const, |
1135 | &maybe_const_itself); | |
8ce94e44 | 1136 | if (eptype) |
db3927fb | 1137 | ret = fold_convert_loc (loc, eptype, ret); |
928c19bb JM |
1138 | *maybe_const &= maybe_const_itself; |
1139 | return ret; | |
1140 | } | |
1141 | ||
1142 | /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for | |
1143 | c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands | |
1144 | not permitted, while *MAYBE_CONST_ITSELF is cleared because of | |
1145 | arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from | |
1146 | both evaluated and unevaluated subexpressions while | |
1147 | *MAYBE_CONST_ITSELF is carried from only evaluated | |
1148 | subexpressions). */ | |
1149 | ||
1150 | static tree | |
1151 | c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands, | |
1152 | bool *maybe_const_itself) | |
1153 | { | |
1154 | tree ret = expr; | |
1155 | enum tree_code code = TREE_CODE (expr); | |
1156 | enum tree_code_class kind = TREE_CODE_CLASS (code); | |
1157 | location_t loc = EXPR_LOCATION (expr); | |
1158 | tree op0, op1, op2, op3; | |
1159 | tree orig_op0, orig_op1, orig_op2; | |
1160 | bool op0_const = true, op1_const = true, op2_const = true; | |
1161 | bool op0_const_self = true, op1_const_self = true, op2_const_self = true; | |
1162 | bool nowarning = TREE_NO_WARNING (expr); | |
02614448 | 1163 | bool unused_p; |
928c19bb JM |
1164 | |
1165 | /* This function is not relevant to C++ because C++ folds while | |
1166 | parsing, and may need changes to be correct for C++ when C++ | |
1167 | stops folding while parsing. */ | |
1168 | if (c_dialect_cxx ()) | |
1169 | gcc_unreachable (); | |
1170 | ||
1171 | /* Constants, declarations, statements, errors, SAVE_EXPRs and | |
1172 | anything else not counted as an expression cannot usefully be | |
1173 | folded further at this point. */ | |
1174 | if (!IS_EXPR_CODE_CLASS (kind) | |
1175 | || kind == tcc_statement | |
1176 | || code == SAVE_EXPR) | |
1177 | return expr; | |
1178 | ||
1179 | /* Operands of variable-length expressions (function calls) have | |
1180 | already been folded, as have __builtin_* function calls, and such | |
1181 | expressions cannot occur in constant expressions. */ | |
1182 | if (kind == tcc_vl_exp) | |
1183 | { | |
1184 | *maybe_const_operands = false; | |
1185 | ret = fold (expr); | |
1186 | goto out; | |
1187 | } | |
1188 | ||
1189 | if (code == C_MAYBE_CONST_EXPR) | |
1190 | { | |
1191 | tree pre = C_MAYBE_CONST_EXPR_PRE (expr); | |
1192 | tree inner = C_MAYBE_CONST_EXPR_EXPR (expr); | |
1193 | if (C_MAYBE_CONST_EXPR_NON_CONST (expr)) | |
1194 | *maybe_const_operands = false; | |
1195 | if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr)) | |
1196 | *maybe_const_itself = false; | |
1197 | if (pre && !in_init) | |
1198 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner); | |
1199 | else | |
1200 | ret = inner; | |
1201 | goto out; | |
1202 | } | |
1203 | ||
1204 | /* Assignment, increment, decrement, function call and comma | |
1205 | operators, and statement expressions, cannot occur in constant | |
1206 | expressions if evaluated / outside of sizeof. (Function calls | |
1207 | were handled above, though VA_ARG_EXPR is treated like a function | |
1208 | call here, and statement expressions are handled through | |
1209 | C_MAYBE_CONST_EXPR to avoid folding inside them.) */ | |
1210 | switch (code) | |
1211 | { | |
1212 | case MODIFY_EXPR: | |
1213 | case PREDECREMENT_EXPR: | |
1214 | case PREINCREMENT_EXPR: | |
1215 | case POSTDECREMENT_EXPR: | |
1216 | case POSTINCREMENT_EXPR: | |
1217 | case COMPOUND_EXPR: | |
1218 | *maybe_const_operands = false; | |
1219 | break; | |
1220 | ||
1221 | case VA_ARG_EXPR: | |
1222 | case TARGET_EXPR: | |
1223 | case BIND_EXPR: | |
1224 | case OBJ_TYPE_REF: | |
1225 | *maybe_const_operands = false; | |
1226 | ret = fold (expr); | |
1227 | goto out; | |
1228 | ||
1229 | default: | |
1230 | break; | |
1231 | } | |
1232 | ||
1233 | /* Fold individual tree codes as appropriate. */ | |
1234 | switch (code) | |
1235 | { | |
1236 | case COMPOUND_LITERAL_EXPR: | |
1237 | /* Any non-constancy will have been marked in a containing | |
1238 | C_MAYBE_CONST_EXPR; there is no more folding to do here. */ | |
1239 | goto out; | |
1240 | ||
1241 | case COMPONENT_REF: | |
1242 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1243 | op1 = TREE_OPERAND (expr, 1); | |
1244 | op2 = TREE_OPERAND (expr, 2); | |
1245 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1246 | maybe_const_itself); | |
e5a94231 | 1247 | STRIP_TYPE_NOPS (op0); |
928c19bb JM |
1248 | if (op0 != orig_op0) |
1249 | ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2); | |
1250 | if (ret != expr) | |
1251 | { | |
1252 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1253 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1254 | } | |
1255 | goto out; | |
1256 | ||
1257 | case ARRAY_REF: | |
1258 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1259 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1260 | op2 = TREE_OPERAND (expr, 2); | |
1261 | op3 = TREE_OPERAND (expr, 3); | |
1262 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1263 | maybe_const_itself); | |
e5a94231 | 1264 | STRIP_TYPE_NOPS (op0); |
928c19bb JM |
1265 | op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, |
1266 | maybe_const_itself); | |
e5a94231 | 1267 | STRIP_TYPE_NOPS (op1); |
928c19bb JM |
1268 | op1 = decl_constant_value_for_optimization (op1); |
1269 | if (op0 != orig_op0 || op1 != orig_op1) | |
1270 | ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3); | |
1271 | if (ret != expr) | |
1272 | { | |
1273 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1274 | TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); | |
1275 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1276 | } | |
1277 | ret = fold (ret); | |
1278 | goto out; | |
1279 | ||
1280 | case COMPOUND_EXPR: | |
1281 | case MODIFY_EXPR: | |
1282 | case PREDECREMENT_EXPR: | |
1283 | case PREINCREMENT_EXPR: | |
1284 | case POSTDECREMENT_EXPR: | |
1285 | case POSTINCREMENT_EXPR: | |
1286 | case PLUS_EXPR: | |
1287 | case MINUS_EXPR: | |
1288 | case MULT_EXPR: | |
1289 | case POINTER_PLUS_EXPR: | |
1290 | case TRUNC_DIV_EXPR: | |
1291 | case CEIL_DIV_EXPR: | |
1292 | case FLOOR_DIV_EXPR: | |
1293 | case TRUNC_MOD_EXPR: | |
1294 | case RDIV_EXPR: | |
1295 | case EXACT_DIV_EXPR: | |
1296 | case LSHIFT_EXPR: | |
1297 | case RSHIFT_EXPR: | |
1298 | case BIT_IOR_EXPR: | |
1299 | case BIT_XOR_EXPR: | |
1300 | case BIT_AND_EXPR: | |
1301 | case LT_EXPR: | |
1302 | case LE_EXPR: | |
1303 | case GT_EXPR: | |
1304 | case GE_EXPR: | |
1305 | case EQ_EXPR: | |
1306 | case NE_EXPR: | |
1307 | case COMPLEX_EXPR: | |
1308 | case TRUTH_AND_EXPR: | |
1309 | case TRUTH_OR_EXPR: | |
1310 | case TRUTH_XOR_EXPR: | |
1311 | case UNORDERED_EXPR: | |
1312 | case ORDERED_EXPR: | |
1313 | case UNLT_EXPR: | |
1314 | case UNLE_EXPR: | |
1315 | case UNGT_EXPR: | |
1316 | case UNGE_EXPR: | |
1317 | case UNEQ_EXPR: | |
1318 | /* Binary operations evaluating both arguments (increment and | |
1319 | decrement are binary internally in GCC). */ | |
1320 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1321 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1322 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1323 | maybe_const_itself); | |
e5a94231 | 1324 | STRIP_TYPE_NOPS (op0); |
928c19bb JM |
1325 | if (code != MODIFY_EXPR |
1326 | && code != PREDECREMENT_EXPR | |
1327 | && code != PREINCREMENT_EXPR | |
1328 | && code != POSTDECREMENT_EXPR | |
1329 | && code != POSTINCREMENT_EXPR) | |
1330 | op0 = decl_constant_value_for_optimization (op0); | |
1331 | /* The RHS of a MODIFY_EXPR was fully folded when building that | |
1332 | expression for the sake of conversion warnings. */ | |
1333 | if (code != MODIFY_EXPR) | |
1334 | op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, | |
1335 | maybe_const_itself); | |
e5a94231 | 1336 | STRIP_TYPE_NOPS (op1); |
928c19bb JM |
1337 | op1 = decl_constant_value_for_optimization (op1); |
1338 | if (op0 != orig_op0 || op1 != orig_op1 || in_init) | |
1339 | ret = in_init | |
db3927fb AH |
1340 | ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) |
1341 | : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); | |
928c19bb JM |
1342 | else |
1343 | ret = fold (expr); | |
f5178456 RS |
1344 | if (TREE_OVERFLOW_P (ret) |
1345 | && !TREE_OVERFLOW_P (op0) | |
1346 | && !TREE_OVERFLOW_P (op1)) | |
1347 | overflow_warning (EXPR_LOCATION (expr), ret); | |
cc83c823 JJ |
1348 | if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR) |
1349 | && TREE_CODE (orig_op1) != INTEGER_CST | |
1350 | && TREE_CODE (op1) == INTEGER_CST | |
1351 | && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE | |
1352 | || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE) | |
1353 | && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE | |
1354 | && c_inhibit_evaluation_warnings == 0) | |
1355 | { | |
1356 | if (tree_int_cst_sgn (op1) < 0) | |
1357 | warning_at (loc, 0, (code == LSHIFT_EXPR | |
1358 | ? G_("left shift count is negative") | |
1359 | : G_("right shift count is negative"))); | |
1360 | else if (compare_tree_int (op1, | |
1361 | TYPE_PRECISION (TREE_TYPE (orig_op0))) | |
1362 | >= 0) | |
1363 | warning_at (loc, 0, (code == LSHIFT_EXPR | |
1364 | ? G_("left shift count >= width of type") | |
1365 | : G_("right shift count >= width of type"))); | |
1366 | } | |
4a9a42ab MP |
1367 | if ((code == TRUNC_DIV_EXPR |
1368 | || code == CEIL_DIV_EXPR | |
1369 | || code == FLOOR_DIV_EXPR | |
1370 | || code == EXACT_DIV_EXPR | |
1371 | || code == TRUNC_MOD_EXPR) | |
1372 | && TREE_CODE (orig_op1) != INTEGER_CST | |
1373 | && TREE_CODE (op1) == INTEGER_CST | |
1374 | && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE | |
1375 | || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE) | |
1376 | && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE) | |
1377 | warn_for_div_by_zero (loc, op1); | |
928c19bb JM |
1378 | goto out; |
1379 | ||
1380 | case INDIRECT_REF: | |
1381 | case FIX_TRUNC_EXPR: | |
1382 | case FLOAT_EXPR: | |
1383 | CASE_CONVERT: | |
2ce064c3 | 1384 | case ADDR_SPACE_CONVERT_EXPR: |
982d62f6 | 1385 | case VIEW_CONVERT_EXPR: |
928c19bb JM |
1386 | case NON_LVALUE_EXPR: |
1387 | case NEGATE_EXPR: | |
1388 | case BIT_NOT_EXPR: | |
1389 | case TRUTH_NOT_EXPR: | |
1390 | case ADDR_EXPR: | |
1391 | case CONJ_EXPR: | |
1392 | case REALPART_EXPR: | |
1393 | case IMAGPART_EXPR: | |
1394 | /* Unary operations. */ | |
1395 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1396 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1397 | maybe_const_itself); | |
e5a94231 | 1398 | STRIP_TYPE_NOPS (op0); |
928c19bb JM |
1399 | if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR) |
1400 | op0 = decl_constant_value_for_optimization (op0); | |
693ddb1b EB |
1401 | /* ??? Cope with user tricks that amount to offsetof. The middle-end is |
1402 | not prepared to deal with them if they occur in initializers. */ | |
1403 | if (op0 != orig_op0 | |
1404 | && code == ADDR_EXPR | |
1405 | && (op1 = get_base_address (op0)) != NULL_TREE | |
1406 | && TREE_CODE (op1) == INDIRECT_REF | |
1407 | && TREE_CONSTANT (TREE_OPERAND (op1, 0))) | |
cf9e9959 | 1408 | ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0)); |
693ddb1b | 1409 | else if (op0 != orig_op0 || in_init) |
928c19bb | 1410 | ret = in_init |
db3927fb AH |
1411 | ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0) |
1412 | : fold_build1_loc (loc, code, TREE_TYPE (expr), op0); | |
928c19bb JM |
1413 | else |
1414 | ret = fold (expr); | |
1415 | if (code == INDIRECT_REF | |
1416 | && ret != expr | |
1417 | && TREE_CODE (ret) == INDIRECT_REF) | |
1418 | { | |
1419 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1420 | TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); | |
1421 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1422 | } | |
f5178456 RS |
1423 | switch (code) |
1424 | { | |
1425 | case FIX_TRUNC_EXPR: | |
1426 | case FLOAT_EXPR: | |
1427 | CASE_CONVERT: | |
1428 | /* Don't warn about explicit conversions. We will already | |
1429 | have warned about suspect implicit conversions. */ | |
1430 | break; | |
1431 | ||
1432 | default: | |
1433 | if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0)) | |
1434 | overflow_warning (EXPR_LOCATION (expr), ret); | |
1435 | break; | |
1436 | } | |
928c19bb JM |
1437 | goto out; |
1438 | ||
1439 | case TRUTH_ANDIF_EXPR: | |
1440 | case TRUTH_ORIF_EXPR: | |
1441 | /* Binary operations not necessarily evaluating both | |
1442 | arguments. */ | |
1443 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1444 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1445 | op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); | |
e5a94231 | 1446 | STRIP_TYPE_NOPS (op0); |
f5178456 RS |
1447 | |
1448 | unused_p = (op0 == (code == TRUTH_ANDIF_EXPR | |
1449 | ? truthvalue_false_node | |
1450 | : truthvalue_true_node)); | |
02614448 | 1451 | c_disable_warnings (unused_p); |
928c19bb | 1452 | op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); |
e5a94231 | 1453 | STRIP_TYPE_NOPS (op1); |
02614448 | 1454 | c_enable_warnings (unused_p); |
f5178456 | 1455 | |
928c19bb JM |
1456 | if (op0 != orig_op0 || op1 != orig_op1 || in_init) |
1457 | ret = in_init | |
db3927fb AH |
1458 | ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) |
1459 | : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); | |
928c19bb JM |
1460 | else |
1461 | ret = fold (expr); | |
1462 | *maybe_const_operands &= op0_const; | |
1463 | *maybe_const_itself &= op0_const_self; | |
1464 | if (!(flag_isoc99 | |
1465 | && op0_const | |
1466 | && op0_const_self | |
1467 | && (code == TRUTH_ANDIF_EXPR | |
1468 | ? op0 == truthvalue_false_node | |
1469 | : op0 == truthvalue_true_node))) | |
1470 | *maybe_const_operands &= op1_const; | |
1471 | if (!(op0_const | |
1472 | && op0_const_self | |
1473 | && (code == TRUTH_ANDIF_EXPR | |
1474 | ? op0 == truthvalue_false_node | |
1475 | : op0 == truthvalue_true_node))) | |
1476 | *maybe_const_itself &= op1_const_self; | |
1477 | goto out; | |
1478 | ||
1479 | case COND_EXPR: | |
1480 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1481 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1482 | orig_op2 = op2 = TREE_OPERAND (expr, 2); | |
1483 | op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); | |
f5178456 | 1484 | |
e5a94231 | 1485 | STRIP_TYPE_NOPS (op0); |
02614448 | 1486 | c_disable_warnings (op0 == truthvalue_false_node); |
928c19bb | 1487 | op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); |
e5a94231 | 1488 | STRIP_TYPE_NOPS (op1); |
02614448 | 1489 | c_enable_warnings (op0 == truthvalue_false_node); |
f5178456 | 1490 | |
02614448 | 1491 | c_disable_warnings (op0 == truthvalue_true_node); |
928c19bb | 1492 | op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self); |
e5a94231 | 1493 | STRIP_TYPE_NOPS (op2); |
02614448 | 1494 | c_enable_warnings (op0 == truthvalue_true_node); |
f5178456 | 1495 | |
928c19bb | 1496 | if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2) |
db3927fb | 1497 | ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2); |
928c19bb JM |
1498 | else |
1499 | ret = fold (expr); | |
1500 | *maybe_const_operands &= op0_const; | |
1501 | *maybe_const_itself &= op0_const_self; | |
1502 | if (!(flag_isoc99 | |
1503 | && op0_const | |
1504 | && op0_const_self | |
1505 | && op0 == truthvalue_false_node)) | |
1506 | *maybe_const_operands &= op1_const; | |
1507 | if (!(op0_const | |
1508 | && op0_const_self | |
1509 | && op0 == truthvalue_false_node)) | |
1510 | *maybe_const_itself &= op1_const_self; | |
1511 | if (!(flag_isoc99 | |
1512 | && op0_const | |
1513 | && op0_const_self | |
1514 | && op0 == truthvalue_true_node)) | |
1515 | *maybe_const_operands &= op2_const; | |
1516 | if (!(op0_const | |
1517 | && op0_const_self | |
1518 | && op0 == truthvalue_true_node)) | |
1519 | *maybe_const_itself &= op2_const_self; | |
1520 | goto out; | |
1521 | ||
8ce94e44 JM |
1522 | case EXCESS_PRECISION_EXPR: |
1523 | /* Each case where an operand with excess precision may be | |
1524 | encountered must remove the EXCESS_PRECISION_EXPR around | |
1525 | inner operands and possibly put one around the whole | |
1526 | expression or possibly convert to the semantic type (which | |
1527 | c_fully_fold does); we cannot tell at this stage which is | |
1528 | appropriate in any particular case. */ | |
1529 | gcc_unreachable (); | |
1530 | ||
928c19bb JM |
1531 | default: |
1532 | /* Various codes may appear through folding built-in functions | |
1533 | and their arguments. */ | |
1534 | goto out; | |
1535 | } | |
1536 | ||
1537 | out: | |
1538 | /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks | |
1539 | have been done by this point, so remove them again. */ | |
1540 | nowarning |= TREE_NO_WARNING (ret); | |
1541 | STRIP_TYPE_NOPS (ret); | |
1542 | if (nowarning && !TREE_NO_WARNING (ret)) | |
1543 | { | |
1544 | if (!CAN_HAVE_LOCATION_P (ret)) | |
1545 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
1546 | TREE_NO_WARNING (ret) = 1; | |
1547 | } | |
1548 | if (ret != expr) | |
1549 | protected_set_expr_location (ret, loc); | |
1550 | return ret; | |
1551 | } | |
1552 | ||
1553 | /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type, | |
1554 | return EXP. Otherwise, return either EXP or its known constant | |
1555 | value (if it has one), but return EXP if EXP has mode BLKmode. ??? | |
1556 | Is the BLKmode test appropriate? */ | |
1557 | ||
1558 | tree | |
1559 | decl_constant_value_for_optimization (tree exp) | |
1560 | { | |
1561 | tree ret; | |
1562 | ||
1563 | /* This function is only used by C, for c_fully_fold and other | |
1564 | optimization, and may not be correct for C++. */ | |
1565 | if (c_dialect_cxx ()) | |
1566 | gcc_unreachable (); | |
1567 | ||
1568 | if (!optimize | |
1569 | || TREE_CODE (exp) != VAR_DECL | |
1570 | || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE | |
1571 | || DECL_MODE (exp) == BLKmode) | |
1572 | return exp; | |
1573 | ||
1574 | ret = decl_constant_value (exp); | |
1575 | /* Avoid unwanted tree sharing between the initializer and current | |
1576 | function's body where the tree can be modified e.g. by the | |
1577 | gimplifier. */ | |
1578 | if (ret != exp && TREE_STATIC (exp)) | |
1579 | ret = unshare_expr (ret); | |
1580 | return ret; | |
1581 | } | |
1582 | ||
d74154d5 RS |
1583 | /* Print a warning if a constant expression had overflow in folding. |
1584 | Invoke this function on every expression that the language | |
1585 | requires to be a constant expression. | |
1586 | Note the ANSI C standard says it is erroneous for a | |
1587 | constant expression to overflow. */ | |
96571883 BK |
1588 | |
1589 | void | |
35b1a6fa | 1590 | constant_expression_warning (tree value) |
393eda6a | 1591 | { |
b8698a0f | 1592 | if (warn_overflow && pedantic |
393eda6a MLI |
1593 | && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
1594 | || TREE_CODE (value) == FIXED_CST | |
1595 | || TREE_CODE (value) == VECTOR_CST | |
1596 | || TREE_CODE (value) == COMPLEX_CST) | |
1597 | && TREE_OVERFLOW (value)) | |
509c9d60 | 1598 | pedwarn (input_location, OPT_Woverflow, "overflow in constant expression"); |
393eda6a MLI |
1599 | } |
1600 | ||
1601 | /* The same as above but print an unconditional error. */ | |
1602 | void | |
1603 | constant_expression_error (tree value) | |
96571883 | 1604 | { |
c05f751c | 1605 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
ab22c1fa | 1606 | || TREE_CODE (value) == FIXED_CST |
69ef87e2 | 1607 | || TREE_CODE (value) == VECTOR_CST |
c05f751c | 1608 | || TREE_CODE (value) == COMPLEX_CST) |
393eda6a MLI |
1609 | && TREE_OVERFLOW (value)) |
1610 | error ("overflow in constant expression"); | |
d74154d5 RS |
1611 | } |
1612 | ||
59c0753d MLI |
1613 | /* Print a warning if an expression had overflow in folding and its |
1614 | operands hadn't. | |
1615 | ||
d74154d5 RS |
1616 | Invoke this function on every expression that |
1617 | (1) appears in the source code, and | |
59c0753d | 1618 | (2) is a constant expression that overflowed, and |
d74154d5 | 1619 | (3) is not already checked by convert_and_check; |
59c0753d MLI |
1620 | however, do not invoke this function on operands of explicit casts |
1621 | or when the expression is the result of an operator and any operand | |
1622 | already overflowed. */ | |
d74154d5 RS |
1623 | |
1624 | void | |
c2255bc4 | 1625 | overflow_warning (location_t loc, tree value) |
d74154d5 | 1626 | { |
7d882b83 ILT |
1627 | if (c_inhibit_evaluation_warnings != 0) |
1628 | return; | |
59c0753d MLI |
1629 | |
1630 | switch (TREE_CODE (value)) | |
69ef87e2 | 1631 | { |
59c0753d | 1632 | case INTEGER_CST: |
c2255bc4 | 1633 | warning_at (loc, OPT_Woverflow, "integer overflow in expression"); |
59c0753d | 1634 | break; |
b8698a0f | 1635 | |
59c0753d | 1636 | case REAL_CST: |
c2255bc4 AH |
1637 | warning_at (loc, OPT_Woverflow, |
1638 | "floating point overflow in expression"); | |
59c0753d | 1639 | break; |
b8698a0f | 1640 | |
ab22c1fa | 1641 | case FIXED_CST: |
c2255bc4 | 1642 | warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression"); |
ab22c1fa CF |
1643 | break; |
1644 | ||
59c0753d | 1645 | case VECTOR_CST: |
c2255bc4 | 1646 | warning_at (loc, OPT_Woverflow, "vector overflow in expression"); |
59c0753d | 1647 | break; |
b8698a0f | 1648 | |
59c0753d MLI |
1649 | case COMPLEX_CST: |
1650 | if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) | |
c2255bc4 AH |
1651 | warning_at (loc, OPT_Woverflow, |
1652 | "complex integer overflow in expression"); | |
59c0753d | 1653 | else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) |
c2255bc4 AH |
1654 | warning_at (loc, OPT_Woverflow, |
1655 | "complex floating point overflow in expression"); | |
59c0753d MLI |
1656 | break; |
1657 | ||
1658 | default: | |
1659 | break; | |
69ef87e2 | 1660 | } |
d74154d5 RS |
1661 | } |
1662 | ||
ca409efd MLI |
1663 | /* Warn about uses of logical || / && operator in a context where it |
1664 | is likely that the bitwise equivalent was intended by the | |
1665 | programmer. We have seen an expression in which CODE is a binary | |
a243fb4a MLI |
1666 | operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding |
1667 | had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ | |
63a08740 | 1668 | void |
a243fb4a | 1669 | warn_logical_operator (location_t location, enum tree_code code, tree type, |
b8698a0f | 1670 | enum tree_code code_left, tree op_left, |
ca409efd | 1671 | enum tree_code ARG_UNUSED (code_right), tree op_right) |
63a08740 | 1672 | { |
a243fb4a MLI |
1673 | int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); |
1674 | int in0_p, in1_p, in_p; | |
1675 | tree low0, low1, low, high0, high1, high, lhs, rhs, tem; | |
1676 | bool strict_overflow_p = false; | |
1677 | ||
ca409efd MLI |
1678 | if (code != TRUTH_ANDIF_EXPR |
1679 | && code != TRUTH_AND_EXPR | |
1680 | && code != TRUTH_ORIF_EXPR | |
1681 | && code != TRUTH_OR_EXPR) | |
1682 | return; | |
1683 | ||
1684 | /* Warn if &&/|| are being used in a context where it is | |
1685 | likely that the bitwise equivalent was intended by the | |
1686 | programmer. That is, an expression such as op && MASK | |
1687 | where op should not be any boolean expression, nor a | |
1688 | constant, and mask seems to be a non-boolean integer constant. */ | |
1689 | if (!truth_value_p (code_left) | |
1690 | && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) | |
1691 | && !CONSTANT_CLASS_P (op_left) | |
1692 | && !TREE_NO_WARNING (op_left) | |
1693 | && TREE_CODE (op_right) == INTEGER_CST | |
1694 | && !integer_zerop (op_right) | |
1695 | && !integer_onep (op_right)) | |
63a08740 | 1696 | { |
a243fb4a | 1697 | if (or_op) |
ca409efd MLI |
1698 | warning_at (location, OPT_Wlogical_op, "logical %<or%>" |
1699 | " applied to non-boolean constant"); | |
1700 | else | |
1701 | warning_at (location, OPT_Wlogical_op, "logical %<and%>" | |
1702 | " applied to non-boolean constant"); | |
1703 | TREE_NO_WARNING (op_left) = true; | |
a243fb4a MLI |
1704 | return; |
1705 | } | |
1706 | ||
1707 | /* We do not warn for constants because they are typical of macro | |
1708 | expansions that test for features. */ | |
1709 | if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right)) | |
1710 | return; | |
1711 | ||
1712 | /* This warning only makes sense with logical operands. */ | |
1713 | if (!(truth_value_p (TREE_CODE (op_left)) | |
1714 | || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) | |
1715 | || !(truth_value_p (TREE_CODE (op_right)) | |
1716 | || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) | |
1717 | return; | |
1718 | ||
3c9aabbd MG |
1719 | /* The range computations only work with scalars. */ |
1720 | if (VECTOR_TYPE_P (TREE_TYPE (op_left)) | |
1721 | || VECTOR_TYPE_P (TREE_TYPE (op_right))) | |
1722 | return; | |
a243fb4a | 1723 | |
f2c4a785 MLI |
1724 | /* We first test whether either side separately is trivially true |
1725 | (with OR) or trivially false (with AND). If so, do not warn. | |
1726 | This is a common idiom for testing ranges of data types in | |
1727 | portable code. */ | |
1728 | lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); | |
1729 | if (!lhs) | |
1730 | return; | |
1731 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) | |
a243fb4a MLI |
1732 | lhs = C_MAYBE_CONST_EXPR_EXPR (lhs); |
1733 | ||
f2c4a785 MLI |
1734 | /* If this is an OR operation, invert both sides; now, the result |
1735 | should be always false to get a warning. */ | |
1736 | if (or_op) | |
1737 | in0_p = !in0_p; | |
1738 | ||
1739 | tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); | |
1e537948 | 1740 | if (tem && integer_zerop (tem)) |
f2c4a785 MLI |
1741 | return; |
1742 | ||
1743 | rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); | |
1744 | if (!rhs) | |
1745 | return; | |
1746 | if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) | |
a243fb4a | 1747 | rhs = C_MAYBE_CONST_EXPR_EXPR (rhs); |
b8698a0f | 1748 | |
f2c4a785 MLI |
1749 | /* If this is an OR operation, invert both sides; now, the result |
1750 | should be always false to get a warning. */ | |
a243fb4a | 1751 | if (or_op) |
f2c4a785 | 1752 | in1_p = !in1_p; |
b8698a0f | 1753 | |
f2c4a785 | 1754 | tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); |
1e537948 | 1755 | if (tem && integer_zerop (tem)) |
f2c4a785 MLI |
1756 | return; |
1757 | ||
1758 | /* If both expressions have the same operand, if we can merge the | |
1759 | ranges, and if the range test is always false, then warn. */ | |
1760 | if (operand_equal_p (lhs, rhs, 0) | |
a243fb4a MLI |
1761 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, |
1762 | in1_p, low1, high1) | |
db3927fb | 1763 | && 0 != (tem = build_range_check (UNKNOWN_LOCATION, |
f2c4a785 MLI |
1764 | type, lhs, in_p, low, high)) |
1765 | && integer_zerop (tem)) | |
a243fb4a | 1766 | { |
a243fb4a MLI |
1767 | if (or_op) |
1768 | warning_at (location, OPT_Wlogical_op, | |
1769 | "logical %<or%> " | |
1770 | "of collectively exhaustive tests is always true"); | |
1771 | else | |
1772 | warning_at (location, OPT_Wlogical_op, | |
1773 | "logical %<and%> " | |
1774 | "of mutually exclusive tests is always false"); | |
63a08740 DM |
1775 | } |
1776 | } | |
1777 | ||
742938c9 MP |
1778 | /* Warn about logical not used on the left hand side operand of a comparison. |
1779 | This function assumes that the LHS is inside of TRUTH_NOT_EXPR. | |
59ea0364 | 1780 | Do not warn if RHS is of a boolean type. */ |
742938c9 MP |
1781 | |
1782 | void | |
1783 | warn_logical_not_parentheses (location_t location, enum tree_code code, | |
59ea0364 | 1784 | tree rhs) |
742938c9 | 1785 | { |
59ea0364 MP |
1786 | if (TREE_CODE_CLASS (code) != tcc_comparison |
1787 | || TREE_TYPE (rhs) == NULL_TREE | |
1788 | || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE) | |
742938c9 MP |
1789 | return; |
1790 | ||
1791 | warning_at (location, OPT_Wlogical_not_parentheses, | |
1792 | "logical not is only applied to the left hand side of " | |
1793 | "comparison"); | |
1794 | } | |
63a08740 | 1795 | |
fd4116f4 MLI |
1796 | /* Warn if EXP contains any computations whose results are not used. |
1797 | Return true if a warning is printed; false otherwise. LOCUS is the | |
1798 | (potential) location of the expression. */ | |
1799 | ||
1800 | bool | |
1801 | warn_if_unused_value (const_tree exp, location_t locus) | |
1802 | { | |
1803 | restart: | |
1804 | if (TREE_USED (exp) || TREE_NO_WARNING (exp)) | |
1805 | return false; | |
1806 | ||
1807 | /* Don't warn about void constructs. This includes casting to void, | |
1808 | void function calls, and statement expressions with a final cast | |
1809 | to void. */ | |
1810 | if (VOID_TYPE_P (TREE_TYPE (exp))) | |
1811 | return false; | |
1812 | ||
1813 | if (EXPR_HAS_LOCATION (exp)) | |
1814 | locus = EXPR_LOCATION (exp); | |
1815 | ||
1816 | switch (TREE_CODE (exp)) | |
1817 | { | |
1818 | case PREINCREMENT_EXPR: | |
1819 | case POSTINCREMENT_EXPR: | |
1820 | case PREDECREMENT_EXPR: | |
1821 | case POSTDECREMENT_EXPR: | |
1822 | case MODIFY_EXPR: | |
1823 | case INIT_EXPR: | |
1824 | case TARGET_EXPR: | |
1825 | case CALL_EXPR: | |
1826 | case TRY_CATCH_EXPR: | |
1827 | case WITH_CLEANUP_EXPR: | |
1828 | case EXIT_EXPR: | |
1829 | case VA_ARG_EXPR: | |
1830 | return false; | |
1831 | ||
1832 | case BIND_EXPR: | |
1833 | /* For a binding, warn if no side effect within it. */ | |
1834 | exp = BIND_EXPR_BODY (exp); | |
1835 | goto restart; | |
1836 | ||
1837 | case SAVE_EXPR: | |
1838 | case NON_LVALUE_EXPR: | |
007a787d | 1839 | case NOP_EXPR: |
fd4116f4 MLI |
1840 | exp = TREE_OPERAND (exp, 0); |
1841 | goto restart; | |
1842 | ||
1843 | case TRUTH_ORIF_EXPR: | |
1844 | case TRUTH_ANDIF_EXPR: | |
1845 | /* In && or ||, warn if 2nd operand has no side effect. */ | |
1846 | exp = TREE_OPERAND (exp, 1); | |
1847 | goto restart; | |
1848 | ||
1849 | case COMPOUND_EXPR: | |
1850 | if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) | |
1851 | return true; | |
1852 | /* Let people do `(foo (), 0)' without a warning. */ | |
1853 | if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) | |
1854 | return false; | |
1855 | exp = TREE_OPERAND (exp, 1); | |
1856 | goto restart; | |
1857 | ||
1858 | case COND_EXPR: | |
1859 | /* If this is an expression with side effects, don't warn; this | |
1860 | case commonly appears in macro expansions. */ | |
1861 | if (TREE_SIDE_EFFECTS (exp)) | |
1862 | return false; | |
1863 | goto warn; | |
1864 | ||
1865 | case INDIRECT_REF: | |
1866 | /* Don't warn about automatic dereferencing of references, since | |
1867 | the user cannot control it. */ | |
1868 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE) | |
1869 | { | |
1870 | exp = TREE_OPERAND (exp, 0); | |
1871 | goto restart; | |
1872 | } | |
1873 | /* Fall through. */ | |
1874 | ||
1875 | default: | |
1876 | /* Referencing a volatile value is a side effect, so don't warn. */ | |
1877 | if ((DECL_P (exp) || REFERENCE_CLASS_P (exp)) | |
1878 | && TREE_THIS_VOLATILE (exp)) | |
1879 | return false; | |
1880 | ||
1881 | /* If this is an expression which has no operands, there is no value | |
1882 | to be unused. There are no such language-independent codes, | |
1883 | but front ends may define such. */ | |
1884 | if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0) | |
1885 | return false; | |
1886 | ||
1887 | warn: | |
1888 | return warning_at (locus, OPT_Wunused_value, "value computed is not used"); | |
1889 | } | |
1890 | } | |
1891 | ||
1892 | ||
de9c56a4 RG |
1893 | /* Print a warning about casts that might indicate violation |
1894 | of strict aliasing rules if -Wstrict-aliasing is used and | |
3f0a2a47 DM |
1895 | strict aliasing mode is in effect. OTYPE is the original |
1896 | TREE_TYPE of EXPR, and TYPE the type we're casting to. */ | |
de9c56a4 | 1897 | |
79bedddc | 1898 | bool |
3f0a2a47 | 1899 | strict_aliasing_warning (tree otype, tree type, tree expr) |
de9c56a4 | 1900 | { |
255d3827 RG |
1901 | /* Strip pointer conversion chains and get to the correct original type. */ |
1902 | STRIP_NOPS (expr); | |
1903 | otype = TREE_TYPE (expr); | |
1904 | ||
ac7ee6ad RG |
1905 | if (!(flag_strict_aliasing |
1906 | && POINTER_TYPE_P (type) | |
1907 | && POINTER_TYPE_P (otype) | |
1908 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1909 | /* If the type we are casting to is a ref-all pointer | |
1910 | dereferencing it is always valid. */ | |
1911 | || TYPE_REF_CAN_ALIAS_ALL (type)) | |
79bedddc SR |
1912 | return false; |
1913 | ||
1914 | if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR | |
de9c56a4 | 1915 | && (DECL_P (TREE_OPERAND (expr, 0)) |
79bedddc | 1916 | || handled_component_p (TREE_OPERAND (expr, 0)))) |
de9c56a4 RG |
1917 | { |
1918 | /* Casting the address of an object to non void pointer. Warn | |
1919 | if the cast breaks type based aliasing. */ | |
79bedddc SR |
1920 | if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) |
1921 | { | |
1922 | warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " | |
1923 | "might break strict-aliasing rules"); | |
1924 | return true; | |
1925 | } | |
de9c56a4 RG |
1926 | else |
1927 | { | |
b8698a0f | 1928 | /* warn_strict_aliasing >= 3. This includes the default (3). |
79bedddc | 1929 | Only warn if the cast is dereferenced immediately. */ |
4862826d | 1930 | alias_set_type set1 = |
79bedddc | 1931 | get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); |
4862826d | 1932 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); |
de9c56a4 | 1933 | |
4653cae5 RG |
1934 | if (set1 != set2 && set2 != 0 |
1935 | && (set1 == 0 || !alias_sets_conflict_p (set1, set2))) | |
79bedddc SR |
1936 | { |
1937 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1938 | "pointer will break strict-aliasing rules"); | |
1939 | return true; | |
1940 | } | |
1941 | else if (warn_strict_aliasing == 2 | |
836f7794 | 1942 | && !alias_sets_must_conflict_p (set1, set2)) |
79bedddc SR |
1943 | { |
1944 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1945 | "pointer might break strict-aliasing rules"); | |
1946 | return true; | |
1947 | } | |
de9c56a4 RG |
1948 | } |
1949 | } | |
79bedddc SR |
1950 | else |
1951 | if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) | |
1952 | { | |
1953 | /* At this level, warn for any conversions, even if an address is | |
1954 | not taken in the same statement. This will likely produce many | |
1955 | false positives, but could be useful to pinpoint problems that | |
1956 | are not revealed at higher levels. */ | |
4862826d ILT |
1957 | alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); |
1958 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); | |
1959 | if (!COMPLETE_TYPE_P (type) | |
836f7794 | 1960 | || !alias_sets_must_conflict_p (set1, set2)) |
79bedddc SR |
1961 | { |
1962 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1963 | "pointer might break strict-aliasing rules"); | |
1964 | return true; | |
1965 | } | |
1966 | } | |
1967 | ||
1968 | return false; | |
de9c56a4 RG |
1969 | } |
1970 | ||
1a4049e7 JJ |
1971 | /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with |
1972 | sizeof as last operand of certain builtins. */ | |
1973 | ||
1974 | void | |
3a785c97 | 1975 | sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee, |
9771b263 | 1976 | vec<tree, va_gc> *params, tree *sizeof_arg, |
1a4049e7 JJ |
1977 | bool (*comp_types) (tree, tree)) |
1978 | { | |
1979 | tree type, dest = NULL_TREE, src = NULL_TREE, tem; | |
3a785c97 JJ |
1980 | bool strop = false, cmp = false; |
1981 | unsigned int idx = ~0; | |
1982 | location_t loc; | |
1a4049e7 JJ |
1983 | |
1984 | if (TREE_CODE (callee) != FUNCTION_DECL | |
1985 | || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL | |
9771b263 | 1986 | || vec_safe_length (params) <= 1) |
1a4049e7 JJ |
1987 | return; |
1988 | ||
1a4049e7 JJ |
1989 | switch (DECL_FUNCTION_CODE (callee)) |
1990 | { | |
1991 | case BUILT_IN_STRNCMP: | |
1992 | case BUILT_IN_STRNCASECMP: | |
3a785c97 JJ |
1993 | cmp = true; |
1994 | /* FALLTHRU */ | |
1a4049e7 | 1995 | case BUILT_IN_STRNCPY: |
3a785c97 | 1996 | case BUILT_IN_STRNCPY_CHK: |
1a4049e7 | 1997 | case BUILT_IN_STRNCAT: |
3a785c97 JJ |
1998 | case BUILT_IN_STRNCAT_CHK: |
1999 | case BUILT_IN_STPNCPY: | |
2000 | case BUILT_IN_STPNCPY_CHK: | |
1a4049e7 JJ |
2001 | strop = true; |
2002 | /* FALLTHRU */ | |
2003 | case BUILT_IN_MEMCPY: | |
3a785c97 | 2004 | case BUILT_IN_MEMCPY_CHK: |
1a4049e7 | 2005 | case BUILT_IN_MEMMOVE: |
3a785c97 | 2006 | case BUILT_IN_MEMMOVE_CHK: |
9771b263 | 2007 | if (params->length () < 3) |
3a785c97 | 2008 | return; |
9771b263 DN |
2009 | src = (*params)[1]; |
2010 | dest = (*params)[0]; | |
3a785c97 JJ |
2011 | idx = 2; |
2012 | break; | |
2013 | case BUILT_IN_BCOPY: | |
9771b263 | 2014 | if (params->length () < 3) |
3a785c97 | 2015 | return; |
9771b263 DN |
2016 | src = (*params)[0]; |
2017 | dest = (*params)[1]; | |
3a785c97 JJ |
2018 | idx = 2; |
2019 | break; | |
1a4049e7 | 2020 | case BUILT_IN_MEMCMP: |
3a785c97 | 2021 | case BUILT_IN_BCMP: |
9771b263 | 2022 | if (params->length () < 3) |
1a4049e7 | 2023 | return; |
9771b263 DN |
2024 | src = (*params)[1]; |
2025 | dest = (*params)[0]; | |
3a785c97 JJ |
2026 | idx = 2; |
2027 | cmp = true; | |
1a4049e7 JJ |
2028 | break; |
2029 | case BUILT_IN_MEMSET: | |
3a785c97 | 2030 | case BUILT_IN_MEMSET_CHK: |
9771b263 | 2031 | if (params->length () < 3) |
1a4049e7 | 2032 | return; |
9771b263 | 2033 | dest = (*params)[0]; |
3a785c97 JJ |
2034 | idx = 2; |
2035 | break; | |
2036 | case BUILT_IN_BZERO: | |
9771b263 | 2037 | dest = (*params)[0]; |
3a785c97 | 2038 | idx = 1; |
1a4049e7 JJ |
2039 | break; |
2040 | case BUILT_IN_STRNDUP: | |
9771b263 | 2041 | src = (*params)[0]; |
1a4049e7 | 2042 | strop = true; |
3a785c97 JJ |
2043 | idx = 1; |
2044 | break; | |
2045 | case BUILT_IN_MEMCHR: | |
9771b263 | 2046 | if (params->length () < 3) |
3a785c97 | 2047 | return; |
9771b263 | 2048 | src = (*params)[0]; |
3a785c97 JJ |
2049 | idx = 2; |
2050 | break; | |
2051 | case BUILT_IN_SNPRINTF: | |
2052 | case BUILT_IN_SNPRINTF_CHK: | |
2053 | case BUILT_IN_VSNPRINTF: | |
2054 | case BUILT_IN_VSNPRINTF_CHK: | |
9771b263 | 2055 | dest = (*params)[0]; |
3a785c97 JJ |
2056 | idx = 1; |
2057 | strop = true; | |
1a4049e7 JJ |
2058 | break; |
2059 | default: | |
2060 | break; | |
2061 | } | |
2062 | ||
3a785c97 JJ |
2063 | if (idx >= 3) |
2064 | return; | |
2065 | ||
2066 | if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node) | |
2067 | return; | |
2068 | ||
2069 | type = TYPE_P (sizeof_arg[idx]) | |
2070 | ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]); | |
2071 | if (!POINTER_TYPE_P (type)) | |
2072 | return; | |
2073 | ||
1a4049e7 JJ |
2074 | if (dest |
2075 | && (tem = tree_strip_nop_conversions (dest)) | |
2076 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
2077 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
2078 | return; | |
2079 | ||
2080 | if (src | |
2081 | && (tem = tree_strip_nop_conversions (src)) | |
2082 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
2083 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
2084 | return; | |
2085 | ||
3a785c97 JJ |
2086 | loc = sizeof_arg_loc[idx]; |
2087 | ||
2088 | if (dest && !cmp) | |
1a4049e7 | 2089 | { |
3a785c97 JJ |
2090 | if (!TYPE_P (sizeof_arg[idx]) |
2091 | && operand_equal_p (dest, sizeof_arg[idx], 0) | |
1a4049e7 JJ |
2092 | && comp_types (TREE_TYPE (dest), type)) |
2093 | { | |
3a785c97 | 2094 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
1a4049e7 JJ |
2095 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
2096 | "argument to %<sizeof%> in %qD call is the same " | |
2097 | "expression as the destination; did you mean to " | |
2098 | "remove the addressof?", callee); | |
2099 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2100 | == TYPE_PRECISION (char_type_node)) | |
2101 | || strop) | |
2102 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2103 | "argument to %<sizeof%> in %qD call is the same " | |
2104 | "expression as the destination; did you mean to " | |
2105 | "provide an explicit length?", callee); | |
2106 | else | |
2107 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2108 | "argument to %<sizeof%> in %qD call is the same " | |
2109 | "expression as the destination; did you mean to " | |
2110 | "dereference it?", callee); | |
2111 | return; | |
2112 | } | |
2113 | ||
2114 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
2115 | && !strop | |
2116 | && comp_types (TREE_TYPE (dest), type) | |
2117 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2118 | { | |
2119 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2120 | "argument to %<sizeof%> in %qD call is the same " | |
2121 | "pointer type %qT as the destination; expected %qT " | |
2122 | "or an explicit length", callee, TREE_TYPE (dest), | |
2123 | TREE_TYPE (TREE_TYPE (dest))); | |
2124 | return; | |
2125 | } | |
2126 | } | |
2127 | ||
3a785c97 | 2128 | if (src && !cmp) |
1a4049e7 | 2129 | { |
3a785c97 JJ |
2130 | if (!TYPE_P (sizeof_arg[idx]) |
2131 | && operand_equal_p (src, sizeof_arg[idx], 0) | |
1a4049e7 JJ |
2132 | && comp_types (TREE_TYPE (src), type)) |
2133 | { | |
3a785c97 | 2134 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) |
1a4049e7 JJ |
2135 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, |
2136 | "argument to %<sizeof%> in %qD call is the same " | |
2137 | "expression as the source; did you mean to " | |
2138 | "remove the addressof?", callee); | |
2139 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2140 | == TYPE_PRECISION (char_type_node)) | |
2141 | || strop) | |
2142 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2143 | "argument to %<sizeof%> in %qD call is the same " | |
2144 | "expression as the source; did you mean to " | |
2145 | "provide an explicit length?", callee); | |
2146 | else | |
2147 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2148 | "argument to %<sizeof%> in %qD call is the same " | |
2149 | "expression as the source; did you mean to " | |
2150 | "dereference it?", callee); | |
2151 | return; | |
2152 | } | |
2153 | ||
2154 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
2155 | && !strop | |
2156 | && comp_types (TREE_TYPE (src), type) | |
2157 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2158 | { | |
2159 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2160 | "argument to %<sizeof%> in %qD call is the same " | |
2161 | "pointer type %qT as the source; expected %qT " | |
2162 | "or an explicit length", callee, TREE_TYPE (src), | |
2163 | TREE_TYPE (TREE_TYPE (src))); | |
2164 | return; | |
2165 | } | |
2166 | } | |
3a785c97 JJ |
2167 | |
2168 | if (dest) | |
2169 | { | |
2170 | if (!TYPE_P (sizeof_arg[idx]) | |
2171 | && operand_equal_p (dest, sizeof_arg[idx], 0) | |
2172 | && comp_types (TREE_TYPE (dest), type)) | |
2173 | { | |
2174 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
2175 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2176 | "argument to %<sizeof%> in %qD call is the same " | |
2177 | "expression as the first source; did you mean to " | |
2178 | "remove the addressof?", callee); | |
2179 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2180 | == TYPE_PRECISION (char_type_node)) | |
2181 | || strop) | |
2182 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2183 | "argument to %<sizeof%> in %qD call is the same " | |
2184 | "expression as the first source; did you mean to " | |
2185 | "provide an explicit length?", callee); | |
2186 | else | |
2187 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2188 | "argument to %<sizeof%> in %qD call is the same " | |
2189 | "expression as the first source; did you mean to " | |
2190 | "dereference it?", callee); | |
2191 | return; | |
2192 | } | |
2193 | ||
2194 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
2195 | && !strop | |
2196 | && comp_types (TREE_TYPE (dest), type) | |
2197 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2198 | { | |
2199 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2200 | "argument to %<sizeof%> in %qD call is the same " | |
2201 | "pointer type %qT as the first source; expected %qT " | |
2202 | "or an explicit length", callee, TREE_TYPE (dest), | |
2203 | TREE_TYPE (TREE_TYPE (dest))); | |
2204 | return; | |
2205 | } | |
2206 | } | |
2207 | ||
2208 | if (src) | |
2209 | { | |
2210 | if (!TYPE_P (sizeof_arg[idx]) | |
2211 | && operand_equal_p (src, sizeof_arg[idx], 0) | |
2212 | && comp_types (TREE_TYPE (src), type)) | |
2213 | { | |
2214 | if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop) | |
2215 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2216 | "argument to %<sizeof%> in %qD call is the same " | |
2217 | "expression as the second source; did you mean to " | |
2218 | "remove the addressof?", callee); | |
2219 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
2220 | == TYPE_PRECISION (char_type_node)) | |
2221 | || strop) | |
2222 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2223 | "argument to %<sizeof%> in %qD call is the same " | |
2224 | "expression as the second source; did you mean to " | |
2225 | "provide an explicit length?", callee); | |
2226 | else | |
2227 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2228 | "argument to %<sizeof%> in %qD call is the same " | |
2229 | "expression as the second source; did you mean to " | |
2230 | "dereference it?", callee); | |
2231 | return; | |
2232 | } | |
2233 | ||
2234 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
2235 | && !strop | |
2236 | && comp_types (TREE_TYPE (src), type) | |
2237 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
2238 | { | |
2239 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
2240 | "argument to %<sizeof%> in %qD call is the same " | |
2241 | "pointer type %qT as the second source; expected %qT " | |
2242 | "or an explicit length", callee, TREE_TYPE (src), | |
2243 | TREE_TYPE (TREE_TYPE (src))); | |
2244 | return; | |
2245 | } | |
2246 | } | |
2247 | ||
1a4049e7 JJ |
2248 | } |
2249 | ||
a1e45ff0 DM |
2250 | /* Warn for unlikely, improbable, or stupid DECL declarations |
2251 | of `main'. */ | |
2252 | ||
2253 | void | |
2254 | check_main_parameter_types (tree decl) | |
2255 | { | |
e19a18d4 NF |
2256 | function_args_iterator iter; |
2257 | tree type; | |
a1e45ff0 DM |
2258 | int argct = 0; |
2259 | ||
e19a18d4 NF |
2260 | FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) |
2261 | { | |
2262 | /* XXX void_type_node belies the abstraction. */ | |
2263 | if (type == void_type_node || type == error_mark_node ) | |
2264 | break; | |
2265 | ||
f827930a MP |
2266 | tree t = type; |
2267 | if (TYPE_ATOMIC (t)) | |
2268 | pedwarn (input_location, OPT_Wmain, | |
2269 | "%<_Atomic%>-qualified parameter type %qT of %q+D", | |
2270 | type, decl); | |
2271 | while (POINTER_TYPE_P (t)) | |
2272 | { | |
2273 | t = TREE_TYPE (t); | |
2274 | if (TYPE_ATOMIC (t)) | |
2275 | pedwarn (input_location, OPT_Wmain, | |
2276 | "%<_Atomic%>-qualified parameter type %qT of %q+D", | |
2277 | type, decl); | |
2278 | } | |
2279 | ||
e19a18d4 NF |
2280 | ++argct; |
2281 | switch (argct) | |
2282 | { | |
2283 | case 1: | |
2284 | if (TYPE_MAIN_VARIANT (type) != integer_type_node) | |
2285 | pedwarn (input_location, OPT_Wmain, | |
2286 | "first argument of %q+D should be %<int%>", decl); | |
2287 | break; | |
2288 | ||
2289 | case 2: | |
2290 | if (TREE_CODE (type) != POINTER_TYPE | |
2291 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
2292 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
2293 | != char_type_node)) | |
2294 | pedwarn (input_location, OPT_Wmain, | |
2295 | "second argument of %q+D should be %<char **%>", decl); | |
2296 | break; | |
2297 | ||
2298 | case 3: | |
2299 | if (TREE_CODE (type) != POINTER_TYPE | |
2300 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
2301 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
2302 | != char_type_node)) | |
2303 | pedwarn (input_location, OPT_Wmain, | |
2304 | "third argument of %q+D should probably be " | |
2305 | "%<char **%>", decl); | |
2306 | break; | |
2307 | } | |
2308 | } | |
a1e45ff0 DM |
2309 | |
2310 | /* It is intentional that this message does not mention the third | |
2311 | argument because it's only mentioned in an appendix of the | |
2312 | standard. */ | |
2313 | if (argct > 0 && (argct < 2 || argct > 3)) | |
e19a18d4 NF |
2314 | pedwarn (input_location, OPT_Wmain, |
2315 | "%q+D takes only zero or two arguments", decl); | |
38e514c0 MP |
2316 | |
2317 | if (stdarg_p (TREE_TYPE (decl))) | |
2318 | pedwarn (input_location, OPT_Wmain, | |
2319 | "%q+D declared as variadic function", decl); | |
a1e45ff0 DM |
2320 | } |
2321 | ||
0af94e6f JR |
2322 | /* vector_targets_convertible_p is used for vector pointer types. The |
2323 | callers perform various checks that the qualifiers are satisfactory, | |
2324 | while OTOH vector_targets_convertible_p ignores the number of elements | |
2325 | in the vectors. That's fine with vector pointers as we can consider, | |
2326 | say, a vector of 8 elements as two consecutive vectors of 4 elements, | |
2327 | and that does not require and conversion of the pointer values. | |
2328 | In contrast, vector_types_convertible_p and | |
2329 | vector_types_compatible_elements_p are used for vector value types. */ | |
f83c7f63 DJ |
2330 | /* True if pointers to distinct types T1 and T2 can be converted to |
2331 | each other without an explicit cast. Only returns true for opaque | |
2332 | vector types. */ | |
2333 | bool | |
2334 | vector_targets_convertible_p (const_tree t1, const_tree t2) | |
2335 | { | |
2336 | if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE | |
b6fc2cdb | 2337 | && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
f83c7f63 DJ |
2338 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
2339 | return true; | |
2340 | ||
2341 | return false; | |
2342 | } | |
2343 | ||
0af94e6f JR |
2344 | /* vector_types_convertible_p is used for vector value types. |
2345 | It could in principle call vector_targets_convertible_p as a subroutine, | |
2346 | but then the check for vector type would be duplicated with its callers, | |
2347 | and also the purpose of vector_targets_convertible_p would become | |
2348 | muddled. | |
2349 | Where vector_types_convertible_p returns true, a conversion might still be | |
2350 | needed to make the types match. | |
2351 | In contrast, vector_targets_convertible_p is used for vector pointer | |
2352 | values, and vector_types_compatible_elements_p is used specifically | |
2353 | in the context for binary operators, as a check if use is possible without | |
2354 | conversion. */ | |
00c8e9f6 MS |
2355 | /* True if vector types T1 and T2 can be converted to each other |
2356 | without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2 | |
2357 | can only be converted with -flax-vector-conversions yet that is not | |
2358 | in effect, emit a note telling the user about that option if such | |
2359 | a note has not previously been emitted. */ | |
2360 | bool | |
58f9752a | 2361 | vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note) |
cc27e657 | 2362 | { |
00c8e9f6 | 2363 | static bool emitted_lax_note = false; |
14e765da JM |
2364 | bool convertible_lax; |
2365 | ||
b6fc2cdb | 2366 | if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
14e765da JM |
2367 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
2368 | return true; | |
2369 | ||
2370 | convertible_lax = | |
2371 | (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) | |
2372 | && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || | |
a5e0cd1d | 2373 | TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)) |
14e765da JM |
2374 | && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) |
2375 | == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); | |
00c8e9f6 MS |
2376 | |
2377 | if (!convertible_lax || flag_lax_vector_conversions) | |
2378 | return convertible_lax; | |
2379 | ||
2380 | if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) | |
cf7bc668 | 2381 | && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
00c8e9f6 MS |
2382 | return true; |
2383 | ||
2384 | if (emit_lax_note && !emitted_lax_note) | |
2385 | { | |
2386 | emitted_lax_note = true; | |
1f5b3869 | 2387 | inform (input_location, "use -flax-vector-conversions to permit " |
00c8e9f6 MS |
2388 | "conversions between vectors with differing " |
2389 | "element types or numbers of subparts"); | |
2390 | } | |
2391 | ||
2392 | return false; | |
cc27e657 PB |
2393 | } |
2394 | ||
9e1a8dd1 RR |
2395 | /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes |
2396 | and have vector types, V0 has the same type as V1, and the number of | |
2397 | elements of V0, V1, MASK is the same. | |
2398 | ||
2399 | In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was | |
2400 | called with two arguments. In this case implementation passes the | |
2401 | first argument twice in order to share the same tree code. This fact | |
2402 | could enable the mask-values being twice the vector length. This is | |
2403 | an implementation accident and this semantics is not guaranteed to | |
2404 | the user. */ | |
2405 | tree | |
bedc293e MG |
2406 | c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask, |
2407 | bool complain) | |
9e1a8dd1 RR |
2408 | { |
2409 | tree ret; | |
2410 | bool wrap = true; | |
2411 | bool maybe_const = false; | |
2412 | bool two_arguments = false; | |
2413 | ||
2414 | if (v1 == NULL_TREE) | |
2415 | { | |
2416 | two_arguments = true; | |
2417 | v1 = v0; | |
2418 | } | |
2419 | ||
2420 | if (v0 == error_mark_node || v1 == error_mark_node | |
2421 | || mask == error_mark_node) | |
2422 | return error_mark_node; | |
2423 | ||
2424 | if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE | |
2425 | || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE) | |
2426 | { | |
bedc293e MG |
2427 | if (complain) |
2428 | error_at (loc, "__builtin_shuffle last argument must " | |
2429 | "be an integer vector"); | |
9e1a8dd1 RR |
2430 | return error_mark_node; |
2431 | } | |
2432 | ||
2433 | if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE | |
2434 | || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE) | |
2435 | { | |
bedc293e MG |
2436 | if (complain) |
2437 | error_at (loc, "__builtin_shuffle arguments must be vectors"); | |
9e1a8dd1 RR |
2438 | return error_mark_node; |
2439 | } | |
2440 | ||
2441 | if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1))) | |
2442 | { | |
bedc293e MG |
2443 | if (complain) |
2444 | error_at (loc, "__builtin_shuffle argument vectors must be of " | |
2445 | "the same type"); | |
9e1a8dd1 RR |
2446 | return error_mark_node; |
2447 | } | |
2448 | ||
2449 | if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)) | |
2450 | != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)) | |
2451 | && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)) | |
2452 | != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))) | |
2453 | { | |
bedc293e MG |
2454 | if (complain) |
2455 | error_at (loc, "__builtin_shuffle number of elements of the " | |
2456 | "argument vector(s) and the mask vector should " | |
2457 | "be the same"); | |
9e1a8dd1 RR |
2458 | return error_mark_node; |
2459 | } | |
2460 | ||
2461 | if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0)))) | |
2462 | != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask))))) | |
2463 | { | |
bedc293e MG |
2464 | if (complain) |
2465 | error_at (loc, "__builtin_shuffle argument vector(s) inner type " | |
2466 | "must have the same size as inner type of the mask"); | |
9e1a8dd1 RR |
2467 | return error_mark_node; |
2468 | } | |
2469 | ||
2470 | if (!c_dialect_cxx ()) | |
2471 | { | |
2472 | /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */ | |
2473 | v0 = c_fully_fold (v0, false, &maybe_const); | |
2474 | wrap &= maybe_const; | |
2475 | ||
2476 | if (two_arguments) | |
2477 | v1 = v0 = save_expr (v0); | |
2478 | else | |
2479 | { | |
2480 | v1 = c_fully_fold (v1, false, &maybe_const); | |
2481 | wrap &= maybe_const; | |
2482 | } | |
2483 | ||
2484 | mask = c_fully_fold (mask, false, &maybe_const); | |
2485 | wrap &= maybe_const; | |
2486 | } | |
bedc293e MG |
2487 | else if (two_arguments) |
2488 | v1 = v0 = save_expr (v0); | |
9e1a8dd1 RR |
2489 | |
2490 | ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask); | |
2491 | ||
2492 | if (!c_dialect_cxx () && !wrap) | |
2493 | ret = c_wrap_maybe_const (ret, true); | |
2494 | ||
2495 | return ret; | |
2496 | } | |
2497 | ||
828fb3ba JM |
2498 | /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum |
2499 | to integral type. */ | |
2500 | ||
2501 | static tree | |
2502 | c_common_get_narrower (tree op, int *unsignedp_ptr) | |
2503 | { | |
2504 | op = get_narrower (op, unsignedp_ptr); | |
2505 | ||
2506 | if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE | |
2507 | && ENUM_IS_SCOPED (TREE_TYPE (op))) | |
2508 | { | |
2509 | /* C++0x scoped enumerations don't implicitly convert to integral | |
2510 | type; if we stripped an explicit conversion to a larger type we | |
2511 | need to replace it so common_type will still work. */ | |
21fa2faf RG |
2512 | tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)), |
2513 | TYPE_UNSIGNED (TREE_TYPE (op))); | |
828fb3ba JM |
2514 | op = fold_convert (type, op); |
2515 | } | |
2516 | return op; | |
2517 | } | |
2518 | ||
6715192c MLI |
2519 | /* This is a helper function of build_binary_op. |
2520 | ||
2521 | For certain operations if both args were extended from the same | |
2522 | smaller type, do the arithmetic in that type and then extend. | |
2523 | ||
2524 | BITWISE indicates a bitwise operation. | |
2525 | For them, this optimization is safe only if | |
2526 | both args are zero-extended or both are sign-extended. | |
2527 | Otherwise, we might change the result. | |
2528 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
b8698a0f | 2529 | but calculated in (unsigned short) it would be (unsigned short)-1. |
6715192c | 2530 | */ |
828fb3ba JM |
2531 | tree |
2532 | shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise) | |
6715192c MLI |
2533 | { |
2534 | int unsigned0, unsigned1; | |
2535 | tree arg0, arg1; | |
2536 | int uns; | |
2537 | tree type; | |
2538 | ||
2539 | /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents | |
2540 | excessive narrowing when we call get_narrower below. For | |
2541 | example, suppose that OP0 is of unsigned int extended | |
2542 | from signed char and that RESULT_TYPE is long long int. | |
2543 | If we explicitly cast OP0 to RESULT_TYPE, OP0 would look | |
2544 | like | |
b8698a0f | 2545 | |
6715192c MLI |
2546 | (long long int) (unsigned int) signed_char |
2547 | ||
2548 | which get_narrower would narrow down to | |
b8698a0f | 2549 | |
6715192c | 2550 | (unsigned int) signed char |
b8698a0f | 2551 | |
6715192c MLI |
2552 | If we do not cast OP0 first, get_narrower would return |
2553 | signed_char, which is inconsistent with the case of the | |
2554 | explicit cast. */ | |
2555 | op0 = convert (result_type, op0); | |
2556 | op1 = convert (result_type, op1); | |
2557 | ||
828fb3ba JM |
2558 | arg0 = c_common_get_narrower (op0, &unsigned0); |
2559 | arg1 = c_common_get_narrower (op1, &unsigned1); | |
6715192c MLI |
2560 | |
2561 | /* UNS is 1 if the operation to be done is an unsigned one. */ | |
2562 | uns = TYPE_UNSIGNED (result_type); | |
2563 | ||
2564 | /* Handle the case that OP0 (or OP1) does not *contain* a conversion | |
2565 | but it *requires* conversion to FINAL_TYPE. */ | |
b8698a0f | 2566 | |
6715192c MLI |
2567 | if ((TYPE_PRECISION (TREE_TYPE (op0)) |
2568 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2569 | && TREE_TYPE (op0) != result_type) | |
2570 | unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); | |
2571 | if ((TYPE_PRECISION (TREE_TYPE (op1)) | |
2572 | == TYPE_PRECISION (TREE_TYPE (arg1))) | |
2573 | && TREE_TYPE (op1) != result_type) | |
2574 | unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
b8698a0f | 2575 | |
6715192c | 2576 | /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ |
b8698a0f | 2577 | |
6715192c MLI |
2578 | /* For bitwise operations, signedness of nominal type |
2579 | does not matter. Consider only how operands were extended. */ | |
2580 | if (bitwise) | |
2581 | uns = unsigned0; | |
b8698a0f | 2582 | |
6715192c MLI |
2583 | /* Note that in all three cases below we refrain from optimizing |
2584 | an unsigned operation on sign-extended args. | |
2585 | That would not be valid. */ | |
b8698a0f | 2586 | |
6715192c MLI |
2587 | /* Both args variable: if both extended in same way |
2588 | from same width, do it in that width. | |
2589 | Do it unsigned if args were zero-extended. */ | |
2590 | if ((TYPE_PRECISION (TREE_TYPE (arg0)) | |
2591 | < TYPE_PRECISION (result_type)) | |
2592 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2593 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2594 | && unsigned0 == unsigned1 | |
2595 | && (unsigned0 || !uns)) | |
2596 | return c_common_signed_or_unsigned_type | |
2597 | (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); | |
2598 | ||
2599 | else if (TREE_CODE (arg0) == INTEGER_CST | |
2600 | && (unsigned1 || !uns) | |
2601 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2602 | < TYPE_PRECISION (result_type)) | |
2603 | && (type | |
2604 | = c_common_signed_or_unsigned_type (unsigned1, | |
2605 | TREE_TYPE (arg1))) | |
2606 | && !POINTER_TYPE_P (type) | |
2607 | && int_fits_type_p (arg0, type)) | |
2608 | return type; | |
2609 | ||
2610 | else if (TREE_CODE (arg1) == INTEGER_CST | |
2611 | && (unsigned0 || !uns) | |
2612 | && (TYPE_PRECISION (TREE_TYPE (arg0)) | |
2613 | < TYPE_PRECISION (result_type)) | |
2614 | && (type | |
2615 | = c_common_signed_or_unsigned_type (unsigned0, | |
2616 | TREE_TYPE (arg0))) | |
2617 | && !POINTER_TYPE_P (type) | |
2618 | && int_fits_type_p (arg1, type)) | |
2619 | return type; | |
2620 | ||
2621 | return result_type; | |
2622 | } | |
2623 | ||
68fca595 | 2624 | /* Checks if expression EXPR of real/integer type cannot be converted |
49b0aa18 | 2625 | to the real/integer type TYPE. Function returns non-zero when: |
68fca595 MP |
2626 | * EXPR is a constant which cannot be exactly converted to TYPE. |
2627 | * EXPR is not a constant and size of EXPR's type > than size of TYPE, | |
0e3a99ae | 2628 | for EXPR type and TYPE being both integers or both real. |
68fca595 MP |
2629 | * EXPR is not a constant of real type and TYPE is an integer. |
2630 | * EXPR is not a constant of integer type which cannot be | |
2631 | exactly converted to real type. | |
0e3a99ae | 2632 | Function allows conversions between types of different signedness and |
49b0aa18 JC |
2633 | can return SAFE_CONVERSION (zero) in that case. Function can produce |
2634 | signedness warnings if PRODUCE_WARNS is true. */ | |
68fca595 | 2635 | |
49b0aa18 | 2636 | enum conversion_safety |
68fca595 | 2637 | unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns) |
422c3a54 | 2638 | { |
49b0aa18 | 2639 | enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */ |
374035cb | 2640 | tree expr_type = TREE_TYPE (expr); |
68fca595 | 2641 | loc = expansion_point_location_if_in_system_header (loc); |
422c3a54 | 2642 | |
0e3a99ae | 2643 | if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST) |
0011dedb | 2644 | { |
422c3a54 | 2645 | /* Warn for real constant that is not an exact integer converted |
0e3a99ae | 2646 | to integer type. */ |
374035cb | 2647 | if (TREE_CODE (expr_type) == REAL_TYPE |
0e3a99ae AS |
2648 | && TREE_CODE (type) == INTEGER_TYPE) |
2649 | { | |
2650 | if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) | |
49b0aa18 | 2651 | give_warning = UNSAFE_REAL; |
0e3a99ae | 2652 | } |
91c41804 | 2653 | /* Warn for an integer constant that does not fit into integer type. */ |
374035cb | 2654 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
0e3a99ae AS |
2655 | && TREE_CODE (type) == INTEGER_TYPE |
2656 | && !int_fits_type_p (expr, type)) | |
2657 | { | |
2658 | if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type) | |
374035cb | 2659 | && tree_int_cst_sgn (expr) < 0) |
0e3a99ae AS |
2660 | { |
2661 | if (produce_warns) | |
2662 | warning_at (loc, OPT_Wsign_conversion, "negative integer" | |
2663 | " implicitly converted to unsigned type"); | |
2664 | } | |
2665 | else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type)) | |
2666 | { | |
2667 | if (produce_warns) | |
2668 | warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned" | |
2669 | " constant value to negative integer"); | |
2670 | } | |
7060db96 | 2671 | else |
49b0aa18 | 2672 | give_warning = UNSAFE_OTHER; |
0e3a99ae | 2673 | } |
422c3a54 | 2674 | else if (TREE_CODE (type) == REAL_TYPE) |
0e3a99ae AS |
2675 | { |
2676 | /* Warn for an integer constant that does not fit into real type. */ | |
2677 | if (TREE_CODE (expr_type) == INTEGER_TYPE) | |
2678 | { | |
2679 | REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); | |
2680 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
49b0aa18 | 2681 | give_warning = UNSAFE_REAL; |
0e3a99ae AS |
2682 | } |
2683 | /* Warn for a real constant that does not fit into a smaller | |
2684 | real type. */ | |
2685 | else if (TREE_CODE (expr_type) == REAL_TYPE | |
2686 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
2687 | { | |
2688 | REAL_VALUE_TYPE a = TREE_REAL_CST (expr); | |
2689 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
49b0aa18 | 2690 | give_warning = UNSAFE_REAL; |
0e3a99ae AS |
2691 | } |
2692 | } | |
2693 | } | |
2694 | else | |
2695 | { | |
422c3a54 | 2696 | /* Warn for real types converted to integer types. */ |
6715192c | 2697 | if (TREE_CODE (expr_type) == REAL_TYPE |
0e3a99ae | 2698 | && TREE_CODE (type) == INTEGER_TYPE) |
49b0aa18 | 2699 | give_warning = UNSAFE_REAL; |
422c3a54 | 2700 | |
6715192c | 2701 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
0e3a99ae AS |
2702 | && TREE_CODE (type) == INTEGER_TYPE) |
2703 | { | |
cfdaefec | 2704 | /* Don't warn about unsigned char y = 0xff, x = (int) y; */ |
c00e8b06 | 2705 | expr = get_unwidened (expr, 0); |
6715192c | 2706 | expr_type = TREE_TYPE (expr); |
cfdaefec | 2707 | |
6715192c | 2708 | /* Don't warn for short y; short x = ((int)y & 0xff); */ |
b8698a0f | 2709 | if (TREE_CODE (expr) == BIT_AND_EXPR |
0e3a99ae | 2710 | || TREE_CODE (expr) == BIT_IOR_EXPR |
6715192c MLI |
2711 | || TREE_CODE (expr) == BIT_XOR_EXPR) |
2712 | { | |
374035cb MLI |
2713 | /* If both args were extended from a shortest type, |
2714 | use that type if that is safe. */ | |
b8698a0f L |
2715 | expr_type = shorten_binary_op (expr_type, |
2716 | TREE_OPERAND (expr, 0), | |
2717 | TREE_OPERAND (expr, 1), | |
6715192c MLI |
2718 | /* bitwise */1); |
2719 | ||
6715192c MLI |
2720 | if (TREE_CODE (expr) == BIT_AND_EXPR) |
2721 | { | |
2722 | tree op0 = TREE_OPERAND (expr, 0); | |
2723 | tree op1 = TREE_OPERAND (expr, 1); | |
9c591bd0 MLI |
2724 | bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
2725 | bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
2726 | ||
2727 | /* If one of the operands is a non-negative constant | |
2728 | that fits in the target type, then the type of the | |
2729 | other operand does not matter. */ | |
6715192c MLI |
2730 | if ((TREE_CODE (op0) == INTEGER_CST |
2731 | && int_fits_type_p (op0, c_common_signed_type (type)) | |
2732 | && int_fits_type_p (op0, c_common_unsigned_type (type))) | |
2733 | || (TREE_CODE (op1) == INTEGER_CST | |
374035cb | 2734 | && int_fits_type_p (op1, c_common_signed_type (type)) |
b8698a0f | 2735 | && int_fits_type_p (op1, |
374035cb | 2736 | c_common_unsigned_type (type)))) |
49b0aa18 | 2737 | return SAFE_CONVERSION; |
9c591bd0 MLI |
2738 | /* If constant is unsigned and fits in the target |
2739 | type, then the result will also fit. */ | |
2740 | else if ((TREE_CODE (op0) == INTEGER_CST | |
b8698a0f | 2741 | && unsigned0 |
9c591bd0 MLI |
2742 | && int_fits_type_p (op0, type)) |
2743 | || (TREE_CODE (op1) == INTEGER_CST | |
2744 | && unsigned1 | |
2745 | && int_fits_type_p (op1, type))) | |
49b0aa18 | 2746 | return SAFE_CONVERSION; |
6715192c MLI |
2747 | } |
2748 | } | |
0e3a99ae | 2749 | /* Warn for integer types converted to smaller integer types. */ |
b8698a0f | 2750 | if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
49b0aa18 | 2751 | give_warning = UNSAFE_OTHER; |
7060db96 MLI |
2752 | |
2753 | /* When they are the same width but different signedness, | |
2754 | then the value may change. */ | |
0e3a99ae | 2755 | else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type) |
6715192c | 2756 | && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type)) |
7060db96 MLI |
2757 | /* Even when converted to a bigger type, if the type is |
2758 | unsigned but expr is signed, then negative values | |
2759 | will be changed. */ | |
0e3a99ae AS |
2760 | || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type))) |
2761 | && produce_warns) | |
6312e84d MLI |
2762 | warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT " |
2763 | "may change the sign of the result", | |
2764 | type, expr_type); | |
0e3a99ae | 2765 | } |
422c3a54 MLI |
2766 | |
2767 | /* Warn for integer types converted to real types if and only if | |
0e3a99ae AS |
2768 | all the range of values of the integer type cannot be |
2769 | represented by the real type. */ | |
6715192c | 2770 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
0e3a99ae AS |
2771 | && TREE_CODE (type) == REAL_TYPE) |
2772 | { | |
58076e21 | 2773 | tree type_low_bound, type_high_bound; |
0e3a99ae | 2774 | REAL_VALUE_TYPE real_low_bound, real_high_bound; |
58076e21 MLI |
2775 | |
2776 | /* Don't warn about char y = 0xff; float x = (int) y; */ | |
2777 | expr = get_unwidened (expr, 0); | |
2778 | expr_type = TREE_TYPE (expr); | |
2779 | ||
0e3a99ae AS |
2780 | type_low_bound = TYPE_MIN_VALUE (expr_type); |
2781 | type_high_bound = TYPE_MAX_VALUE (expr_type); | |
2782 | real_low_bound = real_value_from_int_cst (0, type_low_bound); | |
2783 | real_high_bound = real_value_from_int_cst (0, type_high_bound); | |
422c3a54 | 2784 | |
0e3a99ae AS |
2785 | if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound) |
2786 | || !exact_real_truncate (TYPE_MODE (type), &real_high_bound)) | |
49b0aa18 | 2787 | give_warning = UNSAFE_OTHER; |
0e3a99ae | 2788 | } |
422c3a54 MLI |
2789 | |
2790 | /* Warn for real types converted to smaller real types. */ | |
6715192c | 2791 | else if (TREE_CODE (expr_type) == REAL_TYPE |
0e3a99ae AS |
2792 | && TREE_CODE (type) == REAL_TYPE |
2793 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
49b0aa18 | 2794 | give_warning = UNSAFE_REAL; |
0e3a99ae AS |
2795 | } |
2796 | ||
2797 | return give_warning; | |
2798 | } | |
2799 | ||
2800 | /* Warns if the conversion of EXPR to TYPE may alter a value. | |
2801 | This is a helper function for warnings_for_convert_and_check. */ | |
2802 | ||
2803 | static void | |
68fca595 | 2804 | conversion_warning (location_t loc, tree type, tree expr) |
0e3a99ae | 2805 | { |
0e3a99ae | 2806 | tree expr_type = TREE_TYPE (expr); |
49b0aa18 | 2807 | enum conversion_safety conversion_kind; |
422c3a54 | 2808 | |
49b0aa18 | 2809 | if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion) |
0e3a99ae | 2810 | return; |
422c3a54 | 2811 | |
66f20604 MP |
2812 | /* This may happen, because for LHS op= RHS we preevaluate |
2813 | RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which | |
2814 | means we could no longer see the code of the EXPR. */ | |
2815 | if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR) | |
2816 | expr = C_MAYBE_CONST_EXPR_EXPR (expr); | |
2817 | if (TREE_CODE (expr) == SAVE_EXPR) | |
2818 | expr = TREE_OPERAND (expr, 0); | |
2819 | ||
0e3a99ae AS |
2820 | switch (TREE_CODE (expr)) |
2821 | { | |
2822 | case EQ_EXPR: | |
2823 | case NE_EXPR: | |
2824 | case LE_EXPR: | |
2825 | case GE_EXPR: | |
2826 | case LT_EXPR: | |
2827 | case GT_EXPR: | |
2828 | case TRUTH_ANDIF_EXPR: | |
2829 | case TRUTH_ORIF_EXPR: | |
2830 | case TRUTH_AND_EXPR: | |
2831 | case TRUTH_OR_EXPR: | |
2832 | case TRUTH_XOR_EXPR: | |
2833 | case TRUTH_NOT_EXPR: | |
2834 | /* Conversion from boolean to a signed:1 bit-field (which only | |
2835 | can hold the values 0 and -1) doesn't lose information - but | |
2836 | it does change the value. */ | |
2837 | if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) | |
2838 | warning_at (loc, OPT_Wconversion, | |
2839 | "conversion to %qT from boolean expression", type); | |
2840 | return; | |
2841 | ||
2842 | case REAL_CST: | |
2843 | case INTEGER_CST: | |
68fca595 | 2844 | conversion_kind = unsafe_conversion_p (loc, type, expr, true); |
49b0aa18 JC |
2845 | if (conversion_kind == UNSAFE_REAL) |
2846 | warning_at (loc, OPT_Wfloat_conversion, | |
2847 | "conversion to %qT alters %qT constant value", | |
2848 | type, expr_type); | |
2849 | else if (conversion_kind) | |
0e3a99ae AS |
2850 | warning_at (loc, OPT_Wconversion, |
2851 | "conversion to %qT alters %qT constant value", | |
2852 | type, expr_type); | |
2853 | return; | |
2854 | ||
2855 | case COND_EXPR: | |
2856 | { | |
3f46d6a5 MLI |
2857 | /* In case of COND_EXPR, we do not care about the type of |
2858 | COND_EXPR, only about the conversion of each operand. */ | |
2859 | tree op1 = TREE_OPERAND (expr, 1); | |
2860 | tree op2 = TREE_OPERAND (expr, 2); | |
2861 | ||
68fca595 MP |
2862 | conversion_warning (loc, type, op1); |
2863 | conversion_warning (loc, type, op2); | |
3f46d6a5 | 2864 | return; |
0e3a99ae AS |
2865 | } |
2866 | ||
2867 | default: /* 'expr' is not a constant. */ | |
68fca595 | 2868 | conversion_kind = unsafe_conversion_p (loc, type, expr, true); |
49b0aa18 JC |
2869 | if (conversion_kind == UNSAFE_REAL) |
2870 | warning_at (loc, OPT_Wfloat_conversion, | |
2871 | "conversion to %qT from %qT may alter its value", | |
2872 | type, expr_type); | |
2873 | else if (conversion_kind) | |
0e3a99ae | 2874 | warning_at (loc, OPT_Wconversion, |
6312e84d MLI |
2875 | "conversion to %qT from %qT may alter its value", |
2876 | type, expr_type); | |
422c3a54 MLI |
2877 | } |
2878 | } | |
2879 | ||
07231d4f MLI |
2880 | /* Produce warnings after a conversion. RESULT is the result of |
2881 | converting EXPR to TYPE. This is a helper function for | |
2882 | convert_and_check and cp_convert_and_check. */ | |
d74154d5 | 2883 | |
07231d4f | 2884 | void |
68fca595 MP |
2885 | warnings_for_convert_and_check (location_t loc, tree type, tree expr, |
2886 | tree result) | |
d74154d5 | 2887 | { |
68fca595 | 2888 | loc = expansion_point_location_if_in_system_header (loc); |
5a3c9cf2 | 2889 | |
91c41804 RS |
2890 | if (TREE_CODE (expr) == INTEGER_CST |
2891 | && (TREE_CODE (type) == INTEGER_TYPE | |
2892 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
2893 | && !int_fits_type_p (expr, type)) | |
2894 | { | |
422c3a54 MLI |
2895 | /* Do not diagnose overflow in a constant expression merely |
2896 | because a conversion overflowed. */ | |
91c41804 | 2897 | if (TREE_OVERFLOW (result)) |
d95787e6 RS |
2898 | TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); |
2899 | ||
91c41804 | 2900 | if (TYPE_UNSIGNED (type)) |
422c3a54 | 2901 | { |
91c41804 RS |
2902 | /* This detects cases like converting -129 or 256 to |
2903 | unsigned char. */ | |
2904 | if (!int_fits_type_p (expr, c_common_signed_type (type))) | |
5a3c9cf2 PC |
2905 | warning_at (loc, OPT_Woverflow, |
2906 | "large integer implicitly truncated to unsigned type"); | |
7060db96 | 2907 | else |
68fca595 | 2908 | conversion_warning (loc, type, expr); |
91c41804 | 2909 | } |
b8698a0f | 2910 | else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) |
68fca595 | 2911 | warning_at (loc, OPT_Woverflow, |
f73fe417 MLI |
2912 | "overflow in implicit constant conversion"); |
2913 | /* No warning for converting 0x80000000 to int. */ | |
2914 | else if (pedantic | |
2915 | && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE | |
2916 | || TYPE_PRECISION (TREE_TYPE (expr)) | |
2917 | != TYPE_PRECISION (type))) | |
5a3c9cf2 PC |
2918 | warning_at (loc, OPT_Woverflow, |
2919 | "overflow in implicit constant conversion"); | |
f73fe417 | 2920 | |
7060db96 | 2921 | else |
68fca595 | 2922 | conversion_warning (loc, type, expr); |
d74154d5 | 2923 | } |
ab22c1fa CF |
2924 | else if ((TREE_CODE (result) == INTEGER_CST |
2925 | || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) | |
5a3c9cf2 PC |
2926 | warning_at (loc, OPT_Woverflow, |
2927 | "overflow in implicit constant conversion"); | |
7060db96 | 2928 | else |
68fca595 | 2929 | conversion_warning (loc, type, expr); |
07231d4f MLI |
2930 | } |
2931 | ||
2932 | ||
2933 | /* Convert EXPR to TYPE, warning about conversion problems with constants. | |
2934 | Invoke this function on every expression that is converted implicitly, | |
2935 | i.e. because of language rules and not because of an explicit cast. */ | |
2936 | ||
2937 | tree | |
68fca595 | 2938 | convert_and_check (location_t loc, tree type, tree expr) |
07231d4f MLI |
2939 | { |
2940 | tree result; | |
8ce94e44 JM |
2941 | tree expr_for_warning; |
2942 | ||
2943 | /* Convert from a value with possible excess precision rather than | |
2944 | via the semantic type, but do not warn about values not fitting | |
2945 | exactly in the semantic type. */ | |
2946 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
2947 | { | |
2948 | tree orig_type = TREE_TYPE (expr); | |
2949 | expr = TREE_OPERAND (expr, 0); | |
2950 | expr_for_warning = convert (orig_type, expr); | |
2951 | if (orig_type == type) | |
2952 | return expr_for_warning; | |
2953 | } | |
2954 | else | |
2955 | expr_for_warning = expr; | |
07231d4f MLI |
2956 | |
2957 | if (TREE_TYPE (expr) == type) | |
2958 | return expr; | |
b8698a0f | 2959 | |
07231d4f MLI |
2960 | result = convert (type, expr); |
2961 | ||
7d882b83 ILT |
2962 | if (c_inhibit_evaluation_warnings == 0 |
2963 | && !TREE_OVERFLOW_P (expr) | |
2964 | && result != error_mark_node) | |
68fca595 | 2965 | warnings_for_convert_and_check (loc, type, expr_for_warning, result); |
07231d4f | 2966 | |
91c41804 | 2967 | return result; |
96571883 BK |
2968 | } |
2969 | \f | |
235cfbc4 BS |
2970 | /* A node in a list that describes references to variables (EXPR), which are |
2971 | either read accesses if WRITER is zero, or write accesses, in which case | |
2972 | WRITER is the parent of EXPR. */ | |
2973 | struct tlist | |
2974 | { | |
2975 | struct tlist *next; | |
2976 | tree expr, writer; | |
2977 | }; | |
2978 | ||
2979 | /* Used to implement a cache the results of a call to verify_tree. We only | |
2980 | use this for SAVE_EXPRs. */ | |
2981 | struct tlist_cache | |
2982 | { | |
2983 | struct tlist_cache *next; | |
2984 | struct tlist *cache_before_sp; | |
2985 | struct tlist *cache_after_sp; | |
2986 | tree expr; | |
2683ed8d BS |
2987 | }; |
2988 | ||
235cfbc4 BS |
2989 | /* Obstack to use when allocating tlist structures, and corresponding |
2990 | firstobj. */ | |
2991 | static struct obstack tlist_obstack; | |
2992 | static char *tlist_firstobj = 0; | |
2993 | ||
2994 | /* Keep track of the identifiers we've warned about, so we can avoid duplicate | |
2995 | warnings. */ | |
2996 | static struct tlist *warned_ids; | |
2997 | /* SAVE_EXPRs need special treatment. We process them only once and then | |
2998 | cache the results. */ | |
2999 | static struct tlist_cache *save_expr_cache; | |
3000 | ||
35b1a6fa AJ |
3001 | static void add_tlist (struct tlist **, struct tlist *, tree, int); |
3002 | static void merge_tlist (struct tlist **, struct tlist *, int); | |
3003 | static void verify_tree (tree, struct tlist **, struct tlist **, tree); | |
3004 | static int warning_candidate_p (tree); | |
1e4ae551 | 3005 | static bool candidate_equal_p (const_tree, const_tree); |
35b1a6fa AJ |
3006 | static void warn_for_collisions (struct tlist *); |
3007 | static void warn_for_collisions_1 (tree, tree, struct tlist *, int); | |
3008 | static struct tlist *new_tlist (struct tlist *, tree, tree); | |
2683ed8d | 3009 | |
235cfbc4 BS |
3010 | /* Create a new struct tlist and fill in its fields. */ |
3011 | static struct tlist * | |
35b1a6fa | 3012 | new_tlist (struct tlist *next, tree t, tree writer) |
235cfbc4 BS |
3013 | { |
3014 | struct tlist *l; | |
5d038c4c | 3015 | l = XOBNEW (&tlist_obstack, struct tlist); |
235cfbc4 BS |
3016 | l->next = next; |
3017 | l->expr = t; | |
3018 | l->writer = writer; | |
3019 | return l; | |
3020 | } | |
3021 | ||
3022 | /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER | |
3023 | is nonnull, we ignore any node we find which has a writer equal to it. */ | |
3024 | ||
3025 | static void | |
35b1a6fa | 3026 | add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy) |
235cfbc4 BS |
3027 | { |
3028 | while (add) | |
3029 | { | |
3030 | struct tlist *next = add->next; | |
3f75a254 | 3031 | if (!copy) |
235cfbc4 | 3032 | add->next = *to; |
1e4ae551 | 3033 | if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer)) |
235cfbc4 BS |
3034 | *to = copy ? new_tlist (*to, add->expr, add->writer) : add; |
3035 | add = next; | |
3036 | } | |
3037 | } | |
3038 | ||
3039 | /* Merge the nodes of ADD into TO. This merging process is done so that for | |
3040 | each variable that already exists in TO, no new node is added; however if | |
3041 | there is a write access recorded in ADD, and an occurrence on TO is only | |
3042 | a read access, then the occurrence in TO will be modified to record the | |
3043 | write. */ | |
2683ed8d BS |
3044 | |
3045 | static void | |
35b1a6fa | 3046 | merge_tlist (struct tlist **to, struct tlist *add, int copy) |
235cfbc4 BS |
3047 | { |
3048 | struct tlist **end = to; | |
3049 | ||
3050 | while (*end) | |
3051 | end = &(*end)->next; | |
3052 | ||
3053 | while (add) | |
3054 | { | |
3055 | int found = 0; | |
3056 | struct tlist *tmp2; | |
3057 | struct tlist *next = add->next; | |
3058 | ||
3059 | for (tmp2 = *to; tmp2; tmp2 = tmp2->next) | |
1e4ae551 | 3060 | if (candidate_equal_p (tmp2->expr, add->expr)) |
235cfbc4 BS |
3061 | { |
3062 | found = 1; | |
3f75a254 | 3063 | if (!tmp2->writer) |
235cfbc4 BS |
3064 | tmp2->writer = add->writer; |
3065 | } | |
3f75a254 | 3066 | if (!found) |
235cfbc4 | 3067 | { |
c2bf53a1 | 3068 | *end = copy ? new_tlist (NULL, add->expr, add->writer) : add; |
235cfbc4 BS |
3069 | end = &(*end)->next; |
3070 | *end = 0; | |
3071 | } | |
3072 | add = next; | |
3073 | } | |
3074 | } | |
3075 | ||
3076 | /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable | |
3077 | references in list LIST conflict with it, excluding reads if ONLY writers | |
3078 | is nonzero. */ | |
3079 | ||
3080 | static void | |
35b1a6fa AJ |
3081 | warn_for_collisions_1 (tree written, tree writer, struct tlist *list, |
3082 | int only_writes) | |
235cfbc4 BS |
3083 | { |
3084 | struct tlist *tmp; | |
3085 | ||
3086 | /* Avoid duplicate warnings. */ | |
3087 | for (tmp = warned_ids; tmp; tmp = tmp->next) | |
1e4ae551 | 3088 | if (candidate_equal_p (tmp->expr, written)) |
235cfbc4 BS |
3089 | return; |
3090 | ||
3091 | while (list) | |
3092 | { | |
1e4ae551 MLI |
3093 | if (candidate_equal_p (list->expr, written) |
3094 | && !candidate_equal_p (list->writer, writer) | |
3095 | && (!only_writes || list->writer)) | |
235cfbc4 BS |
3096 | { |
3097 | warned_ids = new_tlist (warned_ids, written, NULL_TREE); | |
8400e75e | 3098 | warning_at (EXPR_LOC_OR_LOC (writer, input_location), |
ca085fd7 MLI |
3099 | OPT_Wsequence_point, "operation on %qE may be undefined", |
3100 | list->expr); | |
235cfbc4 BS |
3101 | } |
3102 | list = list->next; | |
3103 | } | |
3104 | } | |
3105 | ||
3106 | /* Given a list LIST of references to variables, find whether any of these | |
3107 | can cause conflicts due to missing sequence points. */ | |
3108 | ||
3109 | static void | |
35b1a6fa | 3110 | warn_for_collisions (struct tlist *list) |
235cfbc4 BS |
3111 | { |
3112 | struct tlist *tmp; | |
35b1a6fa | 3113 | |
235cfbc4 BS |
3114 | for (tmp = list; tmp; tmp = tmp->next) |
3115 | { | |
3116 | if (tmp->writer) | |
3117 | warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); | |
3118 | } | |
3119 | } | |
3120 | ||
684d9f3b | 3121 | /* Return nonzero if X is a tree that can be verified by the sequence point |
235cfbc4 BS |
3122 | warnings. */ |
3123 | static int | |
35b1a6fa | 3124 | warning_candidate_p (tree x) |
2683ed8d | 3125 | { |
07078664 JJ |
3126 | if (DECL_P (x) && DECL_ARTIFICIAL (x)) |
3127 | return 0; | |
3128 | ||
92e948a8 NF |
3129 | if (TREE_CODE (x) == BLOCK) |
3130 | return 0; | |
3131 | ||
07078664 | 3132 | /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c |
1e4ae551 | 3133 | (lvalue_p) crash on TRY/CATCH. */ |
07078664 JJ |
3134 | if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x))) |
3135 | return 0; | |
3136 | ||
3137 | if (!lvalue_p (x)) | |
3138 | return 0; | |
3139 | ||
3140 | /* No point to track non-const calls, they will never satisfy | |
3141 | operand_equal_p. */ | |
3142 | if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0) | |
3143 | return 0; | |
3144 | ||
3145 | if (TREE_CODE (x) == STRING_CST) | |
3146 | return 0; | |
3147 | ||
3148 | return 1; | |
1e4ae551 MLI |
3149 | } |
3150 | ||
3151 | /* Return nonzero if X and Y appear to be the same candidate (or NULL) */ | |
3152 | static bool | |
3153 | candidate_equal_p (const_tree x, const_tree y) | |
3154 | { | |
3155 | return (x == y) || (x && y && operand_equal_p (x, y, 0)); | |
235cfbc4 | 3156 | } |
2683ed8d | 3157 | |
235cfbc4 BS |
3158 | /* Walk the tree X, and record accesses to variables. If X is written by the |
3159 | parent tree, WRITER is the parent. | |
3160 | We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this | |
3161 | expression or its only operand forces a sequence point, then everything up | |
3162 | to the sequence point is stored in PBEFORE_SP. Everything else gets stored | |
3163 | in PNO_SP. | |
3164 | Once we return, we will have emitted warnings if any subexpression before | |
3165 | such a sequence point could be undefined. On a higher level, however, the | |
3166 | sequence point may not be relevant, and we'll merge the two lists. | |
3167 | ||
3168 | Example: (b++, a) + b; | |
3169 | The call that processes the COMPOUND_EXPR will store the increment of B | |
3170 | in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that | |
3171 | processes the PLUS_EXPR will need to merge the two lists so that | |
3172 | eventually, all accesses end up on the same list (and we'll warn about the | |
3173 | unordered subexpressions b++ and b. | |
3174 | ||
3175 | A note on merging. If we modify the former example so that our expression | |
3176 | becomes | |
3177 | (b++, b) + a | |
3178 | care must be taken not simply to add all three expressions into the final | |
3179 | PNO_SP list. The function merge_tlist takes care of that by merging the | |
3180 | before-SP list of the COMPOUND_EXPR into its after-SP list in a special | |
3181 | way, so that no more than one access to B is recorded. */ | |
2683ed8d | 3182 | |
235cfbc4 | 3183 | static void |
35b1a6fa AJ |
3184 | verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp, |
3185 | tree writer) | |
235cfbc4 BS |
3186 | { |
3187 | struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; | |
3188 | enum tree_code code; | |
6615c446 | 3189 | enum tree_code_class cl; |
2683ed8d | 3190 | |
f9e1917e JM |
3191 | /* X may be NULL if it is the operand of an empty statement expression |
3192 | ({ }). */ | |
3193 | if (x == NULL) | |
3194 | return; | |
3195 | ||
235cfbc4 BS |
3196 | restart: |
3197 | code = TREE_CODE (x); | |
e3a64162 | 3198 | cl = TREE_CODE_CLASS (code); |
2683ed8d | 3199 | |
235cfbc4 | 3200 | if (warning_candidate_p (x)) |
1e4ae551 | 3201 | *pno_sp = new_tlist (*pno_sp, x, writer); |
235cfbc4 BS |
3202 | |
3203 | switch (code) | |
3204 | { | |
52a84e42 | 3205 | case CONSTRUCTOR: |
f7716d57 | 3206 | case SIZEOF_EXPR: |
52a84e42 BS |
3207 | return; |
3208 | ||
235cfbc4 BS |
3209 | case COMPOUND_EXPR: |
3210 | case TRUTH_ANDIF_EXPR: | |
3211 | case TRUTH_ORIF_EXPR: | |
3212 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
3213 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3214 | warn_for_collisions (tmp_nosp); | |
3215 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3216 | merge_tlist (pbefore_sp, tmp_nosp, 0); | |
3217 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); | |
3218 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3219 | return; | |
3220 | ||
3221 | case COND_EXPR: | |
3222 | tmp_before = tmp_list2 = 0; | |
3223 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); | |
3224 | warn_for_collisions (tmp_list2); | |
3225 | merge_tlist (pbefore_sp, tmp_before, 0); | |
c2bf53a1 | 3226 | merge_tlist (pbefore_sp, tmp_list2, 0); |
235cfbc4 BS |
3227 | |
3228 | tmp_list3 = tmp_nosp = 0; | |
3229 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); | |
3230 | warn_for_collisions (tmp_nosp); | |
3231 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3232 | ||
3233 | tmp_list3 = tmp_list2 = 0; | |
3234 | verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); | |
3235 | warn_for_collisions (tmp_list2); | |
3236 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
3237 | /* Rather than add both tmp_nosp and tmp_list2, we have to merge the | |
3238 | two first, to avoid warning for (a ? b++ : b++). */ | |
3239 | merge_tlist (&tmp_nosp, tmp_list2, 0); | |
3240 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
3241 | return; | |
3242 | ||
2683ed8d BS |
3243 | case PREDECREMENT_EXPR: |
3244 | case PREINCREMENT_EXPR: | |
3245 | case POSTDECREMENT_EXPR: | |
3246 | case POSTINCREMENT_EXPR: | |
235cfbc4 BS |
3247 | verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); |
3248 | return; | |
3249 | ||
3250 | case MODIFY_EXPR: | |
3251 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
3252 | verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); | |
3253 | verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); | |
3254 | /* Expressions inside the LHS are not ordered wrt. the sequence points | |
3255 | in the RHS. Example: | |
3256 | *a = (a++, 2) | |
3257 | Despite the fact that the modification of "a" is in the before_sp | |
3258 | list (tmp_before), it conflicts with the use of "a" in the LHS. | |
3259 | We can handle this by adding the contents of tmp_list3 | |
3260 | to those of tmp_before, and redoing the collision warnings for that | |
3261 | list. */ | |
3262 | add_tlist (&tmp_before, tmp_list3, x, 1); | |
3263 | warn_for_collisions (tmp_before); | |
3264 | /* Exclude the LHS itself here; we first have to merge it into the | |
3265 | tmp_nosp list. This is done to avoid warning for "a = a"; if we | |
3266 | didn't exclude the LHS, we'd get it twice, once as a read and once | |
3267 | as a write. */ | |
3268 | add_tlist (pno_sp, tmp_list3, x, 0); | |
3269 | warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); | |
3270 | ||
3271 | merge_tlist (pbefore_sp, tmp_before, 0); | |
3272 | if (warning_candidate_p (TREE_OPERAND (x, 0))) | |
3273 | merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); | |
3274 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); | |
3275 | return; | |
2683ed8d BS |
3276 | |
3277 | case CALL_EXPR: | |
235cfbc4 BS |
3278 | /* We need to warn about conflicts among arguments and conflicts between |
3279 | args and the function address. Side effects of the function address, | |
3280 | however, are not ordered by the sequence point of the call. */ | |
5039610b SL |
3281 | { |
3282 | call_expr_arg_iterator iter; | |
3283 | tree arg; | |
b8698a0f | 3284 | tmp_before = tmp_nosp = 0; |
5039610b SL |
3285 | verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE); |
3286 | FOR_EACH_CALL_EXPR_ARG (arg, iter, x) | |
3287 | { | |
3288 | tmp_list2 = tmp_list3 = 0; | |
3289 | verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE); | |
3290 | merge_tlist (&tmp_list3, tmp_list2, 0); | |
3291 | add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); | |
3292 | } | |
3293 | add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); | |
3294 | warn_for_collisions (tmp_before); | |
3295 | add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); | |
3296 | return; | |
3297 | } | |
2683ed8d BS |
3298 | |
3299 | case TREE_LIST: | |
3300 | /* Scan all the list, e.g. indices of multi dimensional array. */ | |
3301 | while (x) | |
3302 | { | |
235cfbc4 BS |
3303 | tmp_before = tmp_nosp = 0; |
3304 | verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
3305 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
3306 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
2683ed8d BS |
3307 | x = TREE_CHAIN (x); |
3308 | } | |
235cfbc4 | 3309 | return; |
2683ed8d | 3310 | |
235cfbc4 BS |
3311 | case SAVE_EXPR: |
3312 | { | |
3313 | struct tlist_cache *t; | |
3314 | for (t = save_expr_cache; t; t = t->next) | |
1e4ae551 | 3315 | if (candidate_equal_p (t->expr, x)) |
235cfbc4 | 3316 | break; |
2683ed8d | 3317 | |
3f75a254 | 3318 | if (!t) |
2683ed8d | 3319 | { |
5d038c4c | 3320 | t = XOBNEW (&tlist_obstack, struct tlist_cache); |
235cfbc4 BS |
3321 | t->next = save_expr_cache; |
3322 | t->expr = x; | |
3323 | save_expr_cache = t; | |
3324 | ||
3325 | tmp_before = tmp_nosp = 0; | |
3326 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3327 | warn_for_collisions (tmp_nosp); | |
3328 | ||
3329 | tmp_list3 = 0; | |
c2bf53a1 | 3330 | merge_tlist (&tmp_list3, tmp_nosp, 0); |
235cfbc4 BS |
3331 | t->cache_before_sp = tmp_before; |
3332 | t->cache_after_sp = tmp_list3; | |
2683ed8d | 3333 | } |
235cfbc4 BS |
3334 | merge_tlist (pbefore_sp, t->cache_before_sp, 1); |
3335 | add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); | |
3336 | return; | |
3337 | } | |
2683ed8d | 3338 | |
528c22f4 MLI |
3339 | case ADDR_EXPR: |
3340 | x = TREE_OPERAND (x, 0); | |
3341 | if (DECL_P (x)) | |
3342 | return; | |
3343 | writer = 0; | |
3344 | goto restart; | |
3345 | ||
6615c446 JO |
3346 | default: |
3347 | /* For other expressions, simply recurse on their operands. | |
c22cacf3 | 3348 | Manual tail recursion for unary expressions. |
6615c446 JO |
3349 | Other non-expressions need not be processed. */ |
3350 | if (cl == tcc_unary) | |
3351 | { | |
6615c446 JO |
3352 | x = TREE_OPERAND (x, 0); |
3353 | writer = 0; | |
3354 | goto restart; | |
3355 | } | |
3356 | else if (IS_EXPR_CODE_CLASS (cl)) | |
3357 | { | |
3358 | int lp; | |
5039610b | 3359 | int max = TREE_OPERAND_LENGTH (x); |
6615c446 JO |
3360 | for (lp = 0; lp < max; lp++) |
3361 | { | |
3362 | tmp_before = tmp_nosp = 0; | |
3363 | verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0); | |
3364 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
3365 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
3366 | } | |
3367 | } | |
3368 | return; | |
2683ed8d | 3369 | } |
2683ed8d BS |
3370 | } |
3371 | ||
8d9afc4e | 3372 | /* Try to warn for undefined behavior in EXPR due to missing sequence |
2683ed8d BS |
3373 | points. */ |
3374 | ||
24e47c76 | 3375 | DEBUG_FUNCTION void |
35b1a6fa | 3376 | verify_sequence_points (tree expr) |
2683ed8d | 3377 | { |
235cfbc4 | 3378 | struct tlist *before_sp = 0, *after_sp = 0; |
2683ed8d | 3379 | |
235cfbc4 BS |
3380 | warned_ids = 0; |
3381 | save_expr_cache = 0; | |
3382 | if (tlist_firstobj == 0) | |
2683ed8d | 3383 | { |
235cfbc4 | 3384 | gcc_obstack_init (&tlist_obstack); |
28dab132 | 3385 | tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0); |
2683ed8d BS |
3386 | } |
3387 | ||
235cfbc4 BS |
3388 | verify_tree (expr, &before_sp, &after_sp, 0); |
3389 | warn_for_collisions (after_sp); | |
3390 | obstack_free (&tlist_obstack, tlist_firstobj); | |
2683ed8d | 3391 | } |
b30f223b RS |
3392 | \f |
3393 | /* Validate the expression after `case' and apply default promotions. */ | |
3394 | ||
a6c0a76c | 3395 | static tree |
62e4eb35 | 3396 | check_case_value (location_t loc, tree value) |
b30f223b RS |
3397 | { |
3398 | if (value == NULL_TREE) | |
3399 | return value; | |
3400 | ||
522ddfa2 JM |
3401 | if (TREE_CODE (value) == INTEGER_CST) |
3402 | /* Promote char or short to int. */ | |
3403 | value = perform_integral_promotions (value); | |
3404 | else if (value != error_mark_node) | |
b30f223b | 3405 | { |
62e4eb35 | 3406 | error_at (loc, "case label does not reduce to an integer constant"); |
b30f223b RS |
3407 | value = error_mark_node; |
3408 | } | |
b30f223b | 3409 | |
bc690db1 RS |
3410 | constant_expression_warning (value); |
3411 | ||
b30f223b RS |
3412 | return value; |
3413 | } | |
3414 | \f | |
a6c0a76c | 3415 | /* See if the case values LOW and HIGH are in the range of the original |
89dbed81 | 3416 | type (i.e. before the default conversion to int) of the switch testing |
a6c0a76c SB |
3417 | expression. |
3418 | TYPE is the promoted type of the testing expression, and ORIG_TYPE is | |
2a7e31df | 3419 | the type before promoting it. CASE_LOW_P is a pointer to the lower |
a6c0a76c SB |
3420 | bound of the case label, and CASE_HIGH_P is the upper bound or NULL |
3421 | if the case is not a case range. | |
3422 | The caller has to make sure that we are not called with NULL for | |
89dbed81 | 3423 | CASE_LOW_P (i.e. the default case). |
0fa2e4df | 3424 | Returns true if the case label is in range of ORIG_TYPE (saturated or |
a6c0a76c SB |
3425 | untouched) or false if the label is out of range. */ |
3426 | ||
3427 | static bool | |
9d548dfb | 3428 | check_case_bounds (location_t loc, tree type, tree orig_type, |
a6c0a76c SB |
3429 | tree *case_low_p, tree *case_high_p) |
3430 | { | |
3431 | tree min_value, max_value; | |
3432 | tree case_low = *case_low_p; | |
3433 | tree case_high = case_high_p ? *case_high_p : case_low; | |
3434 | ||
3435 | /* If there was a problem with the original type, do nothing. */ | |
3436 | if (orig_type == error_mark_node) | |
3437 | return true; | |
3438 | ||
3439 | min_value = TYPE_MIN_VALUE (orig_type); | |
3440 | max_value = TYPE_MAX_VALUE (orig_type); | |
3441 | ||
3442 | /* Case label is less than minimum for type. */ | |
3443 | if (tree_int_cst_compare (case_low, min_value) < 0 | |
3444 | && tree_int_cst_compare (case_high, min_value) < 0) | |
3445 | { | |
9d548dfb MP |
3446 | warning_at (loc, 0, "case label value is less than minimum value " |
3447 | "for type"); | |
a6c0a76c SB |
3448 | return false; |
3449 | } | |
9f63daea | 3450 | |
a6c0a76c SB |
3451 | /* Case value is greater than maximum for type. */ |
3452 | if (tree_int_cst_compare (case_low, max_value) > 0 | |
3453 | && tree_int_cst_compare (case_high, max_value) > 0) | |
3454 | { | |
9d548dfb | 3455 | warning_at (loc, 0, "case label value exceeds maximum value for type"); |
a6c0a76c SB |
3456 | return false; |
3457 | } | |
3458 | ||
3459 | /* Saturate lower case label value to minimum. */ | |
3460 | if (tree_int_cst_compare (case_high, min_value) >= 0 | |
3461 | && tree_int_cst_compare (case_low, min_value) < 0) | |
3462 | { | |
9d548dfb MP |
3463 | warning_at (loc, 0, "lower value in case label range" |
3464 | " less than minimum value for type"); | |
a6c0a76c SB |
3465 | case_low = min_value; |
3466 | } | |
9f63daea | 3467 | |
a6c0a76c SB |
3468 | /* Saturate upper case label value to maximum. */ |
3469 | if (tree_int_cst_compare (case_low, max_value) <= 0 | |
3470 | && tree_int_cst_compare (case_high, max_value) > 0) | |
3471 | { | |
9d548dfb MP |
3472 | warning_at (loc, 0, "upper value in case label range" |
3473 | " exceeds maximum value for type"); | |
a6c0a76c SB |
3474 | case_high = max_value; |
3475 | } | |
3476 | ||
3477 | if (*case_low_p != case_low) | |
3478 | *case_low_p = convert (type, case_low); | |
3479 | if (case_high_p && *case_high_p != case_high) | |
3480 | *case_high_p = convert (type, case_high); | |
3481 | ||
3482 | return true; | |
3483 | } | |
3484 | \f | |
b30f223b RS |
3485 | /* Return an integer type with BITS bits of precision, |
3486 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
3487 | ||
3488 | tree | |
35b1a6fa | 3489 | c_common_type_for_size (unsigned int bits, int unsignedp) |
b30f223b | 3490 | { |
78a7c317 DD |
3491 | int i; |
3492 | ||
a311b52c JM |
3493 | if (bits == TYPE_PRECISION (integer_type_node)) |
3494 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3495 | ||
3fc7e390 | 3496 | if (bits == TYPE_PRECISION (signed_char_type_node)) |
b30f223b RS |
3497 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
3498 | ||
3fc7e390 | 3499 | if (bits == TYPE_PRECISION (short_integer_type_node)) |
b30f223b RS |
3500 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
3501 | ||
3fc7e390 | 3502 | if (bits == TYPE_PRECISION (long_integer_type_node)) |
b30f223b RS |
3503 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
3504 | ||
3fc7e390 | 3505 | if (bits == TYPE_PRECISION (long_long_integer_type_node)) |
b30f223b RS |
3506 | return (unsignedp ? long_long_unsigned_type_node |
3507 | : long_long_integer_type_node); | |
3508 | ||
78a7c317 DD |
3509 | for (i = 0; i < NUM_INT_N_ENTS; i ++) |
3510 | if (int_n_enabled_p[i] | |
3511 | && bits == int_n_data[i].bitsize) | |
3512 | return (unsignedp ? int_n_trees[i].unsigned_type | |
3513 | : int_n_trees[i].signed_type); | |
a6766312 | 3514 | |
835f9b4d GRK |
3515 | if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) |
3516 | return (unsignedp ? widest_unsigned_literal_type_node | |
3517 | : widest_integer_literal_type_node); | |
3518 | ||
3fc7e390 RS |
3519 | if (bits <= TYPE_PRECISION (intQI_type_node)) |
3520 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3521 | ||
3522 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
3523 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3524 | ||
3525 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
3526 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3527 | ||
3528 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
3529 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3530 | ||
b30f223b RS |
3531 | return 0; |
3532 | } | |
3533 | ||
ab22c1fa CF |
3534 | /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits |
3535 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed; | |
3536 | and saturating if SATP is nonzero, otherwise not saturating. */ | |
3537 | ||
3538 | tree | |
3539 | c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit, | |
3540 | int unsignedp, int satp) | |
3541 | { | |
ef4bddc2 | 3542 | machine_mode mode; |
ab22c1fa CF |
3543 | if (ibit == 0) |
3544 | mode = unsignedp ? UQQmode : QQmode; | |
3545 | else | |
3546 | mode = unsignedp ? UHAmode : HAmode; | |
3547 | ||
3548 | for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode)) | |
3549 | if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit) | |
3550 | break; | |
3551 | ||
3552 | if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode)) | |
3553 | { | |
3554 | sorry ("GCC cannot support operators with integer types and " | |
3555 | "fixed-point types that have too many integral and " | |
3556 | "fractional bits together"); | |
3557 | return 0; | |
3558 | } | |
3559 | ||
3560 | return c_common_type_for_mode (mode, satp); | |
3561 | } | |
3562 | ||
d1d3865f ZW |
3563 | /* Used for communication between c_common_type_for_mode and |
3564 | c_register_builtin_type. */ | |
793c625f | 3565 | tree registered_builtin_types; |
d1d3865f | 3566 | |
b30f223b RS |
3567 | /* Return a data type that has machine mode MODE. |
3568 | If the mode is an integer, | |
ab22c1fa CF |
3569 | then UNSIGNEDP selects between signed and unsigned types. |
3570 | If the mode is a fixed-point mode, | |
3571 | then UNSIGNEDP selects between saturating and nonsaturating types. */ | |
b30f223b RS |
3572 | |
3573 | tree | |
ef4bddc2 | 3574 | c_common_type_for_mode (machine_mode mode, int unsignedp) |
b30f223b | 3575 | { |
d1d3865f | 3576 | tree t; |
78a7c317 | 3577 | int i; |
d1d3865f | 3578 | |
a311b52c JM |
3579 | if (mode == TYPE_MODE (integer_type_node)) |
3580 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3581 | ||
b30f223b RS |
3582 | if (mode == TYPE_MODE (signed_char_type_node)) |
3583 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3584 | ||
3585 | if (mode == TYPE_MODE (short_integer_type_node)) | |
3586 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3587 | ||
b30f223b RS |
3588 | if (mode == TYPE_MODE (long_integer_type_node)) |
3589 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3590 | ||
3591 | if (mode == TYPE_MODE (long_long_integer_type_node)) | |
3592 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
3593 | ||
78a7c317 DD |
3594 | for (i = 0; i < NUM_INT_N_ENTS; i ++) |
3595 | if (int_n_enabled_p[i] | |
3596 | && mode == int_n_data[i].m) | |
3597 | return (unsignedp ? int_n_trees[i].unsigned_type | |
3598 | : int_n_trees[i].signed_type); | |
a6766312 | 3599 | |
835f9b4d | 3600 | if (mode == TYPE_MODE (widest_integer_literal_type_node)) |
d125d268 | 3601 | return unsignedp ? widest_unsigned_literal_type_node |
6de9cd9a | 3602 | : widest_integer_literal_type_node; |
835f9b4d | 3603 | |
0afeef64 | 3604 | if (mode == QImode) |
3fc7e390 RS |
3605 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
3606 | ||
0afeef64 | 3607 | if (mode == HImode) |
3fc7e390 RS |
3608 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
3609 | ||
0afeef64 | 3610 | if (mode == SImode) |
3fc7e390 RS |
3611 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
3612 | ||
0afeef64 | 3613 | if (mode == DImode) |
3fc7e390 RS |
3614 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
3615 | ||
21a9616b | 3616 | #if HOST_BITS_PER_WIDE_INT >= 64 |
a6d7e156 JL |
3617 | if (mode == TYPE_MODE (intTI_type_node)) |
3618 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
21a9616b | 3619 | #endif |
a6d7e156 | 3620 | |
b30f223b RS |
3621 | if (mode == TYPE_MODE (float_type_node)) |
3622 | return float_type_node; | |
3623 | ||
3624 | if (mode == TYPE_MODE (double_type_node)) | |
3625 | return double_type_node; | |
3626 | ||
3627 | if (mode == TYPE_MODE (long_double_type_node)) | |
3628 | return long_double_type_node; | |
3629 | ||
ff42324e NS |
3630 | if (mode == TYPE_MODE (void_type_node)) |
3631 | return void_type_node; | |
9f63daea | 3632 | |
b30f223b | 3633 | if (mode == TYPE_MODE (build_pointer_type (char_type_node))) |
19b3ffbc DD |
3634 | return (unsignedp |
3635 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
3636 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b30f223b RS |
3637 | |
3638 | if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) | |
19b3ffbc DD |
3639 | return (unsignedp |
3640 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
3641 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b30f223b | 3642 | |
7e7e470f RH |
3643 | if (COMPLEX_MODE_P (mode)) |
3644 | { | |
ef4bddc2 | 3645 | machine_mode inner_mode; |
7e7e470f RH |
3646 | tree inner_type; |
3647 | ||
3648 | if (mode == TYPE_MODE (complex_float_type_node)) | |
3649 | return complex_float_type_node; | |
3650 | if (mode == TYPE_MODE (complex_double_type_node)) | |
3651 | return complex_double_type_node; | |
3652 | if (mode == TYPE_MODE (complex_long_double_type_node)) | |
3653 | return complex_long_double_type_node; | |
3654 | ||
3655 | if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp) | |
3656 | return complex_integer_type_node; | |
3657 | ||
3658 | inner_mode = GET_MODE_INNER (mode); | |
3659 | inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
3660 | if (inner_type != NULL_TREE) | |
3661 | return build_complex_type (inner_type); | |
3662 | } | |
3663 | else if (VECTOR_MODE_P (mode)) | |
4a5eab38 | 3664 | { |
ef4bddc2 | 3665 | machine_mode inner_mode = GET_MODE_INNER (mode); |
4a5eab38 PB |
3666 | tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); |
3667 | if (inner_type != NULL_TREE) | |
3668 | return build_vector_type_for_mode (inner_type, mode); | |
0afeef64 | 3669 | } |
4061f623 | 3670 | |
9a8ce21f JG |
3671 | if (mode == TYPE_MODE (dfloat32_type_node)) |
3672 | return dfloat32_type_node; | |
3673 | if (mode == TYPE_MODE (dfloat64_type_node)) | |
3674 | return dfloat64_type_node; | |
3675 | if (mode == TYPE_MODE (dfloat128_type_node)) | |
3676 | return dfloat128_type_node; | |
3677 | ||
ab22c1fa CF |
3678 | if (ALL_SCALAR_FIXED_POINT_MODE_P (mode)) |
3679 | { | |
3680 | if (mode == TYPE_MODE (short_fract_type_node)) | |
3681 | return unsignedp ? sat_short_fract_type_node : short_fract_type_node; | |
3682 | if (mode == TYPE_MODE (fract_type_node)) | |
3683 | return unsignedp ? sat_fract_type_node : fract_type_node; | |
3684 | if (mode == TYPE_MODE (long_fract_type_node)) | |
3685 | return unsignedp ? sat_long_fract_type_node : long_fract_type_node; | |
3686 | if (mode == TYPE_MODE (long_long_fract_type_node)) | |
3687 | return unsignedp ? sat_long_long_fract_type_node | |
3688 | : long_long_fract_type_node; | |
3689 | ||
3690 | if (mode == TYPE_MODE (unsigned_short_fract_type_node)) | |
3691 | return unsignedp ? sat_unsigned_short_fract_type_node | |
3692 | : unsigned_short_fract_type_node; | |
3693 | if (mode == TYPE_MODE (unsigned_fract_type_node)) | |
3694 | return unsignedp ? sat_unsigned_fract_type_node | |
3695 | : unsigned_fract_type_node; | |
3696 | if (mode == TYPE_MODE (unsigned_long_fract_type_node)) | |
3697 | return unsignedp ? sat_unsigned_long_fract_type_node | |
3698 | : unsigned_long_fract_type_node; | |
3699 | if (mode == TYPE_MODE (unsigned_long_long_fract_type_node)) | |
3700 | return unsignedp ? sat_unsigned_long_long_fract_type_node | |
3701 | : unsigned_long_long_fract_type_node; | |
3702 | ||
3703 | if (mode == TYPE_MODE (short_accum_type_node)) | |
3704 | return unsignedp ? sat_short_accum_type_node : short_accum_type_node; | |
3705 | if (mode == TYPE_MODE (accum_type_node)) | |
3706 | return unsignedp ? sat_accum_type_node : accum_type_node; | |
3707 | if (mode == TYPE_MODE (long_accum_type_node)) | |
3708 | return unsignedp ? sat_long_accum_type_node : long_accum_type_node; | |
3709 | if (mode == TYPE_MODE (long_long_accum_type_node)) | |
3710 | return unsignedp ? sat_long_long_accum_type_node | |
3711 | : long_long_accum_type_node; | |
3712 | ||
3713 | if (mode == TYPE_MODE (unsigned_short_accum_type_node)) | |
3714 | return unsignedp ? sat_unsigned_short_accum_type_node | |
3715 | : unsigned_short_accum_type_node; | |
3716 | if (mode == TYPE_MODE (unsigned_accum_type_node)) | |
3717 | return unsignedp ? sat_unsigned_accum_type_node | |
3718 | : unsigned_accum_type_node; | |
3719 | if (mode == TYPE_MODE (unsigned_long_accum_type_node)) | |
3720 | return unsignedp ? sat_unsigned_long_accum_type_node | |
3721 | : unsigned_long_accum_type_node; | |
3722 | if (mode == TYPE_MODE (unsigned_long_long_accum_type_node)) | |
3723 | return unsignedp ? sat_unsigned_long_long_accum_type_node | |
3724 | : unsigned_long_long_accum_type_node; | |
3725 | ||
3726 | if (mode == QQmode) | |
3727 | return unsignedp ? sat_qq_type_node : qq_type_node; | |
3728 | if (mode == HQmode) | |
3729 | return unsignedp ? sat_hq_type_node : hq_type_node; | |
3730 | if (mode == SQmode) | |
3731 | return unsignedp ? sat_sq_type_node : sq_type_node; | |
3732 | if (mode == DQmode) | |
3733 | return unsignedp ? sat_dq_type_node : dq_type_node; | |
3734 | if (mode == TQmode) | |
3735 | return unsignedp ? sat_tq_type_node : tq_type_node; | |
3736 | ||
3737 | if (mode == UQQmode) | |
3738 | return unsignedp ? sat_uqq_type_node : uqq_type_node; | |
3739 | if (mode == UHQmode) | |
3740 | return unsignedp ? sat_uhq_type_node : uhq_type_node; | |
3741 | if (mode == USQmode) | |
3742 | return unsignedp ? sat_usq_type_node : usq_type_node; | |
3743 | if (mode == UDQmode) | |
3744 | return unsignedp ? sat_udq_type_node : udq_type_node; | |
3745 | if (mode == UTQmode) | |
3746 | return unsignedp ? sat_utq_type_node : utq_type_node; | |
3747 | ||
3748 | if (mode == HAmode) | |
3749 | return unsignedp ? sat_ha_type_node : ha_type_node; | |
3750 | if (mode == SAmode) | |
3751 | return unsignedp ? sat_sa_type_node : sa_type_node; | |
3752 | if (mode == DAmode) | |
3753 | return unsignedp ? sat_da_type_node : da_type_node; | |
3754 | if (mode == TAmode) | |
3755 | return unsignedp ? sat_ta_type_node : ta_type_node; | |
3756 | ||
3757 | if (mode == UHAmode) | |
3758 | return unsignedp ? sat_uha_type_node : uha_type_node; | |
3759 | if (mode == USAmode) | |
3760 | return unsignedp ? sat_usa_type_node : usa_type_node; | |
3761 | if (mode == UDAmode) | |
3762 | return unsignedp ? sat_uda_type_node : uda_type_node; | |
3763 | if (mode == UTAmode) | |
3764 | return unsignedp ? sat_uta_type_node : uta_type_node; | |
3765 | } | |
3766 | ||
d1d3865f | 3767 | for (t = registered_builtin_types; t; t = TREE_CHAIN (t)) |
7a421706 MS |
3768 | if (TYPE_MODE (TREE_VALUE (t)) == mode |
3769 | && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t))) | |
d1d3865f ZW |
3770 | return TREE_VALUE (t); |
3771 | ||
b30f223b RS |
3772 | return 0; |
3773 | } | |
693a6128 | 3774 | |
12753674 RE |
3775 | tree |
3776 | c_common_unsigned_type (tree type) | |
3777 | { | |
3778 | return c_common_signed_or_unsigned_type (1, type); | |
3779 | } | |
3780 | ||
693a6128 GRK |
3781 | /* Return a signed type the same as TYPE in other respects. */ |
3782 | ||
3783 | tree | |
35b1a6fa | 3784 | c_common_signed_type (tree type) |
693a6128 | 3785 | { |
ceef8ce4 | 3786 | return c_common_signed_or_unsigned_type (0, type); |
693a6128 GRK |
3787 | } |
3788 | ||
3789 | /* Return a type the same as TYPE except unsigned or | |
3790 | signed according to UNSIGNEDP. */ | |
3791 | ||
3792 | tree | |
35b1a6fa | 3793 | c_common_signed_or_unsigned_type (int unsignedp, tree type) |
693a6128 | 3794 | { |
c74a03d2 | 3795 | tree type1; |
78a7c317 | 3796 | int i; |
693a6128 | 3797 | |
c74a03d2 RAE |
3798 | /* This block of code emulates the behavior of the old |
3799 | c_common_unsigned_type. In particular, it returns | |
3800 | long_unsigned_type_node if passed a long, even when a int would | |
3801 | have the same size. This is necessary for warnings to work | |
3802 | correctly in archs where sizeof(int) == sizeof(long) */ | |
3803 | ||
3804 | type1 = TYPE_MAIN_VARIANT (type); | |
3805 | if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node) | |
3806 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3807 | if (type1 == integer_type_node || type1 == unsigned_type_node) | |
3808 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3809 | if (type1 == short_integer_type_node || type1 == short_unsigned_type_node) | |
3810 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3811 | if (type1 == long_integer_type_node || type1 == long_unsigned_type_node) | |
3812 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3813 | if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node) | |
3814 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
78a7c317 DD |
3815 | |
3816 | for (i = 0; i < NUM_INT_N_ENTS; i ++) | |
3817 | if (int_n_enabled_p[i] | |
3818 | && (type1 == int_n_trees[i].unsigned_type | |
3819 | || type1 == int_n_trees[i].signed_type)) | |
3820 | return (unsignedp ? int_n_trees[i].unsigned_type | |
3821 | : int_n_trees[i].signed_type); | |
3822 | ||
c74a03d2 RAE |
3823 | if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node) |
3824 | return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node; | |
3825 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
3826 | if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node) | |
3827 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
3828 | #endif | |
3829 | if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node) | |
3830 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3831 | if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node) | |
3832 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3833 | if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node) | |
3834 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3835 | if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node) | |
3836 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3837 | ||
70d3fcab AH |
3838 | #define C_COMMON_FIXED_TYPES(NAME) \ |
3839 | if (type1 == short_ ## NAME ## _type_node \ | |
3840 | || type1 == unsigned_short_ ## NAME ## _type_node) \ | |
3841 | return unsignedp ? unsigned_short_ ## NAME ## _type_node \ | |
3842 | : short_ ## NAME ## _type_node; \ | |
3843 | if (type1 == NAME ## _type_node \ | |
3844 | || type1 == unsigned_ ## NAME ## _type_node) \ | |
3845 | return unsignedp ? unsigned_ ## NAME ## _type_node \ | |
3846 | : NAME ## _type_node; \ | |
3847 | if (type1 == long_ ## NAME ## _type_node \ | |
3848 | || type1 == unsigned_long_ ## NAME ## _type_node) \ | |
3849 | return unsignedp ? unsigned_long_ ## NAME ## _type_node \ | |
3850 | : long_ ## NAME ## _type_node; \ | |
3851 | if (type1 == long_long_ ## NAME ## _type_node \ | |
3852 | || type1 == unsigned_long_long_ ## NAME ## _type_node) \ | |
3853 | return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \ | |
3854 | : long_long_ ## NAME ## _type_node; | |
3855 | ||
3856 | #define C_COMMON_FIXED_MODE_TYPES(NAME) \ | |
3857 | if (type1 == NAME ## _type_node \ | |
3858 | || type1 == u ## NAME ## _type_node) \ | |
3859 | return unsignedp ? u ## NAME ## _type_node \ | |
3860 | : NAME ## _type_node; | |
3861 | ||
3862 | #define C_COMMON_FIXED_TYPES_SAT(NAME) \ | |
3863 | if (type1 == sat_ ## short_ ## NAME ## _type_node \ | |
3864 | || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \ | |
3865 | return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \ | |
3866 | : sat_ ## short_ ## NAME ## _type_node; \ | |
3867 | if (type1 == sat_ ## NAME ## _type_node \ | |
3868 | || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \ | |
3869 | return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \ | |
3870 | : sat_ ## NAME ## _type_node; \ | |
3871 | if (type1 == sat_ ## long_ ## NAME ## _type_node \ | |
3872 | || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \ | |
3873 | return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \ | |
3874 | : sat_ ## long_ ## NAME ## _type_node; \ | |
3875 | if (type1 == sat_ ## long_long_ ## NAME ## _type_node \ | |
3876 | || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \ | |
3877 | return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \ | |
3878 | : sat_ ## long_long_ ## NAME ## _type_node; | |
3879 | ||
3880 | #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \ | |
3881 | if (type1 == sat_ ## NAME ## _type_node \ | |
3882 | || type1 == sat_ ## u ## NAME ## _type_node) \ | |
3883 | return unsignedp ? sat_ ## u ## NAME ## _type_node \ | |
3884 | : sat_ ## NAME ## _type_node; | |
3885 | ||
3886 | C_COMMON_FIXED_TYPES (fract); | |
3887 | C_COMMON_FIXED_TYPES_SAT (fract); | |
3888 | C_COMMON_FIXED_TYPES (accum); | |
3889 | C_COMMON_FIXED_TYPES_SAT (accum); | |
3890 | ||
3891 | C_COMMON_FIXED_MODE_TYPES (qq); | |
3892 | C_COMMON_FIXED_MODE_TYPES (hq); | |
3893 | C_COMMON_FIXED_MODE_TYPES (sq); | |
3894 | C_COMMON_FIXED_MODE_TYPES (dq); | |
3895 | C_COMMON_FIXED_MODE_TYPES (tq); | |
3896 | C_COMMON_FIXED_MODE_TYPES_SAT (qq); | |
3897 | C_COMMON_FIXED_MODE_TYPES_SAT (hq); | |
3898 | C_COMMON_FIXED_MODE_TYPES_SAT (sq); | |
3899 | C_COMMON_FIXED_MODE_TYPES_SAT (dq); | |
3900 | C_COMMON_FIXED_MODE_TYPES_SAT (tq); | |
3901 | C_COMMON_FIXED_MODE_TYPES (ha); | |
3902 | C_COMMON_FIXED_MODE_TYPES (sa); | |
3903 | C_COMMON_FIXED_MODE_TYPES (da); | |
3904 | C_COMMON_FIXED_MODE_TYPES (ta); | |
3905 | C_COMMON_FIXED_MODE_TYPES_SAT (ha); | |
3906 | C_COMMON_FIXED_MODE_TYPES_SAT (sa); | |
3907 | C_COMMON_FIXED_MODE_TYPES_SAT (da); | |
3908 | C_COMMON_FIXED_MODE_TYPES_SAT (ta); | |
ab22c1fa | 3909 | |
bc15d0ef JM |
3910 | /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not |
3911 | the precision; they have precision set to match their range, but | |
3912 | may use a wider mode to match an ABI. If we change modes, we may | |
3913 | wind up with bad conversions. For INTEGER_TYPEs in C, must check | |
3914 | the precision as well, so as to yield correct results for | |
3915 | bit-field types. C++ does not have these separate bit-field | |
3916 | types, and producing a signed or unsigned variant of an | |
3917 | ENUMERAL_TYPE may cause other problems as well. */ | |
3918 | ||
1e204133 RAE |
3919 | if (!INTEGRAL_TYPE_P (type) |
3920 | || TYPE_UNSIGNED (type) == unsignedp) | |
3921 | return type; | |
3922 | ||
bc15d0ef JM |
3923 | #define TYPE_OK(node) \ |
3924 | (TYPE_MODE (type) == TYPE_MODE (node) \ | |
41b81065 | 3925 | && TYPE_PRECISION (type) == TYPE_PRECISION (node)) |
bc15d0ef | 3926 | if (TYPE_OK (signed_char_type_node)) |
693a6128 | 3927 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
bc15d0ef | 3928 | if (TYPE_OK (integer_type_node)) |
693a6128 | 3929 | return unsignedp ? unsigned_type_node : integer_type_node; |
bc15d0ef | 3930 | if (TYPE_OK (short_integer_type_node)) |
693a6128 | 3931 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
bc15d0ef | 3932 | if (TYPE_OK (long_integer_type_node)) |
693a6128 | 3933 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
bc15d0ef | 3934 | if (TYPE_OK (long_long_integer_type_node)) |
693a6128 GRK |
3935 | return (unsignedp ? long_long_unsigned_type_node |
3936 | : long_long_integer_type_node); | |
78a7c317 DD |
3937 | |
3938 | for (i = 0; i < NUM_INT_N_ENTS; i ++) | |
3939 | if (int_n_enabled_p[i] | |
3940 | && TYPE_MODE (type) == int_n_data[i].m | |
3941 | && TYPE_PRECISION (type) == int_n_data[i].bitsize) | |
3942 | return (unsignedp ? int_n_trees[i].unsigned_type | |
3943 | : int_n_trees[i].signed_type); | |
3944 | ||
bc15d0ef | 3945 | if (TYPE_OK (widest_integer_literal_type_node)) |
693a6128 GRK |
3946 | return (unsignedp ? widest_unsigned_literal_type_node |
3947 | : widest_integer_literal_type_node); | |
4a063bec RH |
3948 | |
3949 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
bc15d0ef | 3950 | if (TYPE_OK (intTI_type_node)) |
4a063bec RH |
3951 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; |
3952 | #endif | |
bc15d0ef | 3953 | if (TYPE_OK (intDI_type_node)) |
4a063bec | 3954 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
bc15d0ef | 3955 | if (TYPE_OK (intSI_type_node)) |
4a063bec | 3956 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
bc15d0ef | 3957 | if (TYPE_OK (intHI_type_node)) |
4a063bec | 3958 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
bc15d0ef | 3959 | if (TYPE_OK (intQI_type_node)) |
4a063bec | 3960 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
bc15d0ef | 3961 | #undef TYPE_OK |
4a063bec | 3962 | |
41b81065 | 3963 | return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp); |
693a6128 | 3964 | } |
9649812a | 3965 | |
38a4afee MM |
3966 | /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */ |
3967 | ||
3968 | tree | |
3969 | c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp) | |
3970 | { | |
78a7c317 DD |
3971 | int i; |
3972 | ||
38a4afee MM |
3973 | /* Extended integer types of the same width as a standard type have |
3974 | lesser rank, so those of the same width as int promote to int or | |
3975 | unsigned int and are valid for printf formats expecting int or | |
3976 | unsigned int. To avoid such special cases, avoid creating | |
3977 | extended integer types for bit-fields if a standard integer type | |
3978 | is available. */ | |
3979 | if (width == TYPE_PRECISION (integer_type_node)) | |
3980 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3981 | if (width == TYPE_PRECISION (signed_char_type_node)) | |
3982 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3983 | if (width == TYPE_PRECISION (short_integer_type_node)) | |
3984 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3985 | if (width == TYPE_PRECISION (long_integer_type_node)) | |
3986 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3987 | if (width == TYPE_PRECISION (long_long_integer_type_node)) | |
3988 | return (unsignedp ? long_long_unsigned_type_node | |
3989 | : long_long_integer_type_node); | |
78a7c317 DD |
3990 | for (i = 0; i < NUM_INT_N_ENTS; i ++) |
3991 | if (int_n_enabled_p[i] | |
3992 | && width == int_n_data[i].bitsize) | |
3993 | return (unsignedp ? int_n_trees[i].unsigned_type | |
3994 | : int_n_trees[i].signed_type); | |
38a4afee MM |
3995 | return build_nonstandard_integer_type (width, unsignedp); |
3996 | } | |
3997 | ||
9649812a MM |
3998 | /* The C version of the register_builtin_type langhook. */ |
3999 | ||
4000 | void | |
4001 | c_register_builtin_type (tree type, const char* name) | |
4002 | { | |
4003 | tree decl; | |
4004 | ||
c2255bc4 AH |
4005 | decl = build_decl (UNKNOWN_LOCATION, |
4006 | TYPE_DECL, get_identifier (name), type); | |
9649812a MM |
4007 | DECL_ARTIFICIAL (decl) = 1; |
4008 | if (!TYPE_NAME (type)) | |
4009 | TYPE_NAME (type) = decl; | |
4010 | pushdecl (decl); | |
d1d3865f ZW |
4011 | |
4012 | registered_builtin_types = tree_cons (0, type, registered_builtin_types); | |
9649812a | 4013 | } |
6acfe908 | 4014 | \f |
78ef5b89 | 4015 | /* Print an error message for invalid operands to arith operation |
ba47d38d AH |
4016 | CODE with TYPE0 for operand 0, and TYPE1 for operand 1. |
4017 | LOCATION is the location of the message. */ | |
b30f223b RS |
4018 | |
4019 | void | |
ba47d38d AH |
4020 | binary_op_error (location_t location, enum tree_code code, |
4021 | tree type0, tree type1) | |
b30f223b | 4022 | { |
b3694847 | 4023 | const char *opname; |
89c78d7d | 4024 | |
b30f223b RS |
4025 | switch (code) |
4026 | { | |
b30f223b RS |
4027 | case PLUS_EXPR: |
4028 | opname = "+"; break; | |
4029 | case MINUS_EXPR: | |
4030 | opname = "-"; break; | |
4031 | case MULT_EXPR: | |
4032 | opname = "*"; break; | |
4033 | case MAX_EXPR: | |
4034 | opname = "max"; break; | |
4035 | case MIN_EXPR: | |
4036 | opname = "min"; break; | |
4037 | case EQ_EXPR: | |
4038 | opname = "=="; break; | |
4039 | case NE_EXPR: | |
4040 | opname = "!="; break; | |
4041 | case LE_EXPR: | |
4042 | opname = "<="; break; | |
4043 | case GE_EXPR: | |
4044 | opname = ">="; break; | |
4045 | case LT_EXPR: | |
4046 | opname = "<"; break; | |
4047 | case GT_EXPR: | |
4048 | opname = ">"; break; | |
4049 | case LSHIFT_EXPR: | |
4050 | opname = "<<"; break; | |
4051 | case RSHIFT_EXPR: | |
4052 | opname = ">>"; break; | |
4053 | case TRUNC_MOD_EXPR: | |
047de90b | 4054 | case FLOOR_MOD_EXPR: |
b30f223b RS |
4055 | opname = "%"; break; |
4056 | case TRUNC_DIV_EXPR: | |
047de90b | 4057 | case FLOOR_DIV_EXPR: |
b30f223b RS |
4058 | opname = "/"; break; |
4059 | case BIT_AND_EXPR: | |
4060 | opname = "&"; break; | |
4061 | case BIT_IOR_EXPR: | |
4062 | opname = "|"; break; | |
4063 | case TRUTH_ANDIF_EXPR: | |
4064 | opname = "&&"; break; | |
4065 | case TRUTH_ORIF_EXPR: | |
4066 | opname = "||"; break; | |
4067 | case BIT_XOR_EXPR: | |
4068 | opname = "^"; break; | |
6d819282 | 4069 | default: |
37b2f290 | 4070 | gcc_unreachable (); |
b30f223b | 4071 | } |
ba47d38d AH |
4072 | error_at (location, |
4073 | "invalid operands to binary %s (have %qT and %qT)", opname, | |
4074 | type0, type1); | |
b30f223b RS |
4075 | } |
4076 | \f | |
50f305ca MLI |
4077 | /* Given an expression as a tree, return its original type. Do this |
4078 | by stripping any conversion that preserves the sign and precision. */ | |
4079 | static tree | |
4080 | expr_original_type (tree expr) | |
4081 | { | |
4082 | STRIP_SIGN_NOPS (expr); | |
4083 | return TREE_TYPE (expr); | |
4084 | } | |
4085 | ||
b30f223b RS |
4086 | /* Subroutine of build_binary_op, used for comparison operations. |
4087 | See if the operands have both been converted from subword integer types | |
4088 | and, if so, perhaps change them both back to their original type. | |
94dccd9d RS |
4089 | This function is also responsible for converting the two operands |
4090 | to the proper common type for comparison. | |
b30f223b RS |
4091 | |
4092 | The arguments of this function are all pointers to local variables | |
4093 | of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, | |
4094 | RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. | |
4095 | ||
393e8e8b MP |
4096 | LOC is the location of the comparison. |
4097 | ||
b30f223b RS |
4098 | If this function returns nonzero, it means that the comparison has |
4099 | a constant value. What this function returns is an expression for | |
4100 | that value. */ | |
4101 | ||
4102 | tree | |
393e8e8b MP |
4103 | shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr, |
4104 | tree *restype_ptr, enum tree_code *rescode_ptr) | |
b30f223b | 4105 | { |
b3694847 | 4106 | tree type; |
b30f223b RS |
4107 | tree op0 = *op0_ptr; |
4108 | tree op1 = *op1_ptr; | |
4109 | int unsignedp0, unsignedp1; | |
4110 | int real1, real2; | |
4111 | tree primop0, primop1; | |
4112 | enum tree_code code = *rescode_ptr; | |
4113 | ||
4114 | /* Throw away any conversions to wider types | |
4115 | already present in the operands. */ | |
4116 | ||
828fb3ba JM |
4117 | primop0 = c_common_get_narrower (op0, &unsignedp0); |
4118 | primop1 = c_common_get_narrower (op1, &unsignedp1); | |
b30f223b | 4119 | |
126e6609 JJ |
4120 | /* If primopN is first sign-extended from primopN's precision to opN's |
4121 | precision, then zero-extended from opN's precision to | |
4122 | *restype_ptr precision, shortenings might be invalid. */ | |
4123 | if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0)) | |
4124 | && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr) | |
4125 | && !unsignedp0 | |
4126 | && TYPE_UNSIGNED (TREE_TYPE (op0))) | |
4127 | primop0 = op0; | |
4128 | if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1)) | |
4129 | && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr) | |
4130 | && !unsignedp1 | |
4131 | && TYPE_UNSIGNED (TREE_TYPE (op1))) | |
4132 | primop1 = op1; | |
4133 | ||
b30f223b RS |
4134 | /* Handle the case that OP0 does not *contain* a conversion |
4135 | but it *requires* conversion to FINAL_TYPE. */ | |
4136 | ||
4137 | if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) | |
8df83eae | 4138 | unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
b30f223b | 4139 | if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) |
8df83eae | 4140 | unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1)); |
b30f223b RS |
4141 | |
4142 | /* If one of the operands must be floated, we cannot optimize. */ | |
4143 | real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; | |
4144 | real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; | |
4145 | ||
4146 | /* If first arg is constant, swap the args (changing operation | |
5af6001b RK |
4147 | so value is preserved), for canonicalization. Don't do this if |
4148 | the second arg is 0. */ | |
b30f223b | 4149 | |
5af6001b | 4150 | if (TREE_CONSTANT (primop0) |
ab22c1fa CF |
4151 | && !integer_zerop (primop1) && !real_zerop (primop1) |
4152 | && !fixed_zerop (primop1)) | |
b30f223b | 4153 | { |
b3694847 SS |
4154 | tree tem = primop0; |
4155 | int temi = unsignedp0; | |
b30f223b RS |
4156 | primop0 = primop1; |
4157 | primop1 = tem; | |
4158 | tem = op0; | |
4159 | op0 = op1; | |
4160 | op1 = tem; | |
4161 | *op0_ptr = op0; | |
4162 | *op1_ptr = op1; | |
4163 | unsignedp0 = unsignedp1; | |
4164 | unsignedp1 = temi; | |
4165 | temi = real1; | |
4166 | real1 = real2; | |
4167 | real2 = temi; | |
4168 | ||
4169 | switch (code) | |
4170 | { | |
4171 | case LT_EXPR: | |
4172 | code = GT_EXPR; | |
4173 | break; | |
4174 | case GT_EXPR: | |
4175 | code = LT_EXPR; | |
4176 | break; | |
4177 | case LE_EXPR: | |
4178 | code = GE_EXPR; | |
4179 | break; | |
4180 | case GE_EXPR: | |
4181 | code = LE_EXPR; | |
4182 | break; | |
6d819282 MK |
4183 | default: |
4184 | break; | |
b30f223b RS |
4185 | } |
4186 | *rescode_ptr = code; | |
4187 | } | |
4188 | ||
4189 | /* If comparing an integer against a constant more bits wide, | |
4190 | maybe we can deduce a value of 1 or 0 independent of the data. | |
4191 | Or else truncate the constant now | |
4192 | rather than extend the variable at run time. | |
4193 | ||
4194 | This is only interesting if the constant is the wider arg. | |
4195 | Also, it is not safe if the constant is unsigned and the | |
4196 | variable arg is signed, since in this case the variable | |
4197 | would be sign-extended and then regarded as unsigned. | |
4198 | Our technique fails in this case because the lowest/highest | |
4199 | possible unsigned results don't follow naturally from the | |
4200 | lowest/highest possible values of the variable operand. | |
4201 | For just EQ_EXPR and NE_EXPR there is another technique that | |
4202 | could be used: see if the constant can be faithfully represented | |
4203 | in the other operand's type, by truncating it and reextending it | |
4204 | and see if that preserves the constant's value. */ | |
4205 | ||
4206 | if (!real1 && !real2 | |
ab22c1fa | 4207 | && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE |
b30f223b RS |
4208 | && TREE_CODE (primop1) == INTEGER_CST |
4209 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) | |
4210 | { | |
4211 | int min_gt, max_gt, min_lt, max_lt; | |
4212 | tree maxval, minval; | |
4213 | /* 1 if comparison is nominally unsigned. */ | |
8df83eae | 4214 | int unsignedp = TYPE_UNSIGNED (*restype_ptr); |
b30f223b RS |
4215 | tree val; |
4216 | ||
ceef8ce4 NB |
4217 | type = c_common_signed_or_unsigned_type (unsignedp0, |
4218 | TREE_TYPE (primop0)); | |
8bbd5685 | 4219 | |
b30f223b RS |
4220 | maxval = TYPE_MAX_VALUE (type); |
4221 | minval = TYPE_MIN_VALUE (type); | |
4222 | ||
4223 | if (unsignedp && !unsignedp0) | |
ceef8ce4 | 4224 | *restype_ptr = c_common_signed_type (*restype_ptr); |
b30f223b RS |
4225 | |
4226 | if (TREE_TYPE (primop1) != *restype_ptr) | |
fae1b38d | 4227 | { |
af9c6659 NS |
4228 | /* Convert primop1 to target type, but do not introduce |
4229 | additional overflow. We know primop1 is an int_cst. */ | |
807e902e KZ |
4230 | primop1 = force_fit_type (*restype_ptr, |
4231 | wide_int::from | |
4232 | (primop1, | |
4233 | TYPE_PRECISION (*restype_ptr), | |
4234 | TYPE_SIGN (TREE_TYPE (primop1))), | |
4235 | 0, TREE_OVERFLOW (primop1)); | |
fae1b38d | 4236 | } |
b30f223b RS |
4237 | if (type != *restype_ptr) |
4238 | { | |
4239 | minval = convert (*restype_ptr, minval); | |
4240 | maxval = convert (*restype_ptr, maxval); | |
4241 | } | |
4242 | ||
807e902e KZ |
4243 | min_gt = tree_int_cst_lt (primop1, minval); |
4244 | max_gt = tree_int_cst_lt (primop1, maxval); | |
4245 | min_lt = tree_int_cst_lt (minval, primop1); | |
4246 | max_lt = tree_int_cst_lt (maxval, primop1); | |
b30f223b RS |
4247 | |
4248 | val = 0; | |
4249 | /* This used to be a switch, but Genix compiler can't handle that. */ | |
4250 | if (code == NE_EXPR) | |
4251 | { | |
4252 | if (max_lt || min_gt) | |
de7df9eb | 4253 | val = truthvalue_true_node; |
b30f223b RS |
4254 | } |
4255 | else if (code == EQ_EXPR) | |
4256 | { | |
4257 | if (max_lt || min_gt) | |
de7df9eb | 4258 | val = truthvalue_false_node; |
b30f223b RS |
4259 | } |
4260 | else if (code == LT_EXPR) | |
4261 | { | |
4262 | if (max_lt) | |
de7df9eb | 4263 | val = truthvalue_true_node; |
b30f223b | 4264 | if (!min_lt) |
de7df9eb | 4265 | val = truthvalue_false_node; |
b30f223b RS |
4266 | } |
4267 | else if (code == GT_EXPR) | |
4268 | { | |
4269 | if (min_gt) | |
de7df9eb | 4270 | val = truthvalue_true_node; |
b30f223b | 4271 | if (!max_gt) |
de7df9eb | 4272 | val = truthvalue_false_node; |
b30f223b RS |
4273 | } |
4274 | else if (code == LE_EXPR) | |
4275 | { | |
4276 | if (!max_gt) | |
de7df9eb | 4277 | val = truthvalue_true_node; |
b30f223b | 4278 | if (min_gt) |
de7df9eb | 4279 | val = truthvalue_false_node; |
b30f223b RS |
4280 | } |
4281 | else if (code == GE_EXPR) | |
4282 | { | |
4283 | if (!min_lt) | |
de7df9eb | 4284 | val = truthvalue_true_node; |
b30f223b | 4285 | if (max_lt) |
de7df9eb | 4286 | val = truthvalue_false_node; |
b30f223b RS |
4287 | } |
4288 | ||
4289 | /* If primop0 was sign-extended and unsigned comparison specd, | |
4290 | we did a signed comparison above using the signed type bounds. | |
4291 | But the comparison we output must be unsigned. | |
4292 | ||
4293 | Also, for inequalities, VAL is no good; but if the signed | |
4294 | comparison had *any* fixed result, it follows that the | |
4295 | unsigned comparison just tests the sign in reverse | |
4296 | (positive values are LE, negative ones GE). | |
4297 | So we can generate an unsigned comparison | |
4298 | against an extreme value of the signed type. */ | |
4299 | ||
4300 | if (unsignedp && !unsignedp0) | |
4301 | { | |
4302 | if (val != 0) | |
4303 | switch (code) | |
4304 | { | |
4305 | case LT_EXPR: | |
4306 | case GE_EXPR: | |
4307 | primop1 = TYPE_MIN_VALUE (type); | |
4308 | val = 0; | |
4309 | break; | |
4310 | ||
4311 | case LE_EXPR: | |
4312 | case GT_EXPR: | |
4313 | primop1 = TYPE_MAX_VALUE (type); | |
4314 | val = 0; | |
4315 | break; | |
6d819282 MK |
4316 | |
4317 | default: | |
4318 | break; | |
b30f223b | 4319 | } |
12753674 | 4320 | type = c_common_unsigned_type (type); |
b30f223b RS |
4321 | } |
4322 | ||
ca7e759d | 4323 | if (TREE_CODE (primop0) != INTEGER_CST) |
b30f223b | 4324 | { |
de7df9eb | 4325 | if (val == truthvalue_false_node) |
50f305ca MLI |
4326 | warning_at (loc, OPT_Wtype_limits, |
4327 | "comparison is always false due to limited range of data type"); | |
de7df9eb | 4328 | if (val == truthvalue_true_node) |
50f305ca MLI |
4329 | warning_at (loc, OPT_Wtype_limits, |
4330 | "comparison is always true due to limited range of data type"); | |
b30f223b RS |
4331 | } |
4332 | ||
4333 | if (val != 0) | |
4334 | { | |
4335 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4336 | if (TREE_SIDE_EFFECTS (primop0)) | |
53fb4de3 | 4337 | return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); |
b30f223b RS |
4338 | return val; |
4339 | } | |
4340 | ||
4341 | /* Value is not predetermined, but do the comparison | |
4342 | in the type of the operand that is not constant. | |
4343 | TYPE is already properly set. */ | |
4344 | } | |
9a8ce21f JG |
4345 | |
4346 | /* If either arg is decimal float and the other is float, find the | |
4347 | proper common type to use for comparison. */ | |
6f450181 RB |
4348 | else if (real1 && real2 |
4349 | && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0))) | |
4350 | && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))) | |
4351 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4352 | ||
4353 | /* If either arg is decimal float and the other is float, fail. */ | |
9a8ce21f JG |
4354 | else if (real1 && real2 |
4355 | && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0))) | |
4356 | || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))) | |
6f450181 | 4357 | return 0; |
9a8ce21f | 4358 | |
b30f223b | 4359 | else if (real1 && real2 |
766f6c30 RS |
4360 | && (TYPE_PRECISION (TREE_TYPE (primop0)) |
4361 | == TYPE_PRECISION (TREE_TYPE (primop1)))) | |
b30f223b RS |
4362 | type = TREE_TYPE (primop0); |
4363 | ||
4364 | /* If args' natural types are both narrower than nominal type | |
4365 | and both extend in the same manner, compare them | |
4366 | in the type of the wider arg. | |
4367 | Otherwise must actually extend both to the nominal | |
4368 | common type lest different ways of extending | |
4369 | alter the result. | |
4370 | (eg, (short)-1 == (unsigned short)-1 should be 0.) */ | |
4371 | ||
4372 | else if (unsignedp0 == unsignedp1 && real1 == real2 | |
4373 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) | |
4374 | && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) | |
4375 | { | |
4376 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
ceef8ce4 | 4377 | type = c_common_signed_or_unsigned_type (unsignedp0 |
8df83eae | 4378 | || TYPE_UNSIGNED (*restype_ptr), |
ceef8ce4 | 4379 | type); |
b30f223b RS |
4380 | /* Make sure shorter operand is extended the right way |
4381 | to match the longer operand. */ | |
ceef8ce4 NB |
4382 | primop0 |
4383 | = convert (c_common_signed_or_unsigned_type (unsignedp0, | |
4384 | TREE_TYPE (primop0)), | |
4385 | primop0); | |
4386 | primop1 | |
4387 | = convert (c_common_signed_or_unsigned_type (unsignedp1, | |
4388 | TREE_TYPE (primop1)), | |
4389 | primop1); | |
b30f223b RS |
4390 | } |
4391 | else | |
4392 | { | |
4393 | /* Here we must do the comparison on the nominal type | |
4394 | using the args exactly as we received them. */ | |
4395 | type = *restype_ptr; | |
4396 | primop0 = op0; | |
4397 | primop1 = op1; | |
4398 | ||
4399 | if (!real1 && !real2 && integer_zerop (primop1) | |
8df83eae | 4400 | && TYPE_UNSIGNED (*restype_ptr)) |
b30f223b RS |
4401 | { |
4402 | tree value = 0; | |
50f305ca MLI |
4403 | /* All unsigned values are >= 0, so we warn. However, |
4404 | if OP0 is a constant that is >= 0, the signedness of | |
4405 | the comparison isn't an issue, so suppress the | |
4406 | warning. */ | |
4407 | bool warn = | |
8400e75e | 4408 | warn_type_limits && !in_system_header_at (loc) |
50f305ca MLI |
4409 | && !(TREE_CODE (primop0) == INTEGER_CST |
4410 | && !TREE_OVERFLOW (convert (c_common_signed_type (type), | |
4411 | primop0))) | |
4412 | /* Do not warn for enumeration types. */ | |
4413 | && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE); | |
4414 | ||
b30f223b RS |
4415 | switch (code) |
4416 | { | |
4417 | case GE_EXPR: | |
50f305ca MLI |
4418 | if (warn) |
4419 | warning_at (loc, OPT_Wtype_limits, | |
4420 | "comparison of unsigned expression >= 0 is always true"); | |
de7df9eb | 4421 | value = truthvalue_true_node; |
b30f223b RS |
4422 | break; |
4423 | ||
4424 | case LT_EXPR: | |
50f305ca MLI |
4425 | if (warn) |
4426 | warning_at (loc, OPT_Wtype_limits, | |
4427 | "comparison of unsigned expression < 0 is always false"); | |
de7df9eb | 4428 | value = truthvalue_false_node; |
6d819282 MK |
4429 | break; |
4430 | ||
4431 | default: | |
4432 | break; | |
b30f223b RS |
4433 | } |
4434 | ||
4435 | if (value != 0) | |
4436 | { | |
4437 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4438 | if (TREE_SIDE_EFFECTS (primop0)) | |
53fb4de3 RS |
4439 | return build2 (COMPOUND_EXPR, TREE_TYPE (value), |
4440 | primop0, value); | |
b30f223b RS |
4441 | return value; |
4442 | } | |
4443 | } | |
4444 | } | |
4445 | ||
4446 | *op0_ptr = convert (type, primop0); | |
4447 | *op1_ptr = convert (type, primop1); | |
4448 | ||
de7df9eb | 4449 | *restype_ptr = truthvalue_type_node; |
b30f223b RS |
4450 | |
4451 | return 0; | |
4452 | } | |
4453 | \f | |
7552da58 JJ |
4454 | /* Return a tree for the sum or difference (RESULTCODE says which) |
4455 | of pointer PTROP and integer INTOP. */ | |
4456 | ||
4457 | tree | |
db3927fb | 4458 | pointer_int_sum (location_t loc, enum tree_code resultcode, |
fd9b0f32 | 4459 | tree ptrop, tree intop, bool complain) |
7552da58 | 4460 | { |
6ac01510 | 4461 | tree size_exp, ret; |
7552da58 | 4462 | |
7552da58 | 4463 | /* The result is a pointer of the same type that is being added. */ |
7552da58 JJ |
4464 | tree result_type = TREE_TYPE (ptrop); |
4465 | ||
4466 | if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) | |
4467 | { | |
fd9b0f32 PC |
4468 | if (complain && warn_pointer_arith) |
4469 | pedwarn (loc, OPT_Wpointer_arith, | |
4470 | "pointer of type %<void *%> used in arithmetic"); | |
4471 | else if (!complain) | |
4472 | return error_mark_node; | |
7552da58 JJ |
4473 | size_exp = integer_one_node; |
4474 | } | |
4475 | else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) | |
4476 | { | |
fd9b0f32 PC |
4477 | if (complain && warn_pointer_arith) |
4478 | pedwarn (loc, OPT_Wpointer_arith, | |
4479 | "pointer to a function used in arithmetic"); | |
4480 | else if (!complain) | |
4481 | return error_mark_node; | |
7552da58 JJ |
4482 | size_exp = integer_one_node; |
4483 | } | |
7552da58 JJ |
4484 | else |
4485 | size_exp = size_in_bytes (TREE_TYPE (result_type)); | |
4486 | ||
6ac01510 ILT |
4487 | /* We are manipulating pointer values, so we don't need to warn |
4488 | about relying on undefined signed overflow. We disable the | |
4489 | warning here because we use integer types so fold won't know that | |
4490 | they are really pointers. */ | |
4491 | fold_defer_overflow_warnings (); | |
4492 | ||
7552da58 JJ |
4493 | /* If what we are about to multiply by the size of the elements |
4494 | contains a constant term, apply distributive law | |
4495 | and multiply that constant term separately. | |
4496 | This helps produce common subexpressions. */ | |
7552da58 | 4497 | if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) |
3f75a254 | 4498 | && !TREE_CONSTANT (intop) |
7552da58 JJ |
4499 | && TREE_CONSTANT (TREE_OPERAND (intop, 1)) |
4500 | && TREE_CONSTANT (size_exp) | |
4501 | /* If the constant comes from pointer subtraction, | |
4502 | skip this optimization--it would cause an error. */ | |
4503 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE | |
4504 | /* If the constant is unsigned, and smaller than the pointer size, | |
4505 | then we must skip this optimization. This is because it could cause | |
4506 | an overflow error if the constant is negative but INTOP is not. */ | |
3f75a254 | 4507 | && (!TYPE_UNSIGNED (TREE_TYPE (intop)) |
7552da58 JJ |
4508 | || (TYPE_PRECISION (TREE_TYPE (intop)) |
4509 | == TYPE_PRECISION (TREE_TYPE (ptrop))))) | |
4510 | { | |
4511 | enum tree_code subcode = resultcode; | |
4512 | tree int_type = TREE_TYPE (intop); | |
4513 | if (TREE_CODE (intop) == MINUS_EXPR) | |
4514 | subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); | |
4515 | /* Convert both subexpression types to the type of intop, | |
4516 | because weird cases involving pointer arithmetic | |
4517 | can result in a sum or difference with different type args. */ | |
ba47d38d AH |
4518 | ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)), |
4519 | subcode, ptrop, | |
7552da58 JJ |
4520 | convert (int_type, TREE_OPERAND (intop, 1)), 1); |
4521 | intop = convert (int_type, TREE_OPERAND (intop, 0)); | |
4522 | } | |
4523 | ||
4524 | /* Convert the integer argument to a type the same size as sizetype | |
4525 | so the multiply won't overflow spuriously. */ | |
7552da58 | 4526 | if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) |
8df83eae | 4527 | || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype)) |
35b1a6fa | 4528 | intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype), |
8df83eae | 4529 | TYPE_UNSIGNED (sizetype)), intop); |
7552da58 JJ |
4530 | |
4531 | /* Replace the integer argument with a suitable product by the object size. | |
9e9ef331 | 4532 | Do this multiplication as signed, then convert to the appropriate type |
65de6659 | 4533 | for the pointer operation and disregard an overflow that occurred only |
9e9ef331 EB |
4534 | because of the sign-extension change in the latter conversion. */ |
4535 | { | |
4536 | tree t = build_binary_op (loc, | |
4537 | MULT_EXPR, intop, | |
4538 | convert (TREE_TYPE (intop), size_exp), 1); | |
4539 | intop = convert (sizetype, t); | |
4540 | if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t)) | |
807e902e | 4541 | intop = wide_int_to_tree (TREE_TYPE (intop), intop); |
9e9ef331 | 4542 | } |
5be014d5 | 4543 | |
280f1ffa | 4544 | /* Create the sum or difference. */ |
5be014d5 | 4545 | if (resultcode == MINUS_EXPR) |
db3927fb | 4546 | intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop); |
7552da58 | 4547 | |
5d49b6a7 | 4548 | ret = fold_build_pointer_plus_loc (loc, ptrop, intop); |
6ac01510 ILT |
4549 | |
4550 | fold_undefer_and_ignore_overflow_warnings (); | |
4551 | ||
4552 | return ret; | |
7552da58 JJ |
4553 | } |
4554 | \f | |
e5a94231 JM |
4555 | /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded |
4556 | and if NON_CONST is known not to be permitted in an evaluated part | |
4557 | of a constant expression. */ | |
4558 | ||
4559 | tree | |
4560 | c_wrap_maybe_const (tree expr, bool non_const) | |
4561 | { | |
4562 | bool nowarning = TREE_NO_WARNING (expr); | |
4563 | location_t loc = EXPR_LOCATION (expr); | |
4564 | ||
4565 | /* This should never be called for C++. */ | |
4566 | if (c_dialect_cxx ()) | |
4567 | gcc_unreachable (); | |
4568 | ||
4569 | /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */ | |
4570 | STRIP_TYPE_NOPS (expr); | |
4571 | expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr); | |
4572 | C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const; | |
4573 | if (nowarning) | |
4574 | TREE_NO_WARNING (expr) = 1; | |
4575 | protected_set_expr_location (expr, loc); | |
4576 | ||
4577 | return expr; | |
4578 | } | |
4579 | ||
928c19bb JM |
4580 | /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but |
4581 | for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR | |
4582 | around the SAVE_EXPR if needed so that c_fully_fold does not need | |
4583 | to look inside SAVE_EXPRs. */ | |
4584 | ||
4585 | tree | |
4586 | c_save_expr (tree expr) | |
4587 | { | |
4588 | bool maybe_const = true; | |
4589 | if (c_dialect_cxx ()) | |
4590 | return save_expr (expr); | |
4591 | expr = c_fully_fold (expr, false, &maybe_const); | |
4592 | expr = save_expr (expr); | |
4593 | if (!maybe_const) | |
e5a94231 | 4594 | expr = c_wrap_maybe_const (expr, true); |
928c19bb JM |
4595 | return expr; |
4596 | } | |
4597 | ||
b3c6d2ea ILT |
4598 | /* Return whether EXPR is a declaration whose address can never be |
4599 | NULL. */ | |
4600 | ||
4601 | bool | |
58f9752a | 4602 | decl_with_nonnull_addr_p (const_tree expr) |
b3c6d2ea ILT |
4603 | { |
4604 | return (DECL_P (expr) | |
4605 | && (TREE_CODE (expr) == PARM_DECL | |
4606 | || TREE_CODE (expr) == LABEL_DECL | |
4607 | || !DECL_WEAK (expr))); | |
4608 | } | |
4609 | ||
b30f223b | 4610 | /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, |
85498824 JM |
4611 | or for an `if' or `while' statement or ?..: exp. It should already |
4612 | have been validated to be of suitable type; otherwise, a bad | |
4613 | diagnostic may result. | |
b30f223b | 4614 | |
ba47d38d AH |
4615 | The EXPR is located at LOCATION. |
4616 | ||
b30f223b RS |
4617 | This preparation consists of taking the ordinary |
4618 | representation of an expression expr and producing a valid tree | |
4619 | boolean expression describing whether expr is nonzero. We could | |
de7df9eb | 4620 | simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1), |
b30f223b RS |
4621 | but we optimize comparisons, &&, ||, and !. |
4622 | ||
de7df9eb | 4623 | The resulting type should always be `truthvalue_type_node'. */ |
b30f223b RS |
4624 | |
4625 | tree | |
ba47d38d | 4626 | c_common_truthvalue_conversion (location_t location, tree expr) |
b30f223b | 4627 | { |
b30f223b RS |
4628 | switch (TREE_CODE (expr)) |
4629 | { | |
d1a7edaf | 4630 | case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR: |
6f312d18 ZW |
4631 | case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: |
4632 | case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR: | |
4633 | case ORDERED_EXPR: case UNORDERED_EXPR: | |
90ec750d RS |
4634 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4635 | return expr; | |
c2255bc4 | 4636 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
90ec750d | 4637 | TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); |
c2255bc4 | 4638 | goto ret; |
90ec750d | 4639 | |
b30f223b RS |
4640 | case TRUTH_ANDIF_EXPR: |
4641 | case TRUTH_ORIF_EXPR: | |
4642 | case TRUTH_AND_EXPR: | |
4643 | case TRUTH_OR_EXPR: | |
9379fac9 | 4644 | case TRUTH_XOR_EXPR: |
90ec750d RS |
4645 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4646 | return expr; | |
c2255bc4 | 4647 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
b8698a0f | 4648 | c_common_truthvalue_conversion (location, |
c2255bc4 AH |
4649 | TREE_OPERAND (expr, 0)), |
4650 | c_common_truthvalue_conversion (location, | |
4651 | TREE_OPERAND (expr, 1))); | |
4652 | goto ret; | |
18c0f675 | 4653 | |
18d00205 | 4654 | case TRUTH_NOT_EXPR: |
90ec750d RS |
4655 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4656 | return expr; | |
c2255bc4 AH |
4657 | expr = build1 (TREE_CODE (expr), truthvalue_type_node, |
4658 | c_common_truthvalue_conversion (location, | |
4659 | TREE_OPERAND (expr, 0))); | |
4660 | goto ret; | |
18d00205 | 4661 | |
b30f223b RS |
4662 | case ERROR_MARK: |
4663 | return expr; | |
4664 | ||
4665 | case INTEGER_CST: | |
d95787e6 RS |
4666 | return integer_zerop (expr) ? truthvalue_false_node |
4667 | : truthvalue_true_node; | |
b30f223b RS |
4668 | |
4669 | case REAL_CST: | |
010c4d9c RS |
4670 | return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0) |
4671 | ? truthvalue_true_node | |
4672 | : truthvalue_false_node; | |
b30f223b | 4673 | |
ab22c1fa CF |
4674 | case FIXED_CST: |
4675 | return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr), | |
4676 | &FCONST0 (TYPE_MODE (TREE_TYPE (expr)))) | |
4677 | ? truthvalue_true_node | |
4678 | : truthvalue_false_node; | |
4679 | ||
90ec750d | 4680 | case FUNCTION_DECL: |
c9f9eb5d | 4681 | expr = build_unary_op (location, ADDR_EXPR, expr, 0); |
90ec750d RS |
4682 | /* Fall through. */ |
4683 | ||
b30f223b | 4684 | case ADDR_EXPR: |
1998463c | 4685 | { |
f6f08360 | 4686 | tree inner = TREE_OPERAND (expr, 0); |
b3c6d2ea | 4687 | if (decl_with_nonnull_addr_p (inner)) |
1998463c | 4688 | { |
b3c6d2ea | 4689 | /* Common Ada/Pascal programmer's mistake. */ |
ba47d38d AH |
4690 | warning_at (location, |
4691 | OPT_Waddress, | |
4692 | "the address of %qD will always evaluate as %<true%>", | |
4693 | inner); | |
1998463c SB |
4694 | return truthvalue_true_node; |
4695 | } | |
33766b66 | 4696 | break; |
1998463c | 4697 | } |
b30f223b | 4698 | |
766f6c30 | 4699 | case COMPLEX_EXPR: |
c2255bc4 | 4700 | expr = build_binary_op (EXPR_LOCATION (expr), |
ba47d38d | 4701 | (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) |
b839fb3f | 4702 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
ba47d38d AH |
4703 | c_common_truthvalue_conversion (location, |
4704 | TREE_OPERAND (expr, 0)), | |
4705 | c_common_truthvalue_conversion (location, | |
4706 | TREE_OPERAND (expr, 1)), | |
766f6c30 | 4707 | 0); |
c2255bc4 | 4708 | goto ret; |
766f6c30 | 4709 | |
b30f223b RS |
4710 | case NEGATE_EXPR: |
4711 | case ABS_EXPR: | |
4712 | case FLOAT_EXPR: | |
8ce94e44 | 4713 | case EXCESS_PRECISION_EXPR: |
da7d8304 | 4714 | /* These don't change whether an object is nonzero or zero. */ |
ba47d38d | 4715 | return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); |
b30f223b RS |
4716 | |
4717 | case LROTATE_EXPR: | |
4718 | case RROTATE_EXPR: | |
da7d8304 | 4719 | /* These don't change whether an object is zero or nonzero, but |
b30f223b RS |
4720 | we can't ignore them if their second arg has side-effects. */ |
4721 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) | |
c2255bc4 AH |
4722 | { |
4723 | expr = build2 (COMPOUND_EXPR, truthvalue_type_node, | |
4724 | TREE_OPERAND (expr, 1), | |
b8698a0f | 4725 | c_common_truthvalue_conversion |
c2255bc4 AH |
4726 | (location, TREE_OPERAND (expr, 0))); |
4727 | goto ret; | |
4728 | } | |
b30f223b | 4729 | else |
ba47d38d AH |
4730 | return c_common_truthvalue_conversion (location, |
4731 | TREE_OPERAND (expr, 0)); | |
b57062ca | 4732 | |
b30f223b RS |
4733 | case COND_EXPR: |
4734 | /* Distribute the conversion into the arms of a COND_EXPR. */ | |
928c19bb | 4735 | if (c_dialect_cxx ()) |
c2255bc4 | 4736 | { |
4cc4f2f4 JJ |
4737 | tree op1 = TREE_OPERAND (expr, 1); |
4738 | tree op2 = TREE_OPERAND (expr, 2); | |
4739 | /* In C++ one of the arms might have void type if it is throw. */ | |
4740 | if (!VOID_TYPE_P (TREE_TYPE (op1))) | |
4741 | op1 = c_common_truthvalue_conversion (location, op1); | |
4742 | if (!VOID_TYPE_P (TREE_TYPE (op2))) | |
4743 | op2 = c_common_truthvalue_conversion (location, op2); | |
db3927fb | 4744 | expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node, |
4cc4f2f4 | 4745 | TREE_OPERAND (expr, 0), op1, op2); |
c2255bc4 AH |
4746 | goto ret; |
4747 | } | |
928c19bb | 4748 | else |
c2255bc4 AH |
4749 | { |
4750 | /* Folding will happen later for C. */ | |
4751 | expr = build3 (COND_EXPR, truthvalue_type_node, | |
4752 | TREE_OPERAND (expr, 0), | |
4753 | c_common_truthvalue_conversion (location, | |
4754 | TREE_OPERAND (expr, 1)), | |
4755 | c_common_truthvalue_conversion (location, | |
4756 | TREE_OPERAND (expr, 2))); | |
4757 | goto ret; | |
4758 | } | |
b30f223b | 4759 | |
1043771b | 4760 | CASE_CONVERT: |
1ee44b26 JM |
4761 | { |
4762 | tree totype = TREE_TYPE (expr); | |
4763 | tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0)); | |
4764 | ||
4765 | /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, | |
4766 | since that affects how `default_conversion' will behave. */ | |
4767 | if (TREE_CODE (totype) == REFERENCE_TYPE | |
4768 | || TREE_CODE (fromtype) == REFERENCE_TYPE) | |
4769 | break; | |
4770 | /* Don't strip a conversion from C++0x scoped enum, since they | |
4771 | don't implicitly convert to other types. */ | |
4772 | if (TREE_CODE (fromtype) == ENUMERAL_TYPE | |
4773 | && ENUM_IS_SCOPED (fromtype)) | |
4774 | break; | |
4775 | /* If this isn't narrowing the argument, we can ignore it. */ | |
4776 | if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype)) | |
4777 | return c_common_truthvalue_conversion (location, | |
4778 | TREE_OPERAND (expr, 0)); | |
4779 | } | |
b30f223b RS |
4780 | break; |
4781 | ||
e2aab13d | 4782 | case MODIFY_EXPR: |
fbc8d2d3 ILT |
4783 | if (!TREE_NO_WARNING (expr) |
4784 | && warn_parentheses) | |
4785 | { | |
4786 | warning (OPT_Wparentheses, | |
4787 | "suggest parentheses around assignment used as truth value"); | |
4788 | TREE_NO_WARNING (expr) = 1; | |
4789 | } | |
e2aab13d | 4790 | break; |
b57062ca | 4791 | |
6d819282 MK |
4792 | default: |
4793 | break; | |
b30f223b RS |
4794 | } |
4795 | ||
f0b996c5 | 4796 | if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) |
f0b8d9aa | 4797 | { |
5386338c | 4798 | tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr)); |
c2255bc4 | 4799 | expr = (build_binary_op |
ba47d38d AH |
4800 | (EXPR_LOCATION (expr), |
4801 | (TREE_SIDE_EFFECTS (expr) | |
f0b8d9aa | 4802 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
c9f9eb5d AH |
4803 | c_common_truthvalue_conversion |
4804 | (location, | |
4805 | build_unary_op (location, REALPART_EXPR, t, 0)), | |
4806 | c_common_truthvalue_conversion | |
4807 | (location, | |
4808 | build_unary_op (location, IMAGPART_EXPR, t, 0)), | |
f0b8d9aa | 4809 | 0)); |
c2255bc4 | 4810 | goto ret; |
f0b8d9aa | 4811 | } |
f0b996c5 | 4812 | |
ab22c1fa CF |
4813 | if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE) |
4814 | { | |
4815 | tree fixed_zero_node = build_fixed (TREE_TYPE (expr), | |
4816 | FCONST0 (TYPE_MODE | |
4817 | (TREE_TYPE (expr)))); | |
ca80e52b | 4818 | return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1); |
ab22c1fa | 4819 | } |
c2255bc4 AH |
4820 | else |
4821 | return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1); | |
ab22c1fa | 4822 | |
c2255bc4 AH |
4823 | ret: |
4824 | protected_set_expr_location (expr, location); | |
4825 | return expr; | |
b30f223b RS |
4826 | } |
4827 | \f | |
9bc15050 RG |
4828 | static void def_builtin_1 (enum built_in_function fncode, |
4829 | const char *name, | |
4830 | enum built_in_class fnclass, | |
4831 | tree fntype, tree libtype, | |
4832 | bool both_p, bool fallback_p, bool nonansi_p, | |
4833 | tree fnattrs, bool implicit_p); | |
fc2aaf30 | 4834 | |
3932261a MM |
4835 | |
4836 | /* Apply the TYPE_QUALS to the new DECL. */ | |
4837 | ||
4838 | void | |
35b1a6fa | 4839 | c_apply_type_quals_to_decl (int type_quals, tree decl) |
3932261a | 4840 | { |
4b011bbf | 4841 | tree type = TREE_TYPE (decl); |
9f63daea | 4842 | |
5a6159dd AP |
4843 | if (type == error_mark_node) |
4844 | return; | |
4b011bbf | 4845 | |
329af3c7 JM |
4846 | if ((type_quals & TYPE_QUAL_CONST) |
4847 | || (type && TREE_CODE (type) == REFERENCE_TYPE)) | |
4848 | /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr | |
4849 | constructor can produce constant init, so rely on cp_finish_decl to | |
4850 | clear TREE_READONLY if the variable has non-constant init. */ | |
3932261a MM |
4851 | TREE_READONLY (decl) = 1; |
4852 | if (type_quals & TYPE_QUAL_VOLATILE) | |
4853 | { | |
4854 | TREE_SIDE_EFFECTS (decl) = 1; | |
4855 | TREE_THIS_VOLATILE (decl) = 1; | |
4856 | } | |
6946bc60 | 4857 | if (type_quals & TYPE_QUAL_RESTRICT) |
3932261a | 4858 | { |
4b011bbf JM |
4859 | while (type && TREE_CODE (type) == ARRAY_TYPE) |
4860 | /* Allow 'restrict' on arrays of pointers. | |
4861 | FIXME currently we just ignore it. */ | |
4862 | type = TREE_TYPE (type); | |
4863 | if (!type | |
4864 | || !POINTER_TYPE_P (type) | |
4865 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))) | |
bda67431 | 4866 | error ("invalid use of %<restrict%>"); |
3932261a MM |
4867 | } |
4868 | } | |
4869 | ||
2a22f99c TS |
4870 | struct c_type_hasher : ggc_hasher<tree> |
4871 | { | |
4872 | static hashval_t hash (tree); | |
4873 | static bool equal (tree, tree); | |
4874 | }; | |
4875 | ||
6de9cd9a DN |
4876 | /* Hash function for the problem of multiple type definitions in |
4877 | different files. This must hash all types that will compare | |
4878 | equal via comptypes to the same value. In practice it hashes | |
9cf737f8 | 4879 | on some of the simple stuff and leaves the details to comptypes. */ |
6de9cd9a | 4880 | |
2a22f99c TS |
4881 | hashval_t |
4882 | c_type_hasher::hash (tree t) | |
6de9cd9a | 4883 | { |
a19e4d44 | 4884 | int n_elements; |
6de9cd9a | 4885 | int shift, size; |
6de9cd9a DN |
4886 | tree t2; |
4887 | switch (TREE_CODE (t)) | |
4888 | { | |
8c27b7d4 | 4889 | /* For pointers, hash on pointee type plus some swizzling. */ |
325c3691 | 4890 | case POINTER_TYPE: |
2a22f99c | 4891 | return hash (TREE_TYPE (t)) ^ 0x3003003; |
325c3691 RH |
4892 | /* Hash on number of elements and total size. */ |
4893 | case ENUMERAL_TYPE: | |
4894 | shift = 3; | |
4895 | t2 = TYPE_VALUES (t); | |
4896 | break; | |
4897 | case RECORD_TYPE: | |
4898 | shift = 0; | |
4899 | t2 = TYPE_FIELDS (t); | |
4900 | break; | |
4901 | case QUAL_UNION_TYPE: | |
4902 | shift = 1; | |
4903 | t2 = TYPE_FIELDS (t); | |
4904 | break; | |
4905 | case UNION_TYPE: | |
4906 | shift = 2; | |
4907 | t2 = TYPE_FIELDS (t); | |
4908 | break; | |
4909 | default: | |
366de0ce | 4910 | gcc_unreachable (); |
6de9cd9a | 4911 | } |
a19e4d44 NF |
4912 | /* FIXME: We want to use a DECL_CHAIN iteration method here, but |
4913 | TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */ | |
4914 | n_elements = list_length (t2); | |
6fc3c3c0 TT |
4915 | /* We might have a VLA here. */ |
4916 | if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST) | |
4917 | size = 0; | |
4918 | else | |
4919 | size = TREE_INT_CST_LOW (TYPE_SIZE (t)); | |
a19e4d44 | 4920 | return ((size << 24) | (n_elements << shift)); |
6de9cd9a DN |
4921 | } |
4922 | ||
2a22f99c TS |
4923 | bool |
4924 | c_type_hasher::equal (tree t1, tree t2) | |
4925 | { | |
4926 | return lang_hooks.types_compatible_p (t1, t2); | |
4927 | } | |
4928 | ||
4929 | static GTY(()) hash_table<c_type_hasher> *type_hash_table; | |
4fe52ce9 | 4930 | |
41472af8 | 4931 | /* Return the typed-based alias set for T, which may be an expression |
3bdf5ad1 | 4932 | or a type. Return -1 if we don't do anything special. */ |
41472af8 | 4933 | |
4862826d | 4934 | alias_set_type |
35b1a6fa | 4935 | c_common_get_alias_set (tree t) |
41472af8 | 4936 | { |
08bc2431 | 4937 | tree u; |
35b1a6fa | 4938 | |
cb9c2485 JM |
4939 | /* For VLAs, use the alias set of the element type rather than the |
4940 | default of alias set 0 for types compared structurally. */ | |
4941 | if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t)) | |
4942 | { | |
4943 | if (TREE_CODE (t) == ARRAY_TYPE) | |
4944 | return get_alias_set (TREE_TYPE (t)); | |
4945 | return -1; | |
4946 | } | |
4947 | ||
08bc2431 MM |
4948 | /* Permit type-punning when accessing a union, provided the access |
4949 | is directly through the union. For example, this code does not | |
4950 | permit taking the address of a union member and then storing | |
4951 | through it. Even the type-punning allowed here is a GCC | |
4952 | extension, albeit a common and useful one; the C standard says | |
4953 | that such accesses have implementation-defined behavior. */ | |
4954 | for (u = t; | |
4955 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
4956 | u = TREE_OPERAND (u, 0)) | |
4957 | if (TREE_CODE (u) == COMPONENT_REF | |
4958 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
4959 | return 0; | |
ece32014 | 4960 | |
74d86f4f | 4961 | /* That's all the expressions we handle specially. */ |
3f75a254 | 4962 | if (!TYPE_P (t)) |
74d86f4f RH |
4963 | return -1; |
4964 | ||
95bd1dd7 | 4965 | /* The C standard guarantees that any object may be accessed via an |
74d86f4f RH |
4966 | lvalue that has character type. */ |
4967 | if (t == char_type_node | |
4968 | || t == signed_char_type_node | |
4969 | || t == unsigned_char_type_node) | |
3bdf5ad1 | 4970 | return 0; |
3932261a | 4971 | |
f824e5c3 RK |
4972 | /* The C standard specifically allows aliasing between signed and |
4973 | unsigned variants of the same type. We treat the signed | |
4974 | variant as canonical. */ | |
8df83eae | 4975 | if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t)) |
8f215dce | 4976 | { |
ceef8ce4 | 4977 | tree t1 = c_common_signed_type (t); |
f824e5c3 | 4978 | |
8f215dce JJ |
4979 | /* t1 == t can happen for boolean nodes which are always unsigned. */ |
4980 | if (t1 != t) | |
4981 | return get_alias_set (t1); | |
4982 | } | |
ece32014 | 4983 | |
6de9cd9a DN |
4984 | /* Handle the case of multiple type nodes referring to "the same" type, |
4985 | which occurs with IMA. These share an alias set. FIXME: Currently only | |
4986 | C90 is handled. (In C99 type compatibility is not transitive, which | |
4987 | complicates things mightily. The alias set splay trees can theoretically | |
4988 | represent this, but insertion is tricky when you consider all the | |
4989 | different orders things might arrive in.) */ | |
4990 | ||
4991 | if (c_language != clk_c || flag_isoc99) | |
4992 | return -1; | |
4993 | ||
9cf737f8 | 4994 | /* Save time if there's only one input file. */ |
d974312d | 4995 | if (num_in_fnames == 1) |
6de9cd9a DN |
4996 | return -1; |
4997 | ||
4998 | /* Pointers need special handling if they point to any type that | |
4999 | needs special handling (below). */ | |
5000 | if (TREE_CODE (t) == POINTER_TYPE) | |
5001 | { | |
5002 | tree t2; | |
5003 | /* Find bottom type under any nested POINTERs. */ | |
9f63daea | 5004 | for (t2 = TREE_TYPE (t); |
762f7d9d TT |
5005 | TREE_CODE (t2) == POINTER_TYPE; |
5006 | t2 = TREE_TYPE (t2)) | |
5007 | ; | |
9f63daea | 5008 | if (TREE_CODE (t2) != RECORD_TYPE |
762f7d9d TT |
5009 | && TREE_CODE (t2) != ENUMERAL_TYPE |
5010 | && TREE_CODE (t2) != QUAL_UNION_TYPE | |
5011 | && TREE_CODE (t2) != UNION_TYPE) | |
5012 | return -1; | |
6de9cd9a | 5013 | if (TYPE_SIZE (t2) == 0) |
762f7d9d | 5014 | return -1; |
6de9cd9a DN |
5015 | } |
5016 | /* These are the only cases that need special handling. */ | |
9f63daea | 5017 | if (TREE_CODE (t) != RECORD_TYPE |
6de9cd9a DN |
5018 | && TREE_CODE (t) != ENUMERAL_TYPE |
5019 | && TREE_CODE (t) != QUAL_UNION_TYPE | |
5020 | && TREE_CODE (t) != UNION_TYPE | |
5021 | && TREE_CODE (t) != POINTER_TYPE) | |
5022 | return -1; | |
5023 | /* Undefined? */ | |
5024 | if (TYPE_SIZE (t) == 0) | |
5025 | return -1; | |
5026 | ||
9f63daea | 5027 | /* Look up t in hash table. Only one of the compatible types within each |
6de9cd9a DN |
5028 | alias set is recorded in the table. */ |
5029 | if (!type_hash_table) | |
2a22f99c TS |
5030 | type_hash_table = hash_table<c_type_hasher>::create_ggc (1021); |
5031 | tree *slot = type_hash_table->find_slot (t, INSERT); | |
6de9cd9a | 5032 | if (*slot != NULL) |
6a3203c8 AP |
5033 | { |
5034 | TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot); | |
5035 | return TYPE_ALIAS_SET ((tree)*slot); | |
5036 | } | |
6de9cd9a DN |
5037 | else |
5038 | /* Our caller will assign and record (in t) a new alias set; all we need | |
5039 | to do is remember t in the hash table. */ | |
5040 | *slot = t; | |
5041 | ||
3bdf5ad1 | 5042 | return -1; |
41472af8 | 5043 | } |
0213a355 | 5044 | \f |
c2255bc4 | 5045 | /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where |
296674db | 5046 | the IS_SIZEOF parameter indicates which operator is being applied. |
c2255bc4 AH |
5047 | The COMPLAIN flag controls whether we should diagnose possibly |
5048 | ill-formed constructs or not. LOC is the location of the SIZEOF or | |
296674db JM |
5049 | TYPEOF operator. If MIN_ALIGNOF, the least alignment required for |
5050 | a type in any context should be returned, rather than the normal | |
5051 | alignment for that type. */ | |
03a08664 | 5052 | |
0213a355 | 5053 | tree |
c2255bc4 | 5054 | c_sizeof_or_alignof_type (location_t loc, |
296674db JM |
5055 | tree type, bool is_sizeof, bool min_alignof, |
5056 | int complain) | |
0213a355 | 5057 | { |
fa72b064 GDR |
5058 | const char *op_name; |
5059 | tree value = NULL; | |
5060 | enum tree_code type_code = TREE_CODE (type); | |
35b1a6fa | 5061 | |
03a08664 | 5062 | op_name = is_sizeof ? "sizeof" : "__alignof__"; |
35b1a6fa | 5063 | |
fa72b064 | 5064 | if (type_code == FUNCTION_TYPE) |
0213a355 | 5065 | { |
03a08664 | 5066 | if (is_sizeof) |
fa72b064 | 5067 | { |
44d90fe1 PC |
5068 | if (complain && warn_pointer_arith) |
5069 | pedwarn (loc, OPT_Wpointer_arith, | |
fcf73884 | 5070 | "invalid application of %<sizeof%> to a function type"); |
5ade1ed2 DG |
5071 | else if (!complain) |
5072 | return error_mark_node; | |
fa72b064 GDR |
5073 | value = size_one_node; |
5074 | } | |
5075 | else | |
d19fa6b5 JM |
5076 | { |
5077 | if (complain) | |
5078 | { | |
5079 | if (c_dialect_cxx ()) | |
c1771a20 | 5080 | pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit " |
d19fa6b5 JM |
5081 | "%<alignof%> applied to a function type"); |
5082 | else | |
c1771a20 | 5083 | pedwarn (loc, OPT_Wpedantic, "ISO C does not permit " |
d19fa6b5 JM |
5084 | "%<_Alignof%> applied to a function type"); |
5085 | } | |
5086 | value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); | |
5087 | } | |
fa72b064 GDR |
5088 | } |
5089 | else if (type_code == VOID_TYPE || type_code == ERROR_MARK) | |
5090 | { | |
35b1a6fa | 5091 | if (type_code == VOID_TYPE |
44d90fe1 PC |
5092 | && complain && warn_pointer_arith) |
5093 | pedwarn (loc, OPT_Wpointer_arith, | |
fcf73884 | 5094 | "invalid application of %qs to a void type", op_name); |
5ade1ed2 DG |
5095 | else if (!complain) |
5096 | return error_mark_node; | |
fa72b064 | 5097 | value = size_one_node; |
0213a355 | 5098 | } |
73ac190a PC |
5099 | else if (!COMPLETE_TYPE_P (type) |
5100 | && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE)) | |
0213a355 | 5101 | { |
ea793912 | 5102 | if (complain) |
73ac190a | 5103 | error_at (loc, "invalid application of %qs to incomplete type %qT", |
c2255bc4 | 5104 | op_name, type); |
cb6addf4 | 5105 | return error_mark_node; |
0213a355 | 5106 | } |
73ac190a PC |
5107 | else if (c_dialect_cxx () && type_code == ARRAY_TYPE |
5108 | && !COMPLETE_TYPE_P (TREE_TYPE (type))) | |
5109 | { | |
5110 | if (complain) | |
5111 | error_at (loc, "invalid application of %qs to array type %qT of " | |
5112 | "incomplete element type", op_name, type); | |
5113 | return error_mark_node; | |
5114 | } | |
0213a355 | 5115 | else |
fa72b064 | 5116 | { |
03a08664 | 5117 | if (is_sizeof) |
fa72b064 | 5118 | /* Convert in case a char is more than one unit. */ |
db3927fb AH |
5119 | value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
5120 | size_int (TYPE_PRECISION (char_type_node) | |
5121 | / BITS_PER_UNIT)); | |
296674db | 5122 | else if (min_alignof) |
2793eeab | 5123 | value = size_int (min_align_of_type (type)); |
fa72b064 | 5124 | else |
a4e9ffe5 | 5125 | value = size_int (TYPE_ALIGN_UNIT (type)); |
fa72b064 | 5126 | } |
0213a355 | 5127 | |
3ac8781c RG |
5128 | /* VALUE will have the middle-end integer type sizetype. |
5129 | However, we should really return a value of type `size_t', | |
5130 | which is just a typedef for an ordinary integer type. */ | |
db3927fb | 5131 | value = fold_convert_loc (loc, size_type_node, value); |
35b1a6fa | 5132 | |
fa72b064 | 5133 | return value; |
0213a355 JM |
5134 | } |
5135 | ||
5136 | /* Implement the __alignof keyword: Return the minimum required | |
837edd5f GK |
5137 | alignment of EXPR, measured in bytes. For VAR_DECLs, |
5138 | FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set | |
c2255bc4 AH |
5139 | from an "aligned" __attribute__ specification). LOC is the |
5140 | location of the ALIGNOF operator. */ | |
7f4edbcb | 5141 | |
0213a355 | 5142 | tree |
c2255bc4 | 5143 | c_alignof_expr (location_t loc, tree expr) |
0213a355 JM |
5144 | { |
5145 | tree t; | |
5146 | ||
837edd5f | 5147 | if (VAR_OR_FUNCTION_DECL_P (expr)) |
a4e9ffe5 | 5148 | t = size_int (DECL_ALIGN_UNIT (expr)); |
35b1a6fa | 5149 | |
0213a355 JM |
5150 | else if (TREE_CODE (expr) == COMPONENT_REF |
5151 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
5152 | { | |
c2255bc4 | 5153 | error_at (loc, "%<__alignof%> applied to a bit-field"); |
0213a355 JM |
5154 | t = size_one_node; |
5155 | } | |
5156 | else if (TREE_CODE (expr) == COMPONENT_REF | |
173bf5be | 5157 | && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) |
a4e9ffe5 | 5158 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1))); |
35b1a6fa | 5159 | |
0213a355 JM |
5160 | else if (TREE_CODE (expr) == INDIRECT_REF) |
5161 | { | |
5162 | tree t = TREE_OPERAND (expr, 0); | |
5163 | tree best = t; | |
5164 | int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
35b1a6fa | 5165 | |
1043771b | 5166 | while (CONVERT_EXPR_P (t) |
173bf5be | 5167 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) |
0213a355 JM |
5168 | { |
5169 | int thisalign; | |
5170 | ||
5171 | t = TREE_OPERAND (t, 0); | |
5172 | thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
5173 | if (thisalign > bestalign) | |
5174 | best = t, bestalign = thisalign; | |
5175 | } | |
c2255bc4 | 5176 | return c_alignof (loc, TREE_TYPE (TREE_TYPE (best))); |
0213a355 JM |
5177 | } |
5178 | else | |
c2255bc4 | 5179 | return c_alignof (loc, TREE_TYPE (expr)); |
0213a355 | 5180 | |
db3927fb | 5181 | return fold_convert_loc (loc, size_type_node, t); |
0213a355 JM |
5182 | } |
5183 | \f | |
df061a43 RS |
5184 | /* Handle C and C++ default attributes. */ |
5185 | ||
5186 | enum built_in_attribute | |
5187 | { | |
5188 | #define DEF_ATTR_NULL_TREE(ENUM) ENUM, | |
5189 | #define DEF_ATTR_INT(ENUM, VALUE) ENUM, | |
e384e6b5 | 5190 | #define DEF_ATTR_STRING(ENUM, VALUE) ENUM, |
df061a43 RS |
5191 | #define DEF_ATTR_IDENT(ENUM, STRING) ENUM, |
5192 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM, | |
df061a43 RS |
5193 | #include "builtin-attrs.def" |
5194 | #undef DEF_ATTR_NULL_TREE | |
5195 | #undef DEF_ATTR_INT | |
e384e6b5 | 5196 | #undef DEF_ATTR_STRING |
df061a43 RS |
5197 | #undef DEF_ATTR_IDENT |
5198 | #undef DEF_ATTR_TREE_LIST | |
df061a43 RS |
5199 | ATTR_LAST |
5200 | }; | |
5201 | ||
5202 | static GTY(()) tree built_in_attributes[(int) ATTR_LAST]; | |
5203 | ||
35b1a6fa | 5204 | static void c_init_attributes (void); |
df061a43 | 5205 | |
a0274e3e | 5206 | enum c_builtin_type |
7f4edbcb | 5207 | { |
10841285 MM |
5208 | #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME, |
5209 | #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME, | |
5210 | #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME, | |
5211 | #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME, | |
5212 | #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, | |
5213 | #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, | |
10a0d495 | 5214 | #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME, |
f6a7cffc TS |
5215 | #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5216 | ARG6) NAME, | |
5217 | #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5218 | ARG6, ARG7) NAME, | |
5219 | #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5220 | ARG6, ARG7, ARG8) NAME, | |
10841285 MM |
5221 | #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME, |
5222 | #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME, | |
5223 | #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME, | |
08291658 | 5224 | #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, |
10a0d495 | 5225 | #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, |
f6a7cffc | 5226 | #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ |
10a0d495 | 5227 | NAME, |
10841285 MM |
5228 | #define DEF_POINTER_TYPE(NAME, TYPE) NAME, |
5229 | #include "builtin-types.def" | |
5230 | #undef DEF_PRIMITIVE_TYPE | |
5231 | #undef DEF_FUNCTION_TYPE_0 | |
5232 | #undef DEF_FUNCTION_TYPE_1 | |
5233 | #undef DEF_FUNCTION_TYPE_2 | |
5234 | #undef DEF_FUNCTION_TYPE_3 | |
5235 | #undef DEF_FUNCTION_TYPE_4 | |
10a0d495 JJ |
5236 | #undef DEF_FUNCTION_TYPE_5 |
5237 | #undef DEF_FUNCTION_TYPE_6 | |
a0274e3e | 5238 | #undef DEF_FUNCTION_TYPE_7 |
acf0174b | 5239 | #undef DEF_FUNCTION_TYPE_8 |
10841285 MM |
5240 | #undef DEF_FUNCTION_TYPE_VAR_0 |
5241 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
5242 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
08291658 | 5243 | #undef DEF_FUNCTION_TYPE_VAR_3 |
10a0d495 JJ |
5244 | #undef DEF_FUNCTION_TYPE_VAR_4 |
5245 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
10841285 | 5246 | #undef DEF_POINTER_TYPE |
a0274e3e JJ |
5247 | BT_LAST |
5248 | }; | |
5249 | ||
5250 | typedef enum c_builtin_type builtin_type; | |
10841285 | 5251 | |
a0274e3e JJ |
5252 | /* A temporary array for c_common_nodes_and_builtins. Used in |
5253 | communication with def_fn_type. */ | |
5254 | static tree builtin_types[(int) BT_LAST + 1]; | |
10841285 | 5255 | |
a0274e3e JJ |
5256 | /* A helper function for c_common_nodes_and_builtins. Build function type |
5257 | for DEF with return type RET and N arguments. If VAR is true, then the | |
5258 | function should be variadic after those N arguments. | |
5259 | ||
5260 | Takes special care not to ICE if any of the types involved are | |
5261 | error_mark_node, which indicates that said type is not in fact available | |
5262 | (see builtin_type_for_size). In which case the function type as a whole | |
5263 | should be error_mark_node. */ | |
5264 | ||
5265 | static void | |
5266 | def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...) | |
5267 | { | |
8242dd04 NF |
5268 | tree t; |
5269 | tree *args = XALLOCAVEC (tree, n); | |
a0274e3e JJ |
5270 | va_list list; |
5271 | int i; | |
5272 | ||
5273 | va_start (list, n); | |
5274 | for (i = 0; i < n; ++i) | |
5275 | { | |
d75d71e0 | 5276 | builtin_type a = (builtin_type) va_arg (list, int); |
a0274e3e JJ |
5277 | t = builtin_types[a]; |
5278 | if (t == error_mark_node) | |
5279 | goto egress; | |
8242dd04 | 5280 | args[i] = t; |
a0274e3e | 5281 | } |
a0274e3e | 5282 | |
a0274e3e JJ |
5283 | t = builtin_types[ret]; |
5284 | if (t == error_mark_node) | |
5285 | goto egress; | |
8242dd04 NF |
5286 | if (var) |
5287 | t = build_varargs_function_type_array (t, n, args); | |
5288 | else | |
5289 | t = build_function_type_array (t, n, args); | |
a0274e3e JJ |
5290 | |
5291 | egress: | |
5292 | builtin_types[def] = t; | |
0edf1bb2 | 5293 | va_end (list); |
a0274e3e JJ |
5294 | } |
5295 | ||
c6d86fce ILT |
5296 | /* Build builtin functions common to both C and C++ language |
5297 | frontends. */ | |
5298 | ||
5299 | static void | |
5300 | c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node) | |
5301 | { | |
5302 | #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ | |
5303 | builtin_types[ENUM] = VALUE; | |
5304 | #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ | |
5305 | def_fn_type (ENUM, RETURN, 0, 0); | |
5306 | #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ | |
5307 | def_fn_type (ENUM, RETURN, 0, 1, ARG1); | |
5308 | #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ | |
5309 | def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2); | |
5310 | #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
5311 | def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3); | |
5312 | #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
5313 | def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4); | |
5314 | #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
5315 | def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
5316 | #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5317 | ARG6) \ | |
5318 | def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); | |
5319 | #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
5320 | ARG6, ARG7) \ | |
5321 | def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); | |
acf0174b JJ |
5322 | #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ |
5323 | ARG6, ARG7, ARG8) \ | |
5324 | def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \ | |
5325 | ARG7, ARG8); | |
c6d86fce ILT |
5326 | #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ |
5327 | def_fn_type (ENUM, RETURN, 1, 0); | |
5328 | #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ | |
5329 | def_fn_type (ENUM, RETURN, 1, 1, ARG1); | |
5330 | #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ | |
5331 | def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2); | |
5332 | #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
5333 | def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3); | |
5334 | #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
5335 | def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4); | |
5336 | #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
5337 | def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
5338 | #define DEF_POINTER_TYPE(ENUM, TYPE) \ | |
5339 | builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]); | |
5340 | ||
5341 | #include "builtin-types.def" | |
5342 | ||
5343 | #undef DEF_PRIMITIVE_TYPE | |
f6a7cffc | 5344 | #undef DEF_FUNCTION_TYPE_0 |
c6d86fce ILT |
5345 | #undef DEF_FUNCTION_TYPE_1 |
5346 | #undef DEF_FUNCTION_TYPE_2 | |
5347 | #undef DEF_FUNCTION_TYPE_3 | |
5348 | #undef DEF_FUNCTION_TYPE_4 | |
5349 | #undef DEF_FUNCTION_TYPE_5 | |
5350 | #undef DEF_FUNCTION_TYPE_6 | |
f6a7cffc TS |
5351 | #undef DEF_FUNCTION_TYPE_7 |
5352 | #undef DEF_FUNCTION_TYPE_8 | |
c6d86fce ILT |
5353 | #undef DEF_FUNCTION_TYPE_VAR_0 |
5354 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
5355 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
5356 | #undef DEF_FUNCTION_TYPE_VAR_3 | |
5357 | #undef DEF_FUNCTION_TYPE_VAR_4 | |
5358 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
5359 | #undef DEF_POINTER_TYPE | |
5360 | builtin_types[(int) BT_LAST] = NULL_TREE; | |
5361 | ||
5362 | c_init_attributes (); | |
5363 | ||
5364 | #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ | |
5365 | NONANSI_P, ATTRS, IMPLICIT, COND) \ | |
5366 | if (NAME && COND) \ | |
5367 | def_builtin_1 (ENUM, NAME, CLASS, \ | |
5368 | builtin_types[(int) TYPE], \ | |
5369 | builtin_types[(int) LIBTYPE], \ | |
5370 | BOTH_P, FALLBACK_P, NONANSI_P, \ | |
5371 | built_in_attributes[(int) ATTRS], IMPLICIT); | |
5372 | #include "builtins.def" | |
5373 | #undef DEF_BUILTIN | |
5374 | ||
8de7ef2a UB |
5375 | targetm.init_builtins (); |
5376 | ||
384c400a | 5377 | build_common_builtin_nodes (); |
939b37da | 5378 | |
b72271b9 | 5379 | if (flag_cilkplus) |
939b37da | 5380 | cilk_init_builtins (); |
c6d86fce ILT |
5381 | } |
5382 | ||
c1b61fca JM |
5383 | /* Like get_identifier, but avoid warnings about null arguments when |
5384 | the argument may be NULL for targets where GCC lacks stdint.h type | |
5385 | information. */ | |
5386 | ||
5387 | static inline tree | |
5388 | c_get_ident (const char *id) | |
5389 | { | |
5390 | return get_identifier (id); | |
5391 | } | |
5392 | ||
a0274e3e JJ |
5393 | /* Build tree nodes and builtin functions common to both C and C++ language |
5394 | frontends. */ | |
5395 | ||
5396 | void | |
5397 | c_common_nodes_and_builtins (void) | |
5398 | { | |
b6baa67d KVH |
5399 | int char16_type_size; |
5400 | int char32_type_size; | |
eaa7c03f JM |
5401 | int wchar_type_size; |
5402 | tree array_domain_type; | |
9f720c3e | 5403 | tree va_list_ref_type_node; |
daf68dd7 | 5404 | tree va_list_arg_type_node; |
78a7c317 | 5405 | int i; |
d3707adb | 5406 | |
1a072294 | 5407 | build_common_tree_nodes (flag_signed_char, flag_short_double); |
fce5dddd | 5408 | |
eaa7c03f | 5409 | /* Define `int' and `char' first so that dbx will output them first. */ |
6496a589 | 5410 | record_builtin_type (RID_INT, NULL, integer_type_node); |
eaa7c03f JM |
5411 | record_builtin_type (RID_CHAR, "char", char_type_node); |
5412 | ||
5413 | /* `signed' is the same as `int'. FIXME: the declarations of "signed", | |
5414 | "unsigned long", "long long unsigned" and "unsigned short" were in C++ | |
5415 | but not C. Are the conditionals here needed? */ | |
37fa72e9 | 5416 | if (c_dialect_cxx ()) |
6496a589 | 5417 | record_builtin_type (RID_SIGNED, NULL, integer_type_node); |
eaa7c03f JM |
5418 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); |
5419 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); | |
5420 | record_builtin_type (RID_MAX, "long unsigned int", | |
5421 | long_unsigned_type_node); | |
78a7c317 DD |
5422 | |
5423 | for (i = 0; i < NUM_INT_N_ENTS; i ++) | |
a6766312 | 5424 | { |
78a7c317 DD |
5425 | char name[25]; |
5426 | ||
5427 | sprintf (name, "__int%d", int_n_data[i].bitsize); | |
5428 | record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), xstrdup (name), | |
5429 | int_n_trees[i].signed_type); | |
5430 | sprintf (name, "__int%d unsigned", int_n_data[i].bitsize); | |
5431 | record_builtin_type (RID_MAX, xstrdup (name), | |
5432 | int_n_trees[i].unsigned_type); | |
a6766312 | 5433 | } |
78a7c317 | 5434 | |
37fa72e9 | 5435 | if (c_dialect_cxx ()) |
eaa7c03f JM |
5436 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); |
5437 | record_builtin_type (RID_MAX, "long long int", | |
5438 | long_long_integer_type_node); | |
5439 | record_builtin_type (RID_MAX, "long long unsigned int", | |
5440 | long_long_unsigned_type_node); | |
37fa72e9 | 5441 | if (c_dialect_cxx ()) |
eaa7c03f JM |
5442 | record_builtin_type (RID_MAX, "long long unsigned", |
5443 | long_long_unsigned_type_node); | |
5444 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); | |
5445 | record_builtin_type (RID_MAX, "short unsigned int", | |
5446 | short_unsigned_type_node); | |
37fa72e9 | 5447 | if (c_dialect_cxx ()) |
eaa7c03f JM |
5448 | record_builtin_type (RID_MAX, "unsigned short", |
5449 | short_unsigned_type_node); | |
5450 | ||
5451 | /* Define both `signed char' and `unsigned char'. */ | |
5452 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); | |
5453 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); | |
5454 | ||
b0c48229 NB |
5455 | /* These are types that c_common_type_for_size and |
5456 | c_common_type_for_mode use. */ | |
c2255bc4 AH |
5457 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5458 | TYPE_DECL, NULL_TREE, | |
ae2bcd98 | 5459 | intQI_type_node)); |
c2255bc4 AH |
5460 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5461 | TYPE_DECL, NULL_TREE, | |
ae2bcd98 | 5462 | intHI_type_node)); |
c2255bc4 AH |
5463 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5464 | TYPE_DECL, NULL_TREE, | |
ae2bcd98 | 5465 | intSI_type_node)); |
c2255bc4 AH |
5466 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5467 | TYPE_DECL, NULL_TREE, | |
ae2bcd98 | 5468 | intDI_type_node)); |
eaa7c03f | 5469 | #if HOST_BITS_PER_WIDE_INT >= 64 |
78a7c317 DD |
5470 | /* Note that this is different than the __int128 type that's part of |
5471 | the generic __intN support. */ | |
1e1b8649 | 5472 | if (targetm.scalar_mode_supported_p (TImode)) |
c2255bc4 AH |
5473 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5474 | TYPE_DECL, | |
1e1b8649 AP |
5475 | get_identifier ("__int128_t"), |
5476 | intTI_type_node)); | |
eaa7c03f | 5477 | #endif |
c2255bc4 AH |
5478 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5479 | TYPE_DECL, NULL_TREE, | |
ae2bcd98 | 5480 | unsigned_intQI_type_node)); |
c2255bc4 AH |
5481 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5482 | TYPE_DECL, NULL_TREE, | |
ae2bcd98 | 5483 | unsigned_intHI_type_node)); |
c2255bc4 AH |
5484 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5485 | TYPE_DECL, NULL_TREE, | |
ae2bcd98 | 5486 | unsigned_intSI_type_node)); |
c2255bc4 AH |
5487 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5488 | TYPE_DECL, NULL_TREE, | |
ae2bcd98 | 5489 | unsigned_intDI_type_node)); |
eaa7c03f | 5490 | #if HOST_BITS_PER_WIDE_INT >= 64 |
1e1b8649 | 5491 | if (targetm.scalar_mode_supported_p (TImode)) |
c2255bc4 AH |
5492 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5493 | TYPE_DECL, | |
1e1b8649 AP |
5494 | get_identifier ("__uint128_t"), |
5495 | unsigned_intTI_type_node)); | |
eaa7c03f JM |
5496 | #endif |
5497 | ||
5498 | /* Create the widest literal types. */ | |
5499 | widest_integer_literal_type_node | |
5500 | = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
c2255bc4 AH |
5501 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5502 | TYPE_DECL, NULL_TREE, | |
ae2bcd98 | 5503 | widest_integer_literal_type_node)); |
eaa7c03f JM |
5504 | |
5505 | widest_unsigned_literal_type_node | |
5506 | = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
c2255bc4 AH |
5507 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5508 | TYPE_DECL, NULL_TREE, | |
ae2bcd98 | 5509 | widest_unsigned_literal_type_node)); |
eaa7c03f | 5510 | |
c9f8536c | 5511 | signed_size_type_node = c_common_signed_type (size_type_node); |
eaa7c03f | 5512 | |
d1c38823 ZD |
5513 | pid_type_node = |
5514 | TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE))); | |
5515 | ||
6496a589 KG |
5516 | record_builtin_type (RID_FLOAT, NULL, float_type_node); |
5517 | record_builtin_type (RID_DOUBLE, NULL, double_type_node); | |
eaa7c03f JM |
5518 | record_builtin_type (RID_MAX, "long double", long_double_type_node); |
5519 | ||
9a8ce21f JG |
5520 | /* Only supported decimal floating point extension if the target |
5521 | actually supports underlying modes. */ | |
b8698a0f | 5522 | if (targetm.scalar_mode_supported_p (SDmode) |
9a8ce21f JG |
5523 | && targetm.scalar_mode_supported_p (DDmode) |
5524 | && targetm.scalar_mode_supported_p (TDmode)) | |
5525 | { | |
5526 | record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node); | |
5527 | record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node); | |
5528 | record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node); | |
5529 | } | |
5530 | ||
ab22c1fa CF |
5531 | if (targetm.fixed_point_supported_p ()) |
5532 | { | |
5533 | record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node); | |
5534 | record_builtin_type (RID_FRACT, NULL, fract_type_node); | |
5535 | record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node); | |
5536 | record_builtin_type (RID_MAX, "long long _Fract", | |
5537 | long_long_fract_type_node); | |
5538 | record_builtin_type (RID_MAX, "unsigned short _Fract", | |
5539 | unsigned_short_fract_type_node); | |
5540 | record_builtin_type (RID_MAX, "unsigned _Fract", | |
5541 | unsigned_fract_type_node); | |
5542 | record_builtin_type (RID_MAX, "unsigned long _Fract", | |
5543 | unsigned_long_fract_type_node); | |
5544 | record_builtin_type (RID_MAX, "unsigned long long _Fract", | |
5545 | unsigned_long_long_fract_type_node); | |
5546 | record_builtin_type (RID_MAX, "_Sat short _Fract", | |
5547 | sat_short_fract_type_node); | |
5548 | record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node); | |
5549 | record_builtin_type (RID_MAX, "_Sat long _Fract", | |
5550 | sat_long_fract_type_node); | |
5551 | record_builtin_type (RID_MAX, "_Sat long long _Fract", | |
5552 | sat_long_long_fract_type_node); | |
5553 | record_builtin_type (RID_MAX, "_Sat unsigned short _Fract", | |
5554 | sat_unsigned_short_fract_type_node); | |
5555 | record_builtin_type (RID_MAX, "_Sat unsigned _Fract", | |
5556 | sat_unsigned_fract_type_node); | |
5557 | record_builtin_type (RID_MAX, "_Sat unsigned long _Fract", | |
5558 | sat_unsigned_long_fract_type_node); | |
5559 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract", | |
5560 | sat_unsigned_long_long_fract_type_node); | |
5561 | record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node); | |
5562 | record_builtin_type (RID_ACCUM, NULL, accum_type_node); | |
5563 | record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node); | |
5564 | record_builtin_type (RID_MAX, "long long _Accum", | |
5565 | long_long_accum_type_node); | |
5566 | record_builtin_type (RID_MAX, "unsigned short _Accum", | |
5567 | unsigned_short_accum_type_node); | |
5568 | record_builtin_type (RID_MAX, "unsigned _Accum", | |
5569 | unsigned_accum_type_node); | |
5570 | record_builtin_type (RID_MAX, "unsigned long _Accum", | |
5571 | unsigned_long_accum_type_node); | |
5572 | record_builtin_type (RID_MAX, "unsigned long long _Accum", | |
5573 | unsigned_long_long_accum_type_node); | |
5574 | record_builtin_type (RID_MAX, "_Sat short _Accum", | |
5575 | sat_short_accum_type_node); | |
5576 | record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node); | |
5577 | record_builtin_type (RID_MAX, "_Sat long _Accum", | |
5578 | sat_long_accum_type_node); | |
5579 | record_builtin_type (RID_MAX, "_Sat long long _Accum", | |
5580 | sat_long_long_accum_type_node); | |
5581 | record_builtin_type (RID_MAX, "_Sat unsigned short _Accum", | |
5582 | sat_unsigned_short_accum_type_node); | |
5583 | record_builtin_type (RID_MAX, "_Sat unsigned _Accum", | |
5584 | sat_unsigned_accum_type_node); | |
5585 | record_builtin_type (RID_MAX, "_Sat unsigned long _Accum", | |
5586 | sat_unsigned_long_accum_type_node); | |
5587 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum", | |
5588 | sat_unsigned_long_long_accum_type_node); | |
5589 | ||
5590 | } | |
5591 | ||
c2255bc4 AH |
5592 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5593 | TYPE_DECL, | |
ae2bcd98 RS |
5594 | get_identifier ("complex int"), |
5595 | complex_integer_type_node)); | |
c2255bc4 AH |
5596 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5597 | TYPE_DECL, | |
ae2bcd98 RS |
5598 | get_identifier ("complex float"), |
5599 | complex_float_type_node)); | |
c2255bc4 AH |
5600 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5601 | TYPE_DECL, | |
ae2bcd98 RS |
5602 | get_identifier ("complex double"), |
5603 | complex_double_type_node)); | |
5604 | lang_hooks.decls.pushdecl | |
c2255bc4 AH |
5605 | (build_decl (UNKNOWN_LOCATION, |
5606 | TYPE_DECL, get_identifier ("complex long double"), | |
43577e6b | 5607 | complex_long_double_type_node)); |
eaa7c03f | 5608 | |
498c0f27 JJ |
5609 | if (c_dialect_cxx ()) |
5610 | /* For C++, make fileptr_type_node a distinct void * type until | |
5611 | FILE type is defined. */ | |
8dd16ecc | 5612 | fileptr_type_node = build_variant_type_copy (ptr_type_node); |
498c0f27 | 5613 | |
6496a589 | 5614 | record_builtin_type (RID_VOID, NULL, void_type_node); |
eaa7c03f | 5615 | |
06d40de8 DG |
5616 | /* Set the TYPE_NAME for any variants that were built before |
5617 | record_builtin_type gave names to the built-in types. */ | |
5618 | { | |
5619 | tree void_name = TYPE_NAME (void_type_node); | |
5620 | TYPE_NAME (void_type_node) = NULL_TREE; | |
5621 | TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST)) | |
5622 | = void_name; | |
5623 | TYPE_NAME (void_type_node) = void_name; | |
5624 | } | |
5625 | ||
eaa7c03f JM |
5626 | void_list_node = build_void_list_node (); |
5627 | ||
5628 | /* Make a type to be the domain of a few array types | |
5629 | whose domains don't really matter. | |
5630 | 200 is small enough that it always fits in size_t | |
5631 | and large enough that it can hold most function names for the | |
5632 | initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
5633 | array_domain_type = build_index_type (size_int (200)); | |
5634 | ||
5635 | /* Make a type for arrays of characters. | |
5636 | With luck nothing will ever really depend on the length of this | |
5637 | array type. */ | |
5638 | char_array_type_node | |
5639 | = build_array_type (char_type_node, array_domain_type); | |
5640 | ||
10841285 MM |
5641 | string_type_node = build_pointer_type (char_type_node); |
5642 | const_string_type_node | |
5643 | = build_pointer_type (build_qualified_type | |
5644 | (char_type_node, TYPE_QUAL_CONST)); | |
5645 | ||
eaa7c03f | 5646 | /* This is special for C++ so functions can be overloaded. */ |
a11eba95 | 5647 | wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE); |
eaa7c03f JM |
5648 | wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); |
5649 | wchar_type_size = TYPE_PRECISION (wchar_type_node); | |
207bf79d | 5650 | underlying_wchar_type_node = wchar_type_node; |
37fa72e9 | 5651 | if (c_dialect_cxx ()) |
eaa7c03f | 5652 | { |
8df83eae | 5653 | if (TYPE_UNSIGNED (wchar_type_node)) |
eaa7c03f JM |
5654 | wchar_type_node = make_unsigned_type (wchar_type_size); |
5655 | else | |
5656 | wchar_type_node = make_signed_type (wchar_type_size); | |
5657 | record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); | |
5658 | } | |
eaa7c03f JM |
5659 | |
5660 | /* This is for wide string constants. */ | |
5661 | wchar_array_type_node | |
5662 | = build_array_type (wchar_type_node, array_domain_type); | |
5663 | ||
b6baa67d KVH |
5664 | /* Define 'char16_t'. */ |
5665 | char16_type_node = get_identifier (CHAR16_TYPE); | |
5666 | char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node)); | |
5667 | char16_type_size = TYPE_PRECISION (char16_type_node); | |
5668 | if (c_dialect_cxx ()) | |
5669 | { | |
5670 | char16_type_node = make_unsigned_type (char16_type_size); | |
5671 | ||
604b2bfc | 5672 | if (cxx_dialect >= cxx11) |
b6baa67d KVH |
5673 | record_builtin_type (RID_CHAR16, "char16_t", char16_type_node); |
5674 | } | |
5675 | ||
5676 | /* This is for UTF-16 string constants. */ | |
5677 | char16_array_type_node | |
5678 | = build_array_type (char16_type_node, array_domain_type); | |
5679 | ||
5680 | /* Define 'char32_t'. */ | |
5681 | char32_type_node = get_identifier (CHAR32_TYPE); | |
5682 | char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node)); | |
5683 | char32_type_size = TYPE_PRECISION (char32_type_node); | |
5684 | if (c_dialect_cxx ()) | |
5685 | { | |
5686 | char32_type_node = make_unsigned_type (char32_type_size); | |
5687 | ||
604b2bfc | 5688 | if (cxx_dialect >= cxx11) |
b6baa67d KVH |
5689 | record_builtin_type (RID_CHAR32, "char32_t", char32_type_node); |
5690 | } | |
5691 | ||
5692 | /* This is for UTF-32 string constants. */ | |
5693 | char32_array_type_node | |
5694 | = build_array_type (char32_type_node, array_domain_type); | |
5695 | ||
5fd8e536 JM |
5696 | wint_type_node = |
5697 | TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); | |
5698 | ||
5699 | intmax_type_node = | |
5700 | TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); | |
5701 | uintmax_type_node = | |
5702 | TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); | |
5703 | ||
207bf79d JM |
5704 | if (SIG_ATOMIC_TYPE) |
5705 | sig_atomic_type_node = | |
c1b61fca | 5706 | TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE))); |
207bf79d JM |
5707 | if (INT8_TYPE) |
5708 | int8_type_node = | |
c1b61fca | 5709 | TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE))); |
207bf79d JM |
5710 | if (INT16_TYPE) |
5711 | int16_type_node = | |
c1b61fca | 5712 | TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE))); |
207bf79d JM |
5713 | if (INT32_TYPE) |
5714 | int32_type_node = | |
c1b61fca | 5715 | TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE))); |
207bf79d JM |
5716 | if (INT64_TYPE) |
5717 | int64_type_node = | |
c1b61fca | 5718 | TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE))); |
207bf79d JM |
5719 | if (UINT8_TYPE) |
5720 | uint8_type_node = | |
c1b61fca | 5721 | TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE))); |
207bf79d | 5722 | if (UINT16_TYPE) |
cca615af | 5723 | c_uint16_type_node = uint16_type_node = |
c1b61fca | 5724 | TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE))); |
207bf79d | 5725 | if (UINT32_TYPE) |
cca615af | 5726 | c_uint32_type_node = uint32_type_node = |
c1b61fca | 5727 | TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE))); |
207bf79d | 5728 | if (UINT64_TYPE) |
cca615af | 5729 | c_uint64_type_node = uint64_type_node = |
c1b61fca | 5730 | TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE))); |
207bf79d JM |
5731 | if (INT_LEAST8_TYPE) |
5732 | int_least8_type_node = | |
c1b61fca | 5733 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE))); |
207bf79d JM |
5734 | if (INT_LEAST16_TYPE) |
5735 | int_least16_type_node = | |
c1b61fca | 5736 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE))); |
207bf79d JM |
5737 | if (INT_LEAST32_TYPE) |
5738 | int_least32_type_node = | |
c1b61fca | 5739 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE))); |
207bf79d JM |
5740 | if (INT_LEAST64_TYPE) |
5741 | int_least64_type_node = | |
c1b61fca | 5742 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE))); |
207bf79d JM |
5743 | if (UINT_LEAST8_TYPE) |
5744 | uint_least8_type_node = | |
c1b61fca | 5745 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE))); |
207bf79d JM |
5746 | if (UINT_LEAST16_TYPE) |
5747 | uint_least16_type_node = | |
c1b61fca | 5748 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE))); |
207bf79d JM |
5749 | if (UINT_LEAST32_TYPE) |
5750 | uint_least32_type_node = | |
c1b61fca | 5751 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE))); |
207bf79d JM |
5752 | if (UINT_LEAST64_TYPE) |
5753 | uint_least64_type_node = | |
c1b61fca | 5754 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE))); |
207bf79d JM |
5755 | if (INT_FAST8_TYPE) |
5756 | int_fast8_type_node = | |
c1b61fca | 5757 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE))); |
207bf79d JM |
5758 | if (INT_FAST16_TYPE) |
5759 | int_fast16_type_node = | |
c1b61fca | 5760 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE))); |
207bf79d JM |
5761 | if (INT_FAST32_TYPE) |
5762 | int_fast32_type_node = | |
c1b61fca | 5763 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE))); |
207bf79d JM |
5764 | if (INT_FAST64_TYPE) |
5765 | int_fast64_type_node = | |
c1b61fca | 5766 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE))); |
207bf79d JM |
5767 | if (UINT_FAST8_TYPE) |
5768 | uint_fast8_type_node = | |
c1b61fca | 5769 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE))); |
207bf79d JM |
5770 | if (UINT_FAST16_TYPE) |
5771 | uint_fast16_type_node = | |
c1b61fca | 5772 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE))); |
207bf79d JM |
5773 | if (UINT_FAST32_TYPE) |
5774 | uint_fast32_type_node = | |
c1b61fca | 5775 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE))); |
207bf79d JM |
5776 | if (UINT_FAST64_TYPE) |
5777 | uint_fast64_type_node = | |
c1b61fca | 5778 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE))); |
207bf79d JM |
5779 | if (INTPTR_TYPE) |
5780 | intptr_type_node = | |
c1b61fca | 5781 | TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE))); |
207bf79d JM |
5782 | if (UINTPTR_TYPE) |
5783 | uintptr_type_node = | |
c1b61fca | 5784 | TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE))); |
207bf79d | 5785 | |
8242dd04 NF |
5786 | default_function_type |
5787 | = build_varargs_function_type_list (integer_type_node, NULL_TREE); | |
5fd8e536 JM |
5788 | ptrdiff_type_node |
5789 | = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); | |
12753674 | 5790 | unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node); |
5fd8e536 | 5791 | |
ae2bcd98 | 5792 | lang_hooks.decls.pushdecl |
c2255bc4 AH |
5793 | (build_decl (UNKNOWN_LOCATION, |
5794 | TYPE_DECL, get_identifier ("__builtin_va_list"), | |
43577e6b | 5795 | va_list_type_node)); |
38f8b050 | 5796 | if (targetm.enum_va_list_p) |
d4048208 KT |
5797 | { |
5798 | int l; | |
5799 | const char *pname; | |
5800 | tree ptype; | |
35cbb299 | 5801 | |
38f8b050 | 5802 | for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l) |
d4048208 KT |
5803 | { |
5804 | lang_hooks.decls.pushdecl | |
5805 | (build_decl (UNKNOWN_LOCATION, | |
5806 | TYPE_DECL, get_identifier (pname), | |
5807 | ptype)); | |
5808 | ||
5809 | } | |
5810 | } | |
daf68dd7 | 5811 | |
daf68dd7 | 5812 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
9f720c3e GK |
5813 | { |
5814 | va_list_arg_type_node = va_list_ref_type_node = | |
5815 | build_pointer_type (TREE_TYPE (va_list_type_node)); | |
5816 | } | |
daf68dd7 | 5817 | else |
9f720c3e GK |
5818 | { |
5819 | va_list_arg_type_node = va_list_type_node; | |
5820 | va_list_ref_type_node = build_reference_type (va_list_type_node); | |
5821 | } | |
35b1a6fa | 5822 | |
c6d86fce ILT |
5823 | if (!flag_preprocess_only) |
5824 | c_define_builtins (va_list_ref_type_node, va_list_arg_type_node); | |
4677862a | 5825 | |
5b47282c | 5826 | main_identifier_node = get_identifier ("main"); |
b2f97e4a MM |
5827 | |
5828 | /* Create the built-in __null node. It is important that this is | |
5829 | not shared. */ | |
807e902e | 5830 | null_node = make_int_cst (1, 1); |
b2f97e4a | 5831 | TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0); |
a0274e3e JJ |
5832 | |
5833 | /* Since builtin_types isn't gc'ed, don't export these nodes. */ | |
5834 | memset (builtin_types, 0, sizeof (builtin_types)); | |
7f4edbcb | 5835 | } |
d3707adb | 5836 | |
3b2db49f MM |
5837 | /* The number of named compound-literals generated thus far. */ |
5838 | static GTY(()) int compound_literal_number; | |
5839 | ||
5840 | /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */ | |
5841 | ||
5842 | void | |
5843 | set_compound_literal_name (tree decl) | |
5844 | { | |
5845 | char *name; | |
5846 | ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", | |
5847 | compound_literal_number); | |
5848 | compound_literal_number++; | |
5849 | DECL_NAME (decl) = get_identifier (name); | |
5850 | } | |
5851 | ||
d3707adb | 5852 | tree |
c2255bc4 | 5853 | build_va_arg (location_t loc, tree expr, tree type) |
d3707adb | 5854 | { |
c2255bc4 AH |
5855 | expr = build1 (VA_ARG_EXPR, type, expr); |
5856 | SET_EXPR_LOCATION (expr, loc); | |
5857 | return expr; | |
d3707adb | 5858 | } |
fc2aaf30 JM |
5859 | |
5860 | ||
7d14c755 JM |
5861 | /* Linked list of disabled built-in functions. */ |
5862 | ||
5863 | typedef struct disabled_builtin | |
5864 | { | |
5865 | const char *name; | |
5866 | struct disabled_builtin *next; | |
5867 | } disabled_builtin; | |
5868 | static disabled_builtin *disabled_builtins = NULL; | |
5869 | ||
35b1a6fa | 5870 | static bool builtin_function_disabled_p (const char *); |
7d14c755 JM |
5871 | |
5872 | /* Disable a built-in function specified by -fno-builtin-NAME. If NAME | |
5873 | begins with "__builtin_", give an error. */ | |
5874 | ||
5875 | void | |
35b1a6fa | 5876 | disable_builtin_function (const char *name) |
7d14c755 JM |
5877 | { |
5878 | if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0) | |
bda67431 | 5879 | error ("cannot disable built-in function %qs", name); |
7d14c755 JM |
5880 | else |
5881 | { | |
1ad463f4 BI |
5882 | disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin); |
5883 | new_disabled_builtin->name = name; | |
5884 | new_disabled_builtin->next = disabled_builtins; | |
5885 | disabled_builtins = new_disabled_builtin; | |
7d14c755 JM |
5886 | } |
5887 | } | |
5888 | ||
5889 | ||
5890 | /* Return true if the built-in function NAME has been disabled, false | |
5891 | otherwise. */ | |
5892 | ||
5893 | static bool | |
35b1a6fa | 5894 | builtin_function_disabled_p (const char *name) |
7d14c755 JM |
5895 | { |
5896 | disabled_builtin *p; | |
5897 | for (p = disabled_builtins; p != NULL; p = p->next) | |
5898 | { | |
5899 | if (strcmp (name, p->name) == 0) | |
5900 | return true; | |
5901 | } | |
5902 | return false; | |
5903 | } | |
5904 | ||
5905 | ||
9bc15050 RG |
5906 | /* Worker for DEF_BUILTIN. |
5907 | Possibly define a builtin function with one or two names. | |
5908 | Does not declare a non-__builtin_ function if flag_no_builtin, or if | |
5909 | nonansi_p and flag_no_nonansi_builtin. */ | |
fc2aaf30 | 5910 | |
9bc15050 RG |
5911 | static void |
5912 | def_builtin_1 (enum built_in_function fncode, | |
5913 | const char *name, | |
5914 | enum built_in_class fnclass, | |
5915 | tree fntype, tree libtype, | |
5916 | bool both_p, bool fallback_p, bool nonansi_p, | |
5917 | tree fnattrs, bool implicit_p) | |
fc2aaf30 | 5918 | { |
9bc15050 RG |
5919 | tree decl; |
5920 | const char *libname; | |
5921 | ||
a0274e3e JJ |
5922 | if (fntype == error_mark_node) |
5923 | return; | |
5924 | ||
9bc15050 RG |
5925 | gcc_assert ((!both_p && !fallback_p) |
5926 | || !strncmp (name, "__builtin_", | |
5927 | strlen ("__builtin_"))); | |
5928 | ||
5929 | libname = name + strlen ("__builtin_"); | |
c79efc4d RÁE |
5930 | decl = add_builtin_function (name, fntype, fncode, fnclass, |
5931 | (fallback_p ? libname : NULL), | |
5932 | fnattrs); | |
e79983f4 MM |
5933 | |
5934 | set_builtin_decl (fncode, decl, implicit_p); | |
5935 | ||
9bc15050 RG |
5936 | if (both_p |
5937 | && !flag_no_builtin && !builtin_function_disabled_p (libname) | |
7d14c755 | 5938 | && !(nonansi_p && flag_no_nonansi_builtin)) |
c79efc4d RÁE |
5939 | add_builtin_function (libname, libtype, fncode, fnclass, |
5940 | NULL, fnattrs); | |
fc2aaf30 | 5941 | } |
c530479e | 5942 | \f |
d72040f5 RH |
5943 | /* Nonzero if the type T promotes to int. This is (nearly) the |
5944 | integral promotions defined in ISO C99 6.3.1.1/2. */ | |
5945 | ||
5946 | bool | |
58f9752a | 5947 | c_promoting_integer_type_p (const_tree t) |
d72040f5 RH |
5948 | { |
5949 | switch (TREE_CODE (t)) | |
5950 | { | |
5951 | case INTEGER_TYPE: | |
5952 | return (TYPE_MAIN_VARIANT (t) == char_type_node | |
5953 | || TYPE_MAIN_VARIANT (t) == signed_char_type_node | |
5954 | || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node | |
5955 | || TYPE_MAIN_VARIANT (t) == short_integer_type_node | |
c6c04fca RL |
5956 | || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node |
5957 | || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node)); | |
d72040f5 RH |
5958 | |
5959 | case ENUMERAL_TYPE: | |
5960 | /* ??? Technically all enumerations not larger than an int | |
5961 | promote to an int. But this is used along code paths | |
5962 | that only want to notice a size change. */ | |
5963 | return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node); | |
5964 | ||
5965 | case BOOLEAN_TYPE: | |
5966 | return 1; | |
5967 | ||
5968 | default: | |
5969 | return 0; | |
5970 | } | |
5971 | } | |
5972 | ||
c530479e RH |
5973 | /* Return 1 if PARMS specifies a fixed number of parameters |
5974 | and none of their types is affected by default promotions. */ | |
5975 | ||
5976 | int | |
58f9752a | 5977 | self_promoting_args_p (const_tree parms) |
c530479e | 5978 | { |
58f9752a | 5979 | const_tree t; |
c530479e RH |
5980 | for (t = parms; t; t = TREE_CHAIN (t)) |
5981 | { | |
b3694847 | 5982 | tree type = TREE_VALUE (t); |
7e8176d7 | 5983 | |
694fea20 VR |
5984 | if (type == error_mark_node) |
5985 | continue; | |
5986 | ||
c530479e RH |
5987 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
5988 | return 0; | |
5989 | ||
5990 | if (type == 0) | |
5991 | return 0; | |
5992 | ||
5993 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
5994 | return 0; | |
5995 | ||
d72040f5 | 5996 | if (c_promoting_integer_type_p (type)) |
c530479e RH |
5997 | return 0; |
5998 | } | |
5999 | return 1; | |
6000 | } | |
5eda3d66 | 6001 | |
12ea3302 GDR |
6002 | /* Recursively remove any '*' or '&' operator from TYPE. */ |
6003 | tree | |
6004 | strip_pointer_operator (tree t) | |
6005 | { | |
6006 | while (POINTER_TYPE_P (t)) | |
6007 | t = TREE_TYPE (t); | |
6008 | return t; | |
6009 | } | |
6010 | ||
ba992967 SP |
6011 | /* Recursively remove pointer or array type from TYPE. */ |
6012 | tree | |
6013 | strip_pointer_or_array_types (tree t) | |
6014 | { | |
6015 | while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t)) | |
6016 | t = TREE_TYPE (t); | |
6017 | return t; | |
6018 | } | |
6019 | ||
8f17b5c5 MM |
6020 | /* Used to compare case labels. K1 and K2 are actually tree nodes |
6021 | representing case labels, or NULL_TREE for a `default' label. | |
6022 | Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after | |
6023 | K2, and 0 if K1 and K2 are equal. */ | |
6024 | ||
6025 | int | |
35b1a6fa | 6026 | case_compare (splay_tree_key k1, splay_tree_key k2) |
8f17b5c5 MM |
6027 | { |
6028 | /* Consider a NULL key (such as arises with a `default' label) to be | |
6029 | smaller than anything else. */ | |
6030 | if (!k1) | |
6031 | return k2 ? -1 : 0; | |
6032 | else if (!k2) | |
6033 | return k1 ? 1 : 0; | |
6034 | ||
6035 | return tree_int_cst_compare ((tree) k1, (tree) k2); | |
6036 | } | |
6037 | ||
c2255bc4 AH |
6038 | /* Process a case label, located at LOC, for the range LOW_VALUE |
6039 | ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE | |
6040 | then this case label is actually a `default' label. If only | |
6041 | HIGH_VALUE is NULL_TREE, then case label was declared using the | |
6042 | usual C/C++ syntax, rather than the GNU case range extension. | |
6043 | CASES is a tree containing all the case ranges processed so far; | |
6044 | COND is the condition for the switch-statement itself. Returns the | |
6045 | CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR | |
6046 | is created. */ | |
8f17b5c5 MM |
6047 | |
6048 | tree | |
c2255bc4 | 6049 | c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type, |
a6c0a76c | 6050 | tree low_value, tree high_value) |
8f17b5c5 MM |
6051 | { |
6052 | tree type; | |
6053 | tree label; | |
6054 | tree case_label; | |
6055 | splay_tree_node node; | |
6056 | ||
6057 | /* Create the LABEL_DECL itself. */ | |
c2255bc4 | 6058 | label = create_artificial_label (loc); |
8f17b5c5 MM |
6059 | |
6060 | /* If there was an error processing the switch condition, bail now | |
6061 | before we get more confused. */ | |
6062 | if (!cond || cond == error_mark_node) | |
6de9cd9a | 6063 | goto error_out; |
8f17b5c5 | 6064 | |
35b1a6fa AJ |
6065 | if ((low_value && TREE_TYPE (low_value) |
6066 | && POINTER_TYPE_P (TREE_TYPE (low_value))) | |
8f17b5c5 MM |
6067 | || (high_value && TREE_TYPE (high_value) |
6068 | && POINTER_TYPE_P (TREE_TYPE (high_value)))) | |
522ddfa2 | 6069 | { |
c2255bc4 | 6070 | error_at (loc, "pointers are not permitted as case values"); |
522ddfa2 JM |
6071 | goto error_out; |
6072 | } | |
8f17b5c5 MM |
6073 | |
6074 | /* Case ranges are a GNU extension. */ | |
fcf73884 | 6075 | if (high_value) |
c1771a20 | 6076 | pedwarn (loc, OPT_Wpedantic, |
fcf73884 | 6077 | "range expressions in switch statements are non-standard"); |
8f17b5c5 MM |
6078 | |
6079 | type = TREE_TYPE (cond); | |
6080 | if (low_value) | |
6081 | { | |
62e4eb35 | 6082 | low_value = check_case_value (loc, low_value); |
68fca595 | 6083 | low_value = convert_and_check (loc, type, low_value); |
c0e22534 NS |
6084 | if (low_value == error_mark_node) |
6085 | goto error_out; | |
8f17b5c5 MM |
6086 | } |
6087 | if (high_value) | |
6088 | { | |
62e4eb35 | 6089 | high_value = check_case_value (loc, high_value); |
68fca595 | 6090 | high_value = convert_and_check (loc, type, high_value); |
c0e22534 NS |
6091 | if (high_value == error_mark_node) |
6092 | goto error_out; | |
8f17b5c5 MM |
6093 | } |
6094 | ||
c0e22534 NS |
6095 | if (low_value && high_value) |
6096 | { | |
6097 | /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't | |
c22cacf3 MS |
6098 | really a case range, even though it was written that way. |
6099 | Remove the HIGH_VALUE to simplify later processing. */ | |
c0e22534 NS |
6100 | if (tree_int_cst_equal (low_value, high_value)) |
6101 | high_value = NULL_TREE; | |
6102 | else if (!tree_int_cst_lt (low_value, high_value)) | |
c2255bc4 | 6103 | warning_at (loc, 0, "empty range specified"); |
c0e22534 | 6104 | } |
8f17b5c5 | 6105 | |
a6c0a76c SB |
6106 | /* See if the case is in range of the type of the original testing |
6107 | expression. If both low_value and high_value are out of range, | |
6108 | don't insert the case label and return NULL_TREE. */ | |
6109 | if (low_value | |
9d548dfb | 6110 | && !check_case_bounds (loc, type, orig_type, |
3f75a254 | 6111 | &low_value, high_value ? &high_value : NULL)) |
a6c0a76c SB |
6112 | return NULL_TREE; |
6113 | ||
8f17b5c5 MM |
6114 | /* Look up the LOW_VALUE in the table of case labels we already |
6115 | have. */ | |
6116 | node = splay_tree_lookup (cases, (splay_tree_key) low_value); | |
6117 | /* If there was not an exact match, check for overlapping ranges. | |
6118 | There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; | |
6119 | that's a `default' label and the only overlap is an exact match. */ | |
6120 | if (!node && (low_value || high_value)) | |
6121 | { | |
6122 | splay_tree_node low_bound; | |
6123 | splay_tree_node high_bound; | |
6124 | ||
6125 | /* Even though there wasn't an exact match, there might be an | |
6126 | overlap between this case range and another case range. | |
6127 | Since we've (inductively) not allowed any overlapping case | |
6128 | ranges, we simply need to find the greatest low case label | |
6129 | that is smaller that LOW_VALUE, and the smallest low case | |
6130 | label that is greater than LOW_VALUE. If there is an overlap | |
6131 | it will occur in one of these two ranges. */ | |
6132 | low_bound = splay_tree_predecessor (cases, | |
6133 | (splay_tree_key) low_value); | |
6134 | high_bound = splay_tree_successor (cases, | |
6135 | (splay_tree_key) low_value); | |
6136 | ||
6137 | /* Check to see if the LOW_BOUND overlaps. It is smaller than | |
6138 | the LOW_VALUE, so there is no need to check unless the | |
6139 | LOW_BOUND is in fact itself a case range. */ | |
6140 | if (low_bound | |
6141 | && CASE_HIGH ((tree) low_bound->value) | |
6142 | && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), | |
6143 | low_value) >= 0) | |
6144 | node = low_bound; | |
6145 | /* Check to see if the HIGH_BOUND overlaps. The low end of that | |
6146 | range is bigger than the low end of the current range, so we | |
6147 | are only interested if the current range is a real range, and | |
6148 | not an ordinary case label. */ | |
35b1a6fa | 6149 | else if (high_bound |
8f17b5c5 MM |
6150 | && high_value |
6151 | && (tree_int_cst_compare ((tree) high_bound->key, | |
6152 | high_value) | |
6153 | <= 0)) | |
6154 | node = high_bound; | |
6155 | } | |
6156 | /* If there was an overlap, issue an error. */ | |
6157 | if (node) | |
6158 | { | |
8c161995 | 6159 | tree duplicate = CASE_LABEL ((tree) node->value); |
8f17b5c5 MM |
6160 | |
6161 | if (high_value) | |
6162 | { | |
c2255bc4 AH |
6163 | error_at (loc, "duplicate (or overlapping) case value"); |
6164 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
6165 | "this is the first entry overlapping that value"); | |
8f17b5c5 MM |
6166 | } |
6167 | else if (low_value) | |
6168 | { | |
c2255bc4 AH |
6169 | error_at (loc, "duplicate case value") ; |
6170 | error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here"); | |
8f17b5c5 MM |
6171 | } |
6172 | else | |
6173 | { | |
c2255bc4 AH |
6174 | error_at (loc, "multiple default labels in one switch"); |
6175 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
6176 | "this is the first default label"); | |
8f17b5c5 | 6177 | } |
6de9cd9a | 6178 | goto error_out; |
8f17b5c5 MM |
6179 | } |
6180 | ||
6181 | /* Add a CASE_LABEL to the statement-tree. */ | |
3d528853 | 6182 | case_label = add_stmt (build_case_label (low_value, high_value, label)); |
8f17b5c5 | 6183 | /* Register this case label in the splay tree. */ |
35b1a6fa | 6184 | splay_tree_insert (cases, |
8f17b5c5 MM |
6185 | (splay_tree_key) low_value, |
6186 | (splay_tree_value) case_label); | |
6187 | ||
6188 | return case_label; | |
6de9cd9a DN |
6189 | |
6190 | error_out: | |
9e14e18f | 6191 | /* Add a label so that the back-end doesn't think that the beginning of |
6de9cd9a | 6192 | the switch is unreachable. Note that we do not add a case label, as |
41806d92 | 6193 | that just leads to duplicates and thence to failure later on. */ |
6de9cd9a DN |
6194 | if (!cases->root) |
6195 | { | |
c2255bc4 AH |
6196 | tree t = create_artificial_label (loc); |
6197 | add_stmt (build_stmt (loc, LABEL_EXPR, t)); | |
6de9cd9a DN |
6198 | } |
6199 | return error_mark_node; | |
6200 | } | |
6201 | ||
6202 | /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. | |
6203 | Used to verify that case values match up with enumerator values. */ | |
6204 | ||
6205 | static void | |
6206 | match_case_to_enum_1 (tree key, tree type, tree label) | |
6207 | { | |
807e902e KZ |
6208 | char buf[WIDE_INT_PRINT_BUFFER_SIZE]; |
6209 | ||
6210 | if (tree_fits_uhwi_p (key)) | |
6211 | print_dec (key, buf, UNSIGNED); | |
6212 | else if (tree_fits_shwi_p (key)) | |
6213 | print_dec (key, buf, SIGNED); | |
6de9cd9a | 6214 | else |
807e902e | 6215 | print_hex (key, buf); |
6de9cd9a DN |
6216 | |
6217 | if (TYPE_NAME (type) == 0) | |
c5d75364 MLI |
6218 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
6219 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
6220 | "case value %qs not in enumerated type", | |
6221 | buf); | |
6de9cd9a | 6222 | else |
c5d75364 MLI |
6223 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
6224 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
6225 | "case value %qs not in enumerated type %qT", | |
6226 | buf, type); | |
6de9cd9a DN |
6227 | } |
6228 | ||
c782c2fe RS |
6229 | /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. |
6230 | Used to verify that case values match up with enumerator values. */ | |
6231 | ||
6de9cd9a DN |
6232 | static int |
6233 | match_case_to_enum (splay_tree_node node, void *data) | |
6234 | { | |
6235 | tree label = (tree) node->value; | |
28dab132 | 6236 | tree type = (tree) data; |
6de9cd9a DN |
6237 | |
6238 | /* Skip default case. */ | |
6239 | if (!CASE_LOW (label)) | |
6240 | return 0; | |
6241 | ||
c782c2fe | 6242 | /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear |
6de9cd9a | 6243 | when we did our enum->case scan. Reset our scratch bit after. */ |
c782c2fe | 6244 | if (!CASE_LOW_SEEN (label)) |
6de9cd9a DN |
6245 | match_case_to_enum_1 (CASE_LOW (label), type, label); |
6246 | else | |
c782c2fe | 6247 | CASE_LOW_SEEN (label) = 0; |
6de9cd9a | 6248 | |
c782c2fe RS |
6249 | /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is |
6250 | not set, that means that CASE_HIGH did not appear when we did our | |
6251 | enum->case scan. Reset our scratch bit after. */ | |
6de9cd9a DN |
6252 | if (CASE_HIGH (label)) |
6253 | { | |
c782c2fe RS |
6254 | if (!CASE_HIGH_SEEN (label)) |
6255 | match_case_to_enum_1 (CASE_HIGH (label), type, label); | |
6256 | else | |
6257 | CASE_HIGH_SEEN (label) = 0; | |
6de9cd9a DN |
6258 | } |
6259 | ||
6260 | return 0; | |
6261 | } | |
6262 | ||
fbc315db ILT |
6263 | /* Handle -Wswitch*. Called from the front end after parsing the |
6264 | switch construct. */ | |
6265 | /* ??? Should probably be somewhere generic, since other languages | |
6266 | besides C and C++ would want this. At the moment, however, C/C++ | |
6267 | are the only tree-ssa languages that support enumerations at all, | |
6268 | so the point is moot. */ | |
6de9cd9a | 6269 | |
fbc315db ILT |
6270 | void |
6271 | c_do_switch_warnings (splay_tree cases, location_t switch_location, | |
6272 | tree type, tree cond) | |
6de9cd9a | 6273 | { |
9f63daea | 6274 | splay_tree_node default_node; |
c782c2fe RS |
6275 | splay_tree_node node; |
6276 | tree chain; | |
6de9cd9a DN |
6277 | |
6278 | if (!warn_switch && !warn_switch_enum && !warn_switch_default) | |
6279 | return; | |
6280 | ||
6de9cd9a | 6281 | default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); |
44c21c7f | 6282 | if (!default_node) |
fab922b1 MLI |
6283 | warning_at (switch_location, OPT_Wswitch_default, |
6284 | "switch missing default case"); | |
6de9cd9a | 6285 | |
c782c2fe RS |
6286 | /* From here on, we only care about about enumerated types. */ |
6287 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
6288 | return; | |
6289 | ||
cdb88468 ILT |
6290 | /* From here on, we only care about -Wswitch and -Wswitch-enum. */ |
6291 | if (!warn_switch_enum && !warn_switch) | |
c782c2fe RS |
6292 | return; |
6293 | ||
cdb88468 ILT |
6294 | /* Check the cases. Warn about case values which are not members of |
6295 | the enumerated type. For -Wswitch-enum, or for -Wswitch when | |
6296 | there is no default case, check that exactly all enumeration | |
6297 | literals are covered by the cases. */ | |
6298 | ||
c782c2fe RS |
6299 | /* Clearing COND if it is not an integer constant simplifies |
6300 | the tests inside the loop below. */ | |
6301 | if (TREE_CODE (cond) != INTEGER_CST) | |
6302 | cond = NULL_TREE; | |
6303 | ||
6304 | /* The time complexity here is O(N*lg(N)) worst case, but for the | |
6305 | common case of monotonically increasing enumerators, it is | |
6306 | O(N), since the nature of the splay tree will keep the next | |
6307 | element adjacent to the root at all times. */ | |
6de9cd9a | 6308 | |
c782c2fe RS |
6309 | for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) |
6310 | { | |
6311 | tree value = TREE_VALUE (chain); | |
adf2edec DG |
6312 | if (TREE_CODE (value) == CONST_DECL) |
6313 | value = DECL_INITIAL (value); | |
c782c2fe RS |
6314 | node = splay_tree_lookup (cases, (splay_tree_key) value); |
6315 | if (node) | |
6de9cd9a | 6316 | { |
c782c2fe RS |
6317 | /* Mark the CASE_LOW part of the case entry as seen. */ |
6318 | tree label = (tree) node->value; | |
6319 | CASE_LOW_SEEN (label) = 1; | |
6320 | continue; | |
6321 | } | |
6322 | ||
6323 | /* Even though there wasn't an exact match, there might be a | |
fa10beec | 6324 | case range which includes the enumerator's value. */ |
c782c2fe RS |
6325 | node = splay_tree_predecessor (cases, (splay_tree_key) value); |
6326 | if (node && CASE_HIGH ((tree) node->value)) | |
6327 | { | |
6328 | tree label = (tree) node->value; | |
6329 | int cmp = tree_int_cst_compare (CASE_HIGH (label), value); | |
6330 | if (cmp >= 0) | |
6de9cd9a | 6331 | { |
c782c2fe RS |
6332 | /* If we match the upper bound exactly, mark the CASE_HIGH |
6333 | part of the case entry as seen. */ | |
6334 | if (cmp == 0) | |
6335 | CASE_HIGH_SEEN (label) = 1; | |
6336 | continue; | |
6de9cd9a DN |
6337 | } |
6338 | } | |
6339 | ||
c782c2fe RS |
6340 | /* We've now determined that this enumerated literal isn't |
6341 | handled by the case labels of the switch statement. */ | |
6de9cd9a | 6342 | |
c782c2fe RS |
6343 | /* If the switch expression is a constant, we only really care |
6344 | about whether that constant is handled by the switch. */ | |
6345 | if (cond && tree_int_cst_compare (cond, value)) | |
6346 | continue; | |
6de9cd9a | 6347 | |
683d6ff9 | 6348 | /* If there is a default_node, the only relevant option is |
cdb88468 | 6349 | Wswitch-enum. Otherwise, if both are enabled then we prefer |
683d6ff9 MLI |
6350 | to warn using -Wswitch because -Wswitch is enabled by -Wall |
6351 | while -Wswitch-enum is explicit. */ | |
cdb88468 ILT |
6352 | warning_at (switch_location, |
6353 | (default_node || !warn_switch | |
6354 | ? OPT_Wswitch_enum | |
6355 | : OPT_Wswitch), | |
6356 | "enumeration value %qE not handled in switch", | |
6357 | TREE_PURPOSE (chain)); | |
6de9cd9a | 6358 | } |
c782c2fe RS |
6359 | |
6360 | /* Warn if there are case expressions that don't correspond to | |
6361 | enumerators. This can occur since C and C++ don't enforce | |
6362 | type-checking of assignments to enumeration variables. | |
6363 | ||
6364 | The time complexity here is now always O(N) worst case, since | |
6365 | we should have marked both the lower bound and upper bound of | |
6366 | every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN | |
6367 | above. This scan also resets those fields. */ | |
683d6ff9 | 6368 | |
c782c2fe | 6369 | splay_tree_foreach (cases, match_case_to_enum, type); |
8f17b5c5 MM |
6370 | } |
6371 | ||
6b665219 | 6372 | /* Finish an expression taking the address of LABEL (an |
6a3799eb AH |
6373 | IDENTIFIER_NODE). Returns an expression for the address. |
6374 | ||
6375 | LOC is the location for the expression returned. */ | |
15b732b2 | 6376 | |
35b1a6fa | 6377 | tree |
6a3799eb | 6378 | finish_label_address_expr (tree label, location_t loc) |
15b732b2 NB |
6379 | { |
6380 | tree result; | |
6381 | ||
c1771a20 | 6382 | pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard"); |
15b732b2 | 6383 | |
6b665219 MM |
6384 | if (label == error_mark_node) |
6385 | return error_mark_node; | |
6386 | ||
15b732b2 NB |
6387 | label = lookup_label (label); |
6388 | if (label == NULL_TREE) | |
6389 | result = null_pointer_node; | |
6390 | else | |
6391 | { | |
6392 | TREE_USED (label) = 1; | |
6393 | result = build1 (ADDR_EXPR, ptr_type_node, label); | |
5805e48d | 6394 | /* The current function is not necessarily uninlinable. |
15b732b2 NB |
6395 | Computed gotos are incompatible with inlining, but the value |
6396 | here could be used only in a diagnostic, for example. */ | |
6a3799eb | 6397 | protected_set_expr_location (result, loc); |
15b732b2 NB |
6398 | } |
6399 | ||
6400 | return result; | |
6401 | } | |
19552aa5 JM |
6402 | \f |
6403 | ||
6404 | /* Given a boolean expression ARG, return a tree representing an increment | |
6405 | or decrement (as indicated by CODE) of ARG. The front end must check for | |
6406 | invalid cases (e.g., decrement in C++). */ | |
6407 | tree | |
35b1a6fa | 6408 | boolean_increment (enum tree_code code, tree arg) |
19552aa5 JM |
6409 | { |
6410 | tree val; | |
b5119fa1 | 6411 | tree true_res = build_int_cst (TREE_TYPE (arg), 1); |
37fa72e9 | 6412 | |
19552aa5 JM |
6413 | arg = stabilize_reference (arg); |
6414 | switch (code) | |
6415 | { | |
6416 | case PREINCREMENT_EXPR: | |
53fb4de3 | 6417 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
19552aa5 JM |
6418 | break; |
6419 | case POSTINCREMENT_EXPR: | |
53fb4de3 | 6420 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
19552aa5 | 6421 | arg = save_expr (arg); |
53fb4de3 RS |
6422 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
6423 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
19552aa5 JM |
6424 | break; |
6425 | case PREDECREMENT_EXPR: | |
53fb4de3 | 6426 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
db3927fb | 6427 | invert_truthvalue_loc (input_location, arg)); |
19552aa5 JM |
6428 | break; |
6429 | case POSTDECREMENT_EXPR: | |
53fb4de3 | 6430 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
db3927fb | 6431 | invert_truthvalue_loc (input_location, arg)); |
19552aa5 | 6432 | arg = save_expr (arg); |
53fb4de3 RS |
6433 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
6434 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
19552aa5 JM |
6435 | break; |
6436 | default: | |
366de0ce | 6437 | gcc_unreachable (); |
19552aa5 JM |
6438 | } |
6439 | TREE_SIDE_EFFECTS (val) = 1; | |
6440 | return val; | |
6441 | } | |
03dc0325 | 6442 | \f |
207bf79d JM |
6443 | /* Built-in macros for stddef.h and stdint.h, that require macros |
6444 | defined in this file. */ | |
460bd0e3 | 6445 | void |
35b1a6fa | 6446 | c_stddef_cpp_builtins(void) |
3df89291 | 6447 | { |
5279d739 ZW |
6448 | builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0); |
6449 | builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0); | |
6450 | builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0); | |
6451 | builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0); | |
85291069 JM |
6452 | builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0); |
6453 | builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0); | |
c466b2cd KVH |
6454 | builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0); |
6455 | builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0); | |
207bf79d JM |
6456 | if (SIG_ATOMIC_TYPE) |
6457 | builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0); | |
6458 | if (INT8_TYPE) | |
6459 | builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0); | |
6460 | if (INT16_TYPE) | |
6461 | builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0); | |
6462 | if (INT32_TYPE) | |
6463 | builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0); | |
6464 | if (INT64_TYPE) | |
6465 | builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0); | |
6466 | if (UINT8_TYPE) | |
6467 | builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0); | |
6468 | if (UINT16_TYPE) | |
6469 | builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0); | |
6470 | if (UINT32_TYPE) | |
6471 | builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0); | |
6472 | if (UINT64_TYPE) | |
6473 | builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0); | |
6474 | if (INT_LEAST8_TYPE) | |
6475 | builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0); | |
6476 | if (INT_LEAST16_TYPE) | |
6477 | builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0); | |
6478 | if (INT_LEAST32_TYPE) | |
6479 | builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0); | |
6480 | if (INT_LEAST64_TYPE) | |
6481 | builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0); | |
6482 | if (UINT_LEAST8_TYPE) | |
6483 | builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0); | |
6484 | if (UINT_LEAST16_TYPE) | |
6485 | builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0); | |
6486 | if (UINT_LEAST32_TYPE) | |
6487 | builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0); | |
6488 | if (UINT_LEAST64_TYPE) | |
6489 | builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0); | |
6490 | if (INT_FAST8_TYPE) | |
6491 | builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0); | |
6492 | if (INT_FAST16_TYPE) | |
6493 | builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0); | |
6494 | if (INT_FAST32_TYPE) | |
6495 | builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0); | |
6496 | if (INT_FAST64_TYPE) | |
6497 | builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0); | |
6498 | if (UINT_FAST8_TYPE) | |
6499 | builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0); | |
6500 | if (UINT_FAST16_TYPE) | |
6501 | builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0); | |
6502 | if (UINT_FAST32_TYPE) | |
6503 | builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0); | |
6504 | if (UINT_FAST64_TYPE) | |
6505 | builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0); | |
6506 | if (INTPTR_TYPE) | |
6507 | builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0); | |
6508 | if (UINTPTR_TYPE) | |
6509 | builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0); | |
676997cf RH |
6510 | } |
6511 | ||
6431177a | 6512 | static void |
35b1a6fa | 6513 | c_init_attributes (void) |
6431177a JM |
6514 | { |
6515 | /* Fill in the built_in_attributes array. */ | |
4a90aeeb | 6516 | #define DEF_ATTR_NULL_TREE(ENUM) \ |
6431177a | 6517 | built_in_attributes[(int) ENUM] = NULL_TREE; |
4a90aeeb | 6518 | #define DEF_ATTR_INT(ENUM, VALUE) \ |
c62c040f | 6519 | built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE); |
e384e6b5 BS |
6520 | #define DEF_ATTR_STRING(ENUM, VALUE) \ |
6521 | built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE); | |
6431177a JM |
6522 | #define DEF_ATTR_IDENT(ENUM, STRING) \ |
6523 | built_in_attributes[(int) ENUM] = get_identifier (STRING); | |
6524 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \ | |
6525 | built_in_attributes[(int) ENUM] \ | |
6526 | = tree_cons (built_in_attributes[(int) PURPOSE], \ | |
6527 | built_in_attributes[(int) VALUE], \ | |
6528 | built_in_attributes[(int) CHAIN]); | |
6431177a JM |
6529 | #include "builtin-attrs.def" |
6530 | #undef DEF_ATTR_NULL_TREE | |
6531 | #undef DEF_ATTR_INT | |
6532 | #undef DEF_ATTR_IDENT | |
6533 | #undef DEF_ATTR_TREE_LIST | |
03dc0325 | 6534 | } |
26f943fd | 6535 | |
943f82e7 JM |
6536 | /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain |
6537 | identifier as an argument, so the front end shouldn't look it up. */ | |
6538 | ||
6539 | bool | |
564a129d | 6540 | attribute_takes_identifier_p (const_tree attr_id) |
943f82e7 | 6541 | { |
f231b5ff | 6542 | const struct attribute_spec *spec = lookup_attribute_spec (attr_id); |
1b9b91a6 JM |
6543 | if (spec == NULL) |
6544 | /* Unknown attribute that we'll end up ignoring, return true so we | |
6545 | don't complain about an identifier argument. */ | |
6546 | return true; | |
6547 | else if (!strcmp ("mode", spec->name) | |
6548 | || !strcmp ("format", spec->name) | |
6549 | || !strcmp ("cleanup", spec->name)) | |
564a129d JM |
6550 | return true; |
6551 | else | |
6552 | return targetm.attribute_takes_identifier_p (attr_id); | |
943f82e7 JM |
6553 | } |
6554 | ||
349ae713 NB |
6555 | /* Attribute handlers common to C front ends. */ |
6556 | ||
6557 | /* Handle a "packed" attribute; arguments as in | |
6558 | struct attribute_spec.handler. */ | |
6559 | ||
6560 | static tree | |
e18476eb | 6561 | handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
a742c759 | 6562 | int flags, bool *no_add_attrs) |
349ae713 | 6563 | { |
c6e4cc53 | 6564 | if (TYPE_P (*node)) |
349ae713 NB |
6565 | { |
6566 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
8dd16ecc | 6567 | *node = build_variant_type_copy (*node); |
c6e4cc53 | 6568 | TYPE_PACKED (*node) = 1; |
349ae713 NB |
6569 | } |
6570 | else if (TREE_CODE (*node) == FIELD_DECL) | |
646c0835 | 6571 | { |
2cd36c22 AN |
6572 | if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT |
6573 | /* Still pack bitfields. */ | |
6574 | && ! DECL_INITIAL (*node)) | |
646c0835 NS |
6575 | warning (OPT_Wattributes, |
6576 | "%qE attribute ignored for field of type %qT", | |
6577 | name, TREE_TYPE (*node)); | |
6578 | else | |
6579 | DECL_PACKED (*node) = 1; | |
6580 | } | |
349ae713 | 6581 | /* We can't set DECL_PACKED for a VAR_DECL, because the bit is |
c6e4cc53 NS |
6582 | used for DECL_REGISTER. It wouldn't mean anything anyway. |
6583 | We can't set DECL_PACKED on the type of a TYPE_DECL, because | |
6584 | that changes what the typedef is typing. */ | |
349ae713 NB |
6585 | else |
6586 | { | |
5c498b10 | 6587 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
6588 | *no_add_attrs = true; |
6589 | } | |
6590 | ||
6591 | return NULL_TREE; | |
6592 | } | |
6593 | ||
6594 | /* Handle a "nocommon" attribute; arguments as in | |
6595 | struct attribute_spec.handler. */ | |
6596 | ||
6597 | static tree | |
35b1a6fa | 6598 | handle_nocommon_attribute (tree *node, tree name, |
e18476eb BI |
6599 | tree ARG_UNUSED (args), |
6600 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
349ae713 NB |
6601 | { |
6602 | if (TREE_CODE (*node) == VAR_DECL) | |
6603 | DECL_COMMON (*node) = 0; | |
6604 | else | |
6605 | { | |
5c498b10 | 6606 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
6607 | *no_add_attrs = true; |
6608 | } | |
6609 | ||
6610 | return NULL_TREE; | |
6611 | } | |
6612 | ||
6613 | /* Handle a "common" attribute; arguments as in | |
6614 | struct attribute_spec.handler. */ | |
6615 | ||
6616 | static tree | |
e18476eb BI |
6617 | handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6618 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
349ae713 NB |
6619 | { |
6620 | if (TREE_CODE (*node) == VAR_DECL) | |
6621 | DECL_COMMON (*node) = 1; | |
6622 | else | |
6623 | { | |
5c498b10 | 6624 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
6625 | *no_add_attrs = true; |
6626 | } | |
6627 | ||
6628 | return NULL_TREE; | |
6629 | } | |
6630 | ||
6631 | /* Handle a "noreturn" attribute; arguments as in | |
6632 | struct attribute_spec.handler. */ | |
6633 | ||
6634 | static tree | |
e18476eb BI |
6635 | handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6636 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
349ae713 NB |
6637 | { |
6638 | tree type = TREE_TYPE (*node); | |
6639 | ||
6640 | /* See FIXME comment in c_common_attribute_table. */ | |
2debdb4f NP |
6641 | if (TREE_CODE (*node) == FUNCTION_DECL |
6642 | || objc_method_decl (TREE_CODE (*node))) | |
349ae713 NB |
6643 | TREE_THIS_VOLATILE (*node) = 1; |
6644 | else if (TREE_CODE (type) == POINTER_TYPE | |
6645 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
6646 | TREE_TYPE (*node) | |
6e7ceb17 PC |
6647 | = (build_qualified_type |
6648 | (build_pointer_type | |
6649 | (build_type_variant (TREE_TYPE (type), | |
6650 | TYPE_READONLY (TREE_TYPE (type)), 1)), | |
6651 | TYPE_QUALS (type))); | |
349ae713 NB |
6652 | else |
6653 | { | |
5c498b10 | 6654 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
6655 | *no_add_attrs = true; |
6656 | } | |
6657 | ||
6658 | return NULL_TREE; | |
6659 | } | |
6660 | ||
52bf96d2 JH |
6661 | /* Handle a "hot" and attribute; arguments as in |
6662 | struct attribute_spec.handler. */ | |
6663 | ||
6664 | static tree | |
6665 | handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
ab442df7 | 6666 | int ARG_UNUSED (flags), bool *no_add_attrs) |
52bf96d2 | 6667 | { |
e45abe1f RH |
6668 | if (TREE_CODE (*node) == FUNCTION_DECL |
6669 | || TREE_CODE (*node) == LABEL_DECL) | |
52bf96d2 JH |
6670 | { |
6671 | if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL) | |
6672 | { | |
45484dcf MP |
6673 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " |
6674 | "with attribute %qs", name, "cold"); | |
52bf96d2 JH |
6675 | *no_add_attrs = true; |
6676 | } | |
5779e713 MM |
6677 | /* Most of the rest of the hot processing is done later with |
6678 | lookup_attribute. */ | |
52bf96d2 JH |
6679 | } |
6680 | else | |
6681 | { | |
6682 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6683 | *no_add_attrs = true; | |
6684 | } | |
6685 | ||
6686 | return NULL_TREE; | |
6687 | } | |
e45abe1f | 6688 | |
52bf96d2 JH |
6689 | /* Handle a "cold" and attribute; arguments as in |
6690 | struct attribute_spec.handler. */ | |
6691 | ||
6692 | static tree | |
6693 | handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
6694 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6695 | { | |
e45abe1f RH |
6696 | if (TREE_CODE (*node) == FUNCTION_DECL |
6697 | || TREE_CODE (*node) == LABEL_DECL) | |
52bf96d2 JH |
6698 | { |
6699 | if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL) | |
6700 | { | |
45484dcf MP |
6701 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " |
6702 | "with attribute %qs", name, "hot"); | |
52bf96d2 JH |
6703 | *no_add_attrs = true; |
6704 | } | |
5779e713 MM |
6705 | /* Most of the rest of the cold processing is done later with |
6706 | lookup_attribute. */ | |
52bf96d2 JH |
6707 | } |
6708 | else | |
6709 | { | |
6710 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6711 | *no_add_attrs = true; | |
6712 | } | |
6713 | ||
6714 | return NULL_TREE; | |
6715 | } | |
6716 | ||
e664c61c | 6717 | /* Handle a "no_sanitize_address" attribute; arguments as in |
77bc5132 JJ |
6718 | struct attribute_spec.handler. */ |
6719 | ||
6720 | static tree | |
e664c61c KS |
6721 | handle_no_sanitize_address_attribute (tree *node, tree name, tree, int, |
6722 | bool *no_add_attrs) | |
77bc5132 JJ |
6723 | { |
6724 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6725 | { | |
6726 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6727 | *no_add_attrs = true; | |
6728 | } | |
6729 | ||
6730 | return NULL_TREE; | |
6731 | } | |
6732 | ||
e664c61c KS |
6733 | /* Handle a "no_address_safety_analysis" attribute; arguments as in |
6734 | struct attribute_spec.handler. */ | |
6735 | ||
6736 | static tree | |
6737 | handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int, | |
6738 | bool *no_add_attrs) | |
6739 | { | |
6740 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6741 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6742 | else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node))) | |
6743 | DECL_ATTRIBUTES (*node) | |
6744 | = tree_cons (get_identifier ("no_sanitize_address"), | |
6745 | NULL_TREE, DECL_ATTRIBUTES (*node)); | |
6746 | *no_add_attrs = true; | |
6747 | return NULL_TREE; | |
6748 | } | |
6749 | ||
ce6923c5 MP |
6750 | /* Handle a "no_sanitize_undefined" attribute; arguments as in |
6751 | struct attribute_spec.handler. */ | |
6752 | ||
6753 | static tree | |
6754 | handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int, | |
6755 | bool *no_add_attrs) | |
6756 | { | |
6757 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6758 | { | |
6759 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6760 | *no_add_attrs = true; | |
6761 | } | |
6762 | ||
6763 | return NULL_TREE; | |
6764 | } | |
6765 | ||
349ae713 NB |
6766 | /* Handle a "noinline" attribute; arguments as in |
6767 | struct attribute_spec.handler. */ | |
6768 | ||
6769 | static tree | |
35b1a6fa | 6770 | handle_noinline_attribute (tree *node, tree name, |
e18476eb BI |
6771 | tree ARG_UNUSED (args), |
6772 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
349ae713 NB |
6773 | { |
6774 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
45484dcf MP |
6775 | { |
6776 | if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node))) | |
6777 | { | |
6778 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " | |
6779 | "with attribute %qs", name, "always_inline"); | |
6780 | *no_add_attrs = true; | |
6781 | } | |
6782 | else | |
6783 | DECL_UNINLINABLE (*node) = 1; | |
6784 | } | |
349ae713 NB |
6785 | else |
6786 | { | |
5c498b10 | 6787 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
6788 | *no_add_attrs = true; |
6789 | } | |
6790 | ||
6791 | return NULL_TREE; | |
6792 | } | |
6793 | ||
86631ea3 MJ |
6794 | /* Handle a "noclone" attribute; arguments as in |
6795 | struct attribute_spec.handler. */ | |
6796 | ||
6797 | static tree | |
6798 | handle_noclone_attribute (tree *node, tree name, | |
6799 | tree ARG_UNUSED (args), | |
6800 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6801 | { | |
6802 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6803 | { | |
6804 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6805 | *no_add_attrs = true; | |
6806 | } | |
6807 | ||
6808 | return NULL_TREE; | |
6809 | } | |
6810 | ||
349ae713 NB |
6811 | /* Handle a "always_inline" attribute; arguments as in |
6812 | struct attribute_spec.handler. */ | |
6813 | ||
6814 | static tree | |
35b1a6fa | 6815 | handle_always_inline_attribute (tree *node, tree name, |
e18476eb BI |
6816 | tree ARG_UNUSED (args), |
6817 | int ARG_UNUSED (flags), | |
a742c759 | 6818 | bool *no_add_attrs) |
349ae713 NB |
6819 | { |
6820 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6821 | { | |
45484dcf MP |
6822 | if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node))) |
6823 | { | |
6824 | warning (OPT_Wattributes, "%qE attribute ignored due to conflict " | |
6825 | "with %qs attribute", name, "noinline"); | |
6826 | *no_add_attrs = true; | |
6827 | } | |
6828 | else | |
6829 | /* Set the attribute and mark it for disregarding inline | |
6830 | limits. */ | |
6831 | DECL_DISREGARD_INLINE_LIMITS (*node) = 1; | |
349ae713 NB |
6832 | } |
6833 | else | |
6834 | { | |
5c498b10 | 6835 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 | 6836 | *no_add_attrs = true; |
4eb7fd83 JJ |
6837 | } |
6838 | ||
6839 | return NULL_TREE; | |
6840 | } | |
6841 | ||
6842 | /* Handle a "gnu_inline" attribute; arguments as in | |
6843 | struct attribute_spec.handler. */ | |
6844 | ||
6845 | static tree | |
6846 | handle_gnu_inline_attribute (tree *node, tree name, | |
6847 | tree ARG_UNUSED (args), | |
6848 | int ARG_UNUSED (flags), | |
6849 | bool *no_add_attrs) | |
6850 | { | |
6851 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6852 | { | |
6853 | /* Do nothing else, just set the attribute. We'll get at | |
6854 | it later with lookup_attribute. */ | |
6855 | } | |
6856 | else | |
6857 | { | |
6858 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6859 | *no_add_attrs = true; | |
46a4da10 JH |
6860 | } |
6861 | ||
6862 | return NULL_TREE; | |
6863 | } | |
6864 | ||
6865 | /* Handle a "leaf" attribute; arguments as in | |
6866 | struct attribute_spec.handler. */ | |
6867 | ||
6868 | static tree | |
6869 | handle_leaf_attribute (tree *node, tree name, | |
6870 | tree ARG_UNUSED (args), | |
6871 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6872 | { | |
6873 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6874 | { | |
6875 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6876 | *no_add_attrs = true; | |
6877 | } | |
6878 | if (!TREE_PUBLIC (*node)) | |
6879 | { | |
6880 | warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name); | |
6881 | *no_add_attrs = true; | |
349ae713 NB |
6882 | } |
6883 | ||
6884 | return NULL_TREE; | |
6885 | } | |
6886 | ||
d752cfdb JJ |
6887 | /* Handle an "artificial" attribute; arguments as in |
6888 | struct attribute_spec.handler. */ | |
6889 | ||
6890 | static tree | |
6891 | handle_artificial_attribute (tree *node, tree name, | |
6892 | tree ARG_UNUSED (args), | |
6893 | int ARG_UNUSED (flags), | |
6894 | bool *no_add_attrs) | |
6895 | { | |
6896 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6897 | { | |
6898 | /* Do nothing else, just set the attribute. We'll get at | |
6899 | it later with lookup_attribute. */ | |
6900 | } | |
6901 | else | |
6902 | { | |
6903 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6904 | *no_add_attrs = true; | |
6905 | } | |
6906 | ||
6907 | return NULL_TREE; | |
6908 | } | |
6909 | ||
0691d1d4 RG |
6910 | /* Handle a "flatten" attribute; arguments as in |
6911 | struct attribute_spec.handler. */ | |
6912 | ||
6913 | static tree | |
6914 | handle_flatten_attribute (tree *node, tree name, | |
c22cacf3 MS |
6915 | tree args ATTRIBUTE_UNUSED, |
6916 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
0691d1d4 RG |
6917 | { |
6918 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6919 | /* Do nothing else, just set the attribute. We'll get at | |
6920 | it later with lookup_attribute. */ | |
6921 | ; | |
6922 | else | |
6923 | { | |
6924 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6925 | *no_add_attrs = true; | |
6926 | } | |
6927 | ||
6928 | return NULL_TREE; | |
6929 | } | |
6930 | ||
d2af6a68 JJ |
6931 | /* Handle a "warning" or "error" attribute; arguments as in |
6932 | struct attribute_spec.handler. */ | |
6933 | ||
6934 | static tree | |
6935 | handle_error_attribute (tree *node, tree name, tree args, | |
6936 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6937 | { | |
6938 | if (TREE_CODE (*node) == FUNCTION_DECL | |
cf35e2b1 | 6939 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) |
d2af6a68 JJ |
6940 | /* Do nothing else, just set the attribute. We'll get at |
6941 | it later with lookup_attribute. */ | |
6942 | ; | |
6943 | else | |
6944 | { | |
6945 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6946 | *no_add_attrs = true; | |
6947 | } | |
6948 | ||
6949 | return NULL_TREE; | |
6950 | } | |
0691d1d4 | 6951 | |
349ae713 NB |
6952 | /* Handle a "used" attribute; arguments as in |
6953 | struct attribute_spec.handler. */ | |
6954 | ||
6955 | static tree | |
e18476eb BI |
6956 | handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args), |
6957 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
349ae713 | 6958 | { |
d7ddbe24 RH |
6959 | tree node = *pnode; |
6960 | ||
6961 | if (TREE_CODE (node) == FUNCTION_DECL | |
3797cb21 DS |
6962 | || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)) |
6963 | || (TREE_CODE (node) == TYPE_DECL)) | |
4d7d0451 | 6964 | { |
4d7d0451 | 6965 | TREE_USED (node) = 1; |
8e3e233b | 6966 | DECL_PRESERVE_P (node) = 1; |
ebfbbdc5 JJ |
6967 | if (TREE_CODE (node) == VAR_DECL) |
6968 | DECL_READ_P (node) = 1; | |
4d7d0451 | 6969 | } |
349ae713 NB |
6970 | else |
6971 | { | |
5c498b10 | 6972 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
6973 | *no_add_attrs = true; |
6974 | } | |
6975 | ||
6976 | return NULL_TREE; | |
6977 | } | |
6978 | ||
6979 | /* Handle a "unused" attribute; arguments as in | |
6980 | struct attribute_spec.handler. */ | |
6981 | ||
6982 | static tree | |
e18476eb BI |
6983 | handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6984 | int flags, bool *no_add_attrs) | |
349ae713 NB |
6985 | { |
6986 | if (DECL_P (*node)) | |
6987 | { | |
6988 | tree decl = *node; | |
6989 | ||
6990 | if (TREE_CODE (decl) == PARM_DECL | |
6991 | || TREE_CODE (decl) == VAR_DECL | |
6992 | || TREE_CODE (decl) == FUNCTION_DECL | |
6993 | || TREE_CODE (decl) == LABEL_DECL | |
6994 | || TREE_CODE (decl) == TYPE_DECL) | |
ebfbbdc5 JJ |
6995 | { |
6996 | TREE_USED (decl) = 1; | |
6997 | if (TREE_CODE (decl) == VAR_DECL | |
6998 | || TREE_CODE (decl) == PARM_DECL) | |
6999 | DECL_READ_P (decl) = 1; | |
7000 | } | |
349ae713 NB |
7001 | else |
7002 | { | |
5c498b10 | 7003 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
7004 | *no_add_attrs = true; |
7005 | } | |
7006 | } | |
7007 | else | |
7008 | { | |
7009 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
8dd16ecc | 7010 | *node = build_variant_type_copy (*node); |
349ae713 NB |
7011 | TREE_USED (*node) = 1; |
7012 | } | |
7013 | ||
7014 | return NULL_TREE; | |
7015 | } | |
7016 | ||
ce91e74c JH |
7017 | /* Handle a "externally_visible" attribute; arguments as in |
7018 | struct attribute_spec.handler. */ | |
7019 | ||
7020 | static tree | |
7021 | handle_externally_visible_attribute (tree *pnode, tree name, | |
7022 | tree ARG_UNUSED (args), | |
7023 | int ARG_UNUSED (flags), | |
7024 | bool *no_add_attrs) | |
7025 | { | |
7026 | tree node = *pnode; | |
7027 | ||
343d4b27 | 7028 | if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL) |
ce91e74c | 7029 | { |
343d4b27 JJ |
7030 | if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL |
7031 | && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node)) | |
7032 | { | |
7033 | warning (OPT_Wattributes, | |
7034 | "%qE attribute have effect only on public objects", name); | |
7035 | *no_add_attrs = true; | |
7036 | } | |
ce91e74c | 7037 | } |
ce91e74c JH |
7038 | else |
7039 | { | |
7040 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7041 | *no_add_attrs = true; | |
7042 | } | |
7043 | ||
7044 | return NULL_TREE; | |
7045 | } | |
7046 | ||
7861b648 AK |
7047 | /* Handle the "no_reorder" attribute. Arguments as in |
7048 | struct attribute_spec.handler. */ | |
7049 | ||
7050 | static tree | |
7051 | handle_no_reorder_attribute (tree *pnode, | |
7052 | tree name, | |
7053 | tree, | |
7054 | int, | |
7055 | bool *no_add_attrs) | |
7056 | { | |
7057 | tree node = *pnode; | |
7058 | ||
7059 | if ((TREE_CODE (node) != FUNCTION_DECL && TREE_CODE (node) != VAR_DECL) | |
7060 | && !(TREE_STATIC (node) || DECL_EXTERNAL (node))) | |
7061 | { | |
7062 | warning (OPT_Wattributes, | |
7063 | "%qE attribute only affects top level objects", | |
7064 | name); | |
7065 | *no_add_attrs = true; | |
7066 | } | |
7067 | ||
7068 | return NULL_TREE; | |
7069 | } | |
7070 | ||
349ae713 NB |
7071 | /* Handle a "const" attribute; arguments as in |
7072 | struct attribute_spec.handler. */ | |
7073 | ||
7074 | static tree | |
e18476eb BI |
7075 | handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
7076 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
349ae713 NB |
7077 | { |
7078 | tree type = TREE_TYPE (*node); | |
7079 | ||
7080 | /* See FIXME comment on noreturn in c_common_attribute_table. */ | |
7081 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
7082 | TREE_READONLY (*node) = 1; | |
7083 | else if (TREE_CODE (type) == POINTER_TYPE | |
7084 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
7085 | TREE_TYPE (*node) | |
6e7ceb17 PC |
7086 | = (build_qualified_type |
7087 | (build_pointer_type | |
7088 | (build_type_variant (TREE_TYPE (type), 1, | |
7089 | TREE_THIS_VOLATILE (TREE_TYPE (type)))), | |
7090 | TYPE_QUALS (type))); | |
349ae713 NB |
7091 | else |
7092 | { | |
5c498b10 | 7093 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
7094 | *no_add_attrs = true; |
7095 | } | |
7096 | ||
7097 | return NULL_TREE; | |
7098 | } | |
7099 | ||
7100 | /* Handle a "transparent_union" attribute; arguments as in | |
7101 | struct attribute_spec.handler. */ | |
7102 | ||
7103 | static tree | |
35b1a6fa | 7104 | handle_transparent_union_attribute (tree *node, tree name, |
e18476eb | 7105 | tree ARG_UNUSED (args), int flags, |
a742c759 | 7106 | bool *no_add_attrs) |
349ae713 | 7107 | { |
4009f2e7 | 7108 | tree type; |
52dd234b RH |
7109 | |
7110 | *no_add_attrs = true; | |
349ae713 | 7111 | |
e28d52cf DS |
7112 | |
7113 | if (TREE_CODE (*node) == TYPE_DECL | |
7114 | && ! (flags & ATTR_FLAG_CXX11)) | |
4009f2e7 JM |
7115 | node = &TREE_TYPE (*node); |
7116 | type = *node; | |
349ae713 | 7117 | |
52dd234b | 7118 | if (TREE_CODE (type) == UNION_TYPE) |
349ae713 | 7119 | { |
d58d6eb5 JM |
7120 | /* Make sure that the first field will work for a transparent union. |
7121 | If the type isn't complete yet, leave the check to the code in | |
7122 | finish_struct. */ | |
7123 | if (TYPE_SIZE (type)) | |
7124 | { | |
7125 | tree first = first_field (type); | |
7126 | if (first == NULL_TREE | |
7127 | || DECL_ARTIFICIAL (first) | |
7128 | || TYPE_MODE (type) != DECL_MODE (first)) | |
7129 | goto ignored; | |
7130 | } | |
7131 | ||
349ae713 | 7132 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
52dd234b | 7133 | { |
d58d6eb5 JM |
7134 | /* If the type isn't complete yet, setting the flag |
7135 | on a variant wouldn't ever be checked. */ | |
7136 | if (!TYPE_SIZE (type)) | |
7137 | goto ignored; | |
7138 | ||
7139 | /* build_duplicate_type doesn't work for C++. */ | |
7140 | if (c_dialect_cxx ()) | |
52dd234b RH |
7141 | goto ignored; |
7142 | ||
7143 | /* A type variant isn't good enough, since we don't a cast | |
7144 | to such a type removed as a no-op. */ | |
7145 | *node = type = build_duplicate_type (type); | |
7146 | } | |
7147 | ||
ebf0bf7f | 7148 | TYPE_TRANSPARENT_AGGR (type) = 1; |
52dd234b | 7149 | return NULL_TREE; |
349ae713 NB |
7150 | } |
7151 | ||
52dd234b RH |
7152 | ignored: |
7153 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
349ae713 NB |
7154 | return NULL_TREE; |
7155 | } | |
7156 | ||
fc8600f9 MM |
7157 | /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to |
7158 | get the requested priority for a constructor or destructor, | |
7159 | possibly issuing diagnostics for invalid or reserved | |
7160 | priorities. */ | |
7161 | ||
7162 | static priority_type | |
7163 | get_priority (tree args, bool is_destructor) | |
7164 | { | |
7165 | HOST_WIDE_INT pri; | |
b2f4bed8 | 7166 | tree arg; |
fc8600f9 MM |
7167 | |
7168 | if (!args) | |
7169 | return DEFAULT_INIT_PRIORITY; | |
b8698a0f | 7170 | |
f6fc5c86 MM |
7171 | if (!SUPPORTS_INIT_PRIORITY) |
7172 | { | |
7173 | if (is_destructor) | |
7174 | error ("destructor priorities are not supported"); | |
7175 | else | |
7176 | error ("constructor priorities are not supported"); | |
7177 | return DEFAULT_INIT_PRIORITY; | |
7178 | } | |
7179 | ||
b2f4bed8 | 7180 | arg = TREE_VALUE (args); |
fad7652e JJ |
7181 | if (TREE_CODE (arg) == IDENTIFIER_NODE) |
7182 | goto invalid; | |
7183 | if (arg == error_mark_node) | |
7184 | return DEFAULT_INIT_PRIORITY; | |
8d0d1915 | 7185 | arg = default_conversion (arg); |
9541ffee | 7186 | if (!tree_fits_shwi_p (arg) |
b2f4bed8 | 7187 | || !INTEGRAL_TYPE_P (TREE_TYPE (arg))) |
fc8600f9 MM |
7188 | goto invalid; |
7189 | ||
9439e9a1 | 7190 | pri = tree_to_shwi (arg); |
fc8600f9 MM |
7191 | if (pri < 0 || pri > MAX_INIT_PRIORITY) |
7192 | goto invalid; | |
7193 | ||
7194 | if (pri <= MAX_RESERVED_INIT_PRIORITY) | |
7195 | { | |
7196 | if (is_destructor) | |
7197 | warning (0, | |
7198 | "destructor priorities from 0 to %d are reserved " | |
b8698a0f | 7199 | "for the implementation", |
fc8600f9 MM |
7200 | MAX_RESERVED_INIT_PRIORITY); |
7201 | else | |
7202 | warning (0, | |
7203 | "constructor priorities from 0 to %d are reserved " | |
b8698a0f | 7204 | "for the implementation", |
fc8600f9 MM |
7205 | MAX_RESERVED_INIT_PRIORITY); |
7206 | } | |
7207 | return pri; | |
7208 | ||
7209 | invalid: | |
7210 | if (is_destructor) | |
7211 | error ("destructor priorities must be integers from 0 to %d inclusive", | |
7212 | MAX_INIT_PRIORITY); | |
7213 | else | |
7214 | error ("constructor priorities must be integers from 0 to %d inclusive", | |
7215 | MAX_INIT_PRIORITY); | |
7216 | return DEFAULT_INIT_PRIORITY; | |
7217 | } | |
7218 | ||
349ae713 NB |
7219 | /* Handle a "constructor" attribute; arguments as in |
7220 | struct attribute_spec.handler. */ | |
7221 | ||
7222 | static tree | |
fc8600f9 | 7223 | handle_constructor_attribute (tree *node, tree name, tree args, |
e18476eb | 7224 | int ARG_UNUSED (flags), |
a742c759 | 7225 | bool *no_add_attrs) |
349ae713 NB |
7226 | { |
7227 | tree decl = *node; | |
7228 | tree type = TREE_TYPE (decl); | |
7229 | ||
7230 | if (TREE_CODE (decl) == FUNCTION_DECL | |
7231 | && TREE_CODE (type) == FUNCTION_TYPE | |
7232 | && decl_function_context (decl) == 0) | |
7233 | { | |
fc8600f9 | 7234 | priority_type priority; |
349ae713 | 7235 | DECL_STATIC_CONSTRUCTOR (decl) = 1; |
fc8600f9 MM |
7236 | priority = get_priority (args, /*is_destructor=*/false); |
7237 | SET_DECL_INIT_PRIORITY (decl, priority); | |
349ae713 NB |
7238 | TREE_USED (decl) = 1; |
7239 | } | |
7240 | else | |
7241 | { | |
5c498b10 | 7242 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
7243 | *no_add_attrs = true; |
7244 | } | |
7245 | ||
7246 | return NULL_TREE; | |
7247 | } | |
7248 | ||
7249 | /* Handle a "destructor" attribute; arguments as in | |
7250 | struct attribute_spec.handler. */ | |
7251 | ||
7252 | static tree | |
fc8600f9 | 7253 | handle_destructor_attribute (tree *node, tree name, tree args, |
e18476eb | 7254 | int ARG_UNUSED (flags), |
a742c759 | 7255 | bool *no_add_attrs) |
349ae713 NB |
7256 | { |
7257 | tree decl = *node; | |
7258 | tree type = TREE_TYPE (decl); | |
7259 | ||
7260 | if (TREE_CODE (decl) == FUNCTION_DECL | |
7261 | && TREE_CODE (type) == FUNCTION_TYPE | |
7262 | && decl_function_context (decl) == 0) | |
7263 | { | |
fc8600f9 | 7264 | priority_type priority; |
349ae713 | 7265 | DECL_STATIC_DESTRUCTOR (decl) = 1; |
fc8600f9 MM |
7266 | priority = get_priority (args, /*is_destructor=*/true); |
7267 | SET_DECL_FINI_PRIORITY (decl, priority); | |
349ae713 NB |
7268 | TREE_USED (decl) = 1; |
7269 | } | |
7270 | else | |
7271 | { | |
5c498b10 | 7272 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
7273 | *no_add_attrs = true; |
7274 | } | |
7275 | ||
7276 | return NULL_TREE; | |
7277 | } | |
7278 | ||
4d451982 MLI |
7279 | /* Nonzero if the mode is a valid vector mode for this architecture. |
7280 | This returns nonzero even if there is no hardware support for the | |
7281 | vector mode, but we can emulate with narrower modes. */ | |
7282 | ||
7283 | static int | |
ef4bddc2 | 7284 | vector_mode_valid_p (machine_mode mode) |
4d451982 MLI |
7285 | { |
7286 | enum mode_class mclass = GET_MODE_CLASS (mode); | |
ef4bddc2 | 7287 | machine_mode innermode; |
4d451982 MLI |
7288 | |
7289 | /* Doh! What's going on? */ | |
7290 | if (mclass != MODE_VECTOR_INT | |
7291 | && mclass != MODE_VECTOR_FLOAT | |
7292 | && mclass != MODE_VECTOR_FRACT | |
7293 | && mclass != MODE_VECTOR_UFRACT | |
7294 | && mclass != MODE_VECTOR_ACCUM | |
7295 | && mclass != MODE_VECTOR_UACCUM) | |
7296 | return 0; | |
7297 | ||
7298 | /* Hardware support. Woo hoo! */ | |
7299 | if (targetm.vector_mode_supported_p (mode)) | |
7300 | return 1; | |
7301 | ||
7302 | innermode = GET_MODE_INNER (mode); | |
7303 | ||
7304 | /* We should probably return 1 if requesting V4DI and we have no DI, | |
7305 | but we have V2DI, but this is probably very unlikely. */ | |
7306 | ||
7307 | /* If we have support for the inner mode, we can safely emulate it. | |
7308 | We may not have V2DI, but me can emulate with a pair of DIs. */ | |
7309 | return targetm.scalar_mode_supported_p (innermode); | |
7310 | } | |
7311 | ||
7312 | ||
349ae713 NB |
7313 | /* Handle a "mode" attribute; arguments as in |
7314 | struct attribute_spec.handler. */ | |
7315 | ||
7316 | static tree | |
e18476eb BI |
7317 | handle_mode_attribute (tree *node, tree name, tree args, |
7318 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
349ae713 NB |
7319 | { |
7320 | tree type = *node; | |
88388a52 | 7321 | tree ident = TREE_VALUE (args); |
349ae713 NB |
7322 | |
7323 | *no_add_attrs = true; | |
7324 | ||
88388a52 | 7325 | if (TREE_CODE (ident) != IDENTIFIER_NODE) |
5c498b10 | 7326 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
7327 | else |
7328 | { | |
7329 | int j; | |
88388a52 | 7330 | const char *p = IDENTIFIER_POINTER (ident); |
349ae713 | 7331 | int len = strlen (p); |
ef4bddc2 | 7332 | machine_mode mode = VOIDmode; |
349ae713 | 7333 | tree typefm; |
6dd53648 | 7334 | bool valid_mode; |
349ae713 NB |
7335 | |
7336 | if (len > 4 && p[0] == '_' && p[1] == '_' | |
7337 | && p[len - 1] == '_' && p[len - 2] == '_') | |
7338 | { | |
28dab132 | 7339 | char *newp = (char *) alloca (len - 1); |
349ae713 NB |
7340 | |
7341 | strcpy (newp, &p[2]); | |
7342 | newp[len - 4] = '\0'; | |
7343 | p = newp; | |
7344 | } | |
7345 | ||
7346 | /* Change this type to have a type with the specified mode. | |
7347 | First check for the special modes. */ | |
3f75a254 | 7348 | if (!strcmp (p, "byte")) |
349ae713 NB |
7349 | mode = byte_mode; |
7350 | else if (!strcmp (p, "word")) | |
7351 | mode = word_mode; | |
3f75a254 | 7352 | else if (!strcmp (p, "pointer")) |
349ae713 | 7353 | mode = ptr_mode; |
c7ff6e7a AK |
7354 | else if (!strcmp (p, "libgcc_cmp_return")) |
7355 | mode = targetm.libgcc_cmp_return_mode (); | |
7356 | else if (!strcmp (p, "libgcc_shift_count")) | |
7357 | mode = targetm.libgcc_shift_count_mode (); | |
7b0518e3 UW |
7358 | else if (!strcmp (p, "unwind_word")) |
7359 | mode = targetm.unwind_word_mode (); | |
349ae713 NB |
7360 | else |
7361 | for (j = 0; j < NUM_MACHINE_MODES; j++) | |
7362 | if (!strcmp (p, GET_MODE_NAME (j))) | |
61f03aba | 7363 | { |
ef4bddc2 | 7364 | mode = (machine_mode) j; |
61f03aba RH |
7365 | break; |
7366 | } | |
349ae713 NB |
7367 | |
7368 | if (mode == VOIDmode) | |
4a5eab38 | 7369 | { |
88388a52 | 7370 | error ("unknown machine mode %qE", ident); |
4a5eab38 PB |
7371 | return NULL_TREE; |
7372 | } | |
7373 | ||
6dd53648 RH |
7374 | valid_mode = false; |
7375 | switch (GET_MODE_CLASS (mode)) | |
4a5eab38 | 7376 | { |
6dd53648 RH |
7377 | case MODE_INT: |
7378 | case MODE_PARTIAL_INT: | |
7379 | case MODE_FLOAT: | |
9a8ce21f | 7380 | case MODE_DECIMAL_FLOAT: |
ab22c1fa CF |
7381 | case MODE_FRACT: |
7382 | case MODE_UFRACT: | |
7383 | case MODE_ACCUM: | |
7384 | case MODE_UACCUM: | |
6dd53648 RH |
7385 | valid_mode = targetm.scalar_mode_supported_p (mode); |
7386 | break; | |
7387 | ||
7388 | case MODE_COMPLEX_INT: | |
7389 | case MODE_COMPLEX_FLOAT: | |
7390 | valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode)); | |
7391 | break; | |
7392 | ||
7393 | case MODE_VECTOR_INT: | |
7394 | case MODE_VECTOR_FLOAT: | |
ab22c1fa CF |
7395 | case MODE_VECTOR_FRACT: |
7396 | case MODE_VECTOR_UFRACT: | |
7397 | case MODE_VECTOR_ACCUM: | |
7398 | case MODE_VECTOR_UACCUM: | |
5c498b10 DD |
7399 | warning (OPT_Wattributes, "specifying vector types with " |
7400 | "__attribute__ ((mode)) is deprecated"); | |
7401 | warning (OPT_Wattributes, | |
7402 | "use __attribute__ ((vector_size)) instead"); | |
6dd53648 RH |
7403 | valid_mode = vector_mode_valid_p (mode); |
7404 | break; | |
4a5eab38 | 7405 | |
6dd53648 RH |
7406 | default: |
7407 | break; | |
7408 | } | |
7409 | if (!valid_mode) | |
7410 | { | |
9e637a26 | 7411 | error ("unable to emulate %qs", p); |
6dd53648 RH |
7412 | return NULL_TREE; |
7413 | } | |
4a5eab38 | 7414 | |
6dd53648 | 7415 | if (POINTER_TYPE_P (type)) |
cb2a532e | 7416 | { |
36c5e70a | 7417 | addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type)); |
ef4bddc2 | 7418 | tree (*fn)(tree, machine_mode, bool); |
6dd53648 | 7419 | |
36c5e70a | 7420 | if (!targetm.addr_space.valid_pointer_mode (mode, as)) |
cb2a532e | 7421 | { |
9e637a26 | 7422 | error ("invalid pointer mode %qs", p); |
cb2a532e AH |
7423 | return NULL_TREE; |
7424 | } | |
7425 | ||
c22cacf3 | 7426 | if (TREE_CODE (type) == POINTER_TYPE) |
6dd53648 | 7427 | fn = build_pointer_type_for_mode; |
4977bab6 | 7428 | else |
6dd53648 RH |
7429 | fn = build_reference_type_for_mode; |
7430 | typefm = fn (TREE_TYPE (type), mode, false); | |
cb2a532e | 7431 | } |
6dd53648 | 7432 | else |
ab22c1fa CF |
7433 | { |
7434 | /* For fixed-point modes, we need to test if the signness of type | |
7435 | and the machine mode are consistent. */ | |
7436 | if (ALL_FIXED_POINT_MODE_P (mode) | |
7437 | && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode)) | |
7438 | { | |
d8a07487 | 7439 | error ("signedness of type and machine mode %qs don%'t match", p); |
ab22c1fa CF |
7440 | return NULL_TREE; |
7441 | } | |
7442 | /* For fixed-point modes, we need to pass saturating info. */ | |
7443 | typefm = lang_hooks.types.type_for_mode (mode, | |
7444 | ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type) | |
7445 | : TYPE_UNSIGNED (type)); | |
7446 | } | |
ec8465a5 | 7447 | |
6dd53648 RH |
7448 | if (typefm == NULL_TREE) |
7449 | { | |
61f03aba | 7450 | error ("no data type for mode %qs", p); |
6dd53648 RH |
7451 | return NULL_TREE; |
7452 | } | |
ec8465a5 RK |
7453 | else if (TREE_CODE (type) == ENUMERAL_TYPE) |
7454 | { | |
7455 | /* For enumeral types, copy the precision from the integer | |
7456 | type returned above. If not an INTEGER_TYPE, we can't use | |
7457 | this mode for this type. */ | |
7458 | if (TREE_CODE (typefm) != INTEGER_TYPE) | |
7459 | { | |
61f03aba | 7460 | error ("cannot use mode %qs for enumeral types", p); |
ec8465a5 RK |
7461 | return NULL_TREE; |
7462 | } | |
7463 | ||
99db1ef0 RH |
7464 | if (flags & ATTR_FLAG_TYPE_IN_PLACE) |
7465 | { | |
7466 | TYPE_PRECISION (type) = TYPE_PRECISION (typefm); | |
7467 | typefm = type; | |
7468 | } | |
7469 | else | |
7470 | { | |
7471 | /* We cannot build a type variant, as there's code that assumes | |
7472 | that TYPE_MAIN_VARIANT has the same mode. This includes the | |
7473 | debug generators. Instead, create a subrange type. This | |
7474 | results in all of the enumeral values being emitted only once | |
7475 | in the original, and the subtype gets them by reference. */ | |
7476 | if (TYPE_UNSIGNED (type)) | |
7477 | typefm = make_unsigned_type (TYPE_PRECISION (typefm)); | |
7478 | else | |
7479 | typefm = make_signed_type (TYPE_PRECISION (typefm)); | |
7480 | TREE_TYPE (typefm) = type; | |
7481 | } | |
ec8465a5 | 7482 | } |
a2d36602 RH |
7483 | else if (VECTOR_MODE_P (mode) |
7484 | ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm)) | |
7485 | : TREE_CODE (type) != TREE_CODE (typefm)) | |
61f03aba RH |
7486 | { |
7487 | error ("mode %qs applied to inappropriate type", p); | |
7488 | return NULL_TREE; | |
7489 | } | |
7490 | ||
6dd53648 | 7491 | *node = typefm; |
349ae713 NB |
7492 | } |
7493 | ||
7494 | return NULL_TREE; | |
7495 | } | |
7496 | ||
7497 | /* Handle a "section" attribute; arguments as in | |
7498 | struct attribute_spec.handler. */ | |
7499 | ||
7500 | static tree | |
e18476eb BI |
7501 | handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
7502 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
349ae713 NB |
7503 | { |
7504 | tree decl = *node; | |
7505 | ||
677f3fa8 | 7506 | if (targetm_common.have_named_sections) |
349ae713 | 7507 | { |
9fb32434 CT |
7508 | user_defined_section_attribute = true; |
7509 | ||
349ae713 NB |
7510 | if ((TREE_CODE (decl) == FUNCTION_DECL |
7511 | || TREE_CODE (decl) == VAR_DECL) | |
7512 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
7513 | { | |
7514 | if (TREE_CODE (decl) == VAR_DECL | |
7515 | && current_function_decl != NULL_TREE | |
3f75a254 | 7516 | && !TREE_STATIC (decl)) |
349ae713 | 7517 | { |
b8698a0f | 7518 | error_at (DECL_SOURCE_LOCATION (decl), |
c5d75364 MLI |
7519 | "section attribute cannot be specified for " |
7520 | "local variables"); | |
349ae713 NB |
7521 | *no_add_attrs = true; |
7522 | } | |
7523 | ||
7524 | /* The decl may have already been given a section attribute | |
7525 | from a previous declaration. Ensure they match. */ | |
f961457f JH |
7526 | else if (DECL_SECTION_NAME (decl) != NULL |
7527 | && strcmp (DECL_SECTION_NAME (decl), | |
349ae713 NB |
7528 | TREE_STRING_POINTER (TREE_VALUE (args))) != 0) |
7529 | { | |
dee15844 JM |
7530 | error ("section of %q+D conflicts with previous declaration", |
7531 | *node); | |
349ae713 NB |
7532 | *no_add_attrs = true; |
7533 | } | |
feb60f03 NS |
7534 | else if (TREE_CODE (decl) == VAR_DECL |
7535 | && !targetm.have_tls && targetm.emutls.tmpl_section | |
7536 | && DECL_THREAD_LOCAL_P (decl)) | |
7537 | { | |
7538 | error ("section of %q+D cannot be overridden", *node); | |
7539 | *no_add_attrs = true; | |
7540 | } | |
349ae713 | 7541 | else |
f961457f JH |
7542 | set_decl_section_name (decl, |
7543 | TREE_STRING_POINTER (TREE_VALUE (args))); | |
349ae713 NB |
7544 | } |
7545 | else | |
7546 | { | |
dee15844 | 7547 | error ("section attribute not allowed for %q+D", *node); |
349ae713 NB |
7548 | *no_add_attrs = true; |
7549 | } | |
7550 | } | |
7551 | else | |
7552 | { | |
c5d75364 MLI |
7553 | error_at (DECL_SOURCE_LOCATION (*node), |
7554 | "section attributes are not supported for this target"); | |
349ae713 NB |
7555 | *no_add_attrs = true; |
7556 | } | |
7557 | ||
7558 | return NULL_TREE; | |
7559 | } | |
7560 | ||
d19fa6b5 JM |
7561 | /* Check whether ALIGN is a valid user-specified alignment. If so, |
7562 | return its base-2 log; if not, output an error and return -1. If | |
7563 | ALLOW_ZERO then 0 is valid and should result in a return of -1 with | |
7564 | no error. */ | |
7565 | int | |
7566 | check_user_alignment (const_tree align, bool allow_zero) | |
7567 | { | |
7568 | int i; | |
7569 | ||
661a0813 MP |
7570 | if (error_operand_p (align)) |
7571 | return -1; | |
a859517f MP |
7572 | if (TREE_CODE (align) != INTEGER_CST |
7573 | || !INTEGRAL_TYPE_P (TREE_TYPE (align))) | |
d19fa6b5 JM |
7574 | { |
7575 | error ("requested alignment is not an integer constant"); | |
7576 | return -1; | |
7577 | } | |
7578 | else if (allow_zero && integer_zerop (align)) | |
7579 | return -1; | |
3f12f6e9 SKS |
7580 | else if (tree_int_cst_sgn (align) == -1 |
7581 | || (i = tree_log2 (align)) == -1) | |
d19fa6b5 | 7582 | { |
3f12f6e9 | 7583 | error ("requested alignment is not a positive power of 2"); |
d19fa6b5 JM |
7584 | return -1; |
7585 | } | |
7586 | else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG) | |
7587 | { | |
7588 | error ("requested alignment is too large"); | |
7589 | return -1; | |
7590 | } | |
7591 | return i; | |
7592 | } | |
7593 | ||
e28d52cf DS |
7594 | /* |
7595 | If in c++-11, check if the c++-11 alignment constraint with respect | |
7596 | to fundamental alignment (in [dcl.align]) are satisfied. If not in | |
7597 | c++-11 mode, does nothing. | |
7598 | ||
7599 | [dcl.align]2/ says: | |
7600 | ||
7601 | [* if the constant expression evaluates to a fundamental alignment, | |
7602 | the alignment requirement of the declared entity shall be the | |
7603 | specified fundamental alignment. | |
7604 | ||
7605 | * if the constant expression evaluates to an extended alignment | |
7606 | and the implementation supports that alignment in the context | |
7607 | of the declaration, the alignment of the declared entity shall | |
7608 | be that alignment | |
7609 | ||
7610 | * if the constant expression evaluates to an extended alignment | |
7611 | and the implementation does not support that alignment in the | |
7612 | context of the declaration, the program is ill-formed]. */ | |
7613 | ||
7614 | static bool | |
7615 | check_cxx_fundamental_alignment_constraints (tree node, | |
7616 | unsigned align_log, | |
7617 | int flags) | |
7618 | { | |
7619 | bool alignment_too_large_p = false; | |
7620 | unsigned requested_alignment = 1U << align_log; | |
7621 | unsigned max_align = 0; | |
7622 | ||
7623 | if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat) | |
7624 | || (node == NULL_TREE || node == error_mark_node)) | |
7625 | return true; | |
7626 | ||
7627 | if (cxx_fundamental_alignment_p (requested_alignment)) | |
7628 | return true; | |
7629 | ||
7630 | if (DECL_P (node)) | |
7631 | { | |
7632 | if (TREE_STATIC (node)) | |
7633 | { | |
7634 | /* For file scope variables and static members, the target | |
7635 | supports alignments that are at most | |
7636 | MAX_OFILE_ALIGNMENT. */ | |
7637 | if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT)) | |
7638 | alignment_too_large_p = true; | |
7639 | } | |
7640 | else | |
7641 | { | |
7642 | #ifdef BIGGEST_FIELD_ALIGNMENT | |
7643 | #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT | |
7644 | #else | |
7645 | #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT | |
7646 | #endif | |
7647 | /* For non-static members, the target supports either | |
7648 | alignments that at most either BIGGEST_FIELD_ALIGNMENT | |
7649 | if it is defined or BIGGEST_ALIGNMENT. */ | |
7650 | max_align = MAX_TARGET_FIELD_ALIGNMENT; | |
7651 | if (TREE_CODE (node) == FIELD_DECL | |
7652 | && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT)) | |
7653 | alignment_too_large_p = true; | |
7654 | #undef MAX_TARGET_FIELD_ALIGNMENT | |
7655 | /* For stack variables, the target supports at most | |
7656 | MAX_STACK_ALIGNMENT. */ | |
7657 | else if (decl_function_context (node) != NULL | |
7658 | && requested_alignment > (max_align = MAX_STACK_ALIGNMENT)) | |
7659 | alignment_too_large_p = true; | |
7660 | } | |
7661 | } | |
7662 | else if (TYPE_P (node)) | |
7663 | { | |
7664 | /* Let's be liberal for types. */ | |
7665 | if (requested_alignment > (max_align = BIGGEST_ALIGNMENT)) | |
7666 | alignment_too_large_p = true; | |
7667 | } | |
7668 | ||
7669 | if (alignment_too_large_p) | |
7670 | pedwarn (input_location, OPT_Wattributes, | |
7671 | "requested alignment %d is larger than %d", | |
7672 | requested_alignment, max_align); | |
7673 | ||
7674 | return !alignment_too_large_p; | |
7675 | } | |
7676 | ||
349ae713 NB |
7677 | /* Handle a "aligned" attribute; arguments as in |
7678 | struct attribute_spec.handler. */ | |
7679 | ||
7680 | static tree | |
e18476eb | 7681 | handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
a742c759 | 7682 | int flags, bool *no_add_attrs) |
349ae713 NB |
7683 | { |
7684 | tree decl = NULL_TREE; | |
7685 | tree *type = NULL; | |
7686 | int is_type = 0; | |
5d77fb19 | 7687 | tree align_expr; |
349ae713 NB |
7688 | int i; |
7689 | ||
5d77fb19 MG |
7690 | if (args) |
7691 | { | |
7692 | align_expr = TREE_VALUE (args); | |
661a0813 MP |
7693 | if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE |
7694 | && TREE_CODE (align_expr) != FUNCTION_DECL) | |
5d77fb19 MG |
7695 | align_expr = default_conversion (align_expr); |
7696 | } | |
7697 | else | |
7698 | align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT); | |
7699 | ||
349ae713 NB |
7700 | if (DECL_P (*node)) |
7701 | { | |
7702 | decl = *node; | |
7703 | type = &TREE_TYPE (decl); | |
7704 | is_type = TREE_CODE (*node) == TYPE_DECL; | |
7705 | } | |
7706 | else if (TYPE_P (*node)) | |
7707 | type = node, is_type = 1; | |
7708 | ||
e28d52cf DS |
7709 | if ((i = check_user_alignment (align_expr, false)) == -1 |
7710 | || !check_cxx_fundamental_alignment_constraints (*node, i, flags)) | |
d19fa6b5 | 7711 | *no_add_attrs = true; |
349ae713 NB |
7712 | else if (is_type) |
7713 | { | |
0f559c16 JM |
7714 | if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
7715 | /* OK, modify the type in place. */; | |
349ae713 NB |
7716 | /* If we have a TYPE_DECL, then copy the type, so that we |
7717 | don't accidentally modify a builtin type. See pushdecl. */ | |
0f559c16 JM |
7718 | else if (decl && TREE_TYPE (decl) != error_mark_node |
7719 | && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) | |
349ae713 NB |
7720 | { |
7721 | tree tt = TREE_TYPE (decl); | |
8dd16ecc | 7722 | *type = build_variant_type_copy (*type); |
349ae713 NB |
7723 | DECL_ORIGINAL_TYPE (decl) = tt; |
7724 | TYPE_NAME (*type) = decl; | |
7725 | TREE_USED (*type) = TREE_USED (decl); | |
7726 | TREE_TYPE (decl) = *type; | |
7727 | } | |
0f559c16 | 7728 | else |
8dd16ecc | 7729 | *type = build_variant_type_copy (*type); |
349ae713 | 7730 | |
d9223014 | 7731 | TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT; |
349ae713 NB |
7732 | TYPE_USER_ALIGN (*type) = 1; |
7733 | } | |
837edd5f | 7734 | else if (! VAR_OR_FUNCTION_DECL_P (decl) |
349ae713 NB |
7735 | && TREE_CODE (decl) != FIELD_DECL) |
7736 | { | |
dee15844 | 7737 | error ("alignment may not be specified for %q+D", decl); |
349ae713 NB |
7738 | *no_add_attrs = true; |
7739 | } | |
e28d52cf DS |
7740 | else if (DECL_USER_ALIGN (decl) |
7741 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) | |
7742 | /* C++-11 [dcl.align/4]: | |
7743 | ||
7744 | When multiple alignment-specifiers are specified for an | |
7745 | entity, the alignment requirement shall be set to the | |
7746 | strictest specified alignment. | |
7747 | ||
7748 | This formally comes from the c++11 specification but we are | |
7749 | doing it for the GNU attribute syntax as well. */ | |
7750 | *no_add_attrs = true; | |
837edd5f | 7751 | else if (TREE_CODE (decl) == FUNCTION_DECL |
d9223014 | 7752 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) |
837edd5f GK |
7753 | { |
7754 | if (DECL_USER_ALIGN (decl)) | |
7755 | error ("alignment for %q+D was previously specified as %d " | |
7756 | "and may not be decreased", decl, | |
7757 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
7758 | else | |
7759 | error ("alignment for %q+D must be at least %d", decl, | |
7760 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
7761 | *no_add_attrs = true; | |
7762 | } | |
349ae713 NB |
7763 | else |
7764 | { | |
d9223014 | 7765 | DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT; |
349ae713 NB |
7766 | DECL_USER_ALIGN (decl) = 1; |
7767 | } | |
7768 | ||
7769 | return NULL_TREE; | |
7770 | } | |
7771 | ||
7772 | /* Handle a "weak" attribute; arguments as in | |
7773 | struct attribute_spec.handler. */ | |
7774 | ||
7775 | static tree | |
55af93a8 | 7776 | handle_weak_attribute (tree *node, tree name, |
e18476eb BI |
7777 | tree ARG_UNUSED (args), |
7778 | int ARG_UNUSED (flags), | |
7779 | bool * ARG_UNUSED (no_add_attrs)) | |
349ae713 | 7780 | { |
55af93a8 | 7781 | if (TREE_CODE (*node) == FUNCTION_DECL |
6b4e94bc RG |
7782 | && DECL_DECLARED_INLINE_P (*node)) |
7783 | { | |
2aa9c6ae | 7784 | warning (OPT_Wattributes, "inline function %q+D declared weak", *node); |
6b4e94bc RG |
7785 | *no_add_attrs = true; |
7786 | } | |
ba885ec5 NS |
7787 | else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
7788 | { | |
7789 | error ("indirect function %q+D cannot be declared weak", *node); | |
7790 | *no_add_attrs = true; | |
7791 | return NULL_TREE; | |
7792 | } | |
6b4e94bc RG |
7793 | else if (TREE_CODE (*node) == FUNCTION_DECL |
7794 | || TREE_CODE (*node) == VAR_DECL) | |
3cb251b7 JH |
7795 | { |
7796 | struct symtab_node *n = symtab_node::get (*node); | |
7797 | if (n && n->refuse_visibility_changes) | |
7798 | error ("%+D declared weak after being used", *node); | |
7799 | declare_weak (*node); | |
7800 | } | |
55af93a8 DS |
7801 | else |
7802 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
349ae713 NB |
7803 | |
7804 | return NULL_TREE; | |
7805 | } | |
7806 | ||
ba885ec5 NS |
7807 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
7808 | struct attribute_spec.handler, except that IS_ALIAS tells us | |
7809 | whether this is an alias as opposed to ifunc attribute. */ | |
349ae713 NB |
7810 | |
7811 | static tree | |
ba885ec5 NS |
7812 | handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args, |
7813 | bool *no_add_attrs) | |
349ae713 NB |
7814 | { |
7815 | tree decl = *node; | |
7816 | ||
ba885ec5 NS |
7817 | if (TREE_CODE (decl) != FUNCTION_DECL |
7818 | && (!is_alias || TREE_CODE (decl) != VAR_DECL)) | |
feab5a67 JM |
7819 | { |
7820 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7821 | *no_add_attrs = true; | |
7822 | } | |
7823 | else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) | |
b8698a0f | 7824 | || (TREE_CODE (decl) != FUNCTION_DECL |
a9b0b825 GK |
7825 | && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) |
7826 | /* A static variable declaration is always a tentative definition, | |
7827 | but the alias is a non-tentative definition which overrides. */ | |
b8698a0f | 7828 | || (TREE_CODE (decl) != FUNCTION_DECL |
a9b0b825 | 7829 | && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl))) |
349ae713 | 7830 | { |
ba885ec5 | 7831 | error ("%q+D defined both normally and as %qE attribute", decl, name); |
349ae713 | 7832 | *no_add_attrs = true; |
ba885ec5 | 7833 | return NULL_TREE; |
349ae713 | 7834 | } |
ba885ec5 NS |
7835 | else if (!is_alias |
7836 | && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl)) | |
7837 | || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))) | |
7838 | { | |
7839 | error ("weak %q+D cannot be defined %qE", decl, name); | |
7840 | *no_add_attrs = true; | |
7841 | return NULL_TREE; | |
7842 | } | |
f6a76b9f RH |
7843 | |
7844 | /* Note that the very first time we process a nested declaration, | |
7845 | decl_function_context will not be set. Indeed, *would* never | |
7846 | be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that | |
7847 | we do below. After such frobbery, pushdecl would set the context. | |
7848 | In any case, this is never what we want. */ | |
7849 | else if (decl_function_context (decl) == 0 && current_function_decl == NULL) | |
349ae713 NB |
7850 | { |
7851 | tree id; | |
7852 | ||
7853 | id = TREE_VALUE (args); | |
7854 | if (TREE_CODE (id) != STRING_CST) | |
7855 | { | |
ba885ec5 | 7856 | error ("attribute %qE argument not a string", name); |
349ae713 NB |
7857 | *no_add_attrs = true; |
7858 | return NULL_TREE; | |
7859 | } | |
7860 | id = get_identifier (TREE_STRING_POINTER (id)); | |
7861 | /* This counts as a use of the object pointed to. */ | |
7862 | TREE_USED (id) = 1; | |
7863 | ||
7864 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
7865 | DECL_INITIAL (decl) = error_mark_node; | |
7866 | else | |
08346abd | 7867 | TREE_STATIC (decl) = 1; |
ba885ec5 NS |
7868 | |
7869 | if (!is_alias) | |
7870 | /* ifuncs are also aliases, so set that attribute too. */ | |
7871 | DECL_ATTRIBUTES (decl) | |
7872 | = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl)); | |
349ae713 NB |
7873 | } |
7874 | else | |
7875 | { | |
5c498b10 | 7876 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
7877 | *no_add_attrs = true; |
7878 | } | |
7879 | ||
f7217cde JH |
7880 | if (decl_in_symtab_p (*node)) |
7881 | { | |
7882 | struct symtab_node *n = symtab_node::get (decl); | |
7883 | if (n && n->refuse_visibility_changes) | |
7884 | { | |
7885 | if (is_alias) | |
7886 | error ("%+D declared alias after being used", decl); | |
7887 | else | |
7888 | error ("%+D declared ifunc after being used", decl); | |
7889 | } | |
7890 | } | |
7891 | ||
7892 | ||
349ae713 NB |
7893 | return NULL_TREE; |
7894 | } | |
7895 | ||
ba885ec5 NS |
7896 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
7897 | struct attribute_spec.handler. */ | |
7898 | ||
7899 | static tree | |
7900 | handle_ifunc_attribute (tree *node, tree name, tree args, | |
7901 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7902 | { | |
7903 | return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs); | |
7904 | } | |
7905 | ||
7906 | /* Handle an "alias" or "ifunc" attribute; arguments as in | |
7907 | struct attribute_spec.handler. */ | |
7908 | ||
7909 | static tree | |
7910 | handle_alias_attribute (tree *node, tree name, tree args, | |
7911 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7912 | { | |
7913 | return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs); | |
7914 | } | |
7915 | ||
a0203ca7 AO |
7916 | /* Handle a "weakref" attribute; arguments as in struct |
7917 | attribute_spec.handler. */ | |
7918 | ||
7919 | static tree | |
7920 | handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
7921 | int flags, bool *no_add_attrs) | |
7922 | { | |
7923 | tree attr = NULL_TREE; | |
7924 | ||
e1cf56b1 AO |
7925 | /* We must ignore the attribute when it is associated with |
7926 | local-scoped decls, since attribute alias is ignored and many | |
7927 | such symbols do not even have a DECL_WEAK field. */ | |
e7b012c0 JJ |
7928 | if (decl_function_context (*node) |
7929 | || current_function_decl | |
7930 | || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL)) | |
e1cf56b1 AO |
7931 | { |
7932 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7933 | *no_add_attrs = true; | |
7934 | return NULL_TREE; | |
7935 | } | |
7936 | ||
ba885ec5 NS |
7937 | if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
7938 | { | |
7939 | error ("indirect function %q+D cannot be declared weakref", *node); | |
7940 | *no_add_attrs = true; | |
7941 | return NULL_TREE; | |
7942 | } | |
7943 | ||
a0203ca7 AO |
7944 | /* The idea here is that `weakref("name")' mutates into `weakref, |
7945 | alias("name")', and weakref without arguments, in turn, | |
7946 | implicitly adds weak. */ | |
7947 | ||
7948 | if (args) | |
7949 | { | |
7950 | attr = tree_cons (get_identifier ("alias"), args, attr); | |
7951 | attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr); | |
7952 | ||
7953 | *no_add_attrs = true; | |
a9b0b825 GK |
7954 | |
7955 | decl_attributes (node, attr, flags); | |
a0203ca7 AO |
7956 | } |
7957 | else | |
7958 | { | |
7959 | if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node))) | |
c5d75364 MLI |
7960 | error_at (DECL_SOURCE_LOCATION (*node), |
7961 | "weakref attribute must appear before alias attribute"); | |
a0203ca7 | 7962 | |
a9b0b825 GK |
7963 | /* Can't call declare_weak because it wants this to be TREE_PUBLIC, |
7964 | and that isn't supported; and because it wants to add it to | |
7965 | the list of weak decls, which isn't helpful. */ | |
7966 | DECL_WEAK (*node) = 1; | |
a0203ca7 AO |
7967 | } |
7968 | ||
f7217cde JH |
7969 | if (decl_in_symtab_p (*node)) |
7970 | { | |
7971 | struct symtab_node *n = symtab_node::get (*node); | |
7972 | if (n && n->refuse_visibility_changes) | |
7973 | error ("%+D declared weakref after being used", *node); | |
7974 | } | |
7975 | ||
a0203ca7 AO |
7976 | return NULL_TREE; |
7977 | } | |
7978 | ||
349ae713 NB |
7979 | /* Handle an "visibility" attribute; arguments as in |
7980 | struct attribute_spec.handler. */ | |
7981 | ||
7982 | static tree | |
35b1a6fa | 7983 | handle_visibility_attribute (tree *node, tree name, tree args, |
e18476eb | 7984 | int ARG_UNUSED (flags), |
b9e75696 | 7985 | bool *ARG_UNUSED (no_add_attrs)) |
349ae713 NB |
7986 | { |
7987 | tree decl = *node; | |
968b41a1 | 7988 | tree id = TREE_VALUE (args); |
b9e75696 | 7989 | enum symbol_visibility vis; |
349ae713 | 7990 | |
d7afec4b ND |
7991 | if (TYPE_P (*node)) |
7992 | { | |
b9e75696 JM |
7993 | if (TREE_CODE (*node) == ENUMERAL_TYPE) |
7994 | /* OK */; | |
7995 | else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE) | |
7996 | { | |
7997 | warning (OPT_Wattributes, "%qE attribute ignored on non-class types", | |
7998 | name); | |
7999 | return NULL_TREE; | |
8000 | } | |
8001 | else if (TYPE_FIELDS (*node)) | |
8002 | { | |
8003 | error ("%qE attribute ignored because %qT is already defined", | |
8004 | name, *node); | |
8005 | return NULL_TREE; | |
8006 | } | |
d7afec4b | 8007 | } |
3f75a254 | 8008 | else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl)) |
349ae713 | 8009 | { |
5c498b10 | 8010 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
968b41a1 | 8011 | return NULL_TREE; |
349ae713 | 8012 | } |
349ae713 | 8013 | |
968b41a1 MA |
8014 | if (TREE_CODE (id) != STRING_CST) |
8015 | { | |
40b97a2e | 8016 | error ("visibility argument not a string"); |
968b41a1 | 8017 | return NULL_TREE; |
349ae713 | 8018 | } |
9f63daea | 8019 | |
d7afec4b ND |
8020 | /* If this is a type, set the visibility on the type decl. */ |
8021 | if (TYPE_P (decl)) | |
8022 | { | |
8023 | decl = TYPE_NAME (decl); | |
3f75a254 | 8024 | if (!decl) |
c22cacf3 | 8025 | return NULL_TREE; |
e8233ac2 AP |
8026 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
8027 | { | |
5c498b10 | 8028 | warning (OPT_Wattributes, "%qE attribute ignored on types", |
e8233ac2 AP |
8029 | name); |
8030 | return NULL_TREE; | |
8031 | } | |
d7afec4b | 8032 | } |
349ae713 | 8033 | |
968b41a1 | 8034 | if (strcmp (TREE_STRING_POINTER (id), "default") == 0) |
b9e75696 | 8035 | vis = VISIBILITY_DEFAULT; |
968b41a1 | 8036 | else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0) |
b9e75696 | 8037 | vis = VISIBILITY_INTERNAL; |
968b41a1 | 8038 | else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0) |
b9e75696 | 8039 | vis = VISIBILITY_HIDDEN; |
968b41a1 | 8040 | else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0) |
b9e75696 | 8041 | vis = VISIBILITY_PROTECTED; |
968b41a1 | 8042 | else |
b9e75696 JM |
8043 | { |
8044 | error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\""); | |
8045 | vis = VISIBILITY_DEFAULT; | |
8046 | } | |
8047 | ||
8048 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
3a687f8b MM |
8049 | && vis != DECL_VISIBILITY (decl)) |
8050 | { | |
8051 | tree attributes = (TYPE_P (*node) | |
8052 | ? TYPE_ATTRIBUTES (*node) | |
8053 | : DECL_ATTRIBUTES (decl)); | |
8054 | if (lookup_attribute ("visibility", attributes)) | |
8055 | error ("%qD redeclared with different visibility", decl); | |
8056 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
8057 | && lookup_attribute ("dllimport", attributes)) | |
8058 | error ("%qD was declared %qs which implies default visibility", | |
8059 | decl, "dllimport"); | |
8060 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
8061 | && lookup_attribute ("dllexport", attributes)) | |
8062 | error ("%qD was declared %qs which implies default visibility", | |
8063 | decl, "dllexport"); | |
8064 | } | |
b9e75696 JM |
8065 | |
8066 | DECL_VISIBILITY (decl) = vis; | |
d7afec4b ND |
8067 | DECL_VISIBILITY_SPECIFIED (decl) = 1; |
8068 | ||
b9e75696 JM |
8069 | /* Go ahead and attach the attribute to the node as well. This is needed |
8070 | so we can determine whether we have VISIBILITY_DEFAULT because the | |
8071 | visibility was not specified, or because it was explicitly overridden | |
8072 | from the containing scope. */ | |
968b41a1 | 8073 | |
349ae713 NB |
8074 | return NULL_TREE; |
8075 | } | |
8076 | ||
b2ca3702 MM |
8077 | /* Determine the ELF symbol visibility for DECL, which is either a |
8078 | variable or a function. It is an error to use this function if a | |
8079 | definition of DECL is not available in this translation unit. | |
8080 | Returns true if the final visibility has been determined by this | |
8081 | function; false if the caller is free to make additional | |
8082 | modifications. */ | |
8083 | ||
8084 | bool | |
8085 | c_determine_visibility (tree decl) | |
8086 | { | |
4094f4d2 NS |
8087 | gcc_assert (TREE_CODE (decl) == VAR_DECL |
8088 | || TREE_CODE (decl) == FUNCTION_DECL); | |
b2ca3702 MM |
8089 | |
8090 | /* If the user explicitly specified the visibility with an | |
8091 | attribute, honor that. DECL_VISIBILITY will have been set during | |
6d87092d JM |
8092 | the processing of the attribute. We check for an explicit |
8093 | attribute, rather than just checking DECL_VISIBILITY_SPECIFIED, | |
8094 | to distinguish the use of an attribute from the use of a "#pragma | |
8095 | GCC visibility push(...)"; in the latter case we still want other | |
8096 | considerations to be able to overrule the #pragma. */ | |
8097 | if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)) | |
8098 | || (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
8099 | && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) | |
8100 | || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))))) | |
b2ca3702 MM |
8101 | return true; |
8102 | ||
b9e75696 JM |
8103 | /* Set default visibility to whatever the user supplied with |
8104 | visibility_specified depending on #pragma GCC visibility. */ | |
8105 | if (!DECL_VISIBILITY_SPECIFIED (decl)) | |
8106 | { | |
09812622 JJ |
8107 | if (visibility_options.inpragma |
8108 | || DECL_VISIBILITY (decl) != default_visibility) | |
8109 | { | |
8110 | DECL_VISIBILITY (decl) = default_visibility; | |
8111 | DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; | |
8112 | /* If visibility changed and DECL already has DECL_RTL, ensure | |
8113 | symbol flags are updated. */ | |
8114 | if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)) | |
8115 | || TREE_CODE (decl) == FUNCTION_DECL) | |
8116 | && DECL_RTL_SET_P (decl)) | |
8117 | make_decl_rtl (decl); | |
8118 | } | |
b9e75696 | 8119 | } |
b2ca3702 MM |
8120 | return false; |
8121 | } | |
8122 | ||
dce81a1a JJ |
8123 | /* Handle an "tls_model" attribute; arguments as in |
8124 | struct attribute_spec.handler. */ | |
8125 | ||
8126 | static tree | |
35b1a6fa | 8127 | handle_tls_model_attribute (tree *node, tree name, tree args, |
e18476eb | 8128 | int ARG_UNUSED (flags), bool *no_add_attrs) |
dce81a1a | 8129 | { |
c2f7fa15 | 8130 | tree id; |
dce81a1a | 8131 | tree decl = *node; |
c2f7fa15 | 8132 | enum tls_model kind; |
dce81a1a | 8133 | |
c2f7fa15 SB |
8134 | *no_add_attrs = true; |
8135 | ||
5006381c | 8136 | if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl)) |
dce81a1a | 8137 | { |
5c498b10 | 8138 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
c2f7fa15 | 8139 | return NULL_TREE; |
dce81a1a | 8140 | } |
dce81a1a | 8141 | |
c2f7fa15 SB |
8142 | kind = DECL_TLS_MODEL (decl); |
8143 | id = TREE_VALUE (args); | |
8144 | if (TREE_CODE (id) != STRING_CST) | |
8145 | { | |
8146 | error ("tls_model argument not a string"); | |
8147 | return NULL_TREE; | |
dce81a1a JJ |
8148 | } |
8149 | ||
c2f7fa15 SB |
8150 | if (!strcmp (TREE_STRING_POINTER (id), "local-exec")) |
8151 | kind = TLS_MODEL_LOCAL_EXEC; | |
8152 | else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec")) | |
8153 | kind = TLS_MODEL_INITIAL_EXEC; | |
8154 | else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic")) | |
8155 | kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC; | |
8156 | else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic")) | |
8157 | kind = TLS_MODEL_GLOBAL_DYNAMIC; | |
8158 | else | |
8159 | error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\""); | |
8160 | ||
56363ffd | 8161 | set_decl_tls_model (decl, kind); |
dce81a1a JJ |
8162 | return NULL_TREE; |
8163 | } | |
8164 | ||
349ae713 NB |
8165 | /* Handle a "no_instrument_function" attribute; arguments as in |
8166 | struct attribute_spec.handler. */ | |
8167 | ||
8168 | static tree | |
35b1a6fa | 8169 | handle_no_instrument_function_attribute (tree *node, tree name, |
e18476eb BI |
8170 | tree ARG_UNUSED (args), |
8171 | int ARG_UNUSED (flags), | |
a742c759 | 8172 | bool *no_add_attrs) |
349ae713 NB |
8173 | { |
8174 | tree decl = *node; | |
8175 | ||
8176 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8177 | { | |
c5d75364 MLI |
8178 | error_at (DECL_SOURCE_LOCATION (decl), |
8179 | "%qE attribute applies only to functions", name); | |
349ae713 NB |
8180 | *no_add_attrs = true; |
8181 | } | |
349ae713 NB |
8182 | else |
8183 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
8184 | ||
8185 | return NULL_TREE; | |
8186 | } | |
8187 | ||
8188 | /* Handle a "malloc" attribute; arguments as in | |
8189 | struct attribute_spec.handler. */ | |
8190 | ||
8191 | static tree | |
e18476eb BI |
8192 | handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8193 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
349ae713 | 8194 | { |
3425638a JM |
8195 | if (TREE_CODE (*node) == FUNCTION_DECL |
8196 | && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) | |
349ae713 | 8197 | DECL_IS_MALLOC (*node) = 1; |
349ae713 NB |
8198 | else |
8199 | { | |
5c498b10 | 8200 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
8201 | *no_add_attrs = true; |
8202 | } | |
8203 | ||
8204 | return NULL_TREE; | |
8205 | } | |
8206 | ||
51bc54a6 DM |
8207 | /* Handle a "alloc_size" attribute; arguments as in |
8208 | struct attribute_spec.handler. */ | |
8209 | ||
8210 | static tree | |
8211 | handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
8212 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8213 | { | |
f3f75f69 | 8214 | unsigned arg_count = type_num_arguments (*node); |
51bc54a6 DM |
8215 | for (; args; args = TREE_CHAIN (args)) |
8216 | { | |
8217 | tree position = TREE_VALUE (args); | |
5d77fb19 MG |
8218 | if (position && TREE_CODE (position) != IDENTIFIER_NODE |
8219 | && TREE_CODE (position) != FUNCTION_DECL) | |
8220 | position = default_conversion (position); | |
51bc54a6 | 8221 | |
8fcbce72 JJ |
8222 | if (!tree_fits_uhwi_p (position) |
8223 | || !arg_count | |
8224 | || !IN_RANGE (tree_to_uhwi (position), 1, arg_count)) | |
51bc54a6 | 8225 | { |
b8698a0f | 8226 | warning (OPT_Wattributes, |
51bc54a6 DM |
8227 | "alloc_size parameter outside range"); |
8228 | *no_add_attrs = true; | |
8229 | return NULL_TREE; | |
8230 | } | |
8231 | } | |
8232 | return NULL_TREE; | |
8233 | } | |
8234 | ||
8fcbce72 JJ |
8235 | /* Handle a "alloc_align" attribute; arguments as in |
8236 | struct attribute_spec.handler. */ | |
8237 | ||
8238 | static tree | |
8239 | handle_alloc_align_attribute (tree *node, tree, tree args, int, | |
8240 | bool *no_add_attrs) | |
8241 | { | |
8242 | unsigned arg_count = type_num_arguments (*node); | |
8243 | tree position = TREE_VALUE (args); | |
8244 | if (position && TREE_CODE (position) != IDENTIFIER_NODE) | |
8245 | position = default_conversion (position); | |
8246 | ||
8247 | if (!tree_fits_uhwi_p (position) | |
8248 | || !arg_count | |
8249 | || !IN_RANGE (tree_to_uhwi (position), 1, arg_count)) | |
8250 | { | |
8251 | warning (OPT_Wattributes, | |
8252 | "alloc_align parameter outside range"); | |
8253 | *no_add_attrs = true; | |
8254 | return NULL_TREE; | |
8255 | } | |
8256 | return NULL_TREE; | |
8257 | } | |
8258 | ||
8259 | /* Handle a "assume_aligned" attribute; arguments as in | |
8260 | struct attribute_spec.handler. */ | |
8261 | ||
8262 | static tree | |
8263 | handle_assume_aligned_attribute (tree *, tree, tree args, int, | |
8264 | bool *no_add_attrs) | |
8265 | { | |
8266 | for (; args; args = TREE_CHAIN (args)) | |
8267 | { | |
8268 | tree position = TREE_VALUE (args); | |
8269 | if (position && TREE_CODE (position) != IDENTIFIER_NODE | |
8270 | && TREE_CODE (position) != FUNCTION_DECL) | |
8271 | position = default_conversion (position); | |
8272 | ||
8273 | if (TREE_CODE (position) != INTEGER_CST) | |
8274 | { | |
8275 | warning (OPT_Wattributes, | |
8276 | "assume_aligned parameter not integer constant"); | |
8277 | *no_add_attrs = true; | |
8278 | return NULL_TREE; | |
8279 | } | |
8280 | } | |
8281 | return NULL_TREE; | |
8282 | } | |
8283 | ||
0b7b376d RG |
8284 | /* Handle a "fn spec" attribute; arguments as in |
8285 | struct attribute_spec.handler. */ | |
8286 | ||
8287 | static tree | |
8288 | handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name), | |
8289 | tree args, int ARG_UNUSED (flags), | |
8290 | bool *no_add_attrs ATTRIBUTE_UNUSED) | |
8291 | { | |
8292 | gcc_assert (args | |
8293 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST | |
8294 | && !TREE_CHAIN (args)); | |
8295 | return NULL_TREE; | |
8296 | } | |
8297 | ||
d5e254e1 IE |
8298 | /* Handle a "bnd_variable_size" attribute; arguments as in |
8299 | struct attribute_spec.handler. */ | |
8300 | ||
8301 | static tree | |
8302 | handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
8303 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8304 | { | |
8305 | if (TREE_CODE (*node) != FIELD_DECL) | |
8306 | { | |
8307 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8308 | *no_add_attrs = true; | |
8309 | } | |
8310 | ||
8311 | return NULL_TREE; | |
8312 | } | |
8313 | ||
8314 | /* Handle a "bnd_legacy" attribute; arguments as in | |
8315 | struct attribute_spec.handler. */ | |
8316 | ||
8317 | static tree | |
8318 | handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args), | |
8319 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8320 | { | |
8321 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
8322 | { | |
8323 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8324 | *no_add_attrs = true; | |
8325 | } | |
8326 | ||
8327 | return NULL_TREE; | |
8328 | } | |
8329 | ||
8330 | /* Handle a "bnd_instrument" attribute; arguments as in | |
8331 | struct attribute_spec.handler. */ | |
8332 | ||
8333 | static tree | |
8334 | handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args), | |
8335 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8336 | { | |
8337 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
8338 | { | |
8339 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8340 | *no_add_attrs = true; | |
8341 | } | |
8342 | ||
8343 | return NULL_TREE; | |
8344 | } | |
8345 | ||
2a99e5e6 LL |
8346 | /* Handle a "warn_unused" attribute; arguments as in |
8347 | struct attribute_spec.handler. */ | |
8348 | ||
8349 | static tree | |
8350 | handle_warn_unused_attribute (tree *node, tree name, | |
8351 | tree args ATTRIBUTE_UNUSED, | |
8352 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
8353 | { | |
8354 | if (TYPE_P (*node)) | |
8355 | /* Do nothing else, just set the attribute. We'll get at | |
8356 | it later with lookup_attribute. */ | |
8357 | ; | |
8358 | else | |
8359 | { | |
8360 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8361 | *no_add_attrs = true; | |
8362 | } | |
8363 | ||
8364 | return NULL_TREE; | |
8365 | } | |
8366 | ||
acf0174b JJ |
8367 | /* Handle an "omp declare simd" attribute; arguments as in |
8368 | struct attribute_spec.handler. */ | |
8369 | ||
8370 | static tree | |
8371 | handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *) | |
8372 | { | |
8373 | return NULL_TREE; | |
8374 | } | |
8375 | ||
8376 | /* Handle an "omp declare target" attribute; arguments as in | |
8377 | struct attribute_spec.handler. */ | |
8378 | ||
8379 | static tree | |
8380 | handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *) | |
8381 | { | |
8382 | return NULL_TREE; | |
8383 | } | |
8384 | ||
6e9a3221 AN |
8385 | /* Handle a "returns_twice" attribute; arguments as in |
8386 | struct attribute_spec.handler. */ | |
8387 | ||
8388 | static tree | |
8389 | handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
8390 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8391 | { | |
8392 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
8393 | DECL_IS_RETURNS_TWICE (*node) = 1; | |
8394 | else | |
8395 | { | |
5c498b10 | 8396 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
6e9a3221 AN |
8397 | *no_add_attrs = true; |
8398 | } | |
8399 | ||
8400 | return NULL_TREE; | |
8401 | } | |
8402 | ||
349ae713 NB |
8403 | /* Handle a "no_limit_stack" attribute; arguments as in |
8404 | struct attribute_spec.handler. */ | |
8405 | ||
8406 | static tree | |
35b1a6fa | 8407 | handle_no_limit_stack_attribute (tree *node, tree name, |
e18476eb BI |
8408 | tree ARG_UNUSED (args), |
8409 | int ARG_UNUSED (flags), | |
a742c759 | 8410 | bool *no_add_attrs) |
349ae713 NB |
8411 | { |
8412 | tree decl = *node; | |
8413 | ||
8414 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8415 | { | |
c5d75364 MLI |
8416 | error_at (DECL_SOURCE_LOCATION (decl), |
8417 | "%qE attribute applies only to functions", name); | |
349ae713 NB |
8418 | *no_add_attrs = true; |
8419 | } | |
8420 | else if (DECL_INITIAL (decl)) | |
8421 | { | |
c5d75364 MLI |
8422 | error_at (DECL_SOURCE_LOCATION (decl), |
8423 | "can%'t set %qE attribute after definition", name); | |
349ae713 NB |
8424 | *no_add_attrs = true; |
8425 | } | |
8426 | else | |
8427 | DECL_NO_LIMIT_STACK (decl) = 1; | |
8428 | ||
8429 | return NULL_TREE; | |
8430 | } | |
8431 | ||
8432 | /* Handle a "pure" attribute; arguments as in | |
8433 | struct attribute_spec.handler. */ | |
8434 | ||
8435 | static tree | |
e18476eb BI |
8436 | handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8437 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
349ae713 NB |
8438 | { |
8439 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
becfd6e5 | 8440 | DECL_PURE_P (*node) = 1; |
349ae713 NB |
8441 | /* ??? TODO: Support types. */ |
8442 | else | |
8443 | { | |
5c498b10 | 8444 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
8445 | *no_add_attrs = true; |
8446 | } | |
8447 | ||
8448 | return NULL_TREE; | |
8449 | } | |
8450 | ||
0a35513e AH |
8451 | /* Digest an attribute list destined for a transactional memory statement. |
8452 | ALLOWED is the set of attributes that are allowed for this statement; | |
8453 | return the attribute we parsed. Multiple attributes are never allowed. */ | |
8454 | ||
8455 | int | |
8456 | parse_tm_stmt_attr (tree attrs, int allowed) | |
8457 | { | |
8458 | tree a_seen = NULL; | |
8459 | int m_seen = 0; | |
8460 | ||
8461 | for ( ; attrs ; attrs = TREE_CHAIN (attrs)) | |
8462 | { | |
8463 | tree a = TREE_PURPOSE (attrs); | |
8464 | int m = 0; | |
8465 | ||
8466 | if (is_attribute_p ("outer", a)) | |
8467 | m = TM_STMT_ATTR_OUTER; | |
8468 | ||
8469 | if ((m & allowed) == 0) | |
8470 | { | |
8471 | warning (OPT_Wattributes, "%qE attribute directive ignored", a); | |
8472 | continue; | |
8473 | } | |
8474 | ||
8475 | if (m_seen == 0) | |
8476 | { | |
8477 | a_seen = a; | |
8478 | m_seen = m; | |
8479 | } | |
8480 | else if (m_seen == m) | |
8481 | warning (OPT_Wattributes, "%qE attribute duplicated", a); | |
8482 | else | |
8483 | warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen); | |
8484 | } | |
8485 | ||
8486 | return m_seen; | |
8487 | } | |
8488 | ||
8489 | /* Transform a TM attribute name into a maskable integer and back. | |
8490 | Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding | |
8491 | to how the lack of an attribute is treated. */ | |
8492 | ||
8493 | int | |
8494 | tm_attr_to_mask (tree attr) | |
8495 | { | |
8496 | if (attr == NULL) | |
8497 | return 0; | |
8498 | if (is_attribute_p ("transaction_safe", attr)) | |
8499 | return TM_ATTR_SAFE; | |
8500 | if (is_attribute_p ("transaction_callable", attr)) | |
8501 | return TM_ATTR_CALLABLE; | |
8502 | if (is_attribute_p ("transaction_pure", attr)) | |
8503 | return TM_ATTR_PURE; | |
8504 | if (is_attribute_p ("transaction_unsafe", attr)) | |
8505 | return TM_ATTR_IRREVOCABLE; | |
8506 | if (is_attribute_p ("transaction_may_cancel_outer", attr)) | |
8507 | return TM_ATTR_MAY_CANCEL_OUTER; | |
8508 | return 0; | |
8509 | } | |
8510 | ||
8511 | tree | |
8512 | tm_mask_to_attr (int mask) | |
8513 | { | |
8514 | const char *str; | |
8515 | switch (mask) | |
8516 | { | |
8517 | case TM_ATTR_SAFE: | |
8518 | str = "transaction_safe"; | |
8519 | break; | |
8520 | case TM_ATTR_CALLABLE: | |
8521 | str = "transaction_callable"; | |
8522 | break; | |
8523 | case TM_ATTR_PURE: | |
8524 | str = "transaction_pure"; | |
8525 | break; | |
8526 | case TM_ATTR_IRREVOCABLE: | |
8527 | str = "transaction_unsafe"; | |
8528 | break; | |
8529 | case TM_ATTR_MAY_CANCEL_OUTER: | |
8530 | str = "transaction_may_cancel_outer"; | |
8531 | break; | |
8532 | default: | |
8533 | gcc_unreachable (); | |
8534 | } | |
8535 | return get_identifier (str); | |
8536 | } | |
8537 | ||
8538 | /* Return the first TM attribute seen in LIST. */ | |
8539 | ||
8540 | tree | |
8541 | find_tm_attribute (tree list) | |
8542 | { | |
8543 | for (; list ; list = TREE_CHAIN (list)) | |
8544 | { | |
8545 | tree name = TREE_PURPOSE (list); | |
8546 | if (tm_attr_to_mask (name) != 0) | |
8547 | return name; | |
8548 | } | |
8549 | return NULL_TREE; | |
8550 | } | |
8551 | ||
8552 | /* Handle the TM attributes; arguments as in struct attribute_spec.handler. | |
8553 | Here we accept only function types, and verify that none of the other | |
8554 | function TM attributes are also applied. */ | |
8555 | /* ??? We need to accept class types for C++, but not C. This greatly | |
8556 | complicates this function, since we can no longer rely on the extra | |
8557 | processing given by function_type_required. */ | |
8558 | ||
8559 | static tree | |
8560 | handle_tm_attribute (tree *node, tree name, tree args, | |
8561 | int flags, bool *no_add_attrs) | |
8562 | { | |
8563 | /* Only one path adds the attribute; others don't. */ | |
8564 | *no_add_attrs = true; | |
8565 | ||
8566 | switch (TREE_CODE (*node)) | |
8567 | { | |
8568 | case RECORD_TYPE: | |
8569 | case UNION_TYPE: | |
8570 | /* Only tm_callable and tm_safe apply to classes. */ | |
8571 | if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE)) | |
8572 | goto ignored; | |
8573 | /* FALLTHRU */ | |
8574 | ||
8575 | case FUNCTION_TYPE: | |
8576 | case METHOD_TYPE: | |
8577 | { | |
8578 | tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node)); | |
8579 | if (old_name == name) | |
8580 | ; | |
8581 | else if (old_name != NULL_TREE) | |
8582 | error ("type was previously declared %qE", old_name); | |
8583 | else | |
8584 | *no_add_attrs = false; | |
8585 | } | |
8586 | break; | |
8587 | ||
8588 | case POINTER_TYPE: | |
8589 | { | |
8590 | enum tree_code subcode = TREE_CODE (TREE_TYPE (*node)); | |
8591 | if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE) | |
8592 | { | |
8593 | tree fn_tmp = TREE_TYPE (*node); | |
8594 | decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0); | |
8595 | *node = build_pointer_type (fn_tmp); | |
8596 | break; | |
8597 | } | |
8598 | } | |
8599 | /* FALLTHRU */ | |
8600 | ||
8601 | default: | |
8602 | /* If a function is next, pass it on to be tried next. */ | |
8603 | if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) | |
8604 | return tree_cons (name, args, NULL); | |
8605 | ||
8606 | ignored: | |
8607 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8608 | break; | |
8609 | } | |
8610 | ||
8611 | return NULL_TREE; | |
8612 | } | |
8613 | ||
8614 | /* Handle the TM_WRAP attribute; arguments as in | |
8615 | struct attribute_spec.handler. */ | |
8616 | ||
8617 | static tree | |
8618 | handle_tm_wrap_attribute (tree *node, tree name, tree args, | |
8619 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8620 | { | |
8621 | tree decl = *node; | |
8622 | ||
8623 | /* We don't need the attribute even on success, since we | |
8624 | record the entry in an external table. */ | |
8625 | *no_add_attrs = true; | |
8626 | ||
8627 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8628 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8629 | else | |
8630 | { | |
8631 | tree wrap_decl = TREE_VALUE (args); | |
661a0813 MP |
8632 | if (error_operand_p (wrap_decl)) |
8633 | ; | |
8634 | else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE | |
8635 | && TREE_CODE (wrap_decl) != VAR_DECL | |
8636 | && TREE_CODE (wrap_decl) != FUNCTION_DECL) | |
0a35513e AH |
8637 | error ("%qE argument not an identifier", name); |
8638 | else | |
8639 | { | |
8640 | if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE) | |
8641 | wrap_decl = lookup_name (wrap_decl); | |
8642 | if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL) | |
8643 | { | |
8644 | if (lang_hooks.types_compatible_p (TREE_TYPE (decl), | |
8645 | TREE_TYPE (wrap_decl))) | |
8646 | record_tm_replacement (wrap_decl, decl); | |
8647 | else | |
8648 | error ("%qD is not compatible with %qD", wrap_decl, decl); | |
8649 | } | |
8650 | else | |
c07d7c02 | 8651 | error ("%qE argument is not a function", name); |
0a35513e AH |
8652 | } |
8653 | } | |
8654 | ||
8655 | return NULL_TREE; | |
8656 | } | |
8657 | ||
8658 | /* Ignore the given attribute. Used when this attribute may be usefully | |
8659 | overridden by the target, but is not used generically. */ | |
8660 | ||
8661 | static tree | |
8662 | ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name), | |
8663 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8664 | bool *no_add_attrs) | |
8665 | { | |
8666 | *no_add_attrs = true; | |
8667 | return NULL_TREE; | |
8668 | } | |
8669 | ||
dcd6de6d ZD |
8670 | /* Handle a "no vops" attribute; arguments as in |
8671 | struct attribute_spec.handler. */ | |
8672 | ||
8673 | static tree | |
8674 | handle_novops_attribute (tree *node, tree ARG_UNUSED (name), | |
8675 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8676 | bool *ARG_UNUSED (no_add_attrs)) | |
8677 | { | |
8678 | gcc_assert (TREE_CODE (*node) == FUNCTION_DECL); | |
8679 | DECL_IS_NOVOPS (*node) = 1; | |
8680 | return NULL_TREE; | |
8681 | } | |
8682 | ||
349ae713 NB |
8683 | /* Handle a "deprecated" attribute; arguments as in |
8684 | struct attribute_spec.handler. */ | |
35b1a6fa | 8685 | |
349ae713 | 8686 | static tree |
35b1a6fa | 8687 | handle_deprecated_attribute (tree *node, tree name, |
9b86d6bb | 8688 | tree args, int flags, |
a742c759 | 8689 | bool *no_add_attrs) |
349ae713 NB |
8690 | { |
8691 | tree type = NULL_TREE; | |
8692 | int warn = 0; | |
c51a1ba9 | 8693 | tree what = NULL_TREE; |
35b1a6fa | 8694 | |
9b86d6bb L |
8695 | if (!args) |
8696 | *no_add_attrs = true; | |
8697 | else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST) | |
8698 | { | |
8699 | error ("deprecated message is not a string"); | |
8700 | *no_add_attrs = true; | |
8701 | } | |
8702 | ||
349ae713 NB |
8703 | if (DECL_P (*node)) |
8704 | { | |
8705 | tree decl = *node; | |
8706 | type = TREE_TYPE (decl); | |
35b1a6fa | 8707 | |
349ae713 NB |
8708 | if (TREE_CODE (decl) == TYPE_DECL |
8709 | || TREE_CODE (decl) == PARM_DECL | |
8710 | || TREE_CODE (decl) == VAR_DECL | |
8711 | || TREE_CODE (decl) == FUNCTION_DECL | |
a1178b30 IS |
8712 | || TREE_CODE (decl) == FIELD_DECL |
8713 | || objc_method_decl (TREE_CODE (decl))) | |
349ae713 NB |
8714 | TREE_DEPRECATED (decl) = 1; |
8715 | else | |
8716 | warn = 1; | |
8717 | } | |
8718 | else if (TYPE_P (*node)) | |
8719 | { | |
8720 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
8dd16ecc | 8721 | *node = build_variant_type_copy (*node); |
349ae713 NB |
8722 | TREE_DEPRECATED (*node) = 1; |
8723 | type = *node; | |
8724 | } | |
8725 | else | |
8726 | warn = 1; | |
35b1a6fa | 8727 | |
349ae713 NB |
8728 | if (warn) |
8729 | { | |
8730 | *no_add_attrs = true; | |
8731 | if (type && TYPE_NAME (type)) | |
8732 | { | |
8733 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
c51a1ba9 | 8734 | what = TYPE_NAME (*node); |
349ae713 NB |
8735 | else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL |
8736 | && DECL_NAME (TYPE_NAME (type))) | |
c51a1ba9 | 8737 | what = DECL_NAME (TYPE_NAME (type)); |
349ae713 NB |
8738 | } |
8739 | if (what) | |
5c498b10 | 8740 | warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what); |
349ae713 | 8741 | else |
5c498b10 | 8742 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
8743 | } |
8744 | ||
8745 | return NULL_TREE; | |
8746 | } | |
8747 | ||
349ae713 NB |
8748 | /* Handle a "vector_size" attribute; arguments as in |
8749 | struct attribute_spec.handler. */ | |
8750 | ||
8751 | static tree | |
35b1a6fa | 8752 | handle_vector_size_attribute (tree *node, tree name, tree args, |
e18476eb | 8753 | int ARG_UNUSED (flags), |
a742c759 | 8754 | bool *no_add_attrs) |
349ae713 NB |
8755 | { |
8756 | unsigned HOST_WIDE_INT vecsize, nunits; | |
ef4bddc2 | 8757 | machine_mode orig_mode; |
4a5eab38 | 8758 | tree type = *node, new_type, size; |
349ae713 NB |
8759 | |
8760 | *no_add_attrs = true; | |
8761 | ||
4a5eab38 | 8762 | size = TREE_VALUE (args); |
661a0813 MP |
8763 | if (size && TREE_CODE (size) != IDENTIFIER_NODE |
8764 | && TREE_CODE (size) != FUNCTION_DECL) | |
5d77fb19 | 8765 | size = default_conversion (size); |
4a5eab38 | 8766 | |
cc269bb6 | 8767 | if (!tree_fits_uhwi_p (size)) |
349ae713 | 8768 | { |
5c498b10 | 8769 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
349ae713 NB |
8770 | return NULL_TREE; |
8771 | } | |
8772 | ||
8773 | /* Get the vector size (in bytes). */ | |
ae7e9ddd | 8774 | vecsize = tree_to_uhwi (size); |
349ae713 NB |
8775 | |
8776 | /* We need to provide for vector pointers, vector arrays, and | |
8777 | functions returning vectors. For example: | |
8778 | ||
8779 | __attribute__((vector_size(16))) short *foo; | |
8780 | ||
8781 | In this case, the mode is SI, but the type being modified is | |
8782 | HI, so we need to look further. */ | |
8783 | ||
8784 | while (POINTER_TYPE_P (type) | |
8785 | || TREE_CODE (type) == FUNCTION_TYPE | |
43dc123f | 8786 | || TREE_CODE (type) == METHOD_TYPE |
270e749d JJ |
8787 | || TREE_CODE (type) == ARRAY_TYPE |
8788 | || TREE_CODE (type) == OFFSET_TYPE) | |
349ae713 NB |
8789 | type = TREE_TYPE (type); |
8790 | ||
8791 | /* Get the mode of the type being modified. */ | |
8792 | orig_mode = TYPE_MODE (type); | |
8793 | ||
270e749d JJ |
8794 | if ((!INTEGRAL_TYPE_P (type) |
8795 | && !SCALAR_FLOAT_TYPE_P (type) | |
8796 | && !FIXED_POINT_TYPE_P (type)) | |
3d8bf70f | 8797 | || (!SCALAR_FLOAT_MODE_P (orig_mode) |
ab22c1fa CF |
8798 | && GET_MODE_CLASS (orig_mode) != MODE_INT |
8799 | && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode)) | |
cc269bb6 | 8800 | || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)) |
e4e5261f | 8801 | || TREE_CODE (type) == BOOLEAN_TYPE) |
349ae713 | 8802 | { |
c51a1ba9 | 8803 | error ("invalid vector type for attribute %qE", name); |
349ae713 NB |
8804 | return NULL_TREE; |
8805 | } | |
8806 | ||
ae7e9ddd | 8807 | if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type))) |
ee8960e5 JJ |
8808 | { |
8809 | error ("vector size not an integral multiple of component size"); | |
8810 | return NULL; | |
8811 | } | |
8812 | ||
8813 | if (vecsize == 0) | |
8814 | { | |
8815 | error ("zero vector size"); | |
8816 | return NULL; | |
8817 | } | |
8818 | ||
349ae713 | 8819 | /* Calculate how many units fit in the vector. */ |
ae7e9ddd | 8820 | nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
26277d41 | 8821 | if (nunits & (nunits - 1)) |
349ae713 | 8822 | { |
26277d41 | 8823 | error ("number of components of the vector not a power of two"); |
349ae713 NB |
8824 | return NULL_TREE; |
8825 | } | |
8826 | ||
26277d41 | 8827 | new_type = build_vector_type (type, nunits); |
349ae713 NB |
8828 | |
8829 | /* Build back pointers if needed. */ | |
5dc11954 | 8830 | *node = lang_hooks.types.reconstruct_complex_type (*node, new_type); |
349ae713 NB |
8831 | |
8832 | return NULL_TREE; | |
8833 | } | |
8834 | ||
b34c7881 JT |
8835 | /* Handle the "nonnull" attribute. */ |
8836 | static tree | |
e18476eb BI |
8837 | handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), |
8838 | tree args, int ARG_UNUSED (flags), | |
a742c759 | 8839 | bool *no_add_attrs) |
b34c7881 JT |
8840 | { |
8841 | tree type = *node; | |
8842 | unsigned HOST_WIDE_INT attr_arg_num; | |
8843 | ||
8844 | /* If no arguments are specified, all pointer arguments should be | |
95bd1dd7 | 8845 | non-null. Verify a full prototype is given so that the arguments |
b34c7881 | 8846 | will have the correct types when we actually check them later. */ |
3f75a254 | 8847 | if (!args) |
b34c7881 | 8848 | { |
f4da8dce | 8849 | if (!prototype_p (type)) |
b34c7881 JT |
8850 | { |
8851 | error ("nonnull attribute without arguments on a non-prototype"); | |
6de9cd9a | 8852 | *no_add_attrs = true; |
b34c7881 JT |
8853 | } |
8854 | return NULL_TREE; | |
8855 | } | |
8856 | ||
8857 | /* Argument list specified. Verify that each argument number references | |
8858 | a pointer argument. */ | |
5d77fb19 | 8859 | for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args)) |
b34c7881 | 8860 | { |
6de9cd9a | 8861 | unsigned HOST_WIDE_INT arg_num = 0, ck_num; |
b34c7881 | 8862 | |
5d77fb19 MG |
8863 | tree arg = TREE_VALUE (args); |
8864 | if (arg && TREE_CODE (arg) != IDENTIFIER_NODE | |
8865 | && TREE_CODE (arg) != FUNCTION_DECL) | |
8866 | arg = default_conversion (arg); | |
8867 | ||
8868 | if (!get_nonnull_operand (arg, &arg_num)) | |
b34c7881 | 8869 | { |
40b97a2e | 8870 | error ("nonnull argument has invalid operand number (argument %lu)", |
b34c7881 JT |
8871 | (unsigned long) attr_arg_num); |
8872 | *no_add_attrs = true; | |
8873 | return NULL_TREE; | |
8874 | } | |
8875 | ||
e19a18d4 | 8876 | if (prototype_p (type)) |
b34c7881 | 8877 | { |
e19a18d4 NF |
8878 | function_args_iterator iter; |
8879 | tree argument; | |
8880 | ||
8881 | function_args_iter_init (&iter, type); | |
8882 | for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter)) | |
b34c7881 | 8883 | { |
e19a18d4 NF |
8884 | argument = function_args_iter_cond (&iter); |
8885 | if (argument == NULL_TREE || ck_num == arg_num) | |
b34c7881 | 8886 | break; |
b34c7881 JT |
8887 | } |
8888 | ||
3f75a254 | 8889 | if (!argument |
e19a18d4 | 8890 | || TREE_CODE (argument) == VOID_TYPE) |
b34c7881 | 8891 | { |
40b97a2e | 8892 | error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)", |
b34c7881 JT |
8893 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
8894 | *no_add_attrs = true; | |
8895 | return NULL_TREE; | |
8896 | } | |
8897 | ||
e19a18d4 | 8898 | if (TREE_CODE (argument) != POINTER_TYPE) |
b34c7881 | 8899 | { |
40b97a2e | 8900 | error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)", |
b34c7881 JT |
8901 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
8902 | *no_add_attrs = true; | |
8903 | return NULL_TREE; | |
8904 | } | |
8905 | } | |
8906 | } | |
8907 | ||
8908 | return NULL_TREE; | |
8909 | } | |
8910 | ||
8911 | /* Check the argument list of a function call for null in argument slots | |
94a0dd7b SL |
8912 | that are marked as requiring a non-null pointer argument. The NARGS |
8913 | arguments are passed in the array ARGARRAY. | |
8914 | */ | |
b34c7881 JT |
8915 | |
8916 | static void | |
94a0dd7b | 8917 | check_function_nonnull (tree attrs, int nargs, tree *argarray) |
b34c7881 | 8918 | { |
332f1d24 | 8919 | tree a; |
94a0dd7b | 8920 | int i; |
b34c7881 | 8921 | |
332f1d24 JJ |
8922 | attrs = lookup_attribute ("nonnull", attrs); |
8923 | if (attrs == NULL_TREE) | |
8924 | return; | |
8925 | ||
8926 | a = attrs; | |
8927 | /* See if any of the nonnull attributes has no arguments. If so, | |
8928 | then every pointer argument is checked (in which case the check | |
8929 | for pointer type is done in check_nonnull_arg). */ | |
8930 | if (TREE_VALUE (a) != NULL_TREE) | |
8931 | do | |
8932 | a = lookup_attribute ("nonnull", TREE_CHAIN (a)); | |
8933 | while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE); | |
8934 | ||
8935 | if (a != NULL_TREE) | |
8936 | for (i = 0; i < nargs; i++) | |
8937 | check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i], | |
8938 | i + 1); | |
8939 | else | |
b34c7881 | 8940 | { |
332f1d24 JJ |
8941 | /* Walk the argument list. If we encounter an argument number we |
8942 | should check for non-null, do it. */ | |
8943 | for (i = 0; i < nargs; i++) | |
b34c7881 | 8944 | { |
332f1d24 | 8945 | for (a = attrs; ; a = TREE_CHAIN (a)) |
6de9cd9a | 8946 | { |
332f1d24 JJ |
8947 | a = lookup_attribute ("nonnull", a); |
8948 | if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1)) | |
8949 | break; | |
6de9cd9a | 8950 | } |
332f1d24 JJ |
8951 | |
8952 | if (a != NULL_TREE) | |
8953 | check_function_arguments_recurse (check_nonnull_arg, NULL, | |
8954 | argarray[i], i + 1); | |
b34c7881 JT |
8955 | } |
8956 | } | |
8957 | } | |
8958 | ||
254986c7 | 8959 | /* Check that the Nth argument of a function call (counting backwards |
94a0dd7b SL |
8960 | from the end) is a (pointer)0. The NARGS arguments are passed in the |
8961 | array ARGARRAY. */ | |
3d091dac KG |
8962 | |
8963 | static void | |
dde05067 | 8964 | check_function_sentinel (const_tree fntype, int nargs, tree *argarray) |
3d091dac | 8965 | { |
dde05067 | 8966 | tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype)); |
3d091dac KG |
8967 | |
8968 | if (attr) | |
8969 | { | |
94a0dd7b SL |
8970 | int len = 0; |
8971 | int pos = 0; | |
8972 | tree sentinel; | |
dde05067 NF |
8973 | function_args_iterator iter; |
8974 | tree t; | |
c22cacf3 | 8975 | |
94a0dd7b | 8976 | /* Skip over the named arguments. */ |
dde05067 | 8977 | FOREACH_FUNCTION_ARGS (fntype, t, iter) |
c22cacf3 | 8978 | { |
dde05067 NF |
8979 | if (len == nargs) |
8980 | break; | |
94a0dd7b SL |
8981 | len++; |
8982 | } | |
254986c7 | 8983 | |
94a0dd7b SL |
8984 | if (TREE_VALUE (attr)) |
8985 | { | |
8986 | tree p = TREE_VALUE (TREE_VALUE (attr)); | |
8987 | pos = TREE_INT_CST_LOW (p); | |
8988 | } | |
254986c7 | 8989 | |
94a0dd7b SL |
8990 | /* The sentinel must be one of the varargs, i.e. |
8991 | in position >= the number of fixed arguments. */ | |
8992 | if ((nargs - 1 - pos) < len) | |
8993 | { | |
7332899a | 8994 | warning (OPT_Wformat_, |
94a0dd7b SL |
8995 | "not enough variable arguments to fit a sentinel"); |
8996 | return; | |
3d091dac | 8997 | } |
94a0dd7b SL |
8998 | |
8999 | /* Validate the sentinel. */ | |
9000 | sentinel = argarray[nargs - 1 - pos]; | |
9001 | if ((!POINTER_TYPE_P (TREE_TYPE (sentinel)) | |
9002 | || !integer_zerop (sentinel)) | |
9003 | /* Although __null (in C++) is only an integer we allow it | |
9004 | nevertheless, as we are guaranteed that it's exactly | |
9005 | as wide as a pointer, and we don't want to force | |
9006 | users to cast the NULL they have written there. | |
9007 | We warn with -Wstrict-null-sentinel, though. */ | |
9008 | && (warn_strict_null_sentinel || null_node != sentinel)) | |
7332899a | 9009 | warning (OPT_Wformat_, "missing sentinel in function call"); |
3d091dac KG |
9010 | } |
9011 | } | |
9012 | ||
b34c7881 JT |
9013 | /* Helper for check_function_nonnull; given a list of operands which |
9014 | must be non-null in ARGS, determine if operand PARAM_NUM should be | |
9015 | checked. */ | |
9016 | ||
9017 | static bool | |
35b1a6fa | 9018 | nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num) |
b34c7881 | 9019 | { |
6de9cd9a | 9020 | unsigned HOST_WIDE_INT arg_num = 0; |
b34c7881 JT |
9021 | |
9022 | for (; args; args = TREE_CHAIN (args)) | |
9023 | { | |
366de0ce NS |
9024 | bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num); |
9025 | ||
9026 | gcc_assert (found); | |
b34c7881 JT |
9027 | |
9028 | if (arg_num == param_num) | |
9029 | return true; | |
9030 | } | |
9031 | return false; | |
9032 | } | |
9033 | ||
9034 | /* Check that the function argument PARAM (which is operand number | |
9035 | PARAM_NUM) is non-null. This is called by check_function_nonnull | |
9036 | via check_function_arguments_recurse. */ | |
9037 | ||
9038 | static void | |
e18476eb | 9039 | check_nonnull_arg (void * ARG_UNUSED (ctx), tree param, |
35b1a6fa | 9040 | unsigned HOST_WIDE_INT param_num) |
b34c7881 JT |
9041 | { |
9042 | /* Just skip checking the argument if it's not a pointer. This can | |
9043 | happen if the "nonnull" attribute was given without an operand | |
9044 | list (which means to check every pointer argument). */ | |
9045 | ||
9046 | if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) | |
9047 | return; | |
9048 | ||
9049 | if (integer_zerop (param)) | |
30480ec4 DD |
9050 | warning (OPT_Wnonnull, "null argument where non-null required " |
9051 | "(argument %lu)", (unsigned long) param_num); | |
b34c7881 JT |
9052 | } |
9053 | ||
9054 | /* Helper for nonnull attribute handling; fetch the operand number | |
9055 | from the attribute argument list. */ | |
9056 | ||
9057 | static bool | |
35b1a6fa | 9058 | get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) |
b34c7881 | 9059 | { |
807e902e KZ |
9060 | /* Verify the arg number is a small constant. */ |
9061 | if (tree_fits_uhwi_p (arg_num_expr)) | |
9062 | { | |
9063 | *valp = TREE_INT_CST_LOW (arg_num_expr); | |
9064 | return true; | |
9065 | } | |
9066 | else | |
b34c7881 | 9067 | return false; |
b34c7881 | 9068 | } |
39f2f3c8 RS |
9069 | |
9070 | /* Handle a "nothrow" attribute; arguments as in | |
9071 | struct attribute_spec.handler. */ | |
9072 | ||
9073 | static tree | |
e18476eb BI |
9074 | handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
9075 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
39f2f3c8 RS |
9076 | { |
9077 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
9078 | TREE_NOTHROW (*node) = 1; | |
9079 | /* ??? TODO: Support types. */ | |
9080 | else | |
9081 | { | |
5c498b10 | 9082 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
39f2f3c8 RS |
9083 | *no_add_attrs = true; |
9084 | } | |
9085 | ||
9086 | return NULL_TREE; | |
9087 | } | |
0bfa5f65 RH |
9088 | |
9089 | /* Handle a "cleanup" attribute; arguments as in | |
9090 | struct attribute_spec.handler. */ | |
9091 | ||
9092 | static tree | |
35b1a6fa | 9093 | handle_cleanup_attribute (tree *node, tree name, tree args, |
e18476eb | 9094 | int ARG_UNUSED (flags), bool *no_add_attrs) |
0bfa5f65 RH |
9095 | { |
9096 | tree decl = *node; | |
9097 | tree cleanup_id, cleanup_decl; | |
9098 | ||
9099 | /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do | |
9100 | for global destructors in C++. This requires infrastructure that | |
9101 | we don't have generically at the moment. It's also not a feature | |
9102 | we'd be missing too much, since we do have attribute constructor. */ | |
9103 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
9104 | { | |
5c498b10 | 9105 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
0bfa5f65 RH |
9106 | *no_add_attrs = true; |
9107 | return NULL_TREE; | |
9108 | } | |
9109 | ||
9110 | /* Verify that the argument is a function in scope. */ | |
9111 | /* ??? We could support pointers to functions here as well, if | |
9112 | that was considered desirable. */ | |
9113 | cleanup_id = TREE_VALUE (args); | |
9114 | if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE) | |
9115 | { | |
40b97a2e | 9116 | error ("cleanup argument not an identifier"); |
0bfa5f65 RH |
9117 | *no_add_attrs = true; |
9118 | return NULL_TREE; | |
9119 | } | |
10e6657a | 9120 | cleanup_decl = lookup_name (cleanup_id); |
0bfa5f65 RH |
9121 | if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL) |
9122 | { | |
40b97a2e | 9123 | error ("cleanup argument not a function"); |
0bfa5f65 RH |
9124 | *no_add_attrs = true; |
9125 | return NULL_TREE; | |
9126 | } | |
9127 | ||
35b1a6fa | 9128 | /* That the function has proper type is checked with the |
0bfa5f65 RH |
9129 | eventual call to build_function_call. */ |
9130 | ||
9131 | return NULL_TREE; | |
9132 | } | |
72954a4f JM |
9133 | |
9134 | /* Handle a "warn_unused_result" attribute. No special handling. */ | |
9135 | ||
9136 | static tree | |
9137 | handle_warn_unused_result_attribute (tree *node, tree name, | |
e18476eb BI |
9138 | tree ARG_UNUSED (args), |
9139 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
72954a4f JM |
9140 | { |
9141 | /* Ignore the attribute for functions not returning any value. */ | |
9142 | if (VOID_TYPE_P (TREE_TYPE (*node))) | |
9143 | { | |
5c498b10 | 9144 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
72954a4f JM |
9145 | *no_add_attrs = true; |
9146 | } | |
9147 | ||
9148 | return NULL_TREE; | |
9149 | } | |
3d091dac KG |
9150 | |
9151 | /* Handle a "sentinel" attribute. */ | |
9152 | ||
9153 | static tree | |
254986c7 | 9154 | handle_sentinel_attribute (tree *node, tree name, tree args, |
3d091dac KG |
9155 | int ARG_UNUSED (flags), bool *no_add_attrs) |
9156 | { | |
f4da8dce | 9157 | if (!prototype_p (*node)) |
3d091dac | 9158 | { |
5c498b10 DD |
9159 | warning (OPT_Wattributes, |
9160 | "%qE attribute requires prototypes with named arguments", name); | |
3d091dac | 9161 | *no_add_attrs = true; |
3d091dac | 9162 | } |
254986c7 KG |
9163 | else |
9164 | { | |
dcf0c47e | 9165 | if (!stdarg_p (*node)) |
c22cacf3 | 9166 | { |
5c498b10 DD |
9167 | warning (OPT_Wattributes, |
9168 | "%qE attribute only applies to variadic functions", name); | |
254986c7 KG |
9169 | *no_add_attrs = true; |
9170 | } | |
9171 | } | |
c22cacf3 | 9172 | |
254986c7 | 9173 | if (args) |
3d091dac | 9174 | { |
254986c7 | 9175 | tree position = TREE_VALUE (args); |
661a0813 MP |
9176 | if (position && TREE_CODE (position) != IDENTIFIER_NODE |
9177 | && TREE_CODE (position) != FUNCTION_DECL) | |
9178 | position = default_conversion (position); | |
254986c7 | 9179 | |
661a0813 MP |
9180 | if (TREE_CODE (position) != INTEGER_CST |
9181 | || !INTEGRAL_TYPE_P (TREE_TYPE (position))) | |
c22cacf3 | 9182 | { |
b8698a0f | 9183 | warning (OPT_Wattributes, |
aa86a51b | 9184 | "requested position is not an integer constant"); |
254986c7 KG |
9185 | *no_add_attrs = true; |
9186 | } | |
9187 | else | |
c22cacf3 | 9188 | { |
254986c7 KG |
9189 | if (tree_int_cst_lt (position, integer_zero_node)) |
9190 | { | |
aa86a51b DM |
9191 | warning (OPT_Wattributes, |
9192 | "requested position is less than zero"); | |
254986c7 KG |
9193 | *no_add_attrs = true; |
9194 | } | |
9195 | } | |
3d091dac | 9196 | } |
c22cacf3 | 9197 | |
3d091dac KG |
9198 | return NULL_TREE; |
9199 | } | |
b5d32c25 KG |
9200 | |
9201 | /* Handle a "type_generic" attribute. */ | |
9202 | ||
9203 | static tree | |
9204 | handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name), | |
9205 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
9206 | bool * ARG_UNUSED (no_add_attrs)) | |
9207 | { | |
3bf5906b KG |
9208 | /* Ensure we have a function type. */ |
9209 | gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE); | |
b8698a0f | 9210 | |
3bf5906b | 9211 | /* Ensure we have a variadic function. */ |
dcf0c47e | 9212 | gcc_assert (!prototype_p (*node) || stdarg_p (*node)); |
b5d32c25 KG |
9213 | |
9214 | return NULL_TREE; | |
9215 | } | |
ab442df7 | 9216 | |
5779e713 | 9217 | /* Handle a "target" attribute. */ |
ab442df7 MM |
9218 | |
9219 | static tree | |
5779e713 | 9220 | handle_target_attribute (tree *node, tree name, tree args, int flags, |
ab442df7 MM |
9221 | bool *no_add_attrs) |
9222 | { | |
9223 | /* Ensure we have a function type. */ | |
9224 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
9225 | { | |
9226 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
9227 | *no_add_attrs = true; | |
9228 | } | |
ab442df7 | 9229 | else if (! targetm.target_option.valid_attribute_p (*node, name, args, |
32887460 | 9230 | flags)) |
ab442df7 MM |
9231 | *no_add_attrs = true; |
9232 | ||
9233 | return NULL_TREE; | |
9234 | } | |
9235 | ||
9236 | /* Arguments being collected for optimization. */ | |
9237 | typedef const char *const_char_p; /* For DEF_VEC_P. */ | |
9771b263 | 9238 | static GTY(()) vec<const_char_p, va_gc> *optimize_args; |
ab442df7 MM |
9239 | |
9240 | ||
9241 | /* Inner function to convert a TREE_LIST to argv string to parse the optimize | |
9242 | options in ARGS. ATTR_P is true if this is for attribute(optimize), and | |
9243 | false for #pragma GCC optimize. */ | |
9244 | ||
9245 | bool | |
9246 | parse_optimize_options (tree args, bool attr_p) | |
9247 | { | |
9248 | bool ret = true; | |
9249 | unsigned opt_argc; | |
9250 | unsigned i; | |
cbc19f39 | 9251 | int saved_flag_strict_aliasing; |
ab442df7 | 9252 | const char **opt_argv; |
6e2f1956 JM |
9253 | struct cl_decoded_option *decoded_options; |
9254 | unsigned int decoded_options_count; | |
ab442df7 MM |
9255 | tree ap; |
9256 | ||
9257 | /* Build up argv vector. Just in case the string is stored away, use garbage | |
9258 | collected strings. */ | |
9771b263 DN |
9259 | vec_safe_truncate (optimize_args, 0); |
9260 | vec_safe_push (optimize_args, (const char *) NULL); | |
ab442df7 MM |
9261 | |
9262 | for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap)) | |
9263 | { | |
9264 | tree value = TREE_VALUE (ap); | |
9265 | ||
9266 | if (TREE_CODE (value) == INTEGER_CST) | |
9267 | { | |
9268 | char buffer[20]; | |
9269 | sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value)); | |
9771b263 | 9270 | vec_safe_push (optimize_args, ggc_strdup (buffer)); |
ab442df7 MM |
9271 | } |
9272 | ||
9273 | else if (TREE_CODE (value) == STRING_CST) | |
9274 | { | |
9275 | /* Split string into multiple substrings. */ | |
9276 | size_t len = TREE_STRING_LENGTH (value); | |
9277 | char *p = ASTRDUP (TREE_STRING_POINTER (value)); | |
9278 | char *end = p + len; | |
9279 | char *comma; | |
9280 | char *next_p = p; | |
9281 | ||
9282 | while (next_p != NULL) | |
9283 | { | |
9284 | size_t len2; | |
9285 | char *q, *r; | |
9286 | ||
9287 | p = next_p; | |
9288 | comma = strchr (p, ','); | |
9289 | if (comma) | |
9290 | { | |
9291 | len2 = comma - p; | |
9292 | *comma = '\0'; | |
9293 | next_p = comma+1; | |
9294 | } | |
9295 | else | |
9296 | { | |
9297 | len2 = end - p; | |
9298 | next_p = NULL; | |
9299 | } | |
9300 | ||
a9429e29 | 9301 | r = q = (char *) ggc_alloc_atomic (len2 + 3); |
ab442df7 MM |
9302 | |
9303 | /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx | |
9304 | options. */ | |
9305 | if (*p == '-' && p[1] != 'O' && p[1] != 'f') | |
9306 | { | |
9307 | ret = false; | |
9308 | if (attr_p) | |
9309 | warning (OPT_Wattributes, | |
06730c5d | 9310 | "bad option %s to optimize attribute", p); |
ab442df7 MM |
9311 | else |
9312 | warning (OPT_Wpragmas, | |
de621752 | 9313 | "bad option %s to pragma attribute", p); |
ab442df7 MM |
9314 | continue; |
9315 | } | |
9316 | ||
9317 | if (*p != '-') | |
9318 | { | |
9319 | *r++ = '-'; | |
9320 | ||
9321 | /* Assume that Ox is -Ox, a numeric value is -Ox, a s by | |
9322 | itself is -Os, and any other switch begins with a -f. */ | |
9323 | if ((*p >= '0' && *p <= '9') | |
9324 | || (p[0] == 's' && p[1] == '\0')) | |
9325 | *r++ = 'O'; | |
9326 | else if (*p != 'O') | |
9327 | *r++ = 'f'; | |
9328 | } | |
9329 | ||
9330 | memcpy (r, p, len2); | |
9331 | r[len2] = '\0'; | |
9771b263 | 9332 | vec_safe_push (optimize_args, (const char *) q); |
ab442df7 MM |
9333 | } |
9334 | ||
9335 | } | |
9336 | } | |
9337 | ||
9771b263 | 9338 | opt_argc = optimize_args->length (); |
ab442df7 MM |
9339 | opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1)); |
9340 | ||
9341 | for (i = 1; i < opt_argc; i++) | |
9771b263 | 9342 | opt_argv[i] = (*optimize_args)[i]; |
ab442df7 | 9343 | |
cbc19f39 JJ |
9344 | saved_flag_strict_aliasing = flag_strict_aliasing; |
9345 | ||
ab442df7 | 9346 | /* Now parse the options. */ |
a75bfaa6 JM |
9347 | decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv, |
9348 | &decoded_options, | |
9349 | &decoded_options_count); | |
9350 | decode_options (&global_options, &global_options_set, | |
a4d8c676 JM |
9351 | decoded_options, decoded_options_count, |
9352 | input_location, global_dc); | |
ab442df7 | 9353 | |
2b7e2984 SE |
9354 | targetm.override_options_after_change(); |
9355 | ||
cbc19f39 JJ |
9356 | /* Don't allow changing -fstrict-aliasing. */ |
9357 | flag_strict_aliasing = saved_flag_strict_aliasing; | |
9358 | ||
9771b263 | 9359 | optimize_args->truncate (0); |
ab442df7 MM |
9360 | return ret; |
9361 | } | |
9362 | ||
9363 | /* For handling "optimize" attribute. arguments as in | |
9364 | struct attribute_spec.handler. */ | |
9365 | ||
9366 | static tree | |
9367 | handle_optimize_attribute (tree *node, tree name, tree args, | |
9368 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
9369 | { | |
9370 | /* Ensure we have a function type. */ | |
9371 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
9372 | { | |
9373 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
9374 | *no_add_attrs = true; | |
9375 | } | |
9376 | else | |
9377 | { | |
9378 | struct cl_optimization cur_opts; | |
9379 | tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node); | |
9380 | ||
9381 | /* Save current options. */ | |
46625112 | 9382 | cl_optimization_save (&cur_opts, &global_options); |
ab442df7 MM |
9383 | |
9384 | /* If we previously had some optimization options, use them as the | |
9385 | default. */ | |
9386 | if (old_opts) | |
46625112 JM |
9387 | cl_optimization_restore (&global_options, |
9388 | TREE_OPTIMIZATION (old_opts)); | |
ab442df7 MM |
9389 | |
9390 | /* Parse options, and update the vector. */ | |
9391 | parse_optimize_options (args, true); | |
9392 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) | |
bf7b5747 | 9393 | = build_optimization_node (&global_options); |
ab442df7 MM |
9394 | |
9395 | /* Restore current options. */ | |
46625112 | 9396 | cl_optimization_restore (&global_options, &cur_opts); |
ab442df7 MM |
9397 | } |
9398 | ||
9399 | return NULL_TREE; | |
9400 | } | |
7458026b ILT |
9401 | |
9402 | /* Handle a "no_split_stack" attribute. */ | |
9403 | ||
9404 | static tree | |
9405 | handle_no_split_stack_attribute (tree *node, tree name, | |
9406 | tree ARG_UNUSED (args), | |
9407 | int ARG_UNUSED (flags), | |
9408 | bool *no_add_attrs) | |
9409 | { | |
9410 | tree decl = *node; | |
9411 | ||
9412 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
9413 | { | |
9414 | error_at (DECL_SOURCE_LOCATION (decl), | |
9415 | "%qE attribute applies only to functions", name); | |
9416 | *no_add_attrs = true; | |
9417 | } | |
9418 | else if (DECL_INITIAL (decl)) | |
9419 | { | |
9420 | error_at (DECL_SOURCE_LOCATION (decl), | |
9421 | "can%'t set %qE attribute after definition", name); | |
9422 | *no_add_attrs = true; | |
9423 | } | |
9424 | ||
9425 | return NULL_TREE; | |
9426 | } | |
826cacfe MG |
9427 | |
9428 | /* Handle a "returns_nonnull" attribute; arguments as in | |
9429 | struct attribute_spec.handler. */ | |
9430 | ||
9431 | static tree | |
9432 | handle_returns_nonnull_attribute (tree *node, tree, tree, int, | |
9433 | bool *no_add_attrs) | |
9434 | { | |
9435 | // Even without a prototype we still have a return type we can check. | |
9436 | if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE) | |
9437 | { | |
9438 | error ("returns_nonnull attribute on a function not returning a pointer"); | |
9439 | *no_add_attrs = true; | |
9440 | } | |
9441 | return NULL_TREE; | |
9442 | } | |
9443 | ||
976d5a22 TT |
9444 | /* Handle a "designated_init" attribute; arguments as in |
9445 | struct attribute_spec.handler. */ | |
9446 | ||
9447 | static tree | |
9448 | handle_designated_init_attribute (tree *node, tree name, tree, int, | |
9449 | bool *no_add_attrs) | |
9450 | { | |
9451 | if (TREE_CODE (*node) != RECORD_TYPE) | |
9452 | { | |
9453 | error ("%qE attribute is only valid on %<struct%> type", name); | |
9454 | *no_add_attrs = true; | |
9455 | } | |
9456 | return NULL_TREE; | |
9457 | } | |
9458 | ||
b34c7881 | 9459 | \f |
dde05067 NF |
9460 | /* Check for valid arguments being passed to a function with FNTYPE. |
9461 | There are NARGS arguments in the array ARGARRAY. */ | |
b34c7881 | 9462 | void |
dde05067 | 9463 | check_function_arguments (const_tree fntype, int nargs, tree *argarray) |
b34c7881 JT |
9464 | { |
9465 | /* Check for null being passed in a pointer argument that must be | |
9466 | non-null. We also need to do this if format checking is enabled. */ | |
9467 | ||
9468 | if (warn_nonnull) | |
dde05067 | 9469 | check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
b34c7881 JT |
9470 | |
9471 | /* Check for errors in format strings. */ | |
9472 | ||
e6c69da0 | 9473 | if (warn_format || warn_suggest_attribute_format) |
dde05067 | 9474 | check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
7876a414 KG |
9475 | |
9476 | if (warn_format) | |
dde05067 | 9477 | check_function_sentinel (fntype, nargs, argarray); |
b34c7881 JT |
9478 | } |
9479 | ||
9480 | /* Generic argument checking recursion routine. PARAM is the argument to | |
9481 | be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked | |
9482 | once the argument is resolved. CTX is context for the callback. */ | |
9483 | void | |
35b1a6fa AJ |
9484 | check_function_arguments_recurse (void (*callback) |
9485 | (void *, tree, unsigned HOST_WIDE_INT), | |
9486 | void *ctx, tree param, | |
9487 | unsigned HOST_WIDE_INT param_num) | |
b34c7881 | 9488 | { |
1043771b | 9489 | if (CONVERT_EXPR_P (param) |
1344f9a3 JM |
9490 | && (TYPE_PRECISION (TREE_TYPE (param)) |
9491 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0))))) | |
b34c7881 JT |
9492 | { |
9493 | /* Strip coercion. */ | |
9494 | check_function_arguments_recurse (callback, ctx, | |
6de9cd9a | 9495 | TREE_OPERAND (param, 0), param_num); |
b34c7881 JT |
9496 | return; |
9497 | } | |
9498 | ||
9499 | if (TREE_CODE (param) == CALL_EXPR) | |
9500 | { | |
5039610b | 9501 | tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param))); |
b34c7881 JT |
9502 | tree attrs; |
9503 | bool found_format_arg = false; | |
9504 | ||
9505 | /* See if this is a call to a known internationalization function | |
9506 | that modifies a format arg. Such a function may have multiple | |
9507 | format_arg attributes (for example, ngettext). */ | |
9508 | ||
9509 | for (attrs = TYPE_ATTRIBUTES (type); | |
9510 | attrs; | |
9511 | attrs = TREE_CHAIN (attrs)) | |
9512 | if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs))) | |
9513 | { | |
5039610b | 9514 | tree inner_arg; |
b34c7881 JT |
9515 | tree format_num_expr; |
9516 | int format_num; | |
9517 | int i; | |
5039610b | 9518 | call_expr_arg_iterator iter; |
b34c7881 JT |
9519 | |
9520 | /* Extract the argument number, which was previously checked | |
9521 | to be valid. */ | |
9522 | format_num_expr = TREE_VALUE (TREE_VALUE (attrs)); | |
b34c7881 | 9523 | |
6b3b8c27 | 9524 | format_num = tree_to_uhwi (format_num_expr); |
b34c7881 | 9525 | |
5039610b SL |
9526 | for (inner_arg = first_call_expr_arg (param, &iter), i = 1; |
9527 | inner_arg != 0; | |
9528 | inner_arg = next_call_expr_arg (&iter), i++) | |
b34c7881 JT |
9529 | if (i == format_num) |
9530 | { | |
9531 | check_function_arguments_recurse (callback, ctx, | |
5039610b | 9532 | inner_arg, param_num); |
b34c7881 JT |
9533 | found_format_arg = true; |
9534 | break; | |
9535 | } | |
9536 | } | |
9537 | ||
9538 | /* If we found a format_arg attribute and did a recursive check, | |
9539 | we are done with checking this argument. Otherwise, we continue | |
9540 | and this will be considered a non-literal. */ | |
9541 | if (found_format_arg) | |
9542 | return; | |
9543 | } | |
9544 | ||
9545 | if (TREE_CODE (param) == COND_EXPR) | |
9546 | { | |
9547 | /* Check both halves of the conditional expression. */ | |
9548 | check_function_arguments_recurse (callback, ctx, | |
6de9cd9a | 9549 | TREE_OPERAND (param, 1), param_num); |
b34c7881 | 9550 | check_function_arguments_recurse (callback, ctx, |
6de9cd9a | 9551 | TREE_OPERAND (param, 2), param_num); |
b34c7881 JT |
9552 | return; |
9553 | } | |
9554 | ||
9555 | (*callback) (ctx, param, param_num); | |
9556 | } | |
e2500fed | 9557 | |
a98c2819 MLI |
9558 | /* Checks for a builtin function FNDECL that the number of arguments |
9559 | NARGS against the required number REQUIRED and issues an error if | |
9560 | there is a mismatch. Returns true if the number of arguments is | |
9561 | correct, otherwise false. */ | |
83322951 RG |
9562 | |
9563 | static bool | |
a98c2819 | 9564 | builtin_function_validate_nargs (tree fndecl, int nargs, int required) |
83322951 RG |
9565 | { |
9566 | if (nargs < required) | |
9567 | { | |
a98c2819 MLI |
9568 | error_at (input_location, |
9569 | "not enough arguments to function %qE", fndecl); | |
83322951 RG |
9570 | return false; |
9571 | } | |
9572 | else if (nargs > required) | |
9573 | { | |
a98c2819 MLI |
9574 | error_at (input_location, |
9575 | "too many arguments to function %qE", fndecl); | |
83322951 RG |
9576 | return false; |
9577 | } | |
9578 | return true; | |
9579 | } | |
9580 | ||
9581 | /* Verifies the NARGS arguments ARGS to the builtin function FNDECL. | |
9582 | Returns false if there was an error, otherwise true. */ | |
9583 | ||
9584 | bool | |
9585 | check_builtin_function_arguments (tree fndecl, int nargs, tree *args) | |
9586 | { | |
9587 | if (!DECL_BUILT_IN (fndecl) | |
9588 | || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) | |
9589 | return true; | |
9590 | ||
9591 | switch (DECL_FUNCTION_CODE (fndecl)) | |
9592 | { | |
9593 | case BUILT_IN_CONSTANT_P: | |
a98c2819 | 9594 | return builtin_function_validate_nargs (fndecl, nargs, 1); |
83322951 RG |
9595 | |
9596 | case BUILT_IN_ISFINITE: | |
9597 | case BUILT_IN_ISINF: | |
05f41289 | 9598 | case BUILT_IN_ISINF_SIGN: |
83322951 RG |
9599 | case BUILT_IN_ISNAN: |
9600 | case BUILT_IN_ISNORMAL: | |
a98c2819 | 9601 | if (builtin_function_validate_nargs (fndecl, nargs, 1)) |
83322951 RG |
9602 | { |
9603 | if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE) | |
9604 | { | |
9605 | error ("non-floating-point argument in call to " | |
9606 | "function %qE", fndecl); | |
9607 | return false; | |
9608 | } | |
9609 | return true; | |
9610 | } | |
9611 | return false; | |
9612 | ||
9613 | case BUILT_IN_ISGREATER: | |
9614 | case BUILT_IN_ISGREATEREQUAL: | |
9615 | case BUILT_IN_ISLESS: | |
9616 | case BUILT_IN_ISLESSEQUAL: | |
9617 | case BUILT_IN_ISLESSGREATER: | |
9618 | case BUILT_IN_ISUNORDERED: | |
a98c2819 | 9619 | if (builtin_function_validate_nargs (fndecl, nargs, 2)) |
83322951 RG |
9620 | { |
9621 | enum tree_code code0, code1; | |
9622 | code0 = TREE_CODE (TREE_TYPE (args[0])); | |
9623 | code1 = TREE_CODE (TREE_TYPE (args[1])); | |
9624 | if (!((code0 == REAL_TYPE && code1 == REAL_TYPE) | |
9625 | || (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
9626 | || (code0 == INTEGER_TYPE && code1 == REAL_TYPE))) | |
9627 | { | |
9628 | error ("non-floating-point arguments in call to " | |
9629 | "function %qE", fndecl); | |
9630 | return false; | |
9631 | } | |
9632 | return true; | |
9633 | } | |
9634 | return false; | |
9635 | ||
3bf5906b | 9636 | case BUILT_IN_FPCLASSIFY: |
a98c2819 | 9637 | if (builtin_function_validate_nargs (fndecl, nargs, 6)) |
3bf5906b KG |
9638 | { |
9639 | unsigned i; | |
b8698a0f | 9640 | |
3bf5906b KG |
9641 | for (i=0; i<5; i++) |
9642 | if (TREE_CODE (args[i]) != INTEGER_CST) | |
9643 | { | |
9644 | error ("non-const integer argument %u in call to function %qE", | |
9645 | i+1, fndecl); | |
9646 | return false; | |
9647 | } | |
9648 | ||
9649 | if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE) | |
9650 | { | |
9651 | error ("non-floating-point argument in call to function %qE", | |
9652 | fndecl); | |
9653 | return false; | |
9654 | } | |
9655 | return true; | |
9656 | } | |
9657 | return false; | |
9658 | ||
45d439ac JJ |
9659 | case BUILT_IN_ASSUME_ALIGNED: |
9660 | if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2))) | |
9661 | { | |
9662 | if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE) | |
9663 | { | |
9664 | error ("non-integer argument 3 in call to function %qE", fndecl); | |
9665 | return false; | |
9666 | } | |
9667 | return true; | |
9668 | } | |
9669 | return false; | |
9670 | ||
1304953e JJ |
9671 | case BUILT_IN_ADD_OVERFLOW: |
9672 | case BUILT_IN_SUB_OVERFLOW: | |
9673 | case BUILT_IN_MUL_OVERFLOW: | |
9674 | if (builtin_function_validate_nargs (fndecl, nargs, 3)) | |
9675 | { | |
9676 | unsigned i; | |
9677 | for (i = 0; i < 2; i++) | |
9678 | if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i]))) | |
9679 | { | |
9680 | error ("argument %u in call to function %qE does not have " | |
9681 | "integral type", i + 1, fndecl); | |
9682 | return false; | |
9683 | } | |
9684 | if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE | |
9685 | || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE) | |
9686 | { | |
9687 | error ("argument 3 in call to function %qE does not have " | |
9688 | "pointer to integer type", fndecl); | |
9689 | return false; | |
9690 | } | |
9691 | return true; | |
9692 | } | |
9693 | return false; | |
9694 | ||
83322951 RG |
9695 | default: |
9696 | return true; | |
9697 | } | |
9698 | } | |
9699 | ||
d07605f5 AP |
9700 | /* Function to help qsort sort FIELD_DECLs by name order. */ |
9701 | ||
9702 | int | |
9703 | field_decl_cmp (const void *x_p, const void *y_p) | |
9704 | { | |
28dab132 BI |
9705 | const tree *const x = (const tree *const) x_p; |
9706 | const tree *const y = (const tree *const) y_p; | |
9707 | ||
d07605f5 AP |
9708 | if (DECL_NAME (*x) == DECL_NAME (*y)) |
9709 | /* A nontype is "greater" than a type. */ | |
9710 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
9711 | if (DECL_NAME (*x) == NULL_TREE) | |
9712 | return -1; | |
9713 | if (DECL_NAME (*y) == NULL_TREE) | |
9714 | return 1; | |
9715 | if (DECL_NAME (*x) < DECL_NAME (*y)) | |
9716 | return -1; | |
9717 | return 1; | |
9718 | } | |
9719 | ||
9720 | static struct { | |
9721 | gt_pointer_operator new_value; | |
9722 | void *cookie; | |
9723 | } resort_data; | |
9724 | ||
9725 | /* This routine compares two fields like field_decl_cmp but using the | |
9726 | pointer operator in resort_data. */ | |
9727 | ||
9728 | static int | |
9729 | resort_field_decl_cmp (const void *x_p, const void *y_p) | |
9730 | { | |
28dab132 BI |
9731 | const tree *const x = (const tree *const) x_p; |
9732 | const tree *const y = (const tree *const) y_p; | |
d07605f5 AP |
9733 | |
9734 | if (DECL_NAME (*x) == DECL_NAME (*y)) | |
9735 | /* A nontype is "greater" than a type. */ | |
9736 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
9737 | if (DECL_NAME (*x) == NULL_TREE) | |
9738 | return -1; | |
9739 | if (DECL_NAME (*y) == NULL_TREE) | |
9740 | return 1; | |
9741 | { | |
9742 | tree d1 = DECL_NAME (*x); | |
9743 | tree d2 = DECL_NAME (*y); | |
9744 | resort_data.new_value (&d1, resort_data.cookie); | |
9745 | resort_data.new_value (&d2, resort_data.cookie); | |
9746 | if (d1 < d2) | |
9747 | return -1; | |
9748 | } | |
9749 | return 1; | |
9750 | } | |
9751 | ||
9752 | /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ | |
9753 | ||
9754 | void | |
9755 | resort_sorted_fields (void *obj, | |
e18476eb | 9756 | void * ARG_UNUSED (orig_obj), |
6de9cd9a DN |
9757 | gt_pointer_operator new_value, |
9758 | void *cookie) | |
d07605f5 | 9759 | { |
e18476eb | 9760 | struct sorted_fields_type *sf = (struct sorted_fields_type *) obj; |
d07605f5 AP |
9761 | resort_data.new_value = new_value; |
9762 | resort_data.cookie = cookie; | |
9763 | qsort (&sf->elts[0], sf->len, sizeof (tree), | |
6de9cd9a | 9764 | resort_field_decl_cmp); |
d07605f5 AP |
9765 | } |
9766 | ||
0a3ee0fd GDR |
9767 | /* Subroutine of c_parse_error. |
9768 | Return the result of concatenating LHS and RHS. RHS is really | |
9769 | a string literal, its first character is indicated by RHS_START and | |
3292fb42 | 9770 | RHS_SIZE is its length (including the terminating NUL character). |
0a3ee0fd GDR |
9771 | |
9772 | The caller is responsible for deleting the returned pointer. */ | |
9773 | ||
9774 | static char * | |
9775 | catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) | |
9776 | { | |
9777 | const int lhs_size = strlen (lhs); | |
9778 | char *result = XNEWVEC (char, lhs_size + rhs_size); | |
9779 | strncpy (result, lhs, lhs_size); | |
9780 | strncpy (result + lhs_size, rhs_start, rhs_size); | |
9781 | return result; | |
9782 | } | |
9783 | ||
4b794eaf | 9784 | /* Issue the error given by GMSGID, indicating that it occurred before |
4bb8ca28 MM |
9785 | TOKEN, which had the associated VALUE. */ |
9786 | ||
9787 | void | |
b8698a0f | 9788 | c_parse_error (const char *gmsgid, enum cpp_ttype token_type, |
cfc93532 | 9789 | tree value, unsigned char token_flags) |
4bb8ca28 | 9790 | { |
0a3ee0fd GDR |
9791 | #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2)) |
9792 | ||
9793 | char *message = NULL; | |
4bb8ca28 | 9794 | |
cfc93532 | 9795 | if (token_type == CPP_EOF) |
4b794eaf | 9796 | message = catenate_messages (gmsgid, " at end of input"); |
b8698a0f L |
9797 | else if (token_type == CPP_CHAR |
9798 | || token_type == CPP_WCHAR | |
cfc93532 MLI |
9799 | || token_type == CPP_CHAR16 |
9800 | || token_type == CPP_CHAR32) | |
4bb8ca28 MM |
9801 | { |
9802 | unsigned int val = TREE_INT_CST_LOW (value); | |
b6baa67d KVH |
9803 | const char *prefix; |
9804 | ||
cfc93532 | 9805 | switch (token_type) |
b6baa67d KVH |
9806 | { |
9807 | default: | |
9808 | prefix = ""; | |
9809 | break; | |
9810 | case CPP_WCHAR: | |
9811 | prefix = "L"; | |
9812 | break; | |
9813 | case CPP_CHAR16: | |
9814 | prefix = "u"; | |
9815 | break; | |
9816 | case CPP_CHAR32: | |
9817 | prefix = "U"; | |
9818 | break; | |
9819 | } | |
9820 | ||
4bb8ca28 | 9821 | if (val <= UCHAR_MAX && ISGRAPH (val)) |
c22cacf3 | 9822 | message = catenate_messages (gmsgid, " before %s'%c'"); |
4bb8ca28 | 9823 | else |
c22cacf3 | 9824 | message = catenate_messages (gmsgid, " before %s'\\x%x'"); |
0a3ee0fd | 9825 | |
b6baa67d | 9826 | error (message, prefix, val); |
0a3ee0fd GDR |
9827 | free (message); |
9828 | message = NULL; | |
4bb8ca28 | 9829 | } |
65e5a578 ESR |
9830 | else if (token_type == CPP_CHAR_USERDEF |
9831 | || token_type == CPP_WCHAR_USERDEF | |
9832 | || token_type == CPP_CHAR16_USERDEF | |
9833 | || token_type == CPP_CHAR32_USERDEF) | |
9834 | message = catenate_messages (gmsgid, | |
9835 | " before user-defined character literal"); | |
9836 | else if (token_type == CPP_STRING_USERDEF | |
9837 | || token_type == CPP_WSTRING_USERDEF | |
9838 | || token_type == CPP_STRING16_USERDEF | |
9839 | || token_type == CPP_STRING32_USERDEF | |
9840 | || token_type == CPP_UTF8STRING_USERDEF) | |
9841 | message = catenate_messages (gmsgid, " before user-defined string literal"); | |
b8698a0f L |
9842 | else if (token_type == CPP_STRING |
9843 | || token_type == CPP_WSTRING | |
cfc93532 | 9844 | || token_type == CPP_STRING16 |
2c6e3f55 JJ |
9845 | || token_type == CPP_STRING32 |
9846 | || token_type == CPP_UTF8STRING) | |
4b794eaf | 9847 | message = catenate_messages (gmsgid, " before string constant"); |
cfc93532 | 9848 | else if (token_type == CPP_NUMBER) |
4b794eaf | 9849 | message = catenate_messages (gmsgid, " before numeric constant"); |
cfc93532 | 9850 | else if (token_type == CPP_NAME) |
0a3ee0fd | 9851 | { |
4b794eaf | 9852 | message = catenate_messages (gmsgid, " before %qE"); |
c51a1ba9 | 9853 | error (message, value); |
0a3ee0fd GDR |
9854 | free (message); |
9855 | message = NULL; | |
9856 | } | |
cfc93532 | 9857 | else if (token_type == CPP_PRAGMA) |
bc4071dd | 9858 | message = catenate_messages (gmsgid, " before %<#pragma%>"); |
cfc93532 | 9859 | else if (token_type == CPP_PRAGMA_EOL) |
bc4071dd | 9860 | message = catenate_messages (gmsgid, " before end of line"); |
34429675 JM |
9861 | else if (token_type == CPP_DECLTYPE) |
9862 | message = catenate_messages (gmsgid, " before %<decltype%>"); | |
cfc93532 | 9863 | else if (token_type < N_TTYPES) |
0a3ee0fd | 9864 | { |
4b794eaf | 9865 | message = catenate_messages (gmsgid, " before %qs token"); |
cfc93532 | 9866 | error (message, cpp_type2name (token_type, token_flags)); |
0a3ee0fd GDR |
9867 | free (message); |
9868 | message = NULL; | |
9869 | } | |
4bb8ca28 | 9870 | else |
4b794eaf | 9871 | error (gmsgid); |
0a3ee0fd GDR |
9872 | |
9873 | if (message) | |
9874 | { | |
9875 | error (message); | |
9876 | free (message); | |
9877 | } | |
c22cacf3 | 9878 | #undef catenate_messages |
4bb8ca28 MM |
9879 | } |
9880 | ||
87cf0651 SB |
9881 | /* Return the gcc option code associated with the reason for a cpp |
9882 | message, or 0 if none. */ | |
9883 | ||
9884 | static int | |
9885 | c_option_controlling_cpp_error (int reason) | |
9886 | { | |
b559c810 | 9887 | const struct cpp_reason_option_codes_t *entry; |
87cf0651 | 9888 | |
b559c810 | 9889 | for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++) |
87cf0651 SB |
9890 | { |
9891 | if (entry->reason == reason) | |
9892 | return entry->option_code; | |
9893 | } | |
9894 | return 0; | |
9895 | } | |
9896 | ||
148e4216 | 9897 | /* Callback from cpp_error for PFILE to print diagnostics from the |
87cf0651 SB |
9898 | preprocessor. The diagnostic is of type LEVEL, with REASON set |
9899 | to the reason code if LEVEL is represents a warning, at location | |
148e4216 JM |
9900 | LOCATION unless this is after lexing and the compiler's location |
9901 | should be used instead, with column number possibly overridden by | |
9902 | COLUMN_OVERRIDE if not zero; MSG is the translated message and AP | |
9903 | the arguments. Returns true if a diagnostic was emitted, false | |
9904 | otherwise. */ | |
9905 | ||
9906 | bool | |
87cf0651 | 9907 | c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason, |
148e4216 JM |
9908 | location_t location, unsigned int column_override, |
9909 | const char *msg, va_list *ap) | |
9910 | { | |
9911 | diagnostic_info diagnostic; | |
9912 | diagnostic_t dlevel; | |
e3339d0f | 9913 | bool save_warn_system_headers = global_dc->dc_warn_system_headers; |
148e4216 JM |
9914 | bool ret; |
9915 | ||
9916 | switch (level) | |
9917 | { | |
9918 | case CPP_DL_WARNING_SYSHDR: | |
9919 | if (flag_no_output) | |
9920 | return false; | |
e3339d0f | 9921 | global_dc->dc_warn_system_headers = 1; |
148e4216 JM |
9922 | /* Fall through. */ |
9923 | case CPP_DL_WARNING: | |
9924 | if (flag_no_output) | |
9925 | return false; | |
9926 | dlevel = DK_WARNING; | |
9927 | break; | |
9928 | case CPP_DL_PEDWARN: | |
9929 | if (flag_no_output && !flag_pedantic_errors) | |
9930 | return false; | |
9931 | dlevel = DK_PEDWARN; | |
9932 | break; | |
9933 | case CPP_DL_ERROR: | |
9934 | dlevel = DK_ERROR; | |
9935 | break; | |
9936 | case CPP_DL_ICE: | |
9937 | dlevel = DK_ICE; | |
9938 | break; | |
9939 | case CPP_DL_NOTE: | |
9940 | dlevel = DK_NOTE; | |
9941 | break; | |
47580d22 JM |
9942 | case CPP_DL_FATAL: |
9943 | dlevel = DK_FATAL; | |
9944 | break; | |
148e4216 JM |
9945 | default: |
9946 | gcc_unreachable (); | |
9947 | } | |
9948 | if (done_lexing) | |
9949 | location = input_location; | |
9950 | diagnostic_set_info_translated (&diagnostic, msg, ap, | |
9951 | location, dlevel); | |
9952 | if (column_override) | |
9953 | diagnostic_override_column (&diagnostic, column_override); | |
87cf0651 SB |
9954 | diagnostic_override_option_index (&diagnostic, |
9955 | c_option_controlling_cpp_error (reason)); | |
148e4216 JM |
9956 | ret = report_diagnostic (&diagnostic); |
9957 | if (level == CPP_DL_WARNING_SYSHDR) | |
e3339d0f | 9958 | global_dc->dc_warn_system_headers = save_warn_system_headers; |
148e4216 JM |
9959 | return ret; |
9960 | } | |
9961 | ||
c5ff069d ZW |
9962 | /* Convert a character from the host to the target execution character |
9963 | set. cpplib handles this, mostly. */ | |
9964 | ||
9965 | HOST_WIDE_INT | |
9966 | c_common_to_target_charset (HOST_WIDE_INT c) | |
9967 | { | |
9968 | /* Character constants in GCC proper are sign-extended under -fsigned-char, | |
9969 | zero-extended under -fno-signed-char. cpplib insists that characters | |
9970 | and character constants are always unsigned. Hence we must convert | |
9971 | back and forth. */ | |
9972 | cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1); | |
9973 | ||
9974 | uc = cpp_host_to_exec_charset (parse_in, uc); | |
9975 | ||
9976 | if (flag_signed_char) | |
9977 | return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE) | |
9978 | >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE); | |
9979 | else | |
9980 | return uc; | |
9981 | } | |
9982 | ||
cf9e9959 EB |
9983 | /* Fold an offsetof-like expression. EXPR is a nested sequence of component |
9984 | references with an INDIRECT_REF of a constant at the bottom; much like the | |
9985 | traditional rendering of offsetof as a macro. Return the folded result. */ | |
ee8a6a3e | 9986 | |
cf9e9959 EB |
9987 | tree |
9988 | fold_offsetof_1 (tree expr) | |
ee8a6a3e | 9989 | { |
ee8a6a3e RH |
9990 | tree base, off, t; |
9991 | ||
9992 | switch (TREE_CODE (expr)) | |
9993 | { | |
9994 | case ERROR_MARK: | |
9995 | return expr; | |
9996 | ||
545b7d8c VR |
9997 | case VAR_DECL: |
9998 | error ("cannot apply %<offsetof%> to static data member %qD", expr); | |
9999 | return error_mark_node; | |
10000 | ||
6d4d7b0e | 10001 | case CALL_EXPR: |
8d5f60ac | 10002 | case TARGET_EXPR: |
6d4d7b0e PB |
10003 | error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded"); |
10004 | return error_mark_node; | |
10005 | ||
6d4d7b0e PB |
10006 | case NOP_EXPR: |
10007 | case INDIRECT_REF: | |
cf9e9959 | 10008 | if (!TREE_CONSTANT (TREE_OPERAND (expr, 0))) |
61c3c490 DS |
10009 | { |
10010 | error ("cannot apply %<offsetof%> to a non constant address"); | |
10011 | return error_mark_node; | |
10012 | } | |
cf9e9959 | 10013 | return TREE_OPERAND (expr, 0); |
6d4d7b0e | 10014 | |
ee8a6a3e | 10015 | case COMPONENT_REF: |
cf9e9959 | 10016 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); |
ee8a6a3e RH |
10017 | if (base == error_mark_node) |
10018 | return base; | |
10019 | ||
10020 | t = TREE_OPERAND (expr, 1); | |
10021 | if (DECL_C_BIT_FIELD (t)) | |
10022 | { | |
10023 | error ("attempt to take address of bit-field structure " | |
c51a1ba9 | 10024 | "member %qD", t); |
ee8a6a3e RH |
10025 | return error_mark_node; |
10026 | } | |
db3927fb | 10027 | off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t), |
386b1f1f | 10028 | size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t)) |
db3927fb | 10029 | / BITS_PER_UNIT)); |
ee8a6a3e RH |
10030 | break; |
10031 | ||
10032 | case ARRAY_REF: | |
cf9e9959 | 10033 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); |
ee8a6a3e RH |
10034 | if (base == error_mark_node) |
10035 | return base; | |
10036 | ||
10037 | t = TREE_OPERAND (expr, 1); | |
61c3c490 DS |
10038 | |
10039 | /* Check if the offset goes beyond the upper bound of the array. */ | |
cf9e9959 | 10040 | if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0) |
d32599a6 JJ |
10041 | { |
10042 | tree upbound = array_ref_up_bound (expr); | |
10043 | if (upbound != NULL_TREE | |
10044 | && TREE_CODE (upbound) == INTEGER_CST | |
10045 | && !tree_int_cst_equal (upbound, | |
10046 | TYPE_MAX_VALUE (TREE_TYPE (upbound)))) | |
10047 | { | |
10048 | upbound = size_binop (PLUS_EXPR, upbound, | |
10049 | build_int_cst (TREE_TYPE (upbound), 1)); | |
10050 | if (tree_int_cst_lt (upbound, t)) | |
10051 | { | |
10052 | tree v; | |
10053 | ||
10054 | for (v = TREE_OPERAND (expr, 0); | |
10055 | TREE_CODE (v) == COMPONENT_REF; | |
10056 | v = TREE_OPERAND (v, 0)) | |
10057 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) | |
10058 | == RECORD_TYPE) | |
10059 | { | |
910ad8de NF |
10060 | tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); |
10061 | for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) | |
d32599a6 JJ |
10062 | if (TREE_CODE (fld_chain) == FIELD_DECL) |
10063 | break; | |
10064 | ||
10065 | if (fld_chain) | |
10066 | break; | |
10067 | } | |
10068 | /* Don't warn if the array might be considered a poor | |
10069 | man's flexible array member with a very permissive | |
10070 | definition thereof. */ | |
10071 | if (TREE_CODE (v) == ARRAY_REF | |
10072 | || TREE_CODE (v) == COMPONENT_REF) | |
10073 | warning (OPT_Warray_bounds, | |
10074 | "index %E denotes an offset " | |
10075 | "greater than size of %qT", | |
10076 | t, TREE_TYPE (TREE_OPERAND (expr, 0))); | |
10077 | } | |
10078 | } | |
10079 | } | |
cf9e9959 EB |
10080 | |
10081 | t = convert (sizetype, t); | |
10082 | off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); | |
ee8a6a3e RH |
10083 | break; |
10084 | ||
1916c916 VR |
10085 | case COMPOUND_EXPR: |
10086 | /* Handle static members of volatile structs. */ | |
10087 | t = TREE_OPERAND (expr, 1); | |
10088 | gcc_assert (TREE_CODE (t) == VAR_DECL); | |
cf9e9959 | 10089 | return fold_offsetof_1 (t); |
1916c916 | 10090 | |
ee8a6a3e | 10091 | default: |
366de0ce | 10092 | gcc_unreachable (); |
ee8a6a3e RH |
10093 | } |
10094 | ||
cf9e9959 | 10095 | return fold_build_pointer_plus (base, off); |
ee8a6a3e RH |
10096 | } |
10097 | ||
cf9e9959 EB |
10098 | /* Likewise, but convert it to the return type of offsetof. */ |
10099 | ||
ee8a6a3e | 10100 | tree |
cf9e9959 | 10101 | fold_offsetof (tree expr) |
ee8a6a3e | 10102 | { |
cf9e9959 | 10103 | return convert (size_type_node, fold_offsetof_1 (expr)); |
ee8a6a3e RH |
10104 | } |
10105 | ||
d166d4c3 AK |
10106 | /* Warn for A ?: C expressions (with B omitted) where A is a boolean |
10107 | expression, because B will always be true. */ | |
10108 | ||
10109 | void | |
10110 | warn_for_omitted_condop (location_t location, tree cond) | |
10111 | { | |
10112 | if (truth_value_p (TREE_CODE (cond))) | |
10113 | warning_at (location, OPT_Wparentheses, | |
10114 | "the omitted middle operand in ?: will always be %<true%>, " | |
10115 | "suggest explicit middle operand"); | |
10116 | } | |
10117 | ||
4816c593 NF |
10118 | /* Give an error for storing into ARG, which is 'const'. USE indicates |
10119 | how ARG was being used. */ | |
10120 | ||
10121 | void | |
c02065fc | 10122 | readonly_error (location_t loc, tree arg, enum lvalue_use use) |
4816c593 NF |
10123 | { |
10124 | gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement | |
10125 | || use == lv_asm); | |
10126 | /* Using this macro rather than (for example) arrays of messages | |
10127 | ensures that all the format strings are checked at compile | |
10128 | time. */ | |
10129 | #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ | |
10130 | : (use == lv_increment ? (I) \ | |
10131 | : (use == lv_decrement ? (D) : (AS)))) | |
10132 | if (TREE_CODE (arg) == COMPONENT_REF) | |
10133 | { | |
10134 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
c02065fc AH |
10135 | error_at (loc, READONLY_MSG (G_("assignment of member " |
10136 | "%qD in read-only object"), | |
10137 | G_("increment of member " | |
10138 | "%qD in read-only object"), | |
10139 | G_("decrement of member " | |
10140 | "%qD in read-only object"), | |
10141 | G_("member %qD in read-only object " | |
10142 | "used as %<asm%> output")), | |
10143 | TREE_OPERAND (arg, 1)); | |
4816c593 | 10144 | else |
c02065fc AH |
10145 | error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"), |
10146 | G_("increment of read-only member %qD"), | |
10147 | G_("decrement of read-only member %qD"), | |
10148 | G_("read-only member %qD used as %<asm%> output")), | |
10149 | TREE_OPERAND (arg, 1)); | |
4816c593 NF |
10150 | } |
10151 | else if (TREE_CODE (arg) == VAR_DECL) | |
c02065fc AH |
10152 | error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"), |
10153 | G_("increment of read-only variable %qD"), | |
10154 | G_("decrement of read-only variable %qD"), | |
10155 | G_("read-only variable %qD used as %<asm%> output")), | |
10156 | arg); | |
4816c593 | 10157 | else if (TREE_CODE (arg) == PARM_DECL) |
c02065fc AH |
10158 | error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"), |
10159 | G_("increment of read-only parameter %qD"), | |
10160 | G_("decrement of read-only parameter %qD"), | |
10161 | G_("read-only parameter %qD use as %<asm%> output")), | |
10162 | arg); | |
4816c593 NF |
10163 | else if (TREE_CODE (arg) == RESULT_DECL) |
10164 | { | |
10165 | gcc_assert (c_dialect_cxx ()); | |
c02065fc AH |
10166 | error_at (loc, READONLY_MSG (G_("assignment of " |
10167 | "read-only named return value %qD"), | |
10168 | G_("increment of " | |
10169 | "read-only named return value %qD"), | |
10170 | G_("decrement of " | |
10171 | "read-only named return value %qD"), | |
10172 | G_("read-only named return value %qD " | |
10173 | "used as %<asm%>output")), | |
10174 | arg); | |
4816c593 NF |
10175 | } |
10176 | else if (TREE_CODE (arg) == FUNCTION_DECL) | |
c02065fc AH |
10177 | error_at (loc, READONLY_MSG (G_("assignment of function %qD"), |
10178 | G_("increment of function %qD"), | |
10179 | G_("decrement of function %qD"), | |
10180 | G_("function %qD used as %<asm%> output")), | |
10181 | arg); | |
4816c593 | 10182 | else |
c02065fc AH |
10183 | error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"), |
10184 | G_("increment of read-only location %qE"), | |
10185 | G_("decrement of read-only location %qE"), | |
10186 | G_("read-only location %qE used as %<asm%> output")), | |
10187 | arg); | |
4816c593 NF |
10188 | } |
10189 | ||
37dc0d8d | 10190 | /* Print an error message for an invalid lvalue. USE says |
7bd11157 TT |
10191 | how the lvalue is being used and so selects the error message. LOC |
10192 | is the location for the error. */ | |
5ae9ba3e | 10193 | |
37dc0d8d | 10194 | void |
7bd11157 | 10195 | lvalue_error (location_t loc, enum lvalue_use use) |
5ae9ba3e | 10196 | { |
37dc0d8d | 10197 | switch (use) |
5ae9ba3e | 10198 | { |
37dc0d8d | 10199 | case lv_assign: |
7bd11157 | 10200 | error_at (loc, "lvalue required as left operand of assignment"); |
37dc0d8d JM |
10201 | break; |
10202 | case lv_increment: | |
7bd11157 | 10203 | error_at (loc, "lvalue required as increment operand"); |
37dc0d8d JM |
10204 | break; |
10205 | case lv_decrement: | |
7bd11157 | 10206 | error_at (loc, "lvalue required as decrement operand"); |
37dc0d8d JM |
10207 | break; |
10208 | case lv_addressof: | |
7bd11157 | 10209 | error_at (loc, "lvalue required as unary %<&%> operand"); |
37dc0d8d JM |
10210 | break; |
10211 | case lv_asm: | |
7bd11157 | 10212 | error_at (loc, "lvalue required in asm statement"); |
37dc0d8d JM |
10213 | break; |
10214 | default: | |
10215 | gcc_unreachable (); | |
5ae9ba3e | 10216 | } |
5ae9ba3e | 10217 | } |
7a6daeb0 NF |
10218 | |
10219 | /* Print an error message for an invalid indirection of type TYPE. | |
10220 | ERRSTRING is the name of the operator for the indirection. */ | |
10221 | ||
10222 | void | |
10223 | invalid_indirection_error (location_t loc, tree type, ref_operator errstring) | |
10224 | { | |
10225 | switch (errstring) | |
10226 | { | |
10227 | case RO_NULL: | |
10228 | gcc_assert (c_dialect_cxx ()); | |
10229 | error_at (loc, "invalid type argument (have %qT)", type); | |
10230 | break; | |
10231 | case RO_ARRAY_INDEXING: | |
10232 | error_at (loc, | |
10233 | "invalid type argument of array indexing (have %qT)", | |
10234 | type); | |
10235 | break; | |
10236 | case RO_UNARY_STAR: | |
10237 | error_at (loc, | |
10238 | "invalid type argument of unary %<*%> (have %qT)", | |
10239 | type); | |
10240 | break; | |
10241 | case RO_ARROW: | |
10242 | error_at (loc, | |
10243 | "invalid type argument of %<->%> (have %qT)", | |
10244 | type); | |
10245 | break; | |
55a7f02f MP |
10246 | case RO_ARROW_STAR: |
10247 | error_at (loc, | |
10248 | "invalid type argument of %<->*%> (have %qT)", | |
10249 | type); | |
10250 | break; | |
7a6daeb0 NF |
10251 | case RO_IMPLICIT_CONVERSION: |
10252 | error_at (loc, | |
10253 | "invalid type argument of implicit conversion (have %qT)", | |
10254 | type); | |
10255 | break; | |
10256 | default: | |
10257 | gcc_unreachable (); | |
10258 | } | |
10259 | } | |
aab038d5 RH |
10260 | \f |
10261 | /* *PTYPE is an incomplete array. Complete it with a domain based on | |
10262 | INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT | |
10263 | is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
10264 | 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ | |
10265 | ||
10266 | int | |
10267 | complete_array_type (tree *ptype, tree initial_value, bool do_default) | |
10268 | { | |
10269 | tree maxindex, type, main_type, elt, unqual_elt; | |
10270 | int failure = 0, quals; | |
06d40de8 | 10271 | hashval_t hashcode = 0; |
40d3d530 | 10272 | bool overflow_p = false; |
aab038d5 RH |
10273 | |
10274 | maxindex = size_zero_node; | |
10275 | if (initial_value) | |
10276 | { | |
10277 | if (TREE_CODE (initial_value) == STRING_CST) | |
10278 | { | |
10279 | int eltsize | |
10280 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
10281 | maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1); | |
10282 | } | |
10283 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) | |
10284 | { | |
9771b263 | 10285 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value); |
aab038d5 | 10286 | |
9771b263 | 10287 | if (vec_safe_is_empty (v)) |
aab038d5 RH |
10288 | { |
10289 | if (pedantic) | |
10290 | failure = 3; | |
830c740f | 10291 | maxindex = ssize_int (-1); |
aab038d5 RH |
10292 | } |
10293 | else | |
10294 | { | |
10295 | tree curindex; | |
4038c495 GB |
10296 | unsigned HOST_WIDE_INT cnt; |
10297 | constructor_elt *ce; | |
cff7525f | 10298 | bool fold_p = false; |
aab038d5 | 10299 | |
9771b263 | 10300 | if ((*v)[0].index) |
40d3d530 JR |
10301 | maxindex = (*v)[0].index, fold_p = true; |
10302 | ||
aab038d5 RH |
10303 | curindex = maxindex; |
10304 | ||
9771b263 | 10305 | for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++) |
aab038d5 | 10306 | { |
cff7525f | 10307 | bool curfold_p = false; |
4038c495 | 10308 | if (ce->index) |
cff7525f | 10309 | curindex = ce->index, curfold_p = true; |
aab038d5 | 10310 | else |
cff7525f JH |
10311 | { |
10312 | if (fold_p) | |
40d3d530 JR |
10313 | { |
10314 | /* Since we treat size types now as ordinary | |
10315 | unsigned types, we need an explicit overflow | |
10316 | check. */ | |
10317 | tree orig = curindex; | |
10318 | curindex = fold_convert (sizetype, curindex); | |
10319 | overflow_p |= tree_int_cst_lt (curindex, orig); | |
10320 | } | |
db3927fb AH |
10321 | curindex = size_binop (PLUS_EXPR, curindex, |
10322 | size_one_node); | |
cff7525f | 10323 | } |
aab038d5 | 10324 | if (tree_int_cst_lt (maxindex, curindex)) |
cff7525f | 10325 | maxindex = curindex, fold_p = curfold_p; |
aab038d5 | 10326 | } |
40d3d530 JR |
10327 | if (fold_p) |
10328 | { | |
10329 | tree orig = maxindex; | |
10330 | maxindex = fold_convert (sizetype, maxindex); | |
10331 | overflow_p |= tree_int_cst_lt (maxindex, orig); | |
10332 | } | |
aab038d5 RH |
10333 | } |
10334 | } | |
10335 | else | |
10336 | { | |
10337 | /* Make an error message unless that happened already. */ | |
10338 | if (initial_value != error_mark_node) | |
10339 | failure = 1; | |
10340 | } | |
10341 | } | |
10342 | else | |
10343 | { | |
10344 | failure = 2; | |
10345 | if (!do_default) | |
10346 | return failure; | |
10347 | } | |
10348 | ||
10349 | type = *ptype; | |
10350 | elt = TREE_TYPE (type); | |
10351 | quals = TYPE_QUALS (strip_array_types (elt)); | |
10352 | if (quals == 0) | |
10353 | unqual_elt = elt; | |
10354 | else | |
36c5e70a | 10355 | unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals)); |
aab038d5 RH |
10356 | |
10357 | /* Using build_distinct_type_copy and modifying things afterward instead | |
10358 | of using build_array_type to create a new type preserves all of the | |
10359 | TYPE_LANG_FLAG_? bits that the front end may have set. */ | |
10360 | main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); | |
10361 | TREE_TYPE (main_type) = unqual_elt; | |
e6313a78 RG |
10362 | TYPE_DOMAIN (main_type) |
10363 | = build_range_type (TREE_TYPE (maxindex), | |
10364 | build_int_cst (TREE_TYPE (maxindex), 0), maxindex); | |
aab038d5 RH |
10365 | layout_type (main_type); |
10366 | ||
06d40de8 DG |
10367 | /* Make sure we have the canonical MAIN_TYPE. */ |
10368 | hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode); | |
b8698a0f | 10369 | hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)), |
06d40de8 DG |
10370 | hashcode); |
10371 | main_type = type_hash_canon (hashcode, main_type); | |
10372 | ||
9ae165a0 DG |
10373 | /* Fix the canonical type. */ |
10374 | if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type)) | |
10375 | || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type))) | |
10376 | SET_TYPE_STRUCTURAL_EQUALITY (main_type); | |
10377 | else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type) | |
10378 | || (TYPE_CANONICAL (TYPE_DOMAIN (main_type)) | |
10379 | != TYPE_DOMAIN (main_type))) | |
b8698a0f | 10380 | TYPE_CANONICAL (main_type) |
9ae165a0 DG |
10381 | = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)), |
10382 | TYPE_CANONICAL (TYPE_DOMAIN (main_type))); | |
10383 | else | |
10384 | TYPE_CANONICAL (main_type) = main_type; | |
10385 | ||
aab038d5 RH |
10386 | if (quals == 0) |
10387 | type = main_type; | |
10388 | else | |
10389 | type = c_build_qualified_type (main_type, quals); | |
10390 | ||
7bfcb402 JM |
10391 | if (COMPLETE_TYPE_P (type) |
10392 | && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST | |
40d3d530 | 10393 | && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))) |
7bfcb402 JM |
10394 | { |
10395 | error ("size of array is too large"); | |
10396 | /* If we proceed with the array type as it is, we'll eventually | |
386b1f1f | 10397 | crash in tree_to_[su]hwi(). */ |
7bfcb402 JM |
10398 | type = error_mark_node; |
10399 | } | |
10400 | ||
aab038d5 RH |
10401 | *ptype = type; |
10402 | return failure; | |
10403 | } | |
5ae9ba3e | 10404 | |
30cd1c5d AS |
10405 | /* Like c_mark_addressable but don't check register qualifier. */ |
10406 | void | |
10407 | c_common_mark_addressable_vec (tree t) | |
10408 | { | |
10409 | while (handled_component_p (t)) | |
10410 | t = TREE_OPERAND (t, 0); | |
10411 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
10412 | return; | |
10413 | TREE_ADDRESSABLE (t) = 1; | |
10414 | } | |
10415 | ||
10416 | ||
48ae6c13 RH |
10417 | \f |
10418 | /* Used to help initialize the builtin-types.def table. When a type of | |
10419 | the correct size doesn't exist, use error_mark_node instead of NULL. | |
10420 | The later results in segfaults even when a decl using the type doesn't | |
10421 | get invoked. */ | |
10422 | ||
10423 | tree | |
10424 | builtin_type_for_size (int size, bool unsignedp) | |
10425 | { | |
21fa2faf | 10426 | tree type = c_common_type_for_size (size, unsignedp); |
48ae6c13 RH |
10427 | return type ? type : error_mark_node; |
10428 | } | |
10429 | ||
10430 | /* A helper function for resolve_overloaded_builtin in resolving the | |
10431 | overloaded __sync_ builtins. Returns a positive power of 2 if the | |
10432 | first operand of PARAMS is a pointer to a supported data type. | |
10433 | Returns 0 if an error is encountered. */ | |
10434 | ||
10435 | static int | |
9771b263 | 10436 | sync_resolve_size (tree function, vec<tree, va_gc> *params) |
48ae6c13 RH |
10437 | { |
10438 | tree type; | |
10439 | int size; | |
10440 | ||
9771b263 | 10441 | if (!params) |
48ae6c13 RH |
10442 | { |
10443 | error ("too few arguments to function %qE", function); | |
10444 | return 0; | |
10445 | } | |
10446 | ||
9771b263 | 10447 | type = TREE_TYPE ((*params)[0]); |
6415bd5d JM |
10448 | if (TREE_CODE (type) == ARRAY_TYPE) |
10449 | { | |
10450 | /* Force array-to-pointer decay for C++. */ | |
10451 | gcc_assert (c_dialect_cxx()); | |
10452 | (*params)[0] = default_conversion ((*params)[0]); | |
10453 | type = TREE_TYPE ((*params)[0]); | |
10454 | } | |
48ae6c13 RH |
10455 | if (TREE_CODE (type) != POINTER_TYPE) |
10456 | goto incompatible; | |
10457 | ||
10458 | type = TREE_TYPE (type); | |
10459 | if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) | |
10460 | goto incompatible; | |
10461 | ||
ae7e9ddd | 10462 | size = tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
a0274e3e | 10463 | if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16) |
48ae6c13 RH |
10464 | return size; |
10465 | ||
10466 | incompatible: | |
10467 | error ("incompatible type for argument %d of %qE", 1, function); | |
10468 | return 0; | |
10469 | } | |
10470 | ||
c22cacf3 | 10471 | /* A helper function for resolve_overloaded_builtin. Adds casts to |
48ae6c13 RH |
10472 | PARAMS to make arguments match up with those of FUNCTION. Drops |
10473 | the variadic arguments at the end. Returns false if some error | |
10474 | was encountered; true on success. */ | |
10475 | ||
10476 | static bool | |
86951993 | 10477 | sync_resolve_params (location_t loc, tree orig_function, tree function, |
9771b263 | 10478 | vec<tree, va_gc> *params, bool orig_format) |
48ae6c13 | 10479 | { |
e19a18d4 | 10480 | function_args_iterator iter; |
48ae6c13 | 10481 | tree ptype; |
bbbbb16a | 10482 | unsigned int parmnum; |
48ae6c13 | 10483 | |
e19a18d4 | 10484 | function_args_iter_init (&iter, TREE_TYPE (function)); |
48ae6c13 RH |
10485 | /* We've declared the implementation functions to use "volatile void *" |
10486 | as the pointer parameter, so we shouldn't get any complaints from the | |
10487 | call to check_function_arguments what ever type the user used. */ | |
e19a18d4 | 10488 | function_args_iter_next (&iter); |
9771b263 | 10489 | ptype = TREE_TYPE (TREE_TYPE ((*params)[0])); |
267bac10 | 10490 | ptype = TYPE_MAIN_VARIANT (ptype); |
48ae6c13 RH |
10491 | |
10492 | /* For the rest of the values, we need to cast these to FTYPE, so that we | |
10493 | don't get warnings for passing pointer types, etc. */ | |
bbbbb16a | 10494 | parmnum = 0; |
e19a18d4 | 10495 | while (1) |
48ae6c13 | 10496 | { |
e19a18d4 NF |
10497 | tree val, arg_type; |
10498 | ||
10499 | arg_type = function_args_iter_cond (&iter); | |
10500 | /* XXX void_type_node belies the abstraction. */ | |
10501 | if (arg_type == void_type_node) | |
10502 | break; | |
48ae6c13 | 10503 | |
bbbbb16a | 10504 | ++parmnum; |
9771b263 | 10505 | if (params->length () <= parmnum) |
48ae6c13 | 10506 | { |
86951993 | 10507 | error_at (loc, "too few arguments to function %qE", orig_function); |
48ae6c13 RH |
10508 | return false; |
10509 | } | |
10510 | ||
e3793c6f JJ |
10511 | /* Only convert parameters if arg_type is unsigned integer type with |
10512 | new format sync routines, i.e. don't attempt to convert pointer | |
10513 | arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n), | |
10514 | bool arguments (e.g. WEAK argument) or signed int arguments (memmodel | |
10515 | kinds). */ | |
10516 | if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type)) | |
86951993 AM |
10517 | { |
10518 | /* Ideally for the first conversion we'd use convert_for_assignment | |
10519 | so that we get warnings for anything that doesn't match the pointer | |
10520 | type. This isn't portable across the C and C++ front ends atm. */ | |
9771b263 | 10521 | val = (*params)[parmnum]; |
86951993 AM |
10522 | val = convert (ptype, val); |
10523 | val = convert (arg_type, val); | |
9771b263 | 10524 | (*params)[parmnum] = val; |
86951993 | 10525 | } |
48ae6c13 | 10526 | |
e19a18d4 | 10527 | function_args_iter_next (&iter); |
48ae6c13 RH |
10528 | } |
10529 | ||
86951993 | 10530 | /* __atomic routines are not variadic. */ |
9771b263 | 10531 | if (!orig_format && params->length () != parmnum + 1) |
86951993 AM |
10532 | { |
10533 | error_at (loc, "too many arguments to function %qE", orig_function); | |
10534 | return false; | |
10535 | } | |
10536 | ||
48ae6c13 RH |
10537 | /* The definition of these primitives is variadic, with the remaining |
10538 | being "an optional list of variables protected by the memory barrier". | |
10539 | No clue what that's supposed to mean, precisely, but we consider all | |
10540 | call-clobbered variables to be protected so we're safe. */ | |
9771b263 | 10541 | params->truncate (parmnum + 1); |
48ae6c13 RH |
10542 | |
10543 | return true; | |
10544 | } | |
10545 | ||
c22cacf3 | 10546 | /* A helper function for resolve_overloaded_builtin. Adds a cast to |
48ae6c13 RH |
10547 | RESULT to make it match the type of the first pointer argument in |
10548 | PARAMS. */ | |
10549 | ||
10550 | static tree | |
86951993 | 10551 | sync_resolve_return (tree first_param, tree result, bool orig_format) |
48ae6c13 | 10552 | { |
bbbbb16a | 10553 | tree ptype = TREE_TYPE (TREE_TYPE (first_param)); |
86951993 | 10554 | tree rtype = TREE_TYPE (result); |
99db1ef0 | 10555 | ptype = TYPE_MAIN_VARIANT (ptype); |
86951993 AM |
10556 | |
10557 | /* New format doesn't require casting unless the types are the same size. */ | |
10558 | if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype))) | |
10559 | return convert (ptype, result); | |
10560 | else | |
10561 | return result; | |
10562 | } | |
10563 | ||
10564 | /* This function verifies the PARAMS to generic atomic FUNCTION. | |
10565 | It returns the size if all the parameters are the same size, otherwise | |
10566 | 0 is returned if the parameters are invalid. */ | |
10567 | ||
10568 | static int | |
9771b263 DN |
10569 | get_atomic_generic_size (location_t loc, tree function, |
10570 | vec<tree, va_gc> *params) | |
86951993 AM |
10571 | { |
10572 | unsigned int n_param; | |
10573 | unsigned int n_model; | |
10574 | unsigned int x; | |
10575 | int size_0; | |
10576 | tree type_0; | |
10577 | ||
10578 | /* Determine the parameter makeup. */ | |
10579 | switch (DECL_FUNCTION_CODE (function)) | |
10580 | { | |
10581 | case BUILT_IN_ATOMIC_EXCHANGE: | |
10582 | n_param = 4; | |
10583 | n_model = 1; | |
10584 | break; | |
10585 | case BUILT_IN_ATOMIC_LOAD: | |
10586 | case BUILT_IN_ATOMIC_STORE: | |
10587 | n_param = 3; | |
10588 | n_model = 1; | |
10589 | break; | |
10590 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
10591 | n_param = 6; | |
10592 | n_model = 2; | |
10593 | break; | |
10594 | default: | |
c466c4ff | 10595 | gcc_unreachable (); |
86951993 AM |
10596 | } |
10597 | ||
9771b263 | 10598 | if (vec_safe_length (params) != n_param) |
86951993 AM |
10599 | { |
10600 | error_at (loc, "incorrect number of arguments to function %qE", function); | |
10601 | return 0; | |
10602 | } | |
10603 | ||
10604 | /* Get type of first parameter, and determine its size. */ | |
9771b263 | 10605 | type_0 = TREE_TYPE ((*params)[0]); |
6415bd5d JM |
10606 | if (TREE_CODE (type_0) == ARRAY_TYPE) |
10607 | { | |
10608 | /* Force array-to-pointer decay for C++. */ | |
10609 | gcc_assert (c_dialect_cxx()); | |
10610 | (*params)[0] = default_conversion ((*params)[0]); | |
10611 | type_0 = TREE_TYPE ((*params)[0]); | |
10612 | } | |
c466c4ff AM |
10613 | if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0))) |
10614 | { | |
10615 | error_at (loc, "argument 1 of %qE must be a non-void pointer type", | |
10616 | function); | |
10617 | return 0; | |
10618 | } | |
10619 | ||
10620 | /* Types must be compile time constant sizes. */ | |
10621 | if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST) | |
86951993 | 10622 | { |
c466c4ff AM |
10623 | error_at (loc, |
10624 | "argument 1 of %qE must be a pointer to a constant size type", | |
10625 | function); | |
86951993 AM |
10626 | return 0; |
10627 | } | |
c466c4ff | 10628 | |
ae7e9ddd | 10629 | size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0))); |
86951993 | 10630 | |
c466c4ff AM |
10631 | /* Zero size objects are not allowed. */ |
10632 | if (size_0 == 0) | |
10633 | { | |
10634 | error_at (loc, | |
10635 | "argument 1 of %qE must be a pointer to a nonzero size object", | |
10636 | function); | |
10637 | return 0; | |
10638 | } | |
10639 | ||
86951993 AM |
10640 | /* Check each other parameter is a pointer and the same size. */ |
10641 | for (x = 0; x < n_param - n_model; x++) | |
10642 | { | |
10643 | int size; | |
9771b263 | 10644 | tree type = TREE_TYPE ((*params)[x]); |
688010ba | 10645 | /* __atomic_compare_exchange has a bool in the 4th position, skip it. */ |
86951993 AM |
10646 | if (n_param == 6 && x == 3) |
10647 | continue; | |
10648 | if (!POINTER_TYPE_P (type)) | |
10649 | { | |
10650 | error_at (loc, "argument %d of %qE must be a pointer type", x + 1, | |
10651 | function); | |
10652 | return 0; | |
10653 | } | |
7b56b2f8 MP |
10654 | tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type)); |
10655 | size = type_size ? tree_to_uhwi (type_size) : 0; | |
86951993 AM |
10656 | if (size != size_0) |
10657 | { | |
10658 | error_at (loc, "size mismatch in argument %d of %qE", x + 1, | |
10659 | function); | |
10660 | return 0; | |
10661 | } | |
10662 | } | |
10663 | ||
10664 | /* Check memory model parameters for validity. */ | |
10665 | for (x = n_param - n_model ; x < n_param; x++) | |
10666 | { | |
9771b263 | 10667 | tree p = (*params)[x]; |
86951993 AM |
10668 | if (TREE_CODE (p) == INTEGER_CST) |
10669 | { | |
ae7e9ddd | 10670 | int i = tree_to_uhwi (p); |
ad7bac31 | 10671 | if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST) |
86951993 AM |
10672 | { |
10673 | warning_at (loc, OPT_Winvalid_memory_model, | |
10674 | "invalid memory model argument %d of %qE", x + 1, | |
10675 | function); | |
86951993 AM |
10676 | } |
10677 | } | |
10678 | else | |
10679 | if (!INTEGRAL_TYPE_P (TREE_TYPE (p))) | |
10680 | { | |
10681 | error_at (loc, "non-integer memory model argument %d of %qE", x + 1, | |
10682 | function); | |
10683 | return 0; | |
10684 | } | |
10685 | } | |
10686 | ||
10687 | return size_0; | |
10688 | } | |
10689 | ||
10690 | ||
10691 | /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N | |
10692 | at the beginning of the parameter list PARAMS representing the size of the | |
10693 | objects. This is to match the library ABI requirement. LOC is the location | |
10694 | of the function call. | |
10695 | The new function is returned if it needed rebuilding, otherwise NULL_TREE is | |
10696 | returned to allow the external call to be constructed. */ | |
10697 | ||
10698 | static tree | |
10699 | add_atomic_size_parameter (unsigned n, location_t loc, tree function, | |
9771b263 | 10700 | vec<tree, va_gc> *params) |
86951993 AM |
10701 | { |
10702 | tree size_node; | |
10703 | ||
10704 | /* Insert a SIZE_T parameter as the first param. If there isn't | |
10705 | enough space, allocate a new vector and recursively re-build with that. */ | |
9771b263 | 10706 | if (!params->space (1)) |
86951993 AM |
10707 | { |
10708 | unsigned int z, len; | |
9771b263 | 10709 | vec<tree, va_gc> *v; |
86951993 AM |
10710 | tree f; |
10711 | ||
9771b263 DN |
10712 | len = params->length (); |
10713 | vec_alloc (v, len + 1); | |
8edbfaa6 | 10714 | v->quick_push (build_int_cst (size_type_node, n)); |
86951993 | 10715 | for (z = 0; z < len; z++) |
9771b263 | 10716 | v->quick_push ((*params)[z]); |
81e5eca8 | 10717 | f = build_function_call_vec (loc, vNULL, function, v, NULL); |
9771b263 | 10718 | vec_free (v); |
86951993 AM |
10719 | return f; |
10720 | } | |
10721 | ||
10722 | /* Add the size parameter and leave as a function call for processing. */ | |
10723 | size_node = build_int_cst (size_type_node, n); | |
9771b263 | 10724 | params->quick_insert (0, size_node); |
86951993 AM |
10725 | return NULL_TREE; |
10726 | } | |
10727 | ||
10728 | ||
6b28e197 JM |
10729 | /* Return whether atomic operations for naturally aligned N-byte |
10730 | arguments are supported, whether inline or through libatomic. */ | |
10731 | static bool | |
10732 | atomic_size_supported_p (int n) | |
10733 | { | |
10734 | switch (n) | |
10735 | { | |
10736 | case 1: | |
10737 | case 2: | |
10738 | case 4: | |
10739 | case 8: | |
10740 | return true; | |
10741 | ||
10742 | case 16: | |
10743 | return targetm.scalar_mode_supported_p (TImode); | |
10744 | ||
10745 | default: | |
10746 | return false; | |
10747 | } | |
10748 | } | |
10749 | ||
86951993 AM |
10750 | /* This will process an __atomic_exchange function call, determine whether it |
10751 | needs to be mapped to the _N variation, or turned into a library call. | |
10752 | LOC is the location of the builtin call. | |
10753 | FUNCTION is the DECL that has been invoked; | |
10754 | PARAMS is the argument list for the call. The return value is non-null | |
10755 | TRUE is returned if it is translated into the proper format for a call to the | |
10756 | external library, and NEW_RETURN is set the tree for that function. | |
10757 | FALSE is returned if processing for the _N variation is required, and | |
10758 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10759 | static bool | |
10760 | resolve_overloaded_atomic_exchange (location_t loc, tree function, | |
9771b263 | 10761 | vec<tree, va_gc> *params, tree *new_return) |
86951993 AM |
10762 | { |
10763 | tree p0, p1, p2, p3; | |
10764 | tree I_type, I_type_ptr; | |
10765 | int n = get_atomic_generic_size (loc, function, params); | |
10766 | ||
c466c4ff AM |
10767 | /* Size of 0 is an error condition. */ |
10768 | if (n == 0) | |
10769 | { | |
10770 | *new_return = error_mark_node; | |
10771 | return true; | |
10772 | } | |
10773 | ||
86951993 | 10774 | /* If not a lock-free size, change to the library generic format. */ |
6b28e197 | 10775 | if (!atomic_size_supported_p (n)) |
86951993 AM |
10776 | { |
10777 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10778 | return true; | |
10779 | } | |
10780 | ||
10781 | /* Otherwise there is a lockfree match, transform the call from: | |
10782 | void fn(T* mem, T* desired, T* return, model) | |
10783 | into | |
10784 | *return = (T) (fn (In* mem, (In) *desired, model)) */ | |
10785 | ||
9771b263 DN |
10786 | p0 = (*params)[0]; |
10787 | p1 = (*params)[1]; | |
10788 | p2 = (*params)[2]; | |
10789 | p3 = (*params)[3]; | |
86951993 AM |
10790 | |
10791 | /* Create pointer to appropriate size. */ | |
10792 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10793 | I_type_ptr = build_pointer_type (I_type); | |
10794 | ||
10795 | /* Convert object pointer to required type. */ | |
10796 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
9771b263 | 10797 | (*params)[0] = p0; |
86951993 AM |
10798 | /* Convert new value to required type, and dereference it. */ |
10799 | p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10800 | p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); | |
9771b263 | 10801 | (*params)[1] = p1; |
86951993 AM |
10802 | |
10803 | /* Move memory model to the 3rd position, and end param list. */ | |
9771b263 DN |
10804 | (*params)[2] = p3; |
10805 | params->truncate (3); | |
86951993 AM |
10806 | |
10807 | /* Convert return pointer and dereference it for later assignment. */ | |
10808 | *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR); | |
10809 | ||
10810 | return false; | |
48ae6c13 RH |
10811 | } |
10812 | ||
86951993 AM |
10813 | |
10814 | /* This will process an __atomic_compare_exchange function call, determine | |
10815 | whether it needs to be mapped to the _N variation, or turned into a lib call. | |
10816 | LOC is the location of the builtin call. | |
10817 | FUNCTION is the DECL that has been invoked; | |
10818 | PARAMS is the argument list for the call. The return value is non-null | |
10819 | TRUE is returned if it is translated into the proper format for a call to the | |
10820 | external library, and NEW_RETURN is set the tree for that function. | |
10821 | FALSE is returned if processing for the _N variation is required. */ | |
10822 | ||
10823 | static bool | |
10824 | resolve_overloaded_atomic_compare_exchange (location_t loc, tree function, | |
9771b263 | 10825 | vec<tree, va_gc> *params, |
86951993 AM |
10826 | tree *new_return) |
10827 | { | |
10828 | tree p0, p1, p2; | |
10829 | tree I_type, I_type_ptr; | |
10830 | int n = get_atomic_generic_size (loc, function, params); | |
10831 | ||
c466c4ff AM |
10832 | /* Size of 0 is an error condition. */ |
10833 | if (n == 0) | |
10834 | { | |
10835 | *new_return = error_mark_node; | |
10836 | return true; | |
10837 | } | |
10838 | ||
86951993 | 10839 | /* If not a lock-free size, change to the library generic format. */ |
6b28e197 | 10840 | if (!atomic_size_supported_p (n)) |
86951993 AM |
10841 | { |
10842 | /* The library generic format does not have the weak parameter, so | |
10843 | remove it from the param list. Since a parameter has been removed, | |
10844 | we can be sure that there is room for the SIZE_T parameter, meaning | |
10845 | there will not be a recursive rebuilding of the parameter list, so | |
10846 | there is no danger this will be done twice. */ | |
10847 | if (n > 0) | |
10848 | { | |
9771b263 DN |
10849 | (*params)[3] = (*params)[4]; |
10850 | (*params)[4] = (*params)[5]; | |
10851 | params->truncate (5); | |
86951993 AM |
10852 | } |
10853 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10854 | return true; | |
10855 | } | |
10856 | ||
10857 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10858 | bool fn(T* mem, T* desired, T* return, weak, success, failure) | |
10859 | into | |
10860 | bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */ | |
10861 | ||
9771b263 DN |
10862 | p0 = (*params)[0]; |
10863 | p1 = (*params)[1]; | |
10864 | p2 = (*params)[2]; | |
86951993 AM |
10865 | |
10866 | /* Create pointer to appropriate size. */ | |
10867 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10868 | I_type_ptr = build_pointer_type (I_type); | |
10869 | ||
10870 | /* Convert object pointer to required type. */ | |
10871 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
9771b263 | 10872 | (*params)[0] = p0; |
86951993 AM |
10873 | |
10874 | /* Convert expected pointer to required type. */ | |
10875 | p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1); | |
9771b263 | 10876 | (*params)[1] = p1; |
86951993 AM |
10877 | |
10878 | /* Convert desired value to required type, and dereference it. */ | |
10879 | p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR); | |
10880 | p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2); | |
9771b263 | 10881 | (*params)[2] = p2; |
86951993 AM |
10882 | |
10883 | /* The rest of the parameters are fine. NULL means no special return value | |
10884 | processing.*/ | |
10885 | *new_return = NULL; | |
10886 | return false; | |
10887 | } | |
10888 | ||
10889 | ||
10890 | /* This will process an __atomic_load function call, determine whether it | |
10891 | needs to be mapped to the _N variation, or turned into a library call. | |
10892 | LOC is the location of the builtin call. | |
10893 | FUNCTION is the DECL that has been invoked; | |
10894 | PARAMS is the argument list for the call. The return value is non-null | |
10895 | TRUE is returned if it is translated into the proper format for a call to the | |
10896 | external library, and NEW_RETURN is set the tree for that function. | |
10897 | FALSE is returned if processing for the _N variation is required, and | |
10898 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10899 | ||
10900 | static bool | |
10901 | resolve_overloaded_atomic_load (location_t loc, tree function, | |
9771b263 | 10902 | vec<tree, va_gc> *params, tree *new_return) |
86951993 AM |
10903 | { |
10904 | tree p0, p1, p2; | |
10905 | tree I_type, I_type_ptr; | |
10906 | int n = get_atomic_generic_size (loc, function, params); | |
10907 | ||
c466c4ff AM |
10908 | /* Size of 0 is an error condition. */ |
10909 | if (n == 0) | |
10910 | { | |
10911 | *new_return = error_mark_node; | |
10912 | return true; | |
10913 | } | |
10914 | ||
86951993 | 10915 | /* If not a lock-free size, change to the library generic format. */ |
6b28e197 | 10916 | if (!atomic_size_supported_p (n)) |
86951993 AM |
10917 | { |
10918 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10919 | return true; | |
10920 | } | |
10921 | ||
10922 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10923 | void fn(T* mem, T* return, model) | |
10924 | into | |
10925 | *return = (T) (fn ((In *) mem, model)) */ | |
10926 | ||
9771b263 DN |
10927 | p0 = (*params)[0]; |
10928 | p1 = (*params)[1]; | |
10929 | p2 = (*params)[2]; | |
86951993 AM |
10930 | |
10931 | /* Create pointer to appropriate size. */ | |
10932 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10933 | I_type_ptr = build_pointer_type (I_type); | |
10934 | ||
10935 | /* Convert object pointer to required type. */ | |
10936 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
9771b263 | 10937 | (*params)[0] = p0; |
86951993 AM |
10938 | |
10939 | /* Move memory model to the 2nd position, and end param list. */ | |
9771b263 DN |
10940 | (*params)[1] = p2; |
10941 | params->truncate (2); | |
86951993 AM |
10942 | |
10943 | /* Convert return pointer and dereference it for later assignment. */ | |
10944 | *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10945 | ||
10946 | return false; | |
10947 | } | |
10948 | ||
10949 | ||
10950 | /* This will process an __atomic_store function call, determine whether it | |
10951 | needs to be mapped to the _N variation, or turned into a library call. | |
10952 | LOC is the location of the builtin call. | |
10953 | FUNCTION is the DECL that has been invoked; | |
10954 | PARAMS is the argument list for the call. The return value is non-null | |
10955 | TRUE is returned if it is translated into the proper format for a call to the | |
10956 | external library, and NEW_RETURN is set the tree for that function. | |
10957 | FALSE is returned if processing for the _N variation is required, and | |
10958 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10959 | ||
10960 | static bool | |
10961 | resolve_overloaded_atomic_store (location_t loc, tree function, | |
9771b263 | 10962 | vec<tree, va_gc> *params, tree *new_return) |
86951993 AM |
10963 | { |
10964 | tree p0, p1; | |
10965 | tree I_type, I_type_ptr; | |
10966 | int n = get_atomic_generic_size (loc, function, params); | |
10967 | ||
c466c4ff AM |
10968 | /* Size of 0 is an error condition. */ |
10969 | if (n == 0) | |
10970 | { | |
10971 | *new_return = error_mark_node; | |
10972 | return true; | |
10973 | } | |
10974 | ||
86951993 | 10975 | /* If not a lock-free size, change to the library generic format. */ |
6b28e197 | 10976 | if (!atomic_size_supported_p (n)) |
86951993 AM |
10977 | { |
10978 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10979 | return true; | |
10980 | } | |
10981 | ||
10982 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10983 | void fn(T* mem, T* value, model) | |
10984 | into | |
10985 | fn ((In *) mem, (In) *value, model) */ | |
10986 | ||
9771b263 DN |
10987 | p0 = (*params)[0]; |
10988 | p1 = (*params)[1]; | |
86951993 AM |
10989 | |
10990 | /* Create pointer to appropriate size. */ | |
10991 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10992 | I_type_ptr = build_pointer_type (I_type); | |
10993 | ||
10994 | /* Convert object pointer to required type. */ | |
10995 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
9771b263 | 10996 | (*params)[0] = p0; |
86951993 AM |
10997 | |
10998 | /* Convert new value to required type, and dereference it. */ | |
10999 | p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
11000 | p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); | |
9771b263 | 11001 | (*params)[1] = p1; |
86951993 AM |
11002 | |
11003 | /* The memory model is in the right spot already. Return is void. */ | |
11004 | *new_return = NULL_TREE; | |
11005 | ||
11006 | return false; | |
11007 | } | |
11008 | ||
11009 | ||
48ae6c13 RH |
11010 | /* Some builtin functions are placeholders for other expressions. This |
11011 | function should be called immediately after parsing the call expression | |
11012 | before surrounding code has committed to the type of the expression. | |
11013 | ||
c2255bc4 AH |
11014 | LOC is the location of the builtin call. |
11015 | ||
48ae6c13 RH |
11016 | FUNCTION is the DECL that has been invoked; it is known to be a builtin. |
11017 | PARAMS is the argument list for the call. The return value is non-null | |
11018 | when expansion is complete, and null if normal processing should | |
11019 | continue. */ | |
11020 | ||
11021 | tree | |
9771b263 DN |
11022 | resolve_overloaded_builtin (location_t loc, tree function, |
11023 | vec<tree, va_gc> *params) | |
48ae6c13 RH |
11024 | { |
11025 | enum built_in_function orig_code = DECL_FUNCTION_CODE (function); | |
86951993 AM |
11026 | bool orig_format = true; |
11027 | tree new_return = NULL_TREE; | |
11028 | ||
58646b77 PB |
11029 | switch (DECL_BUILT_IN_CLASS (function)) |
11030 | { | |
11031 | case BUILT_IN_NORMAL: | |
11032 | break; | |
11033 | case BUILT_IN_MD: | |
11034 | if (targetm.resolve_overloaded_builtin) | |
c2255bc4 | 11035 | return targetm.resolve_overloaded_builtin (loc, function, params); |
58646b77 | 11036 | else |
c22cacf3 | 11037 | return NULL_TREE; |
58646b77 PB |
11038 | default: |
11039 | return NULL_TREE; | |
11040 | } | |
c22cacf3 | 11041 | |
58646b77 | 11042 | /* Handle BUILT_IN_NORMAL here. */ |
48ae6c13 RH |
11043 | switch (orig_code) |
11044 | { | |
86951993 AM |
11045 | case BUILT_IN_ATOMIC_EXCHANGE: |
11046 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
11047 | case BUILT_IN_ATOMIC_LOAD: | |
11048 | case BUILT_IN_ATOMIC_STORE: | |
11049 | { | |
11050 | /* Handle these 4 together so that they can fall through to the next | |
11051 | case if the call is transformed to an _N variant. */ | |
11052 | switch (orig_code) | |
11053 | { | |
11054 | case BUILT_IN_ATOMIC_EXCHANGE: | |
11055 | { | |
11056 | if (resolve_overloaded_atomic_exchange (loc, function, params, | |
11057 | &new_return)) | |
11058 | return new_return; | |
11059 | /* Change to the _N variant. */ | |
11060 | orig_code = BUILT_IN_ATOMIC_EXCHANGE_N; | |
11061 | break; | |
11062 | } | |
11063 | ||
11064 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
11065 | { | |
11066 | if (resolve_overloaded_atomic_compare_exchange (loc, function, | |
11067 | params, | |
11068 | &new_return)) | |
11069 | return new_return; | |
11070 | /* Change to the _N variant. */ | |
11071 | orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N; | |
11072 | break; | |
11073 | } | |
11074 | case BUILT_IN_ATOMIC_LOAD: | |
11075 | { | |
11076 | if (resolve_overloaded_atomic_load (loc, function, params, | |
11077 | &new_return)) | |
11078 | return new_return; | |
11079 | /* Change to the _N variant. */ | |
11080 | orig_code = BUILT_IN_ATOMIC_LOAD_N; | |
11081 | break; | |
11082 | } | |
11083 | case BUILT_IN_ATOMIC_STORE: | |
11084 | { | |
11085 | if (resolve_overloaded_atomic_store (loc, function, params, | |
11086 | &new_return)) | |
11087 | return new_return; | |
11088 | /* Change to the _N variant. */ | |
11089 | orig_code = BUILT_IN_ATOMIC_STORE_N; | |
11090 | break; | |
11091 | } | |
11092 | default: | |
11093 | gcc_unreachable (); | |
11094 | } | |
11095 | /* Fallthrough to the normal processing. */ | |
11096 | } | |
11097 | case BUILT_IN_ATOMIC_EXCHANGE_N: | |
11098 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N: | |
11099 | case BUILT_IN_ATOMIC_LOAD_N: | |
11100 | case BUILT_IN_ATOMIC_STORE_N: | |
11101 | case BUILT_IN_ATOMIC_ADD_FETCH_N: | |
11102 | case BUILT_IN_ATOMIC_SUB_FETCH_N: | |
11103 | case BUILT_IN_ATOMIC_AND_FETCH_N: | |
11104 | case BUILT_IN_ATOMIC_NAND_FETCH_N: | |
11105 | case BUILT_IN_ATOMIC_XOR_FETCH_N: | |
11106 | case BUILT_IN_ATOMIC_OR_FETCH_N: | |
11107 | case BUILT_IN_ATOMIC_FETCH_ADD_N: | |
11108 | case BUILT_IN_ATOMIC_FETCH_SUB_N: | |
11109 | case BUILT_IN_ATOMIC_FETCH_AND_N: | |
11110 | case BUILT_IN_ATOMIC_FETCH_NAND_N: | |
11111 | case BUILT_IN_ATOMIC_FETCH_XOR_N: | |
11112 | case BUILT_IN_ATOMIC_FETCH_OR_N: | |
11113 | { | |
11114 | orig_format = false; | |
11115 | /* Fallthru for parameter processing. */ | |
11116 | } | |
e0a8ecf2 AM |
11117 | case BUILT_IN_SYNC_FETCH_AND_ADD_N: |
11118 | case BUILT_IN_SYNC_FETCH_AND_SUB_N: | |
11119 | case BUILT_IN_SYNC_FETCH_AND_OR_N: | |
11120 | case BUILT_IN_SYNC_FETCH_AND_AND_N: | |
11121 | case BUILT_IN_SYNC_FETCH_AND_XOR_N: | |
11122 | case BUILT_IN_SYNC_FETCH_AND_NAND_N: | |
11123 | case BUILT_IN_SYNC_ADD_AND_FETCH_N: | |
11124 | case BUILT_IN_SYNC_SUB_AND_FETCH_N: | |
11125 | case BUILT_IN_SYNC_OR_AND_FETCH_N: | |
11126 | case BUILT_IN_SYNC_AND_AND_FETCH_N: | |
11127 | case BUILT_IN_SYNC_XOR_AND_FETCH_N: | |
11128 | case BUILT_IN_SYNC_NAND_AND_FETCH_N: | |
11129 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N: | |
11130 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N: | |
11131 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N: | |
11132 | case BUILT_IN_SYNC_LOCK_RELEASE_N: | |
48ae6c13 RH |
11133 | { |
11134 | int n = sync_resolve_size (function, params); | |
bbbbb16a | 11135 | tree new_function, first_param, result; |
e79983f4 | 11136 | enum built_in_function fncode; |
48ae6c13 RH |
11137 | |
11138 | if (n == 0) | |
11139 | return error_mark_node; | |
11140 | ||
e79983f4 MM |
11141 | fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1); |
11142 | new_function = builtin_decl_explicit (fncode); | |
86951993 AM |
11143 | if (!sync_resolve_params (loc, function, new_function, params, |
11144 | orig_format)) | |
48ae6c13 RH |
11145 | return error_mark_node; |
11146 | ||
9771b263 | 11147 | first_param = (*params)[0]; |
81e5eca8 MP |
11148 | result = build_function_call_vec (loc, vNULL, new_function, params, |
11149 | NULL); | |
86951993 AM |
11150 | if (result == error_mark_node) |
11151 | return result; | |
e0a8ecf2 | 11152 | if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N |
86951993 AM |
11153 | && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N |
11154 | && orig_code != BUILT_IN_ATOMIC_STORE_N) | |
11155 | result = sync_resolve_return (first_param, result, orig_format); | |
48ae6c13 | 11156 | |
86951993 AM |
11157 | /* If new_return is set, assign function to that expr and cast the |
11158 | result to void since the generic interface returned void. */ | |
11159 | if (new_return) | |
11160 | { | |
11161 | /* Cast function result from I{1,2,4,8,16} to the required type. */ | |
11162 | result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result); | |
11163 | result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return, | |
11164 | result); | |
11165 | TREE_SIDE_EFFECTS (result) = 1; | |
11166 | protected_set_expr_location (result, loc); | |
11167 | result = convert (void_type_node, result); | |
11168 | } | |
48ae6c13 RH |
11169 | return result; |
11170 | } | |
11171 | ||
11172 | default: | |
58646b77 | 11173 | return NULL_TREE; |
48ae6c13 RH |
11174 | } |
11175 | } | |
11176 | ||
0af94e6f JR |
11177 | /* vector_types_compatible_elements_p is used in type checks of vectors |
11178 | values used as operands of binary operators. Where it returns true, and | |
11179 | the other checks of the caller succeed (being vector types in he first | |
11180 | place, and matching number of elements), we can just treat the types | |
11181 | as essentially the same. | |
11182 | Contrast with vector_targets_convertible_p, which is used for vector | |
11183 | pointer types, and vector_types_convertible_p, which will allow | |
11184 | language-specific matches under the control of flag_lax_vector_conversions, | |
11185 | and might still require a conversion. */ | |
11186 | /* True if vector types T1 and T2 can be inputs to the same binary | |
11187 | operator without conversion. | |
11188 | We don't check the overall vector size here because some of our callers | |
11189 | want to give different error messages when the vectors are compatible | |
11190 | except for the element count. */ | |
11191 | ||
5bed876a | 11192 | bool |
0af94e6f | 11193 | vector_types_compatible_elements_p (tree t1, tree t2) |
5bed876a | 11194 | { |
0af94e6f JR |
11195 | bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2); |
11196 | t1 = TREE_TYPE (t1); | |
11197 | t2 = TREE_TYPE (t2); | |
11198 | ||
5bed876a AH |
11199 | enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); |
11200 | ||
ab22c1fa CF |
11201 | gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE) |
11202 | && (c2 == INTEGER_TYPE || c2 == REAL_TYPE | |
11203 | || c2 == FIXED_POINT_TYPE)); | |
5bed876a | 11204 | |
0af94e6f JR |
11205 | t1 = c_common_signed_type (t1); |
11206 | t2 = c_common_signed_type (t2); | |
5bed876a AH |
11207 | /* Equality works here because c_common_signed_type uses |
11208 | TYPE_MAIN_VARIANT. */ | |
0af94e6f JR |
11209 | if (t1 == t2) |
11210 | return true; | |
11211 | if (opaque && c1 == c2 | |
11212 | && (c1 == INTEGER_TYPE || c1 == REAL_TYPE) | |
11213 | && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) | |
11214 | return true; | |
11215 | return false; | |
5bed876a AH |
11216 | } |
11217 | ||
104f8784 KG |
11218 | /* Check for missing format attributes on function pointers. LTYPE is |
11219 | the new type or left-hand side type. RTYPE is the old type or | |
11220 | right-hand side type. Returns TRUE if LTYPE is missing the desired | |
11221 | attribute. */ | |
11222 | ||
11223 | bool | |
11224 | check_missing_format_attribute (tree ltype, tree rtype) | |
11225 | { | |
11226 | tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype); | |
11227 | tree ra; | |
11228 | ||
11229 | for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra)) | |
11230 | if (is_attribute_p ("format", TREE_PURPOSE (ra))) | |
11231 | break; | |
11232 | if (ra) | |
11233 | { | |
11234 | tree la; | |
11235 | for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la)) | |
11236 | if (is_attribute_p ("format", TREE_PURPOSE (la))) | |
11237 | break; | |
11238 | return !la; | |
11239 | } | |
11240 | else | |
11241 | return false; | |
11242 | } | |
11243 | ||
ff6b6641 GDR |
11244 | /* Subscripting with type char is likely to lose on a machine where |
11245 | chars are signed. So warn on any machine, but optionally. Don't | |
11246 | warn for unsigned char since that type is safe. Don't warn for | |
11247 | signed char because anyone who uses that must have done so | |
11248 | deliberately. Furthermore, we reduce the false positive load by | |
11249 | warning only for non-constant value of type char. */ | |
11250 | ||
11251 | void | |
5bd012f8 | 11252 | warn_array_subscript_with_type_char (location_t loc, tree index) |
ff6b6641 GDR |
11253 | { |
11254 | if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node | |
11255 | && TREE_CODE (index) != INTEGER_CST) | |
5bd012f8 MP |
11256 | warning_at (loc, OPT_Wchar_subscripts, |
11257 | "array subscript has type %<char%>"); | |
ff6b6641 GDR |
11258 | } |
11259 | ||
2a67bec2 ILT |
11260 | /* Implement -Wparentheses for the unexpected C precedence rules, to |
11261 | cover cases like x + y << z which readers are likely to | |
11262 | misinterpret. We have seen an expression in which CODE is a binary | |
100d537d MLI |
11263 | operator used to combine expressions ARG_LEFT and ARG_RIGHT, which |
11264 | before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and | |
11265 | CODE_RIGHT may be ERROR_MARK, which means that that side of the | |
11266 | expression was not formed using a binary or unary operator, or it | |
11267 | was enclosed in parentheses. */ | |
2a67bec2 ILT |
11268 | |
11269 | void | |
5d9de0d0 | 11270 | warn_about_parentheses (location_t loc, enum tree_code code, |
fb3e178a | 11271 | enum tree_code code_left, tree arg_left, |
100d537d | 11272 | enum tree_code code_right, tree arg_right) |
2a67bec2 ILT |
11273 | { |
11274 | if (!warn_parentheses) | |
11275 | return; | |
11276 | ||
100d537d MLI |
11277 | /* This macro tests that the expression ARG with original tree code |
11278 | CODE appears to be a boolean expression. or the result of folding a | |
11279 | boolean expression. */ | |
11280 | #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ | |
11281 | (truth_value_p (TREE_CODE (ARG)) \ | |
11282 | || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ | |
11283 | /* Folding may create 0 or 1 integers from other expressions. */ \ | |
11284 | || ((CODE) != INTEGER_CST \ | |
11285 | && (integer_onep (ARG) || integer_zerop (ARG)))) | |
11286 | ||
b8698a0f | 11287 | switch (code) |
2a67bec2 | 11288 | { |
100d537d | 11289 | case LSHIFT_EXPR: |
5d9de0d0 PC |
11290 | if (code_left == PLUS_EXPR) |
11291 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11292 | "suggest parentheses around %<+%> inside %<<<%>"); | |
11293 | else if (code_right == PLUS_EXPR) | |
11294 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11295 | "suggest parentheses around %<+%> inside %<<<%>"); | |
11296 | else if (code_left == MINUS_EXPR) | |
11297 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11298 | "suggest parentheses around %<-%> inside %<<<%>"); | |
11299 | else if (code_right == MINUS_EXPR) | |
11300 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11301 | "suggest parentheses around %<-%> inside %<<<%>"); | |
100d537d | 11302 | return; |
2a67bec2 | 11303 | |
100d537d | 11304 | case RSHIFT_EXPR: |
5d9de0d0 PC |
11305 | if (code_left == PLUS_EXPR) |
11306 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11307 | "suggest parentheses around %<+%> inside %<>>%>"); | |
11308 | else if (code_right == PLUS_EXPR) | |
11309 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11310 | "suggest parentheses around %<+%> inside %<>>%>"); | |
11311 | else if (code_left == MINUS_EXPR) | |
11312 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11313 | "suggest parentheses around %<-%> inside %<>>%>"); | |
11314 | else if (code_right == MINUS_EXPR) | |
11315 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11316 | "suggest parentheses around %<-%> inside %<>>%>"); | |
100d537d | 11317 | return; |
2a67bec2 | 11318 | |
100d537d | 11319 | case TRUTH_ORIF_EXPR: |
5d9de0d0 PC |
11320 | if (code_left == TRUTH_ANDIF_EXPR) |
11321 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11322 | "suggest parentheses around %<&&%> within %<||%>"); | |
11323 | else if (code_right == TRUTH_ANDIF_EXPR) | |
11324 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11325 | "suggest parentheses around %<&&%> within %<||%>"); | |
100d537d MLI |
11326 | return; |
11327 | ||
11328 | case BIT_IOR_EXPR: | |
2a67bec2 | 11329 | if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR |
5d9de0d0 PC |
11330 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
11331 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11332 | "suggest parentheses around arithmetic in operand of %<|%>"); | |
11333 | else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR | |
11334 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
11335 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
100d537d | 11336 | "suggest parentheses around arithmetic in operand of %<|%>"); |
2a67bec2 | 11337 | /* Check cases like x|y==z */ |
5d9de0d0 PC |
11338 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11339 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11340 | "suggest parentheses around comparison in operand of %<|%>"); | |
11341 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11342 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
100d537d MLI |
11343 | "suggest parentheses around comparison in operand of %<|%>"); |
11344 | /* Check cases like !x | y */ | |
11345 | else if (code_left == TRUTH_NOT_EXPR | |
11346 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
5d9de0d0 PC |
11347 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11348 | "suggest parentheses around operand of " | |
11349 | "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>"); | |
100d537d | 11350 | return; |
2a67bec2 | 11351 | |
100d537d | 11352 | case BIT_XOR_EXPR: |
2a67bec2 | 11353 | if (code_left == BIT_AND_EXPR |
5d9de0d0 PC |
11354 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
11355 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11356 | "suggest parentheses around arithmetic in operand of %<^%>"); | |
11357 | else if (code_right == BIT_AND_EXPR | |
11358 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
11359 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
100d537d | 11360 | "suggest parentheses around arithmetic in operand of %<^%>"); |
2a67bec2 | 11361 | /* Check cases like x^y==z */ |
5d9de0d0 PC |
11362 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11363 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11364 | "suggest parentheses around comparison in operand of %<^%>"); | |
11365 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11366 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
100d537d MLI |
11367 | "suggest parentheses around comparison in operand of %<^%>"); |
11368 | return; | |
2a67bec2 | 11369 | |
100d537d | 11370 | case BIT_AND_EXPR: |
5d9de0d0 PC |
11371 | if (code_left == PLUS_EXPR) |
11372 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11373 | "suggest parentheses around %<+%> in operand of %<&%>"); | |
11374 | else if (code_right == PLUS_EXPR) | |
11375 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
100d537d | 11376 | "suggest parentheses around %<+%> in operand of %<&%>"); |
5d9de0d0 PC |
11377 | else if (code_left == MINUS_EXPR) |
11378 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11379 | "suggest parentheses around %<-%> in operand of %<&%>"); | |
11380 | else if (code_right == MINUS_EXPR) | |
11381 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
100d537d | 11382 | "suggest parentheses around %<-%> in operand of %<&%>"); |
2a67bec2 | 11383 | /* Check cases like x&y==z */ |
5d9de0d0 PC |
11384 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11385 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11386 | "suggest parentheses around comparison in operand of %<&%>"); | |
11387 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11388 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
100d537d MLI |
11389 | "suggest parentheses around comparison in operand of %<&%>"); |
11390 | /* Check cases like !x & y */ | |
11391 | else if (code_left == TRUTH_NOT_EXPR | |
11392 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
5d9de0d0 PC |
11393 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11394 | "suggest parentheses around operand of " | |
11395 | "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>"); | |
100d537d | 11396 | return; |
2a67bec2 | 11397 | |
100d537d | 11398 | case EQ_EXPR: |
5d9de0d0 PC |
11399 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11400 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11401 | "suggest parentheses around comparison in operand of %<==%>"); | |
11402 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11403 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
100d537d MLI |
11404 | "suggest parentheses around comparison in operand of %<==%>"); |
11405 | return; | |
11406 | case NE_EXPR: | |
5d9de0d0 PC |
11407 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
11408 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
11409 | "suggest parentheses around comparison in operand of %<!=%>"); | |
11410 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
11411 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
100d537d MLI |
11412 | "suggest parentheses around comparison in operand of %<!=%>"); |
11413 | return; | |
11414 | ||
11415 | default: | |
5d9de0d0 PC |
11416 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
11417 | { | |
11418 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
fb3e178a JJ |
11419 | && code_left != NE_EXPR && code_left != EQ_EXPR |
11420 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) | |
5d9de0d0 PC |
11421 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
11422 | "comparisons like %<X<=Y<=Z%> do not " | |
11423 | "have their mathematical meaning"); | |
11424 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison | |
fb3e178a | 11425 | && code_right != NE_EXPR && code_right != EQ_EXPR |
5d9de0d0 PC |
11426 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))) |
11427 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
11428 | "comparisons like %<X<=Y<=Z%> do not " | |
11429 | "have their mathematical meaning"); | |
11430 | } | |
100d537d | 11431 | return; |
e7917d06 | 11432 | } |
100d537d | 11433 | #undef NOT_A_BOOLEAN_EXPR_P |
2a67bec2 ILT |
11434 | } |
11435 | ||
c616e51b MLI |
11436 | /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ |
11437 | ||
11438 | void | |
11439 | warn_for_unused_label (tree label) | |
11440 | { | |
11441 | if (!TREE_USED (label)) | |
11442 | { | |
11443 | if (DECL_INITIAL (label)) | |
11444 | warning (OPT_Wunused_label, "label %q+D defined but not used", label); | |
11445 | else | |
11446 | warning (OPT_Wunused_label, "label %q+D declared but not defined", label); | |
11447 | } | |
11448 | } | |
ff6b6641 | 11449 | |
c9f9eb5d AH |
11450 | /* Warn for division by zero according to the value of DIVISOR. LOC |
11451 | is the location of the division operator. */ | |
2e9cb75e MLI |
11452 | |
11453 | void | |
c9f9eb5d | 11454 | warn_for_div_by_zero (location_t loc, tree divisor) |
2e9cb75e | 11455 | { |
ab22c1fa CF |
11456 | /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning |
11457 | about division by zero. Do not issue a warning if DIVISOR has a | |
2e9cb75e MLI |
11458 | floating-point type, since we consider 0.0/0.0 a valid way of |
11459 | generating a NaN. */ | |
7d882b83 | 11460 | if (c_inhibit_evaluation_warnings == 0 |
ab22c1fa | 11461 | && (integer_zerop (divisor) || fixed_zerop (divisor))) |
c9f9eb5d | 11462 | warning_at (loc, OPT_Wdiv_by_zero, "division by zero"); |
2e9cb75e MLI |
11463 | } |
11464 | ||
2d12797c MLI |
11465 | /* Subroutine of build_binary_op. Give warnings for comparisons |
11466 | between signed and unsigned quantities that may fail. Do the | |
11467 | checking based on the original operand trees ORIG_OP0 and ORIG_OP1, | |
11468 | so that casts will be considered, but default promotions won't | |
ba47d38d AH |
11469 | be. |
11470 | ||
11471 | LOCATION is the location of the comparison operator. | |
2d12797c MLI |
11472 | |
11473 | The arguments of this function map directly to local variables | |
11474 | of build_binary_op. */ | |
11475 | ||
b8698a0f | 11476 | void |
ba47d38d | 11477 | warn_for_sign_compare (location_t location, |
b8698a0f L |
11478 | tree orig_op0, tree orig_op1, |
11479 | tree op0, tree op1, | |
2d12797c MLI |
11480 | tree result_type, enum tree_code resultcode) |
11481 | { | |
11482 | int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); | |
11483 | int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); | |
11484 | int unsignedp0, unsignedp1; | |
b8698a0f | 11485 | |
2d12797c MLI |
11486 | /* In C++, check for comparison of different enum types. */ |
11487 | if (c_dialect_cxx() | |
11488 | && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE | |
11489 | && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE | |
11490 | && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) | |
ead51d36 | 11491 | != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) |
2d12797c | 11492 | { |
ba47d38d AH |
11493 | warning_at (location, |
11494 | OPT_Wsign_compare, "comparison between types %qT and %qT", | |
11495 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); | |
2d12797c MLI |
11496 | } |
11497 | ||
11498 | /* Do not warn if the comparison is being done in a signed type, | |
11499 | since the signed type will only be chosen if it can represent | |
11500 | all the values of the unsigned type. */ | |
11501 | if (!TYPE_UNSIGNED (result_type)) | |
11502 | /* OK */; | |
11503 | /* Do not warn if both operands are unsigned. */ | |
11504 | else if (op0_signed == op1_signed) | |
11505 | /* OK */; | |
11506 | else | |
11507 | { | |
ead51d36 | 11508 | tree sop, uop, base_type; |
2d12797c | 11509 | bool ovf; |
ead51d36 | 11510 | |
2d12797c MLI |
11511 | if (op0_signed) |
11512 | sop = orig_op0, uop = orig_op1; | |
b8698a0f | 11513 | else |
2d12797c MLI |
11514 | sop = orig_op1, uop = orig_op0; |
11515 | ||
b8698a0f | 11516 | STRIP_TYPE_NOPS (sop); |
2d12797c | 11517 | STRIP_TYPE_NOPS (uop); |
ead51d36 JJ |
11518 | base_type = (TREE_CODE (result_type) == COMPLEX_TYPE |
11519 | ? TREE_TYPE (result_type) : result_type); | |
2d12797c MLI |
11520 | |
11521 | /* Do not warn if the signed quantity is an unsuffixed integer | |
11522 | literal (or some static constant expression involving such | |
11523 | literals or a conditional expression involving such literals) | |
11524 | and it is non-negative. */ | |
11525 | if (tree_expr_nonnegative_warnv_p (sop, &ovf)) | |
11526 | /* OK */; | |
11527 | /* Do not warn if the comparison is an equality operation, the | |
11528 | unsigned quantity is an integral constant, and it would fit | |
11529 | in the result if the result were signed. */ | |
11530 | else if (TREE_CODE (uop) == INTEGER_CST | |
11531 | && (resultcode == EQ_EXPR || resultcode == NE_EXPR) | |
ead51d36 | 11532 | && int_fits_type_p (uop, c_common_signed_type (base_type))) |
2d12797c MLI |
11533 | /* OK */; |
11534 | /* In C, do not warn if the unsigned quantity is an enumeration | |
11535 | constant and its maximum value would fit in the result if the | |
11536 | result were signed. */ | |
11537 | else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST | |
11538 | && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE | |
11539 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), | |
ead51d36 | 11540 | c_common_signed_type (base_type))) |
2d12797c | 11541 | /* OK */; |
b8698a0f | 11542 | else |
ba47d38d | 11543 | warning_at (location, |
b8698a0f | 11544 | OPT_Wsign_compare, |
ba47d38d | 11545 | "comparison between signed and unsigned integer expressions"); |
2d12797c | 11546 | } |
b8698a0f | 11547 | |
2d12797c MLI |
11548 | /* Warn if two unsigned values are being compared in a size larger |
11549 | than their original size, and one (and only one) is the result of | |
11550 | a `~' operator. This comparison will always fail. | |
b8698a0f | 11551 | |
2d12797c MLI |
11552 | Also warn if one operand is a constant, and the constant does not |
11553 | have all bits set that are set in the ~ operand when it is | |
11554 | extended. */ | |
11555 | ||
828fb3ba JM |
11556 | op0 = c_common_get_narrower (op0, &unsignedp0); |
11557 | op1 = c_common_get_narrower (op1, &unsignedp1); | |
b8698a0f | 11558 | |
2d12797c MLI |
11559 | if ((TREE_CODE (op0) == BIT_NOT_EXPR) |
11560 | ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) | |
11561 | { | |
11562 | if (TREE_CODE (op0) == BIT_NOT_EXPR) | |
828fb3ba | 11563 | op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); |
2d12797c | 11564 | if (TREE_CODE (op1) == BIT_NOT_EXPR) |
828fb3ba | 11565 | op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); |
2d12797c | 11566 | |
9541ffee | 11567 | if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1)) |
2d12797c MLI |
11568 | { |
11569 | tree primop; | |
11570 | HOST_WIDE_INT constant, mask; | |
11571 | int unsignedp; | |
11572 | unsigned int bits; | |
b8698a0f | 11573 | |
9541ffee | 11574 | if (tree_fits_shwi_p (op0)) |
2d12797c MLI |
11575 | { |
11576 | primop = op1; | |
11577 | unsignedp = unsignedp1; | |
9439e9a1 | 11578 | constant = tree_to_shwi (op0); |
2d12797c MLI |
11579 | } |
11580 | else | |
11581 | { | |
11582 | primop = op0; | |
11583 | unsignedp = unsignedp0; | |
9439e9a1 | 11584 | constant = tree_to_shwi (op1); |
2d12797c | 11585 | } |
b8698a0f | 11586 | |
2d12797c MLI |
11587 | bits = TYPE_PRECISION (TREE_TYPE (primop)); |
11588 | if (bits < TYPE_PRECISION (result_type) | |
11589 | && bits < HOST_BITS_PER_LONG && unsignedp) | |
11590 | { | |
11591 | mask = (~ (HOST_WIDE_INT) 0) << bits; | |
11592 | if ((mask & constant) != mask) | |
11593 | { | |
11594 | if (constant == 0) | |
0a756a3f MP |
11595 | warning_at (location, OPT_Wsign_compare, |
11596 | "promoted ~unsigned is always non-zero"); | |
2d12797c | 11597 | else |
b8698a0f | 11598 | warning_at (location, OPT_Wsign_compare, |
ba47d38d | 11599 | "comparison of promoted ~unsigned with constant"); |
2d12797c MLI |
11600 | } |
11601 | } | |
11602 | } | |
11603 | else if (unsignedp0 && unsignedp1 | |
11604 | && (TYPE_PRECISION (TREE_TYPE (op0)) | |
11605 | < TYPE_PRECISION (result_type)) | |
11606 | && (TYPE_PRECISION (TREE_TYPE (op1)) | |
11607 | < TYPE_PRECISION (result_type))) | |
ba47d38d | 11608 | warning_at (location, OPT_Wsign_compare, |
2d12797c MLI |
11609 | "comparison of promoted ~unsigned with unsigned"); |
11610 | } | |
11611 | } | |
11612 | ||
c5ee1358 MM |
11613 | /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common |
11614 | type via c_common_type. If -Wdouble-promotion is in use, and the | |
11615 | conditions for warning have been met, issue a warning. GMSGID is | |
11616 | the warning message. It must have two %T specifiers for the type | |
11617 | that was converted (generally "float") and the type to which it was | |
11618 | converted (generally "double), respectively. LOC is the location | |
11619 | to which the awrning should refer. */ | |
11620 | ||
11621 | void | |
11622 | do_warn_double_promotion (tree result_type, tree type1, tree type2, | |
11623 | const char *gmsgid, location_t loc) | |
11624 | { | |
11625 | tree source_type; | |
11626 | ||
11627 | if (!warn_double_promotion) | |
11628 | return; | |
11629 | /* If the conversion will not occur at run-time, there is no need to | |
11630 | warn about it. */ | |
11631 | if (c_inhibit_evaluation_warnings) | |
11632 | return; | |
11633 | if (TYPE_MAIN_VARIANT (result_type) != double_type_node | |
11634 | && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) | |
11635 | return; | |
11636 | if (TYPE_MAIN_VARIANT (type1) == float_type_node | |
11637 | || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) | |
11638 | source_type = type1; | |
11639 | else if (TYPE_MAIN_VARIANT (type2) == float_type_node | |
11640 | || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) | |
11641 | source_type = type2; | |
11642 | else | |
11643 | return; | |
11644 | warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); | |
11645 | } | |
11646 | ||
d0940d56 DS |
11647 | /* Setup a TYPE_DECL node as a typedef representation. |
11648 | ||
11649 | X is a TYPE_DECL for a typedef statement. Create a brand new | |
11650 | ..._TYPE node (which will be just a variant of the existing | |
11651 | ..._TYPE node with identical properties) and then install X | |
11652 | as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. | |
11653 | ||
11654 | The whole point here is to end up with a situation where each | |
11655 | and every ..._TYPE node the compiler creates will be uniquely | |
11656 | associated with AT MOST one node representing a typedef name. | |
11657 | This way, even though the compiler substitutes corresponding | |
11658 | ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very | |
11659 | early on, later parts of the compiler can always do the reverse | |
11660 | translation and get back the corresponding typedef name. For | |
11661 | example, given: | |
11662 | ||
11663 | typedef struct S MY_TYPE; | |
11664 | MY_TYPE object; | |
11665 | ||
11666 | Later parts of the compiler might only know that `object' was of | |
11667 | type `struct S' if it were not for code just below. With this | |
11668 | code however, later parts of the compiler see something like: | |
11669 | ||
11670 | struct S' == struct S | |
11671 | typedef struct S' MY_TYPE; | |
11672 | struct S' object; | |
11673 | ||
11674 | And they can then deduce (from the node for type struct S') that | |
11675 | the original object declaration was: | |
11676 | ||
11677 | MY_TYPE object; | |
11678 | ||
11679 | Being able to do this is important for proper support of protoize, | |
11680 | and also for generating precise symbolic debugging information | |
11681 | which takes full account of the programmer's (typedef) vocabulary. | |
11682 | ||
11683 | Obviously, we don't want to generate a duplicate ..._TYPE node if | |
11684 | the TYPE_DECL node that we are now processing really represents a | |
11685 | standard built-in type. */ | |
11686 | ||
11687 | void | |
11688 | set_underlying_type (tree x) | |
11689 | { | |
11690 | if (x == error_mark_node) | |
11691 | return; | |
11692 | if (DECL_IS_BUILTIN (x)) | |
11693 | { | |
11694 | if (TYPE_NAME (TREE_TYPE (x)) == 0) | |
11695 | TYPE_NAME (TREE_TYPE (x)) = x; | |
11696 | } | |
11697 | else if (TREE_TYPE (x) != error_mark_node | |
11698 | && DECL_ORIGINAL_TYPE (x) == NULL_TREE) | |
11699 | { | |
11700 | tree tt = TREE_TYPE (x); | |
11701 | DECL_ORIGINAL_TYPE (x) = tt; | |
11702 | tt = build_variant_type_copy (tt); | |
11703 | TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
11704 | TYPE_NAME (tt) = x; | |
11705 | TREE_USED (tt) = TREE_USED (x); | |
11706 | TREE_TYPE (x) = tt; | |
11707 | } | |
11708 | } | |
11709 | ||
b646ba3f DS |
11710 | /* Record the types used by the current global variable declaration |
11711 | being parsed, so that we can decide later to emit their debug info. | |
11712 | Those types are in types_used_by_cur_var_decl, and we are going to | |
11713 | store them in the types_used_by_vars_hash hash table. | |
11714 | DECL is the declaration of the global variable that has been parsed. */ | |
11715 | ||
11716 | void | |
11717 | record_types_used_by_current_var_decl (tree decl) | |
11718 | { | |
11719 | gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl)); | |
11720 | ||
9771b263 | 11721 | while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ()) |
b646ba3f | 11722 | { |
9771b263 | 11723 | tree type = types_used_by_cur_var_decl->pop (); |
bc87224e | 11724 | types_used_by_var_decl_insert (type, decl); |
b646ba3f DS |
11725 | } |
11726 | } | |
11727 | ||
3797cb21 DS |
11728 | /* If DECL is a typedef that is declared in the current function, |
11729 | record it for the purpose of -Wunused-local-typedefs. */ | |
11730 | ||
11731 | void | |
11732 | record_locally_defined_typedef (tree decl) | |
11733 | { | |
11734 | struct c_language_function *l; | |
11735 | ||
11736 | if (!warn_unused_local_typedefs | |
11737 | || cfun == NULL | |
11738 | /* if this is not a locally defined typedef then we are not | |
11739 | interested. */ | |
11740 | || !is_typedef_decl (decl) | |
11741 | || !decl_function_context (decl)) | |
11742 | return; | |
11743 | ||
11744 | l = (struct c_language_function *) cfun->language; | |
9771b263 | 11745 | vec_safe_push (l->local_typedefs, decl); |
3797cb21 DS |
11746 | } |
11747 | ||
11748 | /* If T is a TYPE_DECL declared locally, mark it as used. */ | |
11749 | ||
11750 | void | |
11751 | maybe_record_typedef_use (tree t) | |
11752 | { | |
11753 | if (!is_typedef_decl (t)) | |
11754 | return; | |
11755 | ||
11756 | TREE_USED (t) = true; | |
11757 | } | |
11758 | ||
11759 | /* Warn if there are some unused locally defined typedefs in the | |
11760 | current function. */ | |
11761 | ||
11762 | void | |
11763 | maybe_warn_unused_local_typedefs (void) | |
11764 | { | |
11765 | int i; | |
11766 | tree decl; | |
11767 | /* The number of times we have emitted -Wunused-local-typedefs | |
11768 | warnings. If this is different from errorcount, that means some | |
11769 | unrelated errors have been issued. In which case, we'll avoid | |
11770 | emitting "unused-local-typedefs" warnings. */ | |
11771 | static int unused_local_typedefs_warn_count; | |
11772 | struct c_language_function *l; | |
11773 | ||
11774 | if (cfun == NULL) | |
11775 | return; | |
11776 | ||
11777 | if ((l = (struct c_language_function *) cfun->language) == NULL) | |
11778 | return; | |
11779 | ||
11780 | if (warn_unused_local_typedefs | |
11781 | && errorcount == unused_local_typedefs_warn_count) | |
11782 | { | |
9771b263 | 11783 | FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl) |
3797cb21 DS |
11784 | if (!TREE_USED (decl)) |
11785 | warning_at (DECL_SOURCE_LOCATION (decl), | |
11786 | OPT_Wunused_local_typedefs, | |
11787 | "typedef %qD locally defined but not used", decl); | |
11788 | unused_local_typedefs_warn_count = errorcount; | |
11789 | } | |
11790 | ||
9771b263 | 11791 | vec_free (l->local_typedefs); |
3797cb21 DS |
11792 | } |
11793 | ||
04159acf MP |
11794 | /* Warn about boolean expression compared with an integer value different |
11795 | from true/false. Warns also e.g. about "(i1 == i2) == 2". | |
11796 | LOC is the location of the comparison, CODE is its code, OP0 and OP1 | |
11797 | are the operands of the comparison. The caller must ensure that | |
11798 | either operand is a boolean expression. */ | |
11799 | ||
11800 | void | |
11801 | maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0, | |
11802 | tree op1) | |
11803 | { | |
11804 | if (TREE_CODE_CLASS (code) != tcc_comparison) | |
11805 | return; | |
11806 | ||
11807 | tree cst = (TREE_CODE (op0) == INTEGER_CST) | |
11808 | ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE; | |
11809 | if (!cst) | |
11810 | return; | |
11811 | ||
11812 | if (!integer_zerop (cst) && !integer_onep (cst)) | |
11813 | { | |
11814 | int sign = (TREE_CODE (op0) == INTEGER_CST) | |
11815 | ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst); | |
11816 | if (code == EQ_EXPR | |
11817 | || ((code == GT_EXPR || code == GE_EXPR) && sign < 0) | |
11818 | || ((code == LT_EXPR || code == LE_EXPR) && sign > 0)) | |
11819 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " | |
11820 | "with boolean expression is always false", cst); | |
11821 | else | |
11822 | warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE " | |
11823 | "with boolean expression is always true", cst); | |
11824 | } | |
11825 | } | |
11826 | ||
c166b898 ILT |
11827 | /* The C and C++ parsers both use vectors to hold function arguments. |
11828 | For efficiency, we keep a cache of unused vectors. This is the | |
11829 | cache. */ | |
11830 | ||
9771b263 DN |
11831 | typedef vec<tree, va_gc> *tree_gc_vec; |
11832 | static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache; | |
c166b898 ILT |
11833 | |
11834 | /* Return a new vector from the cache. If the cache is empty, | |
11835 | allocate a new vector. These vectors are GC'ed, so it is OK if the | |
11836 | pointer is not released.. */ | |
11837 | ||
9771b263 | 11838 | vec<tree, va_gc> * |
c166b898 ILT |
11839 | make_tree_vector (void) |
11840 | { | |
9771b263 DN |
11841 | if (tree_vector_cache && !tree_vector_cache->is_empty ()) |
11842 | return tree_vector_cache->pop (); | |
c166b898 ILT |
11843 | else |
11844 | { | |
9771b263 | 11845 | /* Passing 0 to vec::alloc returns NULL, and our callers require |
c166b898 ILT |
11846 | that we always return a non-NULL value. The vector code uses |
11847 | 4 when growing a NULL vector, so we do too. */ | |
9771b263 DN |
11848 | vec<tree, va_gc> *v; |
11849 | vec_alloc (v, 4); | |
11850 | return v; | |
c166b898 ILT |
11851 | } |
11852 | } | |
11853 | ||
11854 | /* Release a vector of trees back to the cache. */ | |
11855 | ||
11856 | void | |
9771b263 | 11857 | release_tree_vector (vec<tree, va_gc> *vec) |
c166b898 ILT |
11858 | { |
11859 | if (vec != NULL) | |
11860 | { | |
9771b263 DN |
11861 | vec->truncate (0); |
11862 | vec_safe_push (tree_vector_cache, vec); | |
c166b898 ILT |
11863 | } |
11864 | } | |
11865 | ||
11866 | /* Get a new tree vector holding a single tree. */ | |
11867 | ||
9771b263 | 11868 | vec<tree, va_gc> * |
c166b898 ILT |
11869 | make_tree_vector_single (tree t) |
11870 | { | |
9771b263 DN |
11871 | vec<tree, va_gc> *ret = make_tree_vector (); |
11872 | ret->quick_push (t); | |
c166b898 ILT |
11873 | return ret; |
11874 | } | |
11875 | ||
c12ff9d8 JM |
11876 | /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */ |
11877 | ||
9771b263 | 11878 | vec<tree, va_gc> * |
c12ff9d8 JM |
11879 | make_tree_vector_from_list (tree list) |
11880 | { | |
9771b263 | 11881 | vec<tree, va_gc> *ret = make_tree_vector (); |
c12ff9d8 | 11882 | for (; list; list = TREE_CHAIN (list)) |
9771b263 | 11883 | vec_safe_push (ret, TREE_VALUE (list)); |
c12ff9d8 JM |
11884 | return ret; |
11885 | } | |
11886 | ||
c166b898 ILT |
11887 | /* Get a new tree vector which is a copy of an existing one. */ |
11888 | ||
9771b263 DN |
11889 | vec<tree, va_gc> * |
11890 | make_tree_vector_copy (const vec<tree, va_gc> *orig) | |
c166b898 | 11891 | { |
9771b263 | 11892 | vec<tree, va_gc> *ret; |
c166b898 ILT |
11893 | unsigned int ix; |
11894 | tree t; | |
11895 | ||
11896 | ret = make_tree_vector (); | |
9771b263 DN |
11897 | vec_safe_reserve (ret, vec_safe_length (orig)); |
11898 | FOR_EACH_VEC_SAFE_ELT (orig, ix, t) | |
11899 | ret->quick_push (t); | |
c166b898 ILT |
11900 | return ret; |
11901 | } | |
11902 | ||
a9aa2c3a NF |
11903 | /* Return true if KEYWORD starts a type specifier. */ |
11904 | ||
11905 | bool | |
11906 | keyword_begins_type_specifier (enum rid keyword) | |
11907 | { | |
11908 | switch (keyword) | |
11909 | { | |
38b7bc7f | 11910 | case RID_AUTO_TYPE: |
a9aa2c3a NF |
11911 | case RID_INT: |
11912 | case RID_CHAR: | |
11913 | case RID_FLOAT: | |
11914 | case RID_DOUBLE: | |
11915 | case RID_VOID: | |
a9aa2c3a NF |
11916 | case RID_UNSIGNED: |
11917 | case RID_LONG: | |
11918 | case RID_SHORT: | |
11919 | case RID_SIGNED: | |
11920 | case RID_DFLOAT32: | |
11921 | case RID_DFLOAT64: | |
11922 | case RID_DFLOAT128: | |
11923 | case RID_FRACT: | |
11924 | case RID_ACCUM: | |
11925 | case RID_BOOL: | |
11926 | case RID_WCHAR: | |
11927 | case RID_CHAR16: | |
11928 | case RID_CHAR32: | |
11929 | case RID_SAT: | |
11930 | case RID_COMPLEX: | |
11931 | case RID_TYPEOF: | |
11932 | case RID_STRUCT: | |
11933 | case RID_CLASS: | |
11934 | case RID_UNION: | |
11935 | case RID_ENUM: | |
11936 | return true; | |
11937 | default: | |
78a7c317 DD |
11938 | if (keyword >= RID_FIRST_INT_N |
11939 | && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS | |
11940 | && int_n_enabled_p[keyword-RID_FIRST_INT_N]) | |
11941 | return true; | |
a9aa2c3a NF |
11942 | return false; |
11943 | } | |
11944 | } | |
11945 | ||
11946 | /* Return true if KEYWORD names a type qualifier. */ | |
11947 | ||
11948 | bool | |
11949 | keyword_is_type_qualifier (enum rid keyword) | |
11950 | { | |
11951 | switch (keyword) | |
11952 | { | |
11953 | case RID_CONST: | |
11954 | case RID_VOLATILE: | |
11955 | case RID_RESTRICT: | |
267bac10 | 11956 | case RID_ATOMIC: |
a9aa2c3a NF |
11957 | return true; |
11958 | default: | |
11959 | return false; | |
11960 | } | |
11961 | } | |
11962 | ||
11963 | /* Return true if KEYWORD names a storage class specifier. | |
11964 | ||
11965 | RID_TYPEDEF is not included in this list despite `typedef' being | |
11966 | listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as | |
11967 | such for syntactic convenience only. */ | |
11968 | ||
11969 | bool | |
11970 | keyword_is_storage_class_specifier (enum rid keyword) | |
11971 | { | |
11972 | switch (keyword) | |
11973 | { | |
11974 | case RID_STATIC: | |
11975 | case RID_EXTERN: | |
11976 | case RID_REGISTER: | |
11977 | case RID_AUTO: | |
11978 | case RID_MUTABLE: | |
11979 | case RID_THREAD: | |
11980 | return true; | |
11981 | default: | |
11982 | return false; | |
11983 | } | |
11984 | } | |
11985 | ||
ba9e6dd5 NF |
11986 | /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */ |
11987 | ||
11988 | static bool | |
11989 | keyword_is_function_specifier (enum rid keyword) | |
11990 | { | |
11991 | switch (keyword) | |
11992 | { | |
11993 | case RID_INLINE: | |
bbceee64 | 11994 | case RID_NORETURN: |
ba9e6dd5 NF |
11995 | case RID_VIRTUAL: |
11996 | case RID_EXPLICIT: | |
11997 | return true; | |
11998 | default: | |
11999 | return false; | |
12000 | } | |
12001 | } | |
12002 | ||
12003 | /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a | |
12004 | declaration-specifier (C99 6.7). */ | |
12005 | ||
12006 | bool | |
12007 | keyword_is_decl_specifier (enum rid keyword) | |
12008 | { | |
12009 | if (keyword_is_storage_class_specifier (keyword) | |
12010 | || keyword_is_type_qualifier (keyword) | |
12011 | || keyword_is_function_specifier (keyword)) | |
12012 | return true; | |
12013 | ||
12014 | switch (keyword) | |
12015 | { | |
12016 | case RID_TYPEDEF: | |
12017 | case RID_FRIEND: | |
12018 | case RID_CONSTEXPR: | |
12019 | return true; | |
12020 | default: | |
12021 | return false; | |
12022 | } | |
12023 | } | |
12024 | ||
81f653d6 NF |
12025 | /* Initialize language-specific-bits of tree_contains_struct. */ |
12026 | ||
12027 | void | |
12028 | c_common_init_ts (void) | |
12029 | { | |
12030 | MARK_TS_TYPED (C_MAYBE_CONST_EXPR); | |
12031 | MARK_TS_TYPED (EXCESS_PRECISION_EXPR); | |
36536d79 | 12032 | MARK_TS_TYPED (ARRAY_NOTATION_REF); |
81f653d6 NF |
12033 | } |
12034 | ||
3ce4f9e4 ESR |
12035 | /* Build a user-defined numeric literal out of an integer constant type VALUE |
12036 | with identifier SUFFIX. */ | |
12037 | ||
12038 | tree | |
2d7aa578 ESR |
12039 | build_userdef_literal (tree suffix_id, tree value, |
12040 | enum overflow_type overflow, tree num_string) | |
3ce4f9e4 ESR |
12041 | { |
12042 | tree literal = make_node (USERDEF_LITERAL); | |
12043 | USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id; | |
12044 | USERDEF_LITERAL_VALUE (literal) = value; | |
2d7aa578 | 12045 | USERDEF_LITERAL_OVERFLOW (literal) = overflow; |
3ce4f9e4 ESR |
12046 | USERDEF_LITERAL_NUM_STRING (literal) = num_string; |
12047 | return literal; | |
12048 | } | |
12049 | ||
7edaa4d2 | 12050 | /* For vector[index], convert the vector to a |
aa7da51a JJ |
12051 | pointer of the underlying type. Return true if the resulting |
12052 | ARRAY_REF should not be an lvalue. */ | |
12053 | ||
12054 | bool | |
7edaa4d2 | 12055 | convert_vector_to_pointer_for_subscript (location_t loc, |
aa7da51a | 12056 | tree *vecp, tree index) |
7edaa4d2 | 12057 | { |
aa7da51a | 12058 | bool ret = false; |
7edaa4d2 MG |
12059 | if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE) |
12060 | { | |
12061 | tree type = TREE_TYPE (*vecp); | |
12062 | tree type1; | |
12063 | ||
aa7da51a | 12064 | ret = !lvalue_p (*vecp); |
7edaa4d2 | 12065 | if (TREE_CODE (index) == INTEGER_CST) |
cc269bb6 | 12066 | if (!tree_fits_uhwi_p (index) |
7d362f6c | 12067 | || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type)) |
7edaa4d2 MG |
12068 | warning_at (loc, OPT_Warray_bounds, "index value is out of bound"); |
12069 | ||
aa7da51a JJ |
12070 | if (ret) |
12071 | { | |
b731b390 | 12072 | tree tmp = create_tmp_var_raw (type); |
aa7da51a JJ |
12073 | DECL_SOURCE_LOCATION (tmp) = loc; |
12074 | *vecp = c_save_expr (*vecp); | |
12075 | if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR) | |
12076 | { | |
12077 | bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp); | |
12078 | *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp); | |
12079 | *vecp | |
12080 | = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp, | |
12081 | *vecp, NULL_TREE, NULL_TREE), | |
12082 | non_const); | |
12083 | } | |
12084 | else | |
12085 | *vecp = build4 (TARGET_EXPR, type, tmp, *vecp, | |
12086 | NULL_TREE, NULL_TREE); | |
12087 | SET_EXPR_LOCATION (*vecp, loc); | |
12088 | c_common_mark_addressable_vec (tmp); | |
12089 | } | |
12090 | else | |
12091 | c_common_mark_addressable_vec (*vecp); | |
7edaa4d2 | 12092 | type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); |
7edaa4d2 | 12093 | type1 = build_pointer_type (TREE_TYPE (*vecp)); |
b0f1bf36 RB |
12094 | bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1); |
12095 | if (!ref_all | |
12096 | && !DECL_P (*vecp)) | |
12097 | { | |
12098 | /* If the original vector isn't declared may_alias and it | |
12099 | isn't a bare vector look if the subscripting would | |
12100 | alias the vector we subscript, and if not, force ref-all. */ | |
12101 | alias_set_type vecset = get_alias_set (*vecp); | |
12102 | alias_set_type sset = get_alias_set (type); | |
12103 | if (!alias_sets_must_conflict_p (sset, vecset) | |
12104 | && !alias_set_subset_of (sset, vecset)) | |
12105 | ref_all = true; | |
12106 | } | |
12107 | type = build_pointer_type_for_mode (type, ptr_mode, ref_all); | |
7edaa4d2 MG |
12108 | *vecp = build1 (ADDR_EXPR, type1, *vecp); |
12109 | *vecp = convert (type, *vecp); | |
12110 | } | |
aa7da51a | 12111 | return ret; |
7edaa4d2 MG |
12112 | } |
12113 | ||
a212e43f MG |
12114 | /* Determine which of the operands, if any, is a scalar that needs to be |
12115 | converted to a vector, for the range of operations. */ | |
12116 | enum stv_conv | |
12117 | scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1, | |
12118 | bool complain) | |
12119 | { | |
12120 | tree type0 = TREE_TYPE (op0); | |
12121 | tree type1 = TREE_TYPE (op1); | |
12122 | bool integer_only_op = false; | |
12123 | enum stv_conv ret = stv_firstarg; | |
12124 | ||
12125 | gcc_assert (TREE_CODE (type0) == VECTOR_TYPE | |
12126 | || TREE_CODE (type1) == VECTOR_TYPE); | |
12127 | switch (code) | |
12128 | { | |
12129 | /* Most GENERIC binary expressions require homogeneous arguments. | |
12130 | LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first | |
12131 | argument that is a vector and a second one that is a scalar, so | |
12132 | we never return stv_secondarg for them. */ | |
12133 | case RSHIFT_EXPR: | |
12134 | case LSHIFT_EXPR: | |
12135 | if (TREE_CODE (type0) == INTEGER_TYPE | |
12136 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
12137 | { | |
68fca595 | 12138 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
a212e43f MG |
12139 | { |
12140 | if (complain) | |
12141 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
12142 | "involves truncation", type0, type1); | |
12143 | return stv_error; | |
12144 | } | |
12145 | else | |
12146 | return stv_firstarg; | |
12147 | } | |
12148 | break; | |
12149 | ||
12150 | case BIT_IOR_EXPR: | |
12151 | case BIT_XOR_EXPR: | |
12152 | case BIT_AND_EXPR: | |
12153 | integer_only_op = true; | |
12154 | /* ... fall through ... */ | |
12155 | ||
93100c6b MG |
12156 | case VEC_COND_EXPR: |
12157 | ||
a212e43f MG |
12158 | case PLUS_EXPR: |
12159 | case MINUS_EXPR: | |
12160 | case MULT_EXPR: | |
12161 | case TRUNC_DIV_EXPR: | |
12162 | case CEIL_DIV_EXPR: | |
12163 | case FLOOR_DIV_EXPR: | |
12164 | case ROUND_DIV_EXPR: | |
12165 | case EXACT_DIV_EXPR: | |
12166 | case TRUNC_MOD_EXPR: | |
12167 | case FLOOR_MOD_EXPR: | |
12168 | case RDIV_EXPR: | |
12169 | case EQ_EXPR: | |
12170 | case NE_EXPR: | |
12171 | case LE_EXPR: | |
12172 | case GE_EXPR: | |
12173 | case LT_EXPR: | |
12174 | case GT_EXPR: | |
12175 | /* What about UNLT_EXPR? */ | |
12176 | if (TREE_CODE (type0) == VECTOR_TYPE) | |
12177 | { | |
12178 | tree tmp; | |
12179 | ret = stv_secondarg; | |
12180 | /* Swap TYPE0 with TYPE1 and OP0 with OP1 */ | |
12181 | tmp = type0; type0 = type1; type1 = tmp; | |
12182 | tmp = op0; op0 = op1; op1 = tmp; | |
12183 | } | |
12184 | ||
12185 | if (TREE_CODE (type0) == INTEGER_TYPE | |
12186 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
12187 | { | |
68fca595 | 12188 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
a212e43f MG |
12189 | { |
12190 | if (complain) | |
12191 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
12192 | "involves truncation", type0, type1); | |
12193 | return stv_error; | |
12194 | } | |
12195 | return ret; | |
12196 | } | |
12197 | else if (!integer_only_op | |
12198 | /* Allow integer --> real conversion if safe. */ | |
12199 | && (TREE_CODE (type0) == REAL_TYPE | |
12200 | || TREE_CODE (type0) == INTEGER_TYPE) | |
12201 | && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1))) | |
12202 | { | |
68fca595 | 12203 | if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false)) |
a212e43f MG |
12204 | { |
12205 | if (complain) | |
12206 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
12207 | "involves truncation", type0, type1); | |
12208 | return stv_error; | |
12209 | } | |
12210 | return ret; | |
12211 | } | |
12212 | default: | |
12213 | break; | |
12214 | } | |
12215 | ||
12216 | return stv_nothing; | |
12217 | } | |
12218 | ||
e28d52cf DS |
12219 | /* Return true iff ALIGN is an integral constant that is a fundamental |
12220 | alignment, as defined by [basic.align] in the c++-11 | |
12221 | specifications. | |
12222 | ||
12223 | That is: | |
12224 | ||
12225 | [A fundamental alignment is represented by an alignment less than or | |
12226 | equal to the greatest alignment supported by the implementation | |
12227 | in all contexts, which is equal to | |
12228 | alignof(max_align_t)]. */ | |
12229 | ||
12230 | bool | |
12231 | cxx_fundamental_alignment_p (unsigned align) | |
12232 | { | |
12233 | return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node), | |
12234 | TYPE_ALIGN (long_double_type_node))); | |
12235 | } | |
12236 | ||
f04dda30 MP |
12237 | /* Return true if T is a pointer to a zero-sized aggregate. */ |
12238 | ||
12239 | bool | |
12240 | pointer_to_zero_sized_aggr_p (tree t) | |
12241 | { | |
12242 | if (!POINTER_TYPE_P (t)) | |
12243 | return false; | |
12244 | t = TREE_TYPE (t); | |
12245 | return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t))); | |
12246 | } | |
12247 | ||
39dabefd | 12248 | #include "gt-c-family-c-common.h" |