]>
Commit | Line | Data |
---|---|---|
b0fc3e72 | 1 | /* Subroutines shared by all languages that are variants of C. |
d513ec2f | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
0f6a7cb7 | 3 | 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 |
2d9d8740 | 4 | Free Software Foundation, Inc. |
b0fc3e72 | 5 | |
f12b58b3 | 6 | This file is part of GCC. |
b0fc3e72 | 7 | |
f12b58b3 | 8 | GCC is free software; you can redistribute it and/or modify it under |
9 | the terms of the GNU General Public License as published by the Free | |
8c4c00c1 | 10 | Software Foundation; either version 3, or (at your option) any later |
f12b58b3 | 11 | version. |
b0fc3e72 | 12 | |
f12b58b3 | 13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
b0fc3e72 | 17 | |
18 | You should have received a copy of the GNU General Public License | |
8c4c00c1 | 19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
b0fc3e72 | 21 | |
22 | #include "config.h" | |
405711de | 23 | #include "system.h" |
805e22b2 | 24 | #include "coretypes.h" |
25 | #include "tm.h" | |
e48d0f41 | 26 | #include "intl.h" |
b0fc3e72 | 27 | #include "tree.h" |
b0fc3e72 | 28 | #include "flags.h" |
a3fa7feb | 29 | #include "c-pragma.h" |
dc12af01 | 30 | #include "ggc.h" |
e41f0d80 | 31 | #include "c-common.h" |
6c536c4f | 32 | #include "c-objc.h" |
d8c9779c | 33 | #include "tm_p.h" |
4e91a871 | 34 | #include "obstack.h" |
a654e028 | 35 | #include "cpplib.h" |
8ee295a7 | 36 | #include "target.h" |
218e3e4e | 37 | #include "common/common-target.h" |
96554925 | 38 | #include "langhooks.h" |
f3dde807 | 39 | #include "tree-inline.h" |
69579044 | 40 | #include "toplev.h" |
7f5f3953 | 41 | #include "diagnostic.h" |
4ee9c684 | 42 | #include "tree-iterator.h" |
43 | #include "hashtab.h" | |
b55af61c | 44 | #include "tree-mudflap.h" |
e08bd2f4 | 45 | #include "opts.h" |
62eec3b4 | 46 | #include "cgraph.h" |
b9fc964a | 47 | #include "target-def.h" |
fd6f6435 | 48 | |
90cc7820 | 49 | cpp_reader *parse_in; /* Declared in c-pragma.h. */ |
a654e028 | 50 | |
72040e7e | 51 | /* The following symbols are subsumed in the c_global_trees array, and |
44e9fa65 | 52 | listed here individually for documentation purposes. |
72040e7e | 53 | |
54 | INTEGER_TYPE and REAL_TYPE nodes for the standard data types. | |
55 | ||
56 | tree short_integer_type_node; | |
57 | tree long_integer_type_node; | |
58 | tree long_long_integer_type_node; | |
6388cfe2 | 59 | tree int128_integer_type_node; |
72040e7e | 60 | |
61 | tree short_unsigned_type_node; | |
62 | tree long_unsigned_type_node; | |
63 | tree long_long_unsigned_type_node; | |
6388cfe2 | 64 | tree int128_unsigned_type_node; |
72040e7e | 65 | |
3c2239cf | 66 | tree truthvalue_type_node; |
67 | tree truthvalue_false_node; | |
68 | tree truthvalue_true_node; | |
72040e7e | 69 | |
70 | tree ptrdiff_type_node; | |
71 | ||
72 | tree unsigned_char_type_node; | |
73 | tree signed_char_type_node; | |
74 | tree wchar_type_node; | |
72040e7e | 75 | |
924bbf02 | 76 | tree char16_type_node; |
77 | tree char32_type_node; | |
78 | ||
72040e7e | 79 | tree float_type_node; |
80 | tree double_type_node; | |
81 | tree long_double_type_node; | |
82 | ||
83 | tree complex_integer_type_node; | |
84 | tree complex_float_type_node; | |
85 | tree complex_double_type_node; | |
86 | tree complex_long_double_type_node; | |
87 | ||
c4503c0a | 88 | tree dfloat32_type_node; |
89 | tree dfloat64_type_node; | |
90 | tree_dfloat128_type_node; | |
91 | ||
72040e7e | 92 | tree intQI_type_node; |
93 | tree intHI_type_node; | |
94 | tree intSI_type_node; | |
95 | tree intDI_type_node; | |
96 | tree intTI_type_node; | |
97 | ||
98 | tree unsigned_intQI_type_node; | |
99 | tree unsigned_intHI_type_node; | |
100 | tree unsigned_intSI_type_node; | |
101 | tree unsigned_intDI_type_node; | |
102 | tree unsigned_intTI_type_node; | |
103 | ||
104 | tree widest_integer_literal_type_node; | |
105 | tree widest_unsigned_literal_type_node; | |
106 | ||
107 | Nodes for types `void *' and `const void *'. | |
108 | ||
109 | tree ptr_type_node, const_ptr_type_node; | |
110 | ||
111 | Nodes for types `char *' and `const char *'. | |
112 | ||
113 | tree string_type_node, const_string_type_node; | |
114 | ||
115 | Type `char[SOMENUMBER]'. | |
116 | Used when an array of char is needed and the size is irrelevant. | |
117 | ||
118 | tree char_array_type_node; | |
119 | ||
120 | Type `int[SOMENUMBER]' or something like it. | |
121 | Used when an array of int needed and the size is irrelevant. | |
122 | ||
123 | tree int_array_type_node; | |
124 | ||
125 | Type `wchar_t[SOMENUMBER]' or something like it. | |
126 | Used when a wide string literal is created. | |
127 | ||
128 | tree wchar_array_type_node; | |
129 | ||
924bbf02 | 130 | Type `char16_t[SOMENUMBER]' or something like it. |
131 | Used when a UTF-16 string literal is created. | |
132 | ||
133 | tree char16_array_type_node; | |
134 | ||
135 | Type `char32_t[SOMENUMBER]' or something like it. | |
136 | Used when a UTF-32 string literal is created. | |
137 | ||
138 | tree char32_array_type_node; | |
139 | ||
72040e7e | 140 | Type `int ()' -- used for implicit declaration of functions. |
141 | ||
142 | tree default_function_type; | |
143 | ||
72040e7e | 144 | A VOID_TYPE node, packaged in a TREE_LIST. |
145 | ||
146 | tree void_list_node; | |
147 | ||
734c98be | 148 | The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__, |
65b7f83f | 149 | and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__ |
150 | VAR_DECLS, but C++ does.) | |
71d9fc9b | 151 | |
65b7f83f | 152 | tree function_name_decl_node; |
734c98be | 153 | tree pretty_function_name_decl_node; |
65b7f83f | 154 | tree c99_function_name_decl_node; |
155 | ||
156 | Stack of nested function name VAR_DECLs. | |
1cae46be | 157 | |
65b7f83f | 158 | tree saved_function_name_decls; |
71d9fc9b | 159 | |
72040e7e | 160 | */ |
161 | ||
162 | tree c_global_trees[CTI_MAX]; | |
573aba85 | 163 | \f |
574a6990 | 164 | /* Switches common to the C front ends. */ |
165 | ||
f7070933 | 166 | /* Nonzero means don't output line number information. */ |
167 | ||
168 | char flag_no_line_commands; | |
169 | ||
170 | /* Nonzero causes -E output not to be done, but directives such as | |
171 | #define that have side effects are still obeyed. */ | |
172 | ||
173 | char flag_no_output; | |
174 | ||
175 | /* Nonzero means dump macros in some fashion. */ | |
176 | ||
177 | char flag_dump_macros; | |
178 | ||
179 | /* Nonzero means pass #include lines through to the output. */ | |
180 | ||
181 | char flag_dump_includes; | |
182 | ||
d718b525 | 183 | /* Nonzero means process PCH files while preprocessing. */ |
184 | ||
185 | bool flag_pch_preprocess; | |
186 | ||
573aba85 | 187 | /* The file name to which we should write a precompiled header, or |
188 | NULL if no header will be written in this compile. */ | |
189 | ||
190 | const char *pch_file; | |
191 | ||
1ed9d5f5 | 192 | /* Nonzero if an ISO standard was selected. It rejects macros in the |
193 | user's namespace. */ | |
194 | int flag_iso; | |
195 | ||
1cae46be | 196 | /* Warn about #pragma directives that are not recognized. */ |
574a6990 | 197 | |
1cae46be | 198 | int warn_unknown_pragmas; /* Tri state variable. */ |
574a6990 | 199 | |
574a6990 | 200 | /* Warn about format/argument anomalies in calls to formatted I/O functions |
201 | (*printf, *scanf, strftime, strfmon, etc.). */ | |
202 | ||
203 | int warn_format; | |
204 | ||
574a6990 | 205 | /* C/ObjC language option variables. */ |
206 | ||
207 | ||
574a6990 | 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 | ||
32074525 | 217 | /* Nonzero means use the ISO C99 (or C11) dialect of C. */ |
574a6990 | 218 | |
219 | int flag_isoc99; | |
220 | ||
32074525 | 221 | /* Nonzero means use the ISO C11 dialect of C. */ |
39012afb | 222 | |
32074525 | 223 | int flag_isoc11; |
39012afb | 224 | |
8b332087 | 225 | /* Nonzero means that we have builtin functions, and main is an int. */ |
574a6990 | 226 | |
227 | int flag_hosted = 1; | |
228 | ||
574a6990 | 229 | |
230 | /* ObjC language option variables. */ | |
231 | ||
232 | ||
574a6990 | 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 | ||
f0b5f617 | 239 | /* Tells the compiler what is the constant string class for ObjC. */ |
574a6990 | 240 | |
241 | const char *constant_string_class_name; | |
242 | ||
574a6990 | 243 | |
244 | /* C++ language option variables. */ | |
245 | ||
246 | ||
574a6990 | 247 | /* Nonzero means generate separate instantiation control files and |
248 | juggle them at link time. */ | |
249 | ||
250 | int flag_use_repository; | |
251 | ||
6dcdb5de | 252 | /* The C++ dialect being used. C++98 is the default. */ |
0fe6eeac | 253 | |
6dcdb5de | 254 | enum cxx_dialect cxx_dialect = cxx98; |
0fe6eeac | 255 | |
9dcd0d49 | 256 | /* Maximum template instantiation depth. This limit exists to limit the |
8ce59854 | 257 | time it takes to notice excessively recursive template instantiations. |
574a6990 | 258 | |
8ce59854 | 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; | |
574a6990 | 264 | |
988fc1d1 | 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 | ||
e60a6f7b | 269 | tree (*make_fname_decl) (location_t, tree, int); |
9e5a737d | 270 | |
48d94ede | 271 | /* Nonzero means don't warn about problems that occur when the code is |
272 | executed. */ | |
273 | int c_inhibit_evaluation_warnings; | |
e78703c1 | 274 | |
93be21c0 | 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 | ||
7f5f3953 | 283 | /* Whether lexing has been completed, so subsequent preprocessor |
284 | errors should use the compiler's input_location. */ | |
285 | bool done_lexing = false; | |
286 | ||
2c0e001b | 287 | /* Information about how a function name is generated. */ |
65b7f83f | 288 | struct fname_var_t |
289 | { | |
e99c3a1d | 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? */ | |
65b7f83f | 293 | }; |
294 | ||
2c0e001b | 295 | /* The three ways of getting then name of the current function. */ |
65b7f83f | 296 | |
297 | const struct fname_var_t fname_vars[] = | |
298 | { | |
2c0e001b | 299 | /* C99 compliant __func__, must be first. */ |
65b7f83f | 300 | {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0}, |
2c0e001b | 301 | /* GCC __FUNCTION__ compliant. */ |
65b7f83f | 302 | {&function_name_decl_node, RID_FUNCTION_NAME, 0}, |
2c0e001b | 303 | /* GCC __PRETTY_FUNCTION__ compliant. */ |
65b7f83f | 304 | {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1}, |
305 | {NULL, 0, 0}, | |
306 | }; | |
307 | ||
79396169 | 308 | /* Global visibility options. */ |
309 | struct visibility_flags visibility_options; | |
310 | ||
a75b1c71 | 311 | static tree c_fully_fold_internal (tree expr, bool, bool *, bool *); |
2ca392fd | 312 | static tree check_case_value (tree); |
313 | static bool check_case_bounds (tree, tree, tree *, tree *); | |
be43ff5a | 314 | |
1cae46be | 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 *); | |
5de92639 | 319 | static tree handle_hot_attribute (tree *, tree, tree, int, bool *); |
320 | static tree handle_cold_attribute (tree *, tree, tree, int, bool *); | |
1cae46be | 321 | static tree handle_noinline_attribute (tree *, tree, tree, int, bool *); |
bdb1f0d1 | 322 | static tree handle_noclone_attribute (tree *, tree, tree, int, bool *); |
7bd95dfd | 323 | static tree handle_leaf_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 324 | static tree handle_always_inline_attribute (tree *, tree, tree, int, |
325 | bool *); | |
1b16fc45 | 326 | static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *); |
327 | static tree handle_artificial_attribute (tree *, tree, tree, int, bool *); | |
0cdd9887 | 328 | static tree handle_flatten_attribute (tree *, tree, tree, int, bool *); |
10fc867f | 329 | static tree handle_error_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 330 | static tree handle_used_attribute (tree *, tree, tree, int, bool *); |
331 | static tree handle_unused_attribute (tree *, tree, tree, int, bool *); | |
62eec3b4 | 332 | static tree handle_externally_visible_attribute (tree *, tree, tree, int, |
333 | bool *); | |
1cae46be | 334 | static tree handle_const_attribute (tree *, tree, tree, int, bool *); |
335 | static tree handle_transparent_union_attribute (tree *, tree, tree, | |
336 | int, bool *); | |
337 | static tree handle_constructor_attribute (tree *, tree, tree, int, bool *); | |
338 | static tree handle_destructor_attribute (tree *, tree, tree, int, bool *); | |
339 | static tree handle_mode_attribute (tree *, tree, tree, int, bool *); | |
340 | static tree handle_section_attribute (tree *, tree, tree, int, bool *); | |
341 | static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); | |
342 | static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; | |
85c0a25c | 343 | static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *); |
344 | static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *); | |
1cae46be | 345 | static tree handle_alias_attribute (tree *, tree, tree, int, bool *); |
f4a30bd7 | 346 | static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ; |
1cae46be | 347 | static tree handle_visibility_attribute (tree *, tree, tree, int, |
348 | bool *); | |
349 | static tree handle_tls_model_attribute (tree *, tree, tree, int, | |
350 | bool *); | |
351 | static tree handle_no_instrument_function_attribute (tree *, tree, | |
352 | tree, int, bool *); | |
353 | static tree handle_malloc_attribute (tree *, tree, tree, int, bool *); | |
26d1c5ff | 354 | static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 355 | static tree handle_no_limit_stack_attribute (tree *, tree, tree, int, |
356 | bool *); | |
357 | static tree handle_pure_attribute (tree *, tree, tree, int, bool *); | |
4c0315d0 | 358 | static tree handle_tm_attribute (tree *, tree, tree, int, bool *); |
359 | static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *); | |
fc09b200 | 360 | static tree handle_novops_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 361 | static tree handle_deprecated_attribute (tree *, tree, tree, int, |
362 | bool *); | |
363 | static tree handle_vector_size_attribute (tree *, tree, tree, int, | |
364 | bool *); | |
365 | static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *); | |
366 | static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *); | |
367 | static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *); | |
8a8cdb8d | 368 | static tree handle_warn_unused_result_attribute (tree *, tree, tree, int, |
369 | bool *); | |
bf6c8de0 | 370 | static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *); |
b5c26b42 | 371 | static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *); |
4a29c97c | 372 | static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *); |
24470055 | 373 | static tree handle_target_attribute (tree *, tree, tree, int, bool *); |
46f8e3b0 | 374 | static tree handle_optimize_attribute (tree *, tree, tree, int, bool *); |
4c0315d0 | 375 | static tree ignore_attribute (tree *, tree, tree, int, bool *); |
48b14f50 | 376 | static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *); |
8ce86007 | 377 | static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 378 | |
d01f58f9 | 379 | static void check_function_nonnull (tree, int, tree *); |
1cae46be | 380 | static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); |
381 | static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT); | |
382 | static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); | |
860251be | 383 | static int resort_field_decl_cmp (const void *, const void *); |
dbf6c367 | 384 | |
5c6e5756 | 385 | /* Reserved words. The third field is a mask: keywords are disabled |
386 | if they match the mask. | |
387 | ||
388 | Masks for languages: | |
389 | C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC | |
390 | C --std=c99: D_CXXONLY | D_OBJC | |
391 | ObjC is like C except that D_OBJC and D_CXX_OBJC are not set | |
392 | C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | |
393 | C++ --std=c0x: D_CONLY | D_OBJC | |
394 | ObjC++ is like C++ except that D_OBJC is not set | |
395 | ||
396 | If -fno-asm is used, D_ASM is added to the mask. If | |
397 | -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in | |
398 | C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords. | |
b27e241e | 399 | In C with -Wc++-compat, we warn if D_CXXWARN is set. |
5c6e5756 | 400 | |
b27e241e | 401 | Note the complication of the D_CXX_OBJC keywords. These are |
402 | reserved words such as 'class'. In C++, 'class' is a reserved | |
403 | word. In Objective-C++ it is too. In Objective-C, it is a | |
404 | reserved word too, but only if it follows an '@' sign. | |
405 | */ | |
5c6e5756 | 406 | const struct c_common_resword c_common_reswords[] = |
407 | { | |
83e25171 | 408 | { "_Alignas", RID_ALIGNAS, D_CONLY }, |
409 | { "_Alignof", RID_ALIGNOF, D_CONLY }, | |
5c6e5756 | 410 | { "_Bool", RID_BOOL, D_CONLY }, |
411 | { "_Complex", RID_COMPLEX, 0 }, | |
c1800156 | 412 | { "_Imaginary", RID_IMAGINARY, D_CONLY }, |
5c6e5756 | 413 | { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT }, |
414 | { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT }, | |
415 | { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT }, | |
416 | { "_Fract", RID_FRACT, D_CONLY | D_EXT }, | |
417 | { "_Accum", RID_ACCUM, D_CONLY | D_EXT }, | |
418 | { "_Sat", RID_SAT, D_CONLY | D_EXT }, | |
f80e7755 | 419 | { "_Static_assert", RID_STATIC_ASSERT, D_CONLY }, |
985c6e3a | 420 | { "_Noreturn", RID_NORETURN, D_CONLY }, |
5c6e5756 | 421 | { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, |
422 | { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, | |
423 | { "__alignof", RID_ALIGNOF, 0 }, | |
424 | { "__alignof__", RID_ALIGNOF, 0 }, | |
425 | { "__asm", RID_ASM, 0 }, | |
426 | { "__asm__", RID_ASM, 0 }, | |
427 | { "__attribute", RID_ATTRIBUTE, 0 }, | |
428 | { "__attribute__", RID_ATTRIBUTE, 0 }, | |
e6014a82 | 429 | { "__bases", RID_BASES, D_CXXONLY }, |
5c6e5756 | 430 | { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, |
bff4ad11 | 431 | { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY }, |
bf0cb017 | 432 | { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 }, |
5c6e5756 | 433 | { "__builtin_offsetof", RID_OFFSETOF, 0 }, |
434 | { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, | |
435 | { "__builtin_va_arg", RID_VA_ARG, 0 }, | |
436 | { "__complex", RID_COMPLEX, 0 }, | |
437 | { "__complex__", RID_COMPLEX, 0 }, | |
438 | { "__const", RID_CONST, 0 }, | |
439 | { "__const__", RID_CONST, 0 }, | |
440 | { "__decltype", RID_DECLTYPE, D_CXXONLY }, | |
e6014a82 | 441 | { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY }, |
5c6e5756 | 442 | { "__extension__", RID_EXTENSION, 0 }, |
443 | { "__func__", RID_C99_FUNCTION_NAME, 0 }, | |
444 | { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY }, | |
445 | { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY }, | |
446 | { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY }, | |
447 | { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY }, | |
448 | { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY }, | |
449 | { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY }, | |
450 | { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY }, | |
451 | { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY }, | |
23407dc9 | 452 | { "__imag", RID_IMAGPART, 0 }, |
453 | { "__imag__", RID_IMAGPART, 0 }, | |
454 | { "__inline", RID_INLINE, 0 }, | |
455 | { "__inline__", RID_INLINE, 0 }, | |
6388cfe2 | 456 | { "__int128", RID_INT128, 0 }, |
5c6e5756 | 457 | { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY }, |
458 | { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY }, | |
459 | { "__is_class", RID_IS_CLASS, D_CXXONLY }, | |
460 | { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY }, | |
461 | { "__is_empty", RID_IS_EMPTY, D_CXXONLY }, | |
462 | { "__is_enum", RID_IS_ENUM, D_CXXONLY }, | |
aa4313eb | 463 | { "__is_final", RID_IS_FINAL, D_CXXONLY }, |
23407dc9 | 464 | { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY }, |
5c6e5756 | 465 | { "__is_pod", RID_IS_POD, D_CXXONLY }, |
466 | { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY }, | |
c1c67b4f | 467 | { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY }, |
468 | { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY }, | |
5c6e5756 | 469 | { "__is_union", RID_IS_UNION, D_CXXONLY }, |
5c6e5756 | 470 | { "__label__", RID_LABEL, 0 }, |
471 | { "__null", RID_NULL, 0 }, | |
472 | { "__real", RID_REALPART, 0 }, | |
473 | { "__real__", RID_REALPART, 0 }, | |
474 | { "__restrict", RID_RESTRICT, 0 }, | |
475 | { "__restrict__", RID_RESTRICT, 0 }, | |
476 | { "__signed", RID_SIGNED, 0 }, | |
477 | { "__signed__", RID_SIGNED, 0 }, | |
478 | { "__thread", RID_THREAD, 0 }, | |
4c0315d0 | 479 | { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 }, |
480 | { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 }, | |
481 | { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 }, | |
5c6e5756 | 482 | { "__typeof", RID_TYPEOF, 0 }, |
483 | { "__typeof__", RID_TYPEOF, 0 }, | |
23407dc9 | 484 | { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY }, |
5c6e5756 | 485 | { "__volatile", RID_VOLATILE, 0 }, |
486 | { "__volatile__", RID_VOLATILE, 0 }, | |
ffcdbf9c | 487 | { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
e463efd7 | 488 | { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 489 | { "asm", RID_ASM, D_ASM }, |
490 | { "auto", RID_AUTO, 0 }, | |
dbd982c9 | 491 | { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 492 | { "break", RID_BREAK, 0 }, |
493 | { "case", RID_CASE, 0 }, | |
51030405 | 494 | { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN }, |
5c6e5756 | 495 | { "char", RID_CHAR, 0 }, |
51030405 | 496 | { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
497 | { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
498 | { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 499 | { "const", RID_CONST, 0 }, |
17814aca | 500 | { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 501 | { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN }, |
502 | { "continue", RID_CONTINUE, 0 }, | |
51030405 | 503 | { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 504 | { "default", RID_DEFAULT, 0 }, |
51030405 | 505 | { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 506 | { "do", RID_DO, 0 }, |
507 | { "double", RID_DOUBLE, 0 }, | |
508 | { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN }, | |
509 | { "else", RID_ELSE, 0 }, | |
510 | { "enum", RID_ENUM, 0 }, | |
51030405 | 511 | { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN }, |
512 | { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 513 | { "extern", RID_EXTERN, 0 }, |
51030405 | 514 | { "false", RID_FALSE, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 515 | { "float", RID_FLOAT, 0 }, |
516 | { "for", RID_FOR, 0 }, | |
51030405 | 517 | { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 518 | { "goto", RID_GOTO, 0 }, |
519 | { "if", RID_IF, 0 }, | |
520 | { "inline", RID_INLINE, D_EXT89 }, | |
521 | { "int", RID_INT, 0 }, | |
522 | { "long", RID_LONG, 0 }, | |
523 | { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN }, | |
51030405 | 524 | { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN }, |
525 | { "new", RID_NEW, D_CXXONLY | D_CXXWARN }, | |
98fe9664 | 526 | { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
6fe11077 | 527 | { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
51030405 | 528 | { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN }, |
529 | { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN }, | |
530 | { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN }, | |
531 | { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 532 | { "register", RID_REGISTER, 0 }, |
533 | { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN }, | |
534 | { "restrict", RID_RESTRICT, D_CONLY | D_C99 }, | |
535 | { "return", RID_RETURN, 0 }, | |
536 | { "short", RID_SHORT, 0 }, | |
537 | { "signed", RID_SIGNED, 0 }, | |
538 | { "sizeof", RID_SIZEOF, 0 }, | |
539 | { "static", RID_STATIC, 0 }, | |
540 | { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
541 | { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN }, | |
542 | { "struct", RID_STRUCT, 0 }, | |
543 | { "switch", RID_SWITCH, 0 }, | |
51030405 | 544 | { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN }, |
545 | { "this", RID_THIS, D_CXXONLY | D_CXXWARN }, | |
3740094c | 546 | { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
51030405 | 547 | { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN }, |
548 | { "true", RID_TRUE, D_CXXONLY | D_CXXWARN }, | |
549 | { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 550 | { "typedef", RID_TYPEDEF, 0 }, |
51030405 | 551 | { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN }, |
552 | { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 553 | { "typeof", RID_TYPEOF, D_ASM | D_EXT }, |
554 | { "union", RID_UNION, 0 }, | |
555 | { "unsigned", RID_UNSIGNED, 0 }, | |
51030405 | 556 | { "using", RID_USING, D_CXXONLY | D_CXXWARN }, |
557 | { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 558 | { "void", RID_VOID, 0 }, |
559 | { "volatile", RID_VOLATILE, 0 }, | |
560 | { "wchar_t", RID_WCHAR, D_CXXONLY }, | |
561 | { "while", RID_WHILE, 0 }, | |
562 | /* These Objective-C keywords are recognized only immediately after | |
563 | an '@'. */ | |
564 | { "compatibility_alias", RID_AT_ALIAS, D_OBJC }, | |
565 | { "defs", RID_AT_DEFS, D_OBJC }, | |
566 | { "encode", RID_AT_ENCODE, D_OBJC }, | |
567 | { "end", RID_AT_END, D_OBJC }, | |
568 | { "implementation", RID_AT_IMPLEMENTATION, D_OBJC }, | |
569 | { "interface", RID_AT_INTERFACE, D_OBJC }, | |
570 | { "protocol", RID_AT_PROTOCOL, D_OBJC }, | |
571 | { "selector", RID_AT_SELECTOR, D_OBJC }, | |
572 | { "finally", RID_AT_FINALLY, D_OBJC }, | |
573 | { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC }, | |
069761fb | 574 | { "optional", RID_AT_OPTIONAL, D_OBJC }, |
575 | { "required", RID_AT_REQUIRED, D_OBJC }, | |
86c110ac | 576 | { "property", RID_AT_PROPERTY, D_OBJC }, |
4a8875ed | 577 | { "package", RID_AT_PACKAGE, D_OBJC }, |
e1f293c0 | 578 | { "synthesize", RID_AT_SYNTHESIZE, D_OBJC }, |
579 | { "dynamic", RID_AT_DYNAMIC, D_OBJC }, | |
5c6e5756 | 580 | /* These are recognized only in protocol-qualifier context |
581 | (see above) */ | |
582 | { "bycopy", RID_BYCOPY, D_OBJC }, | |
583 | { "byref", RID_BYREF, D_OBJC }, | |
584 | { "in", RID_IN, D_OBJC }, | |
585 | { "inout", RID_INOUT, D_OBJC }, | |
586 | { "oneway", RID_ONEWAY, D_OBJC }, | |
587 | { "out", RID_OUT, D_OBJC }, | |
86c110ac | 588 | /* These are recognized inside a property attribute list */ |
7590f0e5 | 589 | { "assign", RID_ASSIGN, D_OBJC }, |
590 | { "copy", RID_COPY, D_OBJC }, | |
86c110ac | 591 | { "getter", RID_GETTER, D_OBJC }, |
7590f0e5 | 592 | { "nonatomic", RID_NONATOMIC, D_OBJC }, |
593 | { "readonly", RID_READONLY, D_OBJC }, | |
594 | { "readwrite", RID_READWRITE, D_OBJC }, | |
595 | { "retain", RID_RETAIN, D_OBJC }, | |
86c110ac | 596 | { "setter", RID_SETTER, D_OBJC }, |
5c6e5756 | 597 | }; |
598 | ||
599 | const unsigned int num_c_common_reswords = | |
600 | sizeof c_common_reswords / sizeof (struct c_common_resword); | |
601 | ||
f8e93a2e | 602 | /* Table of machine-independent attributes common to all C-like languages. */ |
603 | const struct attribute_spec c_common_attribute_table[] = | |
604 | { | |
ac86af5d | 605 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
606 | affects_type_identity } */ | |
f8e93a2e | 607 | { "packed", 0, 0, false, false, false, |
ac86af5d | 608 | handle_packed_attribute , false}, |
f8e93a2e | 609 | { "nocommon", 0, 0, true, false, false, |
ac86af5d | 610 | handle_nocommon_attribute, false}, |
f8e93a2e | 611 | { "common", 0, 0, true, false, false, |
ac86af5d | 612 | handle_common_attribute, false }, |
f8e93a2e | 613 | /* FIXME: logically, noreturn attributes should be listed as |
614 | "false, true, true" and apply to function types. But implementing this | |
615 | would require all the places in the compiler that use TREE_THIS_VOLATILE | |
616 | on a decl to identify non-returning functions to be located and fixed | |
617 | to check the function type instead. */ | |
618 | { "noreturn", 0, 0, true, false, false, | |
ac86af5d | 619 | handle_noreturn_attribute, false }, |
f8e93a2e | 620 | { "volatile", 0, 0, true, false, false, |
ac86af5d | 621 | handle_noreturn_attribute, false }, |
f8e93a2e | 622 | { "noinline", 0, 0, true, false, false, |
ac86af5d | 623 | handle_noinline_attribute, false }, |
bdb1f0d1 | 624 | { "noclone", 0, 0, true, false, false, |
ac86af5d | 625 | handle_noclone_attribute, false }, |
7bd95dfd | 626 | { "leaf", 0, 0, true, false, false, |
ac86af5d | 627 | handle_leaf_attribute, false }, |
f8e93a2e | 628 | { "always_inline", 0, 0, true, false, false, |
ac86af5d | 629 | handle_always_inline_attribute, false }, |
541e4101 | 630 | { "gnu_inline", 0, 0, true, false, false, |
ac86af5d | 631 | handle_gnu_inline_attribute, false }, |
1b16fc45 | 632 | { "artificial", 0, 0, true, false, false, |
ac86af5d | 633 | handle_artificial_attribute, false }, |
0cdd9887 | 634 | { "flatten", 0, 0, true, false, false, |
ac86af5d | 635 | handle_flatten_attribute, false }, |
f8e93a2e | 636 | { "used", 0, 0, true, false, false, |
ac86af5d | 637 | handle_used_attribute, false }, |
f8e93a2e | 638 | { "unused", 0, 0, false, false, false, |
ac86af5d | 639 | handle_unused_attribute, false }, |
62eec3b4 | 640 | { "externally_visible", 0, 0, true, false, false, |
ac86af5d | 641 | handle_externally_visible_attribute, false }, |
f8e93a2e | 642 | /* The same comments as for noreturn attributes apply to const ones. */ |
643 | { "const", 0, 0, true, false, false, | |
ac86af5d | 644 | handle_const_attribute, false }, |
f8e93a2e | 645 | { "transparent_union", 0, 0, false, false, false, |
ac86af5d | 646 | handle_transparent_union_attribute, false }, |
9af7fd5b | 647 | { "constructor", 0, 1, true, false, false, |
ac86af5d | 648 | handle_constructor_attribute, false }, |
9af7fd5b | 649 | { "destructor", 0, 1, true, false, false, |
ac86af5d | 650 | handle_destructor_attribute, false }, |
f8e93a2e | 651 | { "mode", 1, 1, false, true, false, |
ac86af5d | 652 | handle_mode_attribute, false }, |
f8e93a2e | 653 | { "section", 1, 1, true, false, false, |
ac86af5d | 654 | handle_section_attribute, false }, |
f8e93a2e | 655 | { "aligned", 0, 1, false, false, false, |
ac86af5d | 656 | handle_aligned_attribute, false }, |
f8e93a2e | 657 | { "weak", 0, 0, true, false, false, |
ac86af5d | 658 | handle_weak_attribute, false }, |
85c0a25c | 659 | { "ifunc", 1, 1, true, false, false, |
ac86af5d | 660 | handle_ifunc_attribute, false }, |
f8e93a2e | 661 | { "alias", 1, 1, true, false, false, |
ac86af5d | 662 | handle_alias_attribute, false }, |
f4a30bd7 | 663 | { "weakref", 0, 1, true, false, false, |
ac86af5d | 664 | handle_weakref_attribute, false }, |
f8e93a2e | 665 | { "no_instrument_function", 0, 0, true, false, false, |
ac86af5d | 666 | handle_no_instrument_function_attribute, |
667 | false }, | |
f8e93a2e | 668 | { "malloc", 0, 0, true, false, false, |
ac86af5d | 669 | handle_malloc_attribute, false }, |
26d1c5ff | 670 | { "returns_twice", 0, 0, true, false, false, |
ac86af5d | 671 | handle_returns_twice_attribute, false }, |
f8e93a2e | 672 | { "no_stack_limit", 0, 0, true, false, false, |
ac86af5d | 673 | handle_no_limit_stack_attribute, false }, |
f8e93a2e | 674 | { "pure", 0, 0, true, false, false, |
ac86af5d | 675 | handle_pure_attribute, false }, |
4c0315d0 | 676 | { "transaction_callable", 0, 0, false, true, false, |
677 | handle_tm_attribute, false }, | |
678 | { "transaction_unsafe", 0, 0, false, true, false, | |
679 | handle_tm_attribute, false }, | |
680 | { "transaction_safe", 0, 0, false, true, false, | |
681 | handle_tm_attribute, false }, | |
682 | { "transaction_may_cancel_outer", 0, 0, false, true, false, | |
683 | handle_tm_attribute, false }, | |
684 | /* ??? These two attributes didn't make the transition from the | |
685 | Intel language document to the multi-vendor language document. */ | |
686 | { "transaction_pure", 0, 0, false, true, false, | |
687 | handle_tm_attribute, false }, | |
688 | { "transaction_wrap", 1, 1, true, false, false, | |
689 | handle_tm_wrap_attribute, false }, | |
fc09b200 | 690 | /* For internal use (marking of builtins) only. The name contains space |
691 | to prevent its usage in source code. */ | |
692 | { "no vops", 0, 0, true, false, false, | |
ac86af5d | 693 | handle_novops_attribute, false }, |
45c4e798 | 694 | { "deprecated", 0, 1, false, false, false, |
ac86af5d | 695 | handle_deprecated_attribute, false }, |
f8e93a2e | 696 | { "vector_size", 1, 1, false, true, false, |
ac86af5d | 697 | handle_vector_size_attribute, false }, |
b212f378 | 698 | { "visibility", 1, 1, false, false, false, |
ac86af5d | 699 | handle_visibility_attribute, false }, |
24dfead4 | 700 | { "tls_model", 1, 1, true, false, false, |
ac86af5d | 701 | handle_tls_model_attribute, false }, |
dbf6c367 | 702 | { "nonnull", 0, -1, false, true, true, |
ac86af5d | 703 | handle_nonnull_attribute, false }, |
fa987697 | 704 | { "nothrow", 0, 0, true, false, false, |
ac86af5d | 705 | handle_nothrow_attribute, false }, |
706 | { "may_alias", 0, 0, false, true, false, NULL, false }, | |
7acb29a3 | 707 | { "cleanup", 1, 1, true, false, false, |
ac86af5d | 708 | handle_cleanup_attribute, false }, |
8a8cdb8d | 709 | { "warn_unused_result", 0, 0, false, true, true, |
ac86af5d | 710 | handle_warn_unused_result_attribute, false }, |
50ca527f | 711 | { "sentinel", 0, 1, false, true, true, |
ac86af5d | 712 | handle_sentinel_attribute, false }, |
b5c26b42 | 713 | /* For internal use (marking of builtins) only. The name contains space |
714 | to prevent its usage in source code. */ | |
715 | { "type generic", 0, 0, false, true, true, | |
ac86af5d | 716 | handle_type_generic_attribute, false }, |
4a29c97c | 717 | { "alloc_size", 1, 2, false, true, true, |
ac86af5d | 718 | handle_alloc_size_attribute, false }, |
5de92639 | 719 | { "cold", 0, 0, true, false, false, |
ac86af5d | 720 | handle_cold_attribute, false }, |
5de92639 | 721 | { "hot", 0, 0, true, false, false, |
ac86af5d | 722 | handle_hot_attribute, false }, |
10fc867f | 723 | { "warning", 1, 1, true, false, false, |
ac86af5d | 724 | handle_error_attribute, false }, |
10fc867f | 725 | { "error", 1, 1, true, false, false, |
ac86af5d | 726 | handle_error_attribute, false }, |
24470055 | 727 | { "target", 1, -1, true, false, false, |
ac86af5d | 728 | handle_target_attribute, false }, |
46f8e3b0 | 729 | { "optimize", 1, -1, true, false, false, |
ac86af5d | 730 | handle_optimize_attribute, false }, |
4c0315d0 | 731 | /* For internal use only. The leading '*' both prevents its usage in |
732 | source code and signals that it may be overridden by machine tables. */ | |
733 | { "*tm regparm", 0, 0, false, true, true, | |
734 | ignore_attribute, false }, | |
48b14f50 | 735 | { "no_split_stack", 0, 0, true, false, false, |
ac86af5d | 736 | handle_no_split_stack_attribute, false }, |
8ce86007 | 737 | /* For internal use (marking of builtins and runtime functions) only. |
738 | The name contains space to prevent its usage in source code. */ | |
739 | { "fn spec", 1, 1, false, true, true, | |
ac86af5d | 740 | handle_fnspec_attribute, false }, |
741 | { NULL, 0, 0, false, false, false, NULL, false } | |
f8e93a2e | 742 | }; |
743 | ||
744 | /* Give the specifications for the format attributes, used by C and all | |
d716ce75 | 745 | descendants. */ |
f8e93a2e | 746 | |
747 | const struct attribute_spec c_common_format_attribute_table[] = | |
748 | { | |
ac86af5d | 749 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
750 | affects_type_identity } */ | |
f8e93a2e | 751 | { "format", 3, 3, false, true, true, |
ac86af5d | 752 | handle_format_attribute, false }, |
f8e93a2e | 753 | { "format_arg", 1, 1, false, true, true, |
ac86af5d | 754 | handle_format_arg_attribute, false }, |
755 | { NULL, 0, 0, false, false, false, NULL, false } | |
f8e93a2e | 756 | }; |
757 | ||
6d5d708e | 758 | /* Return identifier for address space AS. */ |
34208e18 | 759 | |
6d5d708e | 760 | const char * |
761 | c_addr_space_name (addr_space_t as) | |
762 | { | |
34208e18 | 763 | int rid = RID_FIRST_ADDR_SPACE + as; |
764 | gcc_assert (ridpointers [rid]); | |
765 | return IDENTIFIER_POINTER (ridpointers [rid]); | |
6d5d708e | 766 | } |
767 | ||
2c0e001b | 768 | /* Push current bindings for the function name VAR_DECLS. */ |
f4e3c278 | 769 | |
770 | void | |
1cae46be | 771 | start_fname_decls (void) |
f4e3c278 | 772 | { |
65b7f83f | 773 | unsigned ix; |
774 | tree saved = NULL_TREE; | |
1cae46be | 775 | |
65b7f83f | 776 | for (ix = 0; fname_vars[ix].decl; ix++) |
777 | { | |
778 | tree decl = *fname_vars[ix].decl; | |
f4e3c278 | 779 | |
65b7f83f | 780 | if (decl) |
781 | { | |
ceb7b692 | 782 | saved = tree_cons (decl, build_int_cst (integer_type_node, ix), |
783 | saved); | |
65b7f83f | 784 | *fname_vars[ix].decl = NULL_TREE; |
785 | } | |
786 | } | |
787 | if (saved || saved_function_name_decls) | |
788 | /* Normally they'll have been NULL, so only push if we've got a | |
789 | stack, or they are non-NULL. */ | |
790 | saved_function_name_decls = tree_cons (saved, NULL_TREE, | |
791 | saved_function_name_decls); | |
792 | } | |
793 | ||
2363ef00 | 794 | /* Finish up the current bindings, adding them into the current function's |
795 | statement tree. This must be done _before_ finish_stmt_tree is called. | |
796 | If there is no current function, we must be at file scope and no statements | |
797 | are involved. Pop the previous bindings. */ | |
65b7f83f | 798 | |
799 | void | |
1cae46be | 800 | finish_fname_decls (void) |
65b7f83f | 801 | { |
802 | unsigned ix; | |
2363ef00 | 803 | tree stmts = NULL_TREE; |
65b7f83f | 804 | tree stack = saved_function_name_decls; |
805 | ||
806 | for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack)) | |
2363ef00 | 807 | append_to_statement_list (TREE_VALUE (stack), &stmts); |
1cae46be | 808 | |
2363ef00 | 809 | if (stmts) |
65b7f83f | 810 | { |
2363ef00 | 811 | tree *bodyp = &DECL_SAVED_TREE (current_function_decl); |
5c423bd6 | 812 | |
2363ef00 | 813 | if (TREE_CODE (*bodyp) == BIND_EXPR) |
814 | bodyp = &BIND_EXPR_BODY (*bodyp); | |
81010c97 | 815 | |
bc2b76e0 | 816 | append_to_statement_list_force (*bodyp, &stmts); |
2363ef00 | 817 | *bodyp = stmts; |
65b7f83f | 818 | } |
1cae46be | 819 | |
65b7f83f | 820 | for (ix = 0; fname_vars[ix].decl; ix++) |
821 | *fname_vars[ix].decl = NULL_TREE; | |
1cae46be | 822 | |
65b7f83f | 823 | if (stack) |
f4e3c278 | 824 | { |
2c0e001b | 825 | /* We had saved values, restore them. */ |
65b7f83f | 826 | tree saved; |
827 | ||
828 | for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved)) | |
829 | { | |
830 | tree decl = TREE_PURPOSE (saved); | |
831 | unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved)); | |
1cae46be | 832 | |
65b7f83f | 833 | *fname_vars[ix].decl = decl; |
834 | } | |
835 | stack = TREE_CHAIN (stack); | |
f4e3c278 | 836 | } |
65b7f83f | 837 | saved_function_name_decls = stack; |
838 | } | |
839 | ||
81010c97 | 840 | /* Return the text name of the current function, suitably prettified |
5fc7fa69 | 841 | by PRETTY_P. Return string must be freed by caller. */ |
65b7f83f | 842 | |
843 | const char * | |
1cae46be | 844 | fname_as_string (int pretty_p) |
65b7f83f | 845 | { |
9ad4bb1e | 846 | const char *name = "top level"; |
5fc7fa69 | 847 | char *namep; |
8115b8be | 848 | int vrb = 2, len; |
849 | cpp_string cstr = { 0, 0 }, strname; | |
9ad4bb1e | 850 | |
84166705 | 851 | if (!pretty_p) |
9ad4bb1e | 852 | { |
853 | name = ""; | |
854 | vrb = 0; | |
855 | } | |
856 | ||
857 | if (current_function_decl) | |
dc24ddbd | 858 | name = lang_hooks.decl_printable_name (current_function_decl, vrb); |
9ad4bb1e | 859 | |
8115b8be | 860 | len = strlen (name) + 3; /* Two for '"'s. One for NULL. */ |
5fc7fa69 | 861 | |
8115b8be | 862 | namep = XNEWVEC (char, len); |
863 | snprintf (namep, len, "\"%s\"", name); | |
864 | strname.text = (unsigned char *) namep; | |
865 | strname.len = len - 1; | |
5fc7fa69 | 866 | |
924bbf02 | 867 | if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING)) |
8115b8be | 868 | { |
869 | XDELETEVEC (namep); | |
870 | return (const char *) cstr.text; | |
5fc7fa69 | 871 | } |
5fc7fa69 | 872 | |
873 | return namep; | |
65b7f83f | 874 | } |
875 | ||
65b7f83f | 876 | /* Return the VAR_DECL for a const char array naming the current |
877 | function. If the VAR_DECL has not yet been created, create it | |
878 | now. RID indicates how it should be formatted and IDENTIFIER_NODE | |
879 | ID is its name (unfortunately C and C++ hold the RID values of | |
880 | keywords in different places, so we can't derive RID from ID in | |
e3b80d49 | 881 | this language independent code. LOC is the location of the |
882 | function. */ | |
65b7f83f | 883 | |
884 | tree | |
e3b80d49 | 885 | fname_decl (location_t loc, unsigned int rid, tree id) |
65b7f83f | 886 | { |
887 | unsigned ix; | |
888 | tree decl = NULL_TREE; | |
889 | ||
890 | for (ix = 0; fname_vars[ix].decl; ix++) | |
891 | if (fname_vars[ix].rid == rid) | |
892 | break; | |
893 | ||
894 | decl = *fname_vars[ix].decl; | |
895 | if (!decl) | |
f4e3c278 | 896 | { |
2222b3c6 | 897 | /* If a tree is built here, it would normally have the lineno of |
898 | the current statement. Later this tree will be moved to the | |
899 | beginning of the function and this line number will be wrong. | |
900 | To avoid this problem set the lineno to 0 here; that prevents | |
7299020b | 901 | it from appearing in the RTL. */ |
2363ef00 | 902 | tree stmts; |
9a6486a6 | 903 | location_t saved_location = input_location; |
9a6486a6 | 904 | input_location = UNKNOWN_LOCATION; |
1cae46be | 905 | |
2363ef00 | 906 | stmts = push_stmt_list (); |
e60a6f7b | 907 | decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty); |
2363ef00 | 908 | stmts = pop_stmt_list (stmts); |
909 | if (!IS_EMPTY_STMT (stmts)) | |
910 | saved_function_name_decls | |
911 | = tree_cons (decl, stmts, saved_function_name_decls); | |
65b7f83f | 912 | *fname_vars[ix].decl = decl; |
9a6486a6 | 913 | input_location = saved_location; |
f4e3c278 | 914 | } |
65b7f83f | 915 | if (!ix && !current_function_decl) |
e3b80d49 | 916 | pedwarn (loc, 0, "%qD is not defined outside of function scope", decl); |
81010c97 | 917 | |
65b7f83f | 918 | return decl; |
f4e3c278 | 919 | } |
920 | ||
070236f0 | 921 | /* Given a STRING_CST, give it a suitable array-of-chars data type. */ |
b0fc3e72 | 922 | |
923 | tree | |
1cae46be | 924 | fix_string_type (tree value) |
b0fc3e72 | 925 | { |
070236f0 | 926 | int length = TREE_STRING_LENGTH (value); |
927 | int nchars; | |
00d26680 | 928 | tree e_type, i_type, a_type; |
929 | ||
73be5127 | 930 | /* Compute the number of elements, for the array type. */ |
924bbf02 | 931 | if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value)) |
932 | { | |
933 | nchars = length; | |
934 | e_type = char_type_node; | |
935 | } | |
936 | else if (TREE_TYPE (value) == char16_array_type_node) | |
937 | { | |
938 | nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT); | |
939 | e_type = char16_type_node; | |
940 | } | |
941 | else if (TREE_TYPE (value) == char32_array_type_node) | |
942 | { | |
943 | nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT); | |
944 | e_type = char32_type_node; | |
945 | } | |
946 | else | |
947 | { | |
948 | nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT); | |
949 | e_type = wchar_type_node; | |
950 | } | |
b0fc3e72 | 951 | |
1d752508 | 952 | /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous |
953 | limit in C++98 Annex B is very large (65536) and is not normative, | |
954 | so we do not diagnose it (warn_overlength_strings is forced off | |
955 | in c_common_post_options). */ | |
956 | if (warn_overlength_strings) | |
957 | { | |
958 | const int nchars_max = flag_isoc99 ? 4095 : 509; | |
959 | const int relevant_std = flag_isoc99 ? 99 : 90; | |
960 | if (nchars - 1 > nchars_max) | |
961 | /* Translators: The %d after 'ISO C' will be 90 or 99. Do not | |
962 | separate the %d from the 'C'. 'ISO' should not be | |
963 | translated, but it may be moved after 'C%d' in languages | |
964 | where modifiers follow nouns. */ | |
21ca8540 | 965 | pedwarn (input_location, OPT_Woverlength_strings, |
8864917d | 966 | "string length %qd is greater than the length %qd " |
1d752508 | 967 | "ISO C%d compilers are required to support", |
968 | nchars - 1, nchars_max, relevant_std); | |
969 | } | |
82cfc7f7 | 970 | |
390be14e | 971 | /* Create the array type for the string constant. The ISO C++ |
972 | standard says that a string literal has type `const char[N]' or | |
973 | `const wchar_t[N]'. We use the same logic when invoked as a C | |
974 | front-end with -Wwrite-strings. | |
975 | ??? We should change the type of an expression depending on the | |
976 | state of a warning flag. We should just be warning -- see how | |
977 | this is handled in the C++ front-end for the deprecated implicit | |
978 | conversion from string literals to `char*' or `wchar_t*'. | |
00d26680 | 979 | |
980 | The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified | |
981 | array type being the unqualified version of that type. | |
982 | Therefore, if we are constructing an array of const char, we must | |
983 | construct the matching unqualified array type first. The C front | |
984 | end does not require this, but it does no harm, so we do it | |
985 | unconditionally. */ | |
ceb7b692 | 986 | i_type = build_index_type (size_int (nchars - 1)); |
00d26680 | 987 | a_type = build_array_type (e_type, i_type); |
390be14e | 988 | if (c_dialect_cxx() || warn_write_strings) |
aebc8537 | 989 | a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST); |
3a10ba35 | 990 | |
00d26680 | 991 | TREE_TYPE (value) = a_type; |
b8e3b7ad | 992 | TREE_CONSTANT (value) = 1; |
a814bad5 | 993 | TREE_READONLY (value) = 1; |
b0fc3e72 | 994 | TREE_STATIC (value) = 1; |
995 | return value; | |
996 | } | |
997 | \f | |
fc501191 | 998 | /* If DISABLE is true, stop issuing warnings. This is used when |
999 | parsing code that we know will not be executed. This function may | |
1000 | be called multiple times, and works as a stack. */ | |
1001 | ||
1002 | static void | |
1003 | c_disable_warnings (bool disable) | |
1004 | { | |
1005 | if (disable) | |
1006 | { | |
1007 | ++c_inhibit_evaluation_warnings; | |
1008 | fold_defer_overflow_warnings (); | |
1009 | } | |
1010 | } | |
1011 | ||
1012 | /* If ENABLE is true, reenable issuing warnings. */ | |
1013 | ||
1014 | static void | |
1015 | c_enable_warnings (bool enable) | |
1016 | { | |
1017 | if (enable) | |
1018 | { | |
1019 | --c_inhibit_evaluation_warnings; | |
1020 | fold_undefer_and_ignore_overflow_warnings (); | |
1021 | } | |
1022 | } | |
1023 | ||
a75b1c71 | 1024 | /* Fully fold EXPR, an expression that was not folded (beyond integer |
1025 | constant expressions and null pointer constants) when being built | |
1026 | up. If IN_INIT, this is in a static initializer and certain | |
1027 | changes are made to the folding done. Clear *MAYBE_CONST if | |
1028 | MAYBE_CONST is not NULL and EXPR is definitely not a constant | |
1029 | expression because it contains an evaluated operator (in C99) or an | |
1030 | operator outside of sizeof returning an integer constant (in C90) | |
1031 | not permitted in constant expressions, or because it contains an | |
1032 | evaluated arithmetic overflow. (*MAYBE_CONST should typically be | |
1033 | set to true by callers before calling this function.) Return the | |
1034 | folded expression. Function arguments have already been folded | |
1035 | before calling this function, as have the contents of SAVE_EXPR, | |
1036 | TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and | |
1037 | C_MAYBE_CONST_EXPR. */ | |
1038 | ||
1039 | tree | |
1040 | c_fully_fold (tree expr, bool in_init, bool *maybe_const) | |
1041 | { | |
1042 | tree ret; | |
c6418a4e | 1043 | tree eptype = NULL_TREE; |
a75b1c71 | 1044 | bool dummy = true; |
1045 | bool maybe_const_itself = true; | |
389dd41b | 1046 | location_t loc = EXPR_LOCATION (expr); |
a75b1c71 | 1047 | |
1048 | /* This function is not relevant to C++ because C++ folds while | |
1049 | parsing, and may need changes to be correct for C++ when C++ | |
1050 | stops folding while parsing. */ | |
1051 | if (c_dialect_cxx ()) | |
1052 | gcc_unreachable (); | |
1053 | ||
1054 | if (!maybe_const) | |
1055 | maybe_const = &dummy; | |
c6418a4e | 1056 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) |
1057 | { | |
1058 | eptype = TREE_TYPE (expr); | |
1059 | expr = TREE_OPERAND (expr, 0); | |
1060 | } | |
a75b1c71 | 1061 | ret = c_fully_fold_internal (expr, in_init, maybe_const, |
1062 | &maybe_const_itself); | |
c6418a4e | 1063 | if (eptype) |
389dd41b | 1064 | ret = fold_convert_loc (loc, eptype, ret); |
a75b1c71 | 1065 | *maybe_const &= maybe_const_itself; |
1066 | return ret; | |
1067 | } | |
1068 | ||
1069 | /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for | |
1070 | c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands | |
1071 | not permitted, while *MAYBE_CONST_ITSELF is cleared because of | |
1072 | arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from | |
1073 | both evaluated and unevaluated subexpressions while | |
1074 | *MAYBE_CONST_ITSELF is carried from only evaluated | |
1075 | subexpressions). */ | |
1076 | ||
1077 | static tree | |
1078 | c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands, | |
1079 | bool *maybe_const_itself) | |
1080 | { | |
1081 | tree ret = expr; | |
1082 | enum tree_code code = TREE_CODE (expr); | |
1083 | enum tree_code_class kind = TREE_CODE_CLASS (code); | |
1084 | location_t loc = EXPR_LOCATION (expr); | |
1085 | tree op0, op1, op2, op3; | |
1086 | tree orig_op0, orig_op1, orig_op2; | |
1087 | bool op0_const = true, op1_const = true, op2_const = true; | |
1088 | bool op0_const_self = true, op1_const_self = true, op2_const_self = true; | |
1089 | bool nowarning = TREE_NO_WARNING (expr); | |
fc501191 | 1090 | bool unused_p; |
a75b1c71 | 1091 | |
1092 | /* This function is not relevant to C++ because C++ folds while | |
1093 | parsing, and may need changes to be correct for C++ when C++ | |
1094 | stops folding while parsing. */ | |
1095 | if (c_dialect_cxx ()) | |
1096 | gcc_unreachable (); | |
1097 | ||
1098 | /* Constants, declarations, statements, errors, SAVE_EXPRs and | |
1099 | anything else not counted as an expression cannot usefully be | |
1100 | folded further at this point. */ | |
1101 | if (!IS_EXPR_CODE_CLASS (kind) | |
1102 | || kind == tcc_statement | |
1103 | || code == SAVE_EXPR) | |
1104 | return expr; | |
1105 | ||
1106 | /* Operands of variable-length expressions (function calls) have | |
1107 | already been folded, as have __builtin_* function calls, and such | |
1108 | expressions cannot occur in constant expressions. */ | |
1109 | if (kind == tcc_vl_exp) | |
1110 | { | |
1111 | *maybe_const_operands = false; | |
1112 | ret = fold (expr); | |
1113 | goto out; | |
1114 | } | |
1115 | ||
1116 | if (code == C_MAYBE_CONST_EXPR) | |
1117 | { | |
1118 | tree pre = C_MAYBE_CONST_EXPR_PRE (expr); | |
1119 | tree inner = C_MAYBE_CONST_EXPR_EXPR (expr); | |
1120 | if (C_MAYBE_CONST_EXPR_NON_CONST (expr)) | |
1121 | *maybe_const_operands = false; | |
1122 | if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr)) | |
1123 | *maybe_const_itself = false; | |
1124 | if (pre && !in_init) | |
1125 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner); | |
1126 | else | |
1127 | ret = inner; | |
1128 | goto out; | |
1129 | } | |
1130 | ||
1131 | /* Assignment, increment, decrement, function call and comma | |
1132 | operators, and statement expressions, cannot occur in constant | |
1133 | expressions if evaluated / outside of sizeof. (Function calls | |
1134 | were handled above, though VA_ARG_EXPR is treated like a function | |
1135 | call here, and statement expressions are handled through | |
1136 | C_MAYBE_CONST_EXPR to avoid folding inside them.) */ | |
1137 | switch (code) | |
1138 | { | |
1139 | case MODIFY_EXPR: | |
1140 | case PREDECREMENT_EXPR: | |
1141 | case PREINCREMENT_EXPR: | |
1142 | case POSTDECREMENT_EXPR: | |
1143 | case POSTINCREMENT_EXPR: | |
1144 | case COMPOUND_EXPR: | |
1145 | *maybe_const_operands = false; | |
1146 | break; | |
1147 | ||
1148 | case VA_ARG_EXPR: | |
1149 | case TARGET_EXPR: | |
1150 | case BIND_EXPR: | |
1151 | case OBJ_TYPE_REF: | |
1152 | *maybe_const_operands = false; | |
1153 | ret = fold (expr); | |
1154 | goto out; | |
1155 | ||
1156 | default: | |
1157 | break; | |
1158 | } | |
1159 | ||
1160 | /* Fold individual tree codes as appropriate. */ | |
1161 | switch (code) | |
1162 | { | |
1163 | case COMPOUND_LITERAL_EXPR: | |
1164 | /* Any non-constancy will have been marked in a containing | |
1165 | C_MAYBE_CONST_EXPR; there is no more folding to do here. */ | |
1166 | goto out; | |
1167 | ||
1168 | case COMPONENT_REF: | |
1169 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1170 | op1 = TREE_OPERAND (expr, 1); | |
1171 | op2 = TREE_OPERAND (expr, 2); | |
1172 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1173 | maybe_const_itself); | |
f59e3889 | 1174 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1175 | if (op0 != orig_op0) |
1176 | ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2); | |
1177 | if (ret != expr) | |
1178 | { | |
1179 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1180 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1181 | } | |
1182 | goto out; | |
1183 | ||
1184 | case ARRAY_REF: | |
1185 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1186 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1187 | op2 = TREE_OPERAND (expr, 2); | |
1188 | op3 = TREE_OPERAND (expr, 3); | |
1189 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1190 | maybe_const_itself); | |
f59e3889 | 1191 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1192 | op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, |
1193 | maybe_const_itself); | |
f59e3889 | 1194 | STRIP_TYPE_NOPS (op1); |
a75b1c71 | 1195 | op1 = decl_constant_value_for_optimization (op1); |
1196 | if (op0 != orig_op0 || op1 != orig_op1) | |
1197 | ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3); | |
1198 | if (ret != expr) | |
1199 | { | |
1200 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1201 | TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); | |
1202 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1203 | } | |
1204 | ret = fold (ret); | |
1205 | goto out; | |
1206 | ||
1207 | case COMPOUND_EXPR: | |
1208 | case MODIFY_EXPR: | |
1209 | case PREDECREMENT_EXPR: | |
1210 | case PREINCREMENT_EXPR: | |
1211 | case POSTDECREMENT_EXPR: | |
1212 | case POSTINCREMENT_EXPR: | |
1213 | case PLUS_EXPR: | |
1214 | case MINUS_EXPR: | |
1215 | case MULT_EXPR: | |
1216 | case POINTER_PLUS_EXPR: | |
1217 | case TRUNC_DIV_EXPR: | |
1218 | case CEIL_DIV_EXPR: | |
1219 | case FLOOR_DIV_EXPR: | |
1220 | case TRUNC_MOD_EXPR: | |
1221 | case RDIV_EXPR: | |
1222 | case EXACT_DIV_EXPR: | |
1223 | case LSHIFT_EXPR: | |
1224 | case RSHIFT_EXPR: | |
1225 | case BIT_IOR_EXPR: | |
1226 | case BIT_XOR_EXPR: | |
1227 | case BIT_AND_EXPR: | |
1228 | case LT_EXPR: | |
1229 | case LE_EXPR: | |
1230 | case GT_EXPR: | |
1231 | case GE_EXPR: | |
1232 | case EQ_EXPR: | |
1233 | case NE_EXPR: | |
1234 | case COMPLEX_EXPR: | |
1235 | case TRUTH_AND_EXPR: | |
1236 | case TRUTH_OR_EXPR: | |
1237 | case TRUTH_XOR_EXPR: | |
1238 | case UNORDERED_EXPR: | |
1239 | case ORDERED_EXPR: | |
1240 | case UNLT_EXPR: | |
1241 | case UNLE_EXPR: | |
1242 | case UNGT_EXPR: | |
1243 | case UNGE_EXPR: | |
1244 | case UNEQ_EXPR: | |
1245 | /* Binary operations evaluating both arguments (increment and | |
1246 | decrement are binary internally in GCC). */ | |
1247 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1248 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1249 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1250 | maybe_const_itself); | |
f59e3889 | 1251 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1252 | if (code != MODIFY_EXPR |
1253 | && code != PREDECREMENT_EXPR | |
1254 | && code != PREINCREMENT_EXPR | |
1255 | && code != POSTDECREMENT_EXPR | |
1256 | && code != POSTINCREMENT_EXPR) | |
1257 | op0 = decl_constant_value_for_optimization (op0); | |
1258 | /* The RHS of a MODIFY_EXPR was fully folded when building that | |
1259 | expression for the sake of conversion warnings. */ | |
1260 | if (code != MODIFY_EXPR) | |
1261 | op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, | |
1262 | maybe_const_itself); | |
f59e3889 | 1263 | STRIP_TYPE_NOPS (op1); |
a75b1c71 | 1264 | op1 = decl_constant_value_for_optimization (op1); |
1265 | if (op0 != orig_op0 || op1 != orig_op1 || in_init) | |
1266 | ret = in_init | |
389dd41b | 1267 | ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) |
1268 | : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); | |
a75b1c71 | 1269 | else |
1270 | ret = fold (expr); | |
672d914b | 1271 | if (TREE_OVERFLOW_P (ret) |
1272 | && !TREE_OVERFLOW_P (op0) | |
1273 | && !TREE_OVERFLOW_P (op1)) | |
1274 | overflow_warning (EXPR_LOCATION (expr), ret); | |
a75b1c71 | 1275 | goto out; |
1276 | ||
1277 | case INDIRECT_REF: | |
1278 | case FIX_TRUNC_EXPR: | |
1279 | case FLOAT_EXPR: | |
1280 | CASE_CONVERT: | |
2b19dfe4 | 1281 | case VIEW_CONVERT_EXPR: |
a75b1c71 | 1282 | case NON_LVALUE_EXPR: |
1283 | case NEGATE_EXPR: | |
1284 | case BIT_NOT_EXPR: | |
1285 | case TRUTH_NOT_EXPR: | |
1286 | case ADDR_EXPR: | |
1287 | case CONJ_EXPR: | |
1288 | case REALPART_EXPR: | |
1289 | case IMAGPART_EXPR: | |
1290 | /* Unary operations. */ | |
1291 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1292 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1293 | maybe_const_itself); | |
f59e3889 | 1294 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1295 | if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR) |
1296 | op0 = decl_constant_value_for_optimization (op0); | |
737a23cc | 1297 | /* ??? Cope with user tricks that amount to offsetof. The middle-end is |
1298 | not prepared to deal with them if they occur in initializers. */ | |
1299 | if (op0 != orig_op0 | |
1300 | && code == ADDR_EXPR | |
1301 | && (op1 = get_base_address (op0)) != NULL_TREE | |
1302 | && TREE_CODE (op1) == INDIRECT_REF | |
1303 | && TREE_CONSTANT (TREE_OPERAND (op1, 0))) | |
7549df0d | 1304 | ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0)); |
737a23cc | 1305 | else if (op0 != orig_op0 || in_init) |
a75b1c71 | 1306 | ret = in_init |
389dd41b | 1307 | ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0) |
1308 | : fold_build1_loc (loc, code, TREE_TYPE (expr), op0); | |
a75b1c71 | 1309 | else |
1310 | ret = fold (expr); | |
1311 | if (code == INDIRECT_REF | |
1312 | && ret != expr | |
1313 | && TREE_CODE (ret) == INDIRECT_REF) | |
1314 | { | |
1315 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1316 | TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); | |
1317 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1318 | } | |
672d914b | 1319 | switch (code) |
1320 | { | |
1321 | case FIX_TRUNC_EXPR: | |
1322 | case FLOAT_EXPR: | |
1323 | CASE_CONVERT: | |
1324 | /* Don't warn about explicit conversions. We will already | |
1325 | have warned about suspect implicit conversions. */ | |
1326 | break; | |
1327 | ||
1328 | default: | |
1329 | if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0)) | |
1330 | overflow_warning (EXPR_LOCATION (expr), ret); | |
1331 | break; | |
1332 | } | |
a75b1c71 | 1333 | goto out; |
1334 | ||
1335 | case TRUTH_ANDIF_EXPR: | |
1336 | case TRUTH_ORIF_EXPR: | |
1337 | /* Binary operations not necessarily evaluating both | |
1338 | arguments. */ | |
1339 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1340 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1341 | op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); | |
f59e3889 | 1342 | STRIP_TYPE_NOPS (op0); |
672d914b | 1343 | |
1344 | unused_p = (op0 == (code == TRUTH_ANDIF_EXPR | |
1345 | ? truthvalue_false_node | |
1346 | : truthvalue_true_node)); | |
fc501191 | 1347 | c_disable_warnings (unused_p); |
a75b1c71 | 1348 | op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); |
f59e3889 | 1349 | STRIP_TYPE_NOPS (op1); |
fc501191 | 1350 | c_enable_warnings (unused_p); |
672d914b | 1351 | |
a75b1c71 | 1352 | if (op0 != orig_op0 || op1 != orig_op1 || in_init) |
1353 | ret = in_init | |
389dd41b | 1354 | ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) |
1355 | : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); | |
a75b1c71 | 1356 | else |
1357 | ret = fold (expr); | |
1358 | *maybe_const_operands &= op0_const; | |
1359 | *maybe_const_itself &= op0_const_self; | |
1360 | if (!(flag_isoc99 | |
1361 | && op0_const | |
1362 | && op0_const_self | |
1363 | && (code == TRUTH_ANDIF_EXPR | |
1364 | ? op0 == truthvalue_false_node | |
1365 | : op0 == truthvalue_true_node))) | |
1366 | *maybe_const_operands &= op1_const; | |
1367 | if (!(op0_const | |
1368 | && op0_const_self | |
1369 | && (code == TRUTH_ANDIF_EXPR | |
1370 | ? op0 == truthvalue_false_node | |
1371 | : op0 == truthvalue_true_node))) | |
1372 | *maybe_const_itself &= op1_const_self; | |
1373 | goto out; | |
1374 | ||
1375 | case COND_EXPR: | |
1376 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1377 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1378 | orig_op2 = op2 = TREE_OPERAND (expr, 2); | |
1379 | op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); | |
672d914b | 1380 | |
f59e3889 | 1381 | STRIP_TYPE_NOPS (op0); |
fc501191 | 1382 | c_disable_warnings (op0 == truthvalue_false_node); |
a75b1c71 | 1383 | op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); |
f59e3889 | 1384 | STRIP_TYPE_NOPS (op1); |
fc501191 | 1385 | c_enable_warnings (op0 == truthvalue_false_node); |
672d914b | 1386 | |
fc501191 | 1387 | c_disable_warnings (op0 == truthvalue_true_node); |
a75b1c71 | 1388 | op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self); |
f59e3889 | 1389 | STRIP_TYPE_NOPS (op2); |
fc501191 | 1390 | c_enable_warnings (op0 == truthvalue_true_node); |
672d914b | 1391 | |
a75b1c71 | 1392 | if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2) |
389dd41b | 1393 | ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2); |
a75b1c71 | 1394 | else |
1395 | ret = fold (expr); | |
1396 | *maybe_const_operands &= op0_const; | |
1397 | *maybe_const_itself &= op0_const_self; | |
1398 | if (!(flag_isoc99 | |
1399 | && op0_const | |
1400 | && op0_const_self | |
1401 | && op0 == truthvalue_false_node)) | |
1402 | *maybe_const_operands &= op1_const; | |
1403 | if (!(op0_const | |
1404 | && op0_const_self | |
1405 | && op0 == truthvalue_false_node)) | |
1406 | *maybe_const_itself &= op1_const_self; | |
1407 | if (!(flag_isoc99 | |
1408 | && op0_const | |
1409 | && op0_const_self | |
1410 | && op0 == truthvalue_true_node)) | |
1411 | *maybe_const_operands &= op2_const; | |
1412 | if (!(op0_const | |
1413 | && op0_const_self | |
1414 | && op0 == truthvalue_true_node)) | |
1415 | *maybe_const_itself &= op2_const_self; | |
1416 | goto out; | |
1417 | ||
c6418a4e | 1418 | case EXCESS_PRECISION_EXPR: |
1419 | /* Each case where an operand with excess precision may be | |
1420 | encountered must remove the EXCESS_PRECISION_EXPR around | |
1421 | inner operands and possibly put one around the whole | |
1422 | expression or possibly convert to the semantic type (which | |
1423 | c_fully_fold does); we cannot tell at this stage which is | |
1424 | appropriate in any particular case. */ | |
1425 | gcc_unreachable (); | |
1426 | ||
a75b1c71 | 1427 | default: |
1428 | /* Various codes may appear through folding built-in functions | |
1429 | and their arguments. */ | |
1430 | goto out; | |
1431 | } | |
1432 | ||
1433 | out: | |
1434 | /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks | |
1435 | have been done by this point, so remove them again. */ | |
1436 | nowarning |= TREE_NO_WARNING (ret); | |
1437 | STRIP_TYPE_NOPS (ret); | |
1438 | if (nowarning && !TREE_NO_WARNING (ret)) | |
1439 | { | |
1440 | if (!CAN_HAVE_LOCATION_P (ret)) | |
1441 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
1442 | TREE_NO_WARNING (ret) = 1; | |
1443 | } | |
1444 | if (ret != expr) | |
1445 | protected_set_expr_location (ret, loc); | |
1446 | return ret; | |
1447 | } | |
1448 | ||
1449 | /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type, | |
1450 | return EXP. Otherwise, return either EXP or its known constant | |
1451 | value (if it has one), but return EXP if EXP has mode BLKmode. ??? | |
1452 | Is the BLKmode test appropriate? */ | |
1453 | ||
1454 | tree | |
1455 | decl_constant_value_for_optimization (tree exp) | |
1456 | { | |
1457 | tree ret; | |
1458 | ||
1459 | /* This function is only used by C, for c_fully_fold and other | |
1460 | optimization, and may not be correct for C++. */ | |
1461 | if (c_dialect_cxx ()) | |
1462 | gcc_unreachable (); | |
1463 | ||
1464 | if (!optimize | |
1465 | || TREE_CODE (exp) != VAR_DECL | |
1466 | || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE | |
1467 | || DECL_MODE (exp) == BLKmode) | |
1468 | return exp; | |
1469 | ||
1470 | ret = decl_constant_value (exp); | |
1471 | /* Avoid unwanted tree sharing between the initializer and current | |
1472 | function's body where the tree can be modified e.g. by the | |
1473 | gimplifier. */ | |
1474 | if (ret != exp && TREE_STATIC (exp)) | |
1475 | ret = unshare_expr (ret); | |
1476 | return ret; | |
1477 | } | |
1478 | ||
2a1736ed | 1479 | /* Print a warning if a constant expression had overflow in folding. |
1480 | Invoke this function on every expression that the language | |
1481 | requires to be a constant expression. | |
1482 | Note the ANSI C standard says it is erroneous for a | |
1483 | constant expression to overflow. */ | |
b2806639 | 1484 | |
1485 | void | |
1cae46be | 1486 | constant_expression_warning (tree value) |
07317e69 | 1487 | { |
48e1416a | 1488 | if (warn_overflow && pedantic |
07317e69 | 1489 | && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
1490 | || TREE_CODE (value) == FIXED_CST | |
1491 | || TREE_CODE (value) == VECTOR_CST | |
1492 | || TREE_CODE (value) == COMPLEX_CST) | |
1493 | && TREE_OVERFLOW (value)) | |
21ca8540 | 1494 | pedwarn (input_location, OPT_Woverflow, "overflow in constant expression"); |
07317e69 | 1495 | } |
1496 | ||
1497 | /* The same as above but print an unconditional error. */ | |
1498 | void | |
1499 | constant_expression_error (tree value) | |
b2806639 | 1500 | { |
837e1122 | 1501 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
9421ebb9 | 1502 | || TREE_CODE (value) == FIXED_CST |
886cfd4f | 1503 | || TREE_CODE (value) == VECTOR_CST |
837e1122 | 1504 | || TREE_CODE (value) == COMPLEX_CST) |
07317e69 | 1505 | && TREE_OVERFLOW (value)) |
1506 | error ("overflow in constant expression"); | |
2a1736ed | 1507 | } |
1508 | ||
f170d67f | 1509 | /* Print a warning if an expression had overflow in folding and its |
1510 | operands hadn't. | |
1511 | ||
2a1736ed | 1512 | Invoke this function on every expression that |
1513 | (1) appears in the source code, and | |
f170d67f | 1514 | (2) is a constant expression that overflowed, and |
2a1736ed | 1515 | (3) is not already checked by convert_and_check; |
f170d67f | 1516 | however, do not invoke this function on operands of explicit casts |
1517 | or when the expression is the result of an operator and any operand | |
1518 | already overflowed. */ | |
2a1736ed | 1519 | |
1520 | void | |
e60a6f7b | 1521 | overflow_warning (location_t loc, tree value) |
2a1736ed | 1522 | { |
48d94ede | 1523 | if (c_inhibit_evaluation_warnings != 0) |
1524 | return; | |
f170d67f | 1525 | |
1526 | switch (TREE_CODE (value)) | |
886cfd4f | 1527 | { |
f170d67f | 1528 | case INTEGER_CST: |
e60a6f7b | 1529 | warning_at (loc, OPT_Woverflow, "integer overflow in expression"); |
f170d67f | 1530 | break; |
48e1416a | 1531 | |
f170d67f | 1532 | case REAL_CST: |
e60a6f7b | 1533 | warning_at (loc, OPT_Woverflow, |
1534 | "floating point overflow in expression"); | |
f170d67f | 1535 | break; |
48e1416a | 1536 | |
9421ebb9 | 1537 | case FIXED_CST: |
e60a6f7b | 1538 | warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression"); |
9421ebb9 | 1539 | break; |
1540 | ||
f170d67f | 1541 | case VECTOR_CST: |
e60a6f7b | 1542 | warning_at (loc, OPT_Woverflow, "vector overflow in expression"); |
f170d67f | 1543 | break; |
48e1416a | 1544 | |
f170d67f | 1545 | case COMPLEX_CST: |
1546 | if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) | |
e60a6f7b | 1547 | warning_at (loc, OPT_Woverflow, |
1548 | "complex integer overflow in expression"); | |
f170d67f | 1549 | else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) |
e60a6f7b | 1550 | warning_at (loc, OPT_Woverflow, |
1551 | "complex floating point overflow in expression"); | |
f170d67f | 1552 | break; |
1553 | ||
1554 | default: | |
1555 | break; | |
886cfd4f | 1556 | } |
2a1736ed | 1557 | } |
1558 | ||
03033af4 | 1559 | /* Warn about uses of logical || / && operator in a context where it |
1560 | is likely that the bitwise equivalent was intended by the | |
1561 | programmer. We have seen an expression in which CODE is a binary | |
9c20c4fc | 1562 | operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding |
1563 | had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ | |
b13d1547 | 1564 | void |
9c20c4fc | 1565 | warn_logical_operator (location_t location, enum tree_code code, tree type, |
48e1416a | 1566 | enum tree_code code_left, tree op_left, |
03033af4 | 1567 | enum tree_code ARG_UNUSED (code_right), tree op_right) |
b13d1547 | 1568 | { |
9c20c4fc | 1569 | int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); |
1570 | int in0_p, in1_p, in_p; | |
1571 | tree low0, low1, low, high0, high1, high, lhs, rhs, tem; | |
1572 | bool strict_overflow_p = false; | |
1573 | ||
03033af4 | 1574 | if (code != TRUTH_ANDIF_EXPR |
1575 | && code != TRUTH_AND_EXPR | |
1576 | && code != TRUTH_ORIF_EXPR | |
1577 | && code != TRUTH_OR_EXPR) | |
1578 | return; | |
1579 | ||
1580 | /* Warn if &&/|| are being used in a context where it is | |
1581 | likely that the bitwise equivalent was intended by the | |
1582 | programmer. That is, an expression such as op && MASK | |
1583 | where op should not be any boolean expression, nor a | |
1584 | constant, and mask seems to be a non-boolean integer constant. */ | |
1585 | if (!truth_value_p (code_left) | |
1586 | && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) | |
1587 | && !CONSTANT_CLASS_P (op_left) | |
1588 | && !TREE_NO_WARNING (op_left) | |
1589 | && TREE_CODE (op_right) == INTEGER_CST | |
1590 | && !integer_zerop (op_right) | |
1591 | && !integer_onep (op_right)) | |
b13d1547 | 1592 | { |
9c20c4fc | 1593 | if (or_op) |
03033af4 | 1594 | warning_at (location, OPT_Wlogical_op, "logical %<or%>" |
1595 | " applied to non-boolean constant"); | |
1596 | else | |
1597 | warning_at (location, OPT_Wlogical_op, "logical %<and%>" | |
1598 | " applied to non-boolean constant"); | |
1599 | TREE_NO_WARNING (op_left) = true; | |
9c20c4fc | 1600 | return; |
1601 | } | |
1602 | ||
1603 | /* We do not warn for constants because they are typical of macro | |
1604 | expansions that test for features. */ | |
1605 | if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right)) | |
1606 | return; | |
1607 | ||
1608 | /* This warning only makes sense with logical operands. */ | |
1609 | if (!(truth_value_p (TREE_CODE (op_left)) | |
1610 | || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) | |
1611 | || !(truth_value_p (TREE_CODE (op_right)) | |
1612 | || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) | |
1613 | return; | |
1614 | ||
9c20c4fc | 1615 | |
686369e8 | 1616 | /* We first test whether either side separately is trivially true |
1617 | (with OR) or trivially false (with AND). If so, do not warn. | |
1618 | This is a common idiom for testing ranges of data types in | |
1619 | portable code. */ | |
1620 | lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); | |
1621 | if (!lhs) | |
1622 | return; | |
1623 | if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) | |
9c20c4fc | 1624 | lhs = C_MAYBE_CONST_EXPR_EXPR (lhs); |
1625 | ||
686369e8 | 1626 | /* If this is an OR operation, invert both sides; now, the result |
1627 | should be always false to get a warning. */ | |
1628 | if (or_op) | |
1629 | in0_p = !in0_p; | |
1630 | ||
1631 | tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0); | |
d42e7c5a | 1632 | if (tem && integer_zerop (tem)) |
686369e8 | 1633 | return; |
1634 | ||
1635 | rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); | |
1636 | if (!rhs) | |
1637 | return; | |
1638 | if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) | |
9c20c4fc | 1639 | rhs = C_MAYBE_CONST_EXPR_EXPR (rhs); |
48e1416a | 1640 | |
686369e8 | 1641 | /* If this is an OR operation, invert both sides; now, the result |
1642 | should be always false to get a warning. */ | |
9c20c4fc | 1643 | if (or_op) |
686369e8 | 1644 | in1_p = !in1_p; |
48e1416a | 1645 | |
686369e8 | 1646 | tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1); |
d42e7c5a | 1647 | if (tem && integer_zerop (tem)) |
686369e8 | 1648 | return; |
1649 | ||
1650 | /* If both expressions have the same operand, if we can merge the | |
1651 | ranges, and if the range test is always false, then warn. */ | |
1652 | if (operand_equal_p (lhs, rhs, 0) | |
9c20c4fc | 1653 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, |
1654 | in1_p, low1, high1) | |
389dd41b | 1655 | && 0 != (tem = build_range_check (UNKNOWN_LOCATION, |
686369e8 | 1656 | type, lhs, in_p, low, high)) |
1657 | && integer_zerop (tem)) | |
9c20c4fc | 1658 | { |
9c20c4fc | 1659 | if (or_op) |
1660 | warning_at (location, OPT_Wlogical_op, | |
1661 | "logical %<or%> " | |
1662 | "of collectively exhaustive tests is always true"); | |
1663 | else | |
1664 | warning_at (location, OPT_Wlogical_op, | |
1665 | "logical %<and%> " | |
1666 | "of mutually exclusive tests is always false"); | |
b13d1547 | 1667 | } |
1668 | } | |
1669 | ||
1670 | ||
3d177e8c | 1671 | /* Warn if EXP contains any computations whose results are not used. |
1672 | Return true if a warning is printed; false otherwise. LOCUS is the | |
1673 | (potential) location of the expression. */ | |
1674 | ||
1675 | bool | |
1676 | warn_if_unused_value (const_tree exp, location_t locus) | |
1677 | { | |
1678 | restart: | |
1679 | if (TREE_USED (exp) || TREE_NO_WARNING (exp)) | |
1680 | return false; | |
1681 | ||
1682 | /* Don't warn about void constructs. This includes casting to void, | |
1683 | void function calls, and statement expressions with a final cast | |
1684 | to void. */ | |
1685 | if (VOID_TYPE_P (TREE_TYPE (exp))) | |
1686 | return false; | |
1687 | ||
1688 | if (EXPR_HAS_LOCATION (exp)) | |
1689 | locus = EXPR_LOCATION (exp); | |
1690 | ||
1691 | switch (TREE_CODE (exp)) | |
1692 | { | |
1693 | case PREINCREMENT_EXPR: | |
1694 | case POSTINCREMENT_EXPR: | |
1695 | case PREDECREMENT_EXPR: | |
1696 | case POSTDECREMENT_EXPR: | |
1697 | case MODIFY_EXPR: | |
1698 | case INIT_EXPR: | |
1699 | case TARGET_EXPR: | |
1700 | case CALL_EXPR: | |
1701 | case TRY_CATCH_EXPR: | |
1702 | case WITH_CLEANUP_EXPR: | |
1703 | case EXIT_EXPR: | |
1704 | case VA_ARG_EXPR: | |
1705 | return false; | |
1706 | ||
1707 | case BIND_EXPR: | |
1708 | /* For a binding, warn if no side effect within it. */ | |
1709 | exp = BIND_EXPR_BODY (exp); | |
1710 | goto restart; | |
1711 | ||
1712 | case SAVE_EXPR: | |
1713 | case NON_LVALUE_EXPR: | |
d85dbdb3 | 1714 | case NOP_EXPR: |
3d177e8c | 1715 | exp = TREE_OPERAND (exp, 0); |
1716 | goto restart; | |
1717 | ||
1718 | case TRUTH_ORIF_EXPR: | |
1719 | case TRUTH_ANDIF_EXPR: | |
1720 | /* In && or ||, warn if 2nd operand has no side effect. */ | |
1721 | exp = TREE_OPERAND (exp, 1); | |
1722 | goto restart; | |
1723 | ||
1724 | case COMPOUND_EXPR: | |
1725 | if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus)) | |
1726 | return true; | |
1727 | /* Let people do `(foo (), 0)' without a warning. */ | |
1728 | if (TREE_CONSTANT (TREE_OPERAND (exp, 1))) | |
1729 | return false; | |
1730 | exp = TREE_OPERAND (exp, 1); | |
1731 | goto restart; | |
1732 | ||
1733 | case COND_EXPR: | |
1734 | /* If this is an expression with side effects, don't warn; this | |
1735 | case commonly appears in macro expansions. */ | |
1736 | if (TREE_SIDE_EFFECTS (exp)) | |
1737 | return false; | |
1738 | goto warn; | |
1739 | ||
1740 | case INDIRECT_REF: | |
1741 | /* Don't warn about automatic dereferencing of references, since | |
1742 | the user cannot control it. */ | |
1743 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE) | |
1744 | { | |
1745 | exp = TREE_OPERAND (exp, 0); | |
1746 | goto restart; | |
1747 | } | |
1748 | /* Fall through. */ | |
1749 | ||
1750 | default: | |
1751 | /* Referencing a volatile value is a side effect, so don't warn. */ | |
1752 | if ((DECL_P (exp) || REFERENCE_CLASS_P (exp)) | |
1753 | && TREE_THIS_VOLATILE (exp)) | |
1754 | return false; | |
1755 | ||
1756 | /* If this is an expression which has no operands, there is no value | |
1757 | to be unused. There are no such language-independent codes, | |
1758 | but front ends may define such. */ | |
1759 | if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0) | |
1760 | return false; | |
1761 | ||
1762 | warn: | |
1763 | return warning_at (locus, OPT_Wunused_value, "value computed is not used"); | |
1764 | } | |
1765 | } | |
1766 | ||
1767 | ||
bcf22371 | 1768 | /* Print a warning about casts that might indicate violation |
1769 | of strict aliasing rules if -Wstrict-aliasing is used and | |
1e31ff37 | 1770 | strict aliasing mode is in effect. OTYPE is the original |
1771 | TREE_TYPE of EXPR, and TYPE the type we're casting to. */ | |
bcf22371 | 1772 | |
e6fa0ea6 | 1773 | bool |
1e31ff37 | 1774 | strict_aliasing_warning (tree otype, tree type, tree expr) |
bcf22371 | 1775 | { |
45bb3afb | 1776 | /* Strip pointer conversion chains and get to the correct original type. */ |
1777 | STRIP_NOPS (expr); | |
1778 | otype = TREE_TYPE (expr); | |
1779 | ||
f06537f2 | 1780 | if (!(flag_strict_aliasing |
1781 | && POINTER_TYPE_P (type) | |
1782 | && POINTER_TYPE_P (otype) | |
1783 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1784 | /* If the type we are casting to is a ref-all pointer | |
1785 | dereferencing it is always valid. */ | |
1786 | || TYPE_REF_CAN_ALIAS_ALL (type)) | |
e6fa0ea6 | 1787 | return false; |
1788 | ||
1789 | if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR | |
bcf22371 | 1790 | && (DECL_P (TREE_OPERAND (expr, 0)) |
e6fa0ea6 | 1791 | || handled_component_p (TREE_OPERAND (expr, 0)))) |
bcf22371 | 1792 | { |
1793 | /* Casting the address of an object to non void pointer. Warn | |
1794 | if the cast breaks type based aliasing. */ | |
e6fa0ea6 | 1795 | if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) |
1796 | { | |
1797 | warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " | |
1798 | "might break strict-aliasing rules"); | |
1799 | return true; | |
1800 | } | |
bcf22371 | 1801 | else |
1802 | { | |
48e1416a | 1803 | /* warn_strict_aliasing >= 3. This includes the default (3). |
e6fa0ea6 | 1804 | Only warn if the cast is dereferenced immediately. */ |
32c2fdea | 1805 | alias_set_type set1 = |
e6fa0ea6 | 1806 | get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); |
32c2fdea | 1807 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); |
bcf22371 | 1808 | |
62d823d0 | 1809 | if (set1 != set2 && set2 != 0 |
1810 | && (set1 == 0 || !alias_sets_conflict_p (set1, set2))) | |
e6fa0ea6 | 1811 | { |
1812 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1813 | "pointer will break strict-aliasing rules"); | |
1814 | return true; | |
1815 | } | |
1816 | else if (warn_strict_aliasing == 2 | |
879f881c | 1817 | && !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1818 | { |
1819 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1820 | "pointer might break strict-aliasing rules"); | |
1821 | return true; | |
1822 | } | |
bcf22371 | 1823 | } |
1824 | } | |
e6fa0ea6 | 1825 | else |
1826 | if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) | |
1827 | { | |
1828 | /* At this level, warn for any conversions, even if an address is | |
1829 | not taken in the same statement. This will likely produce many | |
1830 | false positives, but could be useful to pinpoint problems that | |
1831 | are not revealed at higher levels. */ | |
32c2fdea | 1832 | alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); |
1833 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); | |
1834 | if (!COMPLETE_TYPE_P (type) | |
879f881c | 1835 | || !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1836 | { |
1837 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1838 | "pointer might break strict-aliasing rules"); | |
1839 | return true; | |
1840 | } | |
1841 | } | |
1842 | ||
1843 | return false; | |
bcf22371 | 1844 | } |
1845 | ||
f003f5dc | 1846 | /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with |
1847 | sizeof as last operand of certain builtins. */ | |
1848 | ||
1849 | void | |
1850 | sizeof_pointer_memaccess_warning (location_t loc, tree callee, | |
1851 | VEC(tree, gc) *params, tree sizeof_arg, | |
1852 | bool (*comp_types) (tree, tree)) | |
1853 | { | |
1854 | tree type, dest = NULL_TREE, src = NULL_TREE, tem; | |
1855 | bool strop = false; | |
1856 | ||
1857 | if (TREE_CODE (callee) != FUNCTION_DECL | |
1858 | || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL | |
1859 | || sizeof_arg == error_mark_node | |
1860 | || VEC_length (tree, params) <= 1) | |
1861 | return; | |
1862 | ||
1863 | type = TYPE_P (sizeof_arg) ? sizeof_arg : TREE_TYPE (sizeof_arg); | |
1864 | if (!POINTER_TYPE_P (type)) | |
1865 | return; | |
1866 | ||
1867 | switch (DECL_FUNCTION_CODE (callee)) | |
1868 | { | |
1869 | case BUILT_IN_STRNCMP: | |
1870 | case BUILT_IN_STRNCASECMP: | |
1871 | case BUILT_IN_STRNCPY: | |
1872 | case BUILT_IN_STRNCAT: | |
1873 | strop = true; | |
1874 | /* FALLTHRU */ | |
1875 | case BUILT_IN_MEMCPY: | |
1876 | case BUILT_IN_MEMMOVE: | |
1877 | case BUILT_IN_MEMCMP: | |
1878 | if (VEC_length (tree, params) < 3) | |
1879 | return; | |
1880 | src = VEC_index (tree, params, 1); | |
1881 | dest = VEC_index (tree, params, 0); | |
1882 | break; | |
1883 | case BUILT_IN_MEMSET: | |
1884 | if (VEC_length (tree, params) < 3) | |
1885 | return; | |
1886 | dest = VEC_index (tree, params, 0); | |
1887 | break; | |
1888 | case BUILT_IN_STRNDUP: | |
1889 | src = VEC_index (tree, params, 0); | |
1890 | strop = true; | |
1891 | break; | |
1892 | default: | |
1893 | break; | |
1894 | } | |
1895 | ||
1896 | if (dest | |
1897 | && (tem = tree_strip_nop_conversions (dest)) | |
1898 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
1899 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
1900 | return; | |
1901 | ||
1902 | if (src | |
1903 | && (tem = tree_strip_nop_conversions (src)) | |
1904 | && POINTER_TYPE_P (TREE_TYPE (tem)) | |
1905 | && comp_types (TREE_TYPE (TREE_TYPE (tem)), type)) | |
1906 | return; | |
1907 | ||
1908 | if (dest) | |
1909 | { | |
1910 | if (!TYPE_P (sizeof_arg) | |
1911 | && operand_equal_p (dest, sizeof_arg, 0) | |
1912 | && comp_types (TREE_TYPE (dest), type)) | |
1913 | { | |
1914 | if (TREE_CODE (sizeof_arg) == ADDR_EXPR && !strop) | |
1915 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1916 | "argument to %<sizeof%> in %qD call is the same " | |
1917 | "expression as the destination; did you mean to " | |
1918 | "remove the addressof?", callee); | |
1919 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
1920 | == TYPE_PRECISION (char_type_node)) | |
1921 | || strop) | |
1922 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1923 | "argument to %<sizeof%> in %qD call is the same " | |
1924 | "expression as the destination; did you mean to " | |
1925 | "provide an explicit length?", callee); | |
1926 | else | |
1927 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1928 | "argument to %<sizeof%> in %qD call is the same " | |
1929 | "expression as the destination; did you mean to " | |
1930 | "dereference it?", callee); | |
1931 | return; | |
1932 | } | |
1933 | ||
1934 | if (POINTER_TYPE_P (TREE_TYPE (dest)) | |
1935 | && !strop | |
1936 | && comp_types (TREE_TYPE (dest), type) | |
1937 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1938 | { | |
1939 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1940 | "argument to %<sizeof%> in %qD call is the same " | |
1941 | "pointer type %qT as the destination; expected %qT " | |
1942 | "or an explicit length", callee, TREE_TYPE (dest), | |
1943 | TREE_TYPE (TREE_TYPE (dest))); | |
1944 | return; | |
1945 | } | |
1946 | } | |
1947 | ||
1948 | if (src) | |
1949 | { | |
1950 | if (!TYPE_P (sizeof_arg) | |
1951 | && operand_equal_p (src, sizeof_arg, 0) | |
1952 | && comp_types (TREE_TYPE (src), type)) | |
1953 | { | |
1954 | if (TREE_CODE (sizeof_arg) == ADDR_EXPR && !strop) | |
1955 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1956 | "argument to %<sizeof%> in %qD call is the same " | |
1957 | "expression as the source; did you mean to " | |
1958 | "remove the addressof?", callee); | |
1959 | else if ((TYPE_PRECISION (TREE_TYPE (type)) | |
1960 | == TYPE_PRECISION (char_type_node)) | |
1961 | || strop) | |
1962 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1963 | "argument to %<sizeof%> in %qD call is the same " | |
1964 | "expression as the source; did you mean to " | |
1965 | "provide an explicit length?", callee); | |
1966 | else | |
1967 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1968 | "argument to %<sizeof%> in %qD call is the same " | |
1969 | "expression as the source; did you mean to " | |
1970 | "dereference it?", callee); | |
1971 | return; | |
1972 | } | |
1973 | ||
1974 | if (POINTER_TYPE_P (TREE_TYPE (src)) | |
1975 | && !strop | |
1976 | && comp_types (TREE_TYPE (src), type) | |
1977 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1978 | { | |
1979 | warning_at (loc, OPT_Wsizeof_pointer_memaccess, | |
1980 | "argument to %<sizeof%> in %qD call is the same " | |
1981 | "pointer type %qT as the source; expected %qT " | |
1982 | "or an explicit length", callee, TREE_TYPE (src), | |
1983 | TREE_TYPE (TREE_TYPE (src))); | |
1984 | return; | |
1985 | } | |
1986 | } | |
1987 | } | |
1988 | ||
3f08e399 | 1989 | /* Warn for unlikely, improbable, or stupid DECL declarations |
1990 | of `main'. */ | |
1991 | ||
1992 | void | |
1993 | check_main_parameter_types (tree decl) | |
1994 | { | |
d0af78c5 | 1995 | function_args_iterator iter; |
1996 | tree type; | |
3f08e399 | 1997 | int argct = 0; |
1998 | ||
d0af78c5 | 1999 | FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) |
2000 | { | |
2001 | /* XXX void_type_node belies the abstraction. */ | |
2002 | if (type == void_type_node || type == error_mark_node ) | |
2003 | break; | |
2004 | ||
2005 | ++argct; | |
2006 | switch (argct) | |
2007 | { | |
2008 | case 1: | |
2009 | if (TYPE_MAIN_VARIANT (type) != integer_type_node) | |
2010 | pedwarn (input_location, OPT_Wmain, | |
2011 | "first argument of %q+D should be %<int%>", decl); | |
2012 | break; | |
2013 | ||
2014 | case 2: | |
2015 | if (TREE_CODE (type) != POINTER_TYPE | |
2016 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
2017 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
2018 | != char_type_node)) | |
2019 | pedwarn (input_location, OPT_Wmain, | |
2020 | "second argument of %q+D should be %<char **%>", decl); | |
2021 | break; | |
2022 | ||
2023 | case 3: | |
2024 | if (TREE_CODE (type) != POINTER_TYPE | |
2025 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
2026 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
2027 | != char_type_node)) | |
2028 | pedwarn (input_location, OPT_Wmain, | |
2029 | "third argument of %q+D should probably be " | |
2030 | "%<char **%>", decl); | |
2031 | break; | |
2032 | } | |
2033 | } | |
3f08e399 | 2034 | |
2035 | /* It is intentional that this message does not mention the third | |
2036 | argument because it's only mentioned in an appendix of the | |
2037 | standard. */ | |
2038 | if (argct > 0 && (argct < 2 || argct > 3)) | |
d0af78c5 | 2039 | pedwarn (input_location, OPT_Wmain, |
2040 | "%q+D takes only zero or two arguments", decl); | |
3f08e399 | 2041 | } |
2042 | ||
ed7c4e62 | 2043 | /* True if pointers to distinct types T1 and T2 can be converted to |
2044 | each other without an explicit cast. Only returns true for opaque | |
2045 | vector types. */ | |
2046 | bool | |
2047 | vector_targets_convertible_p (const_tree t1, const_tree t2) | |
2048 | { | |
2049 | if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE | |
8d125f7d | 2050 | && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
ed7c4e62 | 2051 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
2052 | return true; | |
2053 | ||
2054 | return false; | |
2055 | } | |
2056 | ||
546c4794 | 2057 | /* True if vector types T1 and T2 can be converted to each other |
2058 | without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2 | |
2059 | can only be converted with -flax-vector-conversions yet that is not | |
2060 | in effect, emit a note telling the user about that option if such | |
2061 | a note has not previously been emitted. */ | |
2062 | bool | |
9f627b1a | 2063 | vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note) |
8b4b9810 | 2064 | { |
546c4794 | 2065 | static bool emitted_lax_note = false; |
ae6db8ab | 2066 | bool convertible_lax; |
2067 | ||
8d125f7d | 2068 | if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
ae6db8ab | 2069 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
2070 | return true; | |
2071 | ||
2072 | convertible_lax = | |
2073 | (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) | |
2074 | && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || | |
2075 | TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) | |
2076 | && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) | |
2077 | == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); | |
546c4794 | 2078 | |
2079 | if (!convertible_lax || flag_lax_vector_conversions) | |
2080 | return convertible_lax; | |
2081 | ||
2082 | if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) | |
29f51994 | 2083 | && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
546c4794 | 2084 | return true; |
2085 | ||
2086 | if (emit_lax_note && !emitted_lax_note) | |
2087 | { | |
2088 | emitted_lax_note = true; | |
5bcc316e | 2089 | inform (input_location, "use -flax-vector-conversions to permit " |
546c4794 | 2090 | "conversions between vectors with differing " |
2091 | "element types or numbers of subparts"); | |
2092 | } | |
2093 | ||
2094 | return false; | |
8b4b9810 | 2095 | } |
2096 | ||
bf0cb017 | 2097 | /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes |
2098 | and have vector types, V0 has the same type as V1, and the number of | |
2099 | elements of V0, V1, MASK is the same. | |
2100 | ||
2101 | In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was | |
2102 | called with two arguments. In this case implementation passes the | |
2103 | first argument twice in order to share the same tree code. This fact | |
2104 | could enable the mask-values being twice the vector length. This is | |
2105 | an implementation accident and this semantics is not guaranteed to | |
2106 | the user. */ | |
2107 | tree | |
2108 | c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask) | |
2109 | { | |
2110 | tree ret; | |
2111 | bool wrap = true; | |
2112 | bool maybe_const = false; | |
2113 | bool two_arguments = false; | |
2114 | ||
2115 | if (v1 == NULL_TREE) | |
2116 | { | |
2117 | two_arguments = true; | |
2118 | v1 = v0; | |
2119 | } | |
2120 | ||
2121 | if (v0 == error_mark_node || v1 == error_mark_node | |
2122 | || mask == error_mark_node) | |
2123 | return error_mark_node; | |
2124 | ||
2125 | if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE | |
2126 | || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE) | |
2127 | { | |
2128 | error_at (loc, "__builtin_shuffle last argument must " | |
2129 | "be an integer vector"); | |
2130 | return error_mark_node; | |
2131 | } | |
2132 | ||
2133 | if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE | |
2134 | || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE) | |
2135 | { | |
2136 | error_at (loc, "__builtin_shuffle arguments must be vectors"); | |
2137 | return error_mark_node; | |
2138 | } | |
2139 | ||
2140 | if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1))) | |
2141 | { | |
2142 | error_at (loc, "__builtin_shuffle argument vectors must be of " | |
2143 | "the same type"); | |
2144 | return error_mark_node; | |
2145 | } | |
2146 | ||
2147 | if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)) | |
2148 | != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)) | |
2149 | && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)) | |
2150 | != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))) | |
2151 | { | |
2152 | error_at (loc, "__builtin_shuffle number of elements of the " | |
2153 | "argument vector(s) and the mask vector should " | |
2154 | "be the same"); | |
2155 | return error_mark_node; | |
2156 | } | |
2157 | ||
2158 | if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0)))) | |
2159 | != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask))))) | |
2160 | { | |
2161 | error_at (loc, "__builtin_shuffle argument vector(s) inner type " | |
2162 | "must have the same size as inner type of the mask"); | |
2163 | return error_mark_node; | |
2164 | } | |
2165 | ||
2166 | if (!c_dialect_cxx ()) | |
2167 | { | |
2168 | /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */ | |
2169 | v0 = c_fully_fold (v0, false, &maybe_const); | |
2170 | wrap &= maybe_const; | |
2171 | ||
2172 | if (two_arguments) | |
2173 | v1 = v0 = save_expr (v0); | |
2174 | else | |
2175 | { | |
2176 | v1 = c_fully_fold (v1, false, &maybe_const); | |
2177 | wrap &= maybe_const; | |
2178 | } | |
2179 | ||
2180 | mask = c_fully_fold (mask, false, &maybe_const); | |
2181 | wrap &= maybe_const; | |
2182 | } | |
2183 | ||
2184 | ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask); | |
2185 | ||
2186 | if (!c_dialect_cxx () && !wrap) | |
2187 | ret = c_wrap_maybe_const (ret, true); | |
2188 | ||
2189 | return ret; | |
2190 | } | |
2191 | ||
7f506bca | 2192 | /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum |
2193 | to integral type. */ | |
2194 | ||
2195 | static tree | |
2196 | c_common_get_narrower (tree op, int *unsignedp_ptr) | |
2197 | { | |
2198 | op = get_narrower (op, unsignedp_ptr); | |
2199 | ||
2200 | if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE | |
2201 | && ENUM_IS_SCOPED (TREE_TYPE (op))) | |
2202 | { | |
2203 | /* C++0x scoped enumerations don't implicitly convert to integral | |
2204 | type; if we stripped an explicit conversion to a larger type we | |
2205 | need to replace it so common_type will still work. */ | |
a51edb4c | 2206 | tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)), |
2207 | TYPE_UNSIGNED (TREE_TYPE (op))); | |
7f506bca | 2208 | op = fold_convert (type, op); |
2209 | } | |
2210 | return op; | |
2211 | } | |
2212 | ||
2561cea2 | 2213 | /* This is a helper function of build_binary_op. |
2214 | ||
2215 | For certain operations if both args were extended from the same | |
2216 | smaller type, do the arithmetic in that type and then extend. | |
2217 | ||
2218 | BITWISE indicates a bitwise operation. | |
2219 | For them, this optimization is safe only if | |
2220 | both args are zero-extended or both are sign-extended. | |
2221 | Otherwise, we might change the result. | |
2222 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
48e1416a | 2223 | but calculated in (unsigned short) it would be (unsigned short)-1. |
2561cea2 | 2224 | */ |
7f506bca | 2225 | tree |
2226 | shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise) | |
2561cea2 | 2227 | { |
2228 | int unsigned0, unsigned1; | |
2229 | tree arg0, arg1; | |
2230 | int uns; | |
2231 | tree type; | |
2232 | ||
2233 | /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents | |
2234 | excessive narrowing when we call get_narrower below. For | |
2235 | example, suppose that OP0 is of unsigned int extended | |
2236 | from signed char and that RESULT_TYPE is long long int. | |
2237 | If we explicitly cast OP0 to RESULT_TYPE, OP0 would look | |
2238 | like | |
48e1416a | 2239 | |
2561cea2 | 2240 | (long long int) (unsigned int) signed_char |
2241 | ||
2242 | which get_narrower would narrow down to | |
48e1416a | 2243 | |
2561cea2 | 2244 | (unsigned int) signed char |
48e1416a | 2245 | |
2561cea2 | 2246 | If we do not cast OP0 first, get_narrower would return |
2247 | signed_char, which is inconsistent with the case of the | |
2248 | explicit cast. */ | |
2249 | op0 = convert (result_type, op0); | |
2250 | op1 = convert (result_type, op1); | |
2251 | ||
7f506bca | 2252 | arg0 = c_common_get_narrower (op0, &unsigned0); |
2253 | arg1 = c_common_get_narrower (op1, &unsigned1); | |
2561cea2 | 2254 | |
2255 | /* UNS is 1 if the operation to be done is an unsigned one. */ | |
2256 | uns = TYPE_UNSIGNED (result_type); | |
2257 | ||
2258 | /* Handle the case that OP0 (or OP1) does not *contain* a conversion | |
2259 | but it *requires* conversion to FINAL_TYPE. */ | |
48e1416a | 2260 | |
2561cea2 | 2261 | if ((TYPE_PRECISION (TREE_TYPE (op0)) |
2262 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2263 | && TREE_TYPE (op0) != result_type) | |
2264 | unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); | |
2265 | if ((TYPE_PRECISION (TREE_TYPE (op1)) | |
2266 | == TYPE_PRECISION (TREE_TYPE (arg1))) | |
2267 | && TREE_TYPE (op1) != result_type) | |
2268 | unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
48e1416a | 2269 | |
2561cea2 | 2270 | /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ |
48e1416a | 2271 | |
2561cea2 | 2272 | /* For bitwise operations, signedness of nominal type |
2273 | does not matter. Consider only how operands were extended. */ | |
2274 | if (bitwise) | |
2275 | uns = unsigned0; | |
48e1416a | 2276 | |
2561cea2 | 2277 | /* Note that in all three cases below we refrain from optimizing |
2278 | an unsigned operation on sign-extended args. | |
2279 | That would not be valid. */ | |
48e1416a | 2280 | |
2561cea2 | 2281 | /* Both args variable: if both extended in same way |
2282 | from same width, do it in that width. | |
2283 | Do it unsigned if args were zero-extended. */ | |
2284 | if ((TYPE_PRECISION (TREE_TYPE (arg0)) | |
2285 | < TYPE_PRECISION (result_type)) | |
2286 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2287 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
2288 | && unsigned0 == unsigned1 | |
2289 | && (unsigned0 || !uns)) | |
2290 | return c_common_signed_or_unsigned_type | |
2291 | (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); | |
2292 | ||
2293 | else if (TREE_CODE (arg0) == INTEGER_CST | |
2294 | && (unsigned1 || !uns) | |
2295 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
2296 | < TYPE_PRECISION (result_type)) | |
2297 | && (type | |
2298 | = c_common_signed_or_unsigned_type (unsigned1, | |
2299 | TREE_TYPE (arg1))) | |
2300 | && !POINTER_TYPE_P (type) | |
2301 | && int_fits_type_p (arg0, type)) | |
2302 | return type; | |
2303 | ||
2304 | else if (TREE_CODE (arg1) == INTEGER_CST | |
2305 | && (unsigned0 || !uns) | |
2306 | && (TYPE_PRECISION (TREE_TYPE (arg0)) | |
2307 | < TYPE_PRECISION (result_type)) | |
2308 | && (type | |
2309 | = c_common_signed_or_unsigned_type (unsigned0, | |
2310 | TREE_TYPE (arg0))) | |
2311 | && !POINTER_TYPE_P (type) | |
2312 | && int_fits_type_p (arg1, type)) | |
2313 | return type; | |
2314 | ||
2315 | return result_type; | |
2316 | } | |
2317 | ||
7dfa155b | 2318 | /* Checks if expression EXPR of real/integer type cannot be converted |
2319 | to the real/integer type TYPE. Function returns true when: | |
2320 | * EXPR is a constant which cannot be exactly converted to TYPE | |
2321 | * EXPR is not a constant and size of EXPR's type > than size of TYPE, | |
2322 | for EXPR type and TYPE being both integers or both real. | |
2323 | * EXPR is not a constant of real type and TYPE is an integer. | |
2324 | * EXPR is not a constant of integer type which cannot be | |
2325 | exactly converted to real type. | |
2326 | Function allows conversions between types of different signedness and | |
2327 | does not return true in that case. Function can produce signedness | |
2328 | warnings if PRODUCE_WARNS is true. */ | |
2329 | bool | |
2330 | unsafe_conversion_p (tree type, tree expr, bool produce_warns) | |
d31d55f0 | 2331 | { |
2332 | bool give_warning = false; | |
27259707 | 2333 | tree expr_type = TREE_TYPE (expr); |
8637f1db | 2334 | location_t loc = EXPR_LOC_OR_HERE (expr); |
d31d55f0 | 2335 | |
7dfa155b | 2336 | if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST) |
5b16c152 | 2337 | { |
d31d55f0 | 2338 | /* Warn for real constant that is not an exact integer converted |
7dfa155b | 2339 | to integer type. */ |
27259707 | 2340 | if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2341 | && TREE_CODE (type) == INTEGER_TYPE) |
2342 | { | |
2343 | if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) | |
2344 | give_warning = true; | |
2345 | } | |
da1fb07b | 2346 | /* Warn for an integer constant that does not fit into integer type. */ |
27259707 | 2347 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2348 | && TREE_CODE (type) == INTEGER_TYPE |
2349 | && !int_fits_type_p (expr, type)) | |
2350 | { | |
2351 | if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type) | |
27259707 | 2352 | && tree_int_cst_sgn (expr) < 0) |
7dfa155b | 2353 | { |
2354 | if (produce_warns) | |
2355 | warning_at (loc, OPT_Wsign_conversion, "negative integer" | |
2356 | " implicitly converted to unsigned type"); | |
2357 | } | |
2358 | else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type)) | |
2359 | { | |
2360 | if (produce_warns) | |
2361 | warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned" | |
2362 | " constant value to negative integer"); | |
2363 | } | |
7ee0d227 | 2364 | else |
2365 | give_warning = true; | |
7dfa155b | 2366 | } |
d31d55f0 | 2367 | else if (TREE_CODE (type) == REAL_TYPE) |
7dfa155b | 2368 | { |
2369 | /* Warn for an integer constant that does not fit into real type. */ | |
2370 | if (TREE_CODE (expr_type) == INTEGER_TYPE) | |
2371 | { | |
2372 | REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); | |
2373 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
2374 | give_warning = true; | |
2375 | } | |
2376 | /* Warn for a real constant that does not fit into a smaller | |
2377 | real type. */ | |
2378 | else if (TREE_CODE (expr_type) == REAL_TYPE | |
2379 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
2380 | { | |
2381 | REAL_VALUE_TYPE a = TREE_REAL_CST (expr); | |
2382 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
2383 | give_warning = true; | |
2384 | } | |
2385 | } | |
2386 | } | |
2387 | else | |
2388 | { | |
d31d55f0 | 2389 | /* Warn for real types converted to integer types. */ |
2561cea2 | 2390 | if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2391 | && TREE_CODE (type) == INTEGER_TYPE) |
2392 | give_warning = true; | |
d31d55f0 | 2393 | |
2561cea2 | 2394 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2395 | && TREE_CODE (type) == INTEGER_TYPE) |
2396 | { | |
69609004 | 2397 | /* Don't warn about unsigned char y = 0xff, x = (int) y; */ |
f9d856a4 | 2398 | expr = get_unwidened (expr, 0); |
2561cea2 | 2399 | expr_type = TREE_TYPE (expr); |
69609004 | 2400 | |
2561cea2 | 2401 | /* Don't warn for short y; short x = ((int)y & 0xff); */ |
48e1416a | 2402 | if (TREE_CODE (expr) == BIT_AND_EXPR |
7dfa155b | 2403 | || TREE_CODE (expr) == BIT_IOR_EXPR |
2561cea2 | 2404 | || TREE_CODE (expr) == BIT_XOR_EXPR) |
2405 | { | |
27259707 | 2406 | /* If both args were extended from a shortest type, |
2407 | use that type if that is safe. */ | |
48e1416a | 2408 | expr_type = shorten_binary_op (expr_type, |
2409 | TREE_OPERAND (expr, 0), | |
2410 | TREE_OPERAND (expr, 1), | |
2561cea2 | 2411 | /* bitwise */1); |
2412 | ||
2561cea2 | 2413 | if (TREE_CODE (expr) == BIT_AND_EXPR) |
2414 | { | |
2415 | tree op0 = TREE_OPERAND (expr, 0); | |
2416 | tree op1 = TREE_OPERAND (expr, 1); | |
30de145b | 2417 | bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
2418 | bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
2419 | ||
2420 | /* If one of the operands is a non-negative constant | |
2421 | that fits in the target type, then the type of the | |
2422 | other operand does not matter. */ | |
2561cea2 | 2423 | if ((TREE_CODE (op0) == INTEGER_CST |
2424 | && int_fits_type_p (op0, c_common_signed_type (type)) | |
2425 | && int_fits_type_p (op0, c_common_unsigned_type (type))) | |
2426 | || (TREE_CODE (op1) == INTEGER_CST | |
27259707 | 2427 | && int_fits_type_p (op1, c_common_signed_type (type)) |
48e1416a | 2428 | && int_fits_type_p (op1, |
27259707 | 2429 | c_common_unsigned_type (type)))) |
7dfa155b | 2430 | return false; |
30de145b | 2431 | /* If constant is unsigned and fits in the target |
2432 | type, then the result will also fit. */ | |
2433 | else if ((TREE_CODE (op0) == INTEGER_CST | |
48e1416a | 2434 | && unsigned0 |
30de145b | 2435 | && int_fits_type_p (op0, type)) |
2436 | || (TREE_CODE (op1) == INTEGER_CST | |
2437 | && unsigned1 | |
2438 | && int_fits_type_p (op1, type))) | |
7dfa155b | 2439 | return false; |
2561cea2 | 2440 | } |
2441 | } | |
7dfa155b | 2442 | /* Warn for integer types converted to smaller integer types. */ |
48e1416a | 2443 | if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
7ee0d227 | 2444 | give_warning = true; |
2445 | ||
2446 | /* When they are the same width but different signedness, | |
2447 | then the value may change. */ | |
7dfa155b | 2448 | else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type) |
2561cea2 | 2449 | && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type)) |
7ee0d227 | 2450 | /* Even when converted to a bigger type, if the type is |
2451 | unsigned but expr is signed, then negative values | |
2452 | will be changed. */ | |
7dfa155b | 2453 | || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type))) |
2454 | && produce_warns) | |
200dd99c | 2455 | warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT " |
2456 | "may change the sign of the result", | |
2457 | type, expr_type); | |
7dfa155b | 2458 | } |
d31d55f0 | 2459 | |
2460 | /* Warn for integer types converted to real types if and only if | |
7dfa155b | 2461 | all the range of values of the integer type cannot be |
2462 | represented by the real type. */ | |
2561cea2 | 2463 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
7dfa155b | 2464 | && TREE_CODE (type) == REAL_TYPE) |
2465 | { | |
4c2cfa81 | 2466 | tree type_low_bound, type_high_bound; |
7dfa155b | 2467 | REAL_VALUE_TYPE real_low_bound, real_high_bound; |
4c2cfa81 | 2468 | |
2469 | /* Don't warn about char y = 0xff; float x = (int) y; */ | |
2470 | expr = get_unwidened (expr, 0); | |
2471 | expr_type = TREE_TYPE (expr); | |
2472 | ||
7dfa155b | 2473 | type_low_bound = TYPE_MIN_VALUE (expr_type); |
2474 | type_high_bound = TYPE_MAX_VALUE (expr_type); | |
2475 | real_low_bound = real_value_from_int_cst (0, type_low_bound); | |
2476 | real_high_bound = real_value_from_int_cst (0, type_high_bound); | |
d31d55f0 | 2477 | |
7dfa155b | 2478 | if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound) |
2479 | || !exact_real_truncate (TYPE_MODE (type), &real_high_bound)) | |
2480 | give_warning = true; | |
2481 | } | |
d31d55f0 | 2482 | |
2483 | /* Warn for real types converted to smaller real types. */ | |
2561cea2 | 2484 | else if (TREE_CODE (expr_type) == REAL_TYPE |
7dfa155b | 2485 | && TREE_CODE (type) == REAL_TYPE |
2486 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
2487 | give_warning = true; | |
2488 | } | |
2489 | ||
2490 | return give_warning; | |
2491 | } | |
2492 | ||
2493 | /* Warns if the conversion of EXPR to TYPE may alter a value. | |
2494 | This is a helper function for warnings_for_convert_and_check. */ | |
2495 | ||
2496 | static void | |
2497 | conversion_warning (tree type, tree expr) | |
2498 | { | |
7dfa155b | 2499 | tree expr_type = TREE_TYPE (expr); |
2500 | location_t loc = EXPR_LOC_OR_HERE (expr); | |
d31d55f0 | 2501 | |
7dfa155b | 2502 | if (!warn_conversion && !warn_sign_conversion) |
2503 | return; | |
d31d55f0 | 2504 | |
7dfa155b | 2505 | switch (TREE_CODE (expr)) |
2506 | { | |
2507 | case EQ_EXPR: | |
2508 | case NE_EXPR: | |
2509 | case LE_EXPR: | |
2510 | case GE_EXPR: | |
2511 | case LT_EXPR: | |
2512 | case GT_EXPR: | |
2513 | case TRUTH_ANDIF_EXPR: | |
2514 | case TRUTH_ORIF_EXPR: | |
2515 | case TRUTH_AND_EXPR: | |
2516 | case TRUTH_OR_EXPR: | |
2517 | case TRUTH_XOR_EXPR: | |
2518 | case TRUTH_NOT_EXPR: | |
2519 | /* Conversion from boolean to a signed:1 bit-field (which only | |
2520 | can hold the values 0 and -1) doesn't lose information - but | |
2521 | it does change the value. */ | |
2522 | if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) | |
2523 | warning_at (loc, OPT_Wconversion, | |
2524 | "conversion to %qT from boolean expression", type); | |
2525 | return; | |
2526 | ||
2527 | case REAL_CST: | |
2528 | case INTEGER_CST: | |
2529 | if (unsafe_conversion_p (type, expr, true)) | |
2530 | warning_at (loc, OPT_Wconversion, | |
2531 | "conversion to %qT alters %qT constant value", | |
2532 | type, expr_type); | |
2533 | return; | |
2534 | ||
2535 | case COND_EXPR: | |
2536 | { | |
2537 | /* In case of COND_EXPR, if both operands are constants or | |
2538 | COND_EXPR, then we do not care about the type of COND_EXPR, | |
2539 | only about the conversion of each operand. */ | |
2540 | tree op1 = TREE_OPERAND (expr, 1); | |
2541 | tree op2 = TREE_OPERAND (expr, 2); | |
2542 | ||
2543 | if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST | |
2544 | || TREE_CODE (op1) == COND_EXPR) | |
2545 | && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST | |
2546 | || TREE_CODE (op2) == COND_EXPR)) | |
2547 | { | |
2548 | conversion_warning (type, op1); | |
2549 | conversion_warning (type, op2); | |
2550 | return; | |
2551 | } | |
2552 | /* Fall through. */ | |
2553 | } | |
2554 | ||
2555 | default: /* 'expr' is not a constant. */ | |
2556 | if (unsafe_conversion_p (type, expr, true)) | |
2557 | warning_at (loc, OPT_Wconversion, | |
200dd99c | 2558 | "conversion to %qT from %qT may alter its value", |
2559 | type, expr_type); | |
d31d55f0 | 2560 | } |
2561 | } | |
2562 | ||
59dd8856 | 2563 | /* Produce warnings after a conversion. RESULT is the result of |
2564 | converting EXPR to TYPE. This is a helper function for | |
2565 | convert_and_check and cp_convert_and_check. */ | |
2a1736ed | 2566 | |
59dd8856 | 2567 | void |
2568 | warnings_for_convert_and_check (tree type, tree expr, tree result) | |
2a1736ed | 2569 | { |
61f69bc9 | 2570 | location_t loc = EXPR_LOC_OR_HERE (expr); |
2571 | ||
da1fb07b | 2572 | if (TREE_CODE (expr) == INTEGER_CST |
2573 | && (TREE_CODE (type) == INTEGER_TYPE | |
2574 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
2575 | && !int_fits_type_p (expr, type)) | |
2576 | { | |
d31d55f0 | 2577 | /* Do not diagnose overflow in a constant expression merely |
2578 | because a conversion overflowed. */ | |
da1fb07b | 2579 | if (TREE_OVERFLOW (result)) |
eddad94a | 2580 | TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); |
2581 | ||
da1fb07b | 2582 | if (TYPE_UNSIGNED (type)) |
d31d55f0 | 2583 | { |
da1fb07b | 2584 | /* This detects cases like converting -129 or 256 to |
2585 | unsigned char. */ | |
2586 | if (!int_fits_type_p (expr, c_common_signed_type (type))) | |
61f69bc9 | 2587 | warning_at (loc, OPT_Woverflow, |
2588 | "large integer implicitly truncated to unsigned type"); | |
7ee0d227 | 2589 | else |
da1fb07b | 2590 | conversion_warning (type, expr); |
2591 | } | |
48e1416a | 2592 | else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) |
e0913805 | 2593 | warning (OPT_Woverflow, |
2594 | "overflow in implicit constant conversion"); | |
2595 | /* No warning for converting 0x80000000 to int. */ | |
2596 | else if (pedantic | |
2597 | && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE | |
2598 | || TYPE_PRECISION (TREE_TYPE (expr)) | |
2599 | != TYPE_PRECISION (type))) | |
61f69bc9 | 2600 | warning_at (loc, OPT_Woverflow, |
2601 | "overflow in implicit constant conversion"); | |
e0913805 | 2602 | |
7ee0d227 | 2603 | else |
e0913805 | 2604 | conversion_warning (type, expr); |
2a1736ed | 2605 | } |
9421ebb9 | 2606 | else if ((TREE_CODE (result) == INTEGER_CST |
2607 | || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) | |
61f69bc9 | 2608 | warning_at (loc, OPT_Woverflow, |
2609 | "overflow in implicit constant conversion"); | |
7ee0d227 | 2610 | else |
da1fb07b | 2611 | conversion_warning (type, expr); |
59dd8856 | 2612 | } |
2613 | ||
2614 | ||
2615 | /* Convert EXPR to TYPE, warning about conversion problems with constants. | |
2616 | Invoke this function on every expression that is converted implicitly, | |
2617 | i.e. because of language rules and not because of an explicit cast. */ | |
2618 | ||
2619 | tree | |
2620 | convert_and_check (tree type, tree expr) | |
2621 | { | |
2622 | tree result; | |
c6418a4e | 2623 | tree expr_for_warning; |
2624 | ||
2625 | /* Convert from a value with possible excess precision rather than | |
2626 | via the semantic type, but do not warn about values not fitting | |
2627 | exactly in the semantic type. */ | |
2628 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
2629 | { | |
2630 | tree orig_type = TREE_TYPE (expr); | |
2631 | expr = TREE_OPERAND (expr, 0); | |
2632 | expr_for_warning = convert (orig_type, expr); | |
2633 | if (orig_type == type) | |
2634 | return expr_for_warning; | |
2635 | } | |
2636 | else | |
2637 | expr_for_warning = expr; | |
59dd8856 | 2638 | |
2639 | if (TREE_TYPE (expr) == type) | |
2640 | return expr; | |
48e1416a | 2641 | |
59dd8856 | 2642 | result = convert (type, expr); |
2643 | ||
48d94ede | 2644 | if (c_inhibit_evaluation_warnings == 0 |
2645 | && !TREE_OVERFLOW_P (expr) | |
2646 | && result != error_mark_node) | |
c6418a4e | 2647 | warnings_for_convert_and_check (type, expr_for_warning, result); |
59dd8856 | 2648 | |
da1fb07b | 2649 | return result; |
b2806639 | 2650 | } |
2651 | \f | |
4e91a871 | 2652 | /* A node in a list that describes references to variables (EXPR), which are |
2653 | either read accesses if WRITER is zero, or write accesses, in which case | |
2654 | WRITER is the parent of EXPR. */ | |
2655 | struct tlist | |
2656 | { | |
2657 | struct tlist *next; | |
2658 | tree expr, writer; | |
2659 | }; | |
2660 | ||
2661 | /* Used to implement a cache the results of a call to verify_tree. We only | |
2662 | use this for SAVE_EXPRs. */ | |
2663 | struct tlist_cache | |
2664 | { | |
2665 | struct tlist_cache *next; | |
2666 | struct tlist *cache_before_sp; | |
2667 | struct tlist *cache_after_sp; | |
2668 | tree expr; | |
481c6ce6 | 2669 | }; |
2670 | ||
4e91a871 | 2671 | /* Obstack to use when allocating tlist structures, and corresponding |
2672 | firstobj. */ | |
2673 | static struct obstack tlist_obstack; | |
2674 | static char *tlist_firstobj = 0; | |
2675 | ||
2676 | /* Keep track of the identifiers we've warned about, so we can avoid duplicate | |
2677 | warnings. */ | |
2678 | static struct tlist *warned_ids; | |
2679 | /* SAVE_EXPRs need special treatment. We process them only once and then | |
2680 | cache the results. */ | |
2681 | static struct tlist_cache *save_expr_cache; | |
2682 | ||
1cae46be | 2683 | static void add_tlist (struct tlist **, struct tlist *, tree, int); |
2684 | static void merge_tlist (struct tlist **, struct tlist *, int); | |
2685 | static void verify_tree (tree, struct tlist **, struct tlist **, tree); | |
2686 | static int warning_candidate_p (tree); | |
79973b57 | 2687 | static bool candidate_equal_p (const_tree, const_tree); |
1cae46be | 2688 | static void warn_for_collisions (struct tlist *); |
2689 | static void warn_for_collisions_1 (tree, tree, struct tlist *, int); | |
2690 | static struct tlist *new_tlist (struct tlist *, tree, tree); | |
481c6ce6 | 2691 | |
4e91a871 | 2692 | /* Create a new struct tlist and fill in its fields. */ |
2693 | static struct tlist * | |
1cae46be | 2694 | new_tlist (struct tlist *next, tree t, tree writer) |
4e91a871 | 2695 | { |
2696 | struct tlist *l; | |
9318f22c | 2697 | l = XOBNEW (&tlist_obstack, struct tlist); |
4e91a871 | 2698 | l->next = next; |
2699 | l->expr = t; | |
2700 | l->writer = writer; | |
2701 | return l; | |
2702 | } | |
2703 | ||
2704 | /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER | |
2705 | is nonnull, we ignore any node we find which has a writer equal to it. */ | |
2706 | ||
2707 | static void | |
1cae46be | 2708 | add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy) |
4e91a871 | 2709 | { |
2710 | while (add) | |
2711 | { | |
2712 | struct tlist *next = add->next; | |
84166705 | 2713 | if (!copy) |
4e91a871 | 2714 | add->next = *to; |
79973b57 | 2715 | if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer)) |
4e91a871 | 2716 | *to = copy ? new_tlist (*to, add->expr, add->writer) : add; |
2717 | add = next; | |
2718 | } | |
2719 | } | |
2720 | ||
2721 | /* Merge the nodes of ADD into TO. This merging process is done so that for | |
2722 | each variable that already exists in TO, no new node is added; however if | |
2723 | there is a write access recorded in ADD, and an occurrence on TO is only | |
2724 | a read access, then the occurrence in TO will be modified to record the | |
2725 | write. */ | |
481c6ce6 | 2726 | |
2727 | static void | |
1cae46be | 2728 | merge_tlist (struct tlist **to, struct tlist *add, int copy) |
4e91a871 | 2729 | { |
2730 | struct tlist **end = to; | |
2731 | ||
2732 | while (*end) | |
2733 | end = &(*end)->next; | |
2734 | ||
2735 | while (add) | |
2736 | { | |
2737 | int found = 0; | |
2738 | struct tlist *tmp2; | |
2739 | struct tlist *next = add->next; | |
2740 | ||
2741 | for (tmp2 = *to; tmp2; tmp2 = tmp2->next) | |
79973b57 | 2742 | if (candidate_equal_p (tmp2->expr, add->expr)) |
4e91a871 | 2743 | { |
2744 | found = 1; | |
84166705 | 2745 | if (!tmp2->writer) |
4e91a871 | 2746 | tmp2->writer = add->writer; |
2747 | } | |
84166705 | 2748 | if (!found) |
4e91a871 | 2749 | { |
2750 | *end = copy ? add : new_tlist (NULL, add->expr, add->writer); | |
2751 | end = &(*end)->next; | |
2752 | *end = 0; | |
2753 | } | |
2754 | add = next; | |
2755 | } | |
2756 | } | |
2757 | ||
2758 | /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable | |
2759 | references in list LIST conflict with it, excluding reads if ONLY writers | |
2760 | is nonzero. */ | |
2761 | ||
2762 | static void | |
1cae46be | 2763 | warn_for_collisions_1 (tree written, tree writer, struct tlist *list, |
2764 | int only_writes) | |
4e91a871 | 2765 | { |
2766 | struct tlist *tmp; | |
2767 | ||
2768 | /* Avoid duplicate warnings. */ | |
2769 | for (tmp = warned_ids; tmp; tmp = tmp->next) | |
79973b57 | 2770 | if (candidate_equal_p (tmp->expr, written)) |
4e91a871 | 2771 | return; |
2772 | ||
2773 | while (list) | |
2774 | { | |
79973b57 | 2775 | if (candidate_equal_p (list->expr, written) |
2776 | && !candidate_equal_p (list->writer, writer) | |
2777 | && (!only_writes || list->writer)) | |
4e91a871 | 2778 | { |
2779 | warned_ids = new_tlist (warned_ids, written, NULL_TREE); | |
8637f1db | 2780 | warning_at (EXPR_LOC_OR_HERE (writer), |
6513b50d | 2781 | OPT_Wsequence_point, "operation on %qE may be undefined", |
2782 | list->expr); | |
4e91a871 | 2783 | } |
2784 | list = list->next; | |
2785 | } | |
2786 | } | |
2787 | ||
2788 | /* Given a list LIST of references to variables, find whether any of these | |
2789 | can cause conflicts due to missing sequence points. */ | |
2790 | ||
2791 | static void | |
1cae46be | 2792 | warn_for_collisions (struct tlist *list) |
4e91a871 | 2793 | { |
2794 | struct tlist *tmp; | |
1cae46be | 2795 | |
4e91a871 | 2796 | for (tmp = list; tmp; tmp = tmp->next) |
2797 | { | |
2798 | if (tmp->writer) | |
2799 | warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); | |
2800 | } | |
2801 | } | |
2802 | ||
734c98be | 2803 | /* Return nonzero if X is a tree that can be verified by the sequence point |
4e91a871 | 2804 | warnings. */ |
2805 | static int | |
1cae46be | 2806 | warning_candidate_p (tree x) |
481c6ce6 | 2807 | { |
6ef8d12f | 2808 | if (DECL_P (x) && DECL_ARTIFICIAL (x)) |
2809 | return 0; | |
2810 | ||
027fc6ef | 2811 | if (TREE_CODE (x) == BLOCK) |
2812 | return 0; | |
2813 | ||
6ef8d12f | 2814 | /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c |
79973b57 | 2815 | (lvalue_p) crash on TRY/CATCH. */ |
6ef8d12f | 2816 | if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x))) |
2817 | return 0; | |
2818 | ||
2819 | if (!lvalue_p (x)) | |
2820 | return 0; | |
2821 | ||
2822 | /* No point to track non-const calls, they will never satisfy | |
2823 | operand_equal_p. */ | |
2824 | if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0) | |
2825 | return 0; | |
2826 | ||
2827 | if (TREE_CODE (x) == STRING_CST) | |
2828 | return 0; | |
2829 | ||
2830 | return 1; | |
79973b57 | 2831 | } |
2832 | ||
2833 | /* Return nonzero if X and Y appear to be the same candidate (or NULL) */ | |
2834 | static bool | |
2835 | candidate_equal_p (const_tree x, const_tree y) | |
2836 | { | |
2837 | return (x == y) || (x && y && operand_equal_p (x, y, 0)); | |
4e91a871 | 2838 | } |
481c6ce6 | 2839 | |
4e91a871 | 2840 | /* Walk the tree X, and record accesses to variables. If X is written by the |
2841 | parent tree, WRITER is the parent. | |
2842 | We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this | |
2843 | expression or its only operand forces a sequence point, then everything up | |
2844 | to the sequence point is stored in PBEFORE_SP. Everything else gets stored | |
2845 | in PNO_SP. | |
2846 | Once we return, we will have emitted warnings if any subexpression before | |
2847 | such a sequence point could be undefined. On a higher level, however, the | |
2848 | sequence point may not be relevant, and we'll merge the two lists. | |
2849 | ||
2850 | Example: (b++, a) + b; | |
2851 | The call that processes the COMPOUND_EXPR will store the increment of B | |
2852 | in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that | |
2853 | processes the PLUS_EXPR will need to merge the two lists so that | |
2854 | eventually, all accesses end up on the same list (and we'll warn about the | |
2855 | unordered subexpressions b++ and b. | |
2856 | ||
2857 | A note on merging. If we modify the former example so that our expression | |
2858 | becomes | |
2859 | (b++, b) + a | |
2860 | care must be taken not simply to add all three expressions into the final | |
2861 | PNO_SP list. The function merge_tlist takes care of that by merging the | |
2862 | before-SP list of the COMPOUND_EXPR into its after-SP list in a special | |
2863 | way, so that no more than one access to B is recorded. */ | |
481c6ce6 | 2864 | |
4e91a871 | 2865 | static void |
1cae46be | 2866 | verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp, |
2867 | tree writer) | |
4e91a871 | 2868 | { |
2869 | struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; | |
2870 | enum tree_code code; | |
ce45a448 | 2871 | enum tree_code_class cl; |
481c6ce6 | 2872 | |
e5b75768 | 2873 | /* X may be NULL if it is the operand of an empty statement expression |
2874 | ({ }). */ | |
2875 | if (x == NULL) | |
2876 | return; | |
2877 | ||
4e91a871 | 2878 | restart: |
2879 | code = TREE_CODE (x); | |
e916c70c | 2880 | cl = TREE_CODE_CLASS (code); |
481c6ce6 | 2881 | |
4e91a871 | 2882 | if (warning_candidate_p (x)) |
79973b57 | 2883 | *pno_sp = new_tlist (*pno_sp, x, writer); |
4e91a871 | 2884 | |
2885 | switch (code) | |
2886 | { | |
67b28e3e | 2887 | case CONSTRUCTOR: |
2888 | return; | |
2889 | ||
4e91a871 | 2890 | case COMPOUND_EXPR: |
2891 | case TRUTH_ANDIF_EXPR: | |
2892 | case TRUTH_ORIF_EXPR: | |
2893 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
2894 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
2895 | warn_for_collisions (tmp_nosp); | |
2896 | merge_tlist (pbefore_sp, tmp_before, 0); | |
2897 | merge_tlist (pbefore_sp, tmp_nosp, 0); | |
2898 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); | |
2899 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
2900 | return; | |
2901 | ||
2902 | case COND_EXPR: | |
2903 | tmp_before = tmp_list2 = 0; | |
2904 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); | |
2905 | warn_for_collisions (tmp_list2); | |
2906 | merge_tlist (pbefore_sp, tmp_before, 0); | |
2907 | merge_tlist (pbefore_sp, tmp_list2, 1); | |
2908 | ||
2909 | tmp_list3 = tmp_nosp = 0; | |
2910 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); | |
2911 | warn_for_collisions (tmp_nosp); | |
2912 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
2913 | ||
2914 | tmp_list3 = tmp_list2 = 0; | |
2915 | verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); | |
2916 | warn_for_collisions (tmp_list2); | |
2917 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
2918 | /* Rather than add both tmp_nosp and tmp_list2, we have to merge the | |
2919 | two first, to avoid warning for (a ? b++ : b++). */ | |
2920 | merge_tlist (&tmp_nosp, tmp_list2, 0); | |
2921 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
2922 | return; | |
2923 | ||
481c6ce6 | 2924 | case PREDECREMENT_EXPR: |
2925 | case PREINCREMENT_EXPR: | |
2926 | case POSTDECREMENT_EXPR: | |
2927 | case POSTINCREMENT_EXPR: | |
4e91a871 | 2928 | verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); |
2929 | return; | |
2930 | ||
2931 | case MODIFY_EXPR: | |
2932 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
2933 | verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); | |
2934 | verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); | |
2935 | /* Expressions inside the LHS are not ordered wrt. the sequence points | |
2936 | in the RHS. Example: | |
2937 | *a = (a++, 2) | |
2938 | Despite the fact that the modification of "a" is in the before_sp | |
2939 | list (tmp_before), it conflicts with the use of "a" in the LHS. | |
2940 | We can handle this by adding the contents of tmp_list3 | |
2941 | to those of tmp_before, and redoing the collision warnings for that | |
2942 | list. */ | |
2943 | add_tlist (&tmp_before, tmp_list3, x, 1); | |
2944 | warn_for_collisions (tmp_before); | |
2945 | /* Exclude the LHS itself here; we first have to merge it into the | |
2946 | tmp_nosp list. This is done to avoid warning for "a = a"; if we | |
2947 | didn't exclude the LHS, we'd get it twice, once as a read and once | |
2948 | as a write. */ | |
2949 | add_tlist (pno_sp, tmp_list3, x, 0); | |
2950 | warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); | |
2951 | ||
2952 | merge_tlist (pbefore_sp, tmp_before, 0); | |
2953 | if (warning_candidate_p (TREE_OPERAND (x, 0))) | |
2954 | merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); | |
2955 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); | |
2956 | return; | |
481c6ce6 | 2957 | |
2958 | case CALL_EXPR: | |
4e91a871 | 2959 | /* We need to warn about conflicts among arguments and conflicts between |
2960 | args and the function address. Side effects of the function address, | |
2961 | however, are not ordered by the sequence point of the call. */ | |
c2f47e15 | 2962 | { |
2963 | call_expr_arg_iterator iter; | |
2964 | tree arg; | |
48e1416a | 2965 | tmp_before = tmp_nosp = 0; |
c2f47e15 | 2966 | verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE); |
2967 | FOR_EACH_CALL_EXPR_ARG (arg, iter, x) | |
2968 | { | |
2969 | tmp_list2 = tmp_list3 = 0; | |
2970 | verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE); | |
2971 | merge_tlist (&tmp_list3, tmp_list2, 0); | |
2972 | add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); | |
2973 | } | |
2974 | add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); | |
2975 | warn_for_collisions (tmp_before); | |
2976 | add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); | |
2977 | return; | |
2978 | } | |
481c6ce6 | 2979 | |
2980 | case TREE_LIST: | |
2981 | /* Scan all the list, e.g. indices of multi dimensional array. */ | |
2982 | while (x) | |
2983 | { | |
4e91a871 | 2984 | tmp_before = tmp_nosp = 0; |
2985 | verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
2986 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
2987 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
481c6ce6 | 2988 | x = TREE_CHAIN (x); |
2989 | } | |
4e91a871 | 2990 | return; |
481c6ce6 | 2991 | |
4e91a871 | 2992 | case SAVE_EXPR: |
2993 | { | |
2994 | struct tlist_cache *t; | |
2995 | for (t = save_expr_cache; t; t = t->next) | |
79973b57 | 2996 | if (candidate_equal_p (t->expr, x)) |
4e91a871 | 2997 | break; |
481c6ce6 | 2998 | |
84166705 | 2999 | if (!t) |
481c6ce6 | 3000 | { |
9318f22c | 3001 | t = XOBNEW (&tlist_obstack, struct tlist_cache); |
4e91a871 | 3002 | t->next = save_expr_cache; |
3003 | t->expr = x; | |
3004 | save_expr_cache = t; | |
3005 | ||
3006 | tmp_before = tmp_nosp = 0; | |
3007 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
3008 | warn_for_collisions (tmp_nosp); | |
3009 | ||
3010 | tmp_list3 = 0; | |
3011 | while (tmp_nosp) | |
3012 | { | |
3013 | struct tlist *t = tmp_nosp; | |
3014 | tmp_nosp = t->next; | |
3015 | merge_tlist (&tmp_list3, t, 0); | |
3016 | } | |
3017 | t->cache_before_sp = tmp_before; | |
3018 | t->cache_after_sp = tmp_list3; | |
481c6ce6 | 3019 | } |
4e91a871 | 3020 | merge_tlist (pbefore_sp, t->cache_before_sp, 1); |
3021 | add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); | |
3022 | return; | |
3023 | } | |
481c6ce6 | 3024 | |
012916cb | 3025 | case ADDR_EXPR: |
3026 | x = TREE_OPERAND (x, 0); | |
3027 | if (DECL_P (x)) | |
3028 | return; | |
3029 | writer = 0; | |
3030 | goto restart; | |
3031 | ||
ce45a448 | 3032 | default: |
3033 | /* For other expressions, simply recurse on their operands. | |
a0c938f0 | 3034 | Manual tail recursion for unary expressions. |
ce45a448 | 3035 | Other non-expressions need not be processed. */ |
3036 | if (cl == tcc_unary) | |
3037 | { | |
ce45a448 | 3038 | x = TREE_OPERAND (x, 0); |
3039 | writer = 0; | |
3040 | goto restart; | |
3041 | } | |
3042 | else if (IS_EXPR_CODE_CLASS (cl)) | |
3043 | { | |
3044 | int lp; | |
c2f47e15 | 3045 | int max = TREE_OPERAND_LENGTH (x); |
ce45a448 | 3046 | for (lp = 0; lp < max; lp++) |
3047 | { | |
3048 | tmp_before = tmp_nosp = 0; | |
3049 | verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0); | |
3050 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
3051 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
3052 | } | |
3053 | } | |
3054 | return; | |
481c6ce6 | 3055 | } |
481c6ce6 | 3056 | } |
3057 | ||
974e2c0c | 3058 | /* Try to warn for undefined behavior in EXPR due to missing sequence |
481c6ce6 | 3059 | points. */ |
3060 | ||
4b987fac | 3061 | DEBUG_FUNCTION void |
1cae46be | 3062 | verify_sequence_points (tree expr) |
481c6ce6 | 3063 | { |
4e91a871 | 3064 | struct tlist *before_sp = 0, *after_sp = 0; |
481c6ce6 | 3065 | |
4e91a871 | 3066 | warned_ids = 0; |
3067 | save_expr_cache = 0; | |
3068 | if (tlist_firstobj == 0) | |
481c6ce6 | 3069 | { |
4e91a871 | 3070 | gcc_obstack_init (&tlist_obstack); |
4fd61bc6 | 3071 | tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0); |
481c6ce6 | 3072 | } |
3073 | ||
4e91a871 | 3074 | verify_tree (expr, &before_sp, &after_sp, 0); |
3075 | warn_for_collisions (after_sp); | |
3076 | obstack_free (&tlist_obstack, tlist_firstobj); | |
481c6ce6 | 3077 | } |
b0fc3e72 | 3078 | \f |
3079 | /* Validate the expression after `case' and apply default promotions. */ | |
3080 | ||
2ca392fd | 3081 | static tree |
1cae46be | 3082 | check_case_value (tree value) |
b0fc3e72 | 3083 | { |
3084 | if (value == NULL_TREE) | |
3085 | return value; | |
3086 | ||
b96dc121 | 3087 | if (TREE_CODE (value) == INTEGER_CST) |
3088 | /* Promote char or short to int. */ | |
3089 | value = perform_integral_promotions (value); | |
3090 | else if (value != error_mark_node) | |
b0fc3e72 | 3091 | { |
3092 | error ("case label does not reduce to an integer constant"); | |
3093 | value = error_mark_node; | |
3094 | } | |
b0fc3e72 | 3095 | |
6433f1c2 | 3096 | constant_expression_warning (value); |
3097 | ||
b0fc3e72 | 3098 | return value; |
3099 | } | |
3100 | \f | |
2ca392fd | 3101 | /* See if the case values LOW and HIGH are in the range of the original |
5c9dae64 | 3102 | type (i.e. before the default conversion to int) of the switch testing |
2ca392fd | 3103 | expression. |
3104 | TYPE is the promoted type of the testing expression, and ORIG_TYPE is | |
91275768 | 3105 | the type before promoting it. CASE_LOW_P is a pointer to the lower |
2ca392fd | 3106 | bound of the case label, and CASE_HIGH_P is the upper bound or NULL |
3107 | if the case is not a case range. | |
3108 | The caller has to make sure that we are not called with NULL for | |
5c9dae64 | 3109 | CASE_LOW_P (i.e. the default case). |
442e3cb9 | 3110 | Returns true if the case label is in range of ORIG_TYPE (saturated or |
2ca392fd | 3111 | untouched) or false if the label is out of range. */ |
3112 | ||
3113 | static bool | |
3114 | check_case_bounds (tree type, tree orig_type, | |
3115 | tree *case_low_p, tree *case_high_p) | |
3116 | { | |
3117 | tree min_value, max_value; | |
3118 | tree case_low = *case_low_p; | |
3119 | tree case_high = case_high_p ? *case_high_p : case_low; | |
3120 | ||
3121 | /* If there was a problem with the original type, do nothing. */ | |
3122 | if (orig_type == error_mark_node) | |
3123 | return true; | |
3124 | ||
3125 | min_value = TYPE_MIN_VALUE (orig_type); | |
3126 | max_value = TYPE_MAX_VALUE (orig_type); | |
3127 | ||
3128 | /* Case label is less than minimum for type. */ | |
3129 | if (tree_int_cst_compare (case_low, min_value) < 0 | |
3130 | && tree_int_cst_compare (case_high, min_value) < 0) | |
3131 | { | |
c3ceba8e | 3132 | warning (0, "case label value is less than minimum value for type"); |
2ca392fd | 3133 | return false; |
3134 | } | |
b27ac6b5 | 3135 | |
2ca392fd | 3136 | /* Case value is greater than maximum for type. */ |
3137 | if (tree_int_cst_compare (case_low, max_value) > 0 | |
3138 | && tree_int_cst_compare (case_high, max_value) > 0) | |
3139 | { | |
c3ceba8e | 3140 | warning (0, "case label value exceeds maximum value for type"); |
2ca392fd | 3141 | return false; |
3142 | } | |
3143 | ||
3144 | /* Saturate lower case label value to minimum. */ | |
3145 | if (tree_int_cst_compare (case_high, min_value) >= 0 | |
3146 | && tree_int_cst_compare (case_low, min_value) < 0) | |
3147 | { | |
c3ceba8e | 3148 | warning (0, "lower value in case label range" |
2ca392fd | 3149 | " less than minimum value for type"); |
3150 | case_low = min_value; | |
3151 | } | |
b27ac6b5 | 3152 | |
2ca392fd | 3153 | /* Saturate upper case label value to maximum. */ |
3154 | if (tree_int_cst_compare (case_low, max_value) <= 0 | |
3155 | && tree_int_cst_compare (case_high, max_value) > 0) | |
3156 | { | |
c3ceba8e | 3157 | warning (0, "upper value in case label range" |
2ca392fd | 3158 | " exceeds maximum value for type"); |
3159 | case_high = max_value; | |
3160 | } | |
3161 | ||
3162 | if (*case_low_p != case_low) | |
3163 | *case_low_p = convert (type, case_low); | |
3164 | if (case_high_p && *case_high_p != case_high) | |
3165 | *case_high_p = convert (type, case_high); | |
3166 | ||
3167 | return true; | |
3168 | } | |
3169 | \f | |
b0fc3e72 | 3170 | /* Return an integer type with BITS bits of precision, |
3171 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
3172 | ||
3173 | tree | |
1cae46be | 3174 | c_common_type_for_size (unsigned int bits, int unsignedp) |
b0fc3e72 | 3175 | { |
46375237 | 3176 | if (bits == TYPE_PRECISION (integer_type_node)) |
3177 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3178 | ||
bacde65a | 3179 | if (bits == TYPE_PRECISION (signed_char_type_node)) |
b0fc3e72 | 3180 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
3181 | ||
bacde65a | 3182 | if (bits == TYPE_PRECISION (short_integer_type_node)) |
b0fc3e72 | 3183 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
3184 | ||
bacde65a | 3185 | if (bits == TYPE_PRECISION (long_integer_type_node)) |
b0fc3e72 | 3186 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
3187 | ||
bacde65a | 3188 | if (bits == TYPE_PRECISION (long_long_integer_type_node)) |
b0fc3e72 | 3189 | return (unsignedp ? long_long_unsigned_type_node |
3190 | : long_long_integer_type_node); | |
3191 | ||
6388cfe2 | 3192 | if (int128_integer_type_node |
3193 | && bits == TYPE_PRECISION (int128_integer_type_node)) | |
3194 | return (unsignedp ? int128_unsigned_type_node | |
3195 | : int128_integer_type_node); | |
3196 | ||
f57fa2ea | 3197 | if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) |
3198 | return (unsignedp ? widest_unsigned_literal_type_node | |
3199 | : widest_integer_literal_type_node); | |
3200 | ||
bacde65a | 3201 | if (bits <= TYPE_PRECISION (intQI_type_node)) |
3202 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3203 | ||
3204 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
3205 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3206 | ||
3207 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
3208 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3209 | ||
3210 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
3211 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3212 | ||
b0fc3e72 | 3213 | return 0; |
3214 | } | |
3215 | ||
9421ebb9 | 3216 | /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits |
3217 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed; | |
3218 | and saturating if SATP is nonzero, otherwise not saturating. */ | |
3219 | ||
3220 | tree | |
3221 | c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit, | |
3222 | int unsignedp, int satp) | |
3223 | { | |
3224 | enum machine_mode mode; | |
3225 | if (ibit == 0) | |
3226 | mode = unsignedp ? UQQmode : QQmode; | |
3227 | else | |
3228 | mode = unsignedp ? UHAmode : HAmode; | |
3229 | ||
3230 | for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode)) | |
3231 | if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit) | |
3232 | break; | |
3233 | ||
3234 | if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode)) | |
3235 | { | |
3236 | sorry ("GCC cannot support operators with integer types and " | |
3237 | "fixed-point types that have too many integral and " | |
3238 | "fractional bits together"); | |
3239 | return 0; | |
3240 | } | |
3241 | ||
3242 | return c_common_type_for_mode (mode, satp); | |
3243 | } | |
3244 | ||
5b247e9f | 3245 | /* Used for communication between c_common_type_for_mode and |
3246 | c_register_builtin_type. */ | |
3247 | static GTY(()) tree registered_builtin_types; | |
3248 | ||
b0fc3e72 | 3249 | /* Return a data type that has machine mode MODE. |
3250 | If the mode is an integer, | |
9421ebb9 | 3251 | then UNSIGNEDP selects between signed and unsigned types. |
3252 | If the mode is a fixed-point mode, | |
3253 | then UNSIGNEDP selects between saturating and nonsaturating types. */ | |
b0fc3e72 | 3254 | |
3255 | tree | |
1cae46be | 3256 | c_common_type_for_mode (enum machine_mode mode, int unsignedp) |
b0fc3e72 | 3257 | { |
5b247e9f | 3258 | tree t; |
3259 | ||
46375237 | 3260 | if (mode == TYPE_MODE (integer_type_node)) |
3261 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3262 | ||
b0fc3e72 | 3263 | if (mode == TYPE_MODE (signed_char_type_node)) |
3264 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3265 | ||
3266 | if (mode == TYPE_MODE (short_integer_type_node)) | |
3267 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3268 | ||
b0fc3e72 | 3269 | if (mode == TYPE_MODE (long_integer_type_node)) |
3270 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3271 | ||
3272 | if (mode == TYPE_MODE (long_long_integer_type_node)) | |
3273 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
3274 | ||
6388cfe2 | 3275 | if (int128_integer_type_node |
3276 | && mode == TYPE_MODE (int128_integer_type_node)) | |
3277 | return unsignedp ? int128_unsigned_type_node : int128_integer_type_node; | |
3278 | ||
f57fa2ea | 3279 | if (mode == TYPE_MODE (widest_integer_literal_type_node)) |
44e9fa65 | 3280 | return unsignedp ? widest_unsigned_literal_type_node |
4ee9c684 | 3281 | : widest_integer_literal_type_node; |
f57fa2ea | 3282 | |
88ae7f04 | 3283 | if (mode == QImode) |
bacde65a | 3284 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
3285 | ||
88ae7f04 | 3286 | if (mode == HImode) |
bacde65a | 3287 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
3288 | ||
88ae7f04 | 3289 | if (mode == SImode) |
bacde65a | 3290 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
3291 | ||
88ae7f04 | 3292 | if (mode == DImode) |
bacde65a | 3293 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
3294 | ||
cc1cc1c7 | 3295 | #if HOST_BITS_PER_WIDE_INT >= 64 |
6274009c | 3296 | if (mode == TYPE_MODE (intTI_type_node)) |
3297 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
cc1cc1c7 | 3298 | #endif |
6274009c | 3299 | |
b0fc3e72 | 3300 | if (mode == TYPE_MODE (float_type_node)) |
3301 | return float_type_node; | |
3302 | ||
3303 | if (mode == TYPE_MODE (double_type_node)) | |
3304 | return double_type_node; | |
3305 | ||
3306 | if (mode == TYPE_MODE (long_double_type_node)) | |
3307 | return long_double_type_node; | |
3308 | ||
545c2bde | 3309 | if (mode == TYPE_MODE (void_type_node)) |
3310 | return void_type_node; | |
b27ac6b5 | 3311 | |
b0fc3e72 | 3312 | if (mode == TYPE_MODE (build_pointer_type (char_type_node))) |
61b9b73c | 3313 | return (unsignedp |
3314 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
3315 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 3316 | |
3317 | if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) | |
61b9b73c | 3318 | return (unsignedp |
3319 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
3320 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 3321 | |
0dfc45b5 | 3322 | if (COMPLEX_MODE_P (mode)) |
3323 | { | |
3324 | enum machine_mode inner_mode; | |
3325 | tree inner_type; | |
3326 | ||
3327 | if (mode == TYPE_MODE (complex_float_type_node)) | |
3328 | return complex_float_type_node; | |
3329 | if (mode == TYPE_MODE (complex_double_type_node)) | |
3330 | return complex_double_type_node; | |
3331 | if (mode == TYPE_MODE (complex_long_double_type_node)) | |
3332 | return complex_long_double_type_node; | |
3333 | ||
3334 | if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp) | |
3335 | return complex_integer_type_node; | |
3336 | ||
3337 | inner_mode = GET_MODE_INNER (mode); | |
3338 | inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
3339 | if (inner_type != NULL_TREE) | |
3340 | return build_complex_type (inner_type); | |
3341 | } | |
3342 | else if (VECTOR_MODE_P (mode)) | |
4917c376 | 3343 | { |
3344 | enum machine_mode inner_mode = GET_MODE_INNER (mode); | |
3345 | tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
3346 | if (inner_type != NULL_TREE) | |
3347 | return build_vector_type_for_mode (inner_type, mode); | |
88ae7f04 | 3348 | } |
e2ea7e3a | 3349 | |
c4503c0a | 3350 | if (mode == TYPE_MODE (dfloat32_type_node)) |
3351 | return dfloat32_type_node; | |
3352 | if (mode == TYPE_MODE (dfloat64_type_node)) | |
3353 | return dfloat64_type_node; | |
3354 | if (mode == TYPE_MODE (dfloat128_type_node)) | |
3355 | return dfloat128_type_node; | |
3356 | ||
9421ebb9 | 3357 | if (ALL_SCALAR_FIXED_POINT_MODE_P (mode)) |
3358 | { | |
3359 | if (mode == TYPE_MODE (short_fract_type_node)) | |
3360 | return unsignedp ? sat_short_fract_type_node : short_fract_type_node; | |
3361 | if (mode == TYPE_MODE (fract_type_node)) | |
3362 | return unsignedp ? sat_fract_type_node : fract_type_node; | |
3363 | if (mode == TYPE_MODE (long_fract_type_node)) | |
3364 | return unsignedp ? sat_long_fract_type_node : long_fract_type_node; | |
3365 | if (mode == TYPE_MODE (long_long_fract_type_node)) | |
3366 | return unsignedp ? sat_long_long_fract_type_node | |
3367 | : long_long_fract_type_node; | |
3368 | ||
3369 | if (mode == TYPE_MODE (unsigned_short_fract_type_node)) | |
3370 | return unsignedp ? sat_unsigned_short_fract_type_node | |
3371 | : unsigned_short_fract_type_node; | |
3372 | if (mode == TYPE_MODE (unsigned_fract_type_node)) | |
3373 | return unsignedp ? sat_unsigned_fract_type_node | |
3374 | : unsigned_fract_type_node; | |
3375 | if (mode == TYPE_MODE (unsigned_long_fract_type_node)) | |
3376 | return unsignedp ? sat_unsigned_long_fract_type_node | |
3377 | : unsigned_long_fract_type_node; | |
3378 | if (mode == TYPE_MODE (unsigned_long_long_fract_type_node)) | |
3379 | return unsignedp ? sat_unsigned_long_long_fract_type_node | |
3380 | : unsigned_long_long_fract_type_node; | |
3381 | ||
3382 | if (mode == TYPE_MODE (short_accum_type_node)) | |
3383 | return unsignedp ? sat_short_accum_type_node : short_accum_type_node; | |
3384 | if (mode == TYPE_MODE (accum_type_node)) | |
3385 | return unsignedp ? sat_accum_type_node : accum_type_node; | |
3386 | if (mode == TYPE_MODE (long_accum_type_node)) | |
3387 | return unsignedp ? sat_long_accum_type_node : long_accum_type_node; | |
3388 | if (mode == TYPE_MODE (long_long_accum_type_node)) | |
3389 | return unsignedp ? sat_long_long_accum_type_node | |
3390 | : long_long_accum_type_node; | |
3391 | ||
3392 | if (mode == TYPE_MODE (unsigned_short_accum_type_node)) | |
3393 | return unsignedp ? sat_unsigned_short_accum_type_node | |
3394 | : unsigned_short_accum_type_node; | |
3395 | if (mode == TYPE_MODE (unsigned_accum_type_node)) | |
3396 | return unsignedp ? sat_unsigned_accum_type_node | |
3397 | : unsigned_accum_type_node; | |
3398 | if (mode == TYPE_MODE (unsigned_long_accum_type_node)) | |
3399 | return unsignedp ? sat_unsigned_long_accum_type_node | |
3400 | : unsigned_long_accum_type_node; | |
3401 | if (mode == TYPE_MODE (unsigned_long_long_accum_type_node)) | |
3402 | return unsignedp ? sat_unsigned_long_long_accum_type_node | |
3403 | : unsigned_long_long_accum_type_node; | |
3404 | ||
3405 | if (mode == QQmode) | |
3406 | return unsignedp ? sat_qq_type_node : qq_type_node; | |
3407 | if (mode == HQmode) | |
3408 | return unsignedp ? sat_hq_type_node : hq_type_node; | |
3409 | if (mode == SQmode) | |
3410 | return unsignedp ? sat_sq_type_node : sq_type_node; | |
3411 | if (mode == DQmode) | |
3412 | return unsignedp ? sat_dq_type_node : dq_type_node; | |
3413 | if (mode == TQmode) | |
3414 | return unsignedp ? sat_tq_type_node : tq_type_node; | |
3415 | ||
3416 | if (mode == UQQmode) | |
3417 | return unsignedp ? sat_uqq_type_node : uqq_type_node; | |
3418 | if (mode == UHQmode) | |
3419 | return unsignedp ? sat_uhq_type_node : uhq_type_node; | |
3420 | if (mode == USQmode) | |
3421 | return unsignedp ? sat_usq_type_node : usq_type_node; | |
3422 | if (mode == UDQmode) | |
3423 | return unsignedp ? sat_udq_type_node : udq_type_node; | |
3424 | if (mode == UTQmode) | |
3425 | return unsignedp ? sat_utq_type_node : utq_type_node; | |
3426 | ||
3427 | if (mode == HAmode) | |
3428 | return unsignedp ? sat_ha_type_node : ha_type_node; | |
3429 | if (mode == SAmode) | |
3430 | return unsignedp ? sat_sa_type_node : sa_type_node; | |
3431 | if (mode == DAmode) | |
3432 | return unsignedp ? sat_da_type_node : da_type_node; | |
3433 | if (mode == TAmode) | |
3434 | return unsignedp ? sat_ta_type_node : ta_type_node; | |
3435 | ||
3436 | if (mode == UHAmode) | |
3437 | return unsignedp ? sat_uha_type_node : uha_type_node; | |
3438 | if (mode == USAmode) | |
3439 | return unsignedp ? sat_usa_type_node : usa_type_node; | |
3440 | if (mode == UDAmode) | |
3441 | return unsignedp ? sat_uda_type_node : uda_type_node; | |
3442 | if (mode == UTAmode) | |
3443 | return unsignedp ? sat_uta_type_node : uta_type_node; | |
3444 | } | |
3445 | ||
5b247e9f | 3446 | for (t = registered_builtin_types; t; t = TREE_CHAIN (t)) |
baec58e1 | 3447 | if (TYPE_MODE (TREE_VALUE (t)) == mode |
3448 | && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t))) | |
5b247e9f | 3449 | return TREE_VALUE (t); |
3450 | ||
b0fc3e72 | 3451 | return 0; |
3452 | } | |
20d39783 | 3453 | |
11773141 | 3454 | tree |
3455 | c_common_unsigned_type (tree type) | |
3456 | { | |
3457 | return c_common_signed_or_unsigned_type (1, type); | |
3458 | } | |
3459 | ||
20d39783 | 3460 | /* Return a signed type the same as TYPE in other respects. */ |
3461 | ||
3462 | tree | |
1cae46be | 3463 | c_common_signed_type (tree type) |
20d39783 | 3464 | { |
4070745f | 3465 | return c_common_signed_or_unsigned_type (0, type); |
20d39783 | 3466 | } |
3467 | ||
3468 | /* Return a type the same as TYPE except unsigned or | |
3469 | signed according to UNSIGNEDP. */ | |
3470 | ||
3471 | tree | |
1cae46be | 3472 | c_common_signed_or_unsigned_type (int unsignedp, tree type) |
20d39783 | 3473 | { |
7a91101f | 3474 | tree type1; |
20d39783 | 3475 | |
7a91101f | 3476 | /* This block of code emulates the behavior of the old |
3477 | c_common_unsigned_type. In particular, it returns | |
3478 | long_unsigned_type_node if passed a long, even when a int would | |
3479 | have the same size. This is necessary for warnings to work | |
3480 | correctly in archs where sizeof(int) == sizeof(long) */ | |
3481 | ||
3482 | type1 = TYPE_MAIN_VARIANT (type); | |
3483 | if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node) | |
3484 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3485 | if (type1 == integer_type_node || type1 == unsigned_type_node) | |
3486 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3487 | if (type1 == short_integer_type_node || type1 == short_unsigned_type_node) | |
3488 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3489 | if (type1 == long_integer_type_node || type1 == long_unsigned_type_node) | |
3490 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3491 | if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node) | |
3492 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
6388cfe2 | 3493 | if (int128_integer_type_node |
3494 | && (type1 == int128_integer_type_node | |
3495 | || type1 == int128_unsigned_type_node)) | |
3496 | return unsignedp ? int128_unsigned_type_node : int128_integer_type_node; | |
7a91101f | 3497 | if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node) |
3498 | return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node; | |
3499 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
3500 | if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node) | |
3501 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
3502 | #endif | |
3503 | if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node) | |
3504 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3505 | if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node) | |
3506 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3507 | if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node) | |
3508 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3509 | if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node) | |
3510 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3511 | ||
9f92e1a2 | 3512 | #define C_COMMON_FIXED_TYPES(NAME) \ |
3513 | if (type1 == short_ ## NAME ## _type_node \ | |
3514 | || type1 == unsigned_short_ ## NAME ## _type_node) \ | |
3515 | return unsignedp ? unsigned_short_ ## NAME ## _type_node \ | |
3516 | : short_ ## NAME ## _type_node; \ | |
3517 | if (type1 == NAME ## _type_node \ | |
3518 | || type1 == unsigned_ ## NAME ## _type_node) \ | |
3519 | return unsignedp ? unsigned_ ## NAME ## _type_node \ | |
3520 | : NAME ## _type_node; \ | |
3521 | if (type1 == long_ ## NAME ## _type_node \ | |
3522 | || type1 == unsigned_long_ ## NAME ## _type_node) \ | |
3523 | return unsignedp ? unsigned_long_ ## NAME ## _type_node \ | |
3524 | : long_ ## NAME ## _type_node; \ | |
3525 | if (type1 == long_long_ ## NAME ## _type_node \ | |
3526 | || type1 == unsigned_long_long_ ## NAME ## _type_node) \ | |
3527 | return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \ | |
3528 | : long_long_ ## NAME ## _type_node; | |
3529 | ||
3530 | #define C_COMMON_FIXED_MODE_TYPES(NAME) \ | |
3531 | if (type1 == NAME ## _type_node \ | |
3532 | || type1 == u ## NAME ## _type_node) \ | |
3533 | return unsignedp ? u ## NAME ## _type_node \ | |
3534 | : NAME ## _type_node; | |
3535 | ||
3536 | #define C_COMMON_FIXED_TYPES_SAT(NAME) \ | |
3537 | if (type1 == sat_ ## short_ ## NAME ## _type_node \ | |
3538 | || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \ | |
3539 | return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \ | |
3540 | : sat_ ## short_ ## NAME ## _type_node; \ | |
3541 | if (type1 == sat_ ## NAME ## _type_node \ | |
3542 | || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \ | |
3543 | return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \ | |
3544 | : sat_ ## NAME ## _type_node; \ | |
3545 | if (type1 == sat_ ## long_ ## NAME ## _type_node \ | |
3546 | || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \ | |
3547 | return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \ | |
3548 | : sat_ ## long_ ## NAME ## _type_node; \ | |
3549 | if (type1 == sat_ ## long_long_ ## NAME ## _type_node \ | |
3550 | || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \ | |
3551 | return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \ | |
3552 | : sat_ ## long_long_ ## NAME ## _type_node; | |
3553 | ||
3554 | #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \ | |
3555 | if (type1 == sat_ ## NAME ## _type_node \ | |
3556 | || type1 == sat_ ## u ## NAME ## _type_node) \ | |
3557 | return unsignedp ? sat_ ## u ## NAME ## _type_node \ | |
3558 | : sat_ ## NAME ## _type_node; | |
3559 | ||
3560 | C_COMMON_FIXED_TYPES (fract); | |
3561 | C_COMMON_FIXED_TYPES_SAT (fract); | |
3562 | C_COMMON_FIXED_TYPES (accum); | |
3563 | C_COMMON_FIXED_TYPES_SAT (accum); | |
3564 | ||
3565 | C_COMMON_FIXED_MODE_TYPES (qq); | |
3566 | C_COMMON_FIXED_MODE_TYPES (hq); | |
3567 | C_COMMON_FIXED_MODE_TYPES (sq); | |
3568 | C_COMMON_FIXED_MODE_TYPES (dq); | |
3569 | C_COMMON_FIXED_MODE_TYPES (tq); | |
3570 | C_COMMON_FIXED_MODE_TYPES_SAT (qq); | |
3571 | C_COMMON_FIXED_MODE_TYPES_SAT (hq); | |
3572 | C_COMMON_FIXED_MODE_TYPES_SAT (sq); | |
3573 | C_COMMON_FIXED_MODE_TYPES_SAT (dq); | |
3574 | C_COMMON_FIXED_MODE_TYPES_SAT (tq); | |
3575 | C_COMMON_FIXED_MODE_TYPES (ha); | |
3576 | C_COMMON_FIXED_MODE_TYPES (sa); | |
3577 | C_COMMON_FIXED_MODE_TYPES (da); | |
3578 | C_COMMON_FIXED_MODE_TYPES (ta); | |
3579 | C_COMMON_FIXED_MODE_TYPES_SAT (ha); | |
3580 | C_COMMON_FIXED_MODE_TYPES_SAT (sa); | |
3581 | C_COMMON_FIXED_MODE_TYPES_SAT (da); | |
3582 | C_COMMON_FIXED_MODE_TYPES_SAT (ta); | |
9421ebb9 | 3583 | |
4f7f7efd | 3584 | /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not |
3585 | the precision; they have precision set to match their range, but | |
3586 | may use a wider mode to match an ABI. If we change modes, we may | |
3587 | wind up with bad conversions. For INTEGER_TYPEs in C, must check | |
3588 | the precision as well, so as to yield correct results for | |
3589 | bit-field types. C++ does not have these separate bit-field | |
3590 | types, and producing a signed or unsigned variant of an | |
3591 | ENUMERAL_TYPE may cause other problems as well. */ | |
3592 | ||
ac265864 | 3593 | if (!INTEGRAL_TYPE_P (type) |
3594 | || TYPE_UNSIGNED (type) == unsignedp) | |
3595 | return type; | |
3596 | ||
4f7f7efd | 3597 | #define TYPE_OK(node) \ |
3598 | (TYPE_MODE (type) == TYPE_MODE (node) \ | |
0c4abe5b | 3599 | && TYPE_PRECISION (type) == TYPE_PRECISION (node)) |
4f7f7efd | 3600 | if (TYPE_OK (signed_char_type_node)) |
20d39783 | 3601 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
4f7f7efd | 3602 | if (TYPE_OK (integer_type_node)) |
20d39783 | 3603 | return unsignedp ? unsigned_type_node : integer_type_node; |
4f7f7efd | 3604 | if (TYPE_OK (short_integer_type_node)) |
20d39783 | 3605 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
4f7f7efd | 3606 | if (TYPE_OK (long_integer_type_node)) |
20d39783 | 3607 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
4f7f7efd | 3608 | if (TYPE_OK (long_long_integer_type_node)) |
20d39783 | 3609 | return (unsignedp ? long_long_unsigned_type_node |
3610 | : long_long_integer_type_node); | |
6388cfe2 | 3611 | if (int128_integer_type_node && TYPE_OK (int128_integer_type_node)) |
3612 | return (unsignedp ? int128_unsigned_type_node | |
3613 | : int128_integer_type_node); | |
4f7f7efd | 3614 | if (TYPE_OK (widest_integer_literal_type_node)) |
20d39783 | 3615 | return (unsignedp ? widest_unsigned_literal_type_node |
3616 | : widest_integer_literal_type_node); | |
ef11801e | 3617 | |
3618 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
4f7f7efd | 3619 | if (TYPE_OK (intTI_type_node)) |
ef11801e | 3620 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; |
3621 | #endif | |
4f7f7efd | 3622 | if (TYPE_OK (intDI_type_node)) |
ef11801e | 3623 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
4f7f7efd | 3624 | if (TYPE_OK (intSI_type_node)) |
ef11801e | 3625 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
4f7f7efd | 3626 | if (TYPE_OK (intHI_type_node)) |
ef11801e | 3627 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
4f7f7efd | 3628 | if (TYPE_OK (intQI_type_node)) |
ef11801e | 3629 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
4f7f7efd | 3630 | #undef TYPE_OK |
ef11801e | 3631 | |
0c4abe5b | 3632 | return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp); |
20d39783 | 3633 | } |
b268e47e | 3634 | |
c0e47fd4 | 3635 | /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */ |
3636 | ||
3637 | tree | |
3638 | c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp) | |
3639 | { | |
3640 | /* Extended integer types of the same width as a standard type have | |
3641 | lesser rank, so those of the same width as int promote to int or | |
3642 | unsigned int and are valid for printf formats expecting int or | |
3643 | unsigned int. To avoid such special cases, avoid creating | |
3644 | extended integer types for bit-fields if a standard integer type | |
3645 | is available. */ | |
3646 | if (width == TYPE_PRECISION (integer_type_node)) | |
3647 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3648 | if (width == TYPE_PRECISION (signed_char_type_node)) | |
3649 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3650 | if (width == TYPE_PRECISION (short_integer_type_node)) | |
3651 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3652 | if (width == TYPE_PRECISION (long_integer_type_node)) | |
3653 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3654 | if (width == TYPE_PRECISION (long_long_integer_type_node)) | |
3655 | return (unsignedp ? long_long_unsigned_type_node | |
3656 | : long_long_integer_type_node); | |
6388cfe2 | 3657 | if (int128_integer_type_node |
3658 | && width == TYPE_PRECISION (int128_integer_type_node)) | |
3659 | return (unsignedp ? int128_unsigned_type_node | |
3660 | : int128_integer_type_node); | |
c0e47fd4 | 3661 | return build_nonstandard_integer_type (width, unsignedp); |
3662 | } | |
3663 | ||
b268e47e | 3664 | /* The C version of the register_builtin_type langhook. */ |
3665 | ||
3666 | void | |
3667 | c_register_builtin_type (tree type, const char* name) | |
3668 | { | |
3669 | tree decl; | |
3670 | ||
e60a6f7b | 3671 | decl = build_decl (UNKNOWN_LOCATION, |
3672 | TYPE_DECL, get_identifier (name), type); | |
b268e47e | 3673 | DECL_ARTIFICIAL (decl) = 1; |
3674 | if (!TYPE_NAME (type)) | |
3675 | TYPE_NAME (type) = decl; | |
3676 | pushdecl (decl); | |
5b247e9f | 3677 | |
3678 | registered_builtin_types = tree_cons (0, type, registered_builtin_types); | |
b268e47e | 3679 | } |
a9b9d10c | 3680 | \f |
aff9e656 | 3681 | /* Print an error message for invalid operands to arith operation |
8e70fb09 | 3682 | CODE with TYPE0 for operand 0, and TYPE1 for operand 1. |
3683 | LOCATION is the location of the message. */ | |
b0fc3e72 | 3684 | |
3685 | void | |
8e70fb09 | 3686 | binary_op_error (location_t location, enum tree_code code, |
3687 | tree type0, tree type1) | |
b0fc3e72 | 3688 | { |
19cb6b50 | 3689 | const char *opname; |
f03946e4 | 3690 | |
b0fc3e72 | 3691 | switch (code) |
3692 | { | |
b0fc3e72 | 3693 | case PLUS_EXPR: |
3694 | opname = "+"; break; | |
3695 | case MINUS_EXPR: | |
3696 | opname = "-"; break; | |
3697 | case MULT_EXPR: | |
3698 | opname = "*"; break; | |
3699 | case MAX_EXPR: | |
3700 | opname = "max"; break; | |
3701 | case MIN_EXPR: | |
3702 | opname = "min"; break; | |
3703 | case EQ_EXPR: | |
3704 | opname = "=="; break; | |
3705 | case NE_EXPR: | |
3706 | opname = "!="; break; | |
3707 | case LE_EXPR: | |
3708 | opname = "<="; break; | |
3709 | case GE_EXPR: | |
3710 | opname = ">="; break; | |
3711 | case LT_EXPR: | |
3712 | opname = "<"; break; | |
3713 | case GT_EXPR: | |
3714 | opname = ">"; break; | |
3715 | case LSHIFT_EXPR: | |
3716 | opname = "<<"; break; | |
3717 | case RSHIFT_EXPR: | |
3718 | opname = ">>"; break; | |
3719 | case TRUNC_MOD_EXPR: | |
66618a1e | 3720 | case FLOOR_MOD_EXPR: |
b0fc3e72 | 3721 | opname = "%"; break; |
3722 | case TRUNC_DIV_EXPR: | |
66618a1e | 3723 | case FLOOR_DIV_EXPR: |
b0fc3e72 | 3724 | opname = "/"; break; |
3725 | case BIT_AND_EXPR: | |
3726 | opname = "&"; break; | |
3727 | case BIT_IOR_EXPR: | |
3728 | opname = "|"; break; | |
3729 | case TRUTH_ANDIF_EXPR: | |
3730 | opname = "&&"; break; | |
3731 | case TRUTH_ORIF_EXPR: | |
3732 | opname = "||"; break; | |
3733 | case BIT_XOR_EXPR: | |
3734 | opname = "^"; break; | |
31f820d2 | 3735 | default: |
315ba355 | 3736 | gcc_unreachable (); |
b0fc3e72 | 3737 | } |
8e70fb09 | 3738 | error_at (location, |
3739 | "invalid operands to binary %s (have %qT and %qT)", opname, | |
3740 | type0, type1); | |
b0fc3e72 | 3741 | } |
3742 | \f | |
03fe1dc2 | 3743 | /* Given an expression as a tree, return its original type. Do this |
3744 | by stripping any conversion that preserves the sign and precision. */ | |
3745 | static tree | |
3746 | expr_original_type (tree expr) | |
3747 | { | |
3748 | STRIP_SIGN_NOPS (expr); | |
3749 | return TREE_TYPE (expr); | |
3750 | } | |
3751 | ||
b0fc3e72 | 3752 | /* Subroutine of build_binary_op, used for comparison operations. |
3753 | See if the operands have both been converted from subword integer types | |
3754 | and, if so, perhaps change them both back to their original type. | |
5b511807 | 3755 | This function is also responsible for converting the two operands |
3756 | to the proper common type for comparison. | |
b0fc3e72 | 3757 | |
3758 | The arguments of this function are all pointers to local variables | |
3759 | of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, | |
3760 | RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. | |
3761 | ||
3762 | If this function returns nonzero, it means that the comparison has | |
3763 | a constant value. What this function returns is an expression for | |
3764 | that value. */ | |
3765 | ||
3766 | tree | |
1cae46be | 3767 | shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr, |
3768 | enum tree_code *rescode_ptr) | |
b0fc3e72 | 3769 | { |
19cb6b50 | 3770 | tree type; |
b0fc3e72 | 3771 | tree op0 = *op0_ptr; |
3772 | tree op1 = *op1_ptr; | |
3773 | int unsignedp0, unsignedp1; | |
3774 | int real1, real2; | |
3775 | tree primop0, primop1; | |
3776 | enum tree_code code = *rescode_ptr; | |
03fe1dc2 | 3777 | location_t loc = EXPR_LOC_OR_HERE (op0); |
b0fc3e72 | 3778 | |
3779 | /* Throw away any conversions to wider types | |
3780 | already present in the operands. */ | |
3781 | ||
7f506bca | 3782 | primop0 = c_common_get_narrower (op0, &unsignedp0); |
3783 | primop1 = c_common_get_narrower (op1, &unsignedp1); | |
b0fc3e72 | 3784 | |
119d06b2 | 3785 | /* If primopN is first sign-extended from primopN's precision to opN's |
3786 | precision, then zero-extended from opN's precision to | |
3787 | *restype_ptr precision, shortenings might be invalid. */ | |
3788 | if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0)) | |
3789 | && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr) | |
3790 | && !unsignedp0 | |
3791 | && TYPE_UNSIGNED (TREE_TYPE (op0))) | |
3792 | primop0 = op0; | |
3793 | if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1)) | |
3794 | && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr) | |
3795 | && !unsignedp1 | |
3796 | && TYPE_UNSIGNED (TREE_TYPE (op1))) | |
3797 | primop1 = op1; | |
3798 | ||
b0fc3e72 | 3799 | /* Handle the case that OP0 does not *contain* a conversion |
3800 | but it *requires* conversion to FINAL_TYPE. */ | |
3801 | ||
3802 | if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) | |
78a8ed03 | 3803 | unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
b0fc3e72 | 3804 | if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) |
78a8ed03 | 3805 | unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1)); |
b0fc3e72 | 3806 | |
3807 | /* If one of the operands must be floated, we cannot optimize. */ | |
3808 | real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; | |
3809 | real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; | |
3810 | ||
3811 | /* If first arg is constant, swap the args (changing operation | |
2bd278cc | 3812 | so value is preserved), for canonicalization. Don't do this if |
3813 | the second arg is 0. */ | |
b0fc3e72 | 3814 | |
2bd278cc | 3815 | if (TREE_CONSTANT (primop0) |
9421ebb9 | 3816 | && !integer_zerop (primop1) && !real_zerop (primop1) |
3817 | && !fixed_zerop (primop1)) | |
b0fc3e72 | 3818 | { |
19cb6b50 | 3819 | tree tem = primop0; |
3820 | int temi = unsignedp0; | |
b0fc3e72 | 3821 | primop0 = primop1; |
3822 | primop1 = tem; | |
3823 | tem = op0; | |
3824 | op0 = op1; | |
3825 | op1 = tem; | |
3826 | *op0_ptr = op0; | |
3827 | *op1_ptr = op1; | |
3828 | unsignedp0 = unsignedp1; | |
3829 | unsignedp1 = temi; | |
3830 | temi = real1; | |
3831 | real1 = real2; | |
3832 | real2 = temi; | |
3833 | ||
3834 | switch (code) | |
3835 | { | |
3836 | case LT_EXPR: | |
3837 | code = GT_EXPR; | |
3838 | break; | |
3839 | case GT_EXPR: | |
3840 | code = LT_EXPR; | |
3841 | break; | |
3842 | case LE_EXPR: | |
3843 | code = GE_EXPR; | |
3844 | break; | |
3845 | case GE_EXPR: | |
3846 | code = LE_EXPR; | |
3847 | break; | |
31f820d2 | 3848 | default: |
3849 | break; | |
b0fc3e72 | 3850 | } |
3851 | *rescode_ptr = code; | |
3852 | } | |
3853 | ||
3854 | /* If comparing an integer against a constant more bits wide, | |
3855 | maybe we can deduce a value of 1 or 0 independent of the data. | |
3856 | Or else truncate the constant now | |
3857 | rather than extend the variable at run time. | |
3858 | ||
3859 | This is only interesting if the constant is the wider arg. | |
3860 | Also, it is not safe if the constant is unsigned and the | |
3861 | variable arg is signed, since in this case the variable | |
3862 | would be sign-extended and then regarded as unsigned. | |
3863 | Our technique fails in this case because the lowest/highest | |
3864 | possible unsigned results don't follow naturally from the | |
3865 | lowest/highest possible values of the variable operand. | |
3866 | For just EQ_EXPR and NE_EXPR there is another technique that | |
3867 | could be used: see if the constant can be faithfully represented | |
3868 | in the other operand's type, by truncating it and reextending it | |
3869 | and see if that preserves the constant's value. */ | |
3870 | ||
3871 | if (!real1 && !real2 | |
9421ebb9 | 3872 | && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE |
b0fc3e72 | 3873 | && TREE_CODE (primop1) == INTEGER_CST |
3874 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) | |
3875 | { | |
3876 | int min_gt, max_gt, min_lt, max_lt; | |
3877 | tree maxval, minval; | |
3878 | /* 1 if comparison is nominally unsigned. */ | |
78a8ed03 | 3879 | int unsignedp = TYPE_UNSIGNED (*restype_ptr); |
b0fc3e72 | 3880 | tree val; |
3881 | ||
4070745f | 3882 | type = c_common_signed_or_unsigned_type (unsignedp0, |
3883 | TREE_TYPE (primop0)); | |
cda09c61 | 3884 | |
b0fc3e72 | 3885 | maxval = TYPE_MAX_VALUE (type); |
3886 | minval = TYPE_MIN_VALUE (type); | |
3887 | ||
3888 | if (unsignedp && !unsignedp0) | |
4070745f | 3889 | *restype_ptr = c_common_signed_type (*restype_ptr); |
b0fc3e72 | 3890 | |
3891 | if (TREE_TYPE (primop1) != *restype_ptr) | |
18dbec6f | 3892 | { |
9a5e8086 | 3893 | /* Convert primop1 to target type, but do not introduce |
3894 | additional overflow. We know primop1 is an int_cst. */ | |
c8110c8f | 3895 | primop1 = force_fit_type_double (*restype_ptr, |
d3237426 | 3896 | tree_to_double_int (primop1), |
3897 | 0, TREE_OVERFLOW (primop1)); | |
18dbec6f | 3898 | } |
b0fc3e72 | 3899 | if (type != *restype_ptr) |
3900 | { | |
3901 | minval = convert (*restype_ptr, minval); | |
3902 | maxval = convert (*restype_ptr, maxval); | |
3903 | } | |
3904 | ||
3905 | if (unsignedp && unsignedp0) | |
3906 | { | |
3907 | min_gt = INT_CST_LT_UNSIGNED (primop1, minval); | |
3908 | max_gt = INT_CST_LT_UNSIGNED (primop1, maxval); | |
3909 | min_lt = INT_CST_LT_UNSIGNED (minval, primop1); | |
3910 | max_lt = INT_CST_LT_UNSIGNED (maxval, primop1); | |
3911 | } | |
3912 | else | |
3913 | { | |
3914 | min_gt = INT_CST_LT (primop1, minval); | |
3915 | max_gt = INT_CST_LT (primop1, maxval); | |
3916 | min_lt = INT_CST_LT (minval, primop1); | |
3917 | max_lt = INT_CST_LT (maxval, primop1); | |
3918 | } | |
3919 | ||
3920 | val = 0; | |
3921 | /* This used to be a switch, but Genix compiler can't handle that. */ | |
3922 | if (code == NE_EXPR) | |
3923 | { | |
3924 | if (max_lt || min_gt) | |
3c2239cf | 3925 | val = truthvalue_true_node; |
b0fc3e72 | 3926 | } |
3927 | else if (code == EQ_EXPR) | |
3928 | { | |
3929 | if (max_lt || min_gt) | |
3c2239cf | 3930 | val = truthvalue_false_node; |
b0fc3e72 | 3931 | } |
3932 | else if (code == LT_EXPR) | |
3933 | { | |
3934 | if (max_lt) | |
3c2239cf | 3935 | val = truthvalue_true_node; |
b0fc3e72 | 3936 | if (!min_lt) |
3c2239cf | 3937 | val = truthvalue_false_node; |
b0fc3e72 | 3938 | } |
3939 | else if (code == GT_EXPR) | |
3940 | { | |
3941 | if (min_gt) | |
3c2239cf | 3942 | val = truthvalue_true_node; |
b0fc3e72 | 3943 | if (!max_gt) |
3c2239cf | 3944 | val = truthvalue_false_node; |
b0fc3e72 | 3945 | } |
3946 | else if (code == LE_EXPR) | |
3947 | { | |
3948 | if (!max_gt) | |
3c2239cf | 3949 | val = truthvalue_true_node; |
b0fc3e72 | 3950 | if (min_gt) |
3c2239cf | 3951 | val = truthvalue_false_node; |
b0fc3e72 | 3952 | } |
3953 | else if (code == GE_EXPR) | |
3954 | { | |
3955 | if (!min_lt) | |
3c2239cf | 3956 | val = truthvalue_true_node; |
b0fc3e72 | 3957 | if (max_lt) |
3c2239cf | 3958 | val = truthvalue_false_node; |
b0fc3e72 | 3959 | } |
3960 | ||
3961 | /* If primop0 was sign-extended and unsigned comparison specd, | |
3962 | we did a signed comparison above using the signed type bounds. | |
3963 | But the comparison we output must be unsigned. | |
3964 | ||
3965 | Also, for inequalities, VAL is no good; but if the signed | |
3966 | comparison had *any* fixed result, it follows that the | |
3967 | unsigned comparison just tests the sign in reverse | |
3968 | (positive values are LE, negative ones GE). | |
3969 | So we can generate an unsigned comparison | |
3970 | against an extreme value of the signed type. */ | |
3971 | ||
3972 | if (unsignedp && !unsignedp0) | |
3973 | { | |
3974 | if (val != 0) | |
3975 | switch (code) | |
3976 | { | |
3977 | case LT_EXPR: | |
3978 | case GE_EXPR: | |
3979 | primop1 = TYPE_MIN_VALUE (type); | |
3980 | val = 0; | |
3981 | break; | |
3982 | ||
3983 | case LE_EXPR: | |
3984 | case GT_EXPR: | |
3985 | primop1 = TYPE_MAX_VALUE (type); | |
3986 | val = 0; | |
3987 | break; | |
31f820d2 | 3988 | |
3989 | default: | |
3990 | break; | |
b0fc3e72 | 3991 | } |
11773141 | 3992 | type = c_common_unsigned_type (type); |
b0fc3e72 | 3993 | } |
3994 | ||
82e6ef7c | 3995 | if (TREE_CODE (primop0) != INTEGER_CST |
3996 | && c_inhibit_evaluation_warnings == 0) | |
b0fc3e72 | 3997 | { |
3c2239cf | 3998 | if (val == truthvalue_false_node) |
03fe1dc2 | 3999 | warning_at (loc, OPT_Wtype_limits, |
4000 | "comparison is always false due to limited range of data type"); | |
3c2239cf | 4001 | if (val == truthvalue_true_node) |
03fe1dc2 | 4002 | warning_at (loc, OPT_Wtype_limits, |
4003 | "comparison is always true due to limited range of data type"); | |
b0fc3e72 | 4004 | } |
4005 | ||
4006 | if (val != 0) | |
4007 | { | |
4008 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4009 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 4010 | return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); |
b0fc3e72 | 4011 | return val; |
4012 | } | |
4013 | ||
4014 | /* Value is not predetermined, but do the comparison | |
4015 | in the type of the operand that is not constant. | |
4016 | TYPE is already properly set. */ | |
4017 | } | |
c4503c0a | 4018 | |
4019 | /* If either arg is decimal float and the other is float, find the | |
4020 | proper common type to use for comparison. */ | |
4021 | else if (real1 && real2 | |
4022 | && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0))) | |
4023 | || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))) | |
4024 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4025 | ||
b0fc3e72 | 4026 | else if (real1 && real2 |
2203bd5c | 4027 | && (TYPE_PRECISION (TREE_TYPE (primop0)) |
4028 | == TYPE_PRECISION (TREE_TYPE (primop1)))) | |
b0fc3e72 | 4029 | type = TREE_TYPE (primop0); |
4030 | ||
4031 | /* If args' natural types are both narrower than nominal type | |
4032 | and both extend in the same manner, compare them | |
4033 | in the type of the wider arg. | |
4034 | Otherwise must actually extend both to the nominal | |
4035 | common type lest different ways of extending | |
4036 | alter the result. | |
4037 | (eg, (short)-1 == (unsigned short)-1 should be 0.) */ | |
4038 | ||
4039 | else if (unsignedp0 == unsignedp1 && real1 == real2 | |
4040 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) | |
4041 | && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) | |
4042 | { | |
4043 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4070745f | 4044 | type = c_common_signed_or_unsigned_type (unsignedp0 |
78a8ed03 | 4045 | || TYPE_UNSIGNED (*restype_ptr), |
4070745f | 4046 | type); |
b0fc3e72 | 4047 | /* Make sure shorter operand is extended the right way |
4048 | to match the longer operand. */ | |
4070745f | 4049 | primop0 |
4050 | = convert (c_common_signed_or_unsigned_type (unsignedp0, | |
4051 | TREE_TYPE (primop0)), | |
4052 | primop0); | |
4053 | primop1 | |
4054 | = convert (c_common_signed_or_unsigned_type (unsignedp1, | |
4055 | TREE_TYPE (primop1)), | |
4056 | primop1); | |
b0fc3e72 | 4057 | } |
4058 | else | |
4059 | { | |
4060 | /* Here we must do the comparison on the nominal type | |
4061 | using the args exactly as we received them. */ | |
4062 | type = *restype_ptr; | |
4063 | primop0 = op0; | |
4064 | primop1 = op1; | |
4065 | ||
4066 | if (!real1 && !real2 && integer_zerop (primop1) | |
78a8ed03 | 4067 | && TYPE_UNSIGNED (*restype_ptr)) |
b0fc3e72 | 4068 | { |
4069 | tree value = 0; | |
03fe1dc2 | 4070 | /* All unsigned values are >= 0, so we warn. However, |
4071 | if OP0 is a constant that is >= 0, the signedness of | |
4072 | the comparison isn't an issue, so suppress the | |
4073 | warning. */ | |
4074 | bool warn = | |
4075 | warn_type_limits && !in_system_header | |
82e6ef7c | 4076 | && c_inhibit_evaluation_warnings == 0 |
03fe1dc2 | 4077 | && !(TREE_CODE (primop0) == INTEGER_CST |
4078 | && !TREE_OVERFLOW (convert (c_common_signed_type (type), | |
4079 | primop0))) | |
4080 | /* Do not warn for enumeration types. */ | |
4081 | && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE); | |
4082 | ||
b0fc3e72 | 4083 | switch (code) |
4084 | { | |
4085 | case GE_EXPR: | |
03fe1dc2 | 4086 | if (warn) |
4087 | warning_at (loc, OPT_Wtype_limits, | |
4088 | "comparison of unsigned expression >= 0 is always true"); | |
3c2239cf | 4089 | value = truthvalue_true_node; |
b0fc3e72 | 4090 | break; |
4091 | ||
4092 | case LT_EXPR: | |
03fe1dc2 | 4093 | if (warn) |
4094 | warning_at (loc, OPT_Wtype_limits, | |
4095 | "comparison of unsigned expression < 0 is always false"); | |
3c2239cf | 4096 | value = truthvalue_false_node; |
31f820d2 | 4097 | break; |
4098 | ||
4099 | default: | |
4100 | break; | |
b0fc3e72 | 4101 | } |
4102 | ||
4103 | if (value != 0) | |
4104 | { | |
4105 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
4106 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 4107 | return build2 (COMPOUND_EXPR, TREE_TYPE (value), |
4108 | primop0, value); | |
b0fc3e72 | 4109 | return value; |
4110 | } | |
4111 | } | |
4112 | } | |
4113 | ||
4114 | *op0_ptr = convert (type, primop0); | |
4115 | *op1_ptr = convert (type, primop1); | |
4116 | ||
3c2239cf | 4117 | *restype_ptr = truthvalue_type_node; |
b0fc3e72 | 4118 | |
4119 | return 0; | |
4120 | } | |
4121 | \f | |
1c26100f | 4122 | /* Return a tree for the sum or difference (RESULTCODE says which) |
4123 | of pointer PTROP and integer INTOP. */ | |
4124 | ||
4125 | tree | |
389dd41b | 4126 | pointer_int_sum (location_t loc, enum tree_code resultcode, |
4127 | tree ptrop, tree intop) | |
1c26100f | 4128 | { |
add6ee5e | 4129 | tree size_exp, ret; |
1c26100f | 4130 | |
1c26100f | 4131 | /* The result is a pointer of the same type that is being added. */ |
1c26100f | 4132 | tree result_type = TREE_TYPE (ptrop); |
4133 | ||
4134 | if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) | |
4135 | { | |
29438999 | 4136 | pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
8864917d | 4137 | "pointer of type %<void *%> used in arithmetic"); |
1c26100f | 4138 | size_exp = integer_one_node; |
4139 | } | |
4140 | else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) | |
4141 | { | |
29438999 | 4142 | pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
8864917d | 4143 | "pointer to a function used in arithmetic"); |
1c26100f | 4144 | size_exp = integer_one_node; |
4145 | } | |
4146 | else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE) | |
4147 | { | |
29438999 | 4148 | pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
8864917d | 4149 | "pointer to member function used in arithmetic"); |
1c26100f | 4150 | size_exp = integer_one_node; |
4151 | } | |
1c26100f | 4152 | else |
4153 | size_exp = size_in_bytes (TREE_TYPE (result_type)); | |
4154 | ||
add6ee5e | 4155 | /* We are manipulating pointer values, so we don't need to warn |
4156 | about relying on undefined signed overflow. We disable the | |
4157 | warning here because we use integer types so fold won't know that | |
4158 | they are really pointers. */ | |
4159 | fold_defer_overflow_warnings (); | |
4160 | ||
1c26100f | 4161 | /* If what we are about to multiply by the size of the elements |
4162 | contains a constant term, apply distributive law | |
4163 | and multiply that constant term separately. | |
4164 | This helps produce common subexpressions. */ | |
1c26100f | 4165 | if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) |
84166705 | 4166 | && !TREE_CONSTANT (intop) |
1c26100f | 4167 | && TREE_CONSTANT (TREE_OPERAND (intop, 1)) |
4168 | && TREE_CONSTANT (size_exp) | |
4169 | /* If the constant comes from pointer subtraction, | |
4170 | skip this optimization--it would cause an error. */ | |
4171 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE | |
4172 | /* If the constant is unsigned, and smaller than the pointer size, | |
4173 | then we must skip this optimization. This is because it could cause | |
4174 | an overflow error if the constant is negative but INTOP is not. */ | |
84166705 | 4175 | && (!TYPE_UNSIGNED (TREE_TYPE (intop)) |
1c26100f | 4176 | || (TYPE_PRECISION (TREE_TYPE (intop)) |
4177 | == TYPE_PRECISION (TREE_TYPE (ptrop))))) | |
4178 | { | |
4179 | enum tree_code subcode = resultcode; | |
4180 | tree int_type = TREE_TYPE (intop); | |
4181 | if (TREE_CODE (intop) == MINUS_EXPR) | |
4182 | subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); | |
4183 | /* Convert both subexpression types to the type of intop, | |
4184 | because weird cases involving pointer arithmetic | |
4185 | can result in a sum or difference with different type args. */ | |
8e70fb09 | 4186 | ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)), |
4187 | subcode, ptrop, | |
1c26100f | 4188 | convert (int_type, TREE_OPERAND (intop, 1)), 1); |
4189 | intop = convert (int_type, TREE_OPERAND (intop, 0)); | |
4190 | } | |
4191 | ||
4192 | /* Convert the integer argument to a type the same size as sizetype | |
4193 | so the multiply won't overflow spuriously. */ | |
1c26100f | 4194 | if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) |
78a8ed03 | 4195 | || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype)) |
1cae46be | 4196 | intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype), |
78a8ed03 | 4197 | TYPE_UNSIGNED (sizetype)), intop); |
1c26100f | 4198 | |
4199 | /* Replace the integer argument with a suitable product by the object size. | |
c6feb9f1 | 4200 | Do this multiplication as signed, then convert to the appropriate type |
8032877c | 4201 | for the pointer operation and disregard an overflow that occurred only |
c6feb9f1 | 4202 | because of the sign-extension change in the latter conversion. */ |
4203 | { | |
4204 | tree t = build_binary_op (loc, | |
4205 | MULT_EXPR, intop, | |
4206 | convert (TREE_TYPE (intop), size_exp), 1); | |
4207 | intop = convert (sizetype, t); | |
4208 | if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t)) | |
4209 | intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop), | |
4210 | TREE_INT_CST_HIGH (intop)); | |
4211 | } | |
0de36bdb | 4212 | |
499e523f | 4213 | /* Create the sum or difference. */ |
0de36bdb | 4214 | if (resultcode == MINUS_EXPR) |
389dd41b | 4215 | intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop); |
1c26100f | 4216 | |
2cc66f2a | 4217 | ret = fold_build_pointer_plus_loc (loc, ptrop, intop); |
add6ee5e | 4218 | |
4219 | fold_undefer_and_ignore_overflow_warnings (); | |
4220 | ||
4221 | return ret; | |
1c26100f | 4222 | } |
4223 | \f | |
f59e3889 | 4224 | /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded |
4225 | and if NON_CONST is known not to be permitted in an evaluated part | |
4226 | of a constant expression. */ | |
4227 | ||
4228 | tree | |
4229 | c_wrap_maybe_const (tree expr, bool non_const) | |
4230 | { | |
4231 | bool nowarning = TREE_NO_WARNING (expr); | |
4232 | location_t loc = EXPR_LOCATION (expr); | |
4233 | ||
4234 | /* This should never be called for C++. */ | |
4235 | if (c_dialect_cxx ()) | |
4236 | gcc_unreachable (); | |
4237 | ||
4238 | /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */ | |
4239 | STRIP_TYPE_NOPS (expr); | |
4240 | expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr); | |
4241 | C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const; | |
4242 | if (nowarning) | |
4243 | TREE_NO_WARNING (expr) = 1; | |
4244 | protected_set_expr_location (expr, loc); | |
4245 | ||
4246 | return expr; | |
4247 | } | |
4248 | ||
a75b1c71 | 4249 | /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but |
4250 | for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR | |
4251 | around the SAVE_EXPR if needed so that c_fully_fold does not need | |
4252 | to look inside SAVE_EXPRs. */ | |
4253 | ||
4254 | tree | |
4255 | c_save_expr (tree expr) | |
4256 | { | |
4257 | bool maybe_const = true; | |
4258 | if (c_dialect_cxx ()) | |
4259 | return save_expr (expr); | |
4260 | expr = c_fully_fold (expr, false, &maybe_const); | |
4261 | expr = save_expr (expr); | |
4262 | if (!maybe_const) | |
f59e3889 | 4263 | expr = c_wrap_maybe_const (expr, true); |
a75b1c71 | 4264 | return expr; |
4265 | } | |
4266 | ||
6b68e71a | 4267 | /* Return whether EXPR is a declaration whose address can never be |
4268 | NULL. */ | |
4269 | ||
4270 | bool | |
9f627b1a | 4271 | decl_with_nonnull_addr_p (const_tree expr) |
6b68e71a | 4272 | { |
4273 | return (DECL_P (expr) | |
4274 | && (TREE_CODE (expr) == PARM_DECL | |
4275 | || TREE_CODE (expr) == LABEL_DECL | |
4276 | || !DECL_WEAK (expr))); | |
4277 | } | |
4278 | ||
b0fc3e72 | 4279 | /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, |
45a78cc0 | 4280 | or for an `if' or `while' statement or ?..: exp. It should already |
4281 | have been validated to be of suitable type; otherwise, a bad | |
4282 | diagnostic may result. | |
b0fc3e72 | 4283 | |
8e70fb09 | 4284 | The EXPR is located at LOCATION. |
4285 | ||
b0fc3e72 | 4286 | This preparation consists of taking the ordinary |
4287 | representation of an expression expr and producing a valid tree | |
4288 | boolean expression describing whether expr is nonzero. We could | |
3c2239cf | 4289 | simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1), |
b0fc3e72 | 4290 | but we optimize comparisons, &&, ||, and !. |
4291 | ||
3c2239cf | 4292 | The resulting type should always be `truthvalue_type_node'. */ |
b0fc3e72 | 4293 | |
4294 | tree | |
8e70fb09 | 4295 | c_common_truthvalue_conversion (location_t location, tree expr) |
b0fc3e72 | 4296 | { |
b0fc3e72 | 4297 | switch (TREE_CODE (expr)) |
4298 | { | |
318a728f | 4299 | case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR: |
e1232ce2 | 4300 | case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: |
4301 | case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR: | |
4302 | case ORDERED_EXPR: case UNORDERED_EXPR: | |
ce04dcdc | 4303 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4304 | return expr; | |
e60a6f7b | 4305 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
ce04dcdc | 4306 | TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); |
e60a6f7b | 4307 | goto ret; |
ce04dcdc | 4308 | |
b0fc3e72 | 4309 | case TRUTH_ANDIF_EXPR: |
4310 | case TRUTH_ORIF_EXPR: | |
4311 | case TRUTH_AND_EXPR: | |
4312 | case TRUTH_OR_EXPR: | |
31f6e93c | 4313 | case TRUTH_XOR_EXPR: |
ce04dcdc | 4314 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4315 | return expr; | |
e60a6f7b | 4316 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
48e1416a | 4317 | c_common_truthvalue_conversion (location, |
e60a6f7b | 4318 | TREE_OPERAND (expr, 0)), |
4319 | c_common_truthvalue_conversion (location, | |
4320 | TREE_OPERAND (expr, 1))); | |
4321 | goto ret; | |
3e851b85 | 4322 | |
f6e28f72 | 4323 | case TRUTH_NOT_EXPR: |
ce04dcdc | 4324 | if (TREE_TYPE (expr) == truthvalue_type_node) |
4325 | return expr; | |
e60a6f7b | 4326 | expr = build1 (TREE_CODE (expr), truthvalue_type_node, |
4327 | c_common_truthvalue_conversion (location, | |
4328 | TREE_OPERAND (expr, 0))); | |
4329 | goto ret; | |
f6e28f72 | 4330 | |
b0fc3e72 | 4331 | case ERROR_MARK: |
4332 | return expr; | |
4333 | ||
4334 | case INTEGER_CST: | |
eddad94a | 4335 | return integer_zerop (expr) ? truthvalue_false_node |
4336 | : truthvalue_true_node; | |
b0fc3e72 | 4337 | |
4338 | case REAL_CST: | |
5000e21c | 4339 | return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0) |
4340 | ? truthvalue_true_node | |
4341 | : truthvalue_false_node; | |
b0fc3e72 | 4342 | |
9421ebb9 | 4343 | case FIXED_CST: |
4344 | return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr), | |
4345 | &FCONST0 (TYPE_MODE (TREE_TYPE (expr)))) | |
4346 | ? truthvalue_true_node | |
4347 | : truthvalue_false_node; | |
4348 | ||
ce04dcdc | 4349 | case FUNCTION_DECL: |
b6889cb0 | 4350 | expr = build_unary_op (location, ADDR_EXPR, expr, 0); |
ce04dcdc | 4351 | /* Fall through. */ |
4352 | ||
b0fc3e72 | 4353 | case ADDR_EXPR: |
ce871053 | 4354 | { |
ee7d4d6a | 4355 | tree inner = TREE_OPERAND (expr, 0); |
6b68e71a | 4356 | if (decl_with_nonnull_addr_p (inner)) |
ce871053 | 4357 | { |
6b68e71a | 4358 | /* Common Ada/Pascal programmer's mistake. */ |
8e70fb09 | 4359 | warning_at (location, |
4360 | OPT_Waddress, | |
4361 | "the address of %qD will always evaluate as %<true%>", | |
4362 | inner); | |
ce871053 | 4363 | return truthvalue_true_node; |
4364 | } | |
d473d901 | 4365 | break; |
ce871053 | 4366 | } |
b0fc3e72 | 4367 | |
2203bd5c | 4368 | case COMPLEX_EXPR: |
e60a6f7b | 4369 | expr = build_binary_op (EXPR_LOCATION (expr), |
8e70fb09 | 4370 | (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) |
95809de4 | 4371 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
8e70fb09 | 4372 | c_common_truthvalue_conversion (location, |
4373 | TREE_OPERAND (expr, 0)), | |
4374 | c_common_truthvalue_conversion (location, | |
4375 | TREE_OPERAND (expr, 1)), | |
2203bd5c | 4376 | 0); |
e60a6f7b | 4377 | goto ret; |
2203bd5c | 4378 | |
b0fc3e72 | 4379 | case NEGATE_EXPR: |
4380 | case ABS_EXPR: | |
4381 | case FLOAT_EXPR: | |
c6418a4e | 4382 | case EXCESS_PRECISION_EXPR: |
d10cfa8d | 4383 | /* These don't change whether an object is nonzero or zero. */ |
8e70fb09 | 4384 | return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); |
b0fc3e72 | 4385 | |
4386 | case LROTATE_EXPR: | |
4387 | case RROTATE_EXPR: | |
d10cfa8d | 4388 | /* These don't change whether an object is zero or nonzero, but |
b0fc3e72 | 4389 | we can't ignore them if their second arg has side-effects. */ |
4390 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) | |
e60a6f7b | 4391 | { |
4392 | expr = build2 (COMPOUND_EXPR, truthvalue_type_node, | |
4393 | TREE_OPERAND (expr, 1), | |
48e1416a | 4394 | c_common_truthvalue_conversion |
e60a6f7b | 4395 | (location, TREE_OPERAND (expr, 0))); |
4396 | goto ret; | |
4397 | } | |
b0fc3e72 | 4398 | else |
8e70fb09 | 4399 | return c_common_truthvalue_conversion (location, |
4400 | TREE_OPERAND (expr, 0)); | |
73be5127 | 4401 | |
b0fc3e72 | 4402 | case COND_EXPR: |
4403 | /* Distribute the conversion into the arms of a COND_EXPR. */ | |
a75b1c71 | 4404 | if (c_dialect_cxx ()) |
e60a6f7b | 4405 | { |
d0389adc | 4406 | tree op1 = TREE_OPERAND (expr, 1); |
4407 | tree op2 = TREE_OPERAND (expr, 2); | |
4408 | /* In C++ one of the arms might have void type if it is throw. */ | |
4409 | if (!VOID_TYPE_P (TREE_TYPE (op1))) | |
4410 | op1 = c_common_truthvalue_conversion (location, op1); | |
4411 | if (!VOID_TYPE_P (TREE_TYPE (op2))) | |
4412 | op2 = c_common_truthvalue_conversion (location, op2); | |
389dd41b | 4413 | expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node, |
d0389adc | 4414 | TREE_OPERAND (expr, 0), op1, op2); |
e60a6f7b | 4415 | goto ret; |
4416 | } | |
a75b1c71 | 4417 | else |
e60a6f7b | 4418 | { |
4419 | /* Folding will happen later for C. */ | |
4420 | expr = build3 (COND_EXPR, truthvalue_type_node, | |
4421 | TREE_OPERAND (expr, 0), | |
4422 | c_common_truthvalue_conversion (location, | |
4423 | TREE_OPERAND (expr, 1)), | |
4424 | c_common_truthvalue_conversion (location, | |
4425 | TREE_OPERAND (expr, 2))); | |
4426 | goto ret; | |
4427 | } | |
b0fc3e72 | 4428 | |
72dd6141 | 4429 | CASE_CONVERT: |
f8913d47 | 4430 | { |
4431 | tree totype = TREE_TYPE (expr); | |
4432 | tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0)); | |
4433 | ||
4434 | /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, | |
4435 | since that affects how `default_conversion' will behave. */ | |
4436 | if (TREE_CODE (totype) == REFERENCE_TYPE | |
4437 | || TREE_CODE (fromtype) == REFERENCE_TYPE) | |
4438 | break; | |
4439 | /* Don't strip a conversion from C++0x scoped enum, since they | |
4440 | don't implicitly convert to other types. */ | |
4441 | if (TREE_CODE (fromtype) == ENUMERAL_TYPE | |
4442 | && ENUM_IS_SCOPED (fromtype)) | |
4443 | break; | |
4444 | /* If this isn't narrowing the argument, we can ignore it. */ | |
4445 | if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype)) | |
4446 | return c_common_truthvalue_conversion (location, | |
4447 | TREE_OPERAND (expr, 0)); | |
4448 | } | |
b0fc3e72 | 4449 | break; |
4450 | ||
16837b18 | 4451 | case MODIFY_EXPR: |
60a0513e | 4452 | if (!TREE_NO_WARNING (expr) |
4453 | && warn_parentheses) | |
4454 | { | |
4455 | warning (OPT_Wparentheses, | |
4456 | "suggest parentheses around assignment used as truth value"); | |
4457 | TREE_NO_WARNING (expr) = 1; | |
4458 | } | |
16837b18 | 4459 | break; |
73be5127 | 4460 | |
31f820d2 | 4461 | default: |
4462 | break; | |
b0fc3e72 | 4463 | } |
4464 | ||
2ba726d2 | 4465 | if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) |
a0748b7d | 4466 | { |
93be21c0 | 4467 | tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr)); |
e60a6f7b | 4468 | expr = (build_binary_op |
8e70fb09 | 4469 | (EXPR_LOCATION (expr), |
4470 | (TREE_SIDE_EFFECTS (expr) | |
a0748b7d | 4471 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
b6889cb0 | 4472 | c_common_truthvalue_conversion |
4473 | (location, | |
4474 | build_unary_op (location, REALPART_EXPR, t, 0)), | |
4475 | c_common_truthvalue_conversion | |
4476 | (location, | |
4477 | build_unary_op (location, IMAGPART_EXPR, t, 0)), | |
a0748b7d | 4478 | 0)); |
e60a6f7b | 4479 | goto ret; |
a0748b7d | 4480 | } |
2ba726d2 | 4481 | |
9421ebb9 | 4482 | if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE) |
4483 | { | |
4484 | tree fixed_zero_node = build_fixed (TREE_TYPE (expr), | |
4485 | FCONST0 (TYPE_MODE | |
4486 | (TREE_TYPE (expr)))); | |
43158006 | 4487 | return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1); |
9421ebb9 | 4488 | } |
e60a6f7b | 4489 | else |
4490 | return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1); | |
9421ebb9 | 4491 | |
e60a6f7b | 4492 | ret: |
4493 | protected_set_expr_location (expr, location); | |
4494 | return expr; | |
b0fc3e72 | 4495 | } |
4496 | \f | |
3237155d | 4497 | static void def_builtin_1 (enum built_in_function fncode, |
4498 | const char *name, | |
4499 | enum built_in_class fnclass, | |
4500 | tree fntype, tree libtype, | |
4501 | bool both_p, bool fallback_p, bool nonansi_p, | |
4502 | tree fnattrs, bool implicit_p); | |
0d4238dc | 4503 | |
a5b1863e | 4504 | |
4505 | /* Apply the TYPE_QUALS to the new DECL. */ | |
4506 | ||
4507 | void | |
1cae46be | 4508 | c_apply_type_quals_to_decl (int type_quals, tree decl) |
a5b1863e | 4509 | { |
adfb367f | 4510 | tree type = TREE_TYPE (decl); |
b27ac6b5 | 4511 | |
e4eabbe4 | 4512 | if (type == error_mark_node) |
4513 | return; | |
adfb367f | 4514 | |
98a33d9f | 4515 | if ((type_quals & TYPE_QUAL_CONST) |
4516 | || (type && TREE_CODE (type) == REFERENCE_TYPE)) | |
4517 | /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr | |
4518 | constructor can produce constant init, so rely on cp_finish_decl to | |
4519 | clear TREE_READONLY if the variable has non-constant init. */ | |
a5b1863e | 4520 | TREE_READONLY (decl) = 1; |
4521 | if (type_quals & TYPE_QUAL_VOLATILE) | |
4522 | { | |
4523 | TREE_SIDE_EFFECTS (decl) = 1; | |
4524 | TREE_THIS_VOLATILE (decl) = 1; | |
4525 | } | |
d91a20bc | 4526 | if (type_quals & TYPE_QUAL_RESTRICT) |
a5b1863e | 4527 | { |
adfb367f | 4528 | while (type && TREE_CODE (type) == ARRAY_TYPE) |
4529 | /* Allow 'restrict' on arrays of pointers. | |
4530 | FIXME currently we just ignore it. */ | |
4531 | type = TREE_TYPE (type); | |
4532 | if (!type | |
4533 | || !POINTER_TYPE_P (type) | |
4534 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))) | |
b0b1af64 | 4535 | error ("invalid use of %<restrict%>"); |
a5b1863e | 4536 | } |
4537 | } | |
4538 | ||
4ee9c684 | 4539 | /* Hash function for the problem of multiple type definitions in |
4540 | different files. This must hash all types that will compare | |
4541 | equal via comptypes to the same value. In practice it hashes | |
0bed3869 | 4542 | on some of the simple stuff and leaves the details to comptypes. */ |
4ee9c684 | 4543 | |
4544 | static hashval_t | |
4545 | c_type_hash (const void *p) | |
4546 | { | |
ecf2703d | 4547 | int n_elements; |
4ee9c684 | 4548 | int shift, size; |
aae87fc3 | 4549 | const_tree const t = (const_tree) p; |
4ee9c684 | 4550 | tree t2; |
4551 | switch (TREE_CODE (t)) | |
4552 | { | |
fbf0afd1 | 4553 | /* For pointers, hash on pointee type plus some swizzling. */ |
2363ef00 | 4554 | case POINTER_TYPE: |
4555 | return c_type_hash (TREE_TYPE (t)) ^ 0x3003003; | |
4556 | /* Hash on number of elements and total size. */ | |
4557 | case ENUMERAL_TYPE: | |
4558 | shift = 3; | |
4559 | t2 = TYPE_VALUES (t); | |
4560 | break; | |
4561 | case RECORD_TYPE: | |
4562 | shift = 0; | |
4563 | t2 = TYPE_FIELDS (t); | |
4564 | break; | |
4565 | case QUAL_UNION_TYPE: | |
4566 | shift = 1; | |
4567 | t2 = TYPE_FIELDS (t); | |
4568 | break; | |
4569 | case UNION_TYPE: | |
4570 | shift = 2; | |
4571 | t2 = TYPE_FIELDS (t); | |
4572 | break; | |
4573 | default: | |
231bd014 | 4574 | gcc_unreachable (); |
4ee9c684 | 4575 | } |
ecf2703d | 4576 | /* FIXME: We want to use a DECL_CHAIN iteration method here, but |
4577 | TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */ | |
4578 | n_elements = list_length (t2); | |
78c2e180 | 4579 | /* We might have a VLA here. */ |
4580 | if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST) | |
4581 | size = 0; | |
4582 | else | |
4583 | size = TREE_INT_CST_LOW (TYPE_SIZE (t)); | |
ecf2703d | 4584 | return ((size << 24) | (n_elements << shift)); |
4ee9c684 | 4585 | } |
4586 | ||
1ecd4018 | 4587 | static GTY((param_is (union tree_node))) htab_t type_hash_table; |
4588 | ||
b5ba9f3a | 4589 | /* Return the typed-based alias set for T, which may be an expression |
f7c44134 | 4590 | or a type. Return -1 if we don't do anything special. */ |
b5ba9f3a | 4591 | |
32c2fdea | 4592 | alias_set_type |
1cae46be | 4593 | c_common_get_alias_set (tree t) |
b5ba9f3a | 4594 | { |
be4f2de7 | 4595 | tree u; |
4ee9c684 | 4596 | PTR *slot; |
1cae46be | 4597 | |
e58c17e7 | 4598 | /* For VLAs, use the alias set of the element type rather than the |
4599 | default of alias set 0 for types compared structurally. */ | |
4600 | if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t)) | |
4601 | { | |
4602 | if (TREE_CODE (t) == ARRAY_TYPE) | |
4603 | return get_alias_set (TREE_TYPE (t)); | |
4604 | return -1; | |
4605 | } | |
4606 | ||
be4f2de7 | 4607 | /* Permit type-punning when accessing a union, provided the access |
4608 | is directly through the union. For example, this code does not | |
4609 | permit taking the address of a union member and then storing | |
4610 | through it. Even the type-punning allowed here is a GCC | |
4611 | extension, albeit a common and useful one; the C standard says | |
4612 | that such accesses have implementation-defined behavior. */ | |
4613 | for (u = t; | |
4614 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
4615 | u = TREE_OPERAND (u, 0)) | |
4616 | if (TREE_CODE (u) == COMPONENT_REF | |
4617 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
4618 | return 0; | |
1e2513d9 | 4619 | |
9fcc3e54 | 4620 | /* That's all the expressions we handle specially. */ |
84166705 | 4621 | if (!TYPE_P (t)) |
9fcc3e54 | 4622 | return -1; |
4623 | ||
d716ce75 | 4624 | /* The C standard guarantees that any object may be accessed via an |
9fcc3e54 | 4625 | lvalue that has character type. */ |
4626 | if (t == char_type_node | |
4627 | || t == signed_char_type_node | |
4628 | || t == unsigned_char_type_node) | |
f7c44134 | 4629 | return 0; |
a5b1863e | 4630 | |
1607663f | 4631 | /* The C standard specifically allows aliasing between signed and |
4632 | unsigned variants of the same type. We treat the signed | |
4633 | variant as canonical. */ | |
78a8ed03 | 4634 | if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t)) |
a8868e19 | 4635 | { |
4070745f | 4636 | tree t1 = c_common_signed_type (t); |
1607663f | 4637 | |
a8868e19 | 4638 | /* t1 == t can happen for boolean nodes which are always unsigned. */ |
4639 | if (t1 != t) | |
4640 | return get_alias_set (t1); | |
4641 | } | |
1e2513d9 | 4642 | |
4ee9c684 | 4643 | /* Handle the case of multiple type nodes referring to "the same" type, |
4644 | which occurs with IMA. These share an alias set. FIXME: Currently only | |
4645 | C90 is handled. (In C99 type compatibility is not transitive, which | |
4646 | complicates things mightily. The alias set splay trees can theoretically | |
4647 | represent this, but insertion is tricky when you consider all the | |
4648 | different orders things might arrive in.) */ | |
4649 | ||
4650 | if (c_language != clk_c || flag_isoc99) | |
4651 | return -1; | |
4652 | ||
0bed3869 | 4653 | /* Save time if there's only one input file. */ |
e08bd2f4 | 4654 | if (num_in_fnames == 1) |
4ee9c684 | 4655 | return -1; |
4656 | ||
4657 | /* Pointers need special handling if they point to any type that | |
4658 | needs special handling (below). */ | |
4659 | if (TREE_CODE (t) == POINTER_TYPE) | |
4660 | { | |
4661 | tree t2; | |
4662 | /* Find bottom type under any nested POINTERs. */ | |
b27ac6b5 | 4663 | for (t2 = TREE_TYPE (t); |
af592f67 | 4664 | TREE_CODE (t2) == POINTER_TYPE; |
4665 | t2 = TREE_TYPE (t2)) | |
4666 | ; | |
b27ac6b5 | 4667 | if (TREE_CODE (t2) != RECORD_TYPE |
af592f67 | 4668 | && TREE_CODE (t2) != ENUMERAL_TYPE |
4669 | && TREE_CODE (t2) != QUAL_UNION_TYPE | |
4670 | && TREE_CODE (t2) != UNION_TYPE) | |
4671 | return -1; | |
4ee9c684 | 4672 | if (TYPE_SIZE (t2) == 0) |
af592f67 | 4673 | return -1; |
4ee9c684 | 4674 | } |
4675 | /* These are the only cases that need special handling. */ | |
b27ac6b5 | 4676 | if (TREE_CODE (t) != RECORD_TYPE |
4ee9c684 | 4677 | && TREE_CODE (t) != ENUMERAL_TYPE |
4678 | && TREE_CODE (t) != QUAL_UNION_TYPE | |
4679 | && TREE_CODE (t) != UNION_TYPE | |
4680 | && TREE_CODE (t) != POINTER_TYPE) | |
4681 | return -1; | |
4682 | /* Undefined? */ | |
4683 | if (TYPE_SIZE (t) == 0) | |
4684 | return -1; | |
4685 | ||
b27ac6b5 | 4686 | /* Look up t in hash table. Only one of the compatible types within each |
4ee9c684 | 4687 | alias set is recorded in the table. */ |
4688 | if (!type_hash_table) | |
1ecd4018 | 4689 | type_hash_table = htab_create_ggc (1021, c_type_hash, |
4ee9c684 | 4690 | (htab_eq) lang_hooks.types_compatible_p, |
4691 | NULL); | |
4692 | slot = htab_find_slot (type_hash_table, t, INSERT); | |
4693 | if (*slot != NULL) | |
ad16cb2c | 4694 | { |
4695 | TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot); | |
4696 | return TYPE_ALIAS_SET ((tree)*slot); | |
4697 | } | |
4ee9c684 | 4698 | else |
4699 | /* Our caller will assign and record (in t) a new alias set; all we need | |
4700 | to do is remember t in the hash table. */ | |
4701 | *slot = t; | |
4702 | ||
f7c44134 | 4703 | return -1; |
b5ba9f3a | 4704 | } |
902b4e01 | 4705 | \f |
e60a6f7b | 4706 | /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where |
4707 | the second parameter indicates which OPERATOR is being applied. | |
4708 | The COMPLAIN flag controls whether we should diagnose possibly | |
4709 | ill-formed constructs or not. LOC is the location of the SIZEOF or | |
4710 | TYPEOF operator. */ | |
908c697e | 4711 | |
902b4e01 | 4712 | tree |
e60a6f7b | 4713 | c_sizeof_or_alignof_type (location_t loc, |
4714 | tree type, bool is_sizeof, int complain) | |
902b4e01 | 4715 | { |
d4c4d95c | 4716 | const char *op_name; |
4717 | tree value = NULL; | |
4718 | enum tree_code type_code = TREE_CODE (type); | |
1cae46be | 4719 | |
908c697e | 4720 | op_name = is_sizeof ? "sizeof" : "__alignof__"; |
1cae46be | 4721 | |
d4c4d95c | 4722 | if (type_code == FUNCTION_TYPE) |
902b4e01 | 4723 | { |
908c697e | 4724 | if (is_sizeof) |
d4c4d95c | 4725 | { |
d3a4d008 | 4726 | if (complain && (pedantic || warn_pointer_arith)) |
29438999 | 4727 | pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
8864917d | 4728 | "invalid application of %<sizeof%> to a function type"); |
ebd21de4 | 4729 | else if (!complain) |
4730 | return error_mark_node; | |
d4c4d95c | 4731 | value = size_one_node; |
4732 | } | |
4733 | else | |
83e25171 | 4734 | { |
4735 | if (complain) | |
4736 | { | |
4737 | if (c_dialect_cxx ()) | |
29438999 | 4738 | pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit " |
83e25171 | 4739 | "%<alignof%> applied to a function type"); |
4740 | else | |
29438999 | 4741 | pedwarn (loc, OPT_Wpedantic, "ISO C does not permit " |
83e25171 | 4742 | "%<_Alignof%> applied to a function type"); |
4743 | } | |
4744 | value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); | |
4745 | } | |
d4c4d95c | 4746 | } |
4747 | else if (type_code == VOID_TYPE || type_code == ERROR_MARK) | |
4748 | { | |
1cae46be | 4749 | if (type_code == VOID_TYPE |
d3a4d008 | 4750 | && complain && (pedantic || warn_pointer_arith)) |
29438999 | 4751 | pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith, |
8864917d | 4752 | "invalid application of %qs to a void type", op_name); |
ebd21de4 | 4753 | else if (!complain) |
4754 | return error_mark_node; | |
d4c4d95c | 4755 | value = size_one_node; |
902b4e01 | 4756 | } |
3df19e1b | 4757 | else if (!COMPLETE_TYPE_P (type) |
4758 | && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE)) | |
902b4e01 | 4759 | { |
d3a4d008 | 4760 | if (complain) |
3df19e1b | 4761 | error_at (loc, "invalid application of %qs to incomplete type %qT", |
e60a6f7b | 4762 | op_name, type); |
9c719c74 | 4763 | return error_mark_node; |
902b4e01 | 4764 | } |
3df19e1b | 4765 | else if (c_dialect_cxx () && type_code == ARRAY_TYPE |
4766 | && !COMPLETE_TYPE_P (TREE_TYPE (type))) | |
4767 | { | |
4768 | if (complain) | |
4769 | error_at (loc, "invalid application of %qs to array type %qT of " | |
4770 | "incomplete element type", op_name, type); | |
4771 | return error_mark_node; | |
4772 | } | |
902b4e01 | 4773 | else |
d4c4d95c | 4774 | { |
908c697e | 4775 | if (is_sizeof) |
d4c4d95c | 4776 | /* Convert in case a char is more than one unit. */ |
389dd41b | 4777 | value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
4778 | size_int (TYPE_PRECISION (char_type_node) | |
4779 | / BITS_PER_UNIT)); | |
d4c4d95c | 4780 | else |
d37625c0 | 4781 | value = size_int (TYPE_ALIGN_UNIT (type)); |
d4c4d95c | 4782 | } |
902b4e01 | 4783 | |
5a1fe2db | 4784 | /* VALUE will have the middle-end integer type sizetype. |
4785 | However, we should really return a value of type `size_t', | |
4786 | which is just a typedef for an ordinary integer type. */ | |
389dd41b | 4787 | value = fold_convert_loc (loc, size_type_node, value); |
1cae46be | 4788 | |
d4c4d95c | 4789 | return value; |
902b4e01 | 4790 | } |
4791 | ||
4792 | /* Implement the __alignof keyword: Return the minimum required | |
097b5c8b | 4793 | alignment of EXPR, measured in bytes. For VAR_DECLs, |
4794 | FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set | |
e60a6f7b | 4795 | from an "aligned" __attribute__ specification). LOC is the |
4796 | location of the ALIGNOF operator. */ | |
72040e7e | 4797 | |
902b4e01 | 4798 | tree |
e60a6f7b | 4799 | c_alignof_expr (location_t loc, tree expr) |
902b4e01 | 4800 | { |
4801 | tree t; | |
4802 | ||
097b5c8b | 4803 | if (VAR_OR_FUNCTION_DECL_P (expr)) |
d37625c0 | 4804 | t = size_int (DECL_ALIGN_UNIT (expr)); |
1cae46be | 4805 | |
902b4e01 | 4806 | else if (TREE_CODE (expr) == COMPONENT_REF |
4807 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
4808 | { | |
e60a6f7b | 4809 | error_at (loc, "%<__alignof%> applied to a bit-field"); |
902b4e01 | 4810 | t = size_one_node; |
4811 | } | |
4812 | else if (TREE_CODE (expr) == COMPONENT_REF | |
7cc7e163 | 4813 | && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) |
d37625c0 | 4814 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1))); |
1cae46be | 4815 | |
902b4e01 | 4816 | else if (TREE_CODE (expr) == INDIRECT_REF) |
4817 | { | |
4818 | tree t = TREE_OPERAND (expr, 0); | |
4819 | tree best = t; | |
4820 | int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
1cae46be | 4821 | |
72dd6141 | 4822 | while (CONVERT_EXPR_P (t) |
7cc7e163 | 4823 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) |
902b4e01 | 4824 | { |
4825 | int thisalign; | |
4826 | ||
4827 | t = TREE_OPERAND (t, 0); | |
4828 | thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
4829 | if (thisalign > bestalign) | |
4830 | best = t, bestalign = thisalign; | |
4831 | } | |
e60a6f7b | 4832 | return c_alignof (loc, TREE_TYPE (TREE_TYPE (best))); |
902b4e01 | 4833 | } |
4834 | else | |
e60a6f7b | 4835 | return c_alignof (loc, TREE_TYPE (expr)); |
902b4e01 | 4836 | |
389dd41b | 4837 | return fold_convert_loc (loc, size_type_node, t); |
902b4e01 | 4838 | } |
4839 | \f | |
8fe4a266 | 4840 | /* Handle C and C++ default attributes. */ |
4841 | ||
4842 | enum built_in_attribute | |
4843 | { | |
4844 | #define DEF_ATTR_NULL_TREE(ENUM) ENUM, | |
4845 | #define DEF_ATTR_INT(ENUM, VALUE) ENUM, | |
c8010b80 | 4846 | #define DEF_ATTR_STRING(ENUM, VALUE) ENUM, |
8fe4a266 | 4847 | #define DEF_ATTR_IDENT(ENUM, STRING) ENUM, |
4848 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM, | |
8fe4a266 | 4849 | #include "builtin-attrs.def" |
4850 | #undef DEF_ATTR_NULL_TREE | |
4851 | #undef DEF_ATTR_INT | |
c8010b80 | 4852 | #undef DEF_ATTR_STRING |
8fe4a266 | 4853 | #undef DEF_ATTR_IDENT |
4854 | #undef DEF_ATTR_TREE_LIST | |
8fe4a266 | 4855 | ATTR_LAST |
4856 | }; | |
4857 | ||
4858 | static GTY(()) tree built_in_attributes[(int) ATTR_LAST]; | |
4859 | ||
1cae46be | 4860 | static void c_init_attributes (void); |
8fe4a266 | 4861 | |
27213ba3 | 4862 | enum c_builtin_type |
72040e7e | 4863 | { |
d2d4bdde | 4864 | #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME, |
4865 | #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME, | |
4866 | #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME, | |
4867 | #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME, | |
4868 | #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, | |
4869 | #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, | |
0a39fd54 | 4870 | #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME, |
27213ba3 | 4871 | #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME, |
4872 | #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME, | |
d2d4bdde | 4873 | #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME, |
4874 | #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME, | |
4875 | #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME, | |
03901330 | 4876 | #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, |
0a39fd54 | 4877 | #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, |
4878 | #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \ | |
4879 | NAME, | |
d2d4bdde | 4880 | #define DEF_POINTER_TYPE(NAME, TYPE) NAME, |
4881 | #include "builtin-types.def" | |
4882 | #undef DEF_PRIMITIVE_TYPE | |
4883 | #undef DEF_FUNCTION_TYPE_0 | |
4884 | #undef DEF_FUNCTION_TYPE_1 | |
4885 | #undef DEF_FUNCTION_TYPE_2 | |
4886 | #undef DEF_FUNCTION_TYPE_3 | |
4887 | #undef DEF_FUNCTION_TYPE_4 | |
0a39fd54 | 4888 | #undef DEF_FUNCTION_TYPE_5 |
4889 | #undef DEF_FUNCTION_TYPE_6 | |
27213ba3 | 4890 | #undef DEF_FUNCTION_TYPE_7 |
d2d4bdde | 4891 | #undef DEF_FUNCTION_TYPE_VAR_0 |
4892 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
4893 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
03901330 | 4894 | #undef DEF_FUNCTION_TYPE_VAR_3 |
0a39fd54 | 4895 | #undef DEF_FUNCTION_TYPE_VAR_4 |
4896 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
d2d4bdde | 4897 | #undef DEF_POINTER_TYPE |
27213ba3 | 4898 | BT_LAST |
4899 | }; | |
4900 | ||
4901 | typedef enum c_builtin_type builtin_type; | |
d2d4bdde | 4902 | |
27213ba3 | 4903 | /* A temporary array for c_common_nodes_and_builtins. Used in |
4904 | communication with def_fn_type. */ | |
4905 | static tree builtin_types[(int) BT_LAST + 1]; | |
d2d4bdde | 4906 | |
27213ba3 | 4907 | /* A helper function for c_common_nodes_and_builtins. Build function type |
4908 | for DEF with return type RET and N arguments. If VAR is true, then the | |
4909 | function should be variadic after those N arguments. | |
4910 | ||
4911 | Takes special care not to ICE if any of the types involved are | |
4912 | error_mark_node, which indicates that said type is not in fact available | |
4913 | (see builtin_type_for_size). In which case the function type as a whole | |
4914 | should be error_mark_node. */ | |
4915 | ||
4916 | static void | |
4917 | def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...) | |
4918 | { | |
3a939d12 | 4919 | tree t; |
4920 | tree *args = XALLOCAVEC (tree, n); | |
27213ba3 | 4921 | va_list list; |
4922 | int i; | |
4923 | ||
4924 | va_start (list, n); | |
4925 | for (i = 0; i < n; ++i) | |
4926 | { | |
7d339f93 | 4927 | builtin_type a = (builtin_type) va_arg (list, int); |
27213ba3 | 4928 | t = builtin_types[a]; |
4929 | if (t == error_mark_node) | |
4930 | goto egress; | |
3a939d12 | 4931 | args[i] = t; |
27213ba3 | 4932 | } |
27213ba3 | 4933 | |
27213ba3 | 4934 | t = builtin_types[ret]; |
4935 | if (t == error_mark_node) | |
4936 | goto egress; | |
3a939d12 | 4937 | if (var) |
4938 | t = build_varargs_function_type_array (t, n, args); | |
4939 | else | |
4940 | t = build_function_type_array (t, n, args); | |
27213ba3 | 4941 | |
4942 | egress: | |
4943 | builtin_types[def] = t; | |
451c8e2f | 4944 | va_end (list); |
27213ba3 | 4945 | } |
4946 | ||
dce22712 | 4947 | /* Build builtin functions common to both C and C++ language |
4948 | frontends. */ | |
4949 | ||
4950 | static void | |
4951 | c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node) | |
4952 | { | |
4953 | #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ | |
4954 | builtin_types[ENUM] = VALUE; | |
4955 | #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ | |
4956 | def_fn_type (ENUM, RETURN, 0, 0); | |
4957 | #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ | |
4958 | def_fn_type (ENUM, RETURN, 0, 1, ARG1); | |
4959 | #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ | |
4960 | def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2); | |
4961 | #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
4962 | def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3); | |
4963 | #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
4964 | def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4); | |
4965 | #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
4966 | def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
4967 | #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
4968 | ARG6) \ | |
4969 | def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); | |
4970 | #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
4971 | ARG6, ARG7) \ | |
4972 | def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); | |
4973 | #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ | |
4974 | def_fn_type (ENUM, RETURN, 1, 0); | |
4975 | #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ | |
4976 | def_fn_type (ENUM, RETURN, 1, 1, ARG1); | |
4977 | #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ | |
4978 | def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2); | |
4979 | #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
4980 | def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3); | |
4981 | #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
4982 | def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4); | |
4983 | #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
4984 | def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
4985 | #define DEF_POINTER_TYPE(ENUM, TYPE) \ | |
4986 | builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]); | |
4987 | ||
4988 | #include "builtin-types.def" | |
4989 | ||
4990 | #undef DEF_PRIMITIVE_TYPE | |
4991 | #undef DEF_FUNCTION_TYPE_1 | |
4992 | #undef DEF_FUNCTION_TYPE_2 | |
4993 | #undef DEF_FUNCTION_TYPE_3 | |
4994 | #undef DEF_FUNCTION_TYPE_4 | |
4995 | #undef DEF_FUNCTION_TYPE_5 | |
4996 | #undef DEF_FUNCTION_TYPE_6 | |
4997 | #undef DEF_FUNCTION_TYPE_VAR_0 | |
4998 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
4999 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
5000 | #undef DEF_FUNCTION_TYPE_VAR_3 | |
5001 | #undef DEF_FUNCTION_TYPE_VAR_4 | |
5002 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
5003 | #undef DEF_POINTER_TYPE | |
5004 | builtin_types[(int) BT_LAST] = NULL_TREE; | |
5005 | ||
5006 | c_init_attributes (); | |
5007 | ||
5008 | #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ | |
5009 | NONANSI_P, ATTRS, IMPLICIT, COND) \ | |
5010 | if (NAME && COND) \ | |
5011 | def_builtin_1 (ENUM, NAME, CLASS, \ | |
5012 | builtin_types[(int) TYPE], \ | |
5013 | builtin_types[(int) LIBTYPE], \ | |
5014 | BOTH_P, FALLBACK_P, NONANSI_P, \ | |
5015 | built_in_attributes[(int) ATTRS], IMPLICIT); | |
5016 | #include "builtins.def" | |
5017 | #undef DEF_BUILTIN | |
5018 | ||
87eb1c28 | 5019 | targetm.init_builtins (); |
5020 | ||
471eff36 | 5021 | build_common_builtin_nodes (); |
dce22712 | 5022 | |
dce22712 | 5023 | if (flag_mudflap) |
5024 | mudflap_init (); | |
5025 | } | |
5026 | ||
9e6687c8 | 5027 | /* Like get_identifier, but avoid warnings about null arguments when |
5028 | the argument may be NULL for targets where GCC lacks stdint.h type | |
5029 | information. */ | |
5030 | ||
5031 | static inline tree | |
5032 | c_get_ident (const char *id) | |
5033 | { | |
5034 | return get_identifier (id); | |
5035 | } | |
5036 | ||
27213ba3 | 5037 | /* Build tree nodes and builtin functions common to both C and C++ language |
5038 | frontends. */ | |
5039 | ||
5040 | void | |
5041 | c_common_nodes_and_builtins (void) | |
5042 | { | |
924bbf02 | 5043 | int char16_type_size; |
5044 | int char32_type_size; | |
174fcc61 | 5045 | int wchar_type_size; |
5046 | tree array_domain_type; | |
2d47cc32 | 5047 | tree va_list_ref_type_node; |
8a15c04a | 5048 | tree va_list_arg_type_node; |
a66c9326 | 5049 | |
c38a75b7 | 5050 | build_common_tree_nodes (flag_signed_char, flag_short_double); |
e593356b | 5051 | |
174fcc61 | 5052 | /* Define `int' and `char' first so that dbx will output them first. */ |
d946ea19 | 5053 | record_builtin_type (RID_INT, NULL, integer_type_node); |
174fcc61 | 5054 | record_builtin_type (RID_CHAR, "char", char_type_node); |
5055 | ||
5056 | /* `signed' is the same as `int'. FIXME: the declarations of "signed", | |
5057 | "unsigned long", "long long unsigned" and "unsigned short" were in C++ | |
5058 | but not C. Are the conditionals here needed? */ | |
c0f19401 | 5059 | if (c_dialect_cxx ()) |
d946ea19 | 5060 | record_builtin_type (RID_SIGNED, NULL, integer_type_node); |
174fcc61 | 5061 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); |
5062 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); | |
5063 | record_builtin_type (RID_MAX, "long unsigned int", | |
5064 | long_unsigned_type_node); | |
6388cfe2 | 5065 | if (int128_integer_type_node != NULL_TREE) |
5066 | { | |
5067 | record_builtin_type (RID_INT128, "__int128", | |
5068 | int128_integer_type_node); | |
5069 | record_builtin_type (RID_MAX, "__int128 unsigned", | |
5070 | int128_unsigned_type_node); | |
5071 | } | |
c0f19401 | 5072 | if (c_dialect_cxx ()) |
174fcc61 | 5073 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); |
5074 | record_builtin_type (RID_MAX, "long long int", | |
5075 | long_long_integer_type_node); | |
5076 | record_builtin_type (RID_MAX, "long long unsigned int", | |
5077 | long_long_unsigned_type_node); | |
c0f19401 | 5078 | if (c_dialect_cxx ()) |
174fcc61 | 5079 | record_builtin_type (RID_MAX, "long long unsigned", |
5080 | long_long_unsigned_type_node); | |
5081 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); | |
5082 | record_builtin_type (RID_MAX, "short unsigned int", | |
5083 | short_unsigned_type_node); | |
c0f19401 | 5084 | if (c_dialect_cxx ()) |
174fcc61 | 5085 | record_builtin_type (RID_MAX, "unsigned short", |
5086 | short_unsigned_type_node); | |
5087 | ||
5088 | /* Define both `signed char' and `unsigned char'. */ | |
5089 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); | |
5090 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); | |
5091 | ||
771d21fa | 5092 | /* These are types that c_common_type_for_size and |
5093 | c_common_type_for_mode use. */ | |
e60a6f7b | 5094 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5095 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5096 | intQI_type_node)); |
e60a6f7b | 5097 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5098 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5099 | intHI_type_node)); |
e60a6f7b | 5100 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5101 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5102 | intSI_type_node)); |
e60a6f7b | 5103 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5104 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5105 | intDI_type_node)); |
174fcc61 | 5106 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 5107 | if (targetm.scalar_mode_supported_p (TImode)) |
e60a6f7b | 5108 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5109 | TYPE_DECL, | |
f1515a39 | 5110 | get_identifier ("__int128_t"), |
5111 | intTI_type_node)); | |
174fcc61 | 5112 | #endif |
e60a6f7b | 5113 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5114 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5115 | unsigned_intQI_type_node)); |
e60a6f7b | 5116 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5117 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5118 | unsigned_intHI_type_node)); |
e60a6f7b | 5119 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5120 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5121 | unsigned_intSI_type_node)); |
e60a6f7b | 5122 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5123 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5124 | unsigned_intDI_type_node)); |
174fcc61 | 5125 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 5126 | if (targetm.scalar_mode_supported_p (TImode)) |
e60a6f7b | 5127 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5128 | TYPE_DECL, | |
f1515a39 | 5129 | get_identifier ("__uint128_t"), |
5130 | unsigned_intTI_type_node)); | |
174fcc61 | 5131 | #endif |
5132 | ||
5133 | /* Create the widest literal types. */ | |
5134 | widest_integer_literal_type_node | |
5135 | = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
e60a6f7b | 5136 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5137 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5138 | widest_integer_literal_type_node)); |
174fcc61 | 5139 | |
5140 | widest_unsigned_literal_type_node | |
5141 | = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
e60a6f7b | 5142 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5143 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 5144 | widest_unsigned_literal_type_node)); |
174fcc61 | 5145 | |
654ef926 | 5146 | signed_size_type_node = c_common_signed_type (size_type_node); |
174fcc61 | 5147 | |
73673831 | 5148 | pid_type_node = |
5149 | TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE))); | |
5150 | ||
d946ea19 | 5151 | record_builtin_type (RID_FLOAT, NULL, float_type_node); |
5152 | record_builtin_type (RID_DOUBLE, NULL, double_type_node); | |
174fcc61 | 5153 | record_builtin_type (RID_MAX, "long double", long_double_type_node); |
5154 | ||
c4503c0a | 5155 | /* Only supported decimal floating point extension if the target |
5156 | actually supports underlying modes. */ | |
48e1416a | 5157 | if (targetm.scalar_mode_supported_p (SDmode) |
c4503c0a | 5158 | && targetm.scalar_mode_supported_p (DDmode) |
5159 | && targetm.scalar_mode_supported_p (TDmode)) | |
5160 | { | |
5161 | record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node); | |
5162 | record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node); | |
5163 | record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node); | |
5164 | } | |
5165 | ||
9421ebb9 | 5166 | if (targetm.fixed_point_supported_p ()) |
5167 | { | |
5168 | record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node); | |
5169 | record_builtin_type (RID_FRACT, NULL, fract_type_node); | |
5170 | record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node); | |
5171 | record_builtin_type (RID_MAX, "long long _Fract", | |
5172 | long_long_fract_type_node); | |
5173 | record_builtin_type (RID_MAX, "unsigned short _Fract", | |
5174 | unsigned_short_fract_type_node); | |
5175 | record_builtin_type (RID_MAX, "unsigned _Fract", | |
5176 | unsigned_fract_type_node); | |
5177 | record_builtin_type (RID_MAX, "unsigned long _Fract", | |
5178 | unsigned_long_fract_type_node); | |
5179 | record_builtin_type (RID_MAX, "unsigned long long _Fract", | |
5180 | unsigned_long_long_fract_type_node); | |
5181 | record_builtin_type (RID_MAX, "_Sat short _Fract", | |
5182 | sat_short_fract_type_node); | |
5183 | record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node); | |
5184 | record_builtin_type (RID_MAX, "_Sat long _Fract", | |
5185 | sat_long_fract_type_node); | |
5186 | record_builtin_type (RID_MAX, "_Sat long long _Fract", | |
5187 | sat_long_long_fract_type_node); | |
5188 | record_builtin_type (RID_MAX, "_Sat unsigned short _Fract", | |
5189 | sat_unsigned_short_fract_type_node); | |
5190 | record_builtin_type (RID_MAX, "_Sat unsigned _Fract", | |
5191 | sat_unsigned_fract_type_node); | |
5192 | record_builtin_type (RID_MAX, "_Sat unsigned long _Fract", | |
5193 | sat_unsigned_long_fract_type_node); | |
5194 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract", | |
5195 | sat_unsigned_long_long_fract_type_node); | |
5196 | record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node); | |
5197 | record_builtin_type (RID_ACCUM, NULL, accum_type_node); | |
5198 | record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node); | |
5199 | record_builtin_type (RID_MAX, "long long _Accum", | |
5200 | long_long_accum_type_node); | |
5201 | record_builtin_type (RID_MAX, "unsigned short _Accum", | |
5202 | unsigned_short_accum_type_node); | |
5203 | record_builtin_type (RID_MAX, "unsigned _Accum", | |
5204 | unsigned_accum_type_node); | |
5205 | record_builtin_type (RID_MAX, "unsigned long _Accum", | |
5206 | unsigned_long_accum_type_node); | |
5207 | record_builtin_type (RID_MAX, "unsigned long long _Accum", | |
5208 | unsigned_long_long_accum_type_node); | |
5209 | record_builtin_type (RID_MAX, "_Sat short _Accum", | |
5210 | sat_short_accum_type_node); | |
5211 | record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node); | |
5212 | record_builtin_type (RID_MAX, "_Sat long _Accum", | |
5213 | sat_long_accum_type_node); | |
5214 | record_builtin_type (RID_MAX, "_Sat long long _Accum", | |
5215 | sat_long_long_accum_type_node); | |
5216 | record_builtin_type (RID_MAX, "_Sat unsigned short _Accum", | |
5217 | sat_unsigned_short_accum_type_node); | |
5218 | record_builtin_type (RID_MAX, "_Sat unsigned _Accum", | |
5219 | sat_unsigned_accum_type_node); | |
5220 | record_builtin_type (RID_MAX, "_Sat unsigned long _Accum", | |
5221 | sat_unsigned_long_accum_type_node); | |
5222 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum", | |
5223 | sat_unsigned_long_long_accum_type_node); | |
5224 | ||
5225 | } | |
5226 | ||
e60a6f7b | 5227 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5228 | TYPE_DECL, | |
dc24ddbd | 5229 | get_identifier ("complex int"), |
5230 | complex_integer_type_node)); | |
e60a6f7b | 5231 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5232 | TYPE_DECL, | |
dc24ddbd | 5233 | get_identifier ("complex float"), |
5234 | complex_float_type_node)); | |
e60a6f7b | 5235 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
5236 | TYPE_DECL, | |
dc24ddbd | 5237 | get_identifier ("complex double"), |
5238 | complex_double_type_node)); | |
5239 | lang_hooks.decls.pushdecl | |
e60a6f7b | 5240 | (build_decl (UNKNOWN_LOCATION, |
5241 | TYPE_DECL, get_identifier ("complex long double"), | |
20325f61 | 5242 | complex_long_double_type_node)); |
174fcc61 | 5243 | |
e256d445 | 5244 | if (c_dialect_cxx ()) |
5245 | /* For C++, make fileptr_type_node a distinct void * type until | |
5246 | FILE type is defined. */ | |
e086912e | 5247 | fileptr_type_node = build_variant_type_copy (ptr_type_node); |
e256d445 | 5248 | |
d946ea19 | 5249 | record_builtin_type (RID_VOID, NULL, void_type_node); |
174fcc61 | 5250 | |
6753bca0 | 5251 | /* Set the TYPE_NAME for any variants that were built before |
5252 | record_builtin_type gave names to the built-in types. */ | |
5253 | { | |
5254 | tree void_name = TYPE_NAME (void_type_node); | |
5255 | TYPE_NAME (void_type_node) = NULL_TREE; | |
5256 | TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST)) | |
5257 | = void_name; | |
5258 | TYPE_NAME (void_type_node) = void_name; | |
5259 | } | |
5260 | ||
fbf0afd1 | 5261 | /* This node must not be shared. */ |
7c446c95 | 5262 | void_zero_node = make_node (INTEGER_CST); |
d2d4bdde | 5263 | TREE_TYPE (void_zero_node) = void_type_node; |
5264 | ||
174fcc61 | 5265 | void_list_node = build_void_list_node (); |
5266 | ||
5267 | /* Make a type to be the domain of a few array types | |
5268 | whose domains don't really matter. | |
5269 | 200 is small enough that it always fits in size_t | |
5270 | and large enough that it can hold most function names for the | |
5271 | initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
5272 | array_domain_type = build_index_type (size_int (200)); | |
5273 | ||
5274 | /* Make a type for arrays of characters. | |
5275 | With luck nothing will ever really depend on the length of this | |
5276 | array type. */ | |
5277 | char_array_type_node | |
5278 | = build_array_type (char_type_node, array_domain_type); | |
5279 | ||
5280 | /* Likewise for arrays of ints. */ | |
5281 | int_array_type_node | |
5282 | = build_array_type (integer_type_node, array_domain_type); | |
5283 | ||
d2d4bdde | 5284 | string_type_node = build_pointer_type (char_type_node); |
5285 | const_string_type_node | |
5286 | = build_pointer_type (build_qualified_type | |
5287 | (char_type_node, TYPE_QUAL_CONST)); | |
5288 | ||
174fcc61 | 5289 | /* This is special for C++ so functions can be overloaded. */ |
18ef7ac2 | 5290 | wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE); |
174fcc61 | 5291 | wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); |
5292 | wchar_type_size = TYPE_PRECISION (wchar_type_node); | |
f3449a3c | 5293 | underlying_wchar_type_node = wchar_type_node; |
c0f19401 | 5294 | if (c_dialect_cxx ()) |
174fcc61 | 5295 | { |
78a8ed03 | 5296 | if (TYPE_UNSIGNED (wchar_type_node)) |
174fcc61 | 5297 | wchar_type_node = make_unsigned_type (wchar_type_size); |
5298 | else | |
5299 | wchar_type_node = make_signed_type (wchar_type_size); | |
5300 | record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); | |
5301 | } | |
174fcc61 | 5302 | |
5303 | /* This is for wide string constants. */ | |
5304 | wchar_array_type_node | |
5305 | = build_array_type (wchar_type_node, array_domain_type); | |
5306 | ||
924bbf02 | 5307 | /* Define 'char16_t'. */ |
5308 | char16_type_node = get_identifier (CHAR16_TYPE); | |
5309 | char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node)); | |
5310 | char16_type_size = TYPE_PRECISION (char16_type_node); | |
5311 | if (c_dialect_cxx ()) | |
5312 | { | |
5313 | char16_type_node = make_unsigned_type (char16_type_size); | |
5314 | ||
543efdbe | 5315 | if (cxx_dialect >= cxx0x) |
924bbf02 | 5316 | record_builtin_type (RID_CHAR16, "char16_t", char16_type_node); |
5317 | } | |
5318 | ||
5319 | /* This is for UTF-16 string constants. */ | |
5320 | char16_array_type_node | |
5321 | = build_array_type (char16_type_node, array_domain_type); | |
5322 | ||
5323 | /* Define 'char32_t'. */ | |
5324 | char32_type_node = get_identifier (CHAR32_TYPE); | |
5325 | char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node)); | |
5326 | char32_type_size = TYPE_PRECISION (char32_type_node); | |
5327 | if (c_dialect_cxx ()) | |
5328 | { | |
5329 | char32_type_node = make_unsigned_type (char32_type_size); | |
5330 | ||
543efdbe | 5331 | if (cxx_dialect >= cxx0x) |
924bbf02 | 5332 | record_builtin_type (RID_CHAR32, "char32_t", char32_type_node); |
5333 | } | |
5334 | ||
5335 | /* This is for UTF-32 string constants. */ | |
5336 | char32_array_type_node | |
5337 | = build_array_type (char32_type_node, array_domain_type); | |
5338 | ||
6bf5ed8d | 5339 | wint_type_node = |
5340 | TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); | |
5341 | ||
5342 | intmax_type_node = | |
5343 | TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); | |
5344 | uintmax_type_node = | |
5345 | TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); | |
5346 | ||
f3449a3c | 5347 | if (SIG_ATOMIC_TYPE) |
5348 | sig_atomic_type_node = | |
9e6687c8 | 5349 | TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE))); |
f3449a3c | 5350 | if (INT8_TYPE) |
5351 | int8_type_node = | |
9e6687c8 | 5352 | TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE))); |
f3449a3c | 5353 | if (INT16_TYPE) |
5354 | int16_type_node = | |
9e6687c8 | 5355 | TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE))); |
f3449a3c | 5356 | if (INT32_TYPE) |
5357 | int32_type_node = | |
9e6687c8 | 5358 | TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE))); |
f3449a3c | 5359 | if (INT64_TYPE) |
5360 | int64_type_node = | |
9e6687c8 | 5361 | TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE))); |
f3449a3c | 5362 | if (UINT8_TYPE) |
5363 | uint8_type_node = | |
9e6687c8 | 5364 | TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE))); |
f3449a3c | 5365 | if (UINT16_TYPE) |
74bdbe96 | 5366 | c_uint16_type_node = |
9e6687c8 | 5367 | TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE))); |
f3449a3c | 5368 | if (UINT32_TYPE) |
5369 | c_uint32_type_node = | |
9e6687c8 | 5370 | TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE))); |
f3449a3c | 5371 | if (UINT64_TYPE) |
5372 | c_uint64_type_node = | |
9e6687c8 | 5373 | TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE))); |
f3449a3c | 5374 | if (INT_LEAST8_TYPE) |
5375 | int_least8_type_node = | |
9e6687c8 | 5376 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE))); |
f3449a3c | 5377 | if (INT_LEAST16_TYPE) |
5378 | int_least16_type_node = | |
9e6687c8 | 5379 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE))); |
f3449a3c | 5380 | if (INT_LEAST32_TYPE) |
5381 | int_least32_type_node = | |
9e6687c8 | 5382 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE))); |
f3449a3c | 5383 | if (INT_LEAST64_TYPE) |
5384 | int_least64_type_node = | |
9e6687c8 | 5385 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE))); |
f3449a3c | 5386 | if (UINT_LEAST8_TYPE) |
5387 | uint_least8_type_node = | |
9e6687c8 | 5388 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE))); |
f3449a3c | 5389 | if (UINT_LEAST16_TYPE) |
5390 | uint_least16_type_node = | |
9e6687c8 | 5391 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE))); |
f3449a3c | 5392 | if (UINT_LEAST32_TYPE) |
5393 | uint_least32_type_node = | |
9e6687c8 | 5394 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE))); |
f3449a3c | 5395 | if (UINT_LEAST64_TYPE) |
5396 | uint_least64_type_node = | |
9e6687c8 | 5397 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE))); |
f3449a3c | 5398 | if (INT_FAST8_TYPE) |
5399 | int_fast8_type_node = | |
9e6687c8 | 5400 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE))); |
f3449a3c | 5401 | if (INT_FAST16_TYPE) |
5402 | int_fast16_type_node = | |
9e6687c8 | 5403 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE))); |
f3449a3c | 5404 | if (INT_FAST32_TYPE) |
5405 | int_fast32_type_node = | |
9e6687c8 | 5406 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE))); |
f3449a3c | 5407 | if (INT_FAST64_TYPE) |
5408 | int_fast64_type_node = | |
9e6687c8 | 5409 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE))); |
f3449a3c | 5410 | if (UINT_FAST8_TYPE) |
5411 | uint_fast8_type_node = | |
9e6687c8 | 5412 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE))); |
f3449a3c | 5413 | if (UINT_FAST16_TYPE) |
5414 | uint_fast16_type_node = | |
9e6687c8 | 5415 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE))); |
f3449a3c | 5416 | if (UINT_FAST32_TYPE) |
5417 | uint_fast32_type_node = | |
9e6687c8 | 5418 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE))); |
f3449a3c | 5419 | if (UINT_FAST64_TYPE) |
5420 | uint_fast64_type_node = | |
9e6687c8 | 5421 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE))); |
f3449a3c | 5422 | if (INTPTR_TYPE) |
5423 | intptr_type_node = | |
9e6687c8 | 5424 | TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE))); |
f3449a3c | 5425 | if (UINTPTR_TYPE) |
5426 | uintptr_type_node = | |
9e6687c8 | 5427 | TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE))); |
f3449a3c | 5428 | |
3a939d12 | 5429 | default_function_type |
5430 | = build_varargs_function_type_list (integer_type_node, NULL_TREE); | |
6bf5ed8d | 5431 | ptrdiff_type_node |
5432 | = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); | |
11773141 | 5433 | unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node); |
6bf5ed8d | 5434 | |
dc24ddbd | 5435 | lang_hooks.decls.pushdecl |
e60a6f7b | 5436 | (build_decl (UNKNOWN_LOCATION, |
5437 | TYPE_DECL, get_identifier ("__builtin_va_list"), | |
20325f61 | 5438 | va_list_type_node)); |
202d6e5f | 5439 | if (targetm.enum_va_list_p) |
acd6f472 | 5440 | { |
5441 | int l; | |
5442 | const char *pname; | |
5443 | tree ptype; | |
5f57a8b1 | 5444 | |
202d6e5f | 5445 | for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l) |
acd6f472 | 5446 | { |
5447 | lang_hooks.decls.pushdecl | |
5448 | (build_decl (UNKNOWN_LOCATION, | |
5449 | TYPE_DECL, get_identifier (pname), | |
5450 | ptype)); | |
5451 | ||
5452 | } | |
5453 | } | |
8a15c04a | 5454 | |
8a15c04a | 5455 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
2d47cc32 | 5456 | { |
5457 | va_list_arg_type_node = va_list_ref_type_node = | |
5458 | build_pointer_type (TREE_TYPE (va_list_type_node)); | |
5459 | } | |
8a15c04a | 5460 | else |
2d47cc32 | 5461 | { |
5462 | va_list_arg_type_node = va_list_type_node; | |
5463 | va_list_ref_type_node = build_reference_type (va_list_type_node); | |
5464 | } | |
1cae46be | 5465 | |
dce22712 | 5466 | if (!flag_preprocess_only) |
5467 | c_define_builtins (va_list_ref_type_node, va_list_arg_type_node); | |
ffa8918b | 5468 | |
5c62f199 | 5469 | main_identifier_node = get_identifier ("main"); |
ae84079f | 5470 | |
5471 | /* Create the built-in __null node. It is important that this is | |
5472 | not shared. */ | |
5473 | null_node = make_node (INTEGER_CST); | |
5474 | TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0); | |
27213ba3 | 5475 | |
5476 | /* Since builtin_types isn't gc'ed, don't export these nodes. */ | |
5477 | memset (builtin_types, 0, sizeof (builtin_types)); | |
72040e7e | 5478 | } |
a66c9326 | 5479 | |
79b01846 | 5480 | /* The number of named compound-literals generated thus far. */ |
5481 | static GTY(()) int compound_literal_number; | |
5482 | ||
5483 | /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */ | |
5484 | ||
5485 | void | |
5486 | set_compound_literal_name (tree decl) | |
5487 | { | |
5488 | char *name; | |
5489 | ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", | |
5490 | compound_literal_number); | |
5491 | compound_literal_number++; | |
5492 | DECL_NAME (decl) = get_identifier (name); | |
5493 | } | |
5494 | ||
a66c9326 | 5495 | tree |
e60a6f7b | 5496 | build_va_arg (location_t loc, tree expr, tree type) |
a66c9326 | 5497 | { |
e60a6f7b | 5498 | expr = build1 (VA_ARG_EXPR, type, expr); |
5499 | SET_EXPR_LOCATION (expr, loc); | |
5500 | return expr; | |
a66c9326 | 5501 | } |
0d4238dc | 5502 | |
5503 | ||
dd878098 | 5504 | /* Linked list of disabled built-in functions. */ |
5505 | ||
5506 | typedef struct disabled_builtin | |
5507 | { | |
5508 | const char *name; | |
5509 | struct disabled_builtin *next; | |
5510 | } disabled_builtin; | |
5511 | static disabled_builtin *disabled_builtins = NULL; | |
5512 | ||
1cae46be | 5513 | static bool builtin_function_disabled_p (const char *); |
dd878098 | 5514 | |
5515 | /* Disable a built-in function specified by -fno-builtin-NAME. If NAME | |
5516 | begins with "__builtin_", give an error. */ | |
5517 | ||
5518 | void | |
1cae46be | 5519 | disable_builtin_function (const char *name) |
dd878098 | 5520 | { |
5521 | if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0) | |
b0b1af64 | 5522 | error ("cannot disable built-in function %qs", name); |
dd878098 | 5523 | else |
5524 | { | |
e85905e5 | 5525 | disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin); |
5526 | new_disabled_builtin->name = name; | |
5527 | new_disabled_builtin->next = disabled_builtins; | |
5528 | disabled_builtins = new_disabled_builtin; | |
dd878098 | 5529 | } |
5530 | } | |
5531 | ||
5532 | ||
5533 | /* Return true if the built-in function NAME has been disabled, false | |
5534 | otherwise. */ | |
5535 | ||
5536 | static bool | |
1cae46be | 5537 | builtin_function_disabled_p (const char *name) |
dd878098 | 5538 | { |
5539 | disabled_builtin *p; | |
5540 | for (p = disabled_builtins; p != NULL; p = p->next) | |
5541 | { | |
5542 | if (strcmp (name, p->name) == 0) | |
5543 | return true; | |
5544 | } | |
5545 | return false; | |
5546 | } | |
5547 | ||
5548 | ||
3237155d | 5549 | /* Worker for DEF_BUILTIN. |
5550 | Possibly define a builtin function with one or two names. | |
5551 | Does not declare a non-__builtin_ function if flag_no_builtin, or if | |
5552 | nonansi_p and flag_no_nonansi_builtin. */ | |
0d4238dc | 5553 | |
3237155d | 5554 | static void |
5555 | def_builtin_1 (enum built_in_function fncode, | |
5556 | const char *name, | |
5557 | enum built_in_class fnclass, | |
5558 | tree fntype, tree libtype, | |
5559 | bool both_p, bool fallback_p, bool nonansi_p, | |
5560 | tree fnattrs, bool implicit_p) | |
0d4238dc | 5561 | { |
3237155d | 5562 | tree decl; |
5563 | const char *libname; | |
5564 | ||
27213ba3 | 5565 | if (fntype == error_mark_node) |
5566 | return; | |
5567 | ||
3237155d | 5568 | gcc_assert ((!both_p && !fallback_p) |
5569 | || !strncmp (name, "__builtin_", | |
5570 | strlen ("__builtin_"))); | |
5571 | ||
5572 | libname = name + strlen ("__builtin_"); | |
54be5d7e | 5573 | decl = add_builtin_function (name, fntype, fncode, fnclass, |
5574 | (fallback_p ? libname : NULL), | |
5575 | fnattrs); | |
b9a16870 | 5576 | |
5577 | set_builtin_decl (fncode, decl, implicit_p); | |
5578 | ||
3237155d | 5579 | if (both_p |
5580 | && !flag_no_builtin && !builtin_function_disabled_p (libname) | |
dd878098 | 5581 | && !(nonansi_p && flag_no_nonansi_builtin)) |
54be5d7e | 5582 | add_builtin_function (libname, libtype, fncode, fnclass, |
5583 | NULL, fnattrs); | |
0d4238dc | 5584 | } |
e94026da | 5585 | \f |
d7aeef06 | 5586 | /* Nonzero if the type T promotes to int. This is (nearly) the |
5587 | integral promotions defined in ISO C99 6.3.1.1/2. */ | |
5588 | ||
5589 | bool | |
9f627b1a | 5590 | c_promoting_integer_type_p (const_tree t) |
d7aeef06 | 5591 | { |
5592 | switch (TREE_CODE (t)) | |
5593 | { | |
5594 | case INTEGER_TYPE: | |
5595 | return (TYPE_MAIN_VARIANT (t) == char_type_node | |
5596 | || TYPE_MAIN_VARIANT (t) == signed_char_type_node | |
5597 | || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node | |
5598 | || TYPE_MAIN_VARIANT (t) == short_integer_type_node | |
7aa1e6eb | 5599 | || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node |
5600 | || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node)); | |
d7aeef06 | 5601 | |
5602 | case ENUMERAL_TYPE: | |
5603 | /* ??? Technically all enumerations not larger than an int | |
5604 | promote to an int. But this is used along code paths | |
5605 | that only want to notice a size change. */ | |
5606 | return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node); | |
5607 | ||
5608 | case BOOLEAN_TYPE: | |
5609 | return 1; | |
5610 | ||
5611 | default: | |
5612 | return 0; | |
5613 | } | |
5614 | } | |
5615 | ||
e94026da | 5616 | /* Return 1 if PARMS specifies a fixed number of parameters |
5617 | and none of their types is affected by default promotions. */ | |
5618 | ||
5619 | int | |
9f627b1a | 5620 | self_promoting_args_p (const_tree parms) |
e94026da | 5621 | { |
9f627b1a | 5622 | const_tree t; |
e94026da | 5623 | for (t = parms; t; t = TREE_CHAIN (t)) |
5624 | { | |
19cb6b50 | 5625 | tree type = TREE_VALUE (t); |
43f74bc4 | 5626 | |
e1d8e198 | 5627 | if (type == error_mark_node) |
5628 | continue; | |
5629 | ||
e94026da | 5630 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
5631 | return 0; | |
5632 | ||
5633 | if (type == 0) | |
5634 | return 0; | |
5635 | ||
5636 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
5637 | return 0; | |
5638 | ||
d7aeef06 | 5639 | if (c_promoting_integer_type_p (type)) |
e94026da | 5640 | return 0; |
5641 | } | |
5642 | return 1; | |
5643 | } | |
605fb01e | 5644 | |
c10de5e7 | 5645 | /* Recursively remove any '*' or '&' operator from TYPE. */ |
5646 | tree | |
5647 | strip_pointer_operator (tree t) | |
5648 | { | |
5649 | while (POINTER_TYPE_P (t)) | |
5650 | t = TREE_TYPE (t); | |
5651 | return t; | |
5652 | } | |
5653 | ||
57a0ed23 | 5654 | /* Recursively remove pointer or array type from TYPE. */ |
5655 | tree | |
5656 | strip_pointer_or_array_types (tree t) | |
5657 | { | |
5658 | while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t)) | |
5659 | t = TREE_TYPE (t); | |
5660 | return t; | |
5661 | } | |
5662 | ||
e41f0d80 | 5663 | /* Used to compare case labels. K1 and K2 are actually tree nodes |
5664 | representing case labels, or NULL_TREE for a `default' label. | |
5665 | Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after | |
5666 | K2, and 0 if K1 and K2 are equal. */ | |
5667 | ||
5668 | int | |
1cae46be | 5669 | case_compare (splay_tree_key k1, splay_tree_key k2) |
e41f0d80 | 5670 | { |
5671 | /* Consider a NULL key (such as arises with a `default' label) to be | |
5672 | smaller than anything else. */ | |
5673 | if (!k1) | |
5674 | return k2 ? -1 : 0; | |
5675 | else if (!k2) | |
5676 | return k1 ? 1 : 0; | |
5677 | ||
5678 | return tree_int_cst_compare ((tree) k1, (tree) k2); | |
5679 | } | |
5680 | ||
e60a6f7b | 5681 | /* Process a case label, located at LOC, for the range LOW_VALUE |
5682 | ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE | |
5683 | then this case label is actually a `default' label. If only | |
5684 | HIGH_VALUE is NULL_TREE, then case label was declared using the | |
5685 | usual C/C++ syntax, rather than the GNU case range extension. | |
5686 | CASES is a tree containing all the case ranges processed so far; | |
5687 | COND is the condition for the switch-statement itself. Returns the | |
5688 | CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR | |
5689 | is created. */ | |
e41f0d80 | 5690 | |
5691 | tree | |
e60a6f7b | 5692 | c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type, |
2ca392fd | 5693 | tree low_value, tree high_value) |
e41f0d80 | 5694 | { |
5695 | tree type; | |
5696 | tree label; | |
5697 | tree case_label; | |
5698 | splay_tree_node node; | |
5699 | ||
5700 | /* Create the LABEL_DECL itself. */ | |
e60a6f7b | 5701 | label = create_artificial_label (loc); |
e41f0d80 | 5702 | |
5703 | /* If there was an error processing the switch condition, bail now | |
5704 | before we get more confused. */ | |
5705 | if (!cond || cond == error_mark_node) | |
4ee9c684 | 5706 | goto error_out; |
e41f0d80 | 5707 | |
1cae46be | 5708 | if ((low_value && TREE_TYPE (low_value) |
5709 | && POINTER_TYPE_P (TREE_TYPE (low_value))) | |
e41f0d80 | 5710 | || (high_value && TREE_TYPE (high_value) |
5711 | && POINTER_TYPE_P (TREE_TYPE (high_value)))) | |
b96dc121 | 5712 | { |
e60a6f7b | 5713 | error_at (loc, "pointers are not permitted as case values"); |
b96dc121 | 5714 | goto error_out; |
5715 | } | |
e41f0d80 | 5716 | |
5717 | /* Case ranges are a GNU extension. */ | |
8864917d | 5718 | if (high_value) |
29438999 | 5719 | pedwarn (loc, OPT_Wpedantic, |
8864917d | 5720 | "range expressions in switch statements are non-standard"); |
e41f0d80 | 5721 | |
5722 | type = TREE_TYPE (cond); | |
5723 | if (low_value) | |
5724 | { | |
5725 | low_value = check_case_value (low_value); | |
5726 | low_value = convert_and_check (type, low_value); | |
96722196 | 5727 | if (low_value == error_mark_node) |
5728 | goto error_out; | |
e41f0d80 | 5729 | } |
5730 | if (high_value) | |
5731 | { | |
5732 | high_value = check_case_value (high_value); | |
5733 | high_value = convert_and_check (type, high_value); | |
96722196 | 5734 | if (high_value == error_mark_node) |
5735 | goto error_out; | |
e41f0d80 | 5736 | } |
5737 | ||
96722196 | 5738 | if (low_value && high_value) |
5739 | { | |
5740 | /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't | |
a0c938f0 | 5741 | really a case range, even though it was written that way. |
5742 | Remove the HIGH_VALUE to simplify later processing. */ | |
96722196 | 5743 | if (tree_int_cst_equal (low_value, high_value)) |
5744 | high_value = NULL_TREE; | |
5745 | else if (!tree_int_cst_lt (low_value, high_value)) | |
e60a6f7b | 5746 | warning_at (loc, 0, "empty range specified"); |
96722196 | 5747 | } |
e41f0d80 | 5748 | |
2ca392fd | 5749 | /* See if the case is in range of the type of the original testing |
5750 | expression. If both low_value and high_value are out of range, | |
5751 | don't insert the case label and return NULL_TREE. */ | |
5752 | if (low_value | |
84166705 | 5753 | && !check_case_bounds (type, orig_type, |
5754 | &low_value, high_value ? &high_value : NULL)) | |
2ca392fd | 5755 | return NULL_TREE; |
5756 | ||
e41f0d80 | 5757 | /* Look up the LOW_VALUE in the table of case labels we already |
5758 | have. */ | |
5759 | node = splay_tree_lookup (cases, (splay_tree_key) low_value); | |
5760 | /* If there was not an exact match, check for overlapping ranges. | |
5761 | There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; | |
5762 | that's a `default' label and the only overlap is an exact match. */ | |
5763 | if (!node && (low_value || high_value)) | |
5764 | { | |
5765 | splay_tree_node low_bound; | |
5766 | splay_tree_node high_bound; | |
5767 | ||
5768 | /* Even though there wasn't an exact match, there might be an | |
5769 | overlap between this case range and another case range. | |
5770 | Since we've (inductively) not allowed any overlapping case | |
5771 | ranges, we simply need to find the greatest low case label | |
5772 | that is smaller that LOW_VALUE, and the smallest low case | |
5773 | label that is greater than LOW_VALUE. If there is an overlap | |
5774 | it will occur in one of these two ranges. */ | |
5775 | low_bound = splay_tree_predecessor (cases, | |
5776 | (splay_tree_key) low_value); | |
5777 | high_bound = splay_tree_successor (cases, | |
5778 | (splay_tree_key) low_value); | |
5779 | ||
5780 | /* Check to see if the LOW_BOUND overlaps. It is smaller than | |
5781 | the LOW_VALUE, so there is no need to check unless the | |
5782 | LOW_BOUND is in fact itself a case range. */ | |
5783 | if (low_bound | |
5784 | && CASE_HIGH ((tree) low_bound->value) | |
5785 | && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), | |
5786 | low_value) >= 0) | |
5787 | node = low_bound; | |
5788 | /* Check to see if the HIGH_BOUND overlaps. The low end of that | |
5789 | range is bigger than the low end of the current range, so we | |
5790 | are only interested if the current range is a real range, and | |
5791 | not an ordinary case label. */ | |
1cae46be | 5792 | else if (high_bound |
e41f0d80 | 5793 | && high_value |
5794 | && (tree_int_cst_compare ((tree) high_bound->key, | |
5795 | high_value) | |
5796 | <= 0)) | |
5797 | node = high_bound; | |
5798 | } | |
5799 | /* If there was an overlap, issue an error. */ | |
5800 | if (node) | |
5801 | { | |
eaae3b75 | 5802 | tree duplicate = CASE_LABEL ((tree) node->value); |
e41f0d80 | 5803 | |
5804 | if (high_value) | |
5805 | { | |
e60a6f7b | 5806 | error_at (loc, "duplicate (or overlapping) case value"); |
5807 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
5808 | "this is the first entry overlapping that value"); | |
e41f0d80 | 5809 | } |
5810 | else if (low_value) | |
5811 | { | |
e60a6f7b | 5812 | error_at (loc, "duplicate case value") ; |
5813 | error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here"); | |
e41f0d80 | 5814 | } |
5815 | else | |
5816 | { | |
e60a6f7b | 5817 | error_at (loc, "multiple default labels in one switch"); |
5818 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
5819 | "this is the first default label"); | |
e41f0d80 | 5820 | } |
4ee9c684 | 5821 | goto error_out; |
e41f0d80 | 5822 | } |
5823 | ||
5824 | /* Add a CASE_LABEL to the statement-tree. */ | |
b6e3dd65 | 5825 | case_label = add_stmt (build_case_label (low_value, high_value, label)); |
e41f0d80 | 5826 | /* Register this case label in the splay tree. */ |
1cae46be | 5827 | splay_tree_insert (cases, |
e41f0d80 | 5828 | (splay_tree_key) low_value, |
5829 | (splay_tree_value) case_label); | |
5830 | ||
5831 | return case_label; | |
4ee9c684 | 5832 | |
5833 | error_out: | |
daf6dff5 | 5834 | /* Add a label so that the back-end doesn't think that the beginning of |
4ee9c684 | 5835 | the switch is unreachable. Note that we do not add a case label, as |
a53ff4c1 | 5836 | that just leads to duplicates and thence to failure later on. */ |
4ee9c684 | 5837 | if (!cases->root) |
5838 | { | |
e60a6f7b | 5839 | tree t = create_artificial_label (loc); |
5840 | add_stmt (build_stmt (loc, LABEL_EXPR, t)); | |
4ee9c684 | 5841 | } |
5842 | return error_mark_node; | |
5843 | } | |
5844 | ||
5845 | /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. | |
5846 | Used to verify that case values match up with enumerator values. */ | |
5847 | ||
5848 | static void | |
5849 | match_case_to_enum_1 (tree key, tree type, tree label) | |
5850 | { | |
5851 | char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1]; | |
5852 | ||
5853 | /* ??? Not working too hard to print the double-word value. | |
5854 | Should perhaps be done with %lwd in the diagnostic routines? */ | |
5855 | if (TREE_INT_CST_HIGH (key) == 0) | |
5856 | snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED, | |
5857 | TREE_INT_CST_LOW (key)); | |
5858 | else if (!TYPE_UNSIGNED (type) | |
5859 | && TREE_INT_CST_HIGH (key) == -1 | |
5860 | && TREE_INT_CST_LOW (key) != 0) | |
5861 | snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED, | |
5862 | -TREE_INT_CST_LOW (key)); | |
5863 | else | |
5864 | snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX, | |
7df7561b | 5865 | (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key), |
5866 | (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key)); | |
4ee9c684 | 5867 | |
5868 | if (TYPE_NAME (type) == 0) | |
712d2297 | 5869 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
5870 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
5871 | "case value %qs not in enumerated type", | |
5872 | buf); | |
4ee9c684 | 5873 | else |
712d2297 | 5874 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
5875 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
5876 | "case value %qs not in enumerated type %qT", | |
5877 | buf, type); | |
4ee9c684 | 5878 | } |
5879 | ||
359d87c6 | 5880 | /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. |
5881 | Used to verify that case values match up with enumerator values. */ | |
5882 | ||
4ee9c684 | 5883 | static int |
5884 | match_case_to_enum (splay_tree_node node, void *data) | |
5885 | { | |
5886 | tree label = (tree) node->value; | |
4fd61bc6 | 5887 | tree type = (tree) data; |
4ee9c684 | 5888 | |
5889 | /* Skip default case. */ | |
5890 | if (!CASE_LOW (label)) | |
5891 | return 0; | |
5892 | ||
359d87c6 | 5893 | /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear |
4ee9c684 | 5894 | when we did our enum->case scan. Reset our scratch bit after. */ |
359d87c6 | 5895 | if (!CASE_LOW_SEEN (label)) |
4ee9c684 | 5896 | match_case_to_enum_1 (CASE_LOW (label), type, label); |
5897 | else | |
359d87c6 | 5898 | CASE_LOW_SEEN (label) = 0; |
4ee9c684 | 5899 | |
359d87c6 | 5900 | /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is |
5901 | not set, that means that CASE_HIGH did not appear when we did our | |
5902 | enum->case scan. Reset our scratch bit after. */ | |
4ee9c684 | 5903 | if (CASE_HIGH (label)) |
5904 | { | |
359d87c6 | 5905 | if (!CASE_HIGH_SEEN (label)) |
5906 | match_case_to_enum_1 (CASE_HIGH (label), type, label); | |
5907 | else | |
5908 | CASE_HIGH_SEEN (label) = 0; | |
4ee9c684 | 5909 | } |
5910 | ||
5911 | return 0; | |
5912 | } | |
5913 | ||
e7911019 | 5914 | /* Handle -Wswitch*. Called from the front end after parsing the |
5915 | switch construct. */ | |
5916 | /* ??? Should probably be somewhere generic, since other languages | |
5917 | besides C and C++ would want this. At the moment, however, C/C++ | |
5918 | are the only tree-ssa languages that support enumerations at all, | |
5919 | so the point is moot. */ | |
4ee9c684 | 5920 | |
e7911019 | 5921 | void |
5922 | c_do_switch_warnings (splay_tree cases, location_t switch_location, | |
5923 | tree type, tree cond) | |
4ee9c684 | 5924 | { |
b27ac6b5 | 5925 | splay_tree_node default_node; |
359d87c6 | 5926 | splay_tree_node node; |
5927 | tree chain; | |
4ee9c684 | 5928 | |
5929 | if (!warn_switch && !warn_switch_enum && !warn_switch_default) | |
5930 | return; | |
5931 | ||
4ee9c684 | 5932 | default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); |
8b6866af | 5933 | if (!default_node) |
5fb6a912 | 5934 | warning_at (switch_location, OPT_Wswitch_default, |
5935 | "switch missing default case"); | |
4ee9c684 | 5936 | |
359d87c6 | 5937 | /* From here on, we only care about about enumerated types. */ |
5938 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
5939 | return; | |
5940 | ||
561017b5 | 5941 | /* From here on, we only care about -Wswitch and -Wswitch-enum. */ |
5942 | if (!warn_switch_enum && !warn_switch) | |
359d87c6 | 5943 | return; |
5944 | ||
561017b5 | 5945 | /* Check the cases. Warn about case values which are not members of |
5946 | the enumerated type. For -Wswitch-enum, or for -Wswitch when | |
5947 | there is no default case, check that exactly all enumeration | |
5948 | literals are covered by the cases. */ | |
5949 | ||
359d87c6 | 5950 | /* Clearing COND if it is not an integer constant simplifies |
5951 | the tests inside the loop below. */ | |
5952 | if (TREE_CODE (cond) != INTEGER_CST) | |
5953 | cond = NULL_TREE; | |
5954 | ||
5955 | /* The time complexity here is O(N*lg(N)) worst case, but for the | |
5956 | common case of monotonically increasing enumerators, it is | |
5957 | O(N), since the nature of the splay tree will keep the next | |
5958 | element adjacent to the root at all times. */ | |
4ee9c684 | 5959 | |
359d87c6 | 5960 | for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) |
5961 | { | |
5962 | tree value = TREE_VALUE (chain); | |
3f00a6c0 | 5963 | if (TREE_CODE (value) == CONST_DECL) |
5964 | value = DECL_INITIAL (value); | |
359d87c6 | 5965 | node = splay_tree_lookup (cases, (splay_tree_key) value); |
5966 | if (node) | |
4ee9c684 | 5967 | { |
359d87c6 | 5968 | /* Mark the CASE_LOW part of the case entry as seen. */ |
5969 | tree label = (tree) node->value; | |
5970 | CASE_LOW_SEEN (label) = 1; | |
5971 | continue; | |
5972 | } | |
5973 | ||
5974 | /* Even though there wasn't an exact match, there might be a | |
f0b5f617 | 5975 | case range which includes the enumerator's value. */ |
359d87c6 | 5976 | node = splay_tree_predecessor (cases, (splay_tree_key) value); |
5977 | if (node && CASE_HIGH ((tree) node->value)) | |
5978 | { | |
5979 | tree label = (tree) node->value; | |
5980 | int cmp = tree_int_cst_compare (CASE_HIGH (label), value); | |
5981 | if (cmp >= 0) | |
4ee9c684 | 5982 | { |
359d87c6 | 5983 | /* If we match the upper bound exactly, mark the CASE_HIGH |
5984 | part of the case entry as seen. */ | |
5985 | if (cmp == 0) | |
5986 | CASE_HIGH_SEEN (label) = 1; | |
5987 | continue; | |
4ee9c684 | 5988 | } |
5989 | } | |
5990 | ||
359d87c6 | 5991 | /* We've now determined that this enumerated literal isn't |
5992 | handled by the case labels of the switch statement. */ | |
4ee9c684 | 5993 | |
359d87c6 | 5994 | /* If the switch expression is a constant, we only really care |
5995 | about whether that constant is handled by the switch. */ | |
5996 | if (cond && tree_int_cst_compare (cond, value)) | |
5997 | continue; | |
4ee9c684 | 5998 | |
6cbbbc89 | 5999 | /* If there is a default_node, the only relevant option is |
561017b5 | 6000 | Wswitch-enum. Otherwise, if both are enabled then we prefer |
6cbbbc89 | 6001 | to warn using -Wswitch because -Wswitch is enabled by -Wall |
6002 | while -Wswitch-enum is explicit. */ | |
561017b5 | 6003 | warning_at (switch_location, |
6004 | (default_node || !warn_switch | |
6005 | ? OPT_Wswitch_enum | |
6006 | : OPT_Wswitch), | |
6007 | "enumeration value %qE not handled in switch", | |
6008 | TREE_PURPOSE (chain)); | |
4ee9c684 | 6009 | } |
359d87c6 | 6010 | |
6011 | /* Warn if there are case expressions that don't correspond to | |
6012 | enumerators. This can occur since C and C++ don't enforce | |
6013 | type-checking of assignments to enumeration variables. | |
6014 | ||
6015 | The time complexity here is now always O(N) worst case, since | |
6016 | we should have marked both the lower bound and upper bound of | |
6017 | every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN | |
6018 | above. This scan also resets those fields. */ | |
6cbbbc89 | 6019 | |
359d87c6 | 6020 | splay_tree_foreach (cases, match_case_to_enum, type); |
e41f0d80 | 6021 | } |
6022 | ||
9dd48740 | 6023 | /* Finish an expression taking the address of LABEL (an |
dda49785 | 6024 | IDENTIFIER_NODE). Returns an expression for the address. |
6025 | ||
6026 | LOC is the location for the expression returned. */ | |
d0a47c8d | 6027 | |
1cae46be | 6028 | tree |
dda49785 | 6029 | finish_label_address_expr (tree label, location_t loc) |
d0a47c8d | 6030 | { |
6031 | tree result; | |
6032 | ||
29438999 | 6033 | pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard"); |
d0a47c8d | 6034 | |
9dd48740 | 6035 | if (label == error_mark_node) |
6036 | return error_mark_node; | |
6037 | ||
d0a47c8d | 6038 | label = lookup_label (label); |
6039 | if (label == NULL_TREE) | |
6040 | result = null_pointer_node; | |
6041 | else | |
6042 | { | |
6043 | TREE_USED (label) = 1; | |
6044 | result = build1 (ADDR_EXPR, ptr_type_node, label); | |
344cd9b2 | 6045 | /* The current function is not necessarily uninlinable. |
d0a47c8d | 6046 | Computed gotos are incompatible with inlining, but the value |
6047 | here could be used only in a diagnostic, for example. */ | |
dda49785 | 6048 | protected_set_expr_location (result, loc); |
d0a47c8d | 6049 | } |
6050 | ||
6051 | return result; | |
6052 | } | |
4f9a1c9b | 6053 | \f |
6054 | ||
6055 | /* Given a boolean expression ARG, return a tree representing an increment | |
6056 | or decrement (as indicated by CODE) of ARG. The front end must check for | |
6057 | invalid cases (e.g., decrement in C++). */ | |
6058 | tree | |
1cae46be | 6059 | boolean_increment (enum tree_code code, tree arg) |
4f9a1c9b | 6060 | { |
6061 | tree val; | |
69db191c | 6062 | tree true_res = build_int_cst (TREE_TYPE (arg), 1); |
c0f19401 | 6063 | |
4f9a1c9b | 6064 | arg = stabilize_reference (arg); |
6065 | switch (code) | |
6066 | { | |
6067 | case PREINCREMENT_EXPR: | |
14ae0310 | 6068 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 6069 | break; |
6070 | case POSTINCREMENT_EXPR: | |
14ae0310 | 6071 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 6072 | arg = save_expr (arg); |
14ae0310 | 6073 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
6074 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 6075 | break; |
6076 | case PREDECREMENT_EXPR: | |
14ae0310 | 6077 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
389dd41b | 6078 | invert_truthvalue_loc (input_location, arg)); |
4f9a1c9b | 6079 | break; |
6080 | case POSTDECREMENT_EXPR: | |
14ae0310 | 6081 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
389dd41b | 6082 | invert_truthvalue_loc (input_location, arg)); |
4f9a1c9b | 6083 | arg = save_expr (arg); |
14ae0310 | 6084 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
6085 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 6086 | break; |
6087 | default: | |
231bd014 | 6088 | gcc_unreachable (); |
4f9a1c9b | 6089 | } |
6090 | TREE_SIDE_EFFECTS (val) = 1; | |
6091 | return val; | |
6092 | } | |
76a6e674 | 6093 | \f |
f3449a3c | 6094 | /* Built-in macros for stddef.h and stdint.h, that require macros |
6095 | defined in this file. */ | |
79cf3ec1 | 6096 | void |
1cae46be | 6097 | c_stddef_cpp_builtins(void) |
1ed9d5f5 | 6098 | { |
63994318 | 6099 | builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0); |
6100 | builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0); | |
6101 | builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0); | |
6102 | builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0); | |
36bccbfc | 6103 | builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0); |
6104 | builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0); | |
b0726616 | 6105 | builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0); |
6106 | builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0); | |
f3449a3c | 6107 | if (SIG_ATOMIC_TYPE) |
6108 | builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0); | |
6109 | if (INT8_TYPE) | |
6110 | builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0); | |
6111 | if (INT16_TYPE) | |
6112 | builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0); | |
6113 | if (INT32_TYPE) | |
6114 | builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0); | |
6115 | if (INT64_TYPE) | |
6116 | builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0); | |
6117 | if (UINT8_TYPE) | |
6118 | builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0); | |
6119 | if (UINT16_TYPE) | |
6120 | builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0); | |
6121 | if (UINT32_TYPE) | |
6122 | builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0); | |
6123 | if (UINT64_TYPE) | |
6124 | builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0); | |
6125 | if (INT_LEAST8_TYPE) | |
6126 | builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0); | |
6127 | if (INT_LEAST16_TYPE) | |
6128 | builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0); | |
6129 | if (INT_LEAST32_TYPE) | |
6130 | builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0); | |
6131 | if (INT_LEAST64_TYPE) | |
6132 | builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0); | |
6133 | if (UINT_LEAST8_TYPE) | |
6134 | builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0); | |
6135 | if (UINT_LEAST16_TYPE) | |
6136 | builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0); | |
6137 | if (UINT_LEAST32_TYPE) | |
6138 | builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0); | |
6139 | if (UINT_LEAST64_TYPE) | |
6140 | builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0); | |
6141 | if (INT_FAST8_TYPE) | |
6142 | builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0); | |
6143 | if (INT_FAST16_TYPE) | |
6144 | builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0); | |
6145 | if (INT_FAST32_TYPE) | |
6146 | builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0); | |
6147 | if (INT_FAST64_TYPE) | |
6148 | builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0); | |
6149 | if (UINT_FAST8_TYPE) | |
6150 | builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0); | |
6151 | if (UINT_FAST16_TYPE) | |
6152 | builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0); | |
6153 | if (UINT_FAST32_TYPE) | |
6154 | builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0); | |
6155 | if (UINT_FAST64_TYPE) | |
6156 | builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0); | |
6157 | if (INTPTR_TYPE) | |
6158 | builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0); | |
6159 | if (UINTPTR_TYPE) | |
6160 | builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0); | |
574006c3 | 6161 | } |
6162 | ||
7d3b509a | 6163 | static void |
1cae46be | 6164 | c_init_attributes (void) |
7d3b509a | 6165 | { |
6166 | /* Fill in the built_in_attributes array. */ | |
7c446c95 | 6167 | #define DEF_ATTR_NULL_TREE(ENUM) \ |
7d3b509a | 6168 | built_in_attributes[(int) ENUM] = NULL_TREE; |
7c446c95 | 6169 | #define DEF_ATTR_INT(ENUM, VALUE) \ |
ceb7b692 | 6170 | built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE); |
c8010b80 | 6171 | #define DEF_ATTR_STRING(ENUM, VALUE) \ |
6172 | built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE); | |
7d3b509a | 6173 | #define DEF_ATTR_IDENT(ENUM, STRING) \ |
6174 | built_in_attributes[(int) ENUM] = get_identifier (STRING); | |
6175 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \ | |
6176 | built_in_attributes[(int) ENUM] \ | |
6177 | = tree_cons (built_in_attributes[(int) PURPOSE], \ | |
6178 | built_in_attributes[(int) VALUE], \ | |
6179 | built_in_attributes[(int) CHAIN]); | |
7d3b509a | 6180 | #include "builtin-attrs.def" |
6181 | #undef DEF_ATTR_NULL_TREE | |
6182 | #undef DEF_ATTR_INT | |
6183 | #undef DEF_ATTR_IDENT | |
6184 | #undef DEF_ATTR_TREE_LIST | |
76a6e674 | 6185 | } |
5f3cead1 | 6186 | |
33199a81 | 6187 | /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain |
6188 | identifier as an argument, so the front end shouldn't look it up. */ | |
6189 | ||
6190 | bool | |
47b19b94 | 6191 | attribute_takes_identifier_p (const_tree attr_id) |
33199a81 | 6192 | { |
9bf1c74e | 6193 | const struct attribute_spec *spec = lookup_attribute_spec (attr_id); |
02cb1060 | 6194 | if (spec == NULL) |
6195 | /* Unknown attribute that we'll end up ignoring, return true so we | |
6196 | don't complain about an identifier argument. */ | |
6197 | return true; | |
6198 | else if (!strcmp ("mode", spec->name) | |
6199 | || !strcmp ("format", spec->name) | |
6200 | || !strcmp ("cleanup", spec->name)) | |
47b19b94 | 6201 | return true; |
6202 | else | |
6203 | return targetm.attribute_takes_identifier_p (attr_id); | |
33199a81 | 6204 | } |
6205 | ||
f8e93a2e | 6206 | /* Attribute handlers common to C front ends. */ |
6207 | ||
6208 | /* Handle a "packed" attribute; arguments as in | |
6209 | struct attribute_spec.handler. */ | |
6210 | ||
6211 | static tree | |
9a03a746 | 6212 | handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
09347743 | 6213 | int flags, bool *no_add_attrs) |
f8e93a2e | 6214 | { |
f40175cb | 6215 | if (TYPE_P (*node)) |
f8e93a2e | 6216 | { |
6217 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 6218 | *node = build_variant_type_copy (*node); |
f40175cb | 6219 | TYPE_PACKED (*node) = 1; |
f8e93a2e | 6220 | } |
6221 | else if (TREE_CODE (*node) == FIELD_DECL) | |
c2ab04f9 | 6222 | { |
9fd767c5 | 6223 | if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT |
6224 | /* Still pack bitfields. */ | |
6225 | && ! DECL_INITIAL (*node)) | |
c2ab04f9 | 6226 | warning (OPT_Wattributes, |
6227 | "%qE attribute ignored for field of type %qT", | |
6228 | name, TREE_TYPE (*node)); | |
6229 | else | |
6230 | DECL_PACKED (*node) = 1; | |
6231 | } | |
f8e93a2e | 6232 | /* We can't set DECL_PACKED for a VAR_DECL, because the bit is |
f40175cb | 6233 | used for DECL_REGISTER. It wouldn't mean anything anyway. |
6234 | We can't set DECL_PACKED on the type of a TYPE_DECL, because | |
6235 | that changes what the typedef is typing. */ | |
f8e93a2e | 6236 | else |
6237 | { | |
9b2d6d13 | 6238 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6239 | *no_add_attrs = true; |
6240 | } | |
6241 | ||
6242 | return NULL_TREE; | |
6243 | } | |
6244 | ||
6245 | /* Handle a "nocommon" attribute; arguments as in | |
6246 | struct attribute_spec.handler. */ | |
6247 | ||
6248 | static tree | |
1cae46be | 6249 | handle_nocommon_attribute (tree *node, tree name, |
9a03a746 | 6250 | tree ARG_UNUSED (args), |
6251 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6252 | { |
6253 | if (TREE_CODE (*node) == VAR_DECL) | |
6254 | DECL_COMMON (*node) = 0; | |
6255 | else | |
6256 | { | |
9b2d6d13 | 6257 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6258 | *no_add_attrs = true; |
6259 | } | |
6260 | ||
6261 | return NULL_TREE; | |
6262 | } | |
6263 | ||
6264 | /* Handle a "common" attribute; arguments as in | |
6265 | struct attribute_spec.handler. */ | |
6266 | ||
6267 | static tree | |
9a03a746 | 6268 | handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6269 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6270 | { |
6271 | if (TREE_CODE (*node) == VAR_DECL) | |
6272 | DECL_COMMON (*node) = 1; | |
6273 | else | |
6274 | { | |
9b2d6d13 | 6275 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6276 | *no_add_attrs = true; |
6277 | } | |
6278 | ||
6279 | return NULL_TREE; | |
6280 | } | |
6281 | ||
6282 | /* Handle a "noreturn" attribute; arguments as in | |
6283 | struct attribute_spec.handler. */ | |
6284 | ||
6285 | static tree | |
9a03a746 | 6286 | handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6287 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6288 | { |
6289 | tree type = TREE_TYPE (*node); | |
6290 | ||
6291 | /* See FIXME comment in c_common_attribute_table. */ | |
8c582e4f | 6292 | if (TREE_CODE (*node) == FUNCTION_DECL |
6293 | || objc_method_decl (TREE_CODE (*node))) | |
f8e93a2e | 6294 | TREE_THIS_VOLATILE (*node) = 1; |
6295 | else if (TREE_CODE (type) == POINTER_TYPE | |
6296 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
6297 | TREE_TYPE (*node) | |
6298 | = build_pointer_type | |
6299 | (build_type_variant (TREE_TYPE (type), | |
71bda45d | 6300 | TYPE_READONLY (TREE_TYPE (type)), 1)); |
f8e93a2e | 6301 | else |
6302 | { | |
9b2d6d13 | 6303 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6304 | *no_add_attrs = true; |
6305 | } | |
6306 | ||
6307 | return NULL_TREE; | |
6308 | } | |
6309 | ||
5de92639 | 6310 | /* Handle a "hot" and attribute; arguments as in |
6311 | struct attribute_spec.handler. */ | |
6312 | ||
6313 | static tree | |
6314 | handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
46f8e3b0 | 6315 | int ARG_UNUSED (flags), bool *no_add_attrs) |
5de92639 | 6316 | { |
758a38ab | 6317 | if (TREE_CODE (*node) == FUNCTION_DECL |
6318 | || TREE_CODE (*node) == LABEL_DECL) | |
5de92639 | 6319 | { |
6320 | if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL) | |
6321 | { | |
6322 | warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", | |
6323 | name, "cold"); | |
6324 | *no_add_attrs = true; | |
6325 | } | |
24470055 | 6326 | /* Most of the rest of the hot processing is done later with |
6327 | lookup_attribute. */ | |
5de92639 | 6328 | } |
6329 | else | |
6330 | { | |
6331 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6332 | *no_add_attrs = true; | |
6333 | } | |
6334 | ||
6335 | return NULL_TREE; | |
6336 | } | |
758a38ab | 6337 | |
5de92639 | 6338 | /* Handle a "cold" and attribute; arguments as in |
6339 | struct attribute_spec.handler. */ | |
6340 | ||
6341 | static tree | |
6342 | handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
6343 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6344 | { | |
758a38ab | 6345 | if (TREE_CODE (*node) == FUNCTION_DECL |
6346 | || TREE_CODE (*node) == LABEL_DECL) | |
5de92639 | 6347 | { |
6348 | if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL) | |
6349 | { | |
6350 | warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", | |
6351 | name, "hot"); | |
6352 | *no_add_attrs = true; | |
6353 | } | |
24470055 | 6354 | /* Most of the rest of the cold processing is done later with |
6355 | lookup_attribute. */ | |
5de92639 | 6356 | } |
6357 | else | |
6358 | { | |
6359 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6360 | *no_add_attrs = true; | |
6361 | } | |
6362 | ||
6363 | return NULL_TREE; | |
6364 | } | |
6365 | ||
f8e93a2e | 6366 | /* Handle a "noinline" attribute; arguments as in |
6367 | struct attribute_spec.handler. */ | |
6368 | ||
6369 | static tree | |
1cae46be | 6370 | handle_noinline_attribute (tree *node, tree name, |
9a03a746 | 6371 | tree ARG_UNUSED (args), |
6372 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6373 | { |
6374 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6375 | DECL_UNINLINABLE (*node) = 1; | |
6376 | else | |
6377 | { | |
9b2d6d13 | 6378 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6379 | *no_add_attrs = true; |
6380 | } | |
6381 | ||
6382 | return NULL_TREE; | |
6383 | } | |
6384 | ||
bdb1f0d1 | 6385 | /* Handle a "noclone" attribute; arguments as in |
6386 | struct attribute_spec.handler. */ | |
6387 | ||
6388 | static tree | |
6389 | handle_noclone_attribute (tree *node, tree name, | |
6390 | tree ARG_UNUSED (args), | |
6391 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6392 | { | |
6393 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6394 | { | |
6395 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6396 | *no_add_attrs = true; | |
6397 | } | |
6398 | ||
6399 | return NULL_TREE; | |
6400 | } | |
6401 | ||
f8e93a2e | 6402 | /* Handle a "always_inline" attribute; arguments as in |
6403 | struct attribute_spec.handler. */ | |
6404 | ||
6405 | static tree | |
1cae46be | 6406 | handle_always_inline_attribute (tree *node, tree name, |
9a03a746 | 6407 | tree ARG_UNUSED (args), |
6408 | int ARG_UNUSED (flags), | |
09347743 | 6409 | bool *no_add_attrs) |
f8e93a2e | 6410 | { |
6411 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6412 | { | |
ebb7d626 | 6413 | /* Set the attribute and mark it for disregarding inline |
6414 | limits. */ | |
6415 | DECL_DISREGARD_INLINE_LIMITS (*node) = 1; | |
f8e93a2e | 6416 | } |
6417 | else | |
6418 | { | |
9b2d6d13 | 6419 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6420 | *no_add_attrs = true; |
541e4101 | 6421 | } |
6422 | ||
6423 | return NULL_TREE; | |
6424 | } | |
6425 | ||
6426 | /* Handle a "gnu_inline" attribute; arguments as in | |
6427 | struct attribute_spec.handler. */ | |
6428 | ||
6429 | static tree | |
6430 | handle_gnu_inline_attribute (tree *node, tree name, | |
6431 | tree ARG_UNUSED (args), | |
6432 | int ARG_UNUSED (flags), | |
6433 | bool *no_add_attrs) | |
6434 | { | |
6435 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6436 | { | |
6437 | /* Do nothing else, just set the attribute. We'll get at | |
6438 | it later with lookup_attribute. */ | |
6439 | } | |
6440 | else | |
6441 | { | |
6442 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6443 | *no_add_attrs = true; | |
7bd95dfd | 6444 | } |
6445 | ||
6446 | return NULL_TREE; | |
6447 | } | |
6448 | ||
6449 | /* Handle a "leaf" attribute; arguments as in | |
6450 | struct attribute_spec.handler. */ | |
6451 | ||
6452 | static tree | |
6453 | handle_leaf_attribute (tree *node, tree name, | |
6454 | tree ARG_UNUSED (args), | |
6455 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6456 | { | |
6457 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
6458 | { | |
6459 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6460 | *no_add_attrs = true; | |
6461 | } | |
6462 | if (!TREE_PUBLIC (*node)) | |
6463 | { | |
6464 | warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name); | |
6465 | *no_add_attrs = true; | |
f8e93a2e | 6466 | } |
6467 | ||
6468 | return NULL_TREE; | |
6469 | } | |
6470 | ||
1b16fc45 | 6471 | /* Handle an "artificial" attribute; arguments as in |
6472 | struct attribute_spec.handler. */ | |
6473 | ||
6474 | static tree | |
6475 | handle_artificial_attribute (tree *node, tree name, | |
6476 | tree ARG_UNUSED (args), | |
6477 | int ARG_UNUSED (flags), | |
6478 | bool *no_add_attrs) | |
6479 | { | |
6480 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6481 | { | |
6482 | /* Do nothing else, just set the attribute. We'll get at | |
6483 | it later with lookup_attribute. */ | |
6484 | } | |
6485 | else | |
6486 | { | |
6487 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6488 | *no_add_attrs = true; | |
6489 | } | |
6490 | ||
6491 | return NULL_TREE; | |
6492 | } | |
6493 | ||
0cdd9887 | 6494 | /* Handle a "flatten" attribute; arguments as in |
6495 | struct attribute_spec.handler. */ | |
6496 | ||
6497 | static tree | |
6498 | handle_flatten_attribute (tree *node, tree name, | |
a0c938f0 | 6499 | tree args ATTRIBUTE_UNUSED, |
6500 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
0cdd9887 | 6501 | { |
6502 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6503 | /* Do nothing else, just set the attribute. We'll get at | |
6504 | it later with lookup_attribute. */ | |
6505 | ; | |
6506 | else | |
6507 | { | |
6508 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6509 | *no_add_attrs = true; | |
6510 | } | |
6511 | ||
6512 | return NULL_TREE; | |
6513 | } | |
6514 | ||
10fc867f | 6515 | /* Handle a "warning" or "error" attribute; arguments as in |
6516 | struct attribute_spec.handler. */ | |
6517 | ||
6518 | static tree | |
6519 | handle_error_attribute (tree *node, tree name, tree args, | |
6520 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6521 | { | |
6522 | if (TREE_CODE (*node) == FUNCTION_DECL | |
6523 | || TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
6524 | /* Do nothing else, just set the attribute. We'll get at | |
6525 | it later with lookup_attribute. */ | |
6526 | ; | |
6527 | else | |
6528 | { | |
6529 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6530 | *no_add_attrs = true; | |
6531 | } | |
6532 | ||
6533 | return NULL_TREE; | |
6534 | } | |
0cdd9887 | 6535 | |
f8e93a2e | 6536 | /* Handle a "used" attribute; arguments as in |
6537 | struct attribute_spec.handler. */ | |
6538 | ||
6539 | static tree | |
9a03a746 | 6540 | handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args), |
6541 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6542 | { |
d0a31bd8 | 6543 | tree node = *pnode; |
6544 | ||
6545 | if (TREE_CODE (node) == FUNCTION_DECL | |
a4e3ffad | 6546 | || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node)) |
6547 | || (TREE_CODE (node) == TYPE_DECL)) | |
f54ed8bc | 6548 | { |
f54ed8bc | 6549 | TREE_USED (node) = 1; |
9423c9b7 | 6550 | DECL_PRESERVE_P (node) = 1; |
abc6c64f | 6551 | if (TREE_CODE (node) == VAR_DECL) |
6552 | DECL_READ_P (node) = 1; | |
f54ed8bc | 6553 | } |
f8e93a2e | 6554 | else |
6555 | { | |
9b2d6d13 | 6556 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6557 | *no_add_attrs = true; |
6558 | } | |
6559 | ||
6560 | return NULL_TREE; | |
6561 | } | |
6562 | ||
6563 | /* Handle a "unused" attribute; arguments as in | |
6564 | struct attribute_spec.handler. */ | |
6565 | ||
6566 | static tree | |
9a03a746 | 6567 | handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6568 | int flags, bool *no_add_attrs) | |
f8e93a2e | 6569 | { |
6570 | if (DECL_P (*node)) | |
6571 | { | |
6572 | tree decl = *node; | |
6573 | ||
6574 | if (TREE_CODE (decl) == PARM_DECL | |
6575 | || TREE_CODE (decl) == VAR_DECL | |
6576 | || TREE_CODE (decl) == FUNCTION_DECL | |
6577 | || TREE_CODE (decl) == LABEL_DECL | |
6578 | || TREE_CODE (decl) == TYPE_DECL) | |
abc6c64f | 6579 | { |
6580 | TREE_USED (decl) = 1; | |
6581 | if (TREE_CODE (decl) == VAR_DECL | |
6582 | || TREE_CODE (decl) == PARM_DECL) | |
6583 | DECL_READ_P (decl) = 1; | |
6584 | } | |
f8e93a2e | 6585 | else |
6586 | { | |
9b2d6d13 | 6587 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6588 | *no_add_attrs = true; |
6589 | } | |
6590 | } | |
6591 | else | |
6592 | { | |
6593 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 6594 | *node = build_variant_type_copy (*node); |
f8e93a2e | 6595 | TREE_USED (*node) = 1; |
6596 | } | |
6597 | ||
6598 | return NULL_TREE; | |
6599 | } | |
6600 | ||
62eec3b4 | 6601 | /* Handle a "externally_visible" attribute; arguments as in |
6602 | struct attribute_spec.handler. */ | |
6603 | ||
6604 | static tree | |
6605 | handle_externally_visible_attribute (tree *pnode, tree name, | |
6606 | tree ARG_UNUSED (args), | |
6607 | int ARG_UNUSED (flags), | |
6608 | bool *no_add_attrs) | |
6609 | { | |
6610 | tree node = *pnode; | |
6611 | ||
ba12ea31 | 6612 | if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL) |
62eec3b4 | 6613 | { |
ba12ea31 | 6614 | if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL |
6615 | && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node)) | |
6616 | { | |
6617 | warning (OPT_Wattributes, | |
6618 | "%qE attribute have effect only on public objects", name); | |
6619 | *no_add_attrs = true; | |
6620 | } | |
62eec3b4 | 6621 | } |
62eec3b4 | 6622 | else |
6623 | { | |
6624 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6625 | *no_add_attrs = true; | |
6626 | } | |
6627 | ||
6628 | return NULL_TREE; | |
6629 | } | |
6630 | ||
f8e93a2e | 6631 | /* Handle a "const" attribute; arguments as in |
6632 | struct attribute_spec.handler. */ | |
6633 | ||
6634 | static tree | |
9a03a746 | 6635 | handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6636 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6637 | { |
6638 | tree type = TREE_TYPE (*node); | |
6639 | ||
6640 | /* See FIXME comment on noreturn in c_common_attribute_table. */ | |
6641 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6642 | TREE_READONLY (*node) = 1; | |
6643 | else if (TREE_CODE (type) == POINTER_TYPE | |
6644 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
6645 | TREE_TYPE (*node) | |
6646 | = build_pointer_type | |
6647 | (build_type_variant (TREE_TYPE (type), 1, | |
6648 | TREE_THIS_VOLATILE (TREE_TYPE (type)))); | |
6649 | else | |
6650 | { | |
9b2d6d13 | 6651 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6652 | *no_add_attrs = true; |
6653 | } | |
6654 | ||
6655 | return NULL_TREE; | |
6656 | } | |
6657 | ||
6658 | /* Handle a "transparent_union" attribute; arguments as in | |
6659 | struct attribute_spec.handler. */ | |
6660 | ||
6661 | static tree | |
1cae46be | 6662 | handle_transparent_union_attribute (tree *node, tree name, |
9a03a746 | 6663 | tree ARG_UNUSED (args), int flags, |
09347743 | 6664 | bool *no_add_attrs) |
f8e93a2e | 6665 | { |
881eb642 | 6666 | tree type; |
03908818 | 6667 | |
6668 | *no_add_attrs = true; | |
f8e93a2e | 6669 | |
ffcdbf9c | 6670 | |
6671 | if (TREE_CODE (*node) == TYPE_DECL | |
6672 | && ! (flags & ATTR_FLAG_CXX11)) | |
881eb642 | 6673 | node = &TREE_TYPE (*node); |
6674 | type = *node; | |
f8e93a2e | 6675 | |
03908818 | 6676 | if (TREE_CODE (type) == UNION_TYPE) |
f8e93a2e | 6677 | { |
fca86134 | 6678 | /* Make sure that the first field will work for a transparent union. |
6679 | If the type isn't complete yet, leave the check to the code in | |
6680 | finish_struct. */ | |
6681 | if (TYPE_SIZE (type)) | |
6682 | { | |
6683 | tree first = first_field (type); | |
6684 | if (first == NULL_TREE | |
6685 | || DECL_ARTIFICIAL (first) | |
6686 | || TYPE_MODE (type) != DECL_MODE (first)) | |
6687 | goto ignored; | |
6688 | } | |
6689 | ||
f8e93a2e | 6690 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
03908818 | 6691 | { |
fca86134 | 6692 | /* If the type isn't complete yet, setting the flag |
6693 | on a variant wouldn't ever be checked. */ | |
6694 | if (!TYPE_SIZE (type)) | |
6695 | goto ignored; | |
6696 | ||
6697 | /* build_duplicate_type doesn't work for C++. */ | |
6698 | if (c_dialect_cxx ()) | |
03908818 | 6699 | goto ignored; |
6700 | ||
6701 | /* A type variant isn't good enough, since we don't a cast | |
6702 | to such a type removed as a no-op. */ | |
6703 | *node = type = build_duplicate_type (type); | |
6704 | } | |
6705 | ||
8df5a43d | 6706 | TYPE_TRANSPARENT_AGGR (type) = 1; |
03908818 | 6707 | return NULL_TREE; |
f8e93a2e | 6708 | } |
6709 | ||
03908818 | 6710 | ignored: |
6711 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 6712 | return NULL_TREE; |
6713 | } | |
6714 | ||
9af7fd5b | 6715 | /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to |
6716 | get the requested priority for a constructor or destructor, | |
6717 | possibly issuing diagnostics for invalid or reserved | |
6718 | priorities. */ | |
6719 | ||
6720 | static priority_type | |
6721 | get_priority (tree args, bool is_destructor) | |
6722 | { | |
6723 | HOST_WIDE_INT pri; | |
6c181a06 | 6724 | tree arg; |
9af7fd5b | 6725 | |
6726 | if (!args) | |
6727 | return DEFAULT_INIT_PRIORITY; | |
48e1416a | 6728 | |
28fbc04f | 6729 | if (!SUPPORTS_INIT_PRIORITY) |
6730 | { | |
6731 | if (is_destructor) | |
6732 | error ("destructor priorities are not supported"); | |
6733 | else | |
6734 | error ("constructor priorities are not supported"); | |
6735 | return DEFAULT_INIT_PRIORITY; | |
6736 | } | |
6737 | ||
6c181a06 | 6738 | arg = TREE_VALUE (args); |
c28ddc97 | 6739 | arg = default_conversion (arg); |
6c181a06 | 6740 | if (!host_integerp (arg, /*pos=*/0) |
6741 | || !INTEGRAL_TYPE_P (TREE_TYPE (arg))) | |
9af7fd5b | 6742 | goto invalid; |
6743 | ||
c28ddc97 | 6744 | pri = tree_low_cst (arg, /*pos=*/0); |
9af7fd5b | 6745 | if (pri < 0 || pri > MAX_INIT_PRIORITY) |
6746 | goto invalid; | |
6747 | ||
6748 | if (pri <= MAX_RESERVED_INIT_PRIORITY) | |
6749 | { | |
6750 | if (is_destructor) | |
6751 | warning (0, | |
6752 | "destructor priorities from 0 to %d are reserved " | |
48e1416a | 6753 | "for the implementation", |
9af7fd5b | 6754 | MAX_RESERVED_INIT_PRIORITY); |
6755 | else | |
6756 | warning (0, | |
6757 | "constructor priorities from 0 to %d are reserved " | |
48e1416a | 6758 | "for the implementation", |
9af7fd5b | 6759 | MAX_RESERVED_INIT_PRIORITY); |
6760 | } | |
6761 | return pri; | |
6762 | ||
6763 | invalid: | |
6764 | if (is_destructor) | |
6765 | error ("destructor priorities must be integers from 0 to %d inclusive", | |
6766 | MAX_INIT_PRIORITY); | |
6767 | else | |
6768 | error ("constructor priorities must be integers from 0 to %d inclusive", | |
6769 | MAX_INIT_PRIORITY); | |
6770 | return DEFAULT_INIT_PRIORITY; | |
6771 | } | |
6772 | ||
f8e93a2e | 6773 | /* Handle a "constructor" attribute; arguments as in |
6774 | struct attribute_spec.handler. */ | |
6775 | ||
6776 | static tree | |
9af7fd5b | 6777 | handle_constructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 6778 | int ARG_UNUSED (flags), |
09347743 | 6779 | bool *no_add_attrs) |
f8e93a2e | 6780 | { |
6781 | tree decl = *node; | |
6782 | tree type = TREE_TYPE (decl); | |
6783 | ||
6784 | if (TREE_CODE (decl) == FUNCTION_DECL | |
6785 | && TREE_CODE (type) == FUNCTION_TYPE | |
6786 | && decl_function_context (decl) == 0) | |
6787 | { | |
9af7fd5b | 6788 | priority_type priority; |
f8e93a2e | 6789 | DECL_STATIC_CONSTRUCTOR (decl) = 1; |
9af7fd5b | 6790 | priority = get_priority (args, /*is_destructor=*/false); |
6791 | SET_DECL_INIT_PRIORITY (decl, priority); | |
f8e93a2e | 6792 | TREE_USED (decl) = 1; |
6793 | } | |
6794 | else | |
6795 | { | |
9b2d6d13 | 6796 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6797 | *no_add_attrs = true; |
6798 | } | |
6799 | ||
6800 | return NULL_TREE; | |
6801 | } | |
6802 | ||
6803 | /* Handle a "destructor" attribute; arguments as in | |
6804 | struct attribute_spec.handler. */ | |
6805 | ||
6806 | static tree | |
9af7fd5b | 6807 | handle_destructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 6808 | int ARG_UNUSED (flags), |
09347743 | 6809 | bool *no_add_attrs) |
f8e93a2e | 6810 | { |
6811 | tree decl = *node; | |
6812 | tree type = TREE_TYPE (decl); | |
6813 | ||
6814 | if (TREE_CODE (decl) == FUNCTION_DECL | |
6815 | && TREE_CODE (type) == FUNCTION_TYPE | |
6816 | && decl_function_context (decl) == 0) | |
6817 | { | |
9af7fd5b | 6818 | priority_type priority; |
f8e93a2e | 6819 | DECL_STATIC_DESTRUCTOR (decl) = 1; |
9af7fd5b | 6820 | priority = get_priority (args, /*is_destructor=*/true); |
6821 | SET_DECL_FINI_PRIORITY (decl, priority); | |
f8e93a2e | 6822 | TREE_USED (decl) = 1; |
6823 | } | |
6824 | else | |
6825 | { | |
9b2d6d13 | 6826 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6827 | *no_add_attrs = true; |
6828 | } | |
6829 | ||
6830 | return NULL_TREE; | |
6831 | } | |
6832 | ||
1c58e3f1 | 6833 | /* Nonzero if the mode is a valid vector mode for this architecture. |
6834 | This returns nonzero even if there is no hardware support for the | |
6835 | vector mode, but we can emulate with narrower modes. */ | |
6836 | ||
6837 | static int | |
6838 | vector_mode_valid_p (enum machine_mode mode) | |
6839 | { | |
6840 | enum mode_class mclass = GET_MODE_CLASS (mode); | |
6841 | enum machine_mode innermode; | |
6842 | ||
6843 | /* Doh! What's going on? */ | |
6844 | if (mclass != MODE_VECTOR_INT | |
6845 | && mclass != MODE_VECTOR_FLOAT | |
6846 | && mclass != MODE_VECTOR_FRACT | |
6847 | && mclass != MODE_VECTOR_UFRACT | |
6848 | && mclass != MODE_VECTOR_ACCUM | |
6849 | && mclass != MODE_VECTOR_UACCUM) | |
6850 | return 0; | |
6851 | ||
6852 | /* Hardware support. Woo hoo! */ | |
6853 | if (targetm.vector_mode_supported_p (mode)) | |
6854 | return 1; | |
6855 | ||
6856 | innermode = GET_MODE_INNER (mode); | |
6857 | ||
6858 | /* We should probably return 1 if requesting V4DI and we have no DI, | |
6859 | but we have V2DI, but this is probably very unlikely. */ | |
6860 | ||
6861 | /* If we have support for the inner mode, we can safely emulate it. | |
6862 | We may not have V2DI, but me can emulate with a pair of DIs. */ | |
6863 | return targetm.scalar_mode_supported_p (innermode); | |
6864 | } | |
6865 | ||
6866 | ||
f8e93a2e | 6867 | /* Handle a "mode" attribute; arguments as in |
6868 | struct attribute_spec.handler. */ | |
6869 | ||
6870 | static tree | |
9a03a746 | 6871 | handle_mode_attribute (tree *node, tree name, tree args, |
6872 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6873 | { |
6874 | tree type = *node; | |
d1dd9ac0 | 6875 | tree ident = TREE_VALUE (args); |
f8e93a2e | 6876 | |
6877 | *no_add_attrs = true; | |
6878 | ||
d1dd9ac0 | 6879 | if (TREE_CODE (ident) != IDENTIFIER_NODE) |
9b2d6d13 | 6880 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6881 | else |
6882 | { | |
6883 | int j; | |
d1dd9ac0 | 6884 | const char *p = IDENTIFIER_POINTER (ident); |
f8e93a2e | 6885 | int len = strlen (p); |
6886 | enum machine_mode mode = VOIDmode; | |
6887 | tree typefm; | |
b2aef146 | 6888 | bool valid_mode; |
f8e93a2e | 6889 | |
6890 | if (len > 4 && p[0] == '_' && p[1] == '_' | |
6891 | && p[len - 1] == '_' && p[len - 2] == '_') | |
6892 | { | |
4fd61bc6 | 6893 | char *newp = (char *) alloca (len - 1); |
f8e93a2e | 6894 | |
6895 | strcpy (newp, &p[2]); | |
6896 | newp[len - 4] = '\0'; | |
6897 | p = newp; | |
6898 | } | |
6899 | ||
6900 | /* Change this type to have a type with the specified mode. | |
6901 | First check for the special modes. */ | |
84166705 | 6902 | if (!strcmp (p, "byte")) |
f8e93a2e | 6903 | mode = byte_mode; |
6904 | else if (!strcmp (p, "word")) | |
6905 | mode = word_mode; | |
84166705 | 6906 | else if (!strcmp (p, "pointer")) |
f8e93a2e | 6907 | mode = ptr_mode; |
0ef89dfd | 6908 | else if (!strcmp (p, "libgcc_cmp_return")) |
6909 | mode = targetm.libgcc_cmp_return_mode (); | |
6910 | else if (!strcmp (p, "libgcc_shift_count")) | |
6911 | mode = targetm.libgcc_shift_count_mode (); | |
1bd43494 | 6912 | else if (!strcmp (p, "unwind_word")) |
6913 | mode = targetm.unwind_word_mode (); | |
f8e93a2e | 6914 | else |
6915 | for (j = 0; j < NUM_MACHINE_MODES; j++) | |
6916 | if (!strcmp (p, GET_MODE_NAME (j))) | |
743a6f47 | 6917 | { |
6918 | mode = (enum machine_mode) j; | |
6919 | break; | |
6920 | } | |
f8e93a2e | 6921 | |
6922 | if (mode == VOIDmode) | |
4917c376 | 6923 | { |
d1dd9ac0 | 6924 | error ("unknown machine mode %qE", ident); |
4917c376 | 6925 | return NULL_TREE; |
6926 | } | |
6927 | ||
b2aef146 | 6928 | valid_mode = false; |
6929 | switch (GET_MODE_CLASS (mode)) | |
4917c376 | 6930 | { |
b2aef146 | 6931 | case MODE_INT: |
6932 | case MODE_PARTIAL_INT: | |
6933 | case MODE_FLOAT: | |
c4503c0a | 6934 | case MODE_DECIMAL_FLOAT: |
9421ebb9 | 6935 | case MODE_FRACT: |
6936 | case MODE_UFRACT: | |
6937 | case MODE_ACCUM: | |
6938 | case MODE_UACCUM: | |
b2aef146 | 6939 | valid_mode = targetm.scalar_mode_supported_p (mode); |
6940 | break; | |
6941 | ||
6942 | case MODE_COMPLEX_INT: | |
6943 | case MODE_COMPLEX_FLOAT: | |
6944 | valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode)); | |
6945 | break; | |
6946 | ||
6947 | case MODE_VECTOR_INT: | |
6948 | case MODE_VECTOR_FLOAT: | |
9421ebb9 | 6949 | case MODE_VECTOR_FRACT: |
6950 | case MODE_VECTOR_UFRACT: | |
6951 | case MODE_VECTOR_ACCUM: | |
6952 | case MODE_VECTOR_UACCUM: | |
9b2d6d13 | 6953 | warning (OPT_Wattributes, "specifying vector types with " |
6954 | "__attribute__ ((mode)) is deprecated"); | |
6955 | warning (OPT_Wattributes, | |
6956 | "use __attribute__ ((vector_size)) instead"); | |
b2aef146 | 6957 | valid_mode = vector_mode_valid_p (mode); |
6958 | break; | |
4917c376 | 6959 | |
b2aef146 | 6960 | default: |
6961 | break; | |
6962 | } | |
6963 | if (!valid_mode) | |
6964 | { | |
1e5fcbe2 | 6965 | error ("unable to emulate %qs", p); |
b2aef146 | 6966 | return NULL_TREE; |
6967 | } | |
4917c376 | 6968 | |
b2aef146 | 6969 | if (POINTER_TYPE_P (type)) |
ead34f59 | 6970 | { |
6d5d708e | 6971 | addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type)); |
b2aef146 | 6972 | tree (*fn)(tree, enum machine_mode, bool); |
6973 | ||
6d5d708e | 6974 | if (!targetm.addr_space.valid_pointer_mode (mode, as)) |
ead34f59 | 6975 | { |
1e5fcbe2 | 6976 | error ("invalid pointer mode %qs", p); |
ead34f59 | 6977 | return NULL_TREE; |
6978 | } | |
6979 | ||
a0c938f0 | 6980 | if (TREE_CODE (type) == POINTER_TYPE) |
b2aef146 | 6981 | fn = build_pointer_type_for_mode; |
805e22b2 | 6982 | else |
b2aef146 | 6983 | fn = build_reference_type_for_mode; |
6984 | typefm = fn (TREE_TYPE (type), mode, false); | |
ead34f59 | 6985 | } |
b2aef146 | 6986 | else |
9421ebb9 | 6987 | { |
6988 | /* For fixed-point modes, we need to test if the signness of type | |
6989 | and the machine mode are consistent. */ | |
6990 | if (ALL_FIXED_POINT_MODE_P (mode) | |
6991 | && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode)) | |
6992 | { | |
bf776685 | 6993 | error ("signedness of type and machine mode %qs don%'t match", p); |
9421ebb9 | 6994 | return NULL_TREE; |
6995 | } | |
6996 | /* For fixed-point modes, we need to pass saturating info. */ | |
6997 | typefm = lang_hooks.types.type_for_mode (mode, | |
6998 | ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type) | |
6999 | : TYPE_UNSIGNED (type)); | |
7000 | } | |
3a648ab9 | 7001 | |
b2aef146 | 7002 | if (typefm == NULL_TREE) |
7003 | { | |
743a6f47 | 7004 | error ("no data type for mode %qs", p); |
b2aef146 | 7005 | return NULL_TREE; |
7006 | } | |
3a648ab9 | 7007 | else if (TREE_CODE (type) == ENUMERAL_TYPE) |
7008 | { | |
7009 | /* For enumeral types, copy the precision from the integer | |
7010 | type returned above. If not an INTEGER_TYPE, we can't use | |
7011 | this mode for this type. */ | |
7012 | if (TREE_CODE (typefm) != INTEGER_TYPE) | |
7013 | { | |
743a6f47 | 7014 | error ("cannot use mode %qs for enumeral types", p); |
3a648ab9 | 7015 | return NULL_TREE; |
7016 | } | |
7017 | ||
10080eac | 7018 | if (flags & ATTR_FLAG_TYPE_IN_PLACE) |
7019 | { | |
7020 | TYPE_PRECISION (type) = TYPE_PRECISION (typefm); | |
7021 | typefm = type; | |
7022 | } | |
7023 | else | |
7024 | { | |
7025 | /* We cannot build a type variant, as there's code that assumes | |
7026 | that TYPE_MAIN_VARIANT has the same mode. This includes the | |
7027 | debug generators. Instead, create a subrange type. This | |
7028 | results in all of the enumeral values being emitted only once | |
7029 | in the original, and the subtype gets them by reference. */ | |
7030 | if (TYPE_UNSIGNED (type)) | |
7031 | typefm = make_unsigned_type (TYPE_PRECISION (typefm)); | |
7032 | else | |
7033 | typefm = make_signed_type (TYPE_PRECISION (typefm)); | |
7034 | TREE_TYPE (typefm) = type; | |
7035 | } | |
3a648ab9 | 7036 | } |
4bf450a1 | 7037 | else if (VECTOR_MODE_P (mode) |
7038 | ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm)) | |
7039 | : TREE_CODE (type) != TREE_CODE (typefm)) | |
743a6f47 | 7040 | { |
7041 | error ("mode %qs applied to inappropriate type", p); | |
7042 | return NULL_TREE; | |
7043 | } | |
7044 | ||
b2aef146 | 7045 | *node = typefm; |
f8e93a2e | 7046 | } |
7047 | ||
7048 | return NULL_TREE; | |
7049 | } | |
7050 | ||
7051 | /* Handle a "section" attribute; arguments as in | |
7052 | struct attribute_spec.handler. */ | |
7053 | ||
7054 | static tree | |
9a03a746 | 7055 | handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
7056 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7057 | { |
7058 | tree decl = *node; | |
7059 | ||
218e3e4e | 7060 | if (targetm_common.have_named_sections) |
f8e93a2e | 7061 | { |
065efcb1 | 7062 | user_defined_section_attribute = true; |
7063 | ||
f8e93a2e | 7064 | if ((TREE_CODE (decl) == FUNCTION_DECL |
7065 | || TREE_CODE (decl) == VAR_DECL) | |
7066 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
7067 | { | |
7068 | if (TREE_CODE (decl) == VAR_DECL | |
7069 | && current_function_decl != NULL_TREE | |
84166705 | 7070 | && !TREE_STATIC (decl)) |
f8e93a2e | 7071 | { |
48e1416a | 7072 | error_at (DECL_SOURCE_LOCATION (decl), |
712d2297 | 7073 | "section attribute cannot be specified for " |
7074 | "local variables"); | |
f8e93a2e | 7075 | *no_add_attrs = true; |
7076 | } | |
7077 | ||
7078 | /* The decl may have already been given a section attribute | |
7079 | from a previous declaration. Ensure they match. */ | |
7080 | else if (DECL_SECTION_NAME (decl) != NULL_TREE | |
7081 | && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), | |
7082 | TREE_STRING_POINTER (TREE_VALUE (args))) != 0) | |
7083 | { | |
3cf8b391 | 7084 | error ("section of %q+D conflicts with previous declaration", |
7085 | *node); | |
f8e93a2e | 7086 | *no_add_attrs = true; |
7087 | } | |
38475469 | 7088 | else if (TREE_CODE (decl) == VAR_DECL |
7089 | && !targetm.have_tls && targetm.emutls.tmpl_section | |
7090 | && DECL_THREAD_LOCAL_P (decl)) | |
7091 | { | |
7092 | error ("section of %q+D cannot be overridden", *node); | |
7093 | *no_add_attrs = true; | |
7094 | } | |
f8e93a2e | 7095 | else |
7096 | DECL_SECTION_NAME (decl) = TREE_VALUE (args); | |
7097 | } | |
7098 | else | |
7099 | { | |
3cf8b391 | 7100 | error ("section attribute not allowed for %q+D", *node); |
f8e93a2e | 7101 | *no_add_attrs = true; |
7102 | } | |
7103 | } | |
7104 | else | |
7105 | { | |
712d2297 | 7106 | error_at (DECL_SOURCE_LOCATION (*node), |
7107 | "section attributes are not supported for this target"); | |
f8e93a2e | 7108 | *no_add_attrs = true; |
7109 | } | |
7110 | ||
7111 | return NULL_TREE; | |
7112 | } | |
7113 | ||
83e25171 | 7114 | /* Check whether ALIGN is a valid user-specified alignment. If so, |
7115 | return its base-2 log; if not, output an error and return -1. If | |
7116 | ALLOW_ZERO then 0 is valid and should result in a return of -1 with | |
7117 | no error. */ | |
7118 | int | |
7119 | check_user_alignment (const_tree align, bool allow_zero) | |
7120 | { | |
7121 | int i; | |
7122 | ||
7123 | if (!INTEGRAL_TYPE_P (TREE_TYPE (align)) | |
7124 | || TREE_CODE (align) != INTEGER_CST) | |
7125 | { | |
7126 | error ("requested alignment is not an integer constant"); | |
7127 | return -1; | |
7128 | } | |
7129 | else if (allow_zero && integer_zerop (align)) | |
7130 | return -1; | |
7131 | else if ((i = tree_log2 (align)) == -1) | |
7132 | { | |
7133 | error ("requested alignment is not a power of 2"); | |
7134 | return -1; | |
7135 | } | |
7136 | else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG) | |
7137 | { | |
7138 | error ("requested alignment is too large"); | |
7139 | return -1; | |
7140 | } | |
7141 | return i; | |
7142 | } | |
7143 | ||
ffcdbf9c | 7144 | /* |
7145 | If in c++-11, check if the c++-11 alignment constraint with respect | |
7146 | to fundamental alignment (in [dcl.align]) are satisfied. If not in | |
7147 | c++-11 mode, does nothing. | |
7148 | ||
7149 | [dcl.align]2/ says: | |
7150 | ||
7151 | [* if the constant expression evaluates to a fundamental alignment, | |
7152 | the alignment requirement of the declared entity shall be the | |
7153 | specified fundamental alignment. | |
7154 | ||
7155 | * if the constant expression evaluates to an extended alignment | |
7156 | and the implementation supports that alignment in the context | |
7157 | of the declaration, the alignment of the declared entity shall | |
7158 | be that alignment | |
7159 | ||
7160 | * if the constant expression evaluates to an extended alignment | |
7161 | and the implementation does not support that alignment in the | |
7162 | context of the declaration, the program is ill-formed]. */ | |
7163 | ||
7164 | static bool | |
7165 | check_cxx_fundamental_alignment_constraints (tree node, | |
7166 | unsigned align_log, | |
7167 | int flags) | |
7168 | { | |
7169 | bool alignment_too_large_p = false; | |
7170 | unsigned requested_alignment = 1U << align_log; | |
7171 | unsigned max_align = 0; | |
7172 | ||
7173 | if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat) | |
7174 | || (node == NULL_TREE || node == error_mark_node)) | |
7175 | return true; | |
7176 | ||
7177 | if (cxx_fundamental_alignment_p (requested_alignment)) | |
7178 | return true; | |
7179 | ||
7180 | if (DECL_P (node)) | |
7181 | { | |
7182 | if (TREE_STATIC (node)) | |
7183 | { | |
7184 | /* For file scope variables and static members, the target | |
7185 | supports alignments that are at most | |
7186 | MAX_OFILE_ALIGNMENT. */ | |
7187 | if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT)) | |
7188 | alignment_too_large_p = true; | |
7189 | } | |
7190 | else | |
7191 | { | |
7192 | #ifdef BIGGEST_FIELD_ALIGNMENT | |
7193 | #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT | |
7194 | #else | |
7195 | #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT | |
7196 | #endif | |
7197 | /* For non-static members, the target supports either | |
7198 | alignments that at most either BIGGEST_FIELD_ALIGNMENT | |
7199 | if it is defined or BIGGEST_ALIGNMENT. */ | |
7200 | max_align = MAX_TARGET_FIELD_ALIGNMENT; | |
7201 | if (TREE_CODE (node) == FIELD_DECL | |
7202 | && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT)) | |
7203 | alignment_too_large_p = true; | |
7204 | #undef MAX_TARGET_FIELD_ALIGNMENT | |
7205 | /* For stack variables, the target supports at most | |
7206 | MAX_STACK_ALIGNMENT. */ | |
7207 | else if (decl_function_context (node) != NULL | |
7208 | && requested_alignment > (max_align = MAX_STACK_ALIGNMENT)) | |
7209 | alignment_too_large_p = true; | |
7210 | } | |
7211 | } | |
7212 | else if (TYPE_P (node)) | |
7213 | { | |
7214 | /* Let's be liberal for types. */ | |
7215 | if (requested_alignment > (max_align = BIGGEST_ALIGNMENT)) | |
7216 | alignment_too_large_p = true; | |
7217 | } | |
7218 | ||
7219 | if (alignment_too_large_p) | |
7220 | pedwarn (input_location, OPT_Wattributes, | |
7221 | "requested alignment %d is larger than %d", | |
7222 | requested_alignment, max_align); | |
7223 | ||
7224 | return !alignment_too_large_p; | |
7225 | } | |
7226 | ||
f8e93a2e | 7227 | /* Handle a "aligned" attribute; arguments as in |
7228 | struct attribute_spec.handler. */ | |
7229 | ||
7230 | static tree | |
9a03a746 | 7231 | handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
09347743 | 7232 | int flags, bool *no_add_attrs) |
f8e93a2e | 7233 | { |
7234 | tree decl = NULL_TREE; | |
7235 | tree *type = NULL; | |
7236 | int is_type = 0; | |
7237 | tree align_expr = (args ? TREE_VALUE (args) | |
02421213 | 7238 | : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT)); |
f8e93a2e | 7239 | int i; |
7240 | ||
7241 | if (DECL_P (*node)) | |
7242 | { | |
7243 | decl = *node; | |
7244 | type = &TREE_TYPE (decl); | |
7245 | is_type = TREE_CODE (*node) == TYPE_DECL; | |
7246 | } | |
7247 | else if (TYPE_P (*node)) | |
7248 | type = node, is_type = 1; | |
7249 | ||
ffcdbf9c | 7250 | if ((i = check_user_alignment (align_expr, false)) == -1 |
7251 | || !check_cxx_fundamental_alignment_constraints (*node, i, flags)) | |
83e25171 | 7252 | *no_add_attrs = true; |
f8e93a2e | 7253 | else if (is_type) |
7254 | { | |
2ec3af9c | 7255 | if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
7256 | /* OK, modify the type in place. */; | |
f8e93a2e | 7257 | /* If we have a TYPE_DECL, then copy the type, so that we |
7258 | don't accidentally modify a builtin type. See pushdecl. */ | |
2ec3af9c | 7259 | else if (decl && TREE_TYPE (decl) != error_mark_node |
7260 | && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) | |
f8e93a2e | 7261 | { |
7262 | tree tt = TREE_TYPE (decl); | |
e086912e | 7263 | *type = build_variant_type_copy (*type); |
f8e93a2e | 7264 | DECL_ORIGINAL_TYPE (decl) = tt; |
7265 | TYPE_NAME (*type) = decl; | |
7266 | TREE_USED (*type) = TREE_USED (decl); | |
7267 | TREE_TYPE (decl) = *type; | |
7268 | } | |
2ec3af9c | 7269 | else |
e086912e | 7270 | *type = build_variant_type_copy (*type); |
f8e93a2e | 7271 | |
7cfdc2f0 | 7272 | TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT; |
f8e93a2e | 7273 | TYPE_USER_ALIGN (*type) = 1; |
7274 | } | |
097b5c8b | 7275 | else if (! VAR_OR_FUNCTION_DECL_P (decl) |
f8e93a2e | 7276 | && TREE_CODE (decl) != FIELD_DECL) |
7277 | { | |
3cf8b391 | 7278 | error ("alignment may not be specified for %q+D", decl); |
f8e93a2e | 7279 | *no_add_attrs = true; |
7280 | } | |
ffcdbf9c | 7281 | else if (DECL_USER_ALIGN (decl) |
7282 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) | |
7283 | /* C++-11 [dcl.align/4]: | |
7284 | ||
7285 | When multiple alignment-specifiers are specified for an | |
7286 | entity, the alignment requirement shall be set to the | |
7287 | strictest specified alignment. | |
7288 | ||
7289 | This formally comes from the c++11 specification but we are | |
7290 | doing it for the GNU attribute syntax as well. */ | |
7291 | *no_add_attrs = true; | |
097b5c8b | 7292 | else if (TREE_CODE (decl) == FUNCTION_DECL |
7cfdc2f0 | 7293 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) |
097b5c8b | 7294 | { |
7295 | if (DECL_USER_ALIGN (decl)) | |
7296 | error ("alignment for %q+D was previously specified as %d " | |
7297 | "and may not be decreased", decl, | |
7298 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
7299 | else | |
7300 | error ("alignment for %q+D must be at least %d", decl, | |
7301 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
7302 | *no_add_attrs = true; | |
7303 | } | |
f8e93a2e | 7304 | else |
7305 | { | |
7cfdc2f0 | 7306 | DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT; |
f8e93a2e | 7307 | DECL_USER_ALIGN (decl) = 1; |
7308 | } | |
7309 | ||
7310 | return NULL_TREE; | |
7311 | } | |
7312 | ||
7313 | /* Handle a "weak" attribute; arguments as in | |
7314 | struct attribute_spec.handler. */ | |
7315 | ||
7316 | static tree | |
f948b309 | 7317 | handle_weak_attribute (tree *node, tree name, |
9a03a746 | 7318 | tree ARG_UNUSED (args), |
7319 | int ARG_UNUSED (flags), | |
7320 | bool * ARG_UNUSED (no_add_attrs)) | |
f8e93a2e | 7321 | { |
f948b309 | 7322 | if (TREE_CODE (*node) == FUNCTION_DECL |
059a60f3 | 7323 | && DECL_DECLARED_INLINE_P (*node)) |
7324 | { | |
0725e25c | 7325 | warning (OPT_Wattributes, "inline function %q+D declared weak", *node); |
059a60f3 | 7326 | *no_add_attrs = true; |
7327 | } | |
85c0a25c | 7328 | else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
7329 | { | |
7330 | error ("indirect function %q+D cannot be declared weak", *node); | |
7331 | *no_add_attrs = true; | |
7332 | return NULL_TREE; | |
7333 | } | |
059a60f3 | 7334 | else if (TREE_CODE (*node) == FUNCTION_DECL |
7335 | || TREE_CODE (*node) == VAR_DECL) | |
f948b309 | 7336 | declare_weak (*node); |
7337 | else | |
7338 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 7339 | |
7340 | return NULL_TREE; | |
7341 | } | |
7342 | ||
85c0a25c | 7343 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
7344 | struct attribute_spec.handler, except that IS_ALIAS tells us | |
7345 | whether this is an alias as opposed to ifunc attribute. */ | |
f8e93a2e | 7346 | |
7347 | static tree | |
85c0a25c | 7348 | handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args, |
7349 | bool *no_add_attrs) | |
f8e93a2e | 7350 | { |
7351 | tree decl = *node; | |
7352 | ||
85c0a25c | 7353 | if (TREE_CODE (decl) != FUNCTION_DECL |
7354 | && (!is_alias || TREE_CODE (decl) != VAR_DECL)) | |
9e830260 | 7355 | { |
7356 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7357 | *no_add_attrs = true; | |
7358 | } | |
7359 | else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) | |
48e1416a | 7360 | || (TREE_CODE (decl) != FUNCTION_DECL |
0a3ecdc1 | 7361 | && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) |
7362 | /* A static variable declaration is always a tentative definition, | |
7363 | but the alias is a non-tentative definition which overrides. */ | |
48e1416a | 7364 | || (TREE_CODE (decl) != FUNCTION_DECL |
0a3ecdc1 | 7365 | && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl))) |
f8e93a2e | 7366 | { |
85c0a25c | 7367 | error ("%q+D defined both normally and as %qE attribute", decl, name); |
f8e93a2e | 7368 | *no_add_attrs = true; |
85c0a25c | 7369 | return NULL_TREE; |
f8e93a2e | 7370 | } |
85c0a25c | 7371 | else if (!is_alias |
7372 | && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl)) | |
7373 | || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))) | |
7374 | { | |
7375 | error ("weak %q+D cannot be defined %qE", decl, name); | |
7376 | *no_add_attrs = true; | |
7377 | return NULL_TREE; | |
7378 | } | |
8c42f0d9 | 7379 | |
7380 | /* Note that the very first time we process a nested declaration, | |
7381 | decl_function_context will not be set. Indeed, *would* never | |
7382 | be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that | |
7383 | we do below. After such frobbery, pushdecl would set the context. | |
7384 | In any case, this is never what we want. */ | |
7385 | else if (decl_function_context (decl) == 0 && current_function_decl == NULL) | |
f8e93a2e | 7386 | { |
7387 | tree id; | |
7388 | ||
7389 | id = TREE_VALUE (args); | |
7390 | if (TREE_CODE (id) != STRING_CST) | |
7391 | { | |
85c0a25c | 7392 | error ("attribute %qE argument not a string", name); |
f8e93a2e | 7393 | *no_add_attrs = true; |
7394 | return NULL_TREE; | |
7395 | } | |
7396 | id = get_identifier (TREE_STRING_POINTER (id)); | |
7397 | /* This counts as a use of the object pointed to. */ | |
7398 | TREE_USED (id) = 1; | |
7399 | ||
7400 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
7401 | DECL_INITIAL (decl) = error_mark_node; | |
7402 | else | |
4ee9c684 | 7403 | { |
f4a30bd7 | 7404 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) |
7405 | DECL_EXTERNAL (decl) = 1; | |
7406 | else | |
7407 | DECL_EXTERNAL (decl) = 0; | |
4ee9c684 | 7408 | TREE_STATIC (decl) = 1; |
7409 | } | |
85c0a25c | 7410 | |
7411 | if (!is_alias) | |
7412 | /* ifuncs are also aliases, so set that attribute too. */ | |
7413 | DECL_ATTRIBUTES (decl) | |
7414 | = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl)); | |
f8e93a2e | 7415 | } |
7416 | else | |
7417 | { | |
9b2d6d13 | 7418 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7419 | *no_add_attrs = true; |
7420 | } | |
7421 | ||
7422 | return NULL_TREE; | |
7423 | } | |
7424 | ||
85c0a25c | 7425 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
7426 | struct attribute_spec.handler. */ | |
7427 | ||
7428 | static tree | |
7429 | handle_ifunc_attribute (tree *node, tree name, tree args, | |
7430 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7431 | { | |
7432 | return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs); | |
7433 | } | |
7434 | ||
7435 | /* Handle an "alias" or "ifunc" attribute; arguments as in | |
7436 | struct attribute_spec.handler. */ | |
7437 | ||
7438 | static tree | |
7439 | handle_alias_attribute (tree *node, tree name, tree args, | |
7440 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7441 | { | |
7442 | return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs); | |
7443 | } | |
7444 | ||
f4a30bd7 | 7445 | /* Handle a "weakref" attribute; arguments as in struct |
7446 | attribute_spec.handler. */ | |
7447 | ||
7448 | static tree | |
7449 | handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
7450 | int flags, bool *no_add_attrs) | |
7451 | { | |
7452 | tree attr = NULL_TREE; | |
7453 | ||
83852912 | 7454 | /* We must ignore the attribute when it is associated with |
7455 | local-scoped decls, since attribute alias is ignored and many | |
7456 | such symbols do not even have a DECL_WEAK field. */ | |
64a7bd81 | 7457 | if (decl_function_context (*node) |
7458 | || current_function_decl | |
7459 | || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL)) | |
83852912 | 7460 | { |
7461 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7462 | *no_add_attrs = true; | |
7463 | return NULL_TREE; | |
7464 | } | |
7465 | ||
85c0a25c | 7466 | if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
7467 | { | |
7468 | error ("indirect function %q+D cannot be declared weakref", *node); | |
7469 | *no_add_attrs = true; | |
7470 | return NULL_TREE; | |
7471 | } | |
7472 | ||
f4a30bd7 | 7473 | /* The idea here is that `weakref("name")' mutates into `weakref, |
7474 | alias("name")', and weakref without arguments, in turn, | |
7475 | implicitly adds weak. */ | |
7476 | ||
7477 | if (args) | |
7478 | { | |
7479 | attr = tree_cons (get_identifier ("alias"), args, attr); | |
7480 | attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr); | |
7481 | ||
7482 | *no_add_attrs = true; | |
0a3ecdc1 | 7483 | |
7484 | decl_attributes (node, attr, flags); | |
f4a30bd7 | 7485 | } |
7486 | else | |
7487 | { | |
7488 | if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node))) | |
712d2297 | 7489 | error_at (DECL_SOURCE_LOCATION (*node), |
7490 | "weakref attribute must appear before alias attribute"); | |
f4a30bd7 | 7491 | |
0a3ecdc1 | 7492 | /* Can't call declare_weak because it wants this to be TREE_PUBLIC, |
7493 | and that isn't supported; and because it wants to add it to | |
7494 | the list of weak decls, which isn't helpful. */ | |
7495 | DECL_WEAK (*node) = 1; | |
f4a30bd7 | 7496 | } |
7497 | ||
f4a30bd7 | 7498 | return NULL_TREE; |
7499 | } | |
7500 | ||
f8e93a2e | 7501 | /* Handle an "visibility" attribute; arguments as in |
7502 | struct attribute_spec.handler. */ | |
7503 | ||
7504 | static tree | |
1cae46be | 7505 | handle_visibility_attribute (tree *node, tree name, tree args, |
9a03a746 | 7506 | int ARG_UNUSED (flags), |
4a2849cb | 7507 | bool *ARG_UNUSED (no_add_attrs)) |
f8e93a2e | 7508 | { |
7509 | tree decl = *node; | |
9c40570a | 7510 | tree id = TREE_VALUE (args); |
4a2849cb | 7511 | enum symbol_visibility vis; |
f8e93a2e | 7512 | |
b212f378 | 7513 | if (TYPE_P (*node)) |
7514 | { | |
4a2849cb | 7515 | if (TREE_CODE (*node) == ENUMERAL_TYPE) |
7516 | /* OK */; | |
7517 | else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE) | |
7518 | { | |
7519 | warning (OPT_Wattributes, "%qE attribute ignored on non-class types", | |
7520 | name); | |
7521 | return NULL_TREE; | |
7522 | } | |
7523 | else if (TYPE_FIELDS (*node)) | |
7524 | { | |
7525 | error ("%qE attribute ignored because %qT is already defined", | |
7526 | name, *node); | |
7527 | return NULL_TREE; | |
7528 | } | |
b212f378 | 7529 | } |
84166705 | 7530 | else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl)) |
f8e93a2e | 7531 | { |
9b2d6d13 | 7532 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
9c40570a | 7533 | return NULL_TREE; |
f8e93a2e | 7534 | } |
f8e93a2e | 7535 | |
9c40570a | 7536 | if (TREE_CODE (id) != STRING_CST) |
7537 | { | |
07e3a3d2 | 7538 | error ("visibility argument not a string"); |
9c40570a | 7539 | return NULL_TREE; |
f8e93a2e | 7540 | } |
b27ac6b5 | 7541 | |
b212f378 | 7542 | /* If this is a type, set the visibility on the type decl. */ |
7543 | if (TYPE_P (decl)) | |
7544 | { | |
7545 | decl = TYPE_NAME (decl); | |
84166705 | 7546 | if (!decl) |
a0c938f0 | 7547 | return NULL_TREE; |
e147aab3 | 7548 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
7549 | { | |
9b2d6d13 | 7550 | warning (OPT_Wattributes, "%qE attribute ignored on types", |
e147aab3 | 7551 | name); |
7552 | return NULL_TREE; | |
7553 | } | |
b212f378 | 7554 | } |
f8e93a2e | 7555 | |
9c40570a | 7556 | if (strcmp (TREE_STRING_POINTER (id), "default") == 0) |
4a2849cb | 7557 | vis = VISIBILITY_DEFAULT; |
9c40570a | 7558 | else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0) |
4a2849cb | 7559 | vis = VISIBILITY_INTERNAL; |
9c40570a | 7560 | else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0) |
4a2849cb | 7561 | vis = VISIBILITY_HIDDEN; |
9c40570a | 7562 | else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0) |
4a2849cb | 7563 | vis = VISIBILITY_PROTECTED; |
9c40570a | 7564 | else |
4a2849cb | 7565 | { |
7566 | error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\""); | |
7567 | vis = VISIBILITY_DEFAULT; | |
7568 | } | |
7569 | ||
7570 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
098a01e7 | 7571 | && vis != DECL_VISIBILITY (decl)) |
7572 | { | |
7573 | tree attributes = (TYPE_P (*node) | |
7574 | ? TYPE_ATTRIBUTES (*node) | |
7575 | : DECL_ATTRIBUTES (decl)); | |
7576 | if (lookup_attribute ("visibility", attributes)) | |
7577 | error ("%qD redeclared with different visibility", decl); | |
7578 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
7579 | && lookup_attribute ("dllimport", attributes)) | |
7580 | error ("%qD was declared %qs which implies default visibility", | |
7581 | decl, "dllimport"); | |
7582 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
7583 | && lookup_attribute ("dllexport", attributes)) | |
7584 | error ("%qD was declared %qs which implies default visibility", | |
7585 | decl, "dllexport"); | |
7586 | } | |
4a2849cb | 7587 | |
7588 | DECL_VISIBILITY (decl) = vis; | |
b212f378 | 7589 | DECL_VISIBILITY_SPECIFIED (decl) = 1; |
7590 | ||
4a2849cb | 7591 | /* Go ahead and attach the attribute to the node as well. This is needed |
7592 | so we can determine whether we have VISIBILITY_DEFAULT because the | |
7593 | visibility was not specified, or because it was explicitly overridden | |
7594 | from the containing scope. */ | |
9c40570a | 7595 | |
f8e93a2e | 7596 | return NULL_TREE; |
7597 | } | |
7598 | ||
3aa0c315 | 7599 | /* Determine the ELF symbol visibility for DECL, which is either a |
7600 | variable or a function. It is an error to use this function if a | |
7601 | definition of DECL is not available in this translation unit. | |
7602 | Returns true if the final visibility has been determined by this | |
7603 | function; false if the caller is free to make additional | |
7604 | modifications. */ | |
7605 | ||
7606 | bool | |
7607 | c_determine_visibility (tree decl) | |
7608 | { | |
1d8fc210 | 7609 | gcc_assert (TREE_CODE (decl) == VAR_DECL |
7610 | || TREE_CODE (decl) == FUNCTION_DECL); | |
3aa0c315 | 7611 | |
7612 | /* If the user explicitly specified the visibility with an | |
7613 | attribute, honor that. DECL_VISIBILITY will have been set during | |
920f5a70 | 7614 | the processing of the attribute. We check for an explicit |
7615 | attribute, rather than just checking DECL_VISIBILITY_SPECIFIED, | |
7616 | to distinguish the use of an attribute from the use of a "#pragma | |
7617 | GCC visibility push(...)"; in the latter case we still want other | |
7618 | considerations to be able to overrule the #pragma. */ | |
7619 | if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)) | |
7620 | || (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
7621 | && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) | |
7622 | || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))))) | |
3aa0c315 | 7623 | return true; |
7624 | ||
4a2849cb | 7625 | /* Set default visibility to whatever the user supplied with |
7626 | visibility_specified depending on #pragma GCC visibility. */ | |
7627 | if (!DECL_VISIBILITY_SPECIFIED (decl)) | |
7628 | { | |
2d9d8740 | 7629 | if (visibility_options.inpragma |
7630 | || DECL_VISIBILITY (decl) != default_visibility) | |
7631 | { | |
7632 | DECL_VISIBILITY (decl) = default_visibility; | |
7633 | DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; | |
7634 | /* If visibility changed and DECL already has DECL_RTL, ensure | |
7635 | symbol flags are updated. */ | |
7636 | if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)) | |
7637 | || TREE_CODE (decl) == FUNCTION_DECL) | |
7638 | && DECL_RTL_SET_P (decl)) | |
7639 | make_decl_rtl (decl); | |
7640 | } | |
4a2849cb | 7641 | } |
3aa0c315 | 7642 | return false; |
7643 | } | |
7644 | ||
24dfead4 | 7645 | /* Handle an "tls_model" attribute; arguments as in |
7646 | struct attribute_spec.handler. */ | |
7647 | ||
7648 | static tree | |
1cae46be | 7649 | handle_tls_model_attribute (tree *node, tree name, tree args, |
9a03a746 | 7650 | int ARG_UNUSED (flags), bool *no_add_attrs) |
24dfead4 | 7651 | { |
1b53eb20 | 7652 | tree id; |
24dfead4 | 7653 | tree decl = *node; |
1b53eb20 | 7654 | enum tls_model kind; |
24dfead4 | 7655 | |
1b53eb20 | 7656 | *no_add_attrs = true; |
7657 | ||
c34f8a78 | 7658 | if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl)) |
24dfead4 | 7659 | { |
9b2d6d13 | 7660 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
1b53eb20 | 7661 | return NULL_TREE; |
24dfead4 | 7662 | } |
24dfead4 | 7663 | |
1b53eb20 | 7664 | kind = DECL_TLS_MODEL (decl); |
7665 | id = TREE_VALUE (args); | |
7666 | if (TREE_CODE (id) != STRING_CST) | |
7667 | { | |
7668 | error ("tls_model argument not a string"); | |
7669 | return NULL_TREE; | |
24dfead4 | 7670 | } |
7671 | ||
1b53eb20 | 7672 | if (!strcmp (TREE_STRING_POINTER (id), "local-exec")) |
7673 | kind = TLS_MODEL_LOCAL_EXEC; | |
7674 | else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec")) | |
7675 | kind = TLS_MODEL_INITIAL_EXEC; | |
7676 | else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic")) | |
7677 | kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC; | |
7678 | else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic")) | |
7679 | kind = TLS_MODEL_GLOBAL_DYNAMIC; | |
7680 | else | |
7681 | error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\""); | |
7682 | ||
7683 | DECL_TLS_MODEL (decl) = kind; | |
24dfead4 | 7684 | return NULL_TREE; |
7685 | } | |
7686 | ||
f8e93a2e | 7687 | /* Handle a "no_instrument_function" attribute; arguments as in |
7688 | struct attribute_spec.handler. */ | |
7689 | ||
7690 | static tree | |
1cae46be | 7691 | handle_no_instrument_function_attribute (tree *node, tree name, |
9a03a746 | 7692 | tree ARG_UNUSED (args), |
7693 | int ARG_UNUSED (flags), | |
09347743 | 7694 | bool *no_add_attrs) |
f8e93a2e | 7695 | { |
7696 | tree decl = *node; | |
7697 | ||
7698 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
7699 | { | |
712d2297 | 7700 | error_at (DECL_SOURCE_LOCATION (decl), |
7701 | "%qE attribute applies only to functions", name); | |
f8e93a2e | 7702 | *no_add_attrs = true; |
7703 | } | |
7704 | else if (DECL_INITIAL (decl)) | |
7705 | { | |
712d2297 | 7706 | error_at (DECL_SOURCE_LOCATION (decl), |
7707 | "can%'t set %qE attribute after definition", name); | |
f8e93a2e | 7708 | *no_add_attrs = true; |
7709 | } | |
7710 | else | |
7711 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
7712 | ||
7713 | return NULL_TREE; | |
7714 | } | |
7715 | ||
7716 | /* Handle a "malloc" attribute; arguments as in | |
7717 | struct attribute_spec.handler. */ | |
7718 | ||
7719 | static tree | |
9a03a746 | 7720 | handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
7721 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7722 | { |
a5147fca | 7723 | if (TREE_CODE (*node) == FUNCTION_DECL |
7724 | && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) | |
f8e93a2e | 7725 | DECL_IS_MALLOC (*node) = 1; |
f8e93a2e | 7726 | else |
7727 | { | |
9b2d6d13 | 7728 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7729 | *no_add_attrs = true; |
7730 | } | |
7731 | ||
7732 | return NULL_TREE; | |
7733 | } | |
7734 | ||
4a29c97c | 7735 | /* Handle a "alloc_size" attribute; arguments as in |
7736 | struct attribute_spec.handler. */ | |
7737 | ||
7738 | static tree | |
7739 | handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
7740 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7741 | { | |
2802826e | 7742 | unsigned arg_count = type_num_arguments (*node); |
4a29c97c | 7743 | for (; args; args = TREE_CHAIN (args)) |
7744 | { | |
7745 | tree position = TREE_VALUE (args); | |
7746 | ||
7747 | if (TREE_CODE (position) != INTEGER_CST | |
48e1416a | 7748 | || TREE_INT_CST_HIGH (position) |
4a29c97c | 7749 | || TREE_INT_CST_LOW (position) < 1 |
7750 | || TREE_INT_CST_LOW (position) > arg_count ) | |
7751 | { | |
48e1416a | 7752 | warning (OPT_Wattributes, |
4a29c97c | 7753 | "alloc_size parameter outside range"); |
7754 | *no_add_attrs = true; | |
7755 | return NULL_TREE; | |
7756 | } | |
7757 | } | |
7758 | return NULL_TREE; | |
7759 | } | |
7760 | ||
8ce86007 | 7761 | /* Handle a "fn spec" attribute; arguments as in |
7762 | struct attribute_spec.handler. */ | |
7763 | ||
7764 | static tree | |
7765 | handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name), | |
7766 | tree args, int ARG_UNUSED (flags), | |
7767 | bool *no_add_attrs ATTRIBUTE_UNUSED) | |
7768 | { | |
7769 | gcc_assert (args | |
7770 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST | |
7771 | && !TREE_CHAIN (args)); | |
7772 | return NULL_TREE; | |
7773 | } | |
7774 | ||
26d1c5ff | 7775 | /* Handle a "returns_twice" attribute; arguments as in |
7776 | struct attribute_spec.handler. */ | |
7777 | ||
7778 | static tree | |
7779 | handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
7780 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7781 | { | |
7782 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
7783 | DECL_IS_RETURNS_TWICE (*node) = 1; | |
7784 | else | |
7785 | { | |
9b2d6d13 | 7786 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
26d1c5ff | 7787 | *no_add_attrs = true; |
7788 | } | |
7789 | ||
7790 | return NULL_TREE; | |
7791 | } | |
7792 | ||
f8e93a2e | 7793 | /* Handle a "no_limit_stack" attribute; arguments as in |
7794 | struct attribute_spec.handler. */ | |
7795 | ||
7796 | static tree | |
1cae46be | 7797 | handle_no_limit_stack_attribute (tree *node, tree name, |
9a03a746 | 7798 | tree ARG_UNUSED (args), |
7799 | int ARG_UNUSED (flags), | |
09347743 | 7800 | bool *no_add_attrs) |
f8e93a2e | 7801 | { |
7802 | tree decl = *node; | |
7803 | ||
7804 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
7805 | { | |
712d2297 | 7806 | error_at (DECL_SOURCE_LOCATION (decl), |
7807 | "%qE attribute applies only to functions", name); | |
f8e93a2e | 7808 | *no_add_attrs = true; |
7809 | } | |
7810 | else if (DECL_INITIAL (decl)) | |
7811 | { | |
712d2297 | 7812 | error_at (DECL_SOURCE_LOCATION (decl), |
7813 | "can%'t set %qE attribute after definition", name); | |
f8e93a2e | 7814 | *no_add_attrs = true; |
7815 | } | |
7816 | else | |
7817 | DECL_NO_LIMIT_STACK (decl) = 1; | |
7818 | ||
7819 | return NULL_TREE; | |
7820 | } | |
7821 | ||
7822 | /* Handle a "pure" attribute; arguments as in | |
7823 | struct attribute_spec.handler. */ | |
7824 | ||
7825 | static tree | |
9a03a746 | 7826 | handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
7827 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7828 | { |
7829 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
9c2a0c05 | 7830 | DECL_PURE_P (*node) = 1; |
f8e93a2e | 7831 | /* ??? TODO: Support types. */ |
7832 | else | |
7833 | { | |
9b2d6d13 | 7834 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7835 | *no_add_attrs = true; |
7836 | } | |
7837 | ||
7838 | return NULL_TREE; | |
7839 | } | |
7840 | ||
4c0315d0 | 7841 | /* Digest an attribute list destined for a transactional memory statement. |
7842 | ALLOWED is the set of attributes that are allowed for this statement; | |
7843 | return the attribute we parsed. Multiple attributes are never allowed. */ | |
7844 | ||
7845 | int | |
7846 | parse_tm_stmt_attr (tree attrs, int allowed) | |
7847 | { | |
7848 | tree a_seen = NULL; | |
7849 | int m_seen = 0; | |
7850 | ||
7851 | for ( ; attrs ; attrs = TREE_CHAIN (attrs)) | |
7852 | { | |
7853 | tree a = TREE_PURPOSE (attrs); | |
7854 | int m = 0; | |
7855 | ||
7856 | if (is_attribute_p ("outer", a)) | |
7857 | m = TM_STMT_ATTR_OUTER; | |
7858 | ||
7859 | if ((m & allowed) == 0) | |
7860 | { | |
7861 | warning (OPT_Wattributes, "%qE attribute directive ignored", a); | |
7862 | continue; | |
7863 | } | |
7864 | ||
7865 | if (m_seen == 0) | |
7866 | { | |
7867 | a_seen = a; | |
7868 | m_seen = m; | |
7869 | } | |
7870 | else if (m_seen == m) | |
7871 | warning (OPT_Wattributes, "%qE attribute duplicated", a); | |
7872 | else | |
7873 | warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen); | |
7874 | } | |
7875 | ||
7876 | return m_seen; | |
7877 | } | |
7878 | ||
7879 | /* Transform a TM attribute name into a maskable integer and back. | |
7880 | Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding | |
7881 | to how the lack of an attribute is treated. */ | |
7882 | ||
7883 | int | |
7884 | tm_attr_to_mask (tree attr) | |
7885 | { | |
7886 | if (attr == NULL) | |
7887 | return 0; | |
7888 | if (is_attribute_p ("transaction_safe", attr)) | |
7889 | return TM_ATTR_SAFE; | |
7890 | if (is_attribute_p ("transaction_callable", attr)) | |
7891 | return TM_ATTR_CALLABLE; | |
7892 | if (is_attribute_p ("transaction_pure", attr)) | |
7893 | return TM_ATTR_PURE; | |
7894 | if (is_attribute_p ("transaction_unsafe", attr)) | |
7895 | return TM_ATTR_IRREVOCABLE; | |
7896 | if (is_attribute_p ("transaction_may_cancel_outer", attr)) | |
7897 | return TM_ATTR_MAY_CANCEL_OUTER; | |
7898 | return 0; | |
7899 | } | |
7900 | ||
7901 | tree | |
7902 | tm_mask_to_attr (int mask) | |
7903 | { | |
7904 | const char *str; | |
7905 | switch (mask) | |
7906 | { | |
7907 | case TM_ATTR_SAFE: | |
7908 | str = "transaction_safe"; | |
7909 | break; | |
7910 | case TM_ATTR_CALLABLE: | |
7911 | str = "transaction_callable"; | |
7912 | break; | |
7913 | case TM_ATTR_PURE: | |
7914 | str = "transaction_pure"; | |
7915 | break; | |
7916 | case TM_ATTR_IRREVOCABLE: | |
7917 | str = "transaction_unsafe"; | |
7918 | break; | |
7919 | case TM_ATTR_MAY_CANCEL_OUTER: | |
7920 | str = "transaction_may_cancel_outer"; | |
7921 | break; | |
7922 | default: | |
7923 | gcc_unreachable (); | |
7924 | } | |
7925 | return get_identifier (str); | |
7926 | } | |
7927 | ||
7928 | /* Return the first TM attribute seen in LIST. */ | |
7929 | ||
7930 | tree | |
7931 | find_tm_attribute (tree list) | |
7932 | { | |
7933 | for (; list ; list = TREE_CHAIN (list)) | |
7934 | { | |
7935 | tree name = TREE_PURPOSE (list); | |
7936 | if (tm_attr_to_mask (name) != 0) | |
7937 | return name; | |
7938 | } | |
7939 | return NULL_TREE; | |
7940 | } | |
7941 | ||
7942 | /* Handle the TM attributes; arguments as in struct attribute_spec.handler. | |
7943 | Here we accept only function types, and verify that none of the other | |
7944 | function TM attributes are also applied. */ | |
7945 | /* ??? We need to accept class types for C++, but not C. This greatly | |
7946 | complicates this function, since we can no longer rely on the extra | |
7947 | processing given by function_type_required. */ | |
7948 | ||
7949 | static tree | |
7950 | handle_tm_attribute (tree *node, tree name, tree args, | |
7951 | int flags, bool *no_add_attrs) | |
7952 | { | |
7953 | /* Only one path adds the attribute; others don't. */ | |
7954 | *no_add_attrs = true; | |
7955 | ||
7956 | switch (TREE_CODE (*node)) | |
7957 | { | |
7958 | case RECORD_TYPE: | |
7959 | case UNION_TYPE: | |
7960 | /* Only tm_callable and tm_safe apply to classes. */ | |
7961 | if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE)) | |
7962 | goto ignored; | |
7963 | /* FALLTHRU */ | |
7964 | ||
7965 | case FUNCTION_TYPE: | |
7966 | case METHOD_TYPE: | |
7967 | { | |
7968 | tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node)); | |
7969 | if (old_name == name) | |
7970 | ; | |
7971 | else if (old_name != NULL_TREE) | |
7972 | error ("type was previously declared %qE", old_name); | |
7973 | else | |
7974 | *no_add_attrs = false; | |
7975 | } | |
7976 | break; | |
7977 | ||
7978 | case POINTER_TYPE: | |
7979 | { | |
7980 | enum tree_code subcode = TREE_CODE (TREE_TYPE (*node)); | |
7981 | if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE) | |
7982 | { | |
7983 | tree fn_tmp = TREE_TYPE (*node); | |
7984 | decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0); | |
7985 | *node = build_pointer_type (fn_tmp); | |
7986 | break; | |
7987 | } | |
7988 | } | |
7989 | /* FALLTHRU */ | |
7990 | ||
7991 | default: | |
7992 | /* If a function is next, pass it on to be tried next. */ | |
7993 | if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) | |
7994 | return tree_cons (name, args, NULL); | |
7995 | ||
7996 | ignored: | |
7997 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7998 | break; | |
7999 | } | |
8000 | ||
8001 | return NULL_TREE; | |
8002 | } | |
8003 | ||
8004 | /* Handle the TM_WRAP attribute; arguments as in | |
8005 | struct attribute_spec.handler. */ | |
8006 | ||
8007 | static tree | |
8008 | handle_tm_wrap_attribute (tree *node, tree name, tree args, | |
8009 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8010 | { | |
8011 | tree decl = *node; | |
8012 | ||
8013 | /* We don't need the attribute even on success, since we | |
8014 | record the entry in an external table. */ | |
8015 | *no_add_attrs = true; | |
8016 | ||
8017 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8018 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8019 | else | |
8020 | { | |
8021 | tree wrap_decl = TREE_VALUE (args); | |
8022 | if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE | |
8023 | && TREE_CODE (wrap_decl) != VAR_DECL | |
8024 | && TREE_CODE (wrap_decl) != FUNCTION_DECL) | |
8025 | error ("%qE argument not an identifier", name); | |
8026 | else | |
8027 | { | |
8028 | if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE) | |
8029 | wrap_decl = lookup_name (wrap_decl); | |
8030 | if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL) | |
8031 | { | |
8032 | if (lang_hooks.types_compatible_p (TREE_TYPE (decl), | |
8033 | TREE_TYPE (wrap_decl))) | |
8034 | record_tm_replacement (wrap_decl, decl); | |
8035 | else | |
8036 | error ("%qD is not compatible with %qD", wrap_decl, decl); | |
8037 | } | |
8038 | else | |
8039 | error ("transaction_wrap argument is not a function"); | |
8040 | } | |
8041 | } | |
8042 | ||
8043 | return NULL_TREE; | |
8044 | } | |
8045 | ||
8046 | /* Ignore the given attribute. Used when this attribute may be usefully | |
8047 | overridden by the target, but is not used generically. */ | |
8048 | ||
8049 | static tree | |
8050 | ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name), | |
8051 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8052 | bool *no_add_attrs) | |
8053 | { | |
8054 | *no_add_attrs = true; | |
8055 | return NULL_TREE; | |
8056 | } | |
8057 | ||
fc09b200 | 8058 | /* Handle a "no vops" attribute; arguments as in |
8059 | struct attribute_spec.handler. */ | |
8060 | ||
8061 | static tree | |
8062 | handle_novops_attribute (tree *node, tree ARG_UNUSED (name), | |
8063 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8064 | bool *ARG_UNUSED (no_add_attrs)) | |
8065 | { | |
8066 | gcc_assert (TREE_CODE (*node) == FUNCTION_DECL); | |
8067 | DECL_IS_NOVOPS (*node) = 1; | |
8068 | return NULL_TREE; | |
8069 | } | |
8070 | ||
f8e93a2e | 8071 | /* Handle a "deprecated" attribute; arguments as in |
8072 | struct attribute_spec.handler. */ | |
1cae46be | 8073 | |
f8e93a2e | 8074 | static tree |
1cae46be | 8075 | handle_deprecated_attribute (tree *node, tree name, |
45c4e798 | 8076 | tree args, int flags, |
09347743 | 8077 | bool *no_add_attrs) |
f8e93a2e | 8078 | { |
8079 | tree type = NULL_TREE; | |
8080 | int warn = 0; | |
782858b8 | 8081 | tree what = NULL_TREE; |
1cae46be | 8082 | |
45c4e798 | 8083 | if (!args) |
8084 | *no_add_attrs = true; | |
8085 | else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST) | |
8086 | { | |
8087 | error ("deprecated message is not a string"); | |
8088 | *no_add_attrs = true; | |
8089 | } | |
8090 | ||
f8e93a2e | 8091 | if (DECL_P (*node)) |
8092 | { | |
8093 | tree decl = *node; | |
8094 | type = TREE_TYPE (decl); | |
1cae46be | 8095 | |
f8e93a2e | 8096 | if (TREE_CODE (decl) == TYPE_DECL |
8097 | || TREE_CODE (decl) == PARM_DECL | |
8098 | || TREE_CODE (decl) == VAR_DECL | |
8099 | || TREE_CODE (decl) == FUNCTION_DECL | |
40c8d1dd | 8100 | || TREE_CODE (decl) == FIELD_DECL |
8101 | || objc_method_decl (TREE_CODE (decl))) | |
f8e93a2e | 8102 | TREE_DEPRECATED (decl) = 1; |
8103 | else | |
8104 | warn = 1; | |
8105 | } | |
8106 | else if (TYPE_P (*node)) | |
8107 | { | |
8108 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 8109 | *node = build_variant_type_copy (*node); |
f8e93a2e | 8110 | TREE_DEPRECATED (*node) = 1; |
8111 | type = *node; | |
8112 | } | |
8113 | else | |
8114 | warn = 1; | |
1cae46be | 8115 | |
f8e93a2e | 8116 | if (warn) |
8117 | { | |
8118 | *no_add_attrs = true; | |
8119 | if (type && TYPE_NAME (type)) | |
8120 | { | |
8121 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
782858b8 | 8122 | what = TYPE_NAME (*node); |
f8e93a2e | 8123 | else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL |
8124 | && DECL_NAME (TYPE_NAME (type))) | |
782858b8 | 8125 | what = DECL_NAME (TYPE_NAME (type)); |
f8e93a2e | 8126 | } |
8127 | if (what) | |
9b2d6d13 | 8128 | warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what); |
f8e93a2e | 8129 | else |
9b2d6d13 | 8130 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8131 | } |
8132 | ||
8133 | return NULL_TREE; | |
8134 | } | |
8135 | ||
f8e93a2e | 8136 | /* Handle a "vector_size" attribute; arguments as in |
8137 | struct attribute_spec.handler. */ | |
8138 | ||
8139 | static tree | |
1cae46be | 8140 | handle_vector_size_attribute (tree *node, tree name, tree args, |
9a03a746 | 8141 | int ARG_UNUSED (flags), |
09347743 | 8142 | bool *no_add_attrs) |
f8e93a2e | 8143 | { |
8144 | unsigned HOST_WIDE_INT vecsize, nunits; | |
83e2a11b | 8145 | enum machine_mode orig_mode; |
4917c376 | 8146 | tree type = *node, new_type, size; |
f8e93a2e | 8147 | |
8148 | *no_add_attrs = true; | |
8149 | ||
4917c376 | 8150 | size = TREE_VALUE (args); |
4917c376 | 8151 | |
84166705 | 8152 | if (!host_integerp (size, 1)) |
f8e93a2e | 8153 | { |
9b2d6d13 | 8154 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 8155 | return NULL_TREE; |
8156 | } | |
8157 | ||
8158 | /* Get the vector size (in bytes). */ | |
4917c376 | 8159 | vecsize = tree_low_cst (size, 1); |
f8e93a2e | 8160 | |
8161 | /* We need to provide for vector pointers, vector arrays, and | |
8162 | functions returning vectors. For example: | |
8163 | ||
8164 | __attribute__((vector_size(16))) short *foo; | |
8165 | ||
8166 | In this case, the mode is SI, but the type being modified is | |
8167 | HI, so we need to look further. */ | |
8168 | ||
8169 | while (POINTER_TYPE_P (type) | |
8170 | || TREE_CODE (type) == FUNCTION_TYPE | |
5bfb0742 | 8171 | || TREE_CODE (type) == METHOD_TYPE |
2cb0e5d9 | 8172 | || TREE_CODE (type) == ARRAY_TYPE |
8173 | || TREE_CODE (type) == OFFSET_TYPE) | |
f8e93a2e | 8174 | type = TREE_TYPE (type); |
8175 | ||
8176 | /* Get the mode of the type being modified. */ | |
8177 | orig_mode = TYPE_MODE (type); | |
8178 | ||
2cb0e5d9 | 8179 | if ((!INTEGRAL_TYPE_P (type) |
8180 | && !SCALAR_FLOAT_TYPE_P (type) | |
8181 | && !FIXED_POINT_TYPE_P (type)) | |
cee7491d | 8182 | || (!SCALAR_FLOAT_MODE_P (orig_mode) |
9421ebb9 | 8183 | && GET_MODE_CLASS (orig_mode) != MODE_INT |
8184 | && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode)) | |
7ec31215 | 8185 | || !host_integerp (TYPE_SIZE_UNIT (type), 1) |
8186 | || TREE_CODE (type) == BOOLEAN_TYPE) | |
f8e93a2e | 8187 | { |
782858b8 | 8188 | error ("invalid vector type for attribute %qE", name); |
f8e93a2e | 8189 | return NULL_TREE; |
8190 | } | |
8191 | ||
39cc3e6d | 8192 | if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1)) |
8193 | { | |
8194 | error ("vector size not an integral multiple of component size"); | |
8195 | return NULL; | |
8196 | } | |
8197 | ||
8198 | if (vecsize == 0) | |
8199 | { | |
8200 | error ("zero vector size"); | |
8201 | return NULL; | |
8202 | } | |
8203 | ||
f8e93a2e | 8204 | /* Calculate how many units fit in the vector. */ |
8205 | nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1); | |
83e2a11b | 8206 | if (nunits & (nunits - 1)) |
f8e93a2e | 8207 | { |
83e2a11b | 8208 | error ("number of components of the vector not a power of two"); |
f8e93a2e | 8209 | return NULL_TREE; |
8210 | } | |
8211 | ||
83e2a11b | 8212 | new_type = build_vector_type (type, nunits); |
f8e93a2e | 8213 | |
8214 | /* Build back pointers if needed. */ | |
d991e6e8 | 8215 | *node = lang_hooks.types.reconstruct_complex_type (*node, new_type); |
f8e93a2e | 8216 | |
8217 | return NULL_TREE; | |
8218 | } | |
8219 | ||
dbf6c367 | 8220 | /* Handle the "nonnull" attribute. */ |
8221 | static tree | |
9a03a746 | 8222 | handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), |
8223 | tree args, int ARG_UNUSED (flags), | |
09347743 | 8224 | bool *no_add_attrs) |
dbf6c367 | 8225 | { |
8226 | tree type = *node; | |
8227 | unsigned HOST_WIDE_INT attr_arg_num; | |
8228 | ||
8229 | /* If no arguments are specified, all pointer arguments should be | |
d716ce75 | 8230 | non-null. Verify a full prototype is given so that the arguments |
dbf6c367 | 8231 | will have the correct types when we actually check them later. */ |
84166705 | 8232 | if (!args) |
dbf6c367 | 8233 | { |
a36cf284 | 8234 | if (!prototype_p (type)) |
dbf6c367 | 8235 | { |
8236 | error ("nonnull attribute without arguments on a non-prototype"); | |
4ee9c684 | 8237 | *no_add_attrs = true; |
dbf6c367 | 8238 | } |
8239 | return NULL_TREE; | |
8240 | } | |
8241 | ||
8242 | /* Argument list specified. Verify that each argument number references | |
8243 | a pointer argument. */ | |
8244 | for (attr_arg_num = 1; args; args = TREE_CHAIN (args)) | |
8245 | { | |
4ee9c684 | 8246 | unsigned HOST_WIDE_INT arg_num = 0, ck_num; |
dbf6c367 | 8247 | |
84166705 | 8248 | if (!get_nonnull_operand (TREE_VALUE (args), &arg_num)) |
dbf6c367 | 8249 | { |
07e3a3d2 | 8250 | error ("nonnull argument has invalid operand number (argument %lu)", |
dbf6c367 | 8251 | (unsigned long) attr_arg_num); |
8252 | *no_add_attrs = true; | |
8253 | return NULL_TREE; | |
8254 | } | |
8255 | ||
d0af78c5 | 8256 | if (prototype_p (type)) |
dbf6c367 | 8257 | { |
d0af78c5 | 8258 | function_args_iterator iter; |
8259 | tree argument; | |
8260 | ||
8261 | function_args_iter_init (&iter, type); | |
8262 | for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter)) | |
dbf6c367 | 8263 | { |
d0af78c5 | 8264 | argument = function_args_iter_cond (&iter); |
8265 | if (argument == NULL_TREE || ck_num == arg_num) | |
dbf6c367 | 8266 | break; |
dbf6c367 | 8267 | } |
8268 | ||
84166705 | 8269 | if (!argument |
d0af78c5 | 8270 | || TREE_CODE (argument) == VOID_TYPE) |
dbf6c367 | 8271 | { |
07e3a3d2 | 8272 | error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)", |
dbf6c367 | 8273 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
8274 | *no_add_attrs = true; | |
8275 | return NULL_TREE; | |
8276 | } | |
8277 | ||
d0af78c5 | 8278 | if (TREE_CODE (argument) != POINTER_TYPE) |
dbf6c367 | 8279 | { |
07e3a3d2 | 8280 | error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)", |
dbf6c367 | 8281 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
8282 | *no_add_attrs = true; | |
8283 | return NULL_TREE; | |
8284 | } | |
8285 | } | |
8286 | } | |
8287 | ||
8288 | return NULL_TREE; | |
8289 | } | |
8290 | ||
8291 | /* Check the argument list of a function call for null in argument slots | |
d01f58f9 | 8292 | that are marked as requiring a non-null pointer argument. The NARGS |
8293 | arguments are passed in the array ARGARRAY. | |
8294 | */ | |
dbf6c367 | 8295 | |
8296 | static void | |
d01f58f9 | 8297 | check_function_nonnull (tree attrs, int nargs, tree *argarray) |
dbf6c367 | 8298 | { |
9ca77b08 | 8299 | tree a; |
d01f58f9 | 8300 | int i; |
dbf6c367 | 8301 | |
9ca77b08 | 8302 | attrs = lookup_attribute ("nonnull", attrs); |
8303 | if (attrs == NULL_TREE) | |
8304 | return; | |
8305 | ||
8306 | a = attrs; | |
8307 | /* See if any of the nonnull attributes has no arguments. If so, | |
8308 | then every pointer argument is checked (in which case the check | |
8309 | for pointer type is done in check_nonnull_arg). */ | |
8310 | if (TREE_VALUE (a) != NULL_TREE) | |
8311 | do | |
8312 | a = lookup_attribute ("nonnull", TREE_CHAIN (a)); | |
8313 | while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE); | |
8314 | ||
8315 | if (a != NULL_TREE) | |
8316 | for (i = 0; i < nargs; i++) | |
8317 | check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i], | |
8318 | i + 1); | |
8319 | else | |
dbf6c367 | 8320 | { |
9ca77b08 | 8321 | /* Walk the argument list. If we encounter an argument number we |
8322 | should check for non-null, do it. */ | |
8323 | for (i = 0; i < nargs; i++) | |
dbf6c367 | 8324 | { |
9ca77b08 | 8325 | for (a = attrs; ; a = TREE_CHAIN (a)) |
4ee9c684 | 8326 | { |
9ca77b08 | 8327 | a = lookup_attribute ("nonnull", a); |
8328 | if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1)) | |
8329 | break; | |
4ee9c684 | 8330 | } |
9ca77b08 | 8331 | |
8332 | if (a != NULL_TREE) | |
8333 | check_function_arguments_recurse (check_nonnull_arg, NULL, | |
8334 | argarray[i], i + 1); | |
dbf6c367 | 8335 | } |
8336 | } | |
8337 | } | |
8338 | ||
50ca527f | 8339 | /* Check that the Nth argument of a function call (counting backwards |
d01f58f9 | 8340 | from the end) is a (pointer)0. The NARGS arguments are passed in the |
8341 | array ARGARRAY. */ | |
bf6c8de0 | 8342 | |
8343 | static void | |
774e9d58 | 8344 | check_function_sentinel (const_tree fntype, int nargs, tree *argarray) |
bf6c8de0 | 8345 | { |
774e9d58 | 8346 | tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype)); |
bf6c8de0 | 8347 | |
8348 | if (attr) | |
8349 | { | |
d01f58f9 | 8350 | int len = 0; |
8351 | int pos = 0; | |
8352 | tree sentinel; | |
774e9d58 | 8353 | function_args_iterator iter; |
8354 | tree t; | |
a0c938f0 | 8355 | |
d01f58f9 | 8356 | /* Skip over the named arguments. */ |
774e9d58 | 8357 | FOREACH_FUNCTION_ARGS (fntype, t, iter) |
a0c938f0 | 8358 | { |
774e9d58 | 8359 | if (len == nargs) |
8360 | break; | |
d01f58f9 | 8361 | len++; |
8362 | } | |
50ca527f | 8363 | |
d01f58f9 | 8364 | if (TREE_VALUE (attr)) |
8365 | { | |
8366 | tree p = TREE_VALUE (TREE_VALUE (attr)); | |
8367 | pos = TREE_INT_CST_LOW (p); | |
8368 | } | |
50ca527f | 8369 | |
d01f58f9 | 8370 | /* The sentinel must be one of the varargs, i.e. |
8371 | in position >= the number of fixed arguments. */ | |
8372 | if ((nargs - 1 - pos) < len) | |
8373 | { | |
8374 | warning (OPT_Wformat, | |
8375 | "not enough variable arguments to fit a sentinel"); | |
8376 | return; | |
bf6c8de0 | 8377 | } |
d01f58f9 | 8378 | |
8379 | /* Validate the sentinel. */ | |
8380 | sentinel = argarray[nargs - 1 - pos]; | |
8381 | if ((!POINTER_TYPE_P (TREE_TYPE (sentinel)) | |
8382 | || !integer_zerop (sentinel)) | |
8383 | /* Although __null (in C++) is only an integer we allow it | |
8384 | nevertheless, as we are guaranteed that it's exactly | |
8385 | as wide as a pointer, and we don't want to force | |
8386 | users to cast the NULL they have written there. | |
8387 | We warn with -Wstrict-null-sentinel, though. */ | |
8388 | && (warn_strict_null_sentinel || null_node != sentinel)) | |
8389 | warning (OPT_Wformat, "missing sentinel in function call"); | |
bf6c8de0 | 8390 | } |
8391 | } | |
8392 | ||
dbf6c367 | 8393 | /* Helper for check_function_nonnull; given a list of operands which |
8394 | must be non-null in ARGS, determine if operand PARAM_NUM should be | |
8395 | checked. */ | |
8396 | ||
8397 | static bool | |
1cae46be | 8398 | nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 8399 | { |
4ee9c684 | 8400 | unsigned HOST_WIDE_INT arg_num = 0; |
dbf6c367 | 8401 | |
8402 | for (; args; args = TREE_CHAIN (args)) | |
8403 | { | |
231bd014 | 8404 | bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num); |
8405 | ||
8406 | gcc_assert (found); | |
dbf6c367 | 8407 | |
8408 | if (arg_num == param_num) | |
8409 | return true; | |
8410 | } | |
8411 | return false; | |
8412 | } | |
8413 | ||
8414 | /* Check that the function argument PARAM (which is operand number | |
8415 | PARAM_NUM) is non-null. This is called by check_function_nonnull | |
8416 | via check_function_arguments_recurse. */ | |
8417 | ||
8418 | static void | |
9a03a746 | 8419 | check_nonnull_arg (void * ARG_UNUSED (ctx), tree param, |
1cae46be | 8420 | unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 8421 | { |
8422 | /* Just skip checking the argument if it's not a pointer. This can | |
8423 | happen if the "nonnull" attribute was given without an operand | |
8424 | list (which means to check every pointer argument). */ | |
8425 | ||
8426 | if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) | |
8427 | return; | |
8428 | ||
8429 | if (integer_zerop (param)) | |
155b601b | 8430 | warning (OPT_Wnonnull, "null argument where non-null required " |
8431 | "(argument %lu)", (unsigned long) param_num); | |
dbf6c367 | 8432 | } |
8433 | ||
8434 | /* Helper for nonnull attribute handling; fetch the operand number | |
8435 | from the attribute argument list. */ | |
8436 | ||
8437 | static bool | |
1cae46be | 8438 | get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) |
dbf6c367 | 8439 | { |
67409385 | 8440 | /* Verify the arg number is a constant. */ |
dbf6c367 | 8441 | if (TREE_CODE (arg_num_expr) != INTEGER_CST |
8442 | || TREE_INT_CST_HIGH (arg_num_expr) != 0) | |
8443 | return false; | |
8444 | ||
8445 | *valp = TREE_INT_CST_LOW (arg_num_expr); | |
8446 | return true; | |
8447 | } | |
fa987697 | 8448 | |
8449 | /* Handle a "nothrow" attribute; arguments as in | |
8450 | struct attribute_spec.handler. */ | |
8451 | ||
8452 | static tree | |
9a03a746 | 8453 | handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
8454 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
fa987697 | 8455 | { |
8456 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
8457 | TREE_NOTHROW (*node) = 1; | |
8458 | /* ??? TODO: Support types. */ | |
8459 | else | |
8460 | { | |
9b2d6d13 | 8461 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
fa987697 | 8462 | *no_add_attrs = true; |
8463 | } | |
8464 | ||
8465 | return NULL_TREE; | |
8466 | } | |
7acb29a3 | 8467 | |
8468 | /* Handle a "cleanup" attribute; arguments as in | |
8469 | struct attribute_spec.handler. */ | |
8470 | ||
8471 | static tree | |
1cae46be | 8472 | handle_cleanup_attribute (tree *node, tree name, tree args, |
9a03a746 | 8473 | int ARG_UNUSED (flags), bool *no_add_attrs) |
7acb29a3 | 8474 | { |
8475 | tree decl = *node; | |
8476 | tree cleanup_id, cleanup_decl; | |
8477 | ||
8478 | /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do | |
8479 | for global destructors in C++. This requires infrastructure that | |
8480 | we don't have generically at the moment. It's also not a feature | |
8481 | we'd be missing too much, since we do have attribute constructor. */ | |
8482 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
8483 | { | |
9b2d6d13 | 8484 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
7acb29a3 | 8485 | *no_add_attrs = true; |
8486 | return NULL_TREE; | |
8487 | } | |
8488 | ||
8489 | /* Verify that the argument is a function in scope. */ | |
8490 | /* ??? We could support pointers to functions here as well, if | |
8491 | that was considered desirable. */ | |
8492 | cleanup_id = TREE_VALUE (args); | |
8493 | if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE) | |
8494 | { | |
07e3a3d2 | 8495 | error ("cleanup argument not an identifier"); |
7acb29a3 | 8496 | *no_add_attrs = true; |
8497 | return NULL_TREE; | |
8498 | } | |
d1c41717 | 8499 | cleanup_decl = lookup_name (cleanup_id); |
7acb29a3 | 8500 | if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL) |
8501 | { | |
07e3a3d2 | 8502 | error ("cleanup argument not a function"); |
7acb29a3 | 8503 | *no_add_attrs = true; |
8504 | return NULL_TREE; | |
8505 | } | |
8506 | ||
1cae46be | 8507 | /* That the function has proper type is checked with the |
7acb29a3 | 8508 | eventual call to build_function_call. */ |
8509 | ||
8510 | return NULL_TREE; | |
8511 | } | |
8a8cdb8d | 8512 | |
8513 | /* Handle a "warn_unused_result" attribute. No special handling. */ | |
8514 | ||
8515 | static tree | |
8516 | handle_warn_unused_result_attribute (tree *node, tree name, | |
9a03a746 | 8517 | tree ARG_UNUSED (args), |
8518 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8a8cdb8d | 8519 | { |
8520 | /* Ignore the attribute for functions not returning any value. */ | |
8521 | if (VOID_TYPE_P (TREE_TYPE (*node))) | |
8522 | { | |
9b2d6d13 | 8523 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
8a8cdb8d | 8524 | *no_add_attrs = true; |
8525 | } | |
8526 | ||
8527 | return NULL_TREE; | |
8528 | } | |
bf6c8de0 | 8529 | |
8530 | /* Handle a "sentinel" attribute. */ | |
8531 | ||
8532 | static tree | |
50ca527f | 8533 | handle_sentinel_attribute (tree *node, tree name, tree args, |
bf6c8de0 | 8534 | int ARG_UNUSED (flags), bool *no_add_attrs) |
8535 | { | |
a36cf284 | 8536 | if (!prototype_p (*node)) |
bf6c8de0 | 8537 | { |
9b2d6d13 | 8538 | warning (OPT_Wattributes, |
8539 | "%qE attribute requires prototypes with named arguments", name); | |
bf6c8de0 | 8540 | *no_add_attrs = true; |
bf6c8de0 | 8541 | } |
50ca527f | 8542 | else |
8543 | { | |
c33080b9 | 8544 | if (!stdarg_p (*node)) |
a0c938f0 | 8545 | { |
9b2d6d13 | 8546 | warning (OPT_Wattributes, |
8547 | "%qE attribute only applies to variadic functions", name); | |
50ca527f | 8548 | *no_add_attrs = true; |
8549 | } | |
8550 | } | |
a0c938f0 | 8551 | |
50ca527f | 8552 | if (args) |
bf6c8de0 | 8553 | { |
50ca527f | 8554 | tree position = TREE_VALUE (args); |
8555 | ||
50ca527f | 8556 | if (TREE_CODE (position) != INTEGER_CST) |
a0c938f0 | 8557 | { |
48e1416a | 8558 | warning (OPT_Wattributes, |
01b54db5 | 8559 | "requested position is not an integer constant"); |
50ca527f | 8560 | *no_add_attrs = true; |
8561 | } | |
8562 | else | |
a0c938f0 | 8563 | { |
50ca527f | 8564 | if (tree_int_cst_lt (position, integer_zero_node)) |
8565 | { | |
01b54db5 | 8566 | warning (OPT_Wattributes, |
8567 | "requested position is less than zero"); | |
50ca527f | 8568 | *no_add_attrs = true; |
8569 | } | |
8570 | } | |
bf6c8de0 | 8571 | } |
a0c938f0 | 8572 | |
bf6c8de0 | 8573 | return NULL_TREE; |
8574 | } | |
b5c26b42 | 8575 | |
8576 | /* Handle a "type_generic" attribute. */ | |
8577 | ||
8578 | static tree | |
8579 | handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name), | |
8580 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
8581 | bool * ARG_UNUSED (no_add_attrs)) | |
8582 | { | |
19fbe3a4 | 8583 | /* Ensure we have a function type. */ |
8584 | gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE); | |
48e1416a | 8585 | |
19fbe3a4 | 8586 | /* Ensure we have a variadic function. */ |
c33080b9 | 8587 | gcc_assert (!prototype_p (*node) || stdarg_p (*node)); |
b5c26b42 | 8588 | |
8589 | return NULL_TREE; | |
8590 | } | |
46f8e3b0 | 8591 | |
24470055 | 8592 | /* Handle a "target" attribute. */ |
46f8e3b0 | 8593 | |
8594 | static tree | |
24470055 | 8595 | handle_target_attribute (tree *node, tree name, tree args, int flags, |
46f8e3b0 | 8596 | bool *no_add_attrs) |
8597 | { | |
8598 | /* Ensure we have a function type. */ | |
8599 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
8600 | { | |
8601 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8602 | *no_add_attrs = true; | |
8603 | } | |
46f8e3b0 | 8604 | else if (! targetm.target_option.valid_attribute_p (*node, name, args, |
8605 | flags)) | |
8606 | *no_add_attrs = true; | |
8607 | ||
8608 | return NULL_TREE; | |
8609 | } | |
8610 | ||
8611 | /* Arguments being collected for optimization. */ | |
8612 | typedef const char *const_char_p; /* For DEF_VEC_P. */ | |
8613 | DEF_VEC_P(const_char_p); | |
8614 | DEF_VEC_ALLOC_P(const_char_p, gc); | |
8615 | static GTY(()) VEC(const_char_p, gc) *optimize_args; | |
8616 | ||
8617 | ||
8618 | /* Inner function to convert a TREE_LIST to argv string to parse the optimize | |
8619 | options in ARGS. ATTR_P is true if this is for attribute(optimize), and | |
8620 | false for #pragma GCC optimize. */ | |
8621 | ||
8622 | bool | |
8623 | parse_optimize_options (tree args, bool attr_p) | |
8624 | { | |
8625 | bool ret = true; | |
8626 | unsigned opt_argc; | |
8627 | unsigned i; | |
2becf397 | 8628 | int saved_flag_strict_aliasing; |
46f8e3b0 | 8629 | const char **opt_argv; |
615ef0bb | 8630 | struct cl_decoded_option *decoded_options; |
8631 | unsigned int decoded_options_count; | |
46f8e3b0 | 8632 | tree ap; |
8633 | ||
8634 | /* Build up argv vector. Just in case the string is stored away, use garbage | |
8635 | collected strings. */ | |
8636 | VEC_truncate (const_char_p, optimize_args, 0); | |
e82e4eb5 | 8637 | VEC_safe_push (const_char_p, gc, optimize_args, NULL); |
46f8e3b0 | 8638 | |
8639 | for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap)) | |
8640 | { | |
8641 | tree value = TREE_VALUE (ap); | |
8642 | ||
8643 | if (TREE_CODE (value) == INTEGER_CST) | |
8644 | { | |
8645 | char buffer[20]; | |
8646 | sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value)); | |
8647 | VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer)); | |
8648 | } | |
8649 | ||
8650 | else if (TREE_CODE (value) == STRING_CST) | |
8651 | { | |
8652 | /* Split string into multiple substrings. */ | |
8653 | size_t len = TREE_STRING_LENGTH (value); | |
8654 | char *p = ASTRDUP (TREE_STRING_POINTER (value)); | |
8655 | char *end = p + len; | |
8656 | char *comma; | |
8657 | char *next_p = p; | |
8658 | ||
8659 | while (next_p != NULL) | |
8660 | { | |
8661 | size_t len2; | |
8662 | char *q, *r; | |
8663 | ||
8664 | p = next_p; | |
8665 | comma = strchr (p, ','); | |
8666 | if (comma) | |
8667 | { | |
8668 | len2 = comma - p; | |
8669 | *comma = '\0'; | |
8670 | next_p = comma+1; | |
8671 | } | |
8672 | else | |
8673 | { | |
8674 | len2 = end - p; | |
8675 | next_p = NULL; | |
8676 | } | |
8677 | ||
ba72912a | 8678 | r = q = (char *) ggc_alloc_atomic (len2 + 3); |
46f8e3b0 | 8679 | |
8680 | /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx | |
8681 | options. */ | |
8682 | if (*p == '-' && p[1] != 'O' && p[1] != 'f') | |
8683 | { | |
8684 | ret = false; | |
8685 | if (attr_p) | |
8686 | warning (OPT_Wattributes, | |
ebd7c4c1 | 8687 | "bad option %s to optimize attribute", p); |
46f8e3b0 | 8688 | else |
8689 | warning (OPT_Wpragmas, | |
e44b0a1f | 8690 | "bad option %s to pragma attribute", p); |
46f8e3b0 | 8691 | continue; |
8692 | } | |
8693 | ||
8694 | if (*p != '-') | |
8695 | { | |
8696 | *r++ = '-'; | |
8697 | ||
8698 | /* Assume that Ox is -Ox, a numeric value is -Ox, a s by | |
8699 | itself is -Os, and any other switch begins with a -f. */ | |
8700 | if ((*p >= '0' && *p <= '9') | |
8701 | || (p[0] == 's' && p[1] == '\0')) | |
8702 | *r++ = 'O'; | |
8703 | else if (*p != 'O') | |
8704 | *r++ = 'f'; | |
8705 | } | |
8706 | ||
8707 | memcpy (r, p, len2); | |
8708 | r[len2] = '\0'; | |
8709 | VEC_safe_push (const_char_p, gc, optimize_args, q); | |
8710 | } | |
8711 | ||
8712 | } | |
8713 | } | |
8714 | ||
8715 | opt_argc = VEC_length (const_char_p, optimize_args); | |
8716 | opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1)); | |
8717 | ||
8718 | for (i = 1; i < opt_argc; i++) | |
8719 | opt_argv[i] = VEC_index (const_char_p, optimize_args, i); | |
8720 | ||
2becf397 | 8721 | saved_flag_strict_aliasing = flag_strict_aliasing; |
8722 | ||
46f8e3b0 | 8723 | /* Now parse the options. */ |
f3f006ad | 8724 | decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv, |
8725 | &decoded_options, | |
8726 | &decoded_options_count); | |
8727 | decode_options (&global_options, &global_options_set, | |
3c6c0e40 | 8728 | decoded_options, decoded_options_count, |
8729 | input_location, global_dc); | |
46f8e3b0 | 8730 | |
4bec06b3 | 8731 | targetm.override_options_after_change(); |
8732 | ||
2becf397 | 8733 | /* Don't allow changing -fstrict-aliasing. */ |
8734 | flag_strict_aliasing = saved_flag_strict_aliasing; | |
8735 | ||
46f8e3b0 | 8736 | VEC_truncate (const_char_p, optimize_args, 0); |
8737 | return ret; | |
8738 | } | |
8739 | ||
8740 | /* For handling "optimize" attribute. arguments as in | |
8741 | struct attribute_spec.handler. */ | |
8742 | ||
8743 | static tree | |
8744 | handle_optimize_attribute (tree *node, tree name, tree args, | |
8745 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8746 | { | |
8747 | /* Ensure we have a function type. */ | |
8748 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
8749 | { | |
8750 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
8751 | *no_add_attrs = true; | |
8752 | } | |
8753 | else | |
8754 | { | |
8755 | struct cl_optimization cur_opts; | |
8756 | tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node); | |
8757 | ||
8758 | /* Save current options. */ | |
2c5d2e39 | 8759 | cl_optimization_save (&cur_opts, &global_options); |
46f8e3b0 | 8760 | |
8761 | /* If we previously had some optimization options, use them as the | |
8762 | default. */ | |
8763 | if (old_opts) | |
2c5d2e39 | 8764 | cl_optimization_restore (&global_options, |
8765 | TREE_OPTIMIZATION (old_opts)); | |
46f8e3b0 | 8766 | |
8767 | /* Parse options, and update the vector. */ | |
8768 | parse_optimize_options (args, true); | |
8769 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) | |
8770 | = build_optimization_node (); | |
8771 | ||
8772 | /* Restore current options. */ | |
2c5d2e39 | 8773 | cl_optimization_restore (&global_options, &cur_opts); |
46f8e3b0 | 8774 | } |
8775 | ||
8776 | return NULL_TREE; | |
8777 | } | |
48b14f50 | 8778 | |
8779 | /* Handle a "no_split_stack" attribute. */ | |
8780 | ||
8781 | static tree | |
8782 | handle_no_split_stack_attribute (tree *node, tree name, | |
8783 | tree ARG_UNUSED (args), | |
8784 | int ARG_UNUSED (flags), | |
8785 | bool *no_add_attrs) | |
8786 | { | |
8787 | tree decl = *node; | |
8788 | ||
8789 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
8790 | { | |
8791 | error_at (DECL_SOURCE_LOCATION (decl), | |
8792 | "%qE attribute applies only to functions", name); | |
8793 | *no_add_attrs = true; | |
8794 | } | |
8795 | else if (DECL_INITIAL (decl)) | |
8796 | { | |
8797 | error_at (DECL_SOURCE_LOCATION (decl), | |
8798 | "can%'t set %qE attribute after definition", name); | |
8799 | *no_add_attrs = true; | |
8800 | } | |
8801 | ||
8802 | return NULL_TREE; | |
8803 | } | |
dbf6c367 | 8804 | \f |
774e9d58 | 8805 | /* Check for valid arguments being passed to a function with FNTYPE. |
8806 | There are NARGS arguments in the array ARGARRAY. */ | |
dbf6c367 | 8807 | void |
774e9d58 | 8808 | check_function_arguments (const_tree fntype, int nargs, tree *argarray) |
dbf6c367 | 8809 | { |
8810 | /* Check for null being passed in a pointer argument that must be | |
8811 | non-null. We also need to do this if format checking is enabled. */ | |
8812 | ||
8813 | if (warn_nonnull) | |
774e9d58 | 8814 | check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
dbf6c367 | 8815 | |
8816 | /* Check for errors in format strings. */ | |
8817 | ||
068bea1e | 8818 | if (warn_format || warn_suggest_attribute_format) |
774e9d58 | 8819 | check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
95c90e04 | 8820 | |
8821 | if (warn_format) | |
774e9d58 | 8822 | check_function_sentinel (fntype, nargs, argarray); |
dbf6c367 | 8823 | } |
8824 | ||
8825 | /* Generic argument checking recursion routine. PARAM is the argument to | |
8826 | be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked | |
8827 | once the argument is resolved. CTX is context for the callback. */ | |
8828 | void | |
1cae46be | 8829 | check_function_arguments_recurse (void (*callback) |
8830 | (void *, tree, unsigned HOST_WIDE_INT), | |
8831 | void *ctx, tree param, | |
8832 | unsigned HOST_WIDE_INT param_num) | |
dbf6c367 | 8833 | { |
72dd6141 | 8834 | if (CONVERT_EXPR_P (param) |
c44afe23 | 8835 | && (TYPE_PRECISION (TREE_TYPE (param)) |
8836 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0))))) | |
dbf6c367 | 8837 | { |
8838 | /* Strip coercion. */ | |
8839 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 8840 | TREE_OPERAND (param, 0), param_num); |
dbf6c367 | 8841 | return; |
8842 | } | |
8843 | ||
8844 | if (TREE_CODE (param) == CALL_EXPR) | |
8845 | { | |
c2f47e15 | 8846 | tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param))); |
dbf6c367 | 8847 | tree attrs; |
8848 | bool found_format_arg = false; | |
8849 | ||
8850 | /* See if this is a call to a known internationalization function | |
8851 | that modifies a format arg. Such a function may have multiple | |
8852 | format_arg attributes (for example, ngettext). */ | |
8853 | ||
8854 | for (attrs = TYPE_ATTRIBUTES (type); | |
8855 | attrs; | |
8856 | attrs = TREE_CHAIN (attrs)) | |
8857 | if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs))) | |
8858 | { | |
c2f47e15 | 8859 | tree inner_arg; |
dbf6c367 | 8860 | tree format_num_expr; |
8861 | int format_num; | |
8862 | int i; | |
c2f47e15 | 8863 | call_expr_arg_iterator iter; |
dbf6c367 | 8864 | |
8865 | /* Extract the argument number, which was previously checked | |
8866 | to be valid. */ | |
8867 | format_num_expr = TREE_VALUE (TREE_VALUE (attrs)); | |
dbf6c367 | 8868 | |
231bd014 | 8869 | gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST |
8870 | && !TREE_INT_CST_HIGH (format_num_expr)); | |
dbf6c367 | 8871 | |
8872 | format_num = TREE_INT_CST_LOW (format_num_expr); | |
8873 | ||
c2f47e15 | 8874 | for (inner_arg = first_call_expr_arg (param, &iter), i = 1; |
8875 | inner_arg != 0; | |
8876 | inner_arg = next_call_expr_arg (&iter), i++) | |
dbf6c367 | 8877 | if (i == format_num) |
8878 | { | |
8879 | check_function_arguments_recurse (callback, ctx, | |
c2f47e15 | 8880 | inner_arg, param_num); |
dbf6c367 | 8881 | found_format_arg = true; |
8882 | break; | |
8883 | } | |
8884 | } | |
8885 | ||
8886 | /* If we found a format_arg attribute and did a recursive check, | |
8887 | we are done with checking this argument. Otherwise, we continue | |
8888 | and this will be considered a non-literal. */ | |
8889 | if (found_format_arg) | |
8890 | return; | |
8891 | } | |
8892 | ||
8893 | if (TREE_CODE (param) == COND_EXPR) | |
8894 | { | |
8895 | /* Check both halves of the conditional expression. */ | |
8896 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 8897 | TREE_OPERAND (param, 1), param_num); |
dbf6c367 | 8898 | check_function_arguments_recurse (callback, ctx, |
4ee9c684 | 8899 | TREE_OPERAND (param, 2), param_num); |
dbf6c367 | 8900 | return; |
8901 | } | |
8902 | ||
8903 | (*callback) (ctx, param, param_num); | |
8904 | } | |
1f3233d1 | 8905 | |
60cce472 | 8906 | /* Checks for a builtin function FNDECL that the number of arguments |
8907 | NARGS against the required number REQUIRED and issues an error if | |
8908 | there is a mismatch. Returns true if the number of arguments is | |
8909 | correct, otherwise false. */ | |
d43cee80 | 8910 | |
8911 | static bool | |
60cce472 | 8912 | builtin_function_validate_nargs (tree fndecl, int nargs, int required) |
d43cee80 | 8913 | { |
8914 | if (nargs < required) | |
8915 | { | |
60cce472 | 8916 | error_at (input_location, |
8917 | "not enough arguments to function %qE", fndecl); | |
d43cee80 | 8918 | return false; |
8919 | } | |
8920 | else if (nargs > required) | |
8921 | { | |
60cce472 | 8922 | error_at (input_location, |
8923 | "too many arguments to function %qE", fndecl); | |
d43cee80 | 8924 | return false; |
8925 | } | |
8926 | return true; | |
8927 | } | |
8928 | ||
8929 | /* Verifies the NARGS arguments ARGS to the builtin function FNDECL. | |
8930 | Returns false if there was an error, otherwise true. */ | |
8931 | ||
8932 | bool | |
8933 | check_builtin_function_arguments (tree fndecl, int nargs, tree *args) | |
8934 | { | |
8935 | if (!DECL_BUILT_IN (fndecl) | |
8936 | || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) | |
8937 | return true; | |
8938 | ||
8939 | switch (DECL_FUNCTION_CODE (fndecl)) | |
8940 | { | |
8941 | case BUILT_IN_CONSTANT_P: | |
60cce472 | 8942 | return builtin_function_validate_nargs (fndecl, nargs, 1); |
d43cee80 | 8943 | |
8944 | case BUILT_IN_ISFINITE: | |
8945 | case BUILT_IN_ISINF: | |
c319d56a | 8946 | case BUILT_IN_ISINF_SIGN: |
d43cee80 | 8947 | case BUILT_IN_ISNAN: |
8948 | case BUILT_IN_ISNORMAL: | |
60cce472 | 8949 | if (builtin_function_validate_nargs (fndecl, nargs, 1)) |
d43cee80 | 8950 | { |
8951 | if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE) | |
8952 | { | |
8953 | error ("non-floating-point argument in call to " | |
8954 | "function %qE", fndecl); | |
8955 | return false; | |
8956 | } | |
8957 | return true; | |
8958 | } | |
8959 | return false; | |
8960 | ||
8961 | case BUILT_IN_ISGREATER: | |
8962 | case BUILT_IN_ISGREATEREQUAL: | |
8963 | case BUILT_IN_ISLESS: | |
8964 | case BUILT_IN_ISLESSEQUAL: | |
8965 | case BUILT_IN_ISLESSGREATER: | |
8966 | case BUILT_IN_ISUNORDERED: | |
60cce472 | 8967 | if (builtin_function_validate_nargs (fndecl, nargs, 2)) |
d43cee80 | 8968 | { |
8969 | enum tree_code code0, code1; | |
8970 | code0 = TREE_CODE (TREE_TYPE (args[0])); | |
8971 | code1 = TREE_CODE (TREE_TYPE (args[1])); | |
8972 | if (!((code0 == REAL_TYPE && code1 == REAL_TYPE) | |
8973 | || (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
8974 | || (code0 == INTEGER_TYPE && code1 == REAL_TYPE))) | |
8975 | { | |
8976 | error ("non-floating-point arguments in call to " | |
8977 | "function %qE", fndecl); | |
8978 | return false; | |
8979 | } | |
8980 | return true; | |
8981 | } | |
8982 | return false; | |
8983 | ||
19fbe3a4 | 8984 | case BUILT_IN_FPCLASSIFY: |
60cce472 | 8985 | if (builtin_function_validate_nargs (fndecl, nargs, 6)) |
19fbe3a4 | 8986 | { |
8987 | unsigned i; | |
48e1416a | 8988 | |
19fbe3a4 | 8989 | for (i=0; i<5; i++) |
8990 | if (TREE_CODE (args[i]) != INTEGER_CST) | |
8991 | { | |
8992 | error ("non-const integer argument %u in call to function %qE", | |
8993 | i+1, fndecl); | |
8994 | return false; | |
8995 | } | |
8996 | ||
8997 | if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE) | |
8998 | { | |
8999 | error ("non-floating-point argument in call to function %qE", | |
9000 | fndecl); | |
9001 | return false; | |
9002 | } | |
9003 | return true; | |
9004 | } | |
9005 | return false; | |
9006 | ||
fca0886c | 9007 | case BUILT_IN_ASSUME_ALIGNED: |
9008 | if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2))) | |
9009 | { | |
9010 | if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE) | |
9011 | { | |
9012 | error ("non-integer argument 3 in call to function %qE", fndecl); | |
9013 | return false; | |
9014 | } | |
9015 | return true; | |
9016 | } | |
9017 | return false; | |
9018 | ||
d43cee80 | 9019 | default: |
9020 | return true; | |
9021 | } | |
9022 | } | |
9023 | ||
860251be | 9024 | /* Function to help qsort sort FIELD_DECLs by name order. */ |
9025 | ||
9026 | int | |
9027 | field_decl_cmp (const void *x_p, const void *y_p) | |
9028 | { | |
4fd61bc6 | 9029 | const tree *const x = (const tree *const) x_p; |
9030 | const tree *const y = (const tree *const) y_p; | |
9031 | ||
860251be | 9032 | if (DECL_NAME (*x) == DECL_NAME (*y)) |
9033 | /* A nontype is "greater" than a type. */ | |
9034 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
9035 | if (DECL_NAME (*x) == NULL_TREE) | |
9036 | return -1; | |
9037 | if (DECL_NAME (*y) == NULL_TREE) | |
9038 | return 1; | |
9039 | if (DECL_NAME (*x) < DECL_NAME (*y)) | |
9040 | return -1; | |
9041 | return 1; | |
9042 | } | |
9043 | ||
9044 | static struct { | |
9045 | gt_pointer_operator new_value; | |
9046 | void *cookie; | |
9047 | } resort_data; | |
9048 | ||
9049 | /* This routine compares two fields like field_decl_cmp but using the | |
9050 | pointer operator in resort_data. */ | |
9051 | ||
9052 | static int | |
9053 | resort_field_decl_cmp (const void *x_p, const void *y_p) | |
9054 | { | |
4fd61bc6 | 9055 | const tree *const x = (const tree *const) x_p; |
9056 | const tree *const y = (const tree *const) y_p; | |
860251be | 9057 | |
9058 | if (DECL_NAME (*x) == DECL_NAME (*y)) | |
9059 | /* A nontype is "greater" than a type. */ | |
9060 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
9061 | if (DECL_NAME (*x) == NULL_TREE) | |
9062 | return -1; | |
9063 | if (DECL_NAME (*y) == NULL_TREE) | |
9064 | return 1; | |
9065 | { | |
9066 | tree d1 = DECL_NAME (*x); | |
9067 | tree d2 = DECL_NAME (*y); | |
9068 | resort_data.new_value (&d1, resort_data.cookie); | |
9069 | resort_data.new_value (&d2, resort_data.cookie); | |
9070 | if (d1 < d2) | |
9071 | return -1; | |
9072 | } | |
9073 | return 1; | |
9074 | } | |
9075 | ||
9076 | /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ | |
9077 | ||
9078 | void | |
9079 | resort_sorted_fields (void *obj, | |
9a03a746 | 9080 | void * ARG_UNUSED (orig_obj), |
4ee9c684 | 9081 | gt_pointer_operator new_value, |
9082 | void *cookie) | |
860251be | 9083 | { |
9a03a746 | 9084 | struct sorted_fields_type *sf = (struct sorted_fields_type *) obj; |
860251be | 9085 | resort_data.new_value = new_value; |
9086 | resort_data.cookie = cookie; | |
9087 | qsort (&sf->elts[0], sf->len, sizeof (tree), | |
4ee9c684 | 9088 | resort_field_decl_cmp); |
860251be | 9089 | } |
9090 | ||
209c9752 | 9091 | /* Subroutine of c_parse_error. |
9092 | Return the result of concatenating LHS and RHS. RHS is really | |
9093 | a string literal, its first character is indicated by RHS_START and | |
cfee01e3 | 9094 | RHS_SIZE is its length (including the terminating NUL character). |
209c9752 | 9095 | |
9096 | The caller is responsible for deleting the returned pointer. */ | |
9097 | ||
9098 | static char * | |
9099 | catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) | |
9100 | { | |
9101 | const int lhs_size = strlen (lhs); | |
9102 | char *result = XNEWVEC (char, lhs_size + rhs_size); | |
9103 | strncpy (result, lhs, lhs_size); | |
9104 | strncpy (result + lhs_size, rhs_start, rhs_size); | |
9105 | return result; | |
9106 | } | |
9107 | ||
380c6697 | 9108 | /* Issue the error given by GMSGID, indicating that it occurred before |
92b128ed | 9109 | TOKEN, which had the associated VALUE. */ |
9110 | ||
9111 | void | |
48e1416a | 9112 | c_parse_error (const char *gmsgid, enum cpp_ttype token_type, |
ba99525e | 9113 | tree value, unsigned char token_flags) |
92b128ed | 9114 | { |
209c9752 | 9115 | #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2)) |
9116 | ||
9117 | char *message = NULL; | |
92b128ed | 9118 | |
ba99525e | 9119 | if (token_type == CPP_EOF) |
380c6697 | 9120 | message = catenate_messages (gmsgid, " at end of input"); |
48e1416a | 9121 | else if (token_type == CPP_CHAR |
9122 | || token_type == CPP_WCHAR | |
ba99525e | 9123 | || token_type == CPP_CHAR16 |
9124 | || token_type == CPP_CHAR32) | |
92b128ed | 9125 | { |
9126 | unsigned int val = TREE_INT_CST_LOW (value); | |
924bbf02 | 9127 | const char *prefix; |
9128 | ||
ba99525e | 9129 | switch (token_type) |
924bbf02 | 9130 | { |
9131 | default: | |
9132 | prefix = ""; | |
9133 | break; | |
9134 | case CPP_WCHAR: | |
9135 | prefix = "L"; | |
9136 | break; | |
9137 | case CPP_CHAR16: | |
9138 | prefix = "u"; | |
9139 | break; | |
9140 | case CPP_CHAR32: | |
9141 | prefix = "U"; | |
9142 | break; | |
9143 | } | |
9144 | ||
92b128ed | 9145 | if (val <= UCHAR_MAX && ISGRAPH (val)) |
a0c938f0 | 9146 | message = catenate_messages (gmsgid, " before %s'%c'"); |
92b128ed | 9147 | else |
a0c938f0 | 9148 | message = catenate_messages (gmsgid, " before %s'\\x%x'"); |
209c9752 | 9149 | |
924bbf02 | 9150 | error (message, prefix, val); |
209c9752 | 9151 | free (message); |
9152 | message = NULL; | |
92b128ed | 9153 | } |
48e1416a | 9154 | else if (token_type == CPP_STRING |
9155 | || token_type == CPP_WSTRING | |
ba99525e | 9156 | || token_type == CPP_STRING16 |
538ba11a | 9157 | || token_type == CPP_STRING32 |
9158 | || token_type == CPP_UTF8STRING) | |
380c6697 | 9159 | message = catenate_messages (gmsgid, " before string constant"); |
ba99525e | 9160 | else if (token_type == CPP_NUMBER) |
380c6697 | 9161 | message = catenate_messages (gmsgid, " before numeric constant"); |
ba99525e | 9162 | else if (token_type == CPP_NAME) |
209c9752 | 9163 | { |
380c6697 | 9164 | message = catenate_messages (gmsgid, " before %qE"); |
782858b8 | 9165 | error (message, value); |
209c9752 | 9166 | free (message); |
9167 | message = NULL; | |
9168 | } | |
ba99525e | 9169 | else if (token_type == CPP_PRAGMA) |
b75b98aa | 9170 | message = catenate_messages (gmsgid, " before %<#pragma%>"); |
ba99525e | 9171 | else if (token_type == CPP_PRAGMA_EOL) |
b75b98aa | 9172 | message = catenate_messages (gmsgid, " before end of line"); |
07b8f133 | 9173 | else if (token_type == CPP_DECLTYPE) |
9174 | message = catenate_messages (gmsgid, " before %<decltype%>"); | |
ba99525e | 9175 | else if (token_type < N_TTYPES) |
209c9752 | 9176 | { |
380c6697 | 9177 | message = catenate_messages (gmsgid, " before %qs token"); |
ba99525e | 9178 | error (message, cpp_type2name (token_type, token_flags)); |
209c9752 | 9179 | free (message); |
9180 | message = NULL; | |
9181 | } | |
92b128ed | 9182 | else |
380c6697 | 9183 | error (gmsgid); |
209c9752 | 9184 | |
9185 | if (message) | |
9186 | { | |
9187 | error (message); | |
9188 | free (message); | |
9189 | } | |
a0c938f0 | 9190 | #undef catenate_messages |
92b128ed | 9191 | } |
9192 | ||
3a79f5da | 9193 | /* Mapping for cpp message reasons to the options that enable them. */ |
9194 | ||
9195 | struct reason_option_codes_t | |
9196 | { | |
9197 | const int reason; /* cpplib message reason. */ | |
9198 | const int option_code; /* gcc option that controls this message. */ | |
9199 | }; | |
9200 | ||
9201 | static const struct reason_option_codes_t option_codes[] = { | |
9202 | {CPP_W_DEPRECATED, OPT_Wdeprecated}, | |
67089c6b | 9203 | {CPP_W_COMMENTS, OPT_Wcomment}, |
3a79f5da | 9204 | {CPP_W_TRIGRAPHS, OPT_Wtrigraphs}, |
9205 | {CPP_W_MULTICHAR, OPT_Wmultichar}, | |
9206 | {CPP_W_TRADITIONAL, OPT_Wtraditional}, | |
9207 | {CPP_W_LONG_LONG, OPT_Wlong_long}, | |
9208 | {CPP_W_ENDIF_LABELS, OPT_Wendif_labels}, | |
9209 | {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros}, | |
9210 | {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined}, | |
9211 | {CPP_W_UNDEF, OPT_Wundef}, | |
9212 | {CPP_W_UNUSED_MACROS, OPT_Wunused_macros}, | |
9213 | {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat}, | |
9214 | {CPP_W_NORMALIZE, OPT_Wnormalized_}, | |
9215 | {CPP_W_INVALID_PCH, OPT_Winvalid_pch}, | |
9216 | {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp}, | |
76d340ac | 9217 | {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix}, |
3a79f5da | 9218 | {CPP_W_NONE, 0} |
9219 | }; | |
9220 | ||
9221 | /* Return the gcc option code associated with the reason for a cpp | |
9222 | message, or 0 if none. */ | |
9223 | ||
9224 | static int | |
9225 | c_option_controlling_cpp_error (int reason) | |
9226 | { | |
9227 | const struct reason_option_codes_t *entry; | |
9228 | ||
9229 | for (entry = option_codes; entry->reason != CPP_W_NONE; entry++) | |
9230 | { | |
9231 | if (entry->reason == reason) | |
9232 | return entry->option_code; | |
9233 | } | |
9234 | return 0; | |
9235 | } | |
9236 | ||
7f5f3953 | 9237 | /* Callback from cpp_error for PFILE to print diagnostics from the |
3a79f5da | 9238 | preprocessor. The diagnostic is of type LEVEL, with REASON set |
9239 | to the reason code if LEVEL is represents a warning, at location | |
7f5f3953 | 9240 | LOCATION unless this is after lexing and the compiler's location |
9241 | should be used instead, with column number possibly overridden by | |
9242 | COLUMN_OVERRIDE if not zero; MSG is the translated message and AP | |
9243 | the arguments. Returns true if a diagnostic was emitted, false | |
9244 | otherwise. */ | |
9245 | ||
9246 | bool | |
3a79f5da | 9247 | c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason, |
7f5f3953 | 9248 | location_t location, unsigned int column_override, |
9249 | const char *msg, va_list *ap) | |
9250 | { | |
9251 | diagnostic_info diagnostic; | |
9252 | diagnostic_t dlevel; | |
5ae82d58 | 9253 | bool save_warn_system_headers = global_dc->dc_warn_system_headers; |
7f5f3953 | 9254 | bool ret; |
9255 | ||
9256 | switch (level) | |
9257 | { | |
9258 | case CPP_DL_WARNING_SYSHDR: | |
9259 | if (flag_no_output) | |
9260 | return false; | |
5ae82d58 | 9261 | global_dc->dc_warn_system_headers = 1; |
7f5f3953 | 9262 | /* Fall through. */ |
9263 | case CPP_DL_WARNING: | |
9264 | if (flag_no_output) | |
9265 | return false; | |
9266 | dlevel = DK_WARNING; | |
9267 | break; | |
9268 | case CPP_DL_PEDWARN: | |
9269 | if (flag_no_output && !flag_pedantic_errors) | |
9270 | return false; | |
9271 | dlevel = DK_PEDWARN; | |
9272 | break; | |
9273 | case CPP_DL_ERROR: | |
9274 | dlevel = DK_ERROR; | |
9275 | break; | |
9276 | case CPP_DL_ICE: | |
9277 | dlevel = DK_ICE; | |
9278 | break; | |
9279 | case CPP_DL_NOTE: | |
9280 | dlevel = DK_NOTE; | |
9281 | break; | |
ff903809 | 9282 | case CPP_DL_FATAL: |
9283 | dlevel = DK_FATAL; | |
9284 | break; | |
7f5f3953 | 9285 | default: |
9286 | gcc_unreachable (); | |
9287 | } | |
9288 | if (done_lexing) | |
9289 | location = input_location; | |
9290 | diagnostic_set_info_translated (&diagnostic, msg, ap, | |
9291 | location, dlevel); | |
9292 | if (column_override) | |
9293 | diagnostic_override_column (&diagnostic, column_override); | |
3a79f5da | 9294 | diagnostic_override_option_index (&diagnostic, |
9295 | c_option_controlling_cpp_error (reason)); | |
7f5f3953 | 9296 | ret = report_diagnostic (&diagnostic); |
9297 | if (level == CPP_DL_WARNING_SYSHDR) | |
5ae82d58 | 9298 | global_dc->dc_warn_system_headers = save_warn_system_headers; |
7f5f3953 | 9299 | return ret; |
9300 | } | |
9301 | ||
624d37a6 | 9302 | /* Convert a character from the host to the target execution character |
9303 | set. cpplib handles this, mostly. */ | |
9304 | ||
9305 | HOST_WIDE_INT | |
9306 | c_common_to_target_charset (HOST_WIDE_INT c) | |
9307 | { | |
9308 | /* Character constants in GCC proper are sign-extended under -fsigned-char, | |
9309 | zero-extended under -fno-signed-char. cpplib insists that characters | |
9310 | and character constants are always unsigned. Hence we must convert | |
9311 | back and forth. */ | |
9312 | cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1); | |
9313 | ||
9314 | uc = cpp_host_to_exec_charset (parse_in, uc); | |
9315 | ||
9316 | if (flag_signed_char) | |
9317 | return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE) | |
9318 | >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE); | |
9319 | else | |
9320 | return uc; | |
9321 | } | |
9322 | ||
7549df0d | 9323 | /* Fold an offsetof-like expression. EXPR is a nested sequence of component |
9324 | references with an INDIRECT_REF of a constant at the bottom; much like the | |
9325 | traditional rendering of offsetof as a macro. Return the folded result. */ | |
af28855b | 9326 | |
7549df0d | 9327 | tree |
9328 | fold_offsetof_1 (tree expr) | |
af28855b | 9329 | { |
af28855b | 9330 | tree base, off, t; |
9331 | ||
9332 | switch (TREE_CODE (expr)) | |
9333 | { | |
9334 | case ERROR_MARK: | |
9335 | return expr; | |
9336 | ||
6b11d2e3 | 9337 | case VAR_DECL: |
9338 | error ("cannot apply %<offsetof%> to static data member %qD", expr); | |
9339 | return error_mark_node; | |
9340 | ||
d897f7c2 | 9341 | case CALL_EXPR: |
cf1a89a3 | 9342 | case TARGET_EXPR: |
d897f7c2 | 9343 | error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded"); |
9344 | return error_mark_node; | |
9345 | ||
d897f7c2 | 9346 | case NOP_EXPR: |
9347 | case INDIRECT_REF: | |
7549df0d | 9348 | if (!TREE_CONSTANT (TREE_OPERAND (expr, 0))) |
64ed018c | 9349 | { |
9350 | error ("cannot apply %<offsetof%> to a non constant address"); | |
9351 | return error_mark_node; | |
9352 | } | |
7549df0d | 9353 | return TREE_OPERAND (expr, 0); |
d897f7c2 | 9354 | |
af28855b | 9355 | case COMPONENT_REF: |
7549df0d | 9356 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); |
af28855b | 9357 | if (base == error_mark_node) |
9358 | return base; | |
9359 | ||
9360 | t = TREE_OPERAND (expr, 1); | |
9361 | if (DECL_C_BIT_FIELD (t)) | |
9362 | { | |
9363 | error ("attempt to take address of bit-field structure " | |
782858b8 | 9364 | "member %qD", t); |
af28855b | 9365 | return error_mark_node; |
9366 | } | |
389dd41b | 9367 | off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t), |
9368 | size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t), | |
9369 | 1) | |
9370 | / BITS_PER_UNIT)); | |
af28855b | 9371 | break; |
9372 | ||
9373 | case ARRAY_REF: | |
7549df0d | 9374 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); |
af28855b | 9375 | if (base == error_mark_node) |
9376 | return base; | |
9377 | ||
9378 | t = TREE_OPERAND (expr, 1); | |
64ed018c | 9379 | |
9380 | /* Check if the offset goes beyond the upper bound of the array. */ | |
7549df0d | 9381 | if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0) |
e0559d69 | 9382 | { |
9383 | tree upbound = array_ref_up_bound (expr); | |
9384 | if (upbound != NULL_TREE | |
9385 | && TREE_CODE (upbound) == INTEGER_CST | |
9386 | && !tree_int_cst_equal (upbound, | |
9387 | TYPE_MAX_VALUE (TREE_TYPE (upbound)))) | |
9388 | { | |
9389 | upbound = size_binop (PLUS_EXPR, upbound, | |
9390 | build_int_cst (TREE_TYPE (upbound), 1)); | |
9391 | if (tree_int_cst_lt (upbound, t)) | |
9392 | { | |
9393 | tree v; | |
9394 | ||
9395 | for (v = TREE_OPERAND (expr, 0); | |
9396 | TREE_CODE (v) == COMPONENT_REF; | |
9397 | v = TREE_OPERAND (v, 0)) | |
9398 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) | |
9399 | == RECORD_TYPE) | |
9400 | { | |
1767a056 | 9401 | tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); |
9402 | for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) | |
e0559d69 | 9403 | if (TREE_CODE (fld_chain) == FIELD_DECL) |
9404 | break; | |
9405 | ||
9406 | if (fld_chain) | |
9407 | break; | |
9408 | } | |
9409 | /* Don't warn if the array might be considered a poor | |
9410 | man's flexible array member with a very permissive | |
9411 | definition thereof. */ | |
9412 | if (TREE_CODE (v) == ARRAY_REF | |
9413 | || TREE_CODE (v) == COMPONENT_REF) | |
9414 | warning (OPT_Warray_bounds, | |
9415 | "index %E denotes an offset " | |
9416 | "greater than size of %qT", | |
9417 | t, TREE_TYPE (TREE_OPERAND (expr, 0))); | |
9418 | } | |
9419 | } | |
9420 | } | |
7549df0d | 9421 | |
9422 | t = convert (sizetype, t); | |
9423 | off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); | |
af28855b | 9424 | break; |
9425 | ||
ede90cc2 | 9426 | case COMPOUND_EXPR: |
9427 | /* Handle static members of volatile structs. */ | |
9428 | t = TREE_OPERAND (expr, 1); | |
9429 | gcc_assert (TREE_CODE (t) == VAR_DECL); | |
7549df0d | 9430 | return fold_offsetof_1 (t); |
ede90cc2 | 9431 | |
af28855b | 9432 | default: |
231bd014 | 9433 | gcc_unreachable (); |
af28855b | 9434 | } |
9435 | ||
7549df0d | 9436 | return fold_build_pointer_plus (base, off); |
af28855b | 9437 | } |
9438 | ||
7549df0d | 9439 | /* Likewise, but convert it to the return type of offsetof. */ |
9440 | ||
af28855b | 9441 | tree |
7549df0d | 9442 | fold_offsetof (tree expr) |
af28855b | 9443 | { |
7549df0d | 9444 | return convert (size_type_node, fold_offsetof_1 (expr)); |
af28855b | 9445 | } |
9446 | ||
b9bdfa0b | 9447 | /* Warn for A ?: C expressions (with B omitted) where A is a boolean |
9448 | expression, because B will always be true. */ | |
9449 | ||
9450 | void | |
9451 | warn_for_omitted_condop (location_t location, tree cond) | |
9452 | { | |
9453 | if (truth_value_p (TREE_CODE (cond))) | |
9454 | warning_at (location, OPT_Wparentheses, | |
9455 | "the omitted middle operand in ?: will always be %<true%>, " | |
9456 | "suggest explicit middle operand"); | |
9457 | } | |
9458 | ||
a1f90215 | 9459 | /* Give an error for storing into ARG, which is 'const'. USE indicates |
9460 | how ARG was being used. */ | |
9461 | ||
9462 | void | |
9463 | readonly_error (tree arg, enum lvalue_use use) | |
9464 | { | |
9465 | gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement | |
9466 | || use == lv_asm); | |
9467 | /* Using this macro rather than (for example) arrays of messages | |
9468 | ensures that all the format strings are checked at compile | |
9469 | time. */ | |
9470 | #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ | |
9471 | : (use == lv_increment ? (I) \ | |
9472 | : (use == lv_decrement ? (D) : (AS)))) | |
9473 | if (TREE_CODE (arg) == COMPONENT_REF) | |
9474 | { | |
9475 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
9476 | error (READONLY_MSG (G_("assignment of member " | |
9477 | "%qD in read-only object"), | |
9478 | G_("increment of member " | |
9479 | "%qD in read-only object"), | |
9480 | G_("decrement of member " | |
9481 | "%qD in read-only object"), | |
9482 | G_("member %qD in read-only object " | |
9483 | "used as %<asm%> output")), | |
9484 | TREE_OPERAND (arg, 1)); | |
9485 | else | |
9486 | error (READONLY_MSG (G_("assignment of read-only member %qD"), | |
9487 | G_("increment of read-only member %qD"), | |
9488 | G_("decrement of read-only member %qD"), | |
9489 | G_("read-only member %qD used as %<asm%> output")), | |
9490 | TREE_OPERAND (arg, 1)); | |
9491 | } | |
9492 | else if (TREE_CODE (arg) == VAR_DECL) | |
9493 | error (READONLY_MSG (G_("assignment of read-only variable %qD"), | |
9494 | G_("increment of read-only variable %qD"), | |
9495 | G_("decrement of read-only variable %qD"), | |
9496 | G_("read-only variable %qD used as %<asm%> output")), | |
9497 | arg); | |
9498 | else if (TREE_CODE (arg) == PARM_DECL) | |
9499 | error (READONLY_MSG (G_("assignment of read-only parameter %qD"), | |
9500 | G_("increment of read-only parameter %qD"), | |
9501 | G_("decrement of read-only parameter %qD"), | |
9502 | G_("read-only parameter %qD use as %<asm%> output")), | |
9503 | arg); | |
9504 | else if (TREE_CODE (arg) == RESULT_DECL) | |
9505 | { | |
9506 | gcc_assert (c_dialect_cxx ()); | |
9507 | error (READONLY_MSG (G_("assignment of " | |
9508 | "read-only named return value %qD"), | |
9509 | G_("increment of " | |
9510 | "read-only named return value %qD"), | |
9511 | G_("decrement of " | |
9512 | "read-only named return value %qD"), | |
9513 | G_("read-only named return value %qD " | |
9514 | "used as %<asm%>output")), | |
9515 | arg); | |
9516 | } | |
9517 | else if (TREE_CODE (arg) == FUNCTION_DECL) | |
9518 | error (READONLY_MSG (G_("assignment of function %qD"), | |
9519 | G_("increment of function %qD"), | |
9520 | G_("decrement of function %qD"), | |
9521 | G_("function %qD used as %<asm%> output")), | |
9522 | arg); | |
9523 | else | |
9524 | error (READONLY_MSG (G_("assignment of read-only location %qE"), | |
9525 | G_("increment of read-only location %qE"), | |
9526 | G_("decrement of read-only location %qE"), | |
9527 | G_("read-only location %qE used as %<asm%> output")), | |
9528 | arg); | |
9529 | } | |
9530 | ||
e35976b1 | 9531 | /* Print an error message for an invalid lvalue. USE says |
fdd84b77 | 9532 | how the lvalue is being used and so selects the error message. LOC |
9533 | is the location for the error. */ | |
ab6bb714 | 9534 | |
e35976b1 | 9535 | void |
fdd84b77 | 9536 | lvalue_error (location_t loc, enum lvalue_use use) |
ab6bb714 | 9537 | { |
e35976b1 | 9538 | switch (use) |
ab6bb714 | 9539 | { |
e35976b1 | 9540 | case lv_assign: |
fdd84b77 | 9541 | error_at (loc, "lvalue required as left operand of assignment"); |
e35976b1 | 9542 | break; |
9543 | case lv_increment: | |
fdd84b77 | 9544 | error_at (loc, "lvalue required as increment operand"); |
e35976b1 | 9545 | break; |
9546 | case lv_decrement: | |
fdd84b77 | 9547 | error_at (loc, "lvalue required as decrement operand"); |
e35976b1 | 9548 | break; |
9549 | case lv_addressof: | |
fdd84b77 | 9550 | error_at (loc, "lvalue required as unary %<&%> operand"); |
e35976b1 | 9551 | break; |
9552 | case lv_asm: | |
fdd84b77 | 9553 | error_at (loc, "lvalue required in asm statement"); |
e35976b1 | 9554 | break; |
9555 | default: | |
9556 | gcc_unreachable (); | |
ab6bb714 | 9557 | } |
ab6bb714 | 9558 | } |
b1bbc8e5 | 9559 | |
9560 | /* Print an error message for an invalid indirection of type TYPE. | |
9561 | ERRSTRING is the name of the operator for the indirection. */ | |
9562 | ||
9563 | void | |
9564 | invalid_indirection_error (location_t loc, tree type, ref_operator errstring) | |
9565 | { | |
9566 | switch (errstring) | |
9567 | { | |
9568 | case RO_NULL: | |
9569 | gcc_assert (c_dialect_cxx ()); | |
9570 | error_at (loc, "invalid type argument (have %qT)", type); | |
9571 | break; | |
9572 | case RO_ARRAY_INDEXING: | |
9573 | error_at (loc, | |
9574 | "invalid type argument of array indexing (have %qT)", | |
9575 | type); | |
9576 | break; | |
9577 | case RO_UNARY_STAR: | |
9578 | error_at (loc, | |
9579 | "invalid type argument of unary %<*%> (have %qT)", | |
9580 | type); | |
9581 | break; | |
9582 | case RO_ARROW: | |
9583 | error_at (loc, | |
9584 | "invalid type argument of %<->%> (have %qT)", | |
9585 | type); | |
9586 | break; | |
9587 | case RO_IMPLICIT_CONVERSION: | |
9588 | error_at (loc, | |
9589 | "invalid type argument of implicit conversion (have %qT)", | |
9590 | type); | |
9591 | break; | |
9592 | default: | |
9593 | gcc_unreachable (); | |
9594 | } | |
9595 | } | |
c271bdb2 | 9596 | \f |
9597 | /* *PTYPE is an incomplete array. Complete it with a domain based on | |
9598 | INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT | |
9599 | is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
9600 | 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ | |
9601 | ||
9602 | int | |
9603 | complete_array_type (tree *ptype, tree initial_value, bool do_default) | |
9604 | { | |
9605 | tree maxindex, type, main_type, elt, unqual_elt; | |
9606 | int failure = 0, quals; | |
6753bca0 | 9607 | hashval_t hashcode = 0; |
c271bdb2 | 9608 | |
9609 | maxindex = size_zero_node; | |
9610 | if (initial_value) | |
9611 | { | |
9612 | if (TREE_CODE (initial_value) == STRING_CST) | |
9613 | { | |
9614 | int eltsize | |
9615 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
9616 | maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1); | |
9617 | } | |
9618 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) | |
9619 | { | |
c75b4594 | 9620 | VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value); |
c271bdb2 | 9621 | |
c75b4594 | 9622 | if (VEC_empty (constructor_elt, v)) |
c271bdb2 | 9623 | { |
9624 | if (pedantic) | |
9625 | failure = 3; | |
7542c3b4 | 9626 | maxindex = ssize_int (-1); |
c271bdb2 | 9627 | } |
9628 | else | |
9629 | { | |
9630 | tree curindex; | |
c75b4594 | 9631 | unsigned HOST_WIDE_INT cnt; |
9632 | constructor_elt *ce; | |
cee43f7e | 9633 | bool fold_p = false; |
c271bdb2 | 9634 | |
2b15d2ba | 9635 | if (VEC_index (constructor_elt, v, 0).index) |
389dd41b | 9636 | maxindex = fold_convert_loc (input_location, sizetype, |
9637 | VEC_index (constructor_elt, | |
2b15d2ba | 9638 | v, 0).index); |
c271bdb2 | 9639 | curindex = maxindex; |
9640 | ||
c75b4594 | 9641 | for (cnt = 1; |
9642 | VEC_iterate (constructor_elt, v, cnt, ce); | |
9643 | cnt++) | |
c271bdb2 | 9644 | { |
cee43f7e | 9645 | bool curfold_p = false; |
c75b4594 | 9646 | if (ce->index) |
cee43f7e | 9647 | curindex = ce->index, curfold_p = true; |
c271bdb2 | 9648 | else |
cee43f7e | 9649 | { |
9650 | if (fold_p) | |
9651 | curindex = fold_convert (sizetype, curindex); | |
389dd41b | 9652 | curindex = size_binop (PLUS_EXPR, curindex, |
9653 | size_one_node); | |
cee43f7e | 9654 | } |
c271bdb2 | 9655 | if (tree_int_cst_lt (maxindex, curindex)) |
cee43f7e | 9656 | maxindex = curindex, fold_p = curfold_p; |
c271bdb2 | 9657 | } |
cee43f7e | 9658 | if (fold_p) |
9659 | maxindex = fold_convert (sizetype, maxindex); | |
c271bdb2 | 9660 | } |
9661 | } | |
9662 | else | |
9663 | { | |
9664 | /* Make an error message unless that happened already. */ | |
9665 | if (initial_value != error_mark_node) | |
9666 | failure = 1; | |
9667 | } | |
9668 | } | |
9669 | else | |
9670 | { | |
9671 | failure = 2; | |
9672 | if (!do_default) | |
9673 | return failure; | |
9674 | } | |
9675 | ||
9676 | type = *ptype; | |
9677 | elt = TREE_TYPE (type); | |
9678 | quals = TYPE_QUALS (strip_array_types (elt)); | |
9679 | if (quals == 0) | |
9680 | unqual_elt = elt; | |
9681 | else | |
6d5d708e | 9682 | unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals)); |
c271bdb2 | 9683 | |
9684 | /* Using build_distinct_type_copy and modifying things afterward instead | |
9685 | of using build_array_type to create a new type preserves all of the | |
9686 | TYPE_LANG_FLAG_? bits that the front end may have set. */ | |
9687 | main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); | |
9688 | TREE_TYPE (main_type) = unqual_elt; | |
783bb57e | 9689 | TYPE_DOMAIN (main_type) |
9690 | = build_range_type (TREE_TYPE (maxindex), | |
9691 | build_int_cst (TREE_TYPE (maxindex), 0), maxindex); | |
c271bdb2 | 9692 | layout_type (main_type); |
9693 | ||
6753bca0 | 9694 | /* Make sure we have the canonical MAIN_TYPE. */ |
9695 | hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode); | |
48e1416a | 9696 | hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)), |
6753bca0 | 9697 | hashcode); |
9698 | main_type = type_hash_canon (hashcode, main_type); | |
9699 | ||
796735dc | 9700 | /* Fix the canonical type. */ |
9701 | if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type)) | |
9702 | || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type))) | |
9703 | SET_TYPE_STRUCTURAL_EQUALITY (main_type); | |
9704 | else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type) | |
9705 | || (TYPE_CANONICAL (TYPE_DOMAIN (main_type)) | |
9706 | != TYPE_DOMAIN (main_type))) | |
48e1416a | 9707 | TYPE_CANONICAL (main_type) |
796735dc | 9708 | = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)), |
9709 | TYPE_CANONICAL (TYPE_DOMAIN (main_type))); | |
9710 | else | |
9711 | TYPE_CANONICAL (main_type) = main_type; | |
9712 | ||
c271bdb2 | 9713 | if (quals == 0) |
9714 | type = main_type; | |
9715 | else | |
9716 | type = c_build_qualified_type (main_type, quals); | |
9717 | ||
4f5b8f2a | 9718 | if (COMPLETE_TYPE_P (type) |
9719 | && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST | |
9720 | && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) | |
9721 | { | |
9722 | error ("size of array is too large"); | |
9723 | /* If we proceed with the array type as it is, we'll eventually | |
9724 | crash in tree_low_cst(). */ | |
9725 | type = error_mark_node; | |
9726 | } | |
9727 | ||
c271bdb2 | 9728 | *ptype = type; |
9729 | return failure; | |
9730 | } | |
ab6bb714 | 9731 | |
93426222 | 9732 | /* Like c_mark_addressable but don't check register qualifier. */ |
9733 | void | |
9734 | c_common_mark_addressable_vec (tree t) | |
9735 | { | |
9736 | while (handled_component_p (t)) | |
9737 | t = TREE_OPERAND (t, 0); | |
9738 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
9739 | return; | |
9740 | TREE_ADDRESSABLE (t) = 1; | |
9741 | } | |
9742 | ||
9743 | ||
b6a5fc45 | 9744 | \f |
9745 | /* Used to help initialize the builtin-types.def table. When a type of | |
9746 | the correct size doesn't exist, use error_mark_node instead of NULL. | |
9747 | The later results in segfaults even when a decl using the type doesn't | |
9748 | get invoked. */ | |
9749 | ||
9750 | tree | |
9751 | builtin_type_for_size (int size, bool unsignedp) | |
9752 | { | |
a51edb4c | 9753 | tree type = c_common_type_for_size (size, unsignedp); |
b6a5fc45 | 9754 | return type ? type : error_mark_node; |
9755 | } | |
9756 | ||
9757 | /* A helper function for resolve_overloaded_builtin in resolving the | |
9758 | overloaded __sync_ builtins. Returns a positive power of 2 if the | |
9759 | first operand of PARAMS is a pointer to a supported data type. | |
9760 | Returns 0 if an error is encountered. */ | |
9761 | ||
9762 | static int | |
b9c74b4d | 9763 | sync_resolve_size (tree function, VEC(tree,gc) *params) |
b6a5fc45 | 9764 | { |
9765 | tree type; | |
9766 | int size; | |
9767 | ||
b9c74b4d | 9768 | if (VEC_empty (tree, params)) |
b6a5fc45 | 9769 | { |
9770 | error ("too few arguments to function %qE", function); | |
9771 | return 0; | |
9772 | } | |
9773 | ||
b9c74b4d | 9774 | type = TREE_TYPE (VEC_index (tree, params, 0)); |
b6a5fc45 | 9775 | if (TREE_CODE (type) != POINTER_TYPE) |
9776 | goto incompatible; | |
9777 | ||
9778 | type = TREE_TYPE (type); | |
9779 | if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) | |
9780 | goto incompatible; | |
9781 | ||
9782 | size = tree_low_cst (TYPE_SIZE_UNIT (type), 1); | |
27213ba3 | 9783 | if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16) |
b6a5fc45 | 9784 | return size; |
9785 | ||
9786 | incompatible: | |
9787 | error ("incompatible type for argument %d of %qE", 1, function); | |
9788 | return 0; | |
9789 | } | |
9790 | ||
a0c938f0 | 9791 | /* A helper function for resolve_overloaded_builtin. Adds casts to |
b6a5fc45 | 9792 | PARAMS to make arguments match up with those of FUNCTION. Drops |
9793 | the variadic arguments at the end. Returns false if some error | |
9794 | was encountered; true on success. */ | |
9795 | ||
9796 | static bool | |
1cd6e20d | 9797 | sync_resolve_params (location_t loc, tree orig_function, tree function, |
9798 | VEC(tree, gc) *params, bool orig_format) | |
b6a5fc45 | 9799 | { |
d0af78c5 | 9800 | function_args_iterator iter; |
b6a5fc45 | 9801 | tree ptype; |
b9c74b4d | 9802 | unsigned int parmnum; |
b6a5fc45 | 9803 | |
d0af78c5 | 9804 | function_args_iter_init (&iter, TREE_TYPE (function)); |
b6a5fc45 | 9805 | /* We've declared the implementation functions to use "volatile void *" |
9806 | as the pointer parameter, so we shouldn't get any complaints from the | |
9807 | call to check_function_arguments what ever type the user used. */ | |
d0af78c5 | 9808 | function_args_iter_next (&iter); |
b9c74b4d | 9809 | ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0))); |
b6a5fc45 | 9810 | |
9811 | /* For the rest of the values, we need to cast these to FTYPE, so that we | |
9812 | don't get warnings for passing pointer types, etc. */ | |
b9c74b4d | 9813 | parmnum = 0; |
d0af78c5 | 9814 | while (1) |
b6a5fc45 | 9815 | { |
d0af78c5 | 9816 | tree val, arg_type; |
9817 | ||
9818 | arg_type = function_args_iter_cond (&iter); | |
9819 | /* XXX void_type_node belies the abstraction. */ | |
9820 | if (arg_type == void_type_node) | |
9821 | break; | |
b6a5fc45 | 9822 | |
b9c74b4d | 9823 | ++parmnum; |
9824 | if (VEC_length (tree, params) <= parmnum) | |
b6a5fc45 | 9825 | { |
1cd6e20d | 9826 | error_at (loc, "too few arguments to function %qE", orig_function); |
b6a5fc45 | 9827 | return false; |
9828 | } | |
9829 | ||
0f6a7cb7 | 9830 | /* Only convert parameters if arg_type is unsigned integer type with |
9831 | new format sync routines, i.e. don't attempt to convert pointer | |
9832 | arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n), | |
9833 | bool arguments (e.g. WEAK argument) or signed int arguments (memmodel | |
9834 | kinds). */ | |
9835 | if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type)) | |
1cd6e20d | 9836 | { |
9837 | /* Ideally for the first conversion we'd use convert_for_assignment | |
9838 | so that we get warnings for anything that doesn't match the pointer | |
9839 | type. This isn't portable across the C and C++ front ends atm. */ | |
9840 | val = VEC_index (tree, params, parmnum); | |
9841 | val = convert (ptype, val); | |
9842 | val = convert (arg_type, val); | |
9843 | VEC_replace (tree, params, parmnum, val); | |
9844 | } | |
b6a5fc45 | 9845 | |
d0af78c5 | 9846 | function_args_iter_next (&iter); |
b6a5fc45 | 9847 | } |
9848 | ||
1cd6e20d | 9849 | /* __atomic routines are not variadic. */ |
9850 | if (!orig_format && VEC_length (tree, params) != parmnum + 1) | |
9851 | { | |
9852 | error_at (loc, "too many arguments to function %qE", orig_function); | |
9853 | return false; | |
9854 | } | |
9855 | ||
b6a5fc45 | 9856 | /* The definition of these primitives is variadic, with the remaining |
9857 | being "an optional list of variables protected by the memory barrier". | |
9858 | No clue what that's supposed to mean, precisely, but we consider all | |
9859 | call-clobbered variables to be protected so we're safe. */ | |
b9c74b4d | 9860 | VEC_truncate (tree, params, parmnum + 1); |
b6a5fc45 | 9861 | |
9862 | return true; | |
9863 | } | |
9864 | ||
a0c938f0 | 9865 | /* A helper function for resolve_overloaded_builtin. Adds a cast to |
b6a5fc45 | 9866 | RESULT to make it match the type of the first pointer argument in |
9867 | PARAMS. */ | |
9868 | ||
9869 | static tree | |
1cd6e20d | 9870 | sync_resolve_return (tree first_param, tree result, bool orig_format) |
b6a5fc45 | 9871 | { |
b9c74b4d | 9872 | tree ptype = TREE_TYPE (TREE_TYPE (first_param)); |
1cd6e20d | 9873 | tree rtype = TREE_TYPE (result); |
10080eac | 9874 | ptype = TYPE_MAIN_VARIANT (ptype); |
1cd6e20d | 9875 | |
9876 | /* New format doesn't require casting unless the types are the same size. */ | |
9877 | if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype))) | |
9878 | return convert (ptype, result); | |
9879 | else | |
9880 | return result; | |
9881 | } | |
9882 | ||
9883 | /* This function verifies the PARAMS to generic atomic FUNCTION. | |
9884 | It returns the size if all the parameters are the same size, otherwise | |
9885 | 0 is returned if the parameters are invalid. */ | |
9886 | ||
9887 | static int | |
9888 | get_atomic_generic_size (location_t loc, tree function, VEC(tree,gc) *params) | |
9889 | { | |
9890 | unsigned int n_param; | |
9891 | unsigned int n_model; | |
9892 | unsigned int x; | |
9893 | int size_0; | |
9894 | tree type_0; | |
9895 | ||
9896 | /* Determine the parameter makeup. */ | |
9897 | switch (DECL_FUNCTION_CODE (function)) | |
9898 | { | |
9899 | case BUILT_IN_ATOMIC_EXCHANGE: | |
9900 | n_param = 4; | |
9901 | n_model = 1; | |
9902 | break; | |
9903 | case BUILT_IN_ATOMIC_LOAD: | |
9904 | case BUILT_IN_ATOMIC_STORE: | |
9905 | n_param = 3; | |
9906 | n_model = 1; | |
9907 | break; | |
9908 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
9909 | n_param = 6; | |
9910 | n_model = 2; | |
9911 | break; | |
9912 | default: | |
1d581089 | 9913 | gcc_unreachable (); |
1cd6e20d | 9914 | } |
9915 | ||
9916 | if (VEC_length (tree, params) != n_param) | |
9917 | { | |
9918 | error_at (loc, "incorrect number of arguments to function %qE", function); | |
9919 | return 0; | |
9920 | } | |
9921 | ||
9922 | /* Get type of first parameter, and determine its size. */ | |
9923 | type_0 = TREE_TYPE (VEC_index (tree, params, 0)); | |
1d581089 | 9924 | if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0))) |
9925 | { | |
9926 | error_at (loc, "argument 1 of %qE must be a non-void pointer type", | |
9927 | function); | |
9928 | return 0; | |
9929 | } | |
9930 | ||
9931 | /* Types must be compile time constant sizes. */ | |
9932 | if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST) | |
1cd6e20d | 9933 | { |
1d581089 | 9934 | error_at (loc, |
9935 | "argument 1 of %qE must be a pointer to a constant size type", | |
9936 | function); | |
1cd6e20d | 9937 | return 0; |
9938 | } | |
1d581089 | 9939 | |
1cd6e20d | 9940 | size_0 = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type_0)), 1); |
9941 | ||
1d581089 | 9942 | /* Zero size objects are not allowed. */ |
9943 | if (size_0 == 0) | |
9944 | { | |
9945 | error_at (loc, | |
9946 | "argument 1 of %qE must be a pointer to a nonzero size object", | |
9947 | function); | |
9948 | return 0; | |
9949 | } | |
9950 | ||
1cd6e20d | 9951 | /* Check each other parameter is a pointer and the same size. */ |
9952 | for (x = 0; x < n_param - n_model; x++) | |
9953 | { | |
9954 | int size; | |
9955 | tree type = TREE_TYPE (VEC_index (tree, params, x)); | |
9956 | /* __atomic_compare_exchange has a bool in the 4th postion, skip it. */ | |
9957 | if (n_param == 6 && x == 3) | |
9958 | continue; | |
9959 | if (!POINTER_TYPE_P (type)) | |
9960 | { | |
9961 | error_at (loc, "argument %d of %qE must be a pointer type", x + 1, | |
9962 | function); | |
9963 | return 0; | |
9964 | } | |
9965 | size = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (type)), 1); | |
9966 | if (size != size_0) | |
9967 | { | |
9968 | error_at (loc, "size mismatch in argument %d of %qE", x + 1, | |
9969 | function); | |
9970 | return 0; | |
9971 | } | |
9972 | } | |
9973 | ||
9974 | /* Check memory model parameters for validity. */ | |
9975 | for (x = n_param - n_model ; x < n_param; x++) | |
9976 | { | |
9977 | tree p = VEC_index (tree, params, x); | |
9978 | if (TREE_CODE (p) == INTEGER_CST) | |
9979 | { | |
9980 | int i = tree_low_cst (p, 1); | |
9981 | if (i < 0 || i >= MEMMODEL_LAST) | |
9982 | { | |
9983 | warning_at (loc, OPT_Winvalid_memory_model, | |
9984 | "invalid memory model argument %d of %qE", x + 1, | |
9985 | function); | |
1cd6e20d | 9986 | } |
9987 | } | |
9988 | else | |
9989 | if (!INTEGRAL_TYPE_P (TREE_TYPE (p))) | |
9990 | { | |
9991 | error_at (loc, "non-integer memory model argument %d of %qE", x + 1, | |
9992 | function); | |
9993 | return 0; | |
9994 | } | |
9995 | } | |
9996 | ||
9997 | return size_0; | |
9998 | } | |
9999 | ||
10000 | ||
10001 | /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N | |
10002 | at the beginning of the parameter list PARAMS representing the size of the | |
10003 | objects. This is to match the library ABI requirement. LOC is the location | |
10004 | of the function call. | |
10005 | The new function is returned if it needed rebuilding, otherwise NULL_TREE is | |
10006 | returned to allow the external call to be constructed. */ | |
10007 | ||
10008 | static tree | |
10009 | add_atomic_size_parameter (unsigned n, location_t loc, tree function, | |
10010 | VEC(tree,gc) *params) | |
10011 | { | |
10012 | tree size_node; | |
10013 | ||
10014 | /* Insert a SIZE_T parameter as the first param. If there isn't | |
10015 | enough space, allocate a new vector and recursively re-build with that. */ | |
10016 | if (!VEC_space (tree, params, 1)) | |
10017 | { | |
10018 | unsigned int z, len; | |
10019 | VEC(tree,gc) *vec; | |
10020 | tree f; | |
10021 | ||
10022 | len = VEC_length (tree, params); | |
10023 | vec = VEC_alloc (tree, gc, len + 1); | |
10024 | for (z = 0; z < len; z++) | |
10025 | VEC_quick_push (tree, vec, VEC_index (tree, params, z)); | |
10026 | f = build_function_call_vec (loc, function, vec, NULL); | |
10027 | VEC_free (tree, gc, vec); | |
10028 | return f; | |
10029 | } | |
10030 | ||
10031 | /* Add the size parameter and leave as a function call for processing. */ | |
10032 | size_node = build_int_cst (size_type_node, n); | |
10033 | VEC_quick_insert (tree, params, 0, size_node); | |
10034 | return NULL_TREE; | |
10035 | } | |
10036 | ||
10037 | ||
10038 | /* This will process an __atomic_exchange function call, determine whether it | |
10039 | needs to be mapped to the _N variation, or turned into a library call. | |
10040 | LOC is the location of the builtin call. | |
10041 | FUNCTION is the DECL that has been invoked; | |
10042 | PARAMS is the argument list for the call. The return value is non-null | |
10043 | TRUE is returned if it is translated into the proper format for a call to the | |
10044 | external library, and NEW_RETURN is set the tree for that function. | |
10045 | FALSE is returned if processing for the _N variation is required, and | |
10046 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10047 | static bool | |
10048 | resolve_overloaded_atomic_exchange (location_t loc, tree function, | |
10049 | VEC(tree,gc) *params, tree *new_return) | |
10050 | { | |
10051 | tree p0, p1, p2, p3; | |
10052 | tree I_type, I_type_ptr; | |
10053 | int n = get_atomic_generic_size (loc, function, params); | |
10054 | ||
1d581089 | 10055 | /* Size of 0 is an error condition. */ |
10056 | if (n == 0) | |
10057 | { | |
10058 | *new_return = error_mark_node; | |
10059 | return true; | |
10060 | } | |
10061 | ||
1cd6e20d | 10062 | /* If not a lock-free size, change to the library generic format. */ |
10063 | if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16) | |
10064 | { | |
10065 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10066 | return true; | |
10067 | } | |
10068 | ||
10069 | /* Otherwise there is a lockfree match, transform the call from: | |
10070 | void fn(T* mem, T* desired, T* return, model) | |
10071 | into | |
10072 | *return = (T) (fn (In* mem, (In) *desired, model)) */ | |
10073 | ||
10074 | p0 = VEC_index (tree, params, 0); | |
10075 | p1 = VEC_index (tree, params, 1); | |
10076 | p2 = VEC_index (tree, params, 2); | |
10077 | p3 = VEC_index (tree, params, 3); | |
10078 | ||
10079 | /* Create pointer to appropriate size. */ | |
10080 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10081 | I_type_ptr = build_pointer_type (I_type); | |
10082 | ||
10083 | /* Convert object pointer to required type. */ | |
10084 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
1d581089 | 10085 | VEC_replace (tree, params, 0, p0); |
1cd6e20d | 10086 | /* Convert new value to required type, and dereference it. */ |
10087 | p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10088 | p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); | |
10089 | VEC_replace (tree, params, 1, p1); | |
10090 | ||
10091 | /* Move memory model to the 3rd position, and end param list. */ | |
10092 | VEC_replace (tree, params, 2, p3); | |
10093 | VEC_truncate (tree, params, 3); | |
10094 | ||
10095 | /* Convert return pointer and dereference it for later assignment. */ | |
10096 | *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR); | |
10097 | ||
10098 | return false; | |
b6a5fc45 | 10099 | } |
10100 | ||
1cd6e20d | 10101 | |
10102 | /* This will process an __atomic_compare_exchange function call, determine | |
10103 | whether it needs to be mapped to the _N variation, or turned into a lib call. | |
10104 | LOC is the location of the builtin call. | |
10105 | FUNCTION is the DECL that has been invoked; | |
10106 | PARAMS is the argument list for the call. The return value is non-null | |
10107 | TRUE is returned if it is translated into the proper format for a call to the | |
10108 | external library, and NEW_RETURN is set the tree for that function. | |
10109 | FALSE is returned if processing for the _N variation is required. */ | |
10110 | ||
10111 | static bool | |
10112 | resolve_overloaded_atomic_compare_exchange (location_t loc, tree function, | |
10113 | VEC(tree,gc) *params, | |
10114 | tree *new_return) | |
10115 | { | |
10116 | tree p0, p1, p2; | |
10117 | tree I_type, I_type_ptr; | |
10118 | int n = get_atomic_generic_size (loc, function, params); | |
10119 | ||
1d581089 | 10120 | /* Size of 0 is an error condition. */ |
10121 | if (n == 0) | |
10122 | { | |
10123 | *new_return = error_mark_node; | |
10124 | return true; | |
10125 | } | |
10126 | ||
1cd6e20d | 10127 | /* If not a lock-free size, change to the library generic format. */ |
10128 | if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16) | |
10129 | { | |
10130 | /* The library generic format does not have the weak parameter, so | |
10131 | remove it from the param list. Since a parameter has been removed, | |
10132 | we can be sure that there is room for the SIZE_T parameter, meaning | |
10133 | there will not be a recursive rebuilding of the parameter list, so | |
10134 | there is no danger this will be done twice. */ | |
10135 | if (n > 0) | |
10136 | { | |
10137 | VEC_replace (tree, params, 3, VEC_index (tree, params, 4)); | |
10138 | VEC_replace (tree, params, 4, VEC_index (tree, params, 5)); | |
10139 | VEC_truncate (tree, params, 5); | |
10140 | } | |
10141 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10142 | return true; | |
10143 | } | |
10144 | ||
10145 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10146 | bool fn(T* mem, T* desired, T* return, weak, success, failure) | |
10147 | into | |
10148 | bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */ | |
10149 | ||
10150 | p0 = VEC_index (tree, params, 0); | |
10151 | p1 = VEC_index (tree, params, 1); | |
10152 | p2 = VEC_index (tree, params, 2); | |
10153 | ||
10154 | /* Create pointer to appropriate size. */ | |
10155 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10156 | I_type_ptr = build_pointer_type (I_type); | |
10157 | ||
10158 | /* Convert object pointer to required type. */ | |
10159 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
10160 | VEC_replace (tree, params, 0, p0); | |
10161 | ||
10162 | /* Convert expected pointer to required type. */ | |
10163 | p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1); | |
10164 | VEC_replace (tree, params, 1, p1); | |
10165 | ||
10166 | /* Convert desired value to required type, and dereference it. */ | |
10167 | p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR); | |
10168 | p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2); | |
10169 | VEC_replace (tree, params, 2, p2); | |
10170 | ||
10171 | /* The rest of the parameters are fine. NULL means no special return value | |
10172 | processing.*/ | |
10173 | *new_return = NULL; | |
10174 | return false; | |
10175 | } | |
10176 | ||
10177 | ||
10178 | /* This will process an __atomic_load function call, determine whether it | |
10179 | needs to be mapped to the _N variation, or turned into a library call. | |
10180 | LOC is the location of the builtin call. | |
10181 | FUNCTION is the DECL that has been invoked; | |
10182 | PARAMS is the argument list for the call. The return value is non-null | |
10183 | TRUE is returned if it is translated into the proper format for a call to the | |
10184 | external library, and NEW_RETURN is set the tree for that function. | |
10185 | FALSE is returned if processing for the _N variation is required, and | |
10186 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10187 | ||
10188 | static bool | |
10189 | resolve_overloaded_atomic_load (location_t loc, tree function, | |
10190 | VEC(tree,gc) *params, tree *new_return) | |
10191 | { | |
10192 | tree p0, p1, p2; | |
10193 | tree I_type, I_type_ptr; | |
10194 | int n = get_atomic_generic_size (loc, function, params); | |
10195 | ||
1d581089 | 10196 | /* Size of 0 is an error condition. */ |
10197 | if (n == 0) | |
10198 | { | |
10199 | *new_return = error_mark_node; | |
10200 | return true; | |
10201 | } | |
10202 | ||
1cd6e20d | 10203 | /* If not a lock-free size, change to the library generic format. */ |
10204 | if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16) | |
10205 | { | |
10206 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10207 | return true; | |
10208 | } | |
10209 | ||
10210 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10211 | void fn(T* mem, T* return, model) | |
10212 | into | |
10213 | *return = (T) (fn ((In *) mem, model)) */ | |
10214 | ||
10215 | p0 = VEC_index (tree, params, 0); | |
10216 | p1 = VEC_index (tree, params, 1); | |
10217 | p2 = VEC_index (tree, params, 2); | |
10218 | ||
10219 | /* Create pointer to appropriate size. */ | |
10220 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10221 | I_type_ptr = build_pointer_type (I_type); | |
10222 | ||
10223 | /* Convert object pointer to required type. */ | |
10224 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
10225 | VEC_replace (tree, params, 0, p0); | |
10226 | ||
10227 | /* Move memory model to the 2nd position, and end param list. */ | |
10228 | VEC_replace (tree, params, 1, p2); | |
10229 | VEC_truncate (tree, params, 2); | |
10230 | ||
10231 | /* Convert return pointer and dereference it for later assignment. */ | |
10232 | *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10233 | ||
10234 | return false; | |
10235 | } | |
10236 | ||
10237 | ||
10238 | /* This will process an __atomic_store function call, determine whether it | |
10239 | needs to be mapped to the _N variation, or turned into a library call. | |
10240 | LOC is the location of the builtin call. | |
10241 | FUNCTION is the DECL that has been invoked; | |
10242 | PARAMS is the argument list for the call. The return value is non-null | |
10243 | TRUE is returned if it is translated into the proper format for a call to the | |
10244 | external library, and NEW_RETURN is set the tree for that function. | |
10245 | FALSE is returned if processing for the _N variation is required, and | |
10246 | NEW_RETURN is set to the the return value the result is copied into. */ | |
10247 | ||
10248 | static bool | |
10249 | resolve_overloaded_atomic_store (location_t loc, tree function, | |
10250 | VEC(tree,gc) *params, tree *new_return) | |
10251 | { | |
10252 | tree p0, p1; | |
10253 | tree I_type, I_type_ptr; | |
10254 | int n = get_atomic_generic_size (loc, function, params); | |
10255 | ||
1d581089 | 10256 | /* Size of 0 is an error condition. */ |
10257 | if (n == 0) | |
10258 | { | |
10259 | *new_return = error_mark_node; | |
10260 | return true; | |
10261 | } | |
10262 | ||
1cd6e20d | 10263 | /* If not a lock-free size, change to the library generic format. */ |
10264 | if (n != 1 && n != 2 && n != 4 && n != 8 && n != 16) | |
10265 | { | |
10266 | *new_return = add_atomic_size_parameter (n, loc, function, params); | |
10267 | return true; | |
10268 | } | |
10269 | ||
10270 | /* Otherwise, there is a match, so the call needs to be transformed from: | |
10271 | void fn(T* mem, T* value, model) | |
10272 | into | |
10273 | fn ((In *) mem, (In) *value, model) */ | |
10274 | ||
10275 | p0 = VEC_index (tree, params, 0); | |
10276 | p1 = VEC_index (tree, params, 1); | |
10277 | ||
10278 | /* Create pointer to appropriate size. */ | |
10279 | I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1); | |
10280 | I_type_ptr = build_pointer_type (I_type); | |
10281 | ||
10282 | /* Convert object pointer to required type. */ | |
10283 | p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); | |
10284 | VEC_replace (tree, params, 0, p0); | |
10285 | ||
10286 | /* Convert new value to required type, and dereference it. */ | |
10287 | p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); | |
10288 | p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); | |
10289 | VEC_replace (tree, params, 1, p1); | |
10290 | ||
10291 | /* The memory model is in the right spot already. Return is void. */ | |
10292 | *new_return = NULL_TREE; | |
10293 | ||
10294 | return false; | |
10295 | } | |
10296 | ||
10297 | ||
b6a5fc45 | 10298 | /* Some builtin functions are placeholders for other expressions. This |
10299 | function should be called immediately after parsing the call expression | |
10300 | before surrounding code has committed to the type of the expression. | |
10301 | ||
e60a6f7b | 10302 | LOC is the location of the builtin call. |
10303 | ||
b6a5fc45 | 10304 | FUNCTION is the DECL that has been invoked; it is known to be a builtin. |
10305 | PARAMS is the argument list for the call. The return value is non-null | |
10306 | when expansion is complete, and null if normal processing should | |
10307 | continue. */ | |
10308 | ||
10309 | tree | |
e60a6f7b | 10310 | resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params) |
b6a5fc45 | 10311 | { |
10312 | enum built_in_function orig_code = DECL_FUNCTION_CODE (function); | |
1cd6e20d | 10313 | bool orig_format = true; |
10314 | tree new_return = NULL_TREE; | |
10315 | ||
65441f6f | 10316 | switch (DECL_BUILT_IN_CLASS (function)) |
10317 | { | |
10318 | case BUILT_IN_NORMAL: | |
10319 | break; | |
10320 | case BUILT_IN_MD: | |
10321 | if (targetm.resolve_overloaded_builtin) | |
e60a6f7b | 10322 | return targetm.resolve_overloaded_builtin (loc, function, params); |
65441f6f | 10323 | else |
a0c938f0 | 10324 | return NULL_TREE; |
65441f6f | 10325 | default: |
10326 | return NULL_TREE; | |
10327 | } | |
a0c938f0 | 10328 | |
65441f6f | 10329 | /* Handle BUILT_IN_NORMAL here. */ |
b6a5fc45 | 10330 | switch (orig_code) |
10331 | { | |
1cd6e20d | 10332 | case BUILT_IN_ATOMIC_EXCHANGE: |
10333 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
10334 | case BUILT_IN_ATOMIC_LOAD: | |
10335 | case BUILT_IN_ATOMIC_STORE: | |
10336 | { | |
10337 | /* Handle these 4 together so that they can fall through to the next | |
10338 | case if the call is transformed to an _N variant. */ | |
10339 | switch (orig_code) | |
10340 | { | |
10341 | case BUILT_IN_ATOMIC_EXCHANGE: | |
10342 | { | |
10343 | if (resolve_overloaded_atomic_exchange (loc, function, params, | |
10344 | &new_return)) | |
10345 | return new_return; | |
10346 | /* Change to the _N variant. */ | |
10347 | orig_code = BUILT_IN_ATOMIC_EXCHANGE_N; | |
10348 | break; | |
10349 | } | |
10350 | ||
10351 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | |
10352 | { | |
10353 | if (resolve_overloaded_atomic_compare_exchange (loc, function, | |
10354 | params, | |
10355 | &new_return)) | |
10356 | return new_return; | |
10357 | /* Change to the _N variant. */ | |
10358 | orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N; | |
10359 | break; | |
10360 | } | |
10361 | case BUILT_IN_ATOMIC_LOAD: | |
10362 | { | |
10363 | if (resolve_overloaded_atomic_load (loc, function, params, | |
10364 | &new_return)) | |
10365 | return new_return; | |
10366 | /* Change to the _N variant. */ | |
10367 | orig_code = BUILT_IN_ATOMIC_LOAD_N; | |
10368 | break; | |
10369 | } | |
10370 | case BUILT_IN_ATOMIC_STORE: | |
10371 | { | |
10372 | if (resolve_overloaded_atomic_store (loc, function, params, | |
10373 | &new_return)) | |
10374 | return new_return; | |
10375 | /* Change to the _N variant. */ | |
10376 | orig_code = BUILT_IN_ATOMIC_STORE_N; | |
10377 | break; | |
10378 | } | |
10379 | default: | |
10380 | gcc_unreachable (); | |
10381 | } | |
10382 | /* Fallthrough to the normal processing. */ | |
10383 | } | |
10384 | case BUILT_IN_ATOMIC_EXCHANGE_N: | |
10385 | case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N: | |
10386 | case BUILT_IN_ATOMIC_LOAD_N: | |
10387 | case BUILT_IN_ATOMIC_STORE_N: | |
10388 | case BUILT_IN_ATOMIC_ADD_FETCH_N: | |
10389 | case BUILT_IN_ATOMIC_SUB_FETCH_N: | |
10390 | case BUILT_IN_ATOMIC_AND_FETCH_N: | |
10391 | case BUILT_IN_ATOMIC_NAND_FETCH_N: | |
10392 | case BUILT_IN_ATOMIC_XOR_FETCH_N: | |
10393 | case BUILT_IN_ATOMIC_OR_FETCH_N: | |
10394 | case BUILT_IN_ATOMIC_FETCH_ADD_N: | |
10395 | case BUILT_IN_ATOMIC_FETCH_SUB_N: | |
10396 | case BUILT_IN_ATOMIC_FETCH_AND_N: | |
10397 | case BUILT_IN_ATOMIC_FETCH_NAND_N: | |
10398 | case BUILT_IN_ATOMIC_FETCH_XOR_N: | |
10399 | case BUILT_IN_ATOMIC_FETCH_OR_N: | |
10400 | { | |
10401 | orig_format = false; | |
10402 | /* Fallthru for parameter processing. */ | |
10403 | } | |
2797f13a | 10404 | case BUILT_IN_SYNC_FETCH_AND_ADD_N: |
10405 | case BUILT_IN_SYNC_FETCH_AND_SUB_N: | |
10406 | case BUILT_IN_SYNC_FETCH_AND_OR_N: | |
10407 | case BUILT_IN_SYNC_FETCH_AND_AND_N: | |
10408 | case BUILT_IN_SYNC_FETCH_AND_XOR_N: | |
10409 | case BUILT_IN_SYNC_FETCH_AND_NAND_N: | |
10410 | case BUILT_IN_SYNC_ADD_AND_FETCH_N: | |
10411 | case BUILT_IN_SYNC_SUB_AND_FETCH_N: | |
10412 | case BUILT_IN_SYNC_OR_AND_FETCH_N: | |
10413 | case BUILT_IN_SYNC_AND_AND_FETCH_N: | |
10414 | case BUILT_IN_SYNC_XOR_AND_FETCH_N: | |
10415 | case BUILT_IN_SYNC_NAND_AND_FETCH_N: | |
10416 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N: | |
10417 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N: | |
10418 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N: | |
10419 | case BUILT_IN_SYNC_LOCK_RELEASE_N: | |
b6a5fc45 | 10420 | { |
10421 | int n = sync_resolve_size (function, params); | |
b9c74b4d | 10422 | tree new_function, first_param, result; |
b9a16870 | 10423 | enum built_in_function fncode; |
b6a5fc45 | 10424 | |
10425 | if (n == 0) | |
10426 | return error_mark_node; | |
10427 | ||
b9a16870 | 10428 | fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1); |
10429 | new_function = builtin_decl_explicit (fncode); | |
1cd6e20d | 10430 | if (!sync_resolve_params (loc, function, new_function, params, |
10431 | orig_format)) | |
b6a5fc45 | 10432 | return error_mark_node; |
10433 | ||
b9c74b4d | 10434 | first_param = VEC_index (tree, params, 0); |
e60a6f7b | 10435 | result = build_function_call_vec (loc, new_function, params, NULL); |
1cd6e20d | 10436 | if (result == error_mark_node) |
10437 | return result; | |
2797f13a | 10438 | if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N |
1cd6e20d | 10439 | && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N |
10440 | && orig_code != BUILT_IN_ATOMIC_STORE_N) | |
10441 | result = sync_resolve_return (first_param, result, orig_format); | |
b6a5fc45 | 10442 | |
1cd6e20d | 10443 | /* If new_return is set, assign function to that expr and cast the |
10444 | result to void since the generic interface returned void. */ | |
10445 | if (new_return) | |
10446 | { | |
10447 | /* Cast function result from I{1,2,4,8,16} to the required type. */ | |
10448 | result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result); | |
10449 | result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return, | |
10450 | result); | |
10451 | TREE_SIDE_EFFECTS (result) = 1; | |
10452 | protected_set_expr_location (result, loc); | |
10453 | result = convert (void_type_node, result); | |
10454 | } | |
b6a5fc45 | 10455 | return result; |
10456 | } | |
10457 | ||
10458 | default: | |
65441f6f | 10459 | return NULL_TREE; |
b6a5fc45 | 10460 | } |
10461 | } | |
10462 | ||
491255f5 | 10463 | /* Ignoring their sign, return true if two scalar types are the same. */ |
10464 | bool | |
10465 | same_scalar_type_ignoring_signedness (tree t1, tree t2) | |
10466 | { | |
10467 | enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); | |
10468 | ||
9421ebb9 | 10469 | gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE) |
10470 | && (c2 == INTEGER_TYPE || c2 == REAL_TYPE | |
10471 | || c2 == FIXED_POINT_TYPE)); | |
491255f5 | 10472 | |
10473 | /* Equality works here because c_common_signed_type uses | |
10474 | TYPE_MAIN_VARIANT. */ | |
11773141 | 10475 | return c_common_signed_type (t1) |
10476 | == c_common_signed_type (t2); | |
491255f5 | 10477 | } |
10478 | ||
be7350e7 | 10479 | /* Check for missing format attributes on function pointers. LTYPE is |
10480 | the new type or left-hand side type. RTYPE is the old type or | |
10481 | right-hand side type. Returns TRUE if LTYPE is missing the desired | |
10482 | attribute. */ | |
10483 | ||
10484 | bool | |
10485 | check_missing_format_attribute (tree ltype, tree rtype) | |
10486 | { | |
10487 | tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype); | |
10488 | tree ra; | |
10489 | ||
10490 | for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra)) | |
10491 | if (is_attribute_p ("format", TREE_PURPOSE (ra))) | |
10492 | break; | |
10493 | if (ra) | |
10494 | { | |
10495 | tree la; | |
10496 | for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la)) | |
10497 | if (is_attribute_p ("format", TREE_PURPOSE (la))) | |
10498 | break; | |
10499 | return !la; | |
10500 | } | |
10501 | else | |
10502 | return false; | |
10503 | } | |
10504 | ||
2840aae4 | 10505 | /* Subscripting with type char is likely to lose on a machine where |
10506 | chars are signed. So warn on any machine, but optionally. Don't | |
10507 | warn for unsigned char since that type is safe. Don't warn for | |
10508 | signed char because anyone who uses that must have done so | |
10509 | deliberately. Furthermore, we reduce the false positive load by | |
10510 | warning only for non-constant value of type char. */ | |
10511 | ||
10512 | void | |
10513 | warn_array_subscript_with_type_char (tree index) | |
10514 | { | |
10515 | if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node | |
10516 | && TREE_CODE (index) != INTEGER_CST) | |
10517 | warning (OPT_Wchar_subscripts, "array subscript has type %<char%>"); | |
10518 | } | |
10519 | ||
e534436e | 10520 | /* Implement -Wparentheses for the unexpected C precedence rules, to |
10521 | cover cases like x + y << z which readers are likely to | |
10522 | misinterpret. We have seen an expression in which CODE is a binary | |
82012ffe | 10523 | operator used to combine expressions ARG_LEFT and ARG_RIGHT, which |
10524 | before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and | |
10525 | CODE_RIGHT may be ERROR_MARK, which means that that side of the | |
10526 | expression was not formed using a binary or unary operator, or it | |
10527 | was enclosed in parentheses. */ | |
e534436e | 10528 | |
10529 | void | |
b0e7825e | 10530 | warn_about_parentheses (location_t loc, enum tree_code code, |
269f7979 | 10531 | enum tree_code code_left, tree arg_left, |
82012ffe | 10532 | enum tree_code code_right, tree arg_right) |
e534436e | 10533 | { |
10534 | if (!warn_parentheses) | |
10535 | return; | |
10536 | ||
82012ffe | 10537 | /* This macro tests that the expression ARG with original tree code |
10538 | CODE appears to be a boolean expression. or the result of folding a | |
10539 | boolean expression. */ | |
10540 | #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ | |
10541 | (truth_value_p (TREE_CODE (ARG)) \ | |
10542 | || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ | |
10543 | /* Folding may create 0 or 1 integers from other expressions. */ \ | |
10544 | || ((CODE) != INTEGER_CST \ | |
10545 | && (integer_onep (ARG) || integer_zerop (ARG)))) | |
10546 | ||
48e1416a | 10547 | switch (code) |
e534436e | 10548 | { |
82012ffe | 10549 | case LSHIFT_EXPR: |
b0e7825e | 10550 | if (code_left == PLUS_EXPR) |
10551 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10552 | "suggest parentheses around %<+%> inside %<<<%>"); | |
10553 | else if (code_right == PLUS_EXPR) | |
10554 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10555 | "suggest parentheses around %<+%> inside %<<<%>"); | |
10556 | else if (code_left == MINUS_EXPR) | |
10557 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10558 | "suggest parentheses around %<-%> inside %<<<%>"); | |
10559 | else if (code_right == MINUS_EXPR) | |
10560 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10561 | "suggest parentheses around %<-%> inside %<<<%>"); | |
82012ffe | 10562 | return; |
e534436e | 10563 | |
82012ffe | 10564 | case RSHIFT_EXPR: |
b0e7825e | 10565 | if (code_left == PLUS_EXPR) |
10566 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10567 | "suggest parentheses around %<+%> inside %<>>%>"); | |
10568 | else if (code_right == PLUS_EXPR) | |
10569 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10570 | "suggest parentheses around %<+%> inside %<>>%>"); | |
10571 | else if (code_left == MINUS_EXPR) | |
10572 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10573 | "suggest parentheses around %<-%> inside %<>>%>"); | |
10574 | else if (code_right == MINUS_EXPR) | |
10575 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10576 | "suggest parentheses around %<-%> inside %<>>%>"); | |
82012ffe | 10577 | return; |
e534436e | 10578 | |
82012ffe | 10579 | case TRUTH_ORIF_EXPR: |
b0e7825e | 10580 | if (code_left == TRUTH_ANDIF_EXPR) |
10581 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10582 | "suggest parentheses around %<&&%> within %<||%>"); | |
10583 | else if (code_right == TRUTH_ANDIF_EXPR) | |
10584 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10585 | "suggest parentheses around %<&&%> within %<||%>"); | |
82012ffe | 10586 | return; |
10587 | ||
10588 | case BIT_IOR_EXPR: | |
e534436e | 10589 | if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR |
b0e7825e | 10590 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
10591 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10592 | "suggest parentheses around arithmetic in operand of %<|%>"); | |
10593 | else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR | |
10594 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
10595 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 10596 | "suggest parentheses around arithmetic in operand of %<|%>"); |
e534436e | 10597 | /* Check cases like x|y==z */ |
b0e7825e | 10598 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
10599 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10600 | "suggest parentheses around comparison in operand of %<|%>"); | |
10601 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
10602 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 10603 | "suggest parentheses around comparison in operand of %<|%>"); |
10604 | /* Check cases like !x | y */ | |
10605 | else if (code_left == TRUTH_NOT_EXPR | |
10606 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
b0e7825e | 10607 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
10608 | "suggest parentheses around operand of " | |
10609 | "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>"); | |
82012ffe | 10610 | return; |
e534436e | 10611 | |
82012ffe | 10612 | case BIT_XOR_EXPR: |
e534436e | 10613 | if (code_left == BIT_AND_EXPR |
b0e7825e | 10614 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR) |
10615 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10616 | "suggest parentheses around arithmetic in operand of %<^%>"); | |
10617 | else if (code_right == BIT_AND_EXPR | |
10618 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
10619 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 10620 | "suggest parentheses around arithmetic in operand of %<^%>"); |
e534436e | 10621 | /* Check cases like x^y==z */ |
b0e7825e | 10622 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
10623 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10624 | "suggest parentheses around comparison in operand of %<^%>"); | |
10625 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
10626 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 10627 | "suggest parentheses around comparison in operand of %<^%>"); |
10628 | return; | |
e534436e | 10629 | |
82012ffe | 10630 | case BIT_AND_EXPR: |
b0e7825e | 10631 | if (code_left == PLUS_EXPR) |
10632 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10633 | "suggest parentheses around %<+%> in operand of %<&%>"); | |
10634 | else if (code_right == PLUS_EXPR) | |
10635 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 10636 | "suggest parentheses around %<+%> in operand of %<&%>"); |
b0e7825e | 10637 | else if (code_left == MINUS_EXPR) |
10638 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10639 | "suggest parentheses around %<-%> in operand of %<&%>"); | |
10640 | else if (code_right == MINUS_EXPR) | |
10641 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 10642 | "suggest parentheses around %<-%> in operand of %<&%>"); |
e534436e | 10643 | /* Check cases like x&y==z */ |
b0e7825e | 10644 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
10645 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10646 | "suggest parentheses around comparison in operand of %<&%>"); | |
10647 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
10648 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 10649 | "suggest parentheses around comparison in operand of %<&%>"); |
10650 | /* Check cases like !x & y */ | |
10651 | else if (code_left == TRUTH_NOT_EXPR | |
10652 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
b0e7825e | 10653 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
10654 | "suggest parentheses around operand of " | |
10655 | "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>"); | |
82012ffe | 10656 | return; |
e534436e | 10657 | |
82012ffe | 10658 | case EQ_EXPR: |
b0e7825e | 10659 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
10660 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10661 | "suggest parentheses around comparison in operand of %<==%>"); | |
10662 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
10663 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 10664 | "suggest parentheses around comparison in operand of %<==%>"); |
10665 | return; | |
10666 | case NE_EXPR: | |
b0e7825e | 10667 | if (TREE_CODE_CLASS (code_left) == tcc_comparison) |
10668 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, | |
10669 | "suggest parentheses around comparison in operand of %<!=%>"); | |
10670 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison) | |
10671 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
82012ffe | 10672 | "suggest parentheses around comparison in operand of %<!=%>"); |
10673 | return; | |
10674 | ||
10675 | default: | |
b0e7825e | 10676 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
10677 | { | |
10678 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
269f7979 | 10679 | && code_left != NE_EXPR && code_left != EQ_EXPR |
10680 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) | |
b0e7825e | 10681 | warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses, |
10682 | "comparisons like %<X<=Y<=Z%> do not " | |
10683 | "have their mathematical meaning"); | |
10684 | else if (TREE_CODE_CLASS (code_right) == tcc_comparison | |
269f7979 | 10685 | && code_right != NE_EXPR && code_right != EQ_EXPR |
b0e7825e | 10686 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))) |
10687 | warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses, | |
10688 | "comparisons like %<X<=Y<=Z%> do not " | |
10689 | "have their mathematical meaning"); | |
10690 | } | |
82012ffe | 10691 | return; |
6ce0c450 | 10692 | } |
82012ffe | 10693 | #undef NOT_A_BOOLEAN_EXPR_P |
e534436e | 10694 | } |
10695 | ||
92fccaaa | 10696 | /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ |
10697 | ||
10698 | void | |
10699 | warn_for_unused_label (tree label) | |
10700 | { | |
10701 | if (!TREE_USED (label)) | |
10702 | { | |
10703 | if (DECL_INITIAL (label)) | |
10704 | warning (OPT_Wunused_label, "label %q+D defined but not used", label); | |
10705 | else | |
10706 | warning (OPT_Wunused_label, "label %q+D declared but not defined", label); | |
10707 | } | |
10708 | } | |
2840aae4 | 10709 | |
b6889cb0 | 10710 | /* Warn for division by zero according to the value of DIVISOR. LOC |
10711 | is the location of the division operator. */ | |
f092582b | 10712 | |
10713 | void | |
b6889cb0 | 10714 | warn_for_div_by_zero (location_t loc, tree divisor) |
f092582b | 10715 | { |
9421ebb9 | 10716 | /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning |
10717 | about division by zero. Do not issue a warning if DIVISOR has a | |
f092582b | 10718 | floating-point type, since we consider 0.0/0.0 a valid way of |
10719 | generating a NaN. */ | |
48d94ede | 10720 | if (c_inhibit_evaluation_warnings == 0 |
9421ebb9 | 10721 | && (integer_zerop (divisor) || fixed_zerop (divisor))) |
b6889cb0 | 10722 | warning_at (loc, OPT_Wdiv_by_zero, "division by zero"); |
f092582b | 10723 | } |
10724 | ||
13869a99 | 10725 | /* Subroutine of build_binary_op. Give warnings for comparisons |
10726 | between signed and unsigned quantities that may fail. Do the | |
10727 | checking based on the original operand trees ORIG_OP0 and ORIG_OP1, | |
10728 | so that casts will be considered, but default promotions won't | |
8e70fb09 | 10729 | be. |
10730 | ||
10731 | LOCATION is the location of the comparison operator. | |
13869a99 | 10732 | |
10733 | The arguments of this function map directly to local variables | |
10734 | of build_binary_op. */ | |
10735 | ||
48e1416a | 10736 | void |
8e70fb09 | 10737 | warn_for_sign_compare (location_t location, |
48e1416a | 10738 | tree orig_op0, tree orig_op1, |
10739 | tree op0, tree op1, | |
13869a99 | 10740 | tree result_type, enum tree_code resultcode) |
10741 | { | |
10742 | int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); | |
10743 | int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); | |
10744 | int unsignedp0, unsignedp1; | |
48e1416a | 10745 | |
13869a99 | 10746 | /* In C++, check for comparison of different enum types. */ |
10747 | if (c_dialect_cxx() | |
10748 | && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE | |
10749 | && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE | |
10750 | && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) | |
895b662f | 10751 | != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) |
13869a99 | 10752 | { |
8e70fb09 | 10753 | warning_at (location, |
10754 | OPT_Wsign_compare, "comparison between types %qT and %qT", | |
10755 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); | |
13869a99 | 10756 | } |
10757 | ||
10758 | /* Do not warn if the comparison is being done in a signed type, | |
10759 | since the signed type will only be chosen if it can represent | |
10760 | all the values of the unsigned type. */ | |
10761 | if (!TYPE_UNSIGNED (result_type)) | |
10762 | /* OK */; | |
10763 | /* Do not warn if both operands are unsigned. */ | |
10764 | else if (op0_signed == op1_signed) | |
10765 | /* OK */; | |
10766 | else | |
10767 | { | |
895b662f | 10768 | tree sop, uop, base_type; |
13869a99 | 10769 | bool ovf; |
895b662f | 10770 | |
13869a99 | 10771 | if (op0_signed) |
10772 | sop = orig_op0, uop = orig_op1; | |
48e1416a | 10773 | else |
13869a99 | 10774 | sop = orig_op1, uop = orig_op0; |
10775 | ||
48e1416a | 10776 | STRIP_TYPE_NOPS (sop); |
13869a99 | 10777 | STRIP_TYPE_NOPS (uop); |
895b662f | 10778 | base_type = (TREE_CODE (result_type) == COMPLEX_TYPE |
10779 | ? TREE_TYPE (result_type) : result_type); | |
13869a99 | 10780 | |
10781 | /* Do not warn if the signed quantity is an unsuffixed integer | |
10782 | literal (or some static constant expression involving such | |
10783 | literals or a conditional expression involving such literals) | |
10784 | and it is non-negative. */ | |
10785 | if (tree_expr_nonnegative_warnv_p (sop, &ovf)) | |
10786 | /* OK */; | |
10787 | /* Do not warn if the comparison is an equality operation, the | |
10788 | unsigned quantity is an integral constant, and it would fit | |
10789 | in the result if the result were signed. */ | |
10790 | else if (TREE_CODE (uop) == INTEGER_CST | |
10791 | && (resultcode == EQ_EXPR || resultcode == NE_EXPR) | |
895b662f | 10792 | && int_fits_type_p (uop, c_common_signed_type (base_type))) |
13869a99 | 10793 | /* OK */; |
10794 | /* In C, do not warn if the unsigned quantity is an enumeration | |
10795 | constant and its maximum value would fit in the result if the | |
10796 | result were signed. */ | |
10797 | else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST | |
10798 | && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE | |
10799 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), | |
895b662f | 10800 | c_common_signed_type (base_type))) |
13869a99 | 10801 | /* OK */; |
48e1416a | 10802 | else |
8e70fb09 | 10803 | warning_at (location, |
48e1416a | 10804 | OPT_Wsign_compare, |
8e70fb09 | 10805 | "comparison between signed and unsigned integer expressions"); |
13869a99 | 10806 | } |
48e1416a | 10807 | |
13869a99 | 10808 | /* Warn if two unsigned values are being compared in a size larger |
10809 | than their original size, and one (and only one) is the result of | |
10810 | a `~' operator. This comparison will always fail. | |
48e1416a | 10811 | |
13869a99 | 10812 | Also warn if one operand is a constant, and the constant does not |
10813 | have all bits set that are set in the ~ operand when it is | |
10814 | extended. */ | |
10815 | ||
7f506bca | 10816 | op0 = c_common_get_narrower (op0, &unsignedp0); |
10817 | op1 = c_common_get_narrower (op1, &unsignedp1); | |
48e1416a | 10818 | |
13869a99 | 10819 | if ((TREE_CODE (op0) == BIT_NOT_EXPR) |
10820 | ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) | |
10821 | { | |
10822 | if (TREE_CODE (op0) == BIT_NOT_EXPR) | |
7f506bca | 10823 | op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); |
13869a99 | 10824 | if (TREE_CODE (op1) == BIT_NOT_EXPR) |
7f506bca | 10825 | op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); |
13869a99 | 10826 | |
10827 | if (host_integerp (op0, 0) || host_integerp (op1, 0)) | |
10828 | { | |
10829 | tree primop; | |
10830 | HOST_WIDE_INT constant, mask; | |
10831 | int unsignedp; | |
10832 | unsigned int bits; | |
48e1416a | 10833 | |
13869a99 | 10834 | if (host_integerp (op0, 0)) |
10835 | { | |
10836 | primop = op1; | |
10837 | unsignedp = unsignedp1; | |
10838 | constant = tree_low_cst (op0, 0); | |
10839 | } | |
10840 | else | |
10841 | { | |
10842 | primop = op0; | |
10843 | unsignedp = unsignedp0; | |
10844 | constant = tree_low_cst (op1, 0); | |
10845 | } | |
48e1416a | 10846 | |
13869a99 | 10847 | bits = TYPE_PRECISION (TREE_TYPE (primop)); |
10848 | if (bits < TYPE_PRECISION (result_type) | |
10849 | && bits < HOST_BITS_PER_LONG && unsignedp) | |
10850 | { | |
10851 | mask = (~ (HOST_WIDE_INT) 0) << bits; | |
10852 | if ((mask & constant) != mask) | |
10853 | { | |
10854 | if (constant == 0) | |
48e1416a | 10855 | warning (OPT_Wsign_compare, |
13869a99 | 10856 | "promoted ~unsigned is always non-zero"); |
10857 | else | |
48e1416a | 10858 | warning_at (location, OPT_Wsign_compare, |
8e70fb09 | 10859 | "comparison of promoted ~unsigned with constant"); |
13869a99 | 10860 | } |
10861 | } | |
10862 | } | |
10863 | else if (unsignedp0 && unsignedp1 | |
10864 | && (TYPE_PRECISION (TREE_TYPE (op0)) | |
10865 | < TYPE_PRECISION (result_type)) | |
10866 | && (TYPE_PRECISION (TREE_TYPE (op1)) | |
10867 | < TYPE_PRECISION (result_type))) | |
8e70fb09 | 10868 | warning_at (location, OPT_Wsign_compare, |
13869a99 | 10869 | "comparison of promoted ~unsigned with unsigned"); |
10870 | } | |
10871 | } | |
10872 | ||
5ba33bf4 | 10873 | /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common |
10874 | type via c_common_type. If -Wdouble-promotion is in use, and the | |
10875 | conditions for warning have been met, issue a warning. GMSGID is | |
10876 | the warning message. It must have two %T specifiers for the type | |
10877 | that was converted (generally "float") and the type to which it was | |
10878 | converted (generally "double), respectively. LOC is the location | |
10879 | to which the awrning should refer. */ | |
10880 | ||
10881 | void | |
10882 | do_warn_double_promotion (tree result_type, tree type1, tree type2, | |
10883 | const char *gmsgid, location_t loc) | |
10884 | { | |
10885 | tree source_type; | |
10886 | ||
10887 | if (!warn_double_promotion) | |
10888 | return; | |
10889 | /* If the conversion will not occur at run-time, there is no need to | |
10890 | warn about it. */ | |
10891 | if (c_inhibit_evaluation_warnings) | |
10892 | return; | |
10893 | if (TYPE_MAIN_VARIANT (result_type) != double_type_node | |
10894 | && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) | |
10895 | return; | |
10896 | if (TYPE_MAIN_VARIANT (type1) == float_type_node | |
10897 | || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) | |
10898 | source_type = type1; | |
10899 | else if (TYPE_MAIN_VARIANT (type2) == float_type_node | |
10900 | || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) | |
10901 | source_type = type2; | |
10902 | else | |
10903 | return; | |
10904 | warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); | |
10905 | } | |
10906 | ||
41771881 | 10907 | /* Setup a TYPE_DECL node as a typedef representation. |
10908 | ||
10909 | X is a TYPE_DECL for a typedef statement. Create a brand new | |
10910 | ..._TYPE node (which will be just a variant of the existing | |
10911 | ..._TYPE node with identical properties) and then install X | |
10912 | as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. | |
10913 | ||
10914 | The whole point here is to end up with a situation where each | |
10915 | and every ..._TYPE node the compiler creates will be uniquely | |
10916 | associated with AT MOST one node representing a typedef name. | |
10917 | This way, even though the compiler substitutes corresponding | |
10918 | ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very | |
10919 | early on, later parts of the compiler can always do the reverse | |
10920 | translation and get back the corresponding typedef name. For | |
10921 | example, given: | |
10922 | ||
10923 | typedef struct S MY_TYPE; | |
10924 | MY_TYPE object; | |
10925 | ||
10926 | Later parts of the compiler might only know that `object' was of | |
10927 | type `struct S' if it were not for code just below. With this | |
10928 | code however, later parts of the compiler see something like: | |
10929 | ||
10930 | struct S' == struct S | |
10931 | typedef struct S' MY_TYPE; | |
10932 | struct S' object; | |
10933 | ||
10934 | And they can then deduce (from the node for type struct S') that | |
10935 | the original object declaration was: | |
10936 | ||
10937 | MY_TYPE object; | |
10938 | ||
10939 | Being able to do this is important for proper support of protoize, | |
10940 | and also for generating precise symbolic debugging information | |
10941 | which takes full account of the programmer's (typedef) vocabulary. | |
10942 | ||
10943 | Obviously, we don't want to generate a duplicate ..._TYPE node if | |
10944 | the TYPE_DECL node that we are now processing really represents a | |
10945 | standard built-in type. */ | |
10946 | ||
10947 | void | |
10948 | set_underlying_type (tree x) | |
10949 | { | |
10950 | if (x == error_mark_node) | |
10951 | return; | |
10952 | if (DECL_IS_BUILTIN (x)) | |
10953 | { | |
10954 | if (TYPE_NAME (TREE_TYPE (x)) == 0) | |
10955 | TYPE_NAME (TREE_TYPE (x)) = x; | |
10956 | } | |
10957 | else if (TREE_TYPE (x) != error_mark_node | |
10958 | && DECL_ORIGINAL_TYPE (x) == NULL_TREE) | |
10959 | { | |
10960 | tree tt = TREE_TYPE (x); | |
10961 | DECL_ORIGINAL_TYPE (x) = tt; | |
10962 | tt = build_variant_type_copy (tt); | |
10963 | TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
10964 | TYPE_NAME (tt) = x; | |
10965 | TREE_USED (tt) = TREE_USED (x); | |
10966 | TREE_TYPE (x) = tt; | |
10967 | } | |
10968 | } | |
10969 | ||
1a4c44c5 | 10970 | /* Record the types used by the current global variable declaration |
10971 | being parsed, so that we can decide later to emit their debug info. | |
10972 | Those types are in types_used_by_cur_var_decl, and we are going to | |
10973 | store them in the types_used_by_vars_hash hash table. | |
10974 | DECL is the declaration of the global variable that has been parsed. */ | |
10975 | ||
10976 | void | |
10977 | record_types_used_by_current_var_decl (tree decl) | |
10978 | { | |
10979 | gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl)); | |
10980 | ||
aef48c9a | 10981 | while (!VEC_empty (tree, types_used_by_cur_var_decl)) |
1a4c44c5 | 10982 | { |
aef48c9a | 10983 | tree type = VEC_pop (tree, types_used_by_cur_var_decl); |
10984 | types_used_by_var_decl_insert (type, decl); | |
1a4c44c5 | 10985 | } |
10986 | } | |
10987 | ||
a4e3ffad | 10988 | /* If DECL is a typedef that is declared in the current function, |
10989 | record it for the purpose of -Wunused-local-typedefs. */ | |
10990 | ||
10991 | void | |
10992 | record_locally_defined_typedef (tree decl) | |
10993 | { | |
10994 | struct c_language_function *l; | |
10995 | ||
10996 | if (!warn_unused_local_typedefs | |
10997 | || cfun == NULL | |
10998 | /* if this is not a locally defined typedef then we are not | |
10999 | interested. */ | |
11000 | || !is_typedef_decl (decl) | |
11001 | || !decl_function_context (decl)) | |
11002 | return; | |
11003 | ||
11004 | l = (struct c_language_function *) cfun->language; | |
11005 | VEC_safe_push (tree, gc, l->local_typedefs, decl); | |
11006 | } | |
11007 | ||
11008 | /* If T is a TYPE_DECL declared locally, mark it as used. */ | |
11009 | ||
11010 | void | |
11011 | maybe_record_typedef_use (tree t) | |
11012 | { | |
11013 | if (!is_typedef_decl (t)) | |
11014 | return; | |
11015 | ||
11016 | TREE_USED (t) = true; | |
11017 | } | |
11018 | ||
11019 | /* Warn if there are some unused locally defined typedefs in the | |
11020 | current function. */ | |
11021 | ||
11022 | void | |
11023 | maybe_warn_unused_local_typedefs (void) | |
11024 | { | |
11025 | int i; | |
11026 | tree decl; | |
11027 | /* The number of times we have emitted -Wunused-local-typedefs | |
11028 | warnings. If this is different from errorcount, that means some | |
11029 | unrelated errors have been issued. In which case, we'll avoid | |
11030 | emitting "unused-local-typedefs" warnings. */ | |
11031 | static int unused_local_typedefs_warn_count; | |
11032 | struct c_language_function *l; | |
11033 | ||
11034 | if (cfun == NULL) | |
11035 | return; | |
11036 | ||
11037 | if ((l = (struct c_language_function *) cfun->language) == NULL) | |
11038 | return; | |
11039 | ||
11040 | if (warn_unused_local_typedefs | |
11041 | && errorcount == unused_local_typedefs_warn_count) | |
11042 | { | |
11043 | FOR_EACH_VEC_ELT (tree, l->local_typedefs, i, decl) | |
11044 | if (!TREE_USED (decl)) | |
11045 | warning_at (DECL_SOURCE_LOCATION (decl), | |
11046 | OPT_Wunused_local_typedefs, | |
11047 | "typedef %qD locally defined but not used", decl); | |
11048 | unused_local_typedefs_warn_count = errorcount; | |
11049 | } | |
11050 | ||
11051 | if (l->local_typedefs) | |
11052 | { | |
11053 | VEC_free (tree, gc, l->local_typedefs); | |
11054 | l->local_typedefs = NULL; | |
11055 | } | |
11056 | } | |
11057 | ||
f352a3fb | 11058 | /* The C and C++ parsers both use vectors to hold function arguments. |
11059 | For efficiency, we keep a cache of unused vectors. This is the | |
11060 | cache. */ | |
11061 | ||
11062 | typedef VEC(tree,gc)* tree_gc_vec; | |
11063 | DEF_VEC_P(tree_gc_vec); | |
11064 | DEF_VEC_ALLOC_P(tree_gc_vec,gc); | |
11065 | static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache; | |
11066 | ||
11067 | /* Return a new vector from the cache. If the cache is empty, | |
11068 | allocate a new vector. These vectors are GC'ed, so it is OK if the | |
11069 | pointer is not released.. */ | |
11070 | ||
11071 | VEC(tree,gc) * | |
11072 | make_tree_vector (void) | |
11073 | { | |
11074 | if (!VEC_empty (tree_gc_vec, tree_vector_cache)) | |
11075 | return VEC_pop (tree_gc_vec, tree_vector_cache); | |
11076 | else | |
11077 | { | |
11078 | /* Passing 0 to VEC_alloc returns NULL, and our callers require | |
11079 | that we always return a non-NULL value. The vector code uses | |
11080 | 4 when growing a NULL vector, so we do too. */ | |
11081 | return VEC_alloc (tree, gc, 4); | |
11082 | } | |
11083 | } | |
11084 | ||
11085 | /* Release a vector of trees back to the cache. */ | |
11086 | ||
11087 | void | |
11088 | release_tree_vector (VEC(tree,gc) *vec) | |
11089 | { | |
11090 | if (vec != NULL) | |
11091 | { | |
11092 | VEC_truncate (tree, vec, 0); | |
11093 | VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec); | |
11094 | } | |
11095 | } | |
11096 | ||
11097 | /* Get a new tree vector holding a single tree. */ | |
11098 | ||
11099 | VEC(tree,gc) * | |
11100 | make_tree_vector_single (tree t) | |
11101 | { | |
11102 | VEC(tree,gc) *ret = make_tree_vector (); | |
11103 | VEC_quick_push (tree, ret, t); | |
11104 | return ret; | |
11105 | } | |
11106 | ||
c66c81be | 11107 | /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */ |
11108 | ||
11109 | VEC(tree,gc) * | |
11110 | make_tree_vector_from_list (tree list) | |
11111 | { | |
11112 | VEC(tree,gc) *ret = make_tree_vector (); | |
11113 | for (; list; list = TREE_CHAIN (list)) | |
11114 | VEC_safe_push (tree, gc, ret, TREE_VALUE (list)); | |
11115 | return ret; | |
11116 | } | |
11117 | ||
f352a3fb | 11118 | /* Get a new tree vector which is a copy of an existing one. */ |
11119 | ||
11120 | VEC(tree,gc) * | |
11121 | make_tree_vector_copy (const VEC(tree,gc) *orig) | |
11122 | { | |
11123 | VEC(tree,gc) *ret; | |
11124 | unsigned int ix; | |
11125 | tree t; | |
11126 | ||
11127 | ret = make_tree_vector (); | |
11128 | VEC_reserve (tree, gc, ret, VEC_length (tree, orig)); | |
48148244 | 11129 | FOR_EACH_VEC_ELT (tree, orig, ix, t) |
f352a3fb | 11130 | VEC_quick_push (tree, ret, t); |
11131 | return ret; | |
11132 | } | |
11133 | ||
a9ffdd35 | 11134 | /* Return true if KEYWORD starts a type specifier. */ |
11135 | ||
11136 | bool | |
11137 | keyword_begins_type_specifier (enum rid keyword) | |
11138 | { | |
11139 | switch (keyword) | |
11140 | { | |
11141 | case RID_INT: | |
11142 | case RID_CHAR: | |
11143 | case RID_FLOAT: | |
11144 | case RID_DOUBLE: | |
11145 | case RID_VOID: | |
11146 | case RID_INT128: | |
11147 | case RID_UNSIGNED: | |
11148 | case RID_LONG: | |
11149 | case RID_SHORT: | |
11150 | case RID_SIGNED: | |
11151 | case RID_DFLOAT32: | |
11152 | case RID_DFLOAT64: | |
11153 | case RID_DFLOAT128: | |
11154 | case RID_FRACT: | |
11155 | case RID_ACCUM: | |
11156 | case RID_BOOL: | |
11157 | case RID_WCHAR: | |
11158 | case RID_CHAR16: | |
11159 | case RID_CHAR32: | |
11160 | case RID_SAT: | |
11161 | case RID_COMPLEX: | |
11162 | case RID_TYPEOF: | |
11163 | case RID_STRUCT: | |
11164 | case RID_CLASS: | |
11165 | case RID_UNION: | |
11166 | case RID_ENUM: | |
11167 | return true; | |
11168 | default: | |
11169 | return false; | |
11170 | } | |
11171 | } | |
11172 | ||
11173 | /* Return true if KEYWORD names a type qualifier. */ | |
11174 | ||
11175 | bool | |
11176 | keyword_is_type_qualifier (enum rid keyword) | |
11177 | { | |
11178 | switch (keyword) | |
11179 | { | |
11180 | case RID_CONST: | |
11181 | case RID_VOLATILE: | |
11182 | case RID_RESTRICT: | |
11183 | return true; | |
11184 | default: | |
11185 | return false; | |
11186 | } | |
11187 | } | |
11188 | ||
11189 | /* Return true if KEYWORD names a storage class specifier. | |
11190 | ||
11191 | RID_TYPEDEF is not included in this list despite `typedef' being | |
11192 | listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as | |
11193 | such for syntactic convenience only. */ | |
11194 | ||
11195 | bool | |
11196 | keyword_is_storage_class_specifier (enum rid keyword) | |
11197 | { | |
11198 | switch (keyword) | |
11199 | { | |
11200 | case RID_STATIC: | |
11201 | case RID_EXTERN: | |
11202 | case RID_REGISTER: | |
11203 | case RID_AUTO: | |
11204 | case RID_MUTABLE: | |
11205 | case RID_THREAD: | |
11206 | return true; | |
11207 | default: | |
11208 | return false; | |
11209 | } | |
11210 | } | |
11211 | ||
fad3f658 | 11212 | /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */ |
11213 | ||
11214 | static bool | |
11215 | keyword_is_function_specifier (enum rid keyword) | |
11216 | { | |
11217 | switch (keyword) | |
11218 | { | |
11219 | case RID_INLINE: | |
985c6e3a | 11220 | case RID_NORETURN: |
fad3f658 | 11221 | case RID_VIRTUAL: |
11222 | case RID_EXPLICIT: | |
11223 | return true; | |
11224 | default: | |
11225 | return false; | |
11226 | } | |
11227 | } | |
11228 | ||
11229 | /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a | |
11230 | declaration-specifier (C99 6.7). */ | |
11231 | ||
11232 | bool | |
11233 | keyword_is_decl_specifier (enum rid keyword) | |
11234 | { | |
11235 | if (keyword_is_storage_class_specifier (keyword) | |
11236 | || keyword_is_type_qualifier (keyword) | |
11237 | || keyword_is_function_specifier (keyword)) | |
11238 | return true; | |
11239 | ||
11240 | switch (keyword) | |
11241 | { | |
11242 | case RID_TYPEDEF: | |
11243 | case RID_FRIEND: | |
11244 | case RID_CONSTEXPR: | |
11245 | return true; | |
11246 | default: | |
11247 | return false; | |
11248 | } | |
11249 | } | |
11250 | ||
9b88d08d | 11251 | /* Initialize language-specific-bits of tree_contains_struct. */ |
11252 | ||
11253 | void | |
11254 | c_common_init_ts (void) | |
11255 | { | |
11256 | MARK_TS_TYPED (C_MAYBE_CONST_EXPR); | |
11257 | MARK_TS_TYPED (EXCESS_PRECISION_EXPR); | |
11258 | } | |
11259 | ||
244db24d | 11260 | /* Build a user-defined numeric literal out of an integer constant type VALUE |
11261 | with identifier SUFFIX. */ | |
11262 | ||
11263 | tree | |
11264 | build_userdef_literal (tree suffix_id, tree value, tree num_string) | |
11265 | { | |
11266 | tree literal = make_node (USERDEF_LITERAL); | |
11267 | USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id; | |
11268 | USERDEF_LITERAL_VALUE (literal) = value; | |
11269 | USERDEF_LITERAL_NUM_STRING (literal) = num_string; | |
11270 | return literal; | |
11271 | } | |
11272 | ||
7059d45d | 11273 | /* For vector[index], convert the vector to a |
11274 | pointer of the underlying type. */ | |
11275 | void | |
11276 | convert_vector_to_pointer_for_subscript (location_t loc, | |
11277 | tree* vecp, tree index) | |
11278 | { | |
11279 | if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE) | |
11280 | { | |
11281 | tree type = TREE_TYPE (*vecp); | |
11282 | tree type1; | |
11283 | ||
11284 | if (TREE_CODE (index) == INTEGER_CST) | |
11285 | if (!host_integerp (index, 1) | |
11286 | || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1) | |
11287 | >= TYPE_VECTOR_SUBPARTS (type))) | |
11288 | warning_at (loc, OPT_Warray_bounds, "index value is out of bound"); | |
11289 | ||
11290 | c_common_mark_addressable_vec (*vecp); | |
11291 | type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); | |
11292 | type = build_pointer_type (type); | |
11293 | type1 = build_pointer_type (TREE_TYPE (*vecp)); | |
11294 | *vecp = build1 (ADDR_EXPR, type1, *vecp); | |
11295 | *vecp = convert (type, *vecp); | |
11296 | } | |
11297 | } | |
11298 | ||
41ed701a | 11299 | /* Determine which of the operands, if any, is a scalar that needs to be |
11300 | converted to a vector, for the range of operations. */ | |
11301 | enum stv_conv | |
11302 | scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1, | |
11303 | bool complain) | |
11304 | { | |
11305 | tree type0 = TREE_TYPE (op0); | |
11306 | tree type1 = TREE_TYPE (op1); | |
11307 | bool integer_only_op = false; | |
11308 | enum stv_conv ret = stv_firstarg; | |
11309 | ||
11310 | gcc_assert (TREE_CODE (type0) == VECTOR_TYPE | |
11311 | || TREE_CODE (type1) == VECTOR_TYPE); | |
11312 | switch (code) | |
11313 | { | |
11314 | /* Most GENERIC binary expressions require homogeneous arguments. | |
11315 | LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first | |
11316 | argument that is a vector and a second one that is a scalar, so | |
11317 | we never return stv_secondarg for them. */ | |
11318 | case RSHIFT_EXPR: | |
11319 | case LSHIFT_EXPR: | |
11320 | if (TREE_CODE (type0) == INTEGER_TYPE | |
11321 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
11322 | { | |
11323 | if (unsafe_conversion_p (TREE_TYPE (type1), op0, false)) | |
11324 | { | |
11325 | if (complain) | |
11326 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
11327 | "involves truncation", type0, type1); | |
11328 | return stv_error; | |
11329 | } | |
11330 | else | |
11331 | return stv_firstarg; | |
11332 | } | |
11333 | break; | |
11334 | ||
11335 | case BIT_IOR_EXPR: | |
11336 | case BIT_XOR_EXPR: | |
11337 | case BIT_AND_EXPR: | |
11338 | integer_only_op = true; | |
11339 | /* ... fall through ... */ | |
11340 | ||
11341 | case PLUS_EXPR: | |
11342 | case MINUS_EXPR: | |
11343 | case MULT_EXPR: | |
11344 | case TRUNC_DIV_EXPR: | |
11345 | case CEIL_DIV_EXPR: | |
11346 | case FLOOR_DIV_EXPR: | |
11347 | case ROUND_DIV_EXPR: | |
11348 | case EXACT_DIV_EXPR: | |
11349 | case TRUNC_MOD_EXPR: | |
11350 | case FLOOR_MOD_EXPR: | |
11351 | case RDIV_EXPR: | |
11352 | case EQ_EXPR: | |
11353 | case NE_EXPR: | |
11354 | case LE_EXPR: | |
11355 | case GE_EXPR: | |
11356 | case LT_EXPR: | |
11357 | case GT_EXPR: | |
11358 | /* What about UNLT_EXPR? */ | |
11359 | if (TREE_CODE (type0) == VECTOR_TYPE) | |
11360 | { | |
11361 | tree tmp; | |
11362 | ret = stv_secondarg; | |
11363 | /* Swap TYPE0 with TYPE1 and OP0 with OP1 */ | |
11364 | tmp = type0; type0 = type1; type1 = tmp; | |
11365 | tmp = op0; op0 = op1; op1 = tmp; | |
11366 | } | |
11367 | ||
11368 | if (TREE_CODE (type0) == INTEGER_TYPE | |
11369 | && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE) | |
11370 | { | |
11371 | if (unsafe_conversion_p (TREE_TYPE (type1), op0, false)) | |
11372 | { | |
11373 | if (complain) | |
11374 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
11375 | "involves truncation", type0, type1); | |
11376 | return stv_error; | |
11377 | } | |
11378 | return ret; | |
11379 | } | |
11380 | else if (!integer_only_op | |
11381 | /* Allow integer --> real conversion if safe. */ | |
11382 | && (TREE_CODE (type0) == REAL_TYPE | |
11383 | || TREE_CODE (type0) == INTEGER_TYPE) | |
11384 | && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1))) | |
11385 | { | |
11386 | if (unsafe_conversion_p (TREE_TYPE (type1), op0, false)) | |
11387 | { | |
11388 | if (complain) | |
11389 | error_at (loc, "conversion of scalar %qT to vector %qT " | |
11390 | "involves truncation", type0, type1); | |
11391 | return stv_error; | |
11392 | } | |
11393 | return ret; | |
11394 | } | |
11395 | default: | |
11396 | break; | |
11397 | } | |
11398 | ||
11399 | return stv_nothing; | |
11400 | } | |
11401 | ||
ffcdbf9c | 11402 | /* Return true iff ALIGN is an integral constant that is a fundamental |
11403 | alignment, as defined by [basic.align] in the c++-11 | |
11404 | specifications. | |
11405 | ||
11406 | That is: | |
11407 | ||
11408 | [A fundamental alignment is represented by an alignment less than or | |
11409 | equal to the greatest alignment supported by the implementation | |
11410 | in all contexts, which is equal to | |
11411 | alignof(max_align_t)]. */ | |
11412 | ||
11413 | bool | |
11414 | cxx_fundamental_alignment_p (unsigned align) | |
11415 | { | |
11416 | return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node), | |
11417 | TYPE_ALIGN (long_double_type_node))); | |
11418 | } | |
11419 | ||
7bedc3a0 | 11420 | #include "gt-c-family-c-common.h" |