1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
20 #define GCC_C_COMMON_C
24 #include "coretypes.h"
30 #include "gimple-expr.h"
32 #include "stringpool.h"
34 #include "diagnostic.h"
36 #include "stor-layout.h"
40 #include "trans-mem.h"
42 #include "common/common-target.h"
43 #include "langhooks.h"
44 #include "tree-inline.h"
46 #include "tree-iterator.h"
50 cpp_reader *parse_in; /* Declared in c-pragma.h. */
52 /* Mode used to build pointers (VOIDmode means ptr_mode). */
54 machine_mode c_default_pointer_mode = VOIDmode;
56 /* The following symbols are subsumed in the c_global_trees array, and
57 listed here individually for documentation purposes.
59 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
61 tree short_integer_type_node;
62 tree long_integer_type_node;
63 tree long_long_integer_type_node;
65 tree short_unsigned_type_node;
66 tree long_unsigned_type_node;
67 tree long_long_unsigned_type_node;
69 tree truthvalue_type_node;
70 tree truthvalue_false_node;
71 tree truthvalue_true_node;
73 tree ptrdiff_type_node;
75 tree unsigned_char_type_node;
76 tree signed_char_type_node;
79 tree char16_type_node;
80 tree char32_type_node;
83 tree double_type_node;
84 tree long_double_type_node;
86 tree complex_integer_type_node;
87 tree complex_float_type_node;
88 tree complex_double_type_node;
89 tree complex_long_double_type_node;
91 tree dfloat32_type_node;
92 tree dfloat64_type_node;
93 tree_dfloat128_type_node;
101 tree unsigned_intQI_type_node;
102 tree unsigned_intHI_type_node;
103 tree unsigned_intSI_type_node;
104 tree unsigned_intDI_type_node;
105 tree unsigned_intTI_type_node;
107 tree widest_integer_literal_type_node;
108 tree widest_unsigned_literal_type_node;
110 Nodes for types `void *' and `const void *'.
112 tree ptr_type_node, const_ptr_type_node;
114 Nodes for types `char *' and `const char *'.
116 tree string_type_node, const_string_type_node;
118 Type `char[SOMENUMBER]'.
119 Used when an array of char is needed and the size is irrelevant.
121 tree char_array_type_node;
123 Type `wchar_t[SOMENUMBER]' or something like it.
124 Used when a wide string literal is created.
126 tree wchar_array_type_node;
128 Type `char16_t[SOMENUMBER]' or something like it.
129 Used when a UTF-16 string literal is created.
131 tree char16_array_type_node;
133 Type `char32_t[SOMENUMBER]' or something like it.
134 Used when a UTF-32 string literal is created.
136 tree char32_array_type_node;
138 Type `int ()' -- used for implicit declaration of functions.
140 tree default_function_type;
142 A VOID_TYPE node, packaged in a TREE_LIST.
146 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
147 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
148 VAR_DECLS, but C++ does.)
150 tree function_name_decl_node;
151 tree pretty_function_name_decl_node;
152 tree c99_function_name_decl_node;
154 Stack of nested function name VAR_DECLs.
156 tree saved_function_name_decls;
160 extern bool processing_constexpr_decl;
162 tree c_global_trees[CTI_MAX];
164 /* Switches common to the C front ends. */
166 /* Nonzero means don't output line number information. */
168 char flag_no_line_commands;
170 /* Nonzero causes -E output not to be done, but directives such as
171 #define that have side effects are still obeyed. */
175 /* Nonzero means dump macros in some fashion. */
177 char flag_dump_macros;
179 /* Nonzero means pass #include lines through to the output. */
181 char flag_dump_includes;
183 /* Nonzero means process PCH files while preprocessing. */
185 bool flag_pch_preprocess;
187 /* The file name to which we should write a precompiled header, or
188 NULL if no header will be written in this compile. */
190 const char *pch_file;
192 /* Nonzero if an ISO standard was selected. It rejects macros in the
196 /* C/ObjC language option variables. */
199 /* Nonzero means allow type mismatches in conditional expressions;
200 just make their values `void'. */
202 int flag_cond_mismatch;
204 /* Nonzero means enable C89 Amendment 1 features. */
208 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
212 /* Nonzero means use the ISO C11 dialect of C. */
216 /* Nonzero means that we have builtin functions, and main is an int. */
221 /* ObjC language option variables. */
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. */
228 int print_struct_values;
230 /* Tells the compiler what is the constant string class for ObjC. */
232 const char *constant_string_class_name;
235 /* C++ language option variables. */
237 /* The reference version of the ABI for -Wabi. */
239 int warn_abi_version = -1;
241 /* Nonzero means generate separate instantiation control files and
242 juggle them at link time. */
244 int flag_use_repository;
246 /* The C++ dialect being used. Default set in c_common_post_options. */
248 enum cxx_dialect cxx_dialect = cxx_unset;
250 /* Maximum template instantiation depth. This limit exists to limit the
251 time it takes to notice excessively recursive template instantiations.
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). */
257 int max_tinst_depth = 900;
259 /* The elements of `ridpointers' are identifier nodes for the reserved
260 type names and storage classes. It is indexed by a RID_... value. */
263 tree (*make_fname_decl) (location_t, tree, int);
265 /* Nonzero means don't warn about problems that occur when the code is
267 int c_inhibit_evaluation_warnings;
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
275 bool in_late_binary_op;
277 /* Whether lexing has been completed, so subsequent preprocessor
278 errors should use the compiler's input_location. */
279 bool done_lexing = false;
281 /* Information about how a function name is generated. */
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? */
289 /* The three ways of getting then name of the current function. */
291 const struct fname_var_t fname_vars[] =
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},
302 /* Global visibility options. */
303 struct visibility_flags visibility_options;
305 static tree check_case_value (location_t, tree);
306 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
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,
317 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
319 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
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,
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_unused_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
336 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
338 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_transparent_union_attribute (tree *, tree, tree,
341 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
343 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
349 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
350 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
351 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
353 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
354 static tree handle_visibility_attribute (tree *, tree, tree, int,
356 static tree handle_tls_model_attribute (tree *, tree, tree, int,
358 static tree handle_no_instrument_function_attribute (tree *, tree,
360 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
364 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_deprecated_attribute (tree *, tree, tree, int,
370 static tree handle_vector_size_attribute (tree *, tree, tree, int,
372 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
377 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
385 static tree ignore_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
392 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
395 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
396 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
397 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
398 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
400 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
401 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
402 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
403 static int resort_field_decl_cmp (const void *, const void *);
405 /* Reserved words. The third field is a mask: keywords are disabled
406 if they match the mask.
409 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
410 C --std=c99: D_CXXONLY | D_OBJC
411 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
412 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
413 C++ --std=c0x: D_CONLY | D_OBJC
414 ObjC++ is like C++ except that D_OBJC is not set
416 If -fno-asm is used, D_ASM is added to the mask. If
417 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
418 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
419 In C with -Wc++-compat, we warn if D_CXXWARN is set.
421 Note the complication of the D_CXX_OBJC keywords. These are
422 reserved words such as 'class'. In C++, 'class' is a reserved
423 word. In Objective-C++ it is too. In Objective-C, it is a
424 reserved word too, but only if it follows an '@' sign.
426 const struct c_common_resword c_common_reswords[] =
428 { "_Alignas", RID_ALIGNAS, D_CONLY },
429 { "_Alignof", RID_ALIGNOF, D_CONLY },
430 { "_Atomic", RID_ATOMIC, D_CONLY },
431 { "_Bool", RID_BOOL, D_CONLY },
432 { "_Complex", RID_COMPLEX, 0 },
433 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
434 { "_Cilk_sync", RID_CILK_SYNC, 0 },
435 { "_Cilk_for", RID_CILK_FOR, 0 },
436 { "_Imaginary", RID_IMAGINARY, D_CONLY },
437 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
438 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
439 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
440 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
441 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
442 { "_Sat", RID_SAT, D_CONLY | D_EXT },
443 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
444 { "_Noreturn", RID_NORETURN, D_CONLY },
445 { "_Generic", RID_GENERIC, D_CONLY },
446 { "_Thread_local", RID_THREAD, D_CONLY },
447 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
448 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
449 { "__alignof", RID_ALIGNOF, 0 },
450 { "__alignof__", RID_ALIGNOF, 0 },
451 { "__asm", RID_ASM, 0 },
452 { "__asm__", RID_ASM, 0 },
453 { "__attribute", RID_ATTRIBUTE, 0 },
454 { "__attribute__", RID_ATTRIBUTE, 0 },
455 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
456 { "__bases", RID_BASES, D_CXXONLY },
457 { "__builtin_call_with_static_chain",
458 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
459 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
460 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
461 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
462 { "__builtin_offsetof", RID_OFFSETOF, 0 },
463 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
464 { "__builtin_va_arg", RID_VA_ARG, 0 },
465 { "__complex", RID_COMPLEX, 0 },
466 { "__complex__", RID_COMPLEX, 0 },
467 { "__const", RID_CONST, 0 },
468 { "__const__", RID_CONST, 0 },
469 { "__decltype", RID_DECLTYPE, D_CXXONLY },
470 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
471 { "__extension__", RID_EXTENSION, 0 },
472 { "__func__", RID_C99_FUNCTION_NAME, 0 },
473 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
474 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
475 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
476 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
477 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
478 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
479 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
480 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
481 { "__imag", RID_IMAGPART, 0 },
482 { "__imag__", RID_IMAGPART, 0 },
483 { "__inline", RID_INLINE, 0 },
484 { "__inline__", RID_INLINE, 0 },
485 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
486 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
487 { "__is_class", RID_IS_CLASS, D_CXXONLY },
488 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
489 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
490 { "__is_final", RID_IS_FINAL, D_CXXONLY },
491 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
492 { "__is_pod", RID_IS_POD, D_CXXONLY },
493 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
494 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
495 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
496 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
497 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
498 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
499 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
500 { "__is_union", RID_IS_UNION, D_CXXONLY },
501 { "__label__", RID_LABEL, 0 },
502 { "__null", RID_NULL, 0 },
503 { "__real", RID_REALPART, 0 },
504 { "__real__", RID_REALPART, 0 },
505 { "__restrict", RID_RESTRICT, 0 },
506 { "__restrict__", RID_RESTRICT, 0 },
507 { "__signed", RID_SIGNED, 0 },
508 { "__signed__", RID_SIGNED, 0 },
509 { "__thread", RID_THREAD, 0 },
510 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
511 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
512 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
513 { "__typeof", RID_TYPEOF, 0 },
514 { "__typeof__", RID_TYPEOF, 0 },
515 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
516 { "__volatile", RID_VOLATILE, 0 },
517 { "__volatile__", RID_VOLATILE, 0 },
518 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
519 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
520 { "asm", RID_ASM, D_ASM },
521 { "auto", RID_AUTO, 0 },
522 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
523 { "break", RID_BREAK, 0 },
524 { "case", RID_CASE, 0 },
525 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
526 { "char", RID_CHAR, 0 },
527 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
528 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
529 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
530 { "const", RID_CONST, 0 },
531 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
532 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
533 { "continue", RID_CONTINUE, 0 },
534 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
535 { "default", RID_DEFAULT, 0 },
536 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
538 { "double", RID_DOUBLE, 0 },
539 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
540 { "else", RID_ELSE, 0 },
541 { "enum", RID_ENUM, 0 },
542 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
543 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
544 { "extern", RID_EXTERN, 0 },
545 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
546 { "float", RID_FLOAT, 0 },
547 { "for", RID_FOR, 0 },
548 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
549 { "goto", RID_GOTO, 0 },
551 { "inline", RID_INLINE, D_EXT89 },
552 { "int", RID_INT, 0 },
553 { "long", RID_LONG, 0 },
554 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
555 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
556 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
557 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
558 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
559 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
560 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
561 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
562 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
563 { "register", RID_REGISTER, 0 },
564 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
565 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
566 { "return", RID_RETURN, 0 },
567 { "short", RID_SHORT, 0 },
568 { "signed", RID_SIGNED, 0 },
569 { "sizeof", RID_SIZEOF, 0 },
570 { "static", RID_STATIC, 0 },
571 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
572 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
573 { "struct", RID_STRUCT, 0 },
574 { "switch", RID_SWITCH, 0 },
575 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
576 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
577 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
578 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
579 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
580 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
581 { "typedef", RID_TYPEDEF, 0 },
582 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
583 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
584 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
585 { "union", RID_UNION, 0 },
586 { "unsigned", RID_UNSIGNED, 0 },
587 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
588 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
589 { "void", RID_VOID, 0 },
590 { "volatile", RID_VOLATILE, 0 },
591 { "wchar_t", RID_WCHAR, D_CXXONLY },
592 { "while", RID_WHILE, 0 },
594 /* C++ transactional memory. */
595 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
596 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
597 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
598 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
600 /* Concepts-related keywords */
601 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
602 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
604 /* These Objective-C keywords are recognized only immediately after
606 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
607 { "defs", RID_AT_DEFS, D_OBJC },
608 { "encode", RID_AT_ENCODE, D_OBJC },
609 { "end", RID_AT_END, D_OBJC },
610 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
611 { "interface", RID_AT_INTERFACE, D_OBJC },
612 { "protocol", RID_AT_PROTOCOL, D_OBJC },
613 { "selector", RID_AT_SELECTOR, D_OBJC },
614 { "finally", RID_AT_FINALLY, D_OBJC },
615 { "optional", RID_AT_OPTIONAL, D_OBJC },
616 { "required", RID_AT_REQUIRED, D_OBJC },
617 { "property", RID_AT_PROPERTY, D_OBJC },
618 { "package", RID_AT_PACKAGE, D_OBJC },
619 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
620 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
621 /* These are recognized only in protocol-qualifier context
623 { "bycopy", RID_BYCOPY, D_OBJC },
624 { "byref", RID_BYREF, D_OBJC },
625 { "in", RID_IN, D_OBJC },
626 { "inout", RID_INOUT, D_OBJC },
627 { "oneway", RID_ONEWAY, D_OBJC },
628 { "out", RID_OUT, D_OBJC },
629 /* These are recognized inside a property attribute list */
630 { "assign", RID_ASSIGN, D_OBJC },
631 { "copy", RID_COPY, D_OBJC },
632 { "getter", RID_GETTER, D_OBJC },
633 { "nonatomic", RID_NONATOMIC, D_OBJC },
634 { "readonly", RID_READONLY, D_OBJC },
635 { "readwrite", RID_READWRITE, D_OBJC },
636 { "retain", RID_RETAIN, D_OBJC },
637 { "setter", RID_SETTER, D_OBJC },
640 const unsigned int num_c_common_reswords =
641 sizeof c_common_reswords / sizeof (struct c_common_resword);
643 /* Table of machine-independent attributes common to all C-like languages.
645 All attributes referencing arguments should be additionally processed
646 in chkp_copy_function_type_adding_bounds for correct instrumentation
647 by Pointer Bounds Checker.
648 Current list of processed common attributes: nonnull. */
649 const struct attribute_spec c_common_attribute_table[] =
651 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
652 affects_type_identity } */
653 { "packed", 0, 0, false, false, false,
654 handle_packed_attribute , false},
655 { "nocommon", 0, 0, true, false, false,
656 handle_nocommon_attribute, false},
657 { "common", 0, 0, true, false, false,
658 handle_common_attribute, false },
659 /* FIXME: logically, noreturn attributes should be listed as
660 "false, true, true" and apply to function types. But implementing this
661 would require all the places in the compiler that use TREE_THIS_VOLATILE
662 on a decl to identify non-returning functions to be located and fixed
663 to check the function type instead. */
664 { "noreturn", 0, 0, true, false, false,
665 handle_noreturn_attribute, false },
666 { "volatile", 0, 0, true, false, false,
667 handle_noreturn_attribute, false },
668 { "stack_protect", 0, 0, true, false, false,
669 handle_stack_protect_attribute, false },
670 { "noinline", 0, 0, true, false, false,
671 handle_noinline_attribute, false },
672 { "noclone", 0, 0, true, false, false,
673 handle_noclone_attribute, false },
674 { "no_icf", 0, 0, true, false, false,
675 handle_noicf_attribute, false },
676 { "leaf", 0, 0, true, false, false,
677 handle_leaf_attribute, false },
678 { "always_inline", 0, 0, true, false, false,
679 handle_always_inline_attribute, false },
680 { "gnu_inline", 0, 0, true, false, false,
681 handle_gnu_inline_attribute, false },
682 { "artificial", 0, 0, true, false, false,
683 handle_artificial_attribute, false },
684 { "flatten", 0, 0, true, false, false,
685 handle_flatten_attribute, false },
686 { "used", 0, 0, true, false, false,
687 handle_used_attribute, false },
688 { "unused", 0, 0, false, false, false,
689 handle_unused_attribute, false },
690 { "externally_visible", 0, 0, true, false, false,
691 handle_externally_visible_attribute, false },
692 { "no_reorder", 0, 0, true, false, false,
693 handle_no_reorder_attribute, false },
694 /* The same comments as for noreturn attributes apply to const ones. */
695 { "const", 0, 0, true, false, false,
696 handle_const_attribute, false },
697 { "scalar_storage_order", 1, 1, false, false, false,
698 handle_scalar_storage_order_attribute, false },
699 { "transparent_union", 0, 0, false, false, false,
700 handle_transparent_union_attribute, false },
701 { "constructor", 0, 1, true, false, false,
702 handle_constructor_attribute, false },
703 { "destructor", 0, 1, true, false, false,
704 handle_destructor_attribute, false },
705 { "mode", 1, 1, false, true, false,
706 handle_mode_attribute, false },
707 { "section", 1, 1, true, false, false,
708 handle_section_attribute, false },
709 { "aligned", 0, 1, false, false, false,
710 handle_aligned_attribute, false },
711 { "weak", 0, 0, true, false, false,
712 handle_weak_attribute, false },
713 { "noplt", 0, 0, true, false, false,
714 handle_noplt_attribute, false },
715 { "ifunc", 1, 1, true, false, false,
716 handle_ifunc_attribute, false },
717 { "alias", 1, 1, true, false, false,
718 handle_alias_attribute, false },
719 { "weakref", 0, 1, true, false, false,
720 handle_weakref_attribute, false },
721 { "no_instrument_function", 0, 0, true, false, false,
722 handle_no_instrument_function_attribute,
724 { "malloc", 0, 0, true, false, false,
725 handle_malloc_attribute, false },
726 { "returns_twice", 0, 0, true, false, false,
727 handle_returns_twice_attribute, false },
728 { "no_stack_limit", 0, 0, true, false, false,
729 handle_no_limit_stack_attribute, false },
730 { "pure", 0, 0, true, false, false,
731 handle_pure_attribute, false },
732 { "transaction_callable", 0, 0, false, true, false,
733 handle_tm_attribute, false },
734 { "transaction_unsafe", 0, 0, false, true, false,
735 handle_tm_attribute, true },
736 { "transaction_safe", 0, 0, false, true, false,
737 handle_tm_attribute, true },
738 { "transaction_safe_dynamic", 0, 0, true, false, false,
739 handle_tm_attribute, false },
740 { "transaction_may_cancel_outer", 0, 0, false, true, false,
741 handle_tm_attribute, false },
742 /* ??? These two attributes didn't make the transition from the
743 Intel language document to the multi-vendor language document. */
744 { "transaction_pure", 0, 0, false, true, false,
745 handle_tm_attribute, false },
746 { "transaction_wrap", 1, 1, true, false, false,
747 handle_tm_wrap_attribute, false },
748 /* For internal use (marking of builtins) only. The name contains space
749 to prevent its usage in source code. */
750 { "no vops", 0, 0, true, false, false,
751 handle_novops_attribute, false },
752 { "deprecated", 0, 1, false, false, false,
753 handle_deprecated_attribute, false },
754 { "vector_size", 1, 1, false, true, false,
755 handle_vector_size_attribute, false },
756 { "visibility", 1, 1, false, false, false,
757 handle_visibility_attribute, false },
758 { "tls_model", 1, 1, true, false, false,
759 handle_tls_model_attribute, false },
760 { "nonnull", 0, -1, false, true, true,
761 handle_nonnull_attribute, false },
762 { "nothrow", 0, 0, true, false, false,
763 handle_nothrow_attribute, false },
764 { "may_alias", 0, 0, false, true, false, NULL, false },
765 { "cleanup", 1, 1, true, false, false,
766 handle_cleanup_attribute, false },
767 { "warn_unused_result", 0, 0, false, true, true,
768 handle_warn_unused_result_attribute, false },
769 { "sentinel", 0, 1, false, true, true,
770 handle_sentinel_attribute, false },
771 /* For internal use (marking of builtins) only. The name contains space
772 to prevent its usage in source code. */
773 { "type generic", 0, 0, false, true, true,
774 handle_type_generic_attribute, false },
775 { "alloc_size", 1, 2, false, true, true,
776 handle_alloc_size_attribute, false },
777 { "cold", 0, 0, true, false, false,
778 handle_cold_attribute, false },
779 { "hot", 0, 0, true, false, false,
780 handle_hot_attribute, false },
781 { "no_address_safety_analysis",
782 0, 0, true, false, false,
783 handle_no_address_safety_analysis_attribute,
785 { "no_sanitize_address", 0, 0, true, false, false,
786 handle_no_sanitize_address_attribute,
788 { "no_sanitize_thread", 0, 0, true, false, false,
789 handle_no_sanitize_address_attribute,
791 { "no_sanitize_undefined", 0, 0, true, false, false,
792 handle_no_sanitize_undefined_attribute,
794 { "warning", 1, 1, true, false, false,
795 handle_error_attribute, false },
796 { "error", 1, 1, true, false, false,
797 handle_error_attribute, false },
798 { "target", 1, -1, true, false, false,
799 handle_target_attribute, false },
800 { "target_clones", 1, -1, true, false, false,
801 handle_target_clones_attribute, false },
802 { "optimize", 1, -1, true, false, false,
803 handle_optimize_attribute, false },
804 /* For internal use only. The leading '*' both prevents its usage in
805 source code and signals that it may be overridden by machine tables. */
806 { "*tm regparm", 0, 0, false, true, true,
807 ignore_attribute, false },
808 { "no_split_stack", 0, 0, true, false, false,
809 handle_no_split_stack_attribute, false },
810 /* For internal use (marking of builtins and runtime functions) only.
811 The name contains space to prevent its usage in source code. */
812 { "fn spec", 1, 1, false, true, true,
813 handle_fnspec_attribute, false },
814 { "warn_unused", 0, 0, false, false, false,
815 handle_warn_unused_attribute, false },
816 { "returns_nonnull", 0, 0, false, true, true,
817 handle_returns_nonnull_attribute, false },
818 { "omp declare simd", 0, -1, true, false, false,
819 handle_omp_declare_simd_attribute, false },
820 { "cilk simd function", 0, -1, true, false, false,
821 handle_omp_declare_simd_attribute, false },
822 { "simd", 0, 1, true, false, false,
823 handle_simd_attribute, false },
824 { "omp declare target", 0, 0, true, false, false,
825 handle_omp_declare_target_attribute, false },
826 { "omp declare target link", 0, 0, true, false, false,
827 handle_omp_declare_target_attribute, false },
828 { "alloc_align", 1, 1, false, true, true,
829 handle_alloc_align_attribute, false },
830 { "assume_aligned", 1, 2, false, true, true,
831 handle_assume_aligned_attribute, false },
832 { "designated_init", 0, 0, false, true, false,
833 handle_designated_init_attribute, false },
834 { "bnd_variable_size", 0, 0, true, false, false,
835 handle_bnd_variable_size_attribute, false },
836 { "bnd_legacy", 0, 0, true, false, false,
837 handle_bnd_legacy, false },
838 { "bnd_instrument", 0, 0, true, false, false,
839 handle_bnd_instrument, false },
840 { NULL, 0, 0, false, false, false, NULL, false }
843 /* Give the specifications for the format attributes, used by C and all
846 All attributes referencing arguments should be additionally processed
847 in chkp_copy_function_type_adding_bounds for correct instrumentation
848 by Pointer Bounds Checker.
849 Current list of processed format attributes: format, format_arg. */
850 const struct attribute_spec c_common_format_attribute_table[] =
852 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
853 affects_type_identity } */
854 { "format", 3, 3, false, true, true,
855 handle_format_attribute, false },
856 { "format_arg", 1, 1, false, true, true,
857 handle_format_arg_attribute, false },
858 { NULL, 0, 0, false, false, false, NULL, false }
861 /* Return identifier for address space AS. */
864 c_addr_space_name (addr_space_t as)
866 int rid = RID_FIRST_ADDR_SPACE + as;
867 gcc_assert (ridpointers [rid]);
868 return IDENTIFIER_POINTER (ridpointers [rid]);
871 /* Push current bindings for the function name VAR_DECLS. */
874 start_fname_decls (void)
877 tree saved = NULL_TREE;
879 for (ix = 0; fname_vars[ix].decl; ix++)
881 tree decl = *fname_vars[ix].decl;
885 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
887 *fname_vars[ix].decl = NULL_TREE;
890 if (saved || saved_function_name_decls)
891 /* Normally they'll have been NULL, so only push if we've got a
892 stack, or they are non-NULL. */
893 saved_function_name_decls = tree_cons (saved, NULL_TREE,
894 saved_function_name_decls);
897 /* Finish up the current bindings, adding them into the current function's
898 statement tree. This must be done _before_ finish_stmt_tree is called.
899 If there is no current function, we must be at file scope and no statements
900 are involved. Pop the previous bindings. */
903 finish_fname_decls (void)
906 tree stmts = NULL_TREE;
907 tree stack = saved_function_name_decls;
909 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
910 append_to_statement_list (TREE_VALUE (stack), &stmts);
914 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
916 if (TREE_CODE (*bodyp) == BIND_EXPR)
917 bodyp = &BIND_EXPR_BODY (*bodyp);
919 append_to_statement_list_force (*bodyp, &stmts);
923 for (ix = 0; fname_vars[ix].decl; ix++)
924 *fname_vars[ix].decl = NULL_TREE;
928 /* We had saved values, restore them. */
931 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
933 tree decl = TREE_PURPOSE (saved);
934 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
936 *fname_vars[ix].decl = decl;
938 stack = TREE_CHAIN (stack);
940 saved_function_name_decls = stack;
943 /* Return the text name of the current function, suitably prettified
944 by PRETTY_P. Return string must be freed by caller. */
947 fname_as_string (int pretty_p)
949 const char *name = "top level";
952 cpp_string cstr = { 0, 0 }, strname;
960 if (current_function_decl)
961 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
963 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
965 namep = XNEWVEC (char, len);
966 snprintf (namep, len, "\"%s\"", name);
967 strname.text = (unsigned char *) namep;
968 strname.len = len - 1;
970 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
973 return (const char *) cstr.text;
979 /* Return the VAR_DECL for a const char array naming the current
980 function. If the VAR_DECL has not yet been created, create it
981 now. RID indicates how it should be formatted and IDENTIFIER_NODE
982 ID is its name (unfortunately C and C++ hold the RID values of
983 keywords in different places, so we can't derive RID from ID in
984 this language independent code. LOC is the location of the
988 fname_decl (location_t loc, unsigned int rid, tree id)
991 tree decl = NULL_TREE;
993 for (ix = 0; fname_vars[ix].decl; ix++)
994 if (fname_vars[ix].rid == rid)
997 decl = *fname_vars[ix].decl;
1000 /* If a tree is built here, it would normally have the lineno of
1001 the current statement. Later this tree will be moved to the
1002 beginning of the function and this line number will be wrong.
1003 To avoid this problem set the lineno to 0 here; that prevents
1004 it from appearing in the RTL. */
1006 location_t saved_location = input_location;
1007 input_location = UNKNOWN_LOCATION;
1009 stmts = push_stmt_list ();
1010 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1011 stmts = pop_stmt_list (stmts);
1012 if (!IS_EMPTY_STMT (stmts))
1013 saved_function_name_decls
1014 = tree_cons (decl, stmts, saved_function_name_decls);
1015 *fname_vars[ix].decl = decl;
1016 input_location = saved_location;
1018 if (!ix && !current_function_decl)
1019 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1024 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1027 fix_string_type (tree value)
1029 int length = TREE_STRING_LENGTH (value);
1031 tree e_type, i_type, a_type;
1033 /* Compute the number of elements, for the array type. */
1034 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1037 e_type = char_type_node;
1039 else if (TREE_TYPE (value) == char16_array_type_node)
1041 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1042 e_type = char16_type_node;
1044 else if (TREE_TYPE (value) == char32_array_type_node)
1046 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1047 e_type = char32_type_node;
1051 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1052 e_type = wchar_type_node;
1055 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1056 limit in C++98 Annex B is very large (65536) and is not normative,
1057 so we do not diagnose it (warn_overlength_strings is forced off
1058 in c_common_post_options). */
1059 if (warn_overlength_strings)
1061 const int nchars_max = flag_isoc99 ? 4095 : 509;
1062 const int relevant_std = flag_isoc99 ? 99 : 90;
1063 if (nchars - 1 > nchars_max)
1064 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1065 separate the %d from the 'C'. 'ISO' should not be
1066 translated, but it may be moved after 'C%d' in languages
1067 where modifiers follow nouns. */
1068 pedwarn (input_location, OPT_Woverlength_strings,
1069 "string length %qd is greater than the length %qd "
1070 "ISO C%d compilers are required to support",
1071 nchars - 1, nchars_max, relevant_std);
1074 /* Create the array type for the string constant. The ISO C++
1075 standard says that a string literal has type `const char[N]' or
1076 `const wchar_t[N]'. We use the same logic when invoked as a C
1077 front-end with -Wwrite-strings.
1078 ??? We should change the type of an expression depending on the
1079 state of a warning flag. We should just be warning -- see how
1080 this is handled in the C++ front-end for the deprecated implicit
1081 conversion from string literals to `char*' or `wchar_t*'.
1083 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1084 array type being the unqualified version of that type.
1085 Therefore, if we are constructing an array of const char, we must
1086 construct the matching unqualified array type first. The C front
1087 end does not require this, but it does no harm, so we do it
1089 i_type = build_index_type (size_int (nchars - 1));
1090 a_type = build_array_type (e_type, i_type);
1091 if (c_dialect_cxx() || warn_write_strings)
1092 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1094 TREE_TYPE (value) = a_type;
1095 TREE_CONSTANT (value) = 1;
1096 TREE_READONLY (value) = 1;
1097 TREE_STATIC (value) = 1;
1101 /* Fold X for consideration by one of the warning functions when checking
1102 whether an expression has a constant value. */
1105 fold_for_warn (tree x)
1107 if (c_dialect_cxx ())
1108 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1110 /* The C front-end has already folded X appropriately. */
1114 /* Print a warning if a constant expression had overflow in folding.
1115 Invoke this function on every expression that the language
1116 requires to be a constant expression.
1117 Note the ANSI C standard says it is erroneous for a
1118 constant expression to overflow. */
1121 constant_expression_warning (tree value)
1123 if (warn_overflow && pedantic
1124 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1125 || TREE_CODE (value) == FIXED_CST
1126 || TREE_CODE (value) == VECTOR_CST
1127 || TREE_CODE (value) == COMPLEX_CST)
1128 && TREE_OVERFLOW (value))
1129 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1132 /* The same as above but print an unconditional error. */
1134 constant_expression_error (tree value)
1136 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1137 || TREE_CODE (value) == FIXED_CST
1138 || TREE_CODE (value) == VECTOR_CST
1139 || TREE_CODE (value) == COMPLEX_CST)
1140 && TREE_OVERFLOW (value))
1141 error ("overflow in constant expression");
1144 /* Print a warning if an expression had overflow in folding and its
1147 Invoke this function on every expression that
1148 (1) appears in the source code, and
1149 (2) is a constant expression that overflowed, and
1150 (3) is not already checked by convert_and_check;
1151 however, do not invoke this function on operands of explicit casts
1152 or when the expression is the result of an operator and any operand
1153 already overflowed. */
1156 overflow_warning (location_t loc, tree value)
1158 if (c_inhibit_evaluation_warnings != 0)
1161 switch (TREE_CODE (value))
1164 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1168 warning_at (loc, OPT_Woverflow,
1169 "floating point overflow in expression");
1173 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1177 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1181 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1182 warning_at (loc, OPT_Woverflow,
1183 "complex integer overflow in expression");
1184 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1185 warning_at (loc, OPT_Woverflow,
1186 "complex floating point overflow in expression");
1194 /* Warn about uses of logical || / && operator in a context where it
1195 is likely that the bitwise equivalent was intended by the
1196 programmer. We have seen an expression in which CODE is a binary
1197 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1198 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1200 warn_logical_operator (location_t location, enum tree_code code, tree type,
1201 enum tree_code code_left, tree op_left,
1202 enum tree_code ARG_UNUSED (code_right), tree op_right)
1204 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1205 int in0_p, in1_p, in_p;
1206 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1207 bool strict_overflow_p = false;
1209 if (code != TRUTH_ANDIF_EXPR
1210 && code != TRUTH_AND_EXPR
1211 && code != TRUTH_ORIF_EXPR
1212 && code != TRUTH_OR_EXPR)
1215 /* We don't want to warn if either operand comes from a macro
1216 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1218 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1219 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1222 /* Warn if &&/|| are being used in a context where it is
1223 likely that the bitwise equivalent was intended by the
1224 programmer. That is, an expression such as op && MASK
1225 where op should not be any boolean expression, nor a
1226 constant, and mask seems to be a non-boolean integer constant. */
1227 if (TREE_CODE (op_right) == CONST_DECL)
1228 /* An enumerator counts as a constant. */
1229 op_right = DECL_INITIAL (op_right);
1230 if (!truth_value_p (code_left)
1231 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1232 && !CONSTANT_CLASS_P (op_left)
1233 && !TREE_NO_WARNING (op_left)
1234 && TREE_CODE (op_right) == INTEGER_CST
1235 && !integer_zerop (op_right)
1236 && !integer_onep (op_right))
1239 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1240 " applied to non-boolean constant");
1242 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1243 " applied to non-boolean constant");
1244 TREE_NO_WARNING (op_left) = true;
1248 /* We do not warn for constants because they are typical of macro
1249 expansions that test for features. */
1250 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1251 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
1254 /* This warning only makes sense with logical operands. */
1255 if (!(truth_value_p (TREE_CODE (op_left))
1256 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1257 || !(truth_value_p (TREE_CODE (op_right))
1258 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1261 /* The range computations only work with scalars. */
1262 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1263 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1266 /* We first test whether either side separately is trivially true
1267 (with OR) or trivially false (with AND). If so, do not warn.
1268 This is a common idiom for testing ranges of data types in
1270 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1273 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1274 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1276 /* If this is an OR operation, invert both sides; now, the result
1277 should be always false to get a warning. */
1281 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1282 if (tem && integer_zerop (tem))
1285 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1288 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1289 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1291 /* If this is an OR operation, invert both sides; now, the result
1292 should be always false to get a warning. */
1296 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1297 if (tem && integer_zerop (tem))
1300 /* If both expressions have the same operand, if we can merge the
1302 if (operand_equal_p (lhs, rhs, 0)
1303 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1304 in1_p, low1, high1))
1306 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1307 /* ... and if the range test is always false, then warn. */
1308 if (tem && integer_zerop (tem))
1311 warning_at (location, OPT_Wlogical_op,
1312 "logical %<or%> of collectively exhaustive tests is "
1315 warning_at (location, OPT_Wlogical_op,
1316 "logical %<and%> of mutually exclusive tests is "
1319 /* Or warn if the operands have exactly the same range, e.g.
1321 else if (tree_int_cst_equal (low0, low1)
1322 && tree_int_cst_equal (high0, high1))
1325 warning_at (location, OPT_Wlogical_op,
1326 "logical %<or%> of equal expressions");
1328 warning_at (location, OPT_Wlogical_op,
1329 "logical %<and%> of equal expressions");
1334 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1335 with constant indices. */
1338 find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1340 tree expr = *expr_p;
1342 if ((TREE_CODE (expr) == ARRAY_REF
1343 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1344 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1346 *(bool *) data = true;
1353 /* Warn if a self-comparison always evaluates to true or false. LOC
1354 is the location of the comparison with code CODE, LHS and RHS are
1355 operands of the comparison. */
1358 warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1360 if (TREE_CODE_CLASS (code) != tcc_comparison)
1363 /* Don't warn for various macro expansions. */
1364 if (from_macro_expansion_at (loc)
1365 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1366 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1369 /* We do not warn for constants because they are typical of macro
1370 expansions that test for features, sizeof, and similar. */
1371 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1372 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
1375 /* Don't warn for e.g.
1378 if (n == (long) n) ...
1380 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1381 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1384 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1385 It could be a NaN, and NaN never compares equal to anything, even
1387 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1390 if (operand_equal_p (lhs, rhs, 0))
1392 /* Don't warn about array references with constant indices;
1393 these are likely to come from a macro. */
1395 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1399 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1400 || code == GE_EXPR || code == UNLE_EXPR
1401 || code == UNGE_EXPR || code == UNEQ_EXPR);
1403 warning_at (loc, OPT_Wtautological_compare,
1404 "self-comparison always evaluates to true");
1406 warning_at (loc, OPT_Wtautological_compare,
1407 "self-comparison always evaluates to false");
1411 /* Warn about logical not used on the left hand side operand of a comparison.
1412 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1413 Do not warn if RHS is of a boolean type. */
1416 warn_logical_not_parentheses (location_t location, enum tree_code code,
1419 if (TREE_CODE_CLASS (code) != tcc_comparison
1420 || TREE_TYPE (rhs) == NULL_TREE
1421 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1424 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1425 !(x == 0) or !(y != 0). */
1426 if ((code == EQ_EXPR || code == NE_EXPR)
1427 && integer_zerop (rhs))
1430 warning_at (location, OPT_Wlogical_not_parentheses,
1431 "logical not is only applied to the left hand side of "
1435 /* Warn if EXP contains any computations whose results are not used.
1436 Return true if a warning is printed; false otherwise. LOCUS is the
1437 (potential) location of the expression. */
1440 warn_if_unused_value (const_tree exp, location_t locus)
1443 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1446 /* Don't warn about void constructs. This includes casting to void,
1447 void function calls, and statement expressions with a final cast
1449 if (VOID_TYPE_P (TREE_TYPE (exp)))
1452 if (EXPR_HAS_LOCATION (exp))
1453 locus = EXPR_LOCATION (exp);
1455 switch (TREE_CODE (exp))
1457 case PREINCREMENT_EXPR:
1458 case POSTINCREMENT_EXPR:
1459 case PREDECREMENT_EXPR:
1460 case POSTDECREMENT_EXPR:
1465 case TRY_CATCH_EXPR:
1466 case WITH_CLEANUP_EXPR:
1472 /* For a binding, warn if no side effect within it. */
1473 exp = BIND_EXPR_BODY (exp);
1477 case NON_LVALUE_EXPR:
1479 exp = TREE_OPERAND (exp, 0);
1482 case TRUTH_ORIF_EXPR:
1483 case TRUTH_ANDIF_EXPR:
1484 /* In && or ||, warn if 2nd operand has no side effect. */
1485 exp = TREE_OPERAND (exp, 1);
1489 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1491 /* Let people do `(foo (), 0)' without a warning. */
1492 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1494 exp = TREE_OPERAND (exp, 1);
1498 /* If this is an expression with side effects, don't warn; this
1499 case commonly appears in macro expansions. */
1500 if (TREE_SIDE_EFFECTS (exp))
1505 /* Don't warn about automatic dereferencing of references, since
1506 the user cannot control it. */
1507 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1509 exp = TREE_OPERAND (exp, 0);
1515 /* Referencing a volatile value is a side effect, so don't warn. */
1516 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1517 && TREE_THIS_VOLATILE (exp))
1520 /* If this is an expression which has no operands, there is no value
1521 to be unused. There are no such language-independent codes,
1522 but front ends may define such. */
1523 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1527 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1532 /* Print a warning about casts that might indicate violation
1533 of strict aliasing rules if -Wstrict-aliasing is used and
1534 strict aliasing mode is in effect. OTYPE is the original
1535 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1538 strict_aliasing_warning (tree otype, tree type, tree expr)
1540 /* Strip pointer conversion chains and get to the correct original type. */
1542 otype = TREE_TYPE (expr);
1544 if (!(flag_strict_aliasing
1545 && POINTER_TYPE_P (type)
1546 && POINTER_TYPE_P (otype)
1547 && !VOID_TYPE_P (TREE_TYPE (type)))
1548 /* If the type we are casting to is a ref-all pointer
1549 dereferencing it is always valid. */
1550 || TYPE_REF_CAN_ALIAS_ALL (type))
1553 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1554 && (DECL_P (TREE_OPERAND (expr, 0))
1555 || handled_component_p (TREE_OPERAND (expr, 0))))
1557 /* Casting the address of an object to non void pointer. Warn
1558 if the cast breaks type based aliasing. */
1559 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1561 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1562 "might break strict-aliasing rules");
1567 /* warn_strict_aliasing >= 3. This includes the default (3).
1568 Only warn if the cast is dereferenced immediately. */
1569 alias_set_type set1 =
1570 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1571 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1573 if (set1 != set2 && set2 != 0
1575 || (!alias_set_subset_of (set2, set1)
1576 && !alias_sets_conflict_p (set1, set2))))
1578 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1579 "pointer will break strict-aliasing rules");
1582 else if (warn_strict_aliasing == 2
1583 && !alias_sets_must_conflict_p (set1, set2))
1585 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1586 "pointer might break strict-aliasing rules");
1592 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1594 /* At this level, warn for any conversions, even if an address is
1595 not taken in the same statement. This will likely produce many
1596 false positives, but could be useful to pinpoint problems that
1597 are not revealed at higher levels. */
1598 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1599 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1600 if (!COMPLETE_TYPE_P (type)
1601 || !alias_sets_must_conflict_p (set1, set2))
1603 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1604 "pointer might break strict-aliasing rules");
1612 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1613 sizeof as last operand of certain builtins. */
1616 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1617 vec<tree, va_gc> *params, tree *sizeof_arg,
1618 bool (*comp_types) (tree, tree))
1620 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1621 bool strop = false, cmp = false;
1622 unsigned int idx = ~0;
1625 if (TREE_CODE (callee) != FUNCTION_DECL
1626 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1627 || vec_safe_length (params) <= 1)
1630 switch (DECL_FUNCTION_CODE (callee))
1632 case BUILT_IN_STRNCMP:
1633 case BUILT_IN_STRNCASECMP:
1636 case BUILT_IN_STRNCPY:
1637 case BUILT_IN_STRNCPY_CHK:
1638 case BUILT_IN_STRNCAT:
1639 case BUILT_IN_STRNCAT_CHK:
1640 case BUILT_IN_STPNCPY:
1641 case BUILT_IN_STPNCPY_CHK:
1644 case BUILT_IN_MEMCPY:
1645 case BUILT_IN_MEMCPY_CHK:
1646 case BUILT_IN_MEMMOVE:
1647 case BUILT_IN_MEMMOVE_CHK:
1648 if (params->length () < 3)
1651 dest = (*params)[0];
1654 case BUILT_IN_BCOPY:
1655 if (params->length () < 3)
1658 dest = (*params)[1];
1661 case BUILT_IN_MEMCMP:
1663 if (params->length () < 3)
1666 dest = (*params)[0];
1670 case BUILT_IN_MEMSET:
1671 case BUILT_IN_MEMSET_CHK:
1672 if (params->length () < 3)
1674 dest = (*params)[0];
1677 case BUILT_IN_BZERO:
1678 dest = (*params)[0];
1681 case BUILT_IN_STRNDUP:
1686 case BUILT_IN_MEMCHR:
1687 if (params->length () < 3)
1692 case BUILT_IN_SNPRINTF:
1693 case BUILT_IN_SNPRINTF_CHK:
1694 case BUILT_IN_VSNPRINTF:
1695 case BUILT_IN_VSNPRINTF_CHK:
1696 dest = (*params)[0];
1707 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1710 type = TYPE_P (sizeof_arg[idx])
1711 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1712 if (!POINTER_TYPE_P (type))
1716 && (tem = tree_strip_nop_conversions (dest))
1717 && POINTER_TYPE_P (TREE_TYPE (tem))
1718 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1722 && (tem = tree_strip_nop_conversions (src))
1723 && POINTER_TYPE_P (TREE_TYPE (tem))
1724 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1727 loc = sizeof_arg_loc[idx];
1731 if (!TYPE_P (sizeof_arg[idx])
1732 && operand_equal_p (dest, sizeof_arg[idx], 0)
1733 && comp_types (TREE_TYPE (dest), type))
1735 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1736 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1737 "argument to %<sizeof%> in %qD call is the same "
1738 "expression as the destination; did you mean to "
1739 "remove the addressof?", callee);
1740 else if ((TYPE_PRECISION (TREE_TYPE (type))
1741 == TYPE_PRECISION (char_type_node))
1743 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1744 "argument to %<sizeof%> in %qD call is the same "
1745 "expression as the destination; did you mean to "
1746 "provide an explicit length?", callee);
1748 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1749 "argument to %<sizeof%> in %qD call is the same "
1750 "expression as the destination; did you mean to "
1751 "dereference it?", callee);
1755 if (POINTER_TYPE_P (TREE_TYPE (dest))
1757 && comp_types (TREE_TYPE (dest), type)
1758 && !VOID_TYPE_P (TREE_TYPE (type)))
1760 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1761 "argument to %<sizeof%> in %qD call is the same "
1762 "pointer type %qT as the destination; expected %qT "
1763 "or an explicit length", callee, TREE_TYPE (dest),
1764 TREE_TYPE (TREE_TYPE (dest)));
1771 if (!TYPE_P (sizeof_arg[idx])
1772 && operand_equal_p (src, sizeof_arg[idx], 0)
1773 && comp_types (TREE_TYPE (src), type))
1775 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1776 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1777 "argument to %<sizeof%> in %qD call is the same "
1778 "expression as the source; did you mean to "
1779 "remove the addressof?", callee);
1780 else if ((TYPE_PRECISION (TREE_TYPE (type))
1781 == TYPE_PRECISION (char_type_node))
1783 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1784 "argument to %<sizeof%> in %qD call is the same "
1785 "expression as the source; did you mean to "
1786 "provide an explicit length?", callee);
1788 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1789 "argument to %<sizeof%> in %qD call is the same "
1790 "expression as the source; did you mean to "
1791 "dereference it?", callee);
1795 if (POINTER_TYPE_P (TREE_TYPE (src))
1797 && comp_types (TREE_TYPE (src), type)
1798 && !VOID_TYPE_P (TREE_TYPE (type)))
1800 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1801 "argument to %<sizeof%> in %qD call is the same "
1802 "pointer type %qT as the source; expected %qT "
1803 "or an explicit length", callee, TREE_TYPE (src),
1804 TREE_TYPE (TREE_TYPE (src)));
1811 if (!TYPE_P (sizeof_arg[idx])
1812 && operand_equal_p (dest, sizeof_arg[idx], 0)
1813 && comp_types (TREE_TYPE (dest), type))
1815 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1816 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1817 "argument to %<sizeof%> in %qD call is the same "
1818 "expression as the first source; did you mean to "
1819 "remove the addressof?", callee);
1820 else if ((TYPE_PRECISION (TREE_TYPE (type))
1821 == TYPE_PRECISION (char_type_node))
1823 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1824 "argument to %<sizeof%> in %qD call is the same "
1825 "expression as the first source; did you mean to "
1826 "provide an explicit length?", callee);
1828 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1829 "argument to %<sizeof%> in %qD call is the same "
1830 "expression as the first source; did you mean to "
1831 "dereference it?", callee);
1835 if (POINTER_TYPE_P (TREE_TYPE (dest))
1837 && comp_types (TREE_TYPE (dest), type)
1838 && !VOID_TYPE_P (TREE_TYPE (type)))
1840 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1841 "argument to %<sizeof%> in %qD call is the same "
1842 "pointer type %qT as the first source; expected %qT "
1843 "or an explicit length", callee, TREE_TYPE (dest),
1844 TREE_TYPE (TREE_TYPE (dest)));
1851 if (!TYPE_P (sizeof_arg[idx])
1852 && operand_equal_p (src, sizeof_arg[idx], 0)
1853 && comp_types (TREE_TYPE (src), type))
1855 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1856 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1857 "argument to %<sizeof%> in %qD call is the same "
1858 "expression as the second source; did you mean to "
1859 "remove the addressof?", callee);
1860 else if ((TYPE_PRECISION (TREE_TYPE (type))
1861 == TYPE_PRECISION (char_type_node))
1863 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1864 "argument to %<sizeof%> in %qD call is the same "
1865 "expression as the second source; did you mean to "
1866 "provide an explicit length?", callee);
1868 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1869 "argument to %<sizeof%> in %qD call is the same "
1870 "expression as the second source; did you mean to "
1871 "dereference it?", callee);
1875 if (POINTER_TYPE_P (TREE_TYPE (src))
1877 && comp_types (TREE_TYPE (src), type)
1878 && !VOID_TYPE_P (TREE_TYPE (type)))
1880 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1881 "argument to %<sizeof%> in %qD call is the same "
1882 "pointer type %qT as the second source; expected %qT "
1883 "or an explicit length", callee, TREE_TYPE (src),
1884 TREE_TYPE (TREE_TYPE (src)));
1891 /* Warn for unlikely, improbable, or stupid DECL declarations
1895 check_main_parameter_types (tree decl)
1897 function_args_iterator iter;
1901 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1903 /* XXX void_type_node belies the abstraction. */
1904 if (type == void_type_node || type == error_mark_node )
1908 if (TYPE_ATOMIC (t))
1909 pedwarn (input_location, OPT_Wmain,
1910 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1912 while (POINTER_TYPE_P (t))
1915 if (TYPE_ATOMIC (t))
1916 pedwarn (input_location, OPT_Wmain,
1917 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1925 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1926 pedwarn (input_location, OPT_Wmain,
1927 "first argument of %q+D should be %<int%>", decl);
1931 if (TREE_CODE (type) != POINTER_TYPE
1932 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1933 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1935 pedwarn (input_location, OPT_Wmain,
1936 "second argument of %q+D should be %<char **%>", decl);
1940 if (TREE_CODE (type) != POINTER_TYPE
1941 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1942 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1944 pedwarn (input_location, OPT_Wmain,
1945 "third argument of %q+D should probably be "
1946 "%<char **%>", decl);
1951 /* It is intentional that this message does not mention the third
1952 argument because it's only mentioned in an appendix of the
1954 if (argct > 0 && (argct < 2 || argct > 3))
1955 pedwarn (input_location, OPT_Wmain,
1956 "%q+D takes only zero or two arguments", decl);
1958 if (stdarg_p (TREE_TYPE (decl)))
1959 pedwarn (input_location, OPT_Wmain,
1960 "%q+D declared as variadic function", decl);
1963 /* vector_targets_convertible_p is used for vector pointer types. The
1964 callers perform various checks that the qualifiers are satisfactory,
1965 while OTOH vector_targets_convertible_p ignores the number of elements
1966 in the vectors. That's fine with vector pointers as we can consider,
1967 say, a vector of 8 elements as two consecutive vectors of 4 elements,
1968 and that does not require and conversion of the pointer values.
1969 In contrast, vector_types_convertible_p and
1970 vector_types_compatible_elements_p are used for vector value types. */
1971 /* True if pointers to distinct types T1 and T2 can be converted to
1972 each other without an explicit cast. Only returns true for opaque
1975 vector_targets_convertible_p (const_tree t1, const_tree t2)
1977 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
1978 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1979 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1985 /* vector_types_convertible_p is used for vector value types.
1986 It could in principle call vector_targets_convertible_p as a subroutine,
1987 but then the check for vector type would be duplicated with its callers,
1988 and also the purpose of vector_targets_convertible_p would become
1990 Where vector_types_convertible_p returns true, a conversion might still be
1991 needed to make the types match.
1992 In contrast, vector_targets_convertible_p is used for vector pointer
1993 values, and vector_types_compatible_elements_p is used specifically
1994 in the context for binary operators, as a check if use is possible without
1996 /* True if vector types T1 and T2 can be converted to each other
1997 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1998 can only be converted with -flax-vector-conversions yet that is not
1999 in effect, emit a note telling the user about that option if such
2000 a note has not previously been emitted. */
2002 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2004 static bool emitted_lax_note = false;
2005 bool convertible_lax;
2007 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2008 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2012 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2013 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2014 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2015 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2016 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2018 if (!convertible_lax || flag_lax_vector_conversions)
2019 return convertible_lax;
2021 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2022 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2025 if (emit_lax_note && !emitted_lax_note)
2027 emitted_lax_note = true;
2028 inform (input_location, "use -flax-vector-conversions to permit "
2029 "conversions between vectors with differing "
2030 "element types or numbers of subparts");
2036 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2037 and have vector types, V0 has the same type as V1, and the number of
2038 elements of V0, V1, MASK is the same.
2040 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2041 called with two arguments. In this case implementation passes the
2042 first argument twice in order to share the same tree code. This fact
2043 could enable the mask-values being twice the vector length. This is
2044 an implementation accident and this semantics is not guaranteed to
2047 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2052 bool maybe_const = false;
2053 bool two_arguments = false;
2055 if (v1 == NULL_TREE)
2057 two_arguments = true;
2061 if (v0 == error_mark_node || v1 == error_mark_node
2062 || mask == error_mark_node)
2063 return error_mark_node;
2065 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
2068 error_at (loc, "__builtin_shuffle last argument must "
2069 "be an integer vector");
2070 return error_mark_node;
2073 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2074 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
2077 error_at (loc, "__builtin_shuffle arguments must be vectors");
2078 return error_mark_node;
2081 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2084 error_at (loc, "__builtin_shuffle argument vectors must be of "
2086 return error_mark_node;
2089 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2090 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2091 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2092 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2095 error_at (loc, "__builtin_shuffle number of elements of the "
2096 "argument vector(s) and the mask vector should "
2098 return error_mark_node;
2101 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2102 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2105 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2106 "must have the same size as inner type of the mask");
2107 return error_mark_node;
2110 if (!c_dialect_cxx ())
2112 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2113 v0 = c_fully_fold (v0, false, &maybe_const);
2114 wrap &= maybe_const;
2117 v1 = v0 = save_expr (v0);
2120 v1 = c_fully_fold (v1, false, &maybe_const);
2121 wrap &= maybe_const;
2124 mask = c_fully_fold (mask, false, &maybe_const);
2125 wrap &= maybe_const;
2127 else if (two_arguments)
2128 v1 = v0 = save_expr (v0);
2130 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2132 if (!c_dialect_cxx () && !wrap)
2133 ret = c_wrap_maybe_const (ret, true);
2138 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2139 to integral type. */
2142 c_common_get_narrower (tree op, int *unsignedp_ptr)
2144 op = get_narrower (op, unsignedp_ptr);
2146 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2147 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2149 /* C++0x scoped enumerations don't implicitly convert to integral
2150 type; if we stripped an explicit conversion to a larger type we
2151 need to replace it so common_type will still work. */
2152 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2153 TYPE_UNSIGNED (TREE_TYPE (op)));
2154 op = fold_convert (type, op);
2159 /* This is a helper function of build_binary_op.
2161 For certain operations if both args were extended from the same
2162 smaller type, do the arithmetic in that type and then extend.
2164 BITWISE indicates a bitwise operation.
2165 For them, this optimization is safe only if
2166 both args are zero-extended or both are sign-extended.
2167 Otherwise, we might change the result.
2168 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2169 but calculated in (unsigned short) it would be (unsigned short)-1.
2172 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2174 int unsigned0, unsigned1;
2179 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2180 excessive narrowing when we call get_narrower below. For
2181 example, suppose that OP0 is of unsigned int extended
2182 from signed char and that RESULT_TYPE is long long int.
2183 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2186 (long long int) (unsigned int) signed_char
2188 which get_narrower would narrow down to
2190 (unsigned int) signed char
2192 If we do not cast OP0 first, get_narrower would return
2193 signed_char, which is inconsistent with the case of the
2195 op0 = convert (result_type, op0);
2196 op1 = convert (result_type, op1);
2198 arg0 = c_common_get_narrower (op0, &unsigned0);
2199 arg1 = c_common_get_narrower (op1, &unsigned1);
2201 /* UNS is 1 if the operation to be done is an unsigned one. */
2202 uns = TYPE_UNSIGNED (result_type);
2204 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2205 but it *requires* conversion to FINAL_TYPE. */
2207 if ((TYPE_PRECISION (TREE_TYPE (op0))
2208 == TYPE_PRECISION (TREE_TYPE (arg0)))
2209 && TREE_TYPE (op0) != result_type)
2210 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2211 if ((TYPE_PRECISION (TREE_TYPE (op1))
2212 == TYPE_PRECISION (TREE_TYPE (arg1)))
2213 && TREE_TYPE (op1) != result_type)
2214 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2216 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2218 /* For bitwise operations, signedness of nominal type
2219 does not matter. Consider only how operands were extended. */
2223 /* Note that in all three cases below we refrain from optimizing
2224 an unsigned operation on sign-extended args.
2225 That would not be valid. */
2227 /* Both args variable: if both extended in same way
2228 from same width, do it in that width.
2229 Do it unsigned if args were zero-extended. */
2230 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2231 < TYPE_PRECISION (result_type))
2232 && (TYPE_PRECISION (TREE_TYPE (arg1))
2233 == TYPE_PRECISION (TREE_TYPE (arg0)))
2234 && unsigned0 == unsigned1
2235 && (unsigned0 || !uns))
2236 return c_common_signed_or_unsigned_type
2237 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2239 else if (TREE_CODE (arg0) == INTEGER_CST
2240 && (unsigned1 || !uns)
2241 && (TYPE_PRECISION (TREE_TYPE (arg1))
2242 < TYPE_PRECISION (result_type))
2244 = c_common_signed_or_unsigned_type (unsigned1,
2246 && !POINTER_TYPE_P (type)
2247 && int_fits_type_p (arg0, type))
2250 else if (TREE_CODE (arg1) == INTEGER_CST
2251 && (unsigned0 || !uns)
2252 && (TYPE_PRECISION (TREE_TYPE (arg0))
2253 < TYPE_PRECISION (result_type))
2255 = c_common_signed_or_unsigned_type (unsigned0,
2257 && !POINTER_TYPE_P (type)
2258 && int_fits_type_p (arg1, type))
2264 /* Returns true iff any integer value of type FROM_TYPE can be represented as
2265 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2268 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2270 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2271 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2272 REAL_VALUE_TYPE real_low_bound =
2273 real_value_from_int_cst (0, type_low_bound);
2274 REAL_VALUE_TYPE real_high_bound =
2275 real_value_from_int_cst (0, type_high_bound);
2277 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2278 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2281 /* Checks if expression EXPR of complex/real/integer type cannot be converted
2282 to the complex/real/integer type TYPE. Function returns non-zero when:
2283 * EXPR is a constant which cannot be exactly converted to TYPE.
2284 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2285 for EXPR type and TYPE being both integers or both real, or both
2287 * EXPR is not a constant of complex type and TYPE is a real or
2289 * EXPR is not a constant of real type and TYPE is an integer.
2290 * EXPR is not a constant of integer type which cannot be
2291 exactly converted to real type.
2293 Function allows conversions between types of different signedness and
2294 can return SAFE_CONVERSION (zero) in that case. Function can produce
2295 signedness warnings if PRODUCE_WARNS is true.
2297 Function allows conversions from complex constants to non-complex types,
2298 provided that imaginary part is zero and real part can be safely converted
2301 enum conversion_safety
2302 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2304 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2305 tree expr_type = TREE_TYPE (expr);
2306 loc = expansion_point_location_if_in_system_header (loc);
2308 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2310 /* If type is complex, we are interested in compatibility with
2312 if (TREE_CODE (type) == COMPLEX_TYPE)
2313 type = TREE_TYPE (type);
2315 /* Warn for real constant that is not an exact integer converted
2317 if (TREE_CODE (expr_type) == REAL_TYPE
2318 && TREE_CODE (type) == INTEGER_TYPE)
2320 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2321 give_warning = UNSAFE_REAL;
2323 /* Warn for an integer constant that does not fit into integer type. */
2324 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2325 && TREE_CODE (type) == INTEGER_TYPE
2326 && !int_fits_type_p (expr, type))
2328 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2329 && tree_int_cst_sgn (expr) < 0)
2332 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2333 " implicitly converted to unsigned type");
2335 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2338 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2339 " constant value to negative integer");
2342 give_warning = UNSAFE_OTHER;
2344 else if (TREE_CODE (type) == REAL_TYPE)
2346 /* Warn for an integer constant that does not fit into real type. */
2347 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2349 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2350 if (!exact_real_truncate (TYPE_MODE (type), &a))
2351 give_warning = UNSAFE_REAL;
2353 /* Warn for a real constant that does not fit into a smaller
2355 else if (TREE_CODE (expr_type) == REAL_TYPE
2356 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2358 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2359 if (!exact_real_truncate (TYPE_MODE (type), &a))
2360 give_warning = UNSAFE_REAL;
2365 else if (TREE_CODE (expr) == COMPLEX_CST)
2367 tree imag_part = TREE_IMAGPART (expr);
2368 /* Conversion from complex constant with zero imaginary part,
2369 perform check for conversion of real part. */
2370 if ((TREE_CODE (imag_part) == REAL_CST
2371 && real_zerop (imag_part))
2372 || (TREE_CODE (imag_part) == INTEGER_CST
2373 && integer_zerop (imag_part)))
2374 /* Note: in this branch we use recursive call to unsafe_conversion_p
2375 with different type of EXPR, but it is still safe, because when EXPR
2376 is a constant, it's type is not used in text of generated warnings
2377 (otherwise they could sound misleading). */
2378 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2380 /* Conversion from complex constant with non-zero imaginary part. */
2383 /* Conversion to complex type.
2384 Perform checks for both real and imaginary parts. */
2385 if (TREE_CODE (type) == COMPLEX_TYPE)
2387 /* Unfortunately, produce_warns must be false in two subsequent
2388 calls of unsafe_conversion_p, because otherwise we could
2389 produce strange "double" warnings, if both real and imaginary
2390 parts have conversion problems related to signedness.
2393 int32_t _Complex a = 0x80000000 + 0x80000000i;
2395 Possible solution: add a separate function for checking
2396 constants and combine result of two calls appropriately. */
2397 enum conversion_safety re_safety =
2398 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2399 enum conversion_safety im_safety =
2400 unsafe_conversion_p (loc, type, imag_part, false);
2402 /* Merge the results into appropriate single warning. */
2404 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2405 if (re_safety == im_safety)
2406 give_warning = re_safety;
2407 else if (!re_safety && im_safety)
2408 give_warning = im_safety;
2409 else if (re_safety && !im_safety)
2410 give_warning = re_safety;
2412 give_warning = UNSAFE_OTHER;
2414 /* Warn about conversion from complex to real or integer type. */
2416 give_warning = UNSAFE_IMAGINARY;
2420 /* Checks for remaining case: EXPR is not constant. */
2423 /* Warn for real types converted to integer types. */
2424 if (TREE_CODE (expr_type) == REAL_TYPE
2425 && TREE_CODE (type) == INTEGER_TYPE)
2426 give_warning = UNSAFE_REAL;
2428 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2429 && TREE_CODE (type) == INTEGER_TYPE)
2431 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2432 expr = get_unwidened (expr, 0);
2433 expr_type = TREE_TYPE (expr);
2435 /* Don't warn for short y; short x = ((int)y & 0xff); */
2436 if (TREE_CODE (expr) == BIT_AND_EXPR
2437 || TREE_CODE (expr) == BIT_IOR_EXPR
2438 || TREE_CODE (expr) == BIT_XOR_EXPR)
2440 /* If both args were extended from a shortest type,
2441 use that type if that is safe. */
2442 expr_type = shorten_binary_op (expr_type,
2443 TREE_OPERAND (expr, 0),
2444 TREE_OPERAND (expr, 1),
2447 if (TREE_CODE (expr) == BIT_AND_EXPR)
2449 tree op0 = TREE_OPERAND (expr, 0);
2450 tree op1 = TREE_OPERAND (expr, 1);
2451 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2452 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2454 /* If one of the operands is a non-negative constant
2455 that fits in the target type, then the type of the
2456 other operand does not matter. */
2457 if ((TREE_CODE (op0) == INTEGER_CST
2458 && int_fits_type_p (op0, c_common_signed_type (type))
2459 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2460 || (TREE_CODE (op1) == INTEGER_CST
2461 && int_fits_type_p (op1, c_common_signed_type (type))
2462 && int_fits_type_p (op1,
2463 c_common_unsigned_type (type))))
2464 return SAFE_CONVERSION;
2465 /* If constant is unsigned and fits in the target
2466 type, then the result will also fit. */
2467 else if ((TREE_CODE (op0) == INTEGER_CST
2469 && int_fits_type_p (op0, type))
2470 || (TREE_CODE (op1) == INTEGER_CST
2472 && int_fits_type_p (op1, type)))
2473 return SAFE_CONVERSION;
2476 /* Warn for integer types converted to smaller integer types. */
2477 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2478 give_warning = UNSAFE_OTHER;
2480 /* When they are the same width but different signedness,
2481 then the value may change. */
2482 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2483 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2484 /* Even when converted to a bigger type, if the type is
2485 unsigned but expr is signed, then negative values
2487 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2489 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2490 "may change the sign of the result",
2494 /* Warn for integer types converted to real types if and only if
2495 all the range of values of the integer type cannot be
2496 represented by the real type. */
2497 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2498 && TREE_CODE (type) == REAL_TYPE)
2500 /* Don't warn about char y = 0xff; float x = (int) y; */
2501 expr = get_unwidened (expr, 0);
2502 expr_type = TREE_TYPE (expr);
2504 if (!int_safely_convertible_to_real_p (expr_type, type))
2505 give_warning = UNSAFE_OTHER;
2508 /* Warn for real types converted to smaller real types. */
2509 else if (TREE_CODE (expr_type) == REAL_TYPE
2510 && TREE_CODE (type) == REAL_TYPE
2511 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2512 give_warning = UNSAFE_REAL;
2514 /* Check conversion between two complex types. */
2515 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2516 && TREE_CODE (type) == COMPLEX_TYPE)
2518 /* Extract underlying types (i.e., type of real and imaginary
2519 parts) of expr_type and type. */
2520 tree from_type = TREE_TYPE (expr_type);
2521 tree to_type = TREE_TYPE (type);
2523 /* Warn for real types converted to integer types. */
2524 if (TREE_CODE (from_type) == REAL_TYPE
2525 && TREE_CODE (to_type) == INTEGER_TYPE)
2526 give_warning = UNSAFE_REAL;
2528 /* Warn for real types converted to smaller real types. */
2529 else if (TREE_CODE (from_type) == REAL_TYPE
2530 && TREE_CODE (to_type) == REAL_TYPE
2531 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2532 give_warning = UNSAFE_REAL;
2534 /* Check conversion for complex integer types. Here implementation
2535 is simpler than for real-domain integers because it does not
2536 involve sophisticated cases, such as bitmasks, casts, etc. */
2537 else if (TREE_CODE (from_type) == INTEGER_TYPE
2538 && TREE_CODE (to_type) == INTEGER_TYPE)
2540 /* Warn for integer types converted to smaller integer types. */
2541 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2542 give_warning = UNSAFE_OTHER;
2544 /* Check for different signedness, see case for real-domain
2545 integers (above) for a more detailed comment. */
2546 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2547 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2548 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2550 warning_at (loc, OPT_Wsign_conversion,
2551 "conversion to %qT from %qT "
2552 "may change the sign of the result",
2555 else if (TREE_CODE (from_type) == INTEGER_TYPE
2556 && TREE_CODE (to_type) == REAL_TYPE
2557 && !int_safely_convertible_to_real_p (from_type, to_type))
2558 give_warning = UNSAFE_OTHER;
2561 /* Warn for complex types converted to real or integer types. */
2562 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2563 && TREE_CODE (type) != COMPLEX_TYPE)
2564 give_warning = UNSAFE_IMAGINARY;
2567 return give_warning;
2570 /* Warns if the conversion of EXPR to TYPE may alter a value.
2571 This is a helper function for warnings_for_convert_and_check. */
2574 conversion_warning (location_t loc, tree type, tree expr)
2576 tree expr_type = TREE_TYPE (expr);
2577 enum conversion_safety conversion_kind;
2579 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2582 /* This may happen, because for LHS op= RHS we preevaluate
2583 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2584 means we could no longer see the code of the EXPR. */
2585 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2586 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2587 if (TREE_CODE (expr) == SAVE_EXPR)
2588 expr = TREE_OPERAND (expr, 0);
2590 switch (TREE_CODE (expr))
2598 case TRUTH_ANDIF_EXPR:
2599 case TRUTH_ORIF_EXPR:
2600 case TRUTH_AND_EXPR:
2602 case TRUTH_XOR_EXPR:
2603 case TRUTH_NOT_EXPR:
2604 /* Conversion from boolean to a signed:1 bit-field (which only
2605 can hold the values 0 and -1) doesn't lose information - but
2606 it does change the value. */
2607 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2608 warning_at (loc, OPT_Wconversion,
2609 "conversion to %qT from boolean expression", type);
2615 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2616 if (conversion_kind == UNSAFE_REAL)
2617 warning_at (loc, OPT_Wfloat_conversion,
2618 "conversion to %qT alters %qT constant value",
2620 else if (conversion_kind)
2621 warning_at (loc, OPT_Wconversion,
2622 "conversion to %qT alters %qT constant value",
2628 /* In case of COND_EXPR, we do not care about the type of
2629 COND_EXPR, only about the conversion of each operand. */
2630 tree op1 = TREE_OPERAND (expr, 1);
2631 tree op2 = TREE_OPERAND (expr, 2);
2633 conversion_warning (loc, type, op1);
2634 conversion_warning (loc, type, op2);
2638 default: /* 'expr' is not a constant. */
2639 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2640 if (conversion_kind == UNSAFE_REAL)
2641 warning_at (loc, OPT_Wfloat_conversion,
2642 "conversion to %qT from %qT may alter its value",
2644 else if (conversion_kind == UNSAFE_IMAGINARY)
2645 warning_at (loc, OPT_Wconversion,
2646 "conversion to %qT from %qT discards imaginary component",
2648 else if (conversion_kind)
2649 warning_at (loc, OPT_Wconversion,
2650 "conversion to %qT from %qT may alter its value",
2655 /* Produce warnings after a conversion. RESULT is the result of
2656 converting EXPR to TYPE. This is a helper function for
2657 convert_and_check and cp_convert_and_check. */
2660 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2663 loc = expansion_point_location_if_in_system_header (loc);
2665 if (TREE_CODE (expr) == INTEGER_CST
2666 && (TREE_CODE (type) == INTEGER_TYPE
2667 || TREE_CODE (type) == ENUMERAL_TYPE)
2668 && !int_fits_type_p (expr, type))
2670 /* Do not diagnose overflow in a constant expression merely
2671 because a conversion overflowed. */
2672 if (TREE_OVERFLOW (result))
2673 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2675 if (TYPE_UNSIGNED (type))
2677 /* This detects cases like converting -129 or 256 to
2679 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2680 warning_at (loc, OPT_Woverflow,
2681 "large integer implicitly truncated to unsigned type");
2683 conversion_warning (loc, type, expr);
2685 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2686 warning_at (loc, OPT_Woverflow,
2687 "overflow in implicit constant conversion");
2688 /* No warning for converting 0x80000000 to int. */
2690 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2691 || TYPE_PRECISION (TREE_TYPE (expr))
2692 != TYPE_PRECISION (type)))
2693 warning_at (loc, OPT_Woverflow,
2694 "overflow in implicit constant conversion");
2697 conversion_warning (loc, type, expr);
2699 else if ((TREE_CODE (result) == INTEGER_CST
2700 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2701 warning_at (loc, OPT_Woverflow,
2702 "overflow in implicit constant conversion");
2704 conversion_warning (loc, type, expr);
2708 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2709 Invoke this function on every expression that is converted implicitly,
2710 i.e. because of language rules and not because of an explicit cast. */
2713 convert_and_check (location_t loc, tree type, tree expr)
2716 tree expr_for_warning;
2718 /* Convert from a value with possible excess precision rather than
2719 via the semantic type, but do not warn about values not fitting
2720 exactly in the semantic type. */
2721 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2723 tree orig_type = TREE_TYPE (expr);
2724 expr = TREE_OPERAND (expr, 0);
2725 expr_for_warning = convert (orig_type, expr);
2726 if (orig_type == type)
2727 return expr_for_warning;
2730 expr_for_warning = expr;
2732 if (TREE_TYPE (expr) == type)
2735 result = convert (type, expr);
2737 if (c_inhibit_evaluation_warnings == 0
2738 && !TREE_OVERFLOW_P (expr)
2739 && result != error_mark_node)
2740 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2745 /* A node in a list that describes references to variables (EXPR), which are
2746 either read accesses if WRITER is zero, or write accesses, in which case
2747 WRITER is the parent of EXPR. */
2754 /* Used to implement a cache the results of a call to verify_tree. We only
2755 use this for SAVE_EXPRs. */
2758 struct tlist_cache *next;
2759 struct tlist *cache_before_sp;
2760 struct tlist *cache_after_sp;
2764 /* Obstack to use when allocating tlist structures, and corresponding
2766 static struct obstack tlist_obstack;
2767 static char *tlist_firstobj = 0;
2769 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2771 static struct tlist *warned_ids;
2772 /* SAVE_EXPRs need special treatment. We process them only once and then
2773 cache the results. */
2774 static struct tlist_cache *save_expr_cache;
2776 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2777 static void merge_tlist (struct tlist **, struct tlist *, int);
2778 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2779 static int warning_candidate_p (tree);
2780 static bool candidate_equal_p (const_tree, const_tree);
2781 static void warn_for_collisions (struct tlist *);
2782 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2783 static struct tlist *new_tlist (struct tlist *, tree, tree);
2785 /* Create a new struct tlist and fill in its fields. */
2786 static struct tlist *
2787 new_tlist (struct tlist *next, tree t, tree writer)
2790 l = XOBNEW (&tlist_obstack, struct tlist);
2797 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2798 is nonnull, we ignore any node we find which has a writer equal to it. */
2801 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2805 struct tlist *next = add->next;
2808 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2809 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2814 /* Merge the nodes of ADD into TO. This merging process is done so that for
2815 each variable that already exists in TO, no new node is added; however if
2816 there is a write access recorded in ADD, and an occurrence on TO is only
2817 a read access, then the occurrence in TO will be modified to record the
2821 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2823 struct tlist **end = to;
2826 end = &(*end)->next;
2832 struct tlist *next = add->next;
2834 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2835 if (candidate_equal_p (tmp2->expr, add->expr))
2839 tmp2->writer = add->writer;
2843 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
2844 end = &(*end)->next;
2851 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2852 references in list LIST conflict with it, excluding reads if ONLY writers
2856 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2861 /* Avoid duplicate warnings. */
2862 for (tmp = warned_ids; tmp; tmp = tmp->next)
2863 if (candidate_equal_p (tmp->expr, written))
2868 if (candidate_equal_p (list->expr, written)
2869 && !candidate_equal_p (list->writer, writer)
2870 && (!only_writes || list->writer))
2872 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2873 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
2874 OPT_Wsequence_point, "operation on %qE may be undefined",
2881 /* Given a list LIST of references to variables, find whether any of these
2882 can cause conflicts due to missing sequence points. */
2885 warn_for_collisions (struct tlist *list)
2889 for (tmp = list; tmp; tmp = tmp->next)
2892 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2896 /* Return nonzero if X is a tree that can be verified by the sequence point
2899 warning_candidate_p (tree x)
2901 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2904 if (TREE_CODE (x) == BLOCK)
2907 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2908 (lvalue_p) crash on TRY/CATCH. */
2909 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2915 /* No point to track non-const calls, they will never satisfy
2917 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2920 if (TREE_CODE (x) == STRING_CST)
2926 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2928 candidate_equal_p (const_tree x, const_tree y)
2930 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2933 /* Walk the tree X, and record accesses to variables. If X is written by the
2934 parent tree, WRITER is the parent.
2935 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2936 expression or its only operand forces a sequence point, then everything up
2937 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2939 Once we return, we will have emitted warnings if any subexpression before
2940 such a sequence point could be undefined. On a higher level, however, the
2941 sequence point may not be relevant, and we'll merge the two lists.
2943 Example: (b++, a) + b;
2944 The call that processes the COMPOUND_EXPR will store the increment of B
2945 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2946 processes the PLUS_EXPR will need to merge the two lists so that
2947 eventually, all accesses end up on the same list (and we'll warn about the
2948 unordered subexpressions b++ and b.
2950 A note on merging. If we modify the former example so that our expression
2953 care must be taken not simply to add all three expressions into the final
2954 PNO_SP list. The function merge_tlist takes care of that by merging the
2955 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2956 way, so that no more than one access to B is recorded. */
2959 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2962 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2963 enum tree_code code;
2964 enum tree_code_class cl;
2966 /* X may be NULL if it is the operand of an empty statement expression
2972 code = TREE_CODE (x);
2973 cl = TREE_CODE_CLASS (code);
2975 if (warning_candidate_p (x))
2976 *pno_sp = new_tlist (*pno_sp, x, writer);
2985 case TRUTH_ANDIF_EXPR:
2986 case TRUTH_ORIF_EXPR:
2987 tmp_before = tmp_nosp = tmp_list3 = 0;
2988 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2989 warn_for_collisions (tmp_nosp);
2990 merge_tlist (pbefore_sp, tmp_before, 0);
2991 merge_tlist (pbefore_sp, tmp_nosp, 0);
2992 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2993 merge_tlist (pbefore_sp, tmp_list3, 0);
2997 tmp_before = tmp_list2 = 0;
2998 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2999 warn_for_collisions (tmp_list2);
3000 merge_tlist (pbefore_sp, tmp_before, 0);
3001 merge_tlist (pbefore_sp, tmp_list2, 0);
3003 tmp_list3 = tmp_nosp = 0;
3004 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3005 warn_for_collisions (tmp_nosp);
3006 merge_tlist (pbefore_sp, tmp_list3, 0);
3008 tmp_list3 = tmp_list2 = 0;
3009 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3010 warn_for_collisions (tmp_list2);
3011 merge_tlist (pbefore_sp, tmp_list3, 0);
3012 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3013 two first, to avoid warning for (a ? b++ : b++). */
3014 merge_tlist (&tmp_nosp, tmp_list2, 0);
3015 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3018 case PREDECREMENT_EXPR:
3019 case PREINCREMENT_EXPR:
3020 case POSTDECREMENT_EXPR:
3021 case POSTINCREMENT_EXPR:
3022 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3026 tmp_before = tmp_nosp = tmp_list3 = 0;
3027 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3028 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3029 /* Expressions inside the LHS are not ordered wrt. the sequence points
3030 in the RHS. Example:
3032 Despite the fact that the modification of "a" is in the before_sp
3033 list (tmp_before), it conflicts with the use of "a" in the LHS.
3034 We can handle this by adding the contents of tmp_list3
3035 to those of tmp_before, and redoing the collision warnings for that
3037 add_tlist (&tmp_before, tmp_list3, x, 1);
3038 warn_for_collisions (tmp_before);
3039 /* Exclude the LHS itself here; we first have to merge it into the
3040 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3041 didn't exclude the LHS, we'd get it twice, once as a read and once
3043 add_tlist (pno_sp, tmp_list3, x, 0);
3044 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3046 merge_tlist (pbefore_sp, tmp_before, 0);
3047 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3048 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3049 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3053 /* We need to warn about conflicts among arguments and conflicts between
3054 args and the function address. Side effects of the function address,
3055 however, are not ordered by the sequence point of the call. */
3057 call_expr_arg_iterator iter;
3059 tmp_before = tmp_nosp = 0;
3060 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3061 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3063 tmp_list2 = tmp_list3 = 0;
3064 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3065 merge_tlist (&tmp_list3, tmp_list2, 0);
3066 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3068 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3069 warn_for_collisions (tmp_before);
3070 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3075 /* Scan all the list, e.g. indices of multi dimensional array. */
3078 tmp_before = tmp_nosp = 0;
3079 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3080 merge_tlist (&tmp_nosp, tmp_before, 0);
3081 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3088 struct tlist_cache *t;
3089 for (t = save_expr_cache; t; t = t->next)
3090 if (candidate_equal_p (t->expr, x))
3095 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3096 t->next = save_expr_cache;
3098 save_expr_cache = t;
3100 tmp_before = tmp_nosp = 0;
3101 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3102 warn_for_collisions (tmp_nosp);
3105 merge_tlist (&tmp_list3, tmp_nosp, 0);
3106 t->cache_before_sp = tmp_before;
3107 t->cache_after_sp = tmp_list3;
3109 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3110 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3115 x = TREE_OPERAND (x, 0);
3122 /* For other expressions, simply recurse on their operands.
3123 Manual tail recursion for unary expressions.
3124 Other non-expressions need not be processed. */
3125 if (cl == tcc_unary)
3127 x = TREE_OPERAND (x, 0);
3131 else if (IS_EXPR_CODE_CLASS (cl))
3134 int max = TREE_OPERAND_LENGTH (x);
3135 for (lp = 0; lp < max; lp++)
3137 tmp_before = tmp_nosp = 0;
3138 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3139 merge_tlist (&tmp_nosp, tmp_before, 0);
3140 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3147 /* Try to warn for undefined behavior in EXPR due to missing sequence
3151 verify_sequence_points (tree expr)
3153 struct tlist *before_sp = 0, *after_sp = 0;
3156 save_expr_cache = 0;
3157 if (tlist_firstobj == 0)
3159 gcc_obstack_init (&tlist_obstack);
3160 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3163 verify_tree (expr, &before_sp, &after_sp, 0);
3164 warn_for_collisions (after_sp);
3165 obstack_free (&tlist_obstack, tlist_firstobj);
3168 /* Validate the expression after `case' and apply default promotions. */
3171 check_case_value (location_t loc, tree value)
3173 if (value == NULL_TREE)
3176 if (TREE_CODE (value) == INTEGER_CST)
3177 /* Promote char or short to int. */
3178 value = perform_integral_promotions (value);
3179 else if (value != error_mark_node)
3181 error_at (loc, "case label does not reduce to an integer constant");
3182 value = error_mark_node;
3185 constant_expression_warning (value);
3190 /* See if the case values LOW and HIGH are in the range of the original
3191 type (i.e. before the default conversion to int) of the switch testing
3193 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3194 the type before promoting it. CASE_LOW_P is a pointer to the lower
3195 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3196 if the case is not a case range.
3197 The caller has to make sure that we are not called with NULL for
3198 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3199 was a case value that doesn't fit into the range of the ORIG_TYPE.
3200 Returns true if the case label is in range of ORIG_TYPE (saturated or
3201 untouched) or false if the label is out of range. */
3204 check_case_bounds (location_t loc, tree type, tree orig_type,
3205 tree *case_low_p, tree *case_high_p,
3206 bool *outside_range_p)
3208 tree min_value, max_value;
3209 tree case_low = *case_low_p;
3210 tree case_high = case_high_p ? *case_high_p : case_low;
3212 /* If there was a problem with the original type, do nothing. */
3213 if (orig_type == error_mark_node)
3216 min_value = TYPE_MIN_VALUE (orig_type);
3217 max_value = TYPE_MAX_VALUE (orig_type);
3219 /* We'll really need integer constants here. */
3220 case_low = fold (case_low);
3221 case_high = fold (case_high);
3223 /* Case label is less than minimum for type. */
3224 if (tree_int_cst_compare (case_low, min_value) < 0
3225 && tree_int_cst_compare (case_high, min_value) < 0)
3227 warning_at (loc, 0, "case label value is less than minimum value "
3229 *outside_range_p = true;
3233 /* Case value is greater than maximum for type. */
3234 if (tree_int_cst_compare (case_low, max_value) > 0
3235 && tree_int_cst_compare (case_high, max_value) > 0)
3237 warning_at (loc, 0, "case label value exceeds maximum value for type");
3238 *outside_range_p = true;
3242 /* Saturate lower case label value to minimum. */
3243 if (tree_int_cst_compare (case_high, min_value) >= 0
3244 && tree_int_cst_compare (case_low, min_value) < 0)
3246 warning_at (loc, 0, "lower value in case label range"
3247 " less than minimum value for type");
3248 *outside_range_p = true;
3249 case_low = min_value;
3252 /* Saturate upper case label value to maximum. */
3253 if (tree_int_cst_compare (case_low, max_value) <= 0
3254 && tree_int_cst_compare (case_high, max_value) > 0)
3256 warning_at (loc, 0, "upper value in case label range"
3257 " exceeds maximum value for type");
3258 *outside_range_p = true;
3259 case_high = max_value;
3262 if (*case_low_p != case_low)
3263 *case_low_p = convert (type, case_low);
3264 if (case_high_p && *case_high_p != case_high)
3265 *case_high_p = convert (type, case_high);
3270 /* Return an integer type with BITS bits of precision,
3271 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3274 c_common_type_for_size (unsigned int bits, int unsignedp)
3278 if (bits == TYPE_PRECISION (integer_type_node))
3279 return unsignedp ? unsigned_type_node : integer_type_node;
3281 if (bits == TYPE_PRECISION (signed_char_type_node))
3282 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3284 if (bits == TYPE_PRECISION (short_integer_type_node))
3285 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3287 if (bits == TYPE_PRECISION (long_integer_type_node))
3288 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3290 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3291 return (unsignedp ? long_long_unsigned_type_node
3292 : long_long_integer_type_node);
3294 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3295 if (int_n_enabled_p[i]
3296 && bits == int_n_data[i].bitsize)
3297 return (unsignedp ? int_n_trees[i].unsigned_type
3298 : int_n_trees[i].signed_type);
3300 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3301 return (unsignedp ? widest_unsigned_literal_type_node
3302 : widest_integer_literal_type_node);
3304 if (bits <= TYPE_PRECISION (intQI_type_node))
3305 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3307 if (bits <= TYPE_PRECISION (intHI_type_node))
3308 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3310 if (bits <= TYPE_PRECISION (intSI_type_node))
3311 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3313 if (bits <= TYPE_PRECISION (intDI_type_node))
3314 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3319 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3320 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3321 and saturating if SATP is nonzero, otherwise not saturating. */
3324 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3325 int unsignedp, int satp)
3329 mode = unsignedp ? UQQmode : QQmode;
3331 mode = unsignedp ? UHAmode : HAmode;
3333 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3334 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3337 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3339 sorry ("GCC cannot support operators with integer types and "
3340 "fixed-point types that have too many integral and "
3341 "fractional bits together");
3345 return c_common_type_for_mode (mode, satp);
3348 /* Used for communication between c_common_type_for_mode and
3349 c_register_builtin_type. */
3350 tree registered_builtin_types;
3352 /* Return a data type that has machine mode MODE.
3353 If the mode is an integer,
3354 then UNSIGNEDP selects between signed and unsigned types.
3355 If the mode is a fixed-point mode,
3356 then UNSIGNEDP selects between saturating and nonsaturating types. */
3359 c_common_type_for_mode (machine_mode mode, int unsignedp)
3364 if (mode == TYPE_MODE (integer_type_node))
3365 return unsignedp ? unsigned_type_node : integer_type_node;
3367 if (mode == TYPE_MODE (signed_char_type_node))
3368 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3370 if (mode == TYPE_MODE (short_integer_type_node))
3371 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3373 if (mode == TYPE_MODE (long_integer_type_node))
3374 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3376 if (mode == TYPE_MODE (long_long_integer_type_node))
3377 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3379 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3380 if (int_n_enabled_p[i]
3381 && mode == int_n_data[i].m)
3382 return (unsignedp ? int_n_trees[i].unsigned_type
3383 : int_n_trees[i].signed_type);
3385 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3386 return unsignedp ? widest_unsigned_literal_type_node
3387 : widest_integer_literal_type_node;
3390 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3393 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3396 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3399 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3401 #if HOST_BITS_PER_WIDE_INT >= 64
3402 if (mode == TYPE_MODE (intTI_type_node))
3403 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3406 if (mode == TYPE_MODE (float_type_node))
3407 return float_type_node;
3409 if (mode == TYPE_MODE (double_type_node))
3410 return double_type_node;
3412 if (mode == TYPE_MODE (long_double_type_node))
3413 return long_double_type_node;
3415 if (mode == TYPE_MODE (void_type_node))
3416 return void_type_node;
3418 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3420 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3421 : make_signed_type (GET_MODE_PRECISION (mode)));
3423 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3425 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3426 : make_signed_type (GET_MODE_PRECISION (mode)));
3428 if (COMPLEX_MODE_P (mode))
3430 machine_mode inner_mode;
3433 if (mode == TYPE_MODE (complex_float_type_node))
3434 return complex_float_type_node;
3435 if (mode == TYPE_MODE (complex_double_type_node))
3436 return complex_double_type_node;
3437 if (mode == TYPE_MODE (complex_long_double_type_node))
3438 return complex_long_double_type_node;
3440 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3441 return complex_integer_type_node;
3443 inner_mode = GET_MODE_INNER (mode);
3444 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3445 if (inner_type != NULL_TREE)
3446 return build_complex_type (inner_type);
3448 else if (VECTOR_MODE_P (mode))
3450 machine_mode inner_mode = GET_MODE_INNER (mode);
3451 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3452 if (inner_type != NULL_TREE)
3453 return build_vector_type_for_mode (inner_type, mode);
3456 if (mode == TYPE_MODE (dfloat32_type_node))
3457 return dfloat32_type_node;
3458 if (mode == TYPE_MODE (dfloat64_type_node))
3459 return dfloat64_type_node;
3460 if (mode == TYPE_MODE (dfloat128_type_node))
3461 return dfloat128_type_node;
3463 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3465 if (mode == TYPE_MODE (short_fract_type_node))
3466 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3467 if (mode == TYPE_MODE (fract_type_node))
3468 return unsignedp ? sat_fract_type_node : fract_type_node;
3469 if (mode == TYPE_MODE (long_fract_type_node))
3470 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3471 if (mode == TYPE_MODE (long_long_fract_type_node))
3472 return unsignedp ? sat_long_long_fract_type_node
3473 : long_long_fract_type_node;
3475 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3476 return unsignedp ? sat_unsigned_short_fract_type_node
3477 : unsigned_short_fract_type_node;
3478 if (mode == TYPE_MODE (unsigned_fract_type_node))
3479 return unsignedp ? sat_unsigned_fract_type_node
3480 : unsigned_fract_type_node;
3481 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3482 return unsignedp ? sat_unsigned_long_fract_type_node
3483 : unsigned_long_fract_type_node;
3484 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3485 return unsignedp ? sat_unsigned_long_long_fract_type_node
3486 : unsigned_long_long_fract_type_node;
3488 if (mode == TYPE_MODE (short_accum_type_node))
3489 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3490 if (mode == TYPE_MODE (accum_type_node))
3491 return unsignedp ? sat_accum_type_node : accum_type_node;
3492 if (mode == TYPE_MODE (long_accum_type_node))
3493 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3494 if (mode == TYPE_MODE (long_long_accum_type_node))
3495 return unsignedp ? sat_long_long_accum_type_node
3496 : long_long_accum_type_node;
3498 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3499 return unsignedp ? sat_unsigned_short_accum_type_node
3500 : unsigned_short_accum_type_node;
3501 if (mode == TYPE_MODE (unsigned_accum_type_node))
3502 return unsignedp ? sat_unsigned_accum_type_node
3503 : unsigned_accum_type_node;
3504 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3505 return unsignedp ? sat_unsigned_long_accum_type_node
3506 : unsigned_long_accum_type_node;
3507 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3508 return unsignedp ? sat_unsigned_long_long_accum_type_node
3509 : unsigned_long_long_accum_type_node;
3512 return unsignedp ? sat_qq_type_node : qq_type_node;
3514 return unsignedp ? sat_hq_type_node : hq_type_node;
3516 return unsignedp ? sat_sq_type_node : sq_type_node;
3518 return unsignedp ? sat_dq_type_node : dq_type_node;
3520 return unsignedp ? sat_tq_type_node : tq_type_node;
3522 if (mode == UQQmode)
3523 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3524 if (mode == UHQmode)
3525 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3526 if (mode == USQmode)
3527 return unsignedp ? sat_usq_type_node : usq_type_node;
3528 if (mode == UDQmode)
3529 return unsignedp ? sat_udq_type_node : udq_type_node;
3530 if (mode == UTQmode)
3531 return unsignedp ? sat_utq_type_node : utq_type_node;
3534 return unsignedp ? sat_ha_type_node : ha_type_node;
3536 return unsignedp ? sat_sa_type_node : sa_type_node;
3538 return unsignedp ? sat_da_type_node : da_type_node;
3540 return unsignedp ? sat_ta_type_node : ta_type_node;
3542 if (mode == UHAmode)
3543 return unsignedp ? sat_uha_type_node : uha_type_node;
3544 if (mode == USAmode)
3545 return unsignedp ? sat_usa_type_node : usa_type_node;
3546 if (mode == UDAmode)
3547 return unsignedp ? sat_uda_type_node : uda_type_node;
3548 if (mode == UTAmode)
3549 return unsignedp ? sat_uta_type_node : uta_type_node;
3552 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3553 if (TYPE_MODE (TREE_VALUE (t)) == mode
3554 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3555 return TREE_VALUE (t);
3561 c_common_unsigned_type (tree type)
3563 return c_common_signed_or_unsigned_type (1, type);
3566 /* Return a signed type the same as TYPE in other respects. */
3569 c_common_signed_type (tree type)
3571 return c_common_signed_or_unsigned_type (0, type);
3574 /* Return a type the same as TYPE except unsigned or
3575 signed according to UNSIGNEDP. */
3578 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3583 /* This block of code emulates the behavior of the old
3584 c_common_unsigned_type. In particular, it returns
3585 long_unsigned_type_node if passed a long, even when a int would
3586 have the same size. This is necessary for warnings to work
3587 correctly in archs where sizeof(int) == sizeof(long) */
3589 type1 = TYPE_MAIN_VARIANT (type);
3590 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3591 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3592 if (type1 == integer_type_node || type1 == unsigned_type_node)
3593 return unsignedp ? unsigned_type_node : integer_type_node;
3594 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3595 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3596 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3597 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3598 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3599 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3601 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3602 if (int_n_enabled_p[i]
3603 && (type1 == int_n_trees[i].unsigned_type
3604 || type1 == int_n_trees[i].signed_type))
3605 return (unsignedp ? int_n_trees[i].unsigned_type
3606 : int_n_trees[i].signed_type);
3608 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3609 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3610 #if HOST_BITS_PER_WIDE_INT >= 64
3611 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3612 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3614 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3615 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3616 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3617 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3618 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3619 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3620 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3621 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3623 #define C_COMMON_FIXED_TYPES(NAME) \
3624 if (type1 == short_ ## NAME ## _type_node \
3625 || type1 == unsigned_short_ ## NAME ## _type_node) \
3626 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3627 : short_ ## NAME ## _type_node; \
3628 if (type1 == NAME ## _type_node \
3629 || type1 == unsigned_ ## NAME ## _type_node) \
3630 return unsignedp ? unsigned_ ## NAME ## _type_node \
3631 : NAME ## _type_node; \
3632 if (type1 == long_ ## NAME ## _type_node \
3633 || type1 == unsigned_long_ ## NAME ## _type_node) \
3634 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3635 : long_ ## NAME ## _type_node; \
3636 if (type1 == long_long_ ## NAME ## _type_node \
3637 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3638 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3639 : long_long_ ## NAME ## _type_node;
3641 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3642 if (type1 == NAME ## _type_node \
3643 || type1 == u ## NAME ## _type_node) \
3644 return unsignedp ? u ## NAME ## _type_node \
3645 : NAME ## _type_node;
3647 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3648 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3649 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3650 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3651 : sat_ ## short_ ## NAME ## _type_node; \
3652 if (type1 == sat_ ## NAME ## _type_node \
3653 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3654 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3655 : sat_ ## NAME ## _type_node; \
3656 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3657 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3658 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3659 : sat_ ## long_ ## NAME ## _type_node; \
3660 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3661 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3662 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3663 : sat_ ## long_long_ ## NAME ## _type_node;
3665 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3666 if (type1 == sat_ ## NAME ## _type_node \
3667 || type1 == sat_ ## u ## NAME ## _type_node) \
3668 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3669 : sat_ ## NAME ## _type_node;
3671 C_COMMON_FIXED_TYPES (fract);
3672 C_COMMON_FIXED_TYPES_SAT (fract);
3673 C_COMMON_FIXED_TYPES (accum);
3674 C_COMMON_FIXED_TYPES_SAT (accum);
3676 C_COMMON_FIXED_MODE_TYPES (qq);
3677 C_COMMON_FIXED_MODE_TYPES (hq);
3678 C_COMMON_FIXED_MODE_TYPES (sq);
3679 C_COMMON_FIXED_MODE_TYPES (dq);
3680 C_COMMON_FIXED_MODE_TYPES (tq);
3681 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3682 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3683 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3684 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3685 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3686 C_COMMON_FIXED_MODE_TYPES (ha);
3687 C_COMMON_FIXED_MODE_TYPES (sa);
3688 C_COMMON_FIXED_MODE_TYPES (da);
3689 C_COMMON_FIXED_MODE_TYPES (ta);
3690 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3691 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3692 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3693 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3695 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3696 the precision; they have precision set to match their range, but
3697 may use a wider mode to match an ABI. If we change modes, we may
3698 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3699 the precision as well, so as to yield correct results for
3700 bit-field types. C++ does not have these separate bit-field
3701 types, and producing a signed or unsigned variant of an
3702 ENUMERAL_TYPE may cause other problems as well. */
3704 if (!INTEGRAL_TYPE_P (type)
3705 || TYPE_UNSIGNED (type) == unsignedp)
3708 #define TYPE_OK(node) \
3709 (TYPE_MODE (type) == TYPE_MODE (node) \
3710 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3711 if (TYPE_OK (signed_char_type_node))
3712 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3713 if (TYPE_OK (integer_type_node))
3714 return unsignedp ? unsigned_type_node : integer_type_node;
3715 if (TYPE_OK (short_integer_type_node))
3716 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3717 if (TYPE_OK (long_integer_type_node))
3718 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3719 if (TYPE_OK (long_long_integer_type_node))
3720 return (unsignedp ? long_long_unsigned_type_node
3721 : long_long_integer_type_node);
3723 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3724 if (int_n_enabled_p[i]
3725 && TYPE_MODE (type) == int_n_data[i].m
3726 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3727 return (unsignedp ? int_n_trees[i].unsigned_type
3728 : int_n_trees[i].signed_type);
3730 if (TYPE_OK (widest_integer_literal_type_node))
3731 return (unsignedp ? widest_unsigned_literal_type_node
3732 : widest_integer_literal_type_node);
3734 #if HOST_BITS_PER_WIDE_INT >= 64
3735 if (TYPE_OK (intTI_type_node))
3736 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3738 if (TYPE_OK (intDI_type_node))
3739 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3740 if (TYPE_OK (intSI_type_node))
3741 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3742 if (TYPE_OK (intHI_type_node))
3743 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3744 if (TYPE_OK (intQI_type_node))
3745 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3748 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3751 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3754 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3758 /* Extended integer types of the same width as a standard type have
3759 lesser rank, so those of the same width as int promote to int or
3760 unsigned int and are valid for printf formats expecting int or
3761 unsigned int. To avoid such special cases, avoid creating
3762 extended integer types for bit-fields if a standard integer type
3764 if (width == TYPE_PRECISION (integer_type_node))
3765 return unsignedp ? unsigned_type_node : integer_type_node;
3766 if (width == TYPE_PRECISION (signed_char_type_node))
3767 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3768 if (width == TYPE_PRECISION (short_integer_type_node))
3769 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3770 if (width == TYPE_PRECISION (long_integer_type_node))
3771 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3772 if (width == TYPE_PRECISION (long_long_integer_type_node))
3773 return (unsignedp ? long_long_unsigned_type_node
3774 : long_long_integer_type_node);
3775 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3776 if (int_n_enabled_p[i]
3777 && width == int_n_data[i].bitsize)
3778 return (unsignedp ? int_n_trees[i].unsigned_type
3779 : int_n_trees[i].signed_type);
3780 return build_nonstandard_integer_type (width, unsignedp);
3783 /* The C version of the register_builtin_type langhook. */
3786 c_register_builtin_type (tree type, const char* name)
3790 decl = build_decl (UNKNOWN_LOCATION,
3791 TYPE_DECL, get_identifier (name), type);
3792 DECL_ARTIFICIAL (decl) = 1;
3793 if (!TYPE_NAME (type))
3794 TYPE_NAME (type) = decl;
3797 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3800 /* Print an error message for invalid operands to arith operation
3801 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3802 RICHLOC is a rich location for the message, containing either
3803 three separate locations for each of the operator and operands
3808 (C FE), or one location ranging over all over them
3816 binary_op_error (rich_location *richloc, enum tree_code code,
3817 tree type0, tree type1)
3824 opname = "+"; break;
3826 opname = "-"; break;
3828 opname = "*"; break;
3830 opname = "max"; break;
3832 opname = "min"; break;
3834 opname = "=="; break;
3836 opname = "!="; break;
3838 opname = "<="; break;
3840 opname = ">="; break;
3842 opname = "<"; break;
3844 opname = ">"; break;
3846 opname = "<<"; break;
3848 opname = ">>"; break;
3849 case TRUNC_MOD_EXPR:
3850 case FLOOR_MOD_EXPR:
3851 opname = "%"; break;
3852 case TRUNC_DIV_EXPR:
3853 case FLOOR_DIV_EXPR:
3854 opname = "/"; break;
3856 opname = "&"; break;
3858 opname = "|"; break;
3859 case TRUTH_ANDIF_EXPR:
3860 opname = "&&"; break;
3861 case TRUTH_ORIF_EXPR:
3862 opname = "||"; break;
3864 opname = "^"; break;
3868 error_at_rich_loc (richloc,
3869 "invalid operands to binary %s (have %qT and %qT)",
3870 opname, type0, type1);
3873 /* Given an expression as a tree, return its original type. Do this
3874 by stripping any conversion that preserves the sign and precision. */
3876 expr_original_type (tree expr)
3878 STRIP_SIGN_NOPS (expr);
3879 return TREE_TYPE (expr);
3882 /* Subroutine of build_binary_op, used for comparison operations.
3883 See if the operands have both been converted from subword integer types
3884 and, if so, perhaps change them both back to their original type.
3885 This function is also responsible for converting the two operands
3886 to the proper common type for comparison.
3888 The arguments of this function are all pointers to local variables
3889 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3890 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3892 LOC is the location of the comparison.
3894 If this function returns nonzero, it means that the comparison has
3895 a constant value. What this function returns is an expression for
3899 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3900 tree *restype_ptr, enum tree_code *rescode_ptr)
3903 tree op0 = *op0_ptr;
3904 tree op1 = *op1_ptr;
3905 int unsignedp0, unsignedp1;
3907 tree primop0, primop1;
3908 enum tree_code code = *rescode_ptr;
3910 /* Throw away any conversions to wider types
3911 already present in the operands. */
3913 primop0 = c_common_get_narrower (op0, &unsignedp0);
3914 primop1 = c_common_get_narrower (op1, &unsignedp1);
3916 /* If primopN is first sign-extended from primopN's precision to opN's
3917 precision, then zero-extended from opN's precision to
3918 *restype_ptr precision, shortenings might be invalid. */
3919 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3920 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3922 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3924 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3925 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3927 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3930 /* Handle the case that OP0 does not *contain* a conversion
3931 but it *requires* conversion to FINAL_TYPE. */
3933 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3934 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3935 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3936 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3938 /* If one of the operands must be floated, we cannot optimize. */
3939 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3940 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3942 /* If first arg is constant, swap the args (changing operation
3943 so value is preserved), for canonicalization. Don't do this if
3944 the second arg is 0. */
3946 if (TREE_CONSTANT (primop0)
3947 && !integer_zerop (primop1) && !real_zerop (primop1)
3948 && !fixed_zerop (primop1))
3950 std::swap (primop0, primop1);
3951 std::swap (op0, op1);
3954 std::swap (unsignedp0, unsignedp1);
3955 std::swap (real1, real2);
3974 *rescode_ptr = code;
3977 /* If comparing an integer against a constant more bits wide,
3978 maybe we can deduce a value of 1 or 0 independent of the data.
3979 Or else truncate the constant now
3980 rather than extend the variable at run time.
3982 This is only interesting if the constant is the wider arg.
3983 Also, it is not safe if the constant is unsigned and the
3984 variable arg is signed, since in this case the variable
3985 would be sign-extended and then regarded as unsigned.
3986 Our technique fails in this case because the lowest/highest
3987 possible unsigned results don't follow naturally from the
3988 lowest/highest possible values of the variable operand.
3989 For just EQ_EXPR and NE_EXPR there is another technique that
3990 could be used: see if the constant can be faithfully represented
3991 in the other operand's type, by truncating it and reextending it
3992 and see if that preserves the constant's value. */
3994 if (!real1 && !real2
3995 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3996 && TREE_CODE (primop1) == INTEGER_CST
3997 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3999 int min_gt, max_gt, min_lt, max_lt;
4000 tree maxval, minval;
4001 /* 1 if comparison is nominally unsigned. */
4002 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4005 type = c_common_signed_or_unsigned_type (unsignedp0,
4006 TREE_TYPE (primop0));
4008 maxval = TYPE_MAX_VALUE (type);
4009 minval = TYPE_MIN_VALUE (type);
4011 if (unsignedp && !unsignedp0)
4012 *restype_ptr = c_common_signed_type (*restype_ptr);
4014 if (TREE_TYPE (primop1) != *restype_ptr)
4016 /* Convert primop1 to target type, but do not introduce
4017 additional overflow. We know primop1 is an int_cst. */
4018 primop1 = force_fit_type (*restype_ptr,
4021 TYPE_PRECISION (*restype_ptr),
4022 TYPE_SIGN (TREE_TYPE (primop1))),
4023 0, TREE_OVERFLOW (primop1));
4025 if (type != *restype_ptr)
4027 minval = convert (*restype_ptr, minval);
4028 maxval = convert (*restype_ptr, maxval);
4031 min_gt = tree_int_cst_lt (primop1, minval);
4032 max_gt = tree_int_cst_lt (primop1, maxval);
4033 min_lt = tree_int_cst_lt (minval, primop1);
4034 max_lt = tree_int_cst_lt (maxval, primop1);
4037 /* This used to be a switch, but Genix compiler can't handle that. */
4038 if (code == NE_EXPR)
4040 if (max_lt || min_gt)
4041 val = truthvalue_true_node;
4043 else if (code == EQ_EXPR)
4045 if (max_lt || min_gt)
4046 val = truthvalue_false_node;
4048 else if (code == LT_EXPR)
4051 val = truthvalue_true_node;
4053 val = truthvalue_false_node;
4055 else if (code == GT_EXPR)
4058 val = truthvalue_true_node;
4060 val = truthvalue_false_node;
4062 else if (code == LE_EXPR)
4065 val = truthvalue_true_node;
4067 val = truthvalue_false_node;
4069 else if (code == GE_EXPR)
4072 val = truthvalue_true_node;
4074 val = truthvalue_false_node;
4077 /* If primop0 was sign-extended and unsigned comparison specd,
4078 we did a signed comparison above using the signed type bounds.
4079 But the comparison we output must be unsigned.
4081 Also, for inequalities, VAL is no good; but if the signed
4082 comparison had *any* fixed result, it follows that the
4083 unsigned comparison just tests the sign in reverse
4084 (positive values are LE, negative ones GE).
4085 So we can generate an unsigned comparison
4086 against an extreme value of the signed type. */
4088 if (unsignedp && !unsignedp0)
4095 primop1 = TYPE_MIN_VALUE (type);
4101 primop1 = TYPE_MAX_VALUE (type);
4108 type = c_common_unsigned_type (type);
4111 if (TREE_CODE (primop0) != INTEGER_CST
4112 /* Don't warn if it's from a (non-system) macro. */
4113 && !(from_macro_expansion_at
4114 (expansion_point_location_if_in_system_header
4115 (EXPR_LOCATION (primop0)))))
4117 if (val == truthvalue_false_node)
4118 warning_at (loc, OPT_Wtype_limits,
4119 "comparison is always false due to limited range of data type");
4120 if (val == truthvalue_true_node)
4121 warning_at (loc, OPT_Wtype_limits,
4122 "comparison is always true due to limited range of data type");
4127 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4128 if (TREE_SIDE_EFFECTS (primop0))
4129 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4133 /* Value is not predetermined, but do the comparison
4134 in the type of the operand that is not constant.
4135 TYPE is already properly set. */
4138 /* If either arg is decimal float and the other is float, find the
4139 proper common type to use for comparison. */
4140 else if (real1 && real2
4141 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4142 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4143 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4145 /* If either arg is decimal float and the other is float, fail. */
4146 else if (real1 && real2
4147 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4148 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4151 else if (real1 && real2
4152 && (TYPE_PRECISION (TREE_TYPE (primop0))
4153 == TYPE_PRECISION (TREE_TYPE (primop1))))
4154 type = TREE_TYPE (primop0);
4156 /* If args' natural types are both narrower than nominal type
4157 and both extend in the same manner, compare them
4158 in the type of the wider arg.
4159 Otherwise must actually extend both to the nominal
4160 common type lest different ways of extending
4162 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4164 else if (unsignedp0 == unsignedp1 && real1 == real2
4165 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4166 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4168 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4169 type = c_common_signed_or_unsigned_type (unsignedp0
4170 || TYPE_UNSIGNED (*restype_ptr),
4172 /* Make sure shorter operand is extended the right way
4173 to match the longer operand. */
4175 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4176 TREE_TYPE (primop0)),
4179 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4180 TREE_TYPE (primop1)),
4185 /* Here we must do the comparison on the nominal type
4186 using the args exactly as we received them. */
4187 type = *restype_ptr;
4191 if (!real1 && !real2 && integer_zerop (primop1)
4192 && TYPE_UNSIGNED (*restype_ptr))
4195 /* All unsigned values are >= 0, so we warn. However,
4196 if OP0 is a constant that is >= 0, the signedness of
4197 the comparison isn't an issue, so suppress the
4200 warn_type_limits && !in_system_header_at (loc)
4201 && !(TREE_CODE (primop0) == INTEGER_CST
4202 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4204 /* Do not warn for enumeration types. */
4205 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4211 warning_at (loc, OPT_Wtype_limits,
4212 "comparison of unsigned expression >= 0 is always true");
4213 value = truthvalue_true_node;
4218 warning_at (loc, OPT_Wtype_limits,
4219 "comparison of unsigned expression < 0 is always false");
4220 value = truthvalue_false_node;
4229 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4230 if (TREE_SIDE_EFFECTS (primop0))
4231 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4238 *op0_ptr = convert (type, primop0);
4239 *op1_ptr = convert (type, primop1);
4241 *restype_ptr = truthvalue_type_node;
4246 /* Return a tree for the sum or difference (RESULTCODE says which)
4247 of pointer PTROP and integer INTOP. */
4250 pointer_int_sum (location_t loc, enum tree_code resultcode,
4251 tree ptrop, tree intop, bool complain)
4254 HOST_WIDE_INT op1 = 0;
4256 /* The result is a pointer of the same type that is being added. */
4257 tree result_type = TREE_TYPE (ptrop);
4259 if ((flag_sanitize & SANITIZE_UI_OVERFLOW)
4260 && !((current_function_decl && DECL_LANG_FLAG_8 (current_function_decl))
4261 || processing_constexpr_decl))
4263 /* First, try to avoid FPs if INTOP is negative constant. */
4264 if (tree_fits_shwi_p (intop))
4265 op1 = tree_to_shwi (intop);
4268 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4270 if (complain && warn_pointer_arith)
4271 pedwarn (loc, OPT_Wpointer_arith,
4272 "pointer of type %<void *%> used in arithmetic");
4274 return error_mark_node;
4275 size_exp = integer_one_node;
4277 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4279 if (complain && warn_pointer_arith)
4280 pedwarn (loc, OPT_Wpointer_arith,
4281 "pointer to a function used in arithmetic");
4283 return error_mark_node;
4284 size_exp = integer_one_node;
4287 size_exp = size_in_bytes (TREE_TYPE (result_type));
4289 /* We are manipulating pointer values, so we don't need to warn
4290 about relying on undefined signed overflow. We disable the
4291 warning here because we use integer types so fold won't know that
4292 they are really pointers. */
4293 fold_defer_overflow_warnings ();
4295 /* If what we are about to multiply by the size of the elements
4296 contains a constant term, apply distributive law
4297 and multiply that constant term separately.
4298 This helps produce common subexpressions. */
4299 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4300 && !TREE_CONSTANT (intop)
4301 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4302 && TREE_CONSTANT (size_exp)
4303 /* If the constant comes from pointer subtraction,
4304 skip this optimization--it would cause an error. */
4305 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4306 /* If the constant is unsigned, and smaller than the pointer size,
4307 then we must skip this optimization. This is because it could cause
4308 an overflow error if the constant is negative but INTOP is not. */
4309 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4310 || (TYPE_PRECISION (TREE_TYPE (intop))
4311 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4313 enum tree_code subcode = resultcode;
4314 tree int_type = TREE_TYPE (intop);
4315 if (TREE_CODE (intop) == MINUS_EXPR)
4316 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4317 /* Convert both subexpression types to the type of intop,
4318 because weird cases involving pointer arithmetic
4319 can result in a sum or difference with different type args. */
4320 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4322 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4323 intop = convert (int_type, TREE_OPERAND (intop, 0));
4326 /* Convert the integer argument to a type the same size as sizetype
4327 so the multiply won't overflow spuriously. */
4328 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4329 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4330 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4331 TYPE_UNSIGNED (sizetype)), intop);
4333 /* Replace the integer argument with a suitable product by the object size.
4334 Do this multiplication as signed, then convert to the appropriate type
4335 for the pointer operation and disregard an overflow that occurred only
4336 because of the sign-extension change in the latter conversion. */
4338 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4339 convert (TREE_TYPE (intop), size_exp));
4340 intop = convert (sizetype, t);
4341 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4342 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4345 /* Doing this for constexpr will cause an error, even without ISAN
4346 instrumentation: ‘*(const char*)(((long unsigned int)ptr) +
4347 ((long unsigned int)index))’ is not a constant expression. So, just disable
4348 pointer convertion for constexpr. */
4349 if ((flag_sanitize & SANITIZE_UI_OVERFLOW)
4350 && !((current_function_decl && DECL_LANG_FLAG_8 (current_function_decl))
4351 || processing_constexpr_decl))
4353 tree tmp = NULL_TREE;
4354 /* First, try to avoid FPs if INTOP is negative constant. */
4359 resultcode = (resultcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4362 tmp = build_int_cst (sizetype, op1);
4363 intop = build_binary_op (loc, MULT_EXPR, tmp,
4364 convert (TREE_TYPE (intop), size_exp), 1);
4366 tmp = build_binary_op (loc, resultcode,
4367 convert (sizetype, ptrop),
4368 convert (long_integer_type_node, intop), 1);
4369 ret = convert (result_type, tmp);
4373 /* Create the sum or difference. */
4374 if (resultcode == MINUS_EXPR)
4375 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4377 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4380 fold_undefer_and_ignore_overflow_warnings ();
4385 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4386 and if NON_CONST is known not to be permitted in an evaluated part
4387 of a constant expression. */
4390 c_wrap_maybe_const (tree expr, bool non_const)
4392 bool nowarning = TREE_NO_WARNING (expr);
4393 location_t loc = EXPR_LOCATION (expr);
4395 /* This should never be called for C++. */
4396 if (c_dialect_cxx ())
4399 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4400 STRIP_TYPE_NOPS (expr);
4401 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4402 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4404 TREE_NO_WARNING (expr) = 1;
4405 protected_set_expr_location (expr, loc);
4410 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4411 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4412 around the SAVE_EXPR if needed so that c_fully_fold does not need
4413 to look inside SAVE_EXPRs. */
4416 c_save_expr (tree expr)
4418 bool maybe_const = true;
4419 if (c_dialect_cxx ())
4420 return save_expr (expr);
4421 expr = c_fully_fold (expr, false, &maybe_const);
4422 expr = save_expr (expr);
4424 expr = c_wrap_maybe_const (expr, true);
4428 /* Return whether EXPR is a declaration whose address can never be
4432 decl_with_nonnull_addr_p (const_tree expr)
4434 return (DECL_P (expr)
4435 && (TREE_CODE (expr) == PARM_DECL
4436 || TREE_CODE (expr) == LABEL_DECL
4437 || !DECL_WEAK (expr)));
4440 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4441 or for an `if' or `while' statement or ?..: exp. It should already
4442 have been validated to be of suitable type; otherwise, a bad
4443 diagnostic may result.
4445 The EXPR is located at LOCATION.
4447 This preparation consists of taking the ordinary
4448 representation of an expression expr and producing a valid tree
4449 boolean expression describing whether expr is nonzero. We could
4450 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4451 but we optimize comparisons, &&, ||, and !.
4453 The resulting type should always be `truthvalue_type_node'. */
4456 c_common_truthvalue_conversion (location_t location, tree expr)
4458 switch (TREE_CODE (expr))
4460 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4461 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4462 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4463 case ORDERED_EXPR: case UNORDERED_EXPR:
4464 if (TREE_TYPE (expr) == truthvalue_type_node)
4466 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4467 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4470 case TRUTH_ANDIF_EXPR:
4471 case TRUTH_ORIF_EXPR:
4472 case TRUTH_AND_EXPR:
4474 case TRUTH_XOR_EXPR:
4475 if (TREE_TYPE (expr) == truthvalue_type_node)
4477 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4478 c_common_truthvalue_conversion (location,
4479 TREE_OPERAND (expr, 0)),
4480 c_common_truthvalue_conversion (location,
4481 TREE_OPERAND (expr, 1)));
4484 case TRUTH_NOT_EXPR:
4485 if (TREE_TYPE (expr) == truthvalue_type_node)
4487 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4488 c_common_truthvalue_conversion (location,
4489 TREE_OPERAND (expr, 0)));
4496 return integer_zerop (expr) ? truthvalue_false_node
4497 : truthvalue_true_node;
4500 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4501 ? truthvalue_true_node
4502 : truthvalue_false_node;
4505 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4506 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4507 ? truthvalue_true_node
4508 : truthvalue_false_node;
4511 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4516 tree inner = TREE_OPERAND (expr, 0);
4517 if (decl_with_nonnull_addr_p (inner))
4519 /* Common Ada/Pascal programmer's mistake. */
4520 warning_at (location,
4522 "the address of %qD will always evaluate as %<true%>",
4524 return truthvalue_true_node;
4530 expr = build_binary_op (EXPR_LOCATION (expr),
4531 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4532 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4533 c_common_truthvalue_conversion (location,
4534 TREE_OPERAND (expr, 0)),
4535 c_common_truthvalue_conversion (location,
4536 TREE_OPERAND (expr, 1)),
4543 case EXCESS_PRECISION_EXPR:
4544 /* These don't change whether an object is nonzero or zero. */
4545 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4549 /* These don't change whether an object is zero or nonzero, but
4550 we can't ignore them if their second arg has side-effects. */
4551 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4553 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4554 TREE_OPERAND (expr, 1),
4555 c_common_truthvalue_conversion
4556 (location, TREE_OPERAND (expr, 0)));
4560 return c_common_truthvalue_conversion (location,
4561 TREE_OPERAND (expr, 0));
4564 /* Distribute the conversion into the arms of a COND_EXPR. */
4565 if (c_dialect_cxx ())
4567 tree op1 = TREE_OPERAND (expr, 1);
4568 tree op2 = TREE_OPERAND (expr, 2);
4569 /* In C++ one of the arms might have void type if it is throw. */
4570 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4571 op1 = c_common_truthvalue_conversion (location, op1);
4572 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4573 op2 = c_common_truthvalue_conversion (location, op2);
4574 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4575 TREE_OPERAND (expr, 0), op1, op2);
4580 /* Folding will happen later for C. */
4581 expr = build3 (COND_EXPR, truthvalue_type_node,
4582 TREE_OPERAND (expr, 0),
4583 c_common_truthvalue_conversion (location,
4584 TREE_OPERAND (expr, 1)),
4585 c_common_truthvalue_conversion (location,
4586 TREE_OPERAND (expr, 2)));
4592 tree totype = TREE_TYPE (expr);
4593 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4595 if (POINTER_TYPE_P (totype)
4596 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4602 warning_at (location,
4604 "the compiler can assume that the address of "
4605 "%qD will always evaluate to %<true%>",
4609 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4610 since that affects how `default_conversion' will behave. */
4611 if (TREE_CODE (totype) == REFERENCE_TYPE
4612 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4614 /* Don't strip a conversion from C++0x scoped enum, since they
4615 don't implicitly convert to other types. */
4616 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4617 && ENUM_IS_SCOPED (fromtype))
4619 /* If this isn't narrowing the argument, we can ignore it. */
4620 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4621 return c_common_truthvalue_conversion (location,
4622 TREE_OPERAND (expr, 0));
4627 if (!TREE_NO_WARNING (expr)
4628 && warn_parentheses)
4630 warning (OPT_Wparentheses,
4631 "suggest parentheses around assignment used as truth value");
4632 TREE_NO_WARNING (expr) = 1;
4640 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4642 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4643 expr = (build_binary_op
4644 (EXPR_LOCATION (expr),
4645 (TREE_SIDE_EFFECTS (expr)
4646 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4647 c_common_truthvalue_conversion
4649 build_unary_op (location, REALPART_EXPR, t, 0)),
4650 c_common_truthvalue_conversion
4652 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4657 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4659 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4661 (TREE_TYPE (expr))));
4662 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4665 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4668 protected_set_expr_location (expr, location);
4672 static void def_builtin_1 (enum built_in_function fncode,
4674 enum built_in_class fnclass,
4675 tree fntype, tree libtype,
4676 bool both_p, bool fallback_p, bool nonansi_p,
4677 tree fnattrs, bool implicit_p);
4680 /* Apply the TYPE_QUALS to the new DECL. */
4683 c_apply_type_quals_to_decl (int type_quals, tree decl)
4685 tree type = TREE_TYPE (decl);
4687 if (type == error_mark_node)
4690 if ((type_quals & TYPE_QUAL_CONST)
4691 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4692 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4693 constructor can produce constant init, so rely on cp_finish_decl to
4694 clear TREE_READONLY if the variable has non-constant init. */
4695 TREE_READONLY (decl) = 1;
4696 if (type_quals & TYPE_QUAL_VOLATILE)
4698 TREE_SIDE_EFFECTS (decl) = 1;
4699 TREE_THIS_VOLATILE (decl) = 1;
4701 if (type_quals & TYPE_QUAL_RESTRICT)
4703 while (type && TREE_CODE (type) == ARRAY_TYPE)
4704 /* Allow 'restrict' on arrays of pointers.
4705 FIXME currently we just ignore it. */
4706 type = TREE_TYPE (type);
4708 || !POINTER_TYPE_P (type)
4709 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4710 error ("invalid use of %<restrict%>");
4714 struct c_type_hasher : ggc_ptr_hash<tree_node>
4716 static hashval_t hash (tree);
4717 static bool equal (tree, tree);
4720 /* Hash function for the problem of multiple type definitions in
4721 different files. This must hash all types that will compare
4722 equal via comptypes to the same value. In practice it hashes
4723 on some of the simple stuff and leaves the details to comptypes. */
4726 c_type_hasher::hash (tree t)
4731 switch (TREE_CODE (t))
4733 /* For pointers, hash on pointee type plus some swizzling. */
4735 return hash (TREE_TYPE (t)) ^ 0x3003003;
4736 /* Hash on number of elements and total size. */
4739 t2 = TYPE_VALUES (t);
4743 t2 = TYPE_FIELDS (t);
4745 case QUAL_UNION_TYPE:
4747 t2 = TYPE_FIELDS (t);
4751 t2 = TYPE_FIELDS (t);
4756 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4757 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4758 n_elements = list_length (t2);
4759 /* We might have a VLA here. */
4760 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4763 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4764 return ((size << 24) | (n_elements << shift));
4768 c_type_hasher::equal (tree t1, tree t2)
4770 return lang_hooks.types_compatible_p (t1, t2);
4773 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4775 /* Return the typed-based alias set for T, which may be an expression
4776 or a type. Return -1 if we don't do anything special. */
4779 c_common_get_alias_set (tree t)
4783 /* For VLAs, use the alias set of the element type rather than the
4784 default of alias set 0 for types compared structurally. */
4785 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4787 if (TREE_CODE (t) == ARRAY_TYPE)
4788 return get_alias_set (TREE_TYPE (t));
4792 /* Permit type-punning when accessing a union, provided the access
4793 is directly through the union. For example, this code does not
4794 permit taking the address of a union member and then storing
4795 through it. Even the type-punning allowed here is a GCC
4796 extension, albeit a common and useful one; the C standard says
4797 that such accesses have implementation-defined behavior. */
4799 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4800 u = TREE_OPERAND (u, 0))
4801 if (TREE_CODE (u) == COMPONENT_REF
4802 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4805 /* That's all the expressions we handle specially. */
4809 /* The C standard guarantees that any object may be accessed via an
4810 lvalue that has character type. */
4811 if (t == char_type_node
4812 || t == signed_char_type_node
4813 || t == unsigned_char_type_node)
4816 /* The C standard specifically allows aliasing between signed and
4817 unsigned variants of the same type. We treat the signed
4818 variant as canonical. */
4819 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4821 tree t1 = c_common_signed_type (t);
4823 /* t1 == t can happen for boolean nodes which are always unsigned. */
4825 return get_alias_set (t1);
4828 /* Handle the case of multiple type nodes referring to "the same" type,
4829 which occurs with IMA. These share an alias set. FIXME: Currently only
4830 C90 is handled. (In C99 type compatibility is not transitive, which
4831 complicates things mightily. The alias set splay trees can theoretically
4832 represent this, but insertion is tricky when you consider all the
4833 different orders things might arrive in.) */
4835 if (c_language != clk_c || flag_isoc99)
4838 /* Save time if there's only one input file. */
4839 if (num_in_fnames == 1)
4842 /* Pointers need special handling if they point to any type that
4843 needs special handling (below). */
4844 if (TREE_CODE (t) == POINTER_TYPE)
4847 /* Find bottom type under any nested POINTERs. */
4848 for (t2 = TREE_TYPE (t);
4849 TREE_CODE (t2) == POINTER_TYPE;
4850 t2 = TREE_TYPE (t2))
4852 if (!RECORD_OR_UNION_TYPE_P (t2)
4853 && TREE_CODE (t2) != ENUMERAL_TYPE)
4855 if (TYPE_SIZE (t2) == 0)
4858 /* These are the only cases that need special handling. */
4859 if (!RECORD_OR_UNION_TYPE_P (t)
4860 && TREE_CODE (t) != ENUMERAL_TYPE
4861 && TREE_CODE (t) != POINTER_TYPE)
4864 if (TYPE_SIZE (t) == 0)
4867 /* Look up t in hash table. Only one of the compatible types within each
4868 alias set is recorded in the table. */
4869 if (!type_hash_table)
4870 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4871 tree *slot = type_hash_table->find_slot (t, INSERT);
4874 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4875 return TYPE_ALIAS_SET ((tree)*slot);
4878 /* Our caller will assign and record (in t) a new alias set; all we need
4879 to do is remember t in the hash table. */
4885 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4886 the IS_SIZEOF parameter indicates which operator is being applied.
4887 The COMPLAIN flag controls whether we should diagnose possibly
4888 ill-formed constructs or not. LOC is the location of the SIZEOF or
4889 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4890 a type in any context should be returned, rather than the normal
4891 alignment for that type. */
4894 c_sizeof_or_alignof_type (location_t loc,
4895 tree type, bool is_sizeof, bool min_alignof,
4898 const char *op_name;
4900 enum tree_code type_code = TREE_CODE (type);
4902 op_name = is_sizeof ? "sizeof" : "__alignof__";
4904 if (type_code == FUNCTION_TYPE)
4908 if (complain && warn_pointer_arith)
4909 pedwarn (loc, OPT_Wpointer_arith,
4910 "invalid application of %<sizeof%> to a function type");
4912 return error_mark_node;
4913 value = size_one_node;
4919 if (c_dialect_cxx ())
4920 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4921 "%<alignof%> applied to a function type");
4923 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4924 "%<_Alignof%> applied to a function type");
4926 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4929 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4931 if (type_code == VOID_TYPE
4932 && complain && warn_pointer_arith)
4933 pedwarn (loc, OPT_Wpointer_arith,
4934 "invalid application of %qs to a void type", op_name);
4936 return error_mark_node;
4937 value = size_one_node;
4939 else if (!COMPLETE_TYPE_P (type)
4940 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4943 error_at (loc, "invalid application of %qs to incomplete type %qT",
4945 return error_mark_node;
4947 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4948 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4951 error_at (loc, "invalid application of %qs to array type %qT of "
4952 "incomplete element type", op_name, type);
4953 return error_mark_node;
4958 /* Convert in case a char is more than one unit. */
4959 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4960 size_int (TYPE_PRECISION (char_type_node)
4962 else if (min_alignof)
4963 value = size_int (min_align_of_type (type));
4965 value = size_int (TYPE_ALIGN_UNIT (type));
4968 /* VALUE will have the middle-end integer type sizetype.
4969 However, we should really return a value of type `size_t',
4970 which is just a typedef for an ordinary integer type. */
4971 value = fold_convert_loc (loc, size_type_node, value);
4976 /* Implement the __alignof keyword: Return the minimum required
4977 alignment of EXPR, measured in bytes. For VAR_DECLs,
4978 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4979 from an "aligned" __attribute__ specification). LOC is the
4980 location of the ALIGNOF operator. */
4983 c_alignof_expr (location_t loc, tree expr)
4987 if (VAR_OR_FUNCTION_DECL_P (expr))
4988 t = size_int (DECL_ALIGN_UNIT (expr));
4990 else if (TREE_CODE (expr) == COMPONENT_REF
4991 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4993 error_at (loc, "%<__alignof%> applied to a bit-field");
4996 else if (TREE_CODE (expr) == COMPONENT_REF
4997 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4998 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5000 else if (INDIRECT_REF_P (expr))
5002 tree t = TREE_OPERAND (expr, 0);
5004 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5006 while (CONVERT_EXPR_P (t)
5007 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5011 t = TREE_OPERAND (t, 0);
5012 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5013 if (thisalign > bestalign)
5014 best = t, bestalign = thisalign;
5016 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5019 return c_alignof (loc, TREE_TYPE (expr));
5021 return fold_convert_loc (loc, size_type_node, t);
5024 /* Handle C and C++ default attributes. */
5026 enum built_in_attribute
5028 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5029 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5030 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5031 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5032 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5033 #include "builtin-attrs.def"
5034 #undef DEF_ATTR_NULL_TREE
5036 #undef DEF_ATTR_STRING
5037 #undef DEF_ATTR_IDENT
5038 #undef DEF_ATTR_TREE_LIST
5042 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5044 static void c_init_attributes (void);
5048 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5049 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5050 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5051 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5052 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5053 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5054 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5055 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5057 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5059 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5060 ARG6, ARG7, ARG8) NAME,
5061 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5062 ARG6, ARG7, ARG8, ARG9) NAME,
5063 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5064 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5065 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5066 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5067 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5068 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5069 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5070 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5071 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5072 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5074 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5076 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5078 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5079 #include "builtin-types.def"
5080 #undef DEF_PRIMITIVE_TYPE
5081 #undef DEF_FUNCTION_TYPE_0
5082 #undef DEF_FUNCTION_TYPE_1
5083 #undef DEF_FUNCTION_TYPE_2
5084 #undef DEF_FUNCTION_TYPE_3
5085 #undef DEF_FUNCTION_TYPE_4
5086 #undef DEF_FUNCTION_TYPE_5
5087 #undef DEF_FUNCTION_TYPE_6
5088 #undef DEF_FUNCTION_TYPE_7
5089 #undef DEF_FUNCTION_TYPE_8
5090 #undef DEF_FUNCTION_TYPE_9
5091 #undef DEF_FUNCTION_TYPE_10
5092 #undef DEF_FUNCTION_TYPE_11
5093 #undef DEF_FUNCTION_TYPE_VAR_0
5094 #undef DEF_FUNCTION_TYPE_VAR_1
5095 #undef DEF_FUNCTION_TYPE_VAR_2
5096 #undef DEF_FUNCTION_TYPE_VAR_3
5097 #undef DEF_FUNCTION_TYPE_VAR_4
5098 #undef DEF_FUNCTION_TYPE_VAR_5
5099 #undef DEF_FUNCTION_TYPE_VAR_6
5100 #undef DEF_FUNCTION_TYPE_VAR_7
5101 #undef DEF_POINTER_TYPE
5105 typedef enum c_builtin_type builtin_type;
5107 /* A temporary array for c_common_nodes_and_builtins. Used in
5108 communication with def_fn_type. */
5109 static tree builtin_types[(int) BT_LAST + 1];
5111 /* A helper function for c_common_nodes_and_builtins. Build function type
5112 for DEF with return type RET and N arguments. If VAR is true, then the
5113 function should be variadic after those N arguments.
5115 Takes special care not to ICE if any of the types involved are
5116 error_mark_node, which indicates that said type is not in fact available
5117 (see builtin_type_for_size). In which case the function type as a whole
5118 should be error_mark_node. */
5121 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5124 tree *args = XALLOCAVEC (tree, n);
5129 for (i = 0; i < n; ++i)
5131 builtin_type a = (builtin_type) va_arg (list, int);
5132 t = builtin_types[a];
5133 if (t == error_mark_node)
5138 t = builtin_types[ret];
5139 if (t == error_mark_node)
5142 t = build_varargs_function_type_array (t, n, args);
5144 t = build_function_type_array (t, n, args);
5147 builtin_types[def] = t;
5151 /* Build builtin functions common to both C and C++ language
5155 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5157 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5158 builtin_types[ENUM] = VALUE;
5159 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5160 def_fn_type (ENUM, RETURN, 0, 0);
5161 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5162 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5163 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5164 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5165 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5166 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5167 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5168 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5169 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5170 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5171 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5173 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5174 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5176 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5177 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5179 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5181 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5182 ARG6, ARG7, ARG8, ARG9) \
5183 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5185 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5186 ARG6, ARG7, ARG8, ARG9, ARG10) \
5187 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5188 ARG7, ARG8, ARG9, ARG10);
5189 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5190 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5191 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5192 ARG7, ARG8, ARG9, ARG10, ARG11);
5193 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5194 def_fn_type (ENUM, RETURN, 1, 0);
5195 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5196 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5197 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5198 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5199 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5200 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5201 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5202 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5203 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5204 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5205 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5207 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5208 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5210 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5211 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5212 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5214 #include "builtin-types.def"
5216 #undef DEF_PRIMITIVE_TYPE
5217 #undef DEF_FUNCTION_TYPE_0
5218 #undef DEF_FUNCTION_TYPE_1
5219 #undef DEF_FUNCTION_TYPE_2
5220 #undef DEF_FUNCTION_TYPE_3
5221 #undef DEF_FUNCTION_TYPE_4
5222 #undef DEF_FUNCTION_TYPE_5
5223 #undef DEF_FUNCTION_TYPE_6
5224 #undef DEF_FUNCTION_TYPE_7
5225 #undef DEF_FUNCTION_TYPE_8
5226 #undef DEF_FUNCTION_TYPE_9
5227 #undef DEF_FUNCTION_TYPE_10
5228 #undef DEF_FUNCTION_TYPE_11
5229 #undef DEF_FUNCTION_TYPE_VAR_0
5230 #undef DEF_FUNCTION_TYPE_VAR_1
5231 #undef DEF_FUNCTION_TYPE_VAR_2
5232 #undef DEF_FUNCTION_TYPE_VAR_3
5233 #undef DEF_FUNCTION_TYPE_VAR_4
5234 #undef DEF_FUNCTION_TYPE_VAR_5
5235 #undef DEF_FUNCTION_TYPE_VAR_6
5236 #undef DEF_FUNCTION_TYPE_VAR_7
5237 #undef DEF_POINTER_TYPE
5238 builtin_types[(int) BT_LAST] = NULL_TREE;
5240 c_init_attributes ();
5242 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5243 NONANSI_P, ATTRS, IMPLICIT, COND) \
5245 def_builtin_1 (ENUM, NAME, CLASS, \
5246 builtin_types[(int) TYPE], \
5247 builtin_types[(int) LIBTYPE], \
5248 BOTH_P, FALLBACK_P, NONANSI_P, \
5249 built_in_attributes[(int) ATTRS], IMPLICIT);
5250 #include "builtins.def"
5252 targetm.init_builtins ();
5254 build_common_builtin_nodes ();
5257 cilk_init_builtins ();
5260 /* Like get_identifier, but avoid warnings about null arguments when
5261 the argument may be NULL for targets where GCC lacks stdint.h type
5265 c_get_ident (const char *id)
5267 return get_identifier (id);
5270 /* Build tree nodes and builtin functions common to both C and C++ language
5274 c_common_nodes_and_builtins (void)
5276 int char16_type_size;
5277 int char32_type_size;
5278 int wchar_type_size;
5279 tree array_domain_type;
5280 tree va_list_ref_type_node;
5281 tree va_list_arg_type_node;
5284 build_common_tree_nodes (flag_signed_char);
5286 /* Define `int' and `char' first so that dbx will output them first. */
5287 record_builtin_type (RID_INT, NULL, integer_type_node);
5288 record_builtin_type (RID_CHAR, "char", char_type_node);
5290 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5291 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5292 but not C. Are the conditionals here needed? */
5293 if (c_dialect_cxx ())
5294 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5295 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5296 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5297 record_builtin_type (RID_MAX, "long unsigned int",
5298 long_unsigned_type_node);
5300 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5304 sprintf (name, "__int%d", int_n_data[i].bitsize);
5305 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5306 int_n_trees[i].signed_type);
5307 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5308 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5311 if (c_dialect_cxx ())
5312 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5313 record_builtin_type (RID_MAX, "long long int",
5314 long_long_integer_type_node);
5315 record_builtin_type (RID_MAX, "long long unsigned int",
5316 long_long_unsigned_type_node);
5317 if (c_dialect_cxx ())
5318 record_builtin_type (RID_MAX, "long long unsigned",
5319 long_long_unsigned_type_node);
5320 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5321 record_builtin_type (RID_MAX, "short unsigned int",
5322 short_unsigned_type_node);
5323 if (c_dialect_cxx ())
5324 record_builtin_type (RID_MAX, "unsigned short",
5325 short_unsigned_type_node);
5327 /* Define both `signed char' and `unsigned char'. */
5328 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5329 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5331 /* These are types that c_common_type_for_size and
5332 c_common_type_for_mode use. */
5333 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5334 TYPE_DECL, NULL_TREE,
5336 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5337 TYPE_DECL, NULL_TREE,
5339 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5340 TYPE_DECL, NULL_TREE,
5342 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5343 TYPE_DECL, NULL_TREE,
5345 #if HOST_BITS_PER_WIDE_INT >= 64
5346 /* Note that this is different than the __int128 type that's part of
5347 the generic __intN support. */
5348 if (targetm.scalar_mode_supported_p (TImode))
5349 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5351 get_identifier ("__int128_t"),
5354 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5355 TYPE_DECL, NULL_TREE,
5356 unsigned_intQI_type_node));
5357 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5358 TYPE_DECL, NULL_TREE,
5359 unsigned_intHI_type_node));
5360 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5361 TYPE_DECL, NULL_TREE,
5362 unsigned_intSI_type_node));
5363 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5364 TYPE_DECL, NULL_TREE,
5365 unsigned_intDI_type_node));
5366 #if HOST_BITS_PER_WIDE_INT >= 64
5367 if (targetm.scalar_mode_supported_p (TImode))
5368 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5370 get_identifier ("__uint128_t"),
5371 unsigned_intTI_type_node));
5374 /* Create the widest literal types. */
5375 widest_integer_literal_type_node
5376 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5377 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5378 TYPE_DECL, NULL_TREE,
5379 widest_integer_literal_type_node));
5381 widest_unsigned_literal_type_node
5382 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5383 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5384 TYPE_DECL, NULL_TREE,
5385 widest_unsigned_literal_type_node));
5387 signed_size_type_node = c_common_signed_type (size_type_node);
5390 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5392 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5393 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5394 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5396 /* Only supported decimal floating point extension if the target
5397 actually supports underlying modes. */
5398 if (targetm.scalar_mode_supported_p (SDmode)
5399 && targetm.scalar_mode_supported_p (DDmode)
5400 && targetm.scalar_mode_supported_p (TDmode))
5402 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5403 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5404 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5407 if (targetm.fixed_point_supported_p ())
5409 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5410 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5411 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5412 record_builtin_type (RID_MAX, "long long _Fract",
5413 long_long_fract_type_node);
5414 record_builtin_type (RID_MAX, "unsigned short _Fract",
5415 unsigned_short_fract_type_node);
5416 record_builtin_type (RID_MAX, "unsigned _Fract",
5417 unsigned_fract_type_node);
5418 record_builtin_type (RID_MAX, "unsigned long _Fract",
5419 unsigned_long_fract_type_node);
5420 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5421 unsigned_long_long_fract_type_node);
5422 record_builtin_type (RID_MAX, "_Sat short _Fract",
5423 sat_short_fract_type_node);
5424 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5425 record_builtin_type (RID_MAX, "_Sat long _Fract",
5426 sat_long_fract_type_node);
5427 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5428 sat_long_long_fract_type_node);
5429 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5430 sat_unsigned_short_fract_type_node);
5431 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5432 sat_unsigned_fract_type_node);
5433 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5434 sat_unsigned_long_fract_type_node);
5435 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5436 sat_unsigned_long_long_fract_type_node);
5437 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5438 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5439 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5440 record_builtin_type (RID_MAX, "long long _Accum",
5441 long_long_accum_type_node);
5442 record_builtin_type (RID_MAX, "unsigned short _Accum",
5443 unsigned_short_accum_type_node);
5444 record_builtin_type (RID_MAX, "unsigned _Accum",
5445 unsigned_accum_type_node);
5446 record_builtin_type (RID_MAX, "unsigned long _Accum",
5447 unsigned_long_accum_type_node);
5448 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5449 unsigned_long_long_accum_type_node);
5450 record_builtin_type (RID_MAX, "_Sat short _Accum",
5451 sat_short_accum_type_node);
5452 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5453 record_builtin_type (RID_MAX, "_Sat long _Accum",
5454 sat_long_accum_type_node);
5455 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5456 sat_long_long_accum_type_node);
5457 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5458 sat_unsigned_short_accum_type_node);
5459 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5460 sat_unsigned_accum_type_node);
5461 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5462 sat_unsigned_long_accum_type_node);
5463 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5464 sat_unsigned_long_long_accum_type_node);
5468 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5470 get_identifier ("complex int"),
5471 complex_integer_type_node));
5472 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5474 get_identifier ("complex float"),
5475 complex_float_type_node));
5476 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5478 get_identifier ("complex double"),
5479 complex_double_type_node));
5480 lang_hooks.decls.pushdecl
5481 (build_decl (UNKNOWN_LOCATION,
5482 TYPE_DECL, get_identifier ("complex long double"),
5483 complex_long_double_type_node));
5485 if (c_dialect_cxx ())
5486 /* For C++, make fileptr_type_node a distinct void * type until
5487 FILE type is defined. */
5488 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5490 record_builtin_type (RID_VOID, NULL, void_type_node);
5492 /* Set the TYPE_NAME for any variants that were built before
5493 record_builtin_type gave names to the built-in types. */
5495 tree void_name = TYPE_NAME (void_type_node);
5496 TYPE_NAME (void_type_node) = NULL_TREE;
5497 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5499 TYPE_NAME (void_type_node) = void_name;
5502 void_list_node = build_void_list_node ();
5504 /* Make a type to be the domain of a few array types
5505 whose domains don't really matter.
5506 200 is small enough that it always fits in size_t
5507 and large enough that it can hold most function names for the
5508 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5509 array_domain_type = build_index_type (size_int (200));
5511 /* Make a type for arrays of characters.
5512 With luck nothing will ever really depend on the length of this
5514 char_array_type_node
5515 = build_array_type (char_type_node, array_domain_type);
5517 string_type_node = build_pointer_type (char_type_node);
5518 const_string_type_node
5519 = build_pointer_type (build_qualified_type
5520 (char_type_node, TYPE_QUAL_CONST));
5522 /* This is special for C++ so functions can be overloaded. */
5523 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5524 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5525 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5526 underlying_wchar_type_node = wchar_type_node;
5527 if (c_dialect_cxx ())
5529 if (TYPE_UNSIGNED (wchar_type_node))
5530 wchar_type_node = make_unsigned_type (wchar_type_size);
5532 wchar_type_node = make_signed_type (wchar_type_size);
5533 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5536 /* This is for wide string constants. */
5537 wchar_array_type_node
5538 = build_array_type (wchar_type_node, array_domain_type);
5540 /* Define 'char16_t'. */
5541 char16_type_node = get_identifier (CHAR16_TYPE);
5542 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5543 char16_type_size = TYPE_PRECISION (char16_type_node);
5544 if (c_dialect_cxx ())
5546 char16_type_node = make_unsigned_type (char16_type_size);
5548 if (cxx_dialect >= cxx11)
5549 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5552 /* This is for UTF-16 string constants. */
5553 char16_array_type_node
5554 = build_array_type (char16_type_node, array_domain_type);
5556 /* Define 'char32_t'. */
5557 char32_type_node = get_identifier (CHAR32_TYPE);
5558 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5559 char32_type_size = TYPE_PRECISION (char32_type_node);
5560 if (c_dialect_cxx ())
5562 char32_type_node = make_unsigned_type (char32_type_size);
5564 if (cxx_dialect >= cxx11)
5565 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5568 /* This is for UTF-32 string constants. */
5569 char32_array_type_node
5570 = build_array_type (char32_type_node, array_domain_type);
5573 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5576 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5578 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5580 if (SIG_ATOMIC_TYPE)
5581 sig_atomic_type_node =
5582 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5585 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5588 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5591 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5594 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5597 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5599 c_uint16_type_node = uint16_type_node =
5600 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5602 c_uint32_type_node = uint32_type_node =
5603 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5605 c_uint64_type_node = uint64_type_node =
5606 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5607 if (INT_LEAST8_TYPE)
5608 int_least8_type_node =
5609 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5610 if (INT_LEAST16_TYPE)
5611 int_least16_type_node =
5612 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5613 if (INT_LEAST32_TYPE)
5614 int_least32_type_node =
5615 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5616 if (INT_LEAST64_TYPE)
5617 int_least64_type_node =
5618 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5619 if (UINT_LEAST8_TYPE)
5620 uint_least8_type_node =
5621 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5622 if (UINT_LEAST16_TYPE)
5623 uint_least16_type_node =
5624 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5625 if (UINT_LEAST32_TYPE)
5626 uint_least32_type_node =
5627 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5628 if (UINT_LEAST64_TYPE)
5629 uint_least64_type_node =
5630 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5632 int_fast8_type_node =
5633 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5634 if (INT_FAST16_TYPE)
5635 int_fast16_type_node =
5636 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5637 if (INT_FAST32_TYPE)
5638 int_fast32_type_node =
5639 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5640 if (INT_FAST64_TYPE)
5641 int_fast64_type_node =
5642 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5643 if (UINT_FAST8_TYPE)
5644 uint_fast8_type_node =
5645 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5646 if (UINT_FAST16_TYPE)
5647 uint_fast16_type_node =
5648 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5649 if (UINT_FAST32_TYPE)
5650 uint_fast32_type_node =
5651 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5652 if (UINT_FAST64_TYPE)
5653 uint_fast64_type_node =
5654 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5657 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5660 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5662 default_function_type
5663 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5665 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5666 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5668 lang_hooks.decls.pushdecl
5669 (build_decl (UNKNOWN_LOCATION,
5670 TYPE_DECL, get_identifier ("__builtin_va_list"),
5671 va_list_type_node));
5672 if (targetm.enum_va_list_p)
5678 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5680 lang_hooks.decls.pushdecl
5681 (build_decl (UNKNOWN_LOCATION,
5682 TYPE_DECL, get_identifier (pname),
5688 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5690 va_list_arg_type_node = va_list_ref_type_node =
5691 build_pointer_type (TREE_TYPE (va_list_type_node));
5695 va_list_arg_type_node = va_list_type_node;
5696 va_list_ref_type_node = build_reference_type (va_list_type_node);
5699 if (!flag_preprocess_only)
5700 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5702 main_identifier_node = get_identifier ("main");
5704 /* Create the built-in __null node. It is important that this is
5706 null_node = make_int_cst (1, 1);
5707 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5709 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5710 memset (builtin_types, 0, sizeof (builtin_types));
5713 /* The number of named compound-literals generated thus far. */
5714 static GTY(()) int compound_literal_number;
5716 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5719 set_compound_literal_name (tree decl)
5722 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5723 compound_literal_number);
5724 compound_literal_number++;
5725 DECL_NAME (decl) = get_identifier (name);
5728 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5729 TYPE and operand OP. */
5732 build_va_arg_1 (location_t loc, tree type, tree op)
5734 tree expr = build1 (VA_ARG_EXPR, type, op);
5735 SET_EXPR_LOCATION (expr, loc);
5739 /* Return a VA_ARG_EXPR corresponding to a source-level expression
5740 va_arg (EXPR, TYPE) at source location LOC. */
5743 build_va_arg (location_t loc, tree expr, tree type)
5745 tree va_type = TREE_TYPE (expr);
5746 tree canon_va_type = (va_type == error_mark_node
5748 : targetm.canonical_va_list_type (va_type));
5750 if (va_type == error_mark_node
5751 || canon_va_type == NULL_TREE)
5753 /* Let's handle things neutrallly, if expr:
5754 - has undeclared type, or
5755 - is not an va_list type. */
5756 return build_va_arg_1 (loc, type, expr);
5759 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5761 /* Case 1: Not an array type. */
5763 /* Take the address, to get '&ap'. */
5764 mark_addressable (expr);
5765 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5767 /* Verify that &ap is still recognized as having va_list type. */
5768 tree canon_expr_type
5769 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5770 if (canon_expr_type == NULL_TREE)
5773 "first argument to %<va_arg%> not of type %<va_list%>");
5774 return error_mark_node;
5777 return build_va_arg_1 (loc, type, expr);
5780 /* Case 2: Array type.
5784 For contrast, let's start with the simple case (case 1). If
5785 canon_va_type is not an array type, but say a char *, then when
5786 passing-by-value a va_list, the type of the va_list param decl is
5787 the same as for another va_list decl (all ap's are char *):
5790 D.1815 = VA_ARG (&ap, 0B, 1);
5796 __builtin_va_start (&ap, 0);
5799 __builtin_va_end (&ap);
5803 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5804 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5805 the same as for another va_list decl (case 2a, struct ap[1]).
5808 D.1844 = VA_ARG (ap, 0B, 0);
5813 __builtin_va_start (&ap, 0);
5815 __builtin_va_end (&ap);
5819 Case 2b is different because:
5820 - on the callee side, the parm decl has declared type va_list, but
5821 grokdeclarator changes the type of the parm decl to a pointer to the
5823 - on the caller side, the pass-by-value uses &ap.
5825 We unify these two cases (case 2a: va_list is array type,
5826 case 2b: va_list is pointer to array elem type), by adding '&' for the
5827 array type case, such that we have a pointer to array elem in both
5830 if (TREE_CODE (va_type) == ARRAY_TYPE)
5832 /* Case 2a: va_list is array type. */
5834 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5836 mark_addressable (expr);
5837 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5840 /* Verify that &ap is still recognized as having va_list type. */
5841 tree canon_expr_type
5842 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5843 if (canon_expr_type == NULL_TREE)
5846 "first argument to %<va_arg%> not of type %<va_list%>");
5847 return error_mark_node;
5852 /* Case 2b: va_list is pointer to array elem type. */
5853 gcc_assert (POINTER_TYPE_P (va_type));
5854 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
5856 /* Don't take the address. We've already got '&ap'. */
5860 return build_va_arg_1 (loc, type, expr);
5864 /* Linked list of disabled built-in functions. */
5866 struct disabled_builtin
5869 struct disabled_builtin *next;
5871 static disabled_builtin *disabled_builtins = NULL;
5873 static bool builtin_function_disabled_p (const char *);
5875 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5876 begins with "__builtin_", give an error. */
5879 disable_builtin_function (const char *name)
5881 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5882 error ("cannot disable built-in function %qs", name);
5885 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5886 new_disabled_builtin->name = name;
5887 new_disabled_builtin->next = disabled_builtins;
5888 disabled_builtins = new_disabled_builtin;
5893 /* Return true if the built-in function NAME has been disabled, false
5897 builtin_function_disabled_p (const char *name)
5899 disabled_builtin *p;
5900 for (p = disabled_builtins; p != NULL; p = p->next)
5902 if (strcmp (name, p->name) == 0)
5909 /* Worker for DEF_BUILTIN.
5910 Possibly define a builtin function with one or two names.
5911 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5912 nonansi_p and flag_no_nonansi_builtin. */
5915 def_builtin_1 (enum built_in_function fncode,
5917 enum built_in_class fnclass,
5918 tree fntype, tree libtype,
5919 bool both_p, bool fallback_p, bool nonansi_p,
5920 tree fnattrs, bool implicit_p)
5923 const char *libname;
5925 if (fntype == error_mark_node)
5928 gcc_assert ((!both_p && !fallback_p)
5929 || !strncmp (name, "__builtin_",
5930 strlen ("__builtin_")));
5932 libname = name + strlen ("__builtin_");
5933 decl = add_builtin_function (name, fntype, fncode, fnclass,
5934 (fallback_p ? libname : NULL),
5937 set_builtin_decl (fncode, decl, implicit_p);
5940 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5941 && !(nonansi_p && flag_no_nonansi_builtin))
5942 add_builtin_function (libname, libtype, fncode, fnclass,
5946 /* Nonzero if the type T promotes to int. This is (nearly) the
5947 integral promotions defined in ISO C99 6.3.1.1/2. */
5950 c_promoting_integer_type_p (const_tree t)
5952 switch (TREE_CODE (t))
5955 return (TYPE_MAIN_VARIANT (t) == char_type_node
5956 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5957 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5958 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5959 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5960 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5963 /* ??? Technically all enumerations not larger than an int
5964 promote to an int. But this is used along code paths
5965 that only want to notice a size change. */
5966 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5976 /* Return 1 if PARMS specifies a fixed number of parameters
5977 and none of their types is affected by default promotions. */
5980 self_promoting_args_p (const_tree parms)
5983 for (t = parms; t; t = TREE_CHAIN (t))
5985 tree type = TREE_VALUE (t);
5987 if (type == error_mark_node)
5990 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5996 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5999 if (c_promoting_integer_type_p (type))
6005 /* Recursively remove any '*' or '&' operator from TYPE. */
6007 strip_pointer_operator (tree t)
6009 while (POINTER_TYPE_P (t))
6014 /* Recursively remove pointer or array type from TYPE. */
6016 strip_pointer_or_array_types (tree t)
6018 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6023 /* Used to compare case labels. K1 and K2 are actually tree nodes
6024 representing case labels, or NULL_TREE for a `default' label.
6025 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6026 K2, and 0 if K1 and K2 are equal. */
6029 case_compare (splay_tree_key k1, splay_tree_key k2)
6031 /* Consider a NULL key (such as arises with a `default' label) to be
6032 smaller than anything else. */
6038 return tree_int_cst_compare ((tree) k1, (tree) k2);
6041 /* Process a case label, located at LOC, for the range LOW_VALUE
6042 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6043 then this case label is actually a `default' label. If only
6044 HIGH_VALUE is NULL_TREE, then case label was declared using the
6045 usual C/C++ syntax, rather than the GNU case range extension.
6046 CASES is a tree containing all the case ranges processed so far;
6047 COND is the condition for the switch-statement itself.
6048 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6049 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6050 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
6053 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6054 tree low_value, tree high_value, bool *outside_range_p)
6059 splay_tree_node node;
6061 /* Create the LABEL_DECL itself. */
6062 label = create_artificial_label (loc);
6064 /* If there was an error processing the switch condition, bail now
6065 before we get more confused. */
6066 if (!cond || cond == error_mark_node)
6069 if ((low_value && TREE_TYPE (low_value)
6070 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6071 || (high_value && TREE_TYPE (high_value)
6072 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6074 error_at (loc, "pointers are not permitted as case values");
6078 /* Case ranges are a GNU extension. */
6080 pedwarn (loc, OPT_Wpedantic,
6081 "range expressions in switch statements are non-standard");
6083 type = TREE_TYPE (cond);
6086 low_value = check_case_value (loc, low_value);
6087 low_value = convert_and_check (loc, type, low_value);
6088 if (low_value == error_mark_node)
6093 high_value = check_case_value (loc, high_value);
6094 high_value = convert_and_check (loc, type, high_value);
6095 if (high_value == error_mark_node)
6099 if (low_value && high_value)
6101 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6102 really a case range, even though it was written that way.
6103 Remove the HIGH_VALUE to simplify later processing. */
6104 if (tree_int_cst_equal (low_value, high_value))
6105 high_value = NULL_TREE;
6106 else if (!tree_int_cst_lt (low_value, high_value))
6107 warning_at (loc, 0, "empty range specified");
6110 /* See if the case is in range of the type of the original testing
6111 expression. If both low_value and high_value are out of range,
6112 don't insert the case label and return NULL_TREE. */
6114 && !check_case_bounds (loc, type, orig_type,
6115 &low_value, high_value ? &high_value : NULL,
6119 /* Look up the LOW_VALUE in the table of case labels we already
6121 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6122 /* If there was not an exact match, check for overlapping ranges.
6123 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6124 that's a `default' label and the only overlap is an exact match. */
6125 if (!node && (low_value || high_value))
6127 splay_tree_node low_bound;
6128 splay_tree_node high_bound;
6130 /* Even though there wasn't an exact match, there might be an
6131 overlap between this case range and another case range.
6132 Since we've (inductively) not allowed any overlapping case
6133 ranges, we simply need to find the greatest low case label
6134 that is smaller that LOW_VALUE, and the smallest low case
6135 label that is greater than LOW_VALUE. If there is an overlap
6136 it will occur in one of these two ranges. */
6137 low_bound = splay_tree_predecessor (cases,
6138 (splay_tree_key) low_value);
6139 high_bound = splay_tree_successor (cases,
6140 (splay_tree_key) low_value);
6142 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6143 the LOW_VALUE, so there is no need to check unless the
6144 LOW_BOUND is in fact itself a case range. */
6146 && CASE_HIGH ((tree) low_bound->value)
6147 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6150 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6151 range is bigger than the low end of the current range, so we
6152 are only interested if the current range is a real range, and
6153 not an ordinary case label. */
6156 && (tree_int_cst_compare ((tree) high_bound->key,
6161 /* If there was an overlap, issue an error. */
6164 tree duplicate = CASE_LABEL ((tree) node->value);
6168 error_at (loc, "duplicate (or overlapping) case value");
6169 error_at (DECL_SOURCE_LOCATION (duplicate),
6170 "this is the first entry overlapping that value");
6174 error_at (loc, "duplicate case value") ;
6175 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6179 error_at (loc, "multiple default labels in one switch");
6180 error_at (DECL_SOURCE_LOCATION (duplicate),
6181 "this is the first default label");
6186 /* Add a CASE_LABEL to the statement-tree. */
6187 case_label = add_stmt (build_case_label (low_value, high_value, label));
6188 /* Register this case label in the splay tree. */
6189 splay_tree_insert (cases,
6190 (splay_tree_key) low_value,
6191 (splay_tree_value) case_label);
6196 /* Add a label so that the back-end doesn't think that the beginning of
6197 the switch is unreachable. Note that we do not add a case label, as
6198 that just leads to duplicates and thence to failure later on. */
6201 tree t = create_artificial_label (loc);
6202 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6204 return error_mark_node;
6207 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6208 Used to verify that case values match up with enumerator values. */
6211 match_case_to_enum_1 (tree key, tree type, tree label)
6213 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6215 if (tree_fits_uhwi_p (key))
6216 print_dec (key, buf, UNSIGNED);
6217 else if (tree_fits_shwi_p (key))
6218 print_dec (key, buf, SIGNED);
6220 print_hex (key, buf);
6222 if (TYPE_NAME (type) == 0)
6223 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6224 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6225 "case value %qs not in enumerated type",
6228 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6229 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6230 "case value %qs not in enumerated type %qT",
6234 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6235 Used to verify that case values match up with enumerator values. */
6238 match_case_to_enum (splay_tree_node node, void *data)
6240 tree label = (tree) node->value;
6241 tree type = (tree) data;
6243 /* Skip default case. */
6244 if (!CASE_LOW (label))
6247 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6248 when we did our enum->case scan. Reset our scratch bit after. */
6249 if (!CASE_LOW_SEEN (label))
6250 match_case_to_enum_1 (CASE_LOW (label), type, label);
6252 CASE_LOW_SEEN (label) = 0;
6254 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6255 not set, that means that CASE_HIGH did not appear when we did our
6256 enum->case scan. Reset our scratch bit after. */
6257 if (CASE_HIGH (label))
6259 if (!CASE_HIGH_SEEN (label))
6260 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6262 CASE_HIGH_SEEN (label) = 0;
6268 /* Handle -Wswitch*. Called from the front end after parsing the
6269 switch construct. */
6270 /* ??? Should probably be somewhere generic, since other languages
6271 besides C and C++ would want this. At the moment, however, C/C++
6272 are the only tree-ssa languages that support enumerations at all,
6273 so the point is moot. */
6276 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6277 tree type, tree cond, bool bool_cond_p,
6278 bool outside_range_p)
6280 splay_tree_node default_node;
6281 splay_tree_node node;
6284 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6285 && !warn_switch_bool)
6288 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6290 warning_at (switch_location, OPT_Wswitch_default,
6291 "switch missing default case");
6293 /* There are certain cases where -Wswitch-bool warnings aren't
6300 so be careful here. */
6301 if (warn_switch_bool && bool_cond_p)
6303 splay_tree_node min_node;
6304 /* If there's a default node, it's also the value with the minimal
6305 key. So look at the penultimate key (if any). */
6307 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6309 min_node = splay_tree_min (cases);
6310 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6312 splay_tree_node max_node = splay_tree_max (cases);
6313 /* This might be a case range, so look at the value with the
6314 maximal key and then check CASE_HIGH. */
6315 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6317 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6319 /* If there's a case value > 1 or < 0, that is outside bool
6322 || (max && wi::gts_p (max, 1))
6323 || (min && wi::lts_p (min, 0))
6331 case, where we want to warn. */
6333 && max && wi::eq_p (max, 1)
6334 && min && wi::eq_p (min, 0)))
6335 warning_at (switch_location, OPT_Wswitch_bool,
6336 "switch condition has boolean value");
6339 /* From here on, we only care about enumerated types. */
6340 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6343 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6344 if (!warn_switch_enum && !warn_switch)
6347 /* Check the cases. Warn about case values which are not members of
6348 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6349 there is no default case, check that exactly all enumeration
6350 literals are covered by the cases. */
6352 /* Clearing COND if it is not an integer constant simplifies
6353 the tests inside the loop below. */
6354 if (TREE_CODE (cond) != INTEGER_CST)
6357 /* The time complexity here is O(N*lg(N)) worst case, but for the
6358 common case of monotonically increasing enumerators, it is
6359 O(N), since the nature of the splay tree will keep the next
6360 element adjacent to the root at all times. */
6362 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6364 tree value = TREE_VALUE (chain);
6365 if (TREE_CODE (value) == CONST_DECL)
6366 value = DECL_INITIAL (value);
6367 node = splay_tree_lookup (cases, (splay_tree_key) value);
6370 /* Mark the CASE_LOW part of the case entry as seen. */
6371 tree label = (tree) node->value;
6372 CASE_LOW_SEEN (label) = 1;
6376 /* Even though there wasn't an exact match, there might be a
6377 case range which includes the enumerator's value. */
6378 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6379 if (node && CASE_HIGH ((tree) node->value))
6381 tree label = (tree) node->value;
6382 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6385 /* If we match the upper bound exactly, mark the CASE_HIGH
6386 part of the case entry as seen. */
6388 CASE_HIGH_SEEN (label) = 1;
6393 /* We've now determined that this enumerated literal isn't
6394 handled by the case labels of the switch statement. */
6396 /* If the switch expression is a constant, we only really care
6397 about whether that constant is handled by the switch. */
6398 if (cond && tree_int_cst_compare (cond, value))
6401 /* If there is a default_node, the only relevant option is
6402 Wswitch-enum. Otherwise, if both are enabled then we prefer
6403 to warn using -Wswitch because -Wswitch is enabled by -Wall
6404 while -Wswitch-enum is explicit. */
6405 warning_at (switch_location,
6406 (default_node || !warn_switch
6409 "enumeration value %qE not handled in switch",
6410 TREE_PURPOSE (chain));
6413 /* Warn if there are case expressions that don't correspond to
6414 enumerators. This can occur since C and C++ don't enforce
6415 type-checking of assignments to enumeration variables.
6417 The time complexity here is now always O(N) worst case, since
6418 we should have marked both the lower bound and upper bound of
6419 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6420 above. This scan also resets those fields. */
6422 splay_tree_foreach (cases, match_case_to_enum, type);
6425 /* Finish an expression taking the address of LABEL (an
6426 IDENTIFIER_NODE). Returns an expression for the address.
6428 LOC is the location for the expression returned. */
6431 finish_label_address_expr (tree label, location_t loc)
6435 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6437 if (label == error_mark_node)
6438 return error_mark_node;
6440 label = lookup_label (label);
6441 if (label == NULL_TREE)
6442 result = null_pointer_node;
6445 TREE_USED (label) = 1;
6446 result = build1 (ADDR_EXPR, ptr_type_node, label);
6447 /* The current function is not necessarily uninlinable.
6448 Computed gotos are incompatible with inlining, but the value
6449 here could be used only in a diagnostic, for example. */
6450 protected_set_expr_location (result, loc);
6457 /* Given a boolean expression ARG, return a tree representing an increment
6458 or decrement (as indicated by CODE) of ARG. The front end must check for
6459 invalid cases (e.g., decrement in C++). */
6461 boolean_increment (enum tree_code code, tree arg)
6464 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6466 arg = stabilize_reference (arg);
6469 case PREINCREMENT_EXPR:
6470 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6472 case POSTINCREMENT_EXPR:
6473 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6474 arg = save_expr (arg);
6475 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6476 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6478 case PREDECREMENT_EXPR:
6479 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6480 invert_truthvalue_loc (input_location, arg));
6482 case POSTDECREMENT_EXPR:
6483 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6484 invert_truthvalue_loc (input_location, arg));
6485 arg = save_expr (arg);
6486 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6487 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6492 TREE_SIDE_EFFECTS (val) = 1;
6496 /* Built-in macros for stddef.h and stdint.h, that require macros
6497 defined in this file. */
6499 c_stddef_cpp_builtins(void)
6501 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6502 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6503 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6504 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6505 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6506 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6507 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6508 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6509 if (SIG_ATOMIC_TYPE)
6510 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6512 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6514 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6516 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6518 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6520 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6522 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6524 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6526 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6527 if (INT_LEAST8_TYPE)
6528 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6529 if (INT_LEAST16_TYPE)
6530 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6531 if (INT_LEAST32_TYPE)
6532 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6533 if (INT_LEAST64_TYPE)
6534 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6535 if (UINT_LEAST8_TYPE)
6536 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6537 if (UINT_LEAST16_TYPE)
6538 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6539 if (UINT_LEAST32_TYPE)
6540 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6541 if (UINT_LEAST64_TYPE)
6542 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6544 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6545 if (INT_FAST16_TYPE)
6546 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6547 if (INT_FAST32_TYPE)
6548 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6549 if (INT_FAST64_TYPE)
6550 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6551 if (UINT_FAST8_TYPE)
6552 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6553 if (UINT_FAST16_TYPE)
6554 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6555 if (UINT_FAST32_TYPE)
6556 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6557 if (UINT_FAST64_TYPE)
6558 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6560 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6562 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6566 c_init_attributes (void)
6568 /* Fill in the built_in_attributes array. */
6569 #define DEF_ATTR_NULL_TREE(ENUM) \
6570 built_in_attributes[(int) ENUM] = NULL_TREE;
6571 #define DEF_ATTR_INT(ENUM, VALUE) \
6572 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6573 #define DEF_ATTR_STRING(ENUM, VALUE) \
6574 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6575 #define DEF_ATTR_IDENT(ENUM, STRING) \
6576 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6577 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6578 built_in_attributes[(int) ENUM] \
6579 = tree_cons (built_in_attributes[(int) PURPOSE], \
6580 built_in_attributes[(int) VALUE], \
6581 built_in_attributes[(int) CHAIN]);
6582 #include "builtin-attrs.def"
6583 #undef DEF_ATTR_NULL_TREE
6585 #undef DEF_ATTR_IDENT
6586 #undef DEF_ATTR_TREE_LIST
6589 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6590 identifier as an argument, so the front end shouldn't look it up. */
6593 attribute_takes_identifier_p (const_tree attr_id)
6595 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6597 /* Unknown attribute that we'll end up ignoring, return true so we
6598 don't complain about an identifier argument. */
6600 else if (!strcmp ("mode", spec->name)
6601 || !strcmp ("format", spec->name)
6602 || !strcmp ("cleanup", spec->name))
6605 return targetm.attribute_takes_identifier_p (attr_id);
6608 /* Attribute handlers common to C front ends. */
6610 /* Handle a "packed" attribute; arguments as in
6611 struct attribute_spec.handler. */
6614 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6615 int flags, bool *no_add_attrs)
6619 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6620 *node = build_variant_type_copy (*node);
6621 TYPE_PACKED (*node) = 1;
6623 else if (TREE_CODE (*node) == FIELD_DECL)
6625 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6626 /* Still pack bitfields. */
6627 && ! DECL_INITIAL (*node))
6628 warning (OPT_Wattributes,
6629 "%qE attribute ignored for field of type %qT",
6630 name, TREE_TYPE (*node));
6632 DECL_PACKED (*node) = 1;
6634 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6635 used for DECL_REGISTER. It wouldn't mean anything anyway.
6636 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6637 that changes what the typedef is typing. */
6640 warning (OPT_Wattributes, "%qE attribute ignored", name);
6641 *no_add_attrs = true;
6647 /* Handle a "nocommon" attribute; arguments as in
6648 struct attribute_spec.handler. */
6651 handle_nocommon_attribute (tree *node, tree name,
6652 tree ARG_UNUSED (args),
6653 int ARG_UNUSED (flags), bool *no_add_attrs)
6656 DECL_COMMON (*node) = 0;
6659 warning (OPT_Wattributes, "%qE attribute ignored", name);
6660 *no_add_attrs = true;
6666 /* Handle a "common" attribute; arguments as in
6667 struct attribute_spec.handler. */
6670 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6671 int ARG_UNUSED (flags), bool *no_add_attrs)
6674 DECL_COMMON (*node) = 1;
6677 warning (OPT_Wattributes, "%qE attribute ignored", name);
6678 *no_add_attrs = true;
6684 /* Handle a "noreturn" attribute; arguments as in
6685 struct attribute_spec.handler. */
6688 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6689 int ARG_UNUSED (flags), bool *no_add_attrs)
6691 tree type = TREE_TYPE (*node);
6693 /* See FIXME comment in c_common_attribute_table. */
6694 if (TREE_CODE (*node) == FUNCTION_DECL
6695 || objc_method_decl (TREE_CODE (*node)))
6696 TREE_THIS_VOLATILE (*node) = 1;
6697 else if (TREE_CODE (type) == POINTER_TYPE
6698 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6700 = (build_qualified_type
6702 (build_type_variant (TREE_TYPE (type),
6703 TYPE_READONLY (TREE_TYPE (type)), 1)),
6704 TYPE_QUALS (type)));
6707 warning (OPT_Wattributes, "%qE attribute ignored", name);
6708 *no_add_attrs = true;
6714 /* Handle a "hot" and attribute; arguments as in
6715 struct attribute_spec.handler. */
6718 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6719 int ARG_UNUSED (flags), bool *no_add_attrs)
6721 if (TREE_CODE (*node) == FUNCTION_DECL
6722 || TREE_CODE (*node) == LABEL_DECL)
6724 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6726 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6727 "with attribute %qs", name, "cold");
6728 *no_add_attrs = true;
6730 /* Most of the rest of the hot processing is done later with
6731 lookup_attribute. */
6735 warning (OPT_Wattributes, "%qE attribute ignored", name);
6736 *no_add_attrs = true;
6742 /* Handle a "cold" and attribute; arguments as in
6743 struct attribute_spec.handler. */
6746 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6747 int ARG_UNUSED (flags), bool *no_add_attrs)
6749 if (TREE_CODE (*node) == FUNCTION_DECL
6750 || TREE_CODE (*node) == LABEL_DECL)
6752 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6754 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6755 "with attribute %qs", name, "hot");
6756 *no_add_attrs = true;
6758 /* Most of the rest of the cold processing is done later with
6759 lookup_attribute. */
6763 warning (OPT_Wattributes, "%qE attribute ignored", name);
6764 *no_add_attrs = true;
6770 /* Handle a "no_sanitize_address" attribute; arguments as in
6771 struct attribute_spec.handler. */
6774 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6777 if (TREE_CODE (*node) != FUNCTION_DECL)
6779 warning (OPT_Wattributes, "%qE attribute ignored", name);
6780 *no_add_attrs = true;
6786 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6787 struct attribute_spec.handler. */
6790 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6793 if (TREE_CODE (*node) != FUNCTION_DECL)
6794 warning (OPT_Wattributes, "%qE attribute ignored", name);
6795 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6796 DECL_ATTRIBUTES (*node)
6797 = tree_cons (get_identifier ("no_sanitize_address"),
6798 NULL_TREE, DECL_ATTRIBUTES (*node));
6799 *no_add_attrs = true;
6803 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6804 struct attribute_spec.handler. */
6807 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6810 if (TREE_CODE (*node) != FUNCTION_DECL)
6812 warning (OPT_Wattributes, "%qE attribute ignored", name);
6813 *no_add_attrs = true;
6819 /* Handle a "stack_protect" attribute; arguments as in
6820 struct attribute_spec.handler. */
6822 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6825 if (TREE_CODE (*node) != FUNCTION_DECL)
6827 warning (OPT_Wattributes, "%qE attribute ignored", name);
6828 *no_add_attrs = true;
6831 DECL_ATTRIBUTES (*node)
6832 = tree_cons (get_identifier ("stack_protect"),
6833 NULL_TREE, DECL_ATTRIBUTES (*node));
6838 /* Handle a "noinline" attribute; arguments as in
6839 struct attribute_spec.handler. */
6842 handle_noinline_attribute (tree *node, tree name,
6843 tree ARG_UNUSED (args),
6844 int ARG_UNUSED (flags), bool *no_add_attrs)
6846 if (TREE_CODE (*node) == FUNCTION_DECL)
6848 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6850 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6851 "with attribute %qs", name, "always_inline");
6852 *no_add_attrs = true;
6855 DECL_UNINLINABLE (*node) = 1;
6859 warning (OPT_Wattributes, "%qE attribute ignored", name);
6860 *no_add_attrs = true;
6866 /* Handle a "noclone" attribute; arguments as in
6867 struct attribute_spec.handler. */
6870 handle_noclone_attribute (tree *node, tree name,
6871 tree ARG_UNUSED (args),
6872 int ARG_UNUSED (flags), bool *no_add_attrs)
6874 if (TREE_CODE (*node) != FUNCTION_DECL)
6876 warning (OPT_Wattributes, "%qE attribute ignored", name);
6877 *no_add_attrs = true;
6883 /* Handle a "no_icf" attribute; arguments as in
6884 struct attribute_spec.handler. */
6887 handle_noicf_attribute (tree *node, tree name,
6888 tree ARG_UNUSED (args),
6889 int ARG_UNUSED (flags), bool *no_add_attrs)
6891 if (TREE_CODE (*node) != FUNCTION_DECL)
6893 warning (OPT_Wattributes, "%qE attribute ignored", name);
6894 *no_add_attrs = true;
6901 /* Handle a "always_inline" attribute; arguments as in
6902 struct attribute_spec.handler. */
6905 handle_always_inline_attribute (tree *node, tree name,
6906 tree ARG_UNUSED (args),
6907 int ARG_UNUSED (flags),
6910 if (TREE_CODE (*node) == FUNCTION_DECL)
6912 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6914 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6915 "with %qs attribute", name, "noinline");
6916 *no_add_attrs = true;
6918 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
6920 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6921 "with %qs attribute", name, "target_clones");
6922 *no_add_attrs = true;
6925 /* Set the attribute and mark it for disregarding inline
6927 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6931 warning (OPT_Wattributes, "%qE attribute ignored", name);
6932 *no_add_attrs = true;
6938 /* Handle a "gnu_inline" attribute; arguments as in
6939 struct attribute_spec.handler. */
6942 handle_gnu_inline_attribute (tree *node, tree name,
6943 tree ARG_UNUSED (args),
6944 int ARG_UNUSED (flags),
6947 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6949 /* Do nothing else, just set the attribute. We'll get at
6950 it later with lookup_attribute. */
6954 warning (OPT_Wattributes, "%qE attribute ignored", name);
6955 *no_add_attrs = true;
6961 /* Handle a "leaf" attribute; arguments as in
6962 struct attribute_spec.handler. */
6965 handle_leaf_attribute (tree *node, tree name,
6966 tree ARG_UNUSED (args),
6967 int ARG_UNUSED (flags), bool *no_add_attrs)
6969 if (TREE_CODE (*node) != FUNCTION_DECL)
6971 warning (OPT_Wattributes, "%qE attribute ignored", name);
6972 *no_add_attrs = true;
6974 if (!TREE_PUBLIC (*node))
6976 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6977 *no_add_attrs = true;
6983 /* Handle an "artificial" attribute; arguments as in
6984 struct attribute_spec.handler. */
6987 handle_artificial_attribute (tree *node, tree name,
6988 tree ARG_UNUSED (args),
6989 int ARG_UNUSED (flags),
6992 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6994 /* Do nothing else, just set the attribute. We'll get at
6995 it later with lookup_attribute. */
6999 warning (OPT_Wattributes, "%qE attribute ignored", name);
7000 *no_add_attrs = true;
7006 /* Handle a "flatten" attribute; arguments as in
7007 struct attribute_spec.handler. */
7010 handle_flatten_attribute (tree *node, tree name,
7011 tree args ATTRIBUTE_UNUSED,
7012 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7014 if (TREE_CODE (*node) == FUNCTION_DECL)
7015 /* Do nothing else, just set the attribute. We'll get at
7016 it later with lookup_attribute. */
7020 warning (OPT_Wattributes, "%qE attribute ignored", name);
7021 *no_add_attrs = true;
7027 /* Handle a "warning" or "error" attribute; arguments as in
7028 struct attribute_spec.handler. */
7031 handle_error_attribute (tree *node, tree name, tree args,
7032 int ARG_UNUSED (flags), bool *no_add_attrs)
7034 if (TREE_CODE (*node) == FUNCTION_DECL
7035 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7036 /* Do nothing else, just set the attribute. We'll get at
7037 it later with lookup_attribute. */
7041 warning (OPT_Wattributes, "%qE attribute ignored", name);
7042 *no_add_attrs = true;
7048 /* Handle a "used" attribute; arguments as in
7049 struct attribute_spec.handler. */
7052 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7053 int ARG_UNUSED (flags), bool *no_add_attrs)
7057 if (TREE_CODE (node) == FUNCTION_DECL
7058 || (VAR_P (node) && TREE_STATIC (node))
7059 || (TREE_CODE (node) == TYPE_DECL))
7061 TREE_USED (node) = 1;
7062 DECL_PRESERVE_P (node) = 1;
7064 DECL_READ_P (node) = 1;
7068 warning (OPT_Wattributes, "%qE attribute ignored", name);
7069 *no_add_attrs = true;
7075 /* Handle a "unused" attribute; arguments as in
7076 struct attribute_spec.handler. */
7079 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7080 int flags, bool *no_add_attrs)
7086 if (TREE_CODE (decl) == PARM_DECL
7087 || VAR_OR_FUNCTION_DECL_P (decl)
7088 || TREE_CODE (decl) == LABEL_DECL
7089 || TREE_CODE (decl) == TYPE_DECL)
7091 TREE_USED (decl) = 1;
7092 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
7093 DECL_READ_P (decl) = 1;
7097 warning (OPT_Wattributes, "%qE attribute ignored", name);
7098 *no_add_attrs = true;
7103 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7104 *node = build_variant_type_copy (*node);
7105 TREE_USED (*node) = 1;
7111 /* Handle a "externally_visible" attribute; arguments as in
7112 struct attribute_spec.handler. */
7115 handle_externally_visible_attribute (tree *pnode, tree name,
7116 tree ARG_UNUSED (args),
7117 int ARG_UNUSED (flags),
7122 if (VAR_OR_FUNCTION_DECL_P (node))
7124 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7125 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7127 warning (OPT_Wattributes,
7128 "%qE attribute have effect only on public objects", name);
7129 *no_add_attrs = true;
7134 warning (OPT_Wattributes, "%qE attribute ignored", name);
7135 *no_add_attrs = true;
7141 /* Handle the "no_reorder" attribute. Arguments as in
7142 struct attribute_spec.handler. */
7145 handle_no_reorder_attribute (tree *pnode,
7153 if (!VAR_OR_FUNCTION_DECL_P (node)
7154 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7156 warning (OPT_Wattributes,
7157 "%qE attribute only affects top level objects",
7159 *no_add_attrs = true;
7165 /* Handle a "const" attribute; arguments as in
7166 struct attribute_spec.handler. */
7169 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7170 int ARG_UNUSED (flags), bool *no_add_attrs)
7172 tree type = TREE_TYPE (*node);
7174 /* See FIXME comment on noreturn in c_common_attribute_table. */
7175 if (TREE_CODE (*node) == FUNCTION_DECL)
7176 TREE_READONLY (*node) = 1;
7177 else if (TREE_CODE (type) == POINTER_TYPE
7178 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7180 = (build_qualified_type
7182 (build_type_variant (TREE_TYPE (type), 1,
7183 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7184 TYPE_QUALS (type)));
7187 warning (OPT_Wattributes, "%qE attribute ignored", name);
7188 *no_add_attrs = true;
7194 /* Handle a "scalar_storage_order" attribute; arguments as in
7195 struct attribute_spec.handler. */
7198 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7199 int flags, bool *no_add_attrs)
7201 tree id = TREE_VALUE (args);
7204 if (TREE_CODE (*node) == TYPE_DECL
7205 && ! (flags & ATTR_FLAG_CXX11))
7206 node = &TREE_TYPE (*node);
7209 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7211 error ("scalar_storage_order is not supported because endianness "
7216 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7218 bool reverse = false;
7220 if (TREE_CODE (id) == STRING_CST
7221 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7222 reverse = !BYTES_BIG_ENDIAN;
7223 else if (TREE_CODE (id) == STRING_CST
7224 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7225 reverse = BYTES_BIG_ENDIAN;
7228 error ("scalar_storage_order argument must be one of \"big-endian\""
7229 " or \"little-endian\"");
7233 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7236 /* A type variant isn't good enough, since we don't want a cast
7237 to such a type to be removed as a no-op. */
7238 *node = type = build_duplicate_type (type);
7241 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7245 warning (OPT_Wattributes, "%qE attribute ignored", name);
7246 *no_add_attrs = true;
7250 /* Handle a "transparent_union" attribute; arguments as in
7251 struct attribute_spec.handler. */
7254 handle_transparent_union_attribute (tree *node, tree name,
7255 tree ARG_UNUSED (args), int flags,
7260 *no_add_attrs = true;
7262 if (TREE_CODE (*node) == TYPE_DECL
7263 && ! (flags & ATTR_FLAG_CXX11))
7264 node = &TREE_TYPE (*node);
7267 if (TREE_CODE (type) == UNION_TYPE)
7269 /* Make sure that the first field will work for a transparent union.
7270 If the type isn't complete yet, leave the check to the code in
7272 if (TYPE_SIZE (type))
7274 tree first = first_field (type);
7275 if (first == NULL_TREE
7276 || DECL_ARTIFICIAL (first)
7277 || TYPE_MODE (type) != DECL_MODE (first))
7281 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7283 /* If the type isn't complete yet, setting the flag
7284 on a variant wouldn't ever be checked. */
7285 if (!TYPE_SIZE (type))
7288 /* build_duplicate_type doesn't work for C++. */
7289 if (c_dialect_cxx ())
7292 /* A type variant isn't good enough, since we don't want a cast
7293 to such a type to be removed as a no-op. */
7294 *node = type = build_duplicate_type (type);
7297 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7298 TYPE_TRANSPARENT_AGGR (t) = 1;
7303 warning (OPT_Wattributes, "%qE attribute ignored", name);
7307 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7308 get the requested priority for a constructor or destructor,
7309 possibly issuing diagnostics for invalid or reserved
7312 static priority_type
7313 get_priority (tree args, bool is_destructor)
7319 return DEFAULT_INIT_PRIORITY;
7321 if (!SUPPORTS_INIT_PRIORITY)
7324 error ("destructor priorities are not supported");
7326 error ("constructor priorities are not supported");
7327 return DEFAULT_INIT_PRIORITY;
7330 arg = TREE_VALUE (args);
7331 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7333 if (arg == error_mark_node)
7334 return DEFAULT_INIT_PRIORITY;
7335 arg = default_conversion (arg);
7336 if (!tree_fits_shwi_p (arg)
7337 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7340 pri = tree_to_shwi (arg);
7341 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7344 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7348 "destructor priorities from 0 to %d are reserved "
7349 "for the implementation",
7350 MAX_RESERVED_INIT_PRIORITY);
7353 "constructor priorities from 0 to %d are reserved "
7354 "for the implementation",
7355 MAX_RESERVED_INIT_PRIORITY);
7361 error ("destructor priorities must be integers from 0 to %d inclusive",
7364 error ("constructor priorities must be integers from 0 to %d inclusive",
7366 return DEFAULT_INIT_PRIORITY;
7369 /* Handle a "constructor" attribute; arguments as in
7370 struct attribute_spec.handler. */
7373 handle_constructor_attribute (tree *node, tree name, tree args,
7374 int ARG_UNUSED (flags),
7378 tree type = TREE_TYPE (decl);
7380 if (TREE_CODE (decl) == FUNCTION_DECL
7381 && TREE_CODE (type) == FUNCTION_TYPE
7382 && decl_function_context (decl) == 0)
7384 priority_type priority;
7385 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7386 priority = get_priority (args, /*is_destructor=*/false);
7387 SET_DECL_INIT_PRIORITY (decl, priority);
7388 TREE_USED (decl) = 1;
7392 warning (OPT_Wattributes, "%qE attribute ignored", name);
7393 *no_add_attrs = true;
7399 /* Handle a "destructor" attribute; arguments as in
7400 struct attribute_spec.handler. */
7403 handle_destructor_attribute (tree *node, tree name, tree args,
7404 int ARG_UNUSED (flags),
7408 tree type = TREE_TYPE (decl);
7410 if (TREE_CODE (decl) == FUNCTION_DECL
7411 && TREE_CODE (type) == FUNCTION_TYPE
7412 && decl_function_context (decl) == 0)
7414 priority_type priority;
7415 DECL_STATIC_DESTRUCTOR (decl) = 1;
7416 priority = get_priority (args, /*is_destructor=*/true);
7417 SET_DECL_FINI_PRIORITY (decl, priority);
7418 TREE_USED (decl) = 1;
7422 warning (OPT_Wattributes, "%qE attribute ignored", name);
7423 *no_add_attrs = true;
7429 /* Nonzero if the mode is a valid vector mode for this architecture.
7430 This returns nonzero even if there is no hardware support for the
7431 vector mode, but we can emulate with narrower modes. */
7434 vector_mode_valid_p (machine_mode mode)
7436 enum mode_class mclass = GET_MODE_CLASS (mode);
7437 machine_mode innermode;
7439 /* Doh! What's going on? */
7440 if (mclass != MODE_VECTOR_INT
7441 && mclass != MODE_VECTOR_FLOAT
7442 && mclass != MODE_VECTOR_FRACT
7443 && mclass != MODE_VECTOR_UFRACT
7444 && mclass != MODE_VECTOR_ACCUM
7445 && mclass != MODE_VECTOR_UACCUM)
7448 /* Hardware support. Woo hoo! */
7449 if (targetm.vector_mode_supported_p (mode))
7452 innermode = GET_MODE_INNER (mode);
7454 /* We should probably return 1 if requesting V4DI and we have no DI,
7455 but we have V2DI, but this is probably very unlikely. */
7457 /* If we have support for the inner mode, we can safely emulate it.
7458 We may not have V2DI, but me can emulate with a pair of DIs. */
7459 return targetm.scalar_mode_supported_p (innermode);
7463 /* Handle a "mode" attribute; arguments as in
7464 struct attribute_spec.handler. */
7467 handle_mode_attribute (tree *node, tree name, tree args,
7468 int ARG_UNUSED (flags), bool *no_add_attrs)
7471 tree ident = TREE_VALUE (args);
7473 *no_add_attrs = true;
7475 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7476 warning (OPT_Wattributes, "%qE attribute ignored", name);
7480 const char *p = IDENTIFIER_POINTER (ident);
7481 int len = strlen (p);
7482 machine_mode mode = VOIDmode;
7486 if (len > 4 && p[0] == '_' && p[1] == '_'
7487 && p[len - 1] == '_' && p[len - 2] == '_')
7489 char *newp = (char *) alloca (len - 1);
7491 strcpy (newp, &p[2]);
7492 newp[len - 4] = '\0';
7496 /* Change this type to have a type with the specified mode.
7497 First check for the special modes. */
7498 if (!strcmp (p, "byte"))
7500 else if (!strcmp (p, "word"))
7502 else if (!strcmp (p, "pointer"))
7504 else if (!strcmp (p, "libgcc_cmp_return"))
7505 mode = targetm.libgcc_cmp_return_mode ();
7506 else if (!strcmp (p, "libgcc_shift_count"))
7507 mode = targetm.libgcc_shift_count_mode ();
7508 else if (!strcmp (p, "unwind_word"))
7509 mode = targetm.unwind_word_mode ();
7511 for (j = 0; j < NUM_MACHINE_MODES; j++)
7512 if (!strcmp (p, GET_MODE_NAME (j)))
7514 mode = (machine_mode) j;
7518 if (mode == VOIDmode)
7520 error ("unknown machine mode %qE", ident);
7525 switch (GET_MODE_CLASS (mode))
7528 case MODE_PARTIAL_INT:
7530 case MODE_DECIMAL_FLOAT:
7535 valid_mode = targetm.scalar_mode_supported_p (mode);
7538 case MODE_COMPLEX_INT:
7539 case MODE_COMPLEX_FLOAT:
7540 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7543 case MODE_VECTOR_INT:
7544 case MODE_VECTOR_FLOAT:
7545 case MODE_VECTOR_FRACT:
7546 case MODE_VECTOR_UFRACT:
7547 case MODE_VECTOR_ACCUM:
7548 case MODE_VECTOR_UACCUM:
7549 warning (OPT_Wattributes, "specifying vector types with "
7550 "__attribute__ ((mode)) is deprecated");
7551 warning (OPT_Wattributes,
7552 "use __attribute__ ((vector_size)) instead");
7553 valid_mode = vector_mode_valid_p (mode);
7561 error ("unable to emulate %qs", p);
7565 if (POINTER_TYPE_P (type))
7567 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7568 tree (*fn)(tree, machine_mode, bool);
7570 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7572 error ("invalid pointer mode %qs", p);
7576 if (TREE_CODE (type) == POINTER_TYPE)
7577 fn = build_pointer_type_for_mode;
7579 fn = build_reference_type_for_mode;
7580 typefm = fn (TREE_TYPE (type), mode, false);
7584 /* For fixed-point modes, we need to test if the signness of type
7585 and the machine mode are consistent. */
7586 if (ALL_FIXED_POINT_MODE_P (mode)
7587 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7589 error ("signedness of type and machine mode %qs don%'t match", p);
7592 /* For fixed-point modes, we need to pass saturating info. */
7593 typefm = lang_hooks.types.type_for_mode (mode,
7594 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7595 : TYPE_UNSIGNED (type));
7598 if (typefm == NULL_TREE)
7600 error ("no data type for mode %qs", p);
7603 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7605 /* For enumeral types, copy the precision from the integer
7606 type returned above. If not an INTEGER_TYPE, we can't use
7607 this mode for this type. */
7608 if (TREE_CODE (typefm) != INTEGER_TYPE)
7610 error ("cannot use mode %qs for enumeral types", p);
7614 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7616 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7621 /* We cannot build a type variant, as there's code that assumes
7622 that TYPE_MAIN_VARIANT has the same mode. This includes the
7623 debug generators. Instead, create a subrange type. This
7624 results in all of the enumeral values being emitted only once
7625 in the original, and the subtype gets them by reference. */
7626 if (TYPE_UNSIGNED (type))
7627 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7629 typefm = make_signed_type (TYPE_PRECISION (typefm));
7630 TREE_TYPE (typefm) = type;
7633 else if (VECTOR_MODE_P (mode)
7634 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7635 : TREE_CODE (type) != TREE_CODE (typefm))
7637 error ("mode %qs applied to inappropriate type", p);
7647 /* Handle a "section" attribute; arguments as in
7648 struct attribute_spec.handler. */
7651 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7652 int ARG_UNUSED (flags), bool *no_add_attrs)
7656 if (!targetm_common.have_named_sections)
7658 error_at (DECL_SOURCE_LOCATION (*node),
7659 "section attributes are not supported for this target");
7663 user_defined_section_attribute = true;
7665 if (!VAR_OR_FUNCTION_DECL_P (decl))
7667 error ("section attribute not allowed for %q+D", *node);
7671 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7673 error ("section attribute argument not a string constant");
7678 && current_function_decl != NULL_TREE
7679 && !TREE_STATIC (decl))
7681 error_at (DECL_SOURCE_LOCATION (decl),
7682 "section attribute cannot be specified for local variables");
7686 /* The decl may have already been given a section attribute
7687 from a previous declaration. Ensure they match. */
7688 if (DECL_SECTION_NAME (decl) != NULL
7689 && strcmp (DECL_SECTION_NAME (decl),
7690 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7692 error ("section of %q+D conflicts with previous declaration", *node);
7697 && !targetm.have_tls && targetm.emutls.tmpl_section
7698 && DECL_THREAD_LOCAL_P (decl))
7700 error ("section of %q+D cannot be overridden", *node);
7704 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7708 *no_add_attrs = true;
7712 /* Check whether ALIGN is a valid user-specified alignment. If so,
7713 return its base-2 log; if not, output an error and return -1. If
7714 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7717 check_user_alignment (const_tree align, bool allow_zero)
7721 if (error_operand_p (align))
7723 if (TREE_CODE (align) != INTEGER_CST
7724 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7726 error ("requested alignment is not an integer constant");
7729 else if (allow_zero && integer_zerop (align))
7731 else if (tree_int_cst_sgn (align) == -1
7732 || (i = tree_log2 (align)) == -1)
7734 error ("requested alignment is not a positive power of 2");
7737 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7739 error ("requested alignment is too large");
7746 If in c++-11, check if the c++-11 alignment constraint with respect
7747 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7748 c++-11 mode, does nothing.
7752 [* if the constant expression evaluates to a fundamental alignment,
7753 the alignment requirement of the declared entity shall be the
7754 specified fundamental alignment.
7756 * if the constant expression evaluates to an extended alignment
7757 and the implementation supports that alignment in the context
7758 of the declaration, the alignment of the declared entity shall
7761 * if the constant expression evaluates to an extended alignment
7762 and the implementation does not support that alignment in the
7763 context of the declaration, the program is ill-formed]. */
7766 check_cxx_fundamental_alignment_constraints (tree node,
7770 bool alignment_too_large_p = false;
7771 unsigned requested_alignment = 1U << align_log;
7772 unsigned max_align = 0;
7774 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7775 || (node == NULL_TREE || node == error_mark_node))
7778 if (cxx_fundamental_alignment_p (requested_alignment))
7783 if (TREE_STATIC (node))
7785 /* For file scope variables and static members, the target
7786 supports alignments that are at most
7787 MAX_OFILE_ALIGNMENT. */
7788 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7789 alignment_too_large_p = true;
7793 #ifdef BIGGEST_FIELD_ALIGNMENT
7794 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7796 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7798 /* For non-static members, the target supports either
7799 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7800 if it is defined or BIGGEST_ALIGNMENT. */
7801 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7802 if (TREE_CODE (node) == FIELD_DECL
7803 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7804 alignment_too_large_p = true;
7805 #undef MAX_TARGET_FIELD_ALIGNMENT
7806 /* For stack variables, the target supports at most
7807 MAX_STACK_ALIGNMENT. */
7808 else if (decl_function_context (node) != NULL
7809 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7810 alignment_too_large_p = true;
7813 else if (TYPE_P (node))
7815 /* Let's be liberal for types. */
7816 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7817 alignment_too_large_p = true;
7820 if (alignment_too_large_p)
7821 pedwarn (input_location, OPT_Wattributes,
7822 "requested alignment %d is larger than %d",
7823 requested_alignment, max_align);
7825 return !alignment_too_large_p;
7828 /* Handle a "aligned" attribute; arguments as in
7829 struct attribute_spec.handler. */
7832 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7833 int flags, bool *no_add_attrs)
7835 tree decl = NULL_TREE;
7843 align_expr = TREE_VALUE (args);
7844 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7845 && TREE_CODE (align_expr) != FUNCTION_DECL)
7846 align_expr = default_conversion (align_expr);
7849 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7854 type = &TREE_TYPE (decl);
7855 is_type = TREE_CODE (*node) == TYPE_DECL;
7857 else if (TYPE_P (*node))
7858 type = node, is_type = 1;
7860 if ((i = check_user_alignment (align_expr, false)) == -1
7861 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7862 *no_add_attrs = true;
7865 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7866 /* OK, modify the type in place. */;
7867 /* If we have a TYPE_DECL, then copy the type, so that we
7868 don't accidentally modify a builtin type. See pushdecl. */
7869 else if (decl && TREE_TYPE (decl) != error_mark_node
7870 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7872 tree tt = TREE_TYPE (decl);
7873 *type = build_variant_type_copy (*type);
7874 DECL_ORIGINAL_TYPE (decl) = tt;
7875 TYPE_NAME (*type) = decl;
7876 TREE_USED (*type) = TREE_USED (decl);
7877 TREE_TYPE (decl) = *type;
7880 *type = build_variant_type_copy (*type);
7882 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7883 TYPE_USER_ALIGN (*type) = 1;
7885 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7886 && TREE_CODE (decl) != FIELD_DECL)
7888 error ("alignment may not be specified for %q+D", decl);
7889 *no_add_attrs = true;
7891 else if (DECL_USER_ALIGN (decl)
7892 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7893 /* C++-11 [dcl.align/4]:
7895 When multiple alignment-specifiers are specified for an
7896 entity, the alignment requirement shall be set to the
7897 strictest specified alignment.
7899 This formally comes from the c++11 specification but we are
7900 doing it for the GNU attribute syntax as well. */
7901 *no_add_attrs = true;
7902 else if (TREE_CODE (decl) == FUNCTION_DECL
7903 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7905 if (DECL_USER_ALIGN (decl))
7906 error ("alignment for %q+D was previously specified as %d "
7907 "and may not be decreased", decl,
7908 DECL_ALIGN (decl) / BITS_PER_UNIT);
7910 error ("alignment for %q+D must be at least %d", decl,
7911 DECL_ALIGN (decl) / BITS_PER_UNIT);
7912 *no_add_attrs = true;
7916 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7917 DECL_USER_ALIGN (decl) = 1;
7923 /* Handle a "weak" attribute; arguments as in
7924 struct attribute_spec.handler. */
7927 handle_weak_attribute (tree *node, tree name,
7928 tree ARG_UNUSED (args),
7929 int ARG_UNUSED (flags),
7930 bool * ARG_UNUSED (no_add_attrs))
7932 if (TREE_CODE (*node) == FUNCTION_DECL
7933 && DECL_DECLARED_INLINE_P (*node))
7935 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7936 *no_add_attrs = true;
7938 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7940 error ("indirect function %q+D cannot be declared weak", *node);
7941 *no_add_attrs = true;
7944 else if (VAR_OR_FUNCTION_DECL_P (*node))
7945 declare_weak (*node);
7947 warning (OPT_Wattributes, "%qE attribute ignored", name);
7952 /* Handle a "noplt" attribute; arguments as in
7953 struct attribute_spec.handler. */
7956 handle_noplt_attribute (tree *node, tree name,
7957 tree ARG_UNUSED (args),
7958 int ARG_UNUSED (flags),
7959 bool * ARG_UNUSED (no_add_attrs))
7961 if (TREE_CODE (*node) != FUNCTION_DECL)
7963 warning (OPT_Wattributes,
7964 "%qE attribute is only applicable on functions", name);
7965 *no_add_attrs = true;
7971 /* Handle an "alias" or "ifunc" attribute; arguments as in
7972 struct attribute_spec.handler, except that IS_ALIAS tells us
7973 whether this is an alias as opposed to ifunc attribute. */
7976 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7981 if (TREE_CODE (decl) != FUNCTION_DECL
7982 && (!is_alias || !VAR_P (decl)))
7984 warning (OPT_Wattributes, "%qE attribute ignored", name);
7985 *no_add_attrs = true;
7987 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7988 || (TREE_CODE (decl) != FUNCTION_DECL
7989 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7990 /* A static variable declaration is always a tentative definition,
7991 but the alias is a non-tentative definition which overrides. */
7992 || (TREE_CODE (decl) != FUNCTION_DECL
7993 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7995 error ("%q+D defined both normally and as %qE attribute", decl, name);
7996 *no_add_attrs = true;
8000 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8001 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8003 error ("weak %q+D cannot be defined %qE", decl, name);
8004 *no_add_attrs = true;
8008 /* Note that the very first time we process a nested declaration,
8009 decl_function_context will not be set. Indeed, *would* never
8010 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8011 we do below. After such frobbery, pushdecl would set the context.
8012 In any case, this is never what we want. */
8013 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
8017 id = TREE_VALUE (args);
8018 if (TREE_CODE (id) != STRING_CST)
8020 error ("attribute %qE argument not a string", name);
8021 *no_add_attrs = true;
8024 id = get_identifier (TREE_STRING_POINTER (id));
8025 /* This counts as a use of the object pointed to. */
8028 if (TREE_CODE (decl) == FUNCTION_DECL)
8029 DECL_INITIAL (decl) = error_mark_node;
8031 TREE_STATIC (decl) = 1;
8034 /* ifuncs are also aliases, so set that attribute too. */
8035 DECL_ATTRIBUTES (decl)
8036 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8040 warning (OPT_Wattributes, "%qE attribute ignored", name);
8041 *no_add_attrs = true;
8044 if (decl_in_symtab_p (*node))
8046 struct symtab_node *n = symtab_node::get (decl);
8047 if (n && n->refuse_visibility_changes)
8050 error ("%+D declared alias after being used", decl);
8052 error ("%+D declared ifunc after being used", decl);
8060 /* Handle an "alias" or "ifunc" attribute; arguments as in
8061 struct attribute_spec.handler. */
8064 handle_ifunc_attribute (tree *node, tree name, tree args,
8065 int ARG_UNUSED (flags), bool *no_add_attrs)
8067 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8070 /* Handle an "alias" or "ifunc" attribute; arguments as in
8071 struct attribute_spec.handler. */
8074 handle_alias_attribute (tree *node, tree name, tree args,
8075 int ARG_UNUSED (flags), bool *no_add_attrs)
8077 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8080 /* Handle a "weakref" attribute; arguments as in struct
8081 attribute_spec.handler. */
8084 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8085 int flags, bool *no_add_attrs)
8087 tree attr = NULL_TREE;
8089 /* We must ignore the attribute when it is associated with
8090 local-scoped decls, since attribute alias is ignored and many
8091 such symbols do not even have a DECL_WEAK field. */
8092 if (decl_function_context (*node)
8093 || current_function_decl
8094 || !VAR_OR_FUNCTION_DECL_P (*node))
8096 warning (OPT_Wattributes, "%qE attribute ignored", name);
8097 *no_add_attrs = true;
8101 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8103 error ("indirect function %q+D cannot be declared weakref", *node);
8104 *no_add_attrs = true;
8108 /* The idea here is that `weakref("name")' mutates into `weakref,
8109 alias("name")', and weakref without arguments, in turn,
8110 implicitly adds weak. */
8114 attr = tree_cons (get_identifier ("alias"), args, attr);
8115 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8117 *no_add_attrs = true;
8119 decl_attributes (node, attr, flags);
8123 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8124 error_at (DECL_SOURCE_LOCATION (*node),
8125 "weakref attribute must appear before alias attribute");
8127 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8128 and that isn't supported; and because it wants to add it to
8129 the list of weak decls, which isn't helpful. */
8130 DECL_WEAK (*node) = 1;
8133 if (decl_in_symtab_p (*node))
8135 struct symtab_node *n = symtab_node::get (*node);
8136 if (n && n->refuse_visibility_changes)
8137 error ("%+D declared weakref after being used", *node);
8143 /* Handle an "visibility" attribute; arguments as in
8144 struct attribute_spec.handler. */
8147 handle_visibility_attribute (tree *node, tree name, tree args,
8148 int ARG_UNUSED (flags),
8149 bool *ARG_UNUSED (no_add_attrs))
8152 tree id = TREE_VALUE (args);
8153 enum symbol_visibility vis;
8157 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8159 else if (!RECORD_OR_UNION_TYPE_P (*node))
8161 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8165 else if (TYPE_FIELDS (*node))
8167 error ("%qE attribute ignored because %qT is already defined",
8172 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8174 warning (OPT_Wattributes, "%qE attribute ignored", name);
8178 if (TREE_CODE (id) != STRING_CST)
8180 error ("visibility argument not a string");
8184 /* If this is a type, set the visibility on the type decl. */
8187 decl = TYPE_NAME (decl);
8190 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8192 warning (OPT_Wattributes, "%qE attribute ignored on types",
8198 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8199 vis = VISIBILITY_DEFAULT;
8200 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8201 vis = VISIBILITY_INTERNAL;
8202 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8203 vis = VISIBILITY_HIDDEN;
8204 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8205 vis = VISIBILITY_PROTECTED;
8208 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8209 vis = VISIBILITY_DEFAULT;
8212 if (DECL_VISIBILITY_SPECIFIED (decl)
8213 && vis != DECL_VISIBILITY (decl))
8215 tree attributes = (TYPE_P (*node)
8216 ? TYPE_ATTRIBUTES (*node)
8217 : DECL_ATTRIBUTES (decl));
8218 if (lookup_attribute ("visibility", attributes))
8219 error ("%qD redeclared with different visibility", decl);
8220 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8221 && lookup_attribute ("dllimport", attributes))
8222 error ("%qD was declared %qs which implies default visibility",
8224 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8225 && lookup_attribute ("dllexport", attributes))
8226 error ("%qD was declared %qs which implies default visibility",
8230 DECL_VISIBILITY (decl) = vis;
8231 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8233 /* Go ahead and attach the attribute to the node as well. This is needed
8234 so we can determine whether we have VISIBILITY_DEFAULT because the
8235 visibility was not specified, or because it was explicitly overridden
8236 from the containing scope. */
8241 /* Determine the ELF symbol visibility for DECL, which is either a
8242 variable or a function. It is an error to use this function if a
8243 definition of DECL is not available in this translation unit.
8244 Returns true if the final visibility has been determined by this
8245 function; false if the caller is free to make additional
8249 c_determine_visibility (tree decl)
8251 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8253 /* If the user explicitly specified the visibility with an
8254 attribute, honor that. DECL_VISIBILITY will have been set during
8255 the processing of the attribute. We check for an explicit
8256 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8257 to distinguish the use of an attribute from the use of a "#pragma
8258 GCC visibility push(...)"; in the latter case we still want other
8259 considerations to be able to overrule the #pragma. */
8260 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8261 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8262 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8263 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8266 /* Set default visibility to whatever the user supplied with
8267 visibility_specified depending on #pragma GCC visibility. */
8268 if (!DECL_VISIBILITY_SPECIFIED (decl))
8270 if (visibility_options.inpragma
8271 || DECL_VISIBILITY (decl) != default_visibility)
8273 DECL_VISIBILITY (decl) = default_visibility;
8274 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8275 /* If visibility changed and DECL already has DECL_RTL, ensure
8276 symbol flags are updated. */
8277 if (((VAR_P (decl) && TREE_STATIC (decl))
8278 || TREE_CODE (decl) == FUNCTION_DECL)
8279 && DECL_RTL_SET_P (decl))
8280 make_decl_rtl (decl);
8286 /* Handle an "tls_model" attribute; arguments as in
8287 struct attribute_spec.handler. */
8290 handle_tls_model_attribute (tree *node, tree name, tree args,
8291 int ARG_UNUSED (flags), bool *no_add_attrs)
8295 enum tls_model kind;
8297 *no_add_attrs = true;
8299 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
8301 warning (OPT_Wattributes, "%qE attribute ignored", name);
8305 kind = DECL_TLS_MODEL (decl);
8306 id = TREE_VALUE (args);
8307 if (TREE_CODE (id) != STRING_CST)
8309 error ("tls_model argument not a string");
8313 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8314 kind = TLS_MODEL_LOCAL_EXEC;
8315 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8316 kind = TLS_MODEL_INITIAL_EXEC;
8317 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8318 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8319 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8320 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8322 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8324 set_decl_tls_model (decl, kind);
8328 /* Handle a "no_instrument_function" attribute; arguments as in
8329 struct attribute_spec.handler. */
8332 handle_no_instrument_function_attribute (tree *node, tree name,
8333 tree ARG_UNUSED (args),
8334 int ARG_UNUSED (flags),
8339 if (TREE_CODE (decl) != FUNCTION_DECL)
8341 error_at (DECL_SOURCE_LOCATION (decl),
8342 "%qE attribute applies only to functions", name);
8343 *no_add_attrs = true;
8346 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8351 /* Handle a "malloc" attribute; arguments as in
8352 struct attribute_spec.handler. */
8355 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8356 int ARG_UNUSED (flags), bool *no_add_attrs)
8358 if (TREE_CODE (*node) == FUNCTION_DECL
8359 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8360 DECL_IS_MALLOC (*node) = 1;
8363 warning (OPT_Wattributes, "%qE attribute ignored", name);
8364 *no_add_attrs = true;
8370 /* Handle a "alloc_size" attribute; arguments as in
8371 struct attribute_spec.handler. */
8374 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8375 int ARG_UNUSED (flags), bool *no_add_attrs)
8377 unsigned arg_count = type_num_arguments (*node);
8378 for (; args; args = TREE_CHAIN (args))
8380 tree position = TREE_VALUE (args);
8381 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8382 && TREE_CODE (position) != FUNCTION_DECL)
8383 position = default_conversion (position);
8385 if (!tree_fits_uhwi_p (position)
8387 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8389 warning (OPT_Wattributes,
8390 "alloc_size parameter outside range");
8391 *no_add_attrs = true;
8398 /* Handle a "alloc_align" attribute; arguments as in
8399 struct attribute_spec.handler. */
8402 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8405 unsigned arg_count = type_num_arguments (*node);
8406 tree position = TREE_VALUE (args);
8407 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8408 position = default_conversion (position);
8410 if (!tree_fits_uhwi_p (position)
8412 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8414 warning (OPT_Wattributes,
8415 "alloc_align parameter outside range");
8416 *no_add_attrs = true;
8422 /* Handle a "assume_aligned" attribute; arguments as in
8423 struct attribute_spec.handler. */
8426 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8429 for (; args; args = TREE_CHAIN (args))
8431 tree position = TREE_VALUE (args);
8432 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8433 && TREE_CODE (position) != FUNCTION_DECL)
8434 position = default_conversion (position);
8436 if (TREE_CODE (position) != INTEGER_CST)
8438 warning (OPT_Wattributes,
8439 "assume_aligned parameter not integer constant");
8440 *no_add_attrs = true;
8447 /* Handle a "fn spec" attribute; arguments as in
8448 struct attribute_spec.handler. */
8451 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8452 tree args, int ARG_UNUSED (flags),
8453 bool *no_add_attrs ATTRIBUTE_UNUSED)
8456 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8457 && !TREE_CHAIN (args));
8461 /* Handle a "bnd_variable_size" attribute; arguments as in
8462 struct attribute_spec.handler. */
8465 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8466 int ARG_UNUSED (flags), bool *no_add_attrs)
8468 if (TREE_CODE (*node) != FIELD_DECL)
8470 warning (OPT_Wattributes, "%qE attribute ignored", name);
8471 *no_add_attrs = true;
8477 /* Handle a "bnd_legacy" attribute; arguments as in
8478 struct attribute_spec.handler. */
8481 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8482 int ARG_UNUSED (flags), bool *no_add_attrs)
8484 if (TREE_CODE (*node) != FUNCTION_DECL)
8486 warning (OPT_Wattributes, "%qE attribute ignored", name);
8487 *no_add_attrs = true;
8493 /* Handle a "bnd_instrument" attribute; arguments as in
8494 struct attribute_spec.handler. */
8497 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8498 int ARG_UNUSED (flags), bool *no_add_attrs)
8500 if (TREE_CODE (*node) != FUNCTION_DECL)
8502 warning (OPT_Wattributes, "%qE attribute ignored", name);
8503 *no_add_attrs = true;
8509 /* Handle a "warn_unused" attribute; arguments as in
8510 struct attribute_spec.handler. */
8513 handle_warn_unused_attribute (tree *node, tree name,
8514 tree args ATTRIBUTE_UNUSED,
8515 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8518 /* Do nothing else, just set the attribute. We'll get at
8519 it later with lookup_attribute. */
8523 warning (OPT_Wattributes, "%qE attribute ignored", name);
8524 *no_add_attrs = true;
8530 /* Handle an "omp declare simd" attribute; arguments as in
8531 struct attribute_spec.handler. */
8534 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8539 /* Handle a "simd" attribute. */
8542 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
8544 if (TREE_CODE (*node) == FUNCTION_DECL)
8546 if (lookup_attribute ("cilk simd function",
8547 DECL_ATTRIBUTES (*node)) != NULL)
8549 error_at (DECL_SOURCE_LOCATION (*node),
8550 "%<__simd__%> attribute cannot be used in the same "
8551 "function marked as a Cilk Plus SIMD-enabled function");
8552 *no_add_attrs = true;
8556 tree t = get_identifier ("omp declare simd");
8557 tree attr = NULL_TREE;
8560 tree id = TREE_VALUE (args);
8562 if (TREE_CODE (id) != STRING_CST)
8564 error ("attribute %qE argument not a string", name);
8565 *no_add_attrs = true;
8569 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8570 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8571 OMP_CLAUSE_NOTINBRANCH);
8573 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8574 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8575 OMP_CLAUSE_INBRANCH);
8578 error ("only %<inbranch%> and %<notinbranch%> flags are "
8579 "allowed for %<__simd__%> attribute");
8580 *no_add_attrs = true;
8585 DECL_ATTRIBUTES (*node) = tree_cons (t,
8586 build_tree_list (NULL_TREE,
8588 DECL_ATTRIBUTES (*node));
8593 warning (OPT_Wattributes, "%qE attribute ignored", name);
8594 *no_add_attrs = true;
8600 /* Handle an "omp declare target" attribute; arguments as in
8601 struct attribute_spec.handler. */
8604 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8609 /* Handle a "returns_twice" attribute; arguments as in
8610 struct attribute_spec.handler. */
8613 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8614 int ARG_UNUSED (flags), bool *no_add_attrs)
8616 if (TREE_CODE (*node) == FUNCTION_DECL)
8617 DECL_IS_RETURNS_TWICE (*node) = 1;
8620 warning (OPT_Wattributes, "%qE attribute ignored", name);
8621 *no_add_attrs = true;
8627 /* Handle a "no_limit_stack" attribute; arguments as in
8628 struct attribute_spec.handler. */
8631 handle_no_limit_stack_attribute (tree *node, tree name,
8632 tree ARG_UNUSED (args),
8633 int ARG_UNUSED (flags),
8638 if (TREE_CODE (decl) != FUNCTION_DECL)
8640 error_at (DECL_SOURCE_LOCATION (decl),
8641 "%qE attribute applies only to functions", name);
8642 *no_add_attrs = true;
8644 else if (DECL_INITIAL (decl))
8646 error_at (DECL_SOURCE_LOCATION (decl),
8647 "can%'t set %qE attribute after definition", name);
8648 *no_add_attrs = true;
8651 DECL_NO_LIMIT_STACK (decl) = 1;
8656 /* Handle a "pure" attribute; arguments as in
8657 struct attribute_spec.handler. */
8660 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8661 int ARG_UNUSED (flags), bool *no_add_attrs)
8663 if (TREE_CODE (*node) == FUNCTION_DECL)
8664 DECL_PURE_P (*node) = 1;
8665 /* ??? TODO: Support types. */
8668 warning (OPT_Wattributes, "%qE attribute ignored", name);
8669 *no_add_attrs = true;
8675 /* Digest an attribute list destined for a transactional memory statement.
8676 ALLOWED is the set of attributes that are allowed for this statement;
8677 return the attribute we parsed. Multiple attributes are never allowed. */
8680 parse_tm_stmt_attr (tree attrs, int allowed)
8685 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8687 tree a = TREE_PURPOSE (attrs);
8690 if (is_attribute_p ("outer", a))
8691 m = TM_STMT_ATTR_OUTER;
8693 if ((m & allowed) == 0)
8695 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8704 else if (m_seen == m)
8705 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8707 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8713 /* Transform a TM attribute name into a maskable integer and back.
8714 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8715 to how the lack of an attribute is treated. */
8718 tm_attr_to_mask (tree attr)
8722 if (is_attribute_p ("transaction_safe", attr))
8723 return TM_ATTR_SAFE;
8724 if (is_attribute_p ("transaction_callable", attr))
8725 return TM_ATTR_CALLABLE;
8726 if (is_attribute_p ("transaction_pure", attr))
8727 return TM_ATTR_PURE;
8728 if (is_attribute_p ("transaction_unsafe", attr))
8729 return TM_ATTR_IRREVOCABLE;
8730 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8731 return TM_ATTR_MAY_CANCEL_OUTER;
8736 tm_mask_to_attr (int mask)
8742 str = "transaction_safe";
8744 case TM_ATTR_CALLABLE:
8745 str = "transaction_callable";
8748 str = "transaction_pure";
8750 case TM_ATTR_IRREVOCABLE:
8751 str = "transaction_unsafe";
8753 case TM_ATTR_MAY_CANCEL_OUTER:
8754 str = "transaction_may_cancel_outer";
8759 return get_identifier (str);
8762 /* Return the first TM attribute seen in LIST. */
8765 find_tm_attribute (tree list)
8767 for (; list ; list = TREE_CHAIN (list))
8769 tree name = TREE_PURPOSE (list);
8770 if (tm_attr_to_mask (name) != 0)
8776 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8777 Here we accept only function types, and verify that none of the other
8778 function TM attributes are also applied. */
8779 /* ??? We need to accept class types for C++, but not C. This greatly
8780 complicates this function, since we can no longer rely on the extra
8781 processing given by function_type_required. */
8784 handle_tm_attribute (tree *node, tree name, tree args,
8785 int flags, bool *no_add_attrs)
8787 /* Only one path adds the attribute; others don't. */
8788 *no_add_attrs = true;
8790 switch (TREE_CODE (*node))
8794 /* Only tm_callable and tm_safe apply to classes. */
8795 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8802 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8803 if (old_name == name)
8805 else if (old_name != NULL_TREE)
8806 error ("type was previously declared %qE", old_name);
8808 *no_add_attrs = false;
8814 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8815 want to set transaction_safe on the type. */
8816 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8817 if (!TYPE_P (DECL_CONTEXT (*node)))
8818 error_at (DECL_SOURCE_LOCATION (*node),
8819 "%<transaction_safe_dynamic%> may only be specified for "
8820 "a virtual function");
8821 *no_add_attrs = false;
8822 decl_attributes (&TREE_TYPE (*node),
8823 build_tree_list (get_identifier ("transaction_safe"),
8831 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8832 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8834 tree fn_tmp = TREE_TYPE (*node);
8835 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8836 *node = build_pointer_type (fn_tmp);
8843 /* If a function is next, pass it on to be tried next. */
8844 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8845 return tree_cons (name, args, NULL);
8848 warning (OPT_Wattributes, "%qE attribute ignored", name);
8855 /* Handle the TM_WRAP attribute; arguments as in
8856 struct attribute_spec.handler. */
8859 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8860 int ARG_UNUSED (flags), bool *no_add_attrs)
8864 /* We don't need the attribute even on success, since we
8865 record the entry in an external table. */
8866 *no_add_attrs = true;
8868 if (TREE_CODE (decl) != FUNCTION_DECL)
8869 warning (OPT_Wattributes, "%qE attribute ignored", name);
8872 tree wrap_decl = TREE_VALUE (args);
8873 if (error_operand_p (wrap_decl))
8875 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8876 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
8877 error ("%qE argument not an identifier", name);
8880 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8881 wrap_decl = lookup_name (wrap_decl);
8882 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8884 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8885 TREE_TYPE (wrap_decl)))
8886 record_tm_replacement (wrap_decl, decl);
8888 error ("%qD is not compatible with %qD", wrap_decl, decl);
8891 error ("%qE argument is not a function", name);
8898 /* Ignore the given attribute. Used when this attribute may be usefully
8899 overridden by the target, but is not used generically. */
8902 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8903 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8906 *no_add_attrs = true;
8910 /* Handle a "no vops" attribute; arguments as in
8911 struct attribute_spec.handler. */
8914 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8915 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8916 bool *ARG_UNUSED (no_add_attrs))
8918 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8919 DECL_IS_NOVOPS (*node) = 1;
8923 /* Handle a "deprecated" attribute; arguments as in
8924 struct attribute_spec.handler. */
8927 handle_deprecated_attribute (tree *node, tree name,
8928 tree args, int flags,
8931 tree type = NULL_TREE;
8933 tree what = NULL_TREE;
8936 *no_add_attrs = true;
8937 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8939 error ("deprecated message is not a string");
8940 *no_add_attrs = true;
8946 type = TREE_TYPE (decl);
8948 if (TREE_CODE (decl) == TYPE_DECL
8949 || TREE_CODE (decl) == PARM_DECL
8950 || VAR_OR_FUNCTION_DECL_P (decl)
8951 || TREE_CODE (decl) == FIELD_DECL
8952 || TREE_CODE (decl) == CONST_DECL
8953 || objc_method_decl (TREE_CODE (decl)))
8954 TREE_DEPRECATED (decl) = 1;
8958 else if (TYPE_P (*node))
8960 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8961 *node = build_variant_type_copy (*node);
8962 TREE_DEPRECATED (*node) = 1;
8970 *no_add_attrs = true;
8971 if (type && TYPE_NAME (type))
8973 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8974 what = TYPE_NAME (*node);
8975 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8976 && DECL_NAME (TYPE_NAME (type)))
8977 what = DECL_NAME (TYPE_NAME (type));
8980 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8982 warning (OPT_Wattributes, "%qE attribute ignored", name);
8988 /* Handle a "vector_size" attribute; arguments as in
8989 struct attribute_spec.handler. */
8992 handle_vector_size_attribute (tree *node, tree name, tree args,
8993 int ARG_UNUSED (flags),
8996 unsigned HOST_WIDE_INT vecsize, nunits;
8997 machine_mode orig_mode;
8998 tree type = *node, new_type, size;
9000 *no_add_attrs = true;
9002 size = TREE_VALUE (args);
9003 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9004 && TREE_CODE (size) != FUNCTION_DECL)
9005 size = default_conversion (size);
9007 if (!tree_fits_uhwi_p (size))
9009 warning (OPT_Wattributes, "%qE attribute ignored", name);
9013 /* Get the vector size (in bytes). */
9014 vecsize = tree_to_uhwi (size);
9016 /* We need to provide for vector pointers, vector arrays, and
9017 functions returning vectors. For example:
9019 __attribute__((vector_size(16))) short *foo;
9021 In this case, the mode is SI, but the type being modified is
9022 HI, so we need to look further. */
9024 while (POINTER_TYPE_P (type)
9025 || TREE_CODE (type) == FUNCTION_TYPE
9026 || TREE_CODE (type) == METHOD_TYPE
9027 || TREE_CODE (type) == ARRAY_TYPE
9028 || TREE_CODE (type) == OFFSET_TYPE)
9029 type = TREE_TYPE (type);
9031 /* Get the mode of the type being modified. */
9032 orig_mode = TYPE_MODE (type);
9034 if ((!INTEGRAL_TYPE_P (type)
9035 && !SCALAR_FLOAT_TYPE_P (type)
9036 && !FIXED_POINT_TYPE_P (type))
9037 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9038 && GET_MODE_CLASS (orig_mode) != MODE_INT
9039 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
9040 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9041 || TREE_CODE (type) == BOOLEAN_TYPE)
9043 error ("invalid vector type for attribute %qE", name);
9047 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
9049 error ("vector size not an integral multiple of component size");
9055 error ("zero vector size");
9059 /* Calculate how many units fit in the vector. */
9060 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
9061 if (nunits & (nunits - 1))
9063 error ("number of components of the vector not a power of two");
9067 new_type = build_vector_type (type, nunits);
9069 /* Build back pointers if needed. */
9070 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9075 /* Handle the "nonnull" attribute. */
9077 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9078 tree args, int ARG_UNUSED (flags),
9082 unsigned HOST_WIDE_INT attr_arg_num;
9084 /* If no arguments are specified, all pointer arguments should be
9085 non-null. Verify a full prototype is given so that the arguments
9086 will have the correct types when we actually check them later. */
9089 if (!prototype_p (type))
9091 error ("nonnull attribute without arguments on a non-prototype");
9092 *no_add_attrs = true;
9097 /* Argument list specified. Verify that each argument number references
9098 a pointer argument. */
9099 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9101 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9103 tree arg = TREE_VALUE (args);
9104 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9105 && TREE_CODE (arg) != FUNCTION_DECL)
9106 arg = default_conversion (arg);
9108 if (!get_nonnull_operand (arg, &arg_num))
9110 error ("nonnull argument has invalid operand number (argument %lu)",
9111 (unsigned long) attr_arg_num);
9112 *no_add_attrs = true;
9116 if (prototype_p (type))
9118 function_args_iterator iter;
9121 function_args_iter_init (&iter, type);
9122 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9124 argument = function_args_iter_cond (&iter);
9125 if (argument == NULL_TREE || ck_num == arg_num)
9130 || TREE_CODE (argument) == VOID_TYPE)
9132 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9133 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9134 *no_add_attrs = true;
9138 if (TREE_CODE (argument) != POINTER_TYPE)
9140 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9141 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9142 *no_add_attrs = true;
9151 /* Check the argument list of a function call for null in argument slots
9152 that are marked as requiring a non-null pointer argument. The NARGS
9153 arguments are passed in the array ARGARRAY. */
9156 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
9161 attrs = lookup_attribute ("nonnull", attrs);
9162 if (attrs == NULL_TREE)
9166 /* See if any of the nonnull attributes has no arguments. If so,
9167 then every pointer argument is checked (in which case the check
9168 for pointer type is done in check_nonnull_arg). */
9169 if (TREE_VALUE (a) != NULL_TREE)
9171 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9172 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9175 for (i = 0; i < nargs; i++)
9176 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
9180 /* Walk the argument list. If we encounter an argument number we
9181 should check for non-null, do it. */
9182 for (i = 0; i < nargs; i++)
9184 for (a = attrs; ; a = TREE_CHAIN (a))
9186 a = lookup_attribute ("nonnull", a);
9187 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9192 check_function_arguments_recurse (check_nonnull_arg, &loc,
9193 argarray[i], i + 1);
9198 /* Check that the Nth argument of a function call (counting backwards
9199 from the end) is a (pointer)0. The NARGS arguments are passed in the
9203 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9205 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9212 function_args_iterator iter;
9215 /* Skip over the named arguments. */
9216 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9223 if (TREE_VALUE (attr))
9225 tree p = TREE_VALUE (TREE_VALUE (attr));
9226 pos = TREE_INT_CST_LOW (p);
9229 /* The sentinel must be one of the varargs, i.e.
9230 in position >= the number of fixed arguments. */
9231 if ((nargs - 1 - pos) < len)
9233 warning (OPT_Wformat_,
9234 "not enough variable arguments to fit a sentinel");
9238 /* Validate the sentinel. */
9239 sentinel = argarray[nargs - 1 - pos];
9240 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9241 || !integer_zerop (sentinel))
9242 /* Although __null (in C++) is only an integer we allow it
9243 nevertheless, as we are guaranteed that it's exactly
9244 as wide as a pointer, and we don't want to force
9245 users to cast the NULL they have written there.
9246 We warn with -Wstrict-null-sentinel, though. */
9247 && (warn_strict_null_sentinel || null_node != sentinel))
9248 warning (OPT_Wformat_, "missing sentinel in function call");
9252 /* Helper for check_function_nonnull; given a list of operands which
9253 must be non-null in ARGS, determine if operand PARAM_NUM should be
9257 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9259 unsigned HOST_WIDE_INT arg_num = 0;
9261 for (; args; args = TREE_CHAIN (args))
9263 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9267 if (arg_num == param_num)
9273 /* Check that the function argument PARAM (which is operand number
9274 PARAM_NUM) is non-null. This is called by check_function_nonnull
9275 via check_function_arguments_recurse. */
9278 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
9280 location_t *ploc = (location_t *) ctx;
9282 /* Just skip checking the argument if it's not a pointer. This can
9283 happen if the "nonnull" attribute was given without an operand
9284 list (which means to check every pointer argument). */
9286 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9289 if (integer_zerop (param))
9290 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9291 "(argument %lu)", (unsigned long) param_num);
9294 /* Helper for nonnull attribute handling; fetch the operand number
9295 from the attribute argument list. */
9298 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9300 /* Verify the arg number is a small constant. */
9301 if (tree_fits_uhwi_p (arg_num_expr))
9303 *valp = TREE_INT_CST_LOW (arg_num_expr);
9310 /* Handle a "nothrow" attribute; arguments as in
9311 struct attribute_spec.handler. */
9314 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9315 int ARG_UNUSED (flags), bool *no_add_attrs)
9317 if (TREE_CODE (*node) == FUNCTION_DECL)
9318 TREE_NOTHROW (*node) = 1;
9319 /* ??? TODO: Support types. */
9322 warning (OPT_Wattributes, "%qE attribute ignored", name);
9323 *no_add_attrs = true;
9329 /* Handle a "cleanup" attribute; arguments as in
9330 struct attribute_spec.handler. */
9333 handle_cleanup_attribute (tree *node, tree name, tree args,
9334 int ARG_UNUSED (flags), bool *no_add_attrs)
9337 tree cleanup_id, cleanup_decl;
9339 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9340 for global destructors in C++. This requires infrastructure that
9341 we don't have generically at the moment. It's also not a feature
9342 we'd be missing too much, since we do have attribute constructor. */
9343 if (!VAR_P (decl) || TREE_STATIC (decl))
9345 warning (OPT_Wattributes, "%qE attribute ignored", name);
9346 *no_add_attrs = true;
9350 /* Verify that the argument is a function in scope. */
9351 /* ??? We could support pointers to functions here as well, if
9352 that was considered desirable. */
9353 cleanup_id = TREE_VALUE (args);
9354 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9356 error ("cleanup argument not an identifier");
9357 *no_add_attrs = true;
9360 cleanup_decl = lookup_name (cleanup_id);
9361 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9363 error ("cleanup argument not a function");
9364 *no_add_attrs = true;
9368 /* That the function has proper type is checked with the
9369 eventual call to build_function_call. */
9374 /* Handle a "warn_unused_result" attribute. No special handling. */
9377 handle_warn_unused_result_attribute (tree *node, tree name,
9378 tree ARG_UNUSED (args),
9379 int ARG_UNUSED (flags), bool *no_add_attrs)
9381 /* Ignore the attribute for functions not returning any value. */
9382 if (VOID_TYPE_P (TREE_TYPE (*node)))
9384 warning (OPT_Wattributes, "%qE attribute ignored", name);
9385 *no_add_attrs = true;
9391 /* Handle a "sentinel" attribute. */
9394 handle_sentinel_attribute (tree *node, tree name, tree args,
9395 int ARG_UNUSED (flags), bool *no_add_attrs)
9397 if (!prototype_p (*node))
9399 warning (OPT_Wattributes,
9400 "%qE attribute requires prototypes with named arguments", name);
9401 *no_add_attrs = true;
9405 if (!stdarg_p (*node))
9407 warning (OPT_Wattributes,
9408 "%qE attribute only applies to variadic functions", name);
9409 *no_add_attrs = true;
9415 tree position = TREE_VALUE (args);
9416 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9417 && TREE_CODE (position) != FUNCTION_DECL)
9418 position = default_conversion (position);
9420 if (TREE_CODE (position) != INTEGER_CST
9421 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9423 warning (OPT_Wattributes,
9424 "requested position is not an integer constant");
9425 *no_add_attrs = true;
9429 if (tree_int_cst_lt (position, integer_zero_node))
9431 warning (OPT_Wattributes,
9432 "requested position is less than zero");
9433 *no_add_attrs = true;
9441 /* Handle a "type_generic" attribute. */
9444 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9445 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9446 bool * ARG_UNUSED (no_add_attrs))
9448 /* Ensure we have a function type. */
9449 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9451 /* Ensure we have a variadic function. */
9452 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9457 /* Handle a "target" attribute. */
9460 handle_target_attribute (tree *node, tree name, tree args, int flags,
9463 /* Ensure we have a function type. */
9464 if (TREE_CODE (*node) != FUNCTION_DECL)
9466 warning (OPT_Wattributes, "%qE attribute ignored", name);
9467 *no_add_attrs = true;
9469 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9471 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9472 "with %qs attribute", name, "target_clones");
9473 *no_add_attrs = true;
9475 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9477 *no_add_attrs = true;
9482 /* Handle a "target_clones" attribute. */
9485 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9486 int ARG_UNUSED (flags), bool *no_add_attrs)
9488 /* Ensure we have a function type. */
9489 if (TREE_CODE (*node) == FUNCTION_DECL)
9491 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9493 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9494 "with %qs attribute", name, "always_inline");
9495 *no_add_attrs = true;
9497 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9499 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9500 "with %qs attribute", name, "target");
9501 *no_add_attrs = true;
9504 /* Do not inline functions with multiple clone targets. */
9505 DECL_UNINLINABLE (*node) = 1;
9509 warning (OPT_Wattributes, "%qE attribute ignored", name);
9510 *no_add_attrs = true;
9515 /* Arguments being collected for optimization. */
9516 typedef const char *const_char_p; /* For DEF_VEC_P. */
9517 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9520 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9521 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9522 false for #pragma GCC optimize. */
9525 parse_optimize_options (tree args, bool attr_p)
9530 const char **opt_argv;
9531 struct cl_decoded_option *decoded_options;
9532 unsigned int decoded_options_count;
9535 /* Build up argv vector. Just in case the string is stored away, use garbage
9536 collected strings. */
9537 vec_safe_truncate (optimize_args, 0);
9538 vec_safe_push (optimize_args, (const char *) NULL);
9540 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9542 tree value = TREE_VALUE (ap);
9544 if (TREE_CODE (value) == INTEGER_CST)
9547 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9548 vec_safe_push (optimize_args, ggc_strdup (buffer));
9551 else if (TREE_CODE (value) == STRING_CST)
9553 /* Split string into multiple substrings. */
9554 size_t len = TREE_STRING_LENGTH (value);
9555 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9556 char *end = p + len;
9560 while (next_p != NULL)
9566 comma = strchr (p, ',');
9579 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9581 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9583 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9587 warning (OPT_Wattributes,
9588 "bad option %s to optimize attribute", p);
9590 warning (OPT_Wpragmas,
9591 "bad option %s to pragma attribute", p);
9599 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9600 itself is -Os, and any other switch begins with a -f. */
9601 if ((*p >= '0' && *p <= '9')
9602 || (p[0] == 's' && p[1] == '\0'))
9608 memcpy (r, p, len2);
9610 vec_safe_push (optimize_args, (const char *) q);
9616 opt_argc = optimize_args->length ();
9617 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9619 for (i = 1; i < opt_argc; i++)
9620 opt_argv[i] = (*optimize_args)[i];
9622 /* Now parse the options. */
9623 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9625 &decoded_options_count);
9626 decode_options (&global_options, &global_options_set,
9627 decoded_options, decoded_options_count,
9628 input_location, global_dc);
9630 targetm.override_options_after_change();
9632 optimize_args->truncate (0);
9636 /* For handling "optimize" attribute. arguments as in
9637 struct attribute_spec.handler. */
9640 handle_optimize_attribute (tree *node, tree name, tree args,
9641 int ARG_UNUSED (flags), bool *no_add_attrs)
9643 /* Ensure we have a function type. */
9644 if (TREE_CODE (*node) != FUNCTION_DECL)
9646 warning (OPT_Wattributes, "%qE attribute ignored", name);
9647 *no_add_attrs = true;
9651 struct cl_optimization cur_opts;
9652 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9654 /* Save current options. */
9655 cl_optimization_save (&cur_opts, &global_options);
9657 /* If we previously had some optimization options, use them as the
9660 cl_optimization_restore (&global_options,
9661 TREE_OPTIMIZATION (old_opts));
9663 /* Parse options, and update the vector. */
9664 parse_optimize_options (args, true);
9665 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9666 = build_optimization_node (&global_options);
9668 /* Restore current options. */
9669 cl_optimization_restore (&global_options, &cur_opts);
9675 /* Handle a "no_split_stack" attribute. */
9678 handle_no_split_stack_attribute (tree *node, tree name,
9679 tree ARG_UNUSED (args),
9680 int ARG_UNUSED (flags),
9685 if (TREE_CODE (decl) != FUNCTION_DECL)
9687 error_at (DECL_SOURCE_LOCATION (decl),
9688 "%qE attribute applies only to functions", name);
9689 *no_add_attrs = true;
9691 else if (DECL_INITIAL (decl))
9693 error_at (DECL_SOURCE_LOCATION (decl),
9694 "can%'t set %qE attribute after definition", name);
9695 *no_add_attrs = true;
9701 /* Handle a "returns_nonnull" attribute; arguments as in
9702 struct attribute_spec.handler. */
9705 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9708 // Even without a prototype we still have a return type we can check.
9709 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9711 error ("returns_nonnull attribute on a function not returning a pointer");
9712 *no_add_attrs = true;
9717 /* Handle a "designated_init" attribute; arguments as in
9718 struct attribute_spec.handler. */
9721 handle_designated_init_attribute (tree *node, tree name, tree, int,
9724 if (TREE_CODE (*node) != RECORD_TYPE)
9726 error ("%qE attribute is only valid on %<struct%> type", name);
9727 *no_add_attrs = true;
9733 /* Check for valid arguments being passed to a function with FNTYPE.
9734 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9737 check_function_arguments (location_t loc, const_tree fntype, int nargs,
9740 /* Check for null being passed in a pointer argument that must be
9741 non-null. We also need to do this if format checking is enabled. */
9744 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
9746 /* Check for errors in format strings. */
9748 if (warn_format || warn_suggest_attribute_format)
9749 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9752 check_function_sentinel (fntype, nargs, argarray);
9755 /* Generic argument checking recursion routine. PARAM is the argument to
9756 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9757 once the argument is resolved. CTX is context for the callback. */
9759 check_function_arguments_recurse (void (*callback)
9760 (void *, tree, unsigned HOST_WIDE_INT),
9761 void *ctx, tree param,
9762 unsigned HOST_WIDE_INT param_num)
9764 if (CONVERT_EXPR_P (param)
9765 && (TYPE_PRECISION (TREE_TYPE (param))
9766 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9768 /* Strip coercion. */
9769 check_function_arguments_recurse (callback, ctx,
9770 TREE_OPERAND (param, 0), param_num);
9774 if (TREE_CODE (param) == CALL_EXPR)
9777 = CALL_EXPR_FN (param) ? TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)))
9778 : TREE_TYPE (param);
9780 bool found_format_arg = false;
9782 /* See if this is a call to a known internationalization function
9783 that modifies a format arg. Such a function may have multiple
9784 format_arg attributes (for example, ngettext). */
9786 for (attrs = TYPE_ATTRIBUTES (type);
9788 attrs = TREE_CHAIN (attrs))
9789 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9792 tree format_num_expr;
9795 call_expr_arg_iterator iter;
9797 /* Extract the argument number, which was previously checked
9799 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9801 format_num = tree_to_uhwi (format_num_expr);
9803 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9805 inner_arg = next_call_expr_arg (&iter), i++)
9806 if (i == format_num)
9808 check_function_arguments_recurse (callback, ctx,
9809 inner_arg, param_num);
9810 found_format_arg = true;
9815 /* If we found a format_arg attribute and did a recursive check,
9816 we are done with checking this argument. Otherwise, we continue
9817 and this will be considered a non-literal. */
9818 if (found_format_arg)
9822 if (TREE_CODE (param) == COND_EXPR)
9824 /* Simplify to avoid warning for an impossible case. */
9825 param = fold_for_warn (param);
9826 if (TREE_CODE (param) == COND_EXPR)
9828 /* Check both halves of the conditional expression. */
9829 check_function_arguments_recurse (callback, ctx,
9830 TREE_OPERAND (param, 1),
9832 check_function_arguments_recurse (callback, ctx,
9833 TREE_OPERAND (param, 2),
9839 (*callback) (ctx, param, param_num);
9842 /* Checks for a builtin function FNDECL that the number of arguments
9843 NARGS against the required number REQUIRED and issues an error if
9844 there is a mismatch. Returns true if the number of arguments is
9845 correct, otherwise false. */
9848 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9850 if (nargs < required)
9852 error_at (input_location,
9853 "not enough arguments to function %qE", fndecl);
9856 else if (nargs > required)
9858 error_at (input_location,
9859 "too many arguments to function %qE", fndecl);
9865 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9866 Returns false if there was an error, otherwise true. */
9869 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9871 if (!DECL_BUILT_IN (fndecl)
9872 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9875 switch (DECL_FUNCTION_CODE (fndecl))
9877 case BUILT_IN_ALLOCA_WITH_ALIGN:
9879 /* Get the requested alignment (in bits) if it's a constant
9880 integer expression. */
9881 unsigned HOST_WIDE_INT align
9882 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
9884 /* Determine if the requested alignment is a power of 2. */
9885 if ((align & (align - 1)))
9888 /* The maximum alignment in bits corresponding to the same
9889 maximum in bytes enforced in check_user_alignment(). */
9890 unsigned maxalign = (UINT_MAX >> 1) + 1;
9892 /* Reject invalid alignments. */
9893 if (align < BITS_PER_UNIT || maxalign < align)
9895 error_at (EXPR_LOC_OR_LOC (args[1], input_location),
9896 "second argument to function %qE must be a constant "
9897 "integer power of 2 between %qi and %qu bits",
9898 fndecl, BITS_PER_UNIT, maxalign);
9904 case BUILT_IN_CONSTANT_P:
9905 return builtin_function_validate_nargs (fndecl, nargs, 1);
9907 case BUILT_IN_ISFINITE:
9908 case BUILT_IN_ISINF:
9909 case BUILT_IN_ISINF_SIGN:
9910 case BUILT_IN_ISNAN:
9911 case BUILT_IN_ISNORMAL:
9912 case BUILT_IN_SIGNBIT:
9913 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9915 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9917 error ("non-floating-point argument in call to "
9918 "function %qE", fndecl);
9925 case BUILT_IN_ISGREATER:
9926 case BUILT_IN_ISGREATEREQUAL:
9927 case BUILT_IN_ISLESS:
9928 case BUILT_IN_ISLESSEQUAL:
9929 case BUILT_IN_ISLESSGREATER:
9930 case BUILT_IN_ISUNORDERED:
9931 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9933 enum tree_code code0, code1;
9934 code0 = TREE_CODE (TREE_TYPE (args[0]));
9935 code1 = TREE_CODE (TREE_TYPE (args[1]));
9936 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9937 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9938 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9940 error ("non-floating-point arguments in call to "
9941 "function %qE", fndecl);
9948 case BUILT_IN_FPCLASSIFY:
9949 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9954 if (TREE_CODE (args[i]) != INTEGER_CST)
9956 error ("non-const integer argument %u in call to function %qE",
9961 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9963 error ("non-floating-point argument in call to function %qE",
9971 case BUILT_IN_ASSUME_ALIGNED:
9972 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9974 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9976 error ("non-integer argument 3 in call to function %qE", fndecl);
9983 case BUILT_IN_ADD_OVERFLOW:
9984 case BUILT_IN_SUB_OVERFLOW:
9985 case BUILT_IN_MUL_OVERFLOW:
9986 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9989 for (i = 0; i < 2; i++)
9990 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9992 error ("argument %u in call to function %qE does not have "
9993 "integral type", i + 1, fndecl);
9996 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9997 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9999 error ("argument 3 in call to function %qE does not have "
10000 "pointer to integer type", fndecl);
10012 /* Function to help qsort sort FIELD_DECLs by name order. */
10015 field_decl_cmp (const void *x_p, const void *y_p)
10017 const tree *const x = (const tree *const) x_p;
10018 const tree *const y = (const tree *const) y_p;
10020 if (DECL_NAME (*x) == DECL_NAME (*y))
10021 /* A nontype is "greater" than a type. */
10022 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10023 if (DECL_NAME (*x) == NULL_TREE)
10025 if (DECL_NAME (*y) == NULL_TREE)
10027 if (DECL_NAME (*x) < DECL_NAME (*y))
10033 gt_pointer_operator new_value;
10037 /* This routine compares two fields like field_decl_cmp but using the
10038 pointer operator in resort_data. */
10041 resort_field_decl_cmp (const void *x_p, const void *y_p)
10043 const tree *const x = (const tree *const) x_p;
10044 const tree *const y = (const tree *const) y_p;
10046 if (DECL_NAME (*x) == DECL_NAME (*y))
10047 /* A nontype is "greater" than a type. */
10048 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10049 if (DECL_NAME (*x) == NULL_TREE)
10051 if (DECL_NAME (*y) == NULL_TREE)
10054 tree d1 = DECL_NAME (*x);
10055 tree d2 = DECL_NAME (*y);
10056 resort_data.new_value (&d1, resort_data.cookie);
10057 resort_data.new_value (&d2, resort_data.cookie);
10064 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10067 resort_sorted_fields (void *obj,
10068 void * ARG_UNUSED (orig_obj),
10069 gt_pointer_operator new_value,
10072 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10073 resort_data.new_value = new_value;
10074 resort_data.cookie = cookie;
10075 qsort (&sf->elts[0], sf->len, sizeof (tree),
10076 resort_field_decl_cmp);
10079 /* Subroutine of c_parse_error.
10080 Return the result of concatenating LHS and RHS. RHS is really
10081 a string literal, its first character is indicated by RHS_START and
10082 RHS_SIZE is its length (including the terminating NUL character).
10084 The caller is responsible for deleting the returned pointer. */
10087 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10089 const int lhs_size = strlen (lhs);
10090 char *result = XNEWVEC (char, lhs_size + rhs_size);
10091 strncpy (result, lhs, lhs_size);
10092 strncpy (result + lhs_size, rhs_start, rhs_size);
10096 /* Issue the error given by GMSGID, indicating that it occurred before
10097 TOKEN, which had the associated VALUE. */
10100 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
10101 tree value, unsigned char token_flags)
10103 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10105 char *message = NULL;
10107 if (token_type == CPP_EOF)
10108 message = catenate_messages (gmsgid, " at end of input");
10109 else if (token_type == CPP_CHAR
10110 || token_type == CPP_WCHAR
10111 || token_type == CPP_CHAR16
10112 || token_type == CPP_CHAR32
10113 || token_type == CPP_UTF8CHAR)
10115 unsigned int val = TREE_INT_CST_LOW (value);
10116 const char *prefix;
10118 switch (token_type)
10137 if (val <= UCHAR_MAX && ISGRAPH (val))
10138 message = catenate_messages (gmsgid, " before %s'%c'");
10140 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10142 error (message, prefix, val);
10146 else if (token_type == CPP_CHAR_USERDEF
10147 || token_type == CPP_WCHAR_USERDEF
10148 || token_type == CPP_CHAR16_USERDEF
10149 || token_type == CPP_CHAR32_USERDEF
10150 || token_type == CPP_UTF8CHAR_USERDEF)
10151 message = catenate_messages (gmsgid,
10152 " before user-defined character literal");
10153 else if (token_type == CPP_STRING_USERDEF
10154 || token_type == CPP_WSTRING_USERDEF
10155 || token_type == CPP_STRING16_USERDEF
10156 || token_type == CPP_STRING32_USERDEF
10157 || token_type == CPP_UTF8STRING_USERDEF)
10158 message = catenate_messages (gmsgid, " before user-defined string literal");
10159 else if (token_type == CPP_STRING
10160 || token_type == CPP_WSTRING
10161 || token_type == CPP_STRING16
10162 || token_type == CPP_STRING32
10163 || token_type == CPP_UTF8STRING)
10164 message = catenate_messages (gmsgid, " before string constant");
10165 else if (token_type == CPP_NUMBER)
10166 message = catenate_messages (gmsgid, " before numeric constant");
10167 else if (token_type == CPP_NAME)
10169 message = catenate_messages (gmsgid, " before %qE");
10170 error (message, value);
10174 else if (token_type == CPP_PRAGMA)
10175 message = catenate_messages (gmsgid, " before %<#pragma%>");
10176 else if (token_type == CPP_PRAGMA_EOL)
10177 message = catenate_messages (gmsgid, " before end of line");
10178 else if (token_type == CPP_DECLTYPE)
10179 message = catenate_messages (gmsgid, " before %<decltype%>");
10180 else if (token_type < N_TTYPES)
10182 message = catenate_messages (gmsgid, " before %qs token");
10183 error (message, cpp_type2name (token_type, token_flags));
10195 #undef catenate_messages
10198 /* Return the gcc option code associated with the reason for a cpp
10199 message, or 0 if none. */
10202 c_option_controlling_cpp_error (int reason)
10204 const struct cpp_reason_option_codes_t *entry;
10206 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10208 if (entry->reason == reason)
10209 return entry->option_code;
10214 /* Callback from cpp_error for PFILE to print diagnostics from the
10215 preprocessor. The diagnostic is of type LEVEL, with REASON set
10216 to the reason code if LEVEL is represents a warning, at location
10217 RICHLOC unless this is after lexing and the compiler's location
10218 should be used instead; MSG is the translated message and AP
10219 the arguments. Returns true if a diagnostic was emitted, false
10223 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10224 rich_location *richloc,
10225 const char *msg, va_list *ap)
10227 diagnostic_info diagnostic;
10228 diagnostic_t dlevel;
10229 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10234 case CPP_DL_WARNING_SYSHDR:
10235 if (flag_no_output)
10237 global_dc->dc_warn_system_headers = 1;
10238 /* Fall through. */
10239 case CPP_DL_WARNING:
10240 if (flag_no_output)
10242 dlevel = DK_WARNING;
10244 case CPP_DL_PEDWARN:
10245 if (flag_no_output && !flag_pedantic_errors)
10247 dlevel = DK_PEDWARN;
10262 gcc_unreachable ();
10265 richloc->set_range (line_table, 0, input_location, true);
10266 diagnostic_set_info_translated (&diagnostic, msg, ap,
10268 diagnostic_override_option_index (&diagnostic,
10269 c_option_controlling_cpp_error (reason));
10270 ret = report_diagnostic (&diagnostic);
10271 if (level == CPP_DL_WARNING_SYSHDR)
10272 global_dc->dc_warn_system_headers = save_warn_system_headers;
10276 /* Convert a character from the host to the target execution character
10277 set. cpplib handles this, mostly. */
10280 c_common_to_target_charset (HOST_WIDE_INT c)
10282 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10283 zero-extended under -fno-signed-char. cpplib insists that characters
10284 and character constants are always unsigned. Hence we must convert
10286 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10288 uc = cpp_host_to_exec_charset (parse_in, uc);
10290 if (flag_signed_char)
10291 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10292 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10297 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10298 references with an INDIRECT_REF of a constant at the bottom; much like the
10299 traditional rendering of offsetof as a macro. Return the folded result. */
10302 fold_offsetof_1 (tree expr, enum tree_code ctx)
10305 tree_code code = TREE_CODE (expr);
10312 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10313 return error_mark_node;
10317 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10318 return error_mark_node;
10322 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10324 error ("cannot apply %<offsetof%> to a non constant address");
10325 return error_mark_node;
10327 return TREE_OPERAND (expr, 0);
10329 case COMPONENT_REF:
10330 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10331 if (base == error_mark_node)
10334 t = TREE_OPERAND (expr, 1);
10335 if (DECL_C_BIT_FIELD (t))
10337 error ("attempt to take address of bit-field structure "
10339 return error_mark_node;
10341 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10342 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10347 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10348 if (base == error_mark_node)
10351 t = TREE_OPERAND (expr, 1);
10353 /* Check if the offset goes beyond the upper bound of the array. */
10354 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10356 tree upbound = array_ref_up_bound (expr);
10357 if (upbound != NULL_TREE
10358 && TREE_CODE (upbound) == INTEGER_CST
10359 && !tree_int_cst_equal (upbound,
10360 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10362 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10363 upbound = size_binop (PLUS_EXPR, upbound,
10364 build_int_cst (TREE_TYPE (upbound), 1));
10365 if (tree_int_cst_lt (upbound, t))
10369 for (v = TREE_OPERAND (expr, 0);
10370 TREE_CODE (v) == COMPONENT_REF;
10371 v = TREE_OPERAND (v, 0))
10372 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10375 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10376 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10377 if (TREE_CODE (fld_chain) == FIELD_DECL)
10383 /* Don't warn if the array might be considered a poor
10384 man's flexible array member with a very permissive
10385 definition thereof. */
10386 if (TREE_CODE (v) == ARRAY_REF
10387 || TREE_CODE (v) == COMPONENT_REF)
10388 warning (OPT_Warray_bounds,
10389 "index %E denotes an offset "
10390 "greater than size of %qT",
10391 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10396 t = convert (sizetype, t);
10397 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10400 case COMPOUND_EXPR:
10401 /* Handle static members of volatile structs. */
10402 t = TREE_OPERAND (expr, 1);
10403 gcc_assert (VAR_P (t));
10404 return fold_offsetof_1 (t);
10407 gcc_unreachable ();
10410 return fold_build_pointer_plus (base, off);
10413 /* Likewise, but convert it to the return type of offsetof. */
10416 fold_offsetof (tree expr)
10418 return convert (size_type_node, fold_offsetof_1 (expr));
10421 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10422 expression, because B will always be true. */
10425 warn_for_omitted_condop (location_t location, tree cond)
10427 if (truth_value_p (TREE_CODE (cond)))
10428 warning_at (location, OPT_Wparentheses,
10429 "the omitted middle operand in ?: will always be %<true%>, "
10430 "suggest explicit middle operand");
10433 /* Give an error for storing into ARG, which is 'const'. USE indicates
10434 how ARG was being used. */
10437 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10439 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10441 /* Using this macro rather than (for example) arrays of messages
10442 ensures that all the format strings are checked at compile
10444 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10445 : (use == lv_increment ? (I) \
10446 : (use == lv_decrement ? (D) : (AS))))
10447 if (TREE_CODE (arg) == COMPONENT_REF)
10449 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10450 error_at (loc, READONLY_MSG (G_("assignment of member "
10451 "%qD in read-only object"),
10452 G_("increment of member "
10453 "%qD in read-only object"),
10454 G_("decrement of member "
10455 "%qD in read-only object"),
10456 G_("member %qD in read-only object "
10457 "used as %<asm%> output")),
10458 TREE_OPERAND (arg, 1));
10460 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10461 G_("increment of read-only member %qD"),
10462 G_("decrement of read-only member %qD"),
10463 G_("read-only member %qD used as %<asm%> output")),
10464 TREE_OPERAND (arg, 1));
10466 else if (VAR_P (arg))
10467 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10468 G_("increment of read-only variable %qD"),
10469 G_("decrement of read-only variable %qD"),
10470 G_("read-only variable %qD used as %<asm%> output")),
10472 else if (TREE_CODE (arg) == PARM_DECL)
10473 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10474 G_("increment of read-only parameter %qD"),
10475 G_("decrement of read-only parameter %qD"),
10476 G_("read-only parameter %qD use as %<asm%> output")),
10478 else if (TREE_CODE (arg) == RESULT_DECL)
10480 gcc_assert (c_dialect_cxx ());
10481 error_at (loc, READONLY_MSG (G_("assignment of "
10482 "read-only named return value %qD"),
10484 "read-only named return value %qD"),
10486 "read-only named return value %qD"),
10487 G_("read-only named return value %qD "
10488 "used as %<asm%>output")),
10491 else if (TREE_CODE (arg) == FUNCTION_DECL)
10492 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10493 G_("increment of function %qD"),
10494 G_("decrement of function %qD"),
10495 G_("function %qD used as %<asm%> output")),
10498 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10499 G_("increment of read-only location %qE"),
10500 G_("decrement of read-only location %qE"),
10501 G_("read-only location %qE used as %<asm%> output")),
10505 /* Print an error message for an invalid lvalue. USE says
10506 how the lvalue is being used and so selects the error message. LOC
10507 is the location for the error. */
10510 lvalue_error (location_t loc, enum lvalue_use use)
10515 error_at (loc, "lvalue required as left operand of assignment");
10518 error_at (loc, "lvalue required as increment operand");
10521 error_at (loc, "lvalue required as decrement operand");
10524 error_at (loc, "lvalue required as unary %<&%> operand");
10527 error_at (loc, "lvalue required in asm statement");
10530 gcc_unreachable ();
10534 /* Print an error message for an invalid indirection of type TYPE.
10535 ERRSTRING is the name of the operator for the indirection. */
10538 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10543 gcc_assert (c_dialect_cxx ());
10544 error_at (loc, "invalid type argument (have %qT)", type);
10546 case RO_ARRAY_INDEXING:
10548 "invalid type argument of array indexing (have %qT)",
10551 case RO_UNARY_STAR:
10553 "invalid type argument of unary %<*%> (have %qT)",
10558 "invalid type argument of %<->%> (have %qT)",
10561 case RO_ARROW_STAR:
10563 "invalid type argument of %<->*%> (have %qT)",
10566 case RO_IMPLICIT_CONVERSION:
10568 "invalid type argument of implicit conversion (have %qT)",
10572 gcc_unreachable ();
10576 /* *PTYPE is an incomplete array. Complete it with a domain based on
10577 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10578 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10579 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10582 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10584 tree maxindex, type, main_type, elt, unqual_elt;
10585 int failure = 0, quals;
10586 hashval_t hashcode = 0;
10587 bool overflow_p = false;
10589 maxindex = size_zero_node;
10592 if (TREE_CODE (initial_value) == STRING_CST)
10595 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10596 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10598 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10600 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10602 if (vec_safe_is_empty (v))
10606 maxindex = ssize_int (-1);
10611 unsigned HOST_WIDE_INT cnt;
10612 constructor_elt *ce;
10613 bool fold_p = false;
10616 maxindex = (*v)[0].index, fold_p = true;
10618 curindex = maxindex;
10620 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10622 bool curfold_p = false;
10624 curindex = ce->index, curfold_p = true;
10629 /* Since we treat size types now as ordinary
10630 unsigned types, we need an explicit overflow
10632 tree orig = curindex;
10633 curindex = fold_convert (sizetype, curindex);
10634 overflow_p |= tree_int_cst_lt (curindex, orig);
10636 curindex = size_binop (PLUS_EXPR, curindex,
10639 if (tree_int_cst_lt (maxindex, curindex))
10640 maxindex = curindex, fold_p = curfold_p;
10644 tree orig = maxindex;
10645 maxindex = fold_convert (sizetype, maxindex);
10646 overflow_p |= tree_int_cst_lt (maxindex, orig);
10652 /* Make an error message unless that happened already. */
10653 if (initial_value != error_mark_node)
10665 elt = TREE_TYPE (type);
10666 quals = TYPE_QUALS (strip_array_types (elt));
10670 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10672 /* Using build_distinct_type_copy and modifying things afterward instead
10673 of using build_array_type to create a new type preserves all of the
10674 TYPE_LANG_FLAG_? bits that the front end may have set. */
10675 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10676 TREE_TYPE (main_type) = unqual_elt;
10677 TYPE_DOMAIN (main_type)
10678 = build_range_type (TREE_TYPE (maxindex),
10679 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10680 layout_type (main_type);
10682 /* Make sure we have the canonical MAIN_TYPE. */
10683 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10684 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10686 main_type = type_hash_canon (hashcode, main_type);
10688 /* Fix the canonical type. */
10689 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10690 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10691 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10692 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10693 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10694 != TYPE_DOMAIN (main_type)))
10695 TYPE_CANONICAL (main_type)
10696 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10697 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10699 TYPE_CANONICAL (main_type) = main_type;
10704 type = c_build_qualified_type (main_type, quals);
10706 if (COMPLETE_TYPE_P (type)
10707 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10708 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10710 error ("size of array is too large");
10711 /* If we proceed with the array type as it is, we'll eventually
10712 crash in tree_to_[su]hwi(). */
10713 type = error_mark_node;
10720 /* Like c_mark_addressable but don't check register qualifier. */
10722 c_common_mark_addressable_vec (tree t)
10724 while (handled_component_p (t))
10725 t = TREE_OPERAND (t, 0);
10727 && TREE_CODE (t) != PARM_DECL
10728 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
10730 TREE_ADDRESSABLE (t) = 1;
10735 /* Used to help initialize the builtin-types.def table. When a type of
10736 the correct size doesn't exist, use error_mark_node instead of NULL.
10737 The later results in segfaults even when a decl using the type doesn't
10741 builtin_type_for_size (int size, bool unsignedp)
10743 tree type = c_common_type_for_size (size, unsignedp);
10744 return type ? type : error_mark_node;
10747 /* A helper function for resolve_overloaded_builtin in resolving the
10748 overloaded __sync_ builtins. Returns a positive power of 2 if the
10749 first operand of PARAMS is a pointer to a supported data type.
10750 Returns 0 if an error is encountered.
10751 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10755 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
10757 /* Type of the argument. */
10759 /* Type the argument points to. */
10763 if (vec_safe_is_empty (params))
10765 error ("too few arguments to function %qE", function);
10769 argtype = type = TREE_TYPE ((*params)[0]);
10770 if (TREE_CODE (type) == ARRAY_TYPE)
10772 /* Force array-to-pointer decay for C++. */
10773 gcc_assert (c_dialect_cxx());
10774 (*params)[0] = default_conversion ((*params)[0]);
10775 type = TREE_TYPE ((*params)[0]);
10777 if (TREE_CODE (type) != POINTER_TYPE)
10780 type = TREE_TYPE (type);
10781 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10784 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
10787 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10788 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10792 /* Issue the diagnostic only if the argument is valid, otherwise
10793 it would be redundant at best and could be misleading. */
10794 if (argtype != error_mark_node)
10795 error ("operand type %qT is incompatible with argument %d of %qE",
10796 argtype, 1, function);
10800 /* A helper function for resolve_overloaded_builtin. Adds casts to
10801 PARAMS to make arguments match up with those of FUNCTION. Drops
10802 the variadic arguments at the end. Returns false if some error
10803 was encountered; true on success. */
10806 sync_resolve_params (location_t loc, tree orig_function, tree function,
10807 vec<tree, va_gc> *params, bool orig_format)
10809 function_args_iterator iter;
10811 unsigned int parmnum;
10813 function_args_iter_init (&iter, TREE_TYPE (function));
10814 /* We've declared the implementation functions to use "volatile void *"
10815 as the pointer parameter, so we shouldn't get any complaints from the
10816 call to check_function_arguments what ever type the user used. */
10817 function_args_iter_next (&iter);
10818 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10819 ptype = TYPE_MAIN_VARIANT (ptype);
10821 /* For the rest of the values, we need to cast these to FTYPE, so that we
10822 don't get warnings for passing pointer types, etc. */
10826 tree val, arg_type;
10828 arg_type = function_args_iter_cond (&iter);
10829 /* XXX void_type_node belies the abstraction. */
10830 if (arg_type == void_type_node)
10834 if (params->length () <= parmnum)
10836 error_at (loc, "too few arguments to function %qE", orig_function);
10840 /* Only convert parameters if arg_type is unsigned integer type with
10841 new format sync routines, i.e. don't attempt to convert pointer
10842 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10843 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10845 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10847 /* Ideally for the first conversion we'd use convert_for_assignment
10848 so that we get warnings for anything that doesn't match the pointer
10849 type. This isn't portable across the C and C++ front ends atm. */
10850 val = (*params)[parmnum];
10851 val = convert (ptype, val);
10852 val = convert (arg_type, val);
10853 (*params)[parmnum] = val;
10856 function_args_iter_next (&iter);
10859 /* __atomic routines are not variadic. */
10860 if (!orig_format && params->length () != parmnum + 1)
10862 error_at (loc, "too many arguments to function %qE", orig_function);
10866 /* The definition of these primitives is variadic, with the remaining
10867 being "an optional list of variables protected by the memory barrier".
10868 No clue what that's supposed to mean, precisely, but we consider all
10869 call-clobbered variables to be protected so we're safe. */
10870 params->truncate (parmnum + 1);
10875 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10876 RESULT to make it match the type of the first pointer argument in
10880 sync_resolve_return (tree first_param, tree result, bool orig_format)
10882 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10883 tree rtype = TREE_TYPE (result);
10884 ptype = TYPE_MAIN_VARIANT (ptype);
10886 /* New format doesn't require casting unless the types are the same size. */
10887 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10888 return convert (ptype, result);
10893 /* This function verifies the PARAMS to generic atomic FUNCTION.
10894 It returns the size if all the parameters are the same size, otherwise
10895 0 is returned if the parameters are invalid. */
10898 get_atomic_generic_size (location_t loc, tree function,
10899 vec<tree, va_gc> *params)
10901 unsigned int n_param;
10902 unsigned int n_model;
10907 /* Determine the parameter makeup. */
10908 switch (DECL_FUNCTION_CODE (function))
10910 case BUILT_IN_ATOMIC_EXCHANGE:
10914 case BUILT_IN_ATOMIC_LOAD:
10915 case BUILT_IN_ATOMIC_STORE:
10919 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10924 gcc_unreachable ();
10927 if (vec_safe_length (params) != n_param)
10929 error_at (loc, "incorrect number of arguments to function %qE", function);
10933 /* Get type of first parameter, and determine its size. */
10934 type_0 = TREE_TYPE ((*params)[0]);
10935 if (TREE_CODE (type_0) == ARRAY_TYPE)
10937 /* Force array-to-pointer decay for C++. */
10938 gcc_assert (c_dialect_cxx());
10939 (*params)[0] = default_conversion ((*params)[0]);
10940 type_0 = TREE_TYPE ((*params)[0]);
10942 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10944 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10949 /* Types must be compile time constant sizes. */
10950 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10953 "argument 1 of %qE must be a pointer to a constant size type",
10958 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10960 /* Zero size objects are not allowed. */
10964 "argument 1 of %qE must be a pointer to a nonzero size object",
10969 /* Check each other parameter is a pointer and the same size. */
10970 for (x = 0; x < n_param - n_model; x++)
10973 tree type = TREE_TYPE ((*params)[x]);
10974 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10975 if (n_param == 6 && x == 3)
10977 if (!POINTER_TYPE_P (type))
10979 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10983 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10984 size = type_size ? tree_to_uhwi (type_size) : 0;
10985 if (size != size_0)
10987 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10993 /* Check memory model parameters for validity. */
10994 for (x = n_param - n_model ; x < n_param; x++)
10996 tree p = (*params)[x];
10997 if (TREE_CODE (p) == INTEGER_CST)
10999 int i = tree_to_uhwi (p);
11000 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
11002 warning_at (loc, OPT_Winvalid_memory_model,
11003 "invalid memory model argument %d of %qE", x + 1,
11008 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11010 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11020 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11021 at the beginning of the parameter list PARAMS representing the size of the
11022 objects. This is to match the library ABI requirement. LOC is the location
11023 of the function call.
11024 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11025 returned to allow the external call to be constructed. */
11028 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
11029 vec<tree, va_gc> *params)
11033 /* Insert a SIZE_T parameter as the first param. If there isn't
11034 enough space, allocate a new vector and recursively re-build with that. */
11035 if (!params->space (1))
11037 unsigned int z, len;
11038 vec<tree, va_gc> *v;
11041 len = params->length ();
11042 vec_alloc (v, len + 1);
11043 v->quick_push (build_int_cst (size_type_node, n));
11044 for (z = 0; z < len; z++)
11045 v->quick_push ((*params)[z]);
11046 f = build_function_call_vec (loc, vNULL, function, v, NULL);
11051 /* Add the size parameter and leave as a function call for processing. */
11052 size_node = build_int_cst (size_type_node, n);
11053 params->quick_insert (0, size_node);
11058 /* Return whether atomic operations for naturally aligned N-byte
11059 arguments are supported, whether inline or through libatomic. */
11061 atomic_size_supported_p (int n)
11072 return targetm.scalar_mode_supported_p (TImode);
11079 /* This will process an __atomic_exchange function call, determine whether it
11080 needs to be mapped to the _N variation, or turned into a library call.
11081 LOC is the location of the builtin call.
11082 FUNCTION is the DECL that has been invoked;
11083 PARAMS is the argument list for the call. The return value is non-null
11084 TRUE is returned if it is translated into the proper format for a call to the
11085 external library, and NEW_RETURN is set the tree for that function.
11086 FALSE is returned if processing for the _N variation is required, and
11087 NEW_RETURN is set to the return value the result is copied into. */
11089 resolve_overloaded_atomic_exchange (location_t loc, tree function,
11090 vec<tree, va_gc> *params, tree *new_return)
11092 tree p0, p1, p2, p3;
11093 tree I_type, I_type_ptr;
11094 int n = get_atomic_generic_size (loc, function, params);
11096 /* Size of 0 is an error condition. */
11099 *new_return = error_mark_node;
11103 /* If not a lock-free size, change to the library generic format. */
11104 if (!atomic_size_supported_p (n))
11106 *new_return = add_atomic_size_parameter (n, loc, function, params);
11110 /* Otherwise there is a lockfree match, transform the call from:
11111 void fn(T* mem, T* desired, T* return, model)
11113 *return = (T) (fn (In* mem, (In) *desired, model)) */
11120 /* Create pointer to appropriate size. */
11121 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11122 I_type_ptr = build_pointer_type (I_type);
11124 /* Convert object pointer to required type. */
11125 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11127 /* Convert new value to required type, and dereference it. */
11128 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11129 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11132 /* Move memory model to the 3rd position, and end param list. */
11134 params->truncate (3);
11136 /* Convert return pointer and dereference it for later assignment. */
11137 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11143 /* This will process an __atomic_compare_exchange function call, determine
11144 whether it needs to be mapped to the _N variation, or turned into a lib call.
11145 LOC is the location of the builtin call.
11146 FUNCTION is the DECL that has been invoked;
11147 PARAMS is the argument list for the call. The return value is non-null
11148 TRUE is returned if it is translated into the proper format for a call to the
11149 external library, and NEW_RETURN is set the tree for that function.
11150 FALSE is returned if processing for the _N variation is required. */
11153 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11154 vec<tree, va_gc> *params,
11158 tree I_type, I_type_ptr;
11159 int n = get_atomic_generic_size (loc, function, params);
11161 /* Size of 0 is an error condition. */
11164 *new_return = error_mark_node;
11168 /* If not a lock-free size, change to the library generic format. */
11169 if (!atomic_size_supported_p (n))
11171 /* The library generic format does not have the weak parameter, so
11172 remove it from the param list. Since a parameter has been removed,
11173 we can be sure that there is room for the SIZE_T parameter, meaning
11174 there will not be a recursive rebuilding of the parameter list, so
11175 there is no danger this will be done twice. */
11178 (*params)[3] = (*params)[4];
11179 (*params)[4] = (*params)[5];
11180 params->truncate (5);
11182 *new_return = add_atomic_size_parameter (n, loc, function, params);
11186 /* Otherwise, there is a match, so the call needs to be transformed from:
11187 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11189 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11195 /* Create pointer to appropriate size. */
11196 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11197 I_type_ptr = build_pointer_type (I_type);
11199 /* Convert object pointer to required type. */
11200 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11203 /* Convert expected pointer to required type. */
11204 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11207 /* Convert desired value to required type, and dereference it. */
11208 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11209 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11212 /* The rest of the parameters are fine. NULL means no special return value
11214 *new_return = NULL;
11219 /* This will process an __atomic_load function call, determine whether it
11220 needs to be mapped to the _N variation, or turned into a library call.
11221 LOC is the location of the builtin call.
11222 FUNCTION is the DECL that has been invoked;
11223 PARAMS is the argument list for the call. The return value is non-null
11224 TRUE is returned if it is translated into the proper format for a call to the
11225 external library, and NEW_RETURN is set the tree for that function.
11226 FALSE is returned if processing for the _N variation is required, and
11227 NEW_RETURN is set to the return value the result is copied into. */
11230 resolve_overloaded_atomic_load (location_t loc, tree function,
11231 vec<tree, va_gc> *params, tree *new_return)
11234 tree I_type, I_type_ptr;
11235 int n = get_atomic_generic_size (loc, function, params);
11237 /* Size of 0 is an error condition. */
11240 *new_return = error_mark_node;
11244 /* If not a lock-free size, change to the library generic format. */
11245 if (!atomic_size_supported_p (n))
11247 *new_return = add_atomic_size_parameter (n, loc, function, params);
11251 /* Otherwise, there is a match, so the call needs to be transformed from:
11252 void fn(T* mem, T* return, model)
11254 *return = (T) (fn ((In *) mem, model)) */
11260 /* Create pointer to appropriate size. */
11261 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11262 I_type_ptr = build_pointer_type (I_type);
11264 /* Convert object pointer to required type. */
11265 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11268 /* Move memory model to the 2nd position, and end param list. */
11270 params->truncate (2);
11272 /* Convert return pointer and dereference it for later assignment. */
11273 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11279 /* This will process an __atomic_store function call, determine whether it
11280 needs to be mapped to the _N variation, or turned into a library call.
11281 LOC is the location of the builtin call.
11282 FUNCTION is the DECL that has been invoked;
11283 PARAMS is the argument list for the call. The return value is non-null
11284 TRUE is returned if it is translated into the proper format for a call to the
11285 external library, and NEW_RETURN is set the tree for that function.
11286 FALSE is returned if processing for the _N variation is required, and
11287 NEW_RETURN is set to the return value the result is copied into. */
11290 resolve_overloaded_atomic_store (location_t loc, tree function,
11291 vec<tree, va_gc> *params, tree *new_return)
11294 tree I_type, I_type_ptr;
11295 int n = get_atomic_generic_size (loc, function, params);
11297 /* Size of 0 is an error condition. */
11300 *new_return = error_mark_node;
11304 /* If not a lock-free size, change to the library generic format. */
11305 if (!atomic_size_supported_p (n))
11307 *new_return = add_atomic_size_parameter (n, loc, function, params);
11311 /* Otherwise, there is a match, so the call needs to be transformed from:
11312 void fn(T* mem, T* value, model)
11314 fn ((In *) mem, (In) *value, model) */
11319 /* Create pointer to appropriate size. */
11320 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11321 I_type_ptr = build_pointer_type (I_type);
11323 /* Convert object pointer to required type. */
11324 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11327 /* Convert new value to required type, and dereference it. */
11328 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11329 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11332 /* The memory model is in the right spot already. Return is void. */
11333 *new_return = NULL_TREE;
11339 /* Some builtin functions are placeholders for other expressions. This
11340 function should be called immediately after parsing the call expression
11341 before surrounding code has committed to the type of the expression.
11343 LOC is the location of the builtin call.
11345 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11346 PARAMS is the argument list for the call. The return value is non-null
11347 when expansion is complete, and null if normal processing should
11351 resolve_overloaded_builtin (location_t loc, tree function,
11352 vec<tree, va_gc> *params)
11354 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11356 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11357 Those are not valid to call with a pointer to _Bool (or C++ bool)
11358 and so must be rejected. */
11359 bool fetch_op = true;
11360 bool orig_format = true;
11361 tree new_return = NULL_TREE;
11363 switch (DECL_BUILT_IN_CLASS (function))
11365 case BUILT_IN_NORMAL:
11368 if (targetm.resolve_overloaded_builtin)
11369 return targetm.resolve_overloaded_builtin (loc, function, params);
11376 /* Handle BUILT_IN_NORMAL here. */
11379 case BUILT_IN_ATOMIC_EXCHANGE:
11380 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11381 case BUILT_IN_ATOMIC_LOAD:
11382 case BUILT_IN_ATOMIC_STORE:
11384 /* Handle these 4 together so that they can fall through to the next
11385 case if the call is transformed to an _N variant. */
11388 case BUILT_IN_ATOMIC_EXCHANGE:
11390 if (resolve_overloaded_atomic_exchange (loc, function, params,
11393 /* Change to the _N variant. */
11394 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11398 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11400 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11404 /* Change to the _N variant. */
11405 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11408 case BUILT_IN_ATOMIC_LOAD:
11410 if (resolve_overloaded_atomic_load (loc, function, params,
11413 /* Change to the _N variant. */
11414 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11417 case BUILT_IN_ATOMIC_STORE:
11419 if (resolve_overloaded_atomic_store (loc, function, params,
11422 /* Change to the _N variant. */
11423 orig_code = BUILT_IN_ATOMIC_STORE_N;
11427 gcc_unreachable ();
11429 /* Fallthrough to the normal processing. */
11431 case BUILT_IN_ATOMIC_EXCHANGE_N:
11432 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11433 case BUILT_IN_ATOMIC_LOAD_N:
11434 case BUILT_IN_ATOMIC_STORE_N:
11437 /* Fallthrough to further processing. */
11439 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11440 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11441 case BUILT_IN_ATOMIC_AND_FETCH_N:
11442 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11443 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11444 case BUILT_IN_ATOMIC_OR_FETCH_N:
11445 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11446 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11447 case BUILT_IN_ATOMIC_FETCH_AND_N:
11448 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11449 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11450 case BUILT_IN_ATOMIC_FETCH_OR_N:
11452 orig_format = false;
11453 /* Fallthru for parameter processing. */
11455 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11456 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11457 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11458 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11459 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11460 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11461 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11462 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11463 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11464 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11465 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11466 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11467 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11468 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11469 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11470 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11472 /* The following are not _FETCH_OPs and must be accepted with
11473 pointers to _Bool (or C++ bool). */
11476 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11477 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11478 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11479 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11481 int n = sync_resolve_size (function, params, fetch_op);
11482 tree new_function, first_param, result;
11483 enum built_in_function fncode;
11486 return error_mark_node;
11488 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11489 new_function = builtin_decl_explicit (fncode);
11490 if (!sync_resolve_params (loc, function, new_function, params,
11492 return error_mark_node;
11494 first_param = (*params)[0];
11495 result = build_function_call_vec (loc, vNULL, new_function, params,
11497 if (result == error_mark_node)
11499 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11500 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11501 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11502 result = sync_resolve_return (first_param, result, orig_format);
11505 /* Prevent -Wunused-value warning. */
11506 TREE_USED (result) = true;
11508 /* If new_return is set, assign function to that expr and cast the
11509 result to void since the generic interface returned void. */
11512 /* Cast function result from I{1,2,4,8,16} to the required type. */
11513 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11514 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11516 TREE_SIDE_EFFECTS (result) = 1;
11517 protected_set_expr_location (result, loc);
11518 result = convert (void_type_node, result);
11528 /* vector_types_compatible_elements_p is used in type checks of vectors
11529 values used as operands of binary operators. Where it returns true, and
11530 the other checks of the caller succeed (being vector types in he first
11531 place, and matching number of elements), we can just treat the types
11532 as essentially the same.
11533 Contrast with vector_targets_convertible_p, which is used for vector
11534 pointer types, and vector_types_convertible_p, which will allow
11535 language-specific matches under the control of flag_lax_vector_conversions,
11536 and might still require a conversion. */
11537 /* True if vector types T1 and T2 can be inputs to the same binary
11538 operator without conversion.
11539 We don't check the overall vector size here because some of our callers
11540 want to give different error messages when the vectors are compatible
11541 except for the element count. */
11544 vector_types_compatible_elements_p (tree t1, tree t2)
11546 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11547 t1 = TREE_TYPE (t1);
11548 t2 = TREE_TYPE (t2);
11550 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11552 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11553 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11554 || c2 == FIXED_POINT_TYPE));
11556 t1 = c_common_signed_type (t1);
11557 t2 = c_common_signed_type (t2);
11558 /* Equality works here because c_common_signed_type uses
11559 TYPE_MAIN_VARIANT. */
11562 if (opaque && c1 == c2
11563 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11564 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11569 /* Check for missing format attributes on function pointers. LTYPE is
11570 the new type or left-hand side type. RTYPE is the old type or
11571 right-hand side type. Returns TRUE if LTYPE is missing the desired
11575 check_missing_format_attribute (tree ltype, tree rtype)
11577 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11580 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11581 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11586 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11587 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11595 /* Subscripting with type char is likely to lose on a machine where
11596 chars are signed. So warn on any machine, but optionally. Don't
11597 warn for unsigned char since that type is safe. Don't warn for
11598 signed char because anyone who uses that must have done so
11599 deliberately. Furthermore, we reduce the false positive load by
11600 warning only for non-constant value of type char. */
11603 warn_array_subscript_with_type_char (location_t loc, tree index)
11605 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11606 && TREE_CODE (index) != INTEGER_CST)
11607 warning_at (loc, OPT_Wchar_subscripts,
11608 "array subscript has type %<char%>");
11611 /* Implement -Wparentheses for the unexpected C precedence rules, to
11612 cover cases like x + y << z which readers are likely to
11613 misinterpret. We have seen an expression in which CODE is a binary
11614 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11615 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11616 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11617 expression was not formed using a binary or unary operator, or it
11618 was enclosed in parentheses. */
11621 warn_about_parentheses (location_t loc, enum tree_code code,
11622 enum tree_code code_left, tree arg_left,
11623 enum tree_code code_right, tree arg_right)
11625 if (!warn_parentheses)
11628 /* This macro tests that the expression ARG with original tree code
11629 CODE appears to be a boolean expression. or the result of folding a
11630 boolean expression. */
11631 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11632 (truth_value_p (TREE_CODE (ARG)) \
11633 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11634 /* Folding may create 0 or 1 integers from other expressions. */ \
11635 || ((CODE) != INTEGER_CST \
11636 && (integer_onep (ARG) || integer_zerop (ARG))))
11641 if (code_left == PLUS_EXPR)
11642 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11643 "suggest parentheses around %<+%> inside %<<<%>");
11644 else if (code_right == PLUS_EXPR)
11645 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11646 "suggest parentheses around %<+%> inside %<<<%>");
11647 else if (code_left == MINUS_EXPR)
11648 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11649 "suggest parentheses around %<-%> inside %<<<%>");
11650 else if (code_right == MINUS_EXPR)
11651 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11652 "suggest parentheses around %<-%> inside %<<<%>");
11656 if (code_left == PLUS_EXPR)
11657 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11658 "suggest parentheses around %<+%> inside %<>>%>");
11659 else if (code_right == PLUS_EXPR)
11660 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11661 "suggest parentheses around %<+%> inside %<>>%>");
11662 else if (code_left == MINUS_EXPR)
11663 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11664 "suggest parentheses around %<-%> inside %<>>%>");
11665 else if (code_right == MINUS_EXPR)
11666 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11667 "suggest parentheses around %<-%> inside %<>>%>");
11670 case TRUTH_ORIF_EXPR:
11671 if (code_left == TRUTH_ANDIF_EXPR)
11672 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11673 "suggest parentheses around %<&&%> within %<||%>");
11674 else if (code_right == TRUTH_ANDIF_EXPR)
11675 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11676 "suggest parentheses around %<&&%> within %<||%>");
11680 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11681 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11682 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11683 "suggest parentheses around arithmetic in operand of %<|%>");
11684 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11685 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11686 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11687 "suggest parentheses around arithmetic in operand of %<|%>");
11688 /* Check cases like x|y==z */
11689 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11690 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11691 "suggest parentheses around comparison in operand of %<|%>");
11692 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11693 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11694 "suggest parentheses around comparison in operand of %<|%>");
11695 /* Check cases like !x | y */
11696 else if (code_left == TRUTH_NOT_EXPR
11697 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11698 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11699 "suggest parentheses around operand of "
11700 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11704 if (code_left == BIT_AND_EXPR
11705 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11706 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11707 "suggest parentheses around arithmetic in operand of %<^%>");
11708 else if (code_right == BIT_AND_EXPR
11709 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11710 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11711 "suggest parentheses around arithmetic in operand of %<^%>");
11712 /* Check cases like x^y==z */
11713 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11714 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11715 "suggest parentheses around comparison in operand of %<^%>");
11716 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11717 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11718 "suggest parentheses around comparison in operand of %<^%>");
11722 if (code_left == PLUS_EXPR)
11723 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11724 "suggest parentheses around %<+%> in operand of %<&%>");
11725 else if (code_right == PLUS_EXPR)
11726 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11727 "suggest parentheses around %<+%> in operand of %<&%>");
11728 else if (code_left == MINUS_EXPR)
11729 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11730 "suggest parentheses around %<-%> in operand of %<&%>");
11731 else if (code_right == MINUS_EXPR)
11732 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11733 "suggest parentheses around %<-%> in operand of %<&%>");
11734 /* Check cases like x&y==z */
11735 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11736 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11737 "suggest parentheses around comparison in operand of %<&%>");
11738 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11739 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11740 "suggest parentheses around comparison in operand of %<&%>");
11741 /* Check cases like !x & y */
11742 else if (code_left == TRUTH_NOT_EXPR
11743 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11744 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11745 "suggest parentheses around operand of "
11746 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11750 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11751 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11752 "suggest parentheses around comparison in operand of %<==%>");
11753 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11754 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11755 "suggest parentheses around comparison in operand of %<==%>");
11758 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11759 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11760 "suggest parentheses around comparison in operand of %<!=%>");
11761 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11762 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11763 "suggest parentheses around comparison in operand of %<!=%>");
11767 if (TREE_CODE_CLASS (code) == tcc_comparison)
11769 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11770 && code_left != NE_EXPR && code_left != EQ_EXPR
11771 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11772 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11773 "comparisons like %<X<=Y<=Z%> do not "
11774 "have their mathematical meaning");
11775 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11776 && code_right != NE_EXPR && code_right != EQ_EXPR
11777 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11778 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11779 "comparisons like %<X<=Y<=Z%> do not "
11780 "have their mathematical meaning");
11784 #undef NOT_A_BOOLEAN_EXPR_P
11787 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11790 warn_for_unused_label (tree label)
11792 if (!TREE_USED (label))
11794 if (DECL_INITIAL (label))
11795 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11797 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11801 /* Warn for division by zero according to the value of DIVISOR. LOC
11802 is the location of the division operator. */
11805 warn_for_div_by_zero (location_t loc, tree divisor)
11807 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11808 about division by zero. Do not issue a warning if DIVISOR has a
11809 floating-point type, since we consider 0.0/0.0 a valid way of
11810 generating a NaN. */
11811 if (c_inhibit_evaluation_warnings == 0
11812 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11813 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11816 /* Subroutine of build_binary_op. Give warnings for comparisons
11817 between signed and unsigned quantities that may fail. Do the
11818 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11819 so that casts will be considered, but default promotions won't
11822 LOCATION is the location of the comparison operator.
11824 The arguments of this function map directly to local variables
11825 of build_binary_op. */
11828 warn_for_sign_compare (location_t location,
11829 tree orig_op0, tree orig_op1,
11830 tree op0, tree op1,
11831 tree result_type, enum tree_code resultcode)
11833 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11834 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11835 int unsignedp0, unsignedp1;
11837 /* In C++, check for comparison of different enum types. */
11838 if (c_dialect_cxx()
11839 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11840 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11841 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11842 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11844 warning_at (location,
11845 OPT_Wsign_compare, "comparison between types %qT and %qT",
11846 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11849 /* Do not warn if the comparison is being done in a signed type,
11850 since the signed type will only be chosen if it can represent
11851 all the values of the unsigned type. */
11852 if (!TYPE_UNSIGNED (result_type))
11854 /* Do not warn if both operands are unsigned. */
11855 else if (op0_signed == op1_signed)
11859 tree sop, uop, base_type;
11863 sop = orig_op0, uop = orig_op1;
11865 sop = orig_op1, uop = orig_op0;
11867 STRIP_TYPE_NOPS (sop);
11868 STRIP_TYPE_NOPS (uop);
11869 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11870 ? TREE_TYPE (result_type) : result_type);
11872 /* Do not warn if the signed quantity is an unsuffixed integer
11873 literal (or some static constant expression involving such
11874 literals or a conditional expression involving such literals)
11875 and it is non-negative. */
11876 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11878 /* Do not warn if the comparison is an equality operation, the
11879 unsigned quantity is an integral constant, and it would fit
11880 in the result if the result were signed. */
11881 else if (TREE_CODE (uop) == INTEGER_CST
11882 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11883 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11885 /* In C, do not warn if the unsigned quantity is an enumeration
11886 constant and its maximum value would fit in the result if the
11887 result were signed. */
11888 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11889 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11890 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11891 c_common_signed_type (base_type)))
11894 warning_at (location,
11896 "comparison between signed and unsigned integer expressions");
11899 /* Warn if two unsigned values are being compared in a size larger
11900 than their original size, and one (and only one) is the result of
11901 a `~' operator. This comparison will always fail.
11903 Also warn if one operand is a constant, and the constant does not
11904 have all bits set that are set in the ~ operand when it is
11907 op0 = c_common_get_narrower (op0, &unsignedp0);
11908 op1 = c_common_get_narrower (op1, &unsignedp1);
11910 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11911 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11913 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11914 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11915 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11916 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11918 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11921 HOST_WIDE_INT constant, mask;
11925 if (tree_fits_shwi_p (op0))
11928 unsignedp = unsignedp1;
11929 constant = tree_to_shwi (op0);
11934 unsignedp = unsignedp0;
11935 constant = tree_to_shwi (op1);
11938 bits = TYPE_PRECISION (TREE_TYPE (primop));
11939 if (bits < TYPE_PRECISION (result_type)
11940 && bits < HOST_BITS_PER_LONG && unsignedp)
11942 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
11943 if ((mask & constant) != mask)
11946 warning_at (location, OPT_Wsign_compare,
11947 "promoted ~unsigned is always non-zero");
11949 warning_at (location, OPT_Wsign_compare,
11950 "comparison of promoted ~unsigned with constant");
11954 else if (unsignedp0 && unsignedp1
11955 && (TYPE_PRECISION (TREE_TYPE (op0))
11956 < TYPE_PRECISION (result_type))
11957 && (TYPE_PRECISION (TREE_TYPE (op1))
11958 < TYPE_PRECISION (result_type)))
11959 warning_at (location, OPT_Wsign_compare,
11960 "comparison of promoted ~unsigned with unsigned");
11964 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11965 type via c_common_type. If -Wdouble-promotion is in use, and the
11966 conditions for warning have been met, issue a warning. GMSGID is
11967 the warning message. It must have two %T specifiers for the type
11968 that was converted (generally "float") and the type to which it was
11969 converted (generally "double), respectively. LOC is the location
11970 to which the awrning should refer. */
11973 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11974 const char *gmsgid, location_t loc)
11978 if (!warn_double_promotion)
11980 /* If the conversion will not occur at run-time, there is no need to
11982 if (c_inhibit_evaluation_warnings)
11984 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11985 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11987 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11988 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11989 source_type = type1;
11990 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11991 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11992 source_type = type2;
11995 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11998 /* Possibly warn about unused parameters. */
12001 do_warn_unused_parameter (tree fn)
12005 for (decl = DECL_ARGUMENTS (fn);
12006 decl; decl = DECL_CHAIN (decl))
12007 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12008 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12009 && !TREE_NO_WARNING (decl))
12010 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12011 "unused parameter %qD", decl);
12015 /* Setup a TYPE_DECL node as a typedef representation.
12017 X is a TYPE_DECL for a typedef statement. Create a brand new
12018 ..._TYPE node (which will be just a variant of the existing
12019 ..._TYPE node with identical properties) and then install X
12020 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12022 The whole point here is to end up with a situation where each
12023 and every ..._TYPE node the compiler creates will be uniquely
12024 associated with AT MOST one node representing a typedef name.
12025 This way, even though the compiler substitutes corresponding
12026 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12027 early on, later parts of the compiler can always do the reverse
12028 translation and get back the corresponding typedef name. For
12031 typedef struct S MY_TYPE;
12034 Later parts of the compiler might only know that `object' was of
12035 type `struct S' if it were not for code just below. With this
12036 code however, later parts of the compiler see something like:
12038 struct S' == struct S
12039 typedef struct S' MY_TYPE;
12042 And they can then deduce (from the node for type struct S') that
12043 the original object declaration was:
12047 Being able to do this is important for proper support of protoize,
12048 and also for generating precise symbolic debugging information
12049 which takes full account of the programmer's (typedef) vocabulary.
12051 Obviously, we don't want to generate a duplicate ..._TYPE node if
12052 the TYPE_DECL node that we are now processing really represents a
12053 standard built-in type. */
12056 set_underlying_type (tree x)
12058 if (x == error_mark_node)
12060 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
12062 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12063 TYPE_NAME (TREE_TYPE (x)) = x;
12065 else if (TREE_TYPE (x) != error_mark_node
12066 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12068 tree tt = TREE_TYPE (x);
12069 DECL_ORIGINAL_TYPE (x) = tt;
12070 tt = build_variant_type_copy (tt);
12071 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12072 TYPE_NAME (tt) = x;
12073 TREE_USED (tt) = TREE_USED (x);
12074 TREE_TYPE (x) = tt;
12078 /* Record the types used by the current global variable declaration
12079 being parsed, so that we can decide later to emit their debug info.
12080 Those types are in types_used_by_cur_var_decl, and we are going to
12081 store them in the types_used_by_vars_hash hash table.
12082 DECL is the declaration of the global variable that has been parsed. */
12085 record_types_used_by_current_var_decl (tree decl)
12087 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12089 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
12091 tree type = types_used_by_cur_var_decl->pop ();
12092 types_used_by_var_decl_insert (type, decl);
12096 /* If DECL is a typedef that is declared in the current function,
12097 record it for the purpose of -Wunused-local-typedefs. */
12100 record_locally_defined_typedef (tree decl)
12102 struct c_language_function *l;
12104 if (!warn_unused_local_typedefs
12106 /* if this is not a locally defined typedef then we are not
12108 || !is_typedef_decl (decl)
12109 || !decl_function_context (decl))
12112 l = (struct c_language_function *) cfun->language;
12113 vec_safe_push (l->local_typedefs, decl);
12116 /* If T is a TYPE_DECL declared locally, mark it as used. */
12119 maybe_record_typedef_use (tree t)
12121 if (!is_typedef_decl (t))
12124 TREE_USED (t) = true;
12127 /* Warn if there are some unused locally defined typedefs in the
12128 current function. */
12131 maybe_warn_unused_local_typedefs (void)
12135 /* The number of times we have emitted -Wunused-local-typedefs
12136 warnings. If this is different from errorcount, that means some
12137 unrelated errors have been issued. In which case, we'll avoid
12138 emitting "unused-local-typedefs" warnings. */
12139 static int unused_local_typedefs_warn_count;
12140 struct c_language_function *l;
12145 if ((l = (struct c_language_function *) cfun->language) == NULL)
12148 if (warn_unused_local_typedefs
12149 && errorcount == unused_local_typedefs_warn_count)
12151 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
12152 if (!TREE_USED (decl))
12153 warning_at (DECL_SOURCE_LOCATION (decl),
12154 OPT_Wunused_local_typedefs,
12155 "typedef %qD locally defined but not used", decl);
12156 unused_local_typedefs_warn_count = errorcount;
12159 vec_free (l->local_typedefs);
12162 /* Warn about boolean expression compared with an integer value different
12163 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12164 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12165 are the operands of the comparison. The caller must ensure that
12166 either operand is a boolean expression. */
12169 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12172 if (TREE_CODE_CLASS (code) != tcc_comparison)
12176 if (f = fold_for_warn (op0),
12177 TREE_CODE (f) == INTEGER_CST)
12179 else if (f = fold_for_warn (op1),
12180 TREE_CODE (f) == INTEGER_CST)
12185 if (!integer_zerop (cst) && !integer_onep (cst))
12187 int sign = (TREE_CODE (op0) == INTEGER_CST
12188 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
12189 if (code == EQ_EXPR
12190 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12191 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12192 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12193 "with boolean expression is always false", cst);
12195 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12196 "with boolean expression is always true", cst);
12198 else if (integer_zerop (cst) || integer_onep (cst))
12200 /* If the non-constant operand isn't of a boolean type, we
12201 don't want to warn here. */
12202 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12203 /* Handle booleans promoted to integers. */
12204 if (CONVERT_EXPR_P (noncst)
12205 && TREE_TYPE (noncst) == integer_type_node
12206 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12208 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12209 && !truth_value_p (TREE_CODE (noncst)))
12211 /* Do some magic to get the right diagnostics. */
12212 bool flag = TREE_CODE (op0) == INTEGER_CST;
12213 flag = integer_zerop (cst) ? flag : !flag;
12214 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12215 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12216 "with boolean expression is always true", cst);
12217 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12218 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12219 "with boolean expression is always false", cst);
12223 /* Warn if signed left shift overflows. We don't warn
12224 about left-shifting 1 into the sign bit in C++14; cf.
12225 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12226 LOC is a location of the shift; OP0 and OP1 are the operands.
12227 Return true if an overflow is detected, false otherwise. */
12230 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12232 if (TREE_CODE (op0) != INTEGER_CST
12233 || TREE_CODE (op1) != INTEGER_CST)
12236 tree type0 = TREE_TYPE (op0);
12237 unsigned int prec0 = TYPE_PRECISION (type0);
12239 /* Left-hand operand must be signed. */
12240 if (TYPE_UNSIGNED (type0))
12243 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12244 + TREE_INT_CST_LOW (op1));
12245 /* Handle the case of left-shifting 1 into the sign bit.
12246 * However, shifting 1 _out_ of the sign bit, as in
12247 * INT_MIN << 1, is considered an overflow.
12249 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
12251 /* Never warn for C++14 onwards. */
12252 if (cxx_dialect >= cxx14)
12254 /* Otherwise only if -Wshift-overflow=2. But return
12255 true to signal an overflow for the sake of integer
12256 constant expressions. */
12257 if (warn_shift_overflow < 2)
12261 bool overflowed = min_prec > prec0;
12262 if (overflowed && c_inhibit_evaluation_warnings == 0)
12263 warning_at (loc, OPT_Wshift_overflow_,
12264 "result of %qE requires %u bits to represent, "
12265 "but %qT only has %u bits",
12266 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12267 min_prec, type0, prec0);
12272 /* The C and C++ parsers both use vectors to hold function arguments.
12273 For efficiency, we keep a cache of unused vectors. This is the
12276 typedef vec<tree, va_gc> *tree_gc_vec;
12277 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12279 /* Return a new vector from the cache. If the cache is empty,
12280 allocate a new vector. These vectors are GC'ed, so it is OK if the
12281 pointer is not released.. */
12284 make_tree_vector (void)
12286 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12287 return tree_vector_cache->pop ();
12290 /* Passing 0 to vec::alloc returns NULL, and our callers require
12291 that we always return a non-NULL value. The vector code uses
12292 4 when growing a NULL vector, so we do too. */
12293 vec<tree, va_gc> *v;
12299 /* Release a vector of trees back to the cache. */
12302 release_tree_vector (vec<tree, va_gc> *vec)
12307 vec_safe_push (tree_vector_cache, vec);
12311 /* Get a new tree vector holding a single tree. */
12314 make_tree_vector_single (tree t)
12316 vec<tree, va_gc> *ret = make_tree_vector ();
12317 ret->quick_push (t);
12321 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12324 make_tree_vector_from_list (tree list)
12326 vec<tree, va_gc> *ret = make_tree_vector ();
12327 for (; list; list = TREE_CHAIN (list))
12328 vec_safe_push (ret, TREE_VALUE (list));
12332 /* Get a new tree vector which is a copy of an existing one. */
12335 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12337 vec<tree, va_gc> *ret;
12341 ret = make_tree_vector ();
12342 vec_safe_reserve (ret, vec_safe_length (orig));
12343 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12344 ret->quick_push (t);
12348 /* Return true if KEYWORD starts a type specifier. */
12351 keyword_begins_type_specifier (enum rid keyword)
12355 case RID_AUTO_TYPE:
12367 case RID_DFLOAT128:
12383 if (keyword >= RID_FIRST_INT_N
12384 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12385 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12391 /* Return true if KEYWORD names a type qualifier. */
12394 keyword_is_type_qualifier (enum rid keyword)
12408 /* Return true if KEYWORD names a storage class specifier.
12410 RID_TYPEDEF is not included in this list despite `typedef' being
12411 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12412 such for syntactic convenience only. */
12415 keyword_is_storage_class_specifier (enum rid keyword)
12431 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12434 keyword_is_function_specifier (enum rid keyword)
12448 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12449 declaration-specifier (C99 6.7). */
12452 keyword_is_decl_specifier (enum rid keyword)
12454 if (keyword_is_storage_class_specifier (keyword)
12455 || keyword_is_type_qualifier (keyword)
12456 || keyword_is_function_specifier (keyword))
12463 case RID_CONSTEXPR:
12470 /* Initialize language-specific-bits of tree_contains_struct. */
12473 c_common_init_ts (void)
12475 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12476 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12477 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12480 /* Build a user-defined numeric literal out of an integer constant type VALUE
12481 with identifier SUFFIX. */
12484 build_userdef_literal (tree suffix_id, tree value,
12485 enum overflow_type overflow, tree num_string)
12487 tree literal = make_node (USERDEF_LITERAL);
12488 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12489 USERDEF_LITERAL_VALUE (literal) = value;
12490 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12491 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12495 /* For vector[index], convert the vector to a
12496 pointer of the underlying type. Return true if the resulting
12497 ARRAY_REF should not be an lvalue. */
12500 convert_vector_to_pointer_for_subscript (location_t loc,
12501 tree *vecp, tree index)
12504 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
12506 tree type = TREE_TYPE (*vecp);
12509 ret = !lvalue_p (*vecp);
12510 if (TREE_CODE (index) == INTEGER_CST)
12511 if (!tree_fits_uhwi_p (index)
12512 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12513 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12517 tree tmp = create_tmp_var_raw (type);
12518 DECL_SOURCE_LOCATION (tmp) = loc;
12519 *vecp = c_save_expr (*vecp);
12520 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12522 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12523 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12525 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12526 *vecp, NULL_TREE, NULL_TREE),
12530 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12531 NULL_TREE, NULL_TREE);
12532 SET_EXPR_LOCATION (*vecp, loc);
12533 c_common_mark_addressable_vec (tmp);
12536 c_common_mark_addressable_vec (*vecp);
12537 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12538 type1 = build_pointer_type (TREE_TYPE (*vecp));
12539 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12541 && !DECL_P (*vecp))
12543 /* If the original vector isn't declared may_alias and it
12544 isn't a bare vector look if the subscripting would
12545 alias the vector we subscript, and if not, force ref-all. */
12546 alias_set_type vecset = get_alias_set (*vecp);
12547 alias_set_type sset = get_alias_set (type);
12548 if (!alias_sets_must_conflict_p (sset, vecset)
12549 && !alias_set_subset_of (sset, vecset))
12552 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12553 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12554 *vecp = convert (type, *vecp);
12559 /* Determine which of the operands, if any, is a scalar that needs to be
12560 converted to a vector, for the range of operations. */
12562 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12565 tree type0 = TREE_TYPE (op0);
12566 tree type1 = TREE_TYPE (op1);
12567 bool integer_only_op = false;
12568 enum stv_conv ret = stv_firstarg;
12570 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
12573 /* Most GENERIC binary expressions require homogeneous arguments.
12574 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12575 argument that is a vector and a second one that is a scalar, so
12576 we never return stv_secondarg for them. */
12579 if (TREE_CODE (type0) == INTEGER_TYPE
12580 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12582 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12585 error_at (loc, "conversion of scalar %qT to vector %qT "
12586 "involves truncation", type0, type1);
12590 return stv_firstarg;
12597 integer_only_op = true;
12598 /* ... fall through ... */
12600 case VEC_COND_EXPR:
12605 case TRUNC_DIV_EXPR:
12606 case CEIL_DIV_EXPR:
12607 case FLOOR_DIV_EXPR:
12608 case ROUND_DIV_EXPR:
12609 case EXACT_DIV_EXPR:
12610 case TRUNC_MOD_EXPR:
12611 case FLOOR_MOD_EXPR:
12619 /* What about UNLT_EXPR? */
12620 if (VECTOR_TYPE_P (type0))
12622 ret = stv_secondarg;
12623 std::swap (type0, type1);
12624 std::swap (op0, op1);
12627 if (TREE_CODE (type0) == INTEGER_TYPE
12628 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12630 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12633 error_at (loc, "conversion of scalar %qT to vector %qT "
12634 "involves truncation", type0, type1);
12639 else if (!integer_only_op
12640 /* Allow integer --> real conversion if safe. */
12641 && (TREE_CODE (type0) == REAL_TYPE
12642 || TREE_CODE (type0) == INTEGER_TYPE)
12643 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12645 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12648 error_at (loc, "conversion of scalar %qT to vector %qT "
12649 "involves truncation", type0, type1);
12658 return stv_nothing;
12661 /* Return true iff ALIGN is an integral constant that is a fundamental
12662 alignment, as defined by [basic.align] in the c++-11
12667 [A fundamental alignment is represented by an alignment less than or
12668 equal to the greatest alignment supported by the implementation
12669 in all contexts, which is equal to
12670 alignof(max_align_t)]. */
12673 cxx_fundamental_alignment_p (unsigned align)
12675 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12676 TYPE_ALIGN (long_double_type_node)));
12679 /* Return true if T is a pointer to a zero-sized aggregate. */
12682 pointer_to_zero_sized_aggr_p (tree t)
12684 if (!POINTER_TYPE_P (t))
12687 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12690 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12691 with no library fallback or for an ADDR_EXPR whose operand is such type
12692 issues an error pointing to the location LOC.
12693 Returns true when the expression has been diagnosed and false
12696 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12698 if (TREE_CODE (expr) == ADDR_EXPR)
12699 expr = TREE_OPERAND (expr, 0);
12701 if (TREE_TYPE (expr)
12702 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12703 && TREE_CODE (expr) == FUNCTION_DECL
12704 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12705 false positives for user-declared built-ins such as abs or
12706 strlen, and for C++ operators new and delete.
12707 The c_decl_implicit() test avoids false positives for implicitly
12708 declared built-ins with library fallbacks (such as abs). */
12709 && DECL_BUILT_IN (expr)
12710 && DECL_IS_BUILTIN (expr)
12711 && !c_decl_implicit (expr)
12712 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12714 if (loc == UNKNOWN_LOCATION)
12715 loc = EXPR_LOC_OR_LOC (expr, input_location);
12717 /* Reject arguments that are built-in functions with
12718 no library fallback. */
12719 error_at (loc, "built-in function %qE must be directly called", expr);
12727 /* If we're creating an if-else-if condition chain, first see if we
12728 already have this COND in the CHAIN. If so, warn and don't add COND
12729 into the vector, otherwise add the COND there. LOC is the location
12733 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12735 /* No chain has been created yet. Do nothing. */
12736 if (*chain == NULL)
12739 if (TREE_SIDE_EFFECTS (cond))
12741 /* Uh-oh! This condition has a side-effect, thus invalidates
12742 the whole chain. */
12750 bool found = false;
12751 FOR_EACH_VEC_ELT (**chain, ix, t)
12752 if (operand_equal_p (cond, t, 0))
12754 if (warning_at (loc, OPT_Wduplicated_cond,
12755 "duplicated %<if%> condition"))
12756 inform (EXPR_LOCATION (t), "previously used here");
12762 && !CONSTANT_CLASS_P (cond)
12763 /* Don't infinitely grow the chain. */
12764 && (*chain)->length () < 512)
12765 (*chain)->safe_push (cond);
12768 /* Check if array size calculations overflow or if the array covers more
12769 than half of the address space. Return true if the size of the array
12770 is valid, false otherwise. TYPE is the type of the array and NAME is
12771 the name of the array, or NULL_TREE for unnamed arrays. */
12774 valid_array_size_p (location_t loc, tree type, tree name)
12776 if (type != error_mark_node
12777 && COMPLETE_TYPE_P (type)
12778 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12779 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
12782 error_at (loc, "size of array %qE is too large", name);
12784 error_at (loc, "size of unnamed array is too large");
12790 #include "gt-c-family-c-common.h"