]>
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, |
fdd84b77 | 3 | 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
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" |
cd03a192 | 29 | #include "output.h" |
a3fa7feb | 30 | #include "c-pragma.h" |
dc12af01 | 31 | #include "ggc.h" |
e41f0d80 | 32 | #include "c-common.h" |
6c536c4f | 33 | #include "c-objc.h" |
d8c9779c | 34 | #include "tm_p.h" |
4e91a871 | 35 | #include "obstack.h" |
a654e028 | 36 | #include "cpplib.h" |
8ee295a7 | 37 | #include "target.h" |
218e3e4e | 38 | #include "common/common-target.h" |
96554925 | 39 | #include "langhooks.h" |
f3dde807 | 40 | #include "tree-inline.h" |
69579044 | 41 | #include "toplev.h" |
7f5f3953 | 42 | #include "diagnostic.h" |
4ee9c684 | 43 | #include "tree-iterator.h" |
44 | #include "hashtab.h" | |
b55af61c | 45 | #include "tree-mudflap.h" |
e08bd2f4 | 46 | #include "opts.h" |
62eec3b4 | 47 | #include "cgraph.h" |
b9fc964a | 48 | #include "target-def.h" |
979d3efc | 49 | #include "libfuncs.h" |
fd6f6435 | 50 | |
90cc7820 | 51 | cpp_reader *parse_in; /* Declared in c-pragma.h. */ |
a654e028 | 52 | |
72040e7e | 53 | /* The following symbols are subsumed in the c_global_trees array, and |
44e9fa65 | 54 | listed here individually for documentation purposes. |
72040e7e | 55 | |
56 | INTEGER_TYPE and REAL_TYPE nodes for the standard data types. | |
57 | ||
58 | tree short_integer_type_node; | |
59 | tree long_integer_type_node; | |
60 | tree long_long_integer_type_node; | |
6388cfe2 | 61 | tree int128_integer_type_node; |
72040e7e | 62 | |
63 | tree short_unsigned_type_node; | |
64 | tree long_unsigned_type_node; | |
65 | tree long_long_unsigned_type_node; | |
6388cfe2 | 66 | tree int128_unsigned_type_node; |
72040e7e | 67 | |
3c2239cf | 68 | tree truthvalue_type_node; |
69 | tree truthvalue_false_node; | |
70 | tree truthvalue_true_node; | |
72040e7e | 71 | |
72 | tree ptrdiff_type_node; | |
73 | ||
74 | tree unsigned_char_type_node; | |
75 | tree signed_char_type_node; | |
76 | tree wchar_type_node; | |
72040e7e | 77 | |
924bbf02 | 78 | tree char16_type_node; |
79 | tree char32_type_node; | |
80 | ||
72040e7e | 81 | tree float_type_node; |
82 | tree double_type_node; | |
83 | tree long_double_type_node; | |
84 | ||
85 | tree complex_integer_type_node; | |
86 | tree complex_float_type_node; | |
87 | tree complex_double_type_node; | |
88 | tree complex_long_double_type_node; | |
89 | ||
c4503c0a | 90 | tree dfloat32_type_node; |
91 | tree dfloat64_type_node; | |
92 | tree_dfloat128_type_node; | |
93 | ||
72040e7e | 94 | tree intQI_type_node; |
95 | tree intHI_type_node; | |
96 | tree intSI_type_node; | |
97 | tree intDI_type_node; | |
98 | tree intTI_type_node; | |
99 | ||
100 | tree unsigned_intQI_type_node; | |
101 | tree unsigned_intHI_type_node; | |
102 | tree unsigned_intSI_type_node; | |
103 | tree unsigned_intDI_type_node; | |
104 | tree unsigned_intTI_type_node; | |
105 | ||
106 | tree widest_integer_literal_type_node; | |
107 | tree widest_unsigned_literal_type_node; | |
108 | ||
109 | Nodes for types `void *' and `const void *'. | |
110 | ||
111 | tree ptr_type_node, const_ptr_type_node; | |
112 | ||
113 | Nodes for types `char *' and `const char *'. | |
114 | ||
115 | tree string_type_node, const_string_type_node; | |
116 | ||
117 | Type `char[SOMENUMBER]'. | |
118 | Used when an array of char is needed and the size is irrelevant. | |
119 | ||
120 | tree char_array_type_node; | |
121 | ||
122 | Type `int[SOMENUMBER]' or something like it. | |
123 | Used when an array of int needed and the size is irrelevant. | |
124 | ||
125 | tree int_array_type_node; | |
126 | ||
127 | Type `wchar_t[SOMENUMBER]' or something like it. | |
128 | Used when a wide string literal is created. | |
129 | ||
130 | tree wchar_array_type_node; | |
131 | ||
924bbf02 | 132 | Type `char16_t[SOMENUMBER]' or something like it. |
133 | Used when a UTF-16 string literal is created. | |
134 | ||
135 | tree char16_array_type_node; | |
136 | ||
137 | Type `char32_t[SOMENUMBER]' or something like it. | |
138 | Used when a UTF-32 string literal is created. | |
139 | ||
140 | tree char32_array_type_node; | |
141 | ||
72040e7e | 142 | Type `int ()' -- used for implicit declaration of functions. |
143 | ||
144 | tree default_function_type; | |
145 | ||
72040e7e | 146 | A VOID_TYPE node, packaged in a TREE_LIST. |
147 | ||
148 | tree void_list_node; | |
149 | ||
734c98be | 150 | The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__, |
65b7f83f | 151 | and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__ |
152 | VAR_DECLS, but C++ does.) | |
71d9fc9b | 153 | |
65b7f83f | 154 | tree function_name_decl_node; |
734c98be | 155 | tree pretty_function_name_decl_node; |
65b7f83f | 156 | tree c99_function_name_decl_node; |
157 | ||
158 | Stack of nested function name VAR_DECLs. | |
1cae46be | 159 | |
65b7f83f | 160 | tree saved_function_name_decls; |
71d9fc9b | 161 | |
72040e7e | 162 | */ |
163 | ||
164 | tree c_global_trees[CTI_MAX]; | |
573aba85 | 165 | \f |
574a6990 | 166 | /* Switches common to the C front ends. */ |
167 | ||
f7070933 | 168 | /* Nonzero means don't output line number information. */ |
169 | ||
170 | char flag_no_line_commands; | |
171 | ||
172 | /* Nonzero causes -E output not to be done, but directives such as | |
173 | #define that have side effects are still obeyed. */ | |
174 | ||
175 | char flag_no_output; | |
176 | ||
177 | /* Nonzero means dump macros in some fashion. */ | |
178 | ||
179 | char flag_dump_macros; | |
180 | ||
181 | /* Nonzero means pass #include lines through to the output. */ | |
182 | ||
183 | char flag_dump_includes; | |
184 | ||
d718b525 | 185 | /* Nonzero means process PCH files while preprocessing. */ |
186 | ||
187 | bool flag_pch_preprocess; | |
188 | ||
573aba85 | 189 | /* The file name to which we should write a precompiled header, or |
190 | NULL if no header will be written in this compile. */ | |
191 | ||
192 | const char *pch_file; | |
193 | ||
1ed9d5f5 | 194 | /* Nonzero if an ISO standard was selected. It rejects macros in the |
195 | user's namespace. */ | |
196 | int flag_iso; | |
197 | ||
1cae46be | 198 | /* Warn about #pragma directives that are not recognized. */ |
574a6990 | 199 | |
1cae46be | 200 | int warn_unknown_pragmas; /* Tri state variable. */ |
574a6990 | 201 | |
574a6990 | 202 | /* Warn about format/argument anomalies in calls to formatted I/O functions |
203 | (*printf, *scanf, strftime, strfmon, etc.). */ | |
204 | ||
205 | int warn_format; | |
206 | ||
574a6990 | 207 | /* C/ObjC language option variables. */ |
208 | ||
209 | ||
574a6990 | 210 | /* Nonzero means allow type mismatches in conditional expressions; |
211 | just make their values `void'. */ | |
212 | ||
213 | int flag_cond_mismatch; | |
214 | ||
215 | /* Nonzero means enable C89 Amendment 1 features. */ | |
216 | ||
217 | int flag_isoc94; | |
218 | ||
39012afb | 219 | /* Nonzero means use the ISO C99 (or C1X) dialect of C. */ |
574a6990 | 220 | |
221 | int flag_isoc99; | |
222 | ||
39012afb | 223 | /* Nonzero means use the ISO C1X dialect of C. */ |
224 | ||
225 | int flag_isoc1x; | |
226 | ||
8b332087 | 227 | /* Nonzero means that we have builtin functions, and main is an int. */ |
574a6990 | 228 | |
229 | int flag_hosted = 1; | |
230 | ||
574a6990 | 231 | |
232 | /* ObjC language option variables. */ | |
233 | ||
234 | ||
574a6990 | 235 | /* Tells the compiler that this is a special run. Do not perform any |
236 | compiling, instead we are to test some platform dependent features | |
237 | and output a C header file with appropriate definitions. */ | |
238 | ||
239 | int print_struct_values; | |
240 | ||
f0b5f617 | 241 | /* Tells the compiler what is the constant string class for ObjC. */ |
574a6990 | 242 | |
243 | const char *constant_string_class_name; | |
244 | ||
574a6990 | 245 | |
246 | /* C++ language option variables. */ | |
247 | ||
248 | ||
574a6990 | 249 | /* Nonzero means generate separate instantiation control files and |
250 | juggle them at link time. */ | |
251 | ||
252 | int flag_use_repository; | |
253 | ||
6dcdb5de | 254 | /* The C++ dialect being used. C++98 is the default. */ |
0fe6eeac | 255 | |
6dcdb5de | 256 | enum cxx_dialect cxx_dialect = cxx98; |
0fe6eeac | 257 | |
9dcd0d49 | 258 | /* Maximum template instantiation depth. This limit exists to limit the |
8ce59854 | 259 | time it takes to notice excessively recursive template instantiations. |
574a6990 | 260 | |
8ce59854 | 261 | The default is lower than the 1024 recommended by the C++0x standard |
262 | because G++ runs out of stack before 1024 with highly recursive template | |
263 | argument deduction substitution (g++.dg/cpp0x/enum11.C). */ | |
264 | ||
265 | int max_tinst_depth = 900; | |
574a6990 | 266 | |
988fc1d1 | 267 | /* The elements of `ridpointers' are identifier nodes for the reserved |
268 | type names and storage classes. It is indexed by a RID_... value. */ | |
269 | tree *ridpointers; | |
270 | ||
e60a6f7b | 271 | tree (*make_fname_decl) (location_t, tree, int); |
9e5a737d | 272 | |
48d94ede | 273 | /* Nonzero means don't warn about problems that occur when the code is |
274 | executed. */ | |
275 | int c_inhibit_evaluation_warnings; | |
e78703c1 | 276 | |
93be21c0 | 277 | /* Whether we are building a boolean conversion inside |
278 | convert_for_assignment, or some other late binary operation. If | |
279 | build_binary_op is called for C (from code shared by C and C++) in | |
280 | this case, then the operands have already been folded and the | |
281 | result will not be folded again, so C_MAYBE_CONST_EXPR should not | |
282 | be generated. */ | |
283 | bool in_late_binary_op; | |
284 | ||
7f5f3953 | 285 | /* Whether lexing has been completed, so subsequent preprocessor |
286 | errors should use the compiler's input_location. */ | |
287 | bool done_lexing = false; | |
288 | ||
2c0e001b | 289 | /* Information about how a function name is generated. */ |
65b7f83f | 290 | struct fname_var_t |
291 | { | |
e99c3a1d | 292 | tree *const decl; /* pointer to the VAR_DECL. */ |
293 | const unsigned rid; /* RID number for the identifier. */ | |
294 | const int pretty; /* How pretty is it? */ | |
65b7f83f | 295 | }; |
296 | ||
2c0e001b | 297 | /* The three ways of getting then name of the current function. */ |
65b7f83f | 298 | |
299 | const struct fname_var_t fname_vars[] = | |
300 | { | |
2c0e001b | 301 | /* C99 compliant __func__, must be first. */ |
65b7f83f | 302 | {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0}, |
2c0e001b | 303 | /* GCC __FUNCTION__ compliant. */ |
65b7f83f | 304 | {&function_name_decl_node, RID_FUNCTION_NAME, 0}, |
2c0e001b | 305 | /* GCC __PRETTY_FUNCTION__ compliant. */ |
65b7f83f | 306 | {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1}, |
307 | {NULL, 0, 0}, | |
308 | }; | |
309 | ||
79396169 | 310 | /* Global visibility options. */ |
311 | struct visibility_flags visibility_options; | |
312 | ||
a75b1c71 | 313 | static tree c_fully_fold_internal (tree expr, bool, bool *, bool *); |
2ca392fd | 314 | static tree check_case_value (tree); |
315 | static bool check_case_bounds (tree, tree, tree *, tree *); | |
be43ff5a | 316 | |
1cae46be | 317 | static tree handle_packed_attribute (tree *, tree, tree, int, bool *); |
318 | static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *); | |
319 | static tree handle_common_attribute (tree *, tree, tree, int, bool *); | |
320 | static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *); | |
5de92639 | 321 | static tree handle_hot_attribute (tree *, tree, tree, int, bool *); |
322 | static tree handle_cold_attribute (tree *, tree, tree, int, bool *); | |
1cae46be | 323 | static tree handle_noinline_attribute (tree *, tree, tree, int, bool *); |
bdb1f0d1 | 324 | static tree handle_noclone_attribute (tree *, tree, tree, int, bool *); |
7bd95dfd | 325 | static tree handle_leaf_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 326 | static tree handle_always_inline_attribute (tree *, tree, tree, int, |
327 | bool *); | |
1b16fc45 | 328 | static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *); |
329 | static tree handle_artificial_attribute (tree *, tree, tree, int, bool *); | |
0cdd9887 | 330 | static tree handle_flatten_attribute (tree *, tree, tree, int, bool *); |
10fc867f | 331 | static tree handle_error_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 332 | static tree handle_used_attribute (tree *, tree, tree, int, bool *); |
333 | static tree handle_unused_attribute (tree *, tree, tree, int, bool *); | |
62eec3b4 | 334 | static tree handle_externally_visible_attribute (tree *, tree, tree, int, |
335 | bool *); | |
1cae46be | 336 | static tree handle_const_attribute (tree *, tree, tree, int, bool *); |
337 | static tree handle_transparent_union_attribute (tree *, tree, tree, | |
338 | int, bool *); | |
339 | static tree handle_constructor_attribute (tree *, tree, tree, int, bool *); | |
340 | static tree handle_destructor_attribute (tree *, tree, tree, int, bool *); | |
341 | static tree handle_mode_attribute (tree *, tree, tree, int, bool *); | |
342 | static tree handle_section_attribute (tree *, tree, tree, int, bool *); | |
343 | static tree handle_aligned_attribute (tree *, tree, tree, int, bool *); | |
344 | static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ; | |
85c0a25c | 345 | static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *); |
346 | static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *); | |
1cae46be | 347 | static tree handle_alias_attribute (tree *, tree, tree, int, bool *); |
f4a30bd7 | 348 | static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ; |
1cae46be | 349 | static tree handle_visibility_attribute (tree *, tree, tree, int, |
350 | bool *); | |
351 | static tree handle_tls_model_attribute (tree *, tree, tree, int, | |
352 | bool *); | |
353 | static tree handle_no_instrument_function_attribute (tree *, tree, | |
354 | tree, int, bool *); | |
355 | static tree handle_malloc_attribute (tree *, tree, tree, int, bool *); | |
26d1c5ff | 356 | static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 357 | static tree handle_no_limit_stack_attribute (tree *, tree, tree, int, |
358 | bool *); | |
359 | static tree handle_pure_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 *); |
48b14f50 | 375 | static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *); |
8ce86007 | 376 | static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *); |
1cae46be | 377 | |
d01f58f9 | 378 | static void check_function_nonnull (tree, int, tree *); |
1cae46be | 379 | static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT); |
380 | static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT); | |
381 | static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); | |
860251be | 382 | static int resort_field_decl_cmp (const void *, const void *); |
dbf6c367 | 383 | |
5c6e5756 | 384 | /* Reserved words. The third field is a mask: keywords are disabled |
385 | if they match the mask. | |
386 | ||
387 | Masks for languages: | |
388 | C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC | |
389 | C --std=c99: D_CXXONLY | D_OBJC | |
390 | ObjC is like C except that D_OBJC and D_CXX_OBJC are not set | |
391 | C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC | |
392 | C++ --std=c0x: D_CONLY | D_OBJC | |
393 | ObjC++ is like C++ except that D_OBJC is not set | |
394 | ||
395 | If -fno-asm is used, D_ASM is added to the mask. If | |
396 | -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in | |
397 | C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords. | |
b27e241e | 398 | In C with -Wc++-compat, we warn if D_CXXWARN is set. |
5c6e5756 | 399 | |
b27e241e | 400 | Note the complication of the D_CXX_OBJC keywords. These are |
401 | reserved words such as 'class'. In C++, 'class' is a reserved | |
402 | word. In Objective-C++ it is too. In Objective-C, it is a | |
403 | reserved word too, but only if it follows an '@' sign. | |
404 | */ | |
5c6e5756 | 405 | const struct c_common_resword c_common_reswords[] = |
406 | { | |
407 | { "_Bool", RID_BOOL, D_CONLY }, | |
408 | { "_Complex", RID_COMPLEX, 0 }, | |
c1800156 | 409 | { "_Imaginary", RID_IMAGINARY, D_CONLY }, |
5c6e5756 | 410 | { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT }, |
411 | { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT }, | |
412 | { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT }, | |
413 | { "_Fract", RID_FRACT, D_CONLY | D_EXT }, | |
414 | { "_Accum", RID_ACCUM, D_CONLY | D_EXT }, | |
415 | { "_Sat", RID_SAT, D_CONLY | D_EXT }, | |
f80e7755 | 416 | { "_Static_assert", RID_STATIC_ASSERT, D_CONLY }, |
5c6e5756 | 417 | { "__FUNCTION__", RID_FUNCTION_NAME, 0 }, |
418 | { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 }, | |
419 | { "__alignof", RID_ALIGNOF, 0 }, | |
420 | { "__alignof__", RID_ALIGNOF, 0 }, | |
421 | { "__asm", RID_ASM, 0 }, | |
422 | { "__asm__", RID_ASM, 0 }, | |
423 | { "__attribute", RID_ATTRIBUTE, 0 }, | |
424 | { "__attribute__", RID_ATTRIBUTE, 0 }, | |
425 | { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, | |
426 | { "__builtin_offsetof", RID_OFFSETOF, 0 }, | |
427 | { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, | |
428 | { "__builtin_va_arg", RID_VA_ARG, 0 }, | |
429 | { "__complex", RID_COMPLEX, 0 }, | |
430 | { "__complex__", RID_COMPLEX, 0 }, | |
431 | { "__const", RID_CONST, 0 }, | |
432 | { "__const__", RID_CONST, 0 }, | |
433 | { "__decltype", RID_DECLTYPE, D_CXXONLY }, | |
434 | { "__extension__", RID_EXTENSION, 0 }, | |
435 | { "__func__", RID_C99_FUNCTION_NAME, 0 }, | |
436 | { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY }, | |
437 | { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY }, | |
438 | { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY }, | |
439 | { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY }, | |
440 | { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY }, | |
441 | { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY }, | |
442 | { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY }, | |
443 | { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY }, | |
23407dc9 | 444 | { "__imag", RID_IMAGPART, 0 }, |
445 | { "__imag__", RID_IMAGPART, 0 }, | |
446 | { "__inline", RID_INLINE, 0 }, | |
447 | { "__inline__", RID_INLINE, 0 }, | |
6388cfe2 | 448 | { "__int128", RID_INT128, 0 }, |
5c6e5756 | 449 | { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY }, |
450 | { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY }, | |
451 | { "__is_class", RID_IS_CLASS, D_CXXONLY }, | |
452 | { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY }, | |
453 | { "__is_empty", RID_IS_EMPTY, D_CXXONLY }, | |
454 | { "__is_enum", RID_IS_ENUM, D_CXXONLY }, | |
23407dc9 | 455 | { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY }, |
5c6e5756 | 456 | { "__is_pod", RID_IS_POD, D_CXXONLY }, |
457 | { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY }, | |
c1c67b4f | 458 | { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY }, |
459 | { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY }, | |
5c6e5756 | 460 | { "__is_union", RID_IS_UNION, D_CXXONLY }, |
5c6e5756 | 461 | { "__label__", RID_LABEL, 0 }, |
462 | { "__null", RID_NULL, 0 }, | |
463 | { "__real", RID_REALPART, 0 }, | |
464 | { "__real__", RID_REALPART, 0 }, | |
465 | { "__restrict", RID_RESTRICT, 0 }, | |
466 | { "__restrict__", RID_RESTRICT, 0 }, | |
467 | { "__signed", RID_SIGNED, 0 }, | |
468 | { "__signed__", RID_SIGNED, 0 }, | |
469 | { "__thread", RID_THREAD, 0 }, | |
470 | { "__typeof", RID_TYPEOF, 0 }, | |
471 | { "__typeof__", RID_TYPEOF, 0 }, | |
23407dc9 | 472 | { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY }, |
5c6e5756 | 473 | { "__volatile", RID_VOLATILE, 0 }, |
474 | { "__volatile__", RID_VOLATILE, 0 }, | |
e463efd7 | 475 | { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 476 | { "asm", RID_ASM, D_ASM }, |
477 | { "auto", RID_AUTO, 0 }, | |
dbd982c9 | 478 | { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 479 | { "break", RID_BREAK, 0 }, |
480 | { "case", RID_CASE, 0 }, | |
51030405 | 481 | { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN }, |
5c6e5756 | 482 | { "char", RID_CHAR, 0 }, |
51030405 | 483 | { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
484 | { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
485 | { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 486 | { "const", RID_CONST, 0 }, |
17814aca | 487 | { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 488 | { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN }, |
489 | { "continue", RID_CONTINUE, 0 }, | |
51030405 | 490 | { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
5c6e5756 | 491 | { "default", RID_DEFAULT, 0 }, |
51030405 | 492 | { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 493 | { "do", RID_DO, 0 }, |
494 | { "double", RID_DOUBLE, 0 }, | |
495 | { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN }, | |
496 | { "else", RID_ELSE, 0 }, | |
497 | { "enum", RID_ENUM, 0 }, | |
51030405 | 498 | { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN }, |
499 | { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 500 | { "extern", RID_EXTERN, 0 }, |
51030405 | 501 | { "false", RID_FALSE, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 502 | { "float", RID_FLOAT, 0 }, |
503 | { "for", RID_FOR, 0 }, | |
51030405 | 504 | { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN }, |
5c6e5756 | 505 | { "goto", RID_GOTO, 0 }, |
506 | { "if", RID_IF, 0 }, | |
507 | { "inline", RID_INLINE, D_EXT89 }, | |
508 | { "int", RID_INT, 0 }, | |
509 | { "long", RID_LONG, 0 }, | |
510 | { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN }, | |
51030405 | 511 | { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN }, |
512 | { "new", RID_NEW, D_CXXONLY | D_CXXWARN }, | |
98fe9664 | 513 | { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
6fe11077 | 514 | { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN }, |
51030405 | 515 | { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN }, |
516 | { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN }, | |
517 | { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN }, | |
518 | { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 519 | { "register", RID_REGISTER, 0 }, |
520 | { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN }, | |
521 | { "restrict", RID_RESTRICT, D_CONLY | D_C99 }, | |
522 | { "return", RID_RETURN, 0 }, | |
523 | { "short", RID_SHORT, 0 }, | |
524 | { "signed", RID_SIGNED, 0 }, | |
525 | { "sizeof", RID_SIZEOF, 0 }, | |
526 | { "static", RID_STATIC, 0 }, | |
527 | { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN }, | |
528 | { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN }, | |
529 | { "struct", RID_STRUCT, 0 }, | |
530 | { "switch", RID_SWITCH, 0 }, | |
51030405 | 531 | { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN }, |
532 | { "this", RID_THIS, D_CXXONLY | D_CXXWARN }, | |
533 | { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN }, | |
534 | { "true", RID_TRUE, D_CXXONLY | D_CXXWARN }, | |
535 | { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN }, | |
5c6e5756 | 536 | { "typedef", RID_TYPEDEF, 0 }, |
51030405 | 537 | { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN }, |
538 | { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 539 | { "typeof", RID_TYPEOF, D_ASM | D_EXT }, |
540 | { "union", RID_UNION, 0 }, | |
541 | { "unsigned", RID_UNSIGNED, 0 }, | |
51030405 | 542 | { "using", RID_USING, D_CXXONLY | D_CXXWARN }, |
543 | { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN }, | |
5c6e5756 | 544 | { "void", RID_VOID, 0 }, |
545 | { "volatile", RID_VOLATILE, 0 }, | |
546 | { "wchar_t", RID_WCHAR, D_CXXONLY }, | |
547 | { "while", RID_WHILE, 0 }, | |
548 | /* These Objective-C keywords are recognized only immediately after | |
549 | an '@'. */ | |
550 | { "compatibility_alias", RID_AT_ALIAS, D_OBJC }, | |
551 | { "defs", RID_AT_DEFS, D_OBJC }, | |
552 | { "encode", RID_AT_ENCODE, D_OBJC }, | |
553 | { "end", RID_AT_END, D_OBJC }, | |
554 | { "implementation", RID_AT_IMPLEMENTATION, D_OBJC }, | |
555 | { "interface", RID_AT_INTERFACE, D_OBJC }, | |
556 | { "protocol", RID_AT_PROTOCOL, D_OBJC }, | |
557 | { "selector", RID_AT_SELECTOR, D_OBJC }, | |
558 | { "finally", RID_AT_FINALLY, D_OBJC }, | |
559 | { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC }, | |
069761fb | 560 | { "optional", RID_AT_OPTIONAL, D_OBJC }, |
561 | { "required", RID_AT_REQUIRED, D_OBJC }, | |
86c110ac | 562 | { "property", RID_AT_PROPERTY, D_OBJC }, |
4a8875ed | 563 | { "package", RID_AT_PACKAGE, D_OBJC }, |
e1f293c0 | 564 | { "synthesize", RID_AT_SYNTHESIZE, D_OBJC }, |
565 | { "dynamic", RID_AT_DYNAMIC, D_OBJC }, | |
5c6e5756 | 566 | /* These are recognized only in protocol-qualifier context |
567 | (see above) */ | |
568 | { "bycopy", RID_BYCOPY, D_OBJC }, | |
569 | { "byref", RID_BYREF, D_OBJC }, | |
570 | { "in", RID_IN, D_OBJC }, | |
571 | { "inout", RID_INOUT, D_OBJC }, | |
572 | { "oneway", RID_ONEWAY, D_OBJC }, | |
573 | { "out", RID_OUT, D_OBJC }, | |
86c110ac | 574 | /* These are recognized inside a property attribute list */ |
7590f0e5 | 575 | { "assign", RID_ASSIGN, D_OBJC }, |
576 | { "copy", RID_COPY, D_OBJC }, | |
86c110ac | 577 | { "getter", RID_GETTER, D_OBJC }, |
7590f0e5 | 578 | { "nonatomic", RID_NONATOMIC, D_OBJC }, |
579 | { "readonly", RID_READONLY, D_OBJC }, | |
580 | { "readwrite", RID_READWRITE, D_OBJC }, | |
581 | { "retain", RID_RETAIN, D_OBJC }, | |
86c110ac | 582 | { "setter", RID_SETTER, D_OBJC }, |
5c6e5756 | 583 | }; |
584 | ||
585 | const unsigned int num_c_common_reswords = | |
586 | sizeof c_common_reswords / sizeof (struct c_common_resword); | |
587 | ||
f8e93a2e | 588 | /* Table of machine-independent attributes common to all C-like languages. */ |
589 | const struct attribute_spec c_common_attribute_table[] = | |
590 | { | |
ac86af5d | 591 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
592 | affects_type_identity } */ | |
f8e93a2e | 593 | { "packed", 0, 0, false, false, false, |
ac86af5d | 594 | handle_packed_attribute , false}, |
f8e93a2e | 595 | { "nocommon", 0, 0, true, false, false, |
ac86af5d | 596 | handle_nocommon_attribute, false}, |
f8e93a2e | 597 | { "common", 0, 0, true, false, false, |
ac86af5d | 598 | handle_common_attribute, false }, |
f8e93a2e | 599 | /* FIXME: logically, noreturn attributes should be listed as |
600 | "false, true, true" and apply to function types. But implementing this | |
601 | would require all the places in the compiler that use TREE_THIS_VOLATILE | |
602 | on a decl to identify non-returning functions to be located and fixed | |
603 | to check the function type instead. */ | |
604 | { "noreturn", 0, 0, true, false, false, | |
ac86af5d | 605 | handle_noreturn_attribute, false }, |
f8e93a2e | 606 | { "volatile", 0, 0, true, false, false, |
ac86af5d | 607 | handle_noreturn_attribute, false }, |
f8e93a2e | 608 | { "noinline", 0, 0, true, false, false, |
ac86af5d | 609 | handle_noinline_attribute, false }, |
bdb1f0d1 | 610 | { "noclone", 0, 0, true, false, false, |
ac86af5d | 611 | handle_noclone_attribute, false }, |
7bd95dfd | 612 | { "leaf", 0, 0, true, false, false, |
ac86af5d | 613 | handle_leaf_attribute, false }, |
f8e93a2e | 614 | { "always_inline", 0, 0, true, false, false, |
ac86af5d | 615 | handle_always_inline_attribute, false }, |
541e4101 | 616 | { "gnu_inline", 0, 0, true, false, false, |
ac86af5d | 617 | handle_gnu_inline_attribute, false }, |
1b16fc45 | 618 | { "artificial", 0, 0, true, false, false, |
ac86af5d | 619 | handle_artificial_attribute, false }, |
0cdd9887 | 620 | { "flatten", 0, 0, true, false, false, |
ac86af5d | 621 | handle_flatten_attribute, false }, |
f8e93a2e | 622 | { "used", 0, 0, true, false, false, |
ac86af5d | 623 | handle_used_attribute, false }, |
f8e93a2e | 624 | { "unused", 0, 0, false, false, false, |
ac86af5d | 625 | handle_unused_attribute, false }, |
62eec3b4 | 626 | { "externally_visible", 0, 0, true, false, false, |
ac86af5d | 627 | handle_externally_visible_attribute, false }, |
f8e93a2e | 628 | /* The same comments as for noreturn attributes apply to const ones. */ |
629 | { "const", 0, 0, true, false, false, | |
ac86af5d | 630 | handle_const_attribute, false }, |
f8e93a2e | 631 | { "transparent_union", 0, 0, false, false, false, |
ac86af5d | 632 | handle_transparent_union_attribute, false }, |
9af7fd5b | 633 | { "constructor", 0, 1, true, false, false, |
ac86af5d | 634 | handle_constructor_attribute, false }, |
9af7fd5b | 635 | { "destructor", 0, 1, true, false, false, |
ac86af5d | 636 | handle_destructor_attribute, false }, |
f8e93a2e | 637 | { "mode", 1, 1, false, true, false, |
ac86af5d | 638 | handle_mode_attribute, false }, |
f8e93a2e | 639 | { "section", 1, 1, true, false, false, |
ac86af5d | 640 | handle_section_attribute, false }, |
f8e93a2e | 641 | { "aligned", 0, 1, false, false, false, |
ac86af5d | 642 | handle_aligned_attribute, false }, |
f8e93a2e | 643 | { "weak", 0, 0, true, false, false, |
ac86af5d | 644 | handle_weak_attribute, false }, |
85c0a25c | 645 | { "ifunc", 1, 1, true, false, false, |
ac86af5d | 646 | handle_ifunc_attribute, false }, |
f8e93a2e | 647 | { "alias", 1, 1, true, false, false, |
ac86af5d | 648 | handle_alias_attribute, false }, |
f4a30bd7 | 649 | { "weakref", 0, 1, true, false, false, |
ac86af5d | 650 | handle_weakref_attribute, false }, |
f8e93a2e | 651 | { "no_instrument_function", 0, 0, true, false, false, |
ac86af5d | 652 | handle_no_instrument_function_attribute, |
653 | false }, | |
f8e93a2e | 654 | { "malloc", 0, 0, true, false, false, |
ac86af5d | 655 | handle_malloc_attribute, false }, |
26d1c5ff | 656 | { "returns_twice", 0, 0, true, false, false, |
ac86af5d | 657 | handle_returns_twice_attribute, false }, |
f8e93a2e | 658 | { "no_stack_limit", 0, 0, true, false, false, |
ac86af5d | 659 | handle_no_limit_stack_attribute, false }, |
f8e93a2e | 660 | { "pure", 0, 0, true, false, false, |
ac86af5d | 661 | handle_pure_attribute, false }, |
fc09b200 | 662 | /* For internal use (marking of builtins) only. The name contains space |
663 | to prevent its usage in source code. */ | |
664 | { "no vops", 0, 0, true, false, false, | |
ac86af5d | 665 | handle_novops_attribute, false }, |
45c4e798 | 666 | { "deprecated", 0, 1, false, false, false, |
ac86af5d | 667 | handle_deprecated_attribute, false }, |
f8e93a2e | 668 | { "vector_size", 1, 1, false, true, false, |
ac86af5d | 669 | handle_vector_size_attribute, false }, |
b212f378 | 670 | { "visibility", 1, 1, false, false, false, |
ac86af5d | 671 | handle_visibility_attribute, false }, |
24dfead4 | 672 | { "tls_model", 1, 1, true, false, false, |
ac86af5d | 673 | handle_tls_model_attribute, false }, |
dbf6c367 | 674 | { "nonnull", 0, -1, false, true, true, |
ac86af5d | 675 | handle_nonnull_attribute, false }, |
fa987697 | 676 | { "nothrow", 0, 0, true, false, false, |
ac86af5d | 677 | handle_nothrow_attribute, false }, |
678 | { "may_alias", 0, 0, false, true, false, NULL, false }, | |
7acb29a3 | 679 | { "cleanup", 1, 1, true, false, false, |
ac86af5d | 680 | handle_cleanup_attribute, false }, |
8a8cdb8d | 681 | { "warn_unused_result", 0, 0, false, true, true, |
ac86af5d | 682 | handle_warn_unused_result_attribute, false }, |
50ca527f | 683 | { "sentinel", 0, 1, false, true, true, |
ac86af5d | 684 | handle_sentinel_attribute, false }, |
b5c26b42 | 685 | /* For internal use (marking of builtins) only. The name contains space |
686 | to prevent its usage in source code. */ | |
687 | { "type generic", 0, 0, false, true, true, | |
ac86af5d | 688 | handle_type_generic_attribute, false }, |
4a29c97c | 689 | { "alloc_size", 1, 2, false, true, true, |
ac86af5d | 690 | handle_alloc_size_attribute, false }, |
5de92639 | 691 | { "cold", 0, 0, true, false, false, |
ac86af5d | 692 | handle_cold_attribute, false }, |
5de92639 | 693 | { "hot", 0, 0, true, false, false, |
ac86af5d | 694 | handle_hot_attribute, false }, |
10fc867f | 695 | { "warning", 1, 1, true, false, false, |
ac86af5d | 696 | handle_error_attribute, false }, |
10fc867f | 697 | { "error", 1, 1, true, false, false, |
ac86af5d | 698 | handle_error_attribute, false }, |
24470055 | 699 | { "target", 1, -1, true, false, false, |
ac86af5d | 700 | handle_target_attribute, false }, |
46f8e3b0 | 701 | { "optimize", 1, -1, true, false, false, |
ac86af5d | 702 | handle_optimize_attribute, false }, |
48b14f50 | 703 | { "no_split_stack", 0, 0, true, false, false, |
ac86af5d | 704 | handle_no_split_stack_attribute, false }, |
8ce86007 | 705 | /* For internal use (marking of builtins and runtime functions) only. |
706 | The name contains space to prevent its usage in source code. */ | |
707 | { "fn spec", 1, 1, false, true, true, | |
ac86af5d | 708 | handle_fnspec_attribute, false }, |
709 | { NULL, 0, 0, false, false, false, NULL, false } | |
f8e93a2e | 710 | }; |
711 | ||
712 | /* Give the specifications for the format attributes, used by C and all | |
d716ce75 | 713 | descendants. */ |
f8e93a2e | 714 | |
715 | const struct attribute_spec c_common_format_attribute_table[] = | |
716 | { | |
ac86af5d | 717 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, |
718 | affects_type_identity } */ | |
f8e93a2e | 719 | { "format", 3, 3, false, true, true, |
ac86af5d | 720 | handle_format_attribute, false }, |
f8e93a2e | 721 | { "format_arg", 1, 1, false, true, true, |
ac86af5d | 722 | handle_format_arg_attribute, false }, |
723 | { NULL, 0, 0, false, false, false, NULL, false } | |
f8e93a2e | 724 | }; |
725 | ||
6d5d708e | 726 | /* Return identifier for address space AS. */ |
34208e18 | 727 | |
6d5d708e | 728 | const char * |
729 | c_addr_space_name (addr_space_t as) | |
730 | { | |
34208e18 | 731 | int rid = RID_FIRST_ADDR_SPACE + as; |
732 | gcc_assert (ridpointers [rid]); | |
733 | return IDENTIFIER_POINTER (ridpointers [rid]); | |
6d5d708e | 734 | } |
735 | ||
2c0e001b | 736 | /* Push current bindings for the function name VAR_DECLS. */ |
f4e3c278 | 737 | |
738 | void | |
1cae46be | 739 | start_fname_decls (void) |
f4e3c278 | 740 | { |
65b7f83f | 741 | unsigned ix; |
742 | tree saved = NULL_TREE; | |
1cae46be | 743 | |
65b7f83f | 744 | for (ix = 0; fname_vars[ix].decl; ix++) |
745 | { | |
746 | tree decl = *fname_vars[ix].decl; | |
f4e3c278 | 747 | |
65b7f83f | 748 | if (decl) |
749 | { | |
ceb7b692 | 750 | saved = tree_cons (decl, build_int_cst (integer_type_node, ix), |
751 | saved); | |
65b7f83f | 752 | *fname_vars[ix].decl = NULL_TREE; |
753 | } | |
754 | } | |
755 | if (saved || saved_function_name_decls) | |
756 | /* Normally they'll have been NULL, so only push if we've got a | |
757 | stack, or they are non-NULL. */ | |
758 | saved_function_name_decls = tree_cons (saved, NULL_TREE, | |
759 | saved_function_name_decls); | |
760 | } | |
761 | ||
2363ef00 | 762 | /* Finish up the current bindings, adding them into the current function's |
763 | statement tree. This must be done _before_ finish_stmt_tree is called. | |
764 | If there is no current function, we must be at file scope and no statements | |
765 | are involved. Pop the previous bindings. */ | |
65b7f83f | 766 | |
767 | void | |
1cae46be | 768 | finish_fname_decls (void) |
65b7f83f | 769 | { |
770 | unsigned ix; | |
2363ef00 | 771 | tree stmts = NULL_TREE; |
65b7f83f | 772 | tree stack = saved_function_name_decls; |
773 | ||
774 | for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack)) | |
2363ef00 | 775 | append_to_statement_list (TREE_VALUE (stack), &stmts); |
1cae46be | 776 | |
2363ef00 | 777 | if (stmts) |
65b7f83f | 778 | { |
2363ef00 | 779 | tree *bodyp = &DECL_SAVED_TREE (current_function_decl); |
5c423bd6 | 780 | |
2363ef00 | 781 | if (TREE_CODE (*bodyp) == BIND_EXPR) |
782 | bodyp = &BIND_EXPR_BODY (*bodyp); | |
81010c97 | 783 | |
bc2b76e0 | 784 | append_to_statement_list_force (*bodyp, &stmts); |
2363ef00 | 785 | *bodyp = stmts; |
65b7f83f | 786 | } |
1cae46be | 787 | |
65b7f83f | 788 | for (ix = 0; fname_vars[ix].decl; ix++) |
789 | *fname_vars[ix].decl = NULL_TREE; | |
1cae46be | 790 | |
65b7f83f | 791 | if (stack) |
f4e3c278 | 792 | { |
2c0e001b | 793 | /* We had saved values, restore them. */ |
65b7f83f | 794 | tree saved; |
795 | ||
796 | for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved)) | |
797 | { | |
798 | tree decl = TREE_PURPOSE (saved); | |
799 | unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved)); | |
1cae46be | 800 | |
65b7f83f | 801 | *fname_vars[ix].decl = decl; |
802 | } | |
803 | stack = TREE_CHAIN (stack); | |
f4e3c278 | 804 | } |
65b7f83f | 805 | saved_function_name_decls = stack; |
806 | } | |
807 | ||
81010c97 | 808 | /* Return the text name of the current function, suitably prettified |
5fc7fa69 | 809 | by PRETTY_P. Return string must be freed by caller. */ |
65b7f83f | 810 | |
811 | const char * | |
1cae46be | 812 | fname_as_string (int pretty_p) |
65b7f83f | 813 | { |
9ad4bb1e | 814 | const char *name = "top level"; |
5fc7fa69 | 815 | char *namep; |
8115b8be | 816 | int vrb = 2, len; |
817 | cpp_string cstr = { 0, 0 }, strname; | |
9ad4bb1e | 818 | |
84166705 | 819 | if (!pretty_p) |
9ad4bb1e | 820 | { |
821 | name = ""; | |
822 | vrb = 0; | |
823 | } | |
824 | ||
825 | if (current_function_decl) | |
dc24ddbd | 826 | name = lang_hooks.decl_printable_name (current_function_decl, vrb); |
9ad4bb1e | 827 | |
8115b8be | 828 | len = strlen (name) + 3; /* Two for '"'s. One for NULL. */ |
5fc7fa69 | 829 | |
8115b8be | 830 | namep = XNEWVEC (char, len); |
831 | snprintf (namep, len, "\"%s\"", name); | |
832 | strname.text = (unsigned char *) namep; | |
833 | strname.len = len - 1; | |
5fc7fa69 | 834 | |
924bbf02 | 835 | if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING)) |
8115b8be | 836 | { |
837 | XDELETEVEC (namep); | |
838 | return (const char *) cstr.text; | |
5fc7fa69 | 839 | } |
5fc7fa69 | 840 | |
841 | return namep; | |
65b7f83f | 842 | } |
843 | ||
65b7f83f | 844 | /* Return the VAR_DECL for a const char array naming the current |
845 | function. If the VAR_DECL has not yet been created, create it | |
846 | now. RID indicates how it should be formatted and IDENTIFIER_NODE | |
847 | ID is its name (unfortunately C and C++ hold the RID values of | |
848 | keywords in different places, so we can't derive RID from ID in | |
e3b80d49 | 849 | this language independent code. LOC is the location of the |
850 | function. */ | |
65b7f83f | 851 | |
852 | tree | |
e3b80d49 | 853 | fname_decl (location_t loc, unsigned int rid, tree id) |
65b7f83f | 854 | { |
855 | unsigned ix; | |
856 | tree decl = NULL_TREE; | |
857 | ||
858 | for (ix = 0; fname_vars[ix].decl; ix++) | |
859 | if (fname_vars[ix].rid == rid) | |
860 | break; | |
861 | ||
862 | decl = *fname_vars[ix].decl; | |
863 | if (!decl) | |
f4e3c278 | 864 | { |
2222b3c6 | 865 | /* If a tree is built here, it would normally have the lineno of |
866 | the current statement. Later this tree will be moved to the | |
867 | beginning of the function and this line number will be wrong. | |
868 | To avoid this problem set the lineno to 0 here; that prevents | |
7299020b | 869 | it from appearing in the RTL. */ |
2363ef00 | 870 | tree stmts; |
9a6486a6 | 871 | location_t saved_location = input_location; |
9a6486a6 | 872 | input_location = UNKNOWN_LOCATION; |
1cae46be | 873 | |
2363ef00 | 874 | stmts = push_stmt_list (); |
e60a6f7b | 875 | decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty); |
2363ef00 | 876 | stmts = pop_stmt_list (stmts); |
877 | if (!IS_EMPTY_STMT (stmts)) | |
878 | saved_function_name_decls | |
879 | = tree_cons (decl, stmts, saved_function_name_decls); | |
65b7f83f | 880 | *fname_vars[ix].decl = decl; |
9a6486a6 | 881 | input_location = saved_location; |
f4e3c278 | 882 | } |
65b7f83f | 883 | if (!ix && !current_function_decl) |
e3b80d49 | 884 | pedwarn (loc, 0, "%qD is not defined outside of function scope", decl); |
81010c97 | 885 | |
65b7f83f | 886 | return decl; |
f4e3c278 | 887 | } |
888 | ||
070236f0 | 889 | /* Given a STRING_CST, give it a suitable array-of-chars data type. */ |
b0fc3e72 | 890 | |
891 | tree | |
1cae46be | 892 | fix_string_type (tree value) |
b0fc3e72 | 893 | { |
070236f0 | 894 | int length = TREE_STRING_LENGTH (value); |
895 | int nchars; | |
00d26680 | 896 | tree e_type, i_type, a_type; |
897 | ||
73be5127 | 898 | /* Compute the number of elements, for the array type. */ |
924bbf02 | 899 | if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value)) |
900 | { | |
901 | nchars = length; | |
902 | e_type = char_type_node; | |
903 | } | |
904 | else if (TREE_TYPE (value) == char16_array_type_node) | |
905 | { | |
906 | nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT); | |
907 | e_type = char16_type_node; | |
908 | } | |
909 | else if (TREE_TYPE (value) == char32_array_type_node) | |
910 | { | |
911 | nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT); | |
912 | e_type = char32_type_node; | |
913 | } | |
914 | else | |
915 | { | |
916 | nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT); | |
917 | e_type = wchar_type_node; | |
918 | } | |
b0fc3e72 | 919 | |
1d752508 | 920 | /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous |
921 | limit in C++98 Annex B is very large (65536) and is not normative, | |
922 | so we do not diagnose it (warn_overlength_strings is forced off | |
923 | in c_common_post_options). */ | |
924 | if (warn_overlength_strings) | |
925 | { | |
926 | const int nchars_max = flag_isoc99 ? 4095 : 509; | |
927 | const int relevant_std = flag_isoc99 ? 99 : 90; | |
928 | if (nchars - 1 > nchars_max) | |
929 | /* Translators: The %d after 'ISO C' will be 90 or 99. Do not | |
930 | separate the %d from the 'C'. 'ISO' should not be | |
931 | translated, but it may be moved after 'C%d' in languages | |
932 | where modifiers follow nouns. */ | |
21ca8540 | 933 | pedwarn (input_location, OPT_Woverlength_strings, |
8864917d | 934 | "string length %qd is greater than the length %qd " |
1d752508 | 935 | "ISO C%d compilers are required to support", |
936 | nchars - 1, nchars_max, relevant_std); | |
937 | } | |
82cfc7f7 | 938 | |
390be14e | 939 | /* Create the array type for the string constant. The ISO C++ |
940 | standard says that a string literal has type `const char[N]' or | |
941 | `const wchar_t[N]'. We use the same logic when invoked as a C | |
942 | front-end with -Wwrite-strings. | |
943 | ??? We should change the type of an expression depending on the | |
944 | state of a warning flag. We should just be warning -- see how | |
945 | this is handled in the C++ front-end for the deprecated implicit | |
946 | conversion from string literals to `char*' or `wchar_t*'. | |
00d26680 | 947 | |
948 | The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified | |
949 | array type being the unqualified version of that type. | |
950 | Therefore, if we are constructing an array of const char, we must | |
951 | construct the matching unqualified array type first. The C front | |
952 | end does not require this, but it does no harm, so we do it | |
953 | unconditionally. */ | |
ceb7b692 | 954 | i_type = build_index_type (size_int (nchars - 1)); |
00d26680 | 955 | a_type = build_array_type (e_type, i_type); |
390be14e | 956 | if (c_dialect_cxx() || warn_write_strings) |
aebc8537 | 957 | a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST); |
3a10ba35 | 958 | |
00d26680 | 959 | TREE_TYPE (value) = a_type; |
b8e3b7ad | 960 | TREE_CONSTANT (value) = 1; |
a814bad5 | 961 | TREE_READONLY (value) = 1; |
b0fc3e72 | 962 | TREE_STATIC (value) = 1; |
963 | return value; | |
964 | } | |
965 | \f | |
a75b1c71 | 966 | /* Fully fold EXPR, an expression that was not folded (beyond integer |
967 | constant expressions and null pointer constants) when being built | |
968 | up. If IN_INIT, this is in a static initializer and certain | |
969 | changes are made to the folding done. Clear *MAYBE_CONST if | |
970 | MAYBE_CONST is not NULL and EXPR is definitely not a constant | |
971 | expression because it contains an evaluated operator (in C99) or an | |
972 | operator outside of sizeof returning an integer constant (in C90) | |
973 | not permitted in constant expressions, or because it contains an | |
974 | evaluated arithmetic overflow. (*MAYBE_CONST should typically be | |
975 | set to true by callers before calling this function.) Return the | |
976 | folded expression. Function arguments have already been folded | |
977 | before calling this function, as have the contents of SAVE_EXPR, | |
978 | TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and | |
979 | C_MAYBE_CONST_EXPR. */ | |
980 | ||
981 | tree | |
982 | c_fully_fold (tree expr, bool in_init, bool *maybe_const) | |
983 | { | |
984 | tree ret; | |
c6418a4e | 985 | tree eptype = NULL_TREE; |
a75b1c71 | 986 | bool dummy = true; |
987 | bool maybe_const_itself = true; | |
389dd41b | 988 | location_t loc = EXPR_LOCATION (expr); |
a75b1c71 | 989 | |
990 | /* This function is not relevant to C++ because C++ folds while | |
991 | parsing, and may need changes to be correct for C++ when C++ | |
992 | stops folding while parsing. */ | |
993 | if (c_dialect_cxx ()) | |
994 | gcc_unreachable (); | |
995 | ||
996 | if (!maybe_const) | |
997 | maybe_const = &dummy; | |
c6418a4e | 998 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) |
999 | { | |
1000 | eptype = TREE_TYPE (expr); | |
1001 | expr = TREE_OPERAND (expr, 0); | |
1002 | } | |
a75b1c71 | 1003 | ret = c_fully_fold_internal (expr, in_init, maybe_const, |
1004 | &maybe_const_itself); | |
c6418a4e | 1005 | if (eptype) |
389dd41b | 1006 | ret = fold_convert_loc (loc, eptype, ret); |
a75b1c71 | 1007 | *maybe_const &= maybe_const_itself; |
1008 | return ret; | |
1009 | } | |
1010 | ||
1011 | /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for | |
1012 | c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands | |
1013 | not permitted, while *MAYBE_CONST_ITSELF is cleared because of | |
1014 | arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from | |
1015 | both evaluated and unevaluated subexpressions while | |
1016 | *MAYBE_CONST_ITSELF is carried from only evaluated | |
1017 | subexpressions). */ | |
1018 | ||
1019 | static tree | |
1020 | c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands, | |
1021 | bool *maybe_const_itself) | |
1022 | { | |
1023 | tree ret = expr; | |
1024 | enum tree_code code = TREE_CODE (expr); | |
1025 | enum tree_code_class kind = TREE_CODE_CLASS (code); | |
1026 | location_t loc = EXPR_LOCATION (expr); | |
1027 | tree op0, op1, op2, op3; | |
1028 | tree orig_op0, orig_op1, orig_op2; | |
1029 | bool op0_const = true, op1_const = true, op2_const = true; | |
1030 | bool op0_const_self = true, op1_const_self = true, op2_const_self = true; | |
1031 | bool nowarning = TREE_NO_WARNING (expr); | |
672d914b | 1032 | int unused_p; |
a75b1c71 | 1033 | |
1034 | /* This function is not relevant to C++ because C++ folds while | |
1035 | parsing, and may need changes to be correct for C++ when C++ | |
1036 | stops folding while parsing. */ | |
1037 | if (c_dialect_cxx ()) | |
1038 | gcc_unreachable (); | |
1039 | ||
1040 | /* Constants, declarations, statements, errors, SAVE_EXPRs and | |
1041 | anything else not counted as an expression cannot usefully be | |
1042 | folded further at this point. */ | |
1043 | if (!IS_EXPR_CODE_CLASS (kind) | |
1044 | || kind == tcc_statement | |
1045 | || code == SAVE_EXPR) | |
1046 | return expr; | |
1047 | ||
1048 | /* Operands of variable-length expressions (function calls) have | |
1049 | already been folded, as have __builtin_* function calls, and such | |
1050 | expressions cannot occur in constant expressions. */ | |
1051 | if (kind == tcc_vl_exp) | |
1052 | { | |
1053 | *maybe_const_operands = false; | |
1054 | ret = fold (expr); | |
1055 | goto out; | |
1056 | } | |
1057 | ||
1058 | if (code == C_MAYBE_CONST_EXPR) | |
1059 | { | |
1060 | tree pre = C_MAYBE_CONST_EXPR_PRE (expr); | |
1061 | tree inner = C_MAYBE_CONST_EXPR_EXPR (expr); | |
1062 | if (C_MAYBE_CONST_EXPR_NON_CONST (expr)) | |
1063 | *maybe_const_operands = false; | |
1064 | if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr)) | |
1065 | *maybe_const_itself = false; | |
1066 | if (pre && !in_init) | |
1067 | ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner); | |
1068 | else | |
1069 | ret = inner; | |
1070 | goto out; | |
1071 | } | |
1072 | ||
1073 | /* Assignment, increment, decrement, function call and comma | |
1074 | operators, and statement expressions, cannot occur in constant | |
1075 | expressions if evaluated / outside of sizeof. (Function calls | |
1076 | were handled above, though VA_ARG_EXPR is treated like a function | |
1077 | call here, and statement expressions are handled through | |
1078 | C_MAYBE_CONST_EXPR to avoid folding inside them.) */ | |
1079 | switch (code) | |
1080 | { | |
1081 | case MODIFY_EXPR: | |
1082 | case PREDECREMENT_EXPR: | |
1083 | case PREINCREMENT_EXPR: | |
1084 | case POSTDECREMENT_EXPR: | |
1085 | case POSTINCREMENT_EXPR: | |
1086 | case COMPOUND_EXPR: | |
1087 | *maybe_const_operands = false; | |
1088 | break; | |
1089 | ||
1090 | case VA_ARG_EXPR: | |
1091 | case TARGET_EXPR: | |
1092 | case BIND_EXPR: | |
1093 | case OBJ_TYPE_REF: | |
1094 | *maybe_const_operands = false; | |
1095 | ret = fold (expr); | |
1096 | goto out; | |
1097 | ||
1098 | default: | |
1099 | break; | |
1100 | } | |
1101 | ||
1102 | /* Fold individual tree codes as appropriate. */ | |
1103 | switch (code) | |
1104 | { | |
1105 | case COMPOUND_LITERAL_EXPR: | |
1106 | /* Any non-constancy will have been marked in a containing | |
1107 | C_MAYBE_CONST_EXPR; there is no more folding to do here. */ | |
1108 | goto out; | |
1109 | ||
1110 | case COMPONENT_REF: | |
1111 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1112 | op1 = TREE_OPERAND (expr, 1); | |
1113 | op2 = TREE_OPERAND (expr, 2); | |
1114 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1115 | maybe_const_itself); | |
f59e3889 | 1116 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1117 | if (op0 != orig_op0) |
1118 | ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2); | |
1119 | if (ret != expr) | |
1120 | { | |
1121 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1122 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1123 | } | |
1124 | goto out; | |
1125 | ||
1126 | case ARRAY_REF: | |
1127 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1128 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1129 | op2 = TREE_OPERAND (expr, 2); | |
1130 | op3 = TREE_OPERAND (expr, 3); | |
1131 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1132 | maybe_const_itself); | |
f59e3889 | 1133 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1134 | op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, |
1135 | maybe_const_itself); | |
f59e3889 | 1136 | STRIP_TYPE_NOPS (op1); |
a75b1c71 | 1137 | op1 = decl_constant_value_for_optimization (op1); |
1138 | if (op0 != orig_op0 || op1 != orig_op1) | |
1139 | ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3); | |
1140 | if (ret != expr) | |
1141 | { | |
1142 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1143 | TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); | |
1144 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1145 | } | |
1146 | ret = fold (ret); | |
1147 | goto out; | |
1148 | ||
1149 | case COMPOUND_EXPR: | |
1150 | case MODIFY_EXPR: | |
1151 | case PREDECREMENT_EXPR: | |
1152 | case PREINCREMENT_EXPR: | |
1153 | case POSTDECREMENT_EXPR: | |
1154 | case POSTINCREMENT_EXPR: | |
1155 | case PLUS_EXPR: | |
1156 | case MINUS_EXPR: | |
1157 | case MULT_EXPR: | |
1158 | case POINTER_PLUS_EXPR: | |
1159 | case TRUNC_DIV_EXPR: | |
1160 | case CEIL_DIV_EXPR: | |
1161 | case FLOOR_DIV_EXPR: | |
1162 | case TRUNC_MOD_EXPR: | |
1163 | case RDIV_EXPR: | |
1164 | case EXACT_DIV_EXPR: | |
1165 | case LSHIFT_EXPR: | |
1166 | case RSHIFT_EXPR: | |
1167 | case BIT_IOR_EXPR: | |
1168 | case BIT_XOR_EXPR: | |
1169 | case BIT_AND_EXPR: | |
1170 | case LT_EXPR: | |
1171 | case LE_EXPR: | |
1172 | case GT_EXPR: | |
1173 | case GE_EXPR: | |
1174 | case EQ_EXPR: | |
1175 | case NE_EXPR: | |
1176 | case COMPLEX_EXPR: | |
1177 | case TRUTH_AND_EXPR: | |
1178 | case TRUTH_OR_EXPR: | |
1179 | case TRUTH_XOR_EXPR: | |
1180 | case UNORDERED_EXPR: | |
1181 | case ORDERED_EXPR: | |
1182 | case UNLT_EXPR: | |
1183 | case UNLE_EXPR: | |
1184 | case UNGT_EXPR: | |
1185 | case UNGE_EXPR: | |
1186 | case UNEQ_EXPR: | |
1187 | /* Binary operations evaluating both arguments (increment and | |
1188 | decrement are binary internally in GCC). */ | |
1189 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1190 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1191 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1192 | maybe_const_itself); | |
f59e3889 | 1193 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1194 | if (code != MODIFY_EXPR |
1195 | && code != PREDECREMENT_EXPR | |
1196 | && code != PREINCREMENT_EXPR | |
1197 | && code != POSTDECREMENT_EXPR | |
1198 | && code != POSTINCREMENT_EXPR) | |
1199 | op0 = decl_constant_value_for_optimization (op0); | |
1200 | /* The RHS of a MODIFY_EXPR was fully folded when building that | |
1201 | expression for the sake of conversion warnings. */ | |
1202 | if (code != MODIFY_EXPR) | |
1203 | op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands, | |
1204 | maybe_const_itself); | |
f59e3889 | 1205 | STRIP_TYPE_NOPS (op1); |
a75b1c71 | 1206 | op1 = decl_constant_value_for_optimization (op1); |
1207 | if (op0 != orig_op0 || op1 != orig_op1 || in_init) | |
1208 | ret = in_init | |
389dd41b | 1209 | ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) |
1210 | : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); | |
a75b1c71 | 1211 | else |
1212 | ret = fold (expr); | |
672d914b | 1213 | if (TREE_OVERFLOW_P (ret) |
1214 | && !TREE_OVERFLOW_P (op0) | |
1215 | && !TREE_OVERFLOW_P (op1)) | |
1216 | overflow_warning (EXPR_LOCATION (expr), ret); | |
a75b1c71 | 1217 | goto out; |
1218 | ||
1219 | case INDIRECT_REF: | |
1220 | case FIX_TRUNC_EXPR: | |
1221 | case FLOAT_EXPR: | |
1222 | CASE_CONVERT: | |
2b19dfe4 | 1223 | case VIEW_CONVERT_EXPR: |
a75b1c71 | 1224 | case NON_LVALUE_EXPR: |
1225 | case NEGATE_EXPR: | |
1226 | case BIT_NOT_EXPR: | |
1227 | case TRUTH_NOT_EXPR: | |
1228 | case ADDR_EXPR: | |
1229 | case CONJ_EXPR: | |
1230 | case REALPART_EXPR: | |
1231 | case IMAGPART_EXPR: | |
1232 | /* Unary operations. */ | |
1233 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1234 | op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands, | |
1235 | maybe_const_itself); | |
f59e3889 | 1236 | STRIP_TYPE_NOPS (op0); |
a75b1c71 | 1237 | if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR) |
1238 | op0 = decl_constant_value_for_optimization (op0); | |
1239 | if (op0 != orig_op0 || in_init) | |
1240 | ret = in_init | |
389dd41b | 1241 | ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0) |
1242 | : fold_build1_loc (loc, code, TREE_TYPE (expr), op0); | |
a75b1c71 | 1243 | else |
1244 | ret = fold (expr); | |
1245 | if (code == INDIRECT_REF | |
1246 | && ret != expr | |
1247 | && TREE_CODE (ret) == INDIRECT_REF) | |
1248 | { | |
1249 | TREE_READONLY (ret) = TREE_READONLY (expr); | |
1250 | TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr); | |
1251 | TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr); | |
1252 | } | |
672d914b | 1253 | switch (code) |
1254 | { | |
1255 | case FIX_TRUNC_EXPR: | |
1256 | case FLOAT_EXPR: | |
1257 | CASE_CONVERT: | |
1258 | /* Don't warn about explicit conversions. We will already | |
1259 | have warned about suspect implicit conversions. */ | |
1260 | break; | |
1261 | ||
1262 | default: | |
1263 | if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0)) | |
1264 | overflow_warning (EXPR_LOCATION (expr), ret); | |
1265 | break; | |
1266 | } | |
a75b1c71 | 1267 | goto out; |
1268 | ||
1269 | case TRUTH_ANDIF_EXPR: | |
1270 | case TRUTH_ORIF_EXPR: | |
1271 | /* Binary operations not necessarily evaluating both | |
1272 | arguments. */ | |
1273 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1274 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1275 | op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); | |
f59e3889 | 1276 | STRIP_TYPE_NOPS (op0); |
672d914b | 1277 | |
1278 | unused_p = (op0 == (code == TRUTH_ANDIF_EXPR | |
1279 | ? truthvalue_false_node | |
1280 | : truthvalue_true_node)); | |
1281 | c_inhibit_evaluation_warnings += unused_p; | |
a75b1c71 | 1282 | op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); |
f59e3889 | 1283 | STRIP_TYPE_NOPS (op1); |
672d914b | 1284 | c_inhibit_evaluation_warnings -= unused_p; |
1285 | ||
a75b1c71 | 1286 | if (op0 != orig_op0 || op1 != orig_op1 || in_init) |
1287 | ret = in_init | |
389dd41b | 1288 | ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1) |
1289 | : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1); | |
a75b1c71 | 1290 | else |
1291 | ret = fold (expr); | |
1292 | *maybe_const_operands &= op0_const; | |
1293 | *maybe_const_itself &= op0_const_self; | |
1294 | if (!(flag_isoc99 | |
1295 | && op0_const | |
1296 | && op0_const_self | |
1297 | && (code == TRUTH_ANDIF_EXPR | |
1298 | ? op0 == truthvalue_false_node | |
1299 | : op0 == truthvalue_true_node))) | |
1300 | *maybe_const_operands &= op1_const; | |
1301 | if (!(op0_const | |
1302 | && op0_const_self | |
1303 | && (code == TRUTH_ANDIF_EXPR | |
1304 | ? op0 == truthvalue_false_node | |
1305 | : op0 == truthvalue_true_node))) | |
1306 | *maybe_const_itself &= op1_const_self; | |
1307 | goto out; | |
1308 | ||
1309 | case COND_EXPR: | |
1310 | orig_op0 = op0 = TREE_OPERAND (expr, 0); | |
1311 | orig_op1 = op1 = TREE_OPERAND (expr, 1); | |
1312 | orig_op2 = op2 = TREE_OPERAND (expr, 2); | |
1313 | op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self); | |
672d914b | 1314 | |
f59e3889 | 1315 | STRIP_TYPE_NOPS (op0); |
672d914b | 1316 | c_inhibit_evaluation_warnings += (op0 == truthvalue_false_node); |
a75b1c71 | 1317 | op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self); |
f59e3889 | 1318 | STRIP_TYPE_NOPS (op1); |
672d914b | 1319 | c_inhibit_evaluation_warnings -= (op0 == truthvalue_false_node); |
1320 | ||
1321 | c_inhibit_evaluation_warnings += (op0 == truthvalue_true_node); | |
a75b1c71 | 1322 | op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self); |
f59e3889 | 1323 | STRIP_TYPE_NOPS (op2); |
672d914b | 1324 | c_inhibit_evaluation_warnings -= (op0 == truthvalue_true_node); |
1325 | ||
a75b1c71 | 1326 | if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2) |
389dd41b | 1327 | ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2); |
a75b1c71 | 1328 | else |
1329 | ret = fold (expr); | |
1330 | *maybe_const_operands &= op0_const; | |
1331 | *maybe_const_itself &= op0_const_self; | |
1332 | if (!(flag_isoc99 | |
1333 | && op0_const | |
1334 | && op0_const_self | |
1335 | && op0 == truthvalue_false_node)) | |
1336 | *maybe_const_operands &= op1_const; | |
1337 | if (!(op0_const | |
1338 | && op0_const_self | |
1339 | && op0 == truthvalue_false_node)) | |
1340 | *maybe_const_itself &= op1_const_self; | |
1341 | if (!(flag_isoc99 | |
1342 | && op0_const | |
1343 | && op0_const_self | |
1344 | && op0 == truthvalue_true_node)) | |
1345 | *maybe_const_operands &= op2_const; | |
1346 | if (!(op0_const | |
1347 | && op0_const_self | |
1348 | && op0 == truthvalue_true_node)) | |
1349 | *maybe_const_itself &= op2_const_self; | |
1350 | goto out; | |
1351 | ||
c6418a4e | 1352 | case EXCESS_PRECISION_EXPR: |
1353 | /* Each case where an operand with excess precision may be | |
1354 | encountered must remove the EXCESS_PRECISION_EXPR around | |
1355 | inner operands and possibly put one around the whole | |
1356 | expression or possibly convert to the semantic type (which | |
1357 | c_fully_fold does); we cannot tell at this stage which is | |
1358 | appropriate in any particular case. */ | |
1359 | gcc_unreachable (); | |
1360 | ||
a75b1c71 | 1361 | default: |
1362 | /* Various codes may appear through folding built-in functions | |
1363 | and their arguments. */ | |
1364 | goto out; | |
1365 | } | |
1366 | ||
1367 | out: | |
1368 | /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks | |
1369 | have been done by this point, so remove them again. */ | |
1370 | nowarning |= TREE_NO_WARNING (ret); | |
1371 | STRIP_TYPE_NOPS (ret); | |
1372 | if (nowarning && !TREE_NO_WARNING (ret)) | |
1373 | { | |
1374 | if (!CAN_HAVE_LOCATION_P (ret)) | |
1375 | ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret); | |
1376 | TREE_NO_WARNING (ret) = 1; | |
1377 | } | |
1378 | if (ret != expr) | |
1379 | protected_set_expr_location (ret, loc); | |
1380 | return ret; | |
1381 | } | |
1382 | ||
1383 | /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type, | |
1384 | return EXP. Otherwise, return either EXP or its known constant | |
1385 | value (if it has one), but return EXP if EXP has mode BLKmode. ??? | |
1386 | Is the BLKmode test appropriate? */ | |
1387 | ||
1388 | tree | |
1389 | decl_constant_value_for_optimization (tree exp) | |
1390 | { | |
1391 | tree ret; | |
1392 | ||
1393 | /* This function is only used by C, for c_fully_fold and other | |
1394 | optimization, and may not be correct for C++. */ | |
1395 | if (c_dialect_cxx ()) | |
1396 | gcc_unreachable (); | |
1397 | ||
1398 | if (!optimize | |
1399 | || TREE_CODE (exp) != VAR_DECL | |
1400 | || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE | |
1401 | || DECL_MODE (exp) == BLKmode) | |
1402 | return exp; | |
1403 | ||
1404 | ret = decl_constant_value (exp); | |
1405 | /* Avoid unwanted tree sharing between the initializer and current | |
1406 | function's body where the tree can be modified e.g. by the | |
1407 | gimplifier. */ | |
1408 | if (ret != exp && TREE_STATIC (exp)) | |
1409 | ret = unshare_expr (ret); | |
1410 | return ret; | |
1411 | } | |
1412 | ||
2a1736ed | 1413 | /* Print a warning if a constant expression had overflow in folding. |
1414 | Invoke this function on every expression that the language | |
1415 | requires to be a constant expression. | |
1416 | Note the ANSI C standard says it is erroneous for a | |
1417 | constant expression to overflow. */ | |
b2806639 | 1418 | |
1419 | void | |
1cae46be | 1420 | constant_expression_warning (tree value) |
07317e69 | 1421 | { |
48e1416a | 1422 | if (warn_overflow && pedantic |
07317e69 | 1423 | && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
1424 | || TREE_CODE (value) == FIXED_CST | |
1425 | || TREE_CODE (value) == VECTOR_CST | |
1426 | || TREE_CODE (value) == COMPLEX_CST) | |
1427 | && TREE_OVERFLOW (value)) | |
21ca8540 | 1428 | pedwarn (input_location, OPT_Woverflow, "overflow in constant expression"); |
07317e69 | 1429 | } |
1430 | ||
1431 | /* The same as above but print an unconditional error. */ | |
1432 | void | |
1433 | constant_expression_error (tree value) | |
b2806639 | 1434 | { |
837e1122 | 1435 | if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST |
9421ebb9 | 1436 | || TREE_CODE (value) == FIXED_CST |
886cfd4f | 1437 | || TREE_CODE (value) == VECTOR_CST |
837e1122 | 1438 | || TREE_CODE (value) == COMPLEX_CST) |
07317e69 | 1439 | && TREE_OVERFLOW (value)) |
1440 | error ("overflow in constant expression"); | |
2a1736ed | 1441 | } |
1442 | ||
f170d67f | 1443 | /* Print a warning if an expression had overflow in folding and its |
1444 | operands hadn't. | |
1445 | ||
2a1736ed | 1446 | Invoke this function on every expression that |
1447 | (1) appears in the source code, and | |
f170d67f | 1448 | (2) is a constant expression that overflowed, and |
2a1736ed | 1449 | (3) is not already checked by convert_and_check; |
f170d67f | 1450 | however, do not invoke this function on operands of explicit casts |
1451 | or when the expression is the result of an operator and any operand | |
1452 | already overflowed. */ | |
2a1736ed | 1453 | |
1454 | void | |
e60a6f7b | 1455 | overflow_warning (location_t loc, tree value) |
2a1736ed | 1456 | { |
48d94ede | 1457 | if (c_inhibit_evaluation_warnings != 0) |
1458 | return; | |
f170d67f | 1459 | |
1460 | switch (TREE_CODE (value)) | |
886cfd4f | 1461 | { |
f170d67f | 1462 | case INTEGER_CST: |
e60a6f7b | 1463 | warning_at (loc, OPT_Woverflow, "integer overflow in expression"); |
f170d67f | 1464 | break; |
48e1416a | 1465 | |
f170d67f | 1466 | case REAL_CST: |
e60a6f7b | 1467 | warning_at (loc, OPT_Woverflow, |
1468 | "floating point overflow in expression"); | |
f170d67f | 1469 | break; |
48e1416a | 1470 | |
9421ebb9 | 1471 | case FIXED_CST: |
e60a6f7b | 1472 | warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression"); |
9421ebb9 | 1473 | break; |
1474 | ||
f170d67f | 1475 | case VECTOR_CST: |
e60a6f7b | 1476 | warning_at (loc, OPT_Woverflow, "vector overflow in expression"); |
f170d67f | 1477 | break; |
48e1416a | 1478 | |
f170d67f | 1479 | case COMPLEX_CST: |
1480 | if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST) | |
e60a6f7b | 1481 | warning_at (loc, OPT_Woverflow, |
1482 | "complex integer overflow in expression"); | |
f170d67f | 1483 | else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST) |
e60a6f7b | 1484 | warning_at (loc, OPT_Woverflow, |
1485 | "complex floating point overflow in expression"); | |
f170d67f | 1486 | break; |
1487 | ||
1488 | default: | |
1489 | break; | |
886cfd4f | 1490 | } |
2a1736ed | 1491 | } |
1492 | ||
03033af4 | 1493 | /* Warn about uses of logical || / && operator in a context where it |
1494 | is likely that the bitwise equivalent was intended by the | |
1495 | programmer. We have seen an expression in which CODE is a binary | |
9c20c4fc | 1496 | operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding |
1497 | had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */ | |
b13d1547 | 1498 | void |
9c20c4fc | 1499 | warn_logical_operator (location_t location, enum tree_code code, tree type, |
48e1416a | 1500 | enum tree_code code_left, tree op_left, |
03033af4 | 1501 | enum tree_code ARG_UNUSED (code_right), tree op_right) |
b13d1547 | 1502 | { |
9c20c4fc | 1503 | int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR); |
1504 | int in0_p, in1_p, in_p; | |
1505 | tree low0, low1, low, high0, high1, high, lhs, rhs, tem; | |
1506 | bool strict_overflow_p = false; | |
1507 | ||
03033af4 | 1508 | if (code != TRUTH_ANDIF_EXPR |
1509 | && code != TRUTH_AND_EXPR | |
1510 | && code != TRUTH_ORIF_EXPR | |
1511 | && code != TRUTH_OR_EXPR) | |
1512 | return; | |
1513 | ||
1514 | /* Warn if &&/|| are being used in a context where it is | |
1515 | likely that the bitwise equivalent was intended by the | |
1516 | programmer. That is, an expression such as op && MASK | |
1517 | where op should not be any boolean expression, nor a | |
1518 | constant, and mask seems to be a non-boolean integer constant. */ | |
1519 | if (!truth_value_p (code_left) | |
1520 | && INTEGRAL_TYPE_P (TREE_TYPE (op_left)) | |
1521 | && !CONSTANT_CLASS_P (op_left) | |
1522 | && !TREE_NO_WARNING (op_left) | |
1523 | && TREE_CODE (op_right) == INTEGER_CST | |
1524 | && !integer_zerop (op_right) | |
1525 | && !integer_onep (op_right)) | |
b13d1547 | 1526 | { |
9c20c4fc | 1527 | if (or_op) |
03033af4 | 1528 | warning_at (location, OPT_Wlogical_op, "logical %<or%>" |
1529 | " applied to non-boolean constant"); | |
1530 | else | |
1531 | warning_at (location, OPT_Wlogical_op, "logical %<and%>" | |
1532 | " applied to non-boolean constant"); | |
1533 | TREE_NO_WARNING (op_left) = true; | |
9c20c4fc | 1534 | return; |
1535 | } | |
1536 | ||
1537 | /* We do not warn for constants because they are typical of macro | |
1538 | expansions that test for features. */ | |
1539 | if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right)) | |
1540 | return; | |
1541 | ||
1542 | /* This warning only makes sense with logical operands. */ | |
1543 | if (!(truth_value_p (TREE_CODE (op_left)) | |
1544 | || INTEGRAL_TYPE_P (TREE_TYPE (op_left))) | |
1545 | || !(truth_value_p (TREE_CODE (op_right)) | |
1546 | || INTEGRAL_TYPE_P (TREE_TYPE (op_right)))) | |
1547 | return; | |
1548 | ||
1549 | lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p); | |
1550 | rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p); | |
1551 | ||
1552 | if (lhs && TREE_CODE (lhs) == C_MAYBE_CONST_EXPR) | |
1553 | lhs = C_MAYBE_CONST_EXPR_EXPR (lhs); | |
1554 | ||
1555 | if (rhs && TREE_CODE (rhs) == C_MAYBE_CONST_EXPR) | |
1556 | rhs = C_MAYBE_CONST_EXPR_EXPR (rhs); | |
48e1416a | 1557 | |
9c20c4fc | 1558 | /* If this is an OR operation, invert both sides; we will invert |
1559 | again at the end. */ | |
1560 | if (or_op) | |
1561 | in0_p = !in0_p, in1_p = !in1_p; | |
48e1416a | 1562 | |
9c20c4fc | 1563 | /* If both expressions are the same, if we can merge the ranges, and we |
0969aa9b | 1564 | can build the range test, return it or it inverted. */ |
1565 | if (lhs && rhs && operand_equal_p (lhs, rhs, 0) | |
9c20c4fc | 1566 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, |
1567 | in1_p, low1, high1) | |
389dd41b | 1568 | && 0 != (tem = build_range_check (UNKNOWN_LOCATION, |
1569 | type, lhs, in_p, low, high))) | |
9c20c4fc | 1570 | { |
1571 | if (TREE_CODE (tem) != INTEGER_CST) | |
1572 | return; | |
1573 | ||
1574 | if (or_op) | |
1575 | warning_at (location, OPT_Wlogical_op, | |
1576 | "logical %<or%> " | |
1577 | "of collectively exhaustive tests is always true"); | |
1578 | else | |
1579 | warning_at (location, OPT_Wlogical_op, | |
1580 | "logical %<and%> " | |
1581 | "of mutually exclusive tests is always false"); | |
b13d1547 | 1582 | } |
1583 | } | |
1584 | ||
1585 | ||
bcf22371 | 1586 | /* Print a warning about casts that might indicate violation |
1587 | of strict aliasing rules if -Wstrict-aliasing is used and | |
1e31ff37 | 1588 | strict aliasing mode is in effect. OTYPE is the original |
1589 | TREE_TYPE of EXPR, and TYPE the type we're casting to. */ | |
bcf22371 | 1590 | |
e6fa0ea6 | 1591 | bool |
1e31ff37 | 1592 | strict_aliasing_warning (tree otype, tree type, tree expr) |
bcf22371 | 1593 | { |
45bb3afb | 1594 | /* Strip pointer conversion chains and get to the correct original type. */ |
1595 | STRIP_NOPS (expr); | |
1596 | otype = TREE_TYPE (expr); | |
1597 | ||
f06537f2 | 1598 | if (!(flag_strict_aliasing |
1599 | && POINTER_TYPE_P (type) | |
1600 | && POINTER_TYPE_P (otype) | |
1601 | && !VOID_TYPE_P (TREE_TYPE (type))) | |
1602 | /* If the type we are casting to is a ref-all pointer | |
1603 | dereferencing it is always valid. */ | |
1604 | || TYPE_REF_CAN_ALIAS_ALL (type)) | |
e6fa0ea6 | 1605 | return false; |
1606 | ||
1607 | if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR | |
bcf22371 | 1608 | && (DECL_P (TREE_OPERAND (expr, 0)) |
e6fa0ea6 | 1609 | || handled_component_p (TREE_OPERAND (expr, 0)))) |
bcf22371 | 1610 | { |
1611 | /* Casting the address of an object to non void pointer. Warn | |
1612 | if the cast breaks type based aliasing. */ | |
e6fa0ea6 | 1613 | if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) |
1614 | { | |
1615 | warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " | |
1616 | "might break strict-aliasing rules"); | |
1617 | return true; | |
1618 | } | |
bcf22371 | 1619 | else |
1620 | { | |
48e1416a | 1621 | /* warn_strict_aliasing >= 3. This includes the default (3). |
e6fa0ea6 | 1622 | Only warn if the cast is dereferenced immediately. */ |
32c2fdea | 1623 | alias_set_type set1 = |
e6fa0ea6 | 1624 | get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); |
32c2fdea | 1625 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); |
bcf22371 | 1626 | |
62d823d0 | 1627 | if (set1 != set2 && set2 != 0 |
1628 | && (set1 == 0 || !alias_sets_conflict_p (set1, set2))) | |
e6fa0ea6 | 1629 | { |
1630 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1631 | "pointer will break strict-aliasing rules"); | |
1632 | return true; | |
1633 | } | |
1634 | else if (warn_strict_aliasing == 2 | |
879f881c | 1635 | && !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1636 | { |
1637 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1638 | "pointer might break strict-aliasing rules"); | |
1639 | return true; | |
1640 | } | |
bcf22371 | 1641 | } |
1642 | } | |
e6fa0ea6 | 1643 | else |
1644 | if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) | |
1645 | { | |
1646 | /* At this level, warn for any conversions, even if an address is | |
1647 | not taken in the same statement. This will likely produce many | |
1648 | false positives, but could be useful to pinpoint problems that | |
1649 | are not revealed at higher levels. */ | |
32c2fdea | 1650 | alias_set_type set1 = get_alias_set (TREE_TYPE (otype)); |
1651 | alias_set_type set2 = get_alias_set (TREE_TYPE (type)); | |
1652 | if (!COMPLETE_TYPE_P (type) | |
879f881c | 1653 | || !alias_sets_must_conflict_p (set1, set2)) |
e6fa0ea6 | 1654 | { |
1655 | warning (OPT_Wstrict_aliasing, "dereferencing type-punned " | |
1656 | "pointer might break strict-aliasing rules"); | |
1657 | return true; | |
1658 | } | |
1659 | } | |
1660 | ||
1661 | return false; | |
bcf22371 | 1662 | } |
1663 | ||
3f08e399 | 1664 | /* Warn for unlikely, improbable, or stupid DECL declarations |
1665 | of `main'. */ | |
1666 | ||
1667 | void | |
1668 | check_main_parameter_types (tree decl) | |
1669 | { | |
d0af78c5 | 1670 | function_args_iterator iter; |
1671 | tree type; | |
3f08e399 | 1672 | int argct = 0; |
1673 | ||
d0af78c5 | 1674 | FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter) |
1675 | { | |
1676 | /* XXX void_type_node belies the abstraction. */ | |
1677 | if (type == void_type_node || type == error_mark_node ) | |
1678 | break; | |
1679 | ||
1680 | ++argct; | |
1681 | switch (argct) | |
1682 | { | |
1683 | case 1: | |
1684 | if (TYPE_MAIN_VARIANT (type) != integer_type_node) | |
1685 | pedwarn (input_location, OPT_Wmain, | |
1686 | "first argument of %q+D should be %<int%>", decl); | |
1687 | break; | |
1688 | ||
1689 | case 2: | |
1690 | if (TREE_CODE (type) != POINTER_TYPE | |
1691 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
1692 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
1693 | != char_type_node)) | |
1694 | pedwarn (input_location, OPT_Wmain, | |
1695 | "second argument of %q+D should be %<char **%>", decl); | |
1696 | break; | |
1697 | ||
1698 | case 3: | |
1699 | if (TREE_CODE (type) != POINTER_TYPE | |
1700 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
1701 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
1702 | != char_type_node)) | |
1703 | pedwarn (input_location, OPT_Wmain, | |
1704 | "third argument of %q+D should probably be " | |
1705 | "%<char **%>", decl); | |
1706 | break; | |
1707 | } | |
1708 | } | |
3f08e399 | 1709 | |
1710 | /* It is intentional that this message does not mention the third | |
1711 | argument because it's only mentioned in an appendix of the | |
1712 | standard. */ | |
1713 | if (argct > 0 && (argct < 2 || argct > 3)) | |
d0af78c5 | 1714 | pedwarn (input_location, OPT_Wmain, |
1715 | "%q+D takes only zero or two arguments", decl); | |
3f08e399 | 1716 | } |
1717 | ||
ed7c4e62 | 1718 | /* True if pointers to distinct types T1 and T2 can be converted to |
1719 | each other without an explicit cast. Only returns true for opaque | |
1720 | vector types. */ | |
1721 | bool | |
1722 | vector_targets_convertible_p (const_tree t1, const_tree t2) | |
1723 | { | |
1724 | if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE | |
8d125f7d | 1725 | && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
ed7c4e62 | 1726 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
1727 | return true; | |
1728 | ||
1729 | return false; | |
1730 | } | |
1731 | ||
546c4794 | 1732 | /* True if vector types T1 and T2 can be converted to each other |
1733 | without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2 | |
1734 | can only be converted with -flax-vector-conversions yet that is not | |
1735 | in effect, emit a note telling the user about that option if such | |
1736 | a note has not previously been emitted. */ | |
1737 | bool | |
9f627b1a | 1738 | vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note) |
8b4b9810 | 1739 | { |
546c4794 | 1740 | static bool emitted_lax_note = false; |
ae6db8ab | 1741 | bool convertible_lax; |
1742 | ||
8d125f7d | 1743 | if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2)) |
ae6db8ab | 1744 | && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
1745 | return true; | |
1746 | ||
1747 | convertible_lax = | |
1748 | (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) | |
1749 | && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || | |
1750 | TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) | |
1751 | && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) | |
1752 | == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); | |
546c4794 | 1753 | |
1754 | if (!convertible_lax || flag_lax_vector_conversions) | |
1755 | return convertible_lax; | |
1756 | ||
1757 | if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) | |
29f51994 | 1758 | && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
546c4794 | 1759 | return true; |
1760 | ||
1761 | if (emit_lax_note && !emitted_lax_note) | |
1762 | { | |
1763 | emitted_lax_note = true; | |
5bcc316e | 1764 | inform (input_location, "use -flax-vector-conversions to permit " |
546c4794 | 1765 | "conversions between vectors with differing " |
1766 | "element types or numbers of subparts"); | |
1767 | } | |
1768 | ||
1769 | return false; | |
8b4b9810 | 1770 | } |
1771 | ||
7f506bca | 1772 | /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum |
1773 | to integral type. */ | |
1774 | ||
1775 | static tree | |
1776 | c_common_get_narrower (tree op, int *unsignedp_ptr) | |
1777 | { | |
1778 | op = get_narrower (op, unsignedp_ptr); | |
1779 | ||
1780 | if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE | |
1781 | && ENUM_IS_SCOPED (TREE_TYPE (op))) | |
1782 | { | |
1783 | /* C++0x scoped enumerations don't implicitly convert to integral | |
1784 | type; if we stripped an explicit conversion to a larger type we | |
1785 | need to replace it so common_type will still work. */ | |
1786 | tree type = (lang_hooks.types.type_for_size | |
1787 | (TYPE_PRECISION (TREE_TYPE (op)), | |
1788 | TYPE_UNSIGNED (TREE_TYPE (op)))); | |
1789 | op = fold_convert (type, op); | |
1790 | } | |
1791 | return op; | |
1792 | } | |
1793 | ||
2561cea2 | 1794 | /* This is a helper function of build_binary_op. |
1795 | ||
1796 | For certain operations if both args were extended from the same | |
1797 | smaller type, do the arithmetic in that type and then extend. | |
1798 | ||
1799 | BITWISE indicates a bitwise operation. | |
1800 | For them, this optimization is safe only if | |
1801 | both args are zero-extended or both are sign-extended. | |
1802 | Otherwise, we might change the result. | |
1803 | Eg, (short)-1 | (unsigned short)-1 is (int)-1 | |
48e1416a | 1804 | but calculated in (unsigned short) it would be (unsigned short)-1. |
2561cea2 | 1805 | */ |
7f506bca | 1806 | tree |
1807 | shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise) | |
2561cea2 | 1808 | { |
1809 | int unsigned0, unsigned1; | |
1810 | tree arg0, arg1; | |
1811 | int uns; | |
1812 | tree type; | |
1813 | ||
1814 | /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents | |
1815 | excessive narrowing when we call get_narrower below. For | |
1816 | example, suppose that OP0 is of unsigned int extended | |
1817 | from signed char and that RESULT_TYPE is long long int. | |
1818 | If we explicitly cast OP0 to RESULT_TYPE, OP0 would look | |
1819 | like | |
48e1416a | 1820 | |
2561cea2 | 1821 | (long long int) (unsigned int) signed_char |
1822 | ||
1823 | which get_narrower would narrow down to | |
48e1416a | 1824 | |
2561cea2 | 1825 | (unsigned int) signed char |
48e1416a | 1826 | |
2561cea2 | 1827 | If we do not cast OP0 first, get_narrower would return |
1828 | signed_char, which is inconsistent with the case of the | |
1829 | explicit cast. */ | |
1830 | op0 = convert (result_type, op0); | |
1831 | op1 = convert (result_type, op1); | |
1832 | ||
7f506bca | 1833 | arg0 = c_common_get_narrower (op0, &unsigned0); |
1834 | arg1 = c_common_get_narrower (op1, &unsigned1); | |
2561cea2 | 1835 | |
1836 | /* UNS is 1 if the operation to be done is an unsigned one. */ | |
1837 | uns = TYPE_UNSIGNED (result_type); | |
1838 | ||
1839 | /* Handle the case that OP0 (or OP1) does not *contain* a conversion | |
1840 | but it *requires* conversion to FINAL_TYPE. */ | |
48e1416a | 1841 | |
2561cea2 | 1842 | if ((TYPE_PRECISION (TREE_TYPE (op0)) |
1843 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
1844 | && TREE_TYPE (op0) != result_type) | |
1845 | unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); | |
1846 | if ((TYPE_PRECISION (TREE_TYPE (op1)) | |
1847 | == TYPE_PRECISION (TREE_TYPE (arg1))) | |
1848 | && TREE_TYPE (op1) != result_type) | |
1849 | unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
48e1416a | 1850 | |
2561cea2 | 1851 | /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */ |
48e1416a | 1852 | |
2561cea2 | 1853 | /* For bitwise operations, signedness of nominal type |
1854 | does not matter. Consider only how operands were extended. */ | |
1855 | if (bitwise) | |
1856 | uns = unsigned0; | |
48e1416a | 1857 | |
2561cea2 | 1858 | /* Note that in all three cases below we refrain from optimizing |
1859 | an unsigned operation on sign-extended args. | |
1860 | That would not be valid. */ | |
48e1416a | 1861 | |
2561cea2 | 1862 | /* Both args variable: if both extended in same way |
1863 | from same width, do it in that width. | |
1864 | Do it unsigned if args were zero-extended. */ | |
1865 | if ((TYPE_PRECISION (TREE_TYPE (arg0)) | |
1866 | < TYPE_PRECISION (result_type)) | |
1867 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
1868 | == TYPE_PRECISION (TREE_TYPE (arg0))) | |
1869 | && unsigned0 == unsigned1 | |
1870 | && (unsigned0 || !uns)) | |
1871 | return c_common_signed_or_unsigned_type | |
1872 | (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1))); | |
1873 | ||
1874 | else if (TREE_CODE (arg0) == INTEGER_CST | |
1875 | && (unsigned1 || !uns) | |
1876 | && (TYPE_PRECISION (TREE_TYPE (arg1)) | |
1877 | < TYPE_PRECISION (result_type)) | |
1878 | && (type | |
1879 | = c_common_signed_or_unsigned_type (unsigned1, | |
1880 | TREE_TYPE (arg1))) | |
1881 | && !POINTER_TYPE_P (type) | |
1882 | && int_fits_type_p (arg0, type)) | |
1883 | return type; | |
1884 | ||
1885 | else if (TREE_CODE (arg1) == INTEGER_CST | |
1886 | && (unsigned0 || !uns) | |
1887 | && (TYPE_PRECISION (TREE_TYPE (arg0)) | |
1888 | < TYPE_PRECISION (result_type)) | |
1889 | && (type | |
1890 | = c_common_signed_or_unsigned_type (unsigned0, | |
1891 | TREE_TYPE (arg0))) | |
1892 | && !POINTER_TYPE_P (type) | |
1893 | && int_fits_type_p (arg1, type)) | |
1894 | return type; | |
1895 | ||
1896 | return result_type; | |
1897 | } | |
1898 | ||
d31d55f0 | 1899 | /* Warns if the conversion of EXPR to TYPE may alter a value. |
59dd8856 | 1900 | This is a helper function for warnings_for_convert_and_check. */ |
d31d55f0 | 1901 | |
1902 | static void | |
1903 | conversion_warning (tree type, tree expr) | |
1904 | { | |
1905 | bool give_warning = false; | |
1906 | ||
5b16c152 | 1907 | int i; |
1908 | const int expr_num_operands = TREE_OPERAND_LENGTH (expr); | |
27259707 | 1909 | tree expr_type = TREE_TYPE (expr); |
8637f1db | 1910 | location_t loc = EXPR_LOC_OR_HERE (expr); |
d31d55f0 | 1911 | |
7ee0d227 | 1912 | if (!warn_conversion && !warn_sign_conversion) |
1913 | return; | |
1914 | ||
5b16c152 | 1915 | /* If any operand is artificial, then this expression was generated |
1916 | by the compiler and we do not warn. */ | |
1917 | for (i = 0; i < expr_num_operands; i++) | |
1918 | { | |
1919 | tree op = TREE_OPERAND (expr, i); | |
7bdc5064 | 1920 | if (op && DECL_P (op) && DECL_ARTIFICIAL (op)) |
5b16c152 | 1921 | return; |
1922 | } | |
1923 | ||
27259707 | 1924 | switch (TREE_CODE (expr)) |
d31d55f0 | 1925 | { |
27259707 | 1926 | case EQ_EXPR: |
1927 | case NE_EXPR: | |
1928 | case LE_EXPR: | |
1929 | case GE_EXPR: | |
1930 | case LT_EXPR: | |
1931 | case GT_EXPR: | |
1932 | case TRUTH_ANDIF_EXPR: | |
1933 | case TRUTH_ORIF_EXPR: | |
1934 | case TRUTH_AND_EXPR: | |
1935 | case TRUTH_OR_EXPR: | |
1936 | case TRUTH_XOR_EXPR: | |
1937 | case TRUTH_NOT_EXPR: | |
1938 | /* Conversion from boolean to a signed:1 bit-field (which only | |
1939 | can hold the values 0 and -1) doesn't lose information - but | |
1940 | it does change the value. */ | |
48e1416a | 1941 | if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type)) |
200dd99c | 1942 | warning_at (loc, OPT_Wconversion, |
1943 | "conversion to %qT from boolean expression", type); | |
27259707 | 1944 | return; |
1945 | ||
1946 | case REAL_CST: | |
1947 | case INTEGER_CST: | |
1948 | ||
d31d55f0 | 1949 | /* Warn for real constant that is not an exact integer converted |
1950 | to integer type. */ | |
27259707 | 1951 | if (TREE_CODE (expr_type) == REAL_TYPE |
d31d55f0 | 1952 | && TREE_CODE (type) == INTEGER_TYPE) |
1953 | { | |
27259707 | 1954 | if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) |
d31d55f0 | 1955 | give_warning = true; |
1956 | } | |
da1fb07b | 1957 | /* Warn for an integer constant that does not fit into integer type. */ |
27259707 | 1958 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
d31d55f0 | 1959 | && TREE_CODE (type) == INTEGER_TYPE |
da1fb07b | 1960 | && !int_fits_type_p (expr, type)) |
1961 | { | |
48e1416a | 1962 | if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type) |
27259707 | 1963 | && tree_int_cst_sgn (expr) < 0) |
200dd99c | 1964 | warning_at (loc, OPT_Wsign_conversion, "negative integer" |
1965 | " implicitly converted to unsigned type"); | |
27259707 | 1966 | else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type)) |
200dd99c | 1967 | warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned" |
1968 | " constant value to negative integer"); | |
7ee0d227 | 1969 | else |
1970 | give_warning = true; | |
da1fb07b | 1971 | } |
d31d55f0 | 1972 | else if (TREE_CODE (type) == REAL_TYPE) |
1973 | { | |
1974 | /* Warn for an integer constant that does not fit into real type. */ | |
27259707 | 1975 | if (TREE_CODE (expr_type) == INTEGER_TYPE) |
d31d55f0 | 1976 | { |
1977 | REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); | |
1978 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
1979 | give_warning = true; | |
1980 | } | |
1981 | /* Warn for a real constant that does not fit into a smaller | |
1982 | real type. */ | |
27259707 | 1983 | else if (TREE_CODE (expr_type) == REAL_TYPE |
1984 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) | |
d31d55f0 | 1985 | { |
1986 | REAL_VALUE_TYPE a = TREE_REAL_CST (expr); | |
1987 | if (!exact_real_truncate (TYPE_MODE (type), &a)) | |
1988 | give_warning = true; | |
1989 | } | |
1990 | } | |
1991 | ||
1992 | if (give_warning) | |
200dd99c | 1993 | warning_at (loc, OPT_Wconversion, |
1994 | "conversion to %qT alters %qT constant value", | |
1995 | type, expr_type); | |
27259707 | 1996 | |
1997 | return; | |
1998 | ||
1999 | case COND_EXPR: | |
2000 | { | |
2001 | /* In case of COND_EXPR, if both operands are constants or | |
2002 | COND_EXPR, then we do not care about the type of COND_EXPR, | |
2003 | only about the conversion of each operand. */ | |
2004 | tree op1 = TREE_OPERAND (expr, 1); | |
2005 | tree op2 = TREE_OPERAND (expr, 2); | |
2006 | ||
48e1416a | 2007 | if ((TREE_CODE (op1) == REAL_CST || TREE_CODE (op1) == INTEGER_CST |
27259707 | 2008 | || TREE_CODE (op1) == COND_EXPR) |
2009 | && (TREE_CODE (op2) == REAL_CST || TREE_CODE (op2) == INTEGER_CST | |
2010 | || TREE_CODE (op2) == COND_EXPR)) | |
2011 | { | |
2012 | conversion_warning (type, op1); | |
2013 | conversion_warning (type, op2); | |
2014 | return; | |
2015 | } | |
2016 | /* Fall through. */ | |
2017 | } | |
2018 | ||
2019 | default: /* 'expr' is not a constant. */ | |
2561cea2 | 2020 | |
d31d55f0 | 2021 | /* Warn for real types converted to integer types. */ |
2561cea2 | 2022 | if (TREE_CODE (expr_type) == REAL_TYPE |
d31d55f0 | 2023 | && TREE_CODE (type) == INTEGER_TYPE) |
2024 | give_warning = true; | |
2025 | ||
2561cea2 | 2026 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
d31d55f0 | 2027 | && TREE_CODE (type) == INTEGER_TYPE) |
2028 | { | |
69609004 | 2029 | /* Don't warn about unsigned char y = 0xff, x = (int) y; */ |
f9d856a4 | 2030 | expr = get_unwidened (expr, 0); |
2561cea2 | 2031 | expr_type = TREE_TYPE (expr); |
69609004 | 2032 | |
2561cea2 | 2033 | /* Don't warn for short y; short x = ((int)y & 0xff); */ |
48e1416a | 2034 | if (TREE_CODE (expr) == BIT_AND_EXPR |
2035 | || TREE_CODE (expr) == BIT_IOR_EXPR | |
2561cea2 | 2036 | || TREE_CODE (expr) == BIT_XOR_EXPR) |
2037 | { | |
27259707 | 2038 | /* If both args were extended from a shortest type, |
2039 | use that type if that is safe. */ | |
48e1416a | 2040 | expr_type = shorten_binary_op (expr_type, |
2041 | TREE_OPERAND (expr, 0), | |
2042 | TREE_OPERAND (expr, 1), | |
2561cea2 | 2043 | /* bitwise */1); |
2044 | ||
2561cea2 | 2045 | if (TREE_CODE (expr) == BIT_AND_EXPR) |
2046 | { | |
2047 | tree op0 = TREE_OPERAND (expr, 0); | |
2048 | tree op1 = TREE_OPERAND (expr, 1); | |
30de145b | 2049 | bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
2050 | bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1)); | |
2051 | ||
2052 | /* If one of the operands is a non-negative constant | |
2053 | that fits in the target type, then the type of the | |
2054 | other operand does not matter. */ | |
2561cea2 | 2055 | if ((TREE_CODE (op0) == INTEGER_CST |
2056 | && int_fits_type_p (op0, c_common_signed_type (type)) | |
2057 | && int_fits_type_p (op0, c_common_unsigned_type (type))) | |
2058 | || (TREE_CODE (op1) == INTEGER_CST | |
27259707 | 2059 | && int_fits_type_p (op1, c_common_signed_type (type)) |
48e1416a | 2060 | && int_fits_type_p (op1, |
27259707 | 2061 | c_common_unsigned_type (type)))) |
2561cea2 | 2062 | return; |
30de145b | 2063 | /* If constant is unsigned and fits in the target |
2064 | type, then the result will also fit. */ | |
2065 | else if ((TREE_CODE (op0) == INTEGER_CST | |
48e1416a | 2066 | && unsigned0 |
30de145b | 2067 | && int_fits_type_p (op0, type)) |
2068 | || (TREE_CODE (op1) == INTEGER_CST | |
2069 | && unsigned1 | |
2070 | && int_fits_type_p (op1, type))) | |
2071 | return; | |
2561cea2 | 2072 | } |
2073 | } | |
d31d55f0 | 2074 | /* Warn for integer types converted to smaller integer types. */ |
48e1416a | 2075 | if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
7ee0d227 | 2076 | give_warning = true; |
2077 | ||
2078 | /* When they are the same width but different signedness, | |
2079 | then the value may change. */ | |
27259707 | 2080 | else if ((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type) |
2561cea2 | 2081 | && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type)) |
7ee0d227 | 2082 | /* Even when converted to a bigger type, if the type is |
2083 | unsigned but expr is signed, then negative values | |
2084 | will be changed. */ | |
2561cea2 | 2085 | || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type))) |
200dd99c | 2086 | warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT " |
2087 | "may change the sign of the result", | |
2088 | type, expr_type); | |
d31d55f0 | 2089 | } |
2090 | ||
2091 | /* Warn for integer types converted to real types if and only if | |
2092 | all the range of values of the integer type cannot be | |
2093 | represented by the real type. */ | |
2561cea2 | 2094 | else if (TREE_CODE (expr_type) == INTEGER_TYPE |
d31d55f0 | 2095 | && TREE_CODE (type) == REAL_TYPE) |
2096 | { | |
4c2cfa81 | 2097 | tree type_low_bound, type_high_bound; |
2098 | REAL_VALUE_TYPE real_low_bound, real_high_bound; | |
2099 | ||
2100 | /* Don't warn about char y = 0xff; float x = (int) y; */ | |
2101 | expr = get_unwidened (expr, 0); | |
2102 | expr_type = TREE_TYPE (expr); | |
2103 | ||
2104 | type_low_bound = TYPE_MIN_VALUE (expr_type); | |
2105 | type_high_bound = TYPE_MAX_VALUE (expr_type); | |
2106 | real_low_bound = real_value_from_int_cst (0, type_low_bound); | |
2107 | real_high_bound = real_value_from_int_cst (0, type_high_bound); | |
d31d55f0 | 2108 | |
2109 | if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound) | |
2110 | || !exact_real_truncate (TYPE_MODE (type), &real_high_bound)) | |
2111 | give_warning = true; | |
2112 | } | |
2113 | ||
2114 | /* Warn for real types converted to smaller real types. */ | |
2561cea2 | 2115 | else if (TREE_CODE (expr_type) == REAL_TYPE |
d31d55f0 | 2116 | && TREE_CODE (type) == REAL_TYPE |
27259707 | 2117 | && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
d31d55f0 | 2118 | give_warning = true; |
2119 | ||
2120 | ||
2121 | if (give_warning) | |
200dd99c | 2122 | warning_at (loc, OPT_Wconversion, |
2123 | "conversion to %qT from %qT may alter its value", | |
2124 | type, expr_type); | |
d31d55f0 | 2125 | } |
2126 | } | |
2127 | ||
59dd8856 | 2128 | /* Produce warnings after a conversion. RESULT is the result of |
2129 | converting EXPR to TYPE. This is a helper function for | |
2130 | convert_and_check and cp_convert_and_check. */ | |
2a1736ed | 2131 | |
59dd8856 | 2132 | void |
2133 | warnings_for_convert_and_check (tree type, tree expr, tree result) | |
2a1736ed | 2134 | { |
da1fb07b | 2135 | if (TREE_CODE (expr) == INTEGER_CST |
2136 | && (TREE_CODE (type) == INTEGER_TYPE | |
2137 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
2138 | && !int_fits_type_p (expr, type)) | |
2139 | { | |
d31d55f0 | 2140 | /* Do not diagnose overflow in a constant expression merely |
2141 | because a conversion overflowed. */ | |
da1fb07b | 2142 | if (TREE_OVERFLOW (result)) |
eddad94a | 2143 | TREE_OVERFLOW (result) = TREE_OVERFLOW (expr); |
2144 | ||
da1fb07b | 2145 | if (TYPE_UNSIGNED (type)) |
d31d55f0 | 2146 | { |
da1fb07b | 2147 | /* This detects cases like converting -129 or 256 to |
2148 | unsigned char. */ | |
2149 | if (!int_fits_type_p (expr, c_common_signed_type (type))) | |
2150 | warning (OPT_Woverflow, | |
2151 | "large integer implicitly truncated to unsigned type"); | |
7ee0d227 | 2152 | else |
da1fb07b | 2153 | conversion_warning (type, expr); |
2154 | } | |
48e1416a | 2155 | else if (!int_fits_type_p (expr, c_common_unsigned_type (type))) |
e0913805 | 2156 | warning (OPT_Woverflow, |
2157 | "overflow in implicit constant conversion"); | |
2158 | /* No warning for converting 0x80000000 to int. */ | |
2159 | else if (pedantic | |
2160 | && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE | |
2161 | || TYPE_PRECISION (TREE_TYPE (expr)) | |
2162 | != TYPE_PRECISION (type))) | |
2163 | warning (OPT_Woverflow, | |
2164 | "overflow in implicit constant conversion"); | |
2165 | ||
7ee0d227 | 2166 | else |
e0913805 | 2167 | conversion_warning (type, expr); |
2a1736ed | 2168 | } |
9421ebb9 | 2169 | else if ((TREE_CODE (result) == INTEGER_CST |
2170 | || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result)) | |
da1fb07b | 2171 | warning (OPT_Woverflow, |
2172 | "overflow in implicit constant conversion"); | |
7ee0d227 | 2173 | else |
da1fb07b | 2174 | conversion_warning (type, expr); |
59dd8856 | 2175 | } |
2176 | ||
2177 | ||
2178 | /* Convert EXPR to TYPE, warning about conversion problems with constants. | |
2179 | Invoke this function on every expression that is converted implicitly, | |
2180 | i.e. because of language rules and not because of an explicit cast. */ | |
2181 | ||
2182 | tree | |
2183 | convert_and_check (tree type, tree expr) | |
2184 | { | |
2185 | tree result; | |
c6418a4e | 2186 | tree expr_for_warning; |
2187 | ||
2188 | /* Convert from a value with possible excess precision rather than | |
2189 | via the semantic type, but do not warn about values not fitting | |
2190 | exactly in the semantic type. */ | |
2191 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) | |
2192 | { | |
2193 | tree orig_type = TREE_TYPE (expr); | |
2194 | expr = TREE_OPERAND (expr, 0); | |
2195 | expr_for_warning = convert (orig_type, expr); | |
2196 | if (orig_type == type) | |
2197 | return expr_for_warning; | |
2198 | } | |
2199 | else | |
2200 | expr_for_warning = expr; | |
59dd8856 | 2201 | |
2202 | if (TREE_TYPE (expr) == type) | |
2203 | return expr; | |
48e1416a | 2204 | |
59dd8856 | 2205 | result = convert (type, expr); |
2206 | ||
48d94ede | 2207 | if (c_inhibit_evaluation_warnings == 0 |
2208 | && !TREE_OVERFLOW_P (expr) | |
2209 | && result != error_mark_node) | |
c6418a4e | 2210 | warnings_for_convert_and_check (type, expr_for_warning, result); |
59dd8856 | 2211 | |
da1fb07b | 2212 | return result; |
b2806639 | 2213 | } |
2214 | \f | |
4e91a871 | 2215 | /* A node in a list that describes references to variables (EXPR), which are |
2216 | either read accesses if WRITER is zero, or write accesses, in which case | |
2217 | WRITER is the parent of EXPR. */ | |
2218 | struct tlist | |
2219 | { | |
2220 | struct tlist *next; | |
2221 | tree expr, writer; | |
2222 | }; | |
2223 | ||
2224 | /* Used to implement a cache the results of a call to verify_tree. We only | |
2225 | use this for SAVE_EXPRs. */ | |
2226 | struct tlist_cache | |
2227 | { | |
2228 | struct tlist_cache *next; | |
2229 | struct tlist *cache_before_sp; | |
2230 | struct tlist *cache_after_sp; | |
2231 | tree expr; | |
481c6ce6 | 2232 | }; |
2233 | ||
4e91a871 | 2234 | /* Obstack to use when allocating tlist structures, and corresponding |
2235 | firstobj. */ | |
2236 | static struct obstack tlist_obstack; | |
2237 | static char *tlist_firstobj = 0; | |
2238 | ||
2239 | /* Keep track of the identifiers we've warned about, so we can avoid duplicate | |
2240 | warnings. */ | |
2241 | static struct tlist *warned_ids; | |
2242 | /* SAVE_EXPRs need special treatment. We process them only once and then | |
2243 | cache the results. */ | |
2244 | static struct tlist_cache *save_expr_cache; | |
2245 | ||
1cae46be | 2246 | static void add_tlist (struct tlist **, struct tlist *, tree, int); |
2247 | static void merge_tlist (struct tlist **, struct tlist *, int); | |
2248 | static void verify_tree (tree, struct tlist **, struct tlist **, tree); | |
2249 | static int warning_candidate_p (tree); | |
79973b57 | 2250 | static bool candidate_equal_p (const_tree, const_tree); |
1cae46be | 2251 | static void warn_for_collisions (struct tlist *); |
2252 | static void warn_for_collisions_1 (tree, tree, struct tlist *, int); | |
2253 | static struct tlist *new_tlist (struct tlist *, tree, tree); | |
481c6ce6 | 2254 | |
4e91a871 | 2255 | /* Create a new struct tlist and fill in its fields. */ |
2256 | static struct tlist * | |
1cae46be | 2257 | new_tlist (struct tlist *next, tree t, tree writer) |
4e91a871 | 2258 | { |
2259 | struct tlist *l; | |
9318f22c | 2260 | l = XOBNEW (&tlist_obstack, struct tlist); |
4e91a871 | 2261 | l->next = next; |
2262 | l->expr = t; | |
2263 | l->writer = writer; | |
2264 | return l; | |
2265 | } | |
2266 | ||
2267 | /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER | |
2268 | is nonnull, we ignore any node we find which has a writer equal to it. */ | |
2269 | ||
2270 | static void | |
1cae46be | 2271 | add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy) |
4e91a871 | 2272 | { |
2273 | while (add) | |
2274 | { | |
2275 | struct tlist *next = add->next; | |
84166705 | 2276 | if (!copy) |
4e91a871 | 2277 | add->next = *to; |
79973b57 | 2278 | if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer)) |
4e91a871 | 2279 | *to = copy ? new_tlist (*to, add->expr, add->writer) : add; |
2280 | add = next; | |
2281 | } | |
2282 | } | |
2283 | ||
2284 | /* Merge the nodes of ADD into TO. This merging process is done so that for | |
2285 | each variable that already exists in TO, no new node is added; however if | |
2286 | there is a write access recorded in ADD, and an occurrence on TO is only | |
2287 | a read access, then the occurrence in TO will be modified to record the | |
2288 | write. */ | |
481c6ce6 | 2289 | |
2290 | static void | |
1cae46be | 2291 | merge_tlist (struct tlist **to, struct tlist *add, int copy) |
4e91a871 | 2292 | { |
2293 | struct tlist **end = to; | |
2294 | ||
2295 | while (*end) | |
2296 | end = &(*end)->next; | |
2297 | ||
2298 | while (add) | |
2299 | { | |
2300 | int found = 0; | |
2301 | struct tlist *tmp2; | |
2302 | struct tlist *next = add->next; | |
2303 | ||
2304 | for (tmp2 = *to; tmp2; tmp2 = tmp2->next) | |
79973b57 | 2305 | if (candidate_equal_p (tmp2->expr, add->expr)) |
4e91a871 | 2306 | { |
2307 | found = 1; | |
84166705 | 2308 | if (!tmp2->writer) |
4e91a871 | 2309 | tmp2->writer = add->writer; |
2310 | } | |
84166705 | 2311 | if (!found) |
4e91a871 | 2312 | { |
2313 | *end = copy ? add : new_tlist (NULL, add->expr, add->writer); | |
2314 | end = &(*end)->next; | |
2315 | *end = 0; | |
2316 | } | |
2317 | add = next; | |
2318 | } | |
2319 | } | |
2320 | ||
2321 | /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable | |
2322 | references in list LIST conflict with it, excluding reads if ONLY writers | |
2323 | is nonzero. */ | |
2324 | ||
2325 | static void | |
1cae46be | 2326 | warn_for_collisions_1 (tree written, tree writer, struct tlist *list, |
2327 | int only_writes) | |
4e91a871 | 2328 | { |
2329 | struct tlist *tmp; | |
2330 | ||
2331 | /* Avoid duplicate warnings. */ | |
2332 | for (tmp = warned_ids; tmp; tmp = tmp->next) | |
79973b57 | 2333 | if (candidate_equal_p (tmp->expr, written)) |
4e91a871 | 2334 | return; |
2335 | ||
2336 | while (list) | |
2337 | { | |
79973b57 | 2338 | if (candidate_equal_p (list->expr, written) |
2339 | && !candidate_equal_p (list->writer, writer) | |
2340 | && (!only_writes || list->writer)) | |
4e91a871 | 2341 | { |
2342 | warned_ids = new_tlist (warned_ids, written, NULL_TREE); | |
8637f1db | 2343 | warning_at (EXPR_LOC_OR_HERE (writer), |
6513b50d | 2344 | OPT_Wsequence_point, "operation on %qE may be undefined", |
2345 | list->expr); | |
4e91a871 | 2346 | } |
2347 | list = list->next; | |
2348 | } | |
2349 | } | |
2350 | ||
2351 | /* Given a list LIST of references to variables, find whether any of these | |
2352 | can cause conflicts due to missing sequence points. */ | |
2353 | ||
2354 | static void | |
1cae46be | 2355 | warn_for_collisions (struct tlist *list) |
4e91a871 | 2356 | { |
2357 | struct tlist *tmp; | |
1cae46be | 2358 | |
4e91a871 | 2359 | for (tmp = list; tmp; tmp = tmp->next) |
2360 | { | |
2361 | if (tmp->writer) | |
2362 | warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0); | |
2363 | } | |
2364 | } | |
2365 | ||
734c98be | 2366 | /* Return nonzero if X is a tree that can be verified by the sequence point |
4e91a871 | 2367 | warnings. */ |
2368 | static int | |
1cae46be | 2369 | warning_candidate_p (tree x) |
481c6ce6 | 2370 | { |
6ef8d12f | 2371 | if (DECL_P (x) && DECL_ARTIFICIAL (x)) |
2372 | return 0; | |
2373 | ||
027fc6ef | 2374 | if (TREE_CODE (x) == BLOCK) |
2375 | return 0; | |
2376 | ||
6ef8d12f | 2377 | /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c |
79973b57 | 2378 | (lvalue_p) crash on TRY/CATCH. */ |
6ef8d12f | 2379 | if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x))) |
2380 | return 0; | |
2381 | ||
2382 | if (!lvalue_p (x)) | |
2383 | return 0; | |
2384 | ||
2385 | /* No point to track non-const calls, they will never satisfy | |
2386 | operand_equal_p. */ | |
2387 | if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0) | |
2388 | return 0; | |
2389 | ||
2390 | if (TREE_CODE (x) == STRING_CST) | |
2391 | return 0; | |
2392 | ||
2393 | return 1; | |
79973b57 | 2394 | } |
2395 | ||
2396 | /* Return nonzero if X and Y appear to be the same candidate (or NULL) */ | |
2397 | static bool | |
2398 | candidate_equal_p (const_tree x, const_tree y) | |
2399 | { | |
2400 | return (x == y) || (x && y && operand_equal_p (x, y, 0)); | |
4e91a871 | 2401 | } |
481c6ce6 | 2402 | |
4e91a871 | 2403 | /* Walk the tree X, and record accesses to variables. If X is written by the |
2404 | parent tree, WRITER is the parent. | |
2405 | We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this | |
2406 | expression or its only operand forces a sequence point, then everything up | |
2407 | to the sequence point is stored in PBEFORE_SP. Everything else gets stored | |
2408 | in PNO_SP. | |
2409 | Once we return, we will have emitted warnings if any subexpression before | |
2410 | such a sequence point could be undefined. On a higher level, however, the | |
2411 | sequence point may not be relevant, and we'll merge the two lists. | |
2412 | ||
2413 | Example: (b++, a) + b; | |
2414 | The call that processes the COMPOUND_EXPR will store the increment of B | |
2415 | in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that | |
2416 | processes the PLUS_EXPR will need to merge the two lists so that | |
2417 | eventually, all accesses end up on the same list (and we'll warn about the | |
2418 | unordered subexpressions b++ and b. | |
2419 | ||
2420 | A note on merging. If we modify the former example so that our expression | |
2421 | becomes | |
2422 | (b++, b) + a | |
2423 | care must be taken not simply to add all three expressions into the final | |
2424 | PNO_SP list. The function merge_tlist takes care of that by merging the | |
2425 | before-SP list of the COMPOUND_EXPR into its after-SP list in a special | |
2426 | way, so that no more than one access to B is recorded. */ | |
481c6ce6 | 2427 | |
4e91a871 | 2428 | static void |
1cae46be | 2429 | verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp, |
2430 | tree writer) | |
4e91a871 | 2431 | { |
2432 | struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3; | |
2433 | enum tree_code code; | |
ce45a448 | 2434 | enum tree_code_class cl; |
481c6ce6 | 2435 | |
e5b75768 | 2436 | /* X may be NULL if it is the operand of an empty statement expression |
2437 | ({ }). */ | |
2438 | if (x == NULL) | |
2439 | return; | |
2440 | ||
4e91a871 | 2441 | restart: |
2442 | code = TREE_CODE (x); | |
e916c70c | 2443 | cl = TREE_CODE_CLASS (code); |
481c6ce6 | 2444 | |
4e91a871 | 2445 | if (warning_candidate_p (x)) |
79973b57 | 2446 | *pno_sp = new_tlist (*pno_sp, x, writer); |
4e91a871 | 2447 | |
2448 | switch (code) | |
2449 | { | |
67b28e3e | 2450 | case CONSTRUCTOR: |
2451 | return; | |
2452 | ||
4e91a871 | 2453 | case COMPOUND_EXPR: |
2454 | case TRUTH_ANDIF_EXPR: | |
2455 | case TRUTH_ORIF_EXPR: | |
2456 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
2457 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
2458 | warn_for_collisions (tmp_nosp); | |
2459 | merge_tlist (pbefore_sp, tmp_before, 0); | |
2460 | merge_tlist (pbefore_sp, tmp_nosp, 0); | |
2461 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE); | |
2462 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
2463 | return; | |
2464 | ||
2465 | case COND_EXPR: | |
2466 | tmp_before = tmp_list2 = 0; | |
2467 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE); | |
2468 | warn_for_collisions (tmp_list2); | |
2469 | merge_tlist (pbefore_sp, tmp_before, 0); | |
2470 | merge_tlist (pbefore_sp, tmp_list2, 1); | |
2471 | ||
2472 | tmp_list3 = tmp_nosp = 0; | |
2473 | verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE); | |
2474 | warn_for_collisions (tmp_nosp); | |
2475 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
2476 | ||
2477 | tmp_list3 = tmp_list2 = 0; | |
2478 | verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE); | |
2479 | warn_for_collisions (tmp_list2); | |
2480 | merge_tlist (pbefore_sp, tmp_list3, 0); | |
2481 | /* Rather than add both tmp_nosp and tmp_list2, we have to merge the | |
2482 | two first, to avoid warning for (a ? b++ : b++). */ | |
2483 | merge_tlist (&tmp_nosp, tmp_list2, 0); | |
2484 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
2485 | return; | |
2486 | ||
481c6ce6 | 2487 | case PREDECREMENT_EXPR: |
2488 | case PREINCREMENT_EXPR: | |
2489 | case POSTDECREMENT_EXPR: | |
2490 | case POSTINCREMENT_EXPR: | |
4e91a871 | 2491 | verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x); |
2492 | return; | |
2493 | ||
2494 | case MODIFY_EXPR: | |
2495 | tmp_before = tmp_nosp = tmp_list3 = 0; | |
2496 | verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE); | |
2497 | verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x); | |
2498 | /* Expressions inside the LHS are not ordered wrt. the sequence points | |
2499 | in the RHS. Example: | |
2500 | *a = (a++, 2) | |
2501 | Despite the fact that the modification of "a" is in the before_sp | |
2502 | list (tmp_before), it conflicts with the use of "a" in the LHS. | |
2503 | We can handle this by adding the contents of tmp_list3 | |
2504 | to those of tmp_before, and redoing the collision warnings for that | |
2505 | list. */ | |
2506 | add_tlist (&tmp_before, tmp_list3, x, 1); | |
2507 | warn_for_collisions (tmp_before); | |
2508 | /* Exclude the LHS itself here; we first have to merge it into the | |
2509 | tmp_nosp list. This is done to avoid warning for "a = a"; if we | |
2510 | didn't exclude the LHS, we'd get it twice, once as a read and once | |
2511 | as a write. */ | |
2512 | add_tlist (pno_sp, tmp_list3, x, 0); | |
2513 | warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1); | |
2514 | ||
2515 | merge_tlist (pbefore_sp, tmp_before, 0); | |
2516 | if (warning_candidate_p (TREE_OPERAND (x, 0))) | |
2517 | merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0); | |
2518 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1); | |
2519 | return; | |
481c6ce6 | 2520 | |
2521 | case CALL_EXPR: | |
4e91a871 | 2522 | /* We need to warn about conflicts among arguments and conflicts between |
2523 | args and the function address. Side effects of the function address, | |
2524 | however, are not ordered by the sequence point of the call. */ | |
c2f47e15 | 2525 | { |
2526 | call_expr_arg_iterator iter; | |
2527 | tree arg; | |
48e1416a | 2528 | tmp_before = tmp_nosp = 0; |
c2f47e15 | 2529 | verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE); |
2530 | FOR_EACH_CALL_EXPR_ARG (arg, iter, x) | |
2531 | { | |
2532 | tmp_list2 = tmp_list3 = 0; | |
2533 | verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE); | |
2534 | merge_tlist (&tmp_list3, tmp_list2, 0); | |
2535 | add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0); | |
2536 | } | |
2537 | add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0); | |
2538 | warn_for_collisions (tmp_before); | |
2539 | add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0); | |
2540 | return; | |
2541 | } | |
481c6ce6 | 2542 | |
2543 | case TREE_LIST: | |
2544 | /* Scan all the list, e.g. indices of multi dimensional array. */ | |
2545 | while (x) | |
2546 | { | |
4e91a871 | 2547 | tmp_before = tmp_nosp = 0; |
2548 | verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE); | |
2549 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
2550 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
481c6ce6 | 2551 | x = TREE_CHAIN (x); |
2552 | } | |
4e91a871 | 2553 | return; |
481c6ce6 | 2554 | |
4e91a871 | 2555 | case SAVE_EXPR: |
2556 | { | |
2557 | struct tlist_cache *t; | |
2558 | for (t = save_expr_cache; t; t = t->next) | |
79973b57 | 2559 | if (candidate_equal_p (t->expr, x)) |
4e91a871 | 2560 | break; |
481c6ce6 | 2561 | |
84166705 | 2562 | if (!t) |
481c6ce6 | 2563 | { |
9318f22c | 2564 | t = XOBNEW (&tlist_obstack, struct tlist_cache); |
4e91a871 | 2565 | t->next = save_expr_cache; |
2566 | t->expr = x; | |
2567 | save_expr_cache = t; | |
2568 | ||
2569 | tmp_before = tmp_nosp = 0; | |
2570 | verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE); | |
2571 | warn_for_collisions (tmp_nosp); | |
2572 | ||
2573 | tmp_list3 = 0; | |
2574 | while (tmp_nosp) | |
2575 | { | |
2576 | struct tlist *t = tmp_nosp; | |
2577 | tmp_nosp = t->next; | |
2578 | merge_tlist (&tmp_list3, t, 0); | |
2579 | } | |
2580 | t->cache_before_sp = tmp_before; | |
2581 | t->cache_after_sp = tmp_list3; | |
481c6ce6 | 2582 | } |
4e91a871 | 2583 | merge_tlist (pbefore_sp, t->cache_before_sp, 1); |
2584 | add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1); | |
2585 | return; | |
2586 | } | |
481c6ce6 | 2587 | |
012916cb | 2588 | case ADDR_EXPR: |
2589 | x = TREE_OPERAND (x, 0); | |
2590 | if (DECL_P (x)) | |
2591 | return; | |
2592 | writer = 0; | |
2593 | goto restart; | |
2594 | ||
ce45a448 | 2595 | default: |
2596 | /* For other expressions, simply recurse on their operands. | |
a0c938f0 | 2597 | Manual tail recursion for unary expressions. |
ce45a448 | 2598 | Other non-expressions need not be processed. */ |
2599 | if (cl == tcc_unary) | |
2600 | { | |
ce45a448 | 2601 | x = TREE_OPERAND (x, 0); |
2602 | writer = 0; | |
2603 | goto restart; | |
2604 | } | |
2605 | else if (IS_EXPR_CODE_CLASS (cl)) | |
2606 | { | |
2607 | int lp; | |
c2f47e15 | 2608 | int max = TREE_OPERAND_LENGTH (x); |
ce45a448 | 2609 | for (lp = 0; lp < max; lp++) |
2610 | { | |
2611 | tmp_before = tmp_nosp = 0; | |
2612 | verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0); | |
2613 | merge_tlist (&tmp_nosp, tmp_before, 0); | |
2614 | add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0); | |
2615 | } | |
2616 | } | |
2617 | return; | |
481c6ce6 | 2618 | } |
481c6ce6 | 2619 | } |
2620 | ||
974e2c0c | 2621 | /* Try to warn for undefined behavior in EXPR due to missing sequence |
481c6ce6 | 2622 | points. */ |
2623 | ||
4b987fac | 2624 | DEBUG_FUNCTION void |
1cae46be | 2625 | verify_sequence_points (tree expr) |
481c6ce6 | 2626 | { |
4e91a871 | 2627 | struct tlist *before_sp = 0, *after_sp = 0; |
481c6ce6 | 2628 | |
4e91a871 | 2629 | warned_ids = 0; |
2630 | save_expr_cache = 0; | |
2631 | if (tlist_firstobj == 0) | |
481c6ce6 | 2632 | { |
4e91a871 | 2633 | gcc_obstack_init (&tlist_obstack); |
4fd61bc6 | 2634 | tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0); |
481c6ce6 | 2635 | } |
2636 | ||
4e91a871 | 2637 | verify_tree (expr, &before_sp, &after_sp, 0); |
2638 | warn_for_collisions (after_sp); | |
2639 | obstack_free (&tlist_obstack, tlist_firstobj); | |
481c6ce6 | 2640 | } |
b0fc3e72 | 2641 | \f |
2642 | /* Validate the expression after `case' and apply default promotions. */ | |
2643 | ||
2ca392fd | 2644 | static tree |
1cae46be | 2645 | check_case_value (tree value) |
b0fc3e72 | 2646 | { |
2647 | if (value == NULL_TREE) | |
2648 | return value; | |
2649 | ||
b96dc121 | 2650 | if (TREE_CODE (value) == INTEGER_CST) |
2651 | /* Promote char or short to int. */ | |
2652 | value = perform_integral_promotions (value); | |
2653 | else if (value != error_mark_node) | |
b0fc3e72 | 2654 | { |
2655 | error ("case label does not reduce to an integer constant"); | |
2656 | value = error_mark_node; | |
2657 | } | |
b0fc3e72 | 2658 | |
6433f1c2 | 2659 | constant_expression_warning (value); |
2660 | ||
b0fc3e72 | 2661 | return value; |
2662 | } | |
2663 | \f | |
2ca392fd | 2664 | /* See if the case values LOW and HIGH are in the range of the original |
5c9dae64 | 2665 | type (i.e. before the default conversion to int) of the switch testing |
2ca392fd | 2666 | expression. |
2667 | TYPE is the promoted type of the testing expression, and ORIG_TYPE is | |
91275768 | 2668 | the type before promoting it. CASE_LOW_P is a pointer to the lower |
2ca392fd | 2669 | bound of the case label, and CASE_HIGH_P is the upper bound or NULL |
2670 | if the case is not a case range. | |
2671 | The caller has to make sure that we are not called with NULL for | |
5c9dae64 | 2672 | CASE_LOW_P (i.e. the default case). |
442e3cb9 | 2673 | Returns true if the case label is in range of ORIG_TYPE (saturated or |
2ca392fd | 2674 | untouched) or false if the label is out of range. */ |
2675 | ||
2676 | static bool | |
2677 | check_case_bounds (tree type, tree orig_type, | |
2678 | tree *case_low_p, tree *case_high_p) | |
2679 | { | |
2680 | tree min_value, max_value; | |
2681 | tree case_low = *case_low_p; | |
2682 | tree case_high = case_high_p ? *case_high_p : case_low; | |
2683 | ||
2684 | /* If there was a problem with the original type, do nothing. */ | |
2685 | if (orig_type == error_mark_node) | |
2686 | return true; | |
2687 | ||
2688 | min_value = TYPE_MIN_VALUE (orig_type); | |
2689 | max_value = TYPE_MAX_VALUE (orig_type); | |
2690 | ||
2691 | /* Case label is less than minimum for type. */ | |
2692 | if (tree_int_cst_compare (case_low, min_value) < 0 | |
2693 | && tree_int_cst_compare (case_high, min_value) < 0) | |
2694 | { | |
c3ceba8e | 2695 | warning (0, "case label value is less than minimum value for type"); |
2ca392fd | 2696 | return false; |
2697 | } | |
b27ac6b5 | 2698 | |
2ca392fd | 2699 | /* Case value is greater than maximum for type. */ |
2700 | if (tree_int_cst_compare (case_low, max_value) > 0 | |
2701 | && tree_int_cst_compare (case_high, max_value) > 0) | |
2702 | { | |
c3ceba8e | 2703 | warning (0, "case label value exceeds maximum value for type"); |
2ca392fd | 2704 | return false; |
2705 | } | |
2706 | ||
2707 | /* Saturate lower case label value to minimum. */ | |
2708 | if (tree_int_cst_compare (case_high, min_value) >= 0 | |
2709 | && tree_int_cst_compare (case_low, min_value) < 0) | |
2710 | { | |
c3ceba8e | 2711 | warning (0, "lower value in case label range" |
2ca392fd | 2712 | " less than minimum value for type"); |
2713 | case_low = min_value; | |
2714 | } | |
b27ac6b5 | 2715 | |
2ca392fd | 2716 | /* Saturate upper case label value to maximum. */ |
2717 | if (tree_int_cst_compare (case_low, max_value) <= 0 | |
2718 | && tree_int_cst_compare (case_high, max_value) > 0) | |
2719 | { | |
c3ceba8e | 2720 | warning (0, "upper value in case label range" |
2ca392fd | 2721 | " exceeds maximum value for type"); |
2722 | case_high = max_value; | |
2723 | } | |
2724 | ||
2725 | if (*case_low_p != case_low) | |
2726 | *case_low_p = convert (type, case_low); | |
2727 | if (case_high_p && *case_high_p != case_high) | |
2728 | *case_high_p = convert (type, case_high); | |
2729 | ||
2730 | return true; | |
2731 | } | |
2732 | \f | |
b0fc3e72 | 2733 | /* Return an integer type with BITS bits of precision, |
2734 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */ | |
2735 | ||
2736 | tree | |
1cae46be | 2737 | c_common_type_for_size (unsigned int bits, int unsignedp) |
b0fc3e72 | 2738 | { |
46375237 | 2739 | if (bits == TYPE_PRECISION (integer_type_node)) |
2740 | return unsignedp ? unsigned_type_node : integer_type_node; | |
2741 | ||
bacde65a | 2742 | if (bits == TYPE_PRECISION (signed_char_type_node)) |
b0fc3e72 | 2743 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
2744 | ||
bacde65a | 2745 | if (bits == TYPE_PRECISION (short_integer_type_node)) |
b0fc3e72 | 2746 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
2747 | ||
bacde65a | 2748 | if (bits == TYPE_PRECISION (long_integer_type_node)) |
b0fc3e72 | 2749 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
2750 | ||
bacde65a | 2751 | if (bits == TYPE_PRECISION (long_long_integer_type_node)) |
b0fc3e72 | 2752 | return (unsignedp ? long_long_unsigned_type_node |
2753 | : long_long_integer_type_node); | |
2754 | ||
6388cfe2 | 2755 | if (int128_integer_type_node |
2756 | && bits == TYPE_PRECISION (int128_integer_type_node)) | |
2757 | return (unsignedp ? int128_unsigned_type_node | |
2758 | : int128_integer_type_node); | |
2759 | ||
f57fa2ea | 2760 | if (bits == TYPE_PRECISION (widest_integer_literal_type_node)) |
2761 | return (unsignedp ? widest_unsigned_literal_type_node | |
2762 | : widest_integer_literal_type_node); | |
2763 | ||
bacde65a | 2764 | if (bits <= TYPE_PRECISION (intQI_type_node)) |
2765 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
2766 | ||
2767 | if (bits <= TYPE_PRECISION (intHI_type_node)) | |
2768 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
2769 | ||
2770 | if (bits <= TYPE_PRECISION (intSI_type_node)) | |
2771 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
2772 | ||
2773 | if (bits <= TYPE_PRECISION (intDI_type_node)) | |
2774 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
2775 | ||
b0fc3e72 | 2776 | return 0; |
2777 | } | |
2778 | ||
9421ebb9 | 2779 | /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits |
2780 | that is unsigned if UNSIGNEDP is nonzero, otherwise signed; | |
2781 | and saturating if SATP is nonzero, otherwise not saturating. */ | |
2782 | ||
2783 | tree | |
2784 | c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit, | |
2785 | int unsignedp, int satp) | |
2786 | { | |
2787 | enum machine_mode mode; | |
2788 | if (ibit == 0) | |
2789 | mode = unsignedp ? UQQmode : QQmode; | |
2790 | else | |
2791 | mode = unsignedp ? UHAmode : HAmode; | |
2792 | ||
2793 | for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode)) | |
2794 | if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit) | |
2795 | break; | |
2796 | ||
2797 | if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode)) | |
2798 | { | |
2799 | sorry ("GCC cannot support operators with integer types and " | |
2800 | "fixed-point types that have too many integral and " | |
2801 | "fractional bits together"); | |
2802 | return 0; | |
2803 | } | |
2804 | ||
2805 | return c_common_type_for_mode (mode, satp); | |
2806 | } | |
2807 | ||
5b247e9f | 2808 | /* Used for communication between c_common_type_for_mode and |
2809 | c_register_builtin_type. */ | |
2810 | static GTY(()) tree registered_builtin_types; | |
2811 | ||
b0fc3e72 | 2812 | /* Return a data type that has machine mode MODE. |
2813 | If the mode is an integer, | |
9421ebb9 | 2814 | then UNSIGNEDP selects between signed and unsigned types. |
2815 | If the mode is a fixed-point mode, | |
2816 | then UNSIGNEDP selects between saturating and nonsaturating types. */ | |
b0fc3e72 | 2817 | |
2818 | tree | |
1cae46be | 2819 | c_common_type_for_mode (enum machine_mode mode, int unsignedp) |
b0fc3e72 | 2820 | { |
5b247e9f | 2821 | tree t; |
2822 | ||
46375237 | 2823 | if (mode == TYPE_MODE (integer_type_node)) |
2824 | return unsignedp ? unsigned_type_node : integer_type_node; | |
2825 | ||
b0fc3e72 | 2826 | if (mode == TYPE_MODE (signed_char_type_node)) |
2827 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
2828 | ||
2829 | if (mode == TYPE_MODE (short_integer_type_node)) | |
2830 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
2831 | ||
b0fc3e72 | 2832 | if (mode == TYPE_MODE (long_integer_type_node)) |
2833 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
2834 | ||
2835 | if (mode == TYPE_MODE (long_long_integer_type_node)) | |
2836 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
2837 | ||
6388cfe2 | 2838 | if (int128_integer_type_node |
2839 | && mode == TYPE_MODE (int128_integer_type_node)) | |
2840 | return unsignedp ? int128_unsigned_type_node : int128_integer_type_node; | |
2841 | ||
f57fa2ea | 2842 | if (mode == TYPE_MODE (widest_integer_literal_type_node)) |
44e9fa65 | 2843 | return unsignedp ? widest_unsigned_literal_type_node |
4ee9c684 | 2844 | : widest_integer_literal_type_node; |
f57fa2ea | 2845 | |
88ae7f04 | 2846 | if (mode == QImode) |
bacde65a | 2847 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
2848 | ||
88ae7f04 | 2849 | if (mode == HImode) |
bacde65a | 2850 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
2851 | ||
88ae7f04 | 2852 | if (mode == SImode) |
bacde65a | 2853 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
2854 | ||
88ae7f04 | 2855 | if (mode == DImode) |
bacde65a | 2856 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
2857 | ||
cc1cc1c7 | 2858 | #if HOST_BITS_PER_WIDE_INT >= 64 |
6274009c | 2859 | if (mode == TYPE_MODE (intTI_type_node)) |
2860 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
cc1cc1c7 | 2861 | #endif |
6274009c | 2862 | |
b0fc3e72 | 2863 | if (mode == TYPE_MODE (float_type_node)) |
2864 | return float_type_node; | |
2865 | ||
2866 | if (mode == TYPE_MODE (double_type_node)) | |
2867 | return double_type_node; | |
2868 | ||
2869 | if (mode == TYPE_MODE (long_double_type_node)) | |
2870 | return long_double_type_node; | |
2871 | ||
545c2bde | 2872 | if (mode == TYPE_MODE (void_type_node)) |
2873 | return void_type_node; | |
b27ac6b5 | 2874 | |
b0fc3e72 | 2875 | if (mode == TYPE_MODE (build_pointer_type (char_type_node))) |
61b9b73c | 2876 | return (unsignedp |
2877 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
2878 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 2879 | |
2880 | if (mode == TYPE_MODE (build_pointer_type (integer_type_node))) | |
61b9b73c | 2881 | return (unsignedp |
2882 | ? make_unsigned_type (GET_MODE_PRECISION (mode)) | |
2883 | : make_signed_type (GET_MODE_PRECISION (mode))); | |
b0fc3e72 | 2884 | |
0dfc45b5 | 2885 | if (COMPLEX_MODE_P (mode)) |
2886 | { | |
2887 | enum machine_mode inner_mode; | |
2888 | tree inner_type; | |
2889 | ||
2890 | if (mode == TYPE_MODE (complex_float_type_node)) | |
2891 | return complex_float_type_node; | |
2892 | if (mode == TYPE_MODE (complex_double_type_node)) | |
2893 | return complex_double_type_node; | |
2894 | if (mode == TYPE_MODE (complex_long_double_type_node)) | |
2895 | return complex_long_double_type_node; | |
2896 | ||
2897 | if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp) | |
2898 | return complex_integer_type_node; | |
2899 | ||
2900 | inner_mode = GET_MODE_INNER (mode); | |
2901 | inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
2902 | if (inner_type != NULL_TREE) | |
2903 | return build_complex_type (inner_type); | |
2904 | } | |
2905 | else if (VECTOR_MODE_P (mode)) | |
4917c376 | 2906 | { |
2907 | enum machine_mode inner_mode = GET_MODE_INNER (mode); | |
2908 | tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); | |
2909 | if (inner_type != NULL_TREE) | |
2910 | return build_vector_type_for_mode (inner_type, mode); | |
88ae7f04 | 2911 | } |
e2ea7e3a | 2912 | |
c4503c0a | 2913 | if (mode == TYPE_MODE (dfloat32_type_node)) |
2914 | return dfloat32_type_node; | |
2915 | if (mode == TYPE_MODE (dfloat64_type_node)) | |
2916 | return dfloat64_type_node; | |
2917 | if (mode == TYPE_MODE (dfloat128_type_node)) | |
2918 | return dfloat128_type_node; | |
2919 | ||
9421ebb9 | 2920 | if (ALL_SCALAR_FIXED_POINT_MODE_P (mode)) |
2921 | { | |
2922 | if (mode == TYPE_MODE (short_fract_type_node)) | |
2923 | return unsignedp ? sat_short_fract_type_node : short_fract_type_node; | |
2924 | if (mode == TYPE_MODE (fract_type_node)) | |
2925 | return unsignedp ? sat_fract_type_node : fract_type_node; | |
2926 | if (mode == TYPE_MODE (long_fract_type_node)) | |
2927 | return unsignedp ? sat_long_fract_type_node : long_fract_type_node; | |
2928 | if (mode == TYPE_MODE (long_long_fract_type_node)) | |
2929 | return unsignedp ? sat_long_long_fract_type_node | |
2930 | : long_long_fract_type_node; | |
2931 | ||
2932 | if (mode == TYPE_MODE (unsigned_short_fract_type_node)) | |
2933 | return unsignedp ? sat_unsigned_short_fract_type_node | |
2934 | : unsigned_short_fract_type_node; | |
2935 | if (mode == TYPE_MODE (unsigned_fract_type_node)) | |
2936 | return unsignedp ? sat_unsigned_fract_type_node | |
2937 | : unsigned_fract_type_node; | |
2938 | if (mode == TYPE_MODE (unsigned_long_fract_type_node)) | |
2939 | return unsignedp ? sat_unsigned_long_fract_type_node | |
2940 | : unsigned_long_fract_type_node; | |
2941 | if (mode == TYPE_MODE (unsigned_long_long_fract_type_node)) | |
2942 | return unsignedp ? sat_unsigned_long_long_fract_type_node | |
2943 | : unsigned_long_long_fract_type_node; | |
2944 | ||
2945 | if (mode == TYPE_MODE (short_accum_type_node)) | |
2946 | return unsignedp ? sat_short_accum_type_node : short_accum_type_node; | |
2947 | if (mode == TYPE_MODE (accum_type_node)) | |
2948 | return unsignedp ? sat_accum_type_node : accum_type_node; | |
2949 | if (mode == TYPE_MODE (long_accum_type_node)) | |
2950 | return unsignedp ? sat_long_accum_type_node : long_accum_type_node; | |
2951 | if (mode == TYPE_MODE (long_long_accum_type_node)) | |
2952 | return unsignedp ? sat_long_long_accum_type_node | |
2953 | : long_long_accum_type_node; | |
2954 | ||
2955 | if (mode == TYPE_MODE (unsigned_short_accum_type_node)) | |
2956 | return unsignedp ? sat_unsigned_short_accum_type_node | |
2957 | : unsigned_short_accum_type_node; | |
2958 | if (mode == TYPE_MODE (unsigned_accum_type_node)) | |
2959 | return unsignedp ? sat_unsigned_accum_type_node | |
2960 | : unsigned_accum_type_node; | |
2961 | if (mode == TYPE_MODE (unsigned_long_accum_type_node)) | |
2962 | return unsignedp ? sat_unsigned_long_accum_type_node | |
2963 | : unsigned_long_accum_type_node; | |
2964 | if (mode == TYPE_MODE (unsigned_long_long_accum_type_node)) | |
2965 | return unsignedp ? sat_unsigned_long_long_accum_type_node | |
2966 | : unsigned_long_long_accum_type_node; | |
2967 | ||
2968 | if (mode == QQmode) | |
2969 | return unsignedp ? sat_qq_type_node : qq_type_node; | |
2970 | if (mode == HQmode) | |
2971 | return unsignedp ? sat_hq_type_node : hq_type_node; | |
2972 | if (mode == SQmode) | |
2973 | return unsignedp ? sat_sq_type_node : sq_type_node; | |
2974 | if (mode == DQmode) | |
2975 | return unsignedp ? sat_dq_type_node : dq_type_node; | |
2976 | if (mode == TQmode) | |
2977 | return unsignedp ? sat_tq_type_node : tq_type_node; | |
2978 | ||
2979 | if (mode == UQQmode) | |
2980 | return unsignedp ? sat_uqq_type_node : uqq_type_node; | |
2981 | if (mode == UHQmode) | |
2982 | return unsignedp ? sat_uhq_type_node : uhq_type_node; | |
2983 | if (mode == USQmode) | |
2984 | return unsignedp ? sat_usq_type_node : usq_type_node; | |
2985 | if (mode == UDQmode) | |
2986 | return unsignedp ? sat_udq_type_node : udq_type_node; | |
2987 | if (mode == UTQmode) | |
2988 | return unsignedp ? sat_utq_type_node : utq_type_node; | |
2989 | ||
2990 | if (mode == HAmode) | |
2991 | return unsignedp ? sat_ha_type_node : ha_type_node; | |
2992 | if (mode == SAmode) | |
2993 | return unsignedp ? sat_sa_type_node : sa_type_node; | |
2994 | if (mode == DAmode) | |
2995 | return unsignedp ? sat_da_type_node : da_type_node; | |
2996 | if (mode == TAmode) | |
2997 | return unsignedp ? sat_ta_type_node : ta_type_node; | |
2998 | ||
2999 | if (mode == UHAmode) | |
3000 | return unsignedp ? sat_uha_type_node : uha_type_node; | |
3001 | if (mode == USAmode) | |
3002 | return unsignedp ? sat_usa_type_node : usa_type_node; | |
3003 | if (mode == UDAmode) | |
3004 | return unsignedp ? sat_uda_type_node : uda_type_node; | |
3005 | if (mode == UTAmode) | |
3006 | return unsignedp ? sat_uta_type_node : uta_type_node; | |
3007 | } | |
3008 | ||
5b247e9f | 3009 | for (t = registered_builtin_types; t; t = TREE_CHAIN (t)) |
3010 | if (TYPE_MODE (TREE_VALUE (t)) == mode) | |
3011 | return TREE_VALUE (t); | |
3012 | ||
b0fc3e72 | 3013 | return 0; |
3014 | } | |
20d39783 | 3015 | |
11773141 | 3016 | tree |
3017 | c_common_unsigned_type (tree type) | |
3018 | { | |
3019 | return c_common_signed_or_unsigned_type (1, type); | |
3020 | } | |
3021 | ||
20d39783 | 3022 | /* Return a signed type the same as TYPE in other respects. */ |
3023 | ||
3024 | tree | |
1cae46be | 3025 | c_common_signed_type (tree type) |
20d39783 | 3026 | { |
4070745f | 3027 | return c_common_signed_or_unsigned_type (0, type); |
20d39783 | 3028 | } |
3029 | ||
3030 | /* Return a type the same as TYPE except unsigned or | |
3031 | signed according to UNSIGNEDP. */ | |
3032 | ||
3033 | tree | |
1cae46be | 3034 | c_common_signed_or_unsigned_type (int unsignedp, tree type) |
20d39783 | 3035 | { |
7a91101f | 3036 | tree type1; |
20d39783 | 3037 | |
7a91101f | 3038 | /* This block of code emulates the behavior of the old |
3039 | c_common_unsigned_type. In particular, it returns | |
3040 | long_unsigned_type_node if passed a long, even when a int would | |
3041 | have the same size. This is necessary for warnings to work | |
3042 | correctly in archs where sizeof(int) == sizeof(long) */ | |
3043 | ||
3044 | type1 = TYPE_MAIN_VARIANT (type); | |
3045 | if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node) | |
3046 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3047 | if (type1 == integer_type_node || type1 == unsigned_type_node) | |
3048 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3049 | if (type1 == short_integer_type_node || type1 == short_unsigned_type_node) | |
3050 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3051 | if (type1 == long_integer_type_node || type1 == long_unsigned_type_node) | |
3052 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3053 | if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node) | |
3054 | return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node; | |
6388cfe2 | 3055 | if (int128_integer_type_node |
3056 | && (type1 == int128_integer_type_node | |
3057 | || type1 == int128_unsigned_type_node)) | |
3058 | return unsignedp ? int128_unsigned_type_node : int128_integer_type_node; | |
7a91101f | 3059 | if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node) |
3060 | return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node; | |
3061 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
3062 | if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node) | |
3063 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; | |
3064 | #endif | |
3065 | if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node) | |
3066 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; | |
3067 | if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node) | |
3068 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; | |
3069 | if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node) | |
3070 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; | |
3071 | if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node) | |
3072 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; | |
3073 | ||
9f92e1a2 | 3074 | #define C_COMMON_FIXED_TYPES(NAME) \ |
3075 | if (type1 == short_ ## NAME ## _type_node \ | |
3076 | || type1 == unsigned_short_ ## NAME ## _type_node) \ | |
3077 | return unsignedp ? unsigned_short_ ## NAME ## _type_node \ | |
3078 | : short_ ## NAME ## _type_node; \ | |
3079 | if (type1 == NAME ## _type_node \ | |
3080 | || type1 == unsigned_ ## NAME ## _type_node) \ | |
3081 | return unsignedp ? unsigned_ ## NAME ## _type_node \ | |
3082 | : NAME ## _type_node; \ | |
3083 | if (type1 == long_ ## NAME ## _type_node \ | |
3084 | || type1 == unsigned_long_ ## NAME ## _type_node) \ | |
3085 | return unsignedp ? unsigned_long_ ## NAME ## _type_node \ | |
3086 | : long_ ## NAME ## _type_node; \ | |
3087 | if (type1 == long_long_ ## NAME ## _type_node \ | |
3088 | || type1 == unsigned_long_long_ ## NAME ## _type_node) \ | |
3089 | return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \ | |
3090 | : long_long_ ## NAME ## _type_node; | |
3091 | ||
3092 | #define C_COMMON_FIXED_MODE_TYPES(NAME) \ | |
3093 | if (type1 == NAME ## _type_node \ | |
3094 | || type1 == u ## NAME ## _type_node) \ | |
3095 | return unsignedp ? u ## NAME ## _type_node \ | |
3096 | : NAME ## _type_node; | |
3097 | ||
3098 | #define C_COMMON_FIXED_TYPES_SAT(NAME) \ | |
3099 | if (type1 == sat_ ## short_ ## NAME ## _type_node \ | |
3100 | || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \ | |
3101 | return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \ | |
3102 | : sat_ ## short_ ## NAME ## _type_node; \ | |
3103 | if (type1 == sat_ ## NAME ## _type_node \ | |
3104 | || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \ | |
3105 | return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \ | |
3106 | : sat_ ## NAME ## _type_node; \ | |
3107 | if (type1 == sat_ ## long_ ## NAME ## _type_node \ | |
3108 | || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \ | |
3109 | return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \ | |
3110 | : sat_ ## long_ ## NAME ## _type_node; \ | |
3111 | if (type1 == sat_ ## long_long_ ## NAME ## _type_node \ | |
3112 | || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \ | |
3113 | return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \ | |
3114 | : sat_ ## long_long_ ## NAME ## _type_node; | |
3115 | ||
3116 | #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \ | |
3117 | if (type1 == sat_ ## NAME ## _type_node \ | |
3118 | || type1 == sat_ ## u ## NAME ## _type_node) \ | |
3119 | return unsignedp ? sat_ ## u ## NAME ## _type_node \ | |
3120 | : sat_ ## NAME ## _type_node; | |
3121 | ||
3122 | C_COMMON_FIXED_TYPES (fract); | |
3123 | C_COMMON_FIXED_TYPES_SAT (fract); | |
3124 | C_COMMON_FIXED_TYPES (accum); | |
3125 | C_COMMON_FIXED_TYPES_SAT (accum); | |
3126 | ||
3127 | C_COMMON_FIXED_MODE_TYPES (qq); | |
3128 | C_COMMON_FIXED_MODE_TYPES (hq); | |
3129 | C_COMMON_FIXED_MODE_TYPES (sq); | |
3130 | C_COMMON_FIXED_MODE_TYPES (dq); | |
3131 | C_COMMON_FIXED_MODE_TYPES (tq); | |
3132 | C_COMMON_FIXED_MODE_TYPES_SAT (qq); | |
3133 | C_COMMON_FIXED_MODE_TYPES_SAT (hq); | |
3134 | C_COMMON_FIXED_MODE_TYPES_SAT (sq); | |
3135 | C_COMMON_FIXED_MODE_TYPES_SAT (dq); | |
3136 | C_COMMON_FIXED_MODE_TYPES_SAT (tq); | |
3137 | C_COMMON_FIXED_MODE_TYPES (ha); | |
3138 | C_COMMON_FIXED_MODE_TYPES (sa); | |
3139 | C_COMMON_FIXED_MODE_TYPES (da); | |
3140 | C_COMMON_FIXED_MODE_TYPES (ta); | |
3141 | C_COMMON_FIXED_MODE_TYPES_SAT (ha); | |
3142 | C_COMMON_FIXED_MODE_TYPES_SAT (sa); | |
3143 | C_COMMON_FIXED_MODE_TYPES_SAT (da); | |
3144 | C_COMMON_FIXED_MODE_TYPES_SAT (ta); | |
9421ebb9 | 3145 | |
4f7f7efd | 3146 | /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not |
3147 | the precision; they have precision set to match their range, but | |
3148 | may use a wider mode to match an ABI. If we change modes, we may | |
3149 | wind up with bad conversions. For INTEGER_TYPEs in C, must check | |
3150 | the precision as well, so as to yield correct results for | |
3151 | bit-field types. C++ does not have these separate bit-field | |
3152 | types, and producing a signed or unsigned variant of an | |
3153 | ENUMERAL_TYPE may cause other problems as well. */ | |
3154 | ||
ac265864 | 3155 | if (!INTEGRAL_TYPE_P (type) |
3156 | || TYPE_UNSIGNED (type) == unsignedp) | |
3157 | return type; | |
3158 | ||
4f7f7efd | 3159 | #define TYPE_OK(node) \ |
3160 | (TYPE_MODE (type) == TYPE_MODE (node) \ | |
0c4abe5b | 3161 | && TYPE_PRECISION (type) == TYPE_PRECISION (node)) |
4f7f7efd | 3162 | if (TYPE_OK (signed_char_type_node)) |
20d39783 | 3163 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; |
4f7f7efd | 3164 | if (TYPE_OK (integer_type_node)) |
20d39783 | 3165 | return unsignedp ? unsigned_type_node : integer_type_node; |
4f7f7efd | 3166 | if (TYPE_OK (short_integer_type_node)) |
20d39783 | 3167 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; |
4f7f7efd | 3168 | if (TYPE_OK (long_integer_type_node)) |
20d39783 | 3169 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; |
4f7f7efd | 3170 | if (TYPE_OK (long_long_integer_type_node)) |
20d39783 | 3171 | return (unsignedp ? long_long_unsigned_type_node |
3172 | : long_long_integer_type_node); | |
6388cfe2 | 3173 | if (int128_integer_type_node && TYPE_OK (int128_integer_type_node)) |
3174 | return (unsignedp ? int128_unsigned_type_node | |
3175 | : int128_integer_type_node); | |
4f7f7efd | 3176 | if (TYPE_OK (widest_integer_literal_type_node)) |
20d39783 | 3177 | return (unsignedp ? widest_unsigned_literal_type_node |
3178 | : widest_integer_literal_type_node); | |
ef11801e | 3179 | |
3180 | #if HOST_BITS_PER_WIDE_INT >= 64 | |
4f7f7efd | 3181 | if (TYPE_OK (intTI_type_node)) |
ef11801e | 3182 | return unsignedp ? unsigned_intTI_type_node : intTI_type_node; |
3183 | #endif | |
4f7f7efd | 3184 | if (TYPE_OK (intDI_type_node)) |
ef11801e | 3185 | return unsignedp ? unsigned_intDI_type_node : intDI_type_node; |
4f7f7efd | 3186 | if (TYPE_OK (intSI_type_node)) |
ef11801e | 3187 | return unsignedp ? unsigned_intSI_type_node : intSI_type_node; |
4f7f7efd | 3188 | if (TYPE_OK (intHI_type_node)) |
ef11801e | 3189 | return unsignedp ? unsigned_intHI_type_node : intHI_type_node; |
4f7f7efd | 3190 | if (TYPE_OK (intQI_type_node)) |
ef11801e | 3191 | return unsignedp ? unsigned_intQI_type_node : intQI_type_node; |
4f7f7efd | 3192 | #undef TYPE_OK |
ef11801e | 3193 | |
0c4abe5b | 3194 | return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp); |
20d39783 | 3195 | } |
b268e47e | 3196 | |
c0e47fd4 | 3197 | /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */ |
3198 | ||
3199 | tree | |
3200 | c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp) | |
3201 | { | |
3202 | /* Extended integer types of the same width as a standard type have | |
3203 | lesser rank, so those of the same width as int promote to int or | |
3204 | unsigned int and are valid for printf formats expecting int or | |
3205 | unsigned int. To avoid such special cases, avoid creating | |
3206 | extended integer types for bit-fields if a standard integer type | |
3207 | is available. */ | |
3208 | if (width == TYPE_PRECISION (integer_type_node)) | |
3209 | return unsignedp ? unsigned_type_node : integer_type_node; | |
3210 | if (width == TYPE_PRECISION (signed_char_type_node)) | |
3211 | return unsignedp ? unsigned_char_type_node : signed_char_type_node; | |
3212 | if (width == TYPE_PRECISION (short_integer_type_node)) | |
3213 | return unsignedp ? short_unsigned_type_node : short_integer_type_node; | |
3214 | if (width == TYPE_PRECISION (long_integer_type_node)) | |
3215 | return unsignedp ? long_unsigned_type_node : long_integer_type_node; | |
3216 | if (width == TYPE_PRECISION (long_long_integer_type_node)) | |
3217 | return (unsignedp ? long_long_unsigned_type_node | |
3218 | : long_long_integer_type_node); | |
6388cfe2 | 3219 | if (int128_integer_type_node |
3220 | && width == TYPE_PRECISION (int128_integer_type_node)) | |
3221 | return (unsignedp ? int128_unsigned_type_node | |
3222 | : int128_integer_type_node); | |
c0e47fd4 | 3223 | return build_nonstandard_integer_type (width, unsignedp); |
3224 | } | |
3225 | ||
b268e47e | 3226 | /* The C version of the register_builtin_type langhook. */ |
3227 | ||
3228 | void | |
3229 | c_register_builtin_type (tree type, const char* name) | |
3230 | { | |
3231 | tree decl; | |
3232 | ||
e60a6f7b | 3233 | decl = build_decl (UNKNOWN_LOCATION, |
3234 | TYPE_DECL, get_identifier (name), type); | |
b268e47e | 3235 | DECL_ARTIFICIAL (decl) = 1; |
3236 | if (!TYPE_NAME (type)) | |
3237 | TYPE_NAME (type) = decl; | |
3238 | pushdecl (decl); | |
5b247e9f | 3239 | |
3240 | registered_builtin_types = tree_cons (0, type, registered_builtin_types); | |
b268e47e | 3241 | } |
a9b9d10c | 3242 | \f |
aff9e656 | 3243 | /* Print an error message for invalid operands to arith operation |
8e70fb09 | 3244 | CODE with TYPE0 for operand 0, and TYPE1 for operand 1. |
3245 | LOCATION is the location of the message. */ | |
b0fc3e72 | 3246 | |
3247 | void | |
8e70fb09 | 3248 | binary_op_error (location_t location, enum tree_code code, |
3249 | tree type0, tree type1) | |
b0fc3e72 | 3250 | { |
19cb6b50 | 3251 | const char *opname; |
f03946e4 | 3252 | |
b0fc3e72 | 3253 | switch (code) |
3254 | { | |
b0fc3e72 | 3255 | case PLUS_EXPR: |
3256 | opname = "+"; break; | |
3257 | case MINUS_EXPR: | |
3258 | opname = "-"; break; | |
3259 | case MULT_EXPR: | |
3260 | opname = "*"; break; | |
3261 | case MAX_EXPR: | |
3262 | opname = "max"; break; | |
3263 | case MIN_EXPR: | |
3264 | opname = "min"; break; | |
3265 | case EQ_EXPR: | |
3266 | opname = "=="; break; | |
3267 | case NE_EXPR: | |
3268 | opname = "!="; break; | |
3269 | case LE_EXPR: | |
3270 | opname = "<="; break; | |
3271 | case GE_EXPR: | |
3272 | opname = ">="; break; | |
3273 | case LT_EXPR: | |
3274 | opname = "<"; break; | |
3275 | case GT_EXPR: | |
3276 | opname = ">"; break; | |
3277 | case LSHIFT_EXPR: | |
3278 | opname = "<<"; break; | |
3279 | case RSHIFT_EXPR: | |
3280 | opname = ">>"; break; | |
3281 | case TRUNC_MOD_EXPR: | |
66618a1e | 3282 | case FLOOR_MOD_EXPR: |
b0fc3e72 | 3283 | opname = "%"; break; |
3284 | case TRUNC_DIV_EXPR: | |
66618a1e | 3285 | case FLOOR_DIV_EXPR: |
b0fc3e72 | 3286 | opname = "/"; break; |
3287 | case BIT_AND_EXPR: | |
3288 | opname = "&"; break; | |
3289 | case BIT_IOR_EXPR: | |
3290 | opname = "|"; break; | |
3291 | case TRUTH_ANDIF_EXPR: | |
3292 | opname = "&&"; break; | |
3293 | case TRUTH_ORIF_EXPR: | |
3294 | opname = "||"; break; | |
3295 | case BIT_XOR_EXPR: | |
3296 | opname = "^"; break; | |
31f820d2 | 3297 | default: |
315ba355 | 3298 | gcc_unreachable (); |
b0fc3e72 | 3299 | } |
8e70fb09 | 3300 | error_at (location, |
3301 | "invalid operands to binary %s (have %qT and %qT)", opname, | |
3302 | type0, type1); | |
b0fc3e72 | 3303 | } |
3304 | \f | |
3305 | /* Subroutine of build_binary_op, used for comparison operations. | |
3306 | See if the operands have both been converted from subword integer types | |
3307 | and, if so, perhaps change them both back to their original type. | |
5b511807 | 3308 | This function is also responsible for converting the two operands |
3309 | to the proper common type for comparison. | |
b0fc3e72 | 3310 | |
3311 | The arguments of this function are all pointers to local variables | |
3312 | of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1, | |
3313 | RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE. | |
3314 | ||
3315 | If this function returns nonzero, it means that the comparison has | |
3316 | a constant value. What this function returns is an expression for | |
3317 | that value. */ | |
3318 | ||
3319 | tree | |
1cae46be | 3320 | shorten_compare (tree *op0_ptr, tree *op1_ptr, tree *restype_ptr, |
3321 | enum tree_code *rescode_ptr) | |
b0fc3e72 | 3322 | { |
19cb6b50 | 3323 | tree type; |
b0fc3e72 | 3324 | tree op0 = *op0_ptr; |
3325 | tree op1 = *op1_ptr; | |
3326 | int unsignedp0, unsignedp1; | |
3327 | int real1, real2; | |
3328 | tree primop0, primop1; | |
3329 | enum tree_code code = *rescode_ptr; | |
3330 | ||
3331 | /* Throw away any conversions to wider types | |
3332 | already present in the operands. */ | |
3333 | ||
7f506bca | 3334 | primop0 = c_common_get_narrower (op0, &unsignedp0); |
3335 | primop1 = c_common_get_narrower (op1, &unsignedp1); | |
b0fc3e72 | 3336 | |
119d06b2 | 3337 | /* If primopN is first sign-extended from primopN's precision to opN's |
3338 | precision, then zero-extended from opN's precision to | |
3339 | *restype_ptr precision, shortenings might be invalid. */ | |
3340 | if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0)) | |
3341 | && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr) | |
3342 | && !unsignedp0 | |
3343 | && TYPE_UNSIGNED (TREE_TYPE (op0))) | |
3344 | primop0 = op0; | |
3345 | if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1)) | |
3346 | && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr) | |
3347 | && !unsignedp1 | |
3348 | && TYPE_UNSIGNED (TREE_TYPE (op1))) | |
3349 | primop1 = op1; | |
3350 | ||
b0fc3e72 | 3351 | /* Handle the case that OP0 does not *contain* a conversion |
3352 | but it *requires* conversion to FINAL_TYPE. */ | |
3353 | ||
3354 | if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr) | |
78a8ed03 | 3355 | unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0)); |
b0fc3e72 | 3356 | if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr) |
78a8ed03 | 3357 | unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1)); |
b0fc3e72 | 3358 | |
3359 | /* If one of the operands must be floated, we cannot optimize. */ | |
3360 | real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE; | |
3361 | real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE; | |
3362 | ||
3363 | /* If first arg is constant, swap the args (changing operation | |
2bd278cc | 3364 | so value is preserved), for canonicalization. Don't do this if |
3365 | the second arg is 0. */ | |
b0fc3e72 | 3366 | |
2bd278cc | 3367 | if (TREE_CONSTANT (primop0) |
9421ebb9 | 3368 | && !integer_zerop (primop1) && !real_zerop (primop1) |
3369 | && !fixed_zerop (primop1)) | |
b0fc3e72 | 3370 | { |
19cb6b50 | 3371 | tree tem = primop0; |
3372 | int temi = unsignedp0; | |
b0fc3e72 | 3373 | primop0 = primop1; |
3374 | primop1 = tem; | |
3375 | tem = op0; | |
3376 | op0 = op1; | |
3377 | op1 = tem; | |
3378 | *op0_ptr = op0; | |
3379 | *op1_ptr = op1; | |
3380 | unsignedp0 = unsignedp1; | |
3381 | unsignedp1 = temi; | |
3382 | temi = real1; | |
3383 | real1 = real2; | |
3384 | real2 = temi; | |
3385 | ||
3386 | switch (code) | |
3387 | { | |
3388 | case LT_EXPR: | |
3389 | code = GT_EXPR; | |
3390 | break; | |
3391 | case GT_EXPR: | |
3392 | code = LT_EXPR; | |
3393 | break; | |
3394 | case LE_EXPR: | |
3395 | code = GE_EXPR; | |
3396 | break; | |
3397 | case GE_EXPR: | |
3398 | code = LE_EXPR; | |
3399 | break; | |
31f820d2 | 3400 | default: |
3401 | break; | |
b0fc3e72 | 3402 | } |
3403 | *rescode_ptr = code; | |
3404 | } | |
3405 | ||
3406 | /* If comparing an integer against a constant more bits wide, | |
3407 | maybe we can deduce a value of 1 or 0 independent of the data. | |
3408 | Or else truncate the constant now | |
3409 | rather than extend the variable at run time. | |
3410 | ||
3411 | This is only interesting if the constant is the wider arg. | |
3412 | Also, it is not safe if the constant is unsigned and the | |
3413 | variable arg is signed, since in this case the variable | |
3414 | would be sign-extended and then regarded as unsigned. | |
3415 | Our technique fails in this case because the lowest/highest | |
3416 | possible unsigned results don't follow naturally from the | |
3417 | lowest/highest possible values of the variable operand. | |
3418 | For just EQ_EXPR and NE_EXPR there is another technique that | |
3419 | could be used: see if the constant can be faithfully represented | |
3420 | in the other operand's type, by truncating it and reextending it | |
3421 | and see if that preserves the constant's value. */ | |
3422 | ||
3423 | if (!real1 && !real2 | |
9421ebb9 | 3424 | && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE |
b0fc3e72 | 3425 | && TREE_CODE (primop1) == INTEGER_CST |
3426 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)) | |
3427 | { | |
3428 | int min_gt, max_gt, min_lt, max_lt; | |
3429 | tree maxval, minval; | |
3430 | /* 1 if comparison is nominally unsigned. */ | |
78a8ed03 | 3431 | int unsignedp = TYPE_UNSIGNED (*restype_ptr); |
b0fc3e72 | 3432 | tree val; |
3433 | ||
4070745f | 3434 | type = c_common_signed_or_unsigned_type (unsignedp0, |
3435 | TREE_TYPE (primop0)); | |
cda09c61 | 3436 | |
b0fc3e72 | 3437 | maxval = TYPE_MAX_VALUE (type); |
3438 | minval = TYPE_MIN_VALUE (type); | |
3439 | ||
3440 | if (unsignedp && !unsignedp0) | |
4070745f | 3441 | *restype_ptr = c_common_signed_type (*restype_ptr); |
b0fc3e72 | 3442 | |
3443 | if (TREE_TYPE (primop1) != *restype_ptr) | |
18dbec6f | 3444 | { |
9a5e8086 | 3445 | /* Convert primop1 to target type, but do not introduce |
3446 | additional overflow. We know primop1 is an int_cst. */ | |
c8110c8f | 3447 | primop1 = force_fit_type_double (*restype_ptr, |
d3237426 | 3448 | tree_to_double_int (primop1), |
3449 | 0, TREE_OVERFLOW (primop1)); | |
18dbec6f | 3450 | } |
b0fc3e72 | 3451 | if (type != *restype_ptr) |
3452 | { | |
3453 | minval = convert (*restype_ptr, minval); | |
3454 | maxval = convert (*restype_ptr, maxval); | |
3455 | } | |
3456 | ||
3457 | if (unsignedp && unsignedp0) | |
3458 | { | |
3459 | min_gt = INT_CST_LT_UNSIGNED (primop1, minval); | |
3460 | max_gt = INT_CST_LT_UNSIGNED (primop1, maxval); | |
3461 | min_lt = INT_CST_LT_UNSIGNED (minval, primop1); | |
3462 | max_lt = INT_CST_LT_UNSIGNED (maxval, primop1); | |
3463 | } | |
3464 | else | |
3465 | { | |
3466 | min_gt = INT_CST_LT (primop1, minval); | |
3467 | max_gt = INT_CST_LT (primop1, maxval); | |
3468 | min_lt = INT_CST_LT (minval, primop1); | |
3469 | max_lt = INT_CST_LT (maxval, primop1); | |
3470 | } | |
3471 | ||
3472 | val = 0; | |
3473 | /* This used to be a switch, but Genix compiler can't handle that. */ | |
3474 | if (code == NE_EXPR) | |
3475 | { | |
3476 | if (max_lt || min_gt) | |
3c2239cf | 3477 | val = truthvalue_true_node; |
b0fc3e72 | 3478 | } |
3479 | else if (code == EQ_EXPR) | |
3480 | { | |
3481 | if (max_lt || min_gt) | |
3c2239cf | 3482 | val = truthvalue_false_node; |
b0fc3e72 | 3483 | } |
3484 | else if (code == LT_EXPR) | |
3485 | { | |
3486 | if (max_lt) | |
3c2239cf | 3487 | val = truthvalue_true_node; |
b0fc3e72 | 3488 | if (!min_lt) |
3c2239cf | 3489 | val = truthvalue_false_node; |
b0fc3e72 | 3490 | } |
3491 | else if (code == GT_EXPR) | |
3492 | { | |
3493 | if (min_gt) | |
3c2239cf | 3494 | val = truthvalue_true_node; |
b0fc3e72 | 3495 | if (!max_gt) |
3c2239cf | 3496 | val = truthvalue_false_node; |
b0fc3e72 | 3497 | } |
3498 | else if (code == LE_EXPR) | |
3499 | { | |
3500 | if (!max_gt) | |
3c2239cf | 3501 | val = truthvalue_true_node; |
b0fc3e72 | 3502 | if (min_gt) |
3c2239cf | 3503 | val = truthvalue_false_node; |
b0fc3e72 | 3504 | } |
3505 | else if (code == GE_EXPR) | |
3506 | { | |
3507 | if (!min_lt) | |
3c2239cf | 3508 | val = truthvalue_true_node; |
b0fc3e72 | 3509 | if (max_lt) |
3c2239cf | 3510 | val = truthvalue_false_node; |
b0fc3e72 | 3511 | } |
3512 | ||
3513 | /* If primop0 was sign-extended and unsigned comparison specd, | |
3514 | we did a signed comparison above using the signed type bounds. | |
3515 | But the comparison we output must be unsigned. | |
3516 | ||
3517 | Also, for inequalities, VAL is no good; but if the signed | |
3518 | comparison had *any* fixed result, it follows that the | |
3519 | unsigned comparison just tests the sign in reverse | |
3520 | (positive values are LE, negative ones GE). | |
3521 | So we can generate an unsigned comparison | |
3522 | against an extreme value of the signed type. */ | |
3523 | ||
3524 | if (unsignedp && !unsignedp0) | |
3525 | { | |
3526 | if (val != 0) | |
3527 | switch (code) | |
3528 | { | |
3529 | case LT_EXPR: | |
3530 | case GE_EXPR: | |
3531 | primop1 = TYPE_MIN_VALUE (type); | |
3532 | val = 0; | |
3533 | break; | |
3534 | ||
3535 | case LE_EXPR: | |
3536 | case GT_EXPR: | |
3537 | primop1 = TYPE_MAX_VALUE (type); | |
3538 | val = 0; | |
3539 | break; | |
31f820d2 | 3540 | |
3541 | default: | |
3542 | break; | |
b0fc3e72 | 3543 | } |
11773141 | 3544 | type = c_common_unsigned_type (type); |
b0fc3e72 | 3545 | } |
3546 | ||
fe5f2366 | 3547 | if (TREE_CODE (primop0) != INTEGER_CST) |
b0fc3e72 | 3548 | { |
3c2239cf | 3549 | if (val == truthvalue_false_node) |
e6e352cb | 3550 | warning (OPT_Wtype_limits, "comparison is always false due to limited range of data type"); |
3c2239cf | 3551 | if (val == truthvalue_true_node) |
e6e352cb | 3552 | warning (OPT_Wtype_limits, "comparison is always true due to limited range of data type"); |
b0fc3e72 | 3553 | } |
3554 | ||
3555 | if (val != 0) | |
3556 | { | |
3557 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
3558 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 3559 | return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val); |
b0fc3e72 | 3560 | return val; |
3561 | } | |
3562 | ||
3563 | /* Value is not predetermined, but do the comparison | |
3564 | in the type of the operand that is not constant. | |
3565 | TYPE is already properly set. */ | |
3566 | } | |
c4503c0a | 3567 | |
3568 | /* If either arg is decimal float and the other is float, find the | |
3569 | proper common type to use for comparison. */ | |
3570 | else if (real1 && real2 | |
3571 | && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0))) | |
3572 | || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))) | |
3573 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
3574 | ||
b0fc3e72 | 3575 | else if (real1 && real2 |
2203bd5c | 3576 | && (TYPE_PRECISION (TREE_TYPE (primop0)) |
3577 | == TYPE_PRECISION (TREE_TYPE (primop1)))) | |
b0fc3e72 | 3578 | type = TREE_TYPE (primop0); |
3579 | ||
3580 | /* If args' natural types are both narrower than nominal type | |
3581 | and both extend in the same manner, compare them | |
3582 | in the type of the wider arg. | |
3583 | Otherwise must actually extend both to the nominal | |
3584 | common type lest different ways of extending | |
3585 | alter the result. | |
3586 | (eg, (short)-1 == (unsigned short)-1 should be 0.) */ | |
3587 | ||
3588 | else if (unsignedp0 == unsignedp1 && real1 == real2 | |
3589 | && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr) | |
3590 | && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)) | |
3591 | { | |
3592 | type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)); | |
4070745f | 3593 | type = c_common_signed_or_unsigned_type (unsignedp0 |
78a8ed03 | 3594 | || TYPE_UNSIGNED (*restype_ptr), |
4070745f | 3595 | type); |
b0fc3e72 | 3596 | /* Make sure shorter operand is extended the right way |
3597 | to match the longer operand. */ | |
4070745f | 3598 | primop0 |
3599 | = convert (c_common_signed_or_unsigned_type (unsignedp0, | |
3600 | TREE_TYPE (primop0)), | |
3601 | primop0); | |
3602 | primop1 | |
3603 | = convert (c_common_signed_or_unsigned_type (unsignedp1, | |
3604 | TREE_TYPE (primop1)), | |
3605 | primop1); | |
b0fc3e72 | 3606 | } |
3607 | else | |
3608 | { | |
3609 | /* Here we must do the comparison on the nominal type | |
3610 | using the args exactly as we received them. */ | |
3611 | type = *restype_ptr; | |
3612 | primop0 = op0; | |
3613 | primop1 = op1; | |
3614 | ||
3615 | if (!real1 && !real2 && integer_zerop (primop1) | |
78a8ed03 | 3616 | && TYPE_UNSIGNED (*restype_ptr)) |
b0fc3e72 | 3617 | { |
3618 | tree value = 0; | |
3619 | switch (code) | |
3620 | { | |
3621 | case GE_EXPR: | |
e6e352cb | 3622 | /* All unsigned values are >= 0, so we warn. However, |
3623 | if OP0 is a constant that is >= 0, the signedness of | |
3624 | the comparison isn't an issue, so suppress the | |
3625 | warning. */ | |
3626 | if (warn_type_limits && !in_system_header | |
84166705 | 3627 | && !(TREE_CODE (primop0) == INTEGER_CST |
3628 | && !TREE_OVERFLOW (convert (c_common_signed_type (type), | |
3629 | primop0)))) | |
48e1416a | 3630 | warning (OPT_Wtype_limits, |
e6e352cb | 3631 | "comparison of unsigned expression >= 0 is always true"); |
3c2239cf | 3632 | value = truthvalue_true_node; |
b0fc3e72 | 3633 | break; |
3634 | ||
3635 | case LT_EXPR: | |
e6e352cb | 3636 | if (warn_type_limits && !in_system_header |
84166705 | 3637 | && !(TREE_CODE (primop0) == INTEGER_CST |
3638 | && !TREE_OVERFLOW (convert (c_common_signed_type (type), | |
3639 | primop0)))) | |
48e1416a | 3640 | warning (OPT_Wtype_limits, |
e6e352cb | 3641 | "comparison of unsigned expression < 0 is always false"); |
3c2239cf | 3642 | value = truthvalue_false_node; |
31f820d2 | 3643 | break; |
3644 | ||
3645 | default: | |
3646 | break; | |
b0fc3e72 | 3647 | } |
3648 | ||
3649 | if (value != 0) | |
3650 | { | |
3651 | /* Don't forget to evaluate PRIMOP0 if it has side effects. */ | |
3652 | if (TREE_SIDE_EFFECTS (primop0)) | |
14ae0310 | 3653 | return build2 (COMPOUND_EXPR, TREE_TYPE (value), |
3654 | primop0, value); | |
b0fc3e72 | 3655 | return value; |
3656 | } | |
3657 | } | |
3658 | } | |
3659 | ||
3660 | *op0_ptr = convert (type, primop0); | |
3661 | *op1_ptr = convert (type, primop1); | |
3662 | ||
3c2239cf | 3663 | *restype_ptr = truthvalue_type_node; |
b0fc3e72 | 3664 | |
3665 | return 0; | |
3666 | } | |
3667 | \f | |
1c26100f | 3668 | /* Return a tree for the sum or difference (RESULTCODE says which) |
3669 | of pointer PTROP and integer INTOP. */ | |
3670 | ||
3671 | tree | |
389dd41b | 3672 | pointer_int_sum (location_t loc, enum tree_code resultcode, |
3673 | tree ptrop, tree intop) | |
1c26100f | 3674 | { |
add6ee5e | 3675 | tree size_exp, ret; |
1c26100f | 3676 | |
1c26100f | 3677 | /* The result is a pointer of the same type that is being added. */ |
1c26100f | 3678 | tree result_type = TREE_TYPE (ptrop); |
3679 | ||
3680 | if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE) | |
3681 | { | |
48e1416a | 3682 | pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
8864917d | 3683 | "pointer of type %<void *%> used in arithmetic"); |
1c26100f | 3684 | size_exp = integer_one_node; |
3685 | } | |
3686 | else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE) | |
3687 | { | |
48e1416a | 3688 | pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
8864917d | 3689 | "pointer to a function used in arithmetic"); |
1c26100f | 3690 | size_exp = integer_one_node; |
3691 | } | |
3692 | else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE) | |
3693 | { | |
48e1416a | 3694 | pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
8864917d | 3695 | "pointer to member function used in arithmetic"); |
1c26100f | 3696 | size_exp = integer_one_node; |
3697 | } | |
1c26100f | 3698 | else |
3699 | size_exp = size_in_bytes (TREE_TYPE (result_type)); | |
3700 | ||
add6ee5e | 3701 | /* We are manipulating pointer values, so we don't need to warn |
3702 | about relying on undefined signed overflow. We disable the | |
3703 | warning here because we use integer types so fold won't know that | |
3704 | they are really pointers. */ | |
3705 | fold_defer_overflow_warnings (); | |
3706 | ||
1c26100f | 3707 | /* If what we are about to multiply by the size of the elements |
3708 | contains a constant term, apply distributive law | |
3709 | and multiply that constant term separately. | |
3710 | This helps produce common subexpressions. */ | |
1c26100f | 3711 | if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR) |
84166705 | 3712 | && !TREE_CONSTANT (intop) |
1c26100f | 3713 | && TREE_CONSTANT (TREE_OPERAND (intop, 1)) |
3714 | && TREE_CONSTANT (size_exp) | |
3715 | /* If the constant comes from pointer subtraction, | |
3716 | skip this optimization--it would cause an error. */ | |
3717 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE | |
3718 | /* If the constant is unsigned, and smaller than the pointer size, | |
3719 | then we must skip this optimization. This is because it could cause | |
3720 | an overflow error if the constant is negative but INTOP is not. */ | |
84166705 | 3721 | && (!TYPE_UNSIGNED (TREE_TYPE (intop)) |
1c26100f | 3722 | || (TYPE_PRECISION (TREE_TYPE (intop)) |
3723 | == TYPE_PRECISION (TREE_TYPE (ptrop))))) | |
3724 | { | |
3725 | enum tree_code subcode = resultcode; | |
3726 | tree int_type = TREE_TYPE (intop); | |
3727 | if (TREE_CODE (intop) == MINUS_EXPR) | |
3728 | subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR); | |
3729 | /* Convert both subexpression types to the type of intop, | |
3730 | because weird cases involving pointer arithmetic | |
3731 | can result in a sum or difference with different type args. */ | |
8e70fb09 | 3732 | ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)), |
3733 | subcode, ptrop, | |
1c26100f | 3734 | convert (int_type, TREE_OPERAND (intop, 1)), 1); |
3735 | intop = convert (int_type, TREE_OPERAND (intop, 0)); | |
3736 | } | |
3737 | ||
3738 | /* Convert the integer argument to a type the same size as sizetype | |
3739 | so the multiply won't overflow spuriously. */ | |
1c26100f | 3740 | if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype) |
78a8ed03 | 3741 | || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype)) |
1cae46be | 3742 | intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype), |
78a8ed03 | 3743 | TYPE_UNSIGNED (sizetype)), intop); |
1c26100f | 3744 | |
3745 | /* Replace the integer argument with a suitable product by the object size. | |
c6feb9f1 | 3746 | Do this multiplication as signed, then convert to the appropriate type |
3747 | for the pointer operation and disregard an overflow that occured only | |
3748 | because of the sign-extension change in the latter conversion. */ | |
3749 | { | |
3750 | tree t = build_binary_op (loc, | |
3751 | MULT_EXPR, intop, | |
3752 | convert (TREE_TYPE (intop), size_exp), 1); | |
3753 | intop = convert (sizetype, t); | |
3754 | if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t)) | |
3755 | intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop), | |
3756 | TREE_INT_CST_HIGH (intop)); | |
3757 | } | |
0de36bdb | 3758 | |
499e523f | 3759 | /* Create the sum or difference. */ |
0de36bdb | 3760 | if (resultcode == MINUS_EXPR) |
389dd41b | 3761 | intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop); |
1c26100f | 3762 | |
389dd41b | 3763 | ret = fold_build2_loc (loc, POINTER_PLUS_EXPR, result_type, ptrop, intop); |
add6ee5e | 3764 | |
3765 | fold_undefer_and_ignore_overflow_warnings (); | |
3766 | ||
3767 | return ret; | |
1c26100f | 3768 | } |
3769 | \f | |
f59e3889 | 3770 | /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded |
3771 | and if NON_CONST is known not to be permitted in an evaluated part | |
3772 | of a constant expression. */ | |
3773 | ||
3774 | tree | |
3775 | c_wrap_maybe_const (tree expr, bool non_const) | |
3776 | { | |
3777 | bool nowarning = TREE_NO_WARNING (expr); | |
3778 | location_t loc = EXPR_LOCATION (expr); | |
3779 | ||
3780 | /* This should never be called for C++. */ | |
3781 | if (c_dialect_cxx ()) | |
3782 | gcc_unreachable (); | |
3783 | ||
3784 | /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */ | |
3785 | STRIP_TYPE_NOPS (expr); | |
3786 | expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr); | |
3787 | C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const; | |
3788 | if (nowarning) | |
3789 | TREE_NO_WARNING (expr) = 1; | |
3790 | protected_set_expr_location (expr, loc); | |
3791 | ||
3792 | return expr; | |
3793 | } | |
3794 | ||
a75b1c71 | 3795 | /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but |
3796 | for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR | |
3797 | around the SAVE_EXPR if needed so that c_fully_fold does not need | |
3798 | to look inside SAVE_EXPRs. */ | |
3799 | ||
3800 | tree | |
3801 | c_save_expr (tree expr) | |
3802 | { | |
3803 | bool maybe_const = true; | |
3804 | if (c_dialect_cxx ()) | |
3805 | return save_expr (expr); | |
3806 | expr = c_fully_fold (expr, false, &maybe_const); | |
3807 | expr = save_expr (expr); | |
3808 | if (!maybe_const) | |
f59e3889 | 3809 | expr = c_wrap_maybe_const (expr, true); |
a75b1c71 | 3810 | return expr; |
3811 | } | |
3812 | ||
6b68e71a | 3813 | /* Return whether EXPR is a declaration whose address can never be |
3814 | NULL. */ | |
3815 | ||
3816 | bool | |
9f627b1a | 3817 | decl_with_nonnull_addr_p (const_tree expr) |
6b68e71a | 3818 | { |
3819 | return (DECL_P (expr) | |
3820 | && (TREE_CODE (expr) == PARM_DECL | |
3821 | || TREE_CODE (expr) == LABEL_DECL | |
3822 | || !DECL_WEAK (expr))); | |
3823 | } | |
3824 | ||
b0fc3e72 | 3825 | /* Prepare expr to be an argument of a TRUTH_NOT_EXPR, |
45a78cc0 | 3826 | or for an `if' or `while' statement or ?..: exp. It should already |
3827 | have been validated to be of suitable type; otherwise, a bad | |
3828 | diagnostic may result. | |
b0fc3e72 | 3829 | |
8e70fb09 | 3830 | The EXPR is located at LOCATION. |
3831 | ||
b0fc3e72 | 3832 | This preparation consists of taking the ordinary |
3833 | representation of an expression expr and producing a valid tree | |
3834 | boolean expression describing whether expr is nonzero. We could | |
3c2239cf | 3835 | simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1), |
b0fc3e72 | 3836 | but we optimize comparisons, &&, ||, and !. |
3837 | ||
3c2239cf | 3838 | The resulting type should always be `truthvalue_type_node'. */ |
b0fc3e72 | 3839 | |
3840 | tree | |
8e70fb09 | 3841 | c_common_truthvalue_conversion (location_t location, tree expr) |
b0fc3e72 | 3842 | { |
b0fc3e72 | 3843 | switch (TREE_CODE (expr)) |
3844 | { | |
318a728f | 3845 | case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR: |
e1232ce2 | 3846 | case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR: |
3847 | case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR: | |
3848 | case ORDERED_EXPR: case UNORDERED_EXPR: | |
ce04dcdc | 3849 | if (TREE_TYPE (expr) == truthvalue_type_node) |
3850 | return expr; | |
e60a6f7b | 3851 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
ce04dcdc | 3852 | TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1)); |
e60a6f7b | 3853 | goto ret; |
ce04dcdc | 3854 | |
b0fc3e72 | 3855 | case TRUTH_ANDIF_EXPR: |
3856 | case TRUTH_ORIF_EXPR: | |
3857 | case TRUTH_AND_EXPR: | |
3858 | case TRUTH_OR_EXPR: | |
31f6e93c | 3859 | case TRUTH_XOR_EXPR: |
ce04dcdc | 3860 | if (TREE_TYPE (expr) == truthvalue_type_node) |
3861 | return expr; | |
e60a6f7b | 3862 | expr = build2 (TREE_CODE (expr), truthvalue_type_node, |
48e1416a | 3863 | c_common_truthvalue_conversion (location, |
e60a6f7b | 3864 | TREE_OPERAND (expr, 0)), |
3865 | c_common_truthvalue_conversion (location, | |
3866 | TREE_OPERAND (expr, 1))); | |
3867 | goto ret; | |
3e851b85 | 3868 | |
f6e28f72 | 3869 | case TRUTH_NOT_EXPR: |
ce04dcdc | 3870 | if (TREE_TYPE (expr) == truthvalue_type_node) |
3871 | return expr; | |
e60a6f7b | 3872 | expr = build1 (TREE_CODE (expr), truthvalue_type_node, |
3873 | c_common_truthvalue_conversion (location, | |
3874 | TREE_OPERAND (expr, 0))); | |
3875 | goto ret; | |
f6e28f72 | 3876 | |
b0fc3e72 | 3877 | case ERROR_MARK: |
3878 | return expr; | |
3879 | ||
3880 | case INTEGER_CST: | |
eddad94a | 3881 | return integer_zerop (expr) ? truthvalue_false_node |
3882 | : truthvalue_true_node; | |
b0fc3e72 | 3883 | |
3884 | case REAL_CST: | |
5000e21c | 3885 | return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0) |
3886 | ? truthvalue_true_node | |
3887 | : truthvalue_false_node; | |
b0fc3e72 | 3888 | |
9421ebb9 | 3889 | case FIXED_CST: |
3890 | return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr), | |
3891 | &FCONST0 (TYPE_MODE (TREE_TYPE (expr)))) | |
3892 | ? truthvalue_true_node | |
3893 | : truthvalue_false_node; | |
3894 | ||
ce04dcdc | 3895 | case FUNCTION_DECL: |
b6889cb0 | 3896 | expr = build_unary_op (location, ADDR_EXPR, expr, 0); |
ce04dcdc | 3897 | /* Fall through. */ |
3898 | ||
b0fc3e72 | 3899 | case ADDR_EXPR: |
ce871053 | 3900 | { |
ee7d4d6a | 3901 | tree inner = TREE_OPERAND (expr, 0); |
6b68e71a | 3902 | if (decl_with_nonnull_addr_p (inner)) |
ce871053 | 3903 | { |
6b68e71a | 3904 | /* Common Ada/Pascal programmer's mistake. */ |
8e70fb09 | 3905 | warning_at (location, |
3906 | OPT_Waddress, | |
3907 | "the address of %qD will always evaluate as %<true%>", | |
3908 | inner); | |
ce871053 | 3909 | return truthvalue_true_node; |
3910 | } | |
d473d901 | 3911 | break; |
ce871053 | 3912 | } |
b0fc3e72 | 3913 | |
2203bd5c | 3914 | case COMPLEX_EXPR: |
e60a6f7b | 3915 | expr = build_binary_op (EXPR_LOCATION (expr), |
8e70fb09 | 3916 | (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) |
95809de4 | 3917 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
8e70fb09 | 3918 | c_common_truthvalue_conversion (location, |
3919 | TREE_OPERAND (expr, 0)), | |
3920 | c_common_truthvalue_conversion (location, | |
3921 | TREE_OPERAND (expr, 1)), | |
2203bd5c | 3922 | 0); |
e60a6f7b | 3923 | goto ret; |
2203bd5c | 3924 | |
b0fc3e72 | 3925 | case NEGATE_EXPR: |
3926 | case ABS_EXPR: | |
3927 | case FLOAT_EXPR: | |
c6418a4e | 3928 | case EXCESS_PRECISION_EXPR: |
d10cfa8d | 3929 | /* These don't change whether an object is nonzero or zero. */ |
8e70fb09 | 3930 | return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); |
b0fc3e72 | 3931 | |
3932 | case LROTATE_EXPR: | |
3933 | case RROTATE_EXPR: | |
d10cfa8d | 3934 | /* These don't change whether an object is zero or nonzero, but |
b0fc3e72 | 3935 | we can't ignore them if their second arg has side-effects. */ |
3936 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))) | |
e60a6f7b | 3937 | { |
3938 | expr = build2 (COMPOUND_EXPR, truthvalue_type_node, | |
3939 | TREE_OPERAND (expr, 1), | |
48e1416a | 3940 | c_common_truthvalue_conversion |
e60a6f7b | 3941 | (location, TREE_OPERAND (expr, 0))); |
3942 | goto ret; | |
3943 | } | |
b0fc3e72 | 3944 | else |
8e70fb09 | 3945 | return c_common_truthvalue_conversion (location, |
3946 | TREE_OPERAND (expr, 0)); | |
73be5127 | 3947 | |
b0fc3e72 | 3948 | case COND_EXPR: |
3949 | /* Distribute the conversion into the arms of a COND_EXPR. */ | |
a75b1c71 | 3950 | if (c_dialect_cxx ()) |
e60a6f7b | 3951 | { |
d0389adc | 3952 | tree op1 = TREE_OPERAND (expr, 1); |
3953 | tree op2 = TREE_OPERAND (expr, 2); | |
3954 | /* In C++ one of the arms might have void type if it is throw. */ | |
3955 | if (!VOID_TYPE_P (TREE_TYPE (op1))) | |
3956 | op1 = c_common_truthvalue_conversion (location, op1); | |
3957 | if (!VOID_TYPE_P (TREE_TYPE (op2))) | |
3958 | op2 = c_common_truthvalue_conversion (location, op2); | |
389dd41b | 3959 | expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node, |
d0389adc | 3960 | TREE_OPERAND (expr, 0), op1, op2); |
e60a6f7b | 3961 | goto ret; |
3962 | } | |
a75b1c71 | 3963 | else |
e60a6f7b | 3964 | { |
3965 | /* Folding will happen later for C. */ | |
3966 | expr = build3 (COND_EXPR, truthvalue_type_node, | |
3967 | TREE_OPERAND (expr, 0), | |
3968 | c_common_truthvalue_conversion (location, | |
3969 | TREE_OPERAND (expr, 1)), | |
3970 | c_common_truthvalue_conversion (location, | |
3971 | TREE_OPERAND (expr, 2))); | |
3972 | goto ret; | |
3973 | } | |
b0fc3e72 | 3974 | |
72dd6141 | 3975 | CASE_CONVERT: |
f8913d47 | 3976 | { |
3977 | tree totype = TREE_TYPE (expr); | |
3978 | tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0)); | |
3979 | ||
3980 | /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE, | |
3981 | since that affects how `default_conversion' will behave. */ | |
3982 | if (TREE_CODE (totype) == REFERENCE_TYPE | |
3983 | || TREE_CODE (fromtype) == REFERENCE_TYPE) | |
3984 | break; | |
3985 | /* Don't strip a conversion from C++0x scoped enum, since they | |
3986 | don't implicitly convert to other types. */ | |
3987 | if (TREE_CODE (fromtype) == ENUMERAL_TYPE | |
3988 | && ENUM_IS_SCOPED (fromtype)) | |
3989 | break; | |
3990 | /* If this isn't narrowing the argument, we can ignore it. */ | |
3991 | if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype)) | |
3992 | return c_common_truthvalue_conversion (location, | |
3993 | TREE_OPERAND (expr, 0)); | |
3994 | } | |
b0fc3e72 | 3995 | break; |
3996 | ||
16837b18 | 3997 | case MODIFY_EXPR: |
60a0513e | 3998 | if (!TREE_NO_WARNING (expr) |
3999 | && warn_parentheses) | |
4000 | { | |
4001 | warning (OPT_Wparentheses, | |
4002 | "suggest parentheses around assignment used as truth value"); | |
4003 | TREE_NO_WARNING (expr) = 1; | |
4004 | } | |
16837b18 | 4005 | break; |
73be5127 | 4006 | |
31f820d2 | 4007 | default: |
4008 | break; | |
b0fc3e72 | 4009 | } |
4010 | ||
2ba726d2 | 4011 | if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE) |
a0748b7d | 4012 | { |
93be21c0 | 4013 | tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr)); |
e60a6f7b | 4014 | expr = (build_binary_op |
8e70fb09 | 4015 | (EXPR_LOCATION (expr), |
4016 | (TREE_SIDE_EFFECTS (expr) | |
a0748b7d | 4017 | ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR), |
b6889cb0 | 4018 | c_common_truthvalue_conversion |
4019 | (location, | |
4020 | build_unary_op (location, REALPART_EXPR, t, 0)), | |
4021 | c_common_truthvalue_conversion | |
4022 | (location, | |
4023 | build_unary_op (location, IMAGPART_EXPR, t, 0)), | |
a0748b7d | 4024 | 0)); |
e60a6f7b | 4025 | goto ret; |
a0748b7d | 4026 | } |
2ba726d2 | 4027 | |
9421ebb9 | 4028 | if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE) |
4029 | { | |
4030 | tree fixed_zero_node = build_fixed (TREE_TYPE (expr), | |
4031 | FCONST0 (TYPE_MODE | |
4032 | (TREE_TYPE (expr)))); | |
43158006 | 4033 | return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1); |
9421ebb9 | 4034 | } |
e60a6f7b | 4035 | else |
4036 | return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1); | |
9421ebb9 | 4037 | |
e60a6f7b | 4038 | ret: |
4039 | protected_set_expr_location (expr, location); | |
4040 | return expr; | |
b0fc3e72 | 4041 | } |
4042 | \f | |
3237155d | 4043 | static void def_builtin_1 (enum built_in_function fncode, |
4044 | const char *name, | |
4045 | enum built_in_class fnclass, | |
4046 | tree fntype, tree libtype, | |
4047 | bool both_p, bool fallback_p, bool nonansi_p, | |
4048 | tree fnattrs, bool implicit_p); | |
0d4238dc | 4049 | |
a5b1863e | 4050 | |
4051 | /* Apply the TYPE_QUALS to the new DECL. */ | |
4052 | ||
4053 | void | |
1cae46be | 4054 | c_apply_type_quals_to_decl (int type_quals, tree decl) |
a5b1863e | 4055 | { |
adfb367f | 4056 | tree type = TREE_TYPE (decl); |
b27ac6b5 | 4057 | |
e4eabbe4 | 4058 | if (type == error_mark_node) |
4059 | return; | |
adfb367f | 4060 | |
4061 | if (((type_quals & TYPE_QUAL_CONST) | |
4062 | || (type && TREE_CODE (type) == REFERENCE_TYPE)) | |
4063 | /* An object declared 'const' is only readonly after it is | |
4064 | initialized. We don't have any way of expressing this currently, | |
4065 | so we need to be conservative and unset TREE_READONLY for types | |
4066 | with constructors. Otherwise aliasing code will ignore stores in | |
4067 | an inline constructor. */ | |
4068 | && !(type && TYPE_NEEDS_CONSTRUCTING (type))) | |
a5b1863e | 4069 | TREE_READONLY (decl) = 1; |
4070 | if (type_quals & TYPE_QUAL_VOLATILE) | |
4071 | { | |
4072 | TREE_SIDE_EFFECTS (decl) = 1; | |
4073 | TREE_THIS_VOLATILE (decl) = 1; | |
4074 | } | |
d91a20bc | 4075 | if (type_quals & TYPE_QUAL_RESTRICT) |
a5b1863e | 4076 | { |
adfb367f | 4077 | while (type && TREE_CODE (type) == ARRAY_TYPE) |
4078 | /* Allow 'restrict' on arrays of pointers. | |
4079 | FIXME currently we just ignore it. */ | |
4080 | type = TREE_TYPE (type); | |
4081 | if (!type | |
4082 | || !POINTER_TYPE_P (type) | |
4083 | || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))) | |
b0b1af64 | 4084 | error ("invalid use of %<restrict%>"); |
a5b1863e | 4085 | } |
4086 | } | |
4087 | ||
4ee9c684 | 4088 | /* Hash function for the problem of multiple type definitions in |
4089 | different files. This must hash all types that will compare | |
4090 | equal via comptypes to the same value. In practice it hashes | |
0bed3869 | 4091 | on some of the simple stuff and leaves the details to comptypes. */ |
4ee9c684 | 4092 | |
4093 | static hashval_t | |
4094 | c_type_hash (const void *p) | |
4095 | { | |
ecf2703d | 4096 | int n_elements; |
4ee9c684 | 4097 | int shift, size; |
aae87fc3 | 4098 | const_tree const t = (const_tree) p; |
4ee9c684 | 4099 | tree t2; |
4100 | switch (TREE_CODE (t)) | |
4101 | { | |
fbf0afd1 | 4102 | /* For pointers, hash on pointee type plus some swizzling. */ |
2363ef00 | 4103 | case POINTER_TYPE: |
4104 | return c_type_hash (TREE_TYPE (t)) ^ 0x3003003; | |
4105 | /* Hash on number of elements and total size. */ | |
4106 | case ENUMERAL_TYPE: | |
4107 | shift = 3; | |
4108 | t2 = TYPE_VALUES (t); | |
4109 | break; | |
4110 | case RECORD_TYPE: | |
4111 | shift = 0; | |
4112 | t2 = TYPE_FIELDS (t); | |
4113 | break; | |
4114 | case QUAL_UNION_TYPE: | |
4115 | shift = 1; | |
4116 | t2 = TYPE_FIELDS (t); | |
4117 | break; | |
4118 | case UNION_TYPE: | |
4119 | shift = 2; | |
4120 | t2 = TYPE_FIELDS (t); | |
4121 | break; | |
4122 | default: | |
231bd014 | 4123 | gcc_unreachable (); |
4ee9c684 | 4124 | } |
ecf2703d | 4125 | /* FIXME: We want to use a DECL_CHAIN iteration method here, but |
4126 | TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */ | |
4127 | n_elements = list_length (t2); | |
78c2e180 | 4128 | /* We might have a VLA here. */ |
4129 | if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST) | |
4130 | size = 0; | |
4131 | else | |
4132 | size = TREE_INT_CST_LOW (TYPE_SIZE (t)); | |
ecf2703d | 4133 | return ((size << 24) | (n_elements << shift)); |
4ee9c684 | 4134 | } |
4135 | ||
1ecd4018 | 4136 | static GTY((param_is (union tree_node))) htab_t type_hash_table; |
4137 | ||
b5ba9f3a | 4138 | /* Return the typed-based alias set for T, which may be an expression |
f7c44134 | 4139 | or a type. Return -1 if we don't do anything special. */ |
b5ba9f3a | 4140 | |
32c2fdea | 4141 | alias_set_type |
1cae46be | 4142 | c_common_get_alias_set (tree t) |
b5ba9f3a | 4143 | { |
be4f2de7 | 4144 | tree u; |
4ee9c684 | 4145 | PTR *slot; |
1cae46be | 4146 | |
e58c17e7 | 4147 | /* For VLAs, use the alias set of the element type rather than the |
4148 | default of alias set 0 for types compared structurally. */ | |
4149 | if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t)) | |
4150 | { | |
4151 | if (TREE_CODE (t) == ARRAY_TYPE) | |
4152 | return get_alias_set (TREE_TYPE (t)); | |
4153 | return -1; | |
4154 | } | |
4155 | ||
be4f2de7 | 4156 | /* Permit type-punning when accessing a union, provided the access |
4157 | is directly through the union. For example, this code does not | |
4158 | permit taking the address of a union member and then storing | |
4159 | through it. Even the type-punning allowed here is a GCC | |
4160 | extension, albeit a common and useful one; the C standard says | |
4161 | that such accesses have implementation-defined behavior. */ | |
4162 | for (u = t; | |
4163 | TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF; | |
4164 | u = TREE_OPERAND (u, 0)) | |
4165 | if (TREE_CODE (u) == COMPONENT_REF | |
4166 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE) | |
4167 | return 0; | |
1e2513d9 | 4168 | |
9fcc3e54 | 4169 | /* That's all the expressions we handle specially. */ |
84166705 | 4170 | if (!TYPE_P (t)) |
9fcc3e54 | 4171 | return -1; |
4172 | ||
d716ce75 | 4173 | /* The C standard guarantees that any object may be accessed via an |
9fcc3e54 | 4174 | lvalue that has character type. */ |
4175 | if (t == char_type_node | |
4176 | || t == signed_char_type_node | |
4177 | || t == unsigned_char_type_node) | |
f7c44134 | 4178 | return 0; |
a5b1863e | 4179 | |
1607663f | 4180 | /* The C standard specifically allows aliasing between signed and |
4181 | unsigned variants of the same type. We treat the signed | |
4182 | variant as canonical. */ | |
78a8ed03 | 4183 | if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t)) |
a8868e19 | 4184 | { |
4070745f | 4185 | tree t1 = c_common_signed_type (t); |
1607663f | 4186 | |
a8868e19 | 4187 | /* t1 == t can happen for boolean nodes which are always unsigned. */ |
4188 | if (t1 != t) | |
4189 | return get_alias_set (t1); | |
4190 | } | |
1e2513d9 | 4191 | |
4ee9c684 | 4192 | /* Handle the case of multiple type nodes referring to "the same" type, |
4193 | which occurs with IMA. These share an alias set. FIXME: Currently only | |
4194 | C90 is handled. (In C99 type compatibility is not transitive, which | |
4195 | complicates things mightily. The alias set splay trees can theoretically | |
4196 | represent this, but insertion is tricky when you consider all the | |
4197 | different orders things might arrive in.) */ | |
4198 | ||
4199 | if (c_language != clk_c || flag_isoc99) | |
4200 | return -1; | |
4201 | ||
0bed3869 | 4202 | /* Save time if there's only one input file. */ |
e08bd2f4 | 4203 | if (num_in_fnames == 1) |
4ee9c684 | 4204 | return -1; |
4205 | ||
4206 | /* Pointers need special handling if they point to any type that | |
4207 | needs special handling (below). */ | |
4208 | if (TREE_CODE (t) == POINTER_TYPE) | |
4209 | { | |
4210 | tree t2; | |
4211 | /* Find bottom type under any nested POINTERs. */ | |
b27ac6b5 | 4212 | for (t2 = TREE_TYPE (t); |
af592f67 | 4213 | TREE_CODE (t2) == POINTER_TYPE; |
4214 | t2 = TREE_TYPE (t2)) | |
4215 | ; | |
b27ac6b5 | 4216 | if (TREE_CODE (t2) != RECORD_TYPE |
af592f67 | 4217 | && TREE_CODE (t2) != ENUMERAL_TYPE |
4218 | && TREE_CODE (t2) != QUAL_UNION_TYPE | |
4219 | && TREE_CODE (t2) != UNION_TYPE) | |
4220 | return -1; | |
4ee9c684 | 4221 | if (TYPE_SIZE (t2) == 0) |
af592f67 | 4222 | return -1; |
4ee9c684 | 4223 | } |
4224 | /* These are the only cases that need special handling. */ | |
b27ac6b5 | 4225 | if (TREE_CODE (t) != RECORD_TYPE |
4ee9c684 | 4226 | && TREE_CODE (t) != ENUMERAL_TYPE |
4227 | && TREE_CODE (t) != QUAL_UNION_TYPE | |
4228 | && TREE_CODE (t) != UNION_TYPE | |
4229 | && TREE_CODE (t) != POINTER_TYPE) | |
4230 | return -1; | |
4231 | /* Undefined? */ | |
4232 | if (TYPE_SIZE (t) == 0) | |
4233 | return -1; | |
4234 | ||
b27ac6b5 | 4235 | /* Look up t in hash table. Only one of the compatible types within each |
4ee9c684 | 4236 | alias set is recorded in the table. */ |
4237 | if (!type_hash_table) | |
1ecd4018 | 4238 | type_hash_table = htab_create_ggc (1021, c_type_hash, |
4ee9c684 | 4239 | (htab_eq) lang_hooks.types_compatible_p, |
4240 | NULL); | |
4241 | slot = htab_find_slot (type_hash_table, t, INSERT); | |
4242 | if (*slot != NULL) | |
ad16cb2c | 4243 | { |
4244 | TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot); | |
4245 | return TYPE_ALIAS_SET ((tree)*slot); | |
4246 | } | |
4ee9c684 | 4247 | else |
4248 | /* Our caller will assign and record (in t) a new alias set; all we need | |
4249 | to do is remember t in the hash table. */ | |
4250 | *slot = t; | |
4251 | ||
f7c44134 | 4252 | return -1; |
b5ba9f3a | 4253 | } |
902b4e01 | 4254 | \f |
e60a6f7b | 4255 | /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where |
4256 | the second parameter indicates which OPERATOR is being applied. | |
4257 | The COMPLAIN flag controls whether we should diagnose possibly | |
4258 | ill-formed constructs or not. LOC is the location of the SIZEOF or | |
4259 | TYPEOF operator. */ | |
908c697e | 4260 | |
902b4e01 | 4261 | tree |
e60a6f7b | 4262 | c_sizeof_or_alignof_type (location_t loc, |
4263 | tree type, bool is_sizeof, int complain) | |
902b4e01 | 4264 | { |
d4c4d95c | 4265 | const char *op_name; |
4266 | tree value = NULL; | |
4267 | enum tree_code type_code = TREE_CODE (type); | |
1cae46be | 4268 | |
908c697e | 4269 | op_name = is_sizeof ? "sizeof" : "__alignof__"; |
1cae46be | 4270 | |
d4c4d95c | 4271 | if (type_code == FUNCTION_TYPE) |
902b4e01 | 4272 | { |
908c697e | 4273 | if (is_sizeof) |
d4c4d95c | 4274 | { |
d3a4d008 | 4275 | if (complain && (pedantic || warn_pointer_arith)) |
48e1416a | 4276 | pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
8864917d | 4277 | "invalid application of %<sizeof%> to a function type"); |
ebd21de4 | 4278 | else if (!complain) |
4279 | return error_mark_node; | |
d4c4d95c | 4280 | value = size_one_node; |
4281 | } | |
4282 | else | |
4283 | value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT); | |
4284 | } | |
4285 | else if (type_code == VOID_TYPE || type_code == ERROR_MARK) | |
4286 | { | |
1cae46be | 4287 | if (type_code == VOID_TYPE |
d3a4d008 | 4288 | && complain && (pedantic || warn_pointer_arith)) |
48e1416a | 4289 | pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith, |
8864917d | 4290 | "invalid application of %qs to a void type", op_name); |
ebd21de4 | 4291 | else if (!complain) |
4292 | return error_mark_node; | |
d4c4d95c | 4293 | value = size_one_node; |
902b4e01 | 4294 | } |
902b4e01 | 4295 | else if (!COMPLETE_TYPE_P (type)) |
4296 | { | |
d3a4d008 | 4297 | if (complain) |
e60a6f7b | 4298 | error_at (loc, "invalid application of %qs to incomplete type %qT ", |
4299 | op_name, type); | |
9c719c74 | 4300 | return error_mark_node; |
902b4e01 | 4301 | } |
4302 | else | |
d4c4d95c | 4303 | { |
908c697e | 4304 | if (is_sizeof) |
d4c4d95c | 4305 | /* Convert in case a char is more than one unit. */ |
389dd41b | 4306 | value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), |
4307 | size_int (TYPE_PRECISION (char_type_node) | |
4308 | / BITS_PER_UNIT)); | |
d4c4d95c | 4309 | else |
d37625c0 | 4310 | value = size_int (TYPE_ALIGN_UNIT (type)); |
d4c4d95c | 4311 | } |
902b4e01 | 4312 | |
d4c4d95c | 4313 | /* VALUE will have an integer type with TYPE_IS_SIZETYPE set. |
4314 | TYPE_IS_SIZETYPE means that certain things (like overflow) will | |
4315 | never happen. However, this node should really have type | |
4316 | `size_t', which is just a typedef for an ordinary integer type. */ | |
389dd41b | 4317 | value = fold_convert_loc (loc, size_type_node, value); |
1d8fc210 | 4318 | gcc_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value))); |
1cae46be | 4319 | |
d4c4d95c | 4320 | return value; |
902b4e01 | 4321 | } |
4322 | ||
4323 | /* Implement the __alignof keyword: Return the minimum required | |
097b5c8b | 4324 | alignment of EXPR, measured in bytes. For VAR_DECLs, |
4325 | FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set | |
e60a6f7b | 4326 | from an "aligned" __attribute__ specification). LOC is the |
4327 | location of the ALIGNOF operator. */ | |
72040e7e | 4328 | |
902b4e01 | 4329 | tree |
e60a6f7b | 4330 | c_alignof_expr (location_t loc, tree expr) |
902b4e01 | 4331 | { |
4332 | tree t; | |
4333 | ||
097b5c8b | 4334 | if (VAR_OR_FUNCTION_DECL_P (expr)) |
d37625c0 | 4335 | t = size_int (DECL_ALIGN_UNIT (expr)); |
1cae46be | 4336 | |
902b4e01 | 4337 | else if (TREE_CODE (expr) == COMPONENT_REF |
4338 | && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1))) | |
4339 | { | |
e60a6f7b | 4340 | error_at (loc, "%<__alignof%> applied to a bit-field"); |
902b4e01 | 4341 | t = size_one_node; |
4342 | } | |
4343 | else if (TREE_CODE (expr) == COMPONENT_REF | |
7cc7e163 | 4344 | && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL) |
d37625c0 | 4345 | t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1))); |
1cae46be | 4346 | |
902b4e01 | 4347 | else if (TREE_CODE (expr) == INDIRECT_REF) |
4348 | { | |
4349 | tree t = TREE_OPERAND (expr, 0); | |
4350 | tree best = t; | |
4351 | int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
1cae46be | 4352 | |
72dd6141 | 4353 | while (CONVERT_EXPR_P (t) |
7cc7e163 | 4354 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE) |
902b4e01 | 4355 | { |
4356 | int thisalign; | |
4357 | ||
4358 | t = TREE_OPERAND (t, 0); | |
4359 | thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t))); | |
4360 | if (thisalign > bestalign) | |
4361 | best = t, bestalign = thisalign; | |
4362 | } | |
e60a6f7b | 4363 | return c_alignof (loc, TREE_TYPE (TREE_TYPE (best))); |
902b4e01 | 4364 | } |
4365 | else | |
e60a6f7b | 4366 | return c_alignof (loc, TREE_TYPE (expr)); |
902b4e01 | 4367 | |
389dd41b | 4368 | return fold_convert_loc (loc, size_type_node, t); |
902b4e01 | 4369 | } |
4370 | \f | |
8fe4a266 | 4371 | /* Handle C and C++ default attributes. */ |
4372 | ||
4373 | enum built_in_attribute | |
4374 | { | |
4375 | #define DEF_ATTR_NULL_TREE(ENUM) ENUM, | |
4376 | #define DEF_ATTR_INT(ENUM, VALUE) ENUM, | |
4377 | #define DEF_ATTR_IDENT(ENUM, STRING) ENUM, | |
4378 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM, | |
8fe4a266 | 4379 | #include "builtin-attrs.def" |
4380 | #undef DEF_ATTR_NULL_TREE | |
4381 | #undef DEF_ATTR_INT | |
4382 | #undef DEF_ATTR_IDENT | |
4383 | #undef DEF_ATTR_TREE_LIST | |
8fe4a266 | 4384 | ATTR_LAST |
4385 | }; | |
4386 | ||
4387 | static GTY(()) tree built_in_attributes[(int) ATTR_LAST]; | |
4388 | ||
1cae46be | 4389 | static void c_init_attributes (void); |
8fe4a266 | 4390 | |
27213ba3 | 4391 | enum c_builtin_type |
72040e7e | 4392 | { |
d2d4bdde | 4393 | #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME, |
4394 | #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME, | |
4395 | #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME, | |
4396 | #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME, | |
4397 | #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, | |
4398 | #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, | |
0a39fd54 | 4399 | #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME, |
27213ba3 | 4400 | #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME, |
4401 | #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME, | |
d2d4bdde | 4402 | #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME, |
4403 | #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME, | |
4404 | #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME, | |
03901330 | 4405 | #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME, |
0a39fd54 | 4406 | #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME, |
4407 | #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \ | |
4408 | NAME, | |
d2d4bdde | 4409 | #define DEF_POINTER_TYPE(NAME, TYPE) NAME, |
4410 | #include "builtin-types.def" | |
4411 | #undef DEF_PRIMITIVE_TYPE | |
4412 | #undef DEF_FUNCTION_TYPE_0 | |
4413 | #undef DEF_FUNCTION_TYPE_1 | |
4414 | #undef DEF_FUNCTION_TYPE_2 | |
4415 | #undef DEF_FUNCTION_TYPE_3 | |
4416 | #undef DEF_FUNCTION_TYPE_4 | |
0a39fd54 | 4417 | #undef DEF_FUNCTION_TYPE_5 |
4418 | #undef DEF_FUNCTION_TYPE_6 | |
27213ba3 | 4419 | #undef DEF_FUNCTION_TYPE_7 |
d2d4bdde | 4420 | #undef DEF_FUNCTION_TYPE_VAR_0 |
4421 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
4422 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
03901330 | 4423 | #undef DEF_FUNCTION_TYPE_VAR_3 |
0a39fd54 | 4424 | #undef DEF_FUNCTION_TYPE_VAR_4 |
4425 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
d2d4bdde | 4426 | #undef DEF_POINTER_TYPE |
27213ba3 | 4427 | BT_LAST |
4428 | }; | |
4429 | ||
4430 | typedef enum c_builtin_type builtin_type; | |
d2d4bdde | 4431 | |
27213ba3 | 4432 | /* A temporary array for c_common_nodes_and_builtins. Used in |
4433 | communication with def_fn_type. */ | |
4434 | static tree builtin_types[(int) BT_LAST + 1]; | |
d2d4bdde | 4435 | |
27213ba3 | 4436 | /* A helper function for c_common_nodes_and_builtins. Build function type |
4437 | for DEF with return type RET and N arguments. If VAR is true, then the | |
4438 | function should be variadic after those N arguments. | |
4439 | ||
4440 | Takes special care not to ICE if any of the types involved are | |
4441 | error_mark_node, which indicates that said type is not in fact available | |
4442 | (see builtin_type_for_size). In which case the function type as a whole | |
4443 | should be error_mark_node. */ | |
4444 | ||
4445 | static void | |
4446 | def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...) | |
4447 | { | |
3a939d12 | 4448 | tree t; |
4449 | tree *args = XALLOCAVEC (tree, n); | |
27213ba3 | 4450 | va_list list; |
4451 | int i; | |
4452 | ||
4453 | va_start (list, n); | |
4454 | for (i = 0; i < n; ++i) | |
4455 | { | |
7d339f93 | 4456 | builtin_type a = (builtin_type) va_arg (list, int); |
27213ba3 | 4457 | t = builtin_types[a]; |
4458 | if (t == error_mark_node) | |
4459 | goto egress; | |
3a939d12 | 4460 | args[i] = t; |
27213ba3 | 4461 | } |
27213ba3 | 4462 | |
27213ba3 | 4463 | t = builtin_types[ret]; |
4464 | if (t == error_mark_node) | |
4465 | goto egress; | |
3a939d12 | 4466 | if (var) |
4467 | t = build_varargs_function_type_array (t, n, args); | |
4468 | else | |
4469 | t = build_function_type_array (t, n, args); | |
27213ba3 | 4470 | |
4471 | egress: | |
4472 | builtin_types[def] = t; | |
451c8e2f | 4473 | va_end (list); |
27213ba3 | 4474 | } |
4475 | ||
dce22712 | 4476 | /* Build builtin functions common to both C and C++ language |
4477 | frontends. */ | |
4478 | ||
4479 | static void | |
4480 | c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node) | |
4481 | { | |
4482 | #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ | |
4483 | builtin_types[ENUM] = VALUE; | |
4484 | #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ | |
4485 | def_fn_type (ENUM, RETURN, 0, 0); | |
4486 | #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ | |
4487 | def_fn_type (ENUM, RETURN, 0, 1, ARG1); | |
4488 | #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ | |
4489 | def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2); | |
4490 | #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
4491 | def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3); | |
4492 | #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
4493 | def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4); | |
4494 | #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
4495 | def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
4496 | #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
4497 | ARG6) \ | |
4498 | def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); | |
4499 | #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ | |
4500 | ARG6, ARG7) \ | |
4501 | def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); | |
4502 | #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ | |
4503 | def_fn_type (ENUM, RETURN, 1, 0); | |
4504 | #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ | |
4505 | def_fn_type (ENUM, RETURN, 1, 1, ARG1); | |
4506 | #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ | |
4507 | def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2); | |
4508 | #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ | |
4509 | def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3); | |
4510 | #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ | |
4511 | def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4); | |
4512 | #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ | |
4513 | def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5); | |
4514 | #define DEF_POINTER_TYPE(ENUM, TYPE) \ | |
4515 | builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]); | |
4516 | ||
4517 | #include "builtin-types.def" | |
4518 | ||
4519 | #undef DEF_PRIMITIVE_TYPE | |
4520 | #undef DEF_FUNCTION_TYPE_1 | |
4521 | #undef DEF_FUNCTION_TYPE_2 | |
4522 | #undef DEF_FUNCTION_TYPE_3 | |
4523 | #undef DEF_FUNCTION_TYPE_4 | |
4524 | #undef DEF_FUNCTION_TYPE_5 | |
4525 | #undef DEF_FUNCTION_TYPE_6 | |
4526 | #undef DEF_FUNCTION_TYPE_VAR_0 | |
4527 | #undef DEF_FUNCTION_TYPE_VAR_1 | |
4528 | #undef DEF_FUNCTION_TYPE_VAR_2 | |
4529 | #undef DEF_FUNCTION_TYPE_VAR_3 | |
4530 | #undef DEF_FUNCTION_TYPE_VAR_4 | |
4531 | #undef DEF_FUNCTION_TYPE_VAR_5 | |
4532 | #undef DEF_POINTER_TYPE | |
4533 | builtin_types[(int) BT_LAST] = NULL_TREE; | |
4534 | ||
4535 | c_init_attributes (); | |
4536 | ||
4537 | #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ | |
4538 | NONANSI_P, ATTRS, IMPLICIT, COND) \ | |
4539 | if (NAME && COND) \ | |
4540 | def_builtin_1 (ENUM, NAME, CLASS, \ | |
4541 | builtin_types[(int) TYPE], \ | |
4542 | builtin_types[(int) LIBTYPE], \ | |
4543 | BOTH_P, FALLBACK_P, NONANSI_P, \ | |
4544 | built_in_attributes[(int) ATTRS], IMPLICIT); | |
4545 | #include "builtins.def" | |
4546 | #undef DEF_BUILTIN | |
4547 | ||
87eb1c28 | 4548 | targetm.init_builtins (); |
4549 | ||
471eff36 | 4550 | build_common_builtin_nodes (); |
dce22712 | 4551 | |
dce22712 | 4552 | if (flag_mudflap) |
4553 | mudflap_init (); | |
4554 | } | |
4555 | ||
9e6687c8 | 4556 | /* Like get_identifier, but avoid warnings about null arguments when |
4557 | the argument may be NULL for targets where GCC lacks stdint.h type | |
4558 | information. */ | |
4559 | ||
4560 | static inline tree | |
4561 | c_get_ident (const char *id) | |
4562 | { | |
4563 | return get_identifier (id); | |
4564 | } | |
4565 | ||
27213ba3 | 4566 | /* Build tree nodes and builtin functions common to both C and C++ language |
4567 | frontends. */ | |
4568 | ||
4569 | void | |
4570 | c_common_nodes_and_builtins (void) | |
4571 | { | |
924bbf02 | 4572 | int char16_type_size; |
4573 | int char32_type_size; | |
174fcc61 | 4574 | int wchar_type_size; |
4575 | tree array_domain_type; | |
2d47cc32 | 4576 | tree va_list_ref_type_node; |
8a15c04a | 4577 | tree va_list_arg_type_node; |
a66c9326 | 4578 | |
174fcc61 | 4579 | /* Define `int' and `char' first so that dbx will output them first. */ |
d946ea19 | 4580 | record_builtin_type (RID_INT, NULL, integer_type_node); |
174fcc61 | 4581 | record_builtin_type (RID_CHAR, "char", char_type_node); |
4582 | ||
4583 | /* `signed' is the same as `int'. FIXME: the declarations of "signed", | |
4584 | "unsigned long", "long long unsigned" and "unsigned short" were in C++ | |
4585 | but not C. Are the conditionals here needed? */ | |
c0f19401 | 4586 | if (c_dialect_cxx ()) |
d946ea19 | 4587 | record_builtin_type (RID_SIGNED, NULL, integer_type_node); |
174fcc61 | 4588 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); |
4589 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); | |
4590 | record_builtin_type (RID_MAX, "long unsigned int", | |
4591 | long_unsigned_type_node); | |
6388cfe2 | 4592 | if (int128_integer_type_node != NULL_TREE) |
4593 | { | |
4594 | record_builtin_type (RID_INT128, "__int128", | |
4595 | int128_integer_type_node); | |
4596 | record_builtin_type (RID_MAX, "__int128 unsigned", | |
4597 | int128_unsigned_type_node); | |
4598 | } | |
c0f19401 | 4599 | if (c_dialect_cxx ()) |
174fcc61 | 4600 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); |
4601 | record_builtin_type (RID_MAX, "long long int", | |
4602 | long_long_integer_type_node); | |
4603 | record_builtin_type (RID_MAX, "long long unsigned int", | |
4604 | long_long_unsigned_type_node); | |
c0f19401 | 4605 | if (c_dialect_cxx ()) |
174fcc61 | 4606 | record_builtin_type (RID_MAX, "long long unsigned", |
4607 | long_long_unsigned_type_node); | |
4608 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); | |
4609 | record_builtin_type (RID_MAX, "short unsigned int", | |
4610 | short_unsigned_type_node); | |
c0f19401 | 4611 | if (c_dialect_cxx ()) |
174fcc61 | 4612 | record_builtin_type (RID_MAX, "unsigned short", |
4613 | short_unsigned_type_node); | |
4614 | ||
4615 | /* Define both `signed char' and `unsigned char'. */ | |
4616 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); | |
4617 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); | |
4618 | ||
771d21fa | 4619 | /* These are types that c_common_type_for_size and |
4620 | c_common_type_for_mode use. */ | |
e60a6f7b | 4621 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4622 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 4623 | intQI_type_node)); |
e60a6f7b | 4624 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4625 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 4626 | intHI_type_node)); |
e60a6f7b | 4627 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4628 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 4629 | intSI_type_node)); |
e60a6f7b | 4630 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4631 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 4632 | intDI_type_node)); |
174fcc61 | 4633 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 4634 | if (targetm.scalar_mode_supported_p (TImode)) |
e60a6f7b | 4635 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4636 | TYPE_DECL, | |
f1515a39 | 4637 | get_identifier ("__int128_t"), |
4638 | intTI_type_node)); | |
174fcc61 | 4639 | #endif |
e60a6f7b | 4640 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4641 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 4642 | unsigned_intQI_type_node)); |
e60a6f7b | 4643 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4644 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 4645 | unsigned_intHI_type_node)); |
e60a6f7b | 4646 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4647 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 4648 | unsigned_intSI_type_node)); |
e60a6f7b | 4649 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4650 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 4651 | unsigned_intDI_type_node)); |
174fcc61 | 4652 | #if HOST_BITS_PER_WIDE_INT >= 64 |
f1515a39 | 4653 | if (targetm.scalar_mode_supported_p (TImode)) |
e60a6f7b | 4654 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4655 | TYPE_DECL, | |
f1515a39 | 4656 | get_identifier ("__uint128_t"), |
4657 | unsigned_intTI_type_node)); | |
174fcc61 | 4658 | #endif |
4659 | ||
4660 | /* Create the widest literal types. */ | |
4661 | widest_integer_literal_type_node | |
4662 | = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
e60a6f7b | 4663 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4664 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 4665 | widest_integer_literal_type_node)); |
174fcc61 | 4666 | |
4667 | widest_unsigned_literal_type_node | |
4668 | = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
e60a6f7b | 4669 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4670 | TYPE_DECL, NULL_TREE, | |
dc24ddbd | 4671 | widest_unsigned_literal_type_node)); |
174fcc61 | 4672 | |
654ef926 | 4673 | signed_size_type_node = c_common_signed_type (size_type_node); |
174fcc61 | 4674 | |
73673831 | 4675 | pid_type_node = |
4676 | TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE))); | |
4677 | ||
174fcc61 | 4678 | build_common_tree_nodes_2 (flag_short_double); |
4679 | ||
d946ea19 | 4680 | record_builtin_type (RID_FLOAT, NULL, float_type_node); |
4681 | record_builtin_type (RID_DOUBLE, NULL, double_type_node); | |
174fcc61 | 4682 | record_builtin_type (RID_MAX, "long double", long_double_type_node); |
4683 | ||
c4503c0a | 4684 | /* Only supported decimal floating point extension if the target |
4685 | actually supports underlying modes. */ | |
48e1416a | 4686 | if (targetm.scalar_mode_supported_p (SDmode) |
c4503c0a | 4687 | && targetm.scalar_mode_supported_p (DDmode) |
4688 | && targetm.scalar_mode_supported_p (TDmode)) | |
4689 | { | |
4690 | record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node); | |
4691 | record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node); | |
4692 | record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node); | |
4693 | } | |
4694 | ||
9421ebb9 | 4695 | if (targetm.fixed_point_supported_p ()) |
4696 | { | |
4697 | record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node); | |
4698 | record_builtin_type (RID_FRACT, NULL, fract_type_node); | |
4699 | record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node); | |
4700 | record_builtin_type (RID_MAX, "long long _Fract", | |
4701 | long_long_fract_type_node); | |
4702 | record_builtin_type (RID_MAX, "unsigned short _Fract", | |
4703 | unsigned_short_fract_type_node); | |
4704 | record_builtin_type (RID_MAX, "unsigned _Fract", | |
4705 | unsigned_fract_type_node); | |
4706 | record_builtin_type (RID_MAX, "unsigned long _Fract", | |
4707 | unsigned_long_fract_type_node); | |
4708 | record_builtin_type (RID_MAX, "unsigned long long _Fract", | |
4709 | unsigned_long_long_fract_type_node); | |
4710 | record_builtin_type (RID_MAX, "_Sat short _Fract", | |
4711 | sat_short_fract_type_node); | |
4712 | record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node); | |
4713 | record_builtin_type (RID_MAX, "_Sat long _Fract", | |
4714 | sat_long_fract_type_node); | |
4715 | record_builtin_type (RID_MAX, "_Sat long long _Fract", | |
4716 | sat_long_long_fract_type_node); | |
4717 | record_builtin_type (RID_MAX, "_Sat unsigned short _Fract", | |
4718 | sat_unsigned_short_fract_type_node); | |
4719 | record_builtin_type (RID_MAX, "_Sat unsigned _Fract", | |
4720 | sat_unsigned_fract_type_node); | |
4721 | record_builtin_type (RID_MAX, "_Sat unsigned long _Fract", | |
4722 | sat_unsigned_long_fract_type_node); | |
4723 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract", | |
4724 | sat_unsigned_long_long_fract_type_node); | |
4725 | record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node); | |
4726 | record_builtin_type (RID_ACCUM, NULL, accum_type_node); | |
4727 | record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node); | |
4728 | record_builtin_type (RID_MAX, "long long _Accum", | |
4729 | long_long_accum_type_node); | |
4730 | record_builtin_type (RID_MAX, "unsigned short _Accum", | |
4731 | unsigned_short_accum_type_node); | |
4732 | record_builtin_type (RID_MAX, "unsigned _Accum", | |
4733 | unsigned_accum_type_node); | |
4734 | record_builtin_type (RID_MAX, "unsigned long _Accum", | |
4735 | unsigned_long_accum_type_node); | |
4736 | record_builtin_type (RID_MAX, "unsigned long long _Accum", | |
4737 | unsigned_long_long_accum_type_node); | |
4738 | record_builtin_type (RID_MAX, "_Sat short _Accum", | |
4739 | sat_short_accum_type_node); | |
4740 | record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node); | |
4741 | record_builtin_type (RID_MAX, "_Sat long _Accum", | |
4742 | sat_long_accum_type_node); | |
4743 | record_builtin_type (RID_MAX, "_Sat long long _Accum", | |
4744 | sat_long_long_accum_type_node); | |
4745 | record_builtin_type (RID_MAX, "_Sat unsigned short _Accum", | |
4746 | sat_unsigned_short_accum_type_node); | |
4747 | record_builtin_type (RID_MAX, "_Sat unsigned _Accum", | |
4748 | sat_unsigned_accum_type_node); | |
4749 | record_builtin_type (RID_MAX, "_Sat unsigned long _Accum", | |
4750 | sat_unsigned_long_accum_type_node); | |
4751 | record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum", | |
4752 | sat_unsigned_long_long_accum_type_node); | |
4753 | ||
4754 | } | |
4755 | ||
e60a6f7b | 4756 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4757 | TYPE_DECL, | |
dc24ddbd | 4758 | get_identifier ("complex int"), |
4759 | complex_integer_type_node)); | |
e60a6f7b | 4760 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4761 | TYPE_DECL, | |
dc24ddbd | 4762 | get_identifier ("complex float"), |
4763 | complex_float_type_node)); | |
e60a6f7b | 4764 | lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION, |
4765 | TYPE_DECL, | |
dc24ddbd | 4766 | get_identifier ("complex double"), |
4767 | complex_double_type_node)); | |
4768 | lang_hooks.decls.pushdecl | |
e60a6f7b | 4769 | (build_decl (UNKNOWN_LOCATION, |
4770 | TYPE_DECL, get_identifier ("complex long double"), | |
20325f61 | 4771 | complex_long_double_type_node)); |
174fcc61 | 4772 | |
e256d445 | 4773 | if (c_dialect_cxx ()) |
4774 | /* For C++, make fileptr_type_node a distinct void * type until | |
4775 | FILE type is defined. */ | |
e086912e | 4776 | fileptr_type_node = build_variant_type_copy (ptr_type_node); |
e256d445 | 4777 | |
d946ea19 | 4778 | record_builtin_type (RID_VOID, NULL, void_type_node); |
174fcc61 | 4779 | |
6753bca0 | 4780 | /* Set the TYPE_NAME for any variants that were built before |
4781 | record_builtin_type gave names to the built-in types. */ | |
4782 | { | |
4783 | tree void_name = TYPE_NAME (void_type_node); | |
4784 | TYPE_NAME (void_type_node) = NULL_TREE; | |
4785 | TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST)) | |
4786 | = void_name; | |
4787 | TYPE_NAME (void_type_node) = void_name; | |
4788 | } | |
4789 | ||
fbf0afd1 | 4790 | /* This node must not be shared. */ |
7c446c95 | 4791 | void_zero_node = make_node (INTEGER_CST); |
d2d4bdde | 4792 | TREE_TYPE (void_zero_node) = void_type_node; |
4793 | ||
174fcc61 | 4794 | void_list_node = build_void_list_node (); |
4795 | ||
4796 | /* Make a type to be the domain of a few array types | |
4797 | whose domains don't really matter. | |
4798 | 200 is small enough that it always fits in size_t | |
4799 | and large enough that it can hold most function names for the | |
4800 | initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
4801 | array_domain_type = build_index_type (size_int (200)); | |
4802 | ||
4803 | /* Make a type for arrays of characters. | |
4804 | With luck nothing will ever really depend on the length of this | |
4805 | array type. */ | |
4806 | char_array_type_node | |
4807 | = build_array_type (char_type_node, array_domain_type); | |
4808 | ||
4809 | /* Likewise for arrays of ints. */ | |
4810 | int_array_type_node | |
4811 | = build_array_type (integer_type_node, array_domain_type); | |
4812 | ||
d2d4bdde | 4813 | string_type_node = build_pointer_type (char_type_node); |
4814 | const_string_type_node | |
4815 | = build_pointer_type (build_qualified_type | |
4816 | (char_type_node, TYPE_QUAL_CONST)); | |
4817 | ||
174fcc61 | 4818 | /* This is special for C++ so functions can be overloaded. */ |
18ef7ac2 | 4819 | wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE); |
174fcc61 | 4820 | wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node)); |
4821 | wchar_type_size = TYPE_PRECISION (wchar_type_node); | |
f3449a3c | 4822 | underlying_wchar_type_node = wchar_type_node; |
c0f19401 | 4823 | if (c_dialect_cxx ()) |
174fcc61 | 4824 | { |
78a8ed03 | 4825 | if (TYPE_UNSIGNED (wchar_type_node)) |
174fcc61 | 4826 | wchar_type_node = make_unsigned_type (wchar_type_size); |
4827 | else | |
4828 | wchar_type_node = make_signed_type (wchar_type_size); | |
4829 | record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node); | |
4830 | } | |
174fcc61 | 4831 | |
4832 | /* This is for wide string constants. */ | |
4833 | wchar_array_type_node | |
4834 | = build_array_type (wchar_type_node, array_domain_type); | |
4835 | ||
924bbf02 | 4836 | /* Define 'char16_t'. */ |
4837 | char16_type_node = get_identifier (CHAR16_TYPE); | |
4838 | char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node)); | |
4839 | char16_type_size = TYPE_PRECISION (char16_type_node); | |
4840 | if (c_dialect_cxx ()) | |
4841 | { | |
4842 | char16_type_node = make_unsigned_type (char16_type_size); | |
4843 | ||
4844 | if (cxx_dialect == cxx0x) | |
4845 | record_builtin_type (RID_CHAR16, "char16_t", char16_type_node); | |
4846 | } | |
4847 | ||
4848 | /* This is for UTF-16 string constants. */ | |
4849 | char16_array_type_node | |
4850 | = build_array_type (char16_type_node, array_domain_type); | |
4851 | ||
4852 | /* Define 'char32_t'. */ | |
4853 | char32_type_node = get_identifier (CHAR32_TYPE); | |
4854 | char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node)); | |
4855 | char32_type_size = TYPE_PRECISION (char32_type_node); | |
4856 | if (c_dialect_cxx ()) | |
4857 | { | |
4858 | char32_type_node = make_unsigned_type (char32_type_size); | |
4859 | ||
4860 | if (cxx_dialect == cxx0x) | |
4861 | record_builtin_type (RID_CHAR32, "char32_t", char32_type_node); | |
4862 | } | |
4863 | ||
4864 | /* This is for UTF-32 string constants. */ | |
4865 | char32_array_type_node | |
4866 | = build_array_type (char32_type_node, array_domain_type); | |
4867 | ||
6bf5ed8d | 4868 | wint_type_node = |
4869 | TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE))); | |
4870 | ||
4871 | intmax_type_node = | |
4872 | TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE))); | |
4873 | uintmax_type_node = | |
4874 | TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE))); | |
4875 | ||
f3449a3c | 4876 | if (SIG_ATOMIC_TYPE) |
4877 | sig_atomic_type_node = | |
9e6687c8 | 4878 | TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE))); |
f3449a3c | 4879 | if (INT8_TYPE) |
4880 | int8_type_node = | |
9e6687c8 | 4881 | TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE))); |
f3449a3c | 4882 | if (INT16_TYPE) |
4883 | int16_type_node = | |
9e6687c8 | 4884 | TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE))); |
f3449a3c | 4885 | if (INT32_TYPE) |
4886 | int32_type_node = | |
9e6687c8 | 4887 | TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE))); |
f3449a3c | 4888 | if (INT64_TYPE) |
4889 | int64_type_node = | |
9e6687c8 | 4890 | TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE))); |
f3449a3c | 4891 | if (UINT8_TYPE) |
4892 | uint8_type_node = | |
9e6687c8 | 4893 | TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE))); |
f3449a3c | 4894 | if (UINT16_TYPE) |
4895 | uint16_type_node = | |
9e6687c8 | 4896 | TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE))); |
f3449a3c | 4897 | if (UINT32_TYPE) |
4898 | c_uint32_type_node = | |
9e6687c8 | 4899 | TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE))); |
f3449a3c | 4900 | if (UINT64_TYPE) |
4901 | c_uint64_type_node = | |
9e6687c8 | 4902 | TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE))); |
f3449a3c | 4903 | if (INT_LEAST8_TYPE) |
4904 | int_least8_type_node = | |
9e6687c8 | 4905 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE))); |
f3449a3c | 4906 | if (INT_LEAST16_TYPE) |
4907 | int_least16_type_node = | |
9e6687c8 | 4908 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE))); |
f3449a3c | 4909 | if (INT_LEAST32_TYPE) |
4910 | int_least32_type_node = | |
9e6687c8 | 4911 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE))); |
f3449a3c | 4912 | if (INT_LEAST64_TYPE) |
4913 | int_least64_type_node = | |
9e6687c8 | 4914 | TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE))); |
f3449a3c | 4915 | if (UINT_LEAST8_TYPE) |
4916 | uint_least8_type_node = | |
9e6687c8 | 4917 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE))); |
f3449a3c | 4918 | if (UINT_LEAST16_TYPE) |
4919 | uint_least16_type_node = | |
9e6687c8 | 4920 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE))); |
f3449a3c | 4921 | if (UINT_LEAST32_TYPE) |
4922 | uint_least32_type_node = | |
9e6687c8 | 4923 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE))); |
f3449a3c | 4924 | if (UINT_LEAST64_TYPE) |
4925 | uint_least64_type_node = | |
9e6687c8 | 4926 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE))); |
f3449a3c | 4927 | if (INT_FAST8_TYPE) |
4928 | int_fast8_type_node = | |
9e6687c8 | 4929 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE))); |
f3449a3c | 4930 | if (INT_FAST16_TYPE) |
4931 | int_fast16_type_node = | |
9e6687c8 | 4932 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE))); |
f3449a3c | 4933 | if (INT_FAST32_TYPE) |
4934 | int_fast32_type_node = | |
9e6687c8 | 4935 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE))); |
f3449a3c | 4936 | if (INT_FAST64_TYPE) |
4937 | int_fast64_type_node = | |
9e6687c8 | 4938 | TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE))); |
f3449a3c | 4939 | if (UINT_FAST8_TYPE) |
4940 | uint_fast8_type_node = | |
9e6687c8 | 4941 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE))); |
f3449a3c | 4942 | if (UINT_FAST16_TYPE) |
4943 | uint_fast16_type_node = | |
9e6687c8 | 4944 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE))); |
f3449a3c | 4945 | if (UINT_FAST32_TYPE) |
4946 | uint_fast32_type_node = | |
9e6687c8 | 4947 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE))); |
f3449a3c | 4948 | if (UINT_FAST64_TYPE) |
4949 | uint_fast64_type_node = | |
9e6687c8 | 4950 | TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE))); |
f3449a3c | 4951 | if (INTPTR_TYPE) |
4952 | intptr_type_node = | |
9e6687c8 | 4953 | TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE))); |
f3449a3c | 4954 | if (UINTPTR_TYPE) |
4955 | uintptr_type_node = | |
9e6687c8 | 4956 | TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE))); |
f3449a3c | 4957 | |
3a939d12 | 4958 | default_function_type |
4959 | = build_varargs_function_type_list (integer_type_node, NULL_TREE); | |
6bf5ed8d | 4960 | ptrdiff_type_node |
4961 | = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE))); | |
11773141 | 4962 | unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node); |
6bf5ed8d | 4963 | |
dc24ddbd | 4964 | lang_hooks.decls.pushdecl |
e60a6f7b | 4965 | (build_decl (UNKNOWN_LOCATION, |
4966 | TYPE_DECL, get_identifier ("__builtin_va_list"), | |
20325f61 | 4967 | va_list_type_node)); |
202d6e5f | 4968 | if (targetm.enum_va_list_p) |
acd6f472 | 4969 | { |
4970 | int l; | |
4971 | const char *pname; | |
4972 | tree ptype; | |
5f57a8b1 | 4973 | |
202d6e5f | 4974 | for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l) |
acd6f472 | 4975 | { |
4976 | lang_hooks.decls.pushdecl | |
4977 | (build_decl (UNKNOWN_LOCATION, | |
4978 | TYPE_DECL, get_identifier (pname), | |
4979 | ptype)); | |
4980 | ||
4981 | } | |
4982 | } | |
8a15c04a | 4983 | |
8a15c04a | 4984 | if (TREE_CODE (va_list_type_node) == ARRAY_TYPE) |
2d47cc32 | 4985 | { |
4986 | va_list_arg_type_node = va_list_ref_type_node = | |
4987 | build_pointer_type (TREE_TYPE (va_list_type_node)); | |
4988 | } | |
8a15c04a | 4989 | else |
2d47cc32 | 4990 | { |
4991 | va_list_arg_type_node = va_list_type_node; | |
4992 | va_list_ref_type_node = build_reference_type (va_list_type_node); | |
4993 | } | |
1cae46be | 4994 | |
dce22712 | 4995 | if (!flag_preprocess_only) |
4996 | c_define_builtins (va_list_ref_type_node, va_list_arg_type_node); | |
ffa8918b | 4997 | |
5c62f199 | 4998 | main_identifier_node = get_identifier ("main"); |
ae84079f | 4999 | |
5000 | /* Create the built-in __null node. It is important that this is | |
5001 | not shared. */ | |
5002 | null_node = make_node (INTEGER_CST); | |
5003 | TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0); | |
27213ba3 | 5004 | |
5005 | /* Since builtin_types isn't gc'ed, don't export these nodes. */ | |
5006 | memset (builtin_types, 0, sizeof (builtin_types)); | |
72040e7e | 5007 | } |
a66c9326 | 5008 | |
79b01846 | 5009 | /* The number of named compound-literals generated thus far. */ |
5010 | static GTY(()) int compound_literal_number; | |
5011 | ||
5012 | /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */ | |
5013 | ||
5014 | void | |
5015 | set_compound_literal_name (tree decl) | |
5016 | { | |
5017 | char *name; | |
5018 | ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", | |
5019 | compound_literal_number); | |
5020 | compound_literal_number++; | |
5021 | DECL_NAME (decl) = get_identifier (name); | |
5022 | } | |
5023 | ||
a66c9326 | 5024 | tree |
e60a6f7b | 5025 | build_va_arg (location_t loc, tree expr, tree type) |
a66c9326 | 5026 | { |
e60a6f7b | 5027 | expr = build1 (VA_ARG_EXPR, type, expr); |
5028 | SET_EXPR_LOCATION (expr, loc); | |
5029 | return expr; | |
a66c9326 | 5030 | } |
0d4238dc | 5031 | |
5032 | ||
dd878098 | 5033 | /* Linked list of disabled built-in functions. */ |
5034 | ||
5035 | typedef struct disabled_builtin | |
5036 | { | |
5037 | const char *name; | |
5038 | struct disabled_builtin *next; | |
5039 | } disabled_builtin; | |
5040 | static disabled_builtin *disabled_builtins = NULL; | |
5041 | ||
1cae46be | 5042 | static bool builtin_function_disabled_p (const char *); |
dd878098 | 5043 | |
5044 | /* Disable a built-in function specified by -fno-builtin-NAME. If NAME | |
5045 | begins with "__builtin_", give an error. */ | |
5046 | ||
5047 | void | |
1cae46be | 5048 | disable_builtin_function (const char *name) |
dd878098 | 5049 | { |
5050 | if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0) | |
b0b1af64 | 5051 | error ("cannot disable built-in function %qs", name); |
dd878098 | 5052 | else |
5053 | { | |
e85905e5 | 5054 | disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin); |
5055 | new_disabled_builtin->name = name; | |
5056 | new_disabled_builtin->next = disabled_builtins; | |
5057 | disabled_builtins = new_disabled_builtin; | |
dd878098 | 5058 | } |
5059 | } | |
5060 | ||
5061 | ||
5062 | /* Return true if the built-in function NAME has been disabled, false | |
5063 | otherwise. */ | |
5064 | ||
5065 | static bool | |
1cae46be | 5066 | builtin_function_disabled_p (const char *name) |
dd878098 | 5067 | { |
5068 | disabled_builtin *p; | |
5069 | for (p = disabled_builtins; p != NULL; p = p->next) | |
5070 | { | |
5071 | if (strcmp (name, p->name) == 0) | |
5072 | return true; | |
5073 | } | |
5074 | return false; | |
5075 | } | |
5076 | ||
5077 | ||
3237155d | 5078 | /* Worker for DEF_BUILTIN. |
5079 | Possibly define a builtin function with one or two names. | |
5080 | Does not declare a non-__builtin_ function if flag_no_builtin, or if | |
5081 | nonansi_p and flag_no_nonansi_builtin. */ | |
0d4238dc | 5082 | |
3237155d | 5083 | static void |
5084 | def_builtin_1 (enum built_in_function fncode, | |
5085 | const char *name, | |
5086 | enum built_in_class fnclass, | |
5087 | tree fntype, tree libtype, | |
5088 | bool both_p, bool fallback_p, bool nonansi_p, | |
5089 | tree fnattrs, bool implicit_p) | |
0d4238dc | 5090 | { |
3237155d | 5091 | tree decl; |
5092 | const char *libname; | |
5093 | ||
27213ba3 | 5094 | if (fntype == error_mark_node) |
5095 | return; | |
5096 | ||
3237155d | 5097 | gcc_assert ((!both_p && !fallback_p) |
5098 | || !strncmp (name, "__builtin_", | |
5099 | strlen ("__builtin_"))); | |
5100 | ||
5101 | libname = name + strlen ("__builtin_"); | |
54be5d7e | 5102 | decl = add_builtin_function (name, fntype, fncode, fnclass, |
5103 | (fallback_p ? libname : NULL), | |
5104 | fnattrs); | |
3237155d | 5105 | if (both_p |
5106 | && !flag_no_builtin && !builtin_function_disabled_p (libname) | |
dd878098 | 5107 | && !(nonansi_p && flag_no_nonansi_builtin)) |
54be5d7e | 5108 | add_builtin_function (libname, libtype, fncode, fnclass, |
5109 | NULL, fnattrs); | |
a40609e6 | 5110 | |
3237155d | 5111 | built_in_decls[(int) fncode] = decl; |
5112 | if (implicit_p) | |
5113 | implicit_built_in_decls[(int) fncode] = decl; | |
0d4238dc | 5114 | } |
e94026da | 5115 | \f |
d7aeef06 | 5116 | /* Nonzero if the type T promotes to int. This is (nearly) the |
5117 | integral promotions defined in ISO C99 6.3.1.1/2. */ | |
5118 | ||
5119 | bool | |
9f627b1a | 5120 | c_promoting_integer_type_p (const_tree t) |
d7aeef06 | 5121 | { |
5122 | switch (TREE_CODE (t)) | |
5123 | { | |
5124 | case INTEGER_TYPE: | |
5125 | return (TYPE_MAIN_VARIANT (t) == char_type_node | |
5126 | || TYPE_MAIN_VARIANT (t) == signed_char_type_node | |
5127 | || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node | |
5128 | || TYPE_MAIN_VARIANT (t) == short_integer_type_node | |
7aa1e6eb | 5129 | || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node |
5130 | || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node)); | |
d7aeef06 | 5131 | |
5132 | case ENUMERAL_TYPE: | |
5133 | /* ??? Technically all enumerations not larger than an int | |
5134 | promote to an int. But this is used along code paths | |
5135 | that only want to notice a size change. */ | |
5136 | return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node); | |
5137 | ||
5138 | case BOOLEAN_TYPE: | |
5139 | return 1; | |
5140 | ||
5141 | default: | |
5142 | return 0; | |
5143 | } | |
5144 | } | |
5145 | ||
e94026da | 5146 | /* Return 1 if PARMS specifies a fixed number of parameters |
5147 | and none of their types is affected by default promotions. */ | |
5148 | ||
5149 | int | |
9f627b1a | 5150 | self_promoting_args_p (const_tree parms) |
e94026da | 5151 | { |
9f627b1a | 5152 | const_tree t; |
e94026da | 5153 | for (t = parms; t; t = TREE_CHAIN (t)) |
5154 | { | |
19cb6b50 | 5155 | tree type = TREE_VALUE (t); |
43f74bc4 | 5156 | |
e1d8e198 | 5157 | if (type == error_mark_node) |
5158 | continue; | |
5159 | ||
e94026da | 5160 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
5161 | return 0; | |
5162 | ||
5163 | if (type == 0) | |
5164 | return 0; | |
5165 | ||
5166 | if (TYPE_MAIN_VARIANT (type) == float_type_node) | |
5167 | return 0; | |
5168 | ||
d7aeef06 | 5169 | if (c_promoting_integer_type_p (type)) |
e94026da | 5170 | return 0; |
5171 | } | |
5172 | return 1; | |
5173 | } | |
605fb01e | 5174 | |
c10de5e7 | 5175 | /* Recursively remove any '*' or '&' operator from TYPE. */ |
5176 | tree | |
5177 | strip_pointer_operator (tree t) | |
5178 | { | |
5179 | while (POINTER_TYPE_P (t)) | |
5180 | t = TREE_TYPE (t); | |
5181 | return t; | |
5182 | } | |
5183 | ||
57a0ed23 | 5184 | /* Recursively remove pointer or array type from TYPE. */ |
5185 | tree | |
5186 | strip_pointer_or_array_types (tree t) | |
5187 | { | |
5188 | while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t)) | |
5189 | t = TREE_TYPE (t); | |
5190 | return t; | |
5191 | } | |
5192 | ||
e41f0d80 | 5193 | /* Used to compare case labels. K1 and K2 are actually tree nodes |
5194 | representing case labels, or NULL_TREE for a `default' label. | |
5195 | Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after | |
5196 | K2, and 0 if K1 and K2 are equal. */ | |
5197 | ||
5198 | int | |
1cae46be | 5199 | case_compare (splay_tree_key k1, splay_tree_key k2) |
e41f0d80 | 5200 | { |
5201 | /* Consider a NULL key (such as arises with a `default' label) to be | |
5202 | smaller than anything else. */ | |
5203 | if (!k1) | |
5204 | return k2 ? -1 : 0; | |
5205 | else if (!k2) | |
5206 | return k1 ? 1 : 0; | |
5207 | ||
5208 | return tree_int_cst_compare ((tree) k1, (tree) k2); | |
5209 | } | |
5210 | ||
e60a6f7b | 5211 | /* Process a case label, located at LOC, for the range LOW_VALUE |
5212 | ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE | |
5213 | then this case label is actually a `default' label. If only | |
5214 | HIGH_VALUE is NULL_TREE, then case label was declared using the | |
5215 | usual C/C++ syntax, rather than the GNU case range extension. | |
5216 | CASES is a tree containing all the case ranges processed so far; | |
5217 | COND is the condition for the switch-statement itself. Returns the | |
5218 | CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR | |
5219 | is created. */ | |
e41f0d80 | 5220 | |
5221 | tree | |
e60a6f7b | 5222 | c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type, |
2ca392fd | 5223 | tree low_value, tree high_value) |
e41f0d80 | 5224 | { |
5225 | tree type; | |
5226 | tree label; | |
5227 | tree case_label; | |
5228 | splay_tree_node node; | |
5229 | ||
5230 | /* Create the LABEL_DECL itself. */ | |
e60a6f7b | 5231 | label = create_artificial_label (loc); |
e41f0d80 | 5232 | |
5233 | /* If there was an error processing the switch condition, bail now | |
5234 | before we get more confused. */ | |
5235 | if (!cond || cond == error_mark_node) | |
4ee9c684 | 5236 | goto error_out; |
e41f0d80 | 5237 | |
1cae46be | 5238 | if ((low_value && TREE_TYPE (low_value) |
5239 | && POINTER_TYPE_P (TREE_TYPE (low_value))) | |
e41f0d80 | 5240 | || (high_value && TREE_TYPE (high_value) |
5241 | && POINTER_TYPE_P (TREE_TYPE (high_value)))) | |
b96dc121 | 5242 | { |
e60a6f7b | 5243 | error_at (loc, "pointers are not permitted as case values"); |
b96dc121 | 5244 | goto error_out; |
5245 | } | |
e41f0d80 | 5246 | |
5247 | /* Case ranges are a GNU extension. */ | |
8864917d | 5248 | if (high_value) |
48e1416a | 5249 | pedwarn (loc, OPT_pedantic, |
8864917d | 5250 | "range expressions in switch statements are non-standard"); |
e41f0d80 | 5251 | |
5252 | type = TREE_TYPE (cond); | |
5253 | if (low_value) | |
5254 | { | |
5255 | low_value = check_case_value (low_value); | |
5256 | low_value = convert_and_check (type, low_value); | |
96722196 | 5257 | if (low_value == error_mark_node) |
5258 | goto error_out; | |
e41f0d80 | 5259 | } |
5260 | if (high_value) | |
5261 | { | |
5262 | high_value = check_case_value (high_value); | |
5263 | high_value = convert_and_check (type, high_value); | |
96722196 | 5264 | if (high_value == error_mark_node) |
5265 | goto error_out; | |
e41f0d80 | 5266 | } |
5267 | ||
96722196 | 5268 | if (low_value && high_value) |
5269 | { | |
5270 | /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't | |
a0c938f0 | 5271 | really a case range, even though it was written that way. |
5272 | Remove the HIGH_VALUE to simplify later processing. */ | |
96722196 | 5273 | if (tree_int_cst_equal (low_value, high_value)) |
5274 | high_value = NULL_TREE; | |
5275 | else if (!tree_int_cst_lt (low_value, high_value)) | |
e60a6f7b | 5276 | warning_at (loc, 0, "empty range specified"); |
96722196 | 5277 | } |
e41f0d80 | 5278 | |
2ca392fd | 5279 | /* See if the case is in range of the type of the original testing |
5280 | expression. If both low_value and high_value are out of range, | |
5281 | don't insert the case label and return NULL_TREE. */ | |
5282 | if (low_value | |
84166705 | 5283 | && !check_case_bounds (type, orig_type, |
5284 | &low_value, high_value ? &high_value : NULL)) | |
2ca392fd | 5285 | return NULL_TREE; |
5286 | ||
e41f0d80 | 5287 | /* Look up the LOW_VALUE in the table of case labels we already |
5288 | have. */ | |
5289 | node = splay_tree_lookup (cases, (splay_tree_key) low_value); | |
5290 | /* If there was not an exact match, check for overlapping ranges. | |
5291 | There's no need to do this if there's no LOW_VALUE or HIGH_VALUE; | |
5292 | that's a `default' label and the only overlap is an exact match. */ | |
5293 | if (!node && (low_value || high_value)) | |
5294 | { | |
5295 | splay_tree_node low_bound; | |
5296 | splay_tree_node high_bound; | |
5297 | ||
5298 | /* Even though there wasn't an exact match, there might be an | |
5299 | overlap between this case range and another case range. | |
5300 | Since we've (inductively) not allowed any overlapping case | |
5301 | ranges, we simply need to find the greatest low case label | |
5302 | that is smaller that LOW_VALUE, and the smallest low case | |
5303 | label that is greater than LOW_VALUE. If there is an overlap | |
5304 | it will occur in one of these two ranges. */ | |
5305 | low_bound = splay_tree_predecessor (cases, | |
5306 | (splay_tree_key) low_value); | |
5307 | high_bound = splay_tree_successor (cases, | |
5308 | (splay_tree_key) low_value); | |
5309 | ||
5310 | /* Check to see if the LOW_BOUND overlaps. It is smaller than | |
5311 | the LOW_VALUE, so there is no need to check unless the | |
5312 | LOW_BOUND is in fact itself a case range. */ | |
5313 | if (low_bound | |
5314 | && CASE_HIGH ((tree) low_bound->value) | |
5315 | && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value), | |
5316 | low_value) >= 0) | |
5317 | node = low_bound; | |
5318 | /* Check to see if the HIGH_BOUND overlaps. The low end of that | |
5319 | range is bigger than the low end of the current range, so we | |
5320 | are only interested if the current range is a real range, and | |
5321 | not an ordinary case label. */ | |
1cae46be | 5322 | else if (high_bound |
e41f0d80 | 5323 | && high_value |
5324 | && (tree_int_cst_compare ((tree) high_bound->key, | |
5325 | high_value) | |
5326 | <= 0)) | |
5327 | node = high_bound; | |
5328 | } | |
5329 | /* If there was an overlap, issue an error. */ | |
5330 | if (node) | |
5331 | { | |
eaae3b75 | 5332 | tree duplicate = CASE_LABEL ((tree) node->value); |
e41f0d80 | 5333 | |
5334 | if (high_value) | |
5335 | { | |
e60a6f7b | 5336 | error_at (loc, "duplicate (or overlapping) case value"); |
5337 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
5338 | "this is the first entry overlapping that value"); | |
e41f0d80 | 5339 | } |
5340 | else if (low_value) | |
5341 | { | |
e60a6f7b | 5342 | error_at (loc, "duplicate case value") ; |
5343 | error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here"); | |
e41f0d80 | 5344 | } |
5345 | else | |
5346 | { | |
e60a6f7b | 5347 | error_at (loc, "multiple default labels in one switch"); |
5348 | error_at (DECL_SOURCE_LOCATION (duplicate), | |
5349 | "this is the first default label"); | |
e41f0d80 | 5350 | } |
4ee9c684 | 5351 | goto error_out; |
e41f0d80 | 5352 | } |
5353 | ||
5354 | /* Add a CASE_LABEL to the statement-tree. */ | |
b6e3dd65 | 5355 | case_label = add_stmt (build_case_label (low_value, high_value, label)); |
e41f0d80 | 5356 | /* Register this case label in the splay tree. */ |
1cae46be | 5357 | splay_tree_insert (cases, |
e41f0d80 | 5358 | (splay_tree_key) low_value, |
5359 | (splay_tree_value) case_label); | |
5360 | ||
5361 | return case_label; | |
4ee9c684 | 5362 | |
5363 | error_out: | |
daf6dff5 | 5364 | /* Add a label so that the back-end doesn't think that the beginning of |
4ee9c684 | 5365 | the switch is unreachable. Note that we do not add a case label, as |
a53ff4c1 | 5366 | that just leads to duplicates and thence to failure later on. */ |
4ee9c684 | 5367 | if (!cases->root) |
5368 | { | |
e60a6f7b | 5369 | tree t = create_artificial_label (loc); |
5370 | add_stmt (build_stmt (loc, LABEL_EXPR, t)); | |
4ee9c684 | 5371 | } |
5372 | return error_mark_node; | |
5373 | } | |
5374 | ||
5375 | /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach. | |
5376 | Used to verify that case values match up with enumerator values. */ | |
5377 | ||
5378 | static void | |
5379 | match_case_to_enum_1 (tree key, tree type, tree label) | |
5380 | { | |
5381 | char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1]; | |
5382 | ||
5383 | /* ??? Not working too hard to print the double-word value. | |
5384 | Should perhaps be done with %lwd in the diagnostic routines? */ | |
5385 | if (TREE_INT_CST_HIGH (key) == 0) | |
5386 | snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED, | |
5387 | TREE_INT_CST_LOW (key)); | |
5388 | else if (!TYPE_UNSIGNED (type) | |
5389 | && TREE_INT_CST_HIGH (key) == -1 | |
5390 | && TREE_INT_CST_LOW (key) != 0) | |
5391 | snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED, | |
5392 | -TREE_INT_CST_LOW (key)); | |
5393 | else | |
5394 | snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX, | |
7df7561b | 5395 | (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key), |
5396 | (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key)); | |
4ee9c684 | 5397 | |
5398 | if (TYPE_NAME (type) == 0) | |
712d2297 | 5399 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
5400 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
5401 | "case value %qs not in enumerated type", | |
5402 | buf); | |
4ee9c684 | 5403 | else |
712d2297 | 5404 | warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)), |
5405 | warn_switch ? OPT_Wswitch : OPT_Wswitch_enum, | |
5406 | "case value %qs not in enumerated type %qT", | |
5407 | buf, type); | |
4ee9c684 | 5408 | } |
5409 | ||
359d87c6 | 5410 | /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach. |
5411 | Used to verify that case values match up with enumerator values. */ | |
5412 | ||
4ee9c684 | 5413 | static int |
5414 | match_case_to_enum (splay_tree_node node, void *data) | |
5415 | { | |
5416 | tree label = (tree) node->value; | |
4fd61bc6 | 5417 | tree type = (tree) data; |
4ee9c684 | 5418 | |
5419 | /* Skip default case. */ | |
5420 | if (!CASE_LOW (label)) | |
5421 | return 0; | |
5422 | ||
359d87c6 | 5423 | /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear |
4ee9c684 | 5424 | when we did our enum->case scan. Reset our scratch bit after. */ |
359d87c6 | 5425 | if (!CASE_LOW_SEEN (label)) |
4ee9c684 | 5426 | match_case_to_enum_1 (CASE_LOW (label), type, label); |
5427 | else | |
359d87c6 | 5428 | CASE_LOW_SEEN (label) = 0; |
4ee9c684 | 5429 | |
359d87c6 | 5430 | /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is |
5431 | not set, that means that CASE_HIGH did not appear when we did our | |
5432 | enum->case scan. Reset our scratch bit after. */ | |
4ee9c684 | 5433 | if (CASE_HIGH (label)) |
5434 | { | |
359d87c6 | 5435 | if (!CASE_HIGH_SEEN (label)) |
5436 | match_case_to_enum_1 (CASE_HIGH (label), type, label); | |
5437 | else | |
5438 | CASE_HIGH_SEEN (label) = 0; | |
4ee9c684 | 5439 | } |
5440 | ||
5441 | return 0; | |
5442 | } | |
5443 | ||
e7911019 | 5444 | /* Handle -Wswitch*. Called from the front end after parsing the |
5445 | switch construct. */ | |
5446 | /* ??? Should probably be somewhere generic, since other languages | |
5447 | besides C and C++ would want this. At the moment, however, C/C++ | |
5448 | are the only tree-ssa languages that support enumerations at all, | |
5449 | so the point is moot. */ | |
4ee9c684 | 5450 | |
e7911019 | 5451 | void |
5452 | c_do_switch_warnings (splay_tree cases, location_t switch_location, | |
5453 | tree type, tree cond) | |
4ee9c684 | 5454 | { |
b27ac6b5 | 5455 | splay_tree_node default_node; |
359d87c6 | 5456 | splay_tree_node node; |
5457 | tree chain; | |
4ee9c684 | 5458 | |
5459 | if (!warn_switch && !warn_switch_enum && !warn_switch_default) | |
5460 | return; | |
5461 | ||
4ee9c684 | 5462 | default_node = splay_tree_lookup (cases, (splay_tree_key) NULL); |
8b6866af | 5463 | if (!default_node) |
5fb6a912 | 5464 | warning_at (switch_location, OPT_Wswitch_default, |
5465 | "switch missing default case"); | |
4ee9c684 | 5466 | |
359d87c6 | 5467 | /* From here on, we only care about about enumerated types. */ |
5468 | if (!type || TREE_CODE (type) != ENUMERAL_TYPE) | |
5469 | return; | |
5470 | ||
561017b5 | 5471 | /* From here on, we only care about -Wswitch and -Wswitch-enum. */ |
5472 | if (!warn_switch_enum && !warn_switch) | |
359d87c6 | 5473 | return; |
5474 | ||
561017b5 | 5475 | /* Check the cases. Warn about case values which are not members of |
5476 | the enumerated type. For -Wswitch-enum, or for -Wswitch when | |
5477 | there is no default case, check that exactly all enumeration | |
5478 | literals are covered by the cases. */ | |
5479 | ||
359d87c6 | 5480 | /* Clearing COND if it is not an integer constant simplifies |
5481 | the tests inside the loop below. */ | |
5482 | if (TREE_CODE (cond) != INTEGER_CST) | |
5483 | cond = NULL_TREE; | |
5484 | ||
5485 | /* The time complexity here is O(N*lg(N)) worst case, but for the | |
5486 | common case of monotonically increasing enumerators, it is | |
5487 | O(N), since the nature of the splay tree will keep the next | |
5488 | element adjacent to the root at all times. */ | |
4ee9c684 | 5489 | |
359d87c6 | 5490 | for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain)) |
5491 | { | |
5492 | tree value = TREE_VALUE (chain); | |
3f00a6c0 | 5493 | if (TREE_CODE (value) == CONST_DECL) |
5494 | value = DECL_INITIAL (value); | |
359d87c6 | 5495 | node = splay_tree_lookup (cases, (splay_tree_key) value); |
5496 | if (node) | |
4ee9c684 | 5497 | { |
359d87c6 | 5498 | /* Mark the CASE_LOW part of the case entry as seen. */ |
5499 | tree label = (tree) node->value; | |
5500 | CASE_LOW_SEEN (label) = 1; | |
5501 | continue; | |
5502 | } | |
5503 | ||
5504 | /* Even though there wasn't an exact match, there might be a | |
f0b5f617 | 5505 | case range which includes the enumerator's value. */ |
359d87c6 | 5506 | node = splay_tree_predecessor (cases, (splay_tree_key) value); |
5507 | if (node && CASE_HIGH ((tree) node->value)) | |
5508 | { | |
5509 | tree label = (tree) node->value; | |
5510 | int cmp = tree_int_cst_compare (CASE_HIGH (label), value); | |
5511 | if (cmp >= 0) | |
4ee9c684 | 5512 | { |
359d87c6 | 5513 | /* If we match the upper bound exactly, mark the CASE_HIGH |
5514 | part of the case entry as seen. */ | |
5515 | if (cmp == 0) | |
5516 | CASE_HIGH_SEEN (label) = 1; | |
5517 | continue; | |
4ee9c684 | 5518 | } |
5519 | } | |
5520 | ||
359d87c6 | 5521 | /* We've now determined that this enumerated literal isn't |
5522 | handled by the case labels of the switch statement. */ | |
4ee9c684 | 5523 | |
359d87c6 | 5524 | /* If the switch expression is a constant, we only really care |
5525 | about whether that constant is handled by the switch. */ | |
5526 | if (cond && tree_int_cst_compare (cond, value)) | |
5527 | continue; | |
4ee9c684 | 5528 | |
6cbbbc89 | 5529 | /* If there is a default_node, the only relevant option is |
561017b5 | 5530 | Wswitch-enum. Otherwise, if both are enabled then we prefer |
6cbbbc89 | 5531 | to warn using -Wswitch because -Wswitch is enabled by -Wall |
5532 | while -Wswitch-enum is explicit. */ | |
561017b5 | 5533 | warning_at (switch_location, |
5534 | (default_node || !warn_switch | |
5535 | ? OPT_Wswitch_enum | |
5536 | : OPT_Wswitch), | |
5537 | "enumeration value %qE not handled in switch", | |
5538 | TREE_PURPOSE (chain)); | |
4ee9c684 | 5539 | } |
359d87c6 | 5540 | |
5541 | /* Warn if there are case expressions that don't correspond to | |
5542 | enumerators. This can occur since C and C++ don't enforce | |
5543 | type-checking of assignments to enumeration variables. | |
5544 | ||
5545 | The time complexity here is now always O(N) worst case, since | |
5546 | we should have marked both the lower bound and upper bound of | |
5547 | every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN | |
5548 | above. This scan also resets those fields. */ | |
6cbbbc89 | 5549 | |
359d87c6 | 5550 | splay_tree_foreach (cases, match_case_to_enum, type); |
e41f0d80 | 5551 | } |
5552 | ||
9dd48740 | 5553 | /* Finish an expression taking the address of LABEL (an |
dda49785 | 5554 | IDENTIFIER_NODE). Returns an expression for the address. |
5555 | ||
5556 | LOC is the location for the expression returned. */ | |
d0a47c8d | 5557 | |
1cae46be | 5558 | tree |
dda49785 | 5559 | finish_label_address_expr (tree label, location_t loc) |
d0a47c8d | 5560 | { |
5561 | tree result; | |
5562 | ||
21ca8540 | 5563 | pedwarn (input_location, OPT_pedantic, "taking the address of a label is non-standard"); |
d0a47c8d | 5564 | |
9dd48740 | 5565 | if (label == error_mark_node) |
5566 | return error_mark_node; | |
5567 | ||
d0a47c8d | 5568 | label = lookup_label (label); |
5569 | if (label == NULL_TREE) | |
5570 | result = null_pointer_node; | |
5571 | else | |
5572 | { | |
5573 | TREE_USED (label) = 1; | |
5574 | result = build1 (ADDR_EXPR, ptr_type_node, label); | |
344cd9b2 | 5575 | /* The current function is not necessarily uninlinable. |
d0a47c8d | 5576 | Computed gotos are incompatible with inlining, but the value |
5577 | here could be used only in a diagnostic, for example. */ | |
dda49785 | 5578 | protected_set_expr_location (result, loc); |
d0a47c8d | 5579 | } |
5580 | ||
5581 | return result; | |
5582 | } | |
4f9a1c9b | 5583 | \f |
5584 | ||
5585 | /* Given a boolean expression ARG, return a tree representing an increment | |
5586 | or decrement (as indicated by CODE) of ARG. The front end must check for | |
5587 | invalid cases (e.g., decrement in C++). */ | |
5588 | tree | |
1cae46be | 5589 | boolean_increment (enum tree_code code, tree arg) |
4f9a1c9b | 5590 | { |
5591 | tree val; | |
69db191c | 5592 | tree true_res = build_int_cst (TREE_TYPE (arg), 1); |
c0f19401 | 5593 | |
4f9a1c9b | 5594 | arg = stabilize_reference (arg); |
5595 | switch (code) | |
5596 | { | |
5597 | case PREINCREMENT_EXPR: | |
14ae0310 | 5598 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 5599 | break; |
5600 | case POSTINCREMENT_EXPR: | |
14ae0310 | 5601 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res); |
4f9a1c9b | 5602 | arg = save_expr (arg); |
14ae0310 | 5603 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
5604 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 5605 | break; |
5606 | case PREDECREMENT_EXPR: | |
14ae0310 | 5607 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
389dd41b | 5608 | invert_truthvalue_loc (input_location, arg)); |
4f9a1c9b | 5609 | break; |
5610 | case POSTDECREMENT_EXPR: | |
14ae0310 | 5611 | val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, |
389dd41b | 5612 | invert_truthvalue_loc (input_location, arg)); |
4f9a1c9b | 5613 | arg = save_expr (arg); |
14ae0310 | 5614 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg); |
5615 | val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val); | |
4f9a1c9b | 5616 | break; |
5617 | default: | |
231bd014 | 5618 | gcc_unreachable (); |
4f9a1c9b | 5619 | } |
5620 | TREE_SIDE_EFFECTS (val) = 1; | |
5621 | return val; | |
5622 | } | |
76a6e674 | 5623 | \f |
f3449a3c | 5624 | /* Built-in macros for stddef.h and stdint.h, that require macros |
5625 | defined in this file. */ | |
79cf3ec1 | 5626 | void |
1cae46be | 5627 | c_stddef_cpp_builtins(void) |
1ed9d5f5 | 5628 | { |
63994318 | 5629 | builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0); |
5630 | builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0); | |
5631 | builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0); | |
5632 | builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0); | |
36bccbfc | 5633 | builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0); |
5634 | builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0); | |
b0726616 | 5635 | builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0); |
5636 | builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0); | |
f3449a3c | 5637 | if (SIG_ATOMIC_TYPE) |
5638 | builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0); | |
5639 | if (INT8_TYPE) | |
5640 | builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0); | |
5641 | if (INT16_TYPE) | |
5642 | builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0); | |
5643 | if (INT32_TYPE) | |
5644 | builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0); | |
5645 | if (INT64_TYPE) | |
5646 | builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0); | |
5647 | if (UINT8_TYPE) | |
5648 | builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0); | |
5649 | if (UINT16_TYPE) | |
5650 | builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0); | |
5651 | if (UINT32_TYPE) | |
5652 | builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0); | |
5653 | if (UINT64_TYPE) | |
5654 | builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0); | |
5655 | if (INT_LEAST8_TYPE) | |
5656 | builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0); | |
5657 | if (INT_LEAST16_TYPE) | |
5658 | builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0); | |
5659 | if (INT_LEAST32_TYPE) | |
5660 | builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0); | |
5661 | if (INT_LEAST64_TYPE) | |
5662 | builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0); | |
5663 | if (UINT_LEAST8_TYPE) | |
5664 | builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0); | |
5665 | if (UINT_LEAST16_TYPE) | |
5666 | builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0); | |
5667 | if (UINT_LEAST32_TYPE) | |
5668 | builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0); | |
5669 | if (UINT_LEAST64_TYPE) | |
5670 | builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0); | |
5671 | if (INT_FAST8_TYPE) | |
5672 | builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0); | |
5673 | if (INT_FAST16_TYPE) | |
5674 | builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0); | |
5675 | if (INT_FAST32_TYPE) | |
5676 | builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0); | |
5677 | if (INT_FAST64_TYPE) | |
5678 | builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0); | |
5679 | if (UINT_FAST8_TYPE) | |
5680 | builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0); | |
5681 | if (UINT_FAST16_TYPE) | |
5682 | builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0); | |
5683 | if (UINT_FAST32_TYPE) | |
5684 | builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0); | |
5685 | if (UINT_FAST64_TYPE) | |
5686 | builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0); | |
5687 | if (INTPTR_TYPE) | |
5688 | builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0); | |
5689 | if (UINTPTR_TYPE) | |
5690 | builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0); | |
574006c3 | 5691 | } |
5692 | ||
7d3b509a | 5693 | static void |
1cae46be | 5694 | c_init_attributes (void) |
7d3b509a | 5695 | { |
5696 | /* Fill in the built_in_attributes array. */ | |
7c446c95 | 5697 | #define DEF_ATTR_NULL_TREE(ENUM) \ |
7d3b509a | 5698 | built_in_attributes[(int) ENUM] = NULL_TREE; |
7c446c95 | 5699 | #define DEF_ATTR_INT(ENUM, VALUE) \ |
ceb7b692 | 5700 | built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE); |
7d3b509a | 5701 | #define DEF_ATTR_IDENT(ENUM, STRING) \ |
5702 | built_in_attributes[(int) ENUM] = get_identifier (STRING); | |
5703 | #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \ | |
5704 | built_in_attributes[(int) ENUM] \ | |
5705 | = tree_cons (built_in_attributes[(int) PURPOSE], \ | |
5706 | built_in_attributes[(int) VALUE], \ | |
5707 | built_in_attributes[(int) CHAIN]); | |
7d3b509a | 5708 | #include "builtin-attrs.def" |
5709 | #undef DEF_ATTR_NULL_TREE | |
5710 | #undef DEF_ATTR_INT | |
5711 | #undef DEF_ATTR_IDENT | |
5712 | #undef DEF_ATTR_TREE_LIST | |
76a6e674 | 5713 | } |
5f3cead1 | 5714 | |
33199a81 | 5715 | /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain |
5716 | identifier as an argument, so the front end shouldn't look it up. */ | |
5717 | ||
5718 | bool | |
47b19b94 | 5719 | attribute_takes_identifier_p (const_tree attr_id) |
33199a81 | 5720 | { |
9bf1c74e | 5721 | const struct attribute_spec *spec = lookup_attribute_spec (attr_id); |
02cb1060 | 5722 | if (spec == NULL) |
5723 | /* Unknown attribute that we'll end up ignoring, return true so we | |
5724 | don't complain about an identifier argument. */ | |
5725 | return true; | |
5726 | else if (!strcmp ("mode", spec->name) | |
5727 | || !strcmp ("format", spec->name) | |
5728 | || !strcmp ("cleanup", spec->name)) | |
47b19b94 | 5729 | return true; |
5730 | else | |
5731 | return targetm.attribute_takes_identifier_p (attr_id); | |
33199a81 | 5732 | } |
5733 | ||
f8e93a2e | 5734 | /* Attribute handlers common to C front ends. */ |
5735 | ||
5736 | /* Handle a "packed" attribute; arguments as in | |
5737 | struct attribute_spec.handler. */ | |
5738 | ||
5739 | static tree | |
9a03a746 | 5740 | handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
09347743 | 5741 | int flags, bool *no_add_attrs) |
f8e93a2e | 5742 | { |
f40175cb | 5743 | if (TYPE_P (*node)) |
f8e93a2e | 5744 | { |
5745 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 5746 | *node = build_variant_type_copy (*node); |
f40175cb | 5747 | TYPE_PACKED (*node) = 1; |
f8e93a2e | 5748 | } |
5749 | else if (TREE_CODE (*node) == FIELD_DECL) | |
c2ab04f9 | 5750 | { |
9fd767c5 | 5751 | if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT |
5752 | /* Still pack bitfields. */ | |
5753 | && ! DECL_INITIAL (*node)) | |
c2ab04f9 | 5754 | warning (OPT_Wattributes, |
5755 | "%qE attribute ignored for field of type %qT", | |
5756 | name, TREE_TYPE (*node)); | |
5757 | else | |
5758 | DECL_PACKED (*node) = 1; | |
5759 | } | |
f8e93a2e | 5760 | /* We can't set DECL_PACKED for a VAR_DECL, because the bit is |
f40175cb | 5761 | used for DECL_REGISTER. It wouldn't mean anything anyway. |
5762 | We can't set DECL_PACKED on the type of a TYPE_DECL, because | |
5763 | that changes what the typedef is typing. */ | |
f8e93a2e | 5764 | else |
5765 | { | |
9b2d6d13 | 5766 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5767 | *no_add_attrs = true; |
5768 | } | |
5769 | ||
5770 | return NULL_TREE; | |
5771 | } | |
5772 | ||
5773 | /* Handle a "nocommon" attribute; arguments as in | |
5774 | struct attribute_spec.handler. */ | |
5775 | ||
5776 | static tree | |
1cae46be | 5777 | handle_nocommon_attribute (tree *node, tree name, |
9a03a746 | 5778 | tree ARG_UNUSED (args), |
5779 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 5780 | { |
5781 | if (TREE_CODE (*node) == VAR_DECL) | |
5782 | DECL_COMMON (*node) = 0; | |
5783 | else | |
5784 | { | |
9b2d6d13 | 5785 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5786 | *no_add_attrs = true; |
5787 | } | |
5788 | ||
5789 | return NULL_TREE; | |
5790 | } | |
5791 | ||
5792 | /* Handle a "common" attribute; arguments as in | |
5793 | struct attribute_spec.handler. */ | |
5794 | ||
5795 | static tree | |
9a03a746 | 5796 | handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
5797 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 5798 | { |
5799 | if (TREE_CODE (*node) == VAR_DECL) | |
5800 | DECL_COMMON (*node) = 1; | |
5801 | else | |
5802 | { | |
9b2d6d13 | 5803 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5804 | *no_add_attrs = true; |
5805 | } | |
5806 | ||
5807 | return NULL_TREE; | |
5808 | } | |
5809 | ||
5810 | /* Handle a "noreturn" attribute; arguments as in | |
5811 | struct attribute_spec.handler. */ | |
5812 | ||
5813 | static tree | |
9a03a746 | 5814 | handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
5815 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 5816 | { |
5817 | tree type = TREE_TYPE (*node); | |
5818 | ||
5819 | /* See FIXME comment in c_common_attribute_table. */ | |
8c582e4f | 5820 | if (TREE_CODE (*node) == FUNCTION_DECL |
5821 | || objc_method_decl (TREE_CODE (*node))) | |
f8e93a2e | 5822 | TREE_THIS_VOLATILE (*node) = 1; |
5823 | else if (TREE_CODE (type) == POINTER_TYPE | |
5824 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
5825 | TREE_TYPE (*node) | |
5826 | = build_pointer_type | |
5827 | (build_type_variant (TREE_TYPE (type), | |
71bda45d | 5828 | TYPE_READONLY (TREE_TYPE (type)), 1)); |
f8e93a2e | 5829 | else |
5830 | { | |
9b2d6d13 | 5831 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5832 | *no_add_attrs = true; |
5833 | } | |
5834 | ||
5835 | return NULL_TREE; | |
5836 | } | |
5837 | ||
5de92639 | 5838 | /* Handle a "hot" and attribute; arguments as in |
5839 | struct attribute_spec.handler. */ | |
5840 | ||
5841 | static tree | |
5842 | handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
46f8e3b0 | 5843 | int ARG_UNUSED (flags), bool *no_add_attrs) |
5de92639 | 5844 | { |
5845 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5846 | { | |
5847 | if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL) | |
5848 | { | |
5849 | warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", | |
5850 | name, "cold"); | |
5851 | *no_add_attrs = true; | |
5852 | } | |
24470055 | 5853 | /* Most of the rest of the hot processing is done later with |
5854 | lookup_attribute. */ | |
5de92639 | 5855 | } |
5856 | else | |
5857 | { | |
5858 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5859 | *no_add_attrs = true; | |
5860 | } | |
5861 | ||
5862 | return NULL_TREE; | |
5863 | } | |
5864 | /* Handle a "cold" and attribute; arguments as in | |
5865 | struct attribute_spec.handler. */ | |
5866 | ||
5867 | static tree | |
5868 | handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
5869 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
5870 | { | |
5871 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5872 | { | |
5873 | if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL) | |
5874 | { | |
5875 | warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s", | |
5876 | name, "hot"); | |
5877 | *no_add_attrs = true; | |
5878 | } | |
24470055 | 5879 | /* Most of the rest of the cold processing is done later with |
5880 | lookup_attribute. */ | |
5de92639 | 5881 | } |
5882 | else | |
5883 | { | |
5884 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5885 | *no_add_attrs = true; | |
5886 | } | |
5887 | ||
5888 | return NULL_TREE; | |
5889 | } | |
5890 | ||
f8e93a2e | 5891 | /* Handle a "noinline" attribute; arguments as in |
5892 | struct attribute_spec.handler. */ | |
5893 | ||
5894 | static tree | |
1cae46be | 5895 | handle_noinline_attribute (tree *node, tree name, |
9a03a746 | 5896 | tree ARG_UNUSED (args), |
5897 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 5898 | { |
5899 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5900 | DECL_UNINLINABLE (*node) = 1; | |
5901 | else | |
5902 | { | |
9b2d6d13 | 5903 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5904 | *no_add_attrs = true; |
5905 | } | |
5906 | ||
5907 | return NULL_TREE; | |
5908 | } | |
5909 | ||
bdb1f0d1 | 5910 | /* Handle a "noclone" attribute; arguments as in |
5911 | struct attribute_spec.handler. */ | |
5912 | ||
5913 | static tree | |
5914 | handle_noclone_attribute (tree *node, tree name, | |
5915 | tree ARG_UNUSED (args), | |
5916 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
5917 | { | |
5918 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
5919 | { | |
5920 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5921 | *no_add_attrs = true; | |
5922 | } | |
5923 | ||
5924 | return NULL_TREE; | |
5925 | } | |
5926 | ||
f8e93a2e | 5927 | /* Handle a "always_inline" attribute; arguments as in |
5928 | struct attribute_spec.handler. */ | |
5929 | ||
5930 | static tree | |
1cae46be | 5931 | handle_always_inline_attribute (tree *node, tree name, |
9a03a746 | 5932 | tree ARG_UNUSED (args), |
5933 | int ARG_UNUSED (flags), | |
09347743 | 5934 | bool *no_add_attrs) |
f8e93a2e | 5935 | { |
5936 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
5937 | { | |
ebb7d626 | 5938 | /* Set the attribute and mark it for disregarding inline |
5939 | limits. */ | |
5940 | DECL_DISREGARD_INLINE_LIMITS (*node) = 1; | |
f8e93a2e | 5941 | } |
5942 | else | |
5943 | { | |
9b2d6d13 | 5944 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 5945 | *no_add_attrs = true; |
541e4101 | 5946 | } |
5947 | ||
5948 | return NULL_TREE; | |
5949 | } | |
5950 | ||
5951 | /* Handle a "gnu_inline" attribute; arguments as in | |
5952 | struct attribute_spec.handler. */ | |
5953 | ||
5954 | static tree | |
5955 | handle_gnu_inline_attribute (tree *node, tree name, | |
5956 | tree ARG_UNUSED (args), | |
5957 | int ARG_UNUSED (flags), | |
5958 | bool *no_add_attrs) | |
5959 | { | |
5960 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
5961 | { | |
5962 | /* Do nothing else, just set the attribute. We'll get at | |
5963 | it later with lookup_attribute. */ | |
5964 | } | |
5965 | else | |
5966 | { | |
5967 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5968 | *no_add_attrs = true; | |
7bd95dfd | 5969 | } |
5970 | ||
5971 | return NULL_TREE; | |
5972 | } | |
5973 | ||
5974 | /* Handle a "leaf" attribute; arguments as in | |
5975 | struct attribute_spec.handler. */ | |
5976 | ||
5977 | static tree | |
5978 | handle_leaf_attribute (tree *node, tree name, | |
5979 | tree ARG_UNUSED (args), | |
5980 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
5981 | { | |
5982 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
5983 | { | |
5984 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
5985 | *no_add_attrs = true; | |
5986 | } | |
5987 | if (!TREE_PUBLIC (*node)) | |
5988 | { | |
5989 | warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name); | |
5990 | *no_add_attrs = true; | |
f8e93a2e | 5991 | } |
5992 | ||
5993 | return NULL_TREE; | |
5994 | } | |
5995 | ||
1b16fc45 | 5996 | /* Handle an "artificial" attribute; arguments as in |
5997 | struct attribute_spec.handler. */ | |
5998 | ||
5999 | static tree | |
6000 | handle_artificial_attribute (tree *node, tree name, | |
6001 | tree ARG_UNUSED (args), | |
6002 | int ARG_UNUSED (flags), | |
6003 | bool *no_add_attrs) | |
6004 | { | |
6005 | if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node)) | |
6006 | { | |
6007 | /* Do nothing else, just set the attribute. We'll get at | |
6008 | it later with lookup_attribute. */ | |
6009 | } | |
6010 | else | |
6011 | { | |
6012 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6013 | *no_add_attrs = true; | |
6014 | } | |
6015 | ||
6016 | return NULL_TREE; | |
6017 | } | |
6018 | ||
0cdd9887 | 6019 | /* Handle a "flatten" attribute; arguments as in |
6020 | struct attribute_spec.handler. */ | |
6021 | ||
6022 | static tree | |
6023 | handle_flatten_attribute (tree *node, tree name, | |
a0c938f0 | 6024 | tree args ATTRIBUTE_UNUSED, |
6025 | int flags ATTRIBUTE_UNUSED, bool *no_add_attrs) | |
0cdd9887 | 6026 | { |
6027 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6028 | /* Do nothing else, just set the attribute. We'll get at | |
6029 | it later with lookup_attribute. */ | |
6030 | ; | |
6031 | else | |
6032 | { | |
6033 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6034 | *no_add_attrs = true; | |
6035 | } | |
6036 | ||
6037 | return NULL_TREE; | |
6038 | } | |
6039 | ||
10fc867f | 6040 | /* Handle a "warning" or "error" attribute; arguments as in |
6041 | struct attribute_spec.handler. */ | |
6042 | ||
6043 | static tree | |
6044 | handle_error_attribute (tree *node, tree name, tree args, | |
6045 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6046 | { | |
6047 | if (TREE_CODE (*node) == FUNCTION_DECL | |
6048 | || TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
6049 | /* Do nothing else, just set the attribute. We'll get at | |
6050 | it later with lookup_attribute. */ | |
6051 | ; | |
6052 | else | |
6053 | { | |
6054 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6055 | *no_add_attrs = true; | |
6056 | } | |
6057 | ||
6058 | return NULL_TREE; | |
6059 | } | |
0cdd9887 | 6060 | |
f8e93a2e | 6061 | /* Handle a "used" attribute; arguments as in |
6062 | struct attribute_spec.handler. */ | |
6063 | ||
6064 | static tree | |
9a03a746 | 6065 | handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args), |
6066 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6067 | { |
d0a31bd8 | 6068 | tree node = *pnode; |
6069 | ||
6070 | if (TREE_CODE (node) == FUNCTION_DECL | |
6071 | || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))) | |
f54ed8bc | 6072 | { |
f54ed8bc | 6073 | TREE_USED (node) = 1; |
9423c9b7 | 6074 | DECL_PRESERVE_P (node) = 1; |
abc6c64f | 6075 | if (TREE_CODE (node) == VAR_DECL) |
6076 | DECL_READ_P (node) = 1; | |
f54ed8bc | 6077 | } |
f8e93a2e | 6078 | else |
6079 | { | |
9b2d6d13 | 6080 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6081 | *no_add_attrs = true; |
6082 | } | |
6083 | ||
6084 | return NULL_TREE; | |
6085 | } | |
6086 | ||
6087 | /* Handle a "unused" attribute; arguments as in | |
6088 | struct attribute_spec.handler. */ | |
6089 | ||
6090 | static tree | |
9a03a746 | 6091 | handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6092 | int flags, bool *no_add_attrs) | |
f8e93a2e | 6093 | { |
6094 | if (DECL_P (*node)) | |
6095 | { | |
6096 | tree decl = *node; | |
6097 | ||
6098 | if (TREE_CODE (decl) == PARM_DECL | |
6099 | || TREE_CODE (decl) == VAR_DECL | |
6100 | || TREE_CODE (decl) == FUNCTION_DECL | |
6101 | || TREE_CODE (decl) == LABEL_DECL | |
6102 | || TREE_CODE (decl) == TYPE_DECL) | |
abc6c64f | 6103 | { |
6104 | TREE_USED (decl) = 1; | |
6105 | if (TREE_CODE (decl) == VAR_DECL | |
6106 | || TREE_CODE (decl) == PARM_DECL) | |
6107 | DECL_READ_P (decl) = 1; | |
6108 | } | |
f8e93a2e | 6109 | else |
6110 | { | |
9b2d6d13 | 6111 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6112 | *no_add_attrs = true; |
6113 | } | |
6114 | } | |
6115 | else | |
6116 | { | |
6117 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 6118 | *node = build_variant_type_copy (*node); |
f8e93a2e | 6119 | TREE_USED (*node) = 1; |
6120 | } | |
6121 | ||
6122 | return NULL_TREE; | |
6123 | } | |
6124 | ||
62eec3b4 | 6125 | /* Handle a "externally_visible" attribute; arguments as in |
6126 | struct attribute_spec.handler. */ | |
6127 | ||
6128 | static tree | |
6129 | handle_externally_visible_attribute (tree *pnode, tree name, | |
6130 | tree ARG_UNUSED (args), | |
6131 | int ARG_UNUSED (flags), | |
6132 | bool *no_add_attrs) | |
6133 | { | |
6134 | tree node = *pnode; | |
6135 | ||
ba12ea31 | 6136 | if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL) |
62eec3b4 | 6137 | { |
ba12ea31 | 6138 | if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL |
6139 | && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node)) | |
6140 | { | |
6141 | warning (OPT_Wattributes, | |
6142 | "%qE attribute have effect only on public objects", name); | |
6143 | *no_add_attrs = true; | |
6144 | } | |
62eec3b4 | 6145 | } |
62eec3b4 | 6146 | else |
6147 | { | |
6148 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6149 | *no_add_attrs = true; | |
6150 | } | |
6151 | ||
6152 | return NULL_TREE; | |
6153 | } | |
6154 | ||
f8e93a2e | 6155 | /* Handle a "const" attribute; arguments as in |
6156 | struct attribute_spec.handler. */ | |
6157 | ||
6158 | static tree | |
9a03a746 | 6159 | handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
6160 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6161 | { |
6162 | tree type = TREE_TYPE (*node); | |
6163 | ||
6164 | /* See FIXME comment on noreturn in c_common_attribute_table. */ | |
6165 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
6166 | TREE_READONLY (*node) = 1; | |
6167 | else if (TREE_CODE (type) == POINTER_TYPE | |
6168 | && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
6169 | TREE_TYPE (*node) | |
6170 | = build_pointer_type | |
6171 | (build_type_variant (TREE_TYPE (type), 1, | |
6172 | TREE_THIS_VOLATILE (TREE_TYPE (type)))); | |
6173 | else | |
6174 | { | |
9b2d6d13 | 6175 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6176 | *no_add_attrs = true; |
6177 | } | |
6178 | ||
6179 | return NULL_TREE; | |
6180 | } | |
6181 | ||
6182 | /* Handle a "transparent_union" attribute; arguments as in | |
6183 | struct attribute_spec.handler. */ | |
6184 | ||
6185 | static tree | |
1cae46be | 6186 | handle_transparent_union_attribute (tree *node, tree name, |
9a03a746 | 6187 | tree ARG_UNUSED (args), int flags, |
09347743 | 6188 | bool *no_add_attrs) |
f8e93a2e | 6189 | { |
881eb642 | 6190 | tree type; |
03908818 | 6191 | |
6192 | *no_add_attrs = true; | |
f8e93a2e | 6193 | |
881eb642 | 6194 | if (TREE_CODE (*node) == TYPE_DECL) |
6195 | node = &TREE_TYPE (*node); | |
6196 | type = *node; | |
f8e93a2e | 6197 | |
03908818 | 6198 | if (TREE_CODE (type) == UNION_TYPE) |
f8e93a2e | 6199 | { |
03908818 | 6200 | /* When IN_PLACE is set, leave the check for FIELDS and MODE to |
6201 | the code in finish_struct. */ | |
f8e93a2e | 6202 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
03908818 | 6203 | { |
6204 | if (TYPE_FIELDS (type) == NULL_TREE | |
914d1151 | 6205 | || c_dialect_cxx () |
03908818 | 6206 | || TYPE_MODE (type) != DECL_MODE (TYPE_FIELDS (type))) |
6207 | goto ignored; | |
6208 | ||
6209 | /* A type variant isn't good enough, since we don't a cast | |
6210 | to such a type removed as a no-op. */ | |
6211 | *node = type = build_duplicate_type (type); | |
6212 | } | |
6213 | ||
8df5a43d | 6214 | TYPE_TRANSPARENT_AGGR (type) = 1; |
03908818 | 6215 | return NULL_TREE; |
f8e93a2e | 6216 | } |
6217 | ||
03908818 | 6218 | ignored: |
6219 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 6220 | return NULL_TREE; |
6221 | } | |
6222 | ||
9af7fd5b | 6223 | /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to |
6224 | get the requested priority for a constructor or destructor, | |
6225 | possibly issuing diagnostics for invalid or reserved | |
6226 | priorities. */ | |
6227 | ||
6228 | static priority_type | |
6229 | get_priority (tree args, bool is_destructor) | |
6230 | { | |
6231 | HOST_WIDE_INT pri; | |
6c181a06 | 6232 | tree arg; |
9af7fd5b | 6233 | |
6234 | if (!args) | |
6235 | return DEFAULT_INIT_PRIORITY; | |
48e1416a | 6236 | |
28fbc04f | 6237 | if (!SUPPORTS_INIT_PRIORITY) |
6238 | { | |
6239 | if (is_destructor) | |
6240 | error ("destructor priorities are not supported"); | |
6241 | else | |
6242 | error ("constructor priorities are not supported"); | |
6243 | return DEFAULT_INIT_PRIORITY; | |
6244 | } | |
6245 | ||
6c181a06 | 6246 | arg = TREE_VALUE (args); |
6247 | if (!host_integerp (arg, /*pos=*/0) | |
6248 | || !INTEGRAL_TYPE_P (TREE_TYPE (arg))) | |
9af7fd5b | 6249 | goto invalid; |
6250 | ||
6251 | pri = tree_low_cst (TREE_VALUE (args), /*pos=*/0); | |
6252 | if (pri < 0 || pri > MAX_INIT_PRIORITY) | |
6253 | goto invalid; | |
6254 | ||
6255 | if (pri <= MAX_RESERVED_INIT_PRIORITY) | |
6256 | { | |
6257 | if (is_destructor) | |
6258 | warning (0, | |
6259 | "destructor priorities from 0 to %d are reserved " | |
48e1416a | 6260 | "for the implementation", |
9af7fd5b | 6261 | MAX_RESERVED_INIT_PRIORITY); |
6262 | else | |
6263 | warning (0, | |
6264 | "constructor priorities from 0 to %d are reserved " | |
48e1416a | 6265 | "for the implementation", |
9af7fd5b | 6266 | MAX_RESERVED_INIT_PRIORITY); |
6267 | } | |
6268 | return pri; | |
6269 | ||
6270 | invalid: | |
6271 | if (is_destructor) | |
6272 | error ("destructor priorities must be integers from 0 to %d inclusive", | |
6273 | MAX_INIT_PRIORITY); | |
6274 | else | |
6275 | error ("constructor priorities must be integers from 0 to %d inclusive", | |
6276 | MAX_INIT_PRIORITY); | |
6277 | return DEFAULT_INIT_PRIORITY; | |
6278 | } | |
6279 | ||
f8e93a2e | 6280 | /* Handle a "constructor" attribute; arguments as in |
6281 | struct attribute_spec.handler. */ | |
6282 | ||
6283 | static tree | |
9af7fd5b | 6284 | handle_constructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 6285 | int ARG_UNUSED (flags), |
09347743 | 6286 | bool *no_add_attrs) |
f8e93a2e | 6287 | { |
6288 | tree decl = *node; | |
6289 | tree type = TREE_TYPE (decl); | |
6290 | ||
6291 | if (TREE_CODE (decl) == FUNCTION_DECL | |
6292 | && TREE_CODE (type) == FUNCTION_TYPE | |
6293 | && decl_function_context (decl) == 0) | |
6294 | { | |
9af7fd5b | 6295 | priority_type priority; |
f8e93a2e | 6296 | DECL_STATIC_CONSTRUCTOR (decl) = 1; |
9af7fd5b | 6297 | priority = get_priority (args, /*is_destructor=*/false); |
6298 | SET_DECL_INIT_PRIORITY (decl, priority); | |
f8e93a2e | 6299 | TREE_USED (decl) = 1; |
6300 | } | |
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 | ||
6310 | /* Handle a "destructor" attribute; arguments as in | |
6311 | struct attribute_spec.handler. */ | |
6312 | ||
6313 | static tree | |
9af7fd5b | 6314 | handle_destructor_attribute (tree *node, tree name, tree args, |
9a03a746 | 6315 | int ARG_UNUSED (flags), |
09347743 | 6316 | bool *no_add_attrs) |
f8e93a2e | 6317 | { |
6318 | tree decl = *node; | |
6319 | tree type = TREE_TYPE (decl); | |
6320 | ||
6321 | if (TREE_CODE (decl) == FUNCTION_DECL | |
6322 | && TREE_CODE (type) == FUNCTION_TYPE | |
6323 | && decl_function_context (decl) == 0) | |
6324 | { | |
9af7fd5b | 6325 | priority_type priority; |
f8e93a2e | 6326 | DECL_STATIC_DESTRUCTOR (decl) = 1; |
9af7fd5b | 6327 | priority = get_priority (args, /*is_destructor=*/true); |
6328 | SET_DECL_FINI_PRIORITY (decl, priority); | |
f8e93a2e | 6329 | TREE_USED (decl) = 1; |
6330 | } | |
6331 | else | |
6332 | { | |
9b2d6d13 | 6333 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6334 | *no_add_attrs = true; |
6335 | } | |
6336 | ||
6337 | return NULL_TREE; | |
6338 | } | |
6339 | ||
1c58e3f1 | 6340 | /* Nonzero if the mode is a valid vector mode for this architecture. |
6341 | This returns nonzero even if there is no hardware support for the | |
6342 | vector mode, but we can emulate with narrower modes. */ | |
6343 | ||
6344 | static int | |
6345 | vector_mode_valid_p (enum machine_mode mode) | |
6346 | { | |
6347 | enum mode_class mclass = GET_MODE_CLASS (mode); | |
6348 | enum machine_mode innermode; | |
6349 | ||
6350 | /* Doh! What's going on? */ | |
6351 | if (mclass != MODE_VECTOR_INT | |
6352 | && mclass != MODE_VECTOR_FLOAT | |
6353 | && mclass != MODE_VECTOR_FRACT | |
6354 | && mclass != MODE_VECTOR_UFRACT | |
6355 | && mclass != MODE_VECTOR_ACCUM | |
6356 | && mclass != MODE_VECTOR_UACCUM) | |
6357 | return 0; | |
6358 | ||
6359 | /* Hardware support. Woo hoo! */ | |
6360 | if (targetm.vector_mode_supported_p (mode)) | |
6361 | return 1; | |
6362 | ||
6363 | innermode = GET_MODE_INNER (mode); | |
6364 | ||
6365 | /* We should probably return 1 if requesting V4DI and we have no DI, | |
6366 | but we have V2DI, but this is probably very unlikely. */ | |
6367 | ||
6368 | /* If we have support for the inner mode, we can safely emulate it. | |
6369 | We may not have V2DI, but me can emulate with a pair of DIs. */ | |
6370 | return targetm.scalar_mode_supported_p (innermode); | |
6371 | } | |
6372 | ||
6373 | ||
f8e93a2e | 6374 | /* Handle a "mode" attribute; arguments as in |
6375 | struct attribute_spec.handler. */ | |
6376 | ||
6377 | static tree | |
9a03a746 | 6378 | handle_mode_attribute (tree *node, tree name, tree args, |
6379 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6380 | { |
6381 | tree type = *node; | |
d1dd9ac0 | 6382 | tree ident = TREE_VALUE (args); |
f8e93a2e | 6383 | |
6384 | *no_add_attrs = true; | |
6385 | ||
d1dd9ac0 | 6386 | if (TREE_CODE (ident) != IDENTIFIER_NODE) |
9b2d6d13 | 6387 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6388 | else |
6389 | { | |
6390 | int j; | |
d1dd9ac0 | 6391 | const char *p = IDENTIFIER_POINTER (ident); |
f8e93a2e | 6392 | int len = strlen (p); |
6393 | enum machine_mode mode = VOIDmode; | |
6394 | tree typefm; | |
b2aef146 | 6395 | bool valid_mode; |
f8e93a2e | 6396 | |
6397 | if (len > 4 && p[0] == '_' && p[1] == '_' | |
6398 | && p[len - 1] == '_' && p[len - 2] == '_') | |
6399 | { | |
4fd61bc6 | 6400 | char *newp = (char *) alloca (len - 1); |
f8e93a2e | 6401 | |
6402 | strcpy (newp, &p[2]); | |
6403 | newp[len - 4] = '\0'; | |
6404 | p = newp; | |
6405 | } | |
6406 | ||
6407 | /* Change this type to have a type with the specified mode. | |
6408 | First check for the special modes. */ | |
84166705 | 6409 | if (!strcmp (p, "byte")) |
f8e93a2e | 6410 | mode = byte_mode; |
6411 | else if (!strcmp (p, "word")) | |
6412 | mode = word_mode; | |
84166705 | 6413 | else if (!strcmp (p, "pointer")) |
f8e93a2e | 6414 | mode = ptr_mode; |
0ef89dfd | 6415 | else if (!strcmp (p, "libgcc_cmp_return")) |
6416 | mode = targetm.libgcc_cmp_return_mode (); | |
6417 | else if (!strcmp (p, "libgcc_shift_count")) | |
6418 | mode = targetm.libgcc_shift_count_mode (); | |
1bd43494 | 6419 | else if (!strcmp (p, "unwind_word")) |
6420 | mode = targetm.unwind_word_mode (); | |
f8e93a2e | 6421 | else |
6422 | for (j = 0; j < NUM_MACHINE_MODES; j++) | |
6423 | if (!strcmp (p, GET_MODE_NAME (j))) | |
743a6f47 | 6424 | { |
6425 | mode = (enum machine_mode) j; | |
6426 | break; | |
6427 | } | |
f8e93a2e | 6428 | |
6429 | if (mode == VOIDmode) | |
4917c376 | 6430 | { |
d1dd9ac0 | 6431 | error ("unknown machine mode %qE", ident); |
4917c376 | 6432 | return NULL_TREE; |
6433 | } | |
6434 | ||
b2aef146 | 6435 | valid_mode = false; |
6436 | switch (GET_MODE_CLASS (mode)) | |
4917c376 | 6437 | { |
b2aef146 | 6438 | case MODE_INT: |
6439 | case MODE_PARTIAL_INT: | |
6440 | case MODE_FLOAT: | |
c4503c0a | 6441 | case MODE_DECIMAL_FLOAT: |
9421ebb9 | 6442 | case MODE_FRACT: |
6443 | case MODE_UFRACT: | |
6444 | case MODE_ACCUM: | |
6445 | case MODE_UACCUM: | |
b2aef146 | 6446 | valid_mode = targetm.scalar_mode_supported_p (mode); |
6447 | break; | |
6448 | ||
6449 | case MODE_COMPLEX_INT: | |
6450 | case MODE_COMPLEX_FLOAT: | |
6451 | valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode)); | |
6452 | break; | |
6453 | ||
6454 | case MODE_VECTOR_INT: | |
6455 | case MODE_VECTOR_FLOAT: | |
9421ebb9 | 6456 | case MODE_VECTOR_FRACT: |
6457 | case MODE_VECTOR_UFRACT: | |
6458 | case MODE_VECTOR_ACCUM: | |
6459 | case MODE_VECTOR_UACCUM: | |
9b2d6d13 | 6460 | warning (OPT_Wattributes, "specifying vector types with " |
6461 | "__attribute__ ((mode)) is deprecated"); | |
6462 | warning (OPT_Wattributes, | |
6463 | "use __attribute__ ((vector_size)) instead"); | |
b2aef146 | 6464 | valid_mode = vector_mode_valid_p (mode); |
6465 | break; | |
4917c376 | 6466 | |
b2aef146 | 6467 | default: |
6468 | break; | |
6469 | } | |
6470 | if (!valid_mode) | |
6471 | { | |
1e5fcbe2 | 6472 | error ("unable to emulate %qs", p); |
b2aef146 | 6473 | return NULL_TREE; |
6474 | } | |
4917c376 | 6475 | |
b2aef146 | 6476 | if (POINTER_TYPE_P (type)) |
ead34f59 | 6477 | { |
6d5d708e | 6478 | addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type)); |
b2aef146 | 6479 | tree (*fn)(tree, enum machine_mode, bool); |
6480 | ||
6d5d708e | 6481 | if (!targetm.addr_space.valid_pointer_mode (mode, as)) |
ead34f59 | 6482 | { |
1e5fcbe2 | 6483 | error ("invalid pointer mode %qs", p); |
ead34f59 | 6484 | return NULL_TREE; |
6485 | } | |
6486 | ||
a0c938f0 | 6487 | if (TREE_CODE (type) == POINTER_TYPE) |
b2aef146 | 6488 | fn = build_pointer_type_for_mode; |
805e22b2 | 6489 | else |
b2aef146 | 6490 | fn = build_reference_type_for_mode; |
6491 | typefm = fn (TREE_TYPE (type), mode, false); | |
ead34f59 | 6492 | } |
b2aef146 | 6493 | else |
9421ebb9 | 6494 | { |
6495 | /* For fixed-point modes, we need to test if the signness of type | |
6496 | and the machine mode are consistent. */ | |
6497 | if (ALL_FIXED_POINT_MODE_P (mode) | |
6498 | && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode)) | |
6499 | { | |
bf776685 | 6500 | error ("signedness of type and machine mode %qs don%'t match", p); |
9421ebb9 | 6501 | return NULL_TREE; |
6502 | } | |
6503 | /* For fixed-point modes, we need to pass saturating info. */ | |
6504 | typefm = lang_hooks.types.type_for_mode (mode, | |
6505 | ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type) | |
6506 | : TYPE_UNSIGNED (type)); | |
6507 | } | |
3a648ab9 | 6508 | |
b2aef146 | 6509 | if (typefm == NULL_TREE) |
6510 | { | |
743a6f47 | 6511 | error ("no data type for mode %qs", p); |
b2aef146 | 6512 | return NULL_TREE; |
6513 | } | |
3a648ab9 | 6514 | else if (TREE_CODE (type) == ENUMERAL_TYPE) |
6515 | { | |
6516 | /* For enumeral types, copy the precision from the integer | |
6517 | type returned above. If not an INTEGER_TYPE, we can't use | |
6518 | this mode for this type. */ | |
6519 | if (TREE_CODE (typefm) != INTEGER_TYPE) | |
6520 | { | |
743a6f47 | 6521 | error ("cannot use mode %qs for enumeral types", p); |
3a648ab9 | 6522 | return NULL_TREE; |
6523 | } | |
6524 | ||
10080eac | 6525 | if (flags & ATTR_FLAG_TYPE_IN_PLACE) |
6526 | { | |
6527 | TYPE_PRECISION (type) = TYPE_PRECISION (typefm); | |
6528 | typefm = type; | |
6529 | } | |
6530 | else | |
6531 | { | |
6532 | /* We cannot build a type variant, as there's code that assumes | |
6533 | that TYPE_MAIN_VARIANT has the same mode. This includes the | |
6534 | debug generators. Instead, create a subrange type. This | |
6535 | results in all of the enumeral values being emitted only once | |
6536 | in the original, and the subtype gets them by reference. */ | |
6537 | if (TYPE_UNSIGNED (type)) | |
6538 | typefm = make_unsigned_type (TYPE_PRECISION (typefm)); | |
6539 | else | |
6540 | typefm = make_signed_type (TYPE_PRECISION (typefm)); | |
6541 | TREE_TYPE (typefm) = type; | |
6542 | } | |
3a648ab9 | 6543 | } |
4bf450a1 | 6544 | else if (VECTOR_MODE_P (mode) |
6545 | ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm)) | |
6546 | : TREE_CODE (type) != TREE_CODE (typefm)) | |
743a6f47 | 6547 | { |
6548 | error ("mode %qs applied to inappropriate type", p); | |
6549 | return NULL_TREE; | |
6550 | } | |
6551 | ||
b2aef146 | 6552 | *node = typefm; |
f8e93a2e | 6553 | } |
6554 | ||
6555 | return NULL_TREE; | |
6556 | } | |
6557 | ||
6558 | /* Handle a "section" attribute; arguments as in | |
6559 | struct attribute_spec.handler. */ | |
6560 | ||
6561 | static tree | |
9a03a746 | 6562 | handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
6563 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 6564 | { |
6565 | tree decl = *node; | |
6566 | ||
218e3e4e | 6567 | if (targetm_common.have_named_sections) |
f8e93a2e | 6568 | { |
065efcb1 | 6569 | user_defined_section_attribute = true; |
6570 | ||
f8e93a2e | 6571 | if ((TREE_CODE (decl) == FUNCTION_DECL |
6572 | || TREE_CODE (decl) == VAR_DECL) | |
6573 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST) | |
6574 | { | |
6575 | if (TREE_CODE (decl) == VAR_DECL | |
6576 | && current_function_decl != NULL_TREE | |
84166705 | 6577 | && !TREE_STATIC (decl)) |
f8e93a2e | 6578 | { |
48e1416a | 6579 | error_at (DECL_SOURCE_LOCATION (decl), |
712d2297 | 6580 | "section attribute cannot be specified for " |
6581 | "local variables"); | |
f8e93a2e | 6582 | *no_add_attrs = true; |
6583 | } | |
6584 | ||
6585 | /* The decl may have already been given a section attribute | |
6586 | from a previous declaration. Ensure they match. */ | |
6587 | else if (DECL_SECTION_NAME (decl) != NULL_TREE | |
6588 | && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), | |
6589 | TREE_STRING_POINTER (TREE_VALUE (args))) != 0) | |
6590 | { | |
3cf8b391 | 6591 | error ("section of %q+D conflicts with previous declaration", |
6592 | *node); | |
f8e93a2e | 6593 | *no_add_attrs = true; |
6594 | } | |
38475469 | 6595 | else if (TREE_CODE (decl) == VAR_DECL |
6596 | && !targetm.have_tls && targetm.emutls.tmpl_section | |
6597 | && DECL_THREAD_LOCAL_P (decl)) | |
6598 | { | |
6599 | error ("section of %q+D cannot be overridden", *node); | |
6600 | *no_add_attrs = true; | |
6601 | } | |
f8e93a2e | 6602 | else |
6603 | DECL_SECTION_NAME (decl) = TREE_VALUE (args); | |
6604 | } | |
6605 | else | |
6606 | { | |
3cf8b391 | 6607 | error ("section attribute not allowed for %q+D", *node); |
f8e93a2e | 6608 | *no_add_attrs = true; |
6609 | } | |
6610 | } | |
6611 | else | |
6612 | { | |
712d2297 | 6613 | error_at (DECL_SOURCE_LOCATION (*node), |
6614 | "section attributes are not supported for this target"); | |
f8e93a2e | 6615 | *no_add_attrs = true; |
6616 | } | |
6617 | ||
6618 | return NULL_TREE; | |
6619 | } | |
6620 | ||
6621 | /* Handle a "aligned" attribute; arguments as in | |
6622 | struct attribute_spec.handler. */ | |
6623 | ||
6624 | static tree | |
9a03a746 | 6625 | handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args, |
09347743 | 6626 | int flags, bool *no_add_attrs) |
f8e93a2e | 6627 | { |
6628 | tree decl = NULL_TREE; | |
6629 | tree *type = NULL; | |
6630 | int is_type = 0; | |
6631 | tree align_expr = (args ? TREE_VALUE (args) | |
02421213 | 6632 | : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT)); |
f8e93a2e | 6633 | int i; |
6634 | ||
6635 | if (DECL_P (*node)) | |
6636 | { | |
6637 | decl = *node; | |
6638 | type = &TREE_TYPE (decl); | |
6639 | is_type = TREE_CODE (*node) == TYPE_DECL; | |
6640 | } | |
6641 | else if (TYPE_P (*node)) | |
6642 | type = node, is_type = 1; | |
6643 | ||
f8e93a2e | 6644 | if (TREE_CODE (align_expr) != INTEGER_CST) |
6645 | { | |
6646 | error ("requested alignment is not a constant"); | |
6647 | *no_add_attrs = true; | |
6648 | } | |
6649 | else if ((i = tree_log2 (align_expr)) == -1) | |
6650 | { | |
6651 | error ("requested alignment is not a power of 2"); | |
6652 | *no_add_attrs = true; | |
6653 | } | |
7cfdc2f0 | 6654 | else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG) |
f8e93a2e | 6655 | { |
6656 | error ("requested alignment is too large"); | |
6657 | *no_add_attrs = true; | |
6658 | } | |
6659 | else if (is_type) | |
6660 | { | |
2ec3af9c | 6661 | if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) |
6662 | /* OK, modify the type in place. */; | |
f8e93a2e | 6663 | /* If we have a TYPE_DECL, then copy the type, so that we |
6664 | don't accidentally modify a builtin type. See pushdecl. */ | |
2ec3af9c | 6665 | else if (decl && TREE_TYPE (decl) != error_mark_node |
6666 | && DECL_ORIGINAL_TYPE (decl) == NULL_TREE) | |
f8e93a2e | 6667 | { |
6668 | tree tt = TREE_TYPE (decl); | |
e086912e | 6669 | *type = build_variant_type_copy (*type); |
f8e93a2e | 6670 | DECL_ORIGINAL_TYPE (decl) = tt; |
6671 | TYPE_NAME (*type) = decl; | |
6672 | TREE_USED (*type) = TREE_USED (decl); | |
6673 | TREE_TYPE (decl) = *type; | |
6674 | } | |
2ec3af9c | 6675 | else |
e086912e | 6676 | *type = build_variant_type_copy (*type); |
f8e93a2e | 6677 | |
7cfdc2f0 | 6678 | TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT; |
f8e93a2e | 6679 | TYPE_USER_ALIGN (*type) = 1; |
6680 | } | |
097b5c8b | 6681 | else if (! VAR_OR_FUNCTION_DECL_P (decl) |
f8e93a2e | 6682 | && TREE_CODE (decl) != FIELD_DECL) |
6683 | { | |
3cf8b391 | 6684 | error ("alignment may not be specified for %q+D", decl); |
f8e93a2e | 6685 | *no_add_attrs = true; |
6686 | } | |
097b5c8b | 6687 | else if (TREE_CODE (decl) == FUNCTION_DECL |
7cfdc2f0 | 6688 | && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT) |
097b5c8b | 6689 | { |
6690 | if (DECL_USER_ALIGN (decl)) | |
6691 | error ("alignment for %q+D was previously specified as %d " | |
6692 | "and may not be decreased", decl, | |
6693 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
6694 | else | |
6695 | error ("alignment for %q+D must be at least %d", decl, | |
6696 | DECL_ALIGN (decl) / BITS_PER_UNIT); | |
6697 | *no_add_attrs = true; | |
6698 | } | |
f8e93a2e | 6699 | else |
6700 | { | |
7cfdc2f0 | 6701 | DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT; |
f8e93a2e | 6702 | DECL_USER_ALIGN (decl) = 1; |
6703 | } | |
6704 | ||
6705 | return NULL_TREE; | |
6706 | } | |
6707 | ||
6708 | /* Handle a "weak" attribute; arguments as in | |
6709 | struct attribute_spec.handler. */ | |
6710 | ||
6711 | static tree | |
f948b309 | 6712 | handle_weak_attribute (tree *node, tree name, |
9a03a746 | 6713 | tree ARG_UNUSED (args), |
6714 | int ARG_UNUSED (flags), | |
6715 | bool * ARG_UNUSED (no_add_attrs)) | |
f8e93a2e | 6716 | { |
f948b309 | 6717 | if (TREE_CODE (*node) == FUNCTION_DECL |
059a60f3 | 6718 | && DECL_DECLARED_INLINE_P (*node)) |
6719 | { | |
0725e25c | 6720 | warning (OPT_Wattributes, "inline function %q+D declared weak", *node); |
059a60f3 | 6721 | *no_add_attrs = true; |
6722 | } | |
85c0a25c | 6723 | else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
6724 | { | |
6725 | error ("indirect function %q+D cannot be declared weak", *node); | |
6726 | *no_add_attrs = true; | |
6727 | return NULL_TREE; | |
6728 | } | |
059a60f3 | 6729 | else if (TREE_CODE (*node) == FUNCTION_DECL |
6730 | || TREE_CODE (*node) == VAR_DECL) | |
f948b309 | 6731 | declare_weak (*node); |
6732 | else | |
6733 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
f8e93a2e | 6734 | |
6735 | return NULL_TREE; | |
6736 | } | |
6737 | ||
85c0a25c | 6738 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
6739 | struct attribute_spec.handler, except that IS_ALIAS tells us | |
6740 | whether this is an alias as opposed to ifunc attribute. */ | |
f8e93a2e | 6741 | |
6742 | static tree | |
85c0a25c | 6743 | handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args, |
6744 | bool *no_add_attrs) | |
f8e93a2e | 6745 | { |
6746 | tree decl = *node; | |
6747 | ||
85c0a25c | 6748 | if (TREE_CODE (decl) != FUNCTION_DECL |
6749 | && (!is_alias || TREE_CODE (decl) != VAR_DECL)) | |
9e830260 | 6750 | { |
6751 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6752 | *no_add_attrs = true; | |
6753 | } | |
6754 | else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl)) | |
48e1416a | 6755 | || (TREE_CODE (decl) != FUNCTION_DECL |
0a3ecdc1 | 6756 | && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) |
6757 | /* A static variable declaration is always a tentative definition, | |
6758 | but the alias is a non-tentative definition which overrides. */ | |
48e1416a | 6759 | || (TREE_CODE (decl) != FUNCTION_DECL |
0a3ecdc1 | 6760 | && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl))) |
f8e93a2e | 6761 | { |
85c0a25c | 6762 | error ("%q+D defined both normally and as %qE attribute", decl, name); |
f8e93a2e | 6763 | *no_add_attrs = true; |
85c0a25c | 6764 | return NULL_TREE; |
f8e93a2e | 6765 | } |
85c0a25c | 6766 | else if (!is_alias |
6767 | && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl)) | |
6768 | || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))) | |
6769 | { | |
6770 | error ("weak %q+D cannot be defined %qE", decl, name); | |
6771 | *no_add_attrs = true; | |
6772 | return NULL_TREE; | |
6773 | } | |
8c42f0d9 | 6774 | |
6775 | /* Note that the very first time we process a nested declaration, | |
6776 | decl_function_context will not be set. Indeed, *would* never | |
6777 | be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that | |
6778 | we do below. After such frobbery, pushdecl would set the context. | |
6779 | In any case, this is never what we want. */ | |
6780 | else if (decl_function_context (decl) == 0 && current_function_decl == NULL) | |
f8e93a2e | 6781 | { |
6782 | tree id; | |
6783 | ||
6784 | id = TREE_VALUE (args); | |
6785 | if (TREE_CODE (id) != STRING_CST) | |
6786 | { | |
85c0a25c | 6787 | error ("attribute %qE argument not a string", name); |
f8e93a2e | 6788 | *no_add_attrs = true; |
6789 | return NULL_TREE; | |
6790 | } | |
6791 | id = get_identifier (TREE_STRING_POINTER (id)); | |
6792 | /* This counts as a use of the object pointed to. */ | |
6793 | TREE_USED (id) = 1; | |
6794 | ||
6795 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
6796 | DECL_INITIAL (decl) = error_mark_node; | |
6797 | else | |
4ee9c684 | 6798 | { |
f4a30bd7 | 6799 | if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) |
6800 | DECL_EXTERNAL (decl) = 1; | |
6801 | else | |
6802 | DECL_EXTERNAL (decl) = 0; | |
4ee9c684 | 6803 | TREE_STATIC (decl) = 1; |
6804 | } | |
85c0a25c | 6805 | |
6806 | if (!is_alias) | |
6807 | /* ifuncs are also aliases, so set that attribute too. */ | |
6808 | DECL_ATTRIBUTES (decl) | |
6809 | = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl)); | |
f8e93a2e | 6810 | } |
6811 | else | |
6812 | { | |
9b2d6d13 | 6813 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 6814 | *no_add_attrs = true; |
6815 | } | |
6816 | ||
6817 | return NULL_TREE; | |
6818 | } | |
6819 | ||
85c0a25c | 6820 | /* Handle an "alias" or "ifunc" attribute; arguments as in |
6821 | struct attribute_spec.handler. */ | |
6822 | ||
6823 | static tree | |
6824 | handle_ifunc_attribute (tree *node, tree name, tree args, | |
6825 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6826 | { | |
6827 | return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs); | |
6828 | } | |
6829 | ||
6830 | /* Handle an "alias" or "ifunc" attribute; arguments as in | |
6831 | struct attribute_spec.handler. */ | |
6832 | ||
6833 | static tree | |
6834 | handle_alias_attribute (tree *node, tree name, tree args, | |
6835 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
6836 | { | |
6837 | return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs); | |
6838 | } | |
6839 | ||
f4a30bd7 | 6840 | /* Handle a "weakref" attribute; arguments as in struct |
6841 | attribute_spec.handler. */ | |
6842 | ||
6843 | static tree | |
6844 | handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
6845 | int flags, bool *no_add_attrs) | |
6846 | { | |
6847 | tree attr = NULL_TREE; | |
6848 | ||
83852912 | 6849 | /* We must ignore the attribute when it is associated with |
6850 | local-scoped decls, since attribute alias is ignored and many | |
6851 | such symbols do not even have a DECL_WEAK field. */ | |
64a7bd81 | 6852 | if (decl_function_context (*node) |
6853 | || current_function_decl | |
6854 | || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL)) | |
83852912 | 6855 | { |
6856 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
6857 | *no_add_attrs = true; | |
6858 | return NULL_TREE; | |
6859 | } | |
6860 | ||
85c0a25c | 6861 | if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node))) |
6862 | { | |
6863 | error ("indirect function %q+D cannot be declared weakref", *node); | |
6864 | *no_add_attrs = true; | |
6865 | return NULL_TREE; | |
6866 | } | |
6867 | ||
f4a30bd7 | 6868 | /* The idea here is that `weakref("name")' mutates into `weakref, |
6869 | alias("name")', and weakref without arguments, in turn, | |
6870 | implicitly adds weak. */ | |
6871 | ||
6872 | if (args) | |
6873 | { | |
6874 | attr = tree_cons (get_identifier ("alias"), args, attr); | |
6875 | attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr); | |
6876 | ||
6877 | *no_add_attrs = true; | |
0a3ecdc1 | 6878 | |
6879 | decl_attributes (node, attr, flags); | |
f4a30bd7 | 6880 | } |
6881 | else | |
6882 | { | |
6883 | if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node))) | |
712d2297 | 6884 | error_at (DECL_SOURCE_LOCATION (*node), |
6885 | "weakref attribute must appear before alias attribute"); | |
f4a30bd7 | 6886 | |
0a3ecdc1 | 6887 | /* Can't call declare_weak because it wants this to be TREE_PUBLIC, |
6888 | and that isn't supported; and because it wants to add it to | |
6889 | the list of weak decls, which isn't helpful. */ | |
6890 | DECL_WEAK (*node) = 1; | |
f4a30bd7 | 6891 | } |
6892 | ||
f4a30bd7 | 6893 | return NULL_TREE; |
6894 | } | |
6895 | ||
f8e93a2e | 6896 | /* Handle an "visibility" attribute; arguments as in |
6897 | struct attribute_spec.handler. */ | |
6898 | ||
6899 | static tree | |
1cae46be | 6900 | handle_visibility_attribute (tree *node, tree name, tree args, |
9a03a746 | 6901 | int ARG_UNUSED (flags), |
4a2849cb | 6902 | bool *ARG_UNUSED (no_add_attrs)) |
f8e93a2e | 6903 | { |
6904 | tree decl = *node; | |
9c40570a | 6905 | tree id = TREE_VALUE (args); |
4a2849cb | 6906 | enum symbol_visibility vis; |
f8e93a2e | 6907 | |
b212f378 | 6908 | if (TYPE_P (*node)) |
6909 | { | |
4a2849cb | 6910 | if (TREE_CODE (*node) == ENUMERAL_TYPE) |
6911 | /* OK */; | |
6912 | else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE) | |
6913 | { | |
6914 | warning (OPT_Wattributes, "%qE attribute ignored on non-class types", | |
6915 | name); | |
6916 | return NULL_TREE; | |
6917 | } | |
6918 | else if (TYPE_FIELDS (*node)) | |
6919 | { | |
6920 | error ("%qE attribute ignored because %qT is already defined", | |
6921 | name, *node); | |
6922 | return NULL_TREE; | |
6923 | } | |
b212f378 | 6924 | } |
84166705 | 6925 | else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl)) |
f8e93a2e | 6926 | { |
9b2d6d13 | 6927 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
9c40570a | 6928 | return NULL_TREE; |
f8e93a2e | 6929 | } |
f8e93a2e | 6930 | |
9c40570a | 6931 | if (TREE_CODE (id) != STRING_CST) |
6932 | { | |
07e3a3d2 | 6933 | error ("visibility argument not a string"); |
9c40570a | 6934 | return NULL_TREE; |
f8e93a2e | 6935 | } |
b27ac6b5 | 6936 | |
b212f378 | 6937 | /* If this is a type, set the visibility on the type decl. */ |
6938 | if (TYPE_P (decl)) | |
6939 | { | |
6940 | decl = TYPE_NAME (decl); | |
84166705 | 6941 | if (!decl) |
a0c938f0 | 6942 | return NULL_TREE; |
e147aab3 | 6943 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
6944 | { | |
9b2d6d13 | 6945 | warning (OPT_Wattributes, "%qE attribute ignored on types", |
e147aab3 | 6946 | name); |
6947 | return NULL_TREE; | |
6948 | } | |
b212f378 | 6949 | } |
f8e93a2e | 6950 | |
9c40570a | 6951 | if (strcmp (TREE_STRING_POINTER (id), "default") == 0) |
4a2849cb | 6952 | vis = VISIBILITY_DEFAULT; |
9c40570a | 6953 | else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0) |
4a2849cb | 6954 | vis = VISIBILITY_INTERNAL; |
9c40570a | 6955 | else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0) |
4a2849cb | 6956 | vis = VISIBILITY_HIDDEN; |
9c40570a | 6957 | else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0) |
4a2849cb | 6958 | vis = VISIBILITY_PROTECTED; |
9c40570a | 6959 | else |
4a2849cb | 6960 | { |
6961 | error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\""); | |
6962 | vis = VISIBILITY_DEFAULT; | |
6963 | } | |
6964 | ||
6965 | if (DECL_VISIBILITY_SPECIFIED (decl) | |
098a01e7 | 6966 | && vis != DECL_VISIBILITY (decl)) |
6967 | { | |
6968 | tree attributes = (TYPE_P (*node) | |
6969 | ? TYPE_ATTRIBUTES (*node) | |
6970 | : DECL_ATTRIBUTES (decl)); | |
6971 | if (lookup_attribute ("visibility", attributes)) | |
6972 | error ("%qD redeclared with different visibility", decl); | |
6973 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
6974 | && lookup_attribute ("dllimport", attributes)) | |
6975 | error ("%qD was declared %qs which implies default visibility", | |
6976 | decl, "dllimport"); | |
6977 | else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
6978 | && lookup_attribute ("dllexport", attributes)) | |
6979 | error ("%qD was declared %qs which implies default visibility", | |
6980 | decl, "dllexport"); | |
6981 | } | |
4a2849cb | 6982 | |
6983 | DECL_VISIBILITY (decl) = vis; | |
b212f378 | 6984 | DECL_VISIBILITY_SPECIFIED (decl) = 1; |
6985 | ||
4a2849cb | 6986 | /* Go ahead and attach the attribute to the node as well. This is needed |
6987 | so we can determine whether we have VISIBILITY_DEFAULT because the | |
6988 | visibility was not specified, or because it was explicitly overridden | |
6989 | from the containing scope. */ | |
9c40570a | 6990 | |
f8e93a2e | 6991 | return NULL_TREE; |
6992 | } | |
6993 | ||
3aa0c315 | 6994 | /* Determine the ELF symbol visibility for DECL, which is either a |
6995 | variable or a function. It is an error to use this function if a | |
6996 | definition of DECL is not available in this translation unit. | |
6997 | Returns true if the final visibility has been determined by this | |
6998 | function; false if the caller is free to make additional | |
6999 | modifications. */ | |
7000 | ||
7001 | bool | |
7002 | c_determine_visibility (tree decl) | |
7003 | { | |
1d8fc210 | 7004 | gcc_assert (TREE_CODE (decl) == VAR_DECL |
7005 | || TREE_CODE (decl) == FUNCTION_DECL); | |
3aa0c315 | 7006 | |
7007 | /* If the user explicitly specified the visibility with an | |
7008 | attribute, honor that. DECL_VISIBILITY will have been set during | |
7009 | the processing of the attribute. We check for an explicit | |
7010 | attribute, rather than just checking DECL_VISIBILITY_SPECIFIED, | |
7011 | to distinguish the use of an attribute from the use of a "#pragma | |
7012 | GCC visibility push(...)"; in the latter case we still want other | |
7013 | considerations to be able to overrule the #pragma. */ | |
098a01e7 | 7014 | if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)) |
7015 | || (TARGET_DLLIMPORT_DECL_ATTRIBUTES | |
7016 | && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)) | |
7017 | || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))))) | |
3aa0c315 | 7018 | return true; |
7019 | ||
4a2849cb | 7020 | /* Set default visibility to whatever the user supplied with |
7021 | visibility_specified depending on #pragma GCC visibility. */ | |
7022 | if (!DECL_VISIBILITY_SPECIFIED (decl)) | |
7023 | { | |
2d9d8740 | 7024 | if (visibility_options.inpragma |
7025 | || DECL_VISIBILITY (decl) != default_visibility) | |
7026 | { | |
7027 | DECL_VISIBILITY (decl) = default_visibility; | |
7028 | DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; | |
7029 | /* If visibility changed and DECL already has DECL_RTL, ensure | |
7030 | symbol flags are updated. */ | |
7031 | if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)) | |
7032 | || TREE_CODE (decl) == FUNCTION_DECL) | |
7033 | && DECL_RTL_SET_P (decl)) | |
7034 | make_decl_rtl (decl); | |
7035 | } | |
4a2849cb | 7036 | } |
3aa0c315 | 7037 | return false; |
7038 | } | |
7039 | ||
24dfead4 | 7040 | /* Handle an "tls_model" attribute; arguments as in |
7041 | struct attribute_spec.handler. */ | |
7042 | ||
7043 | static tree | |
1cae46be | 7044 | handle_tls_model_attribute (tree *node, tree name, tree args, |
9a03a746 | 7045 | int ARG_UNUSED (flags), bool *no_add_attrs) |
24dfead4 | 7046 | { |
1b53eb20 | 7047 | tree id; |
24dfead4 | 7048 | tree decl = *node; |
1b53eb20 | 7049 | enum tls_model kind; |
24dfead4 | 7050 | |
1b53eb20 | 7051 | *no_add_attrs = true; |
7052 | ||
c34f8a78 | 7053 | if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl)) |
24dfead4 | 7054 | { |
9b2d6d13 | 7055 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
1b53eb20 | 7056 | return NULL_TREE; |
24dfead4 | 7057 | } |
24dfead4 | 7058 | |
1b53eb20 | 7059 | kind = DECL_TLS_MODEL (decl); |
7060 | id = TREE_VALUE (args); | |
7061 | if (TREE_CODE (id) != STRING_CST) | |
7062 | { | |
7063 | error ("tls_model argument not a string"); | |
7064 | return NULL_TREE; | |
24dfead4 | 7065 | } |
7066 | ||
1b53eb20 | 7067 | if (!strcmp (TREE_STRING_POINTER (id), "local-exec")) |
7068 | kind = TLS_MODEL_LOCAL_EXEC; | |
7069 | else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec")) | |
7070 | kind = TLS_MODEL_INITIAL_EXEC; | |
7071 | else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic")) | |
7072 | kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC; | |
7073 | else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic")) | |
7074 | kind = TLS_MODEL_GLOBAL_DYNAMIC; | |
7075 | else | |
7076 | error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\""); | |
7077 | ||
7078 | DECL_TLS_MODEL (decl) = kind; | |
24dfead4 | 7079 | return NULL_TREE; |
7080 | } | |
7081 | ||
f8e93a2e | 7082 | /* Handle a "no_instrument_function" attribute; arguments as in |
7083 | struct attribute_spec.handler. */ | |
7084 | ||
7085 | static tree | |
1cae46be | 7086 | handle_no_instrument_function_attribute (tree *node, tree name, |
9a03a746 | 7087 | tree ARG_UNUSED (args), |
7088 | int ARG_UNUSED (flags), | |
09347743 | 7089 | bool *no_add_attrs) |
f8e93a2e | 7090 | { |
7091 | tree decl = *node; | |
7092 | ||
7093 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
7094 | { | |
712d2297 | 7095 | error_at (DECL_SOURCE_LOCATION (decl), |
7096 | "%qE attribute applies only to functions", name); | |
f8e93a2e | 7097 | *no_add_attrs = true; |
7098 | } | |
7099 | else if (DECL_INITIAL (decl)) | |
7100 | { | |
712d2297 | 7101 | error_at (DECL_SOURCE_LOCATION (decl), |
7102 | "can%'t set %qE attribute after definition", name); | |
f8e93a2e | 7103 | *no_add_attrs = true; |
7104 | } | |
7105 | else | |
7106 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1; | |
7107 | ||
7108 | return NULL_TREE; | |
7109 | } | |
7110 | ||
7111 | /* Handle a "malloc" attribute; arguments as in | |
7112 | struct attribute_spec.handler. */ | |
7113 | ||
7114 | static tree | |
9a03a746 | 7115 | handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
7116 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7117 | { |
a5147fca | 7118 | if (TREE_CODE (*node) == FUNCTION_DECL |
7119 | && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))) | |
f8e93a2e | 7120 | DECL_IS_MALLOC (*node) = 1; |
f8e93a2e | 7121 | else |
7122 | { | |
9b2d6d13 | 7123 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7124 | *no_add_attrs = true; |
7125 | } | |
7126 | ||
7127 | return NULL_TREE; | |
7128 | } | |
7129 | ||
4a29c97c | 7130 | /* Handle a "alloc_size" attribute; arguments as in |
7131 | struct attribute_spec.handler. */ | |
7132 | ||
7133 | static tree | |
7134 | handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args, | |
7135 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7136 | { | |
2802826e | 7137 | unsigned arg_count = type_num_arguments (*node); |
4a29c97c | 7138 | for (; args; args = TREE_CHAIN (args)) |
7139 | { | |
7140 | tree position = TREE_VALUE (args); | |
7141 | ||
7142 | if (TREE_CODE (position) != INTEGER_CST | |
48e1416a | 7143 | || TREE_INT_CST_HIGH (position) |
4a29c97c | 7144 | || TREE_INT_CST_LOW (position) < 1 |
7145 | || TREE_INT_CST_LOW (position) > arg_count ) | |
7146 | { | |
48e1416a | 7147 | warning (OPT_Wattributes, |
4a29c97c | 7148 | "alloc_size parameter outside range"); |
7149 | *no_add_attrs = true; | |
7150 | return NULL_TREE; | |
7151 | } | |
7152 | } | |
7153 | return NULL_TREE; | |
7154 | } | |
7155 | ||
8ce86007 | 7156 | /* Handle a "fn spec" attribute; arguments as in |
7157 | struct attribute_spec.handler. */ | |
7158 | ||
7159 | static tree | |
7160 | handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name), | |
7161 | tree args, int ARG_UNUSED (flags), | |
7162 | bool *no_add_attrs ATTRIBUTE_UNUSED) | |
7163 | { | |
7164 | gcc_assert (args | |
7165 | && TREE_CODE (TREE_VALUE (args)) == STRING_CST | |
7166 | && !TREE_CHAIN (args)); | |
7167 | return NULL_TREE; | |
7168 | } | |
7169 | ||
26d1c5ff | 7170 | /* Handle a "returns_twice" attribute; arguments as in |
7171 | struct attribute_spec.handler. */ | |
7172 | ||
7173 | static tree | |
7174 | handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args), | |
7175 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7176 | { | |
7177 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
7178 | DECL_IS_RETURNS_TWICE (*node) = 1; | |
7179 | else | |
7180 | { | |
9b2d6d13 | 7181 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
26d1c5ff | 7182 | *no_add_attrs = true; |
7183 | } | |
7184 | ||
7185 | return NULL_TREE; | |
7186 | } | |
7187 | ||
f8e93a2e | 7188 | /* Handle a "no_limit_stack" attribute; arguments as in |
7189 | struct attribute_spec.handler. */ | |
7190 | ||
7191 | static tree | |
1cae46be | 7192 | handle_no_limit_stack_attribute (tree *node, tree name, |
9a03a746 | 7193 | tree ARG_UNUSED (args), |
7194 | int ARG_UNUSED (flags), | |
09347743 | 7195 | bool *no_add_attrs) |
f8e93a2e | 7196 | { |
7197 | tree decl = *node; | |
7198 | ||
7199 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
7200 | { | |
712d2297 | 7201 | error_at (DECL_SOURCE_LOCATION (decl), |
7202 | "%qE attribute applies only to functions", name); | |
f8e93a2e | 7203 | *no_add_attrs = true; |
7204 | } | |
7205 | else if (DECL_INITIAL (decl)) | |
7206 | { | |
712d2297 | 7207 | error_at (DECL_SOURCE_LOCATION (decl), |
7208 | "can%'t set %qE attribute after definition", name); | |
f8e93a2e | 7209 | *no_add_attrs = true; |
7210 | } | |
7211 | else | |
7212 | DECL_NO_LIMIT_STACK (decl) = 1; | |
7213 | ||
7214 | return NULL_TREE; | |
7215 | } | |
7216 | ||
7217 | /* Handle a "pure" attribute; arguments as in | |
7218 | struct attribute_spec.handler. */ | |
7219 | ||
7220 | static tree | |
9a03a746 | 7221 | handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
7222 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
f8e93a2e | 7223 | { |
7224 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
9c2a0c05 | 7225 | DECL_PURE_P (*node) = 1; |
f8e93a2e | 7226 | /* ??? TODO: Support types. */ |
7227 | else | |
7228 | { | |
9b2d6d13 | 7229 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7230 | *no_add_attrs = true; |
7231 | } | |
7232 | ||
7233 | return NULL_TREE; | |
7234 | } | |
7235 | ||
fc09b200 | 7236 | /* Handle a "no vops" attribute; arguments as in |
7237 | struct attribute_spec.handler. */ | |
7238 | ||
7239 | static tree | |
7240 | handle_novops_attribute (tree *node, tree ARG_UNUSED (name), | |
7241 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
7242 | bool *ARG_UNUSED (no_add_attrs)) | |
7243 | { | |
7244 | gcc_assert (TREE_CODE (*node) == FUNCTION_DECL); | |
7245 | DECL_IS_NOVOPS (*node) = 1; | |
7246 | return NULL_TREE; | |
7247 | } | |
7248 | ||
f8e93a2e | 7249 | /* Handle a "deprecated" attribute; arguments as in |
7250 | struct attribute_spec.handler. */ | |
1cae46be | 7251 | |
f8e93a2e | 7252 | static tree |
1cae46be | 7253 | handle_deprecated_attribute (tree *node, tree name, |
45c4e798 | 7254 | tree args, int flags, |
09347743 | 7255 | bool *no_add_attrs) |
f8e93a2e | 7256 | { |
7257 | tree type = NULL_TREE; | |
7258 | int warn = 0; | |
782858b8 | 7259 | tree what = NULL_TREE; |
1cae46be | 7260 | |
45c4e798 | 7261 | if (!args) |
7262 | *no_add_attrs = true; | |
7263 | else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST) | |
7264 | { | |
7265 | error ("deprecated message is not a string"); | |
7266 | *no_add_attrs = true; | |
7267 | } | |
7268 | ||
f8e93a2e | 7269 | if (DECL_P (*node)) |
7270 | { | |
7271 | tree decl = *node; | |
7272 | type = TREE_TYPE (decl); | |
1cae46be | 7273 | |
f8e93a2e | 7274 | if (TREE_CODE (decl) == TYPE_DECL |
7275 | || TREE_CODE (decl) == PARM_DECL | |
7276 | || TREE_CODE (decl) == VAR_DECL | |
7277 | || TREE_CODE (decl) == FUNCTION_DECL | |
40c8d1dd | 7278 | || TREE_CODE (decl) == FIELD_DECL |
7279 | || objc_method_decl (TREE_CODE (decl))) | |
f8e93a2e | 7280 | TREE_DEPRECATED (decl) = 1; |
7281 | else | |
7282 | warn = 1; | |
7283 | } | |
7284 | else if (TYPE_P (*node)) | |
7285 | { | |
7286 | if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE)) | |
e086912e | 7287 | *node = build_variant_type_copy (*node); |
f8e93a2e | 7288 | TREE_DEPRECATED (*node) = 1; |
7289 | type = *node; | |
7290 | } | |
7291 | else | |
7292 | warn = 1; | |
1cae46be | 7293 | |
f8e93a2e | 7294 | if (warn) |
7295 | { | |
7296 | *no_add_attrs = true; | |
7297 | if (type && TYPE_NAME (type)) | |
7298 | { | |
7299 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) | |
782858b8 | 7300 | what = TYPE_NAME (*node); |
f8e93a2e | 7301 | else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL |
7302 | && DECL_NAME (TYPE_NAME (type))) | |
782858b8 | 7303 | what = DECL_NAME (TYPE_NAME (type)); |
f8e93a2e | 7304 | } |
7305 | if (what) | |
9b2d6d13 | 7306 | warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what); |
f8e93a2e | 7307 | else |
9b2d6d13 | 7308 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7309 | } |
7310 | ||
7311 | return NULL_TREE; | |
7312 | } | |
7313 | ||
f8e93a2e | 7314 | /* Handle a "vector_size" attribute; arguments as in |
7315 | struct attribute_spec.handler. */ | |
7316 | ||
7317 | static tree | |
1cae46be | 7318 | handle_vector_size_attribute (tree *node, tree name, tree args, |
9a03a746 | 7319 | int ARG_UNUSED (flags), |
09347743 | 7320 | bool *no_add_attrs) |
f8e93a2e | 7321 | { |
7322 | unsigned HOST_WIDE_INT vecsize, nunits; | |
83e2a11b | 7323 | enum machine_mode orig_mode; |
4917c376 | 7324 | tree type = *node, new_type, size; |
f8e93a2e | 7325 | |
7326 | *no_add_attrs = true; | |
7327 | ||
4917c376 | 7328 | size = TREE_VALUE (args); |
4917c376 | 7329 | |
84166705 | 7330 | if (!host_integerp (size, 1)) |
f8e93a2e | 7331 | { |
9b2d6d13 | 7332 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
f8e93a2e | 7333 | return NULL_TREE; |
7334 | } | |
7335 | ||
7336 | /* Get the vector size (in bytes). */ | |
4917c376 | 7337 | vecsize = tree_low_cst (size, 1); |
f8e93a2e | 7338 | |
7339 | /* We need to provide for vector pointers, vector arrays, and | |
7340 | functions returning vectors. For example: | |
7341 | ||
7342 | __attribute__((vector_size(16))) short *foo; | |
7343 | ||
7344 | In this case, the mode is SI, but the type being modified is | |
7345 | HI, so we need to look further. */ | |
7346 | ||
7347 | while (POINTER_TYPE_P (type) | |
7348 | || TREE_CODE (type) == FUNCTION_TYPE | |
5bfb0742 | 7349 | || TREE_CODE (type) == METHOD_TYPE |
2cb0e5d9 | 7350 | || TREE_CODE (type) == ARRAY_TYPE |
7351 | || TREE_CODE (type) == OFFSET_TYPE) | |
f8e93a2e | 7352 | type = TREE_TYPE (type); |
7353 | ||
7354 | /* Get the mode of the type being modified. */ | |
7355 | orig_mode = TYPE_MODE (type); | |
7356 | ||
2cb0e5d9 | 7357 | if ((!INTEGRAL_TYPE_P (type) |
7358 | && !SCALAR_FLOAT_TYPE_P (type) | |
7359 | && !FIXED_POINT_TYPE_P (type)) | |
cee7491d | 7360 | || (!SCALAR_FLOAT_MODE_P (orig_mode) |
9421ebb9 | 7361 | && GET_MODE_CLASS (orig_mode) != MODE_INT |
7362 | && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode)) | |
7ec31215 | 7363 | || !host_integerp (TYPE_SIZE_UNIT (type), 1) |
7364 | || TREE_CODE (type) == BOOLEAN_TYPE) | |
f8e93a2e | 7365 | { |
782858b8 | 7366 | error ("invalid vector type for attribute %qE", name); |
f8e93a2e | 7367 | return NULL_TREE; |
7368 | } | |
7369 | ||
39cc3e6d | 7370 | if (vecsize % tree_low_cst (TYPE_SIZE_UNIT (type), 1)) |
7371 | { | |
7372 | error ("vector size not an integral multiple of component size"); | |
7373 | return NULL; | |
7374 | } | |
7375 | ||
7376 | if (vecsize == 0) | |
7377 | { | |
7378 | error ("zero vector size"); | |
7379 | return NULL; | |
7380 | } | |
7381 | ||
f8e93a2e | 7382 | /* Calculate how many units fit in the vector. */ |
7383 | nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1); | |
83e2a11b | 7384 | if (nunits & (nunits - 1)) |
f8e93a2e | 7385 | { |
83e2a11b | 7386 | error ("number of components of the vector not a power of two"); |
f8e93a2e | 7387 | return NULL_TREE; |
7388 | } | |
7389 | ||
83e2a11b | 7390 | new_type = build_vector_type (type, nunits); |
f8e93a2e | 7391 | |
7392 | /* Build back pointers if needed. */ | |
d991e6e8 | 7393 | *node = lang_hooks.types.reconstruct_complex_type (*node, new_type); |
f8e93a2e | 7394 | |
7395 | return NULL_TREE; | |
7396 | } | |
7397 | ||
dbf6c367 | 7398 | /* Handle the "nonnull" attribute. */ |
7399 | static tree | |
9a03a746 | 7400 | handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name), |
7401 | tree args, int ARG_UNUSED (flags), | |
09347743 | 7402 | bool *no_add_attrs) |
dbf6c367 | 7403 | { |
7404 | tree type = *node; | |
7405 | unsigned HOST_WIDE_INT attr_arg_num; | |
7406 | ||
7407 | /* If no arguments are specified, all pointer arguments should be | |
d716ce75 | 7408 | non-null. Verify a full prototype is given so that the arguments |
dbf6c367 | 7409 | will have the correct types when we actually check them later. */ |
84166705 | 7410 | if (!args) |
dbf6c367 | 7411 | { |
a36cf284 | 7412 | if (!prototype_p (type)) |
dbf6c367 | 7413 | { |
7414 | error ("nonnull attribute without arguments on a non-prototype"); | |
4ee9c684 | 7415 | *no_add_attrs = true; |
dbf6c367 | 7416 | } |
7417 | return NULL_TREE; | |
7418 | } | |
7419 | ||
7420 | /* Argument list specified. Verify that each argument number references | |
7421 | a pointer argument. */ | |
7422 | for (attr_arg_num = 1; args; args = TREE_CHAIN (args)) | |
7423 | { | |
4ee9c684 | 7424 | unsigned HOST_WIDE_INT arg_num = 0, ck_num; |
dbf6c367 | 7425 | |
84166705 | 7426 | if (!get_nonnull_operand (TREE_VALUE (args), &arg_num)) |
dbf6c367 | 7427 | { |
07e3a3d2 | 7428 | error ("nonnull argument has invalid operand number (argument %lu)", |
dbf6c367 | 7429 | (unsigned long) attr_arg_num); |
7430 | *no_add_attrs = true; | |
7431 | return NULL_TREE; | |
7432 | } | |
7433 | ||
d0af78c5 | 7434 | if (prototype_p (type)) |
dbf6c367 | 7435 | { |
d0af78c5 | 7436 | function_args_iterator iter; |
7437 | tree argument; | |
7438 | ||
7439 | function_args_iter_init (&iter, type); | |
7440 | for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter)) | |
dbf6c367 | 7441 | { |
d0af78c5 | 7442 | argument = function_args_iter_cond (&iter); |
7443 | if (argument == NULL_TREE || ck_num == arg_num) | |
dbf6c367 | 7444 | break; |
dbf6c367 | 7445 | } |
7446 | ||
84166705 | 7447 | if (!argument |
d0af78c5 | 7448 | || TREE_CODE (argument) == VOID_TYPE) |
dbf6c367 | 7449 | { |
07e3a3d2 | 7450 | error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)", |
dbf6c367 | 7451 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
7452 | *no_add_attrs = true; | |
7453 | return NULL_TREE; | |
7454 | } | |
7455 | ||
d0af78c5 | 7456 | if (TREE_CODE (argument) != POINTER_TYPE) |
dbf6c367 | 7457 | { |
07e3a3d2 | 7458 | error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)", |
dbf6c367 | 7459 | (unsigned long) attr_arg_num, (unsigned long) arg_num); |
7460 | *no_add_attrs = true; | |
7461 | return NULL_TREE; | |
7462 | } | |
7463 | } | |
7464 | } | |
7465 | ||
7466 | return NULL_TREE; | |
7467 | } | |
7468 | ||
7469 | /* Check the argument list of a function call for null in argument slots | |
d01f58f9 | 7470 | that are marked as requiring a non-null pointer argument. The NARGS |
7471 | arguments are passed in the array ARGARRAY. | |
7472 | */ | |
dbf6c367 | 7473 | |
7474 | static void | |
d01f58f9 | 7475 | check_function_nonnull (tree attrs, int nargs, tree *argarray) |
dbf6c367 | 7476 | { |
d01f58f9 | 7477 | tree a, args; |
7478 | int i; | |
dbf6c367 | 7479 | |
7480 | for (a = attrs; a; a = TREE_CHAIN (a)) | |
7481 | { | |
7482 | if (is_attribute_p ("nonnull", TREE_PURPOSE (a))) | |
7483 | { | |
4ee9c684 | 7484 | args = TREE_VALUE (a); |
dbf6c367 | 7485 | |
4ee9c684 | 7486 | /* Walk the argument list. If we encounter an argument number we |
7487 | should check for non-null, do it. If the attribute has no args, | |
7488 | then every pointer argument is checked (in which case the check | |
dbf6c367 | 7489 | for pointer type is done in check_nonnull_arg). */ |
d01f58f9 | 7490 | for (i = 0; i < nargs; i++) |
4ee9c684 | 7491 | { |
d01f58f9 | 7492 | if (!args || nonnull_check_p (args, i + 1)) |
e4603171 | 7493 | check_function_arguments_recurse (check_nonnull_arg, NULL, |
d01f58f9 | 7494 | argarray[i], |
7495 | i + 1); | |
4ee9c684 | 7496 | } |
dbf6c367 | 7497 | } |
7498 | } | |
7499 | } | |
7500 | ||
50ca527f | 7501 | /* Check that the Nth argument of a function call (counting backwards |
d01f58f9 | 7502 | from the end) is a (pointer)0. The NARGS arguments are passed in the |
7503 | array ARGARRAY. */ | |
bf6c8de0 | 7504 | |
7505 | static void | |
774e9d58 | 7506 | check_function_sentinel (const_tree fntype, int nargs, tree *argarray) |
bf6c8de0 | 7507 | { |
774e9d58 | 7508 | tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype)); |
bf6c8de0 | 7509 | |
7510 | if (attr) | |
7511 | { | |
d01f58f9 | 7512 | int len = 0; |
7513 | int pos = 0; | |
7514 | tree sentinel; | |
774e9d58 | 7515 | function_args_iterator iter; |
7516 | tree t; | |
a0c938f0 | 7517 | |
d01f58f9 | 7518 | /* Skip over the named arguments. */ |
774e9d58 | 7519 | FOREACH_FUNCTION_ARGS (fntype, t, iter) |
a0c938f0 | 7520 | { |
774e9d58 | 7521 | if (len == nargs) |
7522 | break; | |
d01f58f9 | 7523 | len++; |
7524 | } | |
50ca527f | 7525 | |
d01f58f9 | 7526 | if (TREE_VALUE (attr)) |
7527 | { | |
7528 | tree p = TREE_VALUE (TREE_VALUE (attr)); | |
7529 | pos = TREE_INT_CST_LOW (p); | |
7530 | } | |
50ca527f | 7531 | |
d01f58f9 | 7532 | /* The sentinel must be one of the varargs, i.e. |
7533 | in position >= the number of fixed arguments. */ | |
7534 | if ((nargs - 1 - pos) < len) | |
7535 | { | |
7536 | warning (OPT_Wformat, | |
7537 | "not enough variable arguments to fit a sentinel"); | |
7538 | return; | |
bf6c8de0 | 7539 | } |
d01f58f9 | 7540 | |
7541 | /* Validate the sentinel. */ | |
7542 | sentinel = argarray[nargs - 1 - pos]; | |
7543 | if ((!POINTER_TYPE_P (TREE_TYPE (sentinel)) | |
7544 | || !integer_zerop (sentinel)) | |
7545 | /* Although __null (in C++) is only an integer we allow it | |
7546 | nevertheless, as we are guaranteed that it's exactly | |
7547 | as wide as a pointer, and we don't want to force | |
7548 | users to cast the NULL they have written there. | |
7549 | We warn with -Wstrict-null-sentinel, though. */ | |
7550 | && (warn_strict_null_sentinel || null_node != sentinel)) | |
7551 | warning (OPT_Wformat, "missing sentinel in function call"); | |
bf6c8de0 | 7552 | } |
7553 | } | |
7554 | ||
dbf6c367 | 7555 | /* Helper for check_function_nonnull; given a list of operands which |
7556 | must be non-null in ARGS, determine if operand PARAM_NUM should be | |
7557 | checked. */ | |
7558 | ||
7559 | static bool | |
1cae46be | 7560 | nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 7561 | { |
4ee9c684 | 7562 | unsigned HOST_WIDE_INT arg_num = 0; |
dbf6c367 | 7563 | |
7564 | for (; args; args = TREE_CHAIN (args)) | |
7565 | { | |
231bd014 | 7566 | bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num); |
7567 | ||
7568 | gcc_assert (found); | |
dbf6c367 | 7569 | |
7570 | if (arg_num == param_num) | |
7571 | return true; | |
7572 | } | |
7573 | return false; | |
7574 | } | |
7575 | ||
7576 | /* Check that the function argument PARAM (which is operand number | |
7577 | PARAM_NUM) is non-null. This is called by check_function_nonnull | |
7578 | via check_function_arguments_recurse. */ | |
7579 | ||
7580 | static void | |
9a03a746 | 7581 | check_nonnull_arg (void * ARG_UNUSED (ctx), tree param, |
1cae46be | 7582 | unsigned HOST_WIDE_INT param_num) |
dbf6c367 | 7583 | { |
7584 | /* Just skip checking the argument if it's not a pointer. This can | |
7585 | happen if the "nonnull" attribute was given without an operand | |
7586 | list (which means to check every pointer argument). */ | |
7587 | ||
7588 | if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) | |
7589 | return; | |
7590 | ||
7591 | if (integer_zerop (param)) | |
155b601b | 7592 | warning (OPT_Wnonnull, "null argument where non-null required " |
7593 | "(argument %lu)", (unsigned long) param_num); | |
dbf6c367 | 7594 | } |
7595 | ||
7596 | /* Helper for nonnull attribute handling; fetch the operand number | |
7597 | from the attribute argument list. */ | |
7598 | ||
7599 | static bool | |
1cae46be | 7600 | get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) |
dbf6c367 | 7601 | { |
67409385 | 7602 | /* Verify the arg number is a constant. */ |
dbf6c367 | 7603 | if (TREE_CODE (arg_num_expr) != INTEGER_CST |
7604 | || TREE_INT_CST_HIGH (arg_num_expr) != 0) | |
7605 | return false; | |
7606 | ||
7607 | *valp = TREE_INT_CST_LOW (arg_num_expr); | |
7608 | return true; | |
7609 | } | |
fa987697 | 7610 | |
7611 | /* Handle a "nothrow" attribute; arguments as in | |
7612 | struct attribute_spec.handler. */ | |
7613 | ||
7614 | static tree | |
9a03a746 | 7615 | handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args), |
7616 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
fa987697 | 7617 | { |
7618 | if (TREE_CODE (*node) == FUNCTION_DECL) | |
7619 | TREE_NOTHROW (*node) = 1; | |
7620 | /* ??? TODO: Support types. */ | |
7621 | else | |
7622 | { | |
9b2d6d13 | 7623 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
fa987697 | 7624 | *no_add_attrs = true; |
7625 | } | |
7626 | ||
7627 | return NULL_TREE; | |
7628 | } | |
7acb29a3 | 7629 | |
7630 | /* Handle a "cleanup" attribute; arguments as in | |
7631 | struct attribute_spec.handler. */ | |
7632 | ||
7633 | static tree | |
1cae46be | 7634 | handle_cleanup_attribute (tree *node, tree name, tree args, |
9a03a746 | 7635 | int ARG_UNUSED (flags), bool *no_add_attrs) |
7acb29a3 | 7636 | { |
7637 | tree decl = *node; | |
7638 | tree cleanup_id, cleanup_decl; | |
7639 | ||
7640 | /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do | |
7641 | for global destructors in C++. This requires infrastructure that | |
7642 | we don't have generically at the moment. It's also not a feature | |
7643 | we'd be missing too much, since we do have attribute constructor. */ | |
7644 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
7645 | { | |
9b2d6d13 | 7646 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
7acb29a3 | 7647 | *no_add_attrs = true; |
7648 | return NULL_TREE; | |
7649 | } | |
7650 | ||
7651 | /* Verify that the argument is a function in scope. */ | |
7652 | /* ??? We could support pointers to functions here as well, if | |
7653 | that was considered desirable. */ | |
7654 | cleanup_id = TREE_VALUE (args); | |
7655 | if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE) | |
7656 | { | |
07e3a3d2 | 7657 | error ("cleanup argument not an identifier"); |
7acb29a3 | 7658 | *no_add_attrs = true; |
7659 | return NULL_TREE; | |
7660 | } | |
d1c41717 | 7661 | cleanup_decl = lookup_name (cleanup_id); |
7acb29a3 | 7662 | if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL) |
7663 | { | |
07e3a3d2 | 7664 | error ("cleanup argument not a function"); |
7acb29a3 | 7665 | *no_add_attrs = true; |
7666 | return NULL_TREE; | |
7667 | } | |
7668 | ||
1cae46be | 7669 | /* That the function has proper type is checked with the |
7acb29a3 | 7670 | eventual call to build_function_call. */ |
7671 | ||
7672 | return NULL_TREE; | |
7673 | } | |
8a8cdb8d | 7674 | |
7675 | /* Handle a "warn_unused_result" attribute. No special handling. */ | |
7676 | ||
7677 | static tree | |
7678 | handle_warn_unused_result_attribute (tree *node, tree name, | |
9a03a746 | 7679 | tree ARG_UNUSED (args), |
7680 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
8a8cdb8d | 7681 | { |
7682 | /* Ignore the attribute for functions not returning any value. */ | |
7683 | if (VOID_TYPE_P (TREE_TYPE (*node))) | |
7684 | { | |
9b2d6d13 | 7685 | warning (OPT_Wattributes, "%qE attribute ignored", name); |
8a8cdb8d | 7686 | *no_add_attrs = true; |
7687 | } | |
7688 | ||
7689 | return NULL_TREE; | |
7690 | } | |
bf6c8de0 | 7691 | |
7692 | /* Handle a "sentinel" attribute. */ | |
7693 | ||
7694 | static tree | |
50ca527f | 7695 | handle_sentinel_attribute (tree *node, tree name, tree args, |
bf6c8de0 | 7696 | int ARG_UNUSED (flags), bool *no_add_attrs) |
7697 | { | |
a36cf284 | 7698 | if (!prototype_p (*node)) |
bf6c8de0 | 7699 | { |
9b2d6d13 | 7700 | warning (OPT_Wattributes, |
7701 | "%qE attribute requires prototypes with named arguments", name); | |
bf6c8de0 | 7702 | *no_add_attrs = true; |
bf6c8de0 | 7703 | } |
50ca527f | 7704 | else |
7705 | { | |
c33080b9 | 7706 | if (!stdarg_p (*node)) |
a0c938f0 | 7707 | { |
9b2d6d13 | 7708 | warning (OPT_Wattributes, |
7709 | "%qE attribute only applies to variadic functions", name); | |
50ca527f | 7710 | *no_add_attrs = true; |
7711 | } | |
7712 | } | |
a0c938f0 | 7713 | |
50ca527f | 7714 | if (args) |
bf6c8de0 | 7715 | { |
50ca527f | 7716 | tree position = TREE_VALUE (args); |
7717 | ||
50ca527f | 7718 | if (TREE_CODE (position) != INTEGER_CST) |
a0c938f0 | 7719 | { |
48e1416a | 7720 | warning (OPT_Wattributes, |
01b54db5 | 7721 | "requested position is not an integer constant"); |
50ca527f | 7722 | *no_add_attrs = true; |
7723 | } | |
7724 | else | |
a0c938f0 | 7725 | { |
50ca527f | 7726 | if (tree_int_cst_lt (position, integer_zero_node)) |
7727 | { | |
01b54db5 | 7728 | warning (OPT_Wattributes, |
7729 | "requested position is less than zero"); | |
50ca527f | 7730 | *no_add_attrs = true; |
7731 | } | |
7732 | } | |
bf6c8de0 | 7733 | } |
a0c938f0 | 7734 | |
bf6c8de0 | 7735 | return NULL_TREE; |
7736 | } | |
b5c26b42 | 7737 | |
7738 | /* Handle a "type_generic" attribute. */ | |
7739 | ||
7740 | static tree | |
7741 | handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name), | |
7742 | tree ARG_UNUSED (args), int ARG_UNUSED (flags), | |
7743 | bool * ARG_UNUSED (no_add_attrs)) | |
7744 | { | |
19fbe3a4 | 7745 | /* Ensure we have a function type. */ |
7746 | gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE); | |
48e1416a | 7747 | |
19fbe3a4 | 7748 | /* Ensure we have a variadic function. */ |
c33080b9 | 7749 | gcc_assert (!prototype_p (*node) || stdarg_p (*node)); |
b5c26b42 | 7750 | |
7751 | return NULL_TREE; | |
7752 | } | |
46f8e3b0 | 7753 | |
24470055 | 7754 | /* Handle a "target" attribute. */ |
46f8e3b0 | 7755 | |
7756 | static tree | |
24470055 | 7757 | handle_target_attribute (tree *node, tree name, tree args, int flags, |
46f8e3b0 | 7758 | bool *no_add_attrs) |
7759 | { | |
7760 | /* Ensure we have a function type. */ | |
7761 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
7762 | { | |
7763 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7764 | *no_add_attrs = true; | |
7765 | } | |
46f8e3b0 | 7766 | else if (! targetm.target_option.valid_attribute_p (*node, name, args, |
7767 | flags)) | |
7768 | *no_add_attrs = true; | |
7769 | ||
7770 | return NULL_TREE; | |
7771 | } | |
7772 | ||
7773 | /* Arguments being collected for optimization. */ | |
7774 | typedef const char *const_char_p; /* For DEF_VEC_P. */ | |
7775 | DEF_VEC_P(const_char_p); | |
7776 | DEF_VEC_ALLOC_P(const_char_p, gc); | |
7777 | static GTY(()) VEC(const_char_p, gc) *optimize_args; | |
7778 | ||
7779 | ||
7780 | /* Inner function to convert a TREE_LIST to argv string to parse the optimize | |
7781 | options in ARGS. ATTR_P is true if this is for attribute(optimize), and | |
7782 | false for #pragma GCC optimize. */ | |
7783 | ||
7784 | bool | |
7785 | parse_optimize_options (tree args, bool attr_p) | |
7786 | { | |
7787 | bool ret = true; | |
7788 | unsigned opt_argc; | |
7789 | unsigned i; | |
2becf397 | 7790 | int saved_flag_strict_aliasing; |
46f8e3b0 | 7791 | const char **opt_argv; |
615ef0bb | 7792 | struct cl_decoded_option *decoded_options; |
7793 | unsigned int decoded_options_count; | |
46f8e3b0 | 7794 | tree ap; |
7795 | ||
7796 | /* Build up argv vector. Just in case the string is stored away, use garbage | |
7797 | collected strings. */ | |
7798 | VEC_truncate (const_char_p, optimize_args, 0); | |
7799 | VEC_safe_push (const_char_p, gc, optimize_args, NULL); | |
7800 | ||
7801 | for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap)) | |
7802 | { | |
7803 | tree value = TREE_VALUE (ap); | |
7804 | ||
7805 | if (TREE_CODE (value) == INTEGER_CST) | |
7806 | { | |
7807 | char buffer[20]; | |
7808 | sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value)); | |
7809 | VEC_safe_push (const_char_p, gc, optimize_args, ggc_strdup (buffer)); | |
7810 | } | |
7811 | ||
7812 | else if (TREE_CODE (value) == STRING_CST) | |
7813 | { | |
7814 | /* Split string into multiple substrings. */ | |
7815 | size_t len = TREE_STRING_LENGTH (value); | |
7816 | char *p = ASTRDUP (TREE_STRING_POINTER (value)); | |
7817 | char *end = p + len; | |
7818 | char *comma; | |
7819 | char *next_p = p; | |
7820 | ||
7821 | while (next_p != NULL) | |
7822 | { | |
7823 | size_t len2; | |
7824 | char *q, *r; | |
7825 | ||
7826 | p = next_p; | |
7827 | comma = strchr (p, ','); | |
7828 | if (comma) | |
7829 | { | |
7830 | len2 = comma - p; | |
7831 | *comma = '\0'; | |
7832 | next_p = comma+1; | |
7833 | } | |
7834 | else | |
7835 | { | |
7836 | len2 = end - p; | |
7837 | next_p = NULL; | |
7838 | } | |
7839 | ||
ba72912a | 7840 | r = q = (char *) ggc_alloc_atomic (len2 + 3); |
46f8e3b0 | 7841 | |
7842 | /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx | |
7843 | options. */ | |
7844 | if (*p == '-' && p[1] != 'O' && p[1] != 'f') | |
7845 | { | |
7846 | ret = false; | |
7847 | if (attr_p) | |
7848 | warning (OPT_Wattributes, | |
ebd7c4c1 | 7849 | "bad option %s to optimize attribute", p); |
46f8e3b0 | 7850 | else |
7851 | warning (OPT_Wpragmas, | |
e44b0a1f | 7852 | "bad option %s to pragma attribute", p); |
46f8e3b0 | 7853 | continue; |
7854 | } | |
7855 | ||
7856 | if (*p != '-') | |
7857 | { | |
7858 | *r++ = '-'; | |
7859 | ||
7860 | /* Assume that Ox is -Ox, a numeric value is -Ox, a s by | |
7861 | itself is -Os, and any other switch begins with a -f. */ | |
7862 | if ((*p >= '0' && *p <= '9') | |
7863 | || (p[0] == 's' && p[1] == '\0')) | |
7864 | *r++ = 'O'; | |
7865 | else if (*p != 'O') | |
7866 | *r++ = 'f'; | |
7867 | } | |
7868 | ||
7869 | memcpy (r, p, len2); | |
7870 | r[len2] = '\0'; | |
7871 | VEC_safe_push (const_char_p, gc, optimize_args, q); | |
7872 | } | |
7873 | ||
7874 | } | |
7875 | } | |
7876 | ||
7877 | opt_argc = VEC_length (const_char_p, optimize_args); | |
7878 | opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1)); | |
7879 | ||
7880 | for (i = 1; i < opt_argc; i++) | |
7881 | opt_argv[i] = VEC_index (const_char_p, optimize_args, i); | |
7882 | ||
2becf397 | 7883 | saved_flag_strict_aliasing = flag_strict_aliasing; |
7884 | ||
46f8e3b0 | 7885 | /* Now parse the options. */ |
f3f006ad | 7886 | decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv, |
7887 | &decoded_options, | |
7888 | &decoded_options_count); | |
7889 | decode_options (&global_options, &global_options_set, | |
3c6c0e40 | 7890 | decoded_options, decoded_options_count, |
7891 | input_location, global_dc); | |
46f8e3b0 | 7892 | |
4bec06b3 | 7893 | targetm.override_options_after_change(); |
7894 | ||
2becf397 | 7895 | /* Don't allow changing -fstrict-aliasing. */ |
7896 | flag_strict_aliasing = saved_flag_strict_aliasing; | |
7897 | ||
46f8e3b0 | 7898 | VEC_truncate (const_char_p, optimize_args, 0); |
7899 | return ret; | |
7900 | } | |
7901 | ||
7902 | /* For handling "optimize" attribute. arguments as in | |
7903 | struct attribute_spec.handler. */ | |
7904 | ||
7905 | static tree | |
7906 | handle_optimize_attribute (tree *node, tree name, tree args, | |
7907 | int ARG_UNUSED (flags), bool *no_add_attrs) | |
7908 | { | |
7909 | /* Ensure we have a function type. */ | |
7910 | if (TREE_CODE (*node) != FUNCTION_DECL) | |
7911 | { | |
7912 | warning (OPT_Wattributes, "%qE attribute ignored", name); | |
7913 | *no_add_attrs = true; | |
7914 | } | |
7915 | else | |
7916 | { | |
7917 | struct cl_optimization cur_opts; | |
7918 | tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node); | |
7919 | ||
7920 | /* Save current options. */ | |
2c5d2e39 | 7921 | cl_optimization_save (&cur_opts, &global_options); |
46f8e3b0 | 7922 | |
7923 | /* If we previously had some optimization options, use them as the | |
7924 | default. */ | |
7925 | if (old_opts) | |
2c5d2e39 | 7926 | cl_optimization_restore (&global_options, |
7927 | TREE_OPTIMIZATION (old_opts)); | |
46f8e3b0 | 7928 | |
7929 | /* Parse options, and update the vector. */ | |
7930 | parse_optimize_options (args, true); | |
7931 | DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node) | |
7932 | = build_optimization_node (); | |
7933 | ||
7934 | /* Restore current options. */ | |
2c5d2e39 | 7935 | cl_optimization_restore (&global_options, &cur_opts); |
46f8e3b0 | 7936 | } |
7937 | ||
7938 | return NULL_TREE; | |
7939 | } | |
48b14f50 | 7940 | |
7941 | /* Handle a "no_split_stack" attribute. */ | |
7942 | ||
7943 | static tree | |
7944 | handle_no_split_stack_attribute (tree *node, tree name, | |
7945 | tree ARG_UNUSED (args), | |
7946 | int ARG_UNUSED (flags), | |
7947 | bool *no_add_attrs) | |
7948 | { | |
7949 | tree decl = *node; | |
7950 | ||
7951 | if (TREE_CODE (decl) != FUNCTION_DECL) | |
7952 | { | |
7953 | error_at (DECL_SOURCE_LOCATION (decl), | |
7954 | "%qE attribute applies only to functions", name); | |
7955 | *no_add_attrs = true; | |
7956 | } | |
7957 | else if (DECL_INITIAL (decl)) | |
7958 | { | |
7959 | error_at (DECL_SOURCE_LOCATION (decl), | |
7960 | "can%'t set %qE attribute after definition", name); | |
7961 | *no_add_attrs = true; | |
7962 | } | |
7963 | ||
7964 | return NULL_TREE; | |
7965 | } | |
dbf6c367 | 7966 | \f |
774e9d58 | 7967 | /* Check for valid arguments being passed to a function with FNTYPE. |
7968 | There are NARGS arguments in the array ARGARRAY. */ | |
dbf6c367 | 7969 | void |
774e9d58 | 7970 | check_function_arguments (const_tree fntype, int nargs, tree *argarray) |
dbf6c367 | 7971 | { |
7972 | /* Check for null being passed in a pointer argument that must be | |
7973 | non-null. We also need to do this if format checking is enabled. */ | |
7974 | ||
7975 | if (warn_nonnull) | |
774e9d58 | 7976 | check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
dbf6c367 | 7977 | |
7978 | /* Check for errors in format strings. */ | |
7979 | ||
95c90e04 | 7980 | if (warn_format || warn_missing_format_attribute) |
774e9d58 | 7981 | check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray); |
95c90e04 | 7982 | |
7983 | if (warn_format) | |
774e9d58 | 7984 | check_function_sentinel (fntype, nargs, argarray); |
dbf6c367 | 7985 | } |
7986 | ||
7987 | /* Generic argument checking recursion routine. PARAM is the argument to | |
7988 | be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked | |
7989 | once the argument is resolved. CTX is context for the callback. */ | |
7990 | void | |
1cae46be | 7991 | check_function_arguments_recurse (void (*callback) |
7992 | (void *, tree, unsigned HOST_WIDE_INT), | |
7993 | void *ctx, tree param, | |
7994 | unsigned HOST_WIDE_INT param_num) | |
dbf6c367 | 7995 | { |
72dd6141 | 7996 | if (CONVERT_EXPR_P (param) |
c44afe23 | 7997 | && (TYPE_PRECISION (TREE_TYPE (param)) |
7998 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0))))) | |
dbf6c367 | 7999 | { |
8000 | /* Strip coercion. */ | |
8001 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 8002 | TREE_OPERAND (param, 0), param_num); |
dbf6c367 | 8003 | return; |
8004 | } | |
8005 | ||
8006 | if (TREE_CODE (param) == CALL_EXPR) | |
8007 | { | |
c2f47e15 | 8008 | tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param))); |
dbf6c367 | 8009 | tree attrs; |
8010 | bool found_format_arg = false; | |
8011 | ||
8012 | /* See if this is a call to a known internationalization function | |
8013 | that modifies a format arg. Such a function may have multiple | |
8014 | format_arg attributes (for example, ngettext). */ | |
8015 | ||
8016 | for (attrs = TYPE_ATTRIBUTES (type); | |
8017 | attrs; | |
8018 | attrs = TREE_CHAIN (attrs)) | |
8019 | if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs))) | |
8020 | { | |
c2f47e15 | 8021 | tree inner_arg; |
dbf6c367 | 8022 | tree format_num_expr; |
8023 | int format_num; | |
8024 | int i; | |
c2f47e15 | 8025 | call_expr_arg_iterator iter; |
dbf6c367 | 8026 | |
8027 | /* Extract the argument number, which was previously checked | |
8028 | to be valid. */ | |
8029 | format_num_expr = TREE_VALUE (TREE_VALUE (attrs)); | |
dbf6c367 | 8030 | |
231bd014 | 8031 | gcc_assert (TREE_CODE (format_num_expr) == INTEGER_CST |
8032 | && !TREE_INT_CST_HIGH (format_num_expr)); | |
dbf6c367 | 8033 | |
8034 | format_num = TREE_INT_CST_LOW (format_num_expr); | |
8035 | ||
c2f47e15 | 8036 | for (inner_arg = first_call_expr_arg (param, &iter), i = 1; |
8037 | inner_arg != 0; | |
8038 | inner_arg = next_call_expr_arg (&iter), i++) | |
dbf6c367 | 8039 | if (i == format_num) |
8040 | { | |
8041 | check_function_arguments_recurse (callback, ctx, | |
c2f47e15 | 8042 | inner_arg, param_num); |
dbf6c367 | 8043 | found_format_arg = true; |
8044 | break; | |
8045 | } | |
8046 | } | |
8047 | ||
8048 | /* If we found a format_arg attribute and did a recursive check, | |
8049 | we are done with checking this argument. Otherwise, we continue | |
8050 | and this will be considered a non-literal. */ | |
8051 | if (found_format_arg) | |
8052 | return; | |
8053 | } | |
8054 | ||
8055 | if (TREE_CODE (param) == COND_EXPR) | |
8056 | { | |
8057 | /* Check both halves of the conditional expression. */ | |
8058 | check_function_arguments_recurse (callback, ctx, | |
4ee9c684 | 8059 | TREE_OPERAND (param, 1), param_num); |
dbf6c367 | 8060 | check_function_arguments_recurse (callback, ctx, |
4ee9c684 | 8061 | TREE_OPERAND (param, 2), param_num); |
dbf6c367 | 8062 | return; |
8063 | } | |
8064 | ||
8065 | (*callback) (ctx, param, param_num); | |
8066 | } | |
1f3233d1 | 8067 | |
60cce472 | 8068 | /* Checks for a builtin function FNDECL that the number of arguments |
8069 | NARGS against the required number REQUIRED and issues an error if | |
8070 | there is a mismatch. Returns true if the number of arguments is | |
8071 | correct, otherwise false. */ | |
d43cee80 | 8072 | |
8073 | static bool | |
60cce472 | 8074 | builtin_function_validate_nargs (tree fndecl, int nargs, int required) |
d43cee80 | 8075 | { |
8076 | if (nargs < required) | |
8077 | { | |
60cce472 | 8078 | error_at (input_location, |
8079 | "not enough arguments to function %qE", fndecl); | |
d43cee80 | 8080 | return false; |
8081 | } | |
8082 | else if (nargs > required) | |
8083 | { | |
60cce472 | 8084 | error_at (input_location, |
8085 | "too many arguments to function %qE", fndecl); | |
d43cee80 | 8086 | return false; |
8087 | } | |
8088 | return true; | |
8089 | } | |
8090 | ||
8091 | /* Verifies the NARGS arguments ARGS to the builtin function FNDECL. | |
8092 | Returns false if there was an error, otherwise true. */ | |
8093 | ||
8094 | bool | |
8095 | check_builtin_function_arguments (tree fndecl, int nargs, tree *args) | |
8096 | { | |
8097 | if (!DECL_BUILT_IN (fndecl) | |
8098 | || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) | |
8099 | return true; | |
8100 | ||
8101 | switch (DECL_FUNCTION_CODE (fndecl)) | |
8102 | { | |
8103 | case BUILT_IN_CONSTANT_P: | |
60cce472 | 8104 | return builtin_function_validate_nargs (fndecl, nargs, 1); |
d43cee80 | 8105 | |
8106 | case BUILT_IN_ISFINITE: | |
8107 | case BUILT_IN_ISINF: | |
c319d56a | 8108 | case BUILT_IN_ISINF_SIGN: |
d43cee80 | 8109 | case BUILT_IN_ISNAN: |
8110 | case BUILT_IN_ISNORMAL: | |
60cce472 | 8111 | if (builtin_function_validate_nargs (fndecl, nargs, 1)) |
d43cee80 | 8112 | { |
8113 | if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE) | |
8114 | { | |
8115 | error ("non-floating-point argument in call to " | |
8116 | "function %qE", fndecl); | |
8117 | return false; | |
8118 | } | |
8119 | return true; | |
8120 | } | |
8121 | return false; | |
8122 | ||
8123 | case BUILT_IN_ISGREATER: | |
8124 | case BUILT_IN_ISGREATEREQUAL: | |
8125 | case BUILT_IN_ISLESS: | |
8126 | case BUILT_IN_ISLESSEQUAL: | |
8127 | case BUILT_IN_ISLESSGREATER: | |
8128 | case BUILT_IN_ISUNORDERED: | |
60cce472 | 8129 | if (builtin_function_validate_nargs (fndecl, nargs, 2)) |
d43cee80 | 8130 | { |
8131 | enum tree_code code0, code1; | |
8132 | code0 = TREE_CODE (TREE_TYPE (args[0])); | |
8133 | code1 = TREE_CODE (TREE_TYPE (args[1])); | |
8134 | if (!((code0 == REAL_TYPE && code1 == REAL_TYPE) | |
8135 | || (code0 == REAL_TYPE && code1 == INTEGER_TYPE) | |
8136 | || (code0 == INTEGER_TYPE && code1 == REAL_TYPE))) | |
8137 | { | |
8138 | error ("non-floating-point arguments in call to " | |
8139 | "function %qE", fndecl); | |
8140 | return false; | |
8141 | } | |
8142 | return true; | |
8143 | } | |
8144 | return false; | |
8145 | ||
19fbe3a4 | 8146 | case BUILT_IN_FPCLASSIFY: |
60cce472 | 8147 | if (builtin_function_validate_nargs (fndecl, nargs, 6)) |
19fbe3a4 | 8148 | { |
8149 | unsigned i; | |
48e1416a | 8150 | |
19fbe3a4 | 8151 | for (i=0; i<5; i++) |
8152 | if (TREE_CODE (args[i]) != INTEGER_CST) | |
8153 | { | |
8154 | error ("non-const integer argument %u in call to function %qE", | |
8155 | i+1, fndecl); | |
8156 | return false; | |
8157 | } | |
8158 | ||
8159 | if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE) | |
8160 | { | |
8161 | error ("non-floating-point argument in call to function %qE", | |
8162 | fndecl); | |
8163 | return false; | |
8164 | } | |
8165 | return true; | |
8166 | } | |
8167 | return false; | |
8168 | ||
d43cee80 | 8169 | default: |
8170 | return true; | |
8171 | } | |
8172 | } | |
8173 | ||
860251be | 8174 | /* Function to help qsort sort FIELD_DECLs by name order. */ |
8175 | ||
8176 | int | |
8177 | field_decl_cmp (const void *x_p, const void *y_p) | |
8178 | { | |
4fd61bc6 | 8179 | const tree *const x = (const tree *const) x_p; |
8180 | const tree *const y = (const tree *const) y_p; | |
8181 | ||
860251be | 8182 | if (DECL_NAME (*x) == DECL_NAME (*y)) |
8183 | /* A nontype is "greater" than a type. */ | |
8184 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
8185 | if (DECL_NAME (*x) == NULL_TREE) | |
8186 | return -1; | |
8187 | if (DECL_NAME (*y) == NULL_TREE) | |
8188 | return 1; | |
8189 | if (DECL_NAME (*x) < DECL_NAME (*y)) | |
8190 | return -1; | |
8191 | return 1; | |
8192 | } | |
8193 | ||
8194 | static struct { | |
8195 | gt_pointer_operator new_value; | |
8196 | void *cookie; | |
8197 | } resort_data; | |
8198 | ||
8199 | /* This routine compares two fields like field_decl_cmp but using the | |
8200 | pointer operator in resort_data. */ | |
8201 | ||
8202 | static int | |
8203 | resort_field_decl_cmp (const void *x_p, const void *y_p) | |
8204 | { | |
4fd61bc6 | 8205 | const tree *const x = (const tree *const) x_p; |
8206 | const tree *const y = (const tree *const) y_p; | |
860251be | 8207 | |
8208 | if (DECL_NAME (*x) == DECL_NAME (*y)) | |
8209 | /* A nontype is "greater" than a type. */ | |
8210 | return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL); | |
8211 | if (DECL_NAME (*x) == NULL_TREE) | |
8212 | return -1; | |
8213 | if (DECL_NAME (*y) == NULL_TREE) | |
8214 | return 1; | |
8215 | { | |
8216 | tree d1 = DECL_NAME (*x); | |
8217 | tree d2 = DECL_NAME (*y); | |
8218 | resort_data.new_value (&d1, resort_data.cookie); | |
8219 | resort_data.new_value (&d2, resort_data.cookie); | |
8220 | if (d1 < d2) | |
8221 | return -1; | |
8222 | } | |
8223 | return 1; | |
8224 | } | |
8225 | ||
8226 | /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */ | |
8227 | ||
8228 | void | |
8229 | resort_sorted_fields (void *obj, | |
9a03a746 | 8230 | void * ARG_UNUSED (orig_obj), |
4ee9c684 | 8231 | gt_pointer_operator new_value, |
8232 | void *cookie) | |
860251be | 8233 | { |
9a03a746 | 8234 | struct sorted_fields_type *sf = (struct sorted_fields_type *) obj; |
860251be | 8235 | resort_data.new_value = new_value; |
8236 | resort_data.cookie = cookie; | |
8237 | qsort (&sf->elts[0], sf->len, sizeof (tree), | |
4ee9c684 | 8238 | resort_field_decl_cmp); |
860251be | 8239 | } |
8240 | ||
209c9752 | 8241 | /* Subroutine of c_parse_error. |
8242 | Return the result of concatenating LHS and RHS. RHS is really | |
8243 | a string literal, its first character is indicated by RHS_START and | |
cfee01e3 | 8244 | RHS_SIZE is its length (including the terminating NUL character). |
209c9752 | 8245 | |
8246 | The caller is responsible for deleting the returned pointer. */ | |
8247 | ||
8248 | static char * | |
8249 | catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) | |
8250 | { | |
8251 | const int lhs_size = strlen (lhs); | |
8252 | char *result = XNEWVEC (char, lhs_size + rhs_size); | |
8253 | strncpy (result, lhs, lhs_size); | |
8254 | strncpy (result + lhs_size, rhs_start, rhs_size); | |
8255 | return result; | |
8256 | } | |
8257 | ||
380c6697 | 8258 | /* Issue the error given by GMSGID, indicating that it occurred before |
92b128ed | 8259 | TOKEN, which had the associated VALUE. */ |
8260 | ||
8261 | void | |
48e1416a | 8262 | c_parse_error (const char *gmsgid, enum cpp_ttype token_type, |
ba99525e | 8263 | tree value, unsigned char token_flags) |
92b128ed | 8264 | { |
209c9752 | 8265 | #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2)) |
8266 | ||
8267 | char *message = NULL; | |
92b128ed | 8268 | |
ba99525e | 8269 | if (token_type == CPP_EOF) |
380c6697 | 8270 | message = catenate_messages (gmsgid, " at end of input"); |
48e1416a | 8271 | else if (token_type == CPP_CHAR |
8272 | || token_type == CPP_WCHAR | |
ba99525e | 8273 | || token_type == CPP_CHAR16 |
8274 | || token_type == CPP_CHAR32) | |
92b128ed | 8275 | { |
8276 | unsigned int val = TREE_INT_CST_LOW (value); | |
924bbf02 | 8277 | const char *prefix; |
8278 | ||
ba99525e | 8279 | switch (token_type) |
924bbf02 | 8280 | { |
8281 | default: | |
8282 | prefix = ""; | |
8283 | break; | |
8284 | case CPP_WCHAR: | |
8285 | prefix = "L"; | |
8286 | break; | |
8287 | case CPP_CHAR16: | |
8288 | prefix = "u"; | |
8289 | break; | |
8290 | case CPP_CHAR32: | |
8291 | prefix = "U"; | |
8292 | break; | |
8293 | } | |
8294 | ||
92b128ed | 8295 | if (val <= UCHAR_MAX && ISGRAPH (val)) |
a0c938f0 | 8296 | message = catenate_messages (gmsgid, " before %s'%c'"); |
92b128ed | 8297 | else |
a0c938f0 | 8298 | message = catenate_messages (gmsgid, " before %s'\\x%x'"); |
209c9752 | 8299 | |
924bbf02 | 8300 | error (message, prefix, val); |
209c9752 | 8301 | free (message); |
8302 | message = NULL; | |
92b128ed | 8303 | } |
48e1416a | 8304 | else if (token_type == CPP_STRING |
8305 | || token_type == CPP_WSTRING | |
ba99525e | 8306 | || token_type == CPP_STRING16 |
538ba11a | 8307 | || token_type == CPP_STRING32 |
8308 | || token_type == CPP_UTF8STRING) | |
380c6697 | 8309 | message = catenate_messages (gmsgid, " before string constant"); |
ba99525e | 8310 | else if (token_type == CPP_NUMBER) |
380c6697 | 8311 | message = catenate_messages (gmsgid, " before numeric constant"); |
ba99525e | 8312 | else if (token_type == CPP_NAME) |
209c9752 | 8313 | { |
380c6697 | 8314 | message = catenate_messages (gmsgid, " before %qE"); |
782858b8 | 8315 | error (message, value); |
209c9752 | 8316 | free (message); |
8317 | message = NULL; | |
8318 | } | |
ba99525e | 8319 | else if (token_type == CPP_PRAGMA) |
b75b98aa | 8320 | message = catenate_messages (gmsgid, " before %<#pragma%>"); |
ba99525e | 8321 | else if (token_type == CPP_PRAGMA_EOL) |
b75b98aa | 8322 | message = catenate_messages (gmsgid, " before end of line"); |
ba99525e | 8323 | else if (token_type < N_TTYPES) |
209c9752 | 8324 | { |
380c6697 | 8325 | message = catenate_messages (gmsgid, " before %qs token"); |
ba99525e | 8326 | error (message, cpp_type2name (token_type, token_flags)); |
209c9752 | 8327 | free (message); |
8328 | message = NULL; | |
8329 | } | |
92b128ed | 8330 | else |
380c6697 | 8331 | error (gmsgid); |
209c9752 | 8332 | |
8333 | if (message) | |
8334 | { | |
8335 | error (message); | |
8336 | free (message); | |
8337 | } | |
a0c938f0 | 8338 | #undef catenate_messages |
92b128ed | 8339 | } |
8340 | ||
3a79f5da | 8341 | /* Mapping for cpp message reasons to the options that enable them. */ |
8342 | ||
8343 | struct reason_option_codes_t | |
8344 | { | |
8345 | const int reason; /* cpplib message reason. */ | |
8346 | const int option_code; /* gcc option that controls this message. */ | |
8347 | }; | |
8348 | ||
8349 | static const struct reason_option_codes_t option_codes[] = { | |
8350 | {CPP_W_DEPRECATED, OPT_Wdeprecated}, | |
67089c6b | 8351 | {CPP_W_COMMENTS, OPT_Wcomment}, |
3a79f5da | 8352 | {CPP_W_TRIGRAPHS, OPT_Wtrigraphs}, |
8353 | {CPP_W_MULTICHAR, OPT_Wmultichar}, | |
8354 | {CPP_W_TRADITIONAL, OPT_Wtraditional}, | |
8355 | {CPP_W_LONG_LONG, OPT_Wlong_long}, | |
8356 | {CPP_W_ENDIF_LABELS, OPT_Wendif_labels}, | |
8357 | {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros}, | |
8358 | {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined}, | |
8359 | {CPP_W_UNDEF, OPT_Wundef}, | |
8360 | {CPP_W_UNUSED_MACROS, OPT_Wunused_macros}, | |
8361 | {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat}, | |
8362 | {CPP_W_NORMALIZE, OPT_Wnormalized_}, | |
8363 | {CPP_W_INVALID_PCH, OPT_Winvalid_pch}, | |
8364 | {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp}, | |
8365 | {CPP_W_NONE, 0} | |
8366 | }; | |
8367 | ||
8368 | /* Return the gcc option code associated with the reason for a cpp | |
8369 | message, or 0 if none. */ | |
8370 | ||
8371 | static int | |
8372 | c_option_controlling_cpp_error (int reason) | |
8373 | { | |
8374 | const struct reason_option_codes_t *entry; | |
8375 | ||
8376 | for (entry = option_codes; entry->reason != CPP_W_NONE; entry++) | |
8377 | { | |
8378 | if (entry->reason == reason) | |
8379 | return entry->option_code; | |
8380 | } | |
8381 | return 0; | |
8382 | } | |
8383 | ||
7f5f3953 | 8384 | /* Callback from cpp_error for PFILE to print diagnostics from the |
3a79f5da | 8385 | preprocessor. The diagnostic is of type LEVEL, with REASON set |
8386 | to the reason code if LEVEL is represents a warning, at location | |
7f5f3953 | 8387 | LOCATION unless this is after lexing and the compiler's location |
8388 | should be used instead, with column number possibly overridden by | |
8389 | COLUMN_OVERRIDE if not zero; MSG is the translated message and AP | |
8390 | the arguments. Returns true if a diagnostic was emitted, false | |
8391 | otherwise. */ | |
8392 | ||
8393 | bool | |
3a79f5da | 8394 | c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason, |
7f5f3953 | 8395 | location_t location, unsigned int column_override, |
8396 | const char *msg, va_list *ap) | |
8397 | { | |
8398 | diagnostic_info diagnostic; | |
8399 | diagnostic_t dlevel; | |
5ae82d58 | 8400 | bool save_warn_system_headers = global_dc->dc_warn_system_headers; |
7f5f3953 | 8401 | bool ret; |
8402 | ||
8403 | switch (level) | |
8404 | { | |
8405 | case CPP_DL_WARNING_SYSHDR: | |
8406 | if (flag_no_output) | |
8407 | return false; | |
5ae82d58 | 8408 | global_dc->dc_warn_system_headers = 1; |
7f5f3953 | 8409 | /* Fall through. */ |
8410 | case CPP_DL_WARNING: | |
8411 | if (flag_no_output) | |
8412 | return false; | |
8413 | dlevel = DK_WARNING; | |
8414 | break; | |
8415 | case CPP_DL_PEDWARN: | |
8416 | if (flag_no_output && !flag_pedantic_errors) | |
8417 | return false; | |
8418 | dlevel = DK_PEDWARN; | |
8419 | break; | |
8420 | case CPP_DL_ERROR: | |
8421 | dlevel = DK_ERROR; | |
8422 | break; | |
8423 | case CPP_DL_ICE: | |
8424 | dlevel = DK_ICE; | |
8425 | break; | |
8426 | case CPP_DL_NOTE: | |
8427 | dlevel = DK_NOTE; | |
8428 | break; | |
ff903809 | 8429 | case CPP_DL_FATAL: |
8430 | dlevel = DK_FATAL; | |
8431 | break; | |
7f5f3953 | 8432 | default: |
8433 | gcc_unreachable (); | |
8434 | } | |
8435 | if (done_lexing) | |
8436 | location = input_location; | |
8437 | diagnostic_set_info_translated (&diagnostic, msg, ap, | |
8438 | location, dlevel); | |
8439 | if (column_override) | |
8440 | diagnostic_override_column (&diagnostic, column_override); | |
3a79f5da | 8441 | diagnostic_override_option_index (&diagnostic, |
8442 | c_option_controlling_cpp_error (reason)); | |
7f5f3953 | 8443 | ret = report_diagnostic (&diagnostic); |
8444 | if (level == CPP_DL_WARNING_SYSHDR) | |
5ae82d58 | 8445 | global_dc->dc_warn_system_headers = save_warn_system_headers; |
7f5f3953 | 8446 | return ret; |
8447 | } | |
8448 | ||
624d37a6 | 8449 | /* Convert a character from the host to the target execution character |
8450 | set. cpplib handles this, mostly. */ | |
8451 | ||
8452 | HOST_WIDE_INT | |
8453 | c_common_to_target_charset (HOST_WIDE_INT c) | |
8454 | { | |
8455 | /* Character constants in GCC proper are sign-extended under -fsigned-char, | |
8456 | zero-extended under -fno-signed-char. cpplib insists that characters | |
8457 | and character constants are always unsigned. Hence we must convert | |
8458 | back and forth. */ | |
8459 | cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1); | |
8460 | ||
8461 | uc = cpp_host_to_exec_charset (parse_in, uc); | |
8462 | ||
8463 | if (flag_signed_char) | |
8464 | return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE) | |
8465 | >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE); | |
8466 | else | |
8467 | return uc; | |
8468 | } | |
8469 | ||
af28855b | 8470 | /* Build the result of __builtin_offsetof. EXPR is a nested sequence of |
d897f7c2 | 8471 | component references, with STOP_REF, or alternatively an INDIRECT_REF of |
8472 | NULL, at the bottom; much like the traditional rendering of offsetof as a | |
8473 | macro. Returns the folded and properly cast result. */ | |
af28855b | 8474 | |
8475 | static tree | |
d897f7c2 | 8476 | fold_offsetof_1 (tree expr, tree stop_ref) |
af28855b | 8477 | { |
8478 | enum tree_code code = PLUS_EXPR; | |
8479 | tree base, off, t; | |
8480 | ||
d897f7c2 | 8481 | if (expr == stop_ref && TREE_CODE (expr) != ERROR_MARK) |
8482 | return size_zero_node; | |
8483 | ||
af28855b | 8484 | switch (TREE_CODE (expr)) |
8485 | { | |
8486 | case ERROR_MARK: | |
8487 | return expr; | |
8488 | ||
6b11d2e3 | 8489 | case VAR_DECL: |
8490 | error ("cannot apply %<offsetof%> to static data member %qD", expr); | |
8491 | return error_mark_node; | |
8492 | ||
d897f7c2 | 8493 | case CALL_EXPR: |
cf1a89a3 | 8494 | case TARGET_EXPR: |
d897f7c2 | 8495 | error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded"); |
8496 | return error_mark_node; | |
8497 | ||
d897f7c2 | 8498 | case NOP_EXPR: |
8499 | case INDIRECT_REF: | |
64ed018c | 8500 | if (!integer_zerop (TREE_OPERAND (expr, 0))) |
8501 | { | |
8502 | error ("cannot apply %<offsetof%> to a non constant address"); | |
8503 | return error_mark_node; | |
8504 | } | |
8505 | return size_zero_node; | |
d897f7c2 | 8506 | |
af28855b | 8507 | case COMPONENT_REF: |
d897f7c2 | 8508 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref); |
af28855b | 8509 | if (base == error_mark_node) |
8510 | return base; | |
8511 | ||
8512 | t = TREE_OPERAND (expr, 1); | |
8513 | if (DECL_C_BIT_FIELD (t)) | |
8514 | { | |
8515 | error ("attempt to take address of bit-field structure " | |
782858b8 | 8516 | "member %qD", t); |
af28855b | 8517 | return error_mark_node; |
8518 | } | |
389dd41b | 8519 | off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t), |
8520 | size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t), | |
8521 | 1) | |
8522 | / BITS_PER_UNIT)); | |
af28855b | 8523 | break; |
8524 | ||
8525 | case ARRAY_REF: | |
d897f7c2 | 8526 | base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref); |
af28855b | 8527 | if (base == error_mark_node) |
8528 | return base; | |
8529 | ||
8530 | t = TREE_OPERAND (expr, 1); | |
8531 | if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0) | |
8532 | { | |
8533 | code = MINUS_EXPR; | |
389dd41b | 8534 | t = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (t), t); |
af28855b | 8535 | } |
8536 | t = convert (sizetype, t); | |
8537 | off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); | |
64ed018c | 8538 | |
8539 | /* Check if the offset goes beyond the upper bound of the array. */ | |
e0559d69 | 8540 | if (code == PLUS_EXPR && TREE_CODE (t) == INTEGER_CST) |
8541 | { | |
8542 | tree upbound = array_ref_up_bound (expr); | |
8543 | if (upbound != NULL_TREE | |
8544 | && TREE_CODE (upbound) == INTEGER_CST | |
8545 | && !tree_int_cst_equal (upbound, | |
8546 | TYPE_MAX_VALUE (TREE_TYPE (upbound)))) | |
8547 | { | |
8548 | upbound = size_binop (PLUS_EXPR, upbound, | |
8549 | build_int_cst (TREE_TYPE (upbound), 1)); | |
8550 | if (tree_int_cst_lt (upbound, t)) | |
8551 | { | |
8552 | tree v; | |
8553 | ||
8554 | for (v = TREE_OPERAND (expr, 0); | |
8555 | TREE_CODE (v) == COMPONENT_REF; | |
8556 | v = TREE_OPERAND (v, 0)) | |
8557 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0))) | |
8558 | == RECORD_TYPE) | |
8559 | { | |
1767a056 | 8560 | tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1)); |
8561 | for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain)) | |
e0559d69 | 8562 | if (TREE_CODE (fld_chain) == FIELD_DECL) |
8563 | break; | |
8564 | ||
8565 | if (fld_chain) | |
8566 | break; | |
8567 | } | |
8568 | /* Don't warn if the array might be considered a poor | |
8569 | man's flexible array member with a very permissive | |
8570 | definition thereof. */ | |
8571 | if (TREE_CODE (v) == ARRAY_REF | |
8572 | || TREE_CODE (v) == COMPONENT_REF) | |
8573 | warning (OPT_Warray_bounds, | |
8574 | "index %E denotes an offset " | |
8575 | "greater than size of %qT", | |
8576 | t, TREE_TYPE (TREE_OPERAND (expr, 0))); | |
8577 | } | |
8578 | } | |
8579 | } | |
af28855b | 8580 | break; |
8581 | ||
ede90cc2 | 8582 | case COMPOUND_EXPR: |
8583 | /* Handle static members of volatile structs. */ | |
8584 | t = TREE_OPERAND (expr, 1); | |
8585 | gcc_assert (TREE_CODE (t) == VAR_DECL); | |
8586 | return fold_offsetof_1 (t, stop_ref); | |
8587 | ||
af28855b | 8588 | default: |
231bd014 | 8589 | gcc_unreachable (); |
af28855b | 8590 | } |
8591 | ||
8592 | return size_binop (code, base, off); | |
8593 | } | |
8594 | ||
8595 | tree | |
d897f7c2 | 8596 | fold_offsetof (tree expr, tree stop_ref) |
af28855b | 8597 | { |
8598 | /* Convert back from the internal sizetype to size_t. */ | |
d897f7c2 | 8599 | return convert (size_type_node, fold_offsetof_1 (expr, stop_ref)); |
af28855b | 8600 | } |
8601 | ||
b9bdfa0b | 8602 | /* Warn for A ?: C expressions (with B omitted) where A is a boolean |
8603 | expression, because B will always be true. */ | |
8604 | ||
8605 | void | |
8606 | warn_for_omitted_condop (location_t location, tree cond) | |
8607 | { | |
8608 | if (truth_value_p (TREE_CODE (cond))) | |
8609 | warning_at (location, OPT_Wparentheses, | |
8610 | "the omitted middle operand in ?: will always be %<true%>, " | |
8611 | "suggest explicit middle operand"); | |
8612 | } | |
8613 | ||
a1f90215 | 8614 | /* Give an error for storing into ARG, which is 'const'. USE indicates |
8615 | how ARG was being used. */ | |
8616 | ||
8617 | void | |
8618 | readonly_error (tree arg, enum lvalue_use use) | |
8619 | { | |
8620 | gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement | |
8621 | || use == lv_asm); | |
8622 | /* Using this macro rather than (for example) arrays of messages | |
8623 | ensures that all the format strings are checked at compile | |
8624 | time. */ | |
8625 | #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \ | |
8626 | : (use == lv_increment ? (I) \ | |
8627 | : (use == lv_decrement ? (D) : (AS)))) | |
8628 | if (TREE_CODE (arg) == COMPONENT_REF) | |
8629 | { | |
8630 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
8631 | error (READONLY_MSG (G_("assignment of member " | |
8632 | "%qD in read-only object"), | |
8633 | G_("increment of member " | |
8634 | "%qD in read-only object"), | |
8635 | G_("decrement of member " | |
8636 | "%qD in read-only object"), | |
8637 | G_("member %qD in read-only object " | |
8638 | "used as %<asm%> output")), | |
8639 | TREE_OPERAND (arg, 1)); | |
8640 | else | |
8641 | error (READONLY_MSG (G_("assignment of read-only member %qD"), | |
8642 | G_("increment of read-only member %qD"), | |
8643 | G_("decrement of read-only member %qD"), | |
8644 | G_("read-only member %qD used as %<asm%> output")), | |
8645 | TREE_OPERAND (arg, 1)); | |
8646 | } | |
8647 | else if (TREE_CODE (arg) == VAR_DECL) | |
8648 | error (READONLY_MSG (G_("assignment of read-only variable %qD"), | |
8649 | G_("increment of read-only variable %qD"), | |
8650 | G_("decrement of read-only variable %qD"), | |
8651 | G_("read-only variable %qD used as %<asm%> output")), | |
8652 | arg); | |
8653 | else if (TREE_CODE (arg) == PARM_DECL) | |
8654 | error (READONLY_MSG (G_("assignment of read-only parameter %qD"), | |
8655 | G_("increment of read-only parameter %qD"), | |
8656 | G_("decrement of read-only parameter %qD"), | |
8657 | G_("read-only parameter %qD use as %<asm%> output")), | |
8658 | arg); | |
8659 | else if (TREE_CODE (arg) == RESULT_DECL) | |
8660 | { | |
8661 | gcc_assert (c_dialect_cxx ()); | |
8662 | error (READONLY_MSG (G_("assignment of " | |
8663 | "read-only named return value %qD"), | |
8664 | G_("increment of " | |
8665 | "read-only named return value %qD"), | |
8666 | G_("decrement of " | |
8667 | "read-only named return value %qD"), | |
8668 | G_("read-only named return value %qD " | |
8669 | "used as %<asm%>output")), | |
8670 | arg); | |
8671 | } | |
8672 | else if (TREE_CODE (arg) == FUNCTION_DECL) | |
8673 | error (READONLY_MSG (G_("assignment of function %qD"), | |
8674 | G_("increment of function %qD"), | |
8675 | G_("decrement of function %qD"), | |
8676 | G_("function %qD used as %<asm%> output")), | |
8677 | arg); | |
8678 | else | |
8679 | error (READONLY_MSG (G_("assignment of read-only location %qE"), | |
8680 | G_("increment of read-only location %qE"), | |
8681 | G_("decrement of read-only location %qE"), | |
8682 | G_("read-only location %qE used as %<asm%> output")), | |
8683 | arg); | |
8684 | } | |
8685 | ||
e35976b1 | 8686 | /* Print an error message for an invalid lvalue. USE says |
fdd84b77 | 8687 | how the lvalue is being used and so selects the error message. LOC |
8688 | is the location for the error. */ | |
ab6bb714 | 8689 | |
e35976b1 | 8690 | void |
fdd84b77 | 8691 | lvalue_error (location_t loc, enum lvalue_use use) |
ab6bb714 | 8692 | { |
e35976b1 | 8693 | switch (use) |
ab6bb714 | 8694 | { |
e35976b1 | 8695 | case lv_assign: |
fdd84b77 | 8696 | error_at (loc, "lvalue required as left operand of assignment"); |
e35976b1 | 8697 | break; |
8698 | case lv_increment: | |
fdd84b77 | 8699 | error_at (loc, "lvalue required as increment operand"); |
e35976b1 | 8700 | break; |
8701 | case lv_decrement: | |
fdd84b77 | 8702 | error_at (loc, "lvalue required as decrement operand"); |
e35976b1 | 8703 | break; |
8704 | case lv_addressof: | |
fdd84b77 | 8705 | error_at (loc, "lvalue required as unary %<&%> operand"); |
e35976b1 | 8706 | break; |
8707 | case lv_asm: | |
fdd84b77 | 8708 | error_at (loc, "lvalue required in asm statement"); |
e35976b1 | 8709 | break; |
8710 | default: | |
8711 | gcc_unreachable (); | |
ab6bb714 | 8712 | } |
ab6bb714 | 8713 | } |
b1bbc8e5 | 8714 | |
8715 | /* Print an error message for an invalid indirection of type TYPE. | |
8716 | ERRSTRING is the name of the operator for the indirection. */ | |
8717 | ||
8718 | void | |
8719 | invalid_indirection_error (location_t loc, tree type, ref_operator errstring) | |
8720 | { | |
8721 | switch (errstring) | |
8722 | { | |
8723 | case RO_NULL: | |
8724 | gcc_assert (c_dialect_cxx ()); | |
8725 | error_at (loc, "invalid type argument (have %qT)", type); | |
8726 | break; | |
8727 | case RO_ARRAY_INDEXING: | |
8728 | error_at (loc, | |
8729 | "invalid type argument of array indexing (have %qT)", | |
8730 | type); | |
8731 | break; | |
8732 | case RO_UNARY_STAR: | |
8733 | error_at (loc, | |
8734 | "invalid type argument of unary %<*%> (have %qT)", | |
8735 | type); | |
8736 | break; | |
8737 | case RO_ARROW: | |
8738 | error_at (loc, | |
8739 | "invalid type argument of %<->%> (have %qT)", | |
8740 | type); | |
8741 | break; | |
8742 | case RO_IMPLICIT_CONVERSION: | |
8743 | error_at (loc, | |
8744 | "invalid type argument of implicit conversion (have %qT)", | |
8745 | type); | |
8746 | break; | |
8747 | default: | |
8748 | gcc_unreachable (); | |
8749 | } | |
8750 | } | |
c271bdb2 | 8751 | \f |
8752 | /* *PTYPE is an incomplete array. Complete it with a domain based on | |
8753 | INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT | |
8754 | is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
8755 | 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ | |
8756 | ||
8757 | int | |
8758 | complete_array_type (tree *ptype, tree initial_value, bool do_default) | |
8759 | { | |
8760 | tree maxindex, type, main_type, elt, unqual_elt; | |
8761 | int failure = 0, quals; | |
6753bca0 | 8762 | hashval_t hashcode = 0; |
c271bdb2 | 8763 | |
8764 | maxindex = size_zero_node; | |
8765 | if (initial_value) | |
8766 | { | |
8767 | if (TREE_CODE (initial_value) == STRING_CST) | |
8768 | { | |
8769 | int eltsize | |
8770 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
8771 | maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1); | |
8772 | } | |
8773 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) | |
8774 | { | |
c75b4594 | 8775 | VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value); |
c271bdb2 | 8776 | |
c75b4594 | 8777 | if (VEC_empty (constructor_elt, v)) |
c271bdb2 | 8778 | { |
8779 | if (pedantic) | |
8780 | failure = 3; | |
8781 | maxindex = integer_minus_one_node; | |
8782 | } | |
8783 | else | |
8784 | { | |
8785 | tree curindex; | |
c75b4594 | 8786 | unsigned HOST_WIDE_INT cnt; |
8787 | constructor_elt *ce; | |
cee43f7e | 8788 | bool fold_p = false; |
c271bdb2 | 8789 | |
c75b4594 | 8790 | if (VEC_index (constructor_elt, v, 0)->index) |
389dd41b | 8791 | maxindex = fold_convert_loc (input_location, sizetype, |
8792 | VEC_index (constructor_elt, | |
8793 | v, 0)->index); | |
c271bdb2 | 8794 | curindex = maxindex; |
8795 | ||
c75b4594 | 8796 | for (cnt = 1; |
8797 | VEC_iterate (constructor_elt, v, cnt, ce); | |
8798 | cnt++) | |
c271bdb2 | 8799 | { |
cee43f7e | 8800 | bool curfold_p = false; |
c75b4594 | 8801 | if (ce->index) |
cee43f7e | 8802 | curindex = ce->index, curfold_p = true; |
c271bdb2 | 8803 | else |
cee43f7e | 8804 | { |
8805 | if (fold_p) | |
8806 | curindex = fold_convert (sizetype, curindex); | |
389dd41b | 8807 | curindex = size_binop (PLUS_EXPR, curindex, |
8808 | size_one_node); | |
cee43f7e | 8809 | } |
c271bdb2 | 8810 | if (tree_int_cst_lt (maxindex, curindex)) |
cee43f7e | 8811 | maxindex = curindex, fold_p = curfold_p; |
c271bdb2 | 8812 | } |
cee43f7e | 8813 | if (fold_p) |
8814 | maxindex = fold_convert (sizetype, maxindex); | |
c271bdb2 | 8815 | } |
8816 | } | |
8817 | else | |
8818 | { | |
8819 | /* Make an error message unless that happened already. */ | |
8820 | if (initial_value != error_mark_node) | |
8821 | failure = 1; | |
8822 | } | |
8823 | } | |
8824 | else | |
8825 | { | |
8826 | failure = 2; | |
8827 | if (!do_default) | |
8828 | return failure; | |
8829 | } | |
8830 | ||
8831 | type = *ptype; | |
8832 | elt = TREE_TYPE (type); | |
8833 | quals = TYPE_QUALS (strip_array_types (elt)); | |
8834 | if (quals == 0) | |
8835 | unqual_elt = elt; | |
8836 | else | |
6d5d708e | 8837 | unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals)); |
c271bdb2 | 8838 | |
8839 | /* Using build_distinct_type_copy and modifying things afterward instead | |
8840 | of using build_array_type to create a new type preserves all of the | |
8841 | TYPE_LANG_FLAG_? bits that the front end may have set. */ | |
8842 | main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type)); | |
8843 | TREE_TYPE (main_type) = unqual_elt; | |
783bb57e | 8844 | TYPE_DOMAIN (main_type) |
8845 | = build_range_type (TREE_TYPE (maxindex), | |
8846 | build_int_cst (TREE_TYPE (maxindex), 0), maxindex); | |
c271bdb2 | 8847 | layout_type (main_type); |
8848 | ||
6753bca0 | 8849 | /* Make sure we have the canonical MAIN_TYPE. */ |
8850 | hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode); | |
48e1416a | 8851 | hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)), |
6753bca0 | 8852 | hashcode); |
8853 | main_type = type_hash_canon (hashcode, main_type); | |
8854 | ||
796735dc | 8855 | /* Fix the canonical type. */ |
8856 | if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type)) | |
8857 | || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type))) | |
8858 | SET_TYPE_STRUCTURAL_EQUALITY (main_type); | |
8859 | else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type) | |
8860 | || (TYPE_CANONICAL (TYPE_DOMAIN (main_type)) | |
8861 | != TYPE_DOMAIN (main_type))) | |
48e1416a | 8862 | TYPE_CANONICAL (main_type) |
796735dc | 8863 | = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)), |
8864 | TYPE_CANONICAL (TYPE_DOMAIN (main_type))); | |
8865 | else | |
8866 | TYPE_CANONICAL (main_type) = main_type; | |
8867 | ||
c271bdb2 | 8868 | if (quals == 0) |
8869 | type = main_type; | |
8870 | else | |
8871 | type = c_build_qualified_type (main_type, quals); | |
8872 | ||
4f5b8f2a | 8873 | if (COMPLETE_TYPE_P (type) |
8874 | && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST | |
8875 | && TREE_OVERFLOW (TYPE_SIZE_UNIT (type))) | |
8876 | { | |
8877 | error ("size of array is too large"); | |
8878 | /* If we proceed with the array type as it is, we'll eventually | |
8879 | crash in tree_low_cst(). */ | |
8880 | type = error_mark_node; | |
8881 | } | |
8882 | ||
c271bdb2 | 8883 | *ptype = type; |
8884 | return failure; | |
8885 | } | |
ab6bb714 | 8886 | |
93426222 | 8887 | /* Like c_mark_addressable but don't check register qualifier. */ |
8888 | void | |
8889 | c_common_mark_addressable_vec (tree t) | |
8890 | { | |
8891 | while (handled_component_p (t)) | |
8892 | t = TREE_OPERAND (t, 0); | |
8893 | if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL) | |
8894 | return; | |
8895 | TREE_ADDRESSABLE (t) = 1; | |
8896 | } | |
8897 | ||
8898 | ||
b6a5fc45 | 8899 | \f |
8900 | /* Used to help initialize the builtin-types.def table. When a type of | |
8901 | the correct size doesn't exist, use error_mark_node instead of NULL. | |
8902 | The later results in segfaults even when a decl using the type doesn't | |
8903 | get invoked. */ | |
8904 | ||
8905 | tree | |
8906 | builtin_type_for_size (int size, bool unsignedp) | |
8907 | { | |
8908 | tree type = lang_hooks.types.type_for_size (size, unsignedp); | |
8909 | return type ? type : error_mark_node; | |
8910 | } | |
8911 | ||
8912 | /* A helper function for resolve_overloaded_builtin in resolving the | |
8913 | overloaded __sync_ builtins. Returns a positive power of 2 if the | |
8914 | first operand of PARAMS is a pointer to a supported data type. | |
8915 | Returns 0 if an error is encountered. */ | |
8916 | ||
8917 | static int | |
b9c74b4d | 8918 | sync_resolve_size (tree function, VEC(tree,gc) *params) |
b6a5fc45 | 8919 | { |
8920 | tree type; | |
8921 | int size; | |
8922 | ||
b9c74b4d | 8923 | if (VEC_empty (tree, params)) |
b6a5fc45 | 8924 | { |
8925 | error ("too few arguments to function %qE", function); | |
8926 | return 0; | |
8927 | } | |
8928 | ||
b9c74b4d | 8929 | type = TREE_TYPE (VEC_index (tree, params, 0)); |
b6a5fc45 | 8930 | if (TREE_CODE (type) != POINTER_TYPE) |
8931 | goto incompatible; | |
8932 | ||
8933 | type = TREE_TYPE (type); | |
8934 | if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) | |
8935 | goto incompatible; | |
8936 | ||
8937 | size = tree_low_cst (TYPE_SIZE_UNIT (type), 1); | |
27213ba3 | 8938 | if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16) |
b6a5fc45 | 8939 | return size; |
8940 | ||
8941 | incompatible: | |
8942 | error ("incompatible type for argument %d of %qE", 1, function); | |
8943 | return 0; | |
8944 | } | |
8945 | ||
a0c938f0 | 8946 | /* A helper function for resolve_overloaded_builtin. Adds casts to |
b6a5fc45 | 8947 | PARAMS to make arguments match up with those of FUNCTION. Drops |
8948 | the variadic arguments at the end. Returns false if some error | |
8949 | was encountered; true on success. */ | |
8950 | ||
8951 | static bool | |
b9c74b4d | 8952 | sync_resolve_params (tree orig_function, tree function, VEC(tree, gc) *params) |
b6a5fc45 | 8953 | { |
d0af78c5 | 8954 | function_args_iterator iter; |
b6a5fc45 | 8955 | tree ptype; |
b9c74b4d | 8956 | unsigned int parmnum; |
b6a5fc45 | 8957 | |
d0af78c5 | 8958 | function_args_iter_init (&iter, TREE_TYPE (function)); |
b6a5fc45 | 8959 | /* We've declared the implementation functions to use "volatile void *" |
8960 | as the pointer parameter, so we shouldn't get any complaints from the | |
8961 | call to check_function_arguments what ever type the user used. */ | |
d0af78c5 | 8962 | function_args_iter_next (&iter); |
b9c74b4d | 8963 | ptype = TREE_TYPE (TREE_TYPE (VEC_index (tree, params, 0))); |
b6a5fc45 | 8964 | |
8965 | /* For the rest of the values, we need to cast these to FTYPE, so that we | |
8966 | don't get warnings for passing pointer types, etc. */ | |
b9c74b4d | 8967 | parmnum = 0; |
d0af78c5 | 8968 | while (1) |
b6a5fc45 | 8969 | { |
d0af78c5 | 8970 | tree val, arg_type; |
8971 | ||
8972 | arg_type = function_args_iter_cond (&iter); | |
8973 | /* XXX void_type_node belies the abstraction. */ | |
8974 | if (arg_type == void_type_node) | |
8975 | break; | |
b6a5fc45 | 8976 | |
b9c74b4d | 8977 | ++parmnum; |
8978 | if (VEC_length (tree, params) <= parmnum) | |
b6a5fc45 | 8979 | { |
8980 | error ("too few arguments to function %qE", orig_function); | |
8981 | return false; | |
8982 | } | |
8983 | ||
8984 | /* ??? Ideally for the first conversion we'd use convert_for_assignment | |
8985 | so that we get warnings for anything that doesn't match the pointer | |
8986 | type. This isn't portable across the C and C++ front ends atm. */ | |
b9c74b4d | 8987 | val = VEC_index (tree, params, parmnum); |
b6a5fc45 | 8988 | val = convert (ptype, val); |
d0af78c5 | 8989 | val = convert (arg_type, val); |
b9c74b4d | 8990 | VEC_replace (tree, params, parmnum, val); |
b6a5fc45 | 8991 | |
d0af78c5 | 8992 | function_args_iter_next (&iter); |
b6a5fc45 | 8993 | } |
8994 | ||
8995 | /* The definition of these primitives is variadic, with the remaining | |
8996 | being "an optional list of variables protected by the memory barrier". | |
8997 | No clue what that's supposed to mean, precisely, but we consider all | |
8998 | call-clobbered variables to be protected so we're safe. */ | |
b9c74b4d | 8999 | VEC_truncate (tree, params, parmnum + 1); |
b6a5fc45 | 9000 | |
9001 | return true; | |
9002 | } | |
9003 | ||
a0c938f0 | 9004 | /* A helper function for resolve_overloaded_builtin. Adds a cast to |
b6a5fc45 | 9005 | RESULT to make it match the type of the first pointer argument in |
9006 | PARAMS. */ | |
9007 | ||
9008 | static tree | |
b9c74b4d | 9009 | sync_resolve_return (tree first_param, tree result) |
b6a5fc45 | 9010 | { |
b9c74b4d | 9011 | tree ptype = TREE_TYPE (TREE_TYPE (first_param)); |
10080eac | 9012 | ptype = TYPE_MAIN_VARIANT (ptype); |
b6a5fc45 | 9013 | return convert (ptype, result); |
9014 | } | |
9015 | ||
9016 | /* Some builtin functions are placeholders for other expressions. This | |
9017 | function should be called immediately after parsing the call expression | |
9018 | before surrounding code has committed to the type of the expression. | |
9019 | ||
e60a6f7b | 9020 | LOC is the location of the builtin call. |
9021 | ||
b6a5fc45 | 9022 | FUNCTION is the DECL that has been invoked; it is known to be a builtin. |
9023 | PARAMS is the argument list for the call. The return value is non-null | |
9024 | when expansion is complete, and null if normal processing should | |
9025 | continue. */ | |
9026 | ||
9027 | tree | |
e60a6f7b | 9028 | resolve_overloaded_builtin (location_t loc, tree function, VEC(tree,gc) *params) |
b6a5fc45 | 9029 | { |
9030 | enum built_in_function orig_code = DECL_FUNCTION_CODE (function); | |
65441f6f | 9031 | switch (DECL_BUILT_IN_CLASS (function)) |
9032 | { | |
9033 | case BUILT_IN_NORMAL: | |
9034 | break; | |
9035 | case BUILT_IN_MD: | |
9036 | if (targetm.resolve_overloaded_builtin) | |
e60a6f7b | 9037 | return targetm.resolve_overloaded_builtin (loc, function, params); |
65441f6f | 9038 | else |
a0c938f0 | 9039 | return NULL_TREE; |
65441f6f | 9040 | default: |
9041 | return NULL_TREE; | |
9042 | } | |
a0c938f0 | 9043 | |
65441f6f | 9044 | /* Handle BUILT_IN_NORMAL here. */ |
b6a5fc45 | 9045 | switch (orig_code) |
9046 | { | |
2797f13a | 9047 | case BUILT_IN_SYNC_FETCH_AND_ADD_N: |
9048 | case BUILT_IN_SYNC_FETCH_AND_SUB_N: | |
9049 | case BUILT_IN_SYNC_FETCH_AND_OR_N: | |
9050 | case BUILT_IN_SYNC_FETCH_AND_AND_N: | |
9051 | case BUILT_IN_SYNC_FETCH_AND_XOR_N: | |
9052 | case BUILT_IN_SYNC_FETCH_AND_NAND_N: | |
9053 | case BUILT_IN_SYNC_ADD_AND_FETCH_N: | |
9054 | case BUILT_IN_SYNC_SUB_AND_FETCH_N: | |
9055 | case BUILT_IN_SYNC_OR_AND_FETCH_N: | |
9056 | case BUILT_IN_SYNC_AND_AND_FETCH_N: | |
9057 | case BUILT_IN_SYNC_XOR_AND_FETCH_N: | |
9058 | case BUILT_IN_SYNC_NAND_AND_FETCH_N: | |
9059 | case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N: | |
9060 | case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N: | |
9061 | case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N: | |
9062 | case BUILT_IN_SYNC_LOCK_RELEASE_N: | |
b6a5fc45 | 9063 | { |
9064 | int n = sync_resolve_size (function, params); | |
b9c74b4d | 9065 | tree new_function, first_param, result; |
b6a5fc45 | 9066 | |
9067 | if (n == 0) | |
9068 | return error_mark_node; | |
9069 | ||
9070 | new_function = built_in_decls[orig_code + exact_log2 (n) + 1]; | |
9071 | if (!sync_resolve_params (function, new_function, params)) | |
9072 | return error_mark_node; | |
9073 | ||
b9c74b4d | 9074 | first_param = VEC_index (tree, params, 0); |
e60a6f7b | 9075 | result = build_function_call_vec (loc, new_function, params, NULL); |
2797f13a | 9076 | if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N |
9077 | && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N) | |
b9c74b4d | 9078 | result = sync_resolve_return (first_param, result); |
b6a5fc45 | 9079 | |
9080 | return result; | |
9081 | } | |
9082 | ||
9083 | default: | |
65441f6f | 9084 | return NULL_TREE; |
b6a5fc45 | 9085 | } |
9086 | } | |
9087 | ||
491255f5 | 9088 | /* Ignoring their sign, return true if two scalar types are the same. */ |
9089 | bool | |
9090 | same_scalar_type_ignoring_signedness (tree t1, tree t2) | |
9091 | { | |
9092 | enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); | |
9093 | ||
9421ebb9 | 9094 | gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE) |
9095 | && (c2 == INTEGER_TYPE || c2 == REAL_TYPE | |
9096 | || c2 == FIXED_POINT_TYPE)); | |
491255f5 | 9097 | |
9098 | /* Equality works here because c_common_signed_type uses | |
9099 | TYPE_MAIN_VARIANT. */ | |
11773141 | 9100 | return c_common_signed_type (t1) |
9101 | == c_common_signed_type (t2); | |
491255f5 | 9102 | } |
9103 | ||
be7350e7 | 9104 | /* Check for missing format attributes on function pointers. LTYPE is |
9105 | the new type or left-hand side type. RTYPE is the old type or | |
9106 | right-hand side type. Returns TRUE if LTYPE is missing the desired | |
9107 | attribute. */ | |
9108 | ||
9109 | bool | |
9110 | check_missing_format_attribute (tree ltype, tree rtype) | |
9111 | { | |
9112 | tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype); | |
9113 | tree ra; | |
9114 | ||
9115 | for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra)) | |
9116 | if (is_attribute_p ("format", TREE_PURPOSE (ra))) | |
9117 | break; | |
9118 | if (ra) | |
9119 | { | |
9120 | tree la; | |
9121 | for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la)) | |
9122 | if (is_attribute_p ("format", TREE_PURPOSE (la))) | |
9123 | break; | |
9124 | return !la; | |
9125 | } | |
9126 | else | |
9127 | return false; | |
9128 | } | |
9129 | ||
2840aae4 | 9130 | /* Subscripting with type char is likely to lose on a machine where |
9131 | chars are signed. So warn on any machine, but optionally. Don't | |
9132 | warn for unsigned char since that type is safe. Don't warn for | |
9133 | signed char because anyone who uses that must have done so | |
9134 | deliberately. Furthermore, we reduce the false positive load by | |
9135 | warning only for non-constant value of type char. */ | |
9136 | ||
9137 | void | |
9138 | warn_array_subscript_with_type_char (tree index) | |
9139 | { | |
9140 | if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node | |
9141 | && TREE_CODE (index) != INTEGER_CST) | |
9142 | warning (OPT_Wchar_subscripts, "array subscript has type %<char%>"); | |
9143 | } | |
9144 | ||
e534436e | 9145 | /* Implement -Wparentheses for the unexpected C precedence rules, to |
9146 | cover cases like x + y << z which readers are likely to | |
9147 | misinterpret. We have seen an expression in which CODE is a binary | |
82012ffe | 9148 | operator used to combine expressions ARG_LEFT and ARG_RIGHT, which |
9149 | before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and | |
9150 | CODE_RIGHT may be ERROR_MARK, which means that that side of the | |
9151 | expression was not formed using a binary or unary operator, or it | |
9152 | was enclosed in parentheses. */ | |
e534436e | 9153 | |
9154 | void | |
82012ffe | 9155 | warn_about_parentheses (enum tree_code code, |
269f7979 | 9156 | enum tree_code code_left, tree arg_left, |
82012ffe | 9157 | enum tree_code code_right, tree arg_right) |
e534436e | 9158 | { |
9159 | if (!warn_parentheses) | |
9160 | return; | |
9161 | ||
82012ffe | 9162 | /* This macro tests that the expression ARG with original tree code |
9163 | CODE appears to be a boolean expression. or the result of folding a | |
9164 | boolean expression. */ | |
9165 | #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \ | |
9166 | (truth_value_p (TREE_CODE (ARG)) \ | |
9167 | || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \ | |
9168 | /* Folding may create 0 or 1 integers from other expressions. */ \ | |
9169 | || ((CODE) != INTEGER_CST \ | |
9170 | && (integer_onep (ARG) || integer_zerop (ARG)))) | |
9171 | ||
48e1416a | 9172 | switch (code) |
e534436e | 9173 | { |
82012ffe | 9174 | case LSHIFT_EXPR: |
9175 | if (code_left == PLUS_EXPR || code_right == PLUS_EXPR) | |
e534436e | 9176 | warning (OPT_Wparentheses, |
82012ffe | 9177 | "suggest parentheses around %<+%> inside %<<<%>"); |
9178 | else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR) | |
9179 | warning (OPT_Wparentheses, | |
9180 | "suggest parentheses around %<-%> inside %<<<%>"); | |
9181 | return; | |
e534436e | 9182 | |
82012ffe | 9183 | case RSHIFT_EXPR: |
9184 | if (code_left == PLUS_EXPR || code_right == PLUS_EXPR) | |
e534436e | 9185 | warning (OPT_Wparentheses, |
82012ffe | 9186 | "suggest parentheses around %<+%> inside %<>>%>"); |
9187 | else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR) | |
9188 | warning (OPT_Wparentheses, | |
9189 | "suggest parentheses around %<-%> inside %<>>%>"); | |
9190 | return; | |
e534436e | 9191 | |
82012ffe | 9192 | case TRUTH_ORIF_EXPR: |
9193 | if (code_left == TRUTH_ANDIF_EXPR || code_right == TRUTH_ANDIF_EXPR) | |
9194 | warning (OPT_Wparentheses, | |
9195 | "suggest parentheses around %<&&%> within %<||%>"); | |
9196 | return; | |
9197 | ||
9198 | case BIT_IOR_EXPR: | |
e534436e | 9199 | if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR |
9200 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR | |
9201 | || code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR | |
9202 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
9203 | warning (OPT_Wparentheses, | |
82012ffe | 9204 | "suggest parentheses around arithmetic in operand of %<|%>"); |
e534436e | 9205 | /* Check cases like x|y==z */ |
82012ffe | 9206 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison |
9207 | || TREE_CODE_CLASS (code_right) == tcc_comparison) | |
e534436e | 9208 | warning (OPT_Wparentheses, |
82012ffe | 9209 | "suggest parentheses around comparison in operand of %<|%>"); |
9210 | /* Check cases like !x | y */ | |
9211 | else if (code_left == TRUTH_NOT_EXPR | |
9212 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
96bda40f | 9213 | warning (OPT_Wparentheses, "suggest parentheses around operand of " |
82012ffe | 9214 | "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>"); |
9215 | return; | |
e534436e | 9216 | |
82012ffe | 9217 | case BIT_XOR_EXPR: |
e534436e | 9218 | if (code_left == BIT_AND_EXPR |
9219 | || code_left == PLUS_EXPR || code_left == MINUS_EXPR | |
9220 | || code_right == BIT_AND_EXPR | |
9221 | || code_right == PLUS_EXPR || code_right == MINUS_EXPR) | |
9222 | warning (OPT_Wparentheses, | |
82012ffe | 9223 | "suggest parentheses around arithmetic in operand of %<^%>"); |
e534436e | 9224 | /* Check cases like x^y==z */ |
82012ffe | 9225 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison |
9226 | || TREE_CODE_CLASS (code_right) == tcc_comparison) | |
e534436e | 9227 | warning (OPT_Wparentheses, |
82012ffe | 9228 | "suggest parentheses around comparison in operand of %<^%>"); |
9229 | return; | |
e534436e | 9230 | |
82012ffe | 9231 | case BIT_AND_EXPR: |
9232 | if (code_left == PLUS_EXPR || code_right == PLUS_EXPR) | |
9233 | warning (OPT_Wparentheses, | |
9234 | "suggest parentheses around %<+%> in operand of %<&%>"); | |
9235 | else if (code_left == MINUS_EXPR || code_right == MINUS_EXPR) | |
e534436e | 9236 | warning (OPT_Wparentheses, |
82012ffe | 9237 | "suggest parentheses around %<-%> in operand of %<&%>"); |
e534436e | 9238 | /* Check cases like x&y==z */ |
82012ffe | 9239 | else if (TREE_CODE_CLASS (code_left) == tcc_comparison |
9240 | || TREE_CODE_CLASS (code_right) == tcc_comparison) | |
e534436e | 9241 | warning (OPT_Wparentheses, |
82012ffe | 9242 | "suggest parentheses around comparison in operand of %<&%>"); |
9243 | /* Check cases like !x & y */ | |
9244 | else if (code_left == TRUTH_NOT_EXPR | |
9245 | && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right)) | |
96bda40f | 9246 | warning (OPT_Wparentheses, "suggest parentheses around operand of " |
82012ffe | 9247 | "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>"); |
9248 | return; | |
e534436e | 9249 | |
82012ffe | 9250 | case EQ_EXPR: |
6ce0c450 | 9251 | if (TREE_CODE_CLASS (code_left) == tcc_comparison |
9252 | || TREE_CODE_CLASS (code_right) == tcc_comparison) | |
9253 | warning (OPT_Wparentheses, | |
82012ffe | 9254 | "suggest parentheses around comparison in operand of %<==%>"); |
9255 | return; | |
9256 | case NE_EXPR: | |
9257 | if (TREE_CODE_CLASS (code_left) == tcc_comparison | |
9258 | || TREE_CODE_CLASS (code_right) == tcc_comparison) | |
9259 | warning (OPT_Wparentheses, | |
9260 | "suggest parentheses around comparison in operand of %<!=%>"); | |
9261 | return; | |
9262 | ||
9263 | default: | |
9264 | if (TREE_CODE_CLASS (code) == tcc_comparison | |
9265 | && ((TREE_CODE_CLASS (code_left) == tcc_comparison | |
269f7979 | 9266 | && code_left != NE_EXPR && code_left != EQ_EXPR |
9267 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_left))) | |
82012ffe | 9268 | || (TREE_CODE_CLASS (code_right) == tcc_comparison |
269f7979 | 9269 | && code_right != NE_EXPR && code_right != EQ_EXPR |
9270 | && INTEGRAL_TYPE_P (TREE_TYPE (arg_right))))) | |
82012ffe | 9271 | warning (OPT_Wparentheses, "comparisons like %<X<=Y<=Z%> do not " |
6ce0c450 | 9272 | "have their mathematical meaning"); |
82012ffe | 9273 | return; |
6ce0c450 | 9274 | } |
82012ffe | 9275 | #undef NOT_A_BOOLEAN_EXPR_P |
e534436e | 9276 | } |
9277 | ||
92fccaaa | 9278 | /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */ |
9279 | ||
9280 | void | |
9281 | warn_for_unused_label (tree label) | |
9282 | { | |
9283 | if (!TREE_USED (label)) | |
9284 | { | |
9285 | if (DECL_INITIAL (label)) | |
9286 | warning (OPT_Wunused_label, "label %q+D defined but not used", label); | |
9287 | else | |
9288 | warning (OPT_Wunused_label, "label %q+D declared but not defined", label); | |
9289 | } | |
9290 | } | |
2840aae4 | 9291 | |
b6889cb0 | 9292 | /* Warn for division by zero according to the value of DIVISOR. LOC |
9293 | is the location of the division operator. */ | |
f092582b | 9294 | |
9295 | void | |
b6889cb0 | 9296 | warn_for_div_by_zero (location_t loc, tree divisor) |
f092582b | 9297 | { |
9421ebb9 | 9298 | /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning |
9299 | about division by zero. Do not issue a warning if DIVISOR has a | |
f092582b | 9300 | floating-point type, since we consider 0.0/0.0 a valid way of |
9301 | generating a NaN. */ | |
48d94ede | 9302 | if (c_inhibit_evaluation_warnings == 0 |
9421ebb9 | 9303 | && (integer_zerop (divisor) || fixed_zerop (divisor))) |
b6889cb0 | 9304 | warning_at (loc, OPT_Wdiv_by_zero, "division by zero"); |
f092582b | 9305 | } |
9306 | ||
13869a99 | 9307 | /* Subroutine of build_binary_op. Give warnings for comparisons |
9308 | between signed and unsigned quantities that may fail. Do the | |
9309 | checking based on the original operand trees ORIG_OP0 and ORIG_OP1, | |
9310 | so that casts will be considered, but default promotions won't | |
8e70fb09 | 9311 | be. |
9312 | ||
9313 | LOCATION is the location of the comparison operator. | |
13869a99 | 9314 | |
9315 | The arguments of this function map directly to local variables | |
9316 | of build_binary_op. */ | |
9317 | ||
48e1416a | 9318 | void |
8e70fb09 | 9319 | warn_for_sign_compare (location_t location, |
48e1416a | 9320 | tree orig_op0, tree orig_op1, |
9321 | tree op0, tree op1, | |
13869a99 | 9322 | tree result_type, enum tree_code resultcode) |
9323 | { | |
9324 | int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0)); | |
9325 | int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1)); | |
9326 | int unsignedp0, unsignedp1; | |
48e1416a | 9327 | |
13869a99 | 9328 | /* In C++, check for comparison of different enum types. */ |
9329 | if (c_dialect_cxx() | |
9330 | && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE | |
9331 | && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE | |
9332 | && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0)) | |
895b662f | 9333 | != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1))) |
13869a99 | 9334 | { |
8e70fb09 | 9335 | warning_at (location, |
9336 | OPT_Wsign_compare, "comparison between types %qT and %qT", | |
9337 | TREE_TYPE (orig_op0), TREE_TYPE (orig_op1)); | |
13869a99 | 9338 | } |
9339 | ||
9340 | /* Do not warn if the comparison is being done in a signed type, | |
9341 | since the signed type will only be chosen if it can represent | |
9342 | all the values of the unsigned type. */ | |
9343 | if (!TYPE_UNSIGNED (result_type)) | |
9344 | /* OK */; | |
9345 | /* Do not warn if both operands are unsigned. */ | |
9346 | else if (op0_signed == op1_signed) | |
9347 | /* OK */; | |
9348 | else | |
9349 | { | |
895b662f | 9350 | tree sop, uop, base_type; |
13869a99 | 9351 | bool ovf; |
895b662f | 9352 | |
13869a99 | 9353 | if (op0_signed) |
9354 | sop = orig_op0, uop = orig_op1; | |
48e1416a | 9355 | else |
13869a99 | 9356 | sop = orig_op1, uop = orig_op0; |
9357 | ||
48e1416a | 9358 | STRIP_TYPE_NOPS (sop); |
13869a99 | 9359 | STRIP_TYPE_NOPS (uop); |
895b662f | 9360 | base_type = (TREE_CODE (result_type) == COMPLEX_TYPE |
9361 | ? TREE_TYPE (result_type) : result_type); | |
13869a99 | 9362 | |
9363 | /* Do not warn if the signed quantity is an unsuffixed integer | |
9364 | literal (or some static constant expression involving such | |
9365 | literals or a conditional expression involving such literals) | |
9366 | and it is non-negative. */ | |
9367 | if (tree_expr_nonnegative_warnv_p (sop, &ovf)) | |
9368 | /* OK */; | |
9369 | /* Do not warn if the comparison is an equality operation, the | |
9370 | unsigned quantity is an integral constant, and it would fit | |
9371 | in the result if the result were signed. */ | |
9372 | else if (TREE_CODE (uop) == INTEGER_CST | |
9373 | && (resultcode == EQ_EXPR || resultcode == NE_EXPR) | |
895b662f | 9374 | && int_fits_type_p (uop, c_common_signed_type (base_type))) |
13869a99 | 9375 | /* OK */; |
9376 | /* In C, do not warn if the unsigned quantity is an enumeration | |
9377 | constant and its maximum value would fit in the result if the | |
9378 | result were signed. */ | |
9379 | else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST | |
9380 | && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE | |
9381 | && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)), | |
895b662f | 9382 | c_common_signed_type (base_type))) |
13869a99 | 9383 | /* OK */; |
48e1416a | 9384 | else |
8e70fb09 | 9385 | warning_at (location, |
48e1416a | 9386 | OPT_Wsign_compare, |
8e70fb09 | 9387 | "comparison between signed and unsigned integer expressions"); |
13869a99 | 9388 | } |
48e1416a | 9389 | |
13869a99 | 9390 | /* Warn if two unsigned values are being compared in a size larger |
9391 | than their original size, and one (and only one) is the result of | |
9392 | a `~' operator. This comparison will always fail. | |
48e1416a | 9393 | |
13869a99 | 9394 | Also warn if one operand is a constant, and the constant does not |
9395 | have all bits set that are set in the ~ operand when it is | |
9396 | extended. */ | |
9397 | ||
7f506bca | 9398 | op0 = c_common_get_narrower (op0, &unsignedp0); |
9399 | op1 = c_common_get_narrower (op1, &unsignedp1); | |
48e1416a | 9400 | |
13869a99 | 9401 | if ((TREE_CODE (op0) == BIT_NOT_EXPR) |
9402 | ^ (TREE_CODE (op1) == BIT_NOT_EXPR)) | |
9403 | { | |
9404 | if (TREE_CODE (op0) == BIT_NOT_EXPR) | |
7f506bca | 9405 | op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0); |
13869a99 | 9406 | if (TREE_CODE (op1) == BIT_NOT_EXPR) |
7f506bca | 9407 | op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1); |
13869a99 | 9408 | |
9409 | if (host_integerp (op0, 0) || host_integerp (op1, 0)) | |
9410 | { | |
9411 | tree primop; | |
9412 | HOST_WIDE_INT constant, mask; | |
9413 | int unsignedp; | |
9414 | unsigned int bits; | |
48e1416a | 9415 | |
13869a99 | 9416 | if (host_integerp (op0, 0)) |
9417 | { | |
9418 | primop = op1; | |
9419 | unsignedp = unsignedp1; | |
9420 | constant = tree_low_cst (op0, 0); | |
9421 | } | |
9422 | else | |
9423 | { | |
9424 | primop = op0; | |
9425 | unsignedp = unsignedp0; | |
9426 | constant = tree_low_cst (op1, 0); | |
9427 | } | |
48e1416a | 9428 | |
13869a99 | 9429 | bits = TYPE_PRECISION (TREE_TYPE (primop)); |
9430 | if (bits < TYPE_PRECISION (result_type) | |
9431 | && bits < HOST_BITS_PER_LONG && unsignedp) | |
9432 | { | |
9433 | mask = (~ (HOST_WIDE_INT) 0) << bits; | |
9434 | if ((mask & constant) != mask) | |
9435 | { | |
9436 | if (constant == 0) | |
48e1416a | 9437 | warning (OPT_Wsign_compare, |
13869a99 | 9438 | "promoted ~unsigned is always non-zero"); |
9439 | else | |
48e1416a | 9440 | warning_at (location, OPT_Wsign_compare, |
8e70fb09 | 9441 | "comparison of promoted ~unsigned with constant"); |
13869a99 | 9442 | } |
9443 | } | |
9444 | } | |
9445 | else if (unsignedp0 && unsignedp1 | |
9446 | && (TYPE_PRECISION (TREE_TYPE (op0)) | |
9447 | < TYPE_PRECISION (result_type)) | |
9448 | && (TYPE_PRECISION (TREE_TYPE (op1)) | |
9449 | < TYPE_PRECISION (result_type))) | |
8e70fb09 | 9450 | warning_at (location, OPT_Wsign_compare, |
13869a99 | 9451 | "comparison of promoted ~unsigned with unsigned"); |
9452 | } | |
9453 | } | |
9454 | ||
5ba33bf4 | 9455 | /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common |
9456 | type via c_common_type. If -Wdouble-promotion is in use, and the | |
9457 | conditions for warning have been met, issue a warning. GMSGID is | |
9458 | the warning message. It must have two %T specifiers for the type | |
9459 | that was converted (generally "float") and the type to which it was | |
9460 | converted (generally "double), respectively. LOC is the location | |
9461 | to which the awrning should refer. */ | |
9462 | ||
9463 | void | |
9464 | do_warn_double_promotion (tree result_type, tree type1, tree type2, | |
9465 | const char *gmsgid, location_t loc) | |
9466 | { | |
9467 | tree source_type; | |
9468 | ||
9469 | if (!warn_double_promotion) | |
9470 | return; | |
9471 | /* If the conversion will not occur at run-time, there is no need to | |
9472 | warn about it. */ | |
9473 | if (c_inhibit_evaluation_warnings) | |
9474 | return; | |
9475 | if (TYPE_MAIN_VARIANT (result_type) != double_type_node | |
9476 | && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node) | |
9477 | return; | |
9478 | if (TYPE_MAIN_VARIANT (type1) == float_type_node | |
9479 | || TYPE_MAIN_VARIANT (type1) == complex_float_type_node) | |
9480 | source_type = type1; | |
9481 | else if (TYPE_MAIN_VARIANT (type2) == float_type_node | |
9482 | || TYPE_MAIN_VARIANT (type2) == complex_float_type_node) | |
9483 | source_type = type2; | |
9484 | else | |
9485 | return; | |
9486 | warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type); | |
9487 | } | |
9488 | ||
41771881 | 9489 | /* Setup a TYPE_DECL node as a typedef representation. |
9490 | ||
9491 | X is a TYPE_DECL for a typedef statement. Create a brand new | |
9492 | ..._TYPE node (which will be just a variant of the existing | |
9493 | ..._TYPE node with identical properties) and then install X | |
9494 | as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. | |
9495 | ||
9496 | The whole point here is to end up with a situation where each | |
9497 | and every ..._TYPE node the compiler creates will be uniquely | |
9498 | associated with AT MOST one node representing a typedef name. | |
9499 | This way, even though the compiler substitutes corresponding | |
9500 | ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very | |
9501 | early on, later parts of the compiler can always do the reverse | |
9502 | translation and get back the corresponding typedef name. For | |
9503 | example, given: | |
9504 | ||
9505 | typedef struct S MY_TYPE; | |
9506 | MY_TYPE object; | |
9507 | ||
9508 | Later parts of the compiler might only know that `object' was of | |
9509 | type `struct S' if it were not for code just below. With this | |
9510 | code however, later parts of the compiler see something like: | |
9511 | ||
9512 | struct S' == struct S | |
9513 | typedef struct S' MY_TYPE; | |
9514 | struct S' object; | |
9515 | ||
9516 | And they can then deduce (from the node for type struct S') that | |
9517 | the original object declaration was: | |
9518 | ||
9519 | MY_TYPE object; | |
9520 | ||
9521 | Being able to do this is important for proper support of protoize, | |
9522 | and also for generating precise symbolic debugging information | |
9523 | which takes full account of the programmer's (typedef) vocabulary. | |
9524 | ||
9525 | Obviously, we don't want to generate a duplicate ..._TYPE node if | |
9526 | the TYPE_DECL node that we are now processing really represents a | |
9527 | standard built-in type. */ | |
9528 | ||
9529 | void | |
9530 | set_underlying_type (tree x) | |
9531 | { | |
9532 | if (x == error_mark_node) | |
9533 | return; | |
9534 | if (DECL_IS_BUILTIN (x)) | |
9535 | { | |
9536 | if (TYPE_NAME (TREE_TYPE (x)) == 0) | |
9537 | TYPE_NAME (TREE_TYPE (x)) = x; | |
9538 | } | |
9539 | else if (TREE_TYPE (x) != error_mark_node | |
9540 | && DECL_ORIGINAL_TYPE (x) == NULL_TREE) | |
9541 | { | |
9542 | tree tt = TREE_TYPE (x); | |
9543 | DECL_ORIGINAL_TYPE (x) = tt; | |
9544 | tt = build_variant_type_copy (tt); | |
9545 | TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
9546 | TYPE_NAME (tt) = x; | |
9547 | TREE_USED (tt) = TREE_USED (x); | |
9548 | TREE_TYPE (x) = tt; | |
9549 | } | |
9550 | } | |
9551 | ||
1a4c44c5 | 9552 | /* Record the types used by the current global variable declaration |
9553 | being parsed, so that we can decide later to emit their debug info. | |
9554 | Those types are in types_used_by_cur_var_decl, and we are going to | |
9555 | store them in the types_used_by_vars_hash hash table. | |
9556 | DECL is the declaration of the global variable that has been parsed. */ | |
9557 | ||
9558 | void | |
9559 | record_types_used_by_current_var_decl (tree decl) | |
9560 | { | |
9561 | gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl)); | |
9562 | ||
aef48c9a | 9563 | while (!VEC_empty (tree, types_used_by_cur_var_decl)) |
1a4c44c5 | 9564 | { |
aef48c9a | 9565 | tree type = VEC_pop (tree, types_used_by_cur_var_decl); |
9566 | types_used_by_var_decl_insert (type, decl); | |
1a4c44c5 | 9567 | } |
9568 | } | |
9569 | ||
f352a3fb | 9570 | /* The C and C++ parsers both use vectors to hold function arguments. |
9571 | For efficiency, we keep a cache of unused vectors. This is the | |
9572 | cache. */ | |
9573 | ||
9574 | typedef VEC(tree,gc)* tree_gc_vec; | |
9575 | DEF_VEC_P(tree_gc_vec); | |
9576 | DEF_VEC_ALLOC_P(tree_gc_vec,gc); | |
9577 | static GTY((deletable)) VEC(tree_gc_vec,gc) *tree_vector_cache; | |
9578 | ||
9579 | /* Return a new vector from the cache. If the cache is empty, | |
9580 | allocate a new vector. These vectors are GC'ed, so it is OK if the | |
9581 | pointer is not released.. */ | |
9582 | ||
9583 | VEC(tree,gc) * | |
9584 | make_tree_vector (void) | |
9585 | { | |
9586 | if (!VEC_empty (tree_gc_vec, tree_vector_cache)) | |
9587 | return VEC_pop (tree_gc_vec, tree_vector_cache); | |
9588 | else | |
9589 | { | |
9590 | /* Passing 0 to VEC_alloc returns NULL, and our callers require | |
9591 | that we always return a non-NULL value. The vector code uses | |
9592 | 4 when growing a NULL vector, so we do too. */ | |
9593 | return VEC_alloc (tree, gc, 4); | |
9594 | } | |
9595 | } | |
9596 | ||
9597 | /* Release a vector of trees back to the cache. */ | |
9598 | ||
9599 | void | |
9600 | release_tree_vector (VEC(tree,gc) *vec) | |
9601 | { | |
9602 | if (vec != NULL) | |
9603 | { | |
9604 | VEC_truncate (tree, vec, 0); | |
9605 | VEC_safe_push (tree_gc_vec, gc, tree_vector_cache, vec); | |
9606 | } | |
9607 | } | |
9608 | ||
9609 | /* Get a new tree vector holding a single tree. */ | |
9610 | ||
9611 | VEC(tree,gc) * | |
9612 | make_tree_vector_single (tree t) | |
9613 | { | |
9614 | VEC(tree,gc) *ret = make_tree_vector (); | |
9615 | VEC_quick_push (tree, ret, t); | |
9616 | return ret; | |
9617 | } | |
9618 | ||
c66c81be | 9619 | /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */ |
9620 | ||
9621 | VEC(tree,gc) * | |
9622 | make_tree_vector_from_list (tree list) | |
9623 | { | |
9624 | VEC(tree,gc) *ret = make_tree_vector (); | |
9625 | for (; list; list = TREE_CHAIN (list)) | |
9626 | VEC_safe_push (tree, gc, ret, TREE_VALUE (list)); | |
9627 | return ret; | |
9628 | } | |
9629 | ||
f352a3fb | 9630 | /* Get a new tree vector which is a copy of an existing one. */ |
9631 | ||
9632 | VEC(tree,gc) * | |
9633 | make_tree_vector_copy (const VEC(tree,gc) *orig) | |
9634 | { | |
9635 | VEC(tree,gc) *ret; | |
9636 | unsigned int ix; | |
9637 | tree t; | |
9638 | ||
9639 | ret = make_tree_vector (); | |
9640 | VEC_reserve (tree, gc, ret, VEC_length (tree, orig)); | |
48148244 | 9641 | FOR_EACH_VEC_ELT (tree, orig, ix, t) |
f352a3fb | 9642 | VEC_quick_push (tree, ret, t); |
9643 | return ret; | |
9644 | } | |
9645 | ||
a9ffdd35 | 9646 | /* Return true if KEYWORD starts a type specifier. */ |
9647 | ||
9648 | bool | |
9649 | keyword_begins_type_specifier (enum rid keyword) | |
9650 | { | |
9651 | switch (keyword) | |
9652 | { | |
9653 | case RID_INT: | |
9654 | case RID_CHAR: | |
9655 | case RID_FLOAT: | |
9656 | case RID_DOUBLE: | |
9657 | case RID_VOID: | |
9658 | case RID_INT128: | |
9659 | case RID_UNSIGNED: | |
9660 | case RID_LONG: | |
9661 | case RID_SHORT: | |
9662 | case RID_SIGNED: | |
9663 | case RID_DFLOAT32: | |
9664 | case RID_DFLOAT64: | |
9665 | case RID_DFLOAT128: | |
9666 | case RID_FRACT: | |
9667 | case RID_ACCUM: | |
9668 | case RID_BOOL: | |
9669 | case RID_WCHAR: | |
9670 | case RID_CHAR16: | |
9671 | case RID_CHAR32: | |
9672 | case RID_SAT: | |
9673 | case RID_COMPLEX: | |
9674 | case RID_TYPEOF: | |
9675 | case RID_STRUCT: | |
9676 | case RID_CLASS: | |
9677 | case RID_UNION: | |
9678 | case RID_ENUM: | |
9679 | return true; | |
9680 | default: | |
9681 | return false; | |
9682 | } | |
9683 | } | |
9684 | ||
9685 | /* Return true if KEYWORD names a type qualifier. */ | |
9686 | ||
9687 | bool | |
9688 | keyword_is_type_qualifier (enum rid keyword) | |
9689 | { | |
9690 | switch (keyword) | |
9691 | { | |
9692 | case RID_CONST: | |
9693 | case RID_VOLATILE: | |
9694 | case RID_RESTRICT: | |
9695 | return true; | |
9696 | default: | |
9697 | return false; | |
9698 | } | |
9699 | } | |
9700 | ||
9701 | /* Return true if KEYWORD names a storage class specifier. | |
9702 | ||
9703 | RID_TYPEDEF is not included in this list despite `typedef' being | |
9704 | listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as | |
9705 | such for syntactic convenience only. */ | |
9706 | ||
9707 | bool | |
9708 | keyword_is_storage_class_specifier (enum rid keyword) | |
9709 | { | |
9710 | switch (keyword) | |
9711 | { | |
9712 | case RID_STATIC: | |
9713 | case RID_EXTERN: | |
9714 | case RID_REGISTER: | |
9715 | case RID_AUTO: | |
9716 | case RID_MUTABLE: | |
9717 | case RID_THREAD: | |
9718 | return true; | |
9719 | default: | |
9720 | return false; | |
9721 | } | |
9722 | } | |
9723 | ||
fad3f658 | 9724 | /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */ |
9725 | ||
9726 | static bool | |
9727 | keyword_is_function_specifier (enum rid keyword) | |
9728 | { | |
9729 | switch (keyword) | |
9730 | { | |
9731 | case RID_INLINE: | |
9732 | case RID_VIRTUAL: | |
9733 | case RID_EXPLICIT: | |
9734 | return true; | |
9735 | default: | |
9736 | return false; | |
9737 | } | |
9738 | } | |
9739 | ||
9740 | /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a | |
9741 | declaration-specifier (C99 6.7). */ | |
9742 | ||
9743 | bool | |
9744 | keyword_is_decl_specifier (enum rid keyword) | |
9745 | { | |
9746 | if (keyword_is_storage_class_specifier (keyword) | |
9747 | || keyword_is_type_qualifier (keyword) | |
9748 | || keyword_is_function_specifier (keyword)) | |
9749 | return true; | |
9750 | ||
9751 | switch (keyword) | |
9752 | { | |
9753 | case RID_TYPEDEF: | |
9754 | case RID_FRIEND: | |
9755 | case RID_CONSTEXPR: | |
9756 | return true; | |
9757 | default: | |
9758 | return false; | |
9759 | } | |
9760 | } | |
9761 | ||
9b88d08d | 9762 | /* Initialize language-specific-bits of tree_contains_struct. */ |
9763 | ||
9764 | void | |
9765 | c_common_init_ts (void) | |
9766 | { | |
9767 | MARK_TS_TYPED (C_MAYBE_CONST_EXPR); | |
9768 | MARK_TS_TYPED (EXCESS_PRECISION_EXPR); | |
9769 | } | |
9770 | ||
7bedc3a0 | 9771 | #include "gt-c-family-c-common.h" |