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