Do constant folding in warn_* functions.
[gcc.git] / gcc / c-family / c-common.c
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #define GCC_C_COMMON_C
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "target.h"
26 #include "function.h"
27 #include "tree.h"
28 #include "c-common.h"
29 #include "gimple-expr.h"
30 #include "tm_p.h"
31 #include "stringpool.h"
32 #include "cgraph.h"
33 #include "diagnostic.h"
34 #include "intl.h"
35 #include "stor-layout.h"
36 #include "calls.h"
37 #include "attribs.h"
38 #include "varasm.h"
39 #include "trans-mem.h"
40 #include "c-objc.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "tree-iterator.h"
46 #include "opts.h"
47 #include "gimplify.h"
48
49 cpp_reader *parse_in; /* Declared in c-pragma.h. */
50
51 /* Mode used to build pointers (VOIDmode means ptr_mode). */
52
53 machine_mode c_default_pointer_mode = VOIDmode;
54
55 /* The following symbols are subsumed in the c_global_trees array, and
56 listed here individually for documentation purposes.
57
58 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
59
60 tree short_integer_type_node;
61 tree long_integer_type_node;
62 tree long_long_integer_type_node;
63
64 tree short_unsigned_type_node;
65 tree long_unsigned_type_node;
66 tree long_long_unsigned_type_node;
67
68 tree truthvalue_type_node;
69 tree truthvalue_false_node;
70 tree truthvalue_true_node;
71
72 tree ptrdiff_type_node;
73
74 tree unsigned_char_type_node;
75 tree signed_char_type_node;
76 tree wchar_type_node;
77
78 tree char16_type_node;
79 tree char32_type_node;
80
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
90 tree dfloat32_type_node;
91 tree dfloat64_type_node;
92 tree_dfloat128_type_node;
93
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 `wchar_t[SOMENUMBER]' or something like it.
123 Used when a wide string literal is created.
124
125 tree wchar_array_type_node;
126
127 Type `char16_t[SOMENUMBER]' or something like it.
128 Used when a UTF-16 string literal is created.
129
130 tree char16_array_type_node;
131
132 Type `char32_t[SOMENUMBER]' or something like it.
133 Used when a UTF-32 string literal is created.
134
135 tree char32_array_type_node;
136
137 Type `int ()' -- used for implicit declaration of functions.
138
139 tree default_function_type;
140
141 A VOID_TYPE node, packaged in a TREE_LIST.
142
143 tree void_list_node;
144
145 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
146 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
147 VAR_DECLS, but C++ does.)
148
149 tree function_name_decl_node;
150 tree pretty_function_name_decl_node;
151 tree c99_function_name_decl_node;
152
153 Stack of nested function name VAR_DECLs.
154
155 tree saved_function_name_decls;
156
157 */
158
159 tree c_global_trees[CTI_MAX];
160 \f
161 /* Switches common to the C front ends. */
162
163 /* Nonzero means don't output line number information. */
164
165 char flag_no_line_commands;
166
167 /* Nonzero causes -E output not to be done, but directives such as
168 #define that have side effects are still obeyed. */
169
170 char flag_no_output;
171
172 /* Nonzero means dump macros in some fashion. */
173
174 char flag_dump_macros;
175
176 /* Nonzero means pass #include lines through to the output. */
177
178 char flag_dump_includes;
179
180 /* Nonzero means process PCH files while preprocessing. */
181
182 bool flag_pch_preprocess;
183
184 /* The file name to which we should write a precompiled header, or
185 NULL if no header will be written in this compile. */
186
187 const char *pch_file;
188
189 /* Nonzero if an ISO standard was selected. It rejects macros in the
190 user's namespace. */
191 int flag_iso;
192
193 /* C/ObjC language option variables. */
194
195
196 /* Nonzero means allow type mismatches in conditional expressions;
197 just make their values `void'. */
198
199 int flag_cond_mismatch;
200
201 /* Nonzero means enable C89 Amendment 1 features. */
202
203 int flag_isoc94;
204
205 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
206
207 int flag_isoc99;
208
209 /* Nonzero means use the ISO C11 dialect of C. */
210
211 int flag_isoc11;
212
213 /* Nonzero means that we have builtin functions, and main is an int. */
214
215 int flag_hosted = 1;
216
217
218 /* ObjC language option variables. */
219
220
221 /* Tells the compiler that this is a special run. Do not perform any
222 compiling, instead we are to test some platform dependent features
223 and output a C header file with appropriate definitions. */
224
225 int print_struct_values;
226
227 /* Tells the compiler what is the constant string class for ObjC. */
228
229 const char *constant_string_class_name;
230
231
232 /* C++ language option variables. */
233
234 /* The reference version of the ABI for -Wabi. */
235
236 int warn_abi_version = -1;
237
238 /* Nonzero means generate separate instantiation control files and
239 juggle them at link time. */
240
241 int flag_use_repository;
242
243 /* The C++ dialect being used. Default set in c_common_post_options. */
244
245 enum cxx_dialect cxx_dialect = cxx_unset;
246
247 /* Maximum template instantiation depth. This limit exists to limit the
248 time it takes to notice excessively recursive template instantiations.
249
250 The default is lower than the 1024 recommended by the C++0x standard
251 because G++ runs out of stack before 1024 with highly recursive template
252 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
253
254 int max_tinst_depth = 900;
255
256 /* The elements of `ridpointers' are identifier nodes for the reserved
257 type names and storage classes. It is indexed by a RID_... value. */
258 tree *ridpointers;
259
260 tree (*make_fname_decl) (location_t, tree, int);
261
262 /* Nonzero means don't warn about problems that occur when the code is
263 executed. */
264 int c_inhibit_evaluation_warnings;
265
266 /* Whether we are building a boolean conversion inside
267 convert_for_assignment, or some other late binary operation. If
268 build_binary_op is called for C (from code shared by C and C++) in
269 this case, then the operands have already been folded and the
270 result will not be folded again, so C_MAYBE_CONST_EXPR should not
271 be generated. */
272 bool in_late_binary_op;
273
274 /* Whether lexing has been completed, so subsequent preprocessor
275 errors should use the compiler's input_location. */
276 bool done_lexing = false;
277
278 /* Information about how a function name is generated. */
279 struct fname_var_t
280 {
281 tree *const decl; /* pointer to the VAR_DECL. */
282 const unsigned rid; /* RID number for the identifier. */
283 const int pretty; /* How pretty is it? */
284 };
285
286 /* The three ways of getting then name of the current function. */
287
288 const struct fname_var_t fname_vars[] =
289 {
290 /* C99 compliant __func__, must be first. */
291 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
292 /* GCC __FUNCTION__ compliant. */
293 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
294 /* GCC __PRETTY_FUNCTION__ compliant. */
295 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
296 {NULL, 0, 0},
297 };
298
299 /* Global visibility options. */
300 struct visibility_flags visibility_options;
301
302 static tree check_case_value (location_t, tree);
303 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
304 bool *);
305
306 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
312 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
313 int, bool *);
314 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
315 int, bool *);
316 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
317 bool *);
318 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_always_inline_attribute (tree *, tree, tree, int,
324 bool *);
325 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
332 bool *);
333 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
334 bool *);
335 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_transparent_union_attribute (tree *, tree, tree,
337 int, bool *);
338 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
339 int, bool *);
340 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
346 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
347 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
348 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
351 static tree handle_visibility_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_tls_model_attribute (tree *, tree, tree, int,
354 bool *);
355 static tree handle_no_instrument_function_attribute (tree *, tree,
356 tree, int, bool *);
357 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
360 bool *);
361 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_deprecated_attribute (tree *, tree, tree, int,
366 bool *);
367 static tree handle_vector_size_attribute (tree *, tree, tree, int,
368 bool *);
369 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
373 bool *);
374 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
382 static tree ignore_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
388 bool *);
389 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
391 bool *);
392 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
394 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
395 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
396
397 static void check_function_nonnull (tree, int, tree *);
398 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
399 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
400 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
401 static int resort_field_decl_cmp (const void *, const void *);
402
403 /* Reserved words. The third field is a mask: keywords are disabled
404 if they match the mask.
405
406 Masks for languages:
407 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
408 C --std=c99: D_CXXONLY | D_OBJC
409 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
410 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
411 C++ --std=c0x: D_CONLY | D_OBJC
412 ObjC++ is like C++ except that D_OBJC is not set
413
414 If -fno-asm is used, D_ASM is added to the mask. If
415 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
416 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
417 In C with -Wc++-compat, we warn if D_CXXWARN is set.
418
419 Note the complication of the D_CXX_OBJC keywords. These are
420 reserved words such as 'class'. In C++, 'class' is a reserved
421 word. In Objective-C++ it is too. In Objective-C, it is a
422 reserved word too, but only if it follows an '@' sign.
423 */
424 const struct c_common_resword c_common_reswords[] =
425 {
426 { "_Alignas", RID_ALIGNAS, D_CONLY },
427 { "_Alignof", RID_ALIGNOF, D_CONLY },
428 { "_Atomic", RID_ATOMIC, D_CONLY },
429 { "_Bool", RID_BOOL, D_CONLY },
430 { "_Complex", RID_COMPLEX, 0 },
431 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
432 { "_Cilk_sync", RID_CILK_SYNC, 0 },
433 { "_Cilk_for", RID_CILK_FOR, 0 },
434 { "_Imaginary", RID_IMAGINARY, D_CONLY },
435 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
436 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
437 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
438 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
439 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
440 { "_Sat", RID_SAT, D_CONLY | D_EXT },
441 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
442 { "_Noreturn", RID_NORETURN, D_CONLY },
443 { "_Generic", RID_GENERIC, D_CONLY },
444 { "_Thread_local", RID_THREAD, D_CONLY },
445 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
446 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
447 { "__alignof", RID_ALIGNOF, 0 },
448 { "__alignof__", RID_ALIGNOF, 0 },
449 { "__asm", RID_ASM, 0 },
450 { "__asm__", RID_ASM, 0 },
451 { "__attribute", RID_ATTRIBUTE, 0 },
452 { "__attribute__", RID_ATTRIBUTE, 0 },
453 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
454 { "__bases", RID_BASES, D_CXXONLY },
455 { "__builtin_call_with_static_chain",
456 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
457 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
458 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
459 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
460 { "__builtin_offsetof", RID_OFFSETOF, 0 },
461 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
462 { "__builtin_va_arg", RID_VA_ARG, 0 },
463 { "__complex", RID_COMPLEX, 0 },
464 { "__complex__", RID_COMPLEX, 0 },
465 { "__const", RID_CONST, 0 },
466 { "__const__", RID_CONST, 0 },
467 { "__decltype", RID_DECLTYPE, D_CXXONLY },
468 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
469 { "__extension__", RID_EXTENSION, 0 },
470 { "__func__", RID_C99_FUNCTION_NAME, 0 },
471 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
472 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
473 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
474 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
475 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
476 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
477 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
478 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
479 { "__imag", RID_IMAGPART, 0 },
480 { "__imag__", RID_IMAGPART, 0 },
481 { "__inline", RID_INLINE, 0 },
482 { "__inline__", RID_INLINE, 0 },
483 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
484 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
485 { "__is_class", RID_IS_CLASS, D_CXXONLY },
486 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
487 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
488 { "__is_final", RID_IS_FINAL, D_CXXONLY },
489 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
490 { "__is_pod", RID_IS_POD, D_CXXONLY },
491 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
492 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
493 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
494 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
495 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
496 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
497 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
498 { "__is_union", RID_IS_UNION, D_CXXONLY },
499 { "__label__", RID_LABEL, 0 },
500 { "__null", RID_NULL, 0 },
501 { "__real", RID_REALPART, 0 },
502 { "__real__", RID_REALPART, 0 },
503 { "__restrict", RID_RESTRICT, 0 },
504 { "__restrict__", RID_RESTRICT, 0 },
505 { "__signed", RID_SIGNED, 0 },
506 { "__signed__", RID_SIGNED, 0 },
507 { "__thread", RID_THREAD, 0 },
508 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
509 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
510 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
511 { "__typeof", RID_TYPEOF, 0 },
512 { "__typeof__", RID_TYPEOF, 0 },
513 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
514 { "__volatile", RID_VOLATILE, 0 },
515 { "__volatile__", RID_VOLATILE, 0 },
516 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
517 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
518 { "asm", RID_ASM, D_ASM },
519 { "auto", RID_AUTO, 0 },
520 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
521 { "break", RID_BREAK, 0 },
522 { "case", RID_CASE, 0 },
523 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
524 { "char", RID_CHAR, 0 },
525 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
526 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
527 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
528 { "const", RID_CONST, 0 },
529 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
530 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
531 { "continue", RID_CONTINUE, 0 },
532 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
533 { "default", RID_DEFAULT, 0 },
534 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
535 { "do", RID_DO, 0 },
536 { "double", RID_DOUBLE, 0 },
537 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
538 { "else", RID_ELSE, 0 },
539 { "enum", RID_ENUM, 0 },
540 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
541 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
542 { "extern", RID_EXTERN, 0 },
543 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
544 { "float", RID_FLOAT, 0 },
545 { "for", RID_FOR, 0 },
546 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
547 { "goto", RID_GOTO, 0 },
548 { "if", RID_IF, 0 },
549 { "inline", RID_INLINE, D_EXT89 },
550 { "int", RID_INT, 0 },
551 { "long", RID_LONG, 0 },
552 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
553 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
554 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
555 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
556 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
557 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
558 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
559 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
560 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
561 { "register", RID_REGISTER, 0 },
562 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
563 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
564 { "return", RID_RETURN, 0 },
565 { "short", RID_SHORT, 0 },
566 { "signed", RID_SIGNED, 0 },
567 { "sizeof", RID_SIZEOF, 0 },
568 { "static", RID_STATIC, 0 },
569 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
570 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
571 { "struct", RID_STRUCT, 0 },
572 { "switch", RID_SWITCH, 0 },
573 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
574 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
575 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
576 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
577 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
578 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
579 { "typedef", RID_TYPEDEF, 0 },
580 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
581 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
582 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
583 { "union", RID_UNION, 0 },
584 { "unsigned", RID_UNSIGNED, 0 },
585 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
586 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
587 { "void", RID_VOID, 0 },
588 { "volatile", RID_VOLATILE, 0 },
589 { "wchar_t", RID_WCHAR, D_CXXONLY },
590 { "while", RID_WHILE, 0 },
591
592 /* C++ transactional memory. */
593 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
594 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
595 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
596 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
597
598 /* Concepts-related keywords */
599 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
600 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
601
602 /* These Objective-C keywords are recognized only immediately after
603 an '@'. */
604 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
605 { "defs", RID_AT_DEFS, D_OBJC },
606 { "encode", RID_AT_ENCODE, D_OBJC },
607 { "end", RID_AT_END, D_OBJC },
608 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
609 { "interface", RID_AT_INTERFACE, D_OBJC },
610 { "protocol", RID_AT_PROTOCOL, D_OBJC },
611 { "selector", RID_AT_SELECTOR, D_OBJC },
612 { "finally", RID_AT_FINALLY, D_OBJC },
613 { "optional", RID_AT_OPTIONAL, D_OBJC },
614 { "required", RID_AT_REQUIRED, D_OBJC },
615 { "property", RID_AT_PROPERTY, D_OBJC },
616 { "package", RID_AT_PACKAGE, D_OBJC },
617 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
618 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
619 /* These are recognized only in protocol-qualifier context
620 (see above) */
621 { "bycopy", RID_BYCOPY, D_OBJC },
622 { "byref", RID_BYREF, D_OBJC },
623 { "in", RID_IN, D_OBJC },
624 { "inout", RID_INOUT, D_OBJC },
625 { "oneway", RID_ONEWAY, D_OBJC },
626 { "out", RID_OUT, D_OBJC },
627 /* These are recognized inside a property attribute list */
628 { "assign", RID_ASSIGN, D_OBJC },
629 { "copy", RID_COPY, D_OBJC },
630 { "getter", RID_GETTER, D_OBJC },
631 { "nonatomic", RID_NONATOMIC, D_OBJC },
632 { "readonly", RID_READONLY, D_OBJC },
633 { "readwrite", RID_READWRITE, D_OBJC },
634 { "retain", RID_RETAIN, D_OBJC },
635 { "setter", RID_SETTER, D_OBJC },
636 };
637
638 const unsigned int num_c_common_reswords =
639 sizeof c_common_reswords / sizeof (struct c_common_resword);
640
641 /* Table of machine-independent attributes common to all C-like languages.
642
643 All attributes referencing arguments should be additionally processed
644 in chkp_copy_function_type_adding_bounds for correct instrumentation
645 by Pointer Bounds Checker.
646 Current list of processed common attributes: nonnull. */
647 const struct attribute_spec c_common_attribute_table[] =
648 {
649 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
650 affects_type_identity } */
651 { "packed", 0, 0, false, false, false,
652 handle_packed_attribute , false},
653 { "nocommon", 0, 0, true, false, false,
654 handle_nocommon_attribute, false},
655 { "common", 0, 0, true, false, false,
656 handle_common_attribute, false },
657 /* FIXME: logically, noreturn attributes should be listed as
658 "false, true, true" and apply to function types. But implementing this
659 would require all the places in the compiler that use TREE_THIS_VOLATILE
660 on a decl to identify non-returning functions to be located and fixed
661 to check the function type instead. */
662 { "noreturn", 0, 0, true, false, false,
663 handle_noreturn_attribute, false },
664 { "volatile", 0, 0, true, false, false,
665 handle_noreturn_attribute, false },
666 { "stack_protect", 0, 0, true, false, false,
667 handle_stack_protect_attribute, false },
668 { "noinline", 0, 0, true, false, false,
669 handle_noinline_attribute, false },
670 { "noclone", 0, 0, true, false, false,
671 handle_noclone_attribute, false },
672 { "no_icf", 0, 0, true, false, false,
673 handle_noicf_attribute, false },
674 { "leaf", 0, 0, true, false, false,
675 handle_leaf_attribute, false },
676 { "always_inline", 0, 0, true, false, false,
677 handle_always_inline_attribute, false },
678 { "gnu_inline", 0, 0, true, false, false,
679 handle_gnu_inline_attribute, false },
680 { "artificial", 0, 0, true, false, false,
681 handle_artificial_attribute, false },
682 { "flatten", 0, 0, true, false, false,
683 handle_flatten_attribute, false },
684 { "used", 0, 0, true, false, false,
685 handle_used_attribute, false },
686 { "unused", 0, 0, false, false, false,
687 handle_unused_attribute, false },
688 { "externally_visible", 0, 0, true, false, false,
689 handle_externally_visible_attribute, false },
690 { "no_reorder", 0, 0, true, false, false,
691 handle_no_reorder_attribute, false },
692 /* The same comments as for noreturn attributes apply to const ones. */
693 { "const", 0, 0, true, false, false,
694 handle_const_attribute, false },
695 { "scalar_storage_order", 1, 1, false, false, false,
696 handle_scalar_storage_order_attribute, false },
697 { "transparent_union", 0, 0, false, false, false,
698 handle_transparent_union_attribute, false },
699 { "constructor", 0, 1, true, false, false,
700 handle_constructor_attribute, false },
701 { "destructor", 0, 1, true, false, false,
702 handle_destructor_attribute, false },
703 { "mode", 1, 1, false, true, false,
704 handle_mode_attribute, false },
705 { "section", 1, 1, true, false, false,
706 handle_section_attribute, false },
707 { "aligned", 0, 1, false, false, false,
708 handle_aligned_attribute, false },
709 { "weak", 0, 0, true, false, false,
710 handle_weak_attribute, false },
711 { "noplt", 0, 0, true, false, false,
712 handle_noplt_attribute, false },
713 { "ifunc", 1, 1, true, false, false,
714 handle_ifunc_attribute, false },
715 { "alias", 1, 1, true, false, false,
716 handle_alias_attribute, false },
717 { "weakref", 0, 1, true, false, false,
718 handle_weakref_attribute, false },
719 { "no_instrument_function", 0, 0, true, false, false,
720 handle_no_instrument_function_attribute,
721 false },
722 { "malloc", 0, 0, true, false, false,
723 handle_malloc_attribute, false },
724 { "returns_twice", 0, 0, true, false, false,
725 handle_returns_twice_attribute, false },
726 { "no_stack_limit", 0, 0, true, false, false,
727 handle_no_limit_stack_attribute, false },
728 { "pure", 0, 0, true, false, false,
729 handle_pure_attribute, false },
730 { "transaction_callable", 0, 0, false, true, false,
731 handle_tm_attribute, false },
732 { "transaction_unsafe", 0, 0, false, true, false,
733 handle_tm_attribute, true },
734 { "transaction_safe", 0, 0, false, true, false,
735 handle_tm_attribute, true },
736 { "transaction_safe_dynamic", 0, 0, true, false, false,
737 handle_tm_attribute, false },
738 { "transaction_may_cancel_outer", 0, 0, false, true, false,
739 handle_tm_attribute, false },
740 /* ??? These two attributes didn't make the transition from the
741 Intel language document to the multi-vendor language document. */
742 { "transaction_pure", 0, 0, false, true, false,
743 handle_tm_attribute, false },
744 { "transaction_wrap", 1, 1, true, false, false,
745 handle_tm_wrap_attribute, false },
746 /* For internal use (marking of builtins) only. The name contains space
747 to prevent its usage in source code. */
748 { "no vops", 0, 0, true, false, false,
749 handle_novops_attribute, false },
750 { "deprecated", 0, 1, false, false, false,
751 handle_deprecated_attribute, false },
752 { "vector_size", 1, 1, false, true, false,
753 handle_vector_size_attribute, false },
754 { "visibility", 1, 1, false, false, false,
755 handle_visibility_attribute, false },
756 { "tls_model", 1, 1, true, false, false,
757 handle_tls_model_attribute, false },
758 { "nonnull", 0, -1, false, true, true,
759 handle_nonnull_attribute, false },
760 { "nothrow", 0, 0, true, false, false,
761 handle_nothrow_attribute, false },
762 { "may_alias", 0, 0, false, true, false, NULL, false },
763 { "cleanup", 1, 1, true, false, false,
764 handle_cleanup_attribute, false },
765 { "warn_unused_result", 0, 0, false, true, true,
766 handle_warn_unused_result_attribute, false },
767 { "sentinel", 0, 1, false, true, true,
768 handle_sentinel_attribute, false },
769 /* For internal use (marking of builtins) only. The name contains space
770 to prevent its usage in source code. */
771 { "type generic", 0, 0, false, true, true,
772 handle_type_generic_attribute, false },
773 { "alloc_size", 1, 2, false, true, true,
774 handle_alloc_size_attribute, false },
775 { "cold", 0, 0, true, false, false,
776 handle_cold_attribute, false },
777 { "hot", 0, 0, true, false, false,
778 handle_hot_attribute, false },
779 { "no_address_safety_analysis",
780 0, 0, true, false, false,
781 handle_no_address_safety_analysis_attribute,
782 false },
783 { "no_sanitize_address", 0, 0, true, false, false,
784 handle_no_sanitize_address_attribute,
785 false },
786 { "no_sanitize_thread", 0, 0, true, false, false,
787 handle_no_sanitize_address_attribute,
788 false },
789 { "no_sanitize_undefined", 0, 0, true, false, false,
790 handle_no_sanitize_undefined_attribute,
791 false },
792 { "warning", 1, 1, true, false, false,
793 handle_error_attribute, false },
794 { "error", 1, 1, true, false, false,
795 handle_error_attribute, false },
796 { "target", 1, -1, true, false, false,
797 handle_target_attribute, false },
798 { "target_clones", 1, -1, true, false, false,
799 handle_target_clones_attribute, false },
800 { "optimize", 1, -1, true, false, false,
801 handle_optimize_attribute, false },
802 /* For internal use only. The leading '*' both prevents its usage in
803 source code and signals that it may be overridden by machine tables. */
804 { "*tm regparm", 0, 0, false, true, true,
805 ignore_attribute, false },
806 { "no_split_stack", 0, 0, true, false, false,
807 handle_no_split_stack_attribute, false },
808 /* For internal use (marking of builtins and runtime functions) only.
809 The name contains space to prevent its usage in source code. */
810 { "fn spec", 1, 1, false, true, true,
811 handle_fnspec_attribute, false },
812 { "warn_unused", 0, 0, false, false, false,
813 handle_warn_unused_attribute, false },
814 { "returns_nonnull", 0, 0, false, true, true,
815 handle_returns_nonnull_attribute, false },
816 { "omp declare simd", 0, -1, true, false, false,
817 handle_omp_declare_simd_attribute, false },
818 { "cilk simd function", 0, -1, true, false, false,
819 handle_omp_declare_simd_attribute, false },
820 { "simd", 0, 0, true, false, false,
821 handle_simd_attribute, false },
822 { "omp declare target", 0, 0, true, false, false,
823 handle_omp_declare_target_attribute, false },
824 { "alloc_align", 1, 1, false, true, true,
825 handle_alloc_align_attribute, false },
826 { "assume_aligned", 1, 2, false, true, true,
827 handle_assume_aligned_attribute, false },
828 { "designated_init", 0, 0, false, true, false,
829 handle_designated_init_attribute, false },
830 { "bnd_variable_size", 0, 0, true, false, false,
831 handle_bnd_variable_size_attribute, false },
832 { "bnd_legacy", 0, 0, true, false, false,
833 handle_bnd_legacy, false },
834 { "bnd_instrument", 0, 0, true, false, false,
835 handle_bnd_instrument, false },
836 { NULL, 0, 0, false, false, false, NULL, false }
837 };
838
839 /* Give the specifications for the format attributes, used by C and all
840 descendants.
841
842 All attributes referencing arguments should be additionally processed
843 in chkp_copy_function_type_adding_bounds for correct instrumentation
844 by Pointer Bounds Checker.
845 Current list of processed format attributes: format, format_arg. */
846 const struct attribute_spec c_common_format_attribute_table[] =
847 {
848 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
849 affects_type_identity } */
850 { "format", 3, 3, false, true, true,
851 handle_format_attribute, false },
852 { "format_arg", 1, 1, false, true, true,
853 handle_format_arg_attribute, false },
854 { NULL, 0, 0, false, false, false, NULL, false }
855 };
856
857 /* Return identifier for address space AS. */
858
859 const char *
860 c_addr_space_name (addr_space_t as)
861 {
862 int rid = RID_FIRST_ADDR_SPACE + as;
863 gcc_assert (ridpointers [rid]);
864 return IDENTIFIER_POINTER (ridpointers [rid]);
865 }
866
867 /* Push current bindings for the function name VAR_DECLS. */
868
869 void
870 start_fname_decls (void)
871 {
872 unsigned ix;
873 tree saved = NULL_TREE;
874
875 for (ix = 0; fname_vars[ix].decl; ix++)
876 {
877 tree decl = *fname_vars[ix].decl;
878
879 if (decl)
880 {
881 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
882 saved);
883 *fname_vars[ix].decl = NULL_TREE;
884 }
885 }
886 if (saved || saved_function_name_decls)
887 /* Normally they'll have been NULL, so only push if we've got a
888 stack, or they are non-NULL. */
889 saved_function_name_decls = tree_cons (saved, NULL_TREE,
890 saved_function_name_decls);
891 }
892
893 /* Finish up the current bindings, adding them into the current function's
894 statement tree. This must be done _before_ finish_stmt_tree is called.
895 If there is no current function, we must be at file scope and no statements
896 are involved. Pop the previous bindings. */
897
898 void
899 finish_fname_decls (void)
900 {
901 unsigned ix;
902 tree stmts = NULL_TREE;
903 tree stack = saved_function_name_decls;
904
905 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
906 append_to_statement_list (TREE_VALUE (stack), &stmts);
907
908 if (stmts)
909 {
910 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
911
912 if (TREE_CODE (*bodyp) == BIND_EXPR)
913 bodyp = &BIND_EXPR_BODY (*bodyp);
914
915 append_to_statement_list_force (*bodyp, &stmts);
916 *bodyp = stmts;
917 }
918
919 for (ix = 0; fname_vars[ix].decl; ix++)
920 *fname_vars[ix].decl = NULL_TREE;
921
922 if (stack)
923 {
924 /* We had saved values, restore them. */
925 tree saved;
926
927 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
928 {
929 tree decl = TREE_PURPOSE (saved);
930 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
931
932 *fname_vars[ix].decl = decl;
933 }
934 stack = TREE_CHAIN (stack);
935 }
936 saved_function_name_decls = stack;
937 }
938
939 /* Return the text name of the current function, suitably prettified
940 by PRETTY_P. Return string must be freed by caller. */
941
942 const char *
943 fname_as_string (int pretty_p)
944 {
945 const char *name = "top level";
946 char *namep;
947 int vrb = 2, len;
948 cpp_string cstr = { 0, 0 }, strname;
949
950 if (!pretty_p)
951 {
952 name = "";
953 vrb = 0;
954 }
955
956 if (current_function_decl)
957 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
958
959 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
960
961 namep = XNEWVEC (char, len);
962 snprintf (namep, len, "\"%s\"", name);
963 strname.text = (unsigned char *) namep;
964 strname.len = len - 1;
965
966 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
967 {
968 XDELETEVEC (namep);
969 return (const char *) cstr.text;
970 }
971
972 return namep;
973 }
974
975 /* Return the VAR_DECL for a const char array naming the current
976 function. If the VAR_DECL has not yet been created, create it
977 now. RID indicates how it should be formatted and IDENTIFIER_NODE
978 ID is its name (unfortunately C and C++ hold the RID values of
979 keywords in different places, so we can't derive RID from ID in
980 this language independent code. LOC is the location of the
981 function. */
982
983 tree
984 fname_decl (location_t loc, unsigned int rid, tree id)
985 {
986 unsigned ix;
987 tree decl = NULL_TREE;
988
989 for (ix = 0; fname_vars[ix].decl; ix++)
990 if (fname_vars[ix].rid == rid)
991 break;
992
993 decl = *fname_vars[ix].decl;
994 if (!decl)
995 {
996 /* If a tree is built here, it would normally have the lineno of
997 the current statement. Later this tree will be moved to the
998 beginning of the function and this line number will be wrong.
999 To avoid this problem set the lineno to 0 here; that prevents
1000 it from appearing in the RTL. */
1001 tree stmts;
1002 location_t saved_location = input_location;
1003 input_location = UNKNOWN_LOCATION;
1004
1005 stmts = push_stmt_list ();
1006 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1007 stmts = pop_stmt_list (stmts);
1008 if (!IS_EMPTY_STMT (stmts))
1009 saved_function_name_decls
1010 = tree_cons (decl, stmts, saved_function_name_decls);
1011 *fname_vars[ix].decl = decl;
1012 input_location = saved_location;
1013 }
1014 if (!ix && !current_function_decl)
1015 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1016
1017 return decl;
1018 }
1019
1020 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1021
1022 tree
1023 fix_string_type (tree value)
1024 {
1025 int length = TREE_STRING_LENGTH (value);
1026 int nchars;
1027 tree e_type, i_type, a_type;
1028
1029 /* Compute the number of elements, for the array type. */
1030 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1031 {
1032 nchars = length;
1033 e_type = char_type_node;
1034 }
1035 else if (TREE_TYPE (value) == char16_array_type_node)
1036 {
1037 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1038 e_type = char16_type_node;
1039 }
1040 else if (TREE_TYPE (value) == char32_array_type_node)
1041 {
1042 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1043 e_type = char32_type_node;
1044 }
1045 else
1046 {
1047 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1048 e_type = wchar_type_node;
1049 }
1050
1051 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1052 limit in C++98 Annex B is very large (65536) and is not normative,
1053 so we do not diagnose it (warn_overlength_strings is forced off
1054 in c_common_post_options). */
1055 if (warn_overlength_strings)
1056 {
1057 const int nchars_max = flag_isoc99 ? 4095 : 509;
1058 const int relevant_std = flag_isoc99 ? 99 : 90;
1059 if (nchars - 1 > nchars_max)
1060 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1061 separate the %d from the 'C'. 'ISO' should not be
1062 translated, but it may be moved after 'C%d' in languages
1063 where modifiers follow nouns. */
1064 pedwarn (input_location, OPT_Woverlength_strings,
1065 "string length %qd is greater than the length %qd "
1066 "ISO C%d compilers are required to support",
1067 nchars - 1, nchars_max, relevant_std);
1068 }
1069
1070 /* Create the array type for the string constant. The ISO C++
1071 standard says that a string literal has type `const char[N]' or
1072 `const wchar_t[N]'. We use the same logic when invoked as a C
1073 front-end with -Wwrite-strings.
1074 ??? We should change the type of an expression depending on the
1075 state of a warning flag. We should just be warning -- see how
1076 this is handled in the C++ front-end for the deprecated implicit
1077 conversion from string literals to `char*' or `wchar_t*'.
1078
1079 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1080 array type being the unqualified version of that type.
1081 Therefore, if we are constructing an array of const char, we must
1082 construct the matching unqualified array type first. The C front
1083 end does not require this, but it does no harm, so we do it
1084 unconditionally. */
1085 i_type = build_index_type (size_int (nchars - 1));
1086 a_type = build_array_type (e_type, i_type);
1087 if (c_dialect_cxx() || warn_write_strings)
1088 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1089
1090 TREE_TYPE (value) = a_type;
1091 TREE_CONSTANT (value) = 1;
1092 TREE_READONLY (value) = 1;
1093 TREE_STATIC (value) = 1;
1094 return value;
1095 }
1096 \f
1097 /* Fold X for consideration by one of the warning functions when checking
1098 whether an expression has a constant value. */
1099
1100 static tree
1101 fold_for_warn (tree x)
1102 {
1103 if (c_dialect_cxx ())
1104 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1105 else
1106 /* The C front-end has already folded X appropriately. */
1107 return x;
1108 }
1109
1110 /* Print a warning if a constant expression had overflow in folding.
1111 Invoke this function on every expression that the language
1112 requires to be a constant expression.
1113 Note the ANSI C standard says it is erroneous for a
1114 constant expression to overflow. */
1115
1116 void
1117 constant_expression_warning (tree value)
1118 {
1119 if (warn_overflow && pedantic
1120 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1121 || TREE_CODE (value) == FIXED_CST
1122 || TREE_CODE (value) == VECTOR_CST
1123 || TREE_CODE (value) == COMPLEX_CST)
1124 && TREE_OVERFLOW (value))
1125 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1126 }
1127
1128 /* The same as above but print an unconditional error. */
1129 void
1130 constant_expression_error (tree value)
1131 {
1132 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1133 || TREE_CODE (value) == FIXED_CST
1134 || TREE_CODE (value) == VECTOR_CST
1135 || TREE_CODE (value) == COMPLEX_CST)
1136 && TREE_OVERFLOW (value))
1137 error ("overflow in constant expression");
1138 }
1139
1140 /* Print a warning if an expression had overflow in folding and its
1141 operands hadn't.
1142
1143 Invoke this function on every expression that
1144 (1) appears in the source code, and
1145 (2) is a constant expression that overflowed, and
1146 (3) is not already checked by convert_and_check;
1147 however, do not invoke this function on operands of explicit casts
1148 or when the expression is the result of an operator and any operand
1149 already overflowed. */
1150
1151 void
1152 overflow_warning (location_t loc, tree value)
1153 {
1154 if (c_inhibit_evaluation_warnings != 0)
1155 return;
1156
1157 switch (TREE_CODE (value))
1158 {
1159 case INTEGER_CST:
1160 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1161 break;
1162
1163 case REAL_CST:
1164 warning_at (loc, OPT_Woverflow,
1165 "floating point overflow in expression");
1166 break;
1167
1168 case FIXED_CST:
1169 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1170 break;
1171
1172 case VECTOR_CST:
1173 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1174 break;
1175
1176 case COMPLEX_CST:
1177 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1178 warning_at (loc, OPT_Woverflow,
1179 "complex integer overflow in expression");
1180 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1181 warning_at (loc, OPT_Woverflow,
1182 "complex floating point overflow in expression");
1183 break;
1184
1185 default:
1186 break;
1187 }
1188 }
1189
1190 /* Warn about uses of logical || / && operator in a context where it
1191 is likely that the bitwise equivalent was intended by the
1192 programmer. We have seen an expression in which CODE is a binary
1193 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1194 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1195 void
1196 warn_logical_operator (location_t location, enum tree_code code, tree type,
1197 enum tree_code code_left, tree op_left,
1198 enum tree_code ARG_UNUSED (code_right), tree op_right)
1199 {
1200 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1201 int in0_p, in1_p, in_p;
1202 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1203 bool strict_overflow_p = false;
1204
1205 if (code != TRUTH_ANDIF_EXPR
1206 && code != TRUTH_AND_EXPR
1207 && code != TRUTH_ORIF_EXPR
1208 && code != TRUTH_OR_EXPR)
1209 return;
1210
1211 /* We don't want to warn if either operand comes from a macro
1212 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1213 see PR61534. */
1214 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1215 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1216 return;
1217
1218 /* Warn if &&/|| are being used in a context where it is
1219 likely that the bitwise equivalent was intended by the
1220 programmer. That is, an expression such as op && MASK
1221 where op should not be any boolean expression, nor a
1222 constant, and mask seems to be a non-boolean integer constant. */
1223 if (TREE_CODE (op_right) == CONST_DECL)
1224 /* An enumerator counts as a constant. */
1225 op_right = DECL_INITIAL (op_right);
1226 if (!truth_value_p (code_left)
1227 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1228 && !CONSTANT_CLASS_P (op_left)
1229 && !TREE_NO_WARNING (op_left)
1230 && TREE_CODE (op_right) == INTEGER_CST
1231 && !integer_zerop (op_right)
1232 && !integer_onep (op_right))
1233 {
1234 if (or_op)
1235 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1236 " applied to non-boolean constant");
1237 else
1238 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1239 " applied to non-boolean constant");
1240 TREE_NO_WARNING (op_left) = true;
1241 return;
1242 }
1243
1244 /* We do not warn for constants because they are typical of macro
1245 expansions that test for features. */
1246 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1247 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
1248 return;
1249
1250 /* This warning only makes sense with logical operands. */
1251 if (!(truth_value_p (TREE_CODE (op_left))
1252 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1253 || !(truth_value_p (TREE_CODE (op_right))
1254 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1255 return;
1256
1257 /* The range computations only work with scalars. */
1258 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1259 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1260 return;
1261
1262 /* We first test whether either side separately is trivially true
1263 (with OR) or trivially false (with AND). If so, do not warn.
1264 This is a common idiom for testing ranges of data types in
1265 portable code. */
1266 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1267 if (!lhs)
1268 return;
1269 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1270 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1271
1272 /* If this is an OR operation, invert both sides; now, the result
1273 should be always false to get a warning. */
1274 if (or_op)
1275 in0_p = !in0_p;
1276
1277 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1278 if (tem && integer_zerop (tem))
1279 return;
1280
1281 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1282 if (!rhs)
1283 return;
1284 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1285 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1286
1287 /* If this is an OR operation, invert both sides; now, the result
1288 should be always false to get a warning. */
1289 if (or_op)
1290 in1_p = !in1_p;
1291
1292 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1293 if (tem && integer_zerop (tem))
1294 return;
1295
1296 /* If both expressions have the same operand, if we can merge the
1297 ranges, ... */
1298 if (operand_equal_p (lhs, rhs, 0)
1299 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1300 in1_p, low1, high1))
1301 {
1302 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1303 /* ... and if the range test is always false, then warn. */
1304 if (tem && integer_zerop (tem))
1305 {
1306 if (or_op)
1307 warning_at (location, OPT_Wlogical_op,
1308 "logical %<or%> of collectively exhaustive tests is "
1309 "always true");
1310 else
1311 warning_at (location, OPT_Wlogical_op,
1312 "logical %<and%> of mutually exclusive tests is "
1313 "always false");
1314 }
1315 /* Or warn if the operands have exactly the same range, e.g.
1316 A > 0 && A > 0. */
1317 else if (tree_int_cst_equal (low0, low1)
1318 && tree_int_cst_equal (high0, high1))
1319 {
1320 if (or_op)
1321 warning_at (location, OPT_Wlogical_op,
1322 "logical %<or%> of equal expressions");
1323 else
1324 warning_at (location, OPT_Wlogical_op,
1325 "logical %<and%> of equal expressions");
1326 }
1327 }
1328 }
1329
1330 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1331 with constant indices. */
1332
1333 static tree
1334 find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1335 {
1336 tree expr = *expr_p;
1337
1338 if ((TREE_CODE (expr) == ARRAY_REF
1339 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1340 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1341 {
1342 *(bool *) data = true;
1343 *walk_subtrees = 0;
1344 }
1345
1346 return NULL_TREE;
1347 }
1348
1349 /* Warn if a self-comparison always evaluates to true or false. LOC
1350 is the location of the comparison with code CODE, LHS and RHS are
1351 operands of the comparison. */
1352
1353 void
1354 warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1355 {
1356 if (TREE_CODE_CLASS (code) != tcc_comparison)
1357 return;
1358
1359 /* Don't warn for various macro expansions. */
1360 if (from_macro_expansion_at (loc)
1361 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1362 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1363 return;
1364
1365 /* We do not warn for constants because they are typical of macro
1366 expansions that test for features, sizeof, and similar. */
1367 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1368 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
1369 return;
1370
1371 /* Don't warn for e.g.
1372 HOST_WIDE_INT n;
1373 ...
1374 if (n == (long) n) ...
1375 */
1376 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1377 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1378 return;
1379
1380 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1381 It could be a NaN, and NaN never compares equal to anything, even
1382 itself. */
1383 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1384 return;
1385
1386 if (operand_equal_p (lhs, rhs, 0))
1387 {
1388 /* Don't warn about array references with constant indices;
1389 these are likely to come from a macro. */
1390 bool found = false;
1391 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1392 &found);
1393 if (found)
1394 return;
1395 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1396 || code == GE_EXPR || code == UNLE_EXPR
1397 || code == UNGE_EXPR || code == UNEQ_EXPR);
1398 if (always_true)
1399 warning_at (loc, OPT_Wtautological_compare,
1400 "self-comparison always evaluates to true");
1401 else
1402 warning_at (loc, OPT_Wtautological_compare,
1403 "self-comparison always evaluates to false");
1404 }
1405 }
1406
1407 /* Warn about logical not used on the left hand side operand of a comparison.
1408 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1409 Do not warn if RHS is of a boolean type. */
1410
1411 void
1412 warn_logical_not_parentheses (location_t location, enum tree_code code,
1413 tree rhs)
1414 {
1415 if (TREE_CODE_CLASS (code) != tcc_comparison
1416 || TREE_TYPE (rhs) == NULL_TREE
1417 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1418 return;
1419
1420 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1421 !(x == 0) or !(y != 0). */
1422 if ((code == EQ_EXPR || code == NE_EXPR)
1423 && integer_zerop (rhs))
1424 return;
1425
1426 warning_at (location, OPT_Wlogical_not_parentheses,
1427 "logical not is only applied to the left hand side of "
1428 "comparison");
1429 }
1430
1431 /* Warn if EXP contains any computations whose results are not used.
1432 Return true if a warning is printed; false otherwise. LOCUS is the
1433 (potential) location of the expression. */
1434
1435 bool
1436 warn_if_unused_value (const_tree exp, location_t locus)
1437 {
1438 restart:
1439 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1440 return false;
1441
1442 /* Don't warn about void constructs. This includes casting to void,
1443 void function calls, and statement expressions with a final cast
1444 to void. */
1445 if (VOID_TYPE_P (TREE_TYPE (exp)))
1446 return false;
1447
1448 if (EXPR_HAS_LOCATION (exp))
1449 locus = EXPR_LOCATION (exp);
1450
1451 switch (TREE_CODE (exp))
1452 {
1453 case PREINCREMENT_EXPR:
1454 case POSTINCREMENT_EXPR:
1455 case PREDECREMENT_EXPR:
1456 case POSTDECREMENT_EXPR:
1457 case MODIFY_EXPR:
1458 case INIT_EXPR:
1459 case TARGET_EXPR:
1460 case CALL_EXPR:
1461 case TRY_CATCH_EXPR:
1462 case WITH_CLEANUP_EXPR:
1463 case EXIT_EXPR:
1464 case VA_ARG_EXPR:
1465 return false;
1466
1467 case BIND_EXPR:
1468 /* For a binding, warn if no side effect within it. */
1469 exp = BIND_EXPR_BODY (exp);
1470 goto restart;
1471
1472 case SAVE_EXPR:
1473 case NON_LVALUE_EXPR:
1474 case NOP_EXPR:
1475 exp = TREE_OPERAND (exp, 0);
1476 goto restart;
1477
1478 case TRUTH_ORIF_EXPR:
1479 case TRUTH_ANDIF_EXPR:
1480 /* In && or ||, warn if 2nd operand has no side effect. */
1481 exp = TREE_OPERAND (exp, 1);
1482 goto restart;
1483
1484 case COMPOUND_EXPR:
1485 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1486 return true;
1487 /* Let people do `(foo (), 0)' without a warning. */
1488 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1489 return false;
1490 exp = TREE_OPERAND (exp, 1);
1491 goto restart;
1492
1493 case COND_EXPR:
1494 /* If this is an expression with side effects, don't warn; this
1495 case commonly appears in macro expansions. */
1496 if (TREE_SIDE_EFFECTS (exp))
1497 return false;
1498 goto warn;
1499
1500 case INDIRECT_REF:
1501 /* Don't warn about automatic dereferencing of references, since
1502 the user cannot control it. */
1503 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1504 {
1505 exp = TREE_OPERAND (exp, 0);
1506 goto restart;
1507 }
1508 /* Fall through. */
1509
1510 default:
1511 /* Referencing a volatile value is a side effect, so don't warn. */
1512 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1513 && TREE_THIS_VOLATILE (exp))
1514 return false;
1515
1516 /* If this is an expression which has no operands, there is no value
1517 to be unused. There are no such language-independent codes,
1518 but front ends may define such. */
1519 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1520 return false;
1521
1522 warn:
1523 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1524 }
1525 }
1526
1527
1528 /* Print a warning about casts that might indicate violation
1529 of strict aliasing rules if -Wstrict-aliasing is used and
1530 strict aliasing mode is in effect. OTYPE is the original
1531 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1532
1533 bool
1534 strict_aliasing_warning (tree otype, tree type, tree expr)
1535 {
1536 /* Strip pointer conversion chains and get to the correct original type. */
1537 STRIP_NOPS (expr);
1538 otype = TREE_TYPE (expr);
1539
1540 if (!(flag_strict_aliasing
1541 && POINTER_TYPE_P (type)
1542 && POINTER_TYPE_P (otype)
1543 && !VOID_TYPE_P (TREE_TYPE (type)))
1544 /* If the type we are casting to is a ref-all pointer
1545 dereferencing it is always valid. */
1546 || TYPE_REF_CAN_ALIAS_ALL (type))
1547 return false;
1548
1549 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1550 && (DECL_P (TREE_OPERAND (expr, 0))
1551 || handled_component_p (TREE_OPERAND (expr, 0))))
1552 {
1553 /* Casting the address of an object to non void pointer. Warn
1554 if the cast breaks type based aliasing. */
1555 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1556 {
1557 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1558 "might break strict-aliasing rules");
1559 return true;
1560 }
1561 else
1562 {
1563 /* warn_strict_aliasing >= 3. This includes the default (3).
1564 Only warn if the cast is dereferenced immediately. */
1565 alias_set_type set1 =
1566 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1567 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1568
1569 if (set1 != set2 && set2 != 0
1570 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1571 {
1572 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1573 "pointer will break strict-aliasing rules");
1574 return true;
1575 }
1576 else if (warn_strict_aliasing == 2
1577 && !alias_sets_must_conflict_p (set1, set2))
1578 {
1579 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1580 "pointer might break strict-aliasing rules");
1581 return true;
1582 }
1583 }
1584 }
1585 else
1586 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1587 {
1588 /* At this level, warn for any conversions, even if an address is
1589 not taken in the same statement. This will likely produce many
1590 false positives, but could be useful to pinpoint problems that
1591 are not revealed at higher levels. */
1592 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1593 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1594 if (!COMPLETE_TYPE_P (type)
1595 || !alias_sets_must_conflict_p (set1, set2))
1596 {
1597 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1598 "pointer might break strict-aliasing rules");
1599 return true;
1600 }
1601 }
1602
1603 return false;
1604 }
1605
1606 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1607 sizeof as last operand of certain builtins. */
1608
1609 void
1610 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1611 vec<tree, va_gc> *params, tree *sizeof_arg,
1612 bool (*comp_types) (tree, tree))
1613 {
1614 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1615 bool strop = false, cmp = false;
1616 unsigned int idx = ~0;
1617 location_t loc;
1618
1619 if (TREE_CODE (callee) != FUNCTION_DECL
1620 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1621 || vec_safe_length (params) <= 1)
1622 return;
1623
1624 switch (DECL_FUNCTION_CODE (callee))
1625 {
1626 case BUILT_IN_STRNCMP:
1627 case BUILT_IN_STRNCASECMP:
1628 cmp = true;
1629 /* FALLTHRU */
1630 case BUILT_IN_STRNCPY:
1631 case BUILT_IN_STRNCPY_CHK:
1632 case BUILT_IN_STRNCAT:
1633 case BUILT_IN_STRNCAT_CHK:
1634 case BUILT_IN_STPNCPY:
1635 case BUILT_IN_STPNCPY_CHK:
1636 strop = true;
1637 /* FALLTHRU */
1638 case BUILT_IN_MEMCPY:
1639 case BUILT_IN_MEMCPY_CHK:
1640 case BUILT_IN_MEMMOVE:
1641 case BUILT_IN_MEMMOVE_CHK:
1642 if (params->length () < 3)
1643 return;
1644 src = (*params)[1];
1645 dest = (*params)[0];
1646 idx = 2;
1647 break;
1648 case BUILT_IN_BCOPY:
1649 if (params->length () < 3)
1650 return;
1651 src = (*params)[0];
1652 dest = (*params)[1];
1653 idx = 2;
1654 break;
1655 case BUILT_IN_MEMCMP:
1656 case BUILT_IN_BCMP:
1657 if (params->length () < 3)
1658 return;
1659 src = (*params)[1];
1660 dest = (*params)[0];
1661 idx = 2;
1662 cmp = true;
1663 break;
1664 case BUILT_IN_MEMSET:
1665 case BUILT_IN_MEMSET_CHK:
1666 if (params->length () < 3)
1667 return;
1668 dest = (*params)[0];
1669 idx = 2;
1670 break;
1671 case BUILT_IN_BZERO:
1672 dest = (*params)[0];
1673 idx = 1;
1674 break;
1675 case BUILT_IN_STRNDUP:
1676 src = (*params)[0];
1677 strop = true;
1678 idx = 1;
1679 break;
1680 case BUILT_IN_MEMCHR:
1681 if (params->length () < 3)
1682 return;
1683 src = (*params)[0];
1684 idx = 2;
1685 break;
1686 case BUILT_IN_SNPRINTF:
1687 case BUILT_IN_SNPRINTF_CHK:
1688 case BUILT_IN_VSNPRINTF:
1689 case BUILT_IN_VSNPRINTF_CHK:
1690 dest = (*params)[0];
1691 idx = 1;
1692 strop = true;
1693 break;
1694 default:
1695 break;
1696 }
1697
1698 if (idx >= 3)
1699 return;
1700
1701 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1702 return;
1703
1704 type = TYPE_P (sizeof_arg[idx])
1705 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1706 if (!POINTER_TYPE_P (type))
1707 return;
1708
1709 if (dest
1710 && (tem = tree_strip_nop_conversions (dest))
1711 && POINTER_TYPE_P (TREE_TYPE (tem))
1712 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1713 return;
1714
1715 if (src
1716 && (tem = tree_strip_nop_conversions (src))
1717 && POINTER_TYPE_P (TREE_TYPE (tem))
1718 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1719 return;
1720
1721 loc = sizeof_arg_loc[idx];
1722
1723 if (dest && !cmp)
1724 {
1725 if (!TYPE_P (sizeof_arg[idx])
1726 && operand_equal_p (dest, sizeof_arg[idx], 0)
1727 && comp_types (TREE_TYPE (dest), type))
1728 {
1729 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1730 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1731 "argument to %<sizeof%> in %qD call is the same "
1732 "expression as the destination; did you mean to "
1733 "remove the addressof?", callee);
1734 else if ((TYPE_PRECISION (TREE_TYPE (type))
1735 == TYPE_PRECISION (char_type_node))
1736 || strop)
1737 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1738 "argument to %<sizeof%> in %qD call is the same "
1739 "expression as the destination; did you mean to "
1740 "provide an explicit length?", callee);
1741 else
1742 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1743 "argument to %<sizeof%> in %qD call is the same "
1744 "expression as the destination; did you mean to "
1745 "dereference it?", callee);
1746 return;
1747 }
1748
1749 if (POINTER_TYPE_P (TREE_TYPE (dest))
1750 && !strop
1751 && comp_types (TREE_TYPE (dest), type)
1752 && !VOID_TYPE_P (TREE_TYPE (type)))
1753 {
1754 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1755 "argument to %<sizeof%> in %qD call is the same "
1756 "pointer type %qT as the destination; expected %qT "
1757 "or an explicit length", callee, TREE_TYPE (dest),
1758 TREE_TYPE (TREE_TYPE (dest)));
1759 return;
1760 }
1761 }
1762
1763 if (src && !cmp)
1764 {
1765 if (!TYPE_P (sizeof_arg[idx])
1766 && operand_equal_p (src, sizeof_arg[idx], 0)
1767 && comp_types (TREE_TYPE (src), type))
1768 {
1769 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1770 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1771 "argument to %<sizeof%> in %qD call is the same "
1772 "expression as the source; did you mean to "
1773 "remove the addressof?", callee);
1774 else if ((TYPE_PRECISION (TREE_TYPE (type))
1775 == TYPE_PRECISION (char_type_node))
1776 || strop)
1777 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1778 "argument to %<sizeof%> in %qD call is the same "
1779 "expression as the source; did you mean to "
1780 "provide an explicit length?", callee);
1781 else
1782 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1783 "argument to %<sizeof%> in %qD call is the same "
1784 "expression as the source; did you mean to "
1785 "dereference it?", callee);
1786 return;
1787 }
1788
1789 if (POINTER_TYPE_P (TREE_TYPE (src))
1790 && !strop
1791 && comp_types (TREE_TYPE (src), type)
1792 && !VOID_TYPE_P (TREE_TYPE (type)))
1793 {
1794 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1795 "argument to %<sizeof%> in %qD call is the same "
1796 "pointer type %qT as the source; expected %qT "
1797 "or an explicit length", callee, TREE_TYPE (src),
1798 TREE_TYPE (TREE_TYPE (src)));
1799 return;
1800 }
1801 }
1802
1803 if (dest)
1804 {
1805 if (!TYPE_P (sizeof_arg[idx])
1806 && operand_equal_p (dest, sizeof_arg[idx], 0)
1807 && comp_types (TREE_TYPE (dest), type))
1808 {
1809 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1810 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1811 "argument to %<sizeof%> in %qD call is the same "
1812 "expression as the first source; did you mean to "
1813 "remove the addressof?", callee);
1814 else if ((TYPE_PRECISION (TREE_TYPE (type))
1815 == TYPE_PRECISION (char_type_node))
1816 || strop)
1817 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1818 "argument to %<sizeof%> in %qD call is the same "
1819 "expression as the first source; did you mean to "
1820 "provide an explicit length?", callee);
1821 else
1822 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1823 "argument to %<sizeof%> in %qD call is the same "
1824 "expression as the first source; did you mean to "
1825 "dereference it?", callee);
1826 return;
1827 }
1828
1829 if (POINTER_TYPE_P (TREE_TYPE (dest))
1830 && !strop
1831 && comp_types (TREE_TYPE (dest), type)
1832 && !VOID_TYPE_P (TREE_TYPE (type)))
1833 {
1834 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1835 "argument to %<sizeof%> in %qD call is the same "
1836 "pointer type %qT as the first source; expected %qT "
1837 "or an explicit length", callee, TREE_TYPE (dest),
1838 TREE_TYPE (TREE_TYPE (dest)));
1839 return;
1840 }
1841 }
1842
1843 if (src)
1844 {
1845 if (!TYPE_P (sizeof_arg[idx])
1846 && operand_equal_p (src, sizeof_arg[idx], 0)
1847 && comp_types (TREE_TYPE (src), type))
1848 {
1849 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1850 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1851 "argument to %<sizeof%> in %qD call is the same "
1852 "expression as the second source; did you mean to "
1853 "remove the addressof?", callee);
1854 else if ((TYPE_PRECISION (TREE_TYPE (type))
1855 == TYPE_PRECISION (char_type_node))
1856 || strop)
1857 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1858 "argument to %<sizeof%> in %qD call is the same "
1859 "expression as the second source; did you mean to "
1860 "provide an explicit length?", callee);
1861 else
1862 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1863 "argument to %<sizeof%> in %qD call is the same "
1864 "expression as the second source; did you mean to "
1865 "dereference it?", callee);
1866 return;
1867 }
1868
1869 if (POINTER_TYPE_P (TREE_TYPE (src))
1870 && !strop
1871 && comp_types (TREE_TYPE (src), type)
1872 && !VOID_TYPE_P (TREE_TYPE (type)))
1873 {
1874 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1875 "argument to %<sizeof%> in %qD call is the same "
1876 "pointer type %qT as the second source; expected %qT "
1877 "or an explicit length", callee, TREE_TYPE (src),
1878 TREE_TYPE (TREE_TYPE (src)));
1879 return;
1880 }
1881 }
1882
1883 }
1884
1885 /* Warn for unlikely, improbable, or stupid DECL declarations
1886 of `main'. */
1887
1888 void
1889 check_main_parameter_types (tree decl)
1890 {
1891 function_args_iterator iter;
1892 tree type;
1893 int argct = 0;
1894
1895 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1896 {
1897 /* XXX void_type_node belies the abstraction. */
1898 if (type == void_type_node || type == error_mark_node )
1899 break;
1900
1901 tree t = type;
1902 if (TYPE_ATOMIC (t))
1903 pedwarn (input_location, OPT_Wmain,
1904 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1905 type, decl);
1906 while (POINTER_TYPE_P (t))
1907 {
1908 t = TREE_TYPE (t);
1909 if (TYPE_ATOMIC (t))
1910 pedwarn (input_location, OPT_Wmain,
1911 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1912 type, decl);
1913 }
1914
1915 ++argct;
1916 switch (argct)
1917 {
1918 case 1:
1919 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1920 pedwarn (input_location, OPT_Wmain,
1921 "first argument of %q+D should be %<int%>", decl);
1922 break;
1923
1924 case 2:
1925 if (TREE_CODE (type) != POINTER_TYPE
1926 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1927 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1928 != char_type_node))
1929 pedwarn (input_location, OPT_Wmain,
1930 "second argument of %q+D should be %<char **%>", decl);
1931 break;
1932
1933 case 3:
1934 if (TREE_CODE (type) != POINTER_TYPE
1935 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1936 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1937 != char_type_node))
1938 pedwarn (input_location, OPT_Wmain,
1939 "third argument of %q+D should probably be "
1940 "%<char **%>", decl);
1941 break;
1942 }
1943 }
1944
1945 /* It is intentional that this message does not mention the third
1946 argument because it's only mentioned in an appendix of the
1947 standard. */
1948 if (argct > 0 && (argct < 2 || argct > 3))
1949 pedwarn (input_location, OPT_Wmain,
1950 "%q+D takes only zero or two arguments", decl);
1951
1952 if (stdarg_p (TREE_TYPE (decl)))
1953 pedwarn (input_location, OPT_Wmain,
1954 "%q+D declared as variadic function", decl);
1955 }
1956
1957 /* vector_targets_convertible_p is used for vector pointer types. The
1958 callers perform various checks that the qualifiers are satisfactory,
1959 while OTOH vector_targets_convertible_p ignores the number of elements
1960 in the vectors. That's fine with vector pointers as we can consider,
1961 say, a vector of 8 elements as two consecutive vectors of 4 elements,
1962 and that does not require and conversion of the pointer values.
1963 In contrast, vector_types_convertible_p and
1964 vector_types_compatible_elements_p are used for vector value types. */
1965 /* True if pointers to distinct types T1 and T2 can be converted to
1966 each other without an explicit cast. Only returns true for opaque
1967 vector types. */
1968 bool
1969 vector_targets_convertible_p (const_tree t1, const_tree t2)
1970 {
1971 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
1972 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1973 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1974 return true;
1975
1976 return false;
1977 }
1978
1979 /* vector_types_convertible_p is used for vector value types.
1980 It could in principle call vector_targets_convertible_p as a subroutine,
1981 but then the check for vector type would be duplicated with its callers,
1982 and also the purpose of vector_targets_convertible_p would become
1983 muddled.
1984 Where vector_types_convertible_p returns true, a conversion might still be
1985 needed to make the types match.
1986 In contrast, vector_targets_convertible_p is used for vector pointer
1987 values, and vector_types_compatible_elements_p is used specifically
1988 in the context for binary operators, as a check if use is possible without
1989 conversion. */
1990 /* True if vector types T1 and T2 can be converted to each other
1991 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1992 can only be converted with -flax-vector-conversions yet that is not
1993 in effect, emit a note telling the user about that option if such
1994 a note has not previously been emitted. */
1995 bool
1996 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1997 {
1998 static bool emitted_lax_note = false;
1999 bool convertible_lax;
2000
2001 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2002 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2003 return true;
2004
2005 convertible_lax =
2006 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2007 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2008 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2009 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2010 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2011
2012 if (!convertible_lax || flag_lax_vector_conversions)
2013 return convertible_lax;
2014
2015 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2016 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2017 return true;
2018
2019 if (emit_lax_note && !emitted_lax_note)
2020 {
2021 emitted_lax_note = true;
2022 inform (input_location, "use -flax-vector-conversions to permit "
2023 "conversions between vectors with differing "
2024 "element types or numbers of subparts");
2025 }
2026
2027 return false;
2028 }
2029
2030 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2031 and have vector types, V0 has the same type as V1, and the number of
2032 elements of V0, V1, MASK is the same.
2033
2034 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2035 called with two arguments. In this case implementation passes the
2036 first argument twice in order to share the same tree code. This fact
2037 could enable the mask-values being twice the vector length. This is
2038 an implementation accident and this semantics is not guaranteed to
2039 the user. */
2040 tree
2041 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2042 bool complain)
2043 {
2044 tree ret;
2045 bool wrap = true;
2046 bool maybe_const = false;
2047 bool two_arguments = false;
2048
2049 if (v1 == NULL_TREE)
2050 {
2051 two_arguments = true;
2052 v1 = v0;
2053 }
2054
2055 if (v0 == error_mark_node || v1 == error_mark_node
2056 || mask == error_mark_node)
2057 return error_mark_node;
2058
2059 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
2060 {
2061 if (complain)
2062 error_at (loc, "__builtin_shuffle last argument must "
2063 "be an integer vector");
2064 return error_mark_node;
2065 }
2066
2067 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2068 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
2069 {
2070 if (complain)
2071 error_at (loc, "__builtin_shuffle arguments must be vectors");
2072 return error_mark_node;
2073 }
2074
2075 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2076 {
2077 if (complain)
2078 error_at (loc, "__builtin_shuffle argument vectors must be of "
2079 "the same type");
2080 return error_mark_node;
2081 }
2082
2083 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2084 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2085 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2086 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2087 {
2088 if (complain)
2089 error_at (loc, "__builtin_shuffle number of elements of the "
2090 "argument vector(s) and the mask vector should "
2091 "be the same");
2092 return error_mark_node;
2093 }
2094
2095 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2096 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2097 {
2098 if (complain)
2099 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2100 "must have the same size as inner type of the mask");
2101 return error_mark_node;
2102 }
2103
2104 if (!c_dialect_cxx ())
2105 {
2106 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2107 v0 = c_fully_fold (v0, false, &maybe_const);
2108 wrap &= maybe_const;
2109
2110 if (two_arguments)
2111 v1 = v0 = save_expr (v0);
2112 else
2113 {
2114 v1 = c_fully_fold (v1, false, &maybe_const);
2115 wrap &= maybe_const;
2116 }
2117
2118 mask = c_fully_fold (mask, false, &maybe_const);
2119 wrap &= maybe_const;
2120 }
2121 else if (two_arguments)
2122 v1 = v0 = save_expr (v0);
2123
2124 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2125
2126 if (!c_dialect_cxx () && !wrap)
2127 ret = c_wrap_maybe_const (ret, true);
2128
2129 return ret;
2130 }
2131
2132 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2133 to integral type. */
2134
2135 static tree
2136 c_common_get_narrower (tree op, int *unsignedp_ptr)
2137 {
2138 op = get_narrower (op, unsignedp_ptr);
2139
2140 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2141 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2142 {
2143 /* C++0x scoped enumerations don't implicitly convert to integral
2144 type; if we stripped an explicit conversion to a larger type we
2145 need to replace it so common_type will still work. */
2146 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2147 TYPE_UNSIGNED (TREE_TYPE (op)));
2148 op = fold_convert (type, op);
2149 }
2150 return op;
2151 }
2152
2153 /* This is a helper function of build_binary_op.
2154
2155 For certain operations if both args were extended from the same
2156 smaller type, do the arithmetic in that type and then extend.
2157
2158 BITWISE indicates a bitwise operation.
2159 For them, this optimization is safe only if
2160 both args are zero-extended or both are sign-extended.
2161 Otherwise, we might change the result.
2162 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2163 but calculated in (unsigned short) it would be (unsigned short)-1.
2164 */
2165 tree
2166 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2167 {
2168 int unsigned0, unsigned1;
2169 tree arg0, arg1;
2170 int uns;
2171 tree type;
2172
2173 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2174 excessive narrowing when we call get_narrower below. For
2175 example, suppose that OP0 is of unsigned int extended
2176 from signed char and that RESULT_TYPE is long long int.
2177 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2178 like
2179
2180 (long long int) (unsigned int) signed_char
2181
2182 which get_narrower would narrow down to
2183
2184 (unsigned int) signed char
2185
2186 If we do not cast OP0 first, get_narrower would return
2187 signed_char, which is inconsistent with the case of the
2188 explicit cast. */
2189 op0 = convert (result_type, op0);
2190 op1 = convert (result_type, op1);
2191
2192 arg0 = c_common_get_narrower (op0, &unsigned0);
2193 arg1 = c_common_get_narrower (op1, &unsigned1);
2194
2195 /* UNS is 1 if the operation to be done is an unsigned one. */
2196 uns = TYPE_UNSIGNED (result_type);
2197
2198 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2199 but it *requires* conversion to FINAL_TYPE. */
2200
2201 if ((TYPE_PRECISION (TREE_TYPE (op0))
2202 == TYPE_PRECISION (TREE_TYPE (arg0)))
2203 && TREE_TYPE (op0) != result_type)
2204 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2205 if ((TYPE_PRECISION (TREE_TYPE (op1))
2206 == TYPE_PRECISION (TREE_TYPE (arg1)))
2207 && TREE_TYPE (op1) != result_type)
2208 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2209
2210 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2211
2212 /* For bitwise operations, signedness of nominal type
2213 does not matter. Consider only how operands were extended. */
2214 if (bitwise)
2215 uns = unsigned0;
2216
2217 /* Note that in all three cases below we refrain from optimizing
2218 an unsigned operation on sign-extended args.
2219 That would not be valid. */
2220
2221 /* Both args variable: if both extended in same way
2222 from same width, do it in that width.
2223 Do it unsigned if args were zero-extended. */
2224 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2225 < TYPE_PRECISION (result_type))
2226 && (TYPE_PRECISION (TREE_TYPE (arg1))
2227 == TYPE_PRECISION (TREE_TYPE (arg0)))
2228 && unsigned0 == unsigned1
2229 && (unsigned0 || !uns))
2230 return c_common_signed_or_unsigned_type
2231 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2232
2233 else if (TREE_CODE (arg0) == INTEGER_CST
2234 && (unsigned1 || !uns)
2235 && (TYPE_PRECISION (TREE_TYPE (arg1))
2236 < TYPE_PRECISION (result_type))
2237 && (type
2238 = c_common_signed_or_unsigned_type (unsigned1,
2239 TREE_TYPE (arg1)))
2240 && !POINTER_TYPE_P (type)
2241 && int_fits_type_p (arg0, type))
2242 return type;
2243
2244 else if (TREE_CODE (arg1) == INTEGER_CST
2245 && (unsigned0 || !uns)
2246 && (TYPE_PRECISION (TREE_TYPE (arg0))
2247 < TYPE_PRECISION (result_type))
2248 && (type
2249 = c_common_signed_or_unsigned_type (unsigned0,
2250 TREE_TYPE (arg0)))
2251 && !POINTER_TYPE_P (type)
2252 && int_fits_type_p (arg1, type))
2253 return type;
2254
2255 return result_type;
2256 }
2257
2258 /* Returns true iff any integer value of type FROM_TYPE can be represented as
2259 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2260
2261 static bool
2262 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2263 {
2264 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2265 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2266 REAL_VALUE_TYPE real_low_bound =
2267 real_value_from_int_cst (0, type_low_bound);
2268 REAL_VALUE_TYPE real_high_bound =
2269 real_value_from_int_cst (0, type_high_bound);
2270
2271 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2272 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2273 }
2274
2275 /* Checks if expression EXPR of complex/real/integer type cannot be converted
2276 to the complex/real/integer type TYPE. Function returns non-zero when:
2277 * EXPR is a constant which cannot be exactly converted to TYPE.
2278 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2279 for EXPR type and TYPE being both integers or both real, or both
2280 complex.
2281 * EXPR is not a constant of complex type and TYPE is a real or
2282 an integer.
2283 * EXPR is not a constant of real type and TYPE is an integer.
2284 * EXPR is not a constant of integer type which cannot be
2285 exactly converted to real type.
2286
2287 Function allows conversions between types of different signedness and
2288 can return SAFE_CONVERSION (zero) in that case. Function can produce
2289 signedness warnings if PRODUCE_WARNS is true.
2290
2291 Function allows conversions from complex constants to non-complex types,
2292 provided that imaginary part is zero and real part can be safely converted
2293 to TYPE. */
2294
2295 enum conversion_safety
2296 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2297 {
2298 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2299 tree expr_type = TREE_TYPE (expr);
2300 loc = expansion_point_location_if_in_system_header (loc);
2301
2302 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2303 {
2304 /* If type is complex, we are interested in compatibility with
2305 underlying type. */
2306 if (TREE_CODE (type) == COMPLEX_TYPE)
2307 type = TREE_TYPE (type);
2308
2309 /* Warn for real constant that is not an exact integer converted
2310 to integer type. */
2311 if (TREE_CODE (expr_type) == REAL_TYPE
2312 && TREE_CODE (type) == INTEGER_TYPE)
2313 {
2314 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2315 give_warning = UNSAFE_REAL;
2316 }
2317 /* Warn for an integer constant that does not fit into integer type. */
2318 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2319 && TREE_CODE (type) == INTEGER_TYPE
2320 && !int_fits_type_p (expr, type))
2321 {
2322 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2323 && tree_int_cst_sgn (expr) < 0)
2324 {
2325 if (produce_warns)
2326 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2327 " implicitly converted to unsigned type");
2328 }
2329 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2330 {
2331 if (produce_warns)
2332 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2333 " constant value to negative integer");
2334 }
2335 else
2336 give_warning = UNSAFE_OTHER;
2337 }
2338 else if (TREE_CODE (type) == REAL_TYPE)
2339 {
2340 /* Warn for an integer constant that does not fit into real type. */
2341 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2342 {
2343 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2344 if (!exact_real_truncate (TYPE_MODE (type), &a))
2345 give_warning = UNSAFE_REAL;
2346 }
2347 /* Warn for a real constant that does not fit into a smaller
2348 real type. */
2349 else if (TREE_CODE (expr_type) == REAL_TYPE
2350 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2351 {
2352 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2353 if (!exact_real_truncate (TYPE_MODE (type), &a))
2354 give_warning = UNSAFE_REAL;
2355 }
2356 }
2357 }
2358
2359 else if (TREE_CODE (expr) == COMPLEX_CST)
2360 {
2361 tree imag_part = TREE_IMAGPART (expr);
2362 /* Conversion from complex constant with zero imaginary part,
2363 perform check for conversion of real part. */
2364 if ((TREE_CODE (imag_part) == REAL_CST
2365 && real_zerop (imag_part))
2366 || (TREE_CODE (imag_part) == INTEGER_CST
2367 && integer_zerop (imag_part)))
2368 /* Note: in this branch we use recursive call to unsafe_conversion_p
2369 with different type of EXPR, but it is still safe, because when EXPR
2370 is a constant, it's type is not used in text of generated warnings
2371 (otherwise they could sound misleading). */
2372 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2373 produce_warns);
2374 /* Conversion from complex constant with non-zero imaginary part. */
2375 else
2376 {
2377 /* Conversion to complex type.
2378 Perform checks for both real and imaginary parts. */
2379 if (TREE_CODE (type) == COMPLEX_TYPE)
2380 {
2381 /* Unfortunately, produce_warns must be false in two subsequent
2382 calls of unsafe_conversion_p, because otherwise we could
2383 produce strange "double" warnings, if both real and imaginary
2384 parts have conversion problems related to signedness.
2385
2386 For example:
2387 int32_t _Complex a = 0x80000000 + 0x80000000i;
2388
2389 Possible solution: add a separate function for checking
2390 constants and combine result of two calls appropriately. */
2391 enum conversion_safety re_safety =
2392 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2393 enum conversion_safety im_safety =
2394 unsafe_conversion_p (loc, type, imag_part, false);
2395
2396 /* Merge the results into appropriate single warning. */
2397
2398 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2399 if (re_safety == im_safety)
2400 give_warning = re_safety;
2401 else if (!re_safety && im_safety)
2402 give_warning = im_safety;
2403 else if (re_safety && !im_safety)
2404 give_warning = re_safety;
2405 else
2406 give_warning = UNSAFE_OTHER;
2407 }
2408 /* Warn about conversion from complex to real or integer type. */
2409 else
2410 give_warning = UNSAFE_IMAGINARY;
2411 }
2412 }
2413
2414 /* Checks for remaining case: EXPR is not constant. */
2415 else
2416 {
2417 /* Warn for real types converted to integer types. */
2418 if (TREE_CODE (expr_type) == REAL_TYPE
2419 && TREE_CODE (type) == INTEGER_TYPE)
2420 give_warning = UNSAFE_REAL;
2421
2422 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2423 && TREE_CODE (type) == INTEGER_TYPE)
2424 {
2425 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2426 expr = get_unwidened (expr, 0);
2427 expr_type = TREE_TYPE (expr);
2428
2429 /* Don't warn for short y; short x = ((int)y & 0xff); */
2430 if (TREE_CODE (expr) == BIT_AND_EXPR
2431 || TREE_CODE (expr) == BIT_IOR_EXPR
2432 || TREE_CODE (expr) == BIT_XOR_EXPR)
2433 {
2434 /* If both args were extended from a shortest type,
2435 use that type if that is safe. */
2436 expr_type = shorten_binary_op (expr_type,
2437 TREE_OPERAND (expr, 0),
2438 TREE_OPERAND (expr, 1),
2439 /* bitwise */1);
2440
2441 if (TREE_CODE (expr) == BIT_AND_EXPR)
2442 {
2443 tree op0 = TREE_OPERAND (expr, 0);
2444 tree op1 = TREE_OPERAND (expr, 1);
2445 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2446 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2447
2448 /* If one of the operands is a non-negative constant
2449 that fits in the target type, then the type of the
2450 other operand does not matter. */
2451 if ((TREE_CODE (op0) == INTEGER_CST
2452 && int_fits_type_p (op0, c_common_signed_type (type))
2453 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2454 || (TREE_CODE (op1) == INTEGER_CST
2455 && int_fits_type_p (op1, c_common_signed_type (type))
2456 && int_fits_type_p (op1,
2457 c_common_unsigned_type (type))))
2458 return SAFE_CONVERSION;
2459 /* If constant is unsigned and fits in the target
2460 type, then the result will also fit. */
2461 else if ((TREE_CODE (op0) == INTEGER_CST
2462 && unsigned0
2463 && int_fits_type_p (op0, type))
2464 || (TREE_CODE (op1) == INTEGER_CST
2465 && unsigned1
2466 && int_fits_type_p (op1, type)))
2467 return SAFE_CONVERSION;
2468 }
2469 }
2470 /* Warn for integer types converted to smaller integer types. */
2471 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2472 give_warning = UNSAFE_OTHER;
2473
2474 /* When they are the same width but different signedness,
2475 then the value may change. */
2476 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2477 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2478 /* Even when converted to a bigger type, if the type is
2479 unsigned but expr is signed, then negative values
2480 will be changed. */
2481 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2482 && produce_warns)
2483 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2484 "may change the sign of the result",
2485 type, expr_type);
2486 }
2487
2488 /* Warn for integer types converted to real types if and only if
2489 all the range of values of the integer type cannot be
2490 represented by the real type. */
2491 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2492 && TREE_CODE (type) == REAL_TYPE)
2493 {
2494 /* Don't warn about char y = 0xff; float x = (int) y; */
2495 expr = get_unwidened (expr, 0);
2496 expr_type = TREE_TYPE (expr);
2497
2498 if (!int_safely_convertible_to_real_p (expr_type, type))
2499 give_warning = UNSAFE_OTHER;
2500 }
2501
2502 /* Warn for real types converted to smaller real types. */
2503 else if (TREE_CODE (expr_type) == REAL_TYPE
2504 && TREE_CODE (type) == REAL_TYPE
2505 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2506 give_warning = UNSAFE_REAL;
2507
2508 /* Check conversion between two complex types. */
2509 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2510 && TREE_CODE (type) == COMPLEX_TYPE)
2511 {
2512 /* Extract underlying types (i.e., type of real and imaginary
2513 parts) of expr_type and type. */
2514 tree from_type = TREE_TYPE (expr_type);
2515 tree to_type = TREE_TYPE (type);
2516
2517 /* Warn for real types converted to integer types. */
2518 if (TREE_CODE (from_type) == REAL_TYPE
2519 && TREE_CODE (to_type) == INTEGER_TYPE)
2520 give_warning = UNSAFE_REAL;
2521
2522 /* Warn for real types converted to smaller real types. */
2523 else if (TREE_CODE (from_type) == REAL_TYPE
2524 && TREE_CODE (to_type) == REAL_TYPE
2525 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2526 give_warning = UNSAFE_REAL;
2527
2528 /* Check conversion for complex integer types. Here implementation
2529 is simpler than for real-domain integers because it does not
2530 involve sophisticated cases, such as bitmasks, casts, etc. */
2531 else if (TREE_CODE (from_type) == INTEGER_TYPE
2532 && TREE_CODE (to_type) == INTEGER_TYPE)
2533 {
2534 /* Warn for integer types converted to smaller integer types. */
2535 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2536 give_warning = UNSAFE_OTHER;
2537
2538 /* Check for different signedness, see case for real-domain
2539 integers (above) for a more detailed comment. */
2540 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2541 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2542 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2543 && produce_warns)
2544 warning_at (loc, OPT_Wsign_conversion,
2545 "conversion to %qT from %qT "
2546 "may change the sign of the result",
2547 type, expr_type);
2548 }
2549 else if (TREE_CODE (from_type) == INTEGER_TYPE
2550 && TREE_CODE (to_type) == REAL_TYPE
2551 && !int_safely_convertible_to_real_p (from_type, to_type))
2552 give_warning = UNSAFE_OTHER;
2553 }
2554
2555 /* Warn for complex types converted to real or integer types. */
2556 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2557 && TREE_CODE (type) != COMPLEX_TYPE)
2558 give_warning = UNSAFE_IMAGINARY;
2559 }
2560
2561 return give_warning;
2562 }
2563
2564 /* Warns if the conversion of EXPR to TYPE may alter a value.
2565 This is a helper function for warnings_for_convert_and_check. */
2566
2567 static void
2568 conversion_warning (location_t loc, tree type, tree expr)
2569 {
2570 tree expr_type = TREE_TYPE (expr);
2571 enum conversion_safety conversion_kind;
2572
2573 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2574 return;
2575
2576 /* This may happen, because for LHS op= RHS we preevaluate
2577 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2578 means we could no longer see the code of the EXPR. */
2579 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2580 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2581 if (TREE_CODE (expr) == SAVE_EXPR)
2582 expr = TREE_OPERAND (expr, 0);
2583
2584 switch (TREE_CODE (expr))
2585 {
2586 case EQ_EXPR:
2587 case NE_EXPR:
2588 case LE_EXPR:
2589 case GE_EXPR:
2590 case LT_EXPR:
2591 case GT_EXPR:
2592 case TRUTH_ANDIF_EXPR:
2593 case TRUTH_ORIF_EXPR:
2594 case TRUTH_AND_EXPR:
2595 case TRUTH_OR_EXPR:
2596 case TRUTH_XOR_EXPR:
2597 case TRUTH_NOT_EXPR:
2598 /* Conversion from boolean to a signed:1 bit-field (which only
2599 can hold the values 0 and -1) doesn't lose information - but
2600 it does change the value. */
2601 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2602 warning_at (loc, OPT_Wconversion,
2603 "conversion to %qT from boolean expression", type);
2604 return;
2605
2606 case REAL_CST:
2607 case INTEGER_CST:
2608 case COMPLEX_CST:
2609 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2610 if (conversion_kind == UNSAFE_REAL)
2611 warning_at (loc, OPT_Wfloat_conversion,
2612 "conversion to %qT alters %qT constant value",
2613 type, expr_type);
2614 else if (conversion_kind)
2615 warning_at (loc, OPT_Wconversion,
2616 "conversion to %qT alters %qT constant value",
2617 type, expr_type);
2618 return;
2619
2620 case COND_EXPR:
2621 {
2622 /* In case of COND_EXPR, we do not care about the type of
2623 COND_EXPR, only about the conversion of each operand. */
2624 tree op1 = TREE_OPERAND (expr, 1);
2625 tree op2 = TREE_OPERAND (expr, 2);
2626
2627 conversion_warning (loc, type, op1);
2628 conversion_warning (loc, type, op2);
2629 return;
2630 }
2631
2632 default: /* 'expr' is not a constant. */
2633 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2634 if (conversion_kind == UNSAFE_REAL)
2635 warning_at (loc, OPT_Wfloat_conversion,
2636 "conversion to %qT from %qT may alter its value",
2637 type, expr_type);
2638 else if (conversion_kind == UNSAFE_IMAGINARY)
2639 warning_at (loc, OPT_Wconversion,
2640 "conversion to %qT from %qT discards imaginary component",
2641 type, expr_type);
2642 else if (conversion_kind)
2643 warning_at (loc, OPT_Wconversion,
2644 "conversion to %qT from %qT may alter its value",
2645 type, expr_type);
2646 }
2647 }
2648
2649 /* Produce warnings after a conversion. RESULT is the result of
2650 converting EXPR to TYPE. This is a helper function for
2651 convert_and_check and cp_convert_and_check. */
2652
2653 void
2654 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2655 tree result)
2656 {
2657 loc = expansion_point_location_if_in_system_header (loc);
2658
2659 if (TREE_CODE (expr) == INTEGER_CST
2660 && (TREE_CODE (type) == INTEGER_TYPE
2661 || TREE_CODE (type) == ENUMERAL_TYPE)
2662 && !int_fits_type_p (expr, type))
2663 {
2664 /* Do not diagnose overflow in a constant expression merely
2665 because a conversion overflowed. */
2666 if (TREE_OVERFLOW (result))
2667 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2668
2669 if (TYPE_UNSIGNED (type))
2670 {
2671 /* This detects cases like converting -129 or 256 to
2672 unsigned char. */
2673 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2674 warning_at (loc, OPT_Woverflow,
2675 "large integer implicitly truncated to unsigned type");
2676 else
2677 conversion_warning (loc, type, expr);
2678 }
2679 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2680 warning_at (loc, OPT_Woverflow,
2681 "overflow in implicit constant conversion");
2682 /* No warning for converting 0x80000000 to int. */
2683 else if (pedantic
2684 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2685 || TYPE_PRECISION (TREE_TYPE (expr))
2686 != TYPE_PRECISION (type)))
2687 warning_at (loc, OPT_Woverflow,
2688 "overflow in implicit constant conversion");
2689
2690 else
2691 conversion_warning (loc, type, expr);
2692 }
2693 else if ((TREE_CODE (result) == INTEGER_CST
2694 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2695 warning_at (loc, OPT_Woverflow,
2696 "overflow in implicit constant conversion");
2697 else
2698 conversion_warning (loc, type, expr);
2699 }
2700
2701
2702 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2703 Invoke this function on every expression that is converted implicitly,
2704 i.e. because of language rules and not because of an explicit cast. */
2705
2706 tree
2707 convert_and_check (location_t loc, tree type, tree expr)
2708 {
2709 tree result;
2710 tree expr_for_warning;
2711
2712 /* Convert from a value with possible excess precision rather than
2713 via the semantic type, but do not warn about values not fitting
2714 exactly in the semantic type. */
2715 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2716 {
2717 tree orig_type = TREE_TYPE (expr);
2718 expr = TREE_OPERAND (expr, 0);
2719 expr_for_warning = convert (orig_type, expr);
2720 if (orig_type == type)
2721 return expr_for_warning;
2722 }
2723 else
2724 expr_for_warning = expr;
2725
2726 if (TREE_TYPE (expr) == type)
2727 return expr;
2728
2729 result = convert (type, expr);
2730
2731 if (c_inhibit_evaluation_warnings == 0
2732 && !TREE_OVERFLOW_P (expr)
2733 && result != error_mark_node)
2734 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2735
2736 return result;
2737 }
2738 \f
2739 /* A node in a list that describes references to variables (EXPR), which are
2740 either read accesses if WRITER is zero, or write accesses, in which case
2741 WRITER is the parent of EXPR. */
2742 struct tlist
2743 {
2744 struct tlist *next;
2745 tree expr, writer;
2746 };
2747
2748 /* Used to implement a cache the results of a call to verify_tree. We only
2749 use this for SAVE_EXPRs. */
2750 struct tlist_cache
2751 {
2752 struct tlist_cache *next;
2753 struct tlist *cache_before_sp;
2754 struct tlist *cache_after_sp;
2755 tree expr;
2756 };
2757
2758 /* Obstack to use when allocating tlist structures, and corresponding
2759 firstobj. */
2760 static struct obstack tlist_obstack;
2761 static char *tlist_firstobj = 0;
2762
2763 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2764 warnings. */
2765 static struct tlist *warned_ids;
2766 /* SAVE_EXPRs need special treatment. We process them only once and then
2767 cache the results. */
2768 static struct tlist_cache *save_expr_cache;
2769
2770 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2771 static void merge_tlist (struct tlist **, struct tlist *, int);
2772 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2773 static int warning_candidate_p (tree);
2774 static bool candidate_equal_p (const_tree, const_tree);
2775 static void warn_for_collisions (struct tlist *);
2776 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2777 static struct tlist *new_tlist (struct tlist *, tree, tree);
2778
2779 /* Create a new struct tlist and fill in its fields. */
2780 static struct tlist *
2781 new_tlist (struct tlist *next, tree t, tree writer)
2782 {
2783 struct tlist *l;
2784 l = XOBNEW (&tlist_obstack, struct tlist);
2785 l->next = next;
2786 l->expr = t;
2787 l->writer = writer;
2788 return l;
2789 }
2790
2791 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2792 is nonnull, we ignore any node we find which has a writer equal to it. */
2793
2794 static void
2795 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2796 {
2797 while (add)
2798 {
2799 struct tlist *next = add->next;
2800 if (!copy)
2801 add->next = *to;
2802 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2803 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2804 add = next;
2805 }
2806 }
2807
2808 /* Merge the nodes of ADD into TO. This merging process is done so that for
2809 each variable that already exists in TO, no new node is added; however if
2810 there is a write access recorded in ADD, and an occurrence on TO is only
2811 a read access, then the occurrence in TO will be modified to record the
2812 write. */
2813
2814 static void
2815 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2816 {
2817 struct tlist **end = to;
2818
2819 while (*end)
2820 end = &(*end)->next;
2821
2822 while (add)
2823 {
2824 int found = 0;
2825 struct tlist *tmp2;
2826 struct tlist *next = add->next;
2827
2828 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2829 if (candidate_equal_p (tmp2->expr, add->expr))
2830 {
2831 found = 1;
2832 if (!tmp2->writer)
2833 tmp2->writer = add->writer;
2834 }
2835 if (!found)
2836 {
2837 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
2838 end = &(*end)->next;
2839 *end = 0;
2840 }
2841 add = next;
2842 }
2843 }
2844
2845 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2846 references in list LIST conflict with it, excluding reads if ONLY writers
2847 is nonzero. */
2848
2849 static void
2850 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2851 int only_writes)
2852 {
2853 struct tlist *tmp;
2854
2855 /* Avoid duplicate warnings. */
2856 for (tmp = warned_ids; tmp; tmp = tmp->next)
2857 if (candidate_equal_p (tmp->expr, written))
2858 return;
2859
2860 while (list)
2861 {
2862 if (candidate_equal_p (list->expr, written)
2863 && !candidate_equal_p (list->writer, writer)
2864 && (!only_writes || list->writer))
2865 {
2866 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2867 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
2868 OPT_Wsequence_point, "operation on %qE may be undefined",
2869 list->expr);
2870 }
2871 list = list->next;
2872 }
2873 }
2874
2875 /* Given a list LIST of references to variables, find whether any of these
2876 can cause conflicts due to missing sequence points. */
2877
2878 static void
2879 warn_for_collisions (struct tlist *list)
2880 {
2881 struct tlist *tmp;
2882
2883 for (tmp = list; tmp; tmp = tmp->next)
2884 {
2885 if (tmp->writer)
2886 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2887 }
2888 }
2889
2890 /* Return nonzero if X is a tree that can be verified by the sequence point
2891 warnings. */
2892 static int
2893 warning_candidate_p (tree x)
2894 {
2895 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2896 return 0;
2897
2898 if (TREE_CODE (x) == BLOCK)
2899 return 0;
2900
2901 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2902 (lvalue_p) crash on TRY/CATCH. */
2903 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2904 return 0;
2905
2906 if (!lvalue_p (x))
2907 return 0;
2908
2909 /* No point to track non-const calls, they will never satisfy
2910 operand_equal_p. */
2911 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2912 return 0;
2913
2914 if (TREE_CODE (x) == STRING_CST)
2915 return 0;
2916
2917 return 1;
2918 }
2919
2920 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2921 static bool
2922 candidate_equal_p (const_tree x, const_tree y)
2923 {
2924 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2925 }
2926
2927 /* Walk the tree X, and record accesses to variables. If X is written by the
2928 parent tree, WRITER is the parent.
2929 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2930 expression or its only operand forces a sequence point, then everything up
2931 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2932 in PNO_SP.
2933 Once we return, we will have emitted warnings if any subexpression before
2934 such a sequence point could be undefined. On a higher level, however, the
2935 sequence point may not be relevant, and we'll merge the two lists.
2936
2937 Example: (b++, a) + b;
2938 The call that processes the COMPOUND_EXPR will store the increment of B
2939 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2940 processes the PLUS_EXPR will need to merge the two lists so that
2941 eventually, all accesses end up on the same list (and we'll warn about the
2942 unordered subexpressions b++ and b.
2943
2944 A note on merging. If we modify the former example so that our expression
2945 becomes
2946 (b++, b) + a
2947 care must be taken not simply to add all three expressions into the final
2948 PNO_SP list. The function merge_tlist takes care of that by merging the
2949 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2950 way, so that no more than one access to B is recorded. */
2951
2952 static void
2953 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2954 tree writer)
2955 {
2956 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2957 enum tree_code code;
2958 enum tree_code_class cl;
2959
2960 /* X may be NULL if it is the operand of an empty statement expression
2961 ({ }). */
2962 if (x == NULL)
2963 return;
2964
2965 restart:
2966 code = TREE_CODE (x);
2967 cl = TREE_CODE_CLASS (code);
2968
2969 if (warning_candidate_p (x))
2970 *pno_sp = new_tlist (*pno_sp, x, writer);
2971
2972 switch (code)
2973 {
2974 case CONSTRUCTOR:
2975 case SIZEOF_EXPR:
2976 return;
2977
2978 case COMPOUND_EXPR:
2979 case TRUTH_ANDIF_EXPR:
2980 case TRUTH_ORIF_EXPR:
2981 tmp_before = tmp_nosp = tmp_list3 = 0;
2982 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2983 warn_for_collisions (tmp_nosp);
2984 merge_tlist (pbefore_sp, tmp_before, 0);
2985 merge_tlist (pbefore_sp, tmp_nosp, 0);
2986 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2987 merge_tlist (pbefore_sp, tmp_list3, 0);
2988 return;
2989
2990 case COND_EXPR:
2991 tmp_before = tmp_list2 = 0;
2992 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2993 warn_for_collisions (tmp_list2);
2994 merge_tlist (pbefore_sp, tmp_before, 0);
2995 merge_tlist (pbefore_sp, tmp_list2, 0);
2996
2997 tmp_list3 = tmp_nosp = 0;
2998 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2999 warn_for_collisions (tmp_nosp);
3000 merge_tlist (pbefore_sp, tmp_list3, 0);
3001
3002 tmp_list3 = tmp_list2 = 0;
3003 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3004 warn_for_collisions (tmp_list2);
3005 merge_tlist (pbefore_sp, tmp_list3, 0);
3006 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3007 two first, to avoid warning for (a ? b++ : b++). */
3008 merge_tlist (&tmp_nosp, tmp_list2, 0);
3009 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3010 return;
3011
3012 case PREDECREMENT_EXPR:
3013 case PREINCREMENT_EXPR:
3014 case POSTDECREMENT_EXPR:
3015 case POSTINCREMENT_EXPR:
3016 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3017 return;
3018
3019 case MODIFY_EXPR:
3020 tmp_before = tmp_nosp = tmp_list3 = 0;
3021 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3022 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3023 /* Expressions inside the LHS are not ordered wrt. the sequence points
3024 in the RHS. Example:
3025 *a = (a++, 2)
3026 Despite the fact that the modification of "a" is in the before_sp
3027 list (tmp_before), it conflicts with the use of "a" in the LHS.
3028 We can handle this by adding the contents of tmp_list3
3029 to those of tmp_before, and redoing the collision warnings for that
3030 list. */
3031 add_tlist (&tmp_before, tmp_list3, x, 1);
3032 warn_for_collisions (tmp_before);
3033 /* Exclude the LHS itself here; we first have to merge it into the
3034 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3035 didn't exclude the LHS, we'd get it twice, once as a read and once
3036 as a write. */
3037 add_tlist (pno_sp, tmp_list3, x, 0);
3038 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3039
3040 merge_tlist (pbefore_sp, tmp_before, 0);
3041 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3042 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3043 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3044 return;
3045
3046 case CALL_EXPR:
3047 /* We need to warn about conflicts among arguments and conflicts between
3048 args and the function address. Side effects of the function address,
3049 however, are not ordered by the sequence point of the call. */
3050 {
3051 call_expr_arg_iterator iter;
3052 tree arg;
3053 tmp_before = tmp_nosp = 0;
3054 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3055 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3056 {
3057 tmp_list2 = tmp_list3 = 0;
3058 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3059 merge_tlist (&tmp_list3, tmp_list2, 0);
3060 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3061 }
3062 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3063 warn_for_collisions (tmp_before);
3064 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3065 return;
3066 }
3067
3068 case TREE_LIST:
3069 /* Scan all the list, e.g. indices of multi dimensional array. */
3070 while (x)
3071 {
3072 tmp_before = tmp_nosp = 0;
3073 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3074 merge_tlist (&tmp_nosp, tmp_before, 0);
3075 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3076 x = TREE_CHAIN (x);
3077 }
3078 return;
3079
3080 case SAVE_EXPR:
3081 {
3082 struct tlist_cache *t;
3083 for (t = save_expr_cache; t; t = t->next)
3084 if (candidate_equal_p (t->expr, x))
3085 break;
3086
3087 if (!t)
3088 {
3089 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3090 t->next = save_expr_cache;
3091 t->expr = x;
3092 save_expr_cache = t;
3093
3094 tmp_before = tmp_nosp = 0;
3095 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3096 warn_for_collisions (tmp_nosp);
3097
3098 tmp_list3 = 0;
3099 merge_tlist (&tmp_list3, tmp_nosp, 0);
3100 t->cache_before_sp = tmp_before;
3101 t->cache_after_sp = tmp_list3;
3102 }
3103 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3104 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3105 return;
3106 }
3107
3108 case ADDR_EXPR:
3109 x = TREE_OPERAND (x, 0);
3110 if (DECL_P (x))
3111 return;
3112 writer = 0;
3113 goto restart;
3114
3115 default:
3116 /* For other expressions, simply recurse on their operands.
3117 Manual tail recursion for unary expressions.
3118 Other non-expressions need not be processed. */
3119 if (cl == tcc_unary)
3120 {
3121 x = TREE_OPERAND (x, 0);
3122 writer = 0;
3123 goto restart;
3124 }
3125 else if (IS_EXPR_CODE_CLASS (cl))
3126 {
3127 int lp;
3128 int max = TREE_OPERAND_LENGTH (x);
3129 for (lp = 0; lp < max; lp++)
3130 {
3131 tmp_before = tmp_nosp = 0;
3132 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3133 merge_tlist (&tmp_nosp, tmp_before, 0);
3134 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3135 }
3136 }
3137 return;
3138 }
3139 }
3140
3141 /* Try to warn for undefined behavior in EXPR due to missing sequence
3142 points. */
3143
3144 DEBUG_FUNCTION void
3145 verify_sequence_points (tree expr)
3146 {
3147 struct tlist *before_sp = 0, *after_sp = 0;
3148
3149 warned_ids = 0;
3150 save_expr_cache = 0;
3151 if (tlist_firstobj == 0)
3152 {
3153 gcc_obstack_init (&tlist_obstack);
3154 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3155 }
3156
3157 verify_tree (expr, &before_sp, &after_sp, 0);
3158 warn_for_collisions (after_sp);
3159 obstack_free (&tlist_obstack, tlist_firstobj);
3160 }
3161 \f
3162 /* Validate the expression after `case' and apply default promotions. */
3163
3164 static tree
3165 check_case_value (location_t loc, tree value)
3166 {
3167 if (value == NULL_TREE)
3168 return value;
3169
3170 if (TREE_CODE (value) == INTEGER_CST)
3171 /* Promote char or short to int. */
3172 value = perform_integral_promotions (value);
3173 else if (value != error_mark_node)
3174 {
3175 error_at (loc, "case label does not reduce to an integer constant");
3176 value = error_mark_node;
3177 }
3178
3179 constant_expression_warning (value);
3180
3181 return value;
3182 }
3183 \f
3184 /* See if the case values LOW and HIGH are in the range of the original
3185 type (i.e. before the default conversion to int) of the switch testing
3186 expression.
3187 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3188 the type before promoting it. CASE_LOW_P is a pointer to the lower
3189 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3190 if the case is not a case range.
3191 The caller has to make sure that we are not called with NULL for
3192 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3193 was a case value that doesn't fit into the range of the ORIG_TYPE.
3194 Returns true if the case label is in range of ORIG_TYPE (saturated or
3195 untouched) or false if the label is out of range. */
3196
3197 static bool
3198 check_case_bounds (location_t loc, tree type, tree orig_type,
3199 tree *case_low_p, tree *case_high_p,
3200 bool *outside_range_p)
3201 {
3202 tree min_value, max_value;
3203 tree case_low = *case_low_p;
3204 tree case_high = case_high_p ? *case_high_p : case_low;
3205
3206 /* If there was a problem with the original type, do nothing. */
3207 if (orig_type == error_mark_node)
3208 return true;
3209
3210 min_value = TYPE_MIN_VALUE (orig_type);
3211 max_value = TYPE_MAX_VALUE (orig_type);
3212
3213 /* We'll really need integer constants here. */
3214 case_low = fold (case_low);
3215 case_high = fold (case_high);
3216
3217 /* Case label is less than minimum for type. */
3218 if (tree_int_cst_compare (case_low, min_value) < 0
3219 && tree_int_cst_compare (case_high, min_value) < 0)
3220 {
3221 warning_at (loc, 0, "case label value is less than minimum value "
3222 "for type");
3223 *outside_range_p = true;
3224 return false;
3225 }
3226
3227 /* Case value is greater than maximum for type. */
3228 if (tree_int_cst_compare (case_low, max_value) > 0
3229 && tree_int_cst_compare (case_high, max_value) > 0)
3230 {
3231 warning_at (loc, 0, "case label value exceeds maximum value for type");
3232 *outside_range_p = true;
3233 return false;
3234 }
3235
3236 /* Saturate lower case label value to minimum. */
3237 if (tree_int_cst_compare (case_high, min_value) >= 0
3238 && tree_int_cst_compare (case_low, min_value) < 0)
3239 {
3240 warning_at (loc, 0, "lower value in case label range"
3241 " less than minimum value for type");
3242 *outside_range_p = true;
3243 case_low = min_value;
3244 }
3245
3246 /* Saturate upper case label value to maximum. */
3247 if (tree_int_cst_compare (case_low, max_value) <= 0
3248 && tree_int_cst_compare (case_high, max_value) > 0)
3249 {
3250 warning_at (loc, 0, "upper value in case label range"
3251 " exceeds maximum value for type");
3252 *outside_range_p = true;
3253 case_high = max_value;
3254 }
3255
3256 if (*case_low_p != case_low)
3257 *case_low_p = convert (type, case_low);
3258 if (case_high_p && *case_high_p != case_high)
3259 *case_high_p = convert (type, case_high);
3260
3261 return true;
3262 }
3263 \f
3264 /* Return an integer type with BITS bits of precision,
3265 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3266
3267 tree
3268 c_common_type_for_size (unsigned int bits, int unsignedp)
3269 {
3270 int i;
3271
3272 if (bits == TYPE_PRECISION (integer_type_node))
3273 return unsignedp ? unsigned_type_node : integer_type_node;
3274
3275 if (bits == TYPE_PRECISION (signed_char_type_node))
3276 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3277
3278 if (bits == TYPE_PRECISION (short_integer_type_node))
3279 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3280
3281 if (bits == TYPE_PRECISION (long_integer_type_node))
3282 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3283
3284 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3285 return (unsignedp ? long_long_unsigned_type_node
3286 : long_long_integer_type_node);
3287
3288 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3289 if (int_n_enabled_p[i]
3290 && bits == int_n_data[i].bitsize)
3291 return (unsignedp ? int_n_trees[i].unsigned_type
3292 : int_n_trees[i].signed_type);
3293
3294 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3295 return (unsignedp ? widest_unsigned_literal_type_node
3296 : widest_integer_literal_type_node);
3297
3298 if (bits <= TYPE_PRECISION (intQI_type_node))
3299 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3300
3301 if (bits <= TYPE_PRECISION (intHI_type_node))
3302 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3303
3304 if (bits <= TYPE_PRECISION (intSI_type_node))
3305 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3306
3307 if (bits <= TYPE_PRECISION (intDI_type_node))
3308 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3309
3310 return 0;
3311 }
3312
3313 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3314 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3315 and saturating if SATP is nonzero, otherwise not saturating. */
3316
3317 tree
3318 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3319 int unsignedp, int satp)
3320 {
3321 machine_mode mode;
3322 if (ibit == 0)
3323 mode = unsignedp ? UQQmode : QQmode;
3324 else
3325 mode = unsignedp ? UHAmode : HAmode;
3326
3327 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3328 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3329 break;
3330
3331 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3332 {
3333 sorry ("GCC cannot support operators with integer types and "
3334 "fixed-point types that have too many integral and "
3335 "fractional bits together");
3336 return 0;
3337 }
3338
3339 return c_common_type_for_mode (mode, satp);
3340 }
3341
3342 /* Used for communication between c_common_type_for_mode and
3343 c_register_builtin_type. */
3344 tree registered_builtin_types;
3345
3346 /* Return a data type that has machine mode MODE.
3347 If the mode is an integer,
3348 then UNSIGNEDP selects between signed and unsigned types.
3349 If the mode is a fixed-point mode,
3350 then UNSIGNEDP selects between saturating and nonsaturating types. */
3351
3352 tree
3353 c_common_type_for_mode (machine_mode mode, int unsignedp)
3354 {
3355 tree t;
3356 int i;
3357
3358 if (mode == TYPE_MODE (integer_type_node))
3359 return unsignedp ? unsigned_type_node : integer_type_node;
3360
3361 if (mode == TYPE_MODE (signed_char_type_node))
3362 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3363
3364 if (mode == TYPE_MODE (short_integer_type_node))
3365 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3366
3367 if (mode == TYPE_MODE (long_integer_type_node))
3368 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3369
3370 if (mode == TYPE_MODE (long_long_integer_type_node))
3371 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3372
3373 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3374 if (int_n_enabled_p[i]
3375 && mode == int_n_data[i].m)
3376 return (unsignedp ? int_n_trees[i].unsigned_type
3377 : int_n_trees[i].signed_type);
3378
3379 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3380 return unsignedp ? widest_unsigned_literal_type_node
3381 : widest_integer_literal_type_node;
3382
3383 if (mode == QImode)
3384 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3385
3386 if (mode == HImode)
3387 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3388
3389 if (mode == SImode)
3390 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3391
3392 if (mode == DImode)
3393 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3394
3395 #if HOST_BITS_PER_WIDE_INT >= 64
3396 if (mode == TYPE_MODE (intTI_type_node))
3397 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3398 #endif
3399
3400 if (mode == TYPE_MODE (float_type_node))
3401 return float_type_node;
3402
3403 if (mode == TYPE_MODE (double_type_node))
3404 return double_type_node;
3405
3406 if (mode == TYPE_MODE (long_double_type_node))
3407 return long_double_type_node;
3408
3409 if (mode == TYPE_MODE (void_type_node))
3410 return void_type_node;
3411
3412 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3413 return (unsignedp
3414 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3415 : make_signed_type (GET_MODE_PRECISION (mode)));
3416
3417 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3418 return (unsignedp
3419 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3420 : make_signed_type (GET_MODE_PRECISION (mode)));
3421
3422 if (COMPLEX_MODE_P (mode))
3423 {
3424 machine_mode inner_mode;
3425 tree inner_type;
3426
3427 if (mode == TYPE_MODE (complex_float_type_node))
3428 return complex_float_type_node;
3429 if (mode == TYPE_MODE (complex_double_type_node))
3430 return complex_double_type_node;
3431 if (mode == TYPE_MODE (complex_long_double_type_node))
3432 return complex_long_double_type_node;
3433
3434 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3435 return complex_integer_type_node;
3436
3437 inner_mode = GET_MODE_INNER (mode);
3438 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3439 if (inner_type != NULL_TREE)
3440 return build_complex_type (inner_type);
3441 }
3442 else if (VECTOR_MODE_P (mode))
3443 {
3444 machine_mode inner_mode = GET_MODE_INNER (mode);
3445 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3446 if (inner_type != NULL_TREE)
3447 return build_vector_type_for_mode (inner_type, mode);
3448 }
3449
3450 if (mode == TYPE_MODE (dfloat32_type_node))
3451 return dfloat32_type_node;
3452 if (mode == TYPE_MODE (dfloat64_type_node))
3453 return dfloat64_type_node;
3454 if (mode == TYPE_MODE (dfloat128_type_node))
3455 return dfloat128_type_node;
3456
3457 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3458 {
3459 if (mode == TYPE_MODE (short_fract_type_node))
3460 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3461 if (mode == TYPE_MODE (fract_type_node))
3462 return unsignedp ? sat_fract_type_node : fract_type_node;
3463 if (mode == TYPE_MODE (long_fract_type_node))
3464 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3465 if (mode == TYPE_MODE (long_long_fract_type_node))
3466 return unsignedp ? sat_long_long_fract_type_node
3467 : long_long_fract_type_node;
3468
3469 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3470 return unsignedp ? sat_unsigned_short_fract_type_node
3471 : unsigned_short_fract_type_node;
3472 if (mode == TYPE_MODE (unsigned_fract_type_node))
3473 return unsignedp ? sat_unsigned_fract_type_node
3474 : unsigned_fract_type_node;
3475 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3476 return unsignedp ? sat_unsigned_long_fract_type_node
3477 : unsigned_long_fract_type_node;
3478 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3479 return unsignedp ? sat_unsigned_long_long_fract_type_node
3480 : unsigned_long_long_fract_type_node;
3481
3482 if (mode == TYPE_MODE (short_accum_type_node))
3483 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3484 if (mode == TYPE_MODE (accum_type_node))
3485 return unsignedp ? sat_accum_type_node : accum_type_node;
3486 if (mode == TYPE_MODE (long_accum_type_node))
3487 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3488 if (mode == TYPE_MODE (long_long_accum_type_node))
3489 return unsignedp ? sat_long_long_accum_type_node
3490 : long_long_accum_type_node;
3491
3492 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3493 return unsignedp ? sat_unsigned_short_accum_type_node
3494 : unsigned_short_accum_type_node;
3495 if (mode == TYPE_MODE (unsigned_accum_type_node))
3496 return unsignedp ? sat_unsigned_accum_type_node
3497 : unsigned_accum_type_node;
3498 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3499 return unsignedp ? sat_unsigned_long_accum_type_node
3500 : unsigned_long_accum_type_node;
3501 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3502 return unsignedp ? sat_unsigned_long_long_accum_type_node
3503 : unsigned_long_long_accum_type_node;
3504
3505 if (mode == QQmode)
3506 return unsignedp ? sat_qq_type_node : qq_type_node;
3507 if (mode == HQmode)
3508 return unsignedp ? sat_hq_type_node : hq_type_node;
3509 if (mode == SQmode)
3510 return unsignedp ? sat_sq_type_node : sq_type_node;
3511 if (mode == DQmode)
3512 return unsignedp ? sat_dq_type_node : dq_type_node;
3513 if (mode == TQmode)
3514 return unsignedp ? sat_tq_type_node : tq_type_node;
3515
3516 if (mode == UQQmode)
3517 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3518 if (mode == UHQmode)
3519 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3520 if (mode == USQmode)
3521 return unsignedp ? sat_usq_type_node : usq_type_node;
3522 if (mode == UDQmode)
3523 return unsignedp ? sat_udq_type_node : udq_type_node;
3524 if (mode == UTQmode)
3525 return unsignedp ? sat_utq_type_node : utq_type_node;
3526
3527 if (mode == HAmode)
3528 return unsignedp ? sat_ha_type_node : ha_type_node;
3529 if (mode == SAmode)
3530 return unsignedp ? sat_sa_type_node : sa_type_node;
3531 if (mode == DAmode)
3532 return unsignedp ? sat_da_type_node : da_type_node;
3533 if (mode == TAmode)
3534 return unsignedp ? sat_ta_type_node : ta_type_node;
3535
3536 if (mode == UHAmode)
3537 return unsignedp ? sat_uha_type_node : uha_type_node;
3538 if (mode == USAmode)
3539 return unsignedp ? sat_usa_type_node : usa_type_node;
3540 if (mode == UDAmode)
3541 return unsignedp ? sat_uda_type_node : uda_type_node;
3542 if (mode == UTAmode)
3543 return unsignedp ? sat_uta_type_node : uta_type_node;
3544 }
3545
3546 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3547 if (TYPE_MODE (TREE_VALUE (t)) == mode
3548 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3549 return TREE_VALUE (t);
3550
3551 return 0;
3552 }
3553
3554 tree
3555 c_common_unsigned_type (tree type)
3556 {
3557 return c_common_signed_or_unsigned_type (1, type);
3558 }
3559
3560 /* Return a signed type the same as TYPE in other respects. */
3561
3562 tree
3563 c_common_signed_type (tree type)
3564 {
3565 return c_common_signed_or_unsigned_type (0, type);
3566 }
3567
3568 /* Return a type the same as TYPE except unsigned or
3569 signed according to UNSIGNEDP. */
3570
3571 tree
3572 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3573 {
3574 tree type1;
3575 int i;
3576
3577 /* This block of code emulates the behavior of the old
3578 c_common_unsigned_type. In particular, it returns
3579 long_unsigned_type_node if passed a long, even when a int would
3580 have the same size. This is necessary for warnings to work
3581 correctly in archs where sizeof(int) == sizeof(long) */
3582
3583 type1 = TYPE_MAIN_VARIANT (type);
3584 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3585 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3586 if (type1 == integer_type_node || type1 == unsigned_type_node)
3587 return unsignedp ? unsigned_type_node : integer_type_node;
3588 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3589 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3590 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3591 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3592 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3593 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3594
3595 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3596 if (int_n_enabled_p[i]
3597 && (type1 == int_n_trees[i].unsigned_type
3598 || type1 == int_n_trees[i].signed_type))
3599 return (unsignedp ? int_n_trees[i].unsigned_type
3600 : int_n_trees[i].signed_type);
3601
3602 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3603 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3604 #if HOST_BITS_PER_WIDE_INT >= 64
3605 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3606 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3607 #endif
3608 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3609 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3610 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3611 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3612 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3613 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3614 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3615 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3616
3617 #define C_COMMON_FIXED_TYPES(NAME) \
3618 if (type1 == short_ ## NAME ## _type_node \
3619 || type1 == unsigned_short_ ## NAME ## _type_node) \
3620 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3621 : short_ ## NAME ## _type_node; \
3622 if (type1 == NAME ## _type_node \
3623 || type1 == unsigned_ ## NAME ## _type_node) \
3624 return unsignedp ? unsigned_ ## NAME ## _type_node \
3625 : NAME ## _type_node; \
3626 if (type1 == long_ ## NAME ## _type_node \
3627 || type1 == unsigned_long_ ## NAME ## _type_node) \
3628 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3629 : long_ ## NAME ## _type_node; \
3630 if (type1 == long_long_ ## NAME ## _type_node \
3631 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3632 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3633 : long_long_ ## NAME ## _type_node;
3634
3635 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3636 if (type1 == NAME ## _type_node \
3637 || type1 == u ## NAME ## _type_node) \
3638 return unsignedp ? u ## NAME ## _type_node \
3639 : NAME ## _type_node;
3640
3641 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3642 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3643 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3644 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3645 : sat_ ## short_ ## NAME ## _type_node; \
3646 if (type1 == sat_ ## NAME ## _type_node \
3647 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3648 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3649 : sat_ ## NAME ## _type_node; \
3650 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3651 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3652 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3653 : sat_ ## long_ ## NAME ## _type_node; \
3654 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3655 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3656 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3657 : sat_ ## long_long_ ## NAME ## _type_node;
3658
3659 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3660 if (type1 == sat_ ## NAME ## _type_node \
3661 || type1 == sat_ ## u ## NAME ## _type_node) \
3662 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3663 : sat_ ## NAME ## _type_node;
3664
3665 C_COMMON_FIXED_TYPES (fract);
3666 C_COMMON_FIXED_TYPES_SAT (fract);
3667 C_COMMON_FIXED_TYPES (accum);
3668 C_COMMON_FIXED_TYPES_SAT (accum);
3669
3670 C_COMMON_FIXED_MODE_TYPES (qq);
3671 C_COMMON_FIXED_MODE_TYPES (hq);
3672 C_COMMON_FIXED_MODE_TYPES (sq);
3673 C_COMMON_FIXED_MODE_TYPES (dq);
3674 C_COMMON_FIXED_MODE_TYPES (tq);
3675 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3676 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3677 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3678 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3679 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3680 C_COMMON_FIXED_MODE_TYPES (ha);
3681 C_COMMON_FIXED_MODE_TYPES (sa);
3682 C_COMMON_FIXED_MODE_TYPES (da);
3683 C_COMMON_FIXED_MODE_TYPES (ta);
3684 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3685 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3686 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3687 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3688
3689 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3690 the precision; they have precision set to match their range, but
3691 may use a wider mode to match an ABI. If we change modes, we may
3692 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3693 the precision as well, so as to yield correct results for
3694 bit-field types. C++ does not have these separate bit-field
3695 types, and producing a signed or unsigned variant of an
3696 ENUMERAL_TYPE may cause other problems as well. */
3697
3698 if (!INTEGRAL_TYPE_P (type)
3699 || TYPE_UNSIGNED (type) == unsignedp)
3700 return type;
3701
3702 #define TYPE_OK(node) \
3703 (TYPE_MODE (type) == TYPE_MODE (node) \
3704 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3705 if (TYPE_OK (signed_char_type_node))
3706 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3707 if (TYPE_OK (integer_type_node))
3708 return unsignedp ? unsigned_type_node : integer_type_node;
3709 if (TYPE_OK (short_integer_type_node))
3710 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3711 if (TYPE_OK (long_integer_type_node))
3712 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3713 if (TYPE_OK (long_long_integer_type_node))
3714 return (unsignedp ? long_long_unsigned_type_node
3715 : long_long_integer_type_node);
3716
3717 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3718 if (int_n_enabled_p[i]
3719 && TYPE_MODE (type) == int_n_data[i].m
3720 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3721 return (unsignedp ? int_n_trees[i].unsigned_type
3722 : int_n_trees[i].signed_type);
3723
3724 if (TYPE_OK (widest_integer_literal_type_node))
3725 return (unsignedp ? widest_unsigned_literal_type_node
3726 : widest_integer_literal_type_node);
3727
3728 #if HOST_BITS_PER_WIDE_INT >= 64
3729 if (TYPE_OK (intTI_type_node))
3730 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3731 #endif
3732 if (TYPE_OK (intDI_type_node))
3733 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3734 if (TYPE_OK (intSI_type_node))
3735 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3736 if (TYPE_OK (intHI_type_node))
3737 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3738 if (TYPE_OK (intQI_type_node))
3739 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3740 #undef TYPE_OK
3741
3742 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3743 }
3744
3745 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3746
3747 tree
3748 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3749 {
3750 int i;
3751
3752 /* Extended integer types of the same width as a standard type have
3753 lesser rank, so those of the same width as int promote to int or
3754 unsigned int and are valid for printf formats expecting int or
3755 unsigned int. To avoid such special cases, avoid creating
3756 extended integer types for bit-fields if a standard integer type
3757 is available. */
3758 if (width == TYPE_PRECISION (integer_type_node))
3759 return unsignedp ? unsigned_type_node : integer_type_node;
3760 if (width == TYPE_PRECISION (signed_char_type_node))
3761 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3762 if (width == TYPE_PRECISION (short_integer_type_node))
3763 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3764 if (width == TYPE_PRECISION (long_integer_type_node))
3765 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3766 if (width == TYPE_PRECISION (long_long_integer_type_node))
3767 return (unsignedp ? long_long_unsigned_type_node
3768 : long_long_integer_type_node);
3769 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3770 if (int_n_enabled_p[i]
3771 && width == int_n_data[i].bitsize)
3772 return (unsignedp ? int_n_trees[i].unsigned_type
3773 : int_n_trees[i].signed_type);
3774 return build_nonstandard_integer_type (width, unsignedp);
3775 }
3776
3777 /* The C version of the register_builtin_type langhook. */
3778
3779 void
3780 c_register_builtin_type (tree type, const char* name)
3781 {
3782 tree decl;
3783
3784 decl = build_decl (UNKNOWN_LOCATION,
3785 TYPE_DECL, get_identifier (name), type);
3786 DECL_ARTIFICIAL (decl) = 1;
3787 if (!TYPE_NAME (type))
3788 TYPE_NAME (type) = decl;
3789 pushdecl (decl);
3790
3791 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3792 }
3793 \f
3794 /* Print an error message for invalid operands to arith operation
3795 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3796 LOCATION is the location of the message. */
3797
3798 void
3799 binary_op_error (location_t location, enum tree_code code,
3800 tree type0, tree type1)
3801 {
3802 const char *opname;
3803
3804 switch (code)
3805 {
3806 case PLUS_EXPR:
3807 opname = "+"; break;
3808 case MINUS_EXPR:
3809 opname = "-"; break;
3810 case MULT_EXPR:
3811 opname = "*"; break;
3812 case MAX_EXPR:
3813 opname = "max"; break;
3814 case MIN_EXPR:
3815 opname = "min"; break;
3816 case EQ_EXPR:
3817 opname = "=="; break;
3818 case NE_EXPR:
3819 opname = "!="; break;
3820 case LE_EXPR:
3821 opname = "<="; break;
3822 case GE_EXPR:
3823 opname = ">="; break;
3824 case LT_EXPR:
3825 opname = "<"; break;
3826 case GT_EXPR:
3827 opname = ">"; break;
3828 case LSHIFT_EXPR:
3829 opname = "<<"; break;
3830 case RSHIFT_EXPR:
3831 opname = ">>"; break;
3832 case TRUNC_MOD_EXPR:
3833 case FLOOR_MOD_EXPR:
3834 opname = "%"; break;
3835 case TRUNC_DIV_EXPR:
3836 case FLOOR_DIV_EXPR:
3837 opname = "/"; break;
3838 case BIT_AND_EXPR:
3839 opname = "&"; break;
3840 case BIT_IOR_EXPR:
3841 opname = "|"; break;
3842 case TRUTH_ANDIF_EXPR:
3843 opname = "&&"; break;
3844 case TRUTH_ORIF_EXPR:
3845 opname = "||"; break;
3846 case BIT_XOR_EXPR:
3847 opname = "^"; break;
3848 default:
3849 gcc_unreachable ();
3850 }
3851 error_at (location,
3852 "invalid operands to binary %s (have %qT and %qT)", opname,
3853 type0, type1);
3854 }
3855 \f
3856 /* Given an expression as a tree, return its original type. Do this
3857 by stripping any conversion that preserves the sign and precision. */
3858 static tree
3859 expr_original_type (tree expr)
3860 {
3861 STRIP_SIGN_NOPS (expr);
3862 return TREE_TYPE (expr);
3863 }
3864
3865 /* Subroutine of build_binary_op, used for comparison operations.
3866 See if the operands have both been converted from subword integer types
3867 and, if so, perhaps change them both back to their original type.
3868 This function is also responsible for converting the two operands
3869 to the proper common type for comparison.
3870
3871 The arguments of this function are all pointers to local variables
3872 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3873 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3874
3875 LOC is the location of the comparison.
3876
3877 If this function returns nonzero, it means that the comparison has
3878 a constant value. What this function returns is an expression for
3879 that value. */
3880
3881 tree
3882 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3883 tree *restype_ptr, enum tree_code *rescode_ptr)
3884 {
3885 tree type;
3886 tree op0 = *op0_ptr;
3887 tree op1 = *op1_ptr;
3888 int unsignedp0, unsignedp1;
3889 int real1, real2;
3890 tree primop0, primop1;
3891 enum tree_code code = *rescode_ptr;
3892
3893 /* Throw away any conversions to wider types
3894 already present in the operands. */
3895
3896 primop0 = c_common_get_narrower (op0, &unsignedp0);
3897 primop1 = c_common_get_narrower (op1, &unsignedp1);
3898
3899 /* If primopN is first sign-extended from primopN's precision to opN's
3900 precision, then zero-extended from opN's precision to
3901 *restype_ptr precision, shortenings might be invalid. */
3902 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3903 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3904 && !unsignedp0
3905 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3906 primop0 = op0;
3907 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3908 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3909 && !unsignedp1
3910 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3911 primop1 = op1;
3912
3913 /* Handle the case that OP0 does not *contain* a conversion
3914 but it *requires* conversion to FINAL_TYPE. */
3915
3916 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3917 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3918 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3919 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3920
3921 /* If one of the operands must be floated, we cannot optimize. */
3922 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3923 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3924
3925 /* If first arg is constant, swap the args (changing operation
3926 so value is preserved), for canonicalization. Don't do this if
3927 the second arg is 0. */
3928
3929 if (TREE_CONSTANT (primop0)
3930 && !integer_zerop (primop1) && !real_zerop (primop1)
3931 && !fixed_zerop (primop1))
3932 {
3933 std::swap (primop0, primop1);
3934 std::swap (op0, op1);
3935 *op0_ptr = op0;
3936 *op1_ptr = op1;
3937 std::swap (unsignedp0, unsignedp1);
3938 std::swap (real1, real2);
3939
3940 switch (code)
3941 {
3942 case LT_EXPR:
3943 code = GT_EXPR;
3944 break;
3945 case GT_EXPR:
3946 code = LT_EXPR;
3947 break;
3948 case LE_EXPR:
3949 code = GE_EXPR;
3950 break;
3951 case GE_EXPR:
3952 code = LE_EXPR;
3953 break;
3954 default:
3955 break;
3956 }
3957 *rescode_ptr = code;
3958 }
3959
3960 /* If comparing an integer against a constant more bits wide,
3961 maybe we can deduce a value of 1 or 0 independent of the data.
3962 Or else truncate the constant now
3963 rather than extend the variable at run time.
3964
3965 This is only interesting if the constant is the wider arg.
3966 Also, it is not safe if the constant is unsigned and the
3967 variable arg is signed, since in this case the variable
3968 would be sign-extended and then regarded as unsigned.
3969 Our technique fails in this case because the lowest/highest
3970 possible unsigned results don't follow naturally from the
3971 lowest/highest possible values of the variable operand.
3972 For just EQ_EXPR and NE_EXPR there is another technique that
3973 could be used: see if the constant can be faithfully represented
3974 in the other operand's type, by truncating it and reextending it
3975 and see if that preserves the constant's value. */
3976
3977 if (!real1 && !real2
3978 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3979 && TREE_CODE (primop1) == INTEGER_CST
3980 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3981 {
3982 int min_gt, max_gt, min_lt, max_lt;
3983 tree maxval, minval;
3984 /* 1 if comparison is nominally unsigned. */
3985 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3986 tree val;
3987
3988 type = c_common_signed_or_unsigned_type (unsignedp0,
3989 TREE_TYPE (primop0));
3990
3991 maxval = TYPE_MAX_VALUE (type);
3992 minval = TYPE_MIN_VALUE (type);
3993
3994 if (unsignedp && !unsignedp0)
3995 *restype_ptr = c_common_signed_type (*restype_ptr);
3996
3997 if (TREE_TYPE (primop1) != *restype_ptr)
3998 {
3999 /* Convert primop1 to target type, but do not introduce
4000 additional overflow. We know primop1 is an int_cst. */
4001 primop1 = force_fit_type (*restype_ptr,
4002 wide_int::from
4003 (primop1,
4004 TYPE_PRECISION (*restype_ptr),
4005 TYPE_SIGN (TREE_TYPE (primop1))),
4006 0, TREE_OVERFLOW (primop1));
4007 }
4008 if (type != *restype_ptr)
4009 {
4010 minval = convert (*restype_ptr, minval);
4011 maxval = convert (*restype_ptr, maxval);
4012 }
4013
4014 min_gt = tree_int_cst_lt (primop1, minval);
4015 max_gt = tree_int_cst_lt (primop1, maxval);
4016 min_lt = tree_int_cst_lt (minval, primop1);
4017 max_lt = tree_int_cst_lt (maxval, primop1);
4018
4019 val = 0;
4020 /* This used to be a switch, but Genix compiler can't handle that. */
4021 if (code == NE_EXPR)
4022 {
4023 if (max_lt || min_gt)
4024 val = truthvalue_true_node;
4025 }
4026 else if (code == EQ_EXPR)
4027 {
4028 if (max_lt || min_gt)
4029 val = truthvalue_false_node;
4030 }
4031 else if (code == LT_EXPR)
4032 {
4033 if (max_lt)
4034 val = truthvalue_true_node;
4035 if (!min_lt)
4036 val = truthvalue_false_node;
4037 }
4038 else if (code == GT_EXPR)
4039 {
4040 if (min_gt)
4041 val = truthvalue_true_node;
4042 if (!max_gt)
4043 val = truthvalue_false_node;
4044 }
4045 else if (code == LE_EXPR)
4046 {
4047 if (!max_gt)
4048 val = truthvalue_true_node;
4049 if (min_gt)
4050 val = truthvalue_false_node;
4051 }
4052 else if (code == GE_EXPR)
4053 {
4054 if (!min_lt)
4055 val = truthvalue_true_node;
4056 if (max_lt)
4057 val = truthvalue_false_node;
4058 }
4059
4060 /* If primop0 was sign-extended and unsigned comparison specd,
4061 we did a signed comparison above using the signed type bounds.
4062 But the comparison we output must be unsigned.
4063
4064 Also, for inequalities, VAL is no good; but if the signed
4065 comparison had *any* fixed result, it follows that the
4066 unsigned comparison just tests the sign in reverse
4067 (positive values are LE, negative ones GE).
4068 So we can generate an unsigned comparison
4069 against an extreme value of the signed type. */
4070
4071 if (unsignedp && !unsignedp0)
4072 {
4073 if (val != 0)
4074 switch (code)
4075 {
4076 case LT_EXPR:
4077 case GE_EXPR:
4078 primop1 = TYPE_MIN_VALUE (type);
4079 val = 0;
4080 break;
4081
4082 case LE_EXPR:
4083 case GT_EXPR:
4084 primop1 = TYPE_MAX_VALUE (type);
4085 val = 0;
4086 break;
4087
4088 default:
4089 break;
4090 }
4091 type = c_common_unsigned_type (type);
4092 }
4093
4094 if (TREE_CODE (primop0) != INTEGER_CST
4095 /* Don't warn if it's from a (non-system) macro. */
4096 && !(from_macro_expansion_at
4097 (expansion_point_location_if_in_system_header
4098 (EXPR_LOCATION (primop0)))))
4099 {
4100 if (val == truthvalue_false_node)
4101 warning_at (loc, OPT_Wtype_limits,
4102 "comparison is always false due to limited range of data type");
4103 if (val == truthvalue_true_node)
4104 warning_at (loc, OPT_Wtype_limits,
4105 "comparison is always true due to limited range of data type");
4106 }
4107
4108 if (val != 0)
4109 {
4110 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4111 if (TREE_SIDE_EFFECTS (primop0))
4112 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4113 return val;
4114 }
4115
4116 /* Value is not predetermined, but do the comparison
4117 in the type of the operand that is not constant.
4118 TYPE is already properly set. */
4119 }
4120
4121 /* If either arg is decimal float and the other is float, find the
4122 proper common type to use for comparison. */
4123 else if (real1 && real2
4124 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4125 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4126 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4127
4128 /* If either arg is decimal float and the other is float, fail. */
4129 else if (real1 && real2
4130 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4131 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4132 return 0;
4133
4134 else if (real1 && real2
4135 && (TYPE_PRECISION (TREE_TYPE (primop0))
4136 == TYPE_PRECISION (TREE_TYPE (primop1))))
4137 type = TREE_TYPE (primop0);
4138
4139 /* If args' natural types are both narrower than nominal type
4140 and both extend in the same manner, compare them
4141 in the type of the wider arg.
4142 Otherwise must actually extend both to the nominal
4143 common type lest different ways of extending
4144 alter the result.
4145 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4146
4147 else if (unsignedp0 == unsignedp1 && real1 == real2
4148 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4149 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4150 {
4151 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4152 type = c_common_signed_or_unsigned_type (unsignedp0
4153 || TYPE_UNSIGNED (*restype_ptr),
4154 type);
4155 /* Make sure shorter operand is extended the right way
4156 to match the longer operand. */
4157 primop0
4158 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4159 TREE_TYPE (primop0)),
4160 primop0);
4161 primop1
4162 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4163 TREE_TYPE (primop1)),
4164 primop1);
4165 }
4166 else
4167 {
4168 /* Here we must do the comparison on the nominal type
4169 using the args exactly as we received them. */
4170 type = *restype_ptr;
4171 primop0 = op0;
4172 primop1 = op1;
4173
4174 if (!real1 && !real2 && integer_zerop (primop1)
4175 && TYPE_UNSIGNED (*restype_ptr))
4176 {
4177 tree value = 0;
4178 /* All unsigned values are >= 0, so we warn. However,
4179 if OP0 is a constant that is >= 0, the signedness of
4180 the comparison isn't an issue, so suppress the
4181 warning. */
4182 bool warn =
4183 warn_type_limits && !in_system_header_at (loc)
4184 && !(TREE_CODE (primop0) == INTEGER_CST
4185 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4186 primop0)))
4187 /* Do not warn for enumeration types. */
4188 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4189
4190 switch (code)
4191 {
4192 case GE_EXPR:
4193 if (warn)
4194 warning_at (loc, OPT_Wtype_limits,
4195 "comparison of unsigned expression >= 0 is always true");
4196 value = truthvalue_true_node;
4197 break;
4198
4199 case LT_EXPR:
4200 if (warn)
4201 warning_at (loc, OPT_Wtype_limits,
4202 "comparison of unsigned expression < 0 is always false");
4203 value = truthvalue_false_node;
4204 break;
4205
4206 default:
4207 break;
4208 }
4209
4210 if (value != 0)
4211 {
4212 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4213 if (TREE_SIDE_EFFECTS (primop0))
4214 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4215 primop0, value);
4216 return value;
4217 }
4218 }
4219 }
4220
4221 *op0_ptr = convert (type, primop0);
4222 *op1_ptr = convert (type, primop1);
4223
4224 *restype_ptr = truthvalue_type_node;
4225
4226 return 0;
4227 }
4228 \f
4229 /* Return a tree for the sum or difference (RESULTCODE says which)
4230 of pointer PTROP and integer INTOP. */
4231
4232 tree
4233 pointer_int_sum (location_t loc, enum tree_code resultcode,
4234 tree ptrop, tree intop, bool complain)
4235 {
4236 tree size_exp, ret;
4237
4238 /* The result is a pointer of the same type that is being added. */
4239 tree result_type = TREE_TYPE (ptrop);
4240
4241 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4242 {
4243 if (complain && warn_pointer_arith)
4244 pedwarn (loc, OPT_Wpointer_arith,
4245 "pointer of type %<void *%> used in arithmetic");
4246 else if (!complain)
4247 return error_mark_node;
4248 size_exp = integer_one_node;
4249 }
4250 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4251 {
4252 if (complain && warn_pointer_arith)
4253 pedwarn (loc, OPT_Wpointer_arith,
4254 "pointer to a function used in arithmetic");
4255 else if (!complain)
4256 return error_mark_node;
4257 size_exp = integer_one_node;
4258 }
4259 else
4260 size_exp = size_in_bytes (TREE_TYPE (result_type));
4261
4262 /* We are manipulating pointer values, so we don't need to warn
4263 about relying on undefined signed overflow. We disable the
4264 warning here because we use integer types so fold won't know that
4265 they are really pointers. */
4266 fold_defer_overflow_warnings ();
4267
4268 /* If what we are about to multiply by the size of the elements
4269 contains a constant term, apply distributive law
4270 and multiply that constant term separately.
4271 This helps produce common subexpressions. */
4272 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4273 && !TREE_CONSTANT (intop)
4274 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4275 && TREE_CONSTANT (size_exp)
4276 /* If the constant comes from pointer subtraction,
4277 skip this optimization--it would cause an error. */
4278 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4279 /* If the constant is unsigned, and smaller than the pointer size,
4280 then we must skip this optimization. This is because it could cause
4281 an overflow error if the constant is negative but INTOP is not. */
4282 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4283 || (TYPE_PRECISION (TREE_TYPE (intop))
4284 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4285 {
4286 enum tree_code subcode = resultcode;
4287 tree int_type = TREE_TYPE (intop);
4288 if (TREE_CODE (intop) == MINUS_EXPR)
4289 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4290 /* Convert both subexpression types to the type of intop,
4291 because weird cases involving pointer arithmetic
4292 can result in a sum or difference with different type args. */
4293 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4294 subcode, ptrop,
4295 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4296 intop = convert (int_type, TREE_OPERAND (intop, 0));
4297 }
4298
4299 /* Convert the integer argument to a type the same size as sizetype
4300 so the multiply won't overflow spuriously. */
4301 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4302 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4303 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4304 TYPE_UNSIGNED (sizetype)), intop);
4305
4306 /* Replace the integer argument with a suitable product by the object size.
4307 Do this multiplication as signed, then convert to the appropriate type
4308 for the pointer operation and disregard an overflow that occurred only
4309 because of the sign-extension change in the latter conversion. */
4310 {
4311 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4312 convert (TREE_TYPE (intop), size_exp));
4313 intop = convert (sizetype, t);
4314 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4315 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4316 }
4317
4318 /* Create the sum or difference. */
4319 if (resultcode == MINUS_EXPR)
4320 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4321
4322 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4323
4324 fold_undefer_and_ignore_overflow_warnings ();
4325
4326 return ret;
4327 }
4328 \f
4329 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4330 and if NON_CONST is known not to be permitted in an evaluated part
4331 of a constant expression. */
4332
4333 tree
4334 c_wrap_maybe_const (tree expr, bool non_const)
4335 {
4336 bool nowarning = TREE_NO_WARNING (expr);
4337 location_t loc = EXPR_LOCATION (expr);
4338
4339 /* This should never be called for C++. */
4340 if (c_dialect_cxx ())
4341 gcc_unreachable ();
4342
4343 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4344 STRIP_TYPE_NOPS (expr);
4345 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4346 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4347 if (nowarning)
4348 TREE_NO_WARNING (expr) = 1;
4349 protected_set_expr_location (expr, loc);
4350
4351 return expr;
4352 }
4353
4354 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4355 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4356 around the SAVE_EXPR if needed so that c_fully_fold does not need
4357 to look inside SAVE_EXPRs. */
4358
4359 tree
4360 c_save_expr (tree expr)
4361 {
4362 bool maybe_const = true;
4363 if (c_dialect_cxx ())
4364 return save_expr (expr);
4365 expr = c_fully_fold (expr, false, &maybe_const);
4366 expr = save_expr (expr);
4367 if (!maybe_const)
4368 expr = c_wrap_maybe_const (expr, true);
4369 return expr;
4370 }
4371
4372 /* Return whether EXPR is a declaration whose address can never be
4373 NULL. */
4374
4375 bool
4376 decl_with_nonnull_addr_p (const_tree expr)
4377 {
4378 return (DECL_P (expr)
4379 && (TREE_CODE (expr) == PARM_DECL
4380 || TREE_CODE (expr) == LABEL_DECL
4381 || !DECL_WEAK (expr)));
4382 }
4383
4384 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4385 or for an `if' or `while' statement or ?..: exp. It should already
4386 have been validated to be of suitable type; otherwise, a bad
4387 diagnostic may result.
4388
4389 The EXPR is located at LOCATION.
4390
4391 This preparation consists of taking the ordinary
4392 representation of an expression expr and producing a valid tree
4393 boolean expression describing whether expr is nonzero. We could
4394 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4395 but we optimize comparisons, &&, ||, and !.
4396
4397 The resulting type should always be `truthvalue_type_node'. */
4398
4399 tree
4400 c_common_truthvalue_conversion (location_t location, tree expr)
4401 {
4402 switch (TREE_CODE (expr))
4403 {
4404 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4405 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4406 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4407 case ORDERED_EXPR: case UNORDERED_EXPR:
4408 if (TREE_TYPE (expr) == truthvalue_type_node)
4409 return expr;
4410 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4411 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4412 goto ret;
4413
4414 case TRUTH_ANDIF_EXPR:
4415 case TRUTH_ORIF_EXPR:
4416 case TRUTH_AND_EXPR:
4417 case TRUTH_OR_EXPR:
4418 case TRUTH_XOR_EXPR:
4419 if (TREE_TYPE (expr) == truthvalue_type_node)
4420 return expr;
4421 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4422 c_common_truthvalue_conversion (location,
4423 TREE_OPERAND (expr, 0)),
4424 c_common_truthvalue_conversion (location,
4425 TREE_OPERAND (expr, 1)));
4426 goto ret;
4427
4428 case TRUTH_NOT_EXPR:
4429 if (TREE_TYPE (expr) == truthvalue_type_node)
4430 return expr;
4431 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4432 c_common_truthvalue_conversion (location,
4433 TREE_OPERAND (expr, 0)));
4434 goto ret;
4435
4436 case ERROR_MARK:
4437 return expr;
4438
4439 case INTEGER_CST:
4440 return integer_zerop (expr) ? truthvalue_false_node
4441 : truthvalue_true_node;
4442
4443 case REAL_CST:
4444 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4445 ? truthvalue_true_node
4446 : truthvalue_false_node;
4447
4448 case FIXED_CST:
4449 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4450 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4451 ? truthvalue_true_node
4452 : truthvalue_false_node;
4453
4454 case FUNCTION_DECL:
4455 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4456 /* Fall through. */
4457
4458 case ADDR_EXPR:
4459 {
4460 tree inner = TREE_OPERAND (expr, 0);
4461 if (decl_with_nonnull_addr_p (inner))
4462 {
4463 /* Common Ada/Pascal programmer's mistake. */
4464 warning_at (location,
4465 OPT_Waddress,
4466 "the address of %qD will always evaluate as %<true%>",
4467 inner);
4468 return truthvalue_true_node;
4469 }
4470 break;
4471 }
4472
4473 case COMPLEX_EXPR:
4474 expr = build_binary_op (EXPR_LOCATION (expr),
4475 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4476 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4477 c_common_truthvalue_conversion (location,
4478 TREE_OPERAND (expr, 0)),
4479 c_common_truthvalue_conversion (location,
4480 TREE_OPERAND (expr, 1)),
4481 0);
4482 goto ret;
4483
4484 case NEGATE_EXPR:
4485 case ABS_EXPR:
4486 case FLOAT_EXPR:
4487 case EXCESS_PRECISION_EXPR:
4488 /* These don't change whether an object is nonzero or zero. */
4489 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4490
4491 case LROTATE_EXPR:
4492 case RROTATE_EXPR:
4493 /* These don't change whether an object is zero or nonzero, but
4494 we can't ignore them if their second arg has side-effects. */
4495 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4496 {
4497 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4498 TREE_OPERAND (expr, 1),
4499 c_common_truthvalue_conversion
4500 (location, TREE_OPERAND (expr, 0)));
4501 goto ret;
4502 }
4503 else
4504 return c_common_truthvalue_conversion (location,
4505 TREE_OPERAND (expr, 0));
4506
4507 case COND_EXPR:
4508 /* Distribute the conversion into the arms of a COND_EXPR. */
4509 if (c_dialect_cxx ())
4510 {
4511 tree op1 = TREE_OPERAND (expr, 1);
4512 tree op2 = TREE_OPERAND (expr, 2);
4513 /* In C++ one of the arms might have void type if it is throw. */
4514 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4515 op1 = c_common_truthvalue_conversion (location, op1);
4516 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4517 op2 = c_common_truthvalue_conversion (location, op2);
4518 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4519 TREE_OPERAND (expr, 0), op1, op2);
4520 goto ret;
4521 }
4522 else
4523 {
4524 /* Folding will happen later for C. */
4525 expr = build3 (COND_EXPR, truthvalue_type_node,
4526 TREE_OPERAND (expr, 0),
4527 c_common_truthvalue_conversion (location,
4528 TREE_OPERAND (expr, 1)),
4529 c_common_truthvalue_conversion (location,
4530 TREE_OPERAND (expr, 2)));
4531 goto ret;
4532 }
4533
4534 CASE_CONVERT:
4535 {
4536 tree totype = TREE_TYPE (expr);
4537 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4538
4539 if (POINTER_TYPE_P (totype)
4540 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4541 {
4542 tree inner = expr;
4543 STRIP_NOPS (inner);
4544
4545 if (DECL_P (inner))
4546 warning_at (location,
4547 OPT_Waddress,
4548 "the compiler can assume that the address of "
4549 "%qD will always evaluate to %<true%>",
4550 inner);
4551 }
4552
4553 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4554 since that affects how `default_conversion' will behave. */
4555 if (TREE_CODE (totype) == REFERENCE_TYPE
4556 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4557 break;
4558 /* Don't strip a conversion from C++0x scoped enum, since they
4559 don't implicitly convert to other types. */
4560 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4561 && ENUM_IS_SCOPED (fromtype))
4562 break;
4563 /* If this isn't narrowing the argument, we can ignore it. */
4564 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4565 return c_common_truthvalue_conversion (location,
4566 TREE_OPERAND (expr, 0));
4567 }
4568 break;
4569
4570 case MODIFY_EXPR:
4571 if (!TREE_NO_WARNING (expr)
4572 && warn_parentheses)
4573 {
4574 warning (OPT_Wparentheses,
4575 "suggest parentheses around assignment used as truth value");
4576 TREE_NO_WARNING (expr) = 1;
4577 }
4578 break;
4579
4580 default:
4581 break;
4582 }
4583
4584 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4585 {
4586 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4587 expr = (build_binary_op
4588 (EXPR_LOCATION (expr),
4589 (TREE_SIDE_EFFECTS (expr)
4590 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4591 c_common_truthvalue_conversion
4592 (location,
4593 build_unary_op (location, REALPART_EXPR, t, 0)),
4594 c_common_truthvalue_conversion
4595 (location,
4596 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4597 0));
4598 goto ret;
4599 }
4600
4601 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4602 {
4603 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4604 FCONST0 (TYPE_MODE
4605 (TREE_TYPE (expr))));
4606 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4607 }
4608 else
4609 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4610
4611 ret:
4612 protected_set_expr_location (expr, location);
4613 return expr;
4614 }
4615 \f
4616 static void def_builtin_1 (enum built_in_function fncode,
4617 const char *name,
4618 enum built_in_class fnclass,
4619 tree fntype, tree libtype,
4620 bool both_p, bool fallback_p, bool nonansi_p,
4621 tree fnattrs, bool implicit_p);
4622
4623
4624 /* Apply the TYPE_QUALS to the new DECL. */
4625
4626 void
4627 c_apply_type_quals_to_decl (int type_quals, tree decl)
4628 {
4629 tree type = TREE_TYPE (decl);
4630
4631 if (type == error_mark_node)
4632 return;
4633
4634 if ((type_quals & TYPE_QUAL_CONST)
4635 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4636 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4637 constructor can produce constant init, so rely on cp_finish_decl to
4638 clear TREE_READONLY if the variable has non-constant init. */
4639 TREE_READONLY (decl) = 1;
4640 if (type_quals & TYPE_QUAL_VOLATILE)
4641 {
4642 TREE_SIDE_EFFECTS (decl) = 1;
4643 TREE_THIS_VOLATILE (decl) = 1;
4644 }
4645 if (type_quals & TYPE_QUAL_RESTRICT)
4646 {
4647 while (type && TREE_CODE (type) == ARRAY_TYPE)
4648 /* Allow 'restrict' on arrays of pointers.
4649 FIXME currently we just ignore it. */
4650 type = TREE_TYPE (type);
4651 if (!type
4652 || !POINTER_TYPE_P (type)
4653 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4654 error ("invalid use of %<restrict%>");
4655 }
4656 }
4657
4658 struct c_type_hasher : ggc_ptr_hash<tree_node>
4659 {
4660 static hashval_t hash (tree);
4661 static bool equal (tree, tree);
4662 };
4663
4664 /* Hash function for the problem of multiple type definitions in
4665 different files. This must hash all types that will compare
4666 equal via comptypes to the same value. In practice it hashes
4667 on some of the simple stuff and leaves the details to comptypes. */
4668
4669 hashval_t
4670 c_type_hasher::hash (tree t)
4671 {
4672 int n_elements;
4673 int shift, size;
4674 tree t2;
4675 switch (TREE_CODE (t))
4676 {
4677 /* For pointers, hash on pointee type plus some swizzling. */
4678 case POINTER_TYPE:
4679 return hash (TREE_TYPE (t)) ^ 0x3003003;
4680 /* Hash on number of elements and total size. */
4681 case ENUMERAL_TYPE:
4682 shift = 3;
4683 t2 = TYPE_VALUES (t);
4684 break;
4685 case RECORD_TYPE:
4686 shift = 0;
4687 t2 = TYPE_FIELDS (t);
4688 break;
4689 case QUAL_UNION_TYPE:
4690 shift = 1;
4691 t2 = TYPE_FIELDS (t);
4692 break;
4693 case UNION_TYPE:
4694 shift = 2;
4695 t2 = TYPE_FIELDS (t);
4696 break;
4697 default:
4698 gcc_unreachable ();
4699 }
4700 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4701 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4702 n_elements = list_length (t2);
4703 /* We might have a VLA here. */
4704 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4705 size = 0;
4706 else
4707 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4708 return ((size << 24) | (n_elements << shift));
4709 }
4710
4711 bool
4712 c_type_hasher::equal (tree t1, tree t2)
4713 {
4714 return lang_hooks.types_compatible_p (t1, t2);
4715 }
4716
4717 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4718
4719 /* Return the typed-based alias set for T, which may be an expression
4720 or a type. Return -1 if we don't do anything special. */
4721
4722 alias_set_type
4723 c_common_get_alias_set (tree t)
4724 {
4725 tree u;
4726
4727 /* For VLAs, use the alias set of the element type rather than the
4728 default of alias set 0 for types compared structurally. */
4729 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4730 {
4731 if (TREE_CODE (t) == ARRAY_TYPE)
4732 return get_alias_set (TREE_TYPE (t));
4733 return -1;
4734 }
4735
4736 /* Permit type-punning when accessing a union, provided the access
4737 is directly through the union. For example, this code does not
4738 permit taking the address of a union member and then storing
4739 through it. Even the type-punning allowed here is a GCC
4740 extension, albeit a common and useful one; the C standard says
4741 that such accesses have implementation-defined behavior. */
4742 for (u = t;
4743 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4744 u = TREE_OPERAND (u, 0))
4745 if (TREE_CODE (u) == COMPONENT_REF
4746 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4747 return 0;
4748
4749 /* That's all the expressions we handle specially. */
4750 if (!TYPE_P (t))
4751 return -1;
4752
4753 /* The C standard guarantees that any object may be accessed via an
4754 lvalue that has character type. */
4755 if (t == char_type_node
4756 || t == signed_char_type_node
4757 || t == unsigned_char_type_node)
4758 return 0;
4759
4760 /* The C standard specifically allows aliasing between signed and
4761 unsigned variants of the same type. We treat the signed
4762 variant as canonical. */
4763 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4764 {
4765 tree t1 = c_common_signed_type (t);
4766
4767 /* t1 == t can happen for boolean nodes which are always unsigned. */
4768 if (t1 != t)
4769 return get_alias_set (t1);
4770 }
4771
4772 /* Handle the case of multiple type nodes referring to "the same" type,
4773 which occurs with IMA. These share an alias set. FIXME: Currently only
4774 C90 is handled. (In C99 type compatibility is not transitive, which
4775 complicates things mightily. The alias set splay trees can theoretically
4776 represent this, but insertion is tricky when you consider all the
4777 different orders things might arrive in.) */
4778
4779 if (c_language != clk_c || flag_isoc99)
4780 return -1;
4781
4782 /* Save time if there's only one input file. */
4783 if (num_in_fnames == 1)
4784 return -1;
4785
4786 /* Pointers need special handling if they point to any type that
4787 needs special handling (below). */
4788 if (TREE_CODE (t) == POINTER_TYPE)
4789 {
4790 tree t2;
4791 /* Find bottom type under any nested POINTERs. */
4792 for (t2 = TREE_TYPE (t);
4793 TREE_CODE (t2) == POINTER_TYPE;
4794 t2 = TREE_TYPE (t2))
4795 ;
4796 if (!RECORD_OR_UNION_TYPE_P (t2)
4797 && TREE_CODE (t2) != ENUMERAL_TYPE)
4798 return -1;
4799 if (TYPE_SIZE (t2) == 0)
4800 return -1;
4801 }
4802 /* These are the only cases that need special handling. */
4803 if (!RECORD_OR_UNION_TYPE_P (t)
4804 && TREE_CODE (t) != ENUMERAL_TYPE
4805 && TREE_CODE (t) != POINTER_TYPE)
4806 return -1;
4807 /* Undefined? */
4808 if (TYPE_SIZE (t) == 0)
4809 return -1;
4810
4811 /* Look up t in hash table. Only one of the compatible types within each
4812 alias set is recorded in the table. */
4813 if (!type_hash_table)
4814 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4815 tree *slot = type_hash_table->find_slot (t, INSERT);
4816 if (*slot != NULL)
4817 {
4818 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4819 return TYPE_ALIAS_SET ((tree)*slot);
4820 }
4821 else
4822 /* Our caller will assign and record (in t) a new alias set; all we need
4823 to do is remember t in the hash table. */
4824 *slot = t;
4825
4826 return -1;
4827 }
4828 \f
4829 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4830 the IS_SIZEOF parameter indicates which operator is being applied.
4831 The COMPLAIN flag controls whether we should diagnose possibly
4832 ill-formed constructs or not. LOC is the location of the SIZEOF or
4833 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4834 a type in any context should be returned, rather than the normal
4835 alignment for that type. */
4836
4837 tree
4838 c_sizeof_or_alignof_type (location_t loc,
4839 tree type, bool is_sizeof, bool min_alignof,
4840 int complain)
4841 {
4842 const char *op_name;
4843 tree value = NULL;
4844 enum tree_code type_code = TREE_CODE (type);
4845
4846 op_name = is_sizeof ? "sizeof" : "__alignof__";
4847
4848 if (type_code == FUNCTION_TYPE)
4849 {
4850 if (is_sizeof)
4851 {
4852 if (complain && warn_pointer_arith)
4853 pedwarn (loc, OPT_Wpointer_arith,
4854 "invalid application of %<sizeof%> to a function type");
4855 else if (!complain)
4856 return error_mark_node;
4857 value = size_one_node;
4858 }
4859 else
4860 {
4861 if (complain)
4862 {
4863 if (c_dialect_cxx ())
4864 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4865 "%<alignof%> applied to a function type");
4866 else
4867 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4868 "%<_Alignof%> applied to a function type");
4869 }
4870 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4871 }
4872 }
4873 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4874 {
4875 if (type_code == VOID_TYPE
4876 && complain && warn_pointer_arith)
4877 pedwarn (loc, OPT_Wpointer_arith,
4878 "invalid application of %qs to a void type", op_name);
4879 else if (!complain)
4880 return error_mark_node;
4881 value = size_one_node;
4882 }
4883 else if (!COMPLETE_TYPE_P (type)
4884 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4885 {
4886 if (complain)
4887 error_at (loc, "invalid application of %qs to incomplete type %qT",
4888 op_name, type);
4889 return error_mark_node;
4890 }
4891 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4892 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4893 {
4894 if (complain)
4895 error_at (loc, "invalid application of %qs to array type %qT of "
4896 "incomplete element type", op_name, type);
4897 return error_mark_node;
4898 }
4899 else
4900 {
4901 if (is_sizeof)
4902 /* Convert in case a char is more than one unit. */
4903 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4904 size_int (TYPE_PRECISION (char_type_node)
4905 / BITS_PER_UNIT));
4906 else if (min_alignof)
4907 value = size_int (min_align_of_type (type));
4908 else
4909 value = size_int (TYPE_ALIGN_UNIT (type));
4910 }
4911
4912 /* VALUE will have the middle-end integer type sizetype.
4913 However, we should really return a value of type `size_t',
4914 which is just a typedef for an ordinary integer type. */
4915 value = fold_convert_loc (loc, size_type_node, value);
4916
4917 return value;
4918 }
4919
4920 /* Implement the __alignof keyword: Return the minimum required
4921 alignment of EXPR, measured in bytes. For VAR_DECLs,
4922 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4923 from an "aligned" __attribute__ specification). LOC is the
4924 location of the ALIGNOF operator. */
4925
4926 tree
4927 c_alignof_expr (location_t loc, tree expr)
4928 {
4929 tree t;
4930
4931 if (VAR_OR_FUNCTION_DECL_P (expr))
4932 t = size_int (DECL_ALIGN_UNIT (expr));
4933
4934 else if (TREE_CODE (expr) == COMPONENT_REF
4935 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4936 {
4937 error_at (loc, "%<__alignof%> applied to a bit-field");
4938 t = size_one_node;
4939 }
4940 else if (TREE_CODE (expr) == COMPONENT_REF
4941 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4942 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4943
4944 else if (INDIRECT_REF_P (expr))
4945 {
4946 tree t = TREE_OPERAND (expr, 0);
4947 tree best = t;
4948 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4949
4950 while (CONVERT_EXPR_P (t)
4951 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4952 {
4953 int thisalign;
4954
4955 t = TREE_OPERAND (t, 0);
4956 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4957 if (thisalign > bestalign)
4958 best = t, bestalign = thisalign;
4959 }
4960 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4961 }
4962 else
4963 return c_alignof (loc, TREE_TYPE (expr));
4964
4965 return fold_convert_loc (loc, size_type_node, t);
4966 }
4967 \f
4968 /* Handle C and C++ default attributes. */
4969
4970 enum built_in_attribute
4971 {
4972 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4973 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4974 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
4975 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4976 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4977 #include "builtin-attrs.def"
4978 #undef DEF_ATTR_NULL_TREE
4979 #undef DEF_ATTR_INT
4980 #undef DEF_ATTR_STRING
4981 #undef DEF_ATTR_IDENT
4982 #undef DEF_ATTR_TREE_LIST
4983 ATTR_LAST
4984 };
4985
4986 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4987
4988 static void c_init_attributes (void);
4989
4990 enum c_builtin_type
4991 {
4992 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4993 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4994 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4995 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4996 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4997 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4998 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4999 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5000 ARG6) NAME,
5001 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5002 ARG6, ARG7) NAME,
5003 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5004 ARG6, ARG7, ARG8) NAME,
5005 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5006 ARG6, ARG7, ARG8, ARG9) NAME,
5007 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5008 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5009 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5010 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5011 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5012 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5013 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5014 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5015 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5016 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5017 NAME,
5018 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5019 ARG6) NAME,
5020 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5021 ARG6, ARG7) NAME,
5022 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5023 #include "builtin-types.def"
5024 #undef DEF_PRIMITIVE_TYPE
5025 #undef DEF_FUNCTION_TYPE_0
5026 #undef DEF_FUNCTION_TYPE_1
5027 #undef DEF_FUNCTION_TYPE_2
5028 #undef DEF_FUNCTION_TYPE_3
5029 #undef DEF_FUNCTION_TYPE_4
5030 #undef DEF_FUNCTION_TYPE_5
5031 #undef DEF_FUNCTION_TYPE_6
5032 #undef DEF_FUNCTION_TYPE_7
5033 #undef DEF_FUNCTION_TYPE_8
5034 #undef DEF_FUNCTION_TYPE_9
5035 #undef DEF_FUNCTION_TYPE_10
5036 #undef DEF_FUNCTION_TYPE_11
5037 #undef DEF_FUNCTION_TYPE_VAR_0
5038 #undef DEF_FUNCTION_TYPE_VAR_1
5039 #undef DEF_FUNCTION_TYPE_VAR_2
5040 #undef DEF_FUNCTION_TYPE_VAR_3
5041 #undef DEF_FUNCTION_TYPE_VAR_4
5042 #undef DEF_FUNCTION_TYPE_VAR_5
5043 #undef DEF_FUNCTION_TYPE_VAR_6
5044 #undef DEF_FUNCTION_TYPE_VAR_7
5045 #undef DEF_POINTER_TYPE
5046 BT_LAST
5047 };
5048
5049 typedef enum c_builtin_type builtin_type;
5050
5051 /* A temporary array for c_common_nodes_and_builtins. Used in
5052 communication with def_fn_type. */
5053 static tree builtin_types[(int) BT_LAST + 1];
5054
5055 /* A helper function for c_common_nodes_and_builtins. Build function type
5056 for DEF with return type RET and N arguments. If VAR is true, then the
5057 function should be variadic after those N arguments.
5058
5059 Takes special care not to ICE if any of the types involved are
5060 error_mark_node, which indicates that said type is not in fact available
5061 (see builtin_type_for_size). In which case the function type as a whole
5062 should be error_mark_node. */
5063
5064 static void
5065 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5066 {
5067 tree t;
5068 tree *args = XALLOCAVEC (tree, n);
5069 va_list list;
5070 int i;
5071
5072 va_start (list, n);
5073 for (i = 0; i < n; ++i)
5074 {
5075 builtin_type a = (builtin_type) va_arg (list, int);
5076 t = builtin_types[a];
5077 if (t == error_mark_node)
5078 goto egress;
5079 args[i] = t;
5080 }
5081
5082 t = builtin_types[ret];
5083 if (t == error_mark_node)
5084 goto egress;
5085 if (var)
5086 t = build_varargs_function_type_array (t, n, args);
5087 else
5088 t = build_function_type_array (t, n, args);
5089
5090 egress:
5091 builtin_types[def] = t;
5092 va_end (list);
5093 }
5094
5095 /* Build builtin functions common to both C and C++ language
5096 frontends. */
5097
5098 static void
5099 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5100 {
5101 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5102 builtin_types[ENUM] = VALUE;
5103 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5104 def_fn_type (ENUM, RETURN, 0, 0);
5105 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5106 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5107 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5108 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5109 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5110 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5111 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5112 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5113 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5114 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5115 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5116 ARG6) \
5117 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5118 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5119 ARG6, ARG7) \
5120 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5121 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5122 ARG6, ARG7, ARG8) \
5123 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5124 ARG7, ARG8);
5125 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5126 ARG6, ARG7, ARG8, ARG9) \
5127 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5128 ARG7, ARG8, ARG9);
5129 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5130 ARG6, ARG7, ARG8, ARG9, ARG10) \
5131 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5132 ARG7, ARG8, ARG9, ARG10);
5133 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5134 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5135 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5136 ARG7, ARG8, ARG9, ARG10, ARG11);
5137 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5138 def_fn_type (ENUM, RETURN, 1, 0);
5139 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5140 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5141 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5142 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5143 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5144 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5145 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5146 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5147 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5148 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5149 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5150 ARG6) \
5151 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5152 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5153 ARG6, ARG7) \
5154 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5155 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5156 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5157
5158 #include "builtin-types.def"
5159
5160 #undef DEF_PRIMITIVE_TYPE
5161 #undef DEF_FUNCTION_TYPE_0
5162 #undef DEF_FUNCTION_TYPE_1
5163 #undef DEF_FUNCTION_TYPE_2
5164 #undef DEF_FUNCTION_TYPE_3
5165 #undef DEF_FUNCTION_TYPE_4
5166 #undef DEF_FUNCTION_TYPE_5
5167 #undef DEF_FUNCTION_TYPE_6
5168 #undef DEF_FUNCTION_TYPE_7
5169 #undef DEF_FUNCTION_TYPE_8
5170 #undef DEF_FUNCTION_TYPE_9
5171 #undef DEF_FUNCTION_TYPE_10
5172 #undef DEF_FUNCTION_TYPE_11
5173 #undef DEF_FUNCTION_TYPE_VAR_0
5174 #undef DEF_FUNCTION_TYPE_VAR_1
5175 #undef DEF_FUNCTION_TYPE_VAR_2
5176 #undef DEF_FUNCTION_TYPE_VAR_3
5177 #undef DEF_FUNCTION_TYPE_VAR_4
5178 #undef DEF_FUNCTION_TYPE_VAR_5
5179 #undef DEF_FUNCTION_TYPE_VAR_6
5180 #undef DEF_FUNCTION_TYPE_VAR_7
5181 #undef DEF_POINTER_TYPE
5182 builtin_types[(int) BT_LAST] = NULL_TREE;
5183
5184 c_init_attributes ();
5185
5186 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5187 NONANSI_P, ATTRS, IMPLICIT, COND) \
5188 if (NAME && COND) \
5189 def_builtin_1 (ENUM, NAME, CLASS, \
5190 builtin_types[(int) TYPE], \
5191 builtin_types[(int) LIBTYPE], \
5192 BOTH_P, FALLBACK_P, NONANSI_P, \
5193 built_in_attributes[(int) ATTRS], IMPLICIT);
5194 #include "builtins.def"
5195
5196 targetm.init_builtins ();
5197
5198 build_common_builtin_nodes ();
5199
5200 if (flag_cilkplus)
5201 cilk_init_builtins ();
5202 }
5203
5204 /* Like get_identifier, but avoid warnings about null arguments when
5205 the argument may be NULL for targets where GCC lacks stdint.h type
5206 information. */
5207
5208 static inline tree
5209 c_get_ident (const char *id)
5210 {
5211 return get_identifier (id);
5212 }
5213
5214 /* Build tree nodes and builtin functions common to both C and C++ language
5215 frontends. */
5216
5217 void
5218 c_common_nodes_and_builtins (void)
5219 {
5220 int char16_type_size;
5221 int char32_type_size;
5222 int wchar_type_size;
5223 tree array_domain_type;
5224 tree va_list_ref_type_node;
5225 tree va_list_arg_type_node;
5226 int i;
5227
5228 build_common_tree_nodes (flag_signed_char, flag_short_double);
5229
5230 /* Define `int' and `char' first so that dbx will output them first. */
5231 record_builtin_type (RID_INT, NULL, integer_type_node);
5232 record_builtin_type (RID_CHAR, "char", char_type_node);
5233
5234 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5235 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5236 but not C. Are the conditionals here needed? */
5237 if (c_dialect_cxx ())
5238 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5239 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5240 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5241 record_builtin_type (RID_MAX, "long unsigned int",
5242 long_unsigned_type_node);
5243
5244 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5245 {
5246 char name[25];
5247
5248 sprintf (name, "__int%d", int_n_data[i].bitsize);
5249 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5250 int_n_trees[i].signed_type);
5251 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5252 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5253 }
5254
5255 if (c_dialect_cxx ())
5256 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5257 record_builtin_type (RID_MAX, "long long int",
5258 long_long_integer_type_node);
5259 record_builtin_type (RID_MAX, "long long unsigned int",
5260 long_long_unsigned_type_node);
5261 if (c_dialect_cxx ())
5262 record_builtin_type (RID_MAX, "long long unsigned",
5263 long_long_unsigned_type_node);
5264 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5265 record_builtin_type (RID_MAX, "short unsigned int",
5266 short_unsigned_type_node);
5267 if (c_dialect_cxx ())
5268 record_builtin_type (RID_MAX, "unsigned short",
5269 short_unsigned_type_node);
5270
5271 /* Define both `signed char' and `unsigned char'. */
5272 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5273 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5274
5275 /* These are types that c_common_type_for_size and
5276 c_common_type_for_mode use. */
5277 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5278 TYPE_DECL, NULL_TREE,
5279 intQI_type_node));
5280 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5281 TYPE_DECL, NULL_TREE,
5282 intHI_type_node));
5283 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5284 TYPE_DECL, NULL_TREE,
5285 intSI_type_node));
5286 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5287 TYPE_DECL, NULL_TREE,
5288 intDI_type_node));
5289 #if HOST_BITS_PER_WIDE_INT >= 64
5290 /* Note that this is different than the __int128 type that's part of
5291 the generic __intN support. */
5292 if (targetm.scalar_mode_supported_p (TImode))
5293 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5294 TYPE_DECL,
5295 get_identifier ("__int128_t"),
5296 intTI_type_node));
5297 #endif
5298 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5299 TYPE_DECL, NULL_TREE,
5300 unsigned_intQI_type_node));
5301 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5302 TYPE_DECL, NULL_TREE,
5303 unsigned_intHI_type_node));
5304 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5305 TYPE_DECL, NULL_TREE,
5306 unsigned_intSI_type_node));
5307 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5308 TYPE_DECL, NULL_TREE,
5309 unsigned_intDI_type_node));
5310 #if HOST_BITS_PER_WIDE_INT >= 64
5311 if (targetm.scalar_mode_supported_p (TImode))
5312 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5313 TYPE_DECL,
5314 get_identifier ("__uint128_t"),
5315 unsigned_intTI_type_node));
5316 #endif
5317
5318 /* Create the widest literal types. */
5319 widest_integer_literal_type_node
5320 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5321 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5322 TYPE_DECL, NULL_TREE,
5323 widest_integer_literal_type_node));
5324
5325 widest_unsigned_literal_type_node
5326 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5327 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5328 TYPE_DECL, NULL_TREE,
5329 widest_unsigned_literal_type_node));
5330
5331 signed_size_type_node = c_common_signed_type (size_type_node);
5332
5333 pid_type_node =
5334 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5335
5336 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5337 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5338 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5339
5340 /* Only supported decimal floating point extension if the target
5341 actually supports underlying modes. */
5342 if (targetm.scalar_mode_supported_p (SDmode)
5343 && targetm.scalar_mode_supported_p (DDmode)
5344 && targetm.scalar_mode_supported_p (TDmode))
5345 {
5346 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5347 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5348 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5349 }
5350
5351 if (targetm.fixed_point_supported_p ())
5352 {
5353 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5354 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5355 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5356 record_builtin_type (RID_MAX, "long long _Fract",
5357 long_long_fract_type_node);
5358 record_builtin_type (RID_MAX, "unsigned short _Fract",
5359 unsigned_short_fract_type_node);
5360 record_builtin_type (RID_MAX, "unsigned _Fract",
5361 unsigned_fract_type_node);
5362 record_builtin_type (RID_MAX, "unsigned long _Fract",
5363 unsigned_long_fract_type_node);
5364 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5365 unsigned_long_long_fract_type_node);
5366 record_builtin_type (RID_MAX, "_Sat short _Fract",
5367 sat_short_fract_type_node);
5368 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5369 record_builtin_type (RID_MAX, "_Sat long _Fract",
5370 sat_long_fract_type_node);
5371 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5372 sat_long_long_fract_type_node);
5373 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5374 sat_unsigned_short_fract_type_node);
5375 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5376 sat_unsigned_fract_type_node);
5377 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5378 sat_unsigned_long_fract_type_node);
5379 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5380 sat_unsigned_long_long_fract_type_node);
5381 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5382 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5383 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5384 record_builtin_type (RID_MAX, "long long _Accum",
5385 long_long_accum_type_node);
5386 record_builtin_type (RID_MAX, "unsigned short _Accum",
5387 unsigned_short_accum_type_node);
5388 record_builtin_type (RID_MAX, "unsigned _Accum",
5389 unsigned_accum_type_node);
5390 record_builtin_type (RID_MAX, "unsigned long _Accum",
5391 unsigned_long_accum_type_node);
5392 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5393 unsigned_long_long_accum_type_node);
5394 record_builtin_type (RID_MAX, "_Sat short _Accum",
5395 sat_short_accum_type_node);
5396 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5397 record_builtin_type (RID_MAX, "_Sat long _Accum",
5398 sat_long_accum_type_node);
5399 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5400 sat_long_long_accum_type_node);
5401 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5402 sat_unsigned_short_accum_type_node);
5403 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5404 sat_unsigned_accum_type_node);
5405 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5406 sat_unsigned_long_accum_type_node);
5407 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5408 sat_unsigned_long_long_accum_type_node);
5409
5410 }
5411
5412 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5413 TYPE_DECL,
5414 get_identifier ("complex int"),
5415 complex_integer_type_node));
5416 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5417 TYPE_DECL,
5418 get_identifier ("complex float"),
5419 complex_float_type_node));
5420 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5421 TYPE_DECL,
5422 get_identifier ("complex double"),
5423 complex_double_type_node));
5424 lang_hooks.decls.pushdecl
5425 (build_decl (UNKNOWN_LOCATION,
5426 TYPE_DECL, get_identifier ("complex long double"),
5427 complex_long_double_type_node));
5428
5429 if (c_dialect_cxx ())
5430 /* For C++, make fileptr_type_node a distinct void * type until
5431 FILE type is defined. */
5432 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5433
5434 record_builtin_type (RID_VOID, NULL, void_type_node);
5435
5436 /* Set the TYPE_NAME for any variants that were built before
5437 record_builtin_type gave names to the built-in types. */
5438 {
5439 tree void_name = TYPE_NAME (void_type_node);
5440 TYPE_NAME (void_type_node) = NULL_TREE;
5441 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5442 = void_name;
5443 TYPE_NAME (void_type_node) = void_name;
5444 }
5445
5446 void_list_node = build_void_list_node ();
5447
5448 /* Make a type to be the domain of a few array types
5449 whose domains don't really matter.
5450 200 is small enough that it always fits in size_t
5451 and large enough that it can hold most function names for the
5452 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5453 array_domain_type = build_index_type (size_int (200));
5454
5455 /* Make a type for arrays of characters.
5456 With luck nothing will ever really depend on the length of this
5457 array type. */
5458 char_array_type_node
5459 = build_array_type (char_type_node, array_domain_type);
5460
5461 string_type_node = build_pointer_type (char_type_node);
5462 const_string_type_node
5463 = build_pointer_type (build_qualified_type
5464 (char_type_node, TYPE_QUAL_CONST));
5465
5466 /* This is special for C++ so functions can be overloaded. */
5467 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5468 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5469 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5470 underlying_wchar_type_node = wchar_type_node;
5471 if (c_dialect_cxx ())
5472 {
5473 if (TYPE_UNSIGNED (wchar_type_node))
5474 wchar_type_node = make_unsigned_type (wchar_type_size);
5475 else
5476 wchar_type_node = make_signed_type (wchar_type_size);
5477 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5478 }
5479
5480 /* This is for wide string constants. */
5481 wchar_array_type_node
5482 = build_array_type (wchar_type_node, array_domain_type);
5483
5484 /* Define 'char16_t'. */
5485 char16_type_node = get_identifier (CHAR16_TYPE);
5486 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5487 char16_type_size = TYPE_PRECISION (char16_type_node);
5488 if (c_dialect_cxx ())
5489 {
5490 char16_type_node = make_unsigned_type (char16_type_size);
5491
5492 if (cxx_dialect >= cxx11)
5493 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5494 }
5495
5496 /* This is for UTF-16 string constants. */
5497 char16_array_type_node
5498 = build_array_type (char16_type_node, array_domain_type);
5499
5500 /* Define 'char32_t'. */
5501 char32_type_node = get_identifier (CHAR32_TYPE);
5502 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5503 char32_type_size = TYPE_PRECISION (char32_type_node);
5504 if (c_dialect_cxx ())
5505 {
5506 char32_type_node = make_unsigned_type (char32_type_size);
5507
5508 if (cxx_dialect >= cxx11)
5509 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5510 }
5511
5512 /* This is for UTF-32 string constants. */
5513 char32_array_type_node
5514 = build_array_type (char32_type_node, array_domain_type);
5515
5516 wint_type_node =
5517 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5518
5519 intmax_type_node =
5520 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5521 uintmax_type_node =
5522 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5523
5524 if (SIG_ATOMIC_TYPE)
5525 sig_atomic_type_node =
5526 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5527 if (INT8_TYPE)
5528 int8_type_node =
5529 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5530 if (INT16_TYPE)
5531 int16_type_node =
5532 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5533 if (INT32_TYPE)
5534 int32_type_node =
5535 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5536 if (INT64_TYPE)
5537 int64_type_node =
5538 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5539 if (UINT8_TYPE)
5540 uint8_type_node =
5541 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5542 if (UINT16_TYPE)
5543 c_uint16_type_node = uint16_type_node =
5544 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5545 if (UINT32_TYPE)
5546 c_uint32_type_node = uint32_type_node =
5547 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5548 if (UINT64_TYPE)
5549 c_uint64_type_node = uint64_type_node =
5550 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5551 if (INT_LEAST8_TYPE)
5552 int_least8_type_node =
5553 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5554 if (INT_LEAST16_TYPE)
5555 int_least16_type_node =
5556 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5557 if (INT_LEAST32_TYPE)
5558 int_least32_type_node =
5559 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5560 if (INT_LEAST64_TYPE)
5561 int_least64_type_node =
5562 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5563 if (UINT_LEAST8_TYPE)
5564 uint_least8_type_node =
5565 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5566 if (UINT_LEAST16_TYPE)
5567 uint_least16_type_node =
5568 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5569 if (UINT_LEAST32_TYPE)
5570 uint_least32_type_node =
5571 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5572 if (UINT_LEAST64_TYPE)
5573 uint_least64_type_node =
5574 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5575 if (INT_FAST8_TYPE)
5576 int_fast8_type_node =
5577 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5578 if (INT_FAST16_TYPE)
5579 int_fast16_type_node =
5580 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5581 if (INT_FAST32_TYPE)
5582 int_fast32_type_node =
5583 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5584 if (INT_FAST64_TYPE)
5585 int_fast64_type_node =
5586 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5587 if (UINT_FAST8_TYPE)
5588 uint_fast8_type_node =
5589 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5590 if (UINT_FAST16_TYPE)
5591 uint_fast16_type_node =
5592 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5593 if (UINT_FAST32_TYPE)
5594 uint_fast32_type_node =
5595 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5596 if (UINT_FAST64_TYPE)
5597 uint_fast64_type_node =
5598 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5599 if (INTPTR_TYPE)
5600 intptr_type_node =
5601 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5602 if (UINTPTR_TYPE)
5603 uintptr_type_node =
5604 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5605
5606 default_function_type
5607 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5608 ptrdiff_type_node
5609 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5610 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5611
5612 lang_hooks.decls.pushdecl
5613 (build_decl (UNKNOWN_LOCATION,
5614 TYPE_DECL, get_identifier ("__builtin_va_list"),
5615 va_list_type_node));
5616 if (targetm.enum_va_list_p)
5617 {
5618 int l;
5619 const char *pname;
5620 tree ptype;
5621
5622 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5623 {
5624 lang_hooks.decls.pushdecl
5625 (build_decl (UNKNOWN_LOCATION,
5626 TYPE_DECL, get_identifier (pname),
5627 ptype));
5628
5629 }
5630 }
5631
5632 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5633 {
5634 va_list_arg_type_node = va_list_ref_type_node =
5635 build_pointer_type (TREE_TYPE (va_list_type_node));
5636 }
5637 else
5638 {
5639 va_list_arg_type_node = va_list_type_node;
5640 va_list_ref_type_node = build_reference_type (va_list_type_node);
5641 }
5642
5643 if (!flag_preprocess_only)
5644 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5645
5646 main_identifier_node = get_identifier ("main");
5647
5648 /* Create the built-in __null node. It is important that this is
5649 not shared. */
5650 null_node = make_int_cst (1, 1);
5651 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5652
5653 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5654 memset (builtin_types, 0, sizeof (builtin_types));
5655 }
5656
5657 /* The number of named compound-literals generated thus far. */
5658 static GTY(()) int compound_literal_number;
5659
5660 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5661
5662 void
5663 set_compound_literal_name (tree decl)
5664 {
5665 char *name;
5666 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5667 compound_literal_number);
5668 compound_literal_number++;
5669 DECL_NAME (decl) = get_identifier (name);
5670 }
5671
5672 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5673 TYPE and operand OP. */
5674
5675 static tree
5676 build_va_arg_1 (location_t loc, tree type, tree op)
5677 {
5678 tree expr = build1 (VA_ARG_EXPR, type, op);
5679 SET_EXPR_LOCATION (expr, loc);
5680 return expr;
5681 }
5682
5683 /* Return a VA_ARG_EXPR corresponding to a source-level expression
5684 va_arg (EXPR, TYPE) at source location LOC. */
5685
5686 tree
5687 build_va_arg (location_t loc, tree expr, tree type)
5688 {
5689 tree va_type = TREE_TYPE (expr);
5690 tree canon_va_type = (va_type == error_mark_node
5691 ? NULL_TREE
5692 : targetm.canonical_va_list_type (va_type));
5693
5694 if (va_type == error_mark_node
5695 || canon_va_type == NULL_TREE)
5696 {
5697 /* Let's handle things neutrallly, if expr:
5698 - has undeclared type, or
5699 - is not an va_list type. */
5700 return build_va_arg_1 (loc, type, expr);
5701 }
5702
5703 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5704 {
5705 /* Case 1: Not an array type. */
5706
5707 /* Take the address, to get '&ap'. */
5708 mark_addressable (expr);
5709 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5710
5711 /* Verify that &ap is still recognized as having va_list type. */
5712 tree canon_expr_type
5713 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5714 gcc_assert (canon_expr_type != NULL_TREE);
5715
5716 return build_va_arg_1 (loc, type, expr);
5717 }
5718
5719 /* Case 2: Array type.
5720
5721 Background:
5722
5723 For contrast, let's start with the simple case (case 1). If
5724 canon_va_type is not an array type, but say a char *, then when
5725 passing-by-value a va_list, the type of the va_list param decl is
5726 the same as for another va_list decl (all ap's are char *):
5727
5728 f2_1 (char * ap)
5729 D.1815 = VA_ARG (&ap, 0B, 1);
5730 return D.1815;
5731
5732 f2 (int i)
5733 char * ap.0;
5734 char * ap;
5735 __builtin_va_start (&ap, 0);
5736 ap.0 = ap;
5737 res = f2_1 (ap.0);
5738 __builtin_va_end (&ap);
5739 D.1812 = res;
5740 return D.1812;
5741
5742 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5743 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5744 the same as for another va_list decl (case 2a, struct ap[1]).
5745
5746 f2_1 (struct * ap)
5747 D.1844 = VA_ARG (ap, 0B, 0);
5748 return D.1844;
5749
5750 f2 (int i)
5751 struct ap[1];
5752 __builtin_va_start (&ap, 0);
5753 res = f2_1 (&ap);
5754 __builtin_va_end (&ap);
5755 D.1841 = res;
5756 return D.1841;
5757
5758 Case 2b is different because:
5759 - on the callee side, the parm decl has declared type va_list, but
5760 grokdeclarator changes the type of the parm decl to a pointer to the
5761 array elem type.
5762 - on the caller side, the pass-by-value uses &ap.
5763
5764 We unify these two cases (case 2a: va_list is array type,
5765 case 2b: va_list is pointer to array elem type), by adding '&' for the
5766 array type case, such that we have a pointer to array elem in both
5767 cases. */
5768
5769 if (TREE_CODE (va_type) == ARRAY_TYPE)
5770 {
5771 /* Case 2a: va_list is array type. */
5772
5773 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5774 elem type. */
5775 mark_addressable (expr);
5776 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5777 expr);
5778
5779 /* Verify that &ap is still recognized as having va_list type. */
5780 tree canon_expr_type
5781 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5782 gcc_assert (canon_expr_type != NULL_TREE);
5783 }
5784 else
5785 {
5786 /* Case 2b: va_list is pointer to array elem type. */
5787 gcc_assert (POINTER_TYPE_P (va_type));
5788 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
5789
5790 /* Don't take the address. We've already got '&ap'. */
5791 ;
5792 }
5793
5794 return build_va_arg_1 (loc, type, expr);
5795 }
5796
5797
5798 /* Linked list of disabled built-in functions. */
5799
5800 struct disabled_builtin
5801 {
5802 const char *name;
5803 struct disabled_builtin *next;
5804 };
5805 static disabled_builtin *disabled_builtins = NULL;
5806
5807 static bool builtin_function_disabled_p (const char *);
5808
5809 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5810 begins with "__builtin_", give an error. */
5811
5812 void
5813 disable_builtin_function (const char *name)
5814 {
5815 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5816 error ("cannot disable built-in function %qs", name);
5817 else
5818 {
5819 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5820 new_disabled_builtin->name = name;
5821 new_disabled_builtin->next = disabled_builtins;
5822 disabled_builtins = new_disabled_builtin;
5823 }
5824 }
5825
5826
5827 /* Return true if the built-in function NAME has been disabled, false
5828 otherwise. */
5829
5830 static bool
5831 builtin_function_disabled_p (const char *name)
5832 {
5833 disabled_builtin *p;
5834 for (p = disabled_builtins; p != NULL; p = p->next)
5835 {
5836 if (strcmp (name, p->name) == 0)
5837 return true;
5838 }
5839 return false;
5840 }
5841
5842
5843 /* Worker for DEF_BUILTIN.
5844 Possibly define a builtin function with one or two names.
5845 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5846 nonansi_p and flag_no_nonansi_builtin. */
5847
5848 static void
5849 def_builtin_1 (enum built_in_function fncode,
5850 const char *name,
5851 enum built_in_class fnclass,
5852 tree fntype, tree libtype,
5853 bool both_p, bool fallback_p, bool nonansi_p,
5854 tree fnattrs, bool implicit_p)
5855 {
5856 tree decl;
5857 const char *libname;
5858
5859 if (fntype == error_mark_node)
5860 return;
5861
5862 gcc_assert ((!both_p && !fallback_p)
5863 || !strncmp (name, "__builtin_",
5864 strlen ("__builtin_")));
5865
5866 libname = name + strlen ("__builtin_");
5867 decl = add_builtin_function (name, fntype, fncode, fnclass,
5868 (fallback_p ? libname : NULL),
5869 fnattrs);
5870
5871 set_builtin_decl (fncode, decl, implicit_p);
5872
5873 if (both_p
5874 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5875 && !(nonansi_p && flag_no_nonansi_builtin))
5876 add_builtin_function (libname, libtype, fncode, fnclass,
5877 NULL, fnattrs);
5878 }
5879 \f
5880 /* Nonzero if the type T promotes to int. This is (nearly) the
5881 integral promotions defined in ISO C99 6.3.1.1/2. */
5882
5883 bool
5884 c_promoting_integer_type_p (const_tree t)
5885 {
5886 switch (TREE_CODE (t))
5887 {
5888 case INTEGER_TYPE:
5889 return (TYPE_MAIN_VARIANT (t) == char_type_node
5890 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5891 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5892 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5893 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5894 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5895
5896 case ENUMERAL_TYPE:
5897 /* ??? Technically all enumerations not larger than an int
5898 promote to an int. But this is used along code paths
5899 that only want to notice a size change. */
5900 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5901
5902 case BOOLEAN_TYPE:
5903 return 1;
5904
5905 default:
5906 return 0;
5907 }
5908 }
5909
5910 /* Return 1 if PARMS specifies a fixed number of parameters
5911 and none of their types is affected by default promotions. */
5912
5913 int
5914 self_promoting_args_p (const_tree parms)
5915 {
5916 const_tree t;
5917 for (t = parms; t; t = TREE_CHAIN (t))
5918 {
5919 tree type = TREE_VALUE (t);
5920
5921 if (type == error_mark_node)
5922 continue;
5923
5924 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5925 return 0;
5926
5927 if (type == 0)
5928 return 0;
5929
5930 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5931 return 0;
5932
5933 if (c_promoting_integer_type_p (type))
5934 return 0;
5935 }
5936 return 1;
5937 }
5938
5939 /* Recursively remove any '*' or '&' operator from TYPE. */
5940 tree
5941 strip_pointer_operator (tree t)
5942 {
5943 while (POINTER_TYPE_P (t))
5944 t = TREE_TYPE (t);
5945 return t;
5946 }
5947
5948 /* Recursively remove pointer or array type from TYPE. */
5949 tree
5950 strip_pointer_or_array_types (tree t)
5951 {
5952 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5953 t = TREE_TYPE (t);
5954 return t;
5955 }
5956
5957 /* Used to compare case labels. K1 and K2 are actually tree nodes
5958 representing case labels, or NULL_TREE for a `default' label.
5959 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5960 K2, and 0 if K1 and K2 are equal. */
5961
5962 int
5963 case_compare (splay_tree_key k1, splay_tree_key k2)
5964 {
5965 /* Consider a NULL key (such as arises with a `default' label) to be
5966 smaller than anything else. */
5967 if (!k1)
5968 return k2 ? -1 : 0;
5969 else if (!k2)
5970 return k1 ? 1 : 0;
5971
5972 return tree_int_cst_compare ((tree) k1, (tree) k2);
5973 }
5974
5975 /* Process a case label, located at LOC, for the range LOW_VALUE
5976 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5977 then this case label is actually a `default' label. If only
5978 HIGH_VALUE is NULL_TREE, then case label was declared using the
5979 usual C/C++ syntax, rather than the GNU case range extension.
5980 CASES is a tree containing all the case ranges processed so far;
5981 COND is the condition for the switch-statement itself.
5982 OUTSIDE_RANGE_P says whether there was a case value that doesn't
5983 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
5984 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
5985
5986 tree
5987 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5988 tree low_value, tree high_value, bool *outside_range_p)
5989 {
5990 tree type;
5991 tree label;
5992 tree case_label;
5993 splay_tree_node node;
5994
5995 /* Create the LABEL_DECL itself. */
5996 label = create_artificial_label (loc);
5997
5998 /* If there was an error processing the switch condition, bail now
5999 before we get more confused. */
6000 if (!cond || cond == error_mark_node)
6001 goto error_out;
6002
6003 if ((low_value && TREE_TYPE (low_value)
6004 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6005 || (high_value && TREE_TYPE (high_value)
6006 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6007 {
6008 error_at (loc, "pointers are not permitted as case values");
6009 goto error_out;
6010 }
6011
6012 /* Case ranges are a GNU extension. */
6013 if (high_value)
6014 pedwarn (loc, OPT_Wpedantic,
6015 "range expressions in switch statements are non-standard");
6016
6017 type = TREE_TYPE (cond);
6018 if (low_value)
6019 {
6020 low_value = check_case_value (loc, low_value);
6021 low_value = convert_and_check (loc, type, low_value);
6022 if (low_value == error_mark_node)
6023 goto error_out;
6024 }
6025 if (high_value)
6026 {
6027 high_value = check_case_value (loc, high_value);
6028 high_value = convert_and_check (loc, type, high_value);
6029 if (high_value == error_mark_node)
6030 goto error_out;
6031 }
6032
6033 if (low_value && high_value)
6034 {
6035 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6036 really a case range, even though it was written that way.
6037 Remove the HIGH_VALUE to simplify later processing. */
6038 if (tree_int_cst_equal (low_value, high_value))
6039 high_value = NULL_TREE;
6040 else if (!tree_int_cst_lt (low_value, high_value))
6041 warning_at (loc, 0, "empty range specified");
6042 }
6043
6044 /* See if the case is in range of the type of the original testing
6045 expression. If both low_value and high_value are out of range,
6046 don't insert the case label and return NULL_TREE. */
6047 if (low_value
6048 && !check_case_bounds (loc, type, orig_type,
6049 &low_value, high_value ? &high_value : NULL,
6050 outside_range_p))
6051 return NULL_TREE;
6052
6053 /* Look up the LOW_VALUE in the table of case labels we already
6054 have. */
6055 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6056 /* If there was not an exact match, check for overlapping ranges.
6057 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6058 that's a `default' label and the only overlap is an exact match. */
6059 if (!node && (low_value || high_value))
6060 {
6061 splay_tree_node low_bound;
6062 splay_tree_node high_bound;
6063
6064 /* Even though there wasn't an exact match, there might be an
6065 overlap between this case range and another case range.
6066 Since we've (inductively) not allowed any overlapping case
6067 ranges, we simply need to find the greatest low case label
6068 that is smaller that LOW_VALUE, and the smallest low case
6069 label that is greater than LOW_VALUE. If there is an overlap
6070 it will occur in one of these two ranges. */
6071 low_bound = splay_tree_predecessor (cases,
6072 (splay_tree_key) low_value);
6073 high_bound = splay_tree_successor (cases,
6074 (splay_tree_key) low_value);
6075
6076 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6077 the LOW_VALUE, so there is no need to check unless the
6078 LOW_BOUND is in fact itself a case range. */
6079 if (low_bound
6080 && CASE_HIGH ((tree) low_bound->value)
6081 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6082 low_value) >= 0)
6083 node = low_bound;
6084 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6085 range is bigger than the low end of the current range, so we
6086 are only interested if the current range is a real range, and
6087 not an ordinary case label. */
6088 else if (high_bound
6089 && high_value
6090 && (tree_int_cst_compare ((tree) high_bound->key,
6091 high_value)
6092 <= 0))
6093 node = high_bound;
6094 }
6095 /* If there was an overlap, issue an error. */
6096 if (node)
6097 {
6098 tree duplicate = CASE_LABEL ((tree) node->value);
6099
6100 if (high_value)
6101 {
6102 error_at (loc, "duplicate (or overlapping) case value");
6103 error_at (DECL_SOURCE_LOCATION (duplicate),
6104 "this is the first entry overlapping that value");
6105 }
6106 else if (low_value)
6107 {
6108 error_at (loc, "duplicate case value") ;
6109 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6110 }
6111 else
6112 {
6113 error_at (loc, "multiple default labels in one switch");
6114 error_at (DECL_SOURCE_LOCATION (duplicate),
6115 "this is the first default label");
6116 }
6117 goto error_out;
6118 }
6119
6120 /* Add a CASE_LABEL to the statement-tree. */
6121 case_label = add_stmt (build_case_label (low_value, high_value, label));
6122 /* Register this case label in the splay tree. */
6123 splay_tree_insert (cases,
6124 (splay_tree_key) low_value,
6125 (splay_tree_value) case_label);
6126
6127 return case_label;
6128
6129 error_out:
6130 /* Add a label so that the back-end doesn't think that the beginning of
6131 the switch is unreachable. Note that we do not add a case label, as
6132 that just leads to duplicates and thence to failure later on. */
6133 if (!cases->root)
6134 {
6135 tree t = create_artificial_label (loc);
6136 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6137 }
6138 return error_mark_node;
6139 }
6140
6141 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6142 Used to verify that case values match up with enumerator values. */
6143
6144 static void
6145 match_case_to_enum_1 (tree key, tree type, tree label)
6146 {
6147 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6148
6149 if (tree_fits_uhwi_p (key))
6150 print_dec (key, buf, UNSIGNED);
6151 else if (tree_fits_shwi_p (key))
6152 print_dec (key, buf, SIGNED);
6153 else
6154 print_hex (key, buf);
6155
6156 if (TYPE_NAME (type) == 0)
6157 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6158 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6159 "case value %qs not in enumerated type",
6160 buf);
6161 else
6162 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6163 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6164 "case value %qs not in enumerated type %qT",
6165 buf, type);
6166 }
6167
6168 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6169 Used to verify that case values match up with enumerator values. */
6170
6171 static int
6172 match_case_to_enum (splay_tree_node node, void *data)
6173 {
6174 tree label = (tree) node->value;
6175 tree type = (tree) data;
6176
6177 /* Skip default case. */
6178 if (!CASE_LOW (label))
6179 return 0;
6180
6181 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6182 when we did our enum->case scan. Reset our scratch bit after. */
6183 if (!CASE_LOW_SEEN (label))
6184 match_case_to_enum_1 (CASE_LOW (label), type, label);
6185 else
6186 CASE_LOW_SEEN (label) = 0;
6187
6188 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6189 not set, that means that CASE_HIGH did not appear when we did our
6190 enum->case scan. Reset our scratch bit after. */
6191 if (CASE_HIGH (label))
6192 {
6193 if (!CASE_HIGH_SEEN (label))
6194 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6195 else
6196 CASE_HIGH_SEEN (label) = 0;
6197 }
6198
6199 return 0;
6200 }
6201
6202 /* Handle -Wswitch*. Called from the front end after parsing the
6203 switch construct. */
6204 /* ??? Should probably be somewhere generic, since other languages
6205 besides C and C++ would want this. At the moment, however, C/C++
6206 are the only tree-ssa languages that support enumerations at all,
6207 so the point is moot. */
6208
6209 void
6210 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6211 tree type, tree cond, bool bool_cond_p,
6212 bool outside_range_p)
6213 {
6214 splay_tree_node default_node;
6215 splay_tree_node node;
6216 tree chain;
6217
6218 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6219 && !warn_switch_bool)
6220 return;
6221
6222 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6223 if (!default_node)
6224 warning_at (switch_location, OPT_Wswitch_default,
6225 "switch missing default case");
6226
6227 /* There are certain cases where -Wswitch-bool warnings aren't
6228 desirable, such as
6229 switch (boolean)
6230 {
6231 case true: ...
6232 case false: ...
6233 }
6234 so be careful here. */
6235 if (warn_switch_bool && bool_cond_p)
6236 {
6237 splay_tree_node min_node;
6238 /* If there's a default node, it's also the value with the minimal
6239 key. So look at the penultimate key (if any). */
6240 if (default_node)
6241 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6242 else
6243 min_node = splay_tree_min (cases);
6244 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6245
6246 splay_tree_node max_node = splay_tree_max (cases);
6247 /* This might be a case range, so look at the value with the
6248 maximal key and then check CASE_HIGH. */
6249 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6250 if (max)
6251 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6252
6253 /* If there's a case value > 1 or < 0, that is outside bool
6254 range, warn. */
6255 if (outside_range_p
6256 || (max && wi::gts_p (max, 1))
6257 || (min && wi::lts_p (min, 0))
6258 /* And handle the
6259 switch (boolean)
6260 {
6261 case true: ...
6262 case false: ...
6263 default: ...
6264 }
6265 case, where we want to warn. */
6266 || (default_node
6267 && max && wi::eq_p (max, 1)
6268 && min && wi::eq_p (min, 0)))
6269 warning_at (switch_location, OPT_Wswitch_bool,
6270 "switch condition has boolean value");
6271 }
6272
6273 /* From here on, we only care about enumerated types. */
6274 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6275 return;
6276
6277 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6278 if (!warn_switch_enum && !warn_switch)
6279 return;
6280
6281 /* Check the cases. Warn about case values which are not members of
6282 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6283 there is no default case, check that exactly all enumeration
6284 literals are covered by the cases. */
6285
6286 /* Clearing COND if it is not an integer constant simplifies
6287 the tests inside the loop below. */
6288 if (TREE_CODE (cond) != INTEGER_CST)
6289 cond = NULL_TREE;
6290
6291 /* The time complexity here is O(N*lg(N)) worst case, but for the
6292 common case of monotonically increasing enumerators, it is
6293 O(N), since the nature of the splay tree will keep the next
6294 element adjacent to the root at all times. */
6295
6296 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6297 {
6298 tree value = TREE_VALUE (chain);
6299 if (TREE_CODE (value) == CONST_DECL)
6300 value = DECL_INITIAL (value);
6301 node = splay_tree_lookup (cases, (splay_tree_key) value);
6302 if (node)
6303 {
6304 /* Mark the CASE_LOW part of the case entry as seen. */
6305 tree label = (tree) node->value;
6306 CASE_LOW_SEEN (label) = 1;
6307 continue;
6308 }
6309
6310 /* Even though there wasn't an exact match, there might be a
6311 case range which includes the enumerator's value. */
6312 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6313 if (node && CASE_HIGH ((tree) node->value))
6314 {
6315 tree label = (tree) node->value;
6316 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6317 if (cmp >= 0)
6318 {
6319 /* If we match the upper bound exactly, mark the CASE_HIGH
6320 part of the case entry as seen. */
6321 if (cmp == 0)
6322 CASE_HIGH_SEEN (label) = 1;
6323 continue;
6324 }
6325 }
6326
6327 /* We've now determined that this enumerated literal isn't
6328 handled by the case labels of the switch statement. */
6329
6330 /* If the switch expression is a constant, we only really care
6331 about whether that constant is handled by the switch. */
6332 if (cond && tree_int_cst_compare (cond, value))
6333 continue;
6334
6335 /* If there is a default_node, the only relevant option is
6336 Wswitch-enum. Otherwise, if both are enabled then we prefer
6337 to warn using -Wswitch because -Wswitch is enabled by -Wall
6338 while -Wswitch-enum is explicit. */
6339 warning_at (switch_location,
6340 (default_node || !warn_switch
6341 ? OPT_Wswitch_enum
6342 : OPT_Wswitch),
6343 "enumeration value %qE not handled in switch",
6344 TREE_PURPOSE (chain));
6345 }
6346
6347 /* Warn if there are case expressions that don't correspond to
6348 enumerators. This can occur since C and C++ don't enforce
6349 type-checking of assignments to enumeration variables.
6350
6351 The time complexity here is now always O(N) worst case, since
6352 we should have marked both the lower bound and upper bound of
6353 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6354 above. This scan also resets those fields. */
6355
6356 splay_tree_foreach (cases, match_case_to_enum, type);
6357 }
6358
6359 /* Finish an expression taking the address of LABEL (an
6360 IDENTIFIER_NODE). Returns an expression for the address.
6361
6362 LOC is the location for the expression returned. */
6363
6364 tree
6365 finish_label_address_expr (tree label, location_t loc)
6366 {
6367 tree result;
6368
6369 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6370
6371 if (label == error_mark_node)
6372 return error_mark_node;
6373
6374 label = lookup_label (label);
6375 if (label == NULL_TREE)
6376 result = null_pointer_node;
6377 else
6378 {
6379 TREE_USED (label) = 1;
6380 result = build1 (ADDR_EXPR, ptr_type_node, label);
6381 /* The current function is not necessarily uninlinable.
6382 Computed gotos are incompatible with inlining, but the value
6383 here could be used only in a diagnostic, for example. */
6384 protected_set_expr_location (result, loc);
6385 }
6386
6387 return result;
6388 }
6389 \f
6390
6391 /* Given a boolean expression ARG, return a tree representing an increment
6392 or decrement (as indicated by CODE) of ARG. The front end must check for
6393 invalid cases (e.g., decrement in C++). */
6394 tree
6395 boolean_increment (enum tree_code code, tree arg)
6396 {
6397 tree val;
6398 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6399
6400 arg = stabilize_reference (arg);
6401 switch (code)
6402 {
6403 case PREINCREMENT_EXPR:
6404 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6405 break;
6406 case POSTINCREMENT_EXPR:
6407 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6408 arg = save_expr (arg);
6409 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6410 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6411 break;
6412 case PREDECREMENT_EXPR:
6413 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6414 invert_truthvalue_loc (input_location, arg));
6415 break;
6416 case POSTDECREMENT_EXPR:
6417 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6418 invert_truthvalue_loc (input_location, arg));
6419 arg = save_expr (arg);
6420 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6421 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6422 break;
6423 default:
6424 gcc_unreachable ();
6425 }
6426 TREE_SIDE_EFFECTS (val) = 1;
6427 return val;
6428 }
6429 \f
6430 /* Built-in macros for stddef.h and stdint.h, that require macros
6431 defined in this file. */
6432 void
6433 c_stddef_cpp_builtins(void)
6434 {
6435 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6436 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6437 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6438 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6439 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6440 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6441 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6442 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6443 if (SIG_ATOMIC_TYPE)
6444 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6445 if (INT8_TYPE)
6446 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6447 if (INT16_TYPE)
6448 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6449 if (INT32_TYPE)
6450 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6451 if (INT64_TYPE)
6452 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6453 if (UINT8_TYPE)
6454 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6455 if (UINT16_TYPE)
6456 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6457 if (UINT32_TYPE)
6458 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6459 if (UINT64_TYPE)
6460 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6461 if (INT_LEAST8_TYPE)
6462 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6463 if (INT_LEAST16_TYPE)
6464 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6465 if (INT_LEAST32_TYPE)
6466 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6467 if (INT_LEAST64_TYPE)
6468 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6469 if (UINT_LEAST8_TYPE)
6470 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6471 if (UINT_LEAST16_TYPE)
6472 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6473 if (UINT_LEAST32_TYPE)
6474 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6475 if (UINT_LEAST64_TYPE)
6476 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6477 if (INT_FAST8_TYPE)
6478 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6479 if (INT_FAST16_TYPE)
6480 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6481 if (INT_FAST32_TYPE)
6482 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6483 if (INT_FAST64_TYPE)
6484 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6485 if (UINT_FAST8_TYPE)
6486 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6487 if (UINT_FAST16_TYPE)
6488 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6489 if (UINT_FAST32_TYPE)
6490 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6491 if (UINT_FAST64_TYPE)
6492 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6493 if (INTPTR_TYPE)
6494 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6495 if (UINTPTR_TYPE)
6496 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6497 }
6498
6499 static void
6500 c_init_attributes (void)
6501 {
6502 /* Fill in the built_in_attributes array. */
6503 #define DEF_ATTR_NULL_TREE(ENUM) \
6504 built_in_attributes[(int) ENUM] = NULL_TREE;
6505 #define DEF_ATTR_INT(ENUM, VALUE) \
6506 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6507 #define DEF_ATTR_STRING(ENUM, VALUE) \
6508 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6509 #define DEF_ATTR_IDENT(ENUM, STRING) \
6510 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6511 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6512 built_in_attributes[(int) ENUM] \
6513 = tree_cons (built_in_attributes[(int) PURPOSE], \
6514 built_in_attributes[(int) VALUE], \
6515 built_in_attributes[(int) CHAIN]);
6516 #include "builtin-attrs.def"
6517 #undef DEF_ATTR_NULL_TREE
6518 #undef DEF_ATTR_INT
6519 #undef DEF_ATTR_IDENT
6520 #undef DEF_ATTR_TREE_LIST
6521 }
6522
6523 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6524 identifier as an argument, so the front end shouldn't look it up. */
6525
6526 bool
6527 attribute_takes_identifier_p (const_tree attr_id)
6528 {
6529 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6530 if (spec == NULL)
6531 /* Unknown attribute that we'll end up ignoring, return true so we
6532 don't complain about an identifier argument. */
6533 return true;
6534 else if (!strcmp ("mode", spec->name)
6535 || !strcmp ("format", spec->name)
6536 || !strcmp ("cleanup", spec->name))
6537 return true;
6538 else
6539 return targetm.attribute_takes_identifier_p (attr_id);
6540 }
6541
6542 /* Attribute handlers common to C front ends. */
6543
6544 /* Handle a "packed" attribute; arguments as in
6545 struct attribute_spec.handler. */
6546
6547 static tree
6548 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6549 int flags, bool *no_add_attrs)
6550 {
6551 if (TYPE_P (*node))
6552 {
6553 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6554 *node = build_variant_type_copy (*node);
6555 TYPE_PACKED (*node) = 1;
6556 }
6557 else if (TREE_CODE (*node) == FIELD_DECL)
6558 {
6559 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6560 /* Still pack bitfields. */
6561 && ! DECL_INITIAL (*node))
6562 warning (OPT_Wattributes,
6563 "%qE attribute ignored for field of type %qT",
6564 name, TREE_TYPE (*node));
6565 else
6566 DECL_PACKED (*node) = 1;
6567 }
6568 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6569 used for DECL_REGISTER. It wouldn't mean anything anyway.
6570 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6571 that changes what the typedef is typing. */
6572 else
6573 {
6574 warning (OPT_Wattributes, "%qE attribute ignored", name);
6575 *no_add_attrs = true;
6576 }
6577
6578 return NULL_TREE;
6579 }
6580
6581 /* Handle a "nocommon" attribute; arguments as in
6582 struct attribute_spec.handler. */
6583
6584 static tree
6585 handle_nocommon_attribute (tree *node, tree name,
6586 tree ARG_UNUSED (args),
6587 int ARG_UNUSED (flags), bool *no_add_attrs)
6588 {
6589 if (VAR_P (*node))
6590 DECL_COMMON (*node) = 0;
6591 else
6592 {
6593 warning (OPT_Wattributes, "%qE attribute ignored", name);
6594 *no_add_attrs = true;
6595 }
6596
6597 return NULL_TREE;
6598 }
6599
6600 /* Handle a "common" attribute; arguments as in
6601 struct attribute_spec.handler. */
6602
6603 static tree
6604 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6605 int ARG_UNUSED (flags), bool *no_add_attrs)
6606 {
6607 if (VAR_P (*node))
6608 DECL_COMMON (*node) = 1;
6609 else
6610 {
6611 warning (OPT_Wattributes, "%qE attribute ignored", name);
6612 *no_add_attrs = true;
6613 }
6614
6615 return NULL_TREE;
6616 }
6617
6618 /* Handle a "noreturn" attribute; arguments as in
6619 struct attribute_spec.handler. */
6620
6621 static tree
6622 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6623 int ARG_UNUSED (flags), bool *no_add_attrs)
6624 {
6625 tree type = TREE_TYPE (*node);
6626
6627 /* See FIXME comment in c_common_attribute_table. */
6628 if (TREE_CODE (*node) == FUNCTION_DECL
6629 || objc_method_decl (TREE_CODE (*node)))
6630 TREE_THIS_VOLATILE (*node) = 1;
6631 else if (TREE_CODE (type) == POINTER_TYPE
6632 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6633 TREE_TYPE (*node)
6634 = (build_qualified_type
6635 (build_pointer_type
6636 (build_type_variant (TREE_TYPE (type),
6637 TYPE_READONLY (TREE_TYPE (type)), 1)),
6638 TYPE_QUALS (type)));
6639 else
6640 {
6641 warning (OPT_Wattributes, "%qE attribute ignored", name);
6642 *no_add_attrs = true;
6643 }
6644
6645 return NULL_TREE;
6646 }
6647
6648 /* Handle a "hot" and attribute; arguments as in
6649 struct attribute_spec.handler. */
6650
6651 static tree
6652 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6653 int ARG_UNUSED (flags), bool *no_add_attrs)
6654 {
6655 if (TREE_CODE (*node) == FUNCTION_DECL
6656 || TREE_CODE (*node) == LABEL_DECL)
6657 {
6658 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6659 {
6660 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6661 "with attribute %qs", name, "cold");
6662 *no_add_attrs = true;
6663 }
6664 /* Most of the rest of the hot processing is done later with
6665 lookup_attribute. */
6666 }
6667 else
6668 {
6669 warning (OPT_Wattributes, "%qE attribute ignored", name);
6670 *no_add_attrs = true;
6671 }
6672
6673 return NULL_TREE;
6674 }
6675
6676 /* Handle a "cold" and attribute; arguments as in
6677 struct attribute_spec.handler. */
6678
6679 static tree
6680 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6681 int ARG_UNUSED (flags), bool *no_add_attrs)
6682 {
6683 if (TREE_CODE (*node) == FUNCTION_DECL
6684 || TREE_CODE (*node) == LABEL_DECL)
6685 {
6686 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6687 {
6688 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6689 "with attribute %qs", name, "hot");
6690 *no_add_attrs = true;
6691 }
6692 /* Most of the rest of the cold processing is done later with
6693 lookup_attribute. */
6694 }
6695 else
6696 {
6697 warning (OPT_Wattributes, "%qE attribute ignored", name);
6698 *no_add_attrs = true;
6699 }
6700
6701 return NULL_TREE;
6702 }
6703
6704 /* Handle a "no_sanitize_address" attribute; arguments as in
6705 struct attribute_spec.handler. */
6706
6707 static tree
6708 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6709 bool *no_add_attrs)
6710 {
6711 if (TREE_CODE (*node) != FUNCTION_DECL)
6712 {
6713 warning (OPT_Wattributes, "%qE attribute ignored", name);
6714 *no_add_attrs = true;
6715 }
6716
6717 return NULL_TREE;
6718 }
6719
6720 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6721 struct attribute_spec.handler. */
6722
6723 static tree
6724 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6725 bool *no_add_attrs)
6726 {
6727 if (TREE_CODE (*node) != FUNCTION_DECL)
6728 warning (OPT_Wattributes, "%qE attribute ignored", name);
6729 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6730 DECL_ATTRIBUTES (*node)
6731 = tree_cons (get_identifier ("no_sanitize_address"),
6732 NULL_TREE, DECL_ATTRIBUTES (*node));
6733 *no_add_attrs = true;
6734 return NULL_TREE;
6735 }
6736
6737 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6738 struct attribute_spec.handler. */
6739
6740 static tree
6741 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6742 bool *no_add_attrs)
6743 {
6744 if (TREE_CODE (*node) != FUNCTION_DECL)
6745 {
6746 warning (OPT_Wattributes, "%qE attribute ignored", name);
6747 *no_add_attrs = true;
6748 }
6749
6750 return NULL_TREE;
6751 }
6752
6753 /* Handle a "stack_protect" attribute; arguments as in
6754 struct attribute_spec.handler. */
6755 static tree
6756 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6757 bool *no_add_attrs)
6758 {
6759 if (TREE_CODE (*node) != FUNCTION_DECL)
6760 {
6761 warning (OPT_Wattributes, "%qE attribute ignored", name);
6762 *no_add_attrs = true;
6763 }
6764 else
6765 DECL_ATTRIBUTES (*node)
6766 = tree_cons (get_identifier ("stack_protect"),
6767 NULL_TREE, DECL_ATTRIBUTES (*node));
6768
6769 return NULL_TREE;
6770 }
6771
6772 /* Handle a "noinline" attribute; arguments as in
6773 struct attribute_spec.handler. */
6774
6775 static tree
6776 handle_noinline_attribute (tree *node, tree name,
6777 tree ARG_UNUSED (args),
6778 int ARG_UNUSED (flags), bool *no_add_attrs)
6779 {
6780 if (TREE_CODE (*node) == FUNCTION_DECL)
6781 {
6782 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6783 {
6784 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6785 "with attribute %qs", name, "always_inline");
6786 *no_add_attrs = true;
6787 }
6788 else
6789 DECL_UNINLINABLE (*node) = 1;
6790 }
6791 else
6792 {
6793 warning (OPT_Wattributes, "%qE attribute ignored", name);
6794 *no_add_attrs = true;
6795 }
6796
6797 return NULL_TREE;
6798 }
6799
6800 /* Handle a "noclone" attribute; arguments as in
6801 struct attribute_spec.handler. */
6802
6803 static tree
6804 handle_noclone_attribute (tree *node, tree name,
6805 tree ARG_UNUSED (args),
6806 int ARG_UNUSED (flags), bool *no_add_attrs)
6807 {
6808 if (TREE_CODE (*node) != FUNCTION_DECL)
6809 {
6810 warning (OPT_Wattributes, "%qE attribute ignored", name);
6811 *no_add_attrs = true;
6812 }
6813
6814 return NULL_TREE;
6815 }
6816
6817 /* Handle a "no_icf" attribute; arguments as in
6818 struct attribute_spec.handler. */
6819
6820 static tree
6821 handle_noicf_attribute (tree *node, tree name,
6822 tree ARG_UNUSED (args),
6823 int ARG_UNUSED (flags), bool *no_add_attrs)
6824 {
6825 if (TREE_CODE (*node) != FUNCTION_DECL)
6826 {
6827 warning (OPT_Wattributes, "%qE attribute ignored", name);
6828 *no_add_attrs = true;
6829 }
6830
6831 return NULL_TREE;
6832 }
6833
6834
6835 /* Handle a "always_inline" attribute; arguments as in
6836 struct attribute_spec.handler. */
6837
6838 static tree
6839 handle_always_inline_attribute (tree *node, tree name,
6840 tree ARG_UNUSED (args),
6841 int ARG_UNUSED (flags),
6842 bool *no_add_attrs)
6843 {
6844 if (TREE_CODE (*node) == FUNCTION_DECL)
6845 {
6846 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6847 {
6848 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6849 "with %qs attribute", name, "noinline");
6850 *no_add_attrs = true;
6851 }
6852 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
6853 {
6854 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6855 "with %qs attribute", name, "target_clones");
6856 *no_add_attrs = true;
6857 }
6858 else
6859 /* Set the attribute and mark it for disregarding inline
6860 limits. */
6861 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6862 }
6863 else
6864 {
6865 warning (OPT_Wattributes, "%qE attribute ignored", name);
6866 *no_add_attrs = true;
6867 }
6868
6869 return NULL_TREE;
6870 }
6871
6872 /* Handle a "gnu_inline" attribute; arguments as in
6873 struct attribute_spec.handler. */
6874
6875 static tree
6876 handle_gnu_inline_attribute (tree *node, tree name,
6877 tree ARG_UNUSED (args),
6878 int ARG_UNUSED (flags),
6879 bool *no_add_attrs)
6880 {
6881 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6882 {
6883 /* Do nothing else, just set the attribute. We'll get at
6884 it later with lookup_attribute. */
6885 }
6886 else
6887 {
6888 warning (OPT_Wattributes, "%qE attribute ignored", name);
6889 *no_add_attrs = true;
6890 }
6891
6892 return NULL_TREE;
6893 }
6894
6895 /* Handle a "leaf" attribute; arguments as in
6896 struct attribute_spec.handler. */
6897
6898 static tree
6899 handle_leaf_attribute (tree *node, tree name,
6900 tree ARG_UNUSED (args),
6901 int ARG_UNUSED (flags), bool *no_add_attrs)
6902 {
6903 if (TREE_CODE (*node) != FUNCTION_DECL)
6904 {
6905 warning (OPT_Wattributes, "%qE attribute ignored", name);
6906 *no_add_attrs = true;
6907 }
6908 if (!TREE_PUBLIC (*node))
6909 {
6910 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6911 *no_add_attrs = true;
6912 }
6913
6914 return NULL_TREE;
6915 }
6916
6917 /* Handle an "artificial" attribute; arguments as in
6918 struct attribute_spec.handler. */
6919
6920 static tree
6921 handle_artificial_attribute (tree *node, tree name,
6922 tree ARG_UNUSED (args),
6923 int ARG_UNUSED (flags),
6924 bool *no_add_attrs)
6925 {
6926 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6927 {
6928 /* Do nothing else, just set the attribute. We'll get at
6929 it later with lookup_attribute. */
6930 }
6931 else
6932 {
6933 warning (OPT_Wattributes, "%qE attribute ignored", name);
6934 *no_add_attrs = true;
6935 }
6936
6937 return NULL_TREE;
6938 }
6939
6940 /* Handle a "flatten" attribute; arguments as in
6941 struct attribute_spec.handler. */
6942
6943 static tree
6944 handle_flatten_attribute (tree *node, tree name,
6945 tree args ATTRIBUTE_UNUSED,
6946 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6947 {
6948 if (TREE_CODE (*node) == FUNCTION_DECL)
6949 /* Do nothing else, just set the attribute. We'll get at
6950 it later with lookup_attribute. */
6951 ;
6952 else
6953 {
6954 warning (OPT_Wattributes, "%qE attribute ignored", name);
6955 *no_add_attrs = true;
6956 }
6957
6958 return NULL_TREE;
6959 }
6960
6961 /* Handle a "warning" or "error" attribute; arguments as in
6962 struct attribute_spec.handler. */
6963
6964 static tree
6965 handle_error_attribute (tree *node, tree name, tree args,
6966 int ARG_UNUSED (flags), bool *no_add_attrs)
6967 {
6968 if (TREE_CODE (*node) == FUNCTION_DECL
6969 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6970 /* Do nothing else, just set the attribute. We'll get at
6971 it later with lookup_attribute. */
6972 ;
6973 else
6974 {
6975 warning (OPT_Wattributes, "%qE attribute ignored", name);
6976 *no_add_attrs = true;
6977 }
6978
6979 return NULL_TREE;
6980 }
6981
6982 /* Handle a "used" attribute; arguments as in
6983 struct attribute_spec.handler. */
6984
6985 static tree
6986 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6987 int ARG_UNUSED (flags), bool *no_add_attrs)
6988 {
6989 tree node = *pnode;
6990
6991 if (TREE_CODE (node) == FUNCTION_DECL
6992 || (VAR_P (node) && TREE_STATIC (node))
6993 || (TREE_CODE (node) == TYPE_DECL))
6994 {
6995 TREE_USED (node) = 1;
6996 DECL_PRESERVE_P (node) = 1;
6997 if (VAR_P (node))
6998 DECL_READ_P (node) = 1;
6999 }
7000 else
7001 {
7002 warning (OPT_Wattributes, "%qE attribute ignored", name);
7003 *no_add_attrs = true;
7004 }
7005
7006 return NULL_TREE;
7007 }
7008
7009 /* Handle a "unused" attribute; arguments as in
7010 struct attribute_spec.handler. */
7011
7012 static tree
7013 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7014 int flags, bool *no_add_attrs)
7015 {
7016 if (DECL_P (*node))
7017 {
7018 tree decl = *node;
7019
7020 if (TREE_CODE (decl) == PARM_DECL
7021 || VAR_OR_FUNCTION_DECL_P (decl)
7022 || TREE_CODE (decl) == LABEL_DECL
7023 || TREE_CODE (decl) == TYPE_DECL)
7024 {
7025 TREE_USED (decl) = 1;
7026 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
7027 DECL_READ_P (decl) = 1;
7028 }
7029 else
7030 {
7031 warning (OPT_Wattributes, "%qE attribute ignored", name);
7032 *no_add_attrs = true;
7033 }
7034 }
7035 else
7036 {
7037 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7038 *node = build_variant_type_copy (*node);
7039 TREE_USED (*node) = 1;
7040 }
7041
7042 return NULL_TREE;
7043 }
7044
7045 /* Handle a "externally_visible" attribute; arguments as in
7046 struct attribute_spec.handler. */
7047
7048 static tree
7049 handle_externally_visible_attribute (tree *pnode, tree name,
7050 tree ARG_UNUSED (args),
7051 int ARG_UNUSED (flags),
7052 bool *no_add_attrs)
7053 {
7054 tree node = *pnode;
7055
7056 if (VAR_OR_FUNCTION_DECL_P (node))
7057 {
7058 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7059 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7060 {
7061 warning (OPT_Wattributes,
7062 "%qE attribute have effect only on public objects", name);
7063 *no_add_attrs = true;
7064 }
7065 }
7066 else
7067 {
7068 warning (OPT_Wattributes, "%qE attribute ignored", name);
7069 *no_add_attrs = true;
7070 }
7071
7072 return NULL_TREE;
7073 }
7074
7075 /* Handle the "no_reorder" attribute. Arguments as in
7076 struct attribute_spec.handler. */
7077
7078 static tree
7079 handle_no_reorder_attribute (tree *pnode,
7080 tree name,
7081 tree,
7082 int,
7083 bool *no_add_attrs)
7084 {
7085 tree node = *pnode;
7086
7087 if (!VAR_OR_FUNCTION_DECL_P (node)
7088 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7089 {
7090 warning (OPT_Wattributes,
7091 "%qE attribute only affects top level objects",
7092 name);
7093 *no_add_attrs = true;
7094 }
7095
7096 return NULL_TREE;
7097 }
7098
7099 /* Handle a "const" attribute; arguments as in
7100 struct attribute_spec.handler. */
7101
7102 static tree
7103 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7104 int ARG_UNUSED (flags), bool *no_add_attrs)
7105 {
7106 tree type = TREE_TYPE (*node);
7107
7108 /* See FIXME comment on noreturn in c_common_attribute_table. */
7109 if (TREE_CODE (*node) == FUNCTION_DECL)
7110 TREE_READONLY (*node) = 1;
7111 else if (TREE_CODE (type) == POINTER_TYPE
7112 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7113 TREE_TYPE (*node)
7114 = (build_qualified_type
7115 (build_pointer_type
7116 (build_type_variant (TREE_TYPE (type), 1,
7117 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7118 TYPE_QUALS (type)));
7119 else
7120 {
7121 warning (OPT_Wattributes, "%qE attribute ignored", name);
7122 *no_add_attrs = true;
7123 }
7124
7125 return NULL_TREE;
7126 }
7127
7128 /* Handle a "scalar_storage_order" attribute; arguments as in
7129 struct attribute_spec.handler. */
7130
7131 static tree
7132 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7133 int flags, bool *no_add_attrs)
7134 {
7135 tree id = TREE_VALUE (args);
7136 tree type;
7137
7138 if (TREE_CODE (*node) == TYPE_DECL
7139 && ! (flags & ATTR_FLAG_CXX11))
7140 node = &TREE_TYPE (*node);
7141 type = *node;
7142
7143 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7144 {
7145 error ("scalar_storage_order is not supported because endianness "
7146 "is not uniform");
7147 return NULL_TREE;
7148 }
7149
7150 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7151 {
7152 bool reverse = false;
7153
7154 if (TREE_CODE (id) == STRING_CST
7155 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7156 reverse = !BYTES_BIG_ENDIAN;
7157 else if (TREE_CODE (id) == STRING_CST
7158 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7159 reverse = BYTES_BIG_ENDIAN;
7160 else
7161 {
7162 error ("scalar_storage_order argument must be one of \"big-endian\""
7163 " or \"little-endian\"");
7164 return NULL_TREE;
7165 }
7166
7167 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7168 {
7169 if (reverse)
7170 /* A type variant isn't good enough, since we don't want a cast
7171 to such a type to be removed as a no-op. */
7172 *node = type = build_duplicate_type (type);
7173 }
7174
7175 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7176 return NULL_TREE;
7177 }
7178
7179 warning (OPT_Wattributes, "%qE attribute ignored", name);
7180 *no_add_attrs = true;
7181 return NULL_TREE;
7182 }
7183
7184 /* Handle a "transparent_union" attribute; arguments as in
7185 struct attribute_spec.handler. */
7186
7187 static tree
7188 handle_transparent_union_attribute (tree *node, tree name,
7189 tree ARG_UNUSED (args), int flags,
7190 bool *no_add_attrs)
7191 {
7192 tree type;
7193
7194 *no_add_attrs = true;
7195
7196 if (TREE_CODE (*node) == TYPE_DECL
7197 && ! (flags & ATTR_FLAG_CXX11))
7198 node = &TREE_TYPE (*node);
7199 type = *node;
7200
7201 if (TREE_CODE (type) == UNION_TYPE)
7202 {
7203 /* Make sure that the first field will work for a transparent union.
7204 If the type isn't complete yet, leave the check to the code in
7205 finish_struct. */
7206 if (TYPE_SIZE (type))
7207 {
7208 tree first = first_field (type);
7209 if (first == NULL_TREE
7210 || DECL_ARTIFICIAL (first)
7211 || TYPE_MODE (type) != DECL_MODE (first))
7212 goto ignored;
7213 }
7214
7215 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7216 {
7217 /* If the type isn't complete yet, setting the flag
7218 on a variant wouldn't ever be checked. */
7219 if (!TYPE_SIZE (type))
7220 goto ignored;
7221
7222 /* build_duplicate_type doesn't work for C++. */
7223 if (c_dialect_cxx ())
7224 goto ignored;
7225
7226 /* A type variant isn't good enough, since we don't want a cast
7227 to such a type to be removed as a no-op. */
7228 *node = type = build_duplicate_type (type);
7229 }
7230
7231 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7232 TYPE_TRANSPARENT_AGGR (t) = 1;
7233 return NULL_TREE;
7234 }
7235
7236 ignored:
7237 warning (OPT_Wattributes, "%qE attribute ignored", name);
7238 return NULL_TREE;
7239 }
7240
7241 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7242 get the requested priority for a constructor or destructor,
7243 possibly issuing diagnostics for invalid or reserved
7244 priorities. */
7245
7246 static priority_type
7247 get_priority (tree args, bool is_destructor)
7248 {
7249 HOST_WIDE_INT pri;
7250 tree arg;
7251
7252 if (!args)
7253 return DEFAULT_INIT_PRIORITY;
7254
7255 if (!SUPPORTS_INIT_PRIORITY)
7256 {
7257 if (is_destructor)
7258 error ("destructor priorities are not supported");
7259 else
7260 error ("constructor priorities are not supported");
7261 return DEFAULT_INIT_PRIORITY;
7262 }
7263
7264 arg = TREE_VALUE (args);
7265 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7266 goto invalid;
7267 if (arg == error_mark_node)
7268 return DEFAULT_INIT_PRIORITY;
7269 arg = default_conversion (arg);
7270 if (!tree_fits_shwi_p (arg)
7271 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7272 goto invalid;
7273
7274 pri = tree_to_shwi (arg);
7275 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7276 goto invalid;
7277
7278 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7279 {
7280 if (is_destructor)
7281 warning (0,
7282 "destructor priorities from 0 to %d are reserved "
7283 "for the implementation",
7284 MAX_RESERVED_INIT_PRIORITY);
7285 else
7286 warning (0,
7287 "constructor priorities from 0 to %d are reserved "
7288 "for the implementation",
7289 MAX_RESERVED_INIT_PRIORITY);
7290 }
7291 return pri;
7292
7293 invalid:
7294 if (is_destructor)
7295 error ("destructor priorities must be integers from 0 to %d inclusive",
7296 MAX_INIT_PRIORITY);
7297 else
7298 error ("constructor priorities must be integers from 0 to %d inclusive",
7299 MAX_INIT_PRIORITY);
7300 return DEFAULT_INIT_PRIORITY;
7301 }
7302
7303 /* Handle a "constructor" attribute; arguments as in
7304 struct attribute_spec.handler. */
7305
7306 static tree
7307 handle_constructor_attribute (tree *node, tree name, tree args,
7308 int ARG_UNUSED (flags),
7309 bool *no_add_attrs)
7310 {
7311 tree decl = *node;
7312 tree type = TREE_TYPE (decl);
7313
7314 if (TREE_CODE (decl) == FUNCTION_DECL
7315 && TREE_CODE (type) == FUNCTION_TYPE
7316 && decl_function_context (decl) == 0)
7317 {
7318 priority_type priority;
7319 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7320 priority = get_priority (args, /*is_destructor=*/false);
7321 SET_DECL_INIT_PRIORITY (decl, priority);
7322 TREE_USED (decl) = 1;
7323 }
7324 else
7325 {
7326 warning (OPT_Wattributes, "%qE attribute ignored", name);
7327 *no_add_attrs = true;
7328 }
7329
7330 return NULL_TREE;
7331 }
7332
7333 /* Handle a "destructor" attribute; arguments as in
7334 struct attribute_spec.handler. */
7335
7336 static tree
7337 handle_destructor_attribute (tree *node, tree name, tree args,
7338 int ARG_UNUSED (flags),
7339 bool *no_add_attrs)
7340 {
7341 tree decl = *node;
7342 tree type = TREE_TYPE (decl);
7343
7344 if (TREE_CODE (decl) == FUNCTION_DECL
7345 && TREE_CODE (type) == FUNCTION_TYPE
7346 && decl_function_context (decl) == 0)
7347 {
7348 priority_type priority;
7349 DECL_STATIC_DESTRUCTOR (decl) = 1;
7350 priority = get_priority (args, /*is_destructor=*/true);
7351 SET_DECL_FINI_PRIORITY (decl, priority);
7352 TREE_USED (decl) = 1;
7353 }
7354 else
7355 {
7356 warning (OPT_Wattributes, "%qE attribute ignored", name);
7357 *no_add_attrs = true;
7358 }
7359
7360 return NULL_TREE;
7361 }
7362
7363 /* Nonzero if the mode is a valid vector mode for this architecture.
7364 This returns nonzero even if there is no hardware support for the
7365 vector mode, but we can emulate with narrower modes. */
7366
7367 static int
7368 vector_mode_valid_p (machine_mode mode)
7369 {
7370 enum mode_class mclass = GET_MODE_CLASS (mode);
7371 machine_mode innermode;
7372
7373 /* Doh! What's going on? */
7374 if (mclass != MODE_VECTOR_INT
7375 && mclass != MODE_VECTOR_FLOAT
7376 && mclass != MODE_VECTOR_FRACT
7377 && mclass != MODE_VECTOR_UFRACT
7378 && mclass != MODE_VECTOR_ACCUM
7379 && mclass != MODE_VECTOR_UACCUM)
7380 return 0;
7381
7382 /* Hardware support. Woo hoo! */
7383 if (targetm.vector_mode_supported_p (mode))
7384 return 1;
7385
7386 innermode = GET_MODE_INNER (mode);
7387
7388 /* We should probably return 1 if requesting V4DI and we have no DI,
7389 but we have V2DI, but this is probably very unlikely. */
7390
7391 /* If we have support for the inner mode, we can safely emulate it.
7392 We may not have V2DI, but me can emulate with a pair of DIs. */
7393 return targetm.scalar_mode_supported_p (innermode);
7394 }
7395
7396
7397 /* Handle a "mode" attribute; arguments as in
7398 struct attribute_spec.handler. */
7399
7400 static tree
7401 handle_mode_attribute (tree *node, tree name, tree args,
7402 int ARG_UNUSED (flags), bool *no_add_attrs)
7403 {
7404 tree type = *node;
7405 tree ident = TREE_VALUE (args);
7406
7407 *no_add_attrs = true;
7408
7409 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7410 warning (OPT_Wattributes, "%qE attribute ignored", name);
7411 else
7412 {
7413 int j;
7414 const char *p = IDENTIFIER_POINTER (ident);
7415 int len = strlen (p);
7416 machine_mode mode = VOIDmode;
7417 tree typefm;
7418 bool valid_mode;
7419
7420 if (len > 4 && p[0] == '_' && p[1] == '_'
7421 && p[len - 1] == '_' && p[len - 2] == '_')
7422 {
7423 char *newp = (char *) alloca (len - 1);
7424
7425 strcpy (newp, &p[2]);
7426 newp[len - 4] = '\0';
7427 p = newp;
7428 }
7429
7430 /* Change this type to have a type with the specified mode.
7431 First check for the special modes. */
7432 if (!strcmp (p, "byte"))
7433 mode = byte_mode;
7434 else if (!strcmp (p, "word"))
7435 mode = word_mode;
7436 else if (!strcmp (p, "pointer"))
7437 mode = ptr_mode;
7438 else if (!strcmp (p, "libgcc_cmp_return"))
7439 mode = targetm.libgcc_cmp_return_mode ();
7440 else if (!strcmp (p, "libgcc_shift_count"))
7441 mode = targetm.libgcc_shift_count_mode ();
7442 else if (!strcmp (p, "unwind_word"))
7443 mode = targetm.unwind_word_mode ();
7444 else
7445 for (j = 0; j < NUM_MACHINE_MODES; j++)
7446 if (!strcmp (p, GET_MODE_NAME (j)))
7447 {
7448 mode = (machine_mode) j;
7449 break;
7450 }
7451
7452 if (mode == VOIDmode)
7453 {
7454 error ("unknown machine mode %qE", ident);
7455 return NULL_TREE;
7456 }
7457
7458 valid_mode = false;
7459 switch (GET_MODE_CLASS (mode))
7460 {
7461 case MODE_INT:
7462 case MODE_PARTIAL_INT:
7463 case MODE_FLOAT:
7464 case MODE_DECIMAL_FLOAT:
7465 case MODE_FRACT:
7466 case MODE_UFRACT:
7467 case MODE_ACCUM:
7468 case MODE_UACCUM:
7469 valid_mode = targetm.scalar_mode_supported_p (mode);
7470 break;
7471
7472 case MODE_COMPLEX_INT:
7473 case MODE_COMPLEX_FLOAT:
7474 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7475 break;
7476
7477 case MODE_VECTOR_INT:
7478 case MODE_VECTOR_FLOAT:
7479 case MODE_VECTOR_FRACT:
7480 case MODE_VECTOR_UFRACT:
7481 case MODE_VECTOR_ACCUM:
7482 case MODE_VECTOR_UACCUM:
7483 warning (OPT_Wattributes, "specifying vector types with "
7484 "__attribute__ ((mode)) is deprecated");
7485 warning (OPT_Wattributes,
7486 "use __attribute__ ((vector_size)) instead");
7487 valid_mode = vector_mode_valid_p (mode);
7488 break;
7489
7490 default:
7491 break;
7492 }
7493 if (!valid_mode)
7494 {
7495 error ("unable to emulate %qs", p);
7496 return NULL_TREE;
7497 }
7498
7499 if (POINTER_TYPE_P (type))
7500 {
7501 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7502 tree (*fn)(tree, machine_mode, bool);
7503
7504 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7505 {
7506 error ("invalid pointer mode %qs", p);
7507 return NULL_TREE;
7508 }
7509
7510 if (TREE_CODE (type) == POINTER_TYPE)
7511 fn = build_pointer_type_for_mode;
7512 else
7513 fn = build_reference_type_for_mode;
7514 typefm = fn (TREE_TYPE (type), mode, false);
7515 }
7516 else
7517 {
7518 /* For fixed-point modes, we need to test if the signness of type
7519 and the machine mode are consistent. */
7520 if (ALL_FIXED_POINT_MODE_P (mode)
7521 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7522 {
7523 error ("signedness of type and machine mode %qs don%'t match", p);
7524 return NULL_TREE;
7525 }
7526 /* For fixed-point modes, we need to pass saturating info. */
7527 typefm = lang_hooks.types.type_for_mode (mode,
7528 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7529 : TYPE_UNSIGNED (type));
7530 }
7531
7532 if (typefm == NULL_TREE)
7533 {
7534 error ("no data type for mode %qs", p);
7535 return NULL_TREE;
7536 }
7537 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7538 {
7539 /* For enumeral types, copy the precision from the integer
7540 type returned above. If not an INTEGER_TYPE, we can't use
7541 this mode for this type. */
7542 if (TREE_CODE (typefm) != INTEGER_TYPE)
7543 {
7544 error ("cannot use mode %qs for enumeral types", p);
7545 return NULL_TREE;
7546 }
7547
7548 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7549 {
7550 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7551 typefm = type;
7552 }
7553 else
7554 {
7555 /* We cannot build a type variant, as there's code that assumes
7556 that TYPE_MAIN_VARIANT has the same mode. This includes the
7557 debug generators. Instead, create a subrange type. This
7558 results in all of the enumeral values being emitted only once
7559 in the original, and the subtype gets them by reference. */
7560 if (TYPE_UNSIGNED (type))
7561 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7562 else
7563 typefm = make_signed_type (TYPE_PRECISION (typefm));
7564 TREE_TYPE (typefm) = type;
7565 }
7566 }
7567 else if (VECTOR_MODE_P (mode)
7568 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7569 : TREE_CODE (type) != TREE_CODE (typefm))
7570 {
7571 error ("mode %qs applied to inappropriate type", p);
7572 return NULL_TREE;
7573 }
7574
7575 *node = typefm;
7576 }
7577
7578 return NULL_TREE;
7579 }
7580
7581 /* Handle a "section" attribute; arguments as in
7582 struct attribute_spec.handler. */
7583
7584 static tree
7585 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7586 int ARG_UNUSED (flags), bool *no_add_attrs)
7587 {
7588 tree decl = *node;
7589
7590 if (!targetm_common.have_named_sections)
7591 {
7592 error_at (DECL_SOURCE_LOCATION (*node),
7593 "section attributes are not supported for this target");
7594 goto fail;
7595 }
7596
7597 user_defined_section_attribute = true;
7598
7599 if (!VAR_OR_FUNCTION_DECL_P (decl))
7600 {
7601 error ("section attribute not allowed for %q+D", *node);
7602 goto fail;
7603 }
7604
7605 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7606 {
7607 error ("section attribute argument not a string constant");
7608 goto fail;
7609 }
7610
7611 if (VAR_P (decl)
7612 && current_function_decl != NULL_TREE
7613 && !TREE_STATIC (decl))
7614 {
7615 error_at (DECL_SOURCE_LOCATION (decl),
7616 "section attribute cannot be specified for local variables");
7617 goto fail;
7618 }
7619
7620 /* The decl may have already been given a section attribute
7621 from a previous declaration. Ensure they match. */
7622 if (DECL_SECTION_NAME (decl) != NULL
7623 && strcmp (DECL_SECTION_NAME (decl),
7624 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7625 {
7626 error ("section of %q+D conflicts with previous declaration", *node);
7627 goto fail;
7628 }
7629
7630 if (VAR_P (decl)
7631 && !targetm.have_tls && targetm.emutls.tmpl_section
7632 && DECL_THREAD_LOCAL_P (decl))
7633 {
7634 error ("section of %q+D cannot be overridden", *node);
7635 goto fail;
7636 }
7637
7638 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7639 return NULL_TREE;
7640
7641 fail:
7642 *no_add_attrs = true;
7643 return NULL_TREE;
7644 }
7645
7646 /* Check whether ALIGN is a valid user-specified alignment. If so,
7647 return its base-2 log; if not, output an error and return -1. If
7648 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7649 no error. */
7650 int
7651 check_user_alignment (const_tree align, bool allow_zero)
7652 {
7653 int i;
7654
7655 if (error_operand_p (align))
7656 return -1;
7657 if (TREE_CODE (align) != INTEGER_CST
7658 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7659 {
7660 error ("requested alignment is not an integer constant");
7661 return -1;
7662 }
7663 else if (allow_zero && integer_zerop (align))
7664 return -1;
7665 else if (tree_int_cst_sgn (align) == -1
7666 || (i = tree_log2 (align)) == -1)
7667 {
7668 error ("requested alignment is not a positive power of 2");
7669 return -1;
7670 }
7671 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7672 {
7673 error ("requested alignment is too large");
7674 return -1;
7675 }
7676 return i;
7677 }
7678
7679 /*
7680 If in c++-11, check if the c++-11 alignment constraint with respect
7681 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7682 c++-11 mode, does nothing.
7683
7684 [dcl.align]2/ says:
7685
7686 [* if the constant expression evaluates to a fundamental alignment,
7687 the alignment requirement of the declared entity shall be the
7688 specified fundamental alignment.
7689
7690 * if the constant expression evaluates to an extended alignment
7691 and the implementation supports that alignment in the context
7692 of the declaration, the alignment of the declared entity shall
7693 be that alignment
7694
7695 * if the constant expression evaluates to an extended alignment
7696 and the implementation does not support that alignment in the
7697 context of the declaration, the program is ill-formed]. */
7698
7699 static bool
7700 check_cxx_fundamental_alignment_constraints (tree node,
7701 unsigned align_log,
7702 int flags)
7703 {
7704 bool alignment_too_large_p = false;
7705 unsigned requested_alignment = 1U << align_log;
7706 unsigned max_align = 0;
7707
7708 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7709 || (node == NULL_TREE || node == error_mark_node))
7710 return true;
7711
7712 if (cxx_fundamental_alignment_p (requested_alignment))
7713 return true;
7714
7715 if (DECL_P (node))
7716 {
7717 if (TREE_STATIC (node))
7718 {
7719 /* For file scope variables and static members, the target
7720 supports alignments that are at most
7721 MAX_OFILE_ALIGNMENT. */
7722 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7723 alignment_too_large_p = true;
7724 }
7725 else
7726 {
7727 #ifdef BIGGEST_FIELD_ALIGNMENT
7728 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7729 #else
7730 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7731 #endif
7732 /* For non-static members, the target supports either
7733 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7734 if it is defined or BIGGEST_ALIGNMENT. */
7735 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7736 if (TREE_CODE (node) == FIELD_DECL
7737 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7738 alignment_too_large_p = true;
7739 #undef MAX_TARGET_FIELD_ALIGNMENT
7740 /* For stack variables, the target supports at most
7741 MAX_STACK_ALIGNMENT. */
7742 else if (decl_function_context (node) != NULL
7743 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7744 alignment_too_large_p = true;
7745 }
7746 }
7747 else if (TYPE_P (node))
7748 {
7749 /* Let's be liberal for types. */
7750 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7751 alignment_too_large_p = true;
7752 }
7753
7754 if (alignment_too_large_p)
7755 pedwarn (input_location, OPT_Wattributes,
7756 "requested alignment %d is larger than %d",
7757 requested_alignment, max_align);
7758
7759 return !alignment_too_large_p;
7760 }
7761
7762 /* Handle a "aligned" attribute; arguments as in
7763 struct attribute_spec.handler. */
7764
7765 static tree
7766 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7767 int flags, bool *no_add_attrs)
7768 {
7769 tree decl = NULL_TREE;
7770 tree *type = NULL;
7771 int is_type = 0;
7772 tree align_expr;
7773 int i;
7774
7775 if (args)
7776 {
7777 align_expr = TREE_VALUE (args);
7778 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7779 && TREE_CODE (align_expr) != FUNCTION_DECL)
7780 align_expr = default_conversion (align_expr);
7781 }
7782 else
7783 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7784
7785 if (DECL_P (*node))
7786 {
7787 decl = *node;
7788 type = &TREE_TYPE (decl);
7789 is_type = TREE_CODE (*node) == TYPE_DECL;
7790 }
7791 else if (TYPE_P (*node))
7792 type = node, is_type = 1;
7793
7794 if ((i = check_user_alignment (align_expr, false)) == -1
7795 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7796 *no_add_attrs = true;
7797 else if (is_type)
7798 {
7799 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7800 /* OK, modify the type in place. */;
7801 /* If we have a TYPE_DECL, then copy the type, so that we
7802 don't accidentally modify a builtin type. See pushdecl. */
7803 else if (decl && TREE_TYPE (decl) != error_mark_node
7804 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7805 {
7806 tree tt = TREE_TYPE (decl);
7807 *type = build_variant_type_copy (*type);
7808 DECL_ORIGINAL_TYPE (decl) = tt;
7809 TYPE_NAME (*type) = decl;
7810 TREE_USED (*type) = TREE_USED (decl);
7811 TREE_TYPE (decl) = *type;
7812 }
7813 else
7814 *type = build_variant_type_copy (*type);
7815
7816 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7817 TYPE_USER_ALIGN (*type) = 1;
7818 }
7819 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7820 && TREE_CODE (decl) != FIELD_DECL)
7821 {
7822 error ("alignment may not be specified for %q+D", decl);
7823 *no_add_attrs = true;
7824 }
7825 else if (DECL_USER_ALIGN (decl)
7826 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7827 /* C++-11 [dcl.align/4]:
7828
7829 When multiple alignment-specifiers are specified for an
7830 entity, the alignment requirement shall be set to the
7831 strictest specified alignment.
7832
7833 This formally comes from the c++11 specification but we are
7834 doing it for the GNU attribute syntax as well. */
7835 *no_add_attrs = true;
7836 else if (TREE_CODE (decl) == FUNCTION_DECL
7837 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7838 {
7839 if (DECL_USER_ALIGN (decl))
7840 error ("alignment for %q+D was previously specified as %d "
7841 "and may not be decreased", decl,
7842 DECL_ALIGN (decl) / BITS_PER_UNIT);
7843 else
7844 error ("alignment for %q+D must be at least %d", decl,
7845 DECL_ALIGN (decl) / BITS_PER_UNIT);
7846 *no_add_attrs = true;
7847 }
7848 else
7849 {
7850 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7851 DECL_USER_ALIGN (decl) = 1;
7852 }
7853
7854 return NULL_TREE;
7855 }
7856
7857 /* Handle a "weak" attribute; arguments as in
7858 struct attribute_spec.handler. */
7859
7860 static tree
7861 handle_weak_attribute (tree *node, tree name,
7862 tree ARG_UNUSED (args),
7863 int ARG_UNUSED (flags),
7864 bool * ARG_UNUSED (no_add_attrs))
7865 {
7866 if (TREE_CODE (*node) == FUNCTION_DECL
7867 && DECL_DECLARED_INLINE_P (*node))
7868 {
7869 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7870 *no_add_attrs = true;
7871 }
7872 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7873 {
7874 error ("indirect function %q+D cannot be declared weak", *node);
7875 *no_add_attrs = true;
7876 return NULL_TREE;
7877 }
7878 else if (VAR_OR_FUNCTION_DECL_P (*node))
7879 declare_weak (*node);
7880 else
7881 warning (OPT_Wattributes, "%qE attribute ignored", name);
7882
7883 return NULL_TREE;
7884 }
7885
7886 /* Handle a "noplt" attribute; arguments as in
7887 struct attribute_spec.handler. */
7888
7889 static tree
7890 handle_noplt_attribute (tree *node, tree name,
7891 tree ARG_UNUSED (args),
7892 int ARG_UNUSED (flags),
7893 bool * ARG_UNUSED (no_add_attrs))
7894 {
7895 if (TREE_CODE (*node) != FUNCTION_DECL)
7896 {
7897 warning (OPT_Wattributes,
7898 "%qE attribute is only applicable on functions", name);
7899 *no_add_attrs = true;
7900 return NULL_TREE;
7901 }
7902 return NULL_TREE;
7903 }
7904
7905 /* Handle an "alias" or "ifunc" attribute; arguments as in
7906 struct attribute_spec.handler, except that IS_ALIAS tells us
7907 whether this is an alias as opposed to ifunc attribute. */
7908
7909 static tree
7910 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7911 bool *no_add_attrs)
7912 {
7913 tree decl = *node;
7914
7915 if (TREE_CODE (decl) != FUNCTION_DECL
7916 && (!is_alias || !VAR_P (decl)))
7917 {
7918 warning (OPT_Wattributes, "%qE attribute ignored", name);
7919 *no_add_attrs = true;
7920 }
7921 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7922 || (TREE_CODE (decl) != FUNCTION_DECL
7923 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7924 /* A static variable declaration is always a tentative definition,
7925 but the alias is a non-tentative definition which overrides. */
7926 || (TREE_CODE (decl) != FUNCTION_DECL
7927 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7928 {
7929 error ("%q+D defined both normally and as %qE attribute", decl, name);
7930 *no_add_attrs = true;
7931 return NULL_TREE;
7932 }
7933 else if (!is_alias
7934 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7935 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7936 {
7937 error ("weak %q+D cannot be defined %qE", decl, name);
7938 *no_add_attrs = true;
7939 return NULL_TREE;
7940 }
7941
7942 /* Note that the very first time we process a nested declaration,
7943 decl_function_context will not be set. Indeed, *would* never
7944 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7945 we do below. After such frobbery, pushdecl would set the context.
7946 In any case, this is never what we want. */
7947 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7948 {
7949 tree id;
7950
7951 id = TREE_VALUE (args);
7952 if (TREE_CODE (id) != STRING_CST)
7953 {
7954 error ("attribute %qE argument not a string", name);
7955 *no_add_attrs = true;
7956 return NULL_TREE;
7957 }
7958 id = get_identifier (TREE_STRING_POINTER (id));
7959 /* This counts as a use of the object pointed to. */
7960 TREE_USED (id) = 1;
7961
7962 if (TREE_CODE (decl) == FUNCTION_DECL)
7963 DECL_INITIAL (decl) = error_mark_node;
7964 else
7965 TREE_STATIC (decl) = 1;
7966
7967 if (!is_alias)
7968 /* ifuncs are also aliases, so set that attribute too. */
7969 DECL_ATTRIBUTES (decl)
7970 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7971 }
7972 else
7973 {
7974 warning (OPT_Wattributes, "%qE attribute ignored", name);
7975 *no_add_attrs = true;
7976 }
7977
7978 if (decl_in_symtab_p (*node))
7979 {
7980 struct symtab_node *n = symtab_node::get (decl);
7981 if (n && n->refuse_visibility_changes)
7982 {
7983 if (is_alias)
7984 error ("%+D declared alias after being used", decl);
7985 else
7986 error ("%+D declared ifunc after being used", decl);
7987 }
7988 }
7989
7990
7991 return NULL_TREE;
7992 }
7993
7994 /* Handle an "alias" or "ifunc" attribute; arguments as in
7995 struct attribute_spec.handler. */
7996
7997 static tree
7998 handle_ifunc_attribute (tree *node, tree name, tree args,
7999 int ARG_UNUSED (flags), bool *no_add_attrs)
8000 {
8001 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8002 }
8003
8004 /* Handle an "alias" or "ifunc" attribute; arguments as in
8005 struct attribute_spec.handler. */
8006
8007 static tree
8008 handle_alias_attribute (tree *node, tree name, tree args,
8009 int ARG_UNUSED (flags), bool *no_add_attrs)
8010 {
8011 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8012 }
8013
8014 /* Handle a "weakref" attribute; arguments as in struct
8015 attribute_spec.handler. */
8016
8017 static tree
8018 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8019 int flags, bool *no_add_attrs)
8020 {
8021 tree attr = NULL_TREE;
8022
8023 /* We must ignore the attribute when it is associated with
8024 local-scoped decls, since attribute alias is ignored and many
8025 such symbols do not even have a DECL_WEAK field. */
8026 if (decl_function_context (*node)
8027 || current_function_decl
8028 || !VAR_OR_FUNCTION_DECL_P (*node))
8029 {
8030 warning (OPT_Wattributes, "%qE attribute ignored", name);
8031 *no_add_attrs = true;
8032 return NULL_TREE;
8033 }
8034
8035 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8036 {
8037 error ("indirect function %q+D cannot be declared weakref", *node);
8038 *no_add_attrs = true;
8039 return NULL_TREE;
8040 }
8041
8042 /* The idea here is that `weakref("name")' mutates into `weakref,
8043 alias("name")', and weakref without arguments, in turn,
8044 implicitly adds weak. */
8045
8046 if (args)
8047 {
8048 attr = tree_cons (get_identifier ("alias"), args, attr);
8049 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8050
8051 *no_add_attrs = true;
8052
8053 decl_attributes (node, attr, flags);
8054 }
8055 else
8056 {
8057 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8058 error_at (DECL_SOURCE_LOCATION (*node),
8059 "weakref attribute must appear before alias attribute");
8060
8061 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8062 and that isn't supported; and because it wants to add it to
8063 the list of weak decls, which isn't helpful. */
8064 DECL_WEAK (*node) = 1;
8065 }
8066
8067 if (decl_in_symtab_p (*node))
8068 {
8069 struct symtab_node *n = symtab_node::get (*node);
8070 if (n && n->refuse_visibility_changes)
8071 error ("%+D declared weakref after being used", *node);
8072 }
8073
8074 return NULL_TREE;
8075 }
8076
8077 /* Handle an "visibility" attribute; arguments as in
8078 struct attribute_spec.handler. */
8079
8080 static tree
8081 handle_visibility_attribute (tree *node, tree name, tree args,
8082 int ARG_UNUSED (flags),
8083 bool *ARG_UNUSED (no_add_attrs))
8084 {
8085 tree decl = *node;
8086 tree id = TREE_VALUE (args);
8087 enum symbol_visibility vis;
8088
8089 if (TYPE_P (*node))
8090 {
8091 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8092 /* OK */;
8093 else if (!RECORD_OR_UNION_TYPE_P (*node))
8094 {
8095 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8096 name);
8097 return NULL_TREE;
8098 }
8099 else if (TYPE_FIELDS (*node))
8100 {
8101 error ("%qE attribute ignored because %qT is already defined",
8102 name, *node);
8103 return NULL_TREE;
8104 }
8105 }
8106 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8107 {
8108 warning (OPT_Wattributes, "%qE attribute ignored", name);
8109 return NULL_TREE;
8110 }
8111
8112 if (TREE_CODE (id) != STRING_CST)
8113 {
8114 error ("visibility argument not a string");
8115 return NULL_TREE;
8116 }
8117
8118 /* If this is a type, set the visibility on the type decl. */
8119 if (TYPE_P (decl))
8120 {
8121 decl = TYPE_NAME (decl);
8122 if (!decl)
8123 return NULL_TREE;
8124 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8125 {
8126 warning (OPT_Wattributes, "%qE attribute ignored on types",
8127 name);
8128 return NULL_TREE;
8129 }
8130 }
8131
8132 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8133 vis = VISIBILITY_DEFAULT;
8134 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8135 vis = VISIBILITY_INTERNAL;
8136 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8137 vis = VISIBILITY_HIDDEN;
8138 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8139 vis = VISIBILITY_PROTECTED;
8140 else
8141 {
8142 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8143 vis = VISIBILITY_DEFAULT;
8144 }
8145
8146 if (DECL_VISIBILITY_SPECIFIED (decl)
8147 && vis != DECL_VISIBILITY (decl))
8148 {
8149 tree attributes = (TYPE_P (*node)
8150 ? TYPE_ATTRIBUTES (*node)
8151 : DECL_ATTRIBUTES (decl));
8152 if (lookup_attribute ("visibility", attributes))
8153 error ("%qD redeclared with different visibility", decl);
8154 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8155 && lookup_attribute ("dllimport", attributes))
8156 error ("%qD was declared %qs which implies default visibility",
8157 decl, "dllimport");
8158 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8159 && lookup_attribute ("dllexport", attributes))
8160 error ("%qD was declared %qs which implies default visibility",
8161 decl, "dllexport");
8162 }
8163
8164 DECL_VISIBILITY (decl) = vis;
8165 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8166
8167 /* Go ahead and attach the attribute to the node as well. This is needed
8168 so we can determine whether we have VISIBILITY_DEFAULT because the
8169 visibility was not specified, or because it was explicitly overridden
8170 from the containing scope. */
8171
8172 return NULL_TREE;
8173 }
8174
8175 /* Determine the ELF symbol visibility for DECL, which is either a
8176 variable or a function. It is an error to use this function if a
8177 definition of DECL is not available in this translation unit.
8178 Returns true if the final visibility has been determined by this
8179 function; false if the caller is free to make additional
8180 modifications. */
8181
8182 bool
8183 c_determine_visibility (tree decl)
8184 {
8185 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8186
8187 /* If the user explicitly specified the visibility with an
8188 attribute, honor that. DECL_VISIBILITY will have been set during
8189 the processing of the attribute. We check for an explicit
8190 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8191 to distinguish the use of an attribute from the use of a "#pragma
8192 GCC visibility push(...)"; in the latter case we still want other
8193 considerations to be able to overrule the #pragma. */
8194 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8195 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8196 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8197 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8198 return true;
8199
8200 /* Set default visibility to whatever the user supplied with
8201 visibility_specified depending on #pragma GCC visibility. */
8202 if (!DECL_VISIBILITY_SPECIFIED (decl))
8203 {
8204 if (visibility_options.inpragma
8205 || DECL_VISIBILITY (decl) != default_visibility)
8206 {
8207 DECL_VISIBILITY (decl) = default_visibility;
8208 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8209 /* If visibility changed and DECL already has DECL_RTL, ensure
8210 symbol flags are updated. */
8211 if (((VAR_P (decl) && TREE_STATIC (decl))
8212 || TREE_CODE (decl) == FUNCTION_DECL)
8213 && DECL_RTL_SET_P (decl))
8214 make_decl_rtl (decl);
8215 }
8216 }
8217 return false;
8218 }
8219
8220 /* Handle an "tls_model" attribute; arguments as in
8221 struct attribute_spec.handler. */
8222
8223 static tree
8224 handle_tls_model_attribute (tree *node, tree name, tree args,
8225 int ARG_UNUSED (flags), bool *no_add_attrs)
8226 {
8227 tree id;
8228 tree decl = *node;
8229 enum tls_model kind;
8230
8231 *no_add_attrs = true;
8232
8233 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
8234 {
8235 warning (OPT_Wattributes, "%qE attribute ignored", name);
8236 return NULL_TREE;
8237 }
8238
8239 kind = DECL_TLS_MODEL (decl);
8240 id = TREE_VALUE (args);
8241 if (TREE_CODE (id) != STRING_CST)
8242 {
8243 error ("tls_model argument not a string");
8244 return NULL_TREE;
8245 }
8246
8247 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8248 kind = TLS_MODEL_LOCAL_EXEC;
8249 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8250 kind = TLS_MODEL_INITIAL_EXEC;
8251 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8252 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8253 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8254 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8255 else
8256 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8257
8258 set_decl_tls_model (decl, kind);
8259 return NULL_TREE;
8260 }
8261
8262 /* Handle a "no_instrument_function" attribute; arguments as in
8263 struct attribute_spec.handler. */
8264
8265 static tree
8266 handle_no_instrument_function_attribute (tree *node, tree name,
8267 tree ARG_UNUSED (args),
8268 int ARG_UNUSED (flags),
8269 bool *no_add_attrs)
8270 {
8271 tree decl = *node;
8272
8273 if (TREE_CODE (decl) != FUNCTION_DECL)
8274 {
8275 error_at (DECL_SOURCE_LOCATION (decl),
8276 "%qE attribute applies only to functions", name);
8277 *no_add_attrs = true;
8278 }
8279 else
8280 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8281
8282 return NULL_TREE;
8283 }
8284
8285 /* Handle a "malloc" attribute; arguments as in
8286 struct attribute_spec.handler. */
8287
8288 static tree
8289 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8290 int ARG_UNUSED (flags), bool *no_add_attrs)
8291 {
8292 if (TREE_CODE (*node) == FUNCTION_DECL
8293 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8294 DECL_IS_MALLOC (*node) = 1;
8295 else
8296 {
8297 warning (OPT_Wattributes, "%qE attribute ignored", name);
8298 *no_add_attrs = true;
8299 }
8300
8301 return NULL_TREE;
8302 }
8303
8304 /* Handle a "alloc_size" attribute; arguments as in
8305 struct attribute_spec.handler. */
8306
8307 static tree
8308 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8309 int ARG_UNUSED (flags), bool *no_add_attrs)
8310 {
8311 unsigned arg_count = type_num_arguments (*node);
8312 for (; args; args = TREE_CHAIN (args))
8313 {
8314 tree position = TREE_VALUE (args);
8315 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8316 && TREE_CODE (position) != FUNCTION_DECL)
8317 position = default_conversion (position);
8318
8319 if (!tree_fits_uhwi_p (position)
8320 || !arg_count
8321 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8322 {
8323 warning (OPT_Wattributes,
8324 "alloc_size parameter outside range");
8325 *no_add_attrs = true;
8326 return NULL_TREE;
8327 }
8328 }
8329 return NULL_TREE;
8330 }
8331
8332 /* Handle a "alloc_align" attribute; arguments as in
8333 struct attribute_spec.handler. */
8334
8335 static tree
8336 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8337 bool *no_add_attrs)
8338 {
8339 unsigned arg_count = type_num_arguments (*node);
8340 tree position = TREE_VALUE (args);
8341 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8342 position = default_conversion (position);
8343
8344 if (!tree_fits_uhwi_p (position)
8345 || !arg_count
8346 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8347 {
8348 warning (OPT_Wattributes,
8349 "alloc_align parameter outside range");
8350 *no_add_attrs = true;
8351 return NULL_TREE;
8352 }
8353 return NULL_TREE;
8354 }
8355
8356 /* Handle a "assume_aligned" attribute; arguments as in
8357 struct attribute_spec.handler. */
8358
8359 static tree
8360 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8361 bool *no_add_attrs)
8362 {
8363 for (; args; args = TREE_CHAIN (args))
8364 {
8365 tree position = TREE_VALUE (args);
8366 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8367 && TREE_CODE (position) != FUNCTION_DECL)
8368 position = default_conversion (position);
8369
8370 if (TREE_CODE (position) != INTEGER_CST)
8371 {
8372 warning (OPT_Wattributes,
8373 "assume_aligned parameter not integer constant");
8374 *no_add_attrs = true;
8375 return NULL_TREE;
8376 }
8377 }
8378 return NULL_TREE;
8379 }
8380
8381 /* Handle a "fn spec" attribute; arguments as in
8382 struct attribute_spec.handler. */
8383
8384 static tree
8385 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8386 tree args, int ARG_UNUSED (flags),
8387 bool *no_add_attrs ATTRIBUTE_UNUSED)
8388 {
8389 gcc_assert (args
8390 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8391 && !TREE_CHAIN (args));
8392 return NULL_TREE;
8393 }
8394
8395 /* Handle a "bnd_variable_size" attribute; arguments as in
8396 struct attribute_spec.handler. */
8397
8398 static tree
8399 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8400 int ARG_UNUSED (flags), bool *no_add_attrs)
8401 {
8402 if (TREE_CODE (*node) != FIELD_DECL)
8403 {
8404 warning (OPT_Wattributes, "%qE attribute ignored", name);
8405 *no_add_attrs = true;
8406 }
8407
8408 return NULL_TREE;
8409 }
8410
8411 /* Handle a "bnd_legacy" attribute; arguments as in
8412 struct attribute_spec.handler. */
8413
8414 static tree
8415 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8416 int ARG_UNUSED (flags), bool *no_add_attrs)
8417 {
8418 if (TREE_CODE (*node) != FUNCTION_DECL)
8419 {
8420 warning (OPT_Wattributes, "%qE attribute ignored", name);
8421 *no_add_attrs = true;
8422 }
8423
8424 return NULL_TREE;
8425 }
8426
8427 /* Handle a "bnd_instrument" attribute; arguments as in
8428 struct attribute_spec.handler. */
8429
8430 static tree
8431 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8432 int ARG_UNUSED (flags), bool *no_add_attrs)
8433 {
8434 if (TREE_CODE (*node) != FUNCTION_DECL)
8435 {
8436 warning (OPT_Wattributes, "%qE attribute ignored", name);
8437 *no_add_attrs = true;
8438 }
8439
8440 return NULL_TREE;
8441 }
8442
8443 /* Handle a "warn_unused" attribute; arguments as in
8444 struct attribute_spec.handler. */
8445
8446 static tree
8447 handle_warn_unused_attribute (tree *node, tree name,
8448 tree args ATTRIBUTE_UNUSED,
8449 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8450 {
8451 if (TYPE_P (*node))
8452 /* Do nothing else, just set the attribute. We'll get at
8453 it later with lookup_attribute. */
8454 ;
8455 else
8456 {
8457 warning (OPT_Wattributes, "%qE attribute ignored", name);
8458 *no_add_attrs = true;
8459 }
8460
8461 return NULL_TREE;
8462 }
8463
8464 /* Handle an "omp declare simd" attribute; arguments as in
8465 struct attribute_spec.handler. */
8466
8467 static tree
8468 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8469 {
8470 return NULL_TREE;
8471 }
8472
8473 /* Handle a "simd" attribute. */
8474
8475 static tree
8476 handle_simd_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
8477 {
8478 if (TREE_CODE (*node) == FUNCTION_DECL)
8479 {
8480 if (lookup_attribute ("cilk simd function",
8481 DECL_ATTRIBUTES (*node)) != NULL)
8482 {
8483 error_at (DECL_SOURCE_LOCATION (*node),
8484 "%<__simd__%> attribute cannot be used in the same "
8485 "function marked as a Cilk Plus SIMD-enabled function");
8486 *no_add_attrs = true;
8487 }
8488 else
8489 DECL_ATTRIBUTES (*node)
8490 = tree_cons (get_identifier ("omp declare simd"),
8491 NULL_TREE, DECL_ATTRIBUTES (*node));
8492 }
8493 else
8494 {
8495 warning (OPT_Wattributes, "%qE attribute ignored", name);
8496 *no_add_attrs = true;
8497 }
8498
8499 return NULL_TREE;
8500 }
8501
8502 /* Handle an "omp declare target" attribute; arguments as in
8503 struct attribute_spec.handler. */
8504
8505 static tree
8506 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8507 {
8508 return NULL_TREE;
8509 }
8510
8511 /* Handle a "returns_twice" attribute; arguments as in
8512 struct attribute_spec.handler. */
8513
8514 static tree
8515 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8516 int ARG_UNUSED (flags), bool *no_add_attrs)
8517 {
8518 if (TREE_CODE (*node) == FUNCTION_DECL)
8519 DECL_IS_RETURNS_TWICE (*node) = 1;
8520 else
8521 {
8522 warning (OPT_Wattributes, "%qE attribute ignored", name);
8523 *no_add_attrs = true;
8524 }
8525
8526 return NULL_TREE;
8527 }
8528
8529 /* Handle a "no_limit_stack" attribute; arguments as in
8530 struct attribute_spec.handler. */
8531
8532 static tree
8533 handle_no_limit_stack_attribute (tree *node, tree name,
8534 tree ARG_UNUSED (args),
8535 int ARG_UNUSED (flags),
8536 bool *no_add_attrs)
8537 {
8538 tree decl = *node;
8539
8540 if (TREE_CODE (decl) != FUNCTION_DECL)
8541 {
8542 error_at (DECL_SOURCE_LOCATION (decl),
8543 "%qE attribute applies only to functions", name);
8544 *no_add_attrs = true;
8545 }
8546 else if (DECL_INITIAL (decl))
8547 {
8548 error_at (DECL_SOURCE_LOCATION (decl),
8549 "can%'t set %qE attribute after definition", name);
8550 *no_add_attrs = true;
8551 }
8552 else
8553 DECL_NO_LIMIT_STACK (decl) = 1;
8554
8555 return NULL_TREE;
8556 }
8557
8558 /* Handle a "pure" attribute; arguments as in
8559 struct attribute_spec.handler. */
8560
8561 static tree
8562 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8563 int ARG_UNUSED (flags), bool *no_add_attrs)
8564 {
8565 if (TREE_CODE (*node) == FUNCTION_DECL)
8566 DECL_PURE_P (*node) = 1;
8567 /* ??? TODO: Support types. */
8568 else
8569 {
8570 warning (OPT_Wattributes, "%qE attribute ignored", name);
8571 *no_add_attrs = true;
8572 }
8573
8574 return NULL_TREE;
8575 }
8576
8577 /* Digest an attribute list destined for a transactional memory statement.
8578 ALLOWED is the set of attributes that are allowed for this statement;
8579 return the attribute we parsed. Multiple attributes are never allowed. */
8580
8581 int
8582 parse_tm_stmt_attr (tree attrs, int allowed)
8583 {
8584 tree a_seen = NULL;
8585 int m_seen = 0;
8586
8587 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8588 {
8589 tree a = TREE_PURPOSE (attrs);
8590 int m = 0;
8591
8592 if (is_attribute_p ("outer", a))
8593 m = TM_STMT_ATTR_OUTER;
8594
8595 if ((m & allowed) == 0)
8596 {
8597 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8598 continue;
8599 }
8600
8601 if (m_seen == 0)
8602 {
8603 a_seen = a;
8604 m_seen = m;
8605 }
8606 else if (m_seen == m)
8607 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8608 else
8609 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8610 }
8611
8612 return m_seen;
8613 }
8614
8615 /* Transform a TM attribute name into a maskable integer and back.
8616 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8617 to how the lack of an attribute is treated. */
8618
8619 int
8620 tm_attr_to_mask (tree attr)
8621 {
8622 if (attr == NULL)
8623 return 0;
8624 if (is_attribute_p ("transaction_safe", attr))
8625 return TM_ATTR_SAFE;
8626 if (is_attribute_p ("transaction_callable", attr))
8627 return TM_ATTR_CALLABLE;
8628 if (is_attribute_p ("transaction_pure", attr))
8629 return TM_ATTR_PURE;
8630 if (is_attribute_p ("transaction_unsafe", attr))
8631 return TM_ATTR_IRREVOCABLE;
8632 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8633 return TM_ATTR_MAY_CANCEL_OUTER;
8634 return 0;
8635 }
8636
8637 tree
8638 tm_mask_to_attr (int mask)
8639 {
8640 const char *str;
8641 switch (mask)
8642 {
8643 case TM_ATTR_SAFE:
8644 str = "transaction_safe";
8645 break;
8646 case TM_ATTR_CALLABLE:
8647 str = "transaction_callable";
8648 break;
8649 case TM_ATTR_PURE:
8650 str = "transaction_pure";
8651 break;
8652 case TM_ATTR_IRREVOCABLE:
8653 str = "transaction_unsafe";
8654 break;
8655 case TM_ATTR_MAY_CANCEL_OUTER:
8656 str = "transaction_may_cancel_outer";
8657 break;
8658 default:
8659 gcc_unreachable ();
8660 }
8661 return get_identifier (str);
8662 }
8663
8664 /* Return the first TM attribute seen in LIST. */
8665
8666 tree
8667 find_tm_attribute (tree list)
8668 {
8669 for (; list ; list = TREE_CHAIN (list))
8670 {
8671 tree name = TREE_PURPOSE (list);
8672 if (tm_attr_to_mask (name) != 0)
8673 return name;
8674 }
8675 return NULL_TREE;
8676 }
8677
8678 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8679 Here we accept only function types, and verify that none of the other
8680 function TM attributes are also applied. */
8681 /* ??? We need to accept class types for C++, but not C. This greatly
8682 complicates this function, since we can no longer rely on the extra
8683 processing given by function_type_required. */
8684
8685 static tree
8686 handle_tm_attribute (tree *node, tree name, tree args,
8687 int flags, bool *no_add_attrs)
8688 {
8689 /* Only one path adds the attribute; others don't. */
8690 *no_add_attrs = true;
8691
8692 switch (TREE_CODE (*node))
8693 {
8694 case RECORD_TYPE:
8695 case UNION_TYPE:
8696 /* Only tm_callable and tm_safe apply to classes. */
8697 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8698 goto ignored;
8699 /* FALLTHRU */
8700
8701 case FUNCTION_TYPE:
8702 case METHOD_TYPE:
8703 {
8704 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8705 if (old_name == name)
8706 ;
8707 else if (old_name != NULL_TREE)
8708 error ("type was previously declared %qE", old_name);
8709 else
8710 *no_add_attrs = false;
8711 }
8712 break;
8713
8714 case FUNCTION_DECL:
8715 {
8716 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8717 want to set transaction_safe on the type. */
8718 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8719 if (!TYPE_P (DECL_CONTEXT (*node)))
8720 error_at (DECL_SOURCE_LOCATION (*node),
8721 "%<transaction_safe_dynamic%> may only be specified for "
8722 "a virtual function");
8723 *no_add_attrs = false;
8724 decl_attributes (&TREE_TYPE (*node),
8725 build_tree_list (get_identifier ("transaction_safe"),
8726 NULL_TREE),
8727 0);
8728 break;
8729 }
8730
8731 case POINTER_TYPE:
8732 {
8733 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8734 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8735 {
8736 tree fn_tmp = TREE_TYPE (*node);
8737 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8738 *node = build_pointer_type (fn_tmp);
8739 break;
8740 }
8741 }
8742 /* FALLTHRU */
8743
8744 default:
8745 /* If a function is next, pass it on to be tried next. */
8746 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8747 return tree_cons (name, args, NULL);
8748
8749 ignored:
8750 warning (OPT_Wattributes, "%qE attribute ignored", name);
8751 break;
8752 }
8753
8754 return NULL_TREE;
8755 }
8756
8757 /* Handle the TM_WRAP attribute; arguments as in
8758 struct attribute_spec.handler. */
8759
8760 static tree
8761 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8762 int ARG_UNUSED (flags), bool *no_add_attrs)
8763 {
8764 tree decl = *node;
8765
8766 /* We don't need the attribute even on success, since we
8767 record the entry in an external table. */
8768 *no_add_attrs = true;
8769
8770 if (TREE_CODE (decl) != FUNCTION_DECL)
8771 warning (OPT_Wattributes, "%qE attribute ignored", name);
8772 else
8773 {
8774 tree wrap_decl = TREE_VALUE (args);
8775 if (error_operand_p (wrap_decl))
8776 ;
8777 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8778 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
8779 error ("%qE argument not an identifier", name);
8780 else
8781 {
8782 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8783 wrap_decl = lookup_name (wrap_decl);
8784 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8785 {
8786 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8787 TREE_TYPE (wrap_decl)))
8788 record_tm_replacement (wrap_decl, decl);
8789 else
8790 error ("%qD is not compatible with %qD", wrap_decl, decl);
8791 }
8792 else
8793 error ("%qE argument is not a function", name);
8794 }
8795 }
8796
8797 return NULL_TREE;
8798 }
8799
8800 /* Ignore the given attribute. Used when this attribute may be usefully
8801 overridden by the target, but is not used generically. */
8802
8803 static tree
8804 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8805 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8806 bool *no_add_attrs)
8807 {
8808 *no_add_attrs = true;
8809 return NULL_TREE;
8810 }
8811
8812 /* Handle a "no vops" attribute; arguments as in
8813 struct attribute_spec.handler. */
8814
8815 static tree
8816 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8817 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8818 bool *ARG_UNUSED (no_add_attrs))
8819 {
8820 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8821 DECL_IS_NOVOPS (*node) = 1;
8822 return NULL_TREE;
8823 }
8824
8825 /* Handle a "deprecated" attribute; arguments as in
8826 struct attribute_spec.handler. */
8827
8828 static tree
8829 handle_deprecated_attribute (tree *node, tree name,
8830 tree args, int flags,
8831 bool *no_add_attrs)
8832 {
8833 tree type = NULL_TREE;
8834 int warn = 0;
8835 tree what = NULL_TREE;
8836
8837 if (!args)
8838 *no_add_attrs = true;
8839 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8840 {
8841 error ("deprecated message is not a string");
8842 *no_add_attrs = true;
8843 }
8844
8845 if (DECL_P (*node))
8846 {
8847 tree decl = *node;
8848 type = TREE_TYPE (decl);
8849
8850 if (TREE_CODE (decl) == TYPE_DECL
8851 || TREE_CODE (decl) == PARM_DECL
8852 || VAR_OR_FUNCTION_DECL_P (decl)
8853 || TREE_CODE (decl) == FIELD_DECL
8854 || TREE_CODE (decl) == CONST_DECL
8855 || objc_method_decl (TREE_CODE (decl)))
8856 TREE_DEPRECATED (decl) = 1;
8857 else
8858 warn = 1;
8859 }
8860 else if (TYPE_P (*node))
8861 {
8862 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8863 *node = build_variant_type_copy (*node);
8864 TREE_DEPRECATED (*node) = 1;
8865 type = *node;
8866 }
8867 else
8868 warn = 1;
8869
8870 if (warn)
8871 {
8872 *no_add_attrs = true;
8873 if (type && TYPE_NAME (type))
8874 {
8875 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8876 what = TYPE_NAME (*node);
8877 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8878 && DECL_NAME (TYPE_NAME (type)))
8879 what = DECL_NAME (TYPE_NAME (type));
8880 }
8881 if (what)
8882 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8883 else
8884 warning (OPT_Wattributes, "%qE attribute ignored", name);
8885 }
8886
8887 return NULL_TREE;
8888 }
8889
8890 /* Handle a "vector_size" attribute; arguments as in
8891 struct attribute_spec.handler. */
8892
8893 static tree
8894 handle_vector_size_attribute (tree *node, tree name, tree args,
8895 int ARG_UNUSED (flags),
8896 bool *no_add_attrs)
8897 {
8898 unsigned HOST_WIDE_INT vecsize, nunits;
8899 machine_mode orig_mode;
8900 tree type = *node, new_type, size;
8901
8902 *no_add_attrs = true;
8903
8904 size = TREE_VALUE (args);
8905 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8906 && TREE_CODE (size) != FUNCTION_DECL)
8907 size = default_conversion (size);
8908
8909 if (!tree_fits_uhwi_p (size))
8910 {
8911 warning (OPT_Wattributes, "%qE attribute ignored", name);
8912 return NULL_TREE;
8913 }
8914
8915 /* Get the vector size (in bytes). */
8916 vecsize = tree_to_uhwi (size);
8917
8918 /* We need to provide for vector pointers, vector arrays, and
8919 functions returning vectors. For example:
8920
8921 __attribute__((vector_size(16))) short *foo;
8922
8923 In this case, the mode is SI, but the type being modified is
8924 HI, so we need to look further. */
8925
8926 while (POINTER_TYPE_P (type)
8927 || TREE_CODE (type) == FUNCTION_TYPE
8928 || TREE_CODE (type) == METHOD_TYPE
8929 || TREE_CODE (type) == ARRAY_TYPE
8930 || TREE_CODE (type) == OFFSET_TYPE)
8931 type = TREE_TYPE (type);
8932
8933 /* Get the mode of the type being modified. */
8934 orig_mode = TYPE_MODE (type);
8935
8936 if ((!INTEGRAL_TYPE_P (type)
8937 && !SCALAR_FLOAT_TYPE_P (type)
8938 && !FIXED_POINT_TYPE_P (type))
8939 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8940 && GET_MODE_CLASS (orig_mode) != MODE_INT
8941 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8942 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8943 || TREE_CODE (type) == BOOLEAN_TYPE)
8944 {
8945 error ("invalid vector type for attribute %qE", name);
8946 return NULL_TREE;
8947 }
8948
8949 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8950 {
8951 error ("vector size not an integral multiple of component size");
8952 return NULL;
8953 }
8954
8955 if (vecsize == 0)
8956 {
8957 error ("zero vector size");
8958 return NULL;
8959 }
8960
8961 /* Calculate how many units fit in the vector. */
8962 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8963 if (nunits & (nunits - 1))
8964 {
8965 error ("number of components of the vector not a power of two");
8966 return NULL_TREE;
8967 }
8968
8969 new_type = build_vector_type (type, nunits);
8970
8971 /* Build back pointers if needed. */
8972 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8973
8974 return NULL_TREE;
8975 }
8976
8977 /* Handle the "nonnull" attribute. */
8978 static tree
8979 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8980 tree args, int ARG_UNUSED (flags),
8981 bool *no_add_attrs)
8982 {
8983 tree type = *node;
8984 unsigned HOST_WIDE_INT attr_arg_num;
8985
8986 /* If no arguments are specified, all pointer arguments should be
8987 non-null. Verify a full prototype is given so that the arguments
8988 will have the correct types when we actually check them later. */
8989 if (!args)
8990 {
8991 if (!prototype_p (type))
8992 {
8993 error ("nonnull attribute without arguments on a non-prototype");
8994 *no_add_attrs = true;
8995 }
8996 return NULL_TREE;
8997 }
8998
8999 /* Argument list specified. Verify that each argument number references
9000 a pointer argument. */
9001 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9002 {
9003 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9004
9005 tree arg = TREE_VALUE (args);
9006 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9007 && TREE_CODE (arg) != FUNCTION_DECL)
9008 arg = default_conversion (arg);
9009
9010 if (!get_nonnull_operand (arg, &arg_num))
9011 {
9012 error ("nonnull argument has invalid operand number (argument %lu)",
9013 (unsigned long) attr_arg_num);
9014 *no_add_attrs = true;
9015 return NULL_TREE;
9016 }
9017
9018 if (prototype_p (type))
9019 {
9020 function_args_iterator iter;
9021 tree argument;
9022
9023 function_args_iter_init (&iter, type);
9024 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9025 {
9026 argument = function_args_iter_cond (&iter);
9027 if (argument == NULL_TREE || ck_num == arg_num)
9028 break;
9029 }
9030
9031 if (!argument
9032 || TREE_CODE (argument) == VOID_TYPE)
9033 {
9034 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9035 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9036 *no_add_attrs = true;
9037 return NULL_TREE;
9038 }
9039
9040 if (TREE_CODE (argument) != POINTER_TYPE)
9041 {
9042 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9043 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9044 *no_add_attrs = true;
9045 return NULL_TREE;
9046 }
9047 }
9048 }
9049
9050 return NULL_TREE;
9051 }
9052
9053 /* Check the argument list of a function call for null in argument slots
9054 that are marked as requiring a non-null pointer argument. The NARGS
9055 arguments are passed in the array ARGARRAY.
9056 */
9057
9058 static void
9059 check_function_nonnull (tree attrs, int nargs, tree *argarray)
9060 {
9061 tree a;
9062 int i;
9063
9064 attrs = lookup_attribute ("nonnull", attrs);
9065 if (attrs == NULL_TREE)
9066 return;
9067
9068 a = attrs;
9069 /* See if any of the nonnull attributes has no arguments. If so,
9070 then every pointer argument is checked (in which case the check
9071 for pointer type is done in check_nonnull_arg). */
9072 if (TREE_VALUE (a) != NULL_TREE)
9073 do
9074 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9075 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9076
9077 if (a != NULL_TREE)
9078 for (i = 0; i < nargs; i++)
9079 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9080 i + 1);
9081 else
9082 {
9083 /* Walk the argument list. If we encounter an argument number we
9084 should check for non-null, do it. */
9085 for (i = 0; i < nargs; i++)
9086 {
9087 for (a = attrs; ; a = TREE_CHAIN (a))
9088 {
9089 a = lookup_attribute ("nonnull", a);
9090 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9091 break;
9092 }
9093
9094 if (a != NULL_TREE)
9095 check_function_arguments_recurse (check_nonnull_arg, NULL,
9096 argarray[i], i + 1);
9097 }
9098 }
9099 }
9100
9101 /* Check that the Nth argument of a function call (counting backwards
9102 from the end) is a (pointer)0. The NARGS arguments are passed in the
9103 array ARGARRAY. */
9104
9105 static void
9106 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9107 {
9108 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9109
9110 if (attr)
9111 {
9112 int len = 0;
9113 int pos = 0;
9114 tree sentinel;
9115 function_args_iterator iter;
9116 tree t;
9117
9118 /* Skip over the named arguments. */
9119 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9120 {
9121 if (len == nargs)
9122 break;
9123 len++;
9124 }
9125
9126 if (TREE_VALUE (attr))
9127 {
9128 tree p = TREE_VALUE (TREE_VALUE (attr));
9129 pos = TREE_INT_CST_LOW (p);
9130 }
9131
9132 /* The sentinel must be one of the varargs, i.e.
9133 in position >= the number of fixed arguments. */
9134 if ((nargs - 1 - pos) < len)
9135 {
9136 warning (OPT_Wformat_,
9137 "not enough variable arguments to fit a sentinel");
9138 return;
9139 }
9140
9141 /* Validate the sentinel. */
9142 sentinel = argarray[nargs - 1 - pos];
9143 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9144 || !integer_zerop (sentinel))
9145 /* Although __null (in C++) is only an integer we allow it
9146 nevertheless, as we are guaranteed that it's exactly
9147 as wide as a pointer, and we don't want to force
9148 users to cast the NULL they have written there.
9149 We warn with -Wstrict-null-sentinel, though. */
9150 && (warn_strict_null_sentinel || null_node != sentinel))
9151 warning (OPT_Wformat_, "missing sentinel in function call");
9152 }
9153 }
9154
9155 /* Helper for check_function_nonnull; given a list of operands which
9156 must be non-null in ARGS, determine if operand PARAM_NUM should be
9157 checked. */
9158
9159 static bool
9160 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9161 {
9162 unsigned HOST_WIDE_INT arg_num = 0;
9163
9164 for (; args; args = TREE_CHAIN (args))
9165 {
9166 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9167
9168 gcc_assert (found);
9169
9170 if (arg_num == param_num)
9171 return true;
9172 }
9173 return false;
9174 }
9175
9176 /* Check that the function argument PARAM (which is operand number
9177 PARAM_NUM) is non-null. This is called by check_function_nonnull
9178 via check_function_arguments_recurse. */
9179
9180 static void
9181 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9182 unsigned HOST_WIDE_INT param_num)
9183 {
9184 /* Just skip checking the argument if it's not a pointer. This can
9185 happen if the "nonnull" attribute was given without an operand
9186 list (which means to check every pointer argument). */
9187
9188 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9189 return;
9190
9191 if (integer_zerop (param))
9192 warning (OPT_Wnonnull, "null argument where non-null required "
9193 "(argument %lu)", (unsigned long) param_num);
9194 }
9195
9196 /* Helper for nonnull attribute handling; fetch the operand number
9197 from the attribute argument list. */
9198
9199 static bool
9200 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9201 {
9202 /* Verify the arg number is a small constant. */
9203 if (tree_fits_uhwi_p (arg_num_expr))
9204 {
9205 *valp = TREE_INT_CST_LOW (arg_num_expr);
9206 return true;
9207 }
9208 else
9209 return false;
9210 }
9211
9212 /* Handle a "nothrow" attribute; arguments as in
9213 struct attribute_spec.handler. */
9214
9215 static tree
9216 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9217 int ARG_UNUSED (flags), bool *no_add_attrs)
9218 {
9219 if (TREE_CODE (*node) == FUNCTION_DECL)
9220 TREE_NOTHROW (*node) = 1;
9221 /* ??? TODO: Support types. */
9222 else
9223 {
9224 warning (OPT_Wattributes, "%qE attribute ignored", name);
9225 *no_add_attrs = true;
9226 }
9227
9228 return NULL_TREE;
9229 }
9230
9231 /* Handle a "cleanup" attribute; arguments as in
9232 struct attribute_spec.handler. */
9233
9234 static tree
9235 handle_cleanup_attribute (tree *node, tree name, tree args,
9236 int ARG_UNUSED (flags), bool *no_add_attrs)
9237 {
9238 tree decl = *node;
9239 tree cleanup_id, cleanup_decl;
9240
9241 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9242 for global destructors in C++. This requires infrastructure that
9243 we don't have generically at the moment. It's also not a feature
9244 we'd be missing too much, since we do have attribute constructor. */
9245 if (!VAR_P (decl) || TREE_STATIC (decl))
9246 {
9247 warning (OPT_Wattributes, "%qE attribute ignored", name);
9248 *no_add_attrs = true;
9249 return NULL_TREE;
9250 }
9251
9252 /* Verify that the argument is a function in scope. */
9253 /* ??? We could support pointers to functions here as well, if
9254 that was considered desirable. */
9255 cleanup_id = TREE_VALUE (args);
9256 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9257 {
9258 error ("cleanup argument not an identifier");
9259 *no_add_attrs = true;
9260 return NULL_TREE;
9261 }
9262 cleanup_decl = lookup_name (cleanup_id);
9263 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9264 {
9265 error ("cleanup argument not a function");
9266 *no_add_attrs = true;
9267 return NULL_TREE;
9268 }
9269
9270 /* That the function has proper type is checked with the
9271 eventual call to build_function_call. */
9272
9273 return NULL_TREE;
9274 }
9275
9276 /* Handle a "warn_unused_result" attribute. No special handling. */
9277
9278 static tree
9279 handle_warn_unused_result_attribute (tree *node, tree name,
9280 tree ARG_UNUSED (args),
9281 int ARG_UNUSED (flags), bool *no_add_attrs)
9282 {
9283 /* Ignore the attribute for functions not returning any value. */
9284 if (VOID_TYPE_P (TREE_TYPE (*node)))
9285 {
9286 warning (OPT_Wattributes, "%qE attribute ignored", name);
9287 *no_add_attrs = true;
9288 }
9289
9290 return NULL_TREE;
9291 }
9292
9293 /* Handle a "sentinel" attribute. */
9294
9295 static tree
9296 handle_sentinel_attribute (tree *node, tree name, tree args,
9297 int ARG_UNUSED (flags), bool *no_add_attrs)
9298 {
9299 if (!prototype_p (*node))
9300 {
9301 warning (OPT_Wattributes,
9302 "%qE attribute requires prototypes with named arguments", name);
9303 *no_add_attrs = true;
9304 }
9305 else
9306 {
9307 if (!stdarg_p (*node))
9308 {
9309 warning (OPT_Wattributes,
9310 "%qE attribute only applies to variadic functions", name);
9311 *no_add_attrs = true;
9312 }
9313 }
9314
9315 if (args)
9316 {
9317 tree position = TREE_VALUE (args);
9318 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9319 && TREE_CODE (position) != FUNCTION_DECL)
9320 position = default_conversion (position);
9321
9322 if (TREE_CODE (position) != INTEGER_CST
9323 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9324 {
9325 warning (OPT_Wattributes,
9326 "requested position is not an integer constant");
9327 *no_add_attrs = true;
9328 }
9329 else
9330 {
9331 if (tree_int_cst_lt (position, integer_zero_node))
9332 {
9333 warning (OPT_Wattributes,
9334 "requested position is less than zero");
9335 *no_add_attrs = true;
9336 }
9337 }
9338 }
9339
9340 return NULL_TREE;
9341 }
9342
9343 /* Handle a "type_generic" attribute. */
9344
9345 static tree
9346 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9347 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9348 bool * ARG_UNUSED (no_add_attrs))
9349 {
9350 /* Ensure we have a function type. */
9351 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9352
9353 /* Ensure we have a variadic function. */
9354 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9355
9356 return NULL_TREE;
9357 }
9358
9359 /* Handle a "target" attribute. */
9360
9361 static tree
9362 handle_target_attribute (tree *node, tree name, tree args, int flags,
9363 bool *no_add_attrs)
9364 {
9365 /* Ensure we have a function type. */
9366 if (TREE_CODE (*node) != FUNCTION_DECL)
9367 {
9368 warning (OPT_Wattributes, "%qE attribute ignored", name);
9369 *no_add_attrs = true;
9370 }
9371 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9372 {
9373 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9374 "with %qs attribute", name, "target_clones");
9375 *no_add_attrs = true;
9376 }
9377 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9378 flags))
9379 *no_add_attrs = true;
9380
9381 return NULL_TREE;
9382 }
9383
9384 /* Handle a "target_clones" attribute. */
9385
9386 static tree
9387 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9388 int ARG_UNUSED (flags), bool *no_add_attrs)
9389 {
9390 /* Ensure we have a function type. */
9391 if (TREE_CODE (*node) == FUNCTION_DECL)
9392 {
9393 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9394 {
9395 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9396 "with %qs attribute", name, "always_inline");
9397 *no_add_attrs = true;
9398 }
9399 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9400 {
9401 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9402 "with %qs attribute", name, "target");
9403 *no_add_attrs = true;
9404 }
9405 else
9406 /* Do not inline functions with multiple clone targets. */
9407 DECL_UNINLINABLE (*node) = 1;
9408 }
9409 else
9410 {
9411 warning (OPT_Wattributes, "%qE attribute ignored", name);
9412 *no_add_attrs = true;
9413 }
9414 return NULL_TREE;
9415 }
9416
9417 /* Arguments being collected for optimization. */
9418 typedef const char *const_char_p; /* For DEF_VEC_P. */
9419 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9420
9421
9422 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9423 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9424 false for #pragma GCC optimize. */
9425
9426 bool
9427 parse_optimize_options (tree args, bool attr_p)
9428 {
9429 bool ret = true;
9430 unsigned opt_argc;
9431 unsigned i;
9432 const char **opt_argv;
9433 struct cl_decoded_option *decoded_options;
9434 unsigned int decoded_options_count;
9435 tree ap;
9436
9437 /* Build up argv vector. Just in case the string is stored away, use garbage
9438 collected strings. */
9439 vec_safe_truncate (optimize_args, 0);
9440 vec_safe_push (optimize_args, (const char *) NULL);
9441
9442 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9443 {
9444 tree value = TREE_VALUE (ap);
9445
9446 if (TREE_CODE (value) == INTEGER_CST)
9447 {
9448 char buffer[20];
9449 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9450 vec_safe_push (optimize_args, ggc_strdup (buffer));
9451 }
9452
9453 else if (TREE_CODE (value) == STRING_CST)
9454 {
9455 /* Split string into multiple substrings. */
9456 size_t len = TREE_STRING_LENGTH (value);
9457 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9458 char *end = p + len;
9459 char *comma;
9460 char *next_p = p;
9461
9462 while (next_p != NULL)
9463 {
9464 size_t len2;
9465 char *q, *r;
9466
9467 p = next_p;
9468 comma = strchr (p, ',');
9469 if (comma)
9470 {
9471 len2 = comma - p;
9472 *comma = '\0';
9473 next_p = comma+1;
9474 }
9475 else
9476 {
9477 len2 = end - p;
9478 next_p = NULL;
9479 }
9480
9481 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9482
9483 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9484 options. */
9485 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9486 {
9487 ret = false;
9488 if (attr_p)
9489 warning (OPT_Wattributes,
9490 "bad option %s to optimize attribute", p);
9491 else
9492 warning (OPT_Wpragmas,
9493 "bad option %s to pragma attribute", p);
9494 continue;
9495 }
9496
9497 if (*p != '-')
9498 {
9499 *r++ = '-';
9500
9501 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9502 itself is -Os, and any other switch begins with a -f. */
9503 if ((*p >= '0' && *p <= '9')
9504 || (p[0] == 's' && p[1] == '\0'))
9505 *r++ = 'O';
9506 else if (*p != 'O')
9507 *r++ = 'f';
9508 }
9509
9510 memcpy (r, p, len2);
9511 r[len2] = '\0';
9512 vec_safe_push (optimize_args, (const char *) q);
9513 }
9514
9515 }
9516 }
9517
9518 opt_argc = optimize_args->length ();
9519 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9520
9521 for (i = 1; i < opt_argc; i++)
9522 opt_argv[i] = (*optimize_args)[i];
9523
9524 /* Now parse the options. */
9525 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9526 &decoded_options,
9527 &decoded_options_count);
9528 decode_options (&global_options, &global_options_set,
9529 decoded_options, decoded_options_count,
9530 input_location, global_dc);
9531
9532 targetm.override_options_after_change();
9533
9534 optimize_args->truncate (0);
9535 return ret;
9536 }
9537
9538 /* For handling "optimize" attribute. arguments as in
9539 struct attribute_spec.handler. */
9540
9541 static tree
9542 handle_optimize_attribute (tree *node, tree name, tree args,
9543 int ARG_UNUSED (flags), bool *no_add_attrs)
9544 {
9545 /* Ensure we have a function type. */
9546 if (TREE_CODE (*node) != FUNCTION_DECL)
9547 {
9548 warning (OPT_Wattributes, "%qE attribute ignored", name);
9549 *no_add_attrs = true;
9550 }
9551 else
9552 {
9553 struct cl_optimization cur_opts;
9554 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9555
9556 /* Save current options. */
9557 cl_optimization_save (&cur_opts, &global_options);
9558
9559 /* If we previously had some optimization options, use them as the
9560 default. */
9561 if (old_opts)
9562 cl_optimization_restore (&global_options,
9563 TREE_OPTIMIZATION (old_opts));
9564
9565 /* Parse options, and update the vector. */
9566 parse_optimize_options (args, true);
9567 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9568 = build_optimization_node (&global_options);
9569
9570 /* Restore current options. */
9571 cl_optimization_restore (&global_options, &cur_opts);
9572 }
9573
9574 return NULL_TREE;
9575 }
9576
9577 /* Handle a "no_split_stack" attribute. */
9578
9579 static tree
9580 handle_no_split_stack_attribute (tree *node, tree name,
9581 tree ARG_UNUSED (args),
9582 int ARG_UNUSED (flags),
9583 bool *no_add_attrs)
9584 {
9585 tree decl = *node;
9586
9587 if (TREE_CODE (decl) != FUNCTION_DECL)
9588 {
9589 error_at (DECL_SOURCE_LOCATION (decl),
9590 "%qE attribute applies only to functions", name);
9591 *no_add_attrs = true;
9592 }
9593 else if (DECL_INITIAL (decl))
9594 {
9595 error_at (DECL_SOURCE_LOCATION (decl),
9596 "can%'t set %qE attribute after definition", name);
9597 *no_add_attrs = true;
9598 }
9599
9600 return NULL_TREE;
9601 }
9602
9603 /* Handle a "returns_nonnull" attribute; arguments as in
9604 struct attribute_spec.handler. */
9605
9606 static tree
9607 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9608 bool *no_add_attrs)
9609 {
9610 // Even without a prototype we still have a return type we can check.
9611 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9612 {
9613 error ("returns_nonnull attribute on a function not returning a pointer");
9614 *no_add_attrs = true;
9615 }
9616 return NULL_TREE;
9617 }
9618
9619 /* Handle a "designated_init" attribute; arguments as in
9620 struct attribute_spec.handler. */
9621
9622 static tree
9623 handle_designated_init_attribute (tree *node, tree name, tree, int,
9624 bool *no_add_attrs)
9625 {
9626 if (TREE_CODE (*node) != RECORD_TYPE)
9627 {
9628 error ("%qE attribute is only valid on %<struct%> type", name);
9629 *no_add_attrs = true;
9630 }
9631 return NULL_TREE;
9632 }
9633
9634 \f
9635 /* Check for valid arguments being passed to a function with FNTYPE.
9636 There are NARGS arguments in the array ARGARRAY. */
9637 void
9638 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9639 {
9640 /* Check for null being passed in a pointer argument that must be
9641 non-null. We also need to do this if format checking is enabled. */
9642
9643 if (warn_nonnull)
9644 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9645
9646 /* Check for errors in format strings. */
9647
9648 if (warn_format || warn_suggest_attribute_format)
9649 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9650
9651 if (warn_format)
9652 check_function_sentinel (fntype, nargs, argarray);
9653 }
9654
9655 /* Generic argument checking recursion routine. PARAM is the argument to
9656 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9657 once the argument is resolved. CTX is context for the callback. */
9658 void
9659 check_function_arguments_recurse (void (*callback)
9660 (void *, tree, unsigned HOST_WIDE_INT),
9661 void *ctx, tree param,
9662 unsigned HOST_WIDE_INT param_num)
9663 {
9664 if (CONVERT_EXPR_P (param)
9665 && (TYPE_PRECISION (TREE_TYPE (param))
9666 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9667 {
9668 /* Strip coercion. */
9669 check_function_arguments_recurse (callback, ctx,
9670 TREE_OPERAND (param, 0), param_num);
9671 return;
9672 }
9673
9674 if (TREE_CODE (param) == CALL_EXPR)
9675 {
9676 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9677 tree attrs;
9678 bool found_format_arg = false;
9679
9680 /* See if this is a call to a known internationalization function
9681 that modifies a format arg. Such a function may have multiple
9682 format_arg attributes (for example, ngettext). */
9683
9684 for (attrs = TYPE_ATTRIBUTES (type);
9685 attrs;
9686 attrs = TREE_CHAIN (attrs))
9687 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9688 {
9689 tree inner_arg;
9690 tree format_num_expr;
9691 int format_num;
9692 int i;
9693 call_expr_arg_iterator iter;
9694
9695 /* Extract the argument number, which was previously checked
9696 to be valid. */
9697 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9698
9699 format_num = tree_to_uhwi (format_num_expr);
9700
9701 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9702 inner_arg != 0;
9703 inner_arg = next_call_expr_arg (&iter), i++)
9704 if (i == format_num)
9705 {
9706 check_function_arguments_recurse (callback, ctx,
9707 inner_arg, param_num);
9708 found_format_arg = true;
9709 break;
9710 }
9711 }
9712
9713 /* If we found a format_arg attribute and did a recursive check,
9714 we are done with checking this argument. Otherwise, we continue
9715 and this will be considered a non-literal. */
9716 if (found_format_arg)
9717 return;
9718 }
9719
9720 if (TREE_CODE (param) == COND_EXPR)
9721 {
9722 tree cond = fold_for_warn (TREE_OPERAND (param, 0));
9723 /* Check both halves of the conditional expression. */
9724 if (!integer_zerop (cond))
9725 check_function_arguments_recurse (callback, ctx,
9726 TREE_OPERAND (param, 1), param_num);
9727 if (!integer_nonzerop (cond))
9728 check_function_arguments_recurse (callback, ctx,
9729 TREE_OPERAND (param, 2), param_num);
9730 return;
9731 }
9732
9733 (*callback) (ctx, param, param_num);
9734 }
9735
9736 /* Checks for a builtin function FNDECL that the number of arguments
9737 NARGS against the required number REQUIRED and issues an error if
9738 there is a mismatch. Returns true if the number of arguments is
9739 correct, otherwise false. */
9740
9741 static bool
9742 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9743 {
9744 if (nargs < required)
9745 {
9746 error_at (input_location,
9747 "not enough arguments to function %qE", fndecl);
9748 return false;
9749 }
9750 else if (nargs > required)
9751 {
9752 error_at (input_location,
9753 "too many arguments to function %qE", fndecl);
9754 return false;
9755 }
9756 return true;
9757 }
9758
9759 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9760 Returns false if there was an error, otherwise true. */
9761
9762 bool
9763 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9764 {
9765 if (!DECL_BUILT_IN (fndecl)
9766 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9767 return true;
9768
9769 switch (DECL_FUNCTION_CODE (fndecl))
9770 {
9771 case BUILT_IN_CONSTANT_P:
9772 return builtin_function_validate_nargs (fndecl, nargs, 1);
9773
9774 case BUILT_IN_ISFINITE:
9775 case BUILT_IN_ISINF:
9776 case BUILT_IN_ISINF_SIGN:
9777 case BUILT_IN_ISNAN:
9778 case BUILT_IN_ISNORMAL:
9779 case BUILT_IN_SIGNBIT:
9780 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9781 {
9782 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9783 {
9784 error ("non-floating-point argument in call to "
9785 "function %qE", fndecl);
9786 return false;
9787 }
9788 return true;
9789 }
9790 return false;
9791
9792 case BUILT_IN_ISGREATER:
9793 case BUILT_IN_ISGREATEREQUAL:
9794 case BUILT_IN_ISLESS:
9795 case BUILT_IN_ISLESSEQUAL:
9796 case BUILT_IN_ISLESSGREATER:
9797 case BUILT_IN_ISUNORDERED:
9798 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9799 {
9800 enum tree_code code0, code1;
9801 code0 = TREE_CODE (TREE_TYPE (args[0]));
9802 code1 = TREE_CODE (TREE_TYPE (args[1]));
9803 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9804 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9805 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9806 {
9807 error ("non-floating-point arguments in call to "
9808 "function %qE", fndecl);
9809 return false;
9810 }
9811 return true;
9812 }
9813 return false;
9814
9815 case BUILT_IN_FPCLASSIFY:
9816 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9817 {
9818 unsigned i;
9819
9820 for (i=0; i<5; i++)
9821 if (TREE_CODE (args[i]) != INTEGER_CST)
9822 {
9823 error ("non-const integer argument %u in call to function %qE",
9824 i+1, fndecl);
9825 return false;
9826 }
9827
9828 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9829 {
9830 error ("non-floating-point argument in call to function %qE",
9831 fndecl);
9832 return false;
9833 }
9834 return true;
9835 }
9836 return false;
9837
9838 case BUILT_IN_ASSUME_ALIGNED:
9839 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9840 {
9841 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9842 {
9843 error ("non-integer argument 3 in call to function %qE", fndecl);
9844 return false;
9845 }
9846 return true;
9847 }
9848 return false;
9849
9850 case BUILT_IN_ADD_OVERFLOW:
9851 case BUILT_IN_SUB_OVERFLOW:
9852 case BUILT_IN_MUL_OVERFLOW:
9853 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9854 {
9855 unsigned i;
9856 for (i = 0; i < 2; i++)
9857 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9858 {
9859 error ("argument %u in call to function %qE does not have "
9860 "integral type", i + 1, fndecl);
9861 return false;
9862 }
9863 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9864 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9865 {
9866 error ("argument 3 in call to function %qE does not have "
9867 "pointer to integer type", fndecl);
9868 return false;
9869 }
9870 return true;
9871 }
9872 return false;
9873
9874 default:
9875 return true;
9876 }
9877 }
9878
9879 /* Function to help qsort sort FIELD_DECLs by name order. */
9880
9881 int
9882 field_decl_cmp (const void *x_p, const void *y_p)
9883 {
9884 const tree *const x = (const tree *const) x_p;
9885 const tree *const y = (const tree *const) y_p;
9886
9887 if (DECL_NAME (*x) == DECL_NAME (*y))
9888 /* A nontype is "greater" than a type. */
9889 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9890 if (DECL_NAME (*x) == NULL_TREE)
9891 return -1;
9892 if (DECL_NAME (*y) == NULL_TREE)
9893 return 1;
9894 if (DECL_NAME (*x) < DECL_NAME (*y))
9895 return -1;
9896 return 1;
9897 }
9898
9899 static struct {
9900 gt_pointer_operator new_value;
9901 void *cookie;
9902 } resort_data;
9903
9904 /* This routine compares two fields like field_decl_cmp but using the
9905 pointer operator in resort_data. */
9906
9907 static int
9908 resort_field_decl_cmp (const void *x_p, const void *y_p)
9909 {
9910 const tree *const x = (const tree *const) x_p;
9911 const tree *const y = (const tree *const) y_p;
9912
9913 if (DECL_NAME (*x) == DECL_NAME (*y))
9914 /* A nontype is "greater" than a type. */
9915 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9916 if (DECL_NAME (*x) == NULL_TREE)
9917 return -1;
9918 if (DECL_NAME (*y) == NULL_TREE)
9919 return 1;
9920 {
9921 tree d1 = DECL_NAME (*x);
9922 tree d2 = DECL_NAME (*y);
9923 resort_data.new_value (&d1, resort_data.cookie);
9924 resort_data.new_value (&d2, resort_data.cookie);
9925 if (d1 < d2)
9926 return -1;
9927 }
9928 return 1;
9929 }
9930
9931 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9932
9933 void
9934 resort_sorted_fields (void *obj,
9935 void * ARG_UNUSED (orig_obj),
9936 gt_pointer_operator new_value,
9937 void *cookie)
9938 {
9939 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9940 resort_data.new_value = new_value;
9941 resort_data.cookie = cookie;
9942 qsort (&sf->elts[0], sf->len, sizeof (tree),
9943 resort_field_decl_cmp);
9944 }
9945
9946 /* Subroutine of c_parse_error.
9947 Return the result of concatenating LHS and RHS. RHS is really
9948 a string literal, its first character is indicated by RHS_START and
9949 RHS_SIZE is its length (including the terminating NUL character).
9950
9951 The caller is responsible for deleting the returned pointer. */
9952
9953 static char *
9954 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9955 {
9956 const int lhs_size = strlen (lhs);
9957 char *result = XNEWVEC (char, lhs_size + rhs_size);
9958 strncpy (result, lhs, lhs_size);
9959 strncpy (result + lhs_size, rhs_start, rhs_size);
9960 return result;
9961 }
9962
9963 /* Issue the error given by GMSGID, indicating that it occurred before
9964 TOKEN, which had the associated VALUE. */
9965
9966 void
9967 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9968 tree value, unsigned char token_flags)
9969 {
9970 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9971
9972 char *message = NULL;
9973
9974 if (token_type == CPP_EOF)
9975 message = catenate_messages (gmsgid, " at end of input");
9976 else if (token_type == CPP_CHAR
9977 || token_type == CPP_WCHAR
9978 || token_type == CPP_CHAR16
9979 || token_type == CPP_CHAR32
9980 || token_type == CPP_UTF8CHAR)
9981 {
9982 unsigned int val = TREE_INT_CST_LOW (value);
9983 const char *prefix;
9984
9985 switch (token_type)
9986 {
9987 default:
9988 prefix = "";
9989 break;
9990 case CPP_WCHAR:
9991 prefix = "L";
9992 break;
9993 case CPP_CHAR16:
9994 prefix = "u";
9995 break;
9996 case CPP_CHAR32:
9997 prefix = "U";
9998 break;
9999 case CPP_UTF8CHAR:
10000 prefix = "u8";
10001 break;
10002 }
10003
10004 if (val <= UCHAR_MAX && ISGRAPH (val))
10005 message = catenate_messages (gmsgid, " before %s'%c'");
10006 else
10007 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10008
10009 error (message, prefix, val);
10010 free (message);
10011 message = NULL;
10012 }
10013 else if (token_type == CPP_CHAR_USERDEF
10014 || token_type == CPP_WCHAR_USERDEF
10015 || token_type == CPP_CHAR16_USERDEF
10016 || token_type == CPP_CHAR32_USERDEF
10017 || token_type == CPP_UTF8CHAR_USERDEF)
10018 message = catenate_messages (gmsgid,
10019 " before user-defined character literal");
10020 else if (token_type == CPP_STRING_USERDEF
10021 || token_type == CPP_WSTRING_USERDEF
10022 || token_type == CPP_STRING16_USERDEF
10023 || token_type == CPP_STRING32_USERDEF
10024 || token_type == CPP_UTF8STRING_USERDEF)
10025 message = catenate_messages (gmsgid, " before user-defined string literal");
10026 else if (token_type == CPP_STRING
10027 || token_type == CPP_WSTRING
10028 || token_type == CPP_STRING16
10029 || token_type == CPP_STRING32
10030 || token_type == CPP_UTF8STRING)
10031 message = catenate_messages (gmsgid, " before string constant");
10032 else if (token_type == CPP_NUMBER)
10033 message = catenate_messages (gmsgid, " before numeric constant");
10034 else if (token_type == CPP_NAME)
10035 {
10036 message = catenate_messages (gmsgid, " before %qE");
10037 error (message, value);
10038 free (message);
10039 message = NULL;
10040 }
10041 else if (token_type == CPP_PRAGMA)
10042 message = catenate_messages (gmsgid, " before %<#pragma%>");
10043 else if (token_type == CPP_PRAGMA_EOL)
10044 message = catenate_messages (gmsgid, " before end of line");
10045 else if (token_type == CPP_DECLTYPE)
10046 message = catenate_messages (gmsgid, " before %<decltype%>");
10047 else if (token_type < N_TTYPES)
10048 {
10049 message = catenate_messages (gmsgid, " before %qs token");
10050 error (message, cpp_type2name (token_type, token_flags));
10051 free (message);
10052 message = NULL;
10053 }
10054 else
10055 error (gmsgid);
10056
10057 if (message)
10058 {
10059 error (message);
10060 free (message);
10061 }
10062 #undef catenate_messages
10063 }
10064
10065 /* Return the gcc option code associated with the reason for a cpp
10066 message, or 0 if none. */
10067
10068 static int
10069 c_option_controlling_cpp_error (int reason)
10070 {
10071 const struct cpp_reason_option_codes_t *entry;
10072
10073 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10074 {
10075 if (entry->reason == reason)
10076 return entry->option_code;
10077 }
10078 return 0;
10079 }
10080
10081 /* Callback from cpp_error for PFILE to print diagnostics from the
10082 preprocessor. The diagnostic is of type LEVEL, with REASON set
10083 to the reason code if LEVEL is represents a warning, at location
10084 RICHLOC unless this is after lexing and the compiler's location
10085 should be used instead; MSG is the translated message and AP
10086 the arguments. Returns true if a diagnostic was emitted, false
10087 otherwise. */
10088
10089 bool
10090 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10091 rich_location *richloc,
10092 const char *msg, va_list *ap)
10093 {
10094 diagnostic_info diagnostic;
10095 diagnostic_t dlevel;
10096 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10097 bool ret;
10098
10099 switch (level)
10100 {
10101 case CPP_DL_WARNING_SYSHDR:
10102 if (flag_no_output)
10103 return false;
10104 global_dc->dc_warn_system_headers = 1;
10105 /* Fall through. */
10106 case CPP_DL_WARNING:
10107 if (flag_no_output)
10108 return false;
10109 dlevel = DK_WARNING;
10110 break;
10111 case CPP_DL_PEDWARN:
10112 if (flag_no_output && !flag_pedantic_errors)
10113 return false;
10114 dlevel = DK_PEDWARN;
10115 break;
10116 case CPP_DL_ERROR:
10117 dlevel = DK_ERROR;
10118 break;
10119 case CPP_DL_ICE:
10120 dlevel = DK_ICE;
10121 break;
10122 case CPP_DL_NOTE:
10123 dlevel = DK_NOTE;
10124 break;
10125 case CPP_DL_FATAL:
10126 dlevel = DK_FATAL;
10127 break;
10128 default:
10129 gcc_unreachable ();
10130 }
10131 if (done_lexing)
10132 richloc->set_range (0,
10133 source_range::from_location (input_location),
10134 true, true);
10135 diagnostic_set_info_translated (&diagnostic, msg, ap,
10136 richloc, dlevel);
10137 diagnostic_override_option_index (&diagnostic,
10138 c_option_controlling_cpp_error (reason));
10139 ret = report_diagnostic (&diagnostic);
10140 if (level == CPP_DL_WARNING_SYSHDR)
10141 global_dc->dc_warn_system_headers = save_warn_system_headers;
10142 return ret;
10143 }
10144
10145 /* Convert a character from the host to the target execution character
10146 set. cpplib handles this, mostly. */
10147
10148 HOST_WIDE_INT
10149 c_common_to_target_charset (HOST_WIDE_INT c)
10150 {
10151 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10152 zero-extended under -fno-signed-char. cpplib insists that characters
10153 and character constants are always unsigned. Hence we must convert
10154 back and forth. */
10155 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10156
10157 uc = cpp_host_to_exec_charset (parse_in, uc);
10158
10159 if (flag_signed_char)
10160 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10161 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10162 else
10163 return uc;
10164 }
10165
10166 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10167 references with an INDIRECT_REF of a constant at the bottom; much like the
10168 traditional rendering of offsetof as a macro. Return the folded result. */
10169
10170 tree
10171 fold_offsetof_1 (tree expr, enum tree_code ctx)
10172 {
10173 tree base, off, t;
10174 tree_code code = TREE_CODE (expr);
10175 switch (code)
10176 {
10177 case ERROR_MARK:
10178 return expr;
10179
10180 case VAR_DECL:
10181 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10182 return error_mark_node;
10183
10184 case CALL_EXPR:
10185 case TARGET_EXPR:
10186 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10187 return error_mark_node;
10188
10189 case NOP_EXPR:
10190 case INDIRECT_REF:
10191 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10192 {
10193 error ("cannot apply %<offsetof%> to a non constant address");
10194 return error_mark_node;
10195 }
10196 return TREE_OPERAND (expr, 0);
10197
10198 case COMPONENT_REF:
10199 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10200 if (base == error_mark_node)
10201 return base;
10202
10203 t = TREE_OPERAND (expr, 1);
10204 if (DECL_C_BIT_FIELD (t))
10205 {
10206 error ("attempt to take address of bit-field structure "
10207 "member %qD", t);
10208 return error_mark_node;
10209 }
10210 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10211 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10212 / BITS_PER_UNIT));
10213 break;
10214
10215 case ARRAY_REF:
10216 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10217 if (base == error_mark_node)
10218 return base;
10219
10220 t = TREE_OPERAND (expr, 1);
10221
10222 /* Check if the offset goes beyond the upper bound of the array. */
10223 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10224 {
10225 tree upbound = array_ref_up_bound (expr);
10226 if (upbound != NULL_TREE
10227 && TREE_CODE (upbound) == INTEGER_CST
10228 && !tree_int_cst_equal (upbound,
10229 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10230 {
10231 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10232 upbound = size_binop (PLUS_EXPR, upbound,
10233 build_int_cst (TREE_TYPE (upbound), 1));
10234 if (tree_int_cst_lt (upbound, t))
10235 {
10236 tree v;
10237
10238 for (v = TREE_OPERAND (expr, 0);
10239 TREE_CODE (v) == COMPONENT_REF;
10240 v = TREE_OPERAND (v, 0))
10241 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10242 == RECORD_TYPE)
10243 {
10244 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10245 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10246 if (TREE_CODE (fld_chain) == FIELD_DECL)
10247 break;
10248
10249 if (fld_chain)
10250 break;
10251 }
10252 /* Don't warn if the array might be considered a poor
10253 man's flexible array member with a very permissive
10254 definition thereof. */
10255 if (TREE_CODE (v) == ARRAY_REF
10256 || TREE_CODE (v) == COMPONENT_REF)
10257 warning (OPT_Warray_bounds,
10258 "index %E denotes an offset "
10259 "greater than size of %qT",
10260 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10261 }
10262 }
10263 }
10264
10265 t = convert (sizetype, t);
10266 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10267 break;
10268
10269 case COMPOUND_EXPR:
10270 /* Handle static members of volatile structs. */
10271 t = TREE_OPERAND (expr, 1);
10272 gcc_assert (VAR_P (t));
10273 return fold_offsetof_1 (t);
10274
10275 default:
10276 gcc_unreachable ();
10277 }
10278
10279 return fold_build_pointer_plus (base, off);
10280 }
10281
10282 /* Likewise, but convert it to the return type of offsetof. */
10283
10284 tree
10285 fold_offsetof (tree expr)
10286 {
10287 return convert (size_type_node, fold_offsetof_1 (expr));
10288 }
10289
10290 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10291 expression, because B will always be true. */
10292
10293 void
10294 warn_for_omitted_condop (location_t location, tree cond)
10295 {
10296 if (truth_value_p (TREE_CODE (cond)))
10297 warning_at (location, OPT_Wparentheses,
10298 "the omitted middle operand in ?: will always be %<true%>, "
10299 "suggest explicit middle operand");
10300 }
10301
10302 /* Give an error for storing into ARG, which is 'const'. USE indicates
10303 how ARG was being used. */
10304
10305 void
10306 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10307 {
10308 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10309 || use == lv_asm);
10310 /* Using this macro rather than (for example) arrays of messages
10311 ensures that all the format strings are checked at compile
10312 time. */
10313 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10314 : (use == lv_increment ? (I) \
10315 : (use == lv_decrement ? (D) : (AS))))
10316 if (TREE_CODE (arg) == COMPONENT_REF)
10317 {
10318 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10319 error_at (loc, READONLY_MSG (G_("assignment of member "
10320 "%qD in read-only object"),
10321 G_("increment of member "
10322 "%qD in read-only object"),
10323 G_("decrement of member "
10324 "%qD in read-only object"),
10325 G_("member %qD in read-only object "
10326 "used as %<asm%> output")),
10327 TREE_OPERAND (arg, 1));
10328 else
10329 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10330 G_("increment of read-only member %qD"),
10331 G_("decrement of read-only member %qD"),
10332 G_("read-only member %qD used as %<asm%> output")),
10333 TREE_OPERAND (arg, 1));
10334 }
10335 else if (VAR_P (arg))
10336 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10337 G_("increment of read-only variable %qD"),
10338 G_("decrement of read-only variable %qD"),
10339 G_("read-only variable %qD used as %<asm%> output")),
10340 arg);
10341 else if (TREE_CODE (arg) == PARM_DECL)
10342 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10343 G_("increment of read-only parameter %qD"),
10344 G_("decrement of read-only parameter %qD"),
10345 G_("read-only parameter %qD use as %<asm%> output")),
10346 arg);
10347 else if (TREE_CODE (arg) == RESULT_DECL)
10348 {
10349 gcc_assert (c_dialect_cxx ());
10350 error_at (loc, READONLY_MSG (G_("assignment of "
10351 "read-only named return value %qD"),
10352 G_("increment of "
10353 "read-only named return value %qD"),
10354 G_("decrement of "
10355 "read-only named return value %qD"),
10356 G_("read-only named return value %qD "
10357 "used as %<asm%>output")),
10358 arg);
10359 }
10360 else if (TREE_CODE (arg) == FUNCTION_DECL)
10361 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10362 G_("increment of function %qD"),
10363 G_("decrement of function %qD"),
10364 G_("function %qD used as %<asm%> output")),
10365 arg);
10366 else
10367 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10368 G_("increment of read-only location %qE"),
10369 G_("decrement of read-only location %qE"),
10370 G_("read-only location %qE used as %<asm%> output")),
10371 arg);
10372 }
10373
10374 /* Print an error message for an invalid lvalue. USE says
10375 how the lvalue is being used and so selects the error message. LOC
10376 is the location for the error. */
10377
10378 void
10379 lvalue_error (location_t loc, enum lvalue_use use)
10380 {
10381 switch (use)
10382 {
10383 case lv_assign:
10384 error_at (loc, "lvalue required as left operand of assignment");
10385 break;
10386 case lv_increment:
10387 error_at (loc, "lvalue required as increment operand");
10388 break;
10389 case lv_decrement:
10390 error_at (loc, "lvalue required as decrement operand");
10391 break;
10392 case lv_addressof:
10393 error_at (loc, "lvalue required as unary %<&%> operand");
10394 break;
10395 case lv_asm:
10396 error_at (loc, "lvalue required in asm statement");
10397 break;
10398 default:
10399 gcc_unreachable ();
10400 }
10401 }
10402
10403 /* Print an error message for an invalid indirection of type TYPE.
10404 ERRSTRING is the name of the operator for the indirection. */
10405
10406 void
10407 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10408 {
10409 switch (errstring)
10410 {
10411 case RO_NULL:
10412 gcc_assert (c_dialect_cxx ());
10413 error_at (loc, "invalid type argument (have %qT)", type);
10414 break;
10415 case RO_ARRAY_INDEXING:
10416 error_at (loc,
10417 "invalid type argument of array indexing (have %qT)",
10418 type);
10419 break;
10420 case RO_UNARY_STAR:
10421 error_at (loc,
10422 "invalid type argument of unary %<*%> (have %qT)",
10423 type);
10424 break;
10425 case RO_ARROW:
10426 error_at (loc,
10427 "invalid type argument of %<->%> (have %qT)",
10428 type);
10429 break;
10430 case RO_ARROW_STAR:
10431 error_at (loc,
10432 "invalid type argument of %<->*%> (have %qT)",
10433 type);
10434 break;
10435 case RO_IMPLICIT_CONVERSION:
10436 error_at (loc,
10437 "invalid type argument of implicit conversion (have %qT)",
10438 type);
10439 break;
10440 default:
10441 gcc_unreachable ();
10442 }
10443 }
10444 \f
10445 /* *PTYPE is an incomplete array. Complete it with a domain based on
10446 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10447 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10448 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10449
10450 int
10451 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10452 {
10453 tree maxindex, type, main_type, elt, unqual_elt;
10454 int failure = 0, quals;
10455 hashval_t hashcode = 0;
10456 bool overflow_p = false;
10457
10458 maxindex = size_zero_node;
10459 if (initial_value)
10460 {
10461 if (TREE_CODE (initial_value) == STRING_CST)
10462 {
10463 int eltsize
10464 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10465 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10466 }
10467 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10468 {
10469 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10470
10471 if (vec_safe_is_empty (v))
10472 {
10473 if (pedantic)
10474 failure = 3;
10475 maxindex = ssize_int (-1);
10476 }
10477 else
10478 {
10479 tree curindex;
10480 unsigned HOST_WIDE_INT cnt;
10481 constructor_elt *ce;
10482 bool fold_p = false;
10483
10484 if ((*v)[0].index)
10485 maxindex = (*v)[0].index, fold_p = true;
10486
10487 curindex = maxindex;
10488
10489 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10490 {
10491 bool curfold_p = false;
10492 if (ce->index)
10493 curindex = ce->index, curfold_p = true;
10494 else
10495 {
10496 if (fold_p)
10497 {
10498 /* Since we treat size types now as ordinary
10499 unsigned types, we need an explicit overflow
10500 check. */
10501 tree orig = curindex;
10502 curindex = fold_convert (sizetype, curindex);
10503 overflow_p |= tree_int_cst_lt (curindex, orig);
10504 }
10505 curindex = size_binop (PLUS_EXPR, curindex,
10506 size_one_node);
10507 }
10508 if (tree_int_cst_lt (maxindex, curindex))
10509 maxindex = curindex, fold_p = curfold_p;
10510 }
10511 if (fold_p)
10512 {
10513 tree orig = maxindex;
10514 maxindex = fold_convert (sizetype, maxindex);
10515 overflow_p |= tree_int_cst_lt (maxindex, orig);
10516 }
10517 }
10518 }
10519 else
10520 {
10521 /* Make an error message unless that happened already. */
10522 if (initial_value != error_mark_node)
10523 failure = 1;
10524 }
10525 }
10526 else
10527 {
10528 failure = 2;
10529 if (!do_default)
10530 return failure;
10531 }
10532
10533 type = *ptype;
10534 elt = TREE_TYPE (type);
10535 quals = TYPE_QUALS (strip_array_types (elt));
10536 if (quals == 0)
10537 unqual_elt = elt;
10538 else
10539 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10540
10541 /* Using build_distinct_type_copy and modifying things afterward instead
10542 of using build_array_type to create a new type preserves all of the
10543 TYPE_LANG_FLAG_? bits that the front end may have set. */
10544 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10545 TREE_TYPE (main_type) = unqual_elt;
10546 TYPE_DOMAIN (main_type)
10547 = build_range_type (TREE_TYPE (maxindex),
10548 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10549 layout_type (main_type);
10550
10551 /* Make sure we have the canonical MAIN_TYPE. */
10552 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10553 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10554 hashcode);
10555 main_type = type_hash_canon (hashcode, main_type);
10556
10557 /* Fix the canonical type. */
10558 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10559 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10560 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10561 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10562 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10563 != TYPE_DOMAIN (main_type)))
10564 TYPE_CANONICAL (main_type)
10565 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10566 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10567 else
10568 TYPE_CANONICAL (main_type) = main_type;
10569
10570 if (quals == 0)
10571 type = main_type;
10572 else
10573 type = c_build_qualified_type (main_type, quals);
10574
10575 if (COMPLETE_TYPE_P (type)
10576 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10577 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10578 {
10579 error ("size of array is too large");
10580 /* If we proceed with the array type as it is, we'll eventually
10581 crash in tree_to_[su]hwi(). */
10582 type = error_mark_node;
10583 }
10584
10585 *ptype = type;
10586 return failure;
10587 }
10588
10589 /* Like c_mark_addressable but don't check register qualifier. */
10590 void
10591 c_common_mark_addressable_vec (tree t)
10592 {
10593 while (handled_component_p (t))
10594 t = TREE_OPERAND (t, 0);
10595 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
10596 return;
10597 TREE_ADDRESSABLE (t) = 1;
10598 }
10599
10600
10601 \f
10602 /* Used to help initialize the builtin-types.def table. When a type of
10603 the correct size doesn't exist, use error_mark_node instead of NULL.
10604 The later results in segfaults even when a decl using the type doesn't
10605 get invoked. */
10606
10607 tree
10608 builtin_type_for_size (int size, bool unsignedp)
10609 {
10610 tree type = c_common_type_for_size (size, unsignedp);
10611 return type ? type : error_mark_node;
10612 }
10613
10614 /* A helper function for resolve_overloaded_builtin in resolving the
10615 overloaded __sync_ builtins. Returns a positive power of 2 if the
10616 first operand of PARAMS is a pointer to a supported data type.
10617 Returns 0 if an error is encountered. */
10618
10619 static int
10620 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10621 {
10622 tree type;
10623 int size;
10624
10625 if (!params)
10626 {
10627 error ("too few arguments to function %qE", function);
10628 return 0;
10629 }
10630
10631 type = TREE_TYPE ((*params)[0]);
10632 if (TREE_CODE (type) == ARRAY_TYPE)
10633 {
10634 /* Force array-to-pointer decay for C++. */
10635 gcc_assert (c_dialect_cxx());
10636 (*params)[0] = default_conversion ((*params)[0]);
10637 type = TREE_TYPE ((*params)[0]);
10638 }
10639 if (TREE_CODE (type) != POINTER_TYPE)
10640 goto incompatible;
10641
10642 type = TREE_TYPE (type);
10643 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10644 goto incompatible;
10645
10646 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10647 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10648 return size;
10649
10650 incompatible:
10651 error ("incompatible type for argument %d of %qE", 1, function);
10652 return 0;
10653 }
10654
10655 /* A helper function for resolve_overloaded_builtin. Adds casts to
10656 PARAMS to make arguments match up with those of FUNCTION. Drops
10657 the variadic arguments at the end. Returns false if some error
10658 was encountered; true on success. */
10659
10660 static bool
10661 sync_resolve_params (location_t loc, tree orig_function, tree function,
10662 vec<tree, va_gc> *params, bool orig_format)
10663 {
10664 function_args_iterator iter;
10665 tree ptype;
10666 unsigned int parmnum;
10667
10668 function_args_iter_init (&iter, TREE_TYPE (function));
10669 /* We've declared the implementation functions to use "volatile void *"
10670 as the pointer parameter, so we shouldn't get any complaints from the
10671 call to check_function_arguments what ever type the user used. */
10672 function_args_iter_next (&iter);
10673 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10674 ptype = TYPE_MAIN_VARIANT (ptype);
10675
10676 /* For the rest of the values, we need to cast these to FTYPE, so that we
10677 don't get warnings for passing pointer types, etc. */
10678 parmnum = 0;
10679 while (1)
10680 {
10681 tree val, arg_type;
10682
10683 arg_type = function_args_iter_cond (&iter);
10684 /* XXX void_type_node belies the abstraction. */
10685 if (arg_type == void_type_node)
10686 break;
10687
10688 ++parmnum;
10689 if (params->length () <= parmnum)
10690 {
10691 error_at (loc, "too few arguments to function %qE", orig_function);
10692 return false;
10693 }
10694
10695 /* Only convert parameters if arg_type is unsigned integer type with
10696 new format sync routines, i.e. don't attempt to convert pointer
10697 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10698 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10699 kinds). */
10700 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10701 {
10702 /* Ideally for the first conversion we'd use convert_for_assignment
10703 so that we get warnings for anything that doesn't match the pointer
10704 type. This isn't portable across the C and C++ front ends atm. */
10705 val = (*params)[parmnum];
10706 val = convert (ptype, val);
10707 val = convert (arg_type, val);
10708 (*params)[parmnum] = val;
10709 }
10710
10711 function_args_iter_next (&iter);
10712 }
10713
10714 /* __atomic routines are not variadic. */
10715 if (!orig_format && params->length () != parmnum + 1)
10716 {
10717 error_at (loc, "too many arguments to function %qE", orig_function);
10718 return false;
10719 }
10720
10721 /* The definition of these primitives is variadic, with the remaining
10722 being "an optional list of variables protected by the memory barrier".
10723 No clue what that's supposed to mean, precisely, but we consider all
10724 call-clobbered variables to be protected so we're safe. */
10725 params->truncate (parmnum + 1);
10726
10727 return true;
10728 }
10729
10730 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10731 RESULT to make it match the type of the first pointer argument in
10732 PARAMS. */
10733
10734 static tree
10735 sync_resolve_return (tree first_param, tree result, bool orig_format)
10736 {
10737 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10738 tree rtype = TREE_TYPE (result);
10739 ptype = TYPE_MAIN_VARIANT (ptype);
10740
10741 /* New format doesn't require casting unless the types are the same size. */
10742 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10743 return convert (ptype, result);
10744 else
10745 return result;
10746 }
10747
10748 /* This function verifies the PARAMS to generic atomic FUNCTION.
10749 It returns the size if all the parameters are the same size, otherwise
10750 0 is returned if the parameters are invalid. */
10751
10752 static int
10753 get_atomic_generic_size (location_t loc, tree function,
10754 vec<tree, va_gc> *params)
10755 {
10756 unsigned int n_param;
10757 unsigned int n_model;
10758 unsigned int x;
10759 int size_0;
10760 tree type_0;
10761
10762 /* Determine the parameter makeup. */
10763 switch (DECL_FUNCTION_CODE (function))
10764 {
10765 case BUILT_IN_ATOMIC_EXCHANGE:
10766 n_param = 4;
10767 n_model = 1;
10768 break;
10769 case BUILT_IN_ATOMIC_LOAD:
10770 case BUILT_IN_ATOMIC_STORE:
10771 n_param = 3;
10772 n_model = 1;
10773 break;
10774 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10775 n_param = 6;
10776 n_model = 2;
10777 break;
10778 default:
10779 gcc_unreachable ();
10780 }
10781
10782 if (vec_safe_length (params) != n_param)
10783 {
10784 error_at (loc, "incorrect number of arguments to function %qE", function);
10785 return 0;
10786 }
10787
10788 /* Get type of first parameter, and determine its size. */
10789 type_0 = TREE_TYPE ((*params)[0]);
10790 if (TREE_CODE (type_0) == ARRAY_TYPE)
10791 {
10792 /* Force array-to-pointer decay for C++. */
10793 gcc_assert (c_dialect_cxx());
10794 (*params)[0] = default_conversion ((*params)[0]);
10795 type_0 = TREE_TYPE ((*params)[0]);
10796 }
10797 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10798 {
10799 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10800 function);
10801 return 0;
10802 }
10803
10804 /* Types must be compile time constant sizes. */
10805 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10806 {
10807 error_at (loc,
10808 "argument 1 of %qE must be a pointer to a constant size type",
10809 function);
10810 return 0;
10811 }
10812
10813 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10814
10815 /* Zero size objects are not allowed. */
10816 if (size_0 == 0)
10817 {
10818 error_at (loc,
10819 "argument 1 of %qE must be a pointer to a nonzero size object",
10820 function);
10821 return 0;
10822 }
10823
10824 /* Check each other parameter is a pointer and the same size. */
10825 for (x = 0; x < n_param - n_model; x++)
10826 {
10827 int size;
10828 tree type = TREE_TYPE ((*params)[x]);
10829 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10830 if (n_param == 6 && x == 3)
10831 continue;
10832 if (!POINTER_TYPE_P (type))
10833 {
10834 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10835 function);
10836 return 0;
10837 }
10838 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10839 size = type_size ? tree_to_uhwi (type_size) : 0;
10840 if (size != size_0)
10841 {
10842 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10843 function);
10844 return 0;
10845 }
10846 }
10847
10848 /* Check memory model parameters for validity. */
10849 for (x = n_param - n_model ; x < n_param; x++)
10850 {
10851 tree p = (*params)[x];
10852 if (TREE_CODE (p) == INTEGER_CST)
10853 {
10854 int i = tree_to_uhwi (p);
10855 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
10856 {
10857 warning_at (loc, OPT_Winvalid_memory_model,
10858 "invalid memory model argument %d of %qE", x + 1,
10859 function);
10860 }
10861 }
10862 else
10863 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10864 {
10865 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10866 function);
10867 return 0;
10868 }
10869 }
10870
10871 return size_0;
10872 }
10873
10874
10875 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10876 at the beginning of the parameter list PARAMS representing the size of the
10877 objects. This is to match the library ABI requirement. LOC is the location
10878 of the function call.
10879 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10880 returned to allow the external call to be constructed. */
10881
10882 static tree
10883 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10884 vec<tree, va_gc> *params)
10885 {
10886 tree size_node;
10887
10888 /* Insert a SIZE_T parameter as the first param. If there isn't
10889 enough space, allocate a new vector and recursively re-build with that. */
10890 if (!params->space (1))
10891 {
10892 unsigned int z, len;
10893 vec<tree, va_gc> *v;
10894 tree f;
10895
10896 len = params->length ();
10897 vec_alloc (v, len + 1);
10898 v->quick_push (build_int_cst (size_type_node, n));
10899 for (z = 0; z < len; z++)
10900 v->quick_push ((*params)[z]);
10901 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10902 vec_free (v);
10903 return f;
10904 }
10905
10906 /* Add the size parameter and leave as a function call for processing. */
10907 size_node = build_int_cst (size_type_node, n);
10908 params->quick_insert (0, size_node);
10909 return NULL_TREE;
10910 }
10911
10912
10913 /* Return whether atomic operations for naturally aligned N-byte
10914 arguments are supported, whether inline or through libatomic. */
10915 static bool
10916 atomic_size_supported_p (int n)
10917 {
10918 switch (n)
10919 {
10920 case 1:
10921 case 2:
10922 case 4:
10923 case 8:
10924 return true;
10925
10926 case 16:
10927 return targetm.scalar_mode_supported_p (TImode);
10928
10929 default:
10930 return false;
10931 }
10932 }
10933
10934 /* This will process an __atomic_exchange function call, determine whether it
10935 needs to be mapped to the _N variation, or turned into a library call.
10936 LOC is the location of the builtin call.
10937 FUNCTION is the DECL that has been invoked;
10938 PARAMS is the argument list for the call. The return value is non-null
10939 TRUE is returned if it is translated into the proper format for a call to the
10940 external library, and NEW_RETURN is set the tree for that function.
10941 FALSE is returned if processing for the _N variation is required, and
10942 NEW_RETURN is set to the return value the result is copied into. */
10943 static bool
10944 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10945 vec<tree, va_gc> *params, tree *new_return)
10946 {
10947 tree p0, p1, p2, p3;
10948 tree I_type, I_type_ptr;
10949 int n = get_atomic_generic_size (loc, function, params);
10950
10951 /* Size of 0 is an error condition. */
10952 if (n == 0)
10953 {
10954 *new_return = error_mark_node;
10955 return true;
10956 }
10957
10958 /* If not a lock-free size, change to the library generic format. */
10959 if (!atomic_size_supported_p (n))
10960 {
10961 *new_return = add_atomic_size_parameter (n, loc, function, params);
10962 return true;
10963 }
10964
10965 /* Otherwise there is a lockfree match, transform the call from:
10966 void fn(T* mem, T* desired, T* return, model)
10967 into
10968 *return = (T) (fn (In* mem, (In) *desired, model)) */
10969
10970 p0 = (*params)[0];
10971 p1 = (*params)[1];
10972 p2 = (*params)[2];
10973 p3 = (*params)[3];
10974
10975 /* Create pointer to appropriate size. */
10976 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10977 I_type_ptr = build_pointer_type (I_type);
10978
10979 /* Convert object pointer to required type. */
10980 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10981 (*params)[0] = p0;
10982 /* Convert new value to required type, and dereference it. */
10983 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10984 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10985 (*params)[1] = p1;
10986
10987 /* Move memory model to the 3rd position, and end param list. */
10988 (*params)[2] = p3;
10989 params->truncate (3);
10990
10991 /* Convert return pointer and dereference it for later assignment. */
10992 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10993
10994 return false;
10995 }
10996
10997
10998 /* This will process an __atomic_compare_exchange function call, determine
10999 whether it needs to be mapped to the _N variation, or turned into a lib call.
11000 LOC is the location of the builtin call.
11001 FUNCTION is the DECL that has been invoked;
11002 PARAMS is the argument list for the call. The return value is non-null
11003 TRUE is returned if it is translated into the proper format for a call to the
11004 external library, and NEW_RETURN is set the tree for that function.
11005 FALSE is returned if processing for the _N variation is required. */
11006
11007 static bool
11008 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11009 vec<tree, va_gc> *params,
11010 tree *new_return)
11011 {
11012 tree p0, p1, p2;
11013 tree I_type, I_type_ptr;
11014 int n = get_atomic_generic_size (loc, function, params);
11015
11016 /* Size of 0 is an error condition. */
11017 if (n == 0)
11018 {
11019 *new_return = error_mark_node;
11020 return true;
11021 }
11022
11023 /* If not a lock-free size, change to the library generic format. */
11024 if (!atomic_size_supported_p (n))
11025 {
11026 /* The library generic format does not have the weak parameter, so
11027 remove it from the param list. Since a parameter has been removed,
11028 we can be sure that there is room for the SIZE_T parameter, meaning
11029 there will not be a recursive rebuilding of the parameter list, so
11030 there is no danger this will be done twice. */
11031 if (n > 0)
11032 {
11033 (*params)[3] = (*params)[4];
11034 (*params)[4] = (*params)[5];
11035 params->truncate (5);
11036 }
11037 *new_return = add_atomic_size_parameter (n, loc, function, params);
11038 return true;
11039 }
11040
11041 /* Otherwise, there is a match, so the call needs to be transformed from:
11042 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11043 into
11044 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11045
11046 p0 = (*params)[0];
11047 p1 = (*params)[1];
11048 p2 = (*params)[2];
11049
11050 /* Create pointer to appropriate size. */
11051 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11052 I_type_ptr = build_pointer_type (I_type);
11053
11054 /* Convert object pointer to required type. */
11055 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11056 (*params)[0] = p0;
11057
11058 /* Convert expected pointer to required type. */
11059 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11060 (*params)[1] = p1;
11061
11062 /* Convert desired value to required type, and dereference it. */
11063 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11064 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11065 (*params)[2] = p2;
11066
11067 /* The rest of the parameters are fine. NULL means no special return value
11068 processing.*/
11069 *new_return = NULL;
11070 return false;
11071 }
11072
11073
11074 /* This will process an __atomic_load function call, determine whether it
11075 needs to be mapped to the _N variation, or turned into a library call.
11076 LOC is the location of the builtin call.
11077 FUNCTION is the DECL that has been invoked;
11078 PARAMS is the argument list for the call. The return value is non-null
11079 TRUE is returned if it is translated into the proper format for a call to the
11080 external library, and NEW_RETURN is set the tree for that function.
11081 FALSE is returned if processing for the _N variation is required, and
11082 NEW_RETURN is set to the return value the result is copied into. */
11083
11084 static bool
11085 resolve_overloaded_atomic_load (location_t loc, tree function,
11086 vec<tree, va_gc> *params, tree *new_return)
11087 {
11088 tree p0, p1, p2;
11089 tree I_type, I_type_ptr;
11090 int n = get_atomic_generic_size (loc, function, params);
11091
11092 /* Size of 0 is an error condition. */
11093 if (n == 0)
11094 {
11095 *new_return = error_mark_node;
11096 return true;
11097 }
11098
11099 /* If not a lock-free size, change to the library generic format. */
11100 if (!atomic_size_supported_p (n))
11101 {
11102 *new_return = add_atomic_size_parameter (n, loc, function, params);
11103 return true;
11104 }
11105
11106 /* Otherwise, there is a match, so the call needs to be transformed from:
11107 void fn(T* mem, T* return, model)
11108 into
11109 *return = (T) (fn ((In *) mem, model)) */
11110
11111 p0 = (*params)[0];
11112 p1 = (*params)[1];
11113 p2 = (*params)[2];
11114
11115 /* Create pointer to appropriate size. */
11116 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11117 I_type_ptr = build_pointer_type (I_type);
11118
11119 /* Convert object pointer to required type. */
11120 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11121 (*params)[0] = p0;
11122
11123 /* Move memory model to the 2nd position, and end param list. */
11124 (*params)[1] = p2;
11125 params->truncate (2);
11126
11127 /* Convert return pointer and dereference it for later assignment. */
11128 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11129
11130 return false;
11131 }
11132
11133
11134 /* This will process an __atomic_store function call, determine whether it
11135 needs to be mapped to the _N variation, or turned into a library call.
11136 LOC is the location of the builtin call.
11137 FUNCTION is the DECL that has been invoked;
11138 PARAMS is the argument list for the call. The return value is non-null
11139 TRUE is returned if it is translated into the proper format for a call to the
11140 external library, and NEW_RETURN is set the tree for that function.
11141 FALSE is returned if processing for the _N variation is required, and
11142 NEW_RETURN is set to the return value the result is copied into. */
11143
11144 static bool
11145 resolve_overloaded_atomic_store (location_t loc, tree function,
11146 vec<tree, va_gc> *params, tree *new_return)
11147 {
11148 tree p0, p1;
11149 tree I_type, I_type_ptr;
11150 int n = get_atomic_generic_size (loc, function, params);
11151
11152 /* Size of 0 is an error condition. */
11153 if (n == 0)
11154 {
11155 *new_return = error_mark_node;
11156 return true;
11157 }
11158
11159 /* If not a lock-free size, change to the library generic format. */
11160 if (!atomic_size_supported_p (n))
11161 {
11162 *new_return = add_atomic_size_parameter (n, loc, function, params);
11163 return true;
11164 }
11165
11166 /* Otherwise, there is a match, so the call needs to be transformed from:
11167 void fn(T* mem, T* value, model)
11168 into
11169 fn ((In *) mem, (In) *value, model) */
11170
11171 p0 = (*params)[0];
11172 p1 = (*params)[1];
11173
11174 /* Create pointer to appropriate size. */
11175 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11176 I_type_ptr = build_pointer_type (I_type);
11177
11178 /* Convert object pointer to required type. */
11179 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11180 (*params)[0] = p0;
11181
11182 /* Convert new value to required type, and dereference it. */
11183 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11184 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11185 (*params)[1] = p1;
11186
11187 /* The memory model is in the right spot already. Return is void. */
11188 *new_return = NULL_TREE;
11189
11190 return false;
11191 }
11192
11193
11194 /* Some builtin functions are placeholders for other expressions. This
11195 function should be called immediately after parsing the call expression
11196 before surrounding code has committed to the type of the expression.
11197
11198 LOC is the location of the builtin call.
11199
11200 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11201 PARAMS is the argument list for the call. The return value is non-null
11202 when expansion is complete, and null if normal processing should
11203 continue. */
11204
11205 tree
11206 resolve_overloaded_builtin (location_t loc, tree function,
11207 vec<tree, va_gc> *params)
11208 {
11209 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11210 bool orig_format = true;
11211 tree new_return = NULL_TREE;
11212
11213 switch (DECL_BUILT_IN_CLASS (function))
11214 {
11215 case BUILT_IN_NORMAL:
11216 break;
11217 case BUILT_IN_MD:
11218 if (targetm.resolve_overloaded_builtin)
11219 return targetm.resolve_overloaded_builtin (loc, function, params);
11220 else
11221 return NULL_TREE;
11222 default:
11223 return NULL_TREE;
11224 }
11225
11226 /* Handle BUILT_IN_NORMAL here. */
11227 switch (orig_code)
11228 {
11229 case BUILT_IN_ATOMIC_EXCHANGE:
11230 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11231 case BUILT_IN_ATOMIC_LOAD:
11232 case BUILT_IN_ATOMIC_STORE:
11233 {
11234 /* Handle these 4 together so that they can fall through to the next
11235 case if the call is transformed to an _N variant. */
11236 switch (orig_code)
11237 {
11238 case BUILT_IN_ATOMIC_EXCHANGE:
11239 {
11240 if (resolve_overloaded_atomic_exchange (loc, function, params,
11241 &new_return))
11242 return new_return;
11243 /* Change to the _N variant. */
11244 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11245 break;
11246 }
11247
11248 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11249 {
11250 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11251 params,
11252 &new_return))
11253 return new_return;
11254 /* Change to the _N variant. */
11255 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11256 break;
11257 }
11258 case BUILT_IN_ATOMIC_LOAD:
11259 {
11260 if (resolve_overloaded_atomic_load (loc, function, params,
11261 &new_return))
11262 return new_return;
11263 /* Change to the _N variant. */
11264 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11265 break;
11266 }
11267 case BUILT_IN_ATOMIC_STORE:
11268 {
11269 if (resolve_overloaded_atomic_store (loc, function, params,
11270 &new_return))
11271 return new_return;
11272 /* Change to the _N variant. */
11273 orig_code = BUILT_IN_ATOMIC_STORE_N;
11274 break;
11275 }
11276 default:
11277 gcc_unreachable ();
11278 }
11279 /* Fallthrough to the normal processing. */
11280 }
11281 case BUILT_IN_ATOMIC_EXCHANGE_N:
11282 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11283 case BUILT_IN_ATOMIC_LOAD_N:
11284 case BUILT_IN_ATOMIC_STORE_N:
11285 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11286 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11287 case BUILT_IN_ATOMIC_AND_FETCH_N:
11288 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11289 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11290 case BUILT_IN_ATOMIC_OR_FETCH_N:
11291 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11292 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11293 case BUILT_IN_ATOMIC_FETCH_AND_N:
11294 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11295 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11296 case BUILT_IN_ATOMIC_FETCH_OR_N:
11297 {
11298 orig_format = false;
11299 /* Fallthru for parameter processing. */
11300 }
11301 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11302 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11303 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11304 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11305 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11306 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11307 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11308 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11309 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11310 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11311 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11312 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11313 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11314 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11315 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11316 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11317 {
11318 int n = sync_resolve_size (function, params);
11319 tree new_function, first_param, result;
11320 enum built_in_function fncode;
11321
11322 if (n == 0)
11323 return error_mark_node;
11324
11325 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11326 new_function = builtin_decl_explicit (fncode);
11327 if (!sync_resolve_params (loc, function, new_function, params,
11328 orig_format))
11329 return error_mark_node;
11330
11331 first_param = (*params)[0];
11332 result = build_function_call_vec (loc, vNULL, new_function, params,
11333 NULL);
11334 if (result == error_mark_node)
11335 return result;
11336 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11337 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11338 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11339 result = sync_resolve_return (first_param, result, orig_format);
11340
11341 /* If new_return is set, assign function to that expr and cast the
11342 result to void since the generic interface returned void. */
11343 if (new_return)
11344 {
11345 /* Cast function result from I{1,2,4,8,16} to the required type. */
11346 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11347 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11348 result);
11349 TREE_SIDE_EFFECTS (result) = 1;
11350 protected_set_expr_location (result, loc);
11351 result = convert (void_type_node, result);
11352 }
11353 return result;
11354 }
11355
11356 default:
11357 return NULL_TREE;
11358 }
11359 }
11360
11361 /* vector_types_compatible_elements_p is used in type checks of vectors
11362 values used as operands of binary operators. Where it returns true, and
11363 the other checks of the caller succeed (being vector types in he first
11364 place, and matching number of elements), we can just treat the types
11365 as essentially the same.
11366 Contrast with vector_targets_convertible_p, which is used for vector
11367 pointer types, and vector_types_convertible_p, which will allow
11368 language-specific matches under the control of flag_lax_vector_conversions,
11369 and might still require a conversion. */
11370 /* True if vector types T1 and T2 can be inputs to the same binary
11371 operator without conversion.
11372 We don't check the overall vector size here because some of our callers
11373 want to give different error messages when the vectors are compatible
11374 except for the element count. */
11375
11376 bool
11377 vector_types_compatible_elements_p (tree t1, tree t2)
11378 {
11379 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11380 t1 = TREE_TYPE (t1);
11381 t2 = TREE_TYPE (t2);
11382
11383 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11384
11385 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11386 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11387 || c2 == FIXED_POINT_TYPE));
11388
11389 t1 = c_common_signed_type (t1);
11390 t2 = c_common_signed_type (t2);
11391 /* Equality works here because c_common_signed_type uses
11392 TYPE_MAIN_VARIANT. */
11393 if (t1 == t2)
11394 return true;
11395 if (opaque && c1 == c2
11396 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11397 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11398 return true;
11399 return false;
11400 }
11401
11402 /* Check for missing format attributes on function pointers. LTYPE is
11403 the new type or left-hand side type. RTYPE is the old type or
11404 right-hand side type. Returns TRUE if LTYPE is missing the desired
11405 attribute. */
11406
11407 bool
11408 check_missing_format_attribute (tree ltype, tree rtype)
11409 {
11410 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11411 tree ra;
11412
11413 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11414 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11415 break;
11416 if (ra)
11417 {
11418 tree la;
11419 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11420 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11421 break;
11422 return !la;
11423 }
11424 else
11425 return false;
11426 }
11427
11428 /* Subscripting with type char is likely to lose on a machine where
11429 chars are signed. So warn on any machine, but optionally. Don't
11430 warn for unsigned char since that type is safe. Don't warn for
11431 signed char because anyone who uses that must have done so
11432 deliberately. Furthermore, we reduce the false positive load by
11433 warning only for non-constant value of type char. */
11434
11435 void
11436 warn_array_subscript_with_type_char (location_t loc, tree index)
11437 {
11438 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11439 && TREE_CODE (index) != INTEGER_CST)
11440 warning_at (loc, OPT_Wchar_subscripts,
11441 "array subscript has type %<char%>");
11442 }
11443
11444 /* Implement -Wparentheses for the unexpected C precedence rules, to
11445 cover cases like x + y << z which readers are likely to
11446 misinterpret. We have seen an expression in which CODE is a binary
11447 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11448 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11449 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11450 expression was not formed using a binary or unary operator, or it
11451 was enclosed in parentheses. */
11452
11453 void
11454 warn_about_parentheses (location_t loc, enum tree_code code,
11455 enum tree_code code_left, tree arg_left,
11456 enum tree_code code_right, tree arg_right)
11457 {
11458 if (!warn_parentheses)
11459 return;
11460
11461 /* This macro tests that the expression ARG with original tree code
11462 CODE appears to be a boolean expression. or the result of folding a
11463 boolean expression. */
11464 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11465 (truth_value_p (TREE_CODE (ARG)) \
11466 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11467 /* Folding may create 0 or 1 integers from other expressions. */ \
11468 || ((CODE) != INTEGER_CST \
11469 && (integer_onep (ARG) || integer_zerop (ARG))))
11470
11471 switch (code)
11472 {
11473 case LSHIFT_EXPR:
11474 if (code_left == PLUS_EXPR)
11475 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11476 "suggest parentheses around %<+%> inside %<<<%>");
11477 else if (code_right == PLUS_EXPR)
11478 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11479 "suggest parentheses around %<+%> inside %<<<%>");
11480 else if (code_left == MINUS_EXPR)
11481 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11482 "suggest parentheses around %<-%> inside %<<<%>");
11483 else if (code_right == MINUS_EXPR)
11484 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11485 "suggest parentheses around %<-%> inside %<<<%>");
11486 return;
11487
11488 case RSHIFT_EXPR:
11489 if (code_left == PLUS_EXPR)
11490 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11491 "suggest parentheses around %<+%> inside %<>>%>");
11492 else if (code_right == PLUS_EXPR)
11493 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11494 "suggest parentheses around %<+%> inside %<>>%>");
11495 else if (code_left == MINUS_EXPR)
11496 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11497 "suggest parentheses around %<-%> inside %<>>%>");
11498 else if (code_right == MINUS_EXPR)
11499 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11500 "suggest parentheses around %<-%> inside %<>>%>");
11501 return;
11502
11503 case TRUTH_ORIF_EXPR:
11504 if (code_left == TRUTH_ANDIF_EXPR)
11505 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11506 "suggest parentheses around %<&&%> within %<||%>");
11507 else if (code_right == TRUTH_ANDIF_EXPR)
11508 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11509 "suggest parentheses around %<&&%> within %<||%>");
11510 return;
11511
11512 case BIT_IOR_EXPR:
11513 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11514 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11515 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11516 "suggest parentheses around arithmetic in operand of %<|%>");
11517 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11518 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11519 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11520 "suggest parentheses around arithmetic in operand of %<|%>");
11521 /* Check cases like x|y==z */
11522 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11523 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11524 "suggest parentheses around comparison in operand of %<|%>");
11525 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11526 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11527 "suggest parentheses around comparison in operand of %<|%>");
11528 /* Check cases like !x | y */
11529 else if (code_left == TRUTH_NOT_EXPR
11530 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11531 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11532 "suggest parentheses around operand of "
11533 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11534 return;
11535
11536 case BIT_XOR_EXPR:
11537 if (code_left == BIT_AND_EXPR
11538 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11539 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11540 "suggest parentheses around arithmetic in operand of %<^%>");
11541 else if (code_right == BIT_AND_EXPR
11542 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11543 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11544 "suggest parentheses around arithmetic in operand of %<^%>");
11545 /* Check cases like x^y==z */
11546 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11547 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11548 "suggest parentheses around comparison in operand of %<^%>");
11549 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11550 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11551 "suggest parentheses around comparison in operand of %<^%>");
11552 return;
11553
11554 case BIT_AND_EXPR:
11555 if (code_left == PLUS_EXPR)
11556 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11557 "suggest parentheses around %<+%> in operand of %<&%>");
11558 else if (code_right == PLUS_EXPR)
11559 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11560 "suggest parentheses around %<+%> in operand of %<&%>");
11561 else if (code_left == MINUS_EXPR)
11562 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11563 "suggest parentheses around %<-%> in operand of %<&%>");
11564 else if (code_right == MINUS_EXPR)
11565 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11566 "suggest parentheses around %<-%> in operand of %<&%>");
11567 /* Check cases like x&y==z */
11568 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11569 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11570 "suggest parentheses around comparison in operand of %<&%>");
11571 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11572 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11573 "suggest parentheses around comparison in operand of %<&%>");
11574 /* Check cases like !x & y */
11575 else if (code_left == TRUTH_NOT_EXPR
11576 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11577 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11578 "suggest parentheses around operand of "
11579 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11580 return;
11581
11582 case EQ_EXPR:
11583 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11584 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11585 "suggest parentheses around comparison in operand of %<==%>");
11586 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11587 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11588 "suggest parentheses around comparison in operand of %<==%>");
11589 return;
11590 case NE_EXPR:
11591 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11592 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11593 "suggest parentheses around comparison in operand of %<!=%>");
11594 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11595 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11596 "suggest parentheses around comparison in operand of %<!=%>");
11597 return;
11598
11599 default:
11600 if (TREE_CODE_CLASS (code) == tcc_comparison)
11601 {
11602 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11603 && code_left != NE_EXPR && code_left != EQ_EXPR
11604 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11605 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11606 "comparisons like %<X<=Y<=Z%> do not "
11607 "have their mathematical meaning");
11608 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11609 && code_right != NE_EXPR && code_right != EQ_EXPR
11610 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11611 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11612 "comparisons like %<X<=Y<=Z%> do not "
11613 "have their mathematical meaning");
11614 }
11615 return;
11616 }
11617 #undef NOT_A_BOOLEAN_EXPR_P
11618 }
11619
11620 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11621
11622 void
11623 warn_for_unused_label (tree label)
11624 {
11625 if (!TREE_USED (label))
11626 {
11627 if (DECL_INITIAL (label))
11628 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11629 else
11630 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11631 }
11632 }
11633
11634 /* Warn for division by zero according to the value of DIVISOR. LOC
11635 is the location of the division operator. */
11636
11637 void
11638 warn_for_div_by_zero (location_t loc, tree divisor)
11639 {
11640 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11641 about division by zero. Do not issue a warning if DIVISOR has a
11642 floating-point type, since we consider 0.0/0.0 a valid way of
11643 generating a NaN. */
11644 if (c_inhibit_evaluation_warnings == 0
11645 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11646 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11647 }
11648
11649 /* Subroutine of build_binary_op. Give warnings for comparisons
11650 between signed and unsigned quantities that may fail. Do the
11651 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11652 so that casts will be considered, but default promotions won't
11653 be.
11654
11655 LOCATION is the location of the comparison operator.
11656
11657 The arguments of this function map directly to local variables
11658 of build_binary_op. */
11659
11660 void
11661 warn_for_sign_compare (location_t location,
11662 tree orig_op0, tree orig_op1,
11663 tree op0, tree op1,
11664 tree result_type, enum tree_code resultcode)
11665 {
11666 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11667 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11668 int unsignedp0, unsignedp1;
11669
11670 /* In C++, check for comparison of different enum types. */
11671 if (c_dialect_cxx()
11672 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11673 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11674 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11675 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11676 {
11677 warning_at (location,
11678 OPT_Wsign_compare, "comparison between types %qT and %qT",
11679 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11680 }
11681
11682 /* Do not warn if the comparison is being done in a signed type,
11683 since the signed type will only be chosen if it can represent
11684 all the values of the unsigned type. */
11685 if (!TYPE_UNSIGNED (result_type))
11686 /* OK */;
11687 /* Do not warn if both operands are unsigned. */
11688 else if (op0_signed == op1_signed)
11689 /* OK */;
11690 else
11691 {
11692 tree sop, uop, base_type;
11693 bool ovf;
11694
11695 if (op0_signed)
11696 sop = orig_op0, uop = orig_op1;
11697 else
11698 sop = orig_op1, uop = orig_op0;
11699
11700 STRIP_TYPE_NOPS (sop);
11701 STRIP_TYPE_NOPS (uop);
11702 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11703 ? TREE_TYPE (result_type) : result_type);
11704
11705 /* Do not warn if the signed quantity is an unsuffixed integer
11706 literal (or some static constant expression involving such
11707 literals or a conditional expression involving such literals)
11708 and it is non-negative. */
11709 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11710 /* OK */;
11711 /* Do not warn if the comparison is an equality operation, the
11712 unsigned quantity is an integral constant, and it would fit
11713 in the result if the result were signed. */
11714 else if (TREE_CODE (uop) == INTEGER_CST
11715 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11716 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11717 /* OK */;
11718 /* In C, do not warn if the unsigned quantity is an enumeration
11719 constant and its maximum value would fit in the result if the
11720 result were signed. */
11721 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11722 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11723 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11724 c_common_signed_type (base_type)))
11725 /* OK */;
11726 else
11727 warning_at (location,
11728 OPT_Wsign_compare,
11729 "comparison between signed and unsigned integer expressions");
11730 }
11731
11732 /* Warn if two unsigned values are being compared in a size larger
11733 than their original size, and one (and only one) is the result of
11734 a `~' operator. This comparison will always fail.
11735
11736 Also warn if one operand is a constant, and the constant does not
11737 have all bits set that are set in the ~ operand when it is
11738 extended. */
11739
11740 op0 = c_common_get_narrower (op0, &unsignedp0);
11741 op1 = c_common_get_narrower (op1, &unsignedp1);
11742
11743 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11744 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11745 {
11746 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11747 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11748 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11749 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11750
11751 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11752 {
11753 tree primop;
11754 HOST_WIDE_INT constant, mask;
11755 int unsignedp;
11756 unsigned int bits;
11757
11758 if (tree_fits_shwi_p (op0))
11759 {
11760 primop = op1;
11761 unsignedp = unsignedp1;
11762 constant = tree_to_shwi (op0);
11763 }
11764 else
11765 {
11766 primop = op0;
11767 unsignedp = unsignedp0;
11768 constant = tree_to_shwi (op1);
11769 }
11770
11771 bits = TYPE_PRECISION (TREE_TYPE (primop));
11772 if (bits < TYPE_PRECISION (result_type)
11773 && bits < HOST_BITS_PER_LONG && unsignedp)
11774 {
11775 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
11776 if ((mask & constant) != mask)
11777 {
11778 if (constant == 0)
11779 warning_at (location, OPT_Wsign_compare,
11780 "promoted ~unsigned is always non-zero");
11781 else
11782 warning_at (location, OPT_Wsign_compare,
11783 "comparison of promoted ~unsigned with constant");
11784 }
11785 }
11786 }
11787 else if (unsignedp0 && unsignedp1
11788 && (TYPE_PRECISION (TREE_TYPE (op0))
11789 < TYPE_PRECISION (result_type))
11790 && (TYPE_PRECISION (TREE_TYPE (op1))
11791 < TYPE_PRECISION (result_type)))
11792 warning_at (location, OPT_Wsign_compare,
11793 "comparison of promoted ~unsigned with unsigned");
11794 }
11795 }
11796
11797 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11798 type via c_common_type. If -Wdouble-promotion is in use, and the
11799 conditions for warning have been met, issue a warning. GMSGID is
11800 the warning message. It must have two %T specifiers for the type
11801 that was converted (generally "float") and the type to which it was
11802 converted (generally "double), respectively. LOC is the location
11803 to which the awrning should refer. */
11804
11805 void
11806 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11807 const char *gmsgid, location_t loc)
11808 {
11809 tree source_type;
11810
11811 if (!warn_double_promotion)
11812 return;
11813 /* If the conversion will not occur at run-time, there is no need to
11814 warn about it. */
11815 if (c_inhibit_evaluation_warnings)
11816 return;
11817 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11818 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11819 return;
11820 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11821 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11822 source_type = type1;
11823 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11824 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11825 source_type = type2;
11826 else
11827 return;
11828 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11829 }
11830
11831 /* Possibly warn about unused parameters. */
11832
11833 void
11834 do_warn_unused_parameter (tree fn)
11835 {
11836 tree decl;
11837
11838 for (decl = DECL_ARGUMENTS (fn);
11839 decl; decl = DECL_CHAIN (decl))
11840 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
11841 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
11842 && !TREE_NO_WARNING (decl))
11843 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
11844 "unused parameter %qD", decl);
11845 }
11846
11847
11848 /* Setup a TYPE_DECL node as a typedef representation.
11849
11850 X is a TYPE_DECL for a typedef statement. Create a brand new
11851 ..._TYPE node (which will be just a variant of the existing
11852 ..._TYPE node with identical properties) and then install X
11853 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11854
11855 The whole point here is to end up with a situation where each
11856 and every ..._TYPE node the compiler creates will be uniquely
11857 associated with AT MOST one node representing a typedef name.
11858 This way, even though the compiler substitutes corresponding
11859 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11860 early on, later parts of the compiler can always do the reverse
11861 translation and get back the corresponding typedef name. For
11862 example, given:
11863
11864 typedef struct S MY_TYPE;
11865 MY_TYPE object;
11866
11867 Later parts of the compiler might only know that `object' was of
11868 type `struct S' if it were not for code just below. With this
11869 code however, later parts of the compiler see something like:
11870
11871 struct S' == struct S
11872 typedef struct S' MY_TYPE;
11873 struct S' object;
11874
11875 And they can then deduce (from the node for type struct S') that
11876 the original object declaration was:
11877
11878 MY_TYPE object;
11879
11880 Being able to do this is important for proper support of protoize,
11881 and also for generating precise symbolic debugging information
11882 which takes full account of the programmer's (typedef) vocabulary.
11883
11884 Obviously, we don't want to generate a duplicate ..._TYPE node if
11885 the TYPE_DECL node that we are now processing really represents a
11886 standard built-in type. */
11887
11888 void
11889 set_underlying_type (tree x)
11890 {
11891 if (x == error_mark_node)
11892 return;
11893 if (DECL_IS_BUILTIN (x))
11894 {
11895 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11896 TYPE_NAME (TREE_TYPE (x)) = x;
11897 }
11898 else if (TREE_TYPE (x) != error_mark_node
11899 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11900 {
11901 tree tt = TREE_TYPE (x);
11902 DECL_ORIGINAL_TYPE (x) = tt;
11903 tt = build_variant_type_copy (tt);
11904 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11905 TYPE_NAME (tt) = x;
11906 TREE_USED (tt) = TREE_USED (x);
11907 TREE_TYPE (x) = tt;
11908 }
11909 }
11910
11911 /* Record the types used by the current global variable declaration
11912 being parsed, so that we can decide later to emit their debug info.
11913 Those types are in types_used_by_cur_var_decl, and we are going to
11914 store them in the types_used_by_vars_hash hash table.
11915 DECL is the declaration of the global variable that has been parsed. */
11916
11917 void
11918 record_types_used_by_current_var_decl (tree decl)
11919 {
11920 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11921
11922 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11923 {
11924 tree type = types_used_by_cur_var_decl->pop ();
11925 types_used_by_var_decl_insert (type, decl);
11926 }
11927 }
11928
11929 /* If DECL is a typedef that is declared in the current function,
11930 record it for the purpose of -Wunused-local-typedefs. */
11931
11932 void
11933 record_locally_defined_typedef (tree decl)
11934 {
11935 struct c_language_function *l;
11936
11937 if (!warn_unused_local_typedefs
11938 || cfun == NULL
11939 /* if this is not a locally defined typedef then we are not
11940 interested. */
11941 || !is_typedef_decl (decl)
11942 || !decl_function_context (decl))
11943 return;
11944
11945 l = (struct c_language_function *) cfun->language;
11946 vec_safe_push (l->local_typedefs, decl);
11947 }
11948
11949 /* If T is a TYPE_DECL declared locally, mark it as used. */
11950
11951 void
11952 maybe_record_typedef_use (tree t)
11953 {
11954 if (!is_typedef_decl (t))
11955 return;
11956
11957 TREE_USED (t) = true;
11958 }
11959
11960 /* Warn if there are some unused locally defined typedefs in the
11961 current function. */
11962
11963 void
11964 maybe_warn_unused_local_typedefs (void)
11965 {
11966 int i;
11967 tree decl;
11968 /* The number of times we have emitted -Wunused-local-typedefs
11969 warnings. If this is different from errorcount, that means some
11970 unrelated errors have been issued. In which case, we'll avoid
11971 emitting "unused-local-typedefs" warnings. */
11972 static int unused_local_typedefs_warn_count;
11973 struct c_language_function *l;
11974
11975 if (cfun == NULL)
11976 return;
11977
11978 if ((l = (struct c_language_function *) cfun->language) == NULL)
11979 return;
11980
11981 if (warn_unused_local_typedefs
11982 && errorcount == unused_local_typedefs_warn_count)
11983 {
11984 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11985 if (!TREE_USED (decl))
11986 warning_at (DECL_SOURCE_LOCATION (decl),
11987 OPT_Wunused_local_typedefs,
11988 "typedef %qD locally defined but not used", decl);
11989 unused_local_typedefs_warn_count = errorcount;
11990 }
11991
11992 vec_free (l->local_typedefs);
11993 }
11994
11995 /* Warn about boolean expression compared with an integer value different
11996 from true/false. Warns also e.g. about "(i1 == i2) == 2".
11997 LOC is the location of the comparison, CODE is its code, OP0 and OP1
11998 are the operands of the comparison. The caller must ensure that
11999 either operand is a boolean expression. */
12000
12001 void
12002 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12003 tree op1)
12004 {
12005 if (TREE_CODE_CLASS (code) != tcc_comparison)
12006 return;
12007
12008 tree f, cst;
12009 if (f = fold_for_warn (op0),
12010 TREE_CODE (f) == INTEGER_CST)
12011 cst = op0 = f;
12012 else if (f = fold_for_warn (op1),
12013 TREE_CODE (f) == INTEGER_CST)
12014 cst = op1 = f;
12015 else
12016 return;
12017
12018 if (!integer_zerop (cst) && !integer_onep (cst))
12019 {
12020 int sign = (TREE_CODE (op0) == INTEGER_CST
12021 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
12022 if (code == EQ_EXPR
12023 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12024 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12025 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12026 "with boolean expression is always false", cst);
12027 else
12028 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12029 "with boolean expression is always true", cst);
12030 }
12031 else if (integer_zerop (cst) || integer_onep (cst))
12032 {
12033 /* If the non-constant operand isn't of a boolean type, we
12034 don't want to warn here. */
12035 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12036 /* Handle booleans promoted to integers. */
12037 if (CONVERT_EXPR_P (noncst)
12038 && TREE_TYPE (noncst) == integer_type_node
12039 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12040 /* Warn. */;
12041 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12042 && !truth_value_p (TREE_CODE (noncst)))
12043 return;
12044 /* Do some magic to get the right diagnostics. */
12045 bool flag = TREE_CODE (op0) == INTEGER_CST;
12046 flag = integer_zerop (cst) ? flag : !flag;
12047 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12048 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12049 "with boolean expression is always true", cst);
12050 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12051 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12052 "with boolean expression is always false", cst);
12053 }
12054 }
12055
12056 /* Warn if signed left shift overflows. We don't warn
12057 about left-shifting 1 into the sign bit in C++14; cf.
12058 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12059 LOC is a location of the shift; OP0 and OP1 are the operands.
12060 Return true if an overflow is detected, false otherwise. */
12061
12062 bool
12063 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12064 {
12065 if (TREE_CODE (op0) != INTEGER_CST
12066 || TREE_CODE (op1) != INTEGER_CST)
12067 return false;
12068
12069 tree type0 = TREE_TYPE (op0);
12070 unsigned int prec0 = TYPE_PRECISION (type0);
12071
12072 /* Left-hand operand must be signed. */
12073 if (TYPE_UNSIGNED (type0))
12074 return false;
12075
12076 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12077 + TREE_INT_CST_LOW (op1));
12078 /* Handle the left-shifting 1 into the sign bit case. */
12079 if (min_prec == prec0 + 1)
12080 {
12081 /* Never warn for C++14 onwards. */
12082 if (cxx_dialect >= cxx14)
12083 return false;
12084 /* Otherwise only if -Wshift-overflow=2. But return
12085 true to signal an overflow for the sake of integer
12086 constant expressions. */
12087 if (warn_shift_overflow < 2)
12088 return true;
12089 }
12090
12091 bool overflowed = min_prec > prec0;
12092 if (overflowed && c_inhibit_evaluation_warnings == 0)
12093 warning_at (loc, OPT_Wshift_overflow_,
12094 "result of %qE requires %u bits to represent, "
12095 "but %qT only has %u bits",
12096 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12097 min_prec, type0, prec0);
12098
12099 return overflowed;
12100 }
12101
12102 /* The C and C++ parsers both use vectors to hold function arguments.
12103 For efficiency, we keep a cache of unused vectors. This is the
12104 cache. */
12105
12106 typedef vec<tree, va_gc> *tree_gc_vec;
12107 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12108
12109 /* Return a new vector from the cache. If the cache is empty,
12110 allocate a new vector. These vectors are GC'ed, so it is OK if the
12111 pointer is not released.. */
12112
12113 vec<tree, va_gc> *
12114 make_tree_vector (void)
12115 {
12116 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12117 return tree_vector_cache->pop ();
12118 else
12119 {
12120 /* Passing 0 to vec::alloc returns NULL, and our callers require
12121 that we always return a non-NULL value. The vector code uses
12122 4 when growing a NULL vector, so we do too. */
12123 vec<tree, va_gc> *v;
12124 vec_alloc (v, 4);
12125 return v;
12126 }
12127 }
12128
12129 /* Release a vector of trees back to the cache. */
12130
12131 void
12132 release_tree_vector (vec<tree, va_gc> *vec)
12133 {
12134 if (vec != NULL)
12135 {
12136 vec->truncate (0);
12137 vec_safe_push (tree_vector_cache, vec);
12138 }
12139 }
12140
12141 /* Get a new tree vector holding a single tree. */
12142
12143 vec<tree, va_gc> *
12144 make_tree_vector_single (tree t)
12145 {
12146 vec<tree, va_gc> *ret = make_tree_vector ();
12147 ret->quick_push (t);
12148 return ret;
12149 }
12150
12151 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12152
12153 vec<tree, va_gc> *
12154 make_tree_vector_from_list (tree list)
12155 {
12156 vec<tree, va_gc> *ret = make_tree_vector ();
12157 for (; list; list = TREE_CHAIN (list))
12158 vec_safe_push (ret, TREE_VALUE (list));
12159 return ret;
12160 }
12161
12162 /* Get a new tree vector which is a copy of an existing one. */
12163
12164 vec<tree, va_gc> *
12165 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12166 {
12167 vec<tree, va_gc> *ret;
12168 unsigned int ix;
12169 tree t;
12170
12171 ret = make_tree_vector ();
12172 vec_safe_reserve (ret, vec_safe_length (orig));
12173 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12174 ret->quick_push (t);
12175 return ret;
12176 }
12177
12178 /* Return true if KEYWORD starts a type specifier. */
12179
12180 bool
12181 keyword_begins_type_specifier (enum rid keyword)
12182 {
12183 switch (keyword)
12184 {
12185 case RID_AUTO_TYPE:
12186 case RID_INT:
12187 case RID_CHAR:
12188 case RID_FLOAT:
12189 case RID_DOUBLE:
12190 case RID_VOID:
12191 case RID_UNSIGNED:
12192 case RID_LONG:
12193 case RID_SHORT:
12194 case RID_SIGNED:
12195 case RID_DFLOAT32:
12196 case RID_DFLOAT64:
12197 case RID_DFLOAT128:
12198 case RID_FRACT:
12199 case RID_ACCUM:
12200 case RID_BOOL:
12201 case RID_WCHAR:
12202 case RID_CHAR16:
12203 case RID_CHAR32:
12204 case RID_SAT:
12205 case RID_COMPLEX:
12206 case RID_TYPEOF:
12207 case RID_STRUCT:
12208 case RID_CLASS:
12209 case RID_UNION:
12210 case RID_ENUM:
12211 return true;
12212 default:
12213 if (keyword >= RID_FIRST_INT_N
12214 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12215 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12216 return true;
12217 return false;
12218 }
12219 }
12220
12221 /* Return true if KEYWORD names a type qualifier. */
12222
12223 bool
12224 keyword_is_type_qualifier (enum rid keyword)
12225 {
12226 switch (keyword)
12227 {
12228 case RID_CONST:
12229 case RID_VOLATILE:
12230 case RID_RESTRICT:
12231 case RID_ATOMIC:
12232 return true;
12233 default:
12234 return false;
12235 }
12236 }
12237
12238 /* Return true if KEYWORD names a storage class specifier.
12239
12240 RID_TYPEDEF is not included in this list despite `typedef' being
12241 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12242 such for syntactic convenience only. */
12243
12244 bool
12245 keyword_is_storage_class_specifier (enum rid keyword)
12246 {
12247 switch (keyword)
12248 {
12249 case RID_STATIC:
12250 case RID_EXTERN:
12251 case RID_REGISTER:
12252 case RID_AUTO:
12253 case RID_MUTABLE:
12254 case RID_THREAD:
12255 return true;
12256 default:
12257 return false;
12258 }
12259 }
12260
12261 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12262
12263 static bool
12264 keyword_is_function_specifier (enum rid keyword)
12265 {
12266 switch (keyword)
12267 {
12268 case RID_INLINE:
12269 case RID_NORETURN:
12270 case RID_VIRTUAL:
12271 case RID_EXPLICIT:
12272 return true;
12273 default:
12274 return false;
12275 }
12276 }
12277
12278 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12279 declaration-specifier (C99 6.7). */
12280
12281 bool
12282 keyword_is_decl_specifier (enum rid keyword)
12283 {
12284 if (keyword_is_storage_class_specifier (keyword)
12285 || keyword_is_type_qualifier (keyword)
12286 || keyword_is_function_specifier (keyword))
12287 return true;
12288
12289 switch (keyword)
12290 {
12291 case RID_TYPEDEF:
12292 case RID_FRIEND:
12293 case RID_CONSTEXPR:
12294 return true;
12295 default:
12296 return false;
12297 }
12298 }
12299
12300 /* Initialize language-specific-bits of tree_contains_struct. */
12301
12302 void
12303 c_common_init_ts (void)
12304 {
12305 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12306 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12307 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12308 }
12309
12310 /* Build a user-defined numeric literal out of an integer constant type VALUE
12311 with identifier SUFFIX. */
12312
12313 tree
12314 build_userdef_literal (tree suffix_id, tree value,
12315 enum overflow_type overflow, tree num_string)
12316 {
12317 tree literal = make_node (USERDEF_LITERAL);
12318 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12319 USERDEF_LITERAL_VALUE (literal) = value;
12320 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12321 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12322 return literal;
12323 }
12324
12325 /* For vector[index], convert the vector to a
12326 pointer of the underlying type. Return true if the resulting
12327 ARRAY_REF should not be an lvalue. */
12328
12329 bool
12330 convert_vector_to_pointer_for_subscript (location_t loc,
12331 tree *vecp, tree index)
12332 {
12333 bool ret = false;
12334 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
12335 {
12336 tree type = TREE_TYPE (*vecp);
12337 tree type1;
12338
12339 ret = !lvalue_p (*vecp);
12340 if (TREE_CODE (index) == INTEGER_CST)
12341 if (!tree_fits_uhwi_p (index)
12342 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12343 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12344
12345 if (ret)
12346 {
12347 tree tmp = create_tmp_var_raw (type);
12348 DECL_SOURCE_LOCATION (tmp) = loc;
12349 *vecp = c_save_expr (*vecp);
12350 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12351 {
12352 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12353 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12354 *vecp
12355 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12356 *vecp, NULL_TREE, NULL_TREE),
12357 non_const);
12358 }
12359 else
12360 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12361 NULL_TREE, NULL_TREE);
12362 SET_EXPR_LOCATION (*vecp, loc);
12363 c_common_mark_addressable_vec (tmp);
12364 }
12365 else
12366 c_common_mark_addressable_vec (*vecp);
12367 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12368 type1 = build_pointer_type (TREE_TYPE (*vecp));
12369 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12370 if (!ref_all
12371 && !DECL_P (*vecp))
12372 {
12373 /* If the original vector isn't declared may_alias and it
12374 isn't a bare vector look if the subscripting would
12375 alias the vector we subscript, and if not, force ref-all. */
12376 alias_set_type vecset = get_alias_set (*vecp);
12377 alias_set_type sset = get_alias_set (type);
12378 if (!alias_sets_must_conflict_p (sset, vecset)
12379 && !alias_set_subset_of (sset, vecset))
12380 ref_all = true;
12381 }
12382 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12383 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12384 *vecp = convert (type, *vecp);
12385 }
12386 return ret;
12387 }
12388
12389 /* Determine which of the operands, if any, is a scalar that needs to be
12390 converted to a vector, for the range of operations. */
12391 enum stv_conv
12392 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12393 bool complain)
12394 {
12395 tree type0 = TREE_TYPE (op0);
12396 tree type1 = TREE_TYPE (op1);
12397 bool integer_only_op = false;
12398 enum stv_conv ret = stv_firstarg;
12399
12400 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
12401 switch (code)
12402 {
12403 /* Most GENERIC binary expressions require homogeneous arguments.
12404 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12405 argument that is a vector and a second one that is a scalar, so
12406 we never return stv_secondarg for them. */
12407 case RSHIFT_EXPR:
12408 case LSHIFT_EXPR:
12409 if (TREE_CODE (type0) == INTEGER_TYPE
12410 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12411 {
12412 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12413 {
12414 if (complain)
12415 error_at (loc, "conversion of scalar %qT to vector %qT "
12416 "involves truncation", type0, type1);
12417 return stv_error;
12418 }
12419 else
12420 return stv_firstarg;
12421 }
12422 break;
12423
12424 case BIT_IOR_EXPR:
12425 case BIT_XOR_EXPR:
12426 case BIT_AND_EXPR:
12427 integer_only_op = true;
12428 /* ... fall through ... */
12429
12430 case VEC_COND_EXPR:
12431
12432 case PLUS_EXPR:
12433 case MINUS_EXPR:
12434 case MULT_EXPR:
12435 case TRUNC_DIV_EXPR:
12436 case CEIL_DIV_EXPR:
12437 case FLOOR_DIV_EXPR:
12438 case ROUND_DIV_EXPR:
12439 case EXACT_DIV_EXPR:
12440 case TRUNC_MOD_EXPR:
12441 case FLOOR_MOD_EXPR:
12442 case RDIV_EXPR:
12443 case EQ_EXPR:
12444 case NE_EXPR:
12445 case LE_EXPR:
12446 case GE_EXPR:
12447 case LT_EXPR:
12448 case GT_EXPR:
12449 /* What about UNLT_EXPR? */
12450 if (VECTOR_TYPE_P (type0))
12451 {
12452 ret = stv_secondarg;
12453 std::swap (type0, type1);
12454 std::swap (op0, op1);
12455 }
12456
12457 if (TREE_CODE (type0) == INTEGER_TYPE
12458 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12459 {
12460 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12461 {
12462 if (complain)
12463 error_at (loc, "conversion of scalar %qT to vector %qT "
12464 "involves truncation", type0, type1);
12465 return stv_error;
12466 }
12467 return ret;
12468 }
12469 else if (!integer_only_op
12470 /* Allow integer --> real conversion if safe. */
12471 && (TREE_CODE (type0) == REAL_TYPE
12472 || TREE_CODE (type0) == INTEGER_TYPE)
12473 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12474 {
12475 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12476 {
12477 if (complain)
12478 error_at (loc, "conversion of scalar %qT to vector %qT "
12479 "involves truncation", type0, type1);
12480 return stv_error;
12481 }
12482 return ret;
12483 }
12484 default:
12485 break;
12486 }
12487
12488 return stv_nothing;
12489 }
12490
12491 /* Return true iff ALIGN is an integral constant that is a fundamental
12492 alignment, as defined by [basic.align] in the c++-11
12493 specifications.
12494
12495 That is:
12496
12497 [A fundamental alignment is represented by an alignment less than or
12498 equal to the greatest alignment supported by the implementation
12499 in all contexts, which is equal to
12500 alignof(max_align_t)]. */
12501
12502 bool
12503 cxx_fundamental_alignment_p (unsigned align)
12504 {
12505 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12506 TYPE_ALIGN (long_double_type_node)));
12507 }
12508
12509 /* Return true if T is a pointer to a zero-sized aggregate. */
12510
12511 bool
12512 pointer_to_zero_sized_aggr_p (tree t)
12513 {
12514 if (!POINTER_TYPE_P (t))
12515 return false;
12516 t = TREE_TYPE (t);
12517 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12518 }
12519
12520 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12521 with no library fallback or for an ADDR_EXPR whose operand is such type
12522 issues an error pointing to the location LOC.
12523 Returns true when the expression has been diagnosed and false
12524 otherwise. */
12525 bool
12526 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12527 {
12528 if (TREE_CODE (expr) == ADDR_EXPR)
12529 expr = TREE_OPERAND (expr, 0);
12530
12531 if (TREE_TYPE (expr)
12532 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12533 && DECL_P (expr)
12534 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12535 false positives for user-declared built-ins such as abs or
12536 strlen, and for C++ operators new and delete.
12537 The c_decl_implicit() test avoids false positives for implicitly
12538 declared built-ins with library fallbacks (such as abs). */
12539 && DECL_BUILT_IN (expr)
12540 && DECL_IS_BUILTIN (expr)
12541 && !c_decl_implicit (expr)
12542 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12543 {
12544 if (loc == UNKNOWN_LOCATION)
12545 loc = EXPR_LOC_OR_LOC (expr, input_location);
12546
12547 /* Reject arguments that are built-in functions with
12548 no library fallback. */
12549 error_at (loc, "built-in function %qE must be directly called", expr);
12550
12551 return true;
12552 }
12553
12554 return false;
12555 }
12556
12557 /* If we're creating an if-else-if condition chain, first see if we
12558 already have this COND in the CHAIN. If so, warn and don't add COND
12559 into the vector, otherwise add the COND there. LOC is the location
12560 of COND. */
12561
12562 void
12563 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12564 {
12565 /* No chain has been created yet. Do nothing. */
12566 if (*chain == NULL)
12567 return;
12568
12569 if (TREE_SIDE_EFFECTS (cond))
12570 {
12571 /* Uh-oh! This condition has a side-effect, thus invalidates
12572 the whole chain. */
12573 delete *chain;
12574 *chain = NULL;
12575 return;
12576 }
12577
12578 unsigned int ix;
12579 tree t;
12580 bool found = false;
12581 FOR_EACH_VEC_ELT (**chain, ix, t)
12582 if (operand_equal_p (cond, t, 0))
12583 {
12584 if (warning_at (loc, OPT_Wduplicated_cond,
12585 "duplicated %<if%> condition"))
12586 inform (EXPR_LOCATION (t), "previously used here");
12587 found = true;
12588 break;
12589 }
12590
12591 if (!found
12592 && !CONSTANT_CLASS_P (cond)
12593 /* Don't infinitely grow the chain. */
12594 && (*chain)->length () < 512)
12595 (*chain)->safe_push (cond);
12596 }
12597
12598 /* Check if array size calculations overflow or if the array covers more
12599 than half of the address space. Return true if the size of the array
12600 is valid, false otherwise. TYPE is the type of the array and NAME is
12601 the name of the array, or NULL_TREE for unnamed arrays. */
12602
12603 bool
12604 valid_array_size_p (location_t loc, tree type, tree name)
12605 {
12606 if (type != error_mark_node
12607 && COMPLETE_TYPE_P (type)
12608 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12609 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
12610 {
12611 if (name)
12612 error_at (loc, "size of array %qE is too large", name);
12613 else
12614 error_at (loc, "size of unnamed array is too large");
12615 return false;
12616 }
12617 return true;
12618 }
12619
12620 #include "gt-c-family-c-common.h"