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 tree c_global_trees[CTI_MAX];
162 /* Switches common to the C front ends. */
164 /* Nonzero means don't output line number information. */
166 char flag_no_line_commands;
168 /* Nonzero causes -E output not to be done, but directives such as
169 #define that have side effects are still obeyed. */
173 /* Nonzero means dump macros in some fashion. */
175 char flag_dump_macros;
177 /* Nonzero means pass #include lines through to the output. */
179 char flag_dump_includes;
181 /* Nonzero means process PCH files while preprocessing. */
183 bool flag_pch_preprocess;
185 /* The file name to which we should write a precompiled header, or
186 NULL if no header will be written in this compile. */
188 const char *pch_file;
190 /* Nonzero if an ISO standard was selected. It rejects macros in the
194 /* C/ObjC language option variables. */
197 /* Nonzero means allow type mismatches in conditional expressions;
198 just make their values `void'. */
200 int flag_cond_mismatch;
202 /* Nonzero means enable C89 Amendment 1 features. */
206 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
210 /* Nonzero means use the ISO C11 dialect of C. */
214 /* Nonzero means that we have builtin functions, and main is an int. */
219 /* ObjC language option variables. */
222 /* Tells the compiler that this is a special run. Do not perform any
223 compiling, instead we are to test some platform dependent features
224 and output a C header file with appropriate definitions. */
226 int print_struct_values;
228 /* Tells the compiler what is the constant string class for ObjC. */
230 const char *constant_string_class_name;
233 /* C++ language option variables. */
235 /* The reference version of the ABI for -Wabi. */
237 int warn_abi_version = -1;
239 /* Nonzero means generate separate instantiation control files and
240 juggle them at link time. */
242 int flag_use_repository;
244 /* The C++ dialect being used. Default set in c_common_post_options. */
246 enum cxx_dialect cxx_dialect = cxx_unset;
248 /* Maximum template instantiation depth. This limit exists to limit the
249 time it takes to notice excessively recursive template instantiations.
251 The default is lower than the 1024 recommended by the C++0x standard
252 because G++ runs out of stack before 1024 with highly recursive template
253 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
255 int max_tinst_depth = 900;
257 /* The elements of `ridpointers' are identifier nodes for the reserved
258 type names and storage classes. It is indexed by a RID_... value. */
261 tree (*make_fname_decl) (location_t, tree, int);
263 /* Nonzero means don't warn about problems that occur when the code is
265 int c_inhibit_evaluation_warnings;
267 /* Whether we are building a boolean conversion inside
268 convert_for_assignment, or some other late binary operation. If
269 build_binary_op is called for C (from code shared by C and C++) in
270 this case, then the operands have already been folded and the
271 result will not be folded again, so C_MAYBE_CONST_EXPR should not
273 bool in_late_binary_op;
275 /* Whether lexing has been completed, so subsequent preprocessor
276 errors should use the compiler's input_location. */
277 bool done_lexing = false;
279 /* Information about how a function name is generated. */
282 tree *const decl; /* pointer to the VAR_DECL. */
283 const unsigned rid; /* RID number for the identifier. */
284 const int pretty; /* How pretty is it? */
287 /* The three ways of getting then name of the current function. */
289 const struct fname_var_t fname_vars[] =
291 /* C99 compliant __func__, must be first. */
292 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
293 /* GCC __FUNCTION__ compliant. */
294 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
295 /* GCC __PRETTY_FUNCTION__ compliant. */
296 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
300 /* Global visibility options. */
301 struct visibility_flags visibility_options;
303 static tree check_case_value (location_t, tree);
304 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
307 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
312 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
313 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
315 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
317 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
319 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_always_inline_attribute (tree *, tree, tree, int,
326 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
334 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
336 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_transparent_union_attribute (tree *, tree, tree,
339 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
341 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
347 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
348 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
349 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
352 static tree handle_visibility_attribute (tree *, tree, tree, int,
354 static tree handle_tls_model_attribute (tree *, tree, tree, int,
356 static tree handle_no_instrument_function_attribute (tree *, tree,
358 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
362 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_deprecated_attribute (tree *, tree, tree, int,
368 static tree handle_vector_size_attribute (tree *, tree, tree, int,
370 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
375 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
383 static tree ignore_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
390 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
393 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
394 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
395 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
396 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
398 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
399 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
400 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
401 static int resort_field_decl_cmp (const void *, const void *);
403 /* Reserved words. The third field is a mask: keywords are disabled
404 if they match the mask.
407 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
408 C --std=c99: D_CXXONLY | D_OBJC
409 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
410 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
411 C++ --std=c0x: D_CONLY | D_OBJC
412 ObjC++ is like C++ except that D_OBJC is not set
414 If -fno-asm is used, D_ASM is added to the mask. If
415 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
416 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
417 In C with -Wc++-compat, we warn if D_CXXWARN is set.
419 Note the complication of the D_CXX_OBJC keywords. These are
420 reserved words such as 'class'. In C++, 'class' is a reserved
421 word. In Objective-C++ it is too. In Objective-C, it is a
422 reserved word too, but only if it follows an '@' sign.
424 const struct c_common_resword c_common_reswords[] =
426 { "_Alignas", RID_ALIGNAS, D_CONLY },
427 { "_Alignof", RID_ALIGNOF, D_CONLY },
428 { "_Atomic", RID_ATOMIC, D_CONLY },
429 { "_Bool", RID_BOOL, D_CONLY },
430 { "_Complex", RID_COMPLEX, 0 },
431 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
432 { "_Cilk_sync", RID_CILK_SYNC, 0 },
433 { "_Cilk_for", RID_CILK_FOR, 0 },
434 { "_Imaginary", RID_IMAGINARY, D_CONLY },
435 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
436 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
437 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
438 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
439 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
440 { "_Sat", RID_SAT, D_CONLY | D_EXT },
441 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
442 { "_Noreturn", RID_NORETURN, D_CONLY },
443 { "_Generic", RID_GENERIC, D_CONLY },
444 { "_Thread_local", RID_THREAD, D_CONLY },
445 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
446 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
447 { "__alignof", RID_ALIGNOF, 0 },
448 { "__alignof__", RID_ALIGNOF, 0 },
449 { "__asm", RID_ASM, 0 },
450 { "__asm__", RID_ASM, 0 },
451 { "__attribute", RID_ATTRIBUTE, 0 },
452 { "__attribute__", RID_ATTRIBUTE, 0 },
453 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
454 { "__bases", RID_BASES, D_CXXONLY },
455 { "__builtin_call_with_static_chain",
456 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
457 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
458 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
459 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
460 { "__builtin_offsetof", RID_OFFSETOF, 0 },
461 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
462 { "__builtin_va_arg", RID_VA_ARG, 0 },
463 { "__complex", RID_COMPLEX, 0 },
464 { "__complex__", RID_COMPLEX, 0 },
465 { "__const", RID_CONST, 0 },
466 { "__const__", RID_CONST, 0 },
467 { "__decltype", RID_DECLTYPE, D_CXXONLY },
468 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
469 { "__extension__", RID_EXTENSION, 0 },
470 { "__func__", RID_C99_FUNCTION_NAME, 0 },
471 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
472 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
473 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
474 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
475 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
476 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
477 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
478 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
479 { "__imag", RID_IMAGPART, 0 },
480 { "__imag__", RID_IMAGPART, 0 },
481 { "__inline", RID_INLINE, 0 },
482 { "__inline__", RID_INLINE, 0 },
483 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
484 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
485 { "__is_class", RID_IS_CLASS, D_CXXONLY },
486 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
487 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
488 { "__is_final", RID_IS_FINAL, D_CXXONLY },
489 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
490 { "__is_pod", RID_IS_POD, D_CXXONLY },
491 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
492 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
493 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
494 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
495 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
496 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
497 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
498 { "__is_union", RID_IS_UNION, D_CXXONLY },
499 { "__label__", RID_LABEL, 0 },
500 { "__null", RID_NULL, 0 },
501 { "__real", RID_REALPART, 0 },
502 { "__real__", RID_REALPART, 0 },
503 { "__restrict", RID_RESTRICT, 0 },
504 { "__restrict__", RID_RESTRICT, 0 },
505 { "__signed", RID_SIGNED, 0 },
506 { "__signed__", RID_SIGNED, 0 },
507 { "__thread", RID_THREAD, 0 },
508 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
509 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
510 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
511 { "__typeof", RID_TYPEOF, 0 },
512 { "__typeof__", RID_TYPEOF, 0 },
513 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
514 { "__volatile", RID_VOLATILE, 0 },
515 { "__volatile__", RID_VOLATILE, 0 },
516 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
517 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
518 { "asm", RID_ASM, D_ASM },
519 { "auto", RID_AUTO, 0 },
520 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
521 { "break", RID_BREAK, 0 },
522 { "case", RID_CASE, 0 },
523 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
524 { "char", RID_CHAR, 0 },
525 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
526 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
527 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
528 { "const", RID_CONST, 0 },
529 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
530 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
531 { "continue", RID_CONTINUE, 0 },
532 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
533 { "default", RID_DEFAULT, 0 },
534 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
536 { "double", RID_DOUBLE, 0 },
537 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
538 { "else", RID_ELSE, 0 },
539 { "enum", RID_ENUM, 0 },
540 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
541 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
542 { "extern", RID_EXTERN, 0 },
543 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
544 { "float", RID_FLOAT, 0 },
545 { "for", RID_FOR, 0 },
546 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
547 { "goto", RID_GOTO, 0 },
549 { "inline", RID_INLINE, D_EXT89 },
550 { "int", RID_INT, 0 },
551 { "long", RID_LONG, 0 },
552 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
553 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
554 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
555 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
556 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
557 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
558 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
559 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
560 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
561 { "register", RID_REGISTER, 0 },
562 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
563 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
564 { "return", RID_RETURN, 0 },
565 { "short", RID_SHORT, 0 },
566 { "signed", RID_SIGNED, 0 },
567 { "sizeof", RID_SIZEOF, 0 },
568 { "static", RID_STATIC, 0 },
569 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
570 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
571 { "struct", RID_STRUCT, 0 },
572 { "switch", RID_SWITCH, 0 },
573 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
574 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
575 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
576 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
577 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
578 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
579 { "typedef", RID_TYPEDEF, 0 },
580 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
581 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
582 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
583 { "union", RID_UNION, 0 },
584 { "unsigned", RID_UNSIGNED, 0 },
585 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
586 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
587 { "void", RID_VOID, 0 },
588 { "volatile", RID_VOLATILE, 0 },
589 { "wchar_t", RID_WCHAR, D_CXXONLY },
590 { "while", RID_WHILE, 0 },
592 /* C++ transactional memory. */
593 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
594 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
595 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
596 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
598 /* Concepts-related keywords */
599 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
600 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
602 /* These Objective-C keywords are recognized only immediately after
604 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
605 { "defs", RID_AT_DEFS, D_OBJC },
606 { "encode", RID_AT_ENCODE, D_OBJC },
607 { "end", RID_AT_END, D_OBJC },
608 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
609 { "interface", RID_AT_INTERFACE, D_OBJC },
610 { "protocol", RID_AT_PROTOCOL, D_OBJC },
611 { "selector", RID_AT_SELECTOR, D_OBJC },
612 { "finally", RID_AT_FINALLY, D_OBJC },
613 { "optional", RID_AT_OPTIONAL, D_OBJC },
614 { "required", RID_AT_REQUIRED, D_OBJC },
615 { "property", RID_AT_PROPERTY, D_OBJC },
616 { "package", RID_AT_PACKAGE, D_OBJC },
617 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
618 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
619 /* These are recognized only in protocol-qualifier context
621 { "bycopy", RID_BYCOPY, D_OBJC },
622 { "byref", RID_BYREF, D_OBJC },
623 { "in", RID_IN, D_OBJC },
624 { "inout", RID_INOUT, D_OBJC },
625 { "oneway", RID_ONEWAY, D_OBJC },
626 { "out", RID_OUT, D_OBJC },
627 /* These are recognized inside a property attribute list */
628 { "assign", RID_ASSIGN, D_OBJC },
629 { "copy", RID_COPY, D_OBJC },
630 { "getter", RID_GETTER, D_OBJC },
631 { "nonatomic", RID_NONATOMIC, D_OBJC },
632 { "readonly", RID_READONLY, D_OBJC },
633 { "readwrite", RID_READWRITE, D_OBJC },
634 { "retain", RID_RETAIN, D_OBJC },
635 { "setter", RID_SETTER, D_OBJC },
638 const unsigned int num_c_common_reswords =
639 sizeof c_common_reswords / sizeof (struct c_common_resword);
641 /* Table of machine-independent attributes common to all C-like languages.
643 All attributes referencing arguments should be additionally processed
644 in chkp_copy_function_type_adding_bounds for correct instrumentation
645 by Pointer Bounds Checker.
646 Current list of processed common attributes: nonnull. */
647 const struct attribute_spec c_common_attribute_table[] =
649 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
650 affects_type_identity } */
651 { "packed", 0, 0, false, false, false,
652 handle_packed_attribute , false},
653 { "nocommon", 0, 0, true, false, false,
654 handle_nocommon_attribute, false},
655 { "common", 0, 0, true, false, false,
656 handle_common_attribute, false },
657 /* FIXME: logically, noreturn attributes should be listed as
658 "false, true, true" and apply to function types. But implementing this
659 would require all the places in the compiler that use TREE_THIS_VOLATILE
660 on a decl to identify non-returning functions to be located and fixed
661 to check the function type instead. */
662 { "noreturn", 0, 0, true, false, false,
663 handle_noreturn_attribute, false },
664 { "volatile", 0, 0, true, false, false,
665 handle_noreturn_attribute, false },
666 { "stack_protect", 0, 0, true, false, false,
667 handle_stack_protect_attribute, false },
668 { "noinline", 0, 0, true, false, false,
669 handle_noinline_attribute, false },
670 { "noclone", 0, 0, true, false, false,
671 handle_noclone_attribute, false },
672 { "no_icf", 0, 0, true, false, false,
673 handle_noicf_attribute, false },
674 { "leaf", 0, 0, true, false, false,
675 handle_leaf_attribute, false },
676 { "always_inline", 0, 0, true, false, false,
677 handle_always_inline_attribute, false },
678 { "gnu_inline", 0, 0, true, false, false,
679 handle_gnu_inline_attribute, false },
680 { "artificial", 0, 0, true, false, false,
681 handle_artificial_attribute, false },
682 { "flatten", 0, 0, true, false, false,
683 handle_flatten_attribute, false },
684 { "used", 0, 0, true, false, false,
685 handle_used_attribute, false },
686 { "unused", 0, 0, false, false, false,
687 handle_unused_attribute, false },
688 { "externally_visible", 0, 0, true, false, false,
689 handle_externally_visible_attribute, false },
690 { "no_reorder", 0, 0, true, false, false,
691 handle_no_reorder_attribute, false },
692 /* The same comments as for noreturn attributes apply to const ones. */
693 { "const", 0, 0, true, false, false,
694 handle_const_attribute, false },
695 { "scalar_storage_order", 1, 1, false, false, false,
696 handle_scalar_storage_order_attribute, false },
697 { "transparent_union", 0, 0, false, false, false,
698 handle_transparent_union_attribute, false },
699 { "constructor", 0, 1, true, false, false,
700 handle_constructor_attribute, false },
701 { "destructor", 0, 1, true, false, false,
702 handle_destructor_attribute, false },
703 { "mode", 1, 1, false, true, false,
704 handle_mode_attribute, false },
705 { "section", 1, 1, true, false, false,
706 handle_section_attribute, false },
707 { "aligned", 0, 1, false, false, false,
708 handle_aligned_attribute, false },
709 { "weak", 0, 0, true, false, false,
710 handle_weak_attribute, false },
711 { "noplt", 0, 0, true, false, false,
712 handle_noplt_attribute, false },
713 { "ifunc", 1, 1, true, false, false,
714 handle_ifunc_attribute, false },
715 { "alias", 1, 1, true, false, false,
716 handle_alias_attribute, false },
717 { "weakref", 0, 1, true, false, false,
718 handle_weakref_attribute, false },
719 { "no_instrument_function", 0, 0, true, false, false,
720 handle_no_instrument_function_attribute,
722 { "malloc", 0, 0, true, false, false,
723 handle_malloc_attribute, false },
724 { "returns_twice", 0, 0, true, false, false,
725 handle_returns_twice_attribute, false },
726 { "no_stack_limit", 0, 0, true, false, false,
727 handle_no_limit_stack_attribute, false },
728 { "pure", 0, 0, true, false, false,
729 handle_pure_attribute, false },
730 { "transaction_callable", 0, 0, false, true, false,
731 handle_tm_attribute, false },
732 { "transaction_unsafe", 0, 0, false, true, false,
733 handle_tm_attribute, true },
734 { "transaction_safe", 0, 0, false, true, false,
735 handle_tm_attribute, true },
736 { "transaction_safe_dynamic", 0, 0, true, false, false,
737 handle_tm_attribute, false },
738 { "transaction_may_cancel_outer", 0, 0, false, true, false,
739 handle_tm_attribute, false },
740 /* ??? These two attributes didn't make the transition from the
741 Intel language document to the multi-vendor language document. */
742 { "transaction_pure", 0, 0, false, true, false,
743 handle_tm_attribute, false },
744 { "transaction_wrap", 1, 1, true, false, false,
745 handle_tm_wrap_attribute, false },
746 /* For internal use (marking of builtins) only. The name contains space
747 to prevent its usage in source code. */
748 { "no vops", 0, 0, true, false, false,
749 handle_novops_attribute, false },
750 { "deprecated", 0, 1, false, false, false,
751 handle_deprecated_attribute, false },
752 { "vector_size", 1, 1, false, true, false,
753 handle_vector_size_attribute, false },
754 { "visibility", 1, 1, false, false, false,
755 handle_visibility_attribute, false },
756 { "tls_model", 1, 1, true, false, false,
757 handle_tls_model_attribute, false },
758 { "nonnull", 0, -1, false, true, true,
759 handle_nonnull_attribute, false },
760 { "nothrow", 0, 0, true, false, false,
761 handle_nothrow_attribute, false },
762 { "may_alias", 0, 0, false, true, false, NULL, false },
763 { "cleanup", 1, 1, true, false, false,
764 handle_cleanup_attribute, false },
765 { "warn_unused_result", 0, 0, false, true, true,
766 handle_warn_unused_result_attribute, false },
767 { "sentinel", 0, 1, false, true, true,
768 handle_sentinel_attribute, false },
769 /* For internal use (marking of builtins) only. The name contains space
770 to prevent its usage in source code. */
771 { "type generic", 0, 0, false, true, true,
772 handle_type_generic_attribute, false },
773 { "alloc_size", 1, 2, false, true, true,
774 handle_alloc_size_attribute, false },
775 { "cold", 0, 0, true, false, false,
776 handle_cold_attribute, false },
777 { "hot", 0, 0, true, false, false,
778 handle_hot_attribute, false },
779 { "no_address_safety_analysis",
780 0, 0, true, false, false,
781 handle_no_address_safety_analysis_attribute,
783 { "no_sanitize_address", 0, 0, true, false, false,
784 handle_no_sanitize_address_attribute,
786 { "no_sanitize_thread", 0, 0, true, false, false,
787 handle_no_sanitize_address_attribute,
789 { "no_sanitize_undefined", 0, 0, true, false, false,
790 handle_no_sanitize_undefined_attribute,
792 { "warning", 1, 1, true, false, false,
793 handle_error_attribute, false },
794 { "error", 1, 1, true, false, false,
795 handle_error_attribute, false },
796 { "target", 1, -1, true, false, false,
797 handle_target_attribute, false },
798 { "target_clones", 1, -1, true, false, false,
799 handle_target_clones_attribute, false },
800 { "optimize", 1, -1, true, false, false,
801 handle_optimize_attribute, false },
802 /* For internal use only. The leading '*' both prevents its usage in
803 source code and signals that it may be overridden by machine tables. */
804 { "*tm regparm", 0, 0, false, true, true,
805 ignore_attribute, false },
806 { "no_split_stack", 0, 0, true, false, false,
807 handle_no_split_stack_attribute, false },
808 /* For internal use (marking of builtins and runtime functions) only.
809 The name contains space to prevent its usage in source code. */
810 { "fn spec", 1, 1, false, true, true,
811 handle_fnspec_attribute, false },
812 { "warn_unused", 0, 0, false, false, false,
813 handle_warn_unused_attribute, false },
814 { "returns_nonnull", 0, 0, false, true, true,
815 handle_returns_nonnull_attribute, false },
816 { "omp declare simd", 0, -1, true, false, false,
817 handle_omp_declare_simd_attribute, false },
818 { "cilk simd function", 0, -1, true, false, false,
819 handle_omp_declare_simd_attribute, false },
820 { "simd", 0, 1, true, false, false,
821 handle_simd_attribute, false },
822 { "omp declare target", 0, 0, true, false, false,
823 handle_omp_declare_target_attribute, false },
824 { "omp declare target link", 0, 0, true, false, false,
825 handle_omp_declare_target_attribute, false },
826 { "alloc_align", 1, 1, false, true, true,
827 handle_alloc_align_attribute, false },
828 { "assume_aligned", 1, 2, false, true, true,
829 handle_assume_aligned_attribute, false },
830 { "designated_init", 0, 0, false, true, false,
831 handle_designated_init_attribute, false },
832 { "bnd_variable_size", 0, 0, true, false, false,
833 handle_bnd_variable_size_attribute, false },
834 { "bnd_legacy", 0, 0, true, false, false,
835 handle_bnd_legacy, false },
836 { "bnd_instrument", 0, 0, true, false, false,
837 handle_bnd_instrument, false },
838 { NULL, 0, 0, false, false, false, NULL, false }
841 /* Give the specifications for the format attributes, used by C and all
844 All attributes referencing arguments should be additionally processed
845 in chkp_copy_function_type_adding_bounds for correct instrumentation
846 by Pointer Bounds Checker.
847 Current list of processed format attributes: format, format_arg. */
848 const struct attribute_spec c_common_format_attribute_table[] =
850 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
851 affects_type_identity } */
852 { "format", 3, 3, false, true, true,
853 handle_format_attribute, false },
854 { "format_arg", 1, 1, false, true, true,
855 handle_format_arg_attribute, false },
856 { NULL, 0, 0, false, false, false, NULL, false }
859 /* Return identifier for address space AS. */
862 c_addr_space_name (addr_space_t as)
864 int rid = RID_FIRST_ADDR_SPACE + as;
865 gcc_assert (ridpointers [rid]);
866 return IDENTIFIER_POINTER (ridpointers [rid]);
869 /* Push current bindings for the function name VAR_DECLS. */
872 start_fname_decls (void)
875 tree saved = NULL_TREE;
877 for (ix = 0; fname_vars[ix].decl; ix++)
879 tree decl = *fname_vars[ix].decl;
883 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
885 *fname_vars[ix].decl = NULL_TREE;
888 if (saved || saved_function_name_decls)
889 /* Normally they'll have been NULL, so only push if we've got a
890 stack, or they are non-NULL. */
891 saved_function_name_decls = tree_cons (saved, NULL_TREE,
892 saved_function_name_decls);
895 /* Finish up the current bindings, adding them into the current function's
896 statement tree. This must be done _before_ finish_stmt_tree is called.
897 If there is no current function, we must be at file scope and no statements
898 are involved. Pop the previous bindings. */
901 finish_fname_decls (void)
904 tree stmts = NULL_TREE;
905 tree stack = saved_function_name_decls;
907 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
908 append_to_statement_list (TREE_VALUE (stack), &stmts);
912 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
914 if (TREE_CODE (*bodyp) == BIND_EXPR)
915 bodyp = &BIND_EXPR_BODY (*bodyp);
917 append_to_statement_list_force (*bodyp, &stmts);
921 for (ix = 0; fname_vars[ix].decl; ix++)
922 *fname_vars[ix].decl = NULL_TREE;
926 /* We had saved values, restore them. */
929 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
931 tree decl = TREE_PURPOSE (saved);
932 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
934 *fname_vars[ix].decl = decl;
936 stack = TREE_CHAIN (stack);
938 saved_function_name_decls = stack;
941 /* Return the text name of the current function, suitably prettified
942 by PRETTY_P. Return string must be freed by caller. */
945 fname_as_string (int pretty_p)
947 const char *name = "top level";
950 cpp_string cstr = { 0, 0 }, strname;
958 if (current_function_decl)
959 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
961 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
963 namep = XNEWVEC (char, len);
964 snprintf (namep, len, "\"%s\"", name);
965 strname.text = (unsigned char *) namep;
966 strname.len = len - 1;
968 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
971 return (const char *) cstr.text;
977 /* Return the VAR_DECL for a const char array naming the current
978 function. If the VAR_DECL has not yet been created, create it
979 now. RID indicates how it should be formatted and IDENTIFIER_NODE
980 ID is its name (unfortunately C and C++ hold the RID values of
981 keywords in different places, so we can't derive RID from ID in
982 this language independent code. LOC is the location of the
986 fname_decl (location_t loc, unsigned int rid, tree id)
989 tree decl = NULL_TREE;
991 for (ix = 0; fname_vars[ix].decl; ix++)
992 if (fname_vars[ix].rid == rid)
995 decl = *fname_vars[ix].decl;
998 /* If a tree is built here, it would normally have the lineno of
999 the current statement. Later this tree will be moved to the
1000 beginning of the function and this line number will be wrong.
1001 To avoid this problem set the lineno to 0 here; that prevents
1002 it from appearing in the RTL. */
1004 location_t saved_location = input_location;
1005 input_location = UNKNOWN_LOCATION;
1007 stmts = push_stmt_list ();
1008 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1009 stmts = pop_stmt_list (stmts);
1010 if (!IS_EMPTY_STMT (stmts))
1011 saved_function_name_decls
1012 = tree_cons (decl, stmts, saved_function_name_decls);
1013 *fname_vars[ix].decl = decl;
1014 input_location = saved_location;
1016 if (!ix && !current_function_decl)
1017 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1022 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1025 fix_string_type (tree value)
1027 int length = TREE_STRING_LENGTH (value);
1029 tree e_type, i_type, a_type;
1031 /* Compute the number of elements, for the array type. */
1032 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1035 e_type = char_type_node;
1037 else if (TREE_TYPE (value) == char16_array_type_node)
1039 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1040 e_type = char16_type_node;
1042 else if (TREE_TYPE (value) == char32_array_type_node)
1044 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1045 e_type = char32_type_node;
1049 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1050 e_type = wchar_type_node;
1053 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1054 limit in C++98 Annex B is very large (65536) and is not normative,
1055 so we do not diagnose it (warn_overlength_strings is forced off
1056 in c_common_post_options). */
1057 if (warn_overlength_strings)
1059 const int nchars_max = flag_isoc99 ? 4095 : 509;
1060 const int relevant_std = flag_isoc99 ? 99 : 90;
1061 if (nchars - 1 > nchars_max)
1062 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1063 separate the %d from the 'C'. 'ISO' should not be
1064 translated, but it may be moved after 'C%d' in languages
1065 where modifiers follow nouns. */
1066 pedwarn (input_location, OPT_Woverlength_strings,
1067 "string length %qd is greater than the length %qd "
1068 "ISO C%d compilers are required to support",
1069 nchars - 1, nchars_max, relevant_std);
1072 /* Create the array type for the string constant. The ISO C++
1073 standard says that a string literal has type `const char[N]' or
1074 `const wchar_t[N]'. We use the same logic when invoked as a C
1075 front-end with -Wwrite-strings.
1076 ??? We should change the type of an expression depending on the
1077 state of a warning flag. We should just be warning -- see how
1078 this is handled in the C++ front-end for the deprecated implicit
1079 conversion from string literals to `char*' or `wchar_t*'.
1081 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1082 array type being the unqualified version of that type.
1083 Therefore, if we are constructing an array of const char, we must
1084 construct the matching unqualified array type first. The C front
1085 end does not require this, but it does no harm, so we do it
1087 i_type = build_index_type (size_int (nchars - 1));
1088 a_type = build_array_type (e_type, i_type);
1089 if (c_dialect_cxx() || warn_write_strings)
1090 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1092 TREE_TYPE (value) = a_type;
1093 TREE_CONSTANT (value) = 1;
1094 TREE_READONLY (value) = 1;
1095 TREE_STATIC (value) = 1;
1099 /* Fold X for consideration by one of the warning functions when checking
1100 whether an expression has a constant value. */
1103 fold_for_warn (tree x)
1105 if (c_dialect_cxx ())
1106 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1108 /* The C front-end has already folded X appropriately. */
1112 /* Print a warning if a constant expression had overflow in folding.
1113 Invoke this function on every expression that the language
1114 requires to be a constant expression.
1115 Note the ANSI C standard says it is erroneous for a
1116 constant expression to overflow. */
1119 constant_expression_warning (tree value)
1121 if (warn_overflow && pedantic
1122 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1123 || TREE_CODE (value) == FIXED_CST
1124 || TREE_CODE (value) == VECTOR_CST
1125 || TREE_CODE (value) == COMPLEX_CST)
1126 && TREE_OVERFLOW (value))
1127 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1130 /* The same as above but print an unconditional error. */
1132 constant_expression_error (tree value)
1134 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1135 || TREE_CODE (value) == FIXED_CST
1136 || TREE_CODE (value) == VECTOR_CST
1137 || TREE_CODE (value) == COMPLEX_CST)
1138 && TREE_OVERFLOW (value))
1139 error ("overflow in constant expression");
1142 /* Print a warning if an expression had overflow in folding and its
1145 Invoke this function on every expression that
1146 (1) appears in the source code, and
1147 (2) is a constant expression that overflowed, and
1148 (3) is not already checked by convert_and_check;
1149 however, do not invoke this function on operands of explicit casts
1150 or when the expression is the result of an operator and any operand
1151 already overflowed. */
1154 overflow_warning (location_t loc, tree value)
1156 if (c_inhibit_evaluation_warnings != 0)
1159 switch (TREE_CODE (value))
1162 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1166 warning_at (loc, OPT_Woverflow,
1167 "floating point overflow in expression");
1171 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1175 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1179 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1180 warning_at (loc, OPT_Woverflow,
1181 "complex integer overflow in expression");
1182 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1183 warning_at (loc, OPT_Woverflow,
1184 "complex floating point overflow in expression");
1192 /* Warn about uses of logical || / && operator in a context where it
1193 is likely that the bitwise equivalent was intended by the
1194 programmer. We have seen an expression in which CODE is a binary
1195 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1196 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1198 warn_logical_operator (location_t location, enum tree_code code, tree type,
1199 enum tree_code code_left, tree op_left,
1200 enum tree_code ARG_UNUSED (code_right), tree op_right)
1202 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1203 int in0_p, in1_p, in_p;
1204 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1205 bool strict_overflow_p = false;
1207 if (code != TRUTH_ANDIF_EXPR
1208 && code != TRUTH_AND_EXPR
1209 && code != TRUTH_ORIF_EXPR
1210 && code != TRUTH_OR_EXPR)
1213 /* We don't want to warn if either operand comes from a macro
1214 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1216 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1217 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1220 /* Warn if &&/|| are being used in a context where it is
1221 likely that the bitwise equivalent was intended by the
1222 programmer. That is, an expression such as op && MASK
1223 where op should not be any boolean expression, nor a
1224 constant, and mask seems to be a non-boolean integer constant. */
1225 if (TREE_CODE (op_right) == CONST_DECL)
1226 /* An enumerator counts as a constant. */
1227 op_right = DECL_INITIAL (op_right);
1228 if (!truth_value_p (code_left)
1229 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1230 && !CONSTANT_CLASS_P (op_left)
1231 && !TREE_NO_WARNING (op_left)
1232 && TREE_CODE (op_right) == INTEGER_CST
1233 && !integer_zerop (op_right)
1234 && !integer_onep (op_right))
1237 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1238 " applied to non-boolean constant");
1240 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1241 " applied to non-boolean constant");
1242 TREE_NO_WARNING (op_left) = true;
1246 /* We do not warn for constants because they are typical of macro
1247 expansions that test for features. */
1248 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1249 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
1252 /* This warning only makes sense with logical operands. */
1253 if (!(truth_value_p (TREE_CODE (op_left))
1254 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1255 || !(truth_value_p (TREE_CODE (op_right))
1256 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1259 /* The range computations only work with scalars. */
1260 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1261 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1264 /* We first test whether either side separately is trivially true
1265 (with OR) or trivially false (with AND). If so, do not warn.
1266 This is a common idiom for testing ranges of data types in
1268 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1271 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1272 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1274 /* If this is an OR operation, invert both sides; now, the result
1275 should be always false to get a warning. */
1279 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1280 if (tem && integer_zerop (tem))
1283 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1286 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1287 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1289 /* If this is an OR operation, invert both sides; now, the result
1290 should be always false to get a warning. */
1294 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1295 if (tem && integer_zerop (tem))
1298 /* If both expressions have the same operand, if we can merge the
1300 if (operand_equal_p (lhs, rhs, 0)
1301 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1302 in1_p, low1, high1))
1304 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1305 /* ... and if the range test is always false, then warn. */
1306 if (tem && integer_zerop (tem))
1309 warning_at (location, OPT_Wlogical_op,
1310 "logical %<or%> of collectively exhaustive tests is "
1313 warning_at (location, OPT_Wlogical_op,
1314 "logical %<and%> of mutually exclusive tests is "
1317 /* Or warn if the operands have exactly the same range, e.g.
1319 else if (tree_int_cst_equal (low0, low1)
1320 && tree_int_cst_equal (high0, high1))
1323 warning_at (location, OPT_Wlogical_op,
1324 "logical %<or%> of equal expressions");
1326 warning_at (location, OPT_Wlogical_op,
1327 "logical %<and%> of equal expressions");
1332 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1333 with constant indices. */
1336 find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1338 tree expr = *expr_p;
1340 if ((TREE_CODE (expr) == ARRAY_REF
1341 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1342 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1344 *(bool *) data = true;
1351 /* Warn if a self-comparison always evaluates to true or false. LOC
1352 is the location of the comparison with code CODE, LHS and RHS are
1353 operands of the comparison. */
1356 warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1358 if (TREE_CODE_CLASS (code) != tcc_comparison)
1361 /* Don't warn for various macro expansions. */
1362 if (from_macro_expansion_at (loc)
1363 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1364 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1367 /* We do not warn for constants because they are typical of macro
1368 expansions that test for features, sizeof, and similar. */
1369 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1370 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
1373 /* Don't warn for e.g.
1376 if (n == (long) n) ...
1378 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1379 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1382 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1383 It could be a NaN, and NaN never compares equal to anything, even
1385 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1388 if (operand_equal_p (lhs, rhs, 0))
1390 /* Don't warn about array references with constant indices;
1391 these are likely to come from a macro. */
1393 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1397 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1398 || code == GE_EXPR || code == UNLE_EXPR
1399 || code == UNGE_EXPR || code == UNEQ_EXPR);
1401 warning_at (loc, OPT_Wtautological_compare,
1402 "self-comparison always evaluates to true");
1404 warning_at (loc, OPT_Wtautological_compare,
1405 "self-comparison always evaluates to false");
1409 /* Warn about logical not used on the left hand side operand of a comparison.
1410 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1411 Do not warn if RHS is of a boolean type. */
1414 warn_logical_not_parentheses (location_t location, enum tree_code code,
1417 if (TREE_CODE_CLASS (code) != tcc_comparison
1418 || TREE_TYPE (rhs) == NULL_TREE
1419 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1422 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1423 !(x == 0) or !(y != 0). */
1424 if ((code == EQ_EXPR || code == NE_EXPR)
1425 && integer_zerop (rhs))
1428 warning_at (location, OPT_Wlogical_not_parentheses,
1429 "logical not is only applied to the left hand side of "
1433 /* Warn if EXP contains any computations whose results are not used.
1434 Return true if a warning is printed; false otherwise. LOCUS is the
1435 (potential) location of the expression. */
1438 warn_if_unused_value (const_tree exp, location_t locus)
1441 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1444 /* Don't warn about void constructs. This includes casting to void,
1445 void function calls, and statement expressions with a final cast
1447 if (VOID_TYPE_P (TREE_TYPE (exp)))
1450 if (EXPR_HAS_LOCATION (exp))
1451 locus = EXPR_LOCATION (exp);
1453 switch (TREE_CODE (exp))
1455 case PREINCREMENT_EXPR:
1456 case POSTINCREMENT_EXPR:
1457 case PREDECREMENT_EXPR:
1458 case POSTDECREMENT_EXPR:
1463 case TRY_CATCH_EXPR:
1464 case WITH_CLEANUP_EXPR:
1470 /* For a binding, warn if no side effect within it. */
1471 exp = BIND_EXPR_BODY (exp);
1475 case NON_LVALUE_EXPR:
1477 exp = TREE_OPERAND (exp, 0);
1480 case TRUTH_ORIF_EXPR:
1481 case TRUTH_ANDIF_EXPR:
1482 /* In && or ||, warn if 2nd operand has no side effect. */
1483 exp = TREE_OPERAND (exp, 1);
1487 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1489 /* Let people do `(foo (), 0)' without a warning. */
1490 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1492 exp = TREE_OPERAND (exp, 1);
1496 /* If this is an expression with side effects, don't warn; this
1497 case commonly appears in macro expansions. */
1498 if (TREE_SIDE_EFFECTS (exp))
1503 /* Don't warn about automatic dereferencing of references, since
1504 the user cannot control it. */
1505 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1507 exp = TREE_OPERAND (exp, 0);
1513 /* Referencing a volatile value is a side effect, so don't warn. */
1514 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1515 && TREE_THIS_VOLATILE (exp))
1518 /* If this is an expression which has no operands, there is no value
1519 to be unused. There are no such language-independent codes,
1520 but front ends may define such. */
1521 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1525 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1530 /* Print a warning about casts that might indicate violation
1531 of strict aliasing rules if -Wstrict-aliasing is used and
1532 strict aliasing mode is in effect. OTYPE is the original
1533 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1536 strict_aliasing_warning (tree otype, tree type, tree expr)
1538 /* Strip pointer conversion chains and get to the correct original type. */
1540 otype = TREE_TYPE (expr);
1542 if (!(flag_strict_aliasing
1543 && POINTER_TYPE_P (type)
1544 && POINTER_TYPE_P (otype)
1545 && !VOID_TYPE_P (TREE_TYPE (type)))
1546 /* If the type we are casting to is a ref-all pointer
1547 dereferencing it is always valid. */
1548 || TYPE_REF_CAN_ALIAS_ALL (type))
1551 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1552 && (DECL_P (TREE_OPERAND (expr, 0))
1553 || handled_component_p (TREE_OPERAND (expr, 0))))
1555 /* Casting the address of an object to non void pointer. Warn
1556 if the cast breaks type based aliasing. */
1557 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1559 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1560 "might break strict-aliasing rules");
1565 /* warn_strict_aliasing >= 3. This includes the default (3).
1566 Only warn if the cast is dereferenced immediately. */
1567 alias_set_type set1 =
1568 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1569 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1571 if (set1 != set2 && set2 != 0
1573 || (!alias_set_subset_of (set2, set1)
1574 && !alias_sets_conflict_p (set1, set2))))
1576 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1577 "pointer will break strict-aliasing rules");
1580 else if (warn_strict_aliasing == 2
1581 && !alias_sets_must_conflict_p (set1, set2))
1583 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1584 "pointer might break strict-aliasing rules");
1590 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1592 /* At this level, warn for any conversions, even if an address is
1593 not taken in the same statement. This will likely produce many
1594 false positives, but could be useful to pinpoint problems that
1595 are not revealed at higher levels. */
1596 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1597 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1598 if (!COMPLETE_TYPE_P (type)
1599 || !alias_sets_must_conflict_p (set1, set2))
1601 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1602 "pointer might break strict-aliasing rules");
1610 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1611 sizeof as last operand of certain builtins. */
1614 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1615 vec<tree, va_gc> *params, tree *sizeof_arg,
1616 bool (*comp_types) (tree, tree))
1618 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1619 bool strop = false, cmp = false;
1620 unsigned int idx = ~0;
1623 if (TREE_CODE (callee) != FUNCTION_DECL
1624 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1625 || vec_safe_length (params) <= 1)
1628 switch (DECL_FUNCTION_CODE (callee))
1630 case BUILT_IN_STRNCMP:
1631 case BUILT_IN_STRNCASECMP:
1634 case BUILT_IN_STRNCPY:
1635 case BUILT_IN_STRNCPY_CHK:
1636 case BUILT_IN_STRNCAT:
1637 case BUILT_IN_STRNCAT_CHK:
1638 case BUILT_IN_STPNCPY:
1639 case BUILT_IN_STPNCPY_CHK:
1642 case BUILT_IN_MEMCPY:
1643 case BUILT_IN_MEMCPY_CHK:
1644 case BUILT_IN_MEMMOVE:
1645 case BUILT_IN_MEMMOVE_CHK:
1646 if (params->length () < 3)
1649 dest = (*params)[0];
1652 case BUILT_IN_BCOPY:
1653 if (params->length () < 3)
1656 dest = (*params)[1];
1659 case BUILT_IN_MEMCMP:
1661 if (params->length () < 3)
1664 dest = (*params)[0];
1668 case BUILT_IN_MEMSET:
1669 case BUILT_IN_MEMSET_CHK:
1670 if (params->length () < 3)
1672 dest = (*params)[0];
1675 case BUILT_IN_BZERO:
1676 dest = (*params)[0];
1679 case BUILT_IN_STRNDUP:
1684 case BUILT_IN_MEMCHR:
1685 if (params->length () < 3)
1690 case BUILT_IN_SNPRINTF:
1691 case BUILT_IN_SNPRINTF_CHK:
1692 case BUILT_IN_VSNPRINTF:
1693 case BUILT_IN_VSNPRINTF_CHK:
1694 dest = (*params)[0];
1705 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1708 type = TYPE_P (sizeof_arg[idx])
1709 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1710 if (!POINTER_TYPE_P (type))
1714 && (tem = tree_strip_nop_conversions (dest))
1715 && POINTER_TYPE_P (TREE_TYPE (tem))
1716 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1720 && (tem = tree_strip_nop_conversions (src))
1721 && POINTER_TYPE_P (TREE_TYPE (tem))
1722 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1725 loc = sizeof_arg_loc[idx];
1729 if (!TYPE_P (sizeof_arg[idx])
1730 && operand_equal_p (dest, sizeof_arg[idx], 0)
1731 && comp_types (TREE_TYPE (dest), type))
1733 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1734 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1735 "argument to %<sizeof%> in %qD call is the same "
1736 "expression as the destination; did you mean to "
1737 "remove the addressof?", callee);
1738 else if ((TYPE_PRECISION (TREE_TYPE (type))
1739 == TYPE_PRECISION (char_type_node))
1741 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1742 "argument to %<sizeof%> in %qD call is the same "
1743 "expression as the destination; did you mean to "
1744 "provide an explicit length?", callee);
1746 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1747 "argument to %<sizeof%> in %qD call is the same "
1748 "expression as the destination; did you mean to "
1749 "dereference it?", callee);
1753 if (POINTER_TYPE_P (TREE_TYPE (dest))
1755 && comp_types (TREE_TYPE (dest), type)
1756 && !VOID_TYPE_P (TREE_TYPE (type)))
1758 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1759 "argument to %<sizeof%> in %qD call is the same "
1760 "pointer type %qT as the destination; expected %qT "
1761 "or an explicit length", callee, TREE_TYPE (dest),
1762 TREE_TYPE (TREE_TYPE (dest)));
1769 if (!TYPE_P (sizeof_arg[idx])
1770 && operand_equal_p (src, sizeof_arg[idx], 0)
1771 && comp_types (TREE_TYPE (src), type))
1773 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1774 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1775 "argument to %<sizeof%> in %qD call is the same "
1776 "expression as the source; did you mean to "
1777 "remove the addressof?", callee);
1778 else if ((TYPE_PRECISION (TREE_TYPE (type))
1779 == TYPE_PRECISION (char_type_node))
1781 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1782 "argument to %<sizeof%> in %qD call is the same "
1783 "expression as the source; did you mean to "
1784 "provide an explicit length?", callee);
1786 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1787 "argument to %<sizeof%> in %qD call is the same "
1788 "expression as the source; did you mean to "
1789 "dereference it?", callee);
1793 if (POINTER_TYPE_P (TREE_TYPE (src))
1795 && comp_types (TREE_TYPE (src), type)
1796 && !VOID_TYPE_P (TREE_TYPE (type)))
1798 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1799 "argument to %<sizeof%> in %qD call is the same "
1800 "pointer type %qT as the source; expected %qT "
1801 "or an explicit length", callee, TREE_TYPE (src),
1802 TREE_TYPE (TREE_TYPE (src)));
1809 if (!TYPE_P (sizeof_arg[idx])
1810 && operand_equal_p (dest, sizeof_arg[idx], 0)
1811 && comp_types (TREE_TYPE (dest), type))
1813 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1814 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1815 "argument to %<sizeof%> in %qD call is the same "
1816 "expression as the first source; did you mean to "
1817 "remove the addressof?", callee);
1818 else if ((TYPE_PRECISION (TREE_TYPE (type))
1819 == TYPE_PRECISION (char_type_node))
1821 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1822 "argument to %<sizeof%> in %qD call is the same "
1823 "expression as the first source; did you mean to "
1824 "provide an explicit length?", callee);
1826 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1827 "argument to %<sizeof%> in %qD call is the same "
1828 "expression as the first source; did you mean to "
1829 "dereference it?", callee);
1833 if (POINTER_TYPE_P (TREE_TYPE (dest))
1835 && comp_types (TREE_TYPE (dest), type)
1836 && !VOID_TYPE_P (TREE_TYPE (type)))
1838 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1839 "argument to %<sizeof%> in %qD call is the same "
1840 "pointer type %qT as the first source; expected %qT "
1841 "or an explicit length", callee, TREE_TYPE (dest),
1842 TREE_TYPE (TREE_TYPE (dest)));
1849 if (!TYPE_P (sizeof_arg[idx])
1850 && operand_equal_p (src, sizeof_arg[idx], 0)
1851 && comp_types (TREE_TYPE (src), type))
1853 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1854 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1855 "argument to %<sizeof%> in %qD call is the same "
1856 "expression as the second source; did you mean to "
1857 "remove the addressof?", callee);
1858 else if ((TYPE_PRECISION (TREE_TYPE (type))
1859 == TYPE_PRECISION (char_type_node))
1861 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1862 "argument to %<sizeof%> in %qD call is the same "
1863 "expression as the second source; did you mean to "
1864 "provide an explicit length?", callee);
1866 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1867 "argument to %<sizeof%> in %qD call is the same "
1868 "expression as the second source; did you mean to "
1869 "dereference it?", callee);
1873 if (POINTER_TYPE_P (TREE_TYPE (src))
1875 && comp_types (TREE_TYPE (src), type)
1876 && !VOID_TYPE_P (TREE_TYPE (type)))
1878 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1879 "argument to %<sizeof%> in %qD call is the same "
1880 "pointer type %qT as the second source; expected %qT "
1881 "or an explicit length", callee, TREE_TYPE (src),
1882 TREE_TYPE (TREE_TYPE (src)));
1889 /* Warn for unlikely, improbable, or stupid DECL declarations
1893 check_main_parameter_types (tree decl)
1895 function_args_iterator iter;
1899 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1901 /* XXX void_type_node belies the abstraction. */
1902 if (type == void_type_node || type == error_mark_node )
1906 if (TYPE_ATOMIC (t))
1907 pedwarn (input_location, OPT_Wmain,
1908 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1910 while (POINTER_TYPE_P (t))
1913 if (TYPE_ATOMIC (t))
1914 pedwarn (input_location, OPT_Wmain,
1915 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1923 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1924 pedwarn (input_location, OPT_Wmain,
1925 "first argument of %q+D should be %<int%>", decl);
1929 if (TREE_CODE (type) != POINTER_TYPE
1930 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1931 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1933 pedwarn (input_location, OPT_Wmain,
1934 "second argument of %q+D should be %<char **%>", decl);
1938 if (TREE_CODE (type) != POINTER_TYPE
1939 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1940 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1942 pedwarn (input_location, OPT_Wmain,
1943 "third argument of %q+D should probably be "
1944 "%<char **%>", decl);
1949 /* It is intentional that this message does not mention the third
1950 argument because it's only mentioned in an appendix of the
1952 if (argct > 0 && (argct < 2 || argct > 3))
1953 pedwarn (input_location, OPT_Wmain,
1954 "%q+D takes only zero or two arguments", decl);
1956 if (stdarg_p (TREE_TYPE (decl)))
1957 pedwarn (input_location, OPT_Wmain,
1958 "%q+D declared as variadic function", decl);
1961 /* vector_targets_convertible_p is used for vector pointer types. The
1962 callers perform various checks that the qualifiers are satisfactory,
1963 while OTOH vector_targets_convertible_p ignores the number of elements
1964 in the vectors. That's fine with vector pointers as we can consider,
1965 say, a vector of 8 elements as two consecutive vectors of 4 elements,
1966 and that does not require and conversion of the pointer values.
1967 In contrast, vector_types_convertible_p and
1968 vector_types_compatible_elements_p are used for vector value types. */
1969 /* True if pointers to distinct types T1 and T2 can be converted to
1970 each other without an explicit cast. Only returns true for opaque
1973 vector_targets_convertible_p (const_tree t1, const_tree t2)
1975 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
1976 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1977 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1983 /* vector_types_convertible_p is used for vector value types.
1984 It could in principle call vector_targets_convertible_p as a subroutine,
1985 but then the check for vector type would be duplicated with its callers,
1986 and also the purpose of vector_targets_convertible_p would become
1988 Where vector_types_convertible_p returns true, a conversion might still be
1989 needed to make the types match.
1990 In contrast, vector_targets_convertible_p is used for vector pointer
1991 values, and vector_types_compatible_elements_p is used specifically
1992 in the context for binary operators, as a check if use is possible without
1994 /* True if vector types T1 and T2 can be converted to each other
1995 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1996 can only be converted with -flax-vector-conversions yet that is not
1997 in effect, emit a note telling the user about that option if such
1998 a note has not previously been emitted. */
2000 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2002 static bool emitted_lax_note = false;
2003 bool convertible_lax;
2005 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2006 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2010 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2011 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2012 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2013 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2014 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2016 if (!convertible_lax || flag_lax_vector_conversions)
2017 return convertible_lax;
2019 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2020 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2023 if (emit_lax_note && !emitted_lax_note)
2025 emitted_lax_note = true;
2026 inform (input_location, "use -flax-vector-conversions to permit "
2027 "conversions between vectors with differing "
2028 "element types or numbers of subparts");
2034 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2035 and have vector types, V0 has the same type as V1, and the number of
2036 elements of V0, V1, MASK is the same.
2038 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2039 called with two arguments. In this case implementation passes the
2040 first argument twice in order to share the same tree code. This fact
2041 could enable the mask-values being twice the vector length. This is
2042 an implementation accident and this semantics is not guaranteed to
2045 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2050 bool maybe_const = false;
2051 bool two_arguments = false;
2053 if (v1 == NULL_TREE)
2055 two_arguments = true;
2059 if (v0 == error_mark_node || v1 == error_mark_node
2060 || mask == error_mark_node)
2061 return error_mark_node;
2063 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
2066 error_at (loc, "__builtin_shuffle last argument must "
2067 "be an integer vector");
2068 return error_mark_node;
2071 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2072 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
2075 error_at (loc, "__builtin_shuffle arguments must be vectors");
2076 return error_mark_node;
2079 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2082 error_at (loc, "__builtin_shuffle argument vectors must be of "
2084 return error_mark_node;
2087 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2088 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2089 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2090 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2093 error_at (loc, "__builtin_shuffle number of elements of the "
2094 "argument vector(s) and the mask vector should "
2096 return error_mark_node;
2099 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2100 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2103 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2104 "must have the same size as inner type of the mask");
2105 return error_mark_node;
2108 if (!c_dialect_cxx ())
2110 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2111 v0 = c_fully_fold (v0, false, &maybe_const);
2112 wrap &= maybe_const;
2115 v1 = v0 = save_expr (v0);
2118 v1 = c_fully_fold (v1, false, &maybe_const);
2119 wrap &= maybe_const;
2122 mask = c_fully_fold (mask, false, &maybe_const);
2123 wrap &= maybe_const;
2125 else if (two_arguments)
2126 v1 = v0 = save_expr (v0);
2128 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2130 if (!c_dialect_cxx () && !wrap)
2131 ret = c_wrap_maybe_const (ret, true);
2136 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2137 to integral type. */
2140 c_common_get_narrower (tree op, int *unsignedp_ptr)
2142 op = get_narrower (op, unsignedp_ptr);
2144 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2145 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2147 /* C++0x scoped enumerations don't implicitly convert to integral
2148 type; if we stripped an explicit conversion to a larger type we
2149 need to replace it so common_type will still work. */
2150 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2151 TYPE_UNSIGNED (TREE_TYPE (op)));
2152 op = fold_convert (type, op);
2157 /* This is a helper function of build_binary_op.
2159 For certain operations if both args were extended from the same
2160 smaller type, do the arithmetic in that type and then extend.
2162 BITWISE indicates a bitwise operation.
2163 For them, this optimization is safe only if
2164 both args are zero-extended or both are sign-extended.
2165 Otherwise, we might change the result.
2166 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2167 but calculated in (unsigned short) it would be (unsigned short)-1.
2170 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2172 int unsigned0, unsigned1;
2177 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2178 excessive narrowing when we call get_narrower below. For
2179 example, suppose that OP0 is of unsigned int extended
2180 from signed char and that RESULT_TYPE is long long int.
2181 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2184 (long long int) (unsigned int) signed_char
2186 which get_narrower would narrow down to
2188 (unsigned int) signed char
2190 If we do not cast OP0 first, get_narrower would return
2191 signed_char, which is inconsistent with the case of the
2193 op0 = convert (result_type, op0);
2194 op1 = convert (result_type, op1);
2196 arg0 = c_common_get_narrower (op0, &unsigned0);
2197 arg1 = c_common_get_narrower (op1, &unsigned1);
2199 /* UNS is 1 if the operation to be done is an unsigned one. */
2200 uns = TYPE_UNSIGNED (result_type);
2202 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2203 but it *requires* conversion to FINAL_TYPE. */
2205 if ((TYPE_PRECISION (TREE_TYPE (op0))
2206 == TYPE_PRECISION (TREE_TYPE (arg0)))
2207 && TREE_TYPE (op0) != result_type)
2208 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2209 if ((TYPE_PRECISION (TREE_TYPE (op1))
2210 == TYPE_PRECISION (TREE_TYPE (arg1)))
2211 && TREE_TYPE (op1) != result_type)
2212 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2214 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2216 /* For bitwise operations, signedness of nominal type
2217 does not matter. Consider only how operands were extended. */
2221 /* Note that in all three cases below we refrain from optimizing
2222 an unsigned operation on sign-extended args.
2223 That would not be valid. */
2225 /* Both args variable: if both extended in same way
2226 from same width, do it in that width.
2227 Do it unsigned if args were zero-extended. */
2228 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2229 < TYPE_PRECISION (result_type))
2230 && (TYPE_PRECISION (TREE_TYPE (arg1))
2231 == TYPE_PRECISION (TREE_TYPE (arg0)))
2232 && unsigned0 == unsigned1
2233 && (unsigned0 || !uns))
2234 return c_common_signed_or_unsigned_type
2235 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2237 else if (TREE_CODE (arg0) == INTEGER_CST
2238 && (unsigned1 || !uns)
2239 && (TYPE_PRECISION (TREE_TYPE (arg1))
2240 < TYPE_PRECISION (result_type))
2242 = c_common_signed_or_unsigned_type (unsigned1,
2244 && !POINTER_TYPE_P (type)
2245 && int_fits_type_p (arg0, type))
2248 else if (TREE_CODE (arg1) == INTEGER_CST
2249 && (unsigned0 || !uns)
2250 && (TYPE_PRECISION (TREE_TYPE (arg0))
2251 < TYPE_PRECISION (result_type))
2253 = c_common_signed_or_unsigned_type (unsigned0,
2255 && !POINTER_TYPE_P (type)
2256 && int_fits_type_p (arg1, type))
2262 /* Returns true iff any integer value of type FROM_TYPE can be represented as
2263 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2266 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2268 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2269 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2270 REAL_VALUE_TYPE real_low_bound =
2271 real_value_from_int_cst (0, type_low_bound);
2272 REAL_VALUE_TYPE real_high_bound =
2273 real_value_from_int_cst (0, type_high_bound);
2275 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2276 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2279 /* Checks if expression EXPR of complex/real/integer type cannot be converted
2280 to the complex/real/integer type TYPE. Function returns non-zero when:
2281 * EXPR is a constant which cannot be exactly converted to TYPE.
2282 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2283 for EXPR type and TYPE being both integers or both real, or both
2285 * EXPR is not a constant of complex type and TYPE is a real or
2287 * EXPR is not a constant of real type and TYPE is an integer.
2288 * EXPR is not a constant of integer type which cannot be
2289 exactly converted to real type.
2291 Function allows conversions between types of different signedness and
2292 can return SAFE_CONVERSION (zero) in that case. Function can produce
2293 signedness warnings if PRODUCE_WARNS is true.
2295 Function allows conversions from complex constants to non-complex types,
2296 provided that imaginary part is zero and real part can be safely converted
2299 enum conversion_safety
2300 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2302 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2303 tree expr_type = TREE_TYPE (expr);
2304 loc = expansion_point_location_if_in_system_header (loc);
2306 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2308 /* If type is complex, we are interested in compatibility with
2310 if (TREE_CODE (type) == COMPLEX_TYPE)
2311 type = TREE_TYPE (type);
2313 /* Warn for real constant that is not an exact integer converted
2315 if (TREE_CODE (expr_type) == REAL_TYPE
2316 && TREE_CODE (type) == INTEGER_TYPE)
2318 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2319 give_warning = UNSAFE_REAL;
2321 /* Warn for an integer constant that does not fit into integer type. */
2322 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2323 && TREE_CODE (type) == INTEGER_TYPE
2324 && !int_fits_type_p (expr, type))
2326 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2327 && tree_int_cst_sgn (expr) < 0)
2330 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2331 " implicitly converted to unsigned type");
2333 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2336 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2337 " constant value to negative integer");
2340 give_warning = UNSAFE_OTHER;
2342 else if (TREE_CODE (type) == REAL_TYPE)
2344 /* Warn for an integer constant that does not fit into real type. */
2345 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2347 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2348 if (!exact_real_truncate (TYPE_MODE (type), &a))
2349 give_warning = UNSAFE_REAL;
2351 /* Warn for a real constant that does not fit into a smaller
2353 else if (TREE_CODE (expr_type) == REAL_TYPE
2354 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2356 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2357 if (!exact_real_truncate (TYPE_MODE (type), &a))
2358 give_warning = UNSAFE_REAL;
2363 else if (TREE_CODE (expr) == COMPLEX_CST)
2365 tree imag_part = TREE_IMAGPART (expr);
2366 /* Conversion from complex constant with zero imaginary part,
2367 perform check for conversion of real part. */
2368 if ((TREE_CODE (imag_part) == REAL_CST
2369 && real_zerop (imag_part))
2370 || (TREE_CODE (imag_part) == INTEGER_CST
2371 && integer_zerop (imag_part)))
2372 /* Note: in this branch we use recursive call to unsafe_conversion_p
2373 with different type of EXPR, but it is still safe, because when EXPR
2374 is a constant, it's type is not used in text of generated warnings
2375 (otherwise they could sound misleading). */
2376 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2378 /* Conversion from complex constant with non-zero imaginary part. */
2381 /* Conversion to complex type.
2382 Perform checks for both real and imaginary parts. */
2383 if (TREE_CODE (type) == COMPLEX_TYPE)
2385 /* Unfortunately, produce_warns must be false in two subsequent
2386 calls of unsafe_conversion_p, because otherwise we could
2387 produce strange "double" warnings, if both real and imaginary
2388 parts have conversion problems related to signedness.
2391 int32_t _Complex a = 0x80000000 + 0x80000000i;
2393 Possible solution: add a separate function for checking
2394 constants and combine result of two calls appropriately. */
2395 enum conversion_safety re_safety =
2396 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2397 enum conversion_safety im_safety =
2398 unsafe_conversion_p (loc, type, imag_part, false);
2400 /* Merge the results into appropriate single warning. */
2402 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2403 if (re_safety == im_safety)
2404 give_warning = re_safety;
2405 else if (!re_safety && im_safety)
2406 give_warning = im_safety;
2407 else if (re_safety && !im_safety)
2408 give_warning = re_safety;
2410 give_warning = UNSAFE_OTHER;
2412 /* Warn about conversion from complex to real or integer type. */
2414 give_warning = UNSAFE_IMAGINARY;
2418 /* Checks for remaining case: EXPR is not constant. */
2421 /* Warn for real types converted to integer types. */
2422 if (TREE_CODE (expr_type) == REAL_TYPE
2423 && TREE_CODE (type) == INTEGER_TYPE)
2424 give_warning = UNSAFE_REAL;
2426 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2427 && TREE_CODE (type) == INTEGER_TYPE)
2429 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2430 expr = get_unwidened (expr, 0);
2431 expr_type = TREE_TYPE (expr);
2433 /* Don't warn for short y; short x = ((int)y & 0xff); */
2434 if (TREE_CODE (expr) == BIT_AND_EXPR
2435 || TREE_CODE (expr) == BIT_IOR_EXPR
2436 || TREE_CODE (expr) == BIT_XOR_EXPR)
2438 /* If both args were extended from a shortest type,
2439 use that type if that is safe. */
2440 expr_type = shorten_binary_op (expr_type,
2441 TREE_OPERAND (expr, 0),
2442 TREE_OPERAND (expr, 1),
2445 if (TREE_CODE (expr) == BIT_AND_EXPR)
2447 tree op0 = TREE_OPERAND (expr, 0);
2448 tree op1 = TREE_OPERAND (expr, 1);
2449 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2450 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2452 /* If one of the operands is a non-negative constant
2453 that fits in the target type, then the type of the
2454 other operand does not matter. */
2455 if ((TREE_CODE (op0) == INTEGER_CST
2456 && int_fits_type_p (op0, c_common_signed_type (type))
2457 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2458 || (TREE_CODE (op1) == INTEGER_CST
2459 && int_fits_type_p (op1, c_common_signed_type (type))
2460 && int_fits_type_p (op1,
2461 c_common_unsigned_type (type))))
2462 return SAFE_CONVERSION;
2463 /* If constant is unsigned and fits in the target
2464 type, then the result will also fit. */
2465 else if ((TREE_CODE (op0) == INTEGER_CST
2467 && int_fits_type_p (op0, type))
2468 || (TREE_CODE (op1) == INTEGER_CST
2470 && int_fits_type_p (op1, type)))
2471 return SAFE_CONVERSION;
2474 /* Warn for integer types converted to smaller integer types. */
2475 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2476 give_warning = UNSAFE_OTHER;
2478 /* When they are the same width but different signedness,
2479 then the value may change. */
2480 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2481 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2482 /* Even when converted to a bigger type, if the type is
2483 unsigned but expr is signed, then negative values
2485 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2487 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2488 "may change the sign of the result",
2492 /* Warn for integer types converted to real types if and only if
2493 all the range of values of the integer type cannot be
2494 represented by the real type. */
2495 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2496 && TREE_CODE (type) == REAL_TYPE)
2498 /* Don't warn about char y = 0xff; float x = (int) y; */
2499 expr = get_unwidened (expr, 0);
2500 expr_type = TREE_TYPE (expr);
2502 if (!int_safely_convertible_to_real_p (expr_type, type))
2503 give_warning = UNSAFE_OTHER;
2506 /* Warn for real types converted to smaller real types. */
2507 else if (TREE_CODE (expr_type) == REAL_TYPE
2508 && TREE_CODE (type) == REAL_TYPE
2509 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2510 give_warning = UNSAFE_REAL;
2512 /* Check conversion between two complex types. */
2513 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2514 && TREE_CODE (type) == COMPLEX_TYPE)
2516 /* Extract underlying types (i.e., type of real and imaginary
2517 parts) of expr_type and type. */
2518 tree from_type = TREE_TYPE (expr_type);
2519 tree to_type = TREE_TYPE (type);
2521 /* Warn for real types converted to integer types. */
2522 if (TREE_CODE (from_type) == REAL_TYPE
2523 && TREE_CODE (to_type) == INTEGER_TYPE)
2524 give_warning = UNSAFE_REAL;
2526 /* Warn for real types converted to smaller real types. */
2527 else if (TREE_CODE (from_type) == REAL_TYPE
2528 && TREE_CODE (to_type) == REAL_TYPE
2529 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2530 give_warning = UNSAFE_REAL;
2532 /* Check conversion for complex integer types. Here implementation
2533 is simpler than for real-domain integers because it does not
2534 involve sophisticated cases, such as bitmasks, casts, etc. */
2535 else if (TREE_CODE (from_type) == INTEGER_TYPE
2536 && TREE_CODE (to_type) == INTEGER_TYPE)
2538 /* Warn for integer types converted to smaller integer types. */
2539 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2540 give_warning = UNSAFE_OTHER;
2542 /* Check for different signedness, see case for real-domain
2543 integers (above) for a more detailed comment. */
2544 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2545 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2546 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2548 warning_at (loc, OPT_Wsign_conversion,
2549 "conversion to %qT from %qT "
2550 "may change the sign of the result",
2553 else if (TREE_CODE (from_type) == INTEGER_TYPE
2554 && TREE_CODE (to_type) == REAL_TYPE
2555 && !int_safely_convertible_to_real_p (from_type, to_type))
2556 give_warning = UNSAFE_OTHER;
2559 /* Warn for complex types converted to real or integer types. */
2560 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2561 && TREE_CODE (type) != COMPLEX_TYPE)
2562 give_warning = UNSAFE_IMAGINARY;
2565 return give_warning;
2568 /* Warns if the conversion of EXPR to TYPE may alter a value.
2569 This is a helper function for warnings_for_convert_and_check. */
2572 conversion_warning (location_t loc, tree type, tree expr)
2574 tree expr_type = TREE_TYPE (expr);
2575 enum conversion_safety conversion_kind;
2577 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2580 /* This may happen, because for LHS op= RHS we preevaluate
2581 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2582 means we could no longer see the code of the EXPR. */
2583 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2584 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2585 if (TREE_CODE (expr) == SAVE_EXPR)
2586 expr = TREE_OPERAND (expr, 0);
2588 switch (TREE_CODE (expr))
2596 case TRUTH_ANDIF_EXPR:
2597 case TRUTH_ORIF_EXPR:
2598 case TRUTH_AND_EXPR:
2600 case TRUTH_XOR_EXPR:
2601 case TRUTH_NOT_EXPR:
2602 /* Conversion from boolean to a signed:1 bit-field (which only
2603 can hold the values 0 and -1) doesn't lose information - but
2604 it does change the value. */
2605 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2606 warning_at (loc, OPT_Wconversion,
2607 "conversion to %qT from boolean expression", type);
2613 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2614 if (conversion_kind == UNSAFE_REAL)
2615 warning_at (loc, OPT_Wfloat_conversion,
2616 "conversion to %qT alters %qT constant value",
2618 else if (conversion_kind)
2619 warning_at (loc, OPT_Wconversion,
2620 "conversion to %qT alters %qT constant value",
2626 /* In case of COND_EXPR, we do not care about the type of
2627 COND_EXPR, only about the conversion of each operand. */
2628 tree op1 = TREE_OPERAND (expr, 1);
2629 tree op2 = TREE_OPERAND (expr, 2);
2631 conversion_warning (loc, type, op1);
2632 conversion_warning (loc, type, op2);
2636 default: /* 'expr' is not a constant. */
2637 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2638 if (conversion_kind == UNSAFE_REAL)
2639 warning_at (loc, OPT_Wfloat_conversion,
2640 "conversion to %qT from %qT may alter its value",
2642 else if (conversion_kind == UNSAFE_IMAGINARY)
2643 warning_at (loc, OPT_Wconversion,
2644 "conversion to %qT from %qT discards imaginary component",
2646 else if (conversion_kind)
2647 warning_at (loc, OPT_Wconversion,
2648 "conversion to %qT from %qT may alter its value",
2653 /* Produce warnings after a conversion. RESULT is the result of
2654 converting EXPR to TYPE. This is a helper function for
2655 convert_and_check and cp_convert_and_check. */
2658 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2661 loc = expansion_point_location_if_in_system_header (loc);
2663 if (TREE_CODE (expr) == INTEGER_CST
2664 && (TREE_CODE (type) == INTEGER_TYPE
2665 || TREE_CODE (type) == ENUMERAL_TYPE)
2666 && !int_fits_type_p (expr, type))
2668 /* Do not diagnose overflow in a constant expression merely
2669 because a conversion overflowed. */
2670 if (TREE_OVERFLOW (result))
2671 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2673 if (TYPE_UNSIGNED (type))
2675 /* This detects cases like converting -129 or 256 to
2677 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2678 warning_at (loc, OPT_Woverflow,
2679 "large integer implicitly truncated to unsigned type");
2681 conversion_warning (loc, type, expr);
2683 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2684 warning_at (loc, OPT_Woverflow,
2685 "overflow in implicit constant conversion");
2686 /* No warning for converting 0x80000000 to int. */
2688 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2689 || TYPE_PRECISION (TREE_TYPE (expr))
2690 != TYPE_PRECISION (type)))
2691 warning_at (loc, OPT_Woverflow,
2692 "overflow in implicit constant conversion");
2695 conversion_warning (loc, type, expr);
2697 else if ((TREE_CODE (result) == INTEGER_CST
2698 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2699 warning_at (loc, OPT_Woverflow,
2700 "overflow in implicit constant conversion");
2702 conversion_warning (loc, type, expr);
2706 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2707 Invoke this function on every expression that is converted implicitly,
2708 i.e. because of language rules and not because of an explicit cast. */
2711 convert_and_check (location_t loc, tree type, tree expr)
2714 tree expr_for_warning;
2716 /* Convert from a value with possible excess precision rather than
2717 via the semantic type, but do not warn about values not fitting
2718 exactly in the semantic type. */
2719 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2721 tree orig_type = TREE_TYPE (expr);
2722 expr = TREE_OPERAND (expr, 0);
2723 expr_for_warning = convert (orig_type, expr);
2724 if (orig_type == type)
2725 return expr_for_warning;
2728 expr_for_warning = expr;
2730 if (TREE_TYPE (expr) == type)
2733 result = convert (type, expr);
2735 if (c_inhibit_evaluation_warnings == 0
2736 && !TREE_OVERFLOW_P (expr)
2737 && result != error_mark_node)
2738 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2743 /* A node in a list that describes references to variables (EXPR), which are
2744 either read accesses if WRITER is zero, or write accesses, in which case
2745 WRITER is the parent of EXPR. */
2752 /* Used to implement a cache the results of a call to verify_tree. We only
2753 use this for SAVE_EXPRs. */
2756 struct tlist_cache *next;
2757 struct tlist *cache_before_sp;
2758 struct tlist *cache_after_sp;
2762 /* Obstack to use when allocating tlist structures, and corresponding
2764 static struct obstack tlist_obstack;
2765 static char *tlist_firstobj = 0;
2767 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2769 static struct tlist *warned_ids;
2770 /* SAVE_EXPRs need special treatment. We process them only once and then
2771 cache the results. */
2772 static struct tlist_cache *save_expr_cache;
2774 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2775 static void merge_tlist (struct tlist **, struct tlist *, int);
2776 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2777 static int warning_candidate_p (tree);
2778 static bool candidate_equal_p (const_tree, const_tree);
2779 static void warn_for_collisions (struct tlist *);
2780 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2781 static struct tlist *new_tlist (struct tlist *, tree, tree);
2783 /* Create a new struct tlist and fill in its fields. */
2784 static struct tlist *
2785 new_tlist (struct tlist *next, tree t, tree writer)
2788 l = XOBNEW (&tlist_obstack, struct tlist);
2795 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2796 is nonnull, we ignore any node we find which has a writer equal to it. */
2799 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2803 struct tlist *next = add->next;
2806 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2807 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2812 /* Merge the nodes of ADD into TO. This merging process is done so that for
2813 each variable that already exists in TO, no new node is added; however if
2814 there is a write access recorded in ADD, and an occurrence on TO is only
2815 a read access, then the occurrence in TO will be modified to record the
2819 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2821 struct tlist **end = to;
2824 end = &(*end)->next;
2830 struct tlist *next = add->next;
2832 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2833 if (candidate_equal_p (tmp2->expr, add->expr))
2837 tmp2->writer = add->writer;
2841 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
2842 end = &(*end)->next;
2849 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2850 references in list LIST conflict with it, excluding reads if ONLY writers
2854 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2859 /* Avoid duplicate warnings. */
2860 for (tmp = warned_ids; tmp; tmp = tmp->next)
2861 if (candidate_equal_p (tmp->expr, written))
2866 if (candidate_equal_p (list->expr, written)
2867 && !candidate_equal_p (list->writer, writer)
2868 && (!only_writes || list->writer))
2870 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2871 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
2872 OPT_Wsequence_point, "operation on %qE may be undefined",
2879 /* Given a list LIST of references to variables, find whether any of these
2880 can cause conflicts due to missing sequence points. */
2883 warn_for_collisions (struct tlist *list)
2887 for (tmp = list; tmp; tmp = tmp->next)
2890 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2894 /* Return nonzero if X is a tree that can be verified by the sequence point
2897 warning_candidate_p (tree x)
2899 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2902 if (TREE_CODE (x) == BLOCK)
2905 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2906 (lvalue_p) crash on TRY/CATCH. */
2907 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2913 /* No point to track non-const calls, they will never satisfy
2915 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2918 if (TREE_CODE (x) == STRING_CST)
2924 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2926 candidate_equal_p (const_tree x, const_tree y)
2928 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2931 /* Walk the tree X, and record accesses to variables. If X is written by the
2932 parent tree, WRITER is the parent.
2933 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2934 expression or its only operand forces a sequence point, then everything up
2935 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2937 Once we return, we will have emitted warnings if any subexpression before
2938 such a sequence point could be undefined. On a higher level, however, the
2939 sequence point may not be relevant, and we'll merge the two lists.
2941 Example: (b++, a) + b;
2942 The call that processes the COMPOUND_EXPR will store the increment of B
2943 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2944 processes the PLUS_EXPR will need to merge the two lists so that
2945 eventually, all accesses end up on the same list (and we'll warn about the
2946 unordered subexpressions b++ and b.
2948 A note on merging. If we modify the former example so that our expression
2951 care must be taken not simply to add all three expressions into the final
2952 PNO_SP list. The function merge_tlist takes care of that by merging the
2953 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2954 way, so that no more than one access to B is recorded. */
2957 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2960 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2961 enum tree_code code;
2962 enum tree_code_class cl;
2964 /* X may be NULL if it is the operand of an empty statement expression
2970 code = TREE_CODE (x);
2971 cl = TREE_CODE_CLASS (code);
2973 if (warning_candidate_p (x))
2974 *pno_sp = new_tlist (*pno_sp, x, writer);
2983 case TRUTH_ANDIF_EXPR:
2984 case TRUTH_ORIF_EXPR:
2985 tmp_before = tmp_nosp = tmp_list3 = 0;
2986 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2987 warn_for_collisions (tmp_nosp);
2988 merge_tlist (pbefore_sp, tmp_before, 0);
2989 merge_tlist (pbefore_sp, tmp_nosp, 0);
2990 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2991 merge_tlist (pbefore_sp, tmp_list3, 0);
2995 tmp_before = tmp_list2 = 0;
2996 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2997 warn_for_collisions (tmp_list2);
2998 merge_tlist (pbefore_sp, tmp_before, 0);
2999 merge_tlist (pbefore_sp, tmp_list2, 0);
3001 tmp_list3 = tmp_nosp = 0;
3002 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3003 warn_for_collisions (tmp_nosp);
3004 merge_tlist (pbefore_sp, tmp_list3, 0);
3006 tmp_list3 = tmp_list2 = 0;
3007 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3008 warn_for_collisions (tmp_list2);
3009 merge_tlist (pbefore_sp, tmp_list3, 0);
3010 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3011 two first, to avoid warning for (a ? b++ : b++). */
3012 merge_tlist (&tmp_nosp, tmp_list2, 0);
3013 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3016 case PREDECREMENT_EXPR:
3017 case PREINCREMENT_EXPR:
3018 case POSTDECREMENT_EXPR:
3019 case POSTINCREMENT_EXPR:
3020 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3024 tmp_before = tmp_nosp = tmp_list3 = 0;
3025 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3026 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3027 /* Expressions inside the LHS are not ordered wrt. the sequence points
3028 in the RHS. Example:
3030 Despite the fact that the modification of "a" is in the before_sp
3031 list (tmp_before), it conflicts with the use of "a" in the LHS.
3032 We can handle this by adding the contents of tmp_list3
3033 to those of tmp_before, and redoing the collision warnings for that
3035 add_tlist (&tmp_before, tmp_list3, x, 1);
3036 warn_for_collisions (tmp_before);
3037 /* Exclude the LHS itself here; we first have to merge it into the
3038 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3039 didn't exclude the LHS, we'd get it twice, once as a read and once
3041 add_tlist (pno_sp, tmp_list3, x, 0);
3042 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3044 merge_tlist (pbefore_sp, tmp_before, 0);
3045 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3046 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3047 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3051 /* We need to warn about conflicts among arguments and conflicts between
3052 args and the function address. Side effects of the function address,
3053 however, are not ordered by the sequence point of the call. */
3055 call_expr_arg_iterator iter;
3057 tmp_before = tmp_nosp = 0;
3058 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3059 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3061 tmp_list2 = tmp_list3 = 0;
3062 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3063 merge_tlist (&tmp_list3, tmp_list2, 0);
3064 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3066 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3067 warn_for_collisions (tmp_before);
3068 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3073 /* Scan all the list, e.g. indices of multi dimensional array. */
3076 tmp_before = tmp_nosp = 0;
3077 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3078 merge_tlist (&tmp_nosp, tmp_before, 0);
3079 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3086 struct tlist_cache *t;
3087 for (t = save_expr_cache; t; t = t->next)
3088 if (candidate_equal_p (t->expr, x))
3093 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3094 t->next = save_expr_cache;
3096 save_expr_cache = t;
3098 tmp_before = tmp_nosp = 0;
3099 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3100 warn_for_collisions (tmp_nosp);
3103 merge_tlist (&tmp_list3, tmp_nosp, 0);
3104 t->cache_before_sp = tmp_before;
3105 t->cache_after_sp = tmp_list3;
3107 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3108 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3113 x = TREE_OPERAND (x, 0);
3120 /* For other expressions, simply recurse on their operands.
3121 Manual tail recursion for unary expressions.
3122 Other non-expressions need not be processed. */
3123 if (cl == tcc_unary)
3125 x = TREE_OPERAND (x, 0);
3129 else if (IS_EXPR_CODE_CLASS (cl))
3132 int max = TREE_OPERAND_LENGTH (x);
3133 for (lp = 0; lp < max; lp++)
3135 tmp_before = tmp_nosp = 0;
3136 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3137 merge_tlist (&tmp_nosp, tmp_before, 0);
3138 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3145 /* Try to warn for undefined behavior in EXPR due to missing sequence
3149 verify_sequence_points (tree expr)
3151 struct tlist *before_sp = 0, *after_sp = 0;
3154 save_expr_cache = 0;
3155 if (tlist_firstobj == 0)
3157 gcc_obstack_init (&tlist_obstack);
3158 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3161 verify_tree (expr, &before_sp, &after_sp, 0);
3162 warn_for_collisions (after_sp);
3163 obstack_free (&tlist_obstack, tlist_firstobj);
3166 /* Validate the expression after `case' and apply default promotions. */
3169 check_case_value (location_t loc, tree value)
3171 if (value == NULL_TREE)
3174 if (TREE_CODE (value) == INTEGER_CST)
3175 /* Promote char or short to int. */
3176 value = perform_integral_promotions (value);
3177 else if (value != error_mark_node)
3179 error_at (loc, "case label does not reduce to an integer constant");
3180 value = error_mark_node;
3183 constant_expression_warning (value);
3188 /* See if the case values LOW and HIGH are in the range of the original
3189 type (i.e. before the default conversion to int) of the switch testing
3191 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3192 the type before promoting it. CASE_LOW_P is a pointer to the lower
3193 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3194 if the case is not a case range.
3195 The caller has to make sure that we are not called with NULL for
3196 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3197 was a case value that doesn't fit into the range of the ORIG_TYPE.
3198 Returns true if the case label is in range of ORIG_TYPE (saturated or
3199 untouched) or false if the label is out of range. */
3202 check_case_bounds (location_t loc, tree type, tree orig_type,
3203 tree *case_low_p, tree *case_high_p,
3204 bool *outside_range_p)
3206 tree min_value, max_value;
3207 tree case_low = *case_low_p;
3208 tree case_high = case_high_p ? *case_high_p : case_low;
3210 /* If there was a problem with the original type, do nothing. */
3211 if (orig_type == error_mark_node)
3214 min_value = TYPE_MIN_VALUE (orig_type);
3215 max_value = TYPE_MAX_VALUE (orig_type);
3217 /* We'll really need integer constants here. */
3218 case_low = fold (case_low);
3219 case_high = fold (case_high);
3221 /* Case label is less than minimum for type. */
3222 if (tree_int_cst_compare (case_low, min_value) < 0
3223 && tree_int_cst_compare (case_high, min_value) < 0)
3225 warning_at (loc, 0, "case label value is less than minimum value "
3227 *outside_range_p = true;
3231 /* Case value is greater than maximum for type. */
3232 if (tree_int_cst_compare (case_low, max_value) > 0
3233 && tree_int_cst_compare (case_high, max_value) > 0)
3235 warning_at (loc, 0, "case label value exceeds maximum value for type");
3236 *outside_range_p = true;
3240 /* Saturate lower case label value to minimum. */
3241 if (tree_int_cst_compare (case_high, min_value) >= 0
3242 && tree_int_cst_compare (case_low, min_value) < 0)
3244 warning_at (loc, 0, "lower value in case label range"
3245 " less than minimum value for type");
3246 *outside_range_p = true;
3247 case_low = min_value;
3250 /* Saturate upper case label value to maximum. */
3251 if (tree_int_cst_compare (case_low, max_value) <= 0
3252 && tree_int_cst_compare (case_high, max_value) > 0)
3254 warning_at (loc, 0, "upper value in case label range"
3255 " exceeds maximum value for type");
3256 *outside_range_p = true;
3257 case_high = max_value;
3260 if (*case_low_p != case_low)
3261 *case_low_p = convert (type, case_low);
3262 if (case_high_p && *case_high_p != case_high)
3263 *case_high_p = convert (type, case_high);
3268 /* Return an integer type with BITS bits of precision,
3269 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3272 c_common_type_for_size (unsigned int bits, int unsignedp)
3276 if (bits == TYPE_PRECISION (integer_type_node))
3277 return unsignedp ? unsigned_type_node : integer_type_node;
3279 if (bits == TYPE_PRECISION (signed_char_type_node))
3280 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3282 if (bits == TYPE_PRECISION (short_integer_type_node))
3283 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3285 if (bits == TYPE_PRECISION (long_integer_type_node))
3286 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3288 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3289 return (unsignedp ? long_long_unsigned_type_node
3290 : long_long_integer_type_node);
3292 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3293 if (int_n_enabled_p[i]
3294 && bits == int_n_data[i].bitsize)
3295 return (unsignedp ? int_n_trees[i].unsigned_type
3296 : int_n_trees[i].signed_type);
3298 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3299 return (unsignedp ? widest_unsigned_literal_type_node
3300 : widest_integer_literal_type_node);
3302 if (bits <= TYPE_PRECISION (intQI_type_node))
3303 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3305 if (bits <= TYPE_PRECISION (intHI_type_node))
3306 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3308 if (bits <= TYPE_PRECISION (intSI_type_node))
3309 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3311 if (bits <= TYPE_PRECISION (intDI_type_node))
3312 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3317 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3318 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3319 and saturating if SATP is nonzero, otherwise not saturating. */
3322 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3323 int unsignedp, int satp)
3327 mode = unsignedp ? UQQmode : QQmode;
3329 mode = unsignedp ? UHAmode : HAmode;
3331 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3332 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3335 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3337 sorry ("GCC cannot support operators with integer types and "
3338 "fixed-point types that have too many integral and "
3339 "fractional bits together");
3343 return c_common_type_for_mode (mode, satp);
3346 /* Used for communication between c_common_type_for_mode and
3347 c_register_builtin_type. */
3348 tree registered_builtin_types;
3350 /* Return a data type that has machine mode MODE.
3351 If the mode is an integer,
3352 then UNSIGNEDP selects between signed and unsigned types.
3353 If the mode is a fixed-point mode,
3354 then UNSIGNEDP selects between saturating and nonsaturating types. */
3357 c_common_type_for_mode (machine_mode mode, int unsignedp)
3362 if (mode == TYPE_MODE (integer_type_node))
3363 return unsignedp ? unsigned_type_node : integer_type_node;
3365 if (mode == TYPE_MODE (signed_char_type_node))
3366 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3368 if (mode == TYPE_MODE (short_integer_type_node))
3369 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3371 if (mode == TYPE_MODE (long_integer_type_node))
3372 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3374 if (mode == TYPE_MODE (long_long_integer_type_node))
3375 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3377 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3378 if (int_n_enabled_p[i]
3379 && mode == int_n_data[i].m)
3380 return (unsignedp ? int_n_trees[i].unsigned_type
3381 : int_n_trees[i].signed_type);
3383 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3384 return unsignedp ? widest_unsigned_literal_type_node
3385 : widest_integer_literal_type_node;
3388 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3391 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3394 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3397 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3399 #if HOST_BITS_PER_WIDE_INT >= 64
3400 if (mode == TYPE_MODE (intTI_type_node))
3401 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3404 if (mode == TYPE_MODE (float_type_node))
3405 return float_type_node;
3407 if (mode == TYPE_MODE (double_type_node))
3408 return double_type_node;
3410 if (mode == TYPE_MODE (long_double_type_node))
3411 return long_double_type_node;
3413 if (mode == TYPE_MODE (void_type_node))
3414 return void_type_node;
3416 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3418 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3419 : make_signed_type (GET_MODE_PRECISION (mode)));
3421 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3423 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3424 : make_signed_type (GET_MODE_PRECISION (mode)));
3426 if (COMPLEX_MODE_P (mode))
3428 machine_mode inner_mode;
3431 if (mode == TYPE_MODE (complex_float_type_node))
3432 return complex_float_type_node;
3433 if (mode == TYPE_MODE (complex_double_type_node))
3434 return complex_double_type_node;
3435 if (mode == TYPE_MODE (complex_long_double_type_node))
3436 return complex_long_double_type_node;
3438 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3439 return complex_integer_type_node;
3441 inner_mode = GET_MODE_INNER (mode);
3442 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3443 if (inner_type != NULL_TREE)
3444 return build_complex_type (inner_type);
3446 else if (VECTOR_MODE_P (mode))
3448 machine_mode inner_mode = GET_MODE_INNER (mode);
3449 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3450 if (inner_type != NULL_TREE)
3451 return build_vector_type_for_mode (inner_type, mode);
3454 if (mode == TYPE_MODE (dfloat32_type_node))
3455 return dfloat32_type_node;
3456 if (mode == TYPE_MODE (dfloat64_type_node))
3457 return dfloat64_type_node;
3458 if (mode == TYPE_MODE (dfloat128_type_node))
3459 return dfloat128_type_node;
3461 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3463 if (mode == TYPE_MODE (short_fract_type_node))
3464 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3465 if (mode == TYPE_MODE (fract_type_node))
3466 return unsignedp ? sat_fract_type_node : fract_type_node;
3467 if (mode == TYPE_MODE (long_fract_type_node))
3468 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3469 if (mode == TYPE_MODE (long_long_fract_type_node))
3470 return unsignedp ? sat_long_long_fract_type_node
3471 : long_long_fract_type_node;
3473 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3474 return unsignedp ? sat_unsigned_short_fract_type_node
3475 : unsigned_short_fract_type_node;
3476 if (mode == TYPE_MODE (unsigned_fract_type_node))
3477 return unsignedp ? sat_unsigned_fract_type_node
3478 : unsigned_fract_type_node;
3479 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3480 return unsignedp ? sat_unsigned_long_fract_type_node
3481 : unsigned_long_fract_type_node;
3482 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3483 return unsignedp ? sat_unsigned_long_long_fract_type_node
3484 : unsigned_long_long_fract_type_node;
3486 if (mode == TYPE_MODE (short_accum_type_node))
3487 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3488 if (mode == TYPE_MODE (accum_type_node))
3489 return unsignedp ? sat_accum_type_node : accum_type_node;
3490 if (mode == TYPE_MODE (long_accum_type_node))
3491 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3492 if (mode == TYPE_MODE (long_long_accum_type_node))
3493 return unsignedp ? sat_long_long_accum_type_node
3494 : long_long_accum_type_node;
3496 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3497 return unsignedp ? sat_unsigned_short_accum_type_node
3498 : unsigned_short_accum_type_node;
3499 if (mode == TYPE_MODE (unsigned_accum_type_node))
3500 return unsignedp ? sat_unsigned_accum_type_node
3501 : unsigned_accum_type_node;
3502 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3503 return unsignedp ? sat_unsigned_long_accum_type_node
3504 : unsigned_long_accum_type_node;
3505 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3506 return unsignedp ? sat_unsigned_long_long_accum_type_node
3507 : unsigned_long_long_accum_type_node;
3510 return unsignedp ? sat_qq_type_node : qq_type_node;
3512 return unsignedp ? sat_hq_type_node : hq_type_node;
3514 return unsignedp ? sat_sq_type_node : sq_type_node;
3516 return unsignedp ? sat_dq_type_node : dq_type_node;
3518 return unsignedp ? sat_tq_type_node : tq_type_node;
3520 if (mode == UQQmode)
3521 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3522 if (mode == UHQmode)
3523 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3524 if (mode == USQmode)
3525 return unsignedp ? sat_usq_type_node : usq_type_node;
3526 if (mode == UDQmode)
3527 return unsignedp ? sat_udq_type_node : udq_type_node;
3528 if (mode == UTQmode)
3529 return unsignedp ? sat_utq_type_node : utq_type_node;
3532 return unsignedp ? sat_ha_type_node : ha_type_node;
3534 return unsignedp ? sat_sa_type_node : sa_type_node;
3536 return unsignedp ? sat_da_type_node : da_type_node;
3538 return unsignedp ? sat_ta_type_node : ta_type_node;
3540 if (mode == UHAmode)
3541 return unsignedp ? sat_uha_type_node : uha_type_node;
3542 if (mode == USAmode)
3543 return unsignedp ? sat_usa_type_node : usa_type_node;
3544 if (mode == UDAmode)
3545 return unsignedp ? sat_uda_type_node : uda_type_node;
3546 if (mode == UTAmode)
3547 return unsignedp ? sat_uta_type_node : uta_type_node;
3550 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3551 if (TYPE_MODE (TREE_VALUE (t)) == mode
3552 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3553 return TREE_VALUE (t);
3559 c_common_unsigned_type (tree type)
3561 return c_common_signed_or_unsigned_type (1, type);
3564 /* Return a signed type the same as TYPE in other respects. */
3567 c_common_signed_type (tree type)
3569 return c_common_signed_or_unsigned_type (0, type);
3572 /* Return a type the same as TYPE except unsigned or
3573 signed according to UNSIGNEDP. */
3576 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3581 /* This block of code emulates the behavior of the old
3582 c_common_unsigned_type. In particular, it returns
3583 long_unsigned_type_node if passed a long, even when a int would
3584 have the same size. This is necessary for warnings to work
3585 correctly in archs where sizeof(int) == sizeof(long) */
3587 type1 = TYPE_MAIN_VARIANT (type);
3588 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3589 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3590 if (type1 == integer_type_node || type1 == unsigned_type_node)
3591 return unsignedp ? unsigned_type_node : integer_type_node;
3592 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3593 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3594 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3595 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3596 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3597 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3599 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3600 if (int_n_enabled_p[i]
3601 && (type1 == int_n_trees[i].unsigned_type
3602 || type1 == int_n_trees[i].signed_type))
3603 return (unsignedp ? int_n_trees[i].unsigned_type
3604 : int_n_trees[i].signed_type);
3606 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3607 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3608 #if HOST_BITS_PER_WIDE_INT >= 64
3609 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3610 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3612 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3613 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3614 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3615 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3616 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3617 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3618 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3619 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3621 #define C_COMMON_FIXED_TYPES(NAME) \
3622 if (type1 == short_ ## NAME ## _type_node \
3623 || type1 == unsigned_short_ ## NAME ## _type_node) \
3624 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3625 : short_ ## NAME ## _type_node; \
3626 if (type1 == NAME ## _type_node \
3627 || type1 == unsigned_ ## NAME ## _type_node) \
3628 return unsignedp ? unsigned_ ## NAME ## _type_node \
3629 : NAME ## _type_node; \
3630 if (type1 == long_ ## NAME ## _type_node \
3631 || type1 == unsigned_long_ ## NAME ## _type_node) \
3632 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3633 : long_ ## NAME ## _type_node; \
3634 if (type1 == long_long_ ## NAME ## _type_node \
3635 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3636 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3637 : long_long_ ## NAME ## _type_node;
3639 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3640 if (type1 == NAME ## _type_node \
3641 || type1 == u ## NAME ## _type_node) \
3642 return unsignedp ? u ## NAME ## _type_node \
3643 : NAME ## _type_node;
3645 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3646 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3647 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3648 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3649 : sat_ ## short_ ## NAME ## _type_node; \
3650 if (type1 == sat_ ## NAME ## _type_node \
3651 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3652 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3653 : sat_ ## NAME ## _type_node; \
3654 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3655 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3656 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3657 : sat_ ## long_ ## NAME ## _type_node; \
3658 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3659 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3660 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3661 : sat_ ## long_long_ ## NAME ## _type_node;
3663 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3664 if (type1 == sat_ ## NAME ## _type_node \
3665 || type1 == sat_ ## u ## NAME ## _type_node) \
3666 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3667 : sat_ ## NAME ## _type_node;
3669 C_COMMON_FIXED_TYPES (fract);
3670 C_COMMON_FIXED_TYPES_SAT (fract);
3671 C_COMMON_FIXED_TYPES (accum);
3672 C_COMMON_FIXED_TYPES_SAT (accum);
3674 C_COMMON_FIXED_MODE_TYPES (qq);
3675 C_COMMON_FIXED_MODE_TYPES (hq);
3676 C_COMMON_FIXED_MODE_TYPES (sq);
3677 C_COMMON_FIXED_MODE_TYPES (dq);
3678 C_COMMON_FIXED_MODE_TYPES (tq);
3679 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3680 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3681 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3682 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3683 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3684 C_COMMON_FIXED_MODE_TYPES (ha);
3685 C_COMMON_FIXED_MODE_TYPES (sa);
3686 C_COMMON_FIXED_MODE_TYPES (da);
3687 C_COMMON_FIXED_MODE_TYPES (ta);
3688 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3689 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3690 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3691 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3693 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3694 the precision; they have precision set to match their range, but
3695 may use a wider mode to match an ABI. If we change modes, we may
3696 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3697 the precision as well, so as to yield correct results for
3698 bit-field types. C++ does not have these separate bit-field
3699 types, and producing a signed or unsigned variant of an
3700 ENUMERAL_TYPE may cause other problems as well. */
3702 if (!INTEGRAL_TYPE_P (type)
3703 || TYPE_UNSIGNED (type) == unsignedp)
3706 #define TYPE_OK(node) \
3707 (TYPE_MODE (type) == TYPE_MODE (node) \
3708 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3709 if (TYPE_OK (signed_char_type_node))
3710 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3711 if (TYPE_OK (integer_type_node))
3712 return unsignedp ? unsigned_type_node : integer_type_node;
3713 if (TYPE_OK (short_integer_type_node))
3714 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3715 if (TYPE_OK (long_integer_type_node))
3716 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3717 if (TYPE_OK (long_long_integer_type_node))
3718 return (unsignedp ? long_long_unsigned_type_node
3719 : long_long_integer_type_node);
3721 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3722 if (int_n_enabled_p[i]
3723 && TYPE_MODE (type) == int_n_data[i].m
3724 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3725 return (unsignedp ? int_n_trees[i].unsigned_type
3726 : int_n_trees[i].signed_type);
3728 if (TYPE_OK (widest_integer_literal_type_node))
3729 return (unsignedp ? widest_unsigned_literal_type_node
3730 : widest_integer_literal_type_node);
3732 #if HOST_BITS_PER_WIDE_INT >= 64
3733 if (TYPE_OK (intTI_type_node))
3734 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3736 if (TYPE_OK (intDI_type_node))
3737 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3738 if (TYPE_OK (intSI_type_node))
3739 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3740 if (TYPE_OK (intHI_type_node))
3741 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3742 if (TYPE_OK (intQI_type_node))
3743 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3746 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3749 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3752 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3756 /* Extended integer types of the same width as a standard type have
3757 lesser rank, so those of the same width as int promote to int or
3758 unsigned int and are valid for printf formats expecting int or
3759 unsigned int. To avoid such special cases, avoid creating
3760 extended integer types for bit-fields if a standard integer type
3762 if (width == TYPE_PRECISION (integer_type_node))
3763 return unsignedp ? unsigned_type_node : integer_type_node;
3764 if (width == TYPE_PRECISION (signed_char_type_node))
3765 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3766 if (width == TYPE_PRECISION (short_integer_type_node))
3767 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3768 if (width == TYPE_PRECISION (long_integer_type_node))
3769 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3770 if (width == TYPE_PRECISION (long_long_integer_type_node))
3771 return (unsignedp ? long_long_unsigned_type_node
3772 : long_long_integer_type_node);
3773 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3774 if (int_n_enabled_p[i]
3775 && width == int_n_data[i].bitsize)
3776 return (unsignedp ? int_n_trees[i].unsigned_type
3777 : int_n_trees[i].signed_type);
3778 return build_nonstandard_integer_type (width, unsignedp);
3781 /* The C version of the register_builtin_type langhook. */
3784 c_register_builtin_type (tree type, const char* name)
3788 decl = build_decl (UNKNOWN_LOCATION,
3789 TYPE_DECL, get_identifier (name), type);
3790 DECL_ARTIFICIAL (decl) = 1;
3791 if (!TYPE_NAME (type))
3792 TYPE_NAME (type) = decl;
3795 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3798 /* Print an error message for invalid operands to arith operation
3799 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3800 RICHLOC is a rich location for the message, containing either
3801 three separate locations for each of the operator and operands
3806 (C FE), or one location ranging over all over them
3814 binary_op_error (rich_location *richloc, enum tree_code code,
3815 tree type0, tree type1)
3822 opname = "+"; break;
3824 opname = "-"; break;
3826 opname = "*"; break;
3828 opname = "max"; break;
3830 opname = "min"; break;
3832 opname = "=="; break;
3834 opname = "!="; break;
3836 opname = "<="; break;
3838 opname = ">="; break;
3840 opname = "<"; break;
3842 opname = ">"; break;
3844 opname = "<<"; break;
3846 opname = ">>"; break;
3847 case TRUNC_MOD_EXPR:
3848 case FLOOR_MOD_EXPR:
3849 opname = "%"; break;
3850 case TRUNC_DIV_EXPR:
3851 case FLOOR_DIV_EXPR:
3852 opname = "/"; break;
3854 opname = "&"; break;
3856 opname = "|"; break;
3857 case TRUTH_ANDIF_EXPR:
3858 opname = "&&"; break;
3859 case TRUTH_ORIF_EXPR:
3860 opname = "||"; break;
3862 opname = "^"; break;
3866 error_at_rich_loc (richloc,
3867 "invalid operands to binary %s (have %qT and %qT)",
3868 opname, type0, type1);
3871 /* Given an expression as a tree, return its original type. Do this
3872 by stripping any conversion that preserves the sign and precision. */
3874 expr_original_type (tree expr)
3876 STRIP_SIGN_NOPS (expr);
3877 return TREE_TYPE (expr);
3880 /* Subroutine of build_binary_op, used for comparison operations.
3881 See if the operands have both been converted from subword integer types
3882 and, if so, perhaps change them both back to their original type.
3883 This function is also responsible for converting the two operands
3884 to the proper common type for comparison.
3886 The arguments of this function are all pointers to local variables
3887 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3888 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3890 LOC is the location of the comparison.
3892 If this function returns nonzero, it means that the comparison has
3893 a constant value. What this function returns is an expression for
3897 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3898 tree *restype_ptr, enum tree_code *rescode_ptr)
3901 tree op0 = *op0_ptr;
3902 tree op1 = *op1_ptr;
3903 int unsignedp0, unsignedp1;
3905 tree primop0, primop1;
3906 enum tree_code code = *rescode_ptr;
3908 /* Throw away any conversions to wider types
3909 already present in the operands. */
3911 primop0 = c_common_get_narrower (op0, &unsignedp0);
3912 primop1 = c_common_get_narrower (op1, &unsignedp1);
3914 /* If primopN is first sign-extended from primopN's precision to opN's
3915 precision, then zero-extended from opN's precision to
3916 *restype_ptr precision, shortenings might be invalid. */
3917 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3918 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3920 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3922 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3923 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3925 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3928 /* Handle the case that OP0 does not *contain* a conversion
3929 but it *requires* conversion to FINAL_TYPE. */
3931 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3932 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3933 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3934 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3936 /* If one of the operands must be floated, we cannot optimize. */
3937 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3938 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3940 /* If first arg is constant, swap the args (changing operation
3941 so value is preserved), for canonicalization. Don't do this if
3942 the second arg is 0. */
3944 if (TREE_CONSTANT (primop0)
3945 && !integer_zerop (primop1) && !real_zerop (primop1)
3946 && !fixed_zerop (primop1))
3948 std::swap (primop0, primop1);
3949 std::swap (op0, op1);
3952 std::swap (unsignedp0, unsignedp1);
3953 std::swap (real1, real2);
3972 *rescode_ptr = code;
3975 /* If comparing an integer against a constant more bits wide,
3976 maybe we can deduce a value of 1 or 0 independent of the data.
3977 Or else truncate the constant now
3978 rather than extend the variable at run time.
3980 This is only interesting if the constant is the wider arg.
3981 Also, it is not safe if the constant is unsigned and the
3982 variable arg is signed, since in this case the variable
3983 would be sign-extended and then regarded as unsigned.
3984 Our technique fails in this case because the lowest/highest
3985 possible unsigned results don't follow naturally from the
3986 lowest/highest possible values of the variable operand.
3987 For just EQ_EXPR and NE_EXPR there is another technique that
3988 could be used: see if the constant can be faithfully represented
3989 in the other operand's type, by truncating it and reextending it
3990 and see if that preserves the constant's value. */
3992 if (!real1 && !real2
3993 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3994 && TREE_CODE (primop1) == INTEGER_CST
3995 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3997 int min_gt, max_gt, min_lt, max_lt;
3998 tree maxval, minval;
3999 /* 1 if comparison is nominally unsigned. */
4000 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4003 type = c_common_signed_or_unsigned_type (unsignedp0,
4004 TREE_TYPE (primop0));
4006 maxval = TYPE_MAX_VALUE (type);
4007 minval = TYPE_MIN_VALUE (type);
4009 if (unsignedp && !unsignedp0)
4010 *restype_ptr = c_common_signed_type (*restype_ptr);
4012 if (TREE_TYPE (primop1) != *restype_ptr)
4014 /* Convert primop1 to target type, but do not introduce
4015 additional overflow. We know primop1 is an int_cst. */
4016 primop1 = force_fit_type (*restype_ptr,
4019 TYPE_PRECISION (*restype_ptr),
4020 TYPE_SIGN (TREE_TYPE (primop1))),
4021 0, TREE_OVERFLOW (primop1));
4023 if (type != *restype_ptr)
4025 minval = convert (*restype_ptr, minval);
4026 maxval = convert (*restype_ptr, maxval);
4029 min_gt = tree_int_cst_lt (primop1, minval);
4030 max_gt = tree_int_cst_lt (primop1, maxval);
4031 min_lt = tree_int_cst_lt (minval, primop1);
4032 max_lt = tree_int_cst_lt (maxval, primop1);
4035 /* This used to be a switch, but Genix compiler can't handle that. */
4036 if (code == NE_EXPR)
4038 if (max_lt || min_gt)
4039 val = truthvalue_true_node;
4041 else if (code == EQ_EXPR)
4043 if (max_lt || min_gt)
4044 val = truthvalue_false_node;
4046 else if (code == LT_EXPR)
4049 val = truthvalue_true_node;
4051 val = truthvalue_false_node;
4053 else if (code == GT_EXPR)
4056 val = truthvalue_true_node;
4058 val = truthvalue_false_node;
4060 else if (code == LE_EXPR)
4063 val = truthvalue_true_node;
4065 val = truthvalue_false_node;
4067 else if (code == GE_EXPR)
4070 val = truthvalue_true_node;
4072 val = truthvalue_false_node;
4075 /* If primop0 was sign-extended and unsigned comparison specd,
4076 we did a signed comparison above using the signed type bounds.
4077 But the comparison we output must be unsigned.
4079 Also, for inequalities, VAL is no good; but if the signed
4080 comparison had *any* fixed result, it follows that the
4081 unsigned comparison just tests the sign in reverse
4082 (positive values are LE, negative ones GE).
4083 So we can generate an unsigned comparison
4084 against an extreme value of the signed type. */
4086 if (unsignedp && !unsignedp0)
4093 primop1 = TYPE_MIN_VALUE (type);
4099 primop1 = TYPE_MAX_VALUE (type);
4106 type = c_common_unsigned_type (type);
4109 if (TREE_CODE (primop0) != INTEGER_CST
4110 /* Don't warn if it's from a (non-system) macro. */
4111 && !(from_macro_expansion_at
4112 (expansion_point_location_if_in_system_header
4113 (EXPR_LOCATION (primop0)))))
4115 if (val == truthvalue_false_node)
4116 warning_at (loc, OPT_Wtype_limits,
4117 "comparison is always false due to limited range of data type");
4118 if (val == truthvalue_true_node)
4119 warning_at (loc, OPT_Wtype_limits,
4120 "comparison is always true due to limited range of data type");
4125 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4126 if (TREE_SIDE_EFFECTS (primop0))
4127 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4131 /* Value is not predetermined, but do the comparison
4132 in the type of the operand that is not constant.
4133 TYPE is already properly set. */
4136 /* If either arg is decimal float and the other is float, find the
4137 proper common type to use for comparison. */
4138 else if (real1 && real2
4139 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4140 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4141 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4143 /* If either arg is decimal float and the other is float, fail. */
4144 else if (real1 && real2
4145 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4146 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4149 else if (real1 && real2
4150 && (TYPE_PRECISION (TREE_TYPE (primop0))
4151 == TYPE_PRECISION (TREE_TYPE (primop1))))
4152 type = TREE_TYPE (primop0);
4154 /* If args' natural types are both narrower than nominal type
4155 and both extend in the same manner, compare them
4156 in the type of the wider arg.
4157 Otherwise must actually extend both to the nominal
4158 common type lest different ways of extending
4160 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4162 else if (unsignedp0 == unsignedp1 && real1 == real2
4163 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4164 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4166 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4167 type = c_common_signed_or_unsigned_type (unsignedp0
4168 || TYPE_UNSIGNED (*restype_ptr),
4170 /* Make sure shorter operand is extended the right way
4171 to match the longer operand. */
4173 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4174 TREE_TYPE (primop0)),
4177 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4178 TREE_TYPE (primop1)),
4183 /* Here we must do the comparison on the nominal type
4184 using the args exactly as we received them. */
4185 type = *restype_ptr;
4189 if (!real1 && !real2 && integer_zerop (primop1)
4190 && TYPE_UNSIGNED (*restype_ptr))
4193 /* All unsigned values are >= 0, so we warn. However,
4194 if OP0 is a constant that is >= 0, the signedness of
4195 the comparison isn't an issue, so suppress the
4198 warn_type_limits && !in_system_header_at (loc)
4199 && !(TREE_CODE (primop0) == INTEGER_CST
4200 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4202 /* Do not warn for enumeration types. */
4203 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4209 warning_at (loc, OPT_Wtype_limits,
4210 "comparison of unsigned expression >= 0 is always true");
4211 value = truthvalue_true_node;
4216 warning_at (loc, OPT_Wtype_limits,
4217 "comparison of unsigned expression < 0 is always false");
4218 value = truthvalue_false_node;
4227 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4228 if (TREE_SIDE_EFFECTS (primop0))
4229 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4236 *op0_ptr = convert (type, primop0);
4237 *op1_ptr = convert (type, primop1);
4239 *restype_ptr = truthvalue_type_node;
4244 /* Return a tree for the sum or difference (RESULTCODE says which)
4245 of pointer PTROP and integer INTOP. */
4248 pointer_int_sum (location_t loc, enum tree_code resultcode,
4249 tree ptrop, tree intop, bool complain)
4252 HOST_WIDE_INT op1 = 0;
4254 /* The result is a pointer of the same type that is being added. */
4255 tree result_type = TREE_TYPE (ptrop);
4257 if (flag_sanitize & SANITIZE_UI_OVERFLOW)
4259 /* First, try to avoid FPs if INTOP is negative constant. */
4260 if (tree_fits_shwi_p (intop))
4261 op1 = tree_to_shwi (intop);
4264 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4266 if (complain && warn_pointer_arith)
4267 pedwarn (loc, OPT_Wpointer_arith,
4268 "pointer of type %<void *%> used in arithmetic");
4270 return error_mark_node;
4271 size_exp = integer_one_node;
4273 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4275 if (complain && warn_pointer_arith)
4276 pedwarn (loc, OPT_Wpointer_arith,
4277 "pointer to a function used in arithmetic");
4279 return error_mark_node;
4280 size_exp = integer_one_node;
4283 size_exp = size_in_bytes (TREE_TYPE (result_type));
4285 /* We are manipulating pointer values, so we don't need to warn
4286 about relying on undefined signed overflow. We disable the
4287 warning here because we use integer types so fold won't know that
4288 they are really pointers. */
4289 fold_defer_overflow_warnings ();
4291 /* If what we are about to multiply by the size of the elements
4292 contains a constant term, apply distributive law
4293 and multiply that constant term separately.
4294 This helps produce common subexpressions. */
4295 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4296 && !TREE_CONSTANT (intop)
4297 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4298 && TREE_CONSTANT (size_exp)
4299 /* If the constant comes from pointer subtraction,
4300 skip this optimization--it would cause an error. */
4301 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4302 /* If the constant is unsigned, and smaller than the pointer size,
4303 then we must skip this optimization. This is because it could cause
4304 an overflow error if the constant is negative but INTOP is not. */
4305 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4306 || (TYPE_PRECISION (TREE_TYPE (intop))
4307 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4309 enum tree_code subcode = resultcode;
4310 tree int_type = TREE_TYPE (intop);
4311 if (TREE_CODE (intop) == MINUS_EXPR)
4312 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4313 /* Convert both subexpression types to the type of intop,
4314 because weird cases involving pointer arithmetic
4315 can result in a sum or difference with different type args. */
4316 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4318 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4319 intop = convert (int_type, TREE_OPERAND (intop, 0));
4322 /* Convert the integer argument to a type the same size as sizetype
4323 so the multiply won't overflow spuriously. */
4324 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4325 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4326 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4327 TYPE_UNSIGNED (sizetype)), intop);
4329 /* Replace the integer argument with a suitable product by the object size.
4330 Do this multiplication as signed, then convert to the appropriate type
4331 for the pointer operation and disregard an overflow that occurred only
4332 because of the sign-extension change in the latter conversion. */
4334 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4335 convert (TREE_TYPE (intop), size_exp));
4336 intop = convert (sizetype, t);
4337 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4338 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4341 if (flag_sanitize & SANITIZE_UI_OVERFLOW)
4343 tree tmp = NULL_TREE;
4344 /* First, try to avoid FPs if INTOP is negative constant. */
4349 resultcode = (resultcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4352 tmp = build_int_cst (sizetype, op1);
4353 intop = build_binary_op (loc, MULT_EXPR, tmp,
4354 convert (TREE_TYPE (intop), size_exp), 1);
4356 tmp = build_binary_op (loc, resultcode,
4357 convert (sizetype, ptrop),
4358 convert (long_integer_type_node, intop), 1);
4359 ret = convert (result_type, tmp);
4363 /* Create the sum or difference. */
4364 if (resultcode == MINUS_EXPR)
4365 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4367 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4370 fold_undefer_and_ignore_overflow_warnings ();
4375 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4376 and if NON_CONST is known not to be permitted in an evaluated part
4377 of a constant expression. */
4380 c_wrap_maybe_const (tree expr, bool non_const)
4382 bool nowarning = TREE_NO_WARNING (expr);
4383 location_t loc = EXPR_LOCATION (expr);
4385 /* This should never be called for C++. */
4386 if (c_dialect_cxx ())
4389 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4390 STRIP_TYPE_NOPS (expr);
4391 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4392 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4394 TREE_NO_WARNING (expr) = 1;
4395 protected_set_expr_location (expr, loc);
4400 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4401 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4402 around the SAVE_EXPR if needed so that c_fully_fold does not need
4403 to look inside SAVE_EXPRs. */
4406 c_save_expr (tree expr)
4408 bool maybe_const = true;
4409 if (c_dialect_cxx ())
4410 return save_expr (expr);
4411 expr = c_fully_fold (expr, false, &maybe_const);
4412 expr = save_expr (expr);
4414 expr = c_wrap_maybe_const (expr, true);
4418 /* Return whether EXPR is a declaration whose address can never be
4422 decl_with_nonnull_addr_p (const_tree expr)
4424 return (DECL_P (expr)
4425 && (TREE_CODE (expr) == PARM_DECL
4426 || TREE_CODE (expr) == LABEL_DECL
4427 || !DECL_WEAK (expr)));
4430 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4431 or for an `if' or `while' statement or ?..: exp. It should already
4432 have been validated to be of suitable type; otherwise, a bad
4433 diagnostic may result.
4435 The EXPR is located at LOCATION.
4437 This preparation consists of taking the ordinary
4438 representation of an expression expr and producing a valid tree
4439 boolean expression describing whether expr is nonzero. We could
4440 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4441 but we optimize comparisons, &&, ||, and !.
4443 The resulting type should always be `truthvalue_type_node'. */
4446 c_common_truthvalue_conversion (location_t location, tree expr)
4448 switch (TREE_CODE (expr))
4450 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4451 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4452 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4453 case ORDERED_EXPR: case UNORDERED_EXPR:
4454 if (TREE_TYPE (expr) == truthvalue_type_node)
4456 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4457 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4460 case TRUTH_ANDIF_EXPR:
4461 case TRUTH_ORIF_EXPR:
4462 case TRUTH_AND_EXPR:
4464 case TRUTH_XOR_EXPR:
4465 if (TREE_TYPE (expr) == truthvalue_type_node)
4467 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4468 c_common_truthvalue_conversion (location,
4469 TREE_OPERAND (expr, 0)),
4470 c_common_truthvalue_conversion (location,
4471 TREE_OPERAND (expr, 1)));
4474 case TRUTH_NOT_EXPR:
4475 if (TREE_TYPE (expr) == truthvalue_type_node)
4477 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4478 c_common_truthvalue_conversion (location,
4479 TREE_OPERAND (expr, 0)));
4486 return integer_zerop (expr) ? truthvalue_false_node
4487 : truthvalue_true_node;
4490 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4491 ? truthvalue_true_node
4492 : truthvalue_false_node;
4495 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4496 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4497 ? truthvalue_true_node
4498 : truthvalue_false_node;
4501 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4506 tree inner = TREE_OPERAND (expr, 0);
4507 if (decl_with_nonnull_addr_p (inner))
4509 /* Common Ada/Pascal programmer's mistake. */
4510 warning_at (location,
4512 "the address of %qD will always evaluate as %<true%>",
4514 return truthvalue_true_node;
4520 expr = build_binary_op (EXPR_LOCATION (expr),
4521 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4522 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4523 c_common_truthvalue_conversion (location,
4524 TREE_OPERAND (expr, 0)),
4525 c_common_truthvalue_conversion (location,
4526 TREE_OPERAND (expr, 1)),
4533 case EXCESS_PRECISION_EXPR:
4534 /* These don't change whether an object is nonzero or zero. */
4535 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4539 /* These don't change whether an object is zero or nonzero, but
4540 we can't ignore them if their second arg has side-effects. */
4541 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4543 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4544 TREE_OPERAND (expr, 1),
4545 c_common_truthvalue_conversion
4546 (location, TREE_OPERAND (expr, 0)));
4550 return c_common_truthvalue_conversion (location,
4551 TREE_OPERAND (expr, 0));
4554 /* Distribute the conversion into the arms of a COND_EXPR. */
4555 if (c_dialect_cxx ())
4557 tree op1 = TREE_OPERAND (expr, 1);
4558 tree op2 = TREE_OPERAND (expr, 2);
4559 /* In C++ one of the arms might have void type if it is throw. */
4560 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4561 op1 = c_common_truthvalue_conversion (location, op1);
4562 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4563 op2 = c_common_truthvalue_conversion (location, op2);
4564 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4565 TREE_OPERAND (expr, 0), op1, op2);
4570 /* Folding will happen later for C. */
4571 expr = build3 (COND_EXPR, truthvalue_type_node,
4572 TREE_OPERAND (expr, 0),
4573 c_common_truthvalue_conversion (location,
4574 TREE_OPERAND (expr, 1)),
4575 c_common_truthvalue_conversion (location,
4576 TREE_OPERAND (expr, 2)));
4582 tree totype = TREE_TYPE (expr);
4583 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4585 if (POINTER_TYPE_P (totype)
4586 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4592 warning_at (location,
4594 "the compiler can assume that the address of "
4595 "%qD will always evaluate to %<true%>",
4599 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4600 since that affects how `default_conversion' will behave. */
4601 if (TREE_CODE (totype) == REFERENCE_TYPE
4602 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4604 /* Don't strip a conversion from C++0x scoped enum, since they
4605 don't implicitly convert to other types. */
4606 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4607 && ENUM_IS_SCOPED (fromtype))
4609 /* If this isn't narrowing the argument, we can ignore it. */
4610 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4611 return c_common_truthvalue_conversion (location,
4612 TREE_OPERAND (expr, 0));
4617 if (!TREE_NO_WARNING (expr)
4618 && warn_parentheses)
4620 warning (OPT_Wparentheses,
4621 "suggest parentheses around assignment used as truth value");
4622 TREE_NO_WARNING (expr) = 1;
4630 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4632 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4633 expr = (build_binary_op
4634 (EXPR_LOCATION (expr),
4635 (TREE_SIDE_EFFECTS (expr)
4636 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4637 c_common_truthvalue_conversion
4639 build_unary_op (location, REALPART_EXPR, t, 0)),
4640 c_common_truthvalue_conversion
4642 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4647 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4649 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4651 (TREE_TYPE (expr))));
4652 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4655 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4658 protected_set_expr_location (expr, location);
4662 static void def_builtin_1 (enum built_in_function fncode,
4664 enum built_in_class fnclass,
4665 tree fntype, tree libtype,
4666 bool both_p, bool fallback_p, bool nonansi_p,
4667 tree fnattrs, bool implicit_p);
4670 /* Apply the TYPE_QUALS to the new DECL. */
4673 c_apply_type_quals_to_decl (int type_quals, tree decl)
4675 tree type = TREE_TYPE (decl);
4677 if (type == error_mark_node)
4680 if ((type_quals & TYPE_QUAL_CONST)
4681 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4682 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4683 constructor can produce constant init, so rely on cp_finish_decl to
4684 clear TREE_READONLY if the variable has non-constant init. */
4685 TREE_READONLY (decl) = 1;
4686 if (type_quals & TYPE_QUAL_VOLATILE)
4688 TREE_SIDE_EFFECTS (decl) = 1;
4689 TREE_THIS_VOLATILE (decl) = 1;
4691 if (type_quals & TYPE_QUAL_RESTRICT)
4693 while (type && TREE_CODE (type) == ARRAY_TYPE)
4694 /* Allow 'restrict' on arrays of pointers.
4695 FIXME currently we just ignore it. */
4696 type = TREE_TYPE (type);
4698 || !POINTER_TYPE_P (type)
4699 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4700 error ("invalid use of %<restrict%>");
4704 struct c_type_hasher : ggc_ptr_hash<tree_node>
4706 static hashval_t hash (tree);
4707 static bool equal (tree, tree);
4710 /* Hash function for the problem of multiple type definitions in
4711 different files. This must hash all types that will compare
4712 equal via comptypes to the same value. In practice it hashes
4713 on some of the simple stuff and leaves the details to comptypes. */
4716 c_type_hasher::hash (tree t)
4721 switch (TREE_CODE (t))
4723 /* For pointers, hash on pointee type plus some swizzling. */
4725 return hash (TREE_TYPE (t)) ^ 0x3003003;
4726 /* Hash on number of elements and total size. */
4729 t2 = TYPE_VALUES (t);
4733 t2 = TYPE_FIELDS (t);
4735 case QUAL_UNION_TYPE:
4737 t2 = TYPE_FIELDS (t);
4741 t2 = TYPE_FIELDS (t);
4746 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4747 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4748 n_elements = list_length (t2);
4749 /* We might have a VLA here. */
4750 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4753 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4754 return ((size << 24) | (n_elements << shift));
4758 c_type_hasher::equal (tree t1, tree t2)
4760 return lang_hooks.types_compatible_p (t1, t2);
4763 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4765 /* Return the typed-based alias set for T, which may be an expression
4766 or a type. Return -1 if we don't do anything special. */
4769 c_common_get_alias_set (tree t)
4773 /* For VLAs, use the alias set of the element type rather than the
4774 default of alias set 0 for types compared structurally. */
4775 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4777 if (TREE_CODE (t) == ARRAY_TYPE)
4778 return get_alias_set (TREE_TYPE (t));
4782 /* Permit type-punning when accessing a union, provided the access
4783 is directly through the union. For example, this code does not
4784 permit taking the address of a union member and then storing
4785 through it. Even the type-punning allowed here is a GCC
4786 extension, albeit a common and useful one; the C standard says
4787 that such accesses have implementation-defined behavior. */
4789 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4790 u = TREE_OPERAND (u, 0))
4791 if (TREE_CODE (u) == COMPONENT_REF
4792 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4795 /* That's all the expressions we handle specially. */
4799 /* The C standard guarantees that any object may be accessed via an
4800 lvalue that has character type. */
4801 if (t == char_type_node
4802 || t == signed_char_type_node
4803 || t == unsigned_char_type_node)
4806 /* The C standard specifically allows aliasing between signed and
4807 unsigned variants of the same type. We treat the signed
4808 variant as canonical. */
4809 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4811 tree t1 = c_common_signed_type (t);
4813 /* t1 == t can happen for boolean nodes which are always unsigned. */
4815 return get_alias_set (t1);
4818 /* Handle the case of multiple type nodes referring to "the same" type,
4819 which occurs with IMA. These share an alias set. FIXME: Currently only
4820 C90 is handled. (In C99 type compatibility is not transitive, which
4821 complicates things mightily. The alias set splay trees can theoretically
4822 represent this, but insertion is tricky when you consider all the
4823 different orders things might arrive in.) */
4825 if (c_language != clk_c || flag_isoc99)
4828 /* Save time if there's only one input file. */
4829 if (num_in_fnames == 1)
4832 /* Pointers need special handling if they point to any type that
4833 needs special handling (below). */
4834 if (TREE_CODE (t) == POINTER_TYPE)
4837 /* Find bottom type under any nested POINTERs. */
4838 for (t2 = TREE_TYPE (t);
4839 TREE_CODE (t2) == POINTER_TYPE;
4840 t2 = TREE_TYPE (t2))
4842 if (!RECORD_OR_UNION_TYPE_P (t2)
4843 && TREE_CODE (t2) != ENUMERAL_TYPE)
4845 if (TYPE_SIZE (t2) == 0)
4848 /* These are the only cases that need special handling. */
4849 if (!RECORD_OR_UNION_TYPE_P (t)
4850 && TREE_CODE (t) != ENUMERAL_TYPE
4851 && TREE_CODE (t) != POINTER_TYPE)
4854 if (TYPE_SIZE (t) == 0)
4857 /* Look up t in hash table. Only one of the compatible types within each
4858 alias set is recorded in the table. */
4859 if (!type_hash_table)
4860 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4861 tree *slot = type_hash_table->find_slot (t, INSERT);
4864 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4865 return TYPE_ALIAS_SET ((tree)*slot);
4868 /* Our caller will assign and record (in t) a new alias set; all we need
4869 to do is remember t in the hash table. */
4875 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4876 the IS_SIZEOF parameter indicates which operator is being applied.
4877 The COMPLAIN flag controls whether we should diagnose possibly
4878 ill-formed constructs or not. LOC is the location of the SIZEOF or
4879 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4880 a type in any context should be returned, rather than the normal
4881 alignment for that type. */
4884 c_sizeof_or_alignof_type (location_t loc,
4885 tree type, bool is_sizeof, bool min_alignof,
4888 const char *op_name;
4890 enum tree_code type_code = TREE_CODE (type);
4892 op_name = is_sizeof ? "sizeof" : "__alignof__";
4894 if (type_code == FUNCTION_TYPE)
4898 if (complain && warn_pointer_arith)
4899 pedwarn (loc, OPT_Wpointer_arith,
4900 "invalid application of %<sizeof%> to a function type");
4902 return error_mark_node;
4903 value = size_one_node;
4909 if (c_dialect_cxx ())
4910 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4911 "%<alignof%> applied to a function type");
4913 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4914 "%<_Alignof%> applied to a function type");
4916 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4919 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4921 if (type_code == VOID_TYPE
4922 && complain && warn_pointer_arith)
4923 pedwarn (loc, OPT_Wpointer_arith,
4924 "invalid application of %qs to a void type", op_name);
4926 return error_mark_node;
4927 value = size_one_node;
4929 else if (!COMPLETE_TYPE_P (type)
4930 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4933 error_at (loc, "invalid application of %qs to incomplete type %qT",
4935 return error_mark_node;
4937 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4938 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4941 error_at (loc, "invalid application of %qs to array type %qT of "
4942 "incomplete element type", op_name, type);
4943 return error_mark_node;
4948 /* Convert in case a char is more than one unit. */
4949 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4950 size_int (TYPE_PRECISION (char_type_node)
4952 else if (min_alignof)
4953 value = size_int (min_align_of_type (type));
4955 value = size_int (TYPE_ALIGN_UNIT (type));
4958 /* VALUE will have the middle-end integer type sizetype.
4959 However, we should really return a value of type `size_t',
4960 which is just a typedef for an ordinary integer type. */
4961 value = fold_convert_loc (loc, size_type_node, value);
4966 /* Implement the __alignof keyword: Return the minimum required
4967 alignment of EXPR, measured in bytes. For VAR_DECLs,
4968 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4969 from an "aligned" __attribute__ specification). LOC is the
4970 location of the ALIGNOF operator. */
4973 c_alignof_expr (location_t loc, tree expr)
4977 if (VAR_OR_FUNCTION_DECL_P (expr))
4978 t = size_int (DECL_ALIGN_UNIT (expr));
4980 else if (TREE_CODE (expr) == COMPONENT_REF
4981 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4983 error_at (loc, "%<__alignof%> applied to a bit-field");
4986 else if (TREE_CODE (expr) == COMPONENT_REF
4987 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4988 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4990 else if (INDIRECT_REF_P (expr))
4992 tree t = TREE_OPERAND (expr, 0);
4994 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4996 while (CONVERT_EXPR_P (t)
4997 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5001 t = TREE_OPERAND (t, 0);
5002 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5003 if (thisalign > bestalign)
5004 best = t, bestalign = thisalign;
5006 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5009 return c_alignof (loc, TREE_TYPE (expr));
5011 return fold_convert_loc (loc, size_type_node, t);
5014 /* Handle C and C++ default attributes. */
5016 enum built_in_attribute
5018 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5019 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5020 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5021 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5022 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5023 #include "builtin-attrs.def"
5024 #undef DEF_ATTR_NULL_TREE
5026 #undef DEF_ATTR_STRING
5027 #undef DEF_ATTR_IDENT
5028 #undef DEF_ATTR_TREE_LIST
5032 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5034 static void c_init_attributes (void);
5038 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5039 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5040 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5041 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5042 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5043 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5044 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5045 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5047 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5049 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5050 ARG6, ARG7, ARG8) NAME,
5051 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5052 ARG6, ARG7, ARG8, ARG9) NAME,
5053 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5054 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5055 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5056 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5057 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5058 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5059 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5060 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5061 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5062 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5064 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5066 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5068 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5069 #include "builtin-types.def"
5070 #undef DEF_PRIMITIVE_TYPE
5071 #undef DEF_FUNCTION_TYPE_0
5072 #undef DEF_FUNCTION_TYPE_1
5073 #undef DEF_FUNCTION_TYPE_2
5074 #undef DEF_FUNCTION_TYPE_3
5075 #undef DEF_FUNCTION_TYPE_4
5076 #undef DEF_FUNCTION_TYPE_5
5077 #undef DEF_FUNCTION_TYPE_6
5078 #undef DEF_FUNCTION_TYPE_7
5079 #undef DEF_FUNCTION_TYPE_8
5080 #undef DEF_FUNCTION_TYPE_9
5081 #undef DEF_FUNCTION_TYPE_10
5082 #undef DEF_FUNCTION_TYPE_11
5083 #undef DEF_FUNCTION_TYPE_VAR_0
5084 #undef DEF_FUNCTION_TYPE_VAR_1
5085 #undef DEF_FUNCTION_TYPE_VAR_2
5086 #undef DEF_FUNCTION_TYPE_VAR_3
5087 #undef DEF_FUNCTION_TYPE_VAR_4
5088 #undef DEF_FUNCTION_TYPE_VAR_5
5089 #undef DEF_FUNCTION_TYPE_VAR_6
5090 #undef DEF_FUNCTION_TYPE_VAR_7
5091 #undef DEF_POINTER_TYPE
5095 typedef enum c_builtin_type builtin_type;
5097 /* A temporary array for c_common_nodes_and_builtins. Used in
5098 communication with def_fn_type. */
5099 static tree builtin_types[(int) BT_LAST + 1];
5101 /* A helper function for c_common_nodes_and_builtins. Build function type
5102 for DEF with return type RET and N arguments. If VAR is true, then the
5103 function should be variadic after those N arguments.
5105 Takes special care not to ICE if any of the types involved are
5106 error_mark_node, which indicates that said type is not in fact available
5107 (see builtin_type_for_size). In which case the function type as a whole
5108 should be error_mark_node. */
5111 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5114 tree *args = XALLOCAVEC (tree, n);
5119 for (i = 0; i < n; ++i)
5121 builtin_type a = (builtin_type) va_arg (list, int);
5122 t = builtin_types[a];
5123 if (t == error_mark_node)
5128 t = builtin_types[ret];
5129 if (t == error_mark_node)
5132 t = build_varargs_function_type_array (t, n, args);
5134 t = build_function_type_array (t, n, args);
5137 builtin_types[def] = t;
5141 /* Build builtin functions common to both C and C++ language
5145 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5147 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5148 builtin_types[ENUM] = VALUE;
5149 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5150 def_fn_type (ENUM, RETURN, 0, 0);
5151 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5152 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5153 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5154 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5155 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5156 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5157 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5158 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5159 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5160 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5161 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5163 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5164 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5166 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5167 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5169 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5171 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5172 ARG6, ARG7, ARG8, ARG9) \
5173 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5175 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5176 ARG6, ARG7, ARG8, ARG9, ARG10) \
5177 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5178 ARG7, ARG8, ARG9, ARG10);
5179 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5180 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5181 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5182 ARG7, ARG8, ARG9, ARG10, ARG11);
5183 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5184 def_fn_type (ENUM, RETURN, 1, 0);
5185 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5186 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5187 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5188 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5189 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5190 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5191 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5192 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5193 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5194 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5195 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5197 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5198 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5200 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5201 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5202 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5204 #include "builtin-types.def"
5206 #undef DEF_PRIMITIVE_TYPE
5207 #undef DEF_FUNCTION_TYPE_0
5208 #undef DEF_FUNCTION_TYPE_1
5209 #undef DEF_FUNCTION_TYPE_2
5210 #undef DEF_FUNCTION_TYPE_3
5211 #undef DEF_FUNCTION_TYPE_4
5212 #undef DEF_FUNCTION_TYPE_5
5213 #undef DEF_FUNCTION_TYPE_6
5214 #undef DEF_FUNCTION_TYPE_7
5215 #undef DEF_FUNCTION_TYPE_8
5216 #undef DEF_FUNCTION_TYPE_9
5217 #undef DEF_FUNCTION_TYPE_10
5218 #undef DEF_FUNCTION_TYPE_11
5219 #undef DEF_FUNCTION_TYPE_VAR_0
5220 #undef DEF_FUNCTION_TYPE_VAR_1
5221 #undef DEF_FUNCTION_TYPE_VAR_2
5222 #undef DEF_FUNCTION_TYPE_VAR_3
5223 #undef DEF_FUNCTION_TYPE_VAR_4
5224 #undef DEF_FUNCTION_TYPE_VAR_5
5225 #undef DEF_FUNCTION_TYPE_VAR_6
5226 #undef DEF_FUNCTION_TYPE_VAR_7
5227 #undef DEF_POINTER_TYPE
5228 builtin_types[(int) BT_LAST] = NULL_TREE;
5230 c_init_attributes ();
5232 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5233 NONANSI_P, ATTRS, IMPLICIT, COND) \
5235 def_builtin_1 (ENUM, NAME, CLASS, \
5236 builtin_types[(int) TYPE], \
5237 builtin_types[(int) LIBTYPE], \
5238 BOTH_P, FALLBACK_P, NONANSI_P, \
5239 built_in_attributes[(int) ATTRS], IMPLICIT);
5240 #include "builtins.def"
5242 targetm.init_builtins ();
5244 build_common_builtin_nodes ();
5247 cilk_init_builtins ();
5250 /* Like get_identifier, but avoid warnings about null arguments when
5251 the argument may be NULL for targets where GCC lacks stdint.h type
5255 c_get_ident (const char *id)
5257 return get_identifier (id);
5260 /* Build tree nodes and builtin functions common to both C and C++ language
5264 c_common_nodes_and_builtins (void)
5266 int char16_type_size;
5267 int char32_type_size;
5268 int wchar_type_size;
5269 tree array_domain_type;
5270 tree va_list_ref_type_node;
5271 tree va_list_arg_type_node;
5274 build_common_tree_nodes (flag_signed_char);
5276 /* Define `int' and `char' first so that dbx will output them first. */
5277 record_builtin_type (RID_INT, NULL, integer_type_node);
5278 record_builtin_type (RID_CHAR, "char", char_type_node);
5280 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5281 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5282 but not C. Are the conditionals here needed? */
5283 if (c_dialect_cxx ())
5284 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5285 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5286 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5287 record_builtin_type (RID_MAX, "long unsigned int",
5288 long_unsigned_type_node);
5290 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5294 sprintf (name, "__int%d", int_n_data[i].bitsize);
5295 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5296 int_n_trees[i].signed_type);
5297 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5298 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5301 if (c_dialect_cxx ())
5302 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5303 record_builtin_type (RID_MAX, "long long int",
5304 long_long_integer_type_node);
5305 record_builtin_type (RID_MAX, "long long unsigned int",
5306 long_long_unsigned_type_node);
5307 if (c_dialect_cxx ())
5308 record_builtin_type (RID_MAX, "long long unsigned",
5309 long_long_unsigned_type_node);
5310 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5311 record_builtin_type (RID_MAX, "short unsigned int",
5312 short_unsigned_type_node);
5313 if (c_dialect_cxx ())
5314 record_builtin_type (RID_MAX, "unsigned short",
5315 short_unsigned_type_node);
5317 /* Define both `signed char' and `unsigned char'. */
5318 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5319 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5321 /* These are types that c_common_type_for_size and
5322 c_common_type_for_mode use. */
5323 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5324 TYPE_DECL, NULL_TREE,
5326 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5327 TYPE_DECL, NULL_TREE,
5329 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5330 TYPE_DECL, NULL_TREE,
5332 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5333 TYPE_DECL, NULL_TREE,
5335 #if HOST_BITS_PER_WIDE_INT >= 64
5336 /* Note that this is different than the __int128 type that's part of
5337 the generic __intN support. */
5338 if (targetm.scalar_mode_supported_p (TImode))
5339 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5341 get_identifier ("__int128_t"),
5344 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5345 TYPE_DECL, NULL_TREE,
5346 unsigned_intQI_type_node));
5347 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5348 TYPE_DECL, NULL_TREE,
5349 unsigned_intHI_type_node));
5350 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5351 TYPE_DECL, NULL_TREE,
5352 unsigned_intSI_type_node));
5353 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5354 TYPE_DECL, NULL_TREE,
5355 unsigned_intDI_type_node));
5356 #if HOST_BITS_PER_WIDE_INT >= 64
5357 if (targetm.scalar_mode_supported_p (TImode))
5358 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5360 get_identifier ("__uint128_t"),
5361 unsigned_intTI_type_node));
5364 /* Create the widest literal types. */
5365 widest_integer_literal_type_node
5366 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5367 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5368 TYPE_DECL, NULL_TREE,
5369 widest_integer_literal_type_node));
5371 widest_unsigned_literal_type_node
5372 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5373 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5374 TYPE_DECL, NULL_TREE,
5375 widest_unsigned_literal_type_node));
5377 signed_size_type_node = c_common_signed_type (size_type_node);
5380 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5382 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5383 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5384 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5386 /* Only supported decimal floating point extension if the target
5387 actually supports underlying modes. */
5388 if (targetm.scalar_mode_supported_p (SDmode)
5389 && targetm.scalar_mode_supported_p (DDmode)
5390 && targetm.scalar_mode_supported_p (TDmode))
5392 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5393 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5394 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5397 if (targetm.fixed_point_supported_p ())
5399 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5400 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5401 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5402 record_builtin_type (RID_MAX, "long long _Fract",
5403 long_long_fract_type_node);
5404 record_builtin_type (RID_MAX, "unsigned short _Fract",
5405 unsigned_short_fract_type_node);
5406 record_builtin_type (RID_MAX, "unsigned _Fract",
5407 unsigned_fract_type_node);
5408 record_builtin_type (RID_MAX, "unsigned long _Fract",
5409 unsigned_long_fract_type_node);
5410 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5411 unsigned_long_long_fract_type_node);
5412 record_builtin_type (RID_MAX, "_Sat short _Fract",
5413 sat_short_fract_type_node);
5414 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5415 record_builtin_type (RID_MAX, "_Sat long _Fract",
5416 sat_long_fract_type_node);
5417 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5418 sat_long_long_fract_type_node);
5419 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5420 sat_unsigned_short_fract_type_node);
5421 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5422 sat_unsigned_fract_type_node);
5423 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5424 sat_unsigned_long_fract_type_node);
5425 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5426 sat_unsigned_long_long_fract_type_node);
5427 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5428 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5429 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5430 record_builtin_type (RID_MAX, "long long _Accum",
5431 long_long_accum_type_node);
5432 record_builtin_type (RID_MAX, "unsigned short _Accum",
5433 unsigned_short_accum_type_node);
5434 record_builtin_type (RID_MAX, "unsigned _Accum",
5435 unsigned_accum_type_node);
5436 record_builtin_type (RID_MAX, "unsigned long _Accum",
5437 unsigned_long_accum_type_node);
5438 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5439 unsigned_long_long_accum_type_node);
5440 record_builtin_type (RID_MAX, "_Sat short _Accum",
5441 sat_short_accum_type_node);
5442 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5443 record_builtin_type (RID_MAX, "_Sat long _Accum",
5444 sat_long_accum_type_node);
5445 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5446 sat_long_long_accum_type_node);
5447 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5448 sat_unsigned_short_accum_type_node);
5449 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5450 sat_unsigned_accum_type_node);
5451 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5452 sat_unsigned_long_accum_type_node);
5453 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5454 sat_unsigned_long_long_accum_type_node);
5458 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5460 get_identifier ("complex int"),
5461 complex_integer_type_node));
5462 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5464 get_identifier ("complex float"),
5465 complex_float_type_node));
5466 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5468 get_identifier ("complex double"),
5469 complex_double_type_node));
5470 lang_hooks.decls.pushdecl
5471 (build_decl (UNKNOWN_LOCATION,
5472 TYPE_DECL, get_identifier ("complex long double"),
5473 complex_long_double_type_node));
5475 if (c_dialect_cxx ())
5476 /* For C++, make fileptr_type_node a distinct void * type until
5477 FILE type is defined. */
5478 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5480 record_builtin_type (RID_VOID, NULL, void_type_node);
5482 /* Set the TYPE_NAME for any variants that were built before
5483 record_builtin_type gave names to the built-in types. */
5485 tree void_name = TYPE_NAME (void_type_node);
5486 TYPE_NAME (void_type_node) = NULL_TREE;
5487 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5489 TYPE_NAME (void_type_node) = void_name;
5492 void_list_node = build_void_list_node ();
5494 /* Make a type to be the domain of a few array types
5495 whose domains don't really matter.
5496 200 is small enough that it always fits in size_t
5497 and large enough that it can hold most function names for the
5498 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5499 array_domain_type = build_index_type (size_int (200));
5501 /* Make a type for arrays of characters.
5502 With luck nothing will ever really depend on the length of this
5504 char_array_type_node
5505 = build_array_type (char_type_node, array_domain_type);
5507 string_type_node = build_pointer_type (char_type_node);
5508 const_string_type_node
5509 = build_pointer_type (build_qualified_type
5510 (char_type_node, TYPE_QUAL_CONST));
5512 /* This is special for C++ so functions can be overloaded. */
5513 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5514 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5515 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5516 underlying_wchar_type_node = wchar_type_node;
5517 if (c_dialect_cxx ())
5519 if (TYPE_UNSIGNED (wchar_type_node))
5520 wchar_type_node = make_unsigned_type (wchar_type_size);
5522 wchar_type_node = make_signed_type (wchar_type_size);
5523 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5526 /* This is for wide string constants. */
5527 wchar_array_type_node
5528 = build_array_type (wchar_type_node, array_domain_type);
5530 /* Define 'char16_t'. */
5531 char16_type_node = get_identifier (CHAR16_TYPE);
5532 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5533 char16_type_size = TYPE_PRECISION (char16_type_node);
5534 if (c_dialect_cxx ())
5536 char16_type_node = make_unsigned_type (char16_type_size);
5538 if (cxx_dialect >= cxx11)
5539 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5542 /* This is for UTF-16 string constants. */
5543 char16_array_type_node
5544 = build_array_type (char16_type_node, array_domain_type);
5546 /* Define 'char32_t'. */
5547 char32_type_node = get_identifier (CHAR32_TYPE);
5548 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5549 char32_type_size = TYPE_PRECISION (char32_type_node);
5550 if (c_dialect_cxx ())
5552 char32_type_node = make_unsigned_type (char32_type_size);
5554 if (cxx_dialect >= cxx11)
5555 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5558 /* This is for UTF-32 string constants. */
5559 char32_array_type_node
5560 = build_array_type (char32_type_node, array_domain_type);
5563 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5566 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5568 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5570 if (SIG_ATOMIC_TYPE)
5571 sig_atomic_type_node =
5572 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5575 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5578 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5581 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5584 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5587 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5589 c_uint16_type_node = uint16_type_node =
5590 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5592 c_uint32_type_node = uint32_type_node =
5593 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5595 c_uint64_type_node = uint64_type_node =
5596 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5597 if (INT_LEAST8_TYPE)
5598 int_least8_type_node =
5599 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5600 if (INT_LEAST16_TYPE)
5601 int_least16_type_node =
5602 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5603 if (INT_LEAST32_TYPE)
5604 int_least32_type_node =
5605 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5606 if (INT_LEAST64_TYPE)
5607 int_least64_type_node =
5608 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5609 if (UINT_LEAST8_TYPE)
5610 uint_least8_type_node =
5611 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5612 if (UINT_LEAST16_TYPE)
5613 uint_least16_type_node =
5614 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5615 if (UINT_LEAST32_TYPE)
5616 uint_least32_type_node =
5617 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5618 if (UINT_LEAST64_TYPE)
5619 uint_least64_type_node =
5620 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5622 int_fast8_type_node =
5623 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5624 if (INT_FAST16_TYPE)
5625 int_fast16_type_node =
5626 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5627 if (INT_FAST32_TYPE)
5628 int_fast32_type_node =
5629 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5630 if (INT_FAST64_TYPE)
5631 int_fast64_type_node =
5632 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5633 if (UINT_FAST8_TYPE)
5634 uint_fast8_type_node =
5635 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5636 if (UINT_FAST16_TYPE)
5637 uint_fast16_type_node =
5638 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5639 if (UINT_FAST32_TYPE)
5640 uint_fast32_type_node =
5641 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5642 if (UINT_FAST64_TYPE)
5643 uint_fast64_type_node =
5644 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5647 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5650 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5652 default_function_type
5653 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5655 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5656 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5658 lang_hooks.decls.pushdecl
5659 (build_decl (UNKNOWN_LOCATION,
5660 TYPE_DECL, get_identifier ("__builtin_va_list"),
5661 va_list_type_node));
5662 if (targetm.enum_va_list_p)
5668 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5670 lang_hooks.decls.pushdecl
5671 (build_decl (UNKNOWN_LOCATION,
5672 TYPE_DECL, get_identifier (pname),
5678 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5680 va_list_arg_type_node = va_list_ref_type_node =
5681 build_pointer_type (TREE_TYPE (va_list_type_node));
5685 va_list_arg_type_node = va_list_type_node;
5686 va_list_ref_type_node = build_reference_type (va_list_type_node);
5689 if (!flag_preprocess_only)
5690 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5692 main_identifier_node = get_identifier ("main");
5694 /* Create the built-in __null node. It is important that this is
5696 null_node = make_int_cst (1, 1);
5697 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5699 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5700 memset (builtin_types, 0, sizeof (builtin_types));
5703 /* The number of named compound-literals generated thus far. */
5704 static GTY(()) int compound_literal_number;
5706 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5709 set_compound_literal_name (tree decl)
5712 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5713 compound_literal_number);
5714 compound_literal_number++;
5715 DECL_NAME (decl) = get_identifier (name);
5718 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5719 TYPE and operand OP. */
5722 build_va_arg_1 (location_t loc, tree type, tree op)
5724 tree expr = build1 (VA_ARG_EXPR, type, op);
5725 SET_EXPR_LOCATION (expr, loc);
5729 /* Return a VA_ARG_EXPR corresponding to a source-level expression
5730 va_arg (EXPR, TYPE) at source location LOC. */
5733 build_va_arg (location_t loc, tree expr, tree type)
5735 tree va_type = TREE_TYPE (expr);
5736 tree canon_va_type = (va_type == error_mark_node
5738 : targetm.canonical_va_list_type (va_type));
5740 if (va_type == error_mark_node
5741 || canon_va_type == NULL_TREE)
5743 /* Let's handle things neutrallly, if expr:
5744 - has undeclared type, or
5745 - is not an va_list type. */
5746 return build_va_arg_1 (loc, type, expr);
5749 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5751 /* Case 1: Not an array type. */
5753 /* Take the address, to get '&ap'. */
5754 mark_addressable (expr);
5755 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5757 /* Verify that &ap is still recognized as having va_list type. */
5758 tree canon_expr_type
5759 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5760 if (canon_expr_type == NULL_TREE)
5763 "first argument to %<va_arg%> not of type %<va_list%>");
5764 return error_mark_node;
5767 return build_va_arg_1 (loc, type, expr);
5770 /* Case 2: Array type.
5774 For contrast, let's start with the simple case (case 1). If
5775 canon_va_type is not an array type, but say a char *, then when
5776 passing-by-value a va_list, the type of the va_list param decl is
5777 the same as for another va_list decl (all ap's are char *):
5780 D.1815 = VA_ARG (&ap, 0B, 1);
5786 __builtin_va_start (&ap, 0);
5789 __builtin_va_end (&ap);
5793 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5794 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5795 the same as for another va_list decl (case 2a, struct ap[1]).
5798 D.1844 = VA_ARG (ap, 0B, 0);
5803 __builtin_va_start (&ap, 0);
5805 __builtin_va_end (&ap);
5809 Case 2b is different because:
5810 - on the callee side, the parm decl has declared type va_list, but
5811 grokdeclarator changes the type of the parm decl to a pointer to the
5813 - on the caller side, the pass-by-value uses &ap.
5815 We unify these two cases (case 2a: va_list is array type,
5816 case 2b: va_list is pointer to array elem type), by adding '&' for the
5817 array type case, such that we have a pointer to array elem in both
5820 if (TREE_CODE (va_type) == ARRAY_TYPE)
5822 /* Case 2a: va_list is array type. */
5824 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5826 mark_addressable (expr);
5827 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5830 /* Verify that &ap is still recognized as having va_list type. */
5831 tree canon_expr_type
5832 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5833 if (canon_expr_type == NULL_TREE)
5836 "first argument to %<va_arg%> not of type %<va_list%>");
5837 return error_mark_node;
5842 /* Case 2b: va_list is pointer to array elem type. */
5843 gcc_assert (POINTER_TYPE_P (va_type));
5844 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
5846 /* Don't take the address. We've already got '&ap'. */
5850 return build_va_arg_1 (loc, type, expr);
5854 /* Linked list of disabled built-in functions. */
5856 struct disabled_builtin
5859 struct disabled_builtin *next;
5861 static disabled_builtin *disabled_builtins = NULL;
5863 static bool builtin_function_disabled_p (const char *);
5865 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5866 begins with "__builtin_", give an error. */
5869 disable_builtin_function (const char *name)
5871 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5872 error ("cannot disable built-in function %qs", name);
5875 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5876 new_disabled_builtin->name = name;
5877 new_disabled_builtin->next = disabled_builtins;
5878 disabled_builtins = new_disabled_builtin;
5883 /* Return true if the built-in function NAME has been disabled, false
5887 builtin_function_disabled_p (const char *name)
5889 disabled_builtin *p;
5890 for (p = disabled_builtins; p != NULL; p = p->next)
5892 if (strcmp (name, p->name) == 0)
5899 /* Worker for DEF_BUILTIN.
5900 Possibly define a builtin function with one or two names.
5901 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5902 nonansi_p and flag_no_nonansi_builtin. */
5905 def_builtin_1 (enum built_in_function fncode,
5907 enum built_in_class fnclass,
5908 tree fntype, tree libtype,
5909 bool both_p, bool fallback_p, bool nonansi_p,
5910 tree fnattrs, bool implicit_p)
5913 const char *libname;
5915 if (fntype == error_mark_node)
5918 gcc_assert ((!both_p && !fallback_p)
5919 || !strncmp (name, "__builtin_",
5920 strlen ("__builtin_")));
5922 libname = name + strlen ("__builtin_");
5923 decl = add_builtin_function (name, fntype, fncode, fnclass,
5924 (fallback_p ? libname : NULL),
5927 set_builtin_decl (fncode, decl, implicit_p);
5930 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5931 && !(nonansi_p && flag_no_nonansi_builtin))
5932 add_builtin_function (libname, libtype, fncode, fnclass,
5936 /* Nonzero if the type T promotes to int. This is (nearly) the
5937 integral promotions defined in ISO C99 6.3.1.1/2. */
5940 c_promoting_integer_type_p (const_tree t)
5942 switch (TREE_CODE (t))
5945 return (TYPE_MAIN_VARIANT (t) == char_type_node
5946 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5947 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5948 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5949 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5950 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5953 /* ??? Technically all enumerations not larger than an int
5954 promote to an int. But this is used along code paths
5955 that only want to notice a size change. */
5956 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5966 /* Return 1 if PARMS specifies a fixed number of parameters
5967 and none of their types is affected by default promotions. */
5970 self_promoting_args_p (const_tree parms)
5973 for (t = parms; t; t = TREE_CHAIN (t))
5975 tree type = TREE_VALUE (t);
5977 if (type == error_mark_node)
5980 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5986 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5989 if (c_promoting_integer_type_p (type))
5995 /* Recursively remove any '*' or '&' operator from TYPE. */
5997 strip_pointer_operator (tree t)
5999 while (POINTER_TYPE_P (t))
6004 /* Recursively remove pointer or array type from TYPE. */
6006 strip_pointer_or_array_types (tree t)
6008 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6013 /* Used to compare case labels. K1 and K2 are actually tree nodes
6014 representing case labels, or NULL_TREE for a `default' label.
6015 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6016 K2, and 0 if K1 and K2 are equal. */
6019 case_compare (splay_tree_key k1, splay_tree_key k2)
6021 /* Consider a NULL key (such as arises with a `default' label) to be
6022 smaller than anything else. */
6028 return tree_int_cst_compare ((tree) k1, (tree) k2);
6031 /* Process a case label, located at LOC, for the range LOW_VALUE
6032 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6033 then this case label is actually a `default' label. If only
6034 HIGH_VALUE is NULL_TREE, then case label was declared using the
6035 usual C/C++ syntax, rather than the GNU case range extension.
6036 CASES is a tree containing all the case ranges processed so far;
6037 COND is the condition for the switch-statement itself.
6038 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6039 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6040 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
6043 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6044 tree low_value, tree high_value, bool *outside_range_p)
6049 splay_tree_node node;
6051 /* Create the LABEL_DECL itself. */
6052 label = create_artificial_label (loc);
6054 /* If there was an error processing the switch condition, bail now
6055 before we get more confused. */
6056 if (!cond || cond == error_mark_node)
6059 if ((low_value && TREE_TYPE (low_value)
6060 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6061 || (high_value && TREE_TYPE (high_value)
6062 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6064 error_at (loc, "pointers are not permitted as case values");
6068 /* Case ranges are a GNU extension. */
6070 pedwarn (loc, OPT_Wpedantic,
6071 "range expressions in switch statements are non-standard");
6073 type = TREE_TYPE (cond);
6076 low_value = check_case_value (loc, low_value);
6077 low_value = convert_and_check (loc, type, low_value);
6078 if (low_value == error_mark_node)
6083 high_value = check_case_value (loc, high_value);
6084 high_value = convert_and_check (loc, type, high_value);
6085 if (high_value == error_mark_node)
6089 if (low_value && high_value)
6091 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6092 really a case range, even though it was written that way.
6093 Remove the HIGH_VALUE to simplify later processing. */
6094 if (tree_int_cst_equal (low_value, high_value))
6095 high_value = NULL_TREE;
6096 else if (!tree_int_cst_lt (low_value, high_value))
6097 warning_at (loc, 0, "empty range specified");
6100 /* See if the case is in range of the type of the original testing
6101 expression. If both low_value and high_value are out of range,
6102 don't insert the case label and return NULL_TREE. */
6104 && !check_case_bounds (loc, type, orig_type,
6105 &low_value, high_value ? &high_value : NULL,
6109 /* Look up the LOW_VALUE in the table of case labels we already
6111 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6112 /* If there was not an exact match, check for overlapping ranges.
6113 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6114 that's a `default' label and the only overlap is an exact match. */
6115 if (!node && (low_value || high_value))
6117 splay_tree_node low_bound;
6118 splay_tree_node high_bound;
6120 /* Even though there wasn't an exact match, there might be an
6121 overlap between this case range and another case range.
6122 Since we've (inductively) not allowed any overlapping case
6123 ranges, we simply need to find the greatest low case label
6124 that is smaller that LOW_VALUE, and the smallest low case
6125 label that is greater than LOW_VALUE. If there is an overlap
6126 it will occur in one of these two ranges. */
6127 low_bound = splay_tree_predecessor (cases,
6128 (splay_tree_key) low_value);
6129 high_bound = splay_tree_successor (cases,
6130 (splay_tree_key) low_value);
6132 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6133 the LOW_VALUE, so there is no need to check unless the
6134 LOW_BOUND is in fact itself a case range. */
6136 && CASE_HIGH ((tree) low_bound->value)
6137 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6140 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6141 range is bigger than the low end of the current range, so we
6142 are only interested if the current range is a real range, and
6143 not an ordinary case label. */
6146 && (tree_int_cst_compare ((tree) high_bound->key,
6151 /* If there was an overlap, issue an error. */
6154 tree duplicate = CASE_LABEL ((tree) node->value);
6158 error_at (loc, "duplicate (or overlapping) case value");
6159 error_at (DECL_SOURCE_LOCATION (duplicate),
6160 "this is the first entry overlapping that value");
6164 error_at (loc, "duplicate case value") ;
6165 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6169 error_at (loc, "multiple default labels in one switch");
6170 error_at (DECL_SOURCE_LOCATION (duplicate),
6171 "this is the first default label");
6176 /* Add a CASE_LABEL to the statement-tree. */
6177 case_label = add_stmt (build_case_label (low_value, high_value, label));
6178 /* Register this case label in the splay tree. */
6179 splay_tree_insert (cases,
6180 (splay_tree_key) low_value,
6181 (splay_tree_value) case_label);
6186 /* Add a label so that the back-end doesn't think that the beginning of
6187 the switch is unreachable. Note that we do not add a case label, as
6188 that just leads to duplicates and thence to failure later on. */
6191 tree t = create_artificial_label (loc);
6192 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6194 return error_mark_node;
6197 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6198 Used to verify that case values match up with enumerator values. */
6201 match_case_to_enum_1 (tree key, tree type, tree label)
6203 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6205 if (tree_fits_uhwi_p (key))
6206 print_dec (key, buf, UNSIGNED);
6207 else if (tree_fits_shwi_p (key))
6208 print_dec (key, buf, SIGNED);
6210 print_hex (key, buf);
6212 if (TYPE_NAME (type) == 0)
6213 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6214 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6215 "case value %qs not in enumerated type",
6218 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6219 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6220 "case value %qs not in enumerated type %qT",
6224 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6225 Used to verify that case values match up with enumerator values. */
6228 match_case_to_enum (splay_tree_node node, void *data)
6230 tree label = (tree) node->value;
6231 tree type = (tree) data;
6233 /* Skip default case. */
6234 if (!CASE_LOW (label))
6237 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6238 when we did our enum->case scan. Reset our scratch bit after. */
6239 if (!CASE_LOW_SEEN (label))
6240 match_case_to_enum_1 (CASE_LOW (label), type, label);
6242 CASE_LOW_SEEN (label) = 0;
6244 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6245 not set, that means that CASE_HIGH did not appear when we did our
6246 enum->case scan. Reset our scratch bit after. */
6247 if (CASE_HIGH (label))
6249 if (!CASE_HIGH_SEEN (label))
6250 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6252 CASE_HIGH_SEEN (label) = 0;
6258 /* Handle -Wswitch*. Called from the front end after parsing the
6259 switch construct. */
6260 /* ??? Should probably be somewhere generic, since other languages
6261 besides C and C++ would want this. At the moment, however, C/C++
6262 are the only tree-ssa languages that support enumerations at all,
6263 so the point is moot. */
6266 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6267 tree type, tree cond, bool bool_cond_p,
6268 bool outside_range_p)
6270 splay_tree_node default_node;
6271 splay_tree_node node;
6274 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6275 && !warn_switch_bool)
6278 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6280 warning_at (switch_location, OPT_Wswitch_default,
6281 "switch missing default case");
6283 /* There are certain cases where -Wswitch-bool warnings aren't
6290 so be careful here. */
6291 if (warn_switch_bool && bool_cond_p)
6293 splay_tree_node min_node;
6294 /* If there's a default node, it's also the value with the minimal
6295 key. So look at the penultimate key (if any). */
6297 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6299 min_node = splay_tree_min (cases);
6300 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6302 splay_tree_node max_node = splay_tree_max (cases);
6303 /* This might be a case range, so look at the value with the
6304 maximal key and then check CASE_HIGH. */
6305 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6307 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6309 /* If there's a case value > 1 or < 0, that is outside bool
6312 || (max && wi::gts_p (max, 1))
6313 || (min && wi::lts_p (min, 0))
6321 case, where we want to warn. */
6323 && max && wi::eq_p (max, 1)
6324 && min && wi::eq_p (min, 0)))
6325 warning_at (switch_location, OPT_Wswitch_bool,
6326 "switch condition has boolean value");
6329 /* From here on, we only care about enumerated types. */
6330 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6333 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6334 if (!warn_switch_enum && !warn_switch)
6337 /* Check the cases. Warn about case values which are not members of
6338 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6339 there is no default case, check that exactly all enumeration
6340 literals are covered by the cases. */
6342 /* Clearing COND if it is not an integer constant simplifies
6343 the tests inside the loop below. */
6344 if (TREE_CODE (cond) != INTEGER_CST)
6347 /* The time complexity here is O(N*lg(N)) worst case, but for the
6348 common case of monotonically increasing enumerators, it is
6349 O(N), since the nature of the splay tree will keep the next
6350 element adjacent to the root at all times. */
6352 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6354 tree value = TREE_VALUE (chain);
6355 if (TREE_CODE (value) == CONST_DECL)
6356 value = DECL_INITIAL (value);
6357 node = splay_tree_lookup (cases, (splay_tree_key) value);
6360 /* Mark the CASE_LOW part of the case entry as seen. */
6361 tree label = (tree) node->value;
6362 CASE_LOW_SEEN (label) = 1;
6366 /* Even though there wasn't an exact match, there might be a
6367 case range which includes the enumerator's value. */
6368 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6369 if (node && CASE_HIGH ((tree) node->value))
6371 tree label = (tree) node->value;
6372 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6375 /* If we match the upper bound exactly, mark the CASE_HIGH
6376 part of the case entry as seen. */
6378 CASE_HIGH_SEEN (label) = 1;
6383 /* We've now determined that this enumerated literal isn't
6384 handled by the case labels of the switch statement. */
6386 /* If the switch expression is a constant, we only really care
6387 about whether that constant is handled by the switch. */
6388 if (cond && tree_int_cst_compare (cond, value))
6391 /* If there is a default_node, the only relevant option is
6392 Wswitch-enum. Otherwise, if both are enabled then we prefer
6393 to warn using -Wswitch because -Wswitch is enabled by -Wall
6394 while -Wswitch-enum is explicit. */
6395 warning_at (switch_location,
6396 (default_node || !warn_switch
6399 "enumeration value %qE not handled in switch",
6400 TREE_PURPOSE (chain));
6403 /* Warn if there are case expressions that don't correspond to
6404 enumerators. This can occur since C and C++ don't enforce
6405 type-checking of assignments to enumeration variables.
6407 The time complexity here is now always O(N) worst case, since
6408 we should have marked both the lower bound and upper bound of
6409 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6410 above. This scan also resets those fields. */
6412 splay_tree_foreach (cases, match_case_to_enum, type);
6415 /* Finish an expression taking the address of LABEL (an
6416 IDENTIFIER_NODE). Returns an expression for the address.
6418 LOC is the location for the expression returned. */
6421 finish_label_address_expr (tree label, location_t loc)
6425 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6427 if (label == error_mark_node)
6428 return error_mark_node;
6430 label = lookup_label (label);
6431 if (label == NULL_TREE)
6432 result = null_pointer_node;
6435 TREE_USED (label) = 1;
6436 result = build1 (ADDR_EXPR, ptr_type_node, label);
6437 /* The current function is not necessarily uninlinable.
6438 Computed gotos are incompatible with inlining, but the value
6439 here could be used only in a diagnostic, for example. */
6440 protected_set_expr_location (result, loc);
6447 /* Given a boolean expression ARG, return a tree representing an increment
6448 or decrement (as indicated by CODE) of ARG. The front end must check for
6449 invalid cases (e.g., decrement in C++). */
6451 boolean_increment (enum tree_code code, tree arg)
6454 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6456 arg = stabilize_reference (arg);
6459 case PREINCREMENT_EXPR:
6460 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6462 case POSTINCREMENT_EXPR:
6463 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6464 arg = save_expr (arg);
6465 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6466 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6468 case PREDECREMENT_EXPR:
6469 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6470 invert_truthvalue_loc (input_location, arg));
6472 case POSTDECREMENT_EXPR:
6473 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6474 invert_truthvalue_loc (input_location, arg));
6475 arg = save_expr (arg);
6476 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6477 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6482 TREE_SIDE_EFFECTS (val) = 1;
6486 /* Built-in macros for stddef.h and stdint.h, that require macros
6487 defined in this file. */
6489 c_stddef_cpp_builtins(void)
6491 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6492 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6493 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6494 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6495 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6496 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6497 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6498 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6499 if (SIG_ATOMIC_TYPE)
6500 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6502 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6504 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6506 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6508 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6510 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6512 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6514 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6516 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6517 if (INT_LEAST8_TYPE)
6518 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6519 if (INT_LEAST16_TYPE)
6520 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6521 if (INT_LEAST32_TYPE)
6522 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6523 if (INT_LEAST64_TYPE)
6524 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6525 if (UINT_LEAST8_TYPE)
6526 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6527 if (UINT_LEAST16_TYPE)
6528 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6529 if (UINT_LEAST32_TYPE)
6530 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6531 if (UINT_LEAST64_TYPE)
6532 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6534 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6535 if (INT_FAST16_TYPE)
6536 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6537 if (INT_FAST32_TYPE)
6538 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6539 if (INT_FAST64_TYPE)
6540 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6541 if (UINT_FAST8_TYPE)
6542 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6543 if (UINT_FAST16_TYPE)
6544 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6545 if (UINT_FAST32_TYPE)
6546 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6547 if (UINT_FAST64_TYPE)
6548 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6550 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6552 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6556 c_init_attributes (void)
6558 /* Fill in the built_in_attributes array. */
6559 #define DEF_ATTR_NULL_TREE(ENUM) \
6560 built_in_attributes[(int) ENUM] = NULL_TREE;
6561 #define DEF_ATTR_INT(ENUM, VALUE) \
6562 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6563 #define DEF_ATTR_STRING(ENUM, VALUE) \
6564 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6565 #define DEF_ATTR_IDENT(ENUM, STRING) \
6566 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6567 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6568 built_in_attributes[(int) ENUM] \
6569 = tree_cons (built_in_attributes[(int) PURPOSE], \
6570 built_in_attributes[(int) VALUE], \
6571 built_in_attributes[(int) CHAIN]);
6572 #include "builtin-attrs.def"
6573 #undef DEF_ATTR_NULL_TREE
6575 #undef DEF_ATTR_IDENT
6576 #undef DEF_ATTR_TREE_LIST
6579 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6580 identifier as an argument, so the front end shouldn't look it up. */
6583 attribute_takes_identifier_p (const_tree attr_id)
6585 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6587 /* Unknown attribute that we'll end up ignoring, return true so we
6588 don't complain about an identifier argument. */
6590 else if (!strcmp ("mode", spec->name)
6591 || !strcmp ("format", spec->name)
6592 || !strcmp ("cleanup", spec->name))
6595 return targetm.attribute_takes_identifier_p (attr_id);
6598 /* Attribute handlers common to C front ends. */
6600 /* Handle a "packed" attribute; arguments as in
6601 struct attribute_spec.handler. */
6604 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6605 int flags, bool *no_add_attrs)
6609 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6610 *node = build_variant_type_copy (*node);
6611 TYPE_PACKED (*node) = 1;
6613 else if (TREE_CODE (*node) == FIELD_DECL)
6615 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6616 /* Still pack bitfields. */
6617 && ! DECL_INITIAL (*node))
6618 warning (OPT_Wattributes,
6619 "%qE attribute ignored for field of type %qT",
6620 name, TREE_TYPE (*node));
6622 DECL_PACKED (*node) = 1;
6624 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6625 used for DECL_REGISTER. It wouldn't mean anything anyway.
6626 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6627 that changes what the typedef is typing. */
6630 warning (OPT_Wattributes, "%qE attribute ignored", name);
6631 *no_add_attrs = true;
6637 /* Handle a "nocommon" attribute; arguments as in
6638 struct attribute_spec.handler. */
6641 handle_nocommon_attribute (tree *node, tree name,
6642 tree ARG_UNUSED (args),
6643 int ARG_UNUSED (flags), bool *no_add_attrs)
6646 DECL_COMMON (*node) = 0;
6649 warning (OPT_Wattributes, "%qE attribute ignored", name);
6650 *no_add_attrs = true;
6656 /* Handle a "common" attribute; arguments as in
6657 struct attribute_spec.handler. */
6660 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6661 int ARG_UNUSED (flags), bool *no_add_attrs)
6664 DECL_COMMON (*node) = 1;
6667 warning (OPT_Wattributes, "%qE attribute ignored", name);
6668 *no_add_attrs = true;
6674 /* Handle a "noreturn" attribute; arguments as in
6675 struct attribute_spec.handler. */
6678 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6679 int ARG_UNUSED (flags), bool *no_add_attrs)
6681 tree type = TREE_TYPE (*node);
6683 /* See FIXME comment in c_common_attribute_table. */
6684 if (TREE_CODE (*node) == FUNCTION_DECL
6685 || objc_method_decl (TREE_CODE (*node)))
6686 TREE_THIS_VOLATILE (*node) = 1;
6687 else if (TREE_CODE (type) == POINTER_TYPE
6688 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6690 = (build_qualified_type
6692 (build_type_variant (TREE_TYPE (type),
6693 TYPE_READONLY (TREE_TYPE (type)), 1)),
6694 TYPE_QUALS (type)));
6697 warning (OPT_Wattributes, "%qE attribute ignored", name);
6698 *no_add_attrs = true;
6704 /* Handle a "hot" and attribute; arguments as in
6705 struct attribute_spec.handler. */
6708 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6709 int ARG_UNUSED (flags), bool *no_add_attrs)
6711 if (TREE_CODE (*node) == FUNCTION_DECL
6712 || TREE_CODE (*node) == LABEL_DECL)
6714 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6716 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6717 "with attribute %qs", name, "cold");
6718 *no_add_attrs = true;
6720 /* Most of the rest of the hot processing is done later with
6721 lookup_attribute. */
6725 warning (OPT_Wattributes, "%qE attribute ignored", name);
6726 *no_add_attrs = true;
6732 /* Handle a "cold" and attribute; arguments as in
6733 struct attribute_spec.handler. */
6736 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6737 int ARG_UNUSED (flags), bool *no_add_attrs)
6739 if (TREE_CODE (*node) == FUNCTION_DECL
6740 || TREE_CODE (*node) == LABEL_DECL)
6742 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6744 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6745 "with attribute %qs", name, "hot");
6746 *no_add_attrs = true;
6748 /* Most of the rest of the cold processing is done later with
6749 lookup_attribute. */
6753 warning (OPT_Wattributes, "%qE attribute ignored", name);
6754 *no_add_attrs = true;
6760 /* Handle a "no_sanitize_address" attribute; arguments as in
6761 struct attribute_spec.handler. */
6764 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6767 if (TREE_CODE (*node) != FUNCTION_DECL)
6769 warning (OPT_Wattributes, "%qE attribute ignored", name);
6770 *no_add_attrs = true;
6776 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6777 struct attribute_spec.handler. */
6780 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6783 if (TREE_CODE (*node) != FUNCTION_DECL)
6784 warning (OPT_Wattributes, "%qE attribute ignored", name);
6785 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6786 DECL_ATTRIBUTES (*node)
6787 = tree_cons (get_identifier ("no_sanitize_address"),
6788 NULL_TREE, DECL_ATTRIBUTES (*node));
6789 *no_add_attrs = true;
6793 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6794 struct attribute_spec.handler. */
6797 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6800 if (TREE_CODE (*node) != FUNCTION_DECL)
6802 warning (OPT_Wattributes, "%qE attribute ignored", name);
6803 *no_add_attrs = true;
6809 /* Handle a "stack_protect" attribute; arguments as in
6810 struct attribute_spec.handler. */
6812 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6815 if (TREE_CODE (*node) != FUNCTION_DECL)
6817 warning (OPT_Wattributes, "%qE attribute ignored", name);
6818 *no_add_attrs = true;
6821 DECL_ATTRIBUTES (*node)
6822 = tree_cons (get_identifier ("stack_protect"),
6823 NULL_TREE, DECL_ATTRIBUTES (*node));
6828 /* Handle a "noinline" attribute; arguments as in
6829 struct attribute_spec.handler. */
6832 handle_noinline_attribute (tree *node, tree name,
6833 tree ARG_UNUSED (args),
6834 int ARG_UNUSED (flags), bool *no_add_attrs)
6836 if (TREE_CODE (*node) == FUNCTION_DECL)
6838 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6840 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6841 "with attribute %qs", name, "always_inline");
6842 *no_add_attrs = true;
6845 DECL_UNINLINABLE (*node) = 1;
6849 warning (OPT_Wattributes, "%qE attribute ignored", name);
6850 *no_add_attrs = true;
6856 /* Handle a "noclone" attribute; arguments as in
6857 struct attribute_spec.handler. */
6860 handle_noclone_attribute (tree *node, tree name,
6861 tree ARG_UNUSED (args),
6862 int ARG_UNUSED (flags), bool *no_add_attrs)
6864 if (TREE_CODE (*node) != FUNCTION_DECL)
6866 warning (OPT_Wattributes, "%qE attribute ignored", name);
6867 *no_add_attrs = true;
6873 /* Handle a "no_icf" attribute; arguments as in
6874 struct attribute_spec.handler. */
6877 handle_noicf_attribute (tree *node, tree name,
6878 tree ARG_UNUSED (args),
6879 int ARG_UNUSED (flags), bool *no_add_attrs)
6881 if (TREE_CODE (*node) != FUNCTION_DECL)
6883 warning (OPT_Wattributes, "%qE attribute ignored", name);
6884 *no_add_attrs = true;
6891 /* Handle a "always_inline" attribute; arguments as in
6892 struct attribute_spec.handler. */
6895 handle_always_inline_attribute (tree *node, tree name,
6896 tree ARG_UNUSED (args),
6897 int ARG_UNUSED (flags),
6900 if (TREE_CODE (*node) == FUNCTION_DECL)
6902 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6904 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6905 "with %qs attribute", name, "noinline");
6906 *no_add_attrs = true;
6908 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
6910 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6911 "with %qs attribute", name, "target_clones");
6912 *no_add_attrs = true;
6915 /* Set the attribute and mark it for disregarding inline
6917 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6921 warning (OPT_Wattributes, "%qE attribute ignored", name);
6922 *no_add_attrs = true;
6928 /* Handle a "gnu_inline" attribute; arguments as in
6929 struct attribute_spec.handler. */
6932 handle_gnu_inline_attribute (tree *node, tree name,
6933 tree ARG_UNUSED (args),
6934 int ARG_UNUSED (flags),
6937 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6939 /* Do nothing else, just set the attribute. We'll get at
6940 it later with lookup_attribute. */
6944 warning (OPT_Wattributes, "%qE attribute ignored", name);
6945 *no_add_attrs = true;
6951 /* Handle a "leaf" attribute; arguments as in
6952 struct attribute_spec.handler. */
6955 handle_leaf_attribute (tree *node, tree name,
6956 tree ARG_UNUSED (args),
6957 int ARG_UNUSED (flags), bool *no_add_attrs)
6959 if (TREE_CODE (*node) != FUNCTION_DECL)
6961 warning (OPT_Wattributes, "%qE attribute ignored", name);
6962 *no_add_attrs = true;
6964 if (!TREE_PUBLIC (*node))
6966 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6967 *no_add_attrs = true;
6973 /* Handle an "artificial" attribute; arguments as in
6974 struct attribute_spec.handler. */
6977 handle_artificial_attribute (tree *node, tree name,
6978 tree ARG_UNUSED (args),
6979 int ARG_UNUSED (flags),
6982 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6984 /* Do nothing else, just set the attribute. We'll get at
6985 it later with lookup_attribute. */
6989 warning (OPT_Wattributes, "%qE attribute ignored", name);
6990 *no_add_attrs = true;
6996 /* Handle a "flatten" attribute; arguments as in
6997 struct attribute_spec.handler. */
7000 handle_flatten_attribute (tree *node, tree name,
7001 tree args ATTRIBUTE_UNUSED,
7002 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7004 if (TREE_CODE (*node) == FUNCTION_DECL)
7005 /* Do nothing else, just set the attribute. We'll get at
7006 it later with lookup_attribute. */
7010 warning (OPT_Wattributes, "%qE attribute ignored", name);
7011 *no_add_attrs = true;
7017 /* Handle a "warning" or "error" attribute; arguments as in
7018 struct attribute_spec.handler. */
7021 handle_error_attribute (tree *node, tree name, tree args,
7022 int ARG_UNUSED (flags), bool *no_add_attrs)
7024 if (TREE_CODE (*node) == FUNCTION_DECL
7025 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7026 /* Do nothing else, just set the attribute. We'll get at
7027 it later with lookup_attribute. */
7031 warning (OPT_Wattributes, "%qE attribute ignored", name);
7032 *no_add_attrs = true;
7038 /* Handle a "used" attribute; arguments as in
7039 struct attribute_spec.handler. */
7042 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7043 int ARG_UNUSED (flags), bool *no_add_attrs)
7047 if (TREE_CODE (node) == FUNCTION_DECL
7048 || (VAR_P (node) && TREE_STATIC (node))
7049 || (TREE_CODE (node) == TYPE_DECL))
7051 TREE_USED (node) = 1;
7052 DECL_PRESERVE_P (node) = 1;
7054 DECL_READ_P (node) = 1;
7058 warning (OPT_Wattributes, "%qE attribute ignored", name);
7059 *no_add_attrs = true;
7065 /* Handle a "unused" attribute; arguments as in
7066 struct attribute_spec.handler. */
7069 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7070 int flags, bool *no_add_attrs)
7076 if (TREE_CODE (decl) == PARM_DECL
7077 || VAR_OR_FUNCTION_DECL_P (decl)
7078 || TREE_CODE (decl) == LABEL_DECL
7079 || TREE_CODE (decl) == TYPE_DECL)
7081 TREE_USED (decl) = 1;
7082 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
7083 DECL_READ_P (decl) = 1;
7087 warning (OPT_Wattributes, "%qE attribute ignored", name);
7088 *no_add_attrs = true;
7093 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7094 *node = build_variant_type_copy (*node);
7095 TREE_USED (*node) = 1;
7101 /* Handle a "externally_visible" attribute; arguments as in
7102 struct attribute_spec.handler. */
7105 handle_externally_visible_attribute (tree *pnode, tree name,
7106 tree ARG_UNUSED (args),
7107 int ARG_UNUSED (flags),
7112 if (VAR_OR_FUNCTION_DECL_P (node))
7114 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7115 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7117 warning (OPT_Wattributes,
7118 "%qE attribute have effect only on public objects", name);
7119 *no_add_attrs = true;
7124 warning (OPT_Wattributes, "%qE attribute ignored", name);
7125 *no_add_attrs = true;
7131 /* Handle the "no_reorder" attribute. Arguments as in
7132 struct attribute_spec.handler. */
7135 handle_no_reorder_attribute (tree *pnode,
7143 if (!VAR_OR_FUNCTION_DECL_P (node)
7144 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7146 warning (OPT_Wattributes,
7147 "%qE attribute only affects top level objects",
7149 *no_add_attrs = true;
7155 /* Handle a "const" attribute; arguments as in
7156 struct attribute_spec.handler. */
7159 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7160 int ARG_UNUSED (flags), bool *no_add_attrs)
7162 tree type = TREE_TYPE (*node);
7164 /* See FIXME comment on noreturn in c_common_attribute_table. */
7165 if (TREE_CODE (*node) == FUNCTION_DECL)
7166 TREE_READONLY (*node) = 1;
7167 else if (TREE_CODE (type) == POINTER_TYPE
7168 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7170 = (build_qualified_type
7172 (build_type_variant (TREE_TYPE (type), 1,
7173 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7174 TYPE_QUALS (type)));
7177 warning (OPT_Wattributes, "%qE attribute ignored", name);
7178 *no_add_attrs = true;
7184 /* Handle a "scalar_storage_order" attribute; arguments as in
7185 struct attribute_spec.handler. */
7188 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7189 int flags, bool *no_add_attrs)
7191 tree id = TREE_VALUE (args);
7194 if (TREE_CODE (*node) == TYPE_DECL
7195 && ! (flags & ATTR_FLAG_CXX11))
7196 node = &TREE_TYPE (*node);
7199 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7201 error ("scalar_storage_order is not supported because endianness "
7206 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7208 bool reverse = false;
7210 if (TREE_CODE (id) == STRING_CST
7211 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7212 reverse = !BYTES_BIG_ENDIAN;
7213 else if (TREE_CODE (id) == STRING_CST
7214 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7215 reverse = BYTES_BIG_ENDIAN;
7218 error ("scalar_storage_order argument must be one of \"big-endian\""
7219 " or \"little-endian\"");
7223 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7226 /* A type variant isn't good enough, since we don't want a cast
7227 to such a type to be removed as a no-op. */
7228 *node = type = build_duplicate_type (type);
7231 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7235 warning (OPT_Wattributes, "%qE attribute ignored", name);
7236 *no_add_attrs = true;
7240 /* Handle a "transparent_union" attribute; arguments as in
7241 struct attribute_spec.handler. */
7244 handle_transparent_union_attribute (tree *node, tree name,
7245 tree ARG_UNUSED (args), int flags,
7250 *no_add_attrs = true;
7252 if (TREE_CODE (*node) == TYPE_DECL
7253 && ! (flags & ATTR_FLAG_CXX11))
7254 node = &TREE_TYPE (*node);
7257 if (TREE_CODE (type) == UNION_TYPE)
7259 /* Make sure that the first field will work for a transparent union.
7260 If the type isn't complete yet, leave the check to the code in
7262 if (TYPE_SIZE (type))
7264 tree first = first_field (type);
7265 if (first == NULL_TREE
7266 || DECL_ARTIFICIAL (first)
7267 || TYPE_MODE (type) != DECL_MODE (first))
7271 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7273 /* If the type isn't complete yet, setting the flag
7274 on a variant wouldn't ever be checked. */
7275 if (!TYPE_SIZE (type))
7278 /* build_duplicate_type doesn't work for C++. */
7279 if (c_dialect_cxx ())
7282 /* A type variant isn't good enough, since we don't want a cast
7283 to such a type to be removed as a no-op. */
7284 *node = type = build_duplicate_type (type);
7287 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7288 TYPE_TRANSPARENT_AGGR (t) = 1;
7293 warning (OPT_Wattributes, "%qE attribute ignored", name);
7297 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7298 get the requested priority for a constructor or destructor,
7299 possibly issuing diagnostics for invalid or reserved
7302 static priority_type
7303 get_priority (tree args, bool is_destructor)
7309 return DEFAULT_INIT_PRIORITY;
7311 if (!SUPPORTS_INIT_PRIORITY)
7314 error ("destructor priorities are not supported");
7316 error ("constructor priorities are not supported");
7317 return DEFAULT_INIT_PRIORITY;
7320 arg = TREE_VALUE (args);
7321 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7323 if (arg == error_mark_node)
7324 return DEFAULT_INIT_PRIORITY;
7325 arg = default_conversion (arg);
7326 if (!tree_fits_shwi_p (arg)
7327 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7330 pri = tree_to_shwi (arg);
7331 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7334 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7338 "destructor priorities from 0 to %d are reserved "
7339 "for the implementation",
7340 MAX_RESERVED_INIT_PRIORITY);
7343 "constructor priorities from 0 to %d are reserved "
7344 "for the implementation",
7345 MAX_RESERVED_INIT_PRIORITY);
7351 error ("destructor priorities must be integers from 0 to %d inclusive",
7354 error ("constructor priorities must be integers from 0 to %d inclusive",
7356 return DEFAULT_INIT_PRIORITY;
7359 /* Handle a "constructor" attribute; arguments as in
7360 struct attribute_spec.handler. */
7363 handle_constructor_attribute (tree *node, tree name, tree args,
7364 int ARG_UNUSED (flags),
7368 tree type = TREE_TYPE (decl);
7370 if (TREE_CODE (decl) == FUNCTION_DECL
7371 && TREE_CODE (type) == FUNCTION_TYPE
7372 && decl_function_context (decl) == 0)
7374 priority_type priority;
7375 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7376 priority = get_priority (args, /*is_destructor=*/false);
7377 SET_DECL_INIT_PRIORITY (decl, priority);
7378 TREE_USED (decl) = 1;
7382 warning (OPT_Wattributes, "%qE attribute ignored", name);
7383 *no_add_attrs = true;
7389 /* Handle a "destructor" attribute; arguments as in
7390 struct attribute_spec.handler. */
7393 handle_destructor_attribute (tree *node, tree name, tree args,
7394 int ARG_UNUSED (flags),
7398 tree type = TREE_TYPE (decl);
7400 if (TREE_CODE (decl) == FUNCTION_DECL
7401 && TREE_CODE (type) == FUNCTION_TYPE
7402 && decl_function_context (decl) == 0)
7404 priority_type priority;
7405 DECL_STATIC_DESTRUCTOR (decl) = 1;
7406 priority = get_priority (args, /*is_destructor=*/true);
7407 SET_DECL_FINI_PRIORITY (decl, priority);
7408 TREE_USED (decl) = 1;
7412 warning (OPT_Wattributes, "%qE attribute ignored", name);
7413 *no_add_attrs = true;
7419 /* Nonzero if the mode is a valid vector mode for this architecture.
7420 This returns nonzero even if there is no hardware support for the
7421 vector mode, but we can emulate with narrower modes. */
7424 vector_mode_valid_p (machine_mode mode)
7426 enum mode_class mclass = GET_MODE_CLASS (mode);
7427 machine_mode innermode;
7429 /* Doh! What's going on? */
7430 if (mclass != MODE_VECTOR_INT
7431 && mclass != MODE_VECTOR_FLOAT
7432 && mclass != MODE_VECTOR_FRACT
7433 && mclass != MODE_VECTOR_UFRACT
7434 && mclass != MODE_VECTOR_ACCUM
7435 && mclass != MODE_VECTOR_UACCUM)
7438 /* Hardware support. Woo hoo! */
7439 if (targetm.vector_mode_supported_p (mode))
7442 innermode = GET_MODE_INNER (mode);
7444 /* We should probably return 1 if requesting V4DI and we have no DI,
7445 but we have V2DI, but this is probably very unlikely. */
7447 /* If we have support for the inner mode, we can safely emulate it.
7448 We may not have V2DI, but me can emulate with a pair of DIs. */
7449 return targetm.scalar_mode_supported_p (innermode);
7453 /* Handle a "mode" attribute; arguments as in
7454 struct attribute_spec.handler. */
7457 handle_mode_attribute (tree *node, tree name, tree args,
7458 int ARG_UNUSED (flags), bool *no_add_attrs)
7461 tree ident = TREE_VALUE (args);
7463 *no_add_attrs = true;
7465 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7466 warning (OPT_Wattributes, "%qE attribute ignored", name);
7470 const char *p = IDENTIFIER_POINTER (ident);
7471 int len = strlen (p);
7472 machine_mode mode = VOIDmode;
7476 if (len > 4 && p[0] == '_' && p[1] == '_'
7477 && p[len - 1] == '_' && p[len - 2] == '_')
7479 char *newp = (char *) alloca (len - 1);
7481 strcpy (newp, &p[2]);
7482 newp[len - 4] = '\0';
7486 /* Change this type to have a type with the specified mode.
7487 First check for the special modes. */
7488 if (!strcmp (p, "byte"))
7490 else if (!strcmp (p, "word"))
7492 else if (!strcmp (p, "pointer"))
7494 else if (!strcmp (p, "libgcc_cmp_return"))
7495 mode = targetm.libgcc_cmp_return_mode ();
7496 else if (!strcmp (p, "libgcc_shift_count"))
7497 mode = targetm.libgcc_shift_count_mode ();
7498 else if (!strcmp (p, "unwind_word"))
7499 mode = targetm.unwind_word_mode ();
7501 for (j = 0; j < NUM_MACHINE_MODES; j++)
7502 if (!strcmp (p, GET_MODE_NAME (j)))
7504 mode = (machine_mode) j;
7508 if (mode == VOIDmode)
7510 error ("unknown machine mode %qE", ident);
7515 switch (GET_MODE_CLASS (mode))
7518 case MODE_PARTIAL_INT:
7520 case MODE_DECIMAL_FLOAT:
7525 valid_mode = targetm.scalar_mode_supported_p (mode);
7528 case MODE_COMPLEX_INT:
7529 case MODE_COMPLEX_FLOAT:
7530 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7533 case MODE_VECTOR_INT:
7534 case MODE_VECTOR_FLOAT:
7535 case MODE_VECTOR_FRACT:
7536 case MODE_VECTOR_UFRACT:
7537 case MODE_VECTOR_ACCUM:
7538 case MODE_VECTOR_UACCUM:
7539 warning (OPT_Wattributes, "specifying vector types with "
7540 "__attribute__ ((mode)) is deprecated");
7541 warning (OPT_Wattributes,
7542 "use __attribute__ ((vector_size)) instead");
7543 valid_mode = vector_mode_valid_p (mode);
7551 error ("unable to emulate %qs", p);
7555 if (POINTER_TYPE_P (type))
7557 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7558 tree (*fn)(tree, machine_mode, bool);
7560 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7562 error ("invalid pointer mode %qs", p);
7566 if (TREE_CODE (type) == POINTER_TYPE)
7567 fn = build_pointer_type_for_mode;
7569 fn = build_reference_type_for_mode;
7570 typefm = fn (TREE_TYPE (type), mode, false);
7574 /* For fixed-point modes, we need to test if the signness of type
7575 and the machine mode are consistent. */
7576 if (ALL_FIXED_POINT_MODE_P (mode)
7577 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7579 error ("signedness of type and machine mode %qs don%'t match", p);
7582 /* For fixed-point modes, we need to pass saturating info. */
7583 typefm = lang_hooks.types.type_for_mode (mode,
7584 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7585 : TYPE_UNSIGNED (type));
7588 if (typefm == NULL_TREE)
7590 error ("no data type for mode %qs", p);
7593 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7595 /* For enumeral types, copy the precision from the integer
7596 type returned above. If not an INTEGER_TYPE, we can't use
7597 this mode for this type. */
7598 if (TREE_CODE (typefm) != INTEGER_TYPE)
7600 error ("cannot use mode %qs for enumeral types", p);
7604 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7606 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7611 /* We cannot build a type variant, as there's code that assumes
7612 that TYPE_MAIN_VARIANT has the same mode. This includes the
7613 debug generators. Instead, create a subrange type. This
7614 results in all of the enumeral values being emitted only once
7615 in the original, and the subtype gets them by reference. */
7616 if (TYPE_UNSIGNED (type))
7617 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7619 typefm = make_signed_type (TYPE_PRECISION (typefm));
7620 TREE_TYPE (typefm) = type;
7623 else if (VECTOR_MODE_P (mode)
7624 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7625 : TREE_CODE (type) != TREE_CODE (typefm))
7627 error ("mode %qs applied to inappropriate type", p);
7637 /* Handle a "section" attribute; arguments as in
7638 struct attribute_spec.handler. */
7641 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7642 int ARG_UNUSED (flags), bool *no_add_attrs)
7646 if (!targetm_common.have_named_sections)
7648 error_at (DECL_SOURCE_LOCATION (*node),
7649 "section attributes are not supported for this target");
7653 user_defined_section_attribute = true;
7655 if (!VAR_OR_FUNCTION_DECL_P (decl))
7657 error ("section attribute not allowed for %q+D", *node);
7661 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7663 error ("section attribute argument not a string constant");
7668 && current_function_decl != NULL_TREE
7669 && !TREE_STATIC (decl))
7671 error_at (DECL_SOURCE_LOCATION (decl),
7672 "section attribute cannot be specified for local variables");
7676 /* The decl may have already been given a section attribute
7677 from a previous declaration. Ensure they match. */
7678 if (DECL_SECTION_NAME (decl) != NULL
7679 && strcmp (DECL_SECTION_NAME (decl),
7680 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7682 error ("section of %q+D conflicts with previous declaration", *node);
7687 && !targetm.have_tls && targetm.emutls.tmpl_section
7688 && DECL_THREAD_LOCAL_P (decl))
7690 error ("section of %q+D cannot be overridden", *node);
7694 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7698 *no_add_attrs = true;
7702 /* Check whether ALIGN is a valid user-specified alignment. If so,
7703 return its base-2 log; if not, output an error and return -1. If
7704 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7707 check_user_alignment (const_tree align, bool allow_zero)
7711 if (error_operand_p (align))
7713 if (TREE_CODE (align) != INTEGER_CST
7714 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7716 error ("requested alignment is not an integer constant");
7719 else if (allow_zero && integer_zerop (align))
7721 else if (tree_int_cst_sgn (align) == -1
7722 || (i = tree_log2 (align)) == -1)
7724 error ("requested alignment is not a positive power of 2");
7727 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7729 error ("requested alignment is too large");
7736 If in c++-11, check if the c++-11 alignment constraint with respect
7737 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7738 c++-11 mode, does nothing.
7742 [* if the constant expression evaluates to a fundamental alignment,
7743 the alignment requirement of the declared entity shall be the
7744 specified fundamental alignment.
7746 * if the constant expression evaluates to an extended alignment
7747 and the implementation supports that alignment in the context
7748 of the declaration, the alignment of the declared entity shall
7751 * if the constant expression evaluates to an extended alignment
7752 and the implementation does not support that alignment in the
7753 context of the declaration, the program is ill-formed]. */
7756 check_cxx_fundamental_alignment_constraints (tree node,
7760 bool alignment_too_large_p = false;
7761 unsigned requested_alignment = 1U << align_log;
7762 unsigned max_align = 0;
7764 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7765 || (node == NULL_TREE || node == error_mark_node))
7768 if (cxx_fundamental_alignment_p (requested_alignment))
7773 if (TREE_STATIC (node))
7775 /* For file scope variables and static members, the target
7776 supports alignments that are at most
7777 MAX_OFILE_ALIGNMENT. */
7778 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7779 alignment_too_large_p = true;
7783 #ifdef BIGGEST_FIELD_ALIGNMENT
7784 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7786 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7788 /* For non-static members, the target supports either
7789 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7790 if it is defined or BIGGEST_ALIGNMENT. */
7791 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7792 if (TREE_CODE (node) == FIELD_DECL
7793 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7794 alignment_too_large_p = true;
7795 #undef MAX_TARGET_FIELD_ALIGNMENT
7796 /* For stack variables, the target supports at most
7797 MAX_STACK_ALIGNMENT. */
7798 else if (decl_function_context (node) != NULL
7799 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7800 alignment_too_large_p = true;
7803 else if (TYPE_P (node))
7805 /* Let's be liberal for types. */
7806 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7807 alignment_too_large_p = true;
7810 if (alignment_too_large_p)
7811 pedwarn (input_location, OPT_Wattributes,
7812 "requested alignment %d is larger than %d",
7813 requested_alignment, max_align);
7815 return !alignment_too_large_p;
7818 /* Handle a "aligned" attribute; arguments as in
7819 struct attribute_spec.handler. */
7822 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7823 int flags, bool *no_add_attrs)
7825 tree decl = NULL_TREE;
7833 align_expr = TREE_VALUE (args);
7834 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7835 && TREE_CODE (align_expr) != FUNCTION_DECL)
7836 align_expr = default_conversion (align_expr);
7839 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7844 type = &TREE_TYPE (decl);
7845 is_type = TREE_CODE (*node) == TYPE_DECL;
7847 else if (TYPE_P (*node))
7848 type = node, is_type = 1;
7850 if ((i = check_user_alignment (align_expr, false)) == -1
7851 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7852 *no_add_attrs = true;
7855 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7856 /* OK, modify the type in place. */;
7857 /* If we have a TYPE_DECL, then copy the type, so that we
7858 don't accidentally modify a builtin type. See pushdecl. */
7859 else if (decl && TREE_TYPE (decl) != error_mark_node
7860 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7862 tree tt = TREE_TYPE (decl);
7863 *type = build_variant_type_copy (*type);
7864 DECL_ORIGINAL_TYPE (decl) = tt;
7865 TYPE_NAME (*type) = decl;
7866 TREE_USED (*type) = TREE_USED (decl);
7867 TREE_TYPE (decl) = *type;
7870 *type = build_variant_type_copy (*type);
7872 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7873 TYPE_USER_ALIGN (*type) = 1;
7875 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7876 && TREE_CODE (decl) != FIELD_DECL)
7878 error ("alignment may not be specified for %q+D", decl);
7879 *no_add_attrs = true;
7881 else if (DECL_USER_ALIGN (decl)
7882 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7883 /* C++-11 [dcl.align/4]:
7885 When multiple alignment-specifiers are specified for an
7886 entity, the alignment requirement shall be set to the
7887 strictest specified alignment.
7889 This formally comes from the c++11 specification but we are
7890 doing it for the GNU attribute syntax as well. */
7891 *no_add_attrs = true;
7892 else if (TREE_CODE (decl) == FUNCTION_DECL
7893 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7895 if (DECL_USER_ALIGN (decl))
7896 error ("alignment for %q+D was previously specified as %d "
7897 "and may not be decreased", decl,
7898 DECL_ALIGN (decl) / BITS_PER_UNIT);
7900 error ("alignment for %q+D must be at least %d", decl,
7901 DECL_ALIGN (decl) / BITS_PER_UNIT);
7902 *no_add_attrs = true;
7906 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7907 DECL_USER_ALIGN (decl) = 1;
7913 /* Handle a "weak" attribute; arguments as in
7914 struct attribute_spec.handler. */
7917 handle_weak_attribute (tree *node, tree name,
7918 tree ARG_UNUSED (args),
7919 int ARG_UNUSED (flags),
7920 bool * ARG_UNUSED (no_add_attrs))
7922 if (TREE_CODE (*node) == FUNCTION_DECL
7923 && DECL_DECLARED_INLINE_P (*node))
7925 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7926 *no_add_attrs = true;
7928 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7930 error ("indirect function %q+D cannot be declared weak", *node);
7931 *no_add_attrs = true;
7934 else if (VAR_OR_FUNCTION_DECL_P (*node))
7935 declare_weak (*node);
7937 warning (OPT_Wattributes, "%qE attribute ignored", name);
7942 /* Handle a "noplt" attribute; arguments as in
7943 struct attribute_spec.handler. */
7946 handle_noplt_attribute (tree *node, tree name,
7947 tree ARG_UNUSED (args),
7948 int ARG_UNUSED (flags),
7949 bool * ARG_UNUSED (no_add_attrs))
7951 if (TREE_CODE (*node) != FUNCTION_DECL)
7953 warning (OPT_Wattributes,
7954 "%qE attribute is only applicable on functions", name);
7955 *no_add_attrs = true;
7961 /* Handle an "alias" or "ifunc" attribute; arguments as in
7962 struct attribute_spec.handler, except that IS_ALIAS tells us
7963 whether this is an alias as opposed to ifunc attribute. */
7966 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7971 if (TREE_CODE (decl) != FUNCTION_DECL
7972 && (!is_alias || !VAR_P (decl)))
7974 warning (OPT_Wattributes, "%qE attribute ignored", name);
7975 *no_add_attrs = true;
7977 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7978 || (TREE_CODE (decl) != FUNCTION_DECL
7979 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7980 /* A static variable declaration is always a tentative definition,
7981 but the alias is a non-tentative definition which overrides. */
7982 || (TREE_CODE (decl) != FUNCTION_DECL
7983 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7985 error ("%q+D defined both normally and as %qE attribute", decl, name);
7986 *no_add_attrs = true;
7990 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7991 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7993 error ("weak %q+D cannot be defined %qE", decl, name);
7994 *no_add_attrs = true;
7998 /* Note that the very first time we process a nested declaration,
7999 decl_function_context will not be set. Indeed, *would* never
8000 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8001 we do below. After such frobbery, pushdecl would set the context.
8002 In any case, this is never what we want. */
8003 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
8007 id = TREE_VALUE (args);
8008 if (TREE_CODE (id) != STRING_CST)
8010 error ("attribute %qE argument not a string", name);
8011 *no_add_attrs = true;
8014 id = get_identifier (TREE_STRING_POINTER (id));
8015 /* This counts as a use of the object pointed to. */
8018 if (TREE_CODE (decl) == FUNCTION_DECL)
8019 DECL_INITIAL (decl) = error_mark_node;
8021 TREE_STATIC (decl) = 1;
8024 /* ifuncs are also aliases, so set that attribute too. */
8025 DECL_ATTRIBUTES (decl)
8026 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8030 warning (OPT_Wattributes, "%qE attribute ignored", name);
8031 *no_add_attrs = true;
8034 if (decl_in_symtab_p (*node))
8036 struct symtab_node *n = symtab_node::get (decl);
8037 if (n && n->refuse_visibility_changes)
8040 error ("%+D declared alias after being used", decl);
8042 error ("%+D declared ifunc after being used", decl);
8050 /* Handle an "alias" or "ifunc" attribute; arguments as in
8051 struct attribute_spec.handler. */
8054 handle_ifunc_attribute (tree *node, tree name, tree args,
8055 int ARG_UNUSED (flags), bool *no_add_attrs)
8057 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8060 /* Handle an "alias" or "ifunc" attribute; arguments as in
8061 struct attribute_spec.handler. */
8064 handle_alias_attribute (tree *node, tree name, tree args,
8065 int ARG_UNUSED (flags), bool *no_add_attrs)
8067 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8070 /* Handle a "weakref" attribute; arguments as in struct
8071 attribute_spec.handler. */
8074 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8075 int flags, bool *no_add_attrs)
8077 tree attr = NULL_TREE;
8079 /* We must ignore the attribute when it is associated with
8080 local-scoped decls, since attribute alias is ignored and many
8081 such symbols do not even have a DECL_WEAK field. */
8082 if (decl_function_context (*node)
8083 || current_function_decl
8084 || !VAR_OR_FUNCTION_DECL_P (*node))
8086 warning (OPT_Wattributes, "%qE attribute ignored", name);
8087 *no_add_attrs = true;
8091 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8093 error ("indirect function %q+D cannot be declared weakref", *node);
8094 *no_add_attrs = true;
8098 /* The idea here is that `weakref("name")' mutates into `weakref,
8099 alias("name")', and weakref without arguments, in turn,
8100 implicitly adds weak. */
8104 attr = tree_cons (get_identifier ("alias"), args, attr);
8105 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8107 *no_add_attrs = true;
8109 decl_attributes (node, attr, flags);
8113 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8114 error_at (DECL_SOURCE_LOCATION (*node),
8115 "weakref attribute must appear before alias attribute");
8117 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8118 and that isn't supported; and because it wants to add it to
8119 the list of weak decls, which isn't helpful. */
8120 DECL_WEAK (*node) = 1;
8123 if (decl_in_symtab_p (*node))
8125 struct symtab_node *n = symtab_node::get (*node);
8126 if (n && n->refuse_visibility_changes)
8127 error ("%+D declared weakref after being used", *node);
8133 /* Handle an "visibility" attribute; arguments as in
8134 struct attribute_spec.handler. */
8137 handle_visibility_attribute (tree *node, tree name, tree args,
8138 int ARG_UNUSED (flags),
8139 bool *ARG_UNUSED (no_add_attrs))
8142 tree id = TREE_VALUE (args);
8143 enum symbol_visibility vis;
8147 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8149 else if (!RECORD_OR_UNION_TYPE_P (*node))
8151 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8155 else if (TYPE_FIELDS (*node))
8157 error ("%qE attribute ignored because %qT is already defined",
8162 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8164 warning (OPT_Wattributes, "%qE attribute ignored", name);
8168 if (TREE_CODE (id) != STRING_CST)
8170 error ("visibility argument not a string");
8174 /* If this is a type, set the visibility on the type decl. */
8177 decl = TYPE_NAME (decl);
8180 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8182 warning (OPT_Wattributes, "%qE attribute ignored on types",
8188 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8189 vis = VISIBILITY_DEFAULT;
8190 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8191 vis = VISIBILITY_INTERNAL;
8192 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8193 vis = VISIBILITY_HIDDEN;
8194 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8195 vis = VISIBILITY_PROTECTED;
8198 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8199 vis = VISIBILITY_DEFAULT;
8202 if (DECL_VISIBILITY_SPECIFIED (decl)
8203 && vis != DECL_VISIBILITY (decl))
8205 tree attributes = (TYPE_P (*node)
8206 ? TYPE_ATTRIBUTES (*node)
8207 : DECL_ATTRIBUTES (decl));
8208 if (lookup_attribute ("visibility", attributes))
8209 error ("%qD redeclared with different visibility", decl);
8210 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8211 && lookup_attribute ("dllimport", attributes))
8212 error ("%qD was declared %qs which implies default visibility",
8214 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8215 && lookup_attribute ("dllexport", attributes))
8216 error ("%qD was declared %qs which implies default visibility",
8220 DECL_VISIBILITY (decl) = vis;
8221 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8223 /* Go ahead and attach the attribute to the node as well. This is needed
8224 so we can determine whether we have VISIBILITY_DEFAULT because the
8225 visibility was not specified, or because it was explicitly overridden
8226 from the containing scope. */
8231 /* Determine the ELF symbol visibility for DECL, which is either a
8232 variable or a function. It is an error to use this function if a
8233 definition of DECL is not available in this translation unit.
8234 Returns true if the final visibility has been determined by this
8235 function; false if the caller is free to make additional
8239 c_determine_visibility (tree decl)
8241 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8243 /* If the user explicitly specified the visibility with an
8244 attribute, honor that. DECL_VISIBILITY will have been set during
8245 the processing of the attribute. We check for an explicit
8246 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8247 to distinguish the use of an attribute from the use of a "#pragma
8248 GCC visibility push(...)"; in the latter case we still want other
8249 considerations to be able to overrule the #pragma. */
8250 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8251 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8252 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8253 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8256 /* Set default visibility to whatever the user supplied with
8257 visibility_specified depending on #pragma GCC visibility. */
8258 if (!DECL_VISIBILITY_SPECIFIED (decl))
8260 if (visibility_options.inpragma
8261 || DECL_VISIBILITY (decl) != default_visibility)
8263 DECL_VISIBILITY (decl) = default_visibility;
8264 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8265 /* If visibility changed and DECL already has DECL_RTL, ensure
8266 symbol flags are updated. */
8267 if (((VAR_P (decl) && TREE_STATIC (decl))
8268 || TREE_CODE (decl) == FUNCTION_DECL)
8269 && DECL_RTL_SET_P (decl))
8270 make_decl_rtl (decl);
8276 /* Handle an "tls_model" attribute; arguments as in
8277 struct attribute_spec.handler. */
8280 handle_tls_model_attribute (tree *node, tree name, tree args,
8281 int ARG_UNUSED (flags), bool *no_add_attrs)
8285 enum tls_model kind;
8287 *no_add_attrs = true;
8289 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
8291 warning (OPT_Wattributes, "%qE attribute ignored", name);
8295 kind = DECL_TLS_MODEL (decl);
8296 id = TREE_VALUE (args);
8297 if (TREE_CODE (id) != STRING_CST)
8299 error ("tls_model argument not a string");
8303 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8304 kind = TLS_MODEL_LOCAL_EXEC;
8305 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8306 kind = TLS_MODEL_INITIAL_EXEC;
8307 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8308 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8309 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8310 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8312 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8314 set_decl_tls_model (decl, kind);
8318 /* Handle a "no_instrument_function" attribute; arguments as in
8319 struct attribute_spec.handler. */
8322 handle_no_instrument_function_attribute (tree *node, tree name,
8323 tree ARG_UNUSED (args),
8324 int ARG_UNUSED (flags),
8329 if (TREE_CODE (decl) != FUNCTION_DECL)
8331 error_at (DECL_SOURCE_LOCATION (decl),
8332 "%qE attribute applies only to functions", name);
8333 *no_add_attrs = true;
8336 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8341 /* Handle a "malloc" attribute; arguments as in
8342 struct attribute_spec.handler. */
8345 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8346 int ARG_UNUSED (flags), bool *no_add_attrs)
8348 if (TREE_CODE (*node) == FUNCTION_DECL
8349 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8350 DECL_IS_MALLOC (*node) = 1;
8353 warning (OPT_Wattributes, "%qE attribute ignored", name);
8354 *no_add_attrs = true;
8360 /* Handle a "alloc_size" attribute; arguments as in
8361 struct attribute_spec.handler. */
8364 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8365 int ARG_UNUSED (flags), bool *no_add_attrs)
8367 unsigned arg_count = type_num_arguments (*node);
8368 for (; args; args = TREE_CHAIN (args))
8370 tree position = TREE_VALUE (args);
8371 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8372 && TREE_CODE (position) != FUNCTION_DECL)
8373 position = default_conversion (position);
8375 if (!tree_fits_uhwi_p (position)
8377 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8379 warning (OPT_Wattributes,
8380 "alloc_size parameter outside range");
8381 *no_add_attrs = true;
8388 /* Handle a "alloc_align" attribute; arguments as in
8389 struct attribute_spec.handler. */
8392 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8395 unsigned arg_count = type_num_arguments (*node);
8396 tree position = TREE_VALUE (args);
8397 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8398 position = default_conversion (position);
8400 if (!tree_fits_uhwi_p (position)
8402 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8404 warning (OPT_Wattributes,
8405 "alloc_align parameter outside range");
8406 *no_add_attrs = true;
8412 /* Handle a "assume_aligned" attribute; arguments as in
8413 struct attribute_spec.handler. */
8416 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8419 for (; args; args = TREE_CHAIN (args))
8421 tree position = TREE_VALUE (args);
8422 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8423 && TREE_CODE (position) != FUNCTION_DECL)
8424 position = default_conversion (position);
8426 if (TREE_CODE (position) != INTEGER_CST)
8428 warning (OPT_Wattributes,
8429 "assume_aligned parameter not integer constant");
8430 *no_add_attrs = true;
8437 /* Handle a "fn spec" attribute; arguments as in
8438 struct attribute_spec.handler. */
8441 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8442 tree args, int ARG_UNUSED (flags),
8443 bool *no_add_attrs ATTRIBUTE_UNUSED)
8446 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8447 && !TREE_CHAIN (args));
8451 /* Handle a "bnd_variable_size" attribute; arguments as in
8452 struct attribute_spec.handler. */
8455 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8456 int ARG_UNUSED (flags), bool *no_add_attrs)
8458 if (TREE_CODE (*node) != FIELD_DECL)
8460 warning (OPT_Wattributes, "%qE attribute ignored", name);
8461 *no_add_attrs = true;
8467 /* Handle a "bnd_legacy" attribute; arguments as in
8468 struct attribute_spec.handler. */
8471 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8472 int ARG_UNUSED (flags), bool *no_add_attrs)
8474 if (TREE_CODE (*node) != FUNCTION_DECL)
8476 warning (OPT_Wattributes, "%qE attribute ignored", name);
8477 *no_add_attrs = true;
8483 /* Handle a "bnd_instrument" attribute; arguments as in
8484 struct attribute_spec.handler. */
8487 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8488 int ARG_UNUSED (flags), bool *no_add_attrs)
8490 if (TREE_CODE (*node) != FUNCTION_DECL)
8492 warning (OPT_Wattributes, "%qE attribute ignored", name);
8493 *no_add_attrs = true;
8499 /* Handle a "warn_unused" attribute; arguments as in
8500 struct attribute_spec.handler. */
8503 handle_warn_unused_attribute (tree *node, tree name,
8504 tree args ATTRIBUTE_UNUSED,
8505 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8508 /* Do nothing else, just set the attribute. We'll get at
8509 it later with lookup_attribute. */
8513 warning (OPT_Wattributes, "%qE attribute ignored", name);
8514 *no_add_attrs = true;
8520 /* Handle an "omp declare simd" attribute; arguments as in
8521 struct attribute_spec.handler. */
8524 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8529 /* Handle a "simd" attribute. */
8532 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
8534 if (TREE_CODE (*node) == FUNCTION_DECL)
8536 if (lookup_attribute ("cilk simd function",
8537 DECL_ATTRIBUTES (*node)) != NULL)
8539 error_at (DECL_SOURCE_LOCATION (*node),
8540 "%<__simd__%> attribute cannot be used in the same "
8541 "function marked as a Cilk Plus SIMD-enabled function");
8542 *no_add_attrs = true;
8546 tree t = get_identifier ("omp declare simd");
8547 tree attr = NULL_TREE;
8550 tree id = TREE_VALUE (args);
8552 if (TREE_CODE (id) != STRING_CST)
8554 error ("attribute %qE argument not a string", name);
8555 *no_add_attrs = true;
8559 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8560 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8561 OMP_CLAUSE_NOTINBRANCH);
8563 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8564 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8565 OMP_CLAUSE_INBRANCH);
8568 error ("only %<inbranch%> and %<notinbranch%> flags are "
8569 "allowed for %<__simd__%> attribute");
8570 *no_add_attrs = true;
8575 DECL_ATTRIBUTES (*node) = tree_cons (t,
8576 build_tree_list (NULL_TREE,
8578 DECL_ATTRIBUTES (*node));
8583 warning (OPT_Wattributes, "%qE attribute ignored", name);
8584 *no_add_attrs = true;
8590 /* Handle an "omp declare target" attribute; arguments as in
8591 struct attribute_spec.handler. */
8594 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8599 /* Handle a "returns_twice" attribute; arguments as in
8600 struct attribute_spec.handler. */
8603 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8604 int ARG_UNUSED (flags), bool *no_add_attrs)
8606 if (TREE_CODE (*node) == FUNCTION_DECL)
8607 DECL_IS_RETURNS_TWICE (*node) = 1;
8610 warning (OPT_Wattributes, "%qE attribute ignored", name);
8611 *no_add_attrs = true;
8617 /* Handle a "no_limit_stack" attribute; arguments as in
8618 struct attribute_spec.handler. */
8621 handle_no_limit_stack_attribute (tree *node, tree name,
8622 tree ARG_UNUSED (args),
8623 int ARG_UNUSED (flags),
8628 if (TREE_CODE (decl) != FUNCTION_DECL)
8630 error_at (DECL_SOURCE_LOCATION (decl),
8631 "%qE attribute applies only to functions", name);
8632 *no_add_attrs = true;
8634 else if (DECL_INITIAL (decl))
8636 error_at (DECL_SOURCE_LOCATION (decl),
8637 "can%'t set %qE attribute after definition", name);
8638 *no_add_attrs = true;
8641 DECL_NO_LIMIT_STACK (decl) = 1;
8646 /* Handle a "pure" attribute; arguments as in
8647 struct attribute_spec.handler. */
8650 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8651 int ARG_UNUSED (flags), bool *no_add_attrs)
8653 if (TREE_CODE (*node) == FUNCTION_DECL)
8654 DECL_PURE_P (*node) = 1;
8655 /* ??? TODO: Support types. */
8658 warning (OPT_Wattributes, "%qE attribute ignored", name);
8659 *no_add_attrs = true;
8665 /* Digest an attribute list destined for a transactional memory statement.
8666 ALLOWED is the set of attributes that are allowed for this statement;
8667 return the attribute we parsed. Multiple attributes are never allowed. */
8670 parse_tm_stmt_attr (tree attrs, int allowed)
8675 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8677 tree a = TREE_PURPOSE (attrs);
8680 if (is_attribute_p ("outer", a))
8681 m = TM_STMT_ATTR_OUTER;
8683 if ((m & allowed) == 0)
8685 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8694 else if (m_seen == m)
8695 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8697 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8703 /* Transform a TM attribute name into a maskable integer and back.
8704 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8705 to how the lack of an attribute is treated. */
8708 tm_attr_to_mask (tree attr)
8712 if (is_attribute_p ("transaction_safe", attr))
8713 return TM_ATTR_SAFE;
8714 if (is_attribute_p ("transaction_callable", attr))
8715 return TM_ATTR_CALLABLE;
8716 if (is_attribute_p ("transaction_pure", attr))
8717 return TM_ATTR_PURE;
8718 if (is_attribute_p ("transaction_unsafe", attr))
8719 return TM_ATTR_IRREVOCABLE;
8720 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8721 return TM_ATTR_MAY_CANCEL_OUTER;
8726 tm_mask_to_attr (int mask)
8732 str = "transaction_safe";
8734 case TM_ATTR_CALLABLE:
8735 str = "transaction_callable";
8738 str = "transaction_pure";
8740 case TM_ATTR_IRREVOCABLE:
8741 str = "transaction_unsafe";
8743 case TM_ATTR_MAY_CANCEL_OUTER:
8744 str = "transaction_may_cancel_outer";
8749 return get_identifier (str);
8752 /* Return the first TM attribute seen in LIST. */
8755 find_tm_attribute (tree list)
8757 for (; list ; list = TREE_CHAIN (list))
8759 tree name = TREE_PURPOSE (list);
8760 if (tm_attr_to_mask (name) != 0)
8766 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8767 Here we accept only function types, and verify that none of the other
8768 function TM attributes are also applied. */
8769 /* ??? We need to accept class types for C++, but not C. This greatly
8770 complicates this function, since we can no longer rely on the extra
8771 processing given by function_type_required. */
8774 handle_tm_attribute (tree *node, tree name, tree args,
8775 int flags, bool *no_add_attrs)
8777 /* Only one path adds the attribute; others don't. */
8778 *no_add_attrs = true;
8780 switch (TREE_CODE (*node))
8784 /* Only tm_callable and tm_safe apply to classes. */
8785 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8792 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8793 if (old_name == name)
8795 else if (old_name != NULL_TREE)
8796 error ("type was previously declared %qE", old_name);
8798 *no_add_attrs = false;
8804 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8805 want to set transaction_safe on the type. */
8806 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8807 if (!TYPE_P (DECL_CONTEXT (*node)))
8808 error_at (DECL_SOURCE_LOCATION (*node),
8809 "%<transaction_safe_dynamic%> may only be specified for "
8810 "a virtual function");
8811 *no_add_attrs = false;
8812 decl_attributes (&TREE_TYPE (*node),
8813 build_tree_list (get_identifier ("transaction_safe"),
8821 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8822 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8824 tree fn_tmp = TREE_TYPE (*node);
8825 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8826 *node = build_pointer_type (fn_tmp);
8833 /* If a function is next, pass it on to be tried next. */
8834 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8835 return tree_cons (name, args, NULL);
8838 warning (OPT_Wattributes, "%qE attribute ignored", name);
8845 /* Handle the TM_WRAP attribute; arguments as in
8846 struct attribute_spec.handler. */
8849 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8850 int ARG_UNUSED (flags), bool *no_add_attrs)
8854 /* We don't need the attribute even on success, since we
8855 record the entry in an external table. */
8856 *no_add_attrs = true;
8858 if (TREE_CODE (decl) != FUNCTION_DECL)
8859 warning (OPT_Wattributes, "%qE attribute ignored", name);
8862 tree wrap_decl = TREE_VALUE (args);
8863 if (error_operand_p (wrap_decl))
8865 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8866 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
8867 error ("%qE argument not an identifier", name);
8870 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8871 wrap_decl = lookup_name (wrap_decl);
8872 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8874 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8875 TREE_TYPE (wrap_decl)))
8876 record_tm_replacement (wrap_decl, decl);
8878 error ("%qD is not compatible with %qD", wrap_decl, decl);
8881 error ("%qE argument is not a function", name);
8888 /* Ignore the given attribute. Used when this attribute may be usefully
8889 overridden by the target, but is not used generically. */
8892 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8893 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8896 *no_add_attrs = true;
8900 /* Handle a "no vops" attribute; arguments as in
8901 struct attribute_spec.handler. */
8904 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8905 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8906 bool *ARG_UNUSED (no_add_attrs))
8908 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8909 DECL_IS_NOVOPS (*node) = 1;
8913 /* Handle a "deprecated" attribute; arguments as in
8914 struct attribute_spec.handler. */
8917 handle_deprecated_attribute (tree *node, tree name,
8918 tree args, int flags,
8921 tree type = NULL_TREE;
8923 tree what = NULL_TREE;
8926 *no_add_attrs = true;
8927 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8929 error ("deprecated message is not a string");
8930 *no_add_attrs = true;
8936 type = TREE_TYPE (decl);
8938 if (TREE_CODE (decl) == TYPE_DECL
8939 || TREE_CODE (decl) == PARM_DECL
8940 || VAR_OR_FUNCTION_DECL_P (decl)
8941 || TREE_CODE (decl) == FIELD_DECL
8942 || TREE_CODE (decl) == CONST_DECL
8943 || objc_method_decl (TREE_CODE (decl)))
8944 TREE_DEPRECATED (decl) = 1;
8948 else if (TYPE_P (*node))
8950 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8951 *node = build_variant_type_copy (*node);
8952 TREE_DEPRECATED (*node) = 1;
8960 *no_add_attrs = true;
8961 if (type && TYPE_NAME (type))
8963 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8964 what = TYPE_NAME (*node);
8965 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8966 && DECL_NAME (TYPE_NAME (type)))
8967 what = DECL_NAME (TYPE_NAME (type));
8970 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8972 warning (OPT_Wattributes, "%qE attribute ignored", name);
8978 /* Handle a "vector_size" attribute; arguments as in
8979 struct attribute_spec.handler. */
8982 handle_vector_size_attribute (tree *node, tree name, tree args,
8983 int ARG_UNUSED (flags),
8986 unsigned HOST_WIDE_INT vecsize, nunits;
8987 machine_mode orig_mode;
8988 tree type = *node, new_type, size;
8990 *no_add_attrs = true;
8992 size = TREE_VALUE (args);
8993 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8994 && TREE_CODE (size) != FUNCTION_DECL)
8995 size = default_conversion (size);
8997 if (!tree_fits_uhwi_p (size))
8999 warning (OPT_Wattributes, "%qE attribute ignored", name);
9003 /* Get the vector size (in bytes). */
9004 vecsize = tree_to_uhwi (size);
9006 /* We need to provide for vector pointers, vector arrays, and
9007 functions returning vectors. For example:
9009 __attribute__((vector_size(16))) short *foo;
9011 In this case, the mode is SI, but the type being modified is
9012 HI, so we need to look further. */
9014 while (POINTER_TYPE_P (type)
9015 || TREE_CODE (type) == FUNCTION_TYPE
9016 || TREE_CODE (type) == METHOD_TYPE
9017 || TREE_CODE (type) == ARRAY_TYPE
9018 || TREE_CODE (type) == OFFSET_TYPE)
9019 type = TREE_TYPE (type);
9021 /* Get the mode of the type being modified. */
9022 orig_mode = TYPE_MODE (type);
9024 if ((!INTEGRAL_TYPE_P (type)
9025 && !SCALAR_FLOAT_TYPE_P (type)
9026 && !FIXED_POINT_TYPE_P (type))
9027 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9028 && GET_MODE_CLASS (orig_mode) != MODE_INT
9029 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
9030 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9031 || TREE_CODE (type) == BOOLEAN_TYPE)
9033 error ("invalid vector type for attribute %qE", name);
9037 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
9039 error ("vector size not an integral multiple of component size");
9045 error ("zero vector size");
9049 /* Calculate how many units fit in the vector. */
9050 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
9051 if (nunits & (nunits - 1))
9053 error ("number of components of the vector not a power of two");
9057 new_type = build_vector_type (type, nunits);
9059 /* Build back pointers if needed. */
9060 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9065 /* Handle the "nonnull" attribute. */
9067 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9068 tree args, int ARG_UNUSED (flags),
9072 unsigned HOST_WIDE_INT attr_arg_num;
9074 /* If no arguments are specified, all pointer arguments should be
9075 non-null. Verify a full prototype is given so that the arguments
9076 will have the correct types when we actually check them later. */
9079 if (!prototype_p (type))
9081 error ("nonnull attribute without arguments on a non-prototype");
9082 *no_add_attrs = true;
9087 /* Argument list specified. Verify that each argument number references
9088 a pointer argument. */
9089 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9091 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9093 tree arg = TREE_VALUE (args);
9094 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9095 && TREE_CODE (arg) != FUNCTION_DECL)
9096 arg = default_conversion (arg);
9098 if (!get_nonnull_operand (arg, &arg_num))
9100 error ("nonnull argument has invalid operand number (argument %lu)",
9101 (unsigned long) attr_arg_num);
9102 *no_add_attrs = true;
9106 if (prototype_p (type))
9108 function_args_iterator iter;
9111 function_args_iter_init (&iter, type);
9112 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9114 argument = function_args_iter_cond (&iter);
9115 if (argument == NULL_TREE || ck_num == arg_num)
9120 || TREE_CODE (argument) == VOID_TYPE)
9122 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9123 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9124 *no_add_attrs = true;
9128 if (TREE_CODE (argument) != POINTER_TYPE)
9130 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9131 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9132 *no_add_attrs = true;
9141 /* Check the argument list of a function call for null in argument slots
9142 that are marked as requiring a non-null pointer argument. The NARGS
9143 arguments are passed in the array ARGARRAY. */
9146 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
9151 attrs = lookup_attribute ("nonnull", attrs);
9152 if (attrs == NULL_TREE)
9156 /* See if any of the nonnull attributes has no arguments. If so,
9157 then every pointer argument is checked (in which case the check
9158 for pointer type is done in check_nonnull_arg). */
9159 if (TREE_VALUE (a) != NULL_TREE)
9161 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9162 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9165 for (i = 0; i < nargs; i++)
9166 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
9170 /* Walk the argument list. If we encounter an argument number we
9171 should check for non-null, do it. */
9172 for (i = 0; i < nargs; i++)
9174 for (a = attrs; ; a = TREE_CHAIN (a))
9176 a = lookup_attribute ("nonnull", a);
9177 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9182 check_function_arguments_recurse (check_nonnull_arg, &loc,
9183 argarray[i], i + 1);
9188 /* Check that the Nth argument of a function call (counting backwards
9189 from the end) is a (pointer)0. The NARGS arguments are passed in the
9193 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9195 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9202 function_args_iterator iter;
9205 /* Skip over the named arguments. */
9206 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9213 if (TREE_VALUE (attr))
9215 tree p = TREE_VALUE (TREE_VALUE (attr));
9216 pos = TREE_INT_CST_LOW (p);
9219 /* The sentinel must be one of the varargs, i.e.
9220 in position >= the number of fixed arguments. */
9221 if ((nargs - 1 - pos) < len)
9223 warning (OPT_Wformat_,
9224 "not enough variable arguments to fit a sentinel");
9228 /* Validate the sentinel. */
9229 sentinel = argarray[nargs - 1 - pos];
9230 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9231 || !integer_zerop (sentinel))
9232 /* Although __null (in C++) is only an integer we allow it
9233 nevertheless, as we are guaranteed that it's exactly
9234 as wide as a pointer, and we don't want to force
9235 users to cast the NULL they have written there.
9236 We warn with -Wstrict-null-sentinel, though. */
9237 && (warn_strict_null_sentinel || null_node != sentinel))
9238 warning (OPT_Wformat_, "missing sentinel in function call");
9242 /* Helper for check_function_nonnull; given a list of operands which
9243 must be non-null in ARGS, determine if operand PARAM_NUM should be
9247 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9249 unsigned HOST_WIDE_INT arg_num = 0;
9251 for (; args; args = TREE_CHAIN (args))
9253 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9257 if (arg_num == param_num)
9263 /* Check that the function argument PARAM (which is operand number
9264 PARAM_NUM) is non-null. This is called by check_function_nonnull
9265 via check_function_arguments_recurse. */
9268 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
9270 location_t *ploc = (location_t *) ctx;
9272 /* Just skip checking the argument if it's not a pointer. This can
9273 happen if the "nonnull" attribute was given without an operand
9274 list (which means to check every pointer argument). */
9276 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9279 if (integer_zerop (param))
9280 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9281 "(argument %lu)", (unsigned long) param_num);
9284 /* Helper for nonnull attribute handling; fetch the operand number
9285 from the attribute argument list. */
9288 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9290 /* Verify the arg number is a small constant. */
9291 if (tree_fits_uhwi_p (arg_num_expr))
9293 *valp = TREE_INT_CST_LOW (arg_num_expr);
9300 /* Handle a "nothrow" attribute; arguments as in
9301 struct attribute_spec.handler. */
9304 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9305 int ARG_UNUSED (flags), bool *no_add_attrs)
9307 if (TREE_CODE (*node) == FUNCTION_DECL)
9308 TREE_NOTHROW (*node) = 1;
9309 /* ??? TODO: Support types. */
9312 warning (OPT_Wattributes, "%qE attribute ignored", name);
9313 *no_add_attrs = true;
9319 /* Handle a "cleanup" attribute; arguments as in
9320 struct attribute_spec.handler. */
9323 handle_cleanup_attribute (tree *node, tree name, tree args,
9324 int ARG_UNUSED (flags), bool *no_add_attrs)
9327 tree cleanup_id, cleanup_decl;
9329 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9330 for global destructors in C++. This requires infrastructure that
9331 we don't have generically at the moment. It's also not a feature
9332 we'd be missing too much, since we do have attribute constructor. */
9333 if (!VAR_P (decl) || TREE_STATIC (decl))
9335 warning (OPT_Wattributes, "%qE attribute ignored", name);
9336 *no_add_attrs = true;
9340 /* Verify that the argument is a function in scope. */
9341 /* ??? We could support pointers to functions here as well, if
9342 that was considered desirable. */
9343 cleanup_id = TREE_VALUE (args);
9344 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9346 error ("cleanup argument not an identifier");
9347 *no_add_attrs = true;
9350 cleanup_decl = lookup_name (cleanup_id);
9351 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9353 error ("cleanup argument not a function");
9354 *no_add_attrs = true;
9358 /* That the function has proper type is checked with the
9359 eventual call to build_function_call. */
9364 /* Handle a "warn_unused_result" attribute. No special handling. */
9367 handle_warn_unused_result_attribute (tree *node, tree name,
9368 tree ARG_UNUSED (args),
9369 int ARG_UNUSED (flags), bool *no_add_attrs)
9371 /* Ignore the attribute for functions not returning any value. */
9372 if (VOID_TYPE_P (TREE_TYPE (*node)))
9374 warning (OPT_Wattributes, "%qE attribute ignored", name);
9375 *no_add_attrs = true;
9381 /* Handle a "sentinel" attribute. */
9384 handle_sentinel_attribute (tree *node, tree name, tree args,
9385 int ARG_UNUSED (flags), bool *no_add_attrs)
9387 if (!prototype_p (*node))
9389 warning (OPT_Wattributes,
9390 "%qE attribute requires prototypes with named arguments", name);
9391 *no_add_attrs = true;
9395 if (!stdarg_p (*node))
9397 warning (OPT_Wattributes,
9398 "%qE attribute only applies to variadic functions", name);
9399 *no_add_attrs = true;
9405 tree position = TREE_VALUE (args);
9406 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9407 && TREE_CODE (position) != FUNCTION_DECL)
9408 position = default_conversion (position);
9410 if (TREE_CODE (position) != INTEGER_CST
9411 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9413 warning (OPT_Wattributes,
9414 "requested position is not an integer constant");
9415 *no_add_attrs = true;
9419 if (tree_int_cst_lt (position, integer_zero_node))
9421 warning (OPT_Wattributes,
9422 "requested position is less than zero");
9423 *no_add_attrs = true;
9431 /* Handle a "type_generic" attribute. */
9434 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9435 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9436 bool * ARG_UNUSED (no_add_attrs))
9438 /* Ensure we have a function type. */
9439 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9441 /* Ensure we have a variadic function. */
9442 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9447 /* Handle a "target" attribute. */
9450 handle_target_attribute (tree *node, tree name, tree args, int flags,
9453 /* Ensure we have a function type. */
9454 if (TREE_CODE (*node) != FUNCTION_DECL)
9456 warning (OPT_Wattributes, "%qE attribute ignored", name);
9457 *no_add_attrs = true;
9459 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9461 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9462 "with %qs attribute", name, "target_clones");
9463 *no_add_attrs = true;
9465 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9467 *no_add_attrs = true;
9472 /* Handle a "target_clones" attribute. */
9475 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9476 int ARG_UNUSED (flags), bool *no_add_attrs)
9478 /* Ensure we have a function type. */
9479 if (TREE_CODE (*node) == FUNCTION_DECL)
9481 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9483 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9484 "with %qs attribute", name, "always_inline");
9485 *no_add_attrs = true;
9487 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9489 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9490 "with %qs attribute", name, "target");
9491 *no_add_attrs = true;
9494 /* Do not inline functions with multiple clone targets. */
9495 DECL_UNINLINABLE (*node) = 1;
9499 warning (OPT_Wattributes, "%qE attribute ignored", name);
9500 *no_add_attrs = true;
9505 /* Arguments being collected for optimization. */
9506 typedef const char *const_char_p; /* For DEF_VEC_P. */
9507 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9510 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9511 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9512 false for #pragma GCC optimize. */
9515 parse_optimize_options (tree args, bool attr_p)
9520 const char **opt_argv;
9521 struct cl_decoded_option *decoded_options;
9522 unsigned int decoded_options_count;
9525 /* Build up argv vector. Just in case the string is stored away, use garbage
9526 collected strings. */
9527 vec_safe_truncate (optimize_args, 0);
9528 vec_safe_push (optimize_args, (const char *) NULL);
9530 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9532 tree value = TREE_VALUE (ap);
9534 if (TREE_CODE (value) == INTEGER_CST)
9537 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9538 vec_safe_push (optimize_args, ggc_strdup (buffer));
9541 else if (TREE_CODE (value) == STRING_CST)
9543 /* Split string into multiple substrings. */
9544 size_t len = TREE_STRING_LENGTH (value);
9545 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9546 char *end = p + len;
9550 while (next_p != NULL)
9556 comma = strchr (p, ',');
9569 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9571 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9573 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9577 warning (OPT_Wattributes,
9578 "bad option %s to optimize attribute", p);
9580 warning (OPT_Wpragmas,
9581 "bad option %s to pragma attribute", p);
9589 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9590 itself is -Os, and any other switch begins with a -f. */
9591 if ((*p >= '0' && *p <= '9')
9592 || (p[0] == 's' && p[1] == '\0'))
9598 memcpy (r, p, len2);
9600 vec_safe_push (optimize_args, (const char *) q);
9606 opt_argc = optimize_args->length ();
9607 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9609 for (i = 1; i < opt_argc; i++)
9610 opt_argv[i] = (*optimize_args)[i];
9612 /* Now parse the options. */
9613 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9615 &decoded_options_count);
9616 decode_options (&global_options, &global_options_set,
9617 decoded_options, decoded_options_count,
9618 input_location, global_dc);
9620 targetm.override_options_after_change();
9622 optimize_args->truncate (0);
9626 /* For handling "optimize" attribute. arguments as in
9627 struct attribute_spec.handler. */
9630 handle_optimize_attribute (tree *node, tree name, tree args,
9631 int ARG_UNUSED (flags), bool *no_add_attrs)
9633 /* Ensure we have a function type. */
9634 if (TREE_CODE (*node) != FUNCTION_DECL)
9636 warning (OPT_Wattributes, "%qE attribute ignored", name);
9637 *no_add_attrs = true;
9641 struct cl_optimization cur_opts;
9642 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9644 /* Save current options. */
9645 cl_optimization_save (&cur_opts, &global_options);
9647 /* If we previously had some optimization options, use them as the
9650 cl_optimization_restore (&global_options,
9651 TREE_OPTIMIZATION (old_opts));
9653 /* Parse options, and update the vector. */
9654 parse_optimize_options (args, true);
9655 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9656 = build_optimization_node (&global_options);
9658 /* Restore current options. */
9659 cl_optimization_restore (&global_options, &cur_opts);
9665 /* Handle a "no_split_stack" attribute. */
9668 handle_no_split_stack_attribute (tree *node, tree name,
9669 tree ARG_UNUSED (args),
9670 int ARG_UNUSED (flags),
9675 if (TREE_CODE (decl) != FUNCTION_DECL)
9677 error_at (DECL_SOURCE_LOCATION (decl),
9678 "%qE attribute applies only to functions", name);
9679 *no_add_attrs = true;
9681 else if (DECL_INITIAL (decl))
9683 error_at (DECL_SOURCE_LOCATION (decl),
9684 "can%'t set %qE attribute after definition", name);
9685 *no_add_attrs = true;
9691 /* Handle a "returns_nonnull" attribute; arguments as in
9692 struct attribute_spec.handler. */
9695 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9698 // Even without a prototype we still have a return type we can check.
9699 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9701 error ("returns_nonnull attribute on a function not returning a pointer");
9702 *no_add_attrs = true;
9707 /* Handle a "designated_init" attribute; arguments as in
9708 struct attribute_spec.handler. */
9711 handle_designated_init_attribute (tree *node, tree name, tree, int,
9714 if (TREE_CODE (*node) != RECORD_TYPE)
9716 error ("%qE attribute is only valid on %<struct%> type", name);
9717 *no_add_attrs = true;
9723 /* Check for valid arguments being passed to a function with FNTYPE.
9724 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9727 check_function_arguments (location_t loc, const_tree fntype, int nargs,
9730 /* Check for null being passed in a pointer argument that must be
9731 non-null. We also need to do this if format checking is enabled. */
9734 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
9736 /* Check for errors in format strings. */
9738 if (warn_format || warn_suggest_attribute_format)
9739 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9742 check_function_sentinel (fntype, nargs, argarray);
9745 /* Generic argument checking recursion routine. PARAM is the argument to
9746 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9747 once the argument is resolved. CTX is context for the callback. */
9749 check_function_arguments_recurse (void (*callback)
9750 (void *, tree, unsigned HOST_WIDE_INT),
9751 void *ctx, tree param,
9752 unsigned HOST_WIDE_INT param_num)
9754 if (CONVERT_EXPR_P (param)
9755 && (TYPE_PRECISION (TREE_TYPE (param))
9756 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9758 /* Strip coercion. */
9759 check_function_arguments_recurse (callback, ctx,
9760 TREE_OPERAND (param, 0), param_num);
9764 if (TREE_CODE (param) == CALL_EXPR)
9767 = CALL_EXPR_FN (param) ? TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)))
9768 : TREE_TYPE (param);
9770 bool found_format_arg = false;
9772 /* See if this is a call to a known internationalization function
9773 that modifies a format arg. Such a function may have multiple
9774 format_arg attributes (for example, ngettext). */
9776 for (attrs = TYPE_ATTRIBUTES (type);
9778 attrs = TREE_CHAIN (attrs))
9779 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9782 tree format_num_expr;
9785 call_expr_arg_iterator iter;
9787 /* Extract the argument number, which was previously checked
9789 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9791 format_num = tree_to_uhwi (format_num_expr);
9793 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9795 inner_arg = next_call_expr_arg (&iter), i++)
9796 if (i == format_num)
9798 check_function_arguments_recurse (callback, ctx,
9799 inner_arg, param_num);
9800 found_format_arg = true;
9805 /* If we found a format_arg attribute and did a recursive check,
9806 we are done with checking this argument. Otherwise, we continue
9807 and this will be considered a non-literal. */
9808 if (found_format_arg)
9812 if (TREE_CODE (param) == COND_EXPR)
9814 /* Simplify to avoid warning for an impossible case. */
9815 param = fold_for_warn (param);
9816 if (TREE_CODE (param) == COND_EXPR)
9818 /* Check both halves of the conditional expression. */
9819 check_function_arguments_recurse (callback, ctx,
9820 TREE_OPERAND (param, 1),
9822 check_function_arguments_recurse (callback, ctx,
9823 TREE_OPERAND (param, 2),
9829 (*callback) (ctx, param, param_num);
9832 /* Checks for a builtin function FNDECL that the number of arguments
9833 NARGS against the required number REQUIRED and issues an error if
9834 there is a mismatch. Returns true if the number of arguments is
9835 correct, otherwise false. */
9838 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9840 if (nargs < required)
9842 error_at (input_location,
9843 "not enough arguments to function %qE", fndecl);
9846 else if (nargs > required)
9848 error_at (input_location,
9849 "too many arguments to function %qE", fndecl);
9855 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9856 Returns false if there was an error, otherwise true. */
9859 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9861 if (!DECL_BUILT_IN (fndecl)
9862 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9865 switch (DECL_FUNCTION_CODE (fndecl))
9867 case BUILT_IN_ALLOCA_WITH_ALIGN:
9869 /* Get the requested alignment (in bits) if it's a constant
9870 integer expression. */
9871 unsigned HOST_WIDE_INT align
9872 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
9874 /* Determine if the requested alignment is a power of 2. */
9875 if ((align & (align - 1)))
9878 /* The maximum alignment in bits corresponding to the same
9879 maximum in bytes enforced in check_user_alignment(). */
9880 unsigned maxalign = (UINT_MAX >> 1) + 1;
9882 /* Reject invalid alignments. */
9883 if (align < BITS_PER_UNIT || maxalign < align)
9885 error_at (EXPR_LOC_OR_LOC (args[1], input_location),
9886 "second argument to function %qE must be a constant "
9887 "integer power of 2 between %qi and %qu bits",
9888 fndecl, BITS_PER_UNIT, maxalign);
9894 case BUILT_IN_CONSTANT_P:
9895 return builtin_function_validate_nargs (fndecl, nargs, 1);
9897 case BUILT_IN_ISFINITE:
9898 case BUILT_IN_ISINF:
9899 case BUILT_IN_ISINF_SIGN:
9900 case BUILT_IN_ISNAN:
9901 case BUILT_IN_ISNORMAL:
9902 case BUILT_IN_SIGNBIT:
9903 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9905 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9907 error ("non-floating-point argument in call to "
9908 "function %qE", fndecl);
9915 case BUILT_IN_ISGREATER:
9916 case BUILT_IN_ISGREATEREQUAL:
9917 case BUILT_IN_ISLESS:
9918 case BUILT_IN_ISLESSEQUAL:
9919 case BUILT_IN_ISLESSGREATER:
9920 case BUILT_IN_ISUNORDERED:
9921 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9923 enum tree_code code0, code1;
9924 code0 = TREE_CODE (TREE_TYPE (args[0]));
9925 code1 = TREE_CODE (TREE_TYPE (args[1]));
9926 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9927 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9928 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9930 error ("non-floating-point arguments in call to "
9931 "function %qE", fndecl);
9938 case BUILT_IN_FPCLASSIFY:
9939 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9944 if (TREE_CODE (args[i]) != INTEGER_CST)
9946 error ("non-const integer argument %u in call to function %qE",
9951 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9953 error ("non-floating-point argument in call to function %qE",
9961 case BUILT_IN_ASSUME_ALIGNED:
9962 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9964 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9966 error ("non-integer argument 3 in call to function %qE", fndecl);
9973 case BUILT_IN_ADD_OVERFLOW:
9974 case BUILT_IN_SUB_OVERFLOW:
9975 case BUILT_IN_MUL_OVERFLOW:
9976 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9979 for (i = 0; i < 2; i++)
9980 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9982 error ("argument %u in call to function %qE does not have "
9983 "integral type", i + 1, fndecl);
9986 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9987 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9989 error ("argument 3 in call to function %qE does not have "
9990 "pointer to integer type", fndecl);
10002 /* Function to help qsort sort FIELD_DECLs by name order. */
10005 field_decl_cmp (const void *x_p, const void *y_p)
10007 const tree *const x = (const tree *const) x_p;
10008 const tree *const y = (const tree *const) y_p;
10010 if (DECL_NAME (*x) == DECL_NAME (*y))
10011 /* A nontype is "greater" than a type. */
10012 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10013 if (DECL_NAME (*x) == NULL_TREE)
10015 if (DECL_NAME (*y) == NULL_TREE)
10017 if (DECL_NAME (*x) < DECL_NAME (*y))
10023 gt_pointer_operator new_value;
10027 /* This routine compares two fields like field_decl_cmp but using the
10028 pointer operator in resort_data. */
10031 resort_field_decl_cmp (const void *x_p, const void *y_p)
10033 const tree *const x = (const tree *const) x_p;
10034 const tree *const y = (const tree *const) y_p;
10036 if (DECL_NAME (*x) == DECL_NAME (*y))
10037 /* A nontype is "greater" than a type. */
10038 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10039 if (DECL_NAME (*x) == NULL_TREE)
10041 if (DECL_NAME (*y) == NULL_TREE)
10044 tree d1 = DECL_NAME (*x);
10045 tree d2 = DECL_NAME (*y);
10046 resort_data.new_value (&d1, resort_data.cookie);
10047 resort_data.new_value (&d2, resort_data.cookie);
10054 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10057 resort_sorted_fields (void *obj,
10058 void * ARG_UNUSED (orig_obj),
10059 gt_pointer_operator new_value,
10062 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10063 resort_data.new_value = new_value;
10064 resort_data.cookie = cookie;
10065 qsort (&sf->elts[0], sf->len, sizeof (tree),
10066 resort_field_decl_cmp);
10069 /* Subroutine of c_parse_error.
10070 Return the result of concatenating LHS and RHS. RHS is really
10071 a string literal, its first character is indicated by RHS_START and
10072 RHS_SIZE is its length (including the terminating NUL character).
10074 The caller is responsible for deleting the returned pointer. */
10077 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10079 const int lhs_size = strlen (lhs);
10080 char *result = XNEWVEC (char, lhs_size + rhs_size);
10081 strncpy (result, lhs, lhs_size);
10082 strncpy (result + lhs_size, rhs_start, rhs_size);
10086 /* Issue the error given by GMSGID, indicating that it occurred before
10087 TOKEN, which had the associated VALUE. */
10090 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
10091 tree value, unsigned char token_flags)
10093 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10095 char *message = NULL;
10097 if (token_type == CPP_EOF)
10098 message = catenate_messages (gmsgid, " at end of input");
10099 else if (token_type == CPP_CHAR
10100 || token_type == CPP_WCHAR
10101 || token_type == CPP_CHAR16
10102 || token_type == CPP_CHAR32
10103 || token_type == CPP_UTF8CHAR)
10105 unsigned int val = TREE_INT_CST_LOW (value);
10106 const char *prefix;
10108 switch (token_type)
10127 if (val <= UCHAR_MAX && ISGRAPH (val))
10128 message = catenate_messages (gmsgid, " before %s'%c'");
10130 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10132 error (message, prefix, val);
10136 else if (token_type == CPP_CHAR_USERDEF
10137 || token_type == CPP_WCHAR_USERDEF
10138 || token_type == CPP_CHAR16_USERDEF
10139 || token_type == CPP_CHAR32_USERDEF
10140 || token_type == CPP_UTF8CHAR_USERDEF)
10141 message = catenate_messages (gmsgid,
10142 " before user-defined character literal");
10143 else if (token_type == CPP_STRING_USERDEF
10144 || token_type == CPP_WSTRING_USERDEF
10145 || token_type == CPP_STRING16_USERDEF
10146 || token_type == CPP_STRING32_USERDEF
10147 || token_type == CPP_UTF8STRING_USERDEF)
10148 message = catenate_messages (gmsgid, " before user-defined string literal");
10149 else if (token_type == CPP_STRING
10150 || token_type == CPP_WSTRING
10151 || token_type == CPP_STRING16
10152 || token_type == CPP_STRING32
10153 || token_type == CPP_UTF8STRING)
10154 message = catenate_messages (gmsgid, " before string constant");
10155 else if (token_type == CPP_NUMBER)
10156 message = catenate_messages (gmsgid, " before numeric constant");
10157 else if (token_type == CPP_NAME)
10159 message = catenate_messages (gmsgid, " before %qE");
10160 error (message, value);
10164 else if (token_type == CPP_PRAGMA)
10165 message = catenate_messages (gmsgid, " before %<#pragma%>");
10166 else if (token_type == CPP_PRAGMA_EOL)
10167 message = catenate_messages (gmsgid, " before end of line");
10168 else if (token_type == CPP_DECLTYPE)
10169 message = catenate_messages (gmsgid, " before %<decltype%>");
10170 else if (token_type < N_TTYPES)
10172 message = catenate_messages (gmsgid, " before %qs token");
10173 error (message, cpp_type2name (token_type, token_flags));
10185 #undef catenate_messages
10188 /* Return the gcc option code associated with the reason for a cpp
10189 message, or 0 if none. */
10192 c_option_controlling_cpp_error (int reason)
10194 const struct cpp_reason_option_codes_t *entry;
10196 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10198 if (entry->reason == reason)
10199 return entry->option_code;
10204 /* Callback from cpp_error for PFILE to print diagnostics from the
10205 preprocessor. The diagnostic is of type LEVEL, with REASON set
10206 to the reason code if LEVEL is represents a warning, at location
10207 RICHLOC unless this is after lexing and the compiler's location
10208 should be used instead; MSG is the translated message and AP
10209 the arguments. Returns true if a diagnostic was emitted, false
10213 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10214 rich_location *richloc,
10215 const char *msg, va_list *ap)
10217 diagnostic_info diagnostic;
10218 diagnostic_t dlevel;
10219 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10224 case CPP_DL_WARNING_SYSHDR:
10225 if (flag_no_output)
10227 global_dc->dc_warn_system_headers = 1;
10228 /* Fall through. */
10229 case CPP_DL_WARNING:
10230 if (flag_no_output)
10232 dlevel = DK_WARNING;
10234 case CPP_DL_PEDWARN:
10235 if (flag_no_output && !flag_pedantic_errors)
10237 dlevel = DK_PEDWARN;
10252 gcc_unreachable ();
10255 richloc->set_range (line_table, 0, input_location, true);
10256 diagnostic_set_info_translated (&diagnostic, msg, ap,
10258 diagnostic_override_option_index (&diagnostic,
10259 c_option_controlling_cpp_error (reason));
10260 ret = report_diagnostic (&diagnostic);
10261 if (level == CPP_DL_WARNING_SYSHDR)
10262 global_dc->dc_warn_system_headers = save_warn_system_headers;
10266 /* Convert a character from the host to the target execution character
10267 set. cpplib handles this, mostly. */
10270 c_common_to_target_charset (HOST_WIDE_INT c)
10272 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10273 zero-extended under -fno-signed-char. cpplib insists that characters
10274 and character constants are always unsigned. Hence we must convert
10276 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10278 uc = cpp_host_to_exec_charset (parse_in, uc);
10280 if (flag_signed_char)
10281 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10282 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10287 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10288 references with an INDIRECT_REF of a constant at the bottom; much like the
10289 traditional rendering of offsetof as a macro. Return the folded result. */
10292 fold_offsetof_1 (tree expr, enum tree_code ctx)
10295 tree_code code = TREE_CODE (expr);
10302 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10303 return error_mark_node;
10307 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10308 return error_mark_node;
10312 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10314 error ("cannot apply %<offsetof%> to a non constant address");
10315 return error_mark_node;
10317 return TREE_OPERAND (expr, 0);
10319 case COMPONENT_REF:
10320 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10321 if (base == error_mark_node)
10324 t = TREE_OPERAND (expr, 1);
10325 if (DECL_C_BIT_FIELD (t))
10327 error ("attempt to take address of bit-field structure "
10329 return error_mark_node;
10331 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10332 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10337 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10338 if (base == error_mark_node)
10341 t = TREE_OPERAND (expr, 1);
10343 /* Check if the offset goes beyond the upper bound of the array. */
10344 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10346 tree upbound = array_ref_up_bound (expr);
10347 if (upbound != NULL_TREE
10348 && TREE_CODE (upbound) == INTEGER_CST
10349 && !tree_int_cst_equal (upbound,
10350 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10352 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10353 upbound = size_binop (PLUS_EXPR, upbound,
10354 build_int_cst (TREE_TYPE (upbound), 1));
10355 if (tree_int_cst_lt (upbound, t))
10359 for (v = TREE_OPERAND (expr, 0);
10360 TREE_CODE (v) == COMPONENT_REF;
10361 v = TREE_OPERAND (v, 0))
10362 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10365 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10366 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10367 if (TREE_CODE (fld_chain) == FIELD_DECL)
10373 /* Don't warn if the array might be considered a poor
10374 man's flexible array member with a very permissive
10375 definition thereof. */
10376 if (TREE_CODE (v) == ARRAY_REF
10377 || TREE_CODE (v) == COMPONENT_REF)
10378 warning (OPT_Warray_bounds,
10379 "index %E denotes an offset "
10380 "greater than size of %qT",
10381 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10386 t = convert (sizetype, t);
10387 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10390 case COMPOUND_EXPR:
10391 /* Handle static members of volatile structs. */
10392 t = TREE_OPERAND (expr, 1);
10393 gcc_assert (VAR_P (t));
10394 return fold_offsetof_1 (t);
10397 gcc_unreachable ();
10400 return fold_build_pointer_plus (base, off);
10403 /* Likewise, but convert it to the return type of offsetof. */
10406 fold_offsetof (tree expr)
10408 return convert (size_type_node, fold_offsetof_1 (expr));
10411 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10412 expression, because B will always be true. */
10415 warn_for_omitted_condop (location_t location, tree cond)
10417 if (truth_value_p (TREE_CODE (cond)))
10418 warning_at (location, OPT_Wparentheses,
10419 "the omitted middle operand in ?: will always be %<true%>, "
10420 "suggest explicit middle operand");
10423 /* Give an error for storing into ARG, which is 'const'. USE indicates
10424 how ARG was being used. */
10427 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10429 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10431 /* Using this macro rather than (for example) arrays of messages
10432 ensures that all the format strings are checked at compile
10434 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10435 : (use == lv_increment ? (I) \
10436 : (use == lv_decrement ? (D) : (AS))))
10437 if (TREE_CODE (arg) == COMPONENT_REF)
10439 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10440 error_at (loc, READONLY_MSG (G_("assignment of member "
10441 "%qD in read-only object"),
10442 G_("increment of member "
10443 "%qD in read-only object"),
10444 G_("decrement of member "
10445 "%qD in read-only object"),
10446 G_("member %qD in read-only object "
10447 "used as %<asm%> output")),
10448 TREE_OPERAND (arg, 1));
10450 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10451 G_("increment of read-only member %qD"),
10452 G_("decrement of read-only member %qD"),
10453 G_("read-only member %qD used as %<asm%> output")),
10454 TREE_OPERAND (arg, 1));
10456 else if (VAR_P (arg))
10457 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10458 G_("increment of read-only variable %qD"),
10459 G_("decrement of read-only variable %qD"),
10460 G_("read-only variable %qD used as %<asm%> output")),
10462 else if (TREE_CODE (arg) == PARM_DECL)
10463 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10464 G_("increment of read-only parameter %qD"),
10465 G_("decrement of read-only parameter %qD"),
10466 G_("read-only parameter %qD use as %<asm%> output")),
10468 else if (TREE_CODE (arg) == RESULT_DECL)
10470 gcc_assert (c_dialect_cxx ());
10471 error_at (loc, READONLY_MSG (G_("assignment of "
10472 "read-only named return value %qD"),
10474 "read-only named return value %qD"),
10476 "read-only named return value %qD"),
10477 G_("read-only named return value %qD "
10478 "used as %<asm%>output")),
10481 else if (TREE_CODE (arg) == FUNCTION_DECL)
10482 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10483 G_("increment of function %qD"),
10484 G_("decrement of function %qD"),
10485 G_("function %qD used as %<asm%> output")),
10488 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10489 G_("increment of read-only location %qE"),
10490 G_("decrement of read-only location %qE"),
10491 G_("read-only location %qE used as %<asm%> output")),
10495 /* Print an error message for an invalid lvalue. USE says
10496 how the lvalue is being used and so selects the error message. LOC
10497 is the location for the error. */
10500 lvalue_error (location_t loc, enum lvalue_use use)
10505 error_at (loc, "lvalue required as left operand of assignment");
10508 error_at (loc, "lvalue required as increment operand");
10511 error_at (loc, "lvalue required as decrement operand");
10514 error_at (loc, "lvalue required as unary %<&%> operand");
10517 error_at (loc, "lvalue required in asm statement");
10520 gcc_unreachable ();
10524 /* Print an error message for an invalid indirection of type TYPE.
10525 ERRSTRING is the name of the operator for the indirection. */
10528 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10533 gcc_assert (c_dialect_cxx ());
10534 error_at (loc, "invalid type argument (have %qT)", type);
10536 case RO_ARRAY_INDEXING:
10538 "invalid type argument of array indexing (have %qT)",
10541 case RO_UNARY_STAR:
10543 "invalid type argument of unary %<*%> (have %qT)",
10548 "invalid type argument of %<->%> (have %qT)",
10551 case RO_ARROW_STAR:
10553 "invalid type argument of %<->*%> (have %qT)",
10556 case RO_IMPLICIT_CONVERSION:
10558 "invalid type argument of implicit conversion (have %qT)",
10562 gcc_unreachable ();
10566 /* *PTYPE is an incomplete array. Complete it with a domain based on
10567 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10568 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10569 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10572 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10574 tree maxindex, type, main_type, elt, unqual_elt;
10575 int failure = 0, quals;
10576 hashval_t hashcode = 0;
10577 bool overflow_p = false;
10579 maxindex = size_zero_node;
10582 if (TREE_CODE (initial_value) == STRING_CST)
10585 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10586 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10588 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10590 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10592 if (vec_safe_is_empty (v))
10596 maxindex = ssize_int (-1);
10601 unsigned HOST_WIDE_INT cnt;
10602 constructor_elt *ce;
10603 bool fold_p = false;
10606 maxindex = (*v)[0].index, fold_p = true;
10608 curindex = maxindex;
10610 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10612 bool curfold_p = false;
10614 curindex = ce->index, curfold_p = true;
10619 /* Since we treat size types now as ordinary
10620 unsigned types, we need an explicit overflow
10622 tree orig = curindex;
10623 curindex = fold_convert (sizetype, curindex);
10624 overflow_p |= tree_int_cst_lt (curindex, orig);
10626 curindex = size_binop (PLUS_EXPR, curindex,
10629 if (tree_int_cst_lt (maxindex, curindex))
10630 maxindex = curindex, fold_p = curfold_p;
10634 tree orig = maxindex;
10635 maxindex = fold_convert (sizetype, maxindex);
10636 overflow_p |= tree_int_cst_lt (maxindex, orig);
10642 /* Make an error message unless that happened already. */
10643 if (initial_value != error_mark_node)
10655 elt = TREE_TYPE (type);
10656 quals = TYPE_QUALS (strip_array_types (elt));
10660 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10662 /* Using build_distinct_type_copy and modifying things afterward instead
10663 of using build_array_type to create a new type preserves all of the
10664 TYPE_LANG_FLAG_? bits that the front end may have set. */
10665 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10666 TREE_TYPE (main_type) = unqual_elt;
10667 TYPE_DOMAIN (main_type)
10668 = build_range_type (TREE_TYPE (maxindex),
10669 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10670 layout_type (main_type);
10672 /* Make sure we have the canonical MAIN_TYPE. */
10673 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10674 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10676 main_type = type_hash_canon (hashcode, main_type);
10678 /* Fix the canonical type. */
10679 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10680 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10681 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10682 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10683 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10684 != TYPE_DOMAIN (main_type)))
10685 TYPE_CANONICAL (main_type)
10686 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10687 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10689 TYPE_CANONICAL (main_type) = main_type;
10694 type = c_build_qualified_type (main_type, quals);
10696 if (COMPLETE_TYPE_P (type)
10697 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10698 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10700 error ("size of array is too large");
10701 /* If we proceed with the array type as it is, we'll eventually
10702 crash in tree_to_[su]hwi(). */
10703 type = error_mark_node;
10710 /* Like c_mark_addressable but don't check register qualifier. */
10712 c_common_mark_addressable_vec (tree t)
10714 while (handled_component_p (t))
10715 t = TREE_OPERAND (t, 0);
10717 && TREE_CODE (t) != PARM_DECL
10718 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
10720 TREE_ADDRESSABLE (t) = 1;
10725 /* Used to help initialize the builtin-types.def table. When a type of
10726 the correct size doesn't exist, use error_mark_node instead of NULL.
10727 The later results in segfaults even when a decl using the type doesn't
10731 builtin_type_for_size (int size, bool unsignedp)
10733 tree type = c_common_type_for_size (size, unsignedp);
10734 return type ? type : error_mark_node;
10737 /* A helper function for resolve_overloaded_builtin in resolving the
10738 overloaded __sync_ builtins. Returns a positive power of 2 if the
10739 first operand of PARAMS is a pointer to a supported data type.
10740 Returns 0 if an error is encountered.
10741 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10745 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
10747 /* Type of the argument. */
10749 /* Type the argument points to. */
10753 if (vec_safe_is_empty (params))
10755 error ("too few arguments to function %qE", function);
10759 argtype = type = TREE_TYPE ((*params)[0]);
10760 if (TREE_CODE (type) == ARRAY_TYPE)
10762 /* Force array-to-pointer decay for C++. */
10763 gcc_assert (c_dialect_cxx());
10764 (*params)[0] = default_conversion ((*params)[0]);
10765 type = TREE_TYPE ((*params)[0]);
10767 if (TREE_CODE (type) != POINTER_TYPE)
10770 type = TREE_TYPE (type);
10771 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10774 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
10777 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10778 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10782 /* Issue the diagnostic only if the argument is valid, otherwise
10783 it would be redundant at best and could be misleading. */
10784 if (argtype != error_mark_node)
10785 error ("operand type %qT is incompatible with argument %d of %qE",
10786 argtype, 1, function);
10790 /* A helper function for resolve_overloaded_builtin. Adds casts to
10791 PARAMS to make arguments match up with those of FUNCTION. Drops
10792 the variadic arguments at the end. Returns false if some error
10793 was encountered; true on success. */
10796 sync_resolve_params (location_t loc, tree orig_function, tree function,
10797 vec<tree, va_gc> *params, bool orig_format)
10799 function_args_iterator iter;
10801 unsigned int parmnum;
10803 function_args_iter_init (&iter, TREE_TYPE (function));
10804 /* We've declared the implementation functions to use "volatile void *"
10805 as the pointer parameter, so we shouldn't get any complaints from the
10806 call to check_function_arguments what ever type the user used. */
10807 function_args_iter_next (&iter);
10808 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10809 ptype = TYPE_MAIN_VARIANT (ptype);
10811 /* For the rest of the values, we need to cast these to FTYPE, so that we
10812 don't get warnings for passing pointer types, etc. */
10816 tree val, arg_type;
10818 arg_type = function_args_iter_cond (&iter);
10819 /* XXX void_type_node belies the abstraction. */
10820 if (arg_type == void_type_node)
10824 if (params->length () <= parmnum)
10826 error_at (loc, "too few arguments to function %qE", orig_function);
10830 /* Only convert parameters if arg_type is unsigned integer type with
10831 new format sync routines, i.e. don't attempt to convert pointer
10832 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10833 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10835 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10837 /* Ideally for the first conversion we'd use convert_for_assignment
10838 so that we get warnings for anything that doesn't match the pointer
10839 type. This isn't portable across the C and C++ front ends atm. */
10840 val = (*params)[parmnum];
10841 val = convert (ptype, val);
10842 val = convert (arg_type, val);
10843 (*params)[parmnum] = val;
10846 function_args_iter_next (&iter);
10849 /* __atomic routines are not variadic. */
10850 if (!orig_format && params->length () != parmnum + 1)
10852 error_at (loc, "too many arguments to function %qE", orig_function);
10856 /* The definition of these primitives is variadic, with the remaining
10857 being "an optional list of variables protected by the memory barrier".
10858 No clue what that's supposed to mean, precisely, but we consider all
10859 call-clobbered variables to be protected so we're safe. */
10860 params->truncate (parmnum + 1);
10865 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10866 RESULT to make it match the type of the first pointer argument in
10870 sync_resolve_return (tree first_param, tree result, bool orig_format)
10872 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10873 tree rtype = TREE_TYPE (result);
10874 ptype = TYPE_MAIN_VARIANT (ptype);
10876 /* New format doesn't require casting unless the types are the same size. */
10877 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10878 return convert (ptype, result);
10883 /* This function verifies the PARAMS to generic atomic FUNCTION.
10884 It returns the size if all the parameters are the same size, otherwise
10885 0 is returned if the parameters are invalid. */
10888 get_atomic_generic_size (location_t loc, tree function,
10889 vec<tree, va_gc> *params)
10891 unsigned int n_param;
10892 unsigned int n_model;
10897 /* Determine the parameter makeup. */
10898 switch (DECL_FUNCTION_CODE (function))
10900 case BUILT_IN_ATOMIC_EXCHANGE:
10904 case BUILT_IN_ATOMIC_LOAD:
10905 case BUILT_IN_ATOMIC_STORE:
10909 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10914 gcc_unreachable ();
10917 if (vec_safe_length (params) != n_param)
10919 error_at (loc, "incorrect number of arguments to function %qE", function);
10923 /* Get type of first parameter, and determine its size. */
10924 type_0 = TREE_TYPE ((*params)[0]);
10925 if (TREE_CODE (type_0) == ARRAY_TYPE)
10927 /* Force array-to-pointer decay for C++. */
10928 gcc_assert (c_dialect_cxx());
10929 (*params)[0] = default_conversion ((*params)[0]);
10930 type_0 = TREE_TYPE ((*params)[0]);
10932 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10934 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10939 /* Types must be compile time constant sizes. */
10940 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10943 "argument 1 of %qE must be a pointer to a constant size type",
10948 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10950 /* Zero size objects are not allowed. */
10954 "argument 1 of %qE must be a pointer to a nonzero size object",
10959 /* Check each other parameter is a pointer and the same size. */
10960 for (x = 0; x < n_param - n_model; x++)
10963 tree type = TREE_TYPE ((*params)[x]);
10964 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10965 if (n_param == 6 && x == 3)
10967 if (!POINTER_TYPE_P (type))
10969 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10973 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10974 size = type_size ? tree_to_uhwi (type_size) : 0;
10975 if (size != size_0)
10977 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10983 /* Check memory model parameters for validity. */
10984 for (x = n_param - n_model ; x < n_param; x++)
10986 tree p = (*params)[x];
10987 if (TREE_CODE (p) == INTEGER_CST)
10989 int i = tree_to_uhwi (p);
10990 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
10992 warning_at (loc, OPT_Winvalid_memory_model,
10993 "invalid memory model argument %d of %qE", x + 1,
10998 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11000 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11010 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11011 at the beginning of the parameter list PARAMS representing the size of the
11012 objects. This is to match the library ABI requirement. LOC is the location
11013 of the function call.
11014 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11015 returned to allow the external call to be constructed. */
11018 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
11019 vec<tree, va_gc> *params)
11023 /* Insert a SIZE_T parameter as the first param. If there isn't
11024 enough space, allocate a new vector and recursively re-build with that. */
11025 if (!params->space (1))
11027 unsigned int z, len;
11028 vec<tree, va_gc> *v;
11031 len = params->length ();
11032 vec_alloc (v, len + 1);
11033 v->quick_push (build_int_cst (size_type_node, n));
11034 for (z = 0; z < len; z++)
11035 v->quick_push ((*params)[z]);
11036 f = build_function_call_vec (loc, vNULL, function, v, NULL);
11041 /* Add the size parameter and leave as a function call for processing. */
11042 size_node = build_int_cst (size_type_node, n);
11043 params->quick_insert (0, size_node);
11048 /* Return whether atomic operations for naturally aligned N-byte
11049 arguments are supported, whether inline or through libatomic. */
11051 atomic_size_supported_p (int n)
11062 return targetm.scalar_mode_supported_p (TImode);
11069 /* This will process an __atomic_exchange function call, determine whether it
11070 needs to be mapped to the _N variation, or turned into a library call.
11071 LOC is the location of the builtin call.
11072 FUNCTION is the DECL that has been invoked;
11073 PARAMS is the argument list for the call. The return value is non-null
11074 TRUE is returned if it is translated into the proper format for a call to the
11075 external library, and NEW_RETURN is set the tree for that function.
11076 FALSE is returned if processing for the _N variation is required, and
11077 NEW_RETURN is set to the return value the result is copied into. */
11079 resolve_overloaded_atomic_exchange (location_t loc, tree function,
11080 vec<tree, va_gc> *params, tree *new_return)
11082 tree p0, p1, p2, p3;
11083 tree I_type, I_type_ptr;
11084 int n = get_atomic_generic_size (loc, function, params);
11086 /* Size of 0 is an error condition. */
11089 *new_return = error_mark_node;
11093 /* If not a lock-free size, change to the library generic format. */
11094 if (!atomic_size_supported_p (n))
11096 *new_return = add_atomic_size_parameter (n, loc, function, params);
11100 /* Otherwise there is a lockfree match, transform the call from:
11101 void fn(T* mem, T* desired, T* return, model)
11103 *return = (T) (fn (In* mem, (In) *desired, model)) */
11110 /* Create pointer to appropriate size. */
11111 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11112 I_type_ptr = build_pointer_type (I_type);
11114 /* Convert object pointer to required type. */
11115 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11117 /* Convert new value to required type, and dereference it. */
11118 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11119 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11122 /* Move memory model to the 3rd position, and end param list. */
11124 params->truncate (3);
11126 /* Convert return pointer and dereference it for later assignment. */
11127 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11133 /* This will process an __atomic_compare_exchange function call, determine
11134 whether it needs to be mapped to the _N variation, or turned into a lib call.
11135 LOC is the location of the builtin call.
11136 FUNCTION is the DECL that has been invoked;
11137 PARAMS is the argument list for the call. The return value is non-null
11138 TRUE is returned if it is translated into the proper format for a call to the
11139 external library, and NEW_RETURN is set the tree for that function.
11140 FALSE is returned if processing for the _N variation is required. */
11143 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11144 vec<tree, va_gc> *params,
11148 tree I_type, I_type_ptr;
11149 int n = get_atomic_generic_size (loc, function, params);
11151 /* Size of 0 is an error condition. */
11154 *new_return = error_mark_node;
11158 /* If not a lock-free size, change to the library generic format. */
11159 if (!atomic_size_supported_p (n))
11161 /* The library generic format does not have the weak parameter, so
11162 remove it from the param list. Since a parameter has been removed,
11163 we can be sure that there is room for the SIZE_T parameter, meaning
11164 there will not be a recursive rebuilding of the parameter list, so
11165 there is no danger this will be done twice. */
11168 (*params)[3] = (*params)[4];
11169 (*params)[4] = (*params)[5];
11170 params->truncate (5);
11172 *new_return = add_atomic_size_parameter (n, loc, function, params);
11176 /* Otherwise, there is a match, so the call needs to be transformed from:
11177 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11179 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11185 /* Create pointer to appropriate size. */
11186 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11187 I_type_ptr = build_pointer_type (I_type);
11189 /* Convert object pointer to required type. */
11190 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11193 /* Convert expected pointer to required type. */
11194 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11197 /* Convert desired value to required type, and dereference it. */
11198 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11199 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11202 /* The rest of the parameters are fine. NULL means no special return value
11204 *new_return = NULL;
11209 /* This will process an __atomic_load function call, determine whether it
11210 needs to be mapped to the _N variation, or turned into a library call.
11211 LOC is the location of the builtin call.
11212 FUNCTION is the DECL that has been invoked;
11213 PARAMS is the argument list for the call. The return value is non-null
11214 TRUE is returned if it is translated into the proper format for a call to the
11215 external library, and NEW_RETURN is set the tree for that function.
11216 FALSE is returned if processing for the _N variation is required, and
11217 NEW_RETURN is set to the return value the result is copied into. */
11220 resolve_overloaded_atomic_load (location_t loc, tree function,
11221 vec<tree, va_gc> *params, tree *new_return)
11224 tree I_type, I_type_ptr;
11225 int n = get_atomic_generic_size (loc, function, params);
11227 /* Size of 0 is an error condition. */
11230 *new_return = error_mark_node;
11234 /* If not a lock-free size, change to the library generic format. */
11235 if (!atomic_size_supported_p (n))
11237 *new_return = add_atomic_size_parameter (n, loc, function, params);
11241 /* Otherwise, there is a match, so the call needs to be transformed from:
11242 void fn(T* mem, T* return, model)
11244 *return = (T) (fn ((In *) mem, model)) */
11250 /* Create pointer to appropriate size. */
11251 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11252 I_type_ptr = build_pointer_type (I_type);
11254 /* Convert object pointer to required type. */
11255 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11258 /* Move memory model to the 2nd position, and end param list. */
11260 params->truncate (2);
11262 /* Convert return pointer and dereference it for later assignment. */
11263 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11269 /* This will process an __atomic_store function call, determine whether it
11270 needs to be mapped to the _N variation, or turned into a library call.
11271 LOC is the location of the builtin call.
11272 FUNCTION is the DECL that has been invoked;
11273 PARAMS is the argument list for the call. The return value is non-null
11274 TRUE is returned if it is translated into the proper format for a call to the
11275 external library, and NEW_RETURN is set the tree for that function.
11276 FALSE is returned if processing for the _N variation is required, and
11277 NEW_RETURN is set to the return value the result is copied into. */
11280 resolve_overloaded_atomic_store (location_t loc, tree function,
11281 vec<tree, va_gc> *params, tree *new_return)
11284 tree I_type, I_type_ptr;
11285 int n = get_atomic_generic_size (loc, function, params);
11287 /* Size of 0 is an error condition. */
11290 *new_return = error_mark_node;
11294 /* If not a lock-free size, change to the library generic format. */
11295 if (!atomic_size_supported_p (n))
11297 *new_return = add_atomic_size_parameter (n, loc, function, params);
11301 /* Otherwise, there is a match, so the call needs to be transformed from:
11302 void fn(T* mem, T* value, model)
11304 fn ((In *) mem, (In) *value, model) */
11309 /* Create pointer to appropriate size. */
11310 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11311 I_type_ptr = build_pointer_type (I_type);
11313 /* Convert object pointer to required type. */
11314 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11317 /* Convert new value to required type, and dereference it. */
11318 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11319 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11322 /* The memory model is in the right spot already. Return is void. */
11323 *new_return = NULL_TREE;
11329 /* Some builtin functions are placeholders for other expressions. This
11330 function should be called immediately after parsing the call expression
11331 before surrounding code has committed to the type of the expression.
11333 LOC is the location of the builtin call.
11335 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11336 PARAMS is the argument list for the call. The return value is non-null
11337 when expansion is complete, and null if normal processing should
11341 resolve_overloaded_builtin (location_t loc, tree function,
11342 vec<tree, va_gc> *params)
11344 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11346 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11347 Those are not valid to call with a pointer to _Bool (or C++ bool)
11348 and so must be rejected. */
11349 bool fetch_op = true;
11350 bool orig_format = true;
11351 tree new_return = NULL_TREE;
11353 switch (DECL_BUILT_IN_CLASS (function))
11355 case BUILT_IN_NORMAL:
11358 if (targetm.resolve_overloaded_builtin)
11359 return targetm.resolve_overloaded_builtin (loc, function, params);
11366 /* Handle BUILT_IN_NORMAL here. */
11369 case BUILT_IN_ATOMIC_EXCHANGE:
11370 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11371 case BUILT_IN_ATOMIC_LOAD:
11372 case BUILT_IN_ATOMIC_STORE:
11374 /* Handle these 4 together so that they can fall through to the next
11375 case if the call is transformed to an _N variant. */
11378 case BUILT_IN_ATOMIC_EXCHANGE:
11380 if (resolve_overloaded_atomic_exchange (loc, function, params,
11383 /* Change to the _N variant. */
11384 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11388 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11390 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11394 /* Change to the _N variant. */
11395 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11398 case BUILT_IN_ATOMIC_LOAD:
11400 if (resolve_overloaded_atomic_load (loc, function, params,
11403 /* Change to the _N variant. */
11404 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11407 case BUILT_IN_ATOMIC_STORE:
11409 if (resolve_overloaded_atomic_store (loc, function, params,
11412 /* Change to the _N variant. */
11413 orig_code = BUILT_IN_ATOMIC_STORE_N;
11417 gcc_unreachable ();
11419 /* Fallthrough to the normal processing. */
11421 case BUILT_IN_ATOMIC_EXCHANGE_N:
11422 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11423 case BUILT_IN_ATOMIC_LOAD_N:
11424 case BUILT_IN_ATOMIC_STORE_N:
11427 /* Fallthrough to further processing. */
11429 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11430 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11431 case BUILT_IN_ATOMIC_AND_FETCH_N:
11432 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11433 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11434 case BUILT_IN_ATOMIC_OR_FETCH_N:
11435 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11436 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11437 case BUILT_IN_ATOMIC_FETCH_AND_N:
11438 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11439 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11440 case BUILT_IN_ATOMIC_FETCH_OR_N:
11442 orig_format = false;
11443 /* Fallthru for parameter processing. */
11445 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11446 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11447 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11448 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11449 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11450 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11451 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11452 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11453 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11454 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11455 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11456 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11457 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11458 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11459 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11460 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11462 /* The following are not _FETCH_OPs and must be accepted with
11463 pointers to _Bool (or C++ bool). */
11466 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11467 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11468 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11469 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11471 int n = sync_resolve_size (function, params, fetch_op);
11472 tree new_function, first_param, result;
11473 enum built_in_function fncode;
11476 return error_mark_node;
11478 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11479 new_function = builtin_decl_explicit (fncode);
11480 if (!sync_resolve_params (loc, function, new_function, params,
11482 return error_mark_node;
11484 first_param = (*params)[0];
11485 result = build_function_call_vec (loc, vNULL, new_function, params,
11487 if (result == error_mark_node)
11489 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11490 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11491 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11492 result = sync_resolve_return (first_param, result, orig_format);
11495 /* Prevent -Wunused-value warning. */
11496 TREE_USED (result) = true;
11498 /* If new_return is set, assign function to that expr and cast the
11499 result to void since the generic interface returned void. */
11502 /* Cast function result from I{1,2,4,8,16} to the required type. */
11503 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11504 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11506 TREE_SIDE_EFFECTS (result) = 1;
11507 protected_set_expr_location (result, loc);
11508 result = convert (void_type_node, result);
11518 /* vector_types_compatible_elements_p is used in type checks of vectors
11519 values used as operands of binary operators. Where it returns true, and
11520 the other checks of the caller succeed (being vector types in he first
11521 place, and matching number of elements), we can just treat the types
11522 as essentially the same.
11523 Contrast with vector_targets_convertible_p, which is used for vector
11524 pointer types, and vector_types_convertible_p, which will allow
11525 language-specific matches under the control of flag_lax_vector_conversions,
11526 and might still require a conversion. */
11527 /* True if vector types T1 and T2 can be inputs to the same binary
11528 operator without conversion.
11529 We don't check the overall vector size here because some of our callers
11530 want to give different error messages when the vectors are compatible
11531 except for the element count. */
11534 vector_types_compatible_elements_p (tree t1, tree t2)
11536 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11537 t1 = TREE_TYPE (t1);
11538 t2 = TREE_TYPE (t2);
11540 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11542 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11543 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11544 || c2 == FIXED_POINT_TYPE));
11546 t1 = c_common_signed_type (t1);
11547 t2 = c_common_signed_type (t2);
11548 /* Equality works here because c_common_signed_type uses
11549 TYPE_MAIN_VARIANT. */
11552 if (opaque && c1 == c2
11553 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11554 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11559 /* Check for missing format attributes on function pointers. LTYPE is
11560 the new type or left-hand side type. RTYPE is the old type or
11561 right-hand side type. Returns TRUE if LTYPE is missing the desired
11565 check_missing_format_attribute (tree ltype, tree rtype)
11567 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11570 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11571 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11576 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11577 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11585 /* Subscripting with type char is likely to lose on a machine where
11586 chars are signed. So warn on any machine, but optionally. Don't
11587 warn for unsigned char since that type is safe. Don't warn for
11588 signed char because anyone who uses that must have done so
11589 deliberately. Furthermore, we reduce the false positive load by
11590 warning only for non-constant value of type char. */
11593 warn_array_subscript_with_type_char (location_t loc, tree index)
11595 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11596 && TREE_CODE (index) != INTEGER_CST)
11597 warning_at (loc, OPT_Wchar_subscripts,
11598 "array subscript has type %<char%>");
11601 /* Implement -Wparentheses for the unexpected C precedence rules, to
11602 cover cases like x + y << z which readers are likely to
11603 misinterpret. We have seen an expression in which CODE is a binary
11604 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11605 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11606 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11607 expression was not formed using a binary or unary operator, or it
11608 was enclosed in parentheses. */
11611 warn_about_parentheses (location_t loc, enum tree_code code,
11612 enum tree_code code_left, tree arg_left,
11613 enum tree_code code_right, tree arg_right)
11615 if (!warn_parentheses)
11618 /* This macro tests that the expression ARG with original tree code
11619 CODE appears to be a boolean expression. or the result of folding a
11620 boolean expression. */
11621 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11622 (truth_value_p (TREE_CODE (ARG)) \
11623 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11624 /* Folding may create 0 or 1 integers from other expressions. */ \
11625 || ((CODE) != INTEGER_CST \
11626 && (integer_onep (ARG) || integer_zerop (ARG))))
11631 if (code_left == PLUS_EXPR)
11632 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11633 "suggest parentheses around %<+%> inside %<<<%>");
11634 else if (code_right == PLUS_EXPR)
11635 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11636 "suggest parentheses around %<+%> inside %<<<%>");
11637 else if (code_left == MINUS_EXPR)
11638 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11639 "suggest parentheses around %<-%> inside %<<<%>");
11640 else if (code_right == MINUS_EXPR)
11641 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11642 "suggest parentheses around %<-%> inside %<<<%>");
11646 if (code_left == PLUS_EXPR)
11647 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11648 "suggest parentheses around %<+%> inside %<>>%>");
11649 else if (code_right == PLUS_EXPR)
11650 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11651 "suggest parentheses around %<+%> inside %<>>%>");
11652 else if (code_left == MINUS_EXPR)
11653 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11654 "suggest parentheses around %<-%> inside %<>>%>");
11655 else if (code_right == MINUS_EXPR)
11656 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11657 "suggest parentheses around %<-%> inside %<>>%>");
11660 case TRUTH_ORIF_EXPR:
11661 if (code_left == TRUTH_ANDIF_EXPR)
11662 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11663 "suggest parentheses around %<&&%> within %<||%>");
11664 else if (code_right == TRUTH_ANDIF_EXPR)
11665 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11666 "suggest parentheses around %<&&%> within %<||%>");
11670 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11671 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11672 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11673 "suggest parentheses around arithmetic in operand of %<|%>");
11674 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11675 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11676 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11677 "suggest parentheses around arithmetic in operand of %<|%>");
11678 /* Check cases like x|y==z */
11679 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11680 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11681 "suggest parentheses around comparison in operand of %<|%>");
11682 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11683 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11684 "suggest parentheses around comparison in operand of %<|%>");
11685 /* Check cases like !x | y */
11686 else if (code_left == TRUTH_NOT_EXPR
11687 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11688 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11689 "suggest parentheses around operand of "
11690 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11694 if (code_left == BIT_AND_EXPR
11695 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11696 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11697 "suggest parentheses around arithmetic in operand of %<^%>");
11698 else if (code_right == BIT_AND_EXPR
11699 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11700 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11701 "suggest parentheses around arithmetic in operand of %<^%>");
11702 /* Check cases like x^y==z */
11703 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11704 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11705 "suggest parentheses around comparison in operand of %<^%>");
11706 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11707 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11708 "suggest parentheses around comparison in operand of %<^%>");
11712 if (code_left == PLUS_EXPR)
11713 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11714 "suggest parentheses around %<+%> in operand of %<&%>");
11715 else if (code_right == PLUS_EXPR)
11716 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11717 "suggest parentheses around %<+%> in operand of %<&%>");
11718 else if (code_left == MINUS_EXPR)
11719 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11720 "suggest parentheses around %<-%> in operand of %<&%>");
11721 else if (code_right == MINUS_EXPR)
11722 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11723 "suggest parentheses around %<-%> in operand of %<&%>");
11724 /* Check cases like x&y==z */
11725 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11726 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11727 "suggest parentheses around comparison in operand of %<&%>");
11728 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11729 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11730 "suggest parentheses around comparison in operand of %<&%>");
11731 /* Check cases like !x & y */
11732 else if (code_left == TRUTH_NOT_EXPR
11733 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11734 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11735 "suggest parentheses around operand of "
11736 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11740 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11741 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11742 "suggest parentheses around comparison in operand of %<==%>");
11743 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11744 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11745 "suggest parentheses around comparison in operand of %<==%>");
11748 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11749 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11750 "suggest parentheses around comparison in operand of %<!=%>");
11751 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11752 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11753 "suggest parentheses around comparison in operand of %<!=%>");
11757 if (TREE_CODE_CLASS (code) == tcc_comparison)
11759 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11760 && code_left != NE_EXPR && code_left != EQ_EXPR
11761 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11762 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11763 "comparisons like %<X<=Y<=Z%> do not "
11764 "have their mathematical meaning");
11765 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11766 && code_right != NE_EXPR && code_right != EQ_EXPR
11767 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11768 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11769 "comparisons like %<X<=Y<=Z%> do not "
11770 "have their mathematical meaning");
11774 #undef NOT_A_BOOLEAN_EXPR_P
11777 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11780 warn_for_unused_label (tree label)
11782 if (!TREE_USED (label))
11784 if (DECL_INITIAL (label))
11785 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11787 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11791 /* Warn for division by zero according to the value of DIVISOR. LOC
11792 is the location of the division operator. */
11795 warn_for_div_by_zero (location_t loc, tree divisor)
11797 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11798 about division by zero. Do not issue a warning if DIVISOR has a
11799 floating-point type, since we consider 0.0/0.0 a valid way of
11800 generating a NaN. */
11801 if (c_inhibit_evaluation_warnings == 0
11802 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11803 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11806 /* Subroutine of build_binary_op. Give warnings for comparisons
11807 between signed and unsigned quantities that may fail. Do the
11808 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11809 so that casts will be considered, but default promotions won't
11812 LOCATION is the location of the comparison operator.
11814 The arguments of this function map directly to local variables
11815 of build_binary_op. */
11818 warn_for_sign_compare (location_t location,
11819 tree orig_op0, tree orig_op1,
11820 tree op0, tree op1,
11821 tree result_type, enum tree_code resultcode)
11823 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11824 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11825 int unsignedp0, unsignedp1;
11827 /* In C++, check for comparison of different enum types. */
11828 if (c_dialect_cxx()
11829 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11830 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11831 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11832 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11834 warning_at (location,
11835 OPT_Wsign_compare, "comparison between types %qT and %qT",
11836 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11839 /* Do not warn if the comparison is being done in a signed type,
11840 since the signed type will only be chosen if it can represent
11841 all the values of the unsigned type. */
11842 if (!TYPE_UNSIGNED (result_type))
11844 /* Do not warn if both operands are unsigned. */
11845 else if (op0_signed == op1_signed)
11849 tree sop, uop, base_type;
11853 sop = orig_op0, uop = orig_op1;
11855 sop = orig_op1, uop = orig_op0;
11857 STRIP_TYPE_NOPS (sop);
11858 STRIP_TYPE_NOPS (uop);
11859 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11860 ? TREE_TYPE (result_type) : result_type);
11862 /* Do not warn if the signed quantity is an unsuffixed integer
11863 literal (or some static constant expression involving such
11864 literals or a conditional expression involving such literals)
11865 and it is non-negative. */
11866 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11868 /* Do not warn if the comparison is an equality operation, the
11869 unsigned quantity is an integral constant, and it would fit
11870 in the result if the result were signed. */
11871 else if (TREE_CODE (uop) == INTEGER_CST
11872 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11873 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11875 /* In C, do not warn if the unsigned quantity is an enumeration
11876 constant and its maximum value would fit in the result if the
11877 result were signed. */
11878 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11879 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11880 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11881 c_common_signed_type (base_type)))
11884 warning_at (location,
11886 "comparison between signed and unsigned integer expressions");
11889 /* Warn if two unsigned values are being compared in a size larger
11890 than their original size, and one (and only one) is the result of
11891 a `~' operator. This comparison will always fail.
11893 Also warn if one operand is a constant, and the constant does not
11894 have all bits set that are set in the ~ operand when it is
11897 op0 = c_common_get_narrower (op0, &unsignedp0);
11898 op1 = c_common_get_narrower (op1, &unsignedp1);
11900 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11901 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11903 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11904 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11905 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11906 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11908 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11911 HOST_WIDE_INT constant, mask;
11915 if (tree_fits_shwi_p (op0))
11918 unsignedp = unsignedp1;
11919 constant = tree_to_shwi (op0);
11924 unsignedp = unsignedp0;
11925 constant = tree_to_shwi (op1);
11928 bits = TYPE_PRECISION (TREE_TYPE (primop));
11929 if (bits < TYPE_PRECISION (result_type)
11930 && bits < HOST_BITS_PER_LONG && unsignedp)
11932 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
11933 if ((mask & constant) != mask)
11936 warning_at (location, OPT_Wsign_compare,
11937 "promoted ~unsigned is always non-zero");
11939 warning_at (location, OPT_Wsign_compare,
11940 "comparison of promoted ~unsigned with constant");
11944 else if (unsignedp0 && unsignedp1
11945 && (TYPE_PRECISION (TREE_TYPE (op0))
11946 < TYPE_PRECISION (result_type))
11947 && (TYPE_PRECISION (TREE_TYPE (op1))
11948 < TYPE_PRECISION (result_type)))
11949 warning_at (location, OPT_Wsign_compare,
11950 "comparison of promoted ~unsigned with unsigned");
11954 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11955 type via c_common_type. If -Wdouble-promotion is in use, and the
11956 conditions for warning have been met, issue a warning. GMSGID is
11957 the warning message. It must have two %T specifiers for the type
11958 that was converted (generally "float") and the type to which it was
11959 converted (generally "double), respectively. LOC is the location
11960 to which the awrning should refer. */
11963 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11964 const char *gmsgid, location_t loc)
11968 if (!warn_double_promotion)
11970 /* If the conversion will not occur at run-time, there is no need to
11972 if (c_inhibit_evaluation_warnings)
11974 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11975 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11977 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11978 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11979 source_type = type1;
11980 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11981 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11982 source_type = type2;
11985 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11988 /* Possibly warn about unused parameters. */
11991 do_warn_unused_parameter (tree fn)
11995 for (decl = DECL_ARGUMENTS (fn);
11996 decl; decl = DECL_CHAIN (decl))
11997 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
11998 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
11999 && !TREE_NO_WARNING (decl))
12000 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12001 "unused parameter %qD", decl);
12005 /* Setup a TYPE_DECL node as a typedef representation.
12007 X is a TYPE_DECL for a typedef statement. Create a brand new
12008 ..._TYPE node (which will be just a variant of the existing
12009 ..._TYPE node with identical properties) and then install X
12010 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12012 The whole point here is to end up with a situation where each
12013 and every ..._TYPE node the compiler creates will be uniquely
12014 associated with AT MOST one node representing a typedef name.
12015 This way, even though the compiler substitutes corresponding
12016 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12017 early on, later parts of the compiler can always do the reverse
12018 translation and get back the corresponding typedef name. For
12021 typedef struct S MY_TYPE;
12024 Later parts of the compiler might only know that `object' was of
12025 type `struct S' if it were not for code just below. With this
12026 code however, later parts of the compiler see something like:
12028 struct S' == struct S
12029 typedef struct S' MY_TYPE;
12032 And they can then deduce (from the node for type struct S') that
12033 the original object declaration was:
12037 Being able to do this is important for proper support of protoize,
12038 and also for generating precise symbolic debugging information
12039 which takes full account of the programmer's (typedef) vocabulary.
12041 Obviously, we don't want to generate a duplicate ..._TYPE node if
12042 the TYPE_DECL node that we are now processing really represents a
12043 standard built-in type. */
12046 set_underlying_type (tree x)
12048 if (x == error_mark_node)
12050 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
12052 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12053 TYPE_NAME (TREE_TYPE (x)) = x;
12055 else if (TREE_TYPE (x) != error_mark_node
12056 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12058 tree tt = TREE_TYPE (x);
12059 DECL_ORIGINAL_TYPE (x) = tt;
12060 tt = build_variant_type_copy (tt);
12061 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12062 TYPE_NAME (tt) = x;
12063 TREE_USED (tt) = TREE_USED (x);
12064 TREE_TYPE (x) = tt;
12068 /* Record the types used by the current global variable declaration
12069 being parsed, so that we can decide later to emit their debug info.
12070 Those types are in types_used_by_cur_var_decl, and we are going to
12071 store them in the types_used_by_vars_hash hash table.
12072 DECL is the declaration of the global variable that has been parsed. */
12075 record_types_used_by_current_var_decl (tree decl)
12077 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12079 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
12081 tree type = types_used_by_cur_var_decl->pop ();
12082 types_used_by_var_decl_insert (type, decl);
12086 /* If DECL is a typedef that is declared in the current function,
12087 record it for the purpose of -Wunused-local-typedefs. */
12090 record_locally_defined_typedef (tree decl)
12092 struct c_language_function *l;
12094 if (!warn_unused_local_typedefs
12096 /* if this is not a locally defined typedef then we are not
12098 || !is_typedef_decl (decl)
12099 || !decl_function_context (decl))
12102 l = (struct c_language_function *) cfun->language;
12103 vec_safe_push (l->local_typedefs, decl);
12106 /* If T is a TYPE_DECL declared locally, mark it as used. */
12109 maybe_record_typedef_use (tree t)
12111 if (!is_typedef_decl (t))
12114 TREE_USED (t) = true;
12117 /* Warn if there are some unused locally defined typedefs in the
12118 current function. */
12121 maybe_warn_unused_local_typedefs (void)
12125 /* The number of times we have emitted -Wunused-local-typedefs
12126 warnings. If this is different from errorcount, that means some
12127 unrelated errors have been issued. In which case, we'll avoid
12128 emitting "unused-local-typedefs" warnings. */
12129 static int unused_local_typedefs_warn_count;
12130 struct c_language_function *l;
12135 if ((l = (struct c_language_function *) cfun->language) == NULL)
12138 if (warn_unused_local_typedefs
12139 && errorcount == unused_local_typedefs_warn_count)
12141 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
12142 if (!TREE_USED (decl))
12143 warning_at (DECL_SOURCE_LOCATION (decl),
12144 OPT_Wunused_local_typedefs,
12145 "typedef %qD locally defined but not used", decl);
12146 unused_local_typedefs_warn_count = errorcount;
12149 vec_free (l->local_typedefs);
12152 /* Warn about boolean expression compared with an integer value different
12153 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12154 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12155 are the operands of the comparison. The caller must ensure that
12156 either operand is a boolean expression. */
12159 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12162 if (TREE_CODE_CLASS (code) != tcc_comparison)
12166 if (f = fold_for_warn (op0),
12167 TREE_CODE (f) == INTEGER_CST)
12169 else if (f = fold_for_warn (op1),
12170 TREE_CODE (f) == INTEGER_CST)
12175 if (!integer_zerop (cst) && !integer_onep (cst))
12177 int sign = (TREE_CODE (op0) == INTEGER_CST
12178 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
12179 if (code == EQ_EXPR
12180 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12181 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12182 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12183 "with boolean expression is always false", cst);
12185 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12186 "with boolean expression is always true", cst);
12188 else if (integer_zerop (cst) || integer_onep (cst))
12190 /* If the non-constant operand isn't of a boolean type, we
12191 don't want to warn here. */
12192 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12193 /* Handle booleans promoted to integers. */
12194 if (CONVERT_EXPR_P (noncst)
12195 && TREE_TYPE (noncst) == integer_type_node
12196 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12198 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12199 && !truth_value_p (TREE_CODE (noncst)))
12201 /* Do some magic to get the right diagnostics. */
12202 bool flag = TREE_CODE (op0) == INTEGER_CST;
12203 flag = integer_zerop (cst) ? flag : !flag;
12204 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12205 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12206 "with boolean expression is always true", cst);
12207 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12208 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12209 "with boolean expression is always false", cst);
12213 /* Warn if signed left shift overflows. We don't warn
12214 about left-shifting 1 into the sign bit in C++14; cf.
12215 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12216 LOC is a location of the shift; OP0 and OP1 are the operands.
12217 Return true if an overflow is detected, false otherwise. */
12220 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12222 if (TREE_CODE (op0) != INTEGER_CST
12223 || TREE_CODE (op1) != INTEGER_CST)
12226 tree type0 = TREE_TYPE (op0);
12227 unsigned int prec0 = TYPE_PRECISION (type0);
12229 /* Left-hand operand must be signed. */
12230 if (TYPE_UNSIGNED (type0))
12233 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12234 + TREE_INT_CST_LOW (op1));
12235 /* Handle the case of left-shifting 1 into the sign bit.
12236 * However, shifting 1 _out_ of the sign bit, as in
12237 * INT_MIN << 1, is considered an overflow.
12239 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
12241 /* Never warn for C++14 onwards. */
12242 if (cxx_dialect >= cxx14)
12244 /* Otherwise only if -Wshift-overflow=2. But return
12245 true to signal an overflow for the sake of integer
12246 constant expressions. */
12247 if (warn_shift_overflow < 2)
12251 bool overflowed = min_prec > prec0;
12252 if (overflowed && c_inhibit_evaluation_warnings == 0)
12253 warning_at (loc, OPT_Wshift_overflow_,
12254 "result of %qE requires %u bits to represent, "
12255 "but %qT only has %u bits",
12256 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12257 min_prec, type0, prec0);
12262 /* The C and C++ parsers both use vectors to hold function arguments.
12263 For efficiency, we keep a cache of unused vectors. This is the
12266 typedef vec<tree, va_gc> *tree_gc_vec;
12267 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12269 /* Return a new vector from the cache. If the cache is empty,
12270 allocate a new vector. These vectors are GC'ed, so it is OK if the
12271 pointer is not released.. */
12274 make_tree_vector (void)
12276 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12277 return tree_vector_cache->pop ();
12280 /* Passing 0 to vec::alloc returns NULL, and our callers require
12281 that we always return a non-NULL value. The vector code uses
12282 4 when growing a NULL vector, so we do too. */
12283 vec<tree, va_gc> *v;
12289 /* Release a vector of trees back to the cache. */
12292 release_tree_vector (vec<tree, va_gc> *vec)
12297 vec_safe_push (tree_vector_cache, vec);
12301 /* Get a new tree vector holding a single tree. */
12304 make_tree_vector_single (tree t)
12306 vec<tree, va_gc> *ret = make_tree_vector ();
12307 ret->quick_push (t);
12311 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12314 make_tree_vector_from_list (tree list)
12316 vec<tree, va_gc> *ret = make_tree_vector ();
12317 for (; list; list = TREE_CHAIN (list))
12318 vec_safe_push (ret, TREE_VALUE (list));
12322 /* Get a new tree vector which is a copy of an existing one. */
12325 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12327 vec<tree, va_gc> *ret;
12331 ret = make_tree_vector ();
12332 vec_safe_reserve (ret, vec_safe_length (orig));
12333 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12334 ret->quick_push (t);
12338 /* Return true if KEYWORD starts a type specifier. */
12341 keyword_begins_type_specifier (enum rid keyword)
12345 case RID_AUTO_TYPE:
12357 case RID_DFLOAT128:
12373 if (keyword >= RID_FIRST_INT_N
12374 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12375 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12381 /* Return true if KEYWORD names a type qualifier. */
12384 keyword_is_type_qualifier (enum rid keyword)
12398 /* Return true if KEYWORD names a storage class specifier.
12400 RID_TYPEDEF is not included in this list despite `typedef' being
12401 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12402 such for syntactic convenience only. */
12405 keyword_is_storage_class_specifier (enum rid keyword)
12421 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12424 keyword_is_function_specifier (enum rid keyword)
12438 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12439 declaration-specifier (C99 6.7). */
12442 keyword_is_decl_specifier (enum rid keyword)
12444 if (keyword_is_storage_class_specifier (keyword)
12445 || keyword_is_type_qualifier (keyword)
12446 || keyword_is_function_specifier (keyword))
12453 case RID_CONSTEXPR:
12460 /* Initialize language-specific-bits of tree_contains_struct. */
12463 c_common_init_ts (void)
12465 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12466 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12467 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12470 /* Build a user-defined numeric literal out of an integer constant type VALUE
12471 with identifier SUFFIX. */
12474 build_userdef_literal (tree suffix_id, tree value,
12475 enum overflow_type overflow, tree num_string)
12477 tree literal = make_node (USERDEF_LITERAL);
12478 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12479 USERDEF_LITERAL_VALUE (literal) = value;
12480 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12481 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12485 /* For vector[index], convert the vector to a
12486 pointer of the underlying type. Return true if the resulting
12487 ARRAY_REF should not be an lvalue. */
12490 convert_vector_to_pointer_for_subscript (location_t loc,
12491 tree *vecp, tree index)
12494 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
12496 tree type = TREE_TYPE (*vecp);
12499 ret = !lvalue_p (*vecp);
12500 if (TREE_CODE (index) == INTEGER_CST)
12501 if (!tree_fits_uhwi_p (index)
12502 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12503 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12507 tree tmp = create_tmp_var_raw (type);
12508 DECL_SOURCE_LOCATION (tmp) = loc;
12509 *vecp = c_save_expr (*vecp);
12510 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12512 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12513 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12515 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12516 *vecp, NULL_TREE, NULL_TREE),
12520 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12521 NULL_TREE, NULL_TREE);
12522 SET_EXPR_LOCATION (*vecp, loc);
12523 c_common_mark_addressable_vec (tmp);
12526 c_common_mark_addressable_vec (*vecp);
12527 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12528 type1 = build_pointer_type (TREE_TYPE (*vecp));
12529 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12531 && !DECL_P (*vecp))
12533 /* If the original vector isn't declared may_alias and it
12534 isn't a bare vector look if the subscripting would
12535 alias the vector we subscript, and if not, force ref-all. */
12536 alias_set_type vecset = get_alias_set (*vecp);
12537 alias_set_type sset = get_alias_set (type);
12538 if (!alias_sets_must_conflict_p (sset, vecset)
12539 && !alias_set_subset_of (sset, vecset))
12542 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12543 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12544 *vecp = convert (type, *vecp);
12549 /* Determine which of the operands, if any, is a scalar that needs to be
12550 converted to a vector, for the range of operations. */
12552 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12555 tree type0 = TREE_TYPE (op0);
12556 tree type1 = TREE_TYPE (op1);
12557 bool integer_only_op = false;
12558 enum stv_conv ret = stv_firstarg;
12560 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
12563 /* Most GENERIC binary expressions require homogeneous arguments.
12564 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12565 argument that is a vector and a second one that is a scalar, so
12566 we never return stv_secondarg for them. */
12569 if (TREE_CODE (type0) == INTEGER_TYPE
12570 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12572 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12575 error_at (loc, "conversion of scalar %qT to vector %qT "
12576 "involves truncation", type0, type1);
12580 return stv_firstarg;
12587 integer_only_op = true;
12588 /* ... fall through ... */
12590 case VEC_COND_EXPR:
12595 case TRUNC_DIV_EXPR:
12596 case CEIL_DIV_EXPR:
12597 case FLOOR_DIV_EXPR:
12598 case ROUND_DIV_EXPR:
12599 case EXACT_DIV_EXPR:
12600 case TRUNC_MOD_EXPR:
12601 case FLOOR_MOD_EXPR:
12609 /* What about UNLT_EXPR? */
12610 if (VECTOR_TYPE_P (type0))
12612 ret = stv_secondarg;
12613 std::swap (type0, type1);
12614 std::swap (op0, op1);
12617 if (TREE_CODE (type0) == INTEGER_TYPE
12618 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12620 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12623 error_at (loc, "conversion of scalar %qT to vector %qT "
12624 "involves truncation", type0, type1);
12629 else if (!integer_only_op
12630 /* Allow integer --> real conversion if safe. */
12631 && (TREE_CODE (type0) == REAL_TYPE
12632 || TREE_CODE (type0) == INTEGER_TYPE)
12633 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12635 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12638 error_at (loc, "conversion of scalar %qT to vector %qT "
12639 "involves truncation", type0, type1);
12648 return stv_nothing;
12651 /* Return true iff ALIGN is an integral constant that is a fundamental
12652 alignment, as defined by [basic.align] in the c++-11
12657 [A fundamental alignment is represented by an alignment less than or
12658 equal to the greatest alignment supported by the implementation
12659 in all contexts, which is equal to
12660 alignof(max_align_t)]. */
12663 cxx_fundamental_alignment_p (unsigned align)
12665 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12666 TYPE_ALIGN (long_double_type_node)));
12669 /* Return true if T is a pointer to a zero-sized aggregate. */
12672 pointer_to_zero_sized_aggr_p (tree t)
12674 if (!POINTER_TYPE_P (t))
12677 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12680 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12681 with no library fallback or for an ADDR_EXPR whose operand is such type
12682 issues an error pointing to the location LOC.
12683 Returns true when the expression has been diagnosed and false
12686 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12688 if (TREE_CODE (expr) == ADDR_EXPR)
12689 expr = TREE_OPERAND (expr, 0);
12691 if (TREE_TYPE (expr)
12692 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12693 && TREE_CODE (expr) == FUNCTION_DECL
12694 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12695 false positives for user-declared built-ins such as abs or
12696 strlen, and for C++ operators new and delete.
12697 The c_decl_implicit() test avoids false positives for implicitly
12698 declared built-ins with library fallbacks (such as abs). */
12699 && DECL_BUILT_IN (expr)
12700 && DECL_IS_BUILTIN (expr)
12701 && !c_decl_implicit (expr)
12702 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12704 if (loc == UNKNOWN_LOCATION)
12705 loc = EXPR_LOC_OR_LOC (expr, input_location);
12707 /* Reject arguments that are built-in functions with
12708 no library fallback. */
12709 error_at (loc, "built-in function %qE must be directly called", expr);
12717 /* If we're creating an if-else-if condition chain, first see if we
12718 already have this COND in the CHAIN. If so, warn and don't add COND
12719 into the vector, otherwise add the COND there. LOC is the location
12723 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12725 /* No chain has been created yet. Do nothing. */
12726 if (*chain == NULL)
12729 if (TREE_SIDE_EFFECTS (cond))
12731 /* Uh-oh! This condition has a side-effect, thus invalidates
12732 the whole chain. */
12740 bool found = false;
12741 FOR_EACH_VEC_ELT (**chain, ix, t)
12742 if (operand_equal_p (cond, t, 0))
12744 if (warning_at (loc, OPT_Wduplicated_cond,
12745 "duplicated %<if%> condition"))
12746 inform (EXPR_LOCATION (t), "previously used here");
12752 && !CONSTANT_CLASS_P (cond)
12753 /* Don't infinitely grow the chain. */
12754 && (*chain)->length () < 512)
12755 (*chain)->safe_push (cond);
12758 /* Check if array size calculations overflow or if the array covers more
12759 than half of the address space. Return true if the size of the array
12760 is valid, false otherwise. TYPE is the type of the array and NAME is
12761 the name of the array, or NULL_TREE for unnamed arrays. */
12764 valid_array_size_p (location_t loc, tree type, tree name)
12766 if (type != error_mark_node
12767 && COMPLETE_TYPE_P (type)
12768 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12769 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
12772 error_at (loc, "size of array %qE is too large", name);
12774 error_at (loc, "size of unnamed array is too large");
12780 #include "gt-c-family-c-common.h"