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