1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2015 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"
29 #include "fold-const.h"
30 #include "stor-layout.h"
32 #include "stringpool.h"
35 #include "trans-mem.h"
43 #include "common/common-target.h"
44 #include "langhooks.h"
45 #include "tree-inline.h"
47 #include "diagnostic.h"
48 #include "tree-iterator.h"
50 #include "hard-reg-set.h"
54 #include "wide-int-print.h"
55 #include "gimple-expr.h"
57 cpp_reader *parse_in; /* Declared in c-pragma.h. */
59 /* Mode used to build pointers (VOIDmode means ptr_mode). */
61 machine_mode c_default_pointer_mode = VOIDmode;
63 /* The following symbols are subsumed in the c_global_trees array, and
64 listed here individually for documentation purposes.
66 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
68 tree short_integer_type_node;
69 tree long_integer_type_node;
70 tree long_long_integer_type_node;
72 tree short_unsigned_type_node;
73 tree long_unsigned_type_node;
74 tree long_long_unsigned_type_node;
76 tree truthvalue_type_node;
77 tree truthvalue_false_node;
78 tree truthvalue_true_node;
80 tree ptrdiff_type_node;
82 tree unsigned_char_type_node;
83 tree signed_char_type_node;
86 tree char16_type_node;
87 tree char32_type_node;
90 tree double_type_node;
91 tree long_double_type_node;
93 tree complex_integer_type_node;
94 tree complex_float_type_node;
95 tree complex_double_type_node;
96 tree complex_long_double_type_node;
98 tree dfloat32_type_node;
99 tree dfloat64_type_node;
100 tree_dfloat128_type_node;
102 tree intQI_type_node;
103 tree intHI_type_node;
104 tree intSI_type_node;
105 tree intDI_type_node;
106 tree intTI_type_node;
108 tree unsigned_intQI_type_node;
109 tree unsigned_intHI_type_node;
110 tree unsigned_intSI_type_node;
111 tree unsigned_intDI_type_node;
112 tree unsigned_intTI_type_node;
114 tree widest_integer_literal_type_node;
115 tree widest_unsigned_literal_type_node;
117 Nodes for types `void *' and `const void *'.
119 tree ptr_type_node, const_ptr_type_node;
121 Nodes for types `char *' and `const char *'.
123 tree string_type_node, const_string_type_node;
125 Type `char[SOMENUMBER]'.
126 Used when an array of char is needed and the size is irrelevant.
128 tree char_array_type_node;
130 Type `wchar_t[SOMENUMBER]' or something like it.
131 Used when a wide string literal is created.
133 tree wchar_array_type_node;
135 Type `char16_t[SOMENUMBER]' or something like it.
136 Used when a UTF-16 string literal is created.
138 tree char16_array_type_node;
140 Type `char32_t[SOMENUMBER]' or something like it.
141 Used when a UTF-32 string literal is created.
143 tree char32_array_type_node;
145 Type `int ()' -- used for implicit declaration of functions.
147 tree default_function_type;
149 A VOID_TYPE node, packaged in a TREE_LIST.
153 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
154 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
155 VAR_DECLS, but C++ does.)
157 tree function_name_decl_node;
158 tree pretty_function_name_decl_node;
159 tree c99_function_name_decl_node;
161 Stack of nested function name VAR_DECLs.
163 tree saved_function_name_decls;
167 tree c_global_trees[CTI_MAX];
169 /* Switches common to the C front ends. */
171 /* Nonzero means don't output line number information. */
173 char flag_no_line_commands;
175 /* Nonzero causes -E output not to be done, but directives such as
176 #define that have side effects are still obeyed. */
180 /* Nonzero means dump macros in some fashion. */
182 char flag_dump_macros;
184 /* Nonzero means pass #include lines through to the output. */
186 char flag_dump_includes;
188 /* Nonzero means process PCH files while preprocessing. */
190 bool flag_pch_preprocess;
192 /* The file name to which we should write a precompiled header, or
193 NULL if no header will be written in this compile. */
195 const char *pch_file;
197 /* Nonzero if an ISO standard was selected. It rejects macros in the
201 /* C/ObjC language option variables. */
204 /* Nonzero means allow type mismatches in conditional expressions;
205 just make their values `void'. */
207 int flag_cond_mismatch;
209 /* Nonzero means enable C89 Amendment 1 features. */
213 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
217 /* Nonzero means use the ISO C11 dialect of C. */
221 /* Nonzero means that we have builtin functions, and main is an int. */
226 /* ObjC language option variables. */
229 /* Tells the compiler that this is a special run. Do not perform any
230 compiling, instead we are to test some platform dependent features
231 and output a C header file with appropriate definitions. */
233 int print_struct_values;
235 /* Tells the compiler what is the constant string class for ObjC. */
237 const char *constant_string_class_name;
240 /* C++ language option variables. */
242 /* The reference version of the ABI for -Wabi. */
244 int warn_abi_version = -1;
246 /* Nonzero means generate separate instantiation control files and
247 juggle them at link time. */
249 int flag_use_repository;
251 /* The C++ dialect being used. Default set in c_common_post_options. */
253 enum cxx_dialect cxx_dialect = cxx_unset;
255 /* Maximum template instantiation depth. This limit exists to limit the
256 time it takes to notice excessively recursive template instantiations.
258 The default is lower than the 1024 recommended by the C++0x standard
259 because G++ runs out of stack before 1024 with highly recursive template
260 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
262 int max_tinst_depth = 900;
264 /* The elements of `ridpointers' are identifier nodes for the reserved
265 type names and storage classes. It is indexed by a RID_... value. */
268 tree (*make_fname_decl) (location_t, tree, int);
270 /* Nonzero means don't warn about problems that occur when the code is
272 int c_inhibit_evaluation_warnings;
274 /* Whether we are building a boolean conversion inside
275 convert_for_assignment, or some other late binary operation. If
276 build_binary_op is called for C (from code shared by C and C++) in
277 this case, then the operands have already been folded and the
278 result will not be folded again, so C_MAYBE_CONST_EXPR should not
280 bool in_late_binary_op;
282 /* Whether lexing has been completed, so subsequent preprocessor
283 errors should use the compiler's input_location. */
284 bool done_lexing = false;
286 /* Information about how a function name is generated. */
289 tree *const decl; /* pointer to the VAR_DECL. */
290 const unsigned rid; /* RID number for the identifier. */
291 const int pretty; /* How pretty is it? */
294 /* The three ways of getting then name of the current function. */
296 const struct fname_var_t fname_vars[] =
298 /* C99 compliant __func__, must be first. */
299 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
300 /* GCC __FUNCTION__ compliant. */
301 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
302 /* GCC __PRETTY_FUNCTION__ compliant. */
303 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
307 /* Global visibility options. */
308 struct visibility_flags visibility_options;
310 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
311 static tree check_case_value (location_t, tree);
312 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
315 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
316 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
317 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
323 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
325 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
327 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_always_inline_attribute (tree *, tree, tree, int,
334 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
340 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
342 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
344 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_transparent_union_attribute (tree *, tree, tree,
347 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
348 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
353 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
354 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
355 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
358 static tree handle_visibility_attribute (tree *, tree, tree, int,
360 static tree handle_tls_model_attribute (tree *, tree, tree, int,
362 static tree handle_no_instrument_function_attribute (tree *, tree,
364 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
368 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_deprecated_attribute (tree *, tree, tree, int,
374 static tree handle_vector_size_attribute (tree *, tree, tree, int,
376 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
381 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
388 static tree ignore_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
392 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
395 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
397 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
398 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
399 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
400 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
402 static void check_function_nonnull (tree, int, tree *);
403 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
404 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
405 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
406 static int resort_field_decl_cmp (const void *, const void *);
408 /* Reserved words. The third field is a mask: keywords are disabled
409 if they match the mask.
412 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
413 C --std=c99: D_CXXONLY | D_OBJC
414 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
415 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
416 C++ --std=c0x: D_CONLY | D_OBJC
417 ObjC++ is like C++ except that D_OBJC is not set
419 If -fno-asm is used, D_ASM is added to the mask. If
420 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
421 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
422 In C with -Wc++-compat, we warn if D_CXXWARN is set.
424 Note the complication of the D_CXX_OBJC keywords. These are
425 reserved words such as 'class'. In C++, 'class' is a reserved
426 word. In Objective-C++ it is too. In Objective-C, it is a
427 reserved word too, but only if it follows an '@' sign.
429 const struct c_common_resword c_common_reswords[] =
431 { "_Alignas", RID_ALIGNAS, D_CONLY },
432 { "_Alignof", RID_ALIGNOF, D_CONLY },
433 { "_Atomic", RID_ATOMIC, D_CONLY },
434 { "_Bool", RID_BOOL, D_CONLY },
435 { "_Complex", RID_COMPLEX, 0 },
436 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
437 { "_Cilk_sync", RID_CILK_SYNC, 0 },
438 { "_Cilk_for", RID_CILK_FOR, 0 },
439 { "_Imaginary", RID_IMAGINARY, D_CONLY },
440 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
441 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
442 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
443 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
444 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
445 { "_Sat", RID_SAT, D_CONLY | D_EXT },
446 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
447 { "_Noreturn", RID_NORETURN, D_CONLY },
448 { "_Generic", RID_GENERIC, D_CONLY },
449 { "_Thread_local", RID_THREAD, D_CONLY },
450 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
451 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
452 { "__alignof", RID_ALIGNOF, 0 },
453 { "__alignof__", RID_ALIGNOF, 0 },
454 { "__asm", RID_ASM, 0 },
455 { "__asm__", RID_ASM, 0 },
456 { "__attribute", RID_ATTRIBUTE, 0 },
457 { "__attribute__", RID_ATTRIBUTE, 0 },
458 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
459 { "__bases", RID_BASES, D_CXXONLY },
460 { "__builtin_call_with_static_chain",
461 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
462 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
463 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
464 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
465 { "__builtin_offsetof", RID_OFFSETOF, 0 },
466 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
467 { "__builtin_va_arg", RID_VA_ARG, 0 },
468 { "__complex", RID_COMPLEX, 0 },
469 { "__complex__", RID_COMPLEX, 0 },
470 { "__const", RID_CONST, 0 },
471 { "__const__", RID_CONST, 0 },
472 { "__decltype", RID_DECLTYPE, D_CXXONLY },
473 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
474 { "__extension__", RID_EXTENSION, 0 },
475 { "__func__", RID_C99_FUNCTION_NAME, 0 },
476 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
477 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
478 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
479 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
480 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
481 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
482 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
483 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
484 { "__imag", RID_IMAGPART, 0 },
485 { "__imag__", RID_IMAGPART, 0 },
486 { "__inline", RID_INLINE, 0 },
487 { "__inline__", RID_INLINE, 0 },
488 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
489 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
490 { "__is_class", RID_IS_CLASS, D_CXXONLY },
491 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
492 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
493 { "__is_final", RID_IS_FINAL, D_CXXONLY },
494 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
495 { "__is_pod", RID_IS_POD, D_CXXONLY },
496 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
497 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
498 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
499 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
500 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
501 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
502 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
503 { "__is_union", RID_IS_UNION, D_CXXONLY },
504 { "__label__", RID_LABEL, 0 },
505 { "__null", RID_NULL, 0 },
506 { "__real", RID_REALPART, 0 },
507 { "__real__", RID_REALPART, 0 },
508 { "__restrict", RID_RESTRICT, 0 },
509 { "__restrict__", RID_RESTRICT, 0 },
510 { "__signed", RID_SIGNED, 0 },
511 { "__signed__", RID_SIGNED, 0 },
512 { "__thread", RID_THREAD, 0 },
513 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
514 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
515 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
516 { "__typeof", RID_TYPEOF, 0 },
517 { "__typeof__", RID_TYPEOF, 0 },
518 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
519 { "__volatile", RID_VOLATILE, 0 },
520 { "__volatile__", RID_VOLATILE, 0 },
521 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
522 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
523 { "asm", RID_ASM, D_ASM },
524 { "auto", RID_AUTO, 0 },
525 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
526 { "break", RID_BREAK, 0 },
527 { "case", RID_CASE, 0 },
528 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
529 { "char", RID_CHAR, 0 },
530 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
531 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
532 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
533 { "const", RID_CONST, 0 },
534 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
535 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
536 { "continue", RID_CONTINUE, 0 },
537 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
538 { "default", RID_DEFAULT, 0 },
539 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
541 { "double", RID_DOUBLE, 0 },
542 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
543 { "else", RID_ELSE, 0 },
544 { "enum", RID_ENUM, 0 },
545 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
546 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
547 { "extern", RID_EXTERN, 0 },
548 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
549 { "float", RID_FLOAT, 0 },
550 { "for", RID_FOR, 0 },
551 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
552 { "goto", RID_GOTO, 0 },
554 { "inline", RID_INLINE, D_EXT89 },
555 { "int", RID_INT, 0 },
556 { "long", RID_LONG, 0 },
557 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
558 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
559 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
560 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
561 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
562 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
563 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
564 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
565 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
566 { "register", RID_REGISTER, 0 },
567 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
568 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
569 { "return", RID_RETURN, 0 },
570 { "short", RID_SHORT, 0 },
571 { "signed", RID_SIGNED, 0 },
572 { "sizeof", RID_SIZEOF, 0 },
573 { "static", RID_STATIC, 0 },
574 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
575 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
576 { "struct", RID_STRUCT, 0 },
577 { "switch", RID_SWITCH, 0 },
578 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
579 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
580 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
581 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
582 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
583 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
584 { "typedef", RID_TYPEDEF, 0 },
585 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
586 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
587 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
588 { "union", RID_UNION, 0 },
589 { "unsigned", RID_UNSIGNED, 0 },
590 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
591 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
592 { "void", RID_VOID, 0 },
593 { "volatile", RID_VOLATILE, 0 },
594 { "wchar_t", RID_WCHAR, D_CXXONLY },
595 { "while", RID_WHILE, 0 },
597 /* Concepts-related keywords */
598 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
599 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
601 /* These Objective-C keywords are recognized only immediately after
603 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
604 { "defs", RID_AT_DEFS, D_OBJC },
605 { "encode", RID_AT_ENCODE, D_OBJC },
606 { "end", RID_AT_END, D_OBJC },
607 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
608 { "interface", RID_AT_INTERFACE, D_OBJC },
609 { "protocol", RID_AT_PROTOCOL, D_OBJC },
610 { "selector", RID_AT_SELECTOR, D_OBJC },
611 { "finally", RID_AT_FINALLY, D_OBJC },
612 { "synchronized", RID_AT_SYNCHRONIZED, 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 { "transparent_union", 0, 0, false, false, false,
696 handle_transparent_union_attribute, false },
697 { "constructor", 0, 1, true, false, false,
698 handle_constructor_attribute, false },
699 { "destructor", 0, 1, true, false, false,
700 handle_destructor_attribute, false },
701 { "mode", 1, 1, false, true, false,
702 handle_mode_attribute, false },
703 { "section", 1, 1, true, false, false,
704 handle_section_attribute, false },
705 { "aligned", 0, 1, false, false, false,
706 handle_aligned_attribute, false },
707 { "weak", 0, 0, true, false, false,
708 handle_weak_attribute, false },
709 { "noplt", 0, 0, true, false, false,
710 handle_noplt_attribute, false },
711 { "ifunc", 1, 1, true, false, false,
712 handle_ifunc_attribute, false },
713 { "alias", 1, 1, true, false, false,
714 handle_alias_attribute, false },
715 { "weakref", 0, 1, true, false, false,
716 handle_weakref_attribute, false },
717 { "no_instrument_function", 0, 0, true, false, false,
718 handle_no_instrument_function_attribute,
720 { "malloc", 0, 0, true, false, false,
721 handle_malloc_attribute, false },
722 { "returns_twice", 0, 0, true, false, false,
723 handle_returns_twice_attribute, false },
724 { "no_stack_limit", 0, 0, true, false, false,
725 handle_no_limit_stack_attribute, false },
726 { "pure", 0, 0, true, false, false,
727 handle_pure_attribute, false },
728 { "transaction_callable", 0, 0, false, true, false,
729 handle_tm_attribute, false },
730 { "transaction_unsafe", 0, 0, false, true, false,
731 handle_tm_attribute, false },
732 { "transaction_safe", 0, 0, false, true, false,
733 handle_tm_attribute, false },
734 { "transaction_may_cancel_outer", 0, 0, false, true, false,
735 handle_tm_attribute, false },
736 /* ??? These two attributes didn't make the transition from the
737 Intel language document to the multi-vendor language document. */
738 { "transaction_pure", 0, 0, false, true, false,
739 handle_tm_attribute, false },
740 { "transaction_wrap", 1, 1, true, false, false,
741 handle_tm_wrap_attribute, false },
742 /* For internal use (marking of builtins) only. The name contains space
743 to prevent its usage in source code. */
744 { "no vops", 0, 0, true, false, false,
745 handle_novops_attribute, false },
746 { "deprecated", 0, 1, false, false, false,
747 handle_deprecated_attribute, false },
748 { "vector_size", 1, 1, false, true, false,
749 handle_vector_size_attribute, false },
750 { "visibility", 1, 1, false, false, false,
751 handle_visibility_attribute, false },
752 { "tls_model", 1, 1, true, false, false,
753 handle_tls_model_attribute, false },
754 { "nonnull", 0, -1, false, true, true,
755 handle_nonnull_attribute, false },
756 { "nothrow", 0, 0, true, false, false,
757 handle_nothrow_attribute, false },
758 { "may_alias", 0, 0, false, true, false, NULL, false },
759 { "cleanup", 1, 1, true, false, false,
760 handle_cleanup_attribute, false },
761 { "warn_unused_result", 0, 0, false, true, true,
762 handle_warn_unused_result_attribute, false },
763 { "sentinel", 0, 1, false, true, true,
764 handle_sentinel_attribute, false },
765 /* For internal use (marking of builtins) only. The name contains space
766 to prevent its usage in source code. */
767 { "type generic", 0, 0, false, true, true,
768 handle_type_generic_attribute, false },
769 { "alloc_size", 1, 2, false, true, true,
770 handle_alloc_size_attribute, false },
771 { "cold", 0, 0, true, false, false,
772 handle_cold_attribute, false },
773 { "hot", 0, 0, true, false, false,
774 handle_hot_attribute, false },
775 { "no_address_safety_analysis",
776 0, 0, true, false, false,
777 handle_no_address_safety_analysis_attribute,
779 { "no_sanitize_address", 0, 0, true, false, false,
780 handle_no_sanitize_address_attribute,
782 { "no_sanitize_thread", 0, 0, true, false, false,
783 handle_no_sanitize_address_attribute,
785 { "no_sanitize_undefined", 0, 0, true, false, false,
786 handle_no_sanitize_undefined_attribute,
788 { "warning", 1, 1, true, false, false,
789 handle_error_attribute, false },
790 { "error", 1, 1, true, false, false,
791 handle_error_attribute, false },
792 { "target", 1, -1, true, false, false,
793 handle_target_attribute, false },
794 { "optimize", 1, -1, true, false, false,
795 handle_optimize_attribute, false },
796 /* For internal use only. The leading '*' both prevents its usage in
797 source code and signals that it may be overridden by machine tables. */
798 { "*tm regparm", 0, 0, false, true, true,
799 ignore_attribute, false },
800 { "no_split_stack", 0, 0, true, false, false,
801 handle_no_split_stack_attribute, false },
802 /* For internal use (marking of builtins and runtime functions) only.
803 The name contains space to prevent its usage in source code. */
804 { "fn spec", 1, 1, false, true, true,
805 handle_fnspec_attribute, false },
806 { "warn_unused", 0, 0, false, false, false,
807 handle_warn_unused_attribute, false },
808 { "returns_nonnull", 0, 0, false, true, true,
809 handle_returns_nonnull_attribute, false },
810 { "omp declare simd", 0, -1, true, false, false,
811 handle_omp_declare_simd_attribute, false },
812 { "cilk simd function", 0, -1, true, false, false,
813 handle_omp_declare_simd_attribute, false },
814 { "omp declare target", 0, 0, true, false, false,
815 handle_omp_declare_target_attribute, false },
816 { "alloc_align", 1, 1, false, true, true,
817 handle_alloc_align_attribute, false },
818 { "assume_aligned", 1, 2, false, true, true,
819 handle_assume_aligned_attribute, false },
820 { "designated_init", 0, 0, false, true, false,
821 handle_designated_init_attribute, false },
822 { "bnd_variable_size", 0, 0, true, false, false,
823 handle_bnd_variable_size_attribute, false },
824 { "bnd_legacy", 0, 0, true, false, false,
825 handle_bnd_legacy, false },
826 { "bnd_instrument", 0, 0, true, false, false,
827 handle_bnd_instrument, false },
828 { NULL, 0, 0, false, false, false, NULL, false }
831 /* Give the specifications for the format attributes, used by C and all
834 All attributes referencing arguments should be additionally processed
835 in chkp_copy_function_type_adding_bounds for correct instrumentation
836 by Pointer Bounds Checker.
837 Current list of processed format attributes: format, format_arg. */
838 const struct attribute_spec c_common_format_attribute_table[] =
840 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
841 affects_type_identity } */
842 { "format", 3, 3, false, true, true,
843 handle_format_attribute, false },
844 { "format_arg", 1, 1, false, true, true,
845 handle_format_arg_attribute, false },
846 { NULL, 0, 0, false, false, false, NULL, false }
849 /* Return identifier for address space AS. */
852 c_addr_space_name (addr_space_t as)
854 int rid = RID_FIRST_ADDR_SPACE + as;
855 gcc_assert (ridpointers [rid]);
856 return IDENTIFIER_POINTER (ridpointers [rid]);
859 /* Push current bindings for the function name VAR_DECLS. */
862 start_fname_decls (void)
865 tree saved = NULL_TREE;
867 for (ix = 0; fname_vars[ix].decl; ix++)
869 tree decl = *fname_vars[ix].decl;
873 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
875 *fname_vars[ix].decl = NULL_TREE;
878 if (saved || saved_function_name_decls)
879 /* Normally they'll have been NULL, so only push if we've got a
880 stack, or they are non-NULL. */
881 saved_function_name_decls = tree_cons (saved, NULL_TREE,
882 saved_function_name_decls);
885 /* Finish up the current bindings, adding them into the current function's
886 statement tree. This must be done _before_ finish_stmt_tree is called.
887 If there is no current function, we must be at file scope and no statements
888 are involved. Pop the previous bindings. */
891 finish_fname_decls (void)
894 tree stmts = NULL_TREE;
895 tree stack = saved_function_name_decls;
897 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
898 append_to_statement_list (TREE_VALUE (stack), &stmts);
902 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
904 if (TREE_CODE (*bodyp) == BIND_EXPR)
905 bodyp = &BIND_EXPR_BODY (*bodyp);
907 append_to_statement_list_force (*bodyp, &stmts);
911 for (ix = 0; fname_vars[ix].decl; ix++)
912 *fname_vars[ix].decl = NULL_TREE;
916 /* We had saved values, restore them. */
919 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
921 tree decl = TREE_PURPOSE (saved);
922 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
924 *fname_vars[ix].decl = decl;
926 stack = TREE_CHAIN (stack);
928 saved_function_name_decls = stack;
931 /* Return the text name of the current function, suitably prettified
932 by PRETTY_P. Return string must be freed by caller. */
935 fname_as_string (int pretty_p)
937 const char *name = "top level";
940 cpp_string cstr = { 0, 0 }, strname;
948 if (current_function_decl)
949 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
951 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
953 namep = XNEWVEC (char, len);
954 snprintf (namep, len, "\"%s\"", name);
955 strname.text = (unsigned char *) namep;
956 strname.len = len - 1;
958 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
961 return (const char *) cstr.text;
967 /* Return the VAR_DECL for a const char array naming the current
968 function. If the VAR_DECL has not yet been created, create it
969 now. RID indicates how it should be formatted and IDENTIFIER_NODE
970 ID is its name (unfortunately C and C++ hold the RID values of
971 keywords in different places, so we can't derive RID from ID in
972 this language independent code. LOC is the location of the
976 fname_decl (location_t loc, unsigned int rid, tree id)
979 tree decl = NULL_TREE;
981 for (ix = 0; fname_vars[ix].decl; ix++)
982 if (fname_vars[ix].rid == rid)
985 decl = *fname_vars[ix].decl;
988 /* If a tree is built here, it would normally have the lineno of
989 the current statement. Later this tree will be moved to the
990 beginning of the function and this line number will be wrong.
991 To avoid this problem set the lineno to 0 here; that prevents
992 it from appearing in the RTL. */
994 location_t saved_location = input_location;
995 input_location = UNKNOWN_LOCATION;
997 stmts = push_stmt_list ();
998 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
999 stmts = pop_stmt_list (stmts);
1000 if (!IS_EMPTY_STMT (stmts))
1001 saved_function_name_decls
1002 = tree_cons (decl, stmts, saved_function_name_decls);
1003 *fname_vars[ix].decl = decl;
1004 input_location = saved_location;
1006 if (!ix && !current_function_decl)
1007 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1012 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1015 fix_string_type (tree value)
1017 int length = TREE_STRING_LENGTH (value);
1019 tree e_type, i_type, a_type;
1021 /* Compute the number of elements, for the array type. */
1022 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1025 e_type = char_type_node;
1027 else if (TREE_TYPE (value) == char16_array_type_node)
1029 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1030 e_type = char16_type_node;
1032 else if (TREE_TYPE (value) == char32_array_type_node)
1034 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1035 e_type = char32_type_node;
1039 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1040 e_type = wchar_type_node;
1043 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1044 limit in C++98 Annex B is very large (65536) and is not normative,
1045 so we do not diagnose it (warn_overlength_strings is forced off
1046 in c_common_post_options). */
1047 if (warn_overlength_strings)
1049 const int nchars_max = flag_isoc99 ? 4095 : 509;
1050 const int relevant_std = flag_isoc99 ? 99 : 90;
1051 if (nchars - 1 > nchars_max)
1052 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1053 separate the %d from the 'C'. 'ISO' should not be
1054 translated, but it may be moved after 'C%d' in languages
1055 where modifiers follow nouns. */
1056 pedwarn (input_location, OPT_Woverlength_strings,
1057 "string length %qd is greater than the length %qd "
1058 "ISO C%d compilers are required to support",
1059 nchars - 1, nchars_max, relevant_std);
1062 /* Create the array type for the string constant. The ISO C++
1063 standard says that a string literal has type `const char[N]' or
1064 `const wchar_t[N]'. We use the same logic when invoked as a C
1065 front-end with -Wwrite-strings.
1066 ??? We should change the type of an expression depending on the
1067 state of a warning flag. We should just be warning -- see how
1068 this is handled in the C++ front-end for the deprecated implicit
1069 conversion from string literals to `char*' or `wchar_t*'.
1071 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1072 array type being the unqualified version of that type.
1073 Therefore, if we are constructing an array of const char, we must
1074 construct the matching unqualified array type first. The C front
1075 end does not require this, but it does no harm, so we do it
1077 i_type = build_index_type (size_int (nchars - 1));
1078 a_type = build_array_type (e_type, i_type);
1079 if (c_dialect_cxx() || warn_write_strings)
1080 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1082 TREE_TYPE (value) = a_type;
1083 TREE_CONSTANT (value) = 1;
1084 TREE_READONLY (value) = 1;
1085 TREE_STATIC (value) = 1;
1089 /* If DISABLE is true, stop issuing warnings. This is used when
1090 parsing code that we know will not be executed. This function may
1091 be called multiple times, and works as a stack. */
1094 c_disable_warnings (bool disable)
1098 ++c_inhibit_evaluation_warnings;
1099 fold_defer_overflow_warnings ();
1103 /* If ENABLE is true, reenable issuing warnings. */
1106 c_enable_warnings (bool enable)
1110 --c_inhibit_evaluation_warnings;
1111 fold_undefer_and_ignore_overflow_warnings ();
1115 /* Fully fold EXPR, an expression that was not folded (beyond integer
1116 constant expressions and null pointer constants) when being built
1117 up. If IN_INIT, this is in a static initializer and certain
1118 changes are made to the folding done. Clear *MAYBE_CONST if
1119 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1120 expression because it contains an evaluated operator (in C99) or an
1121 operator outside of sizeof returning an integer constant (in C90)
1122 not permitted in constant expressions, or because it contains an
1123 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1124 set to true by callers before calling this function.) Return the
1125 folded expression. Function arguments have already been folded
1126 before calling this function, as have the contents of SAVE_EXPR,
1127 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1128 C_MAYBE_CONST_EXPR. */
1131 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1134 tree eptype = NULL_TREE;
1136 bool maybe_const_itself = true;
1137 location_t loc = EXPR_LOCATION (expr);
1139 /* This function is not relevant to C++ because C++ folds while
1140 parsing, and may need changes to be correct for C++ when C++
1141 stops folding while parsing. */
1142 if (c_dialect_cxx ())
1146 maybe_const = &dummy;
1147 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1149 eptype = TREE_TYPE (expr);
1150 expr = TREE_OPERAND (expr, 0);
1152 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1153 &maybe_const_itself, false);
1155 ret = fold_convert_loc (loc, eptype, ret);
1156 *maybe_const &= maybe_const_itself;
1160 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1161 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1162 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1163 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1164 both evaluated and unevaluated subexpressions while
1165 *MAYBE_CONST_ITSELF is carried from only evaluated
1166 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1167 with integer constant operands, and if any of the operands doesn't
1168 get folded to an integer constant, don't fold the expression itself. */
1171 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1172 bool *maybe_const_itself, bool for_int_const)
1175 enum tree_code code = TREE_CODE (expr);
1176 enum tree_code_class kind = TREE_CODE_CLASS (code);
1177 location_t loc = EXPR_LOCATION (expr);
1178 tree op0, op1, op2, op3;
1179 tree orig_op0, orig_op1, orig_op2;
1180 bool op0_const = true, op1_const = true, op2_const = true;
1181 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1182 bool nowarning = TREE_NO_WARNING (expr);
1185 /* This function is not relevant to C++ because C++ folds while
1186 parsing, and may need changes to be correct for C++ when C++
1187 stops folding while parsing. */
1188 if (c_dialect_cxx ())
1191 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1192 anything else not counted as an expression cannot usefully be
1193 folded further at this point. */
1194 if (!IS_EXPR_CODE_CLASS (kind)
1195 || kind == tcc_statement
1196 || code == SAVE_EXPR)
1199 /* Operands of variable-length expressions (function calls) have
1200 already been folded, as have __builtin_* function calls, and such
1201 expressions cannot occur in constant expressions. */
1202 if (kind == tcc_vl_exp)
1204 *maybe_const_operands = false;
1209 if (code == C_MAYBE_CONST_EXPR)
1211 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1212 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1213 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1214 *maybe_const_operands = false;
1215 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1217 *maybe_const_itself = false;
1218 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1219 maybe_const_itself, true);
1221 if (pre && !in_init)
1222 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1228 /* Assignment, increment, decrement, function call and comma
1229 operators, and statement expressions, cannot occur in constant
1230 expressions if evaluated / outside of sizeof. (Function calls
1231 were handled above, though VA_ARG_EXPR is treated like a function
1232 call here, and statement expressions are handled through
1233 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1237 case PREDECREMENT_EXPR:
1238 case PREINCREMENT_EXPR:
1239 case POSTDECREMENT_EXPR:
1240 case POSTINCREMENT_EXPR:
1242 *maybe_const_operands = false;
1249 *maybe_const_operands = false;
1257 /* Fold individual tree codes as appropriate. */
1260 case COMPOUND_LITERAL_EXPR:
1261 /* Any non-constancy will have been marked in a containing
1262 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1266 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1267 op1 = TREE_OPERAND (expr, 1);
1268 op2 = TREE_OPERAND (expr, 2);
1269 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1270 maybe_const_itself, for_int_const);
1271 STRIP_TYPE_NOPS (op0);
1272 if (op0 != orig_op0)
1273 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1276 TREE_READONLY (ret) = TREE_READONLY (expr);
1277 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1282 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1283 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1284 op2 = TREE_OPERAND (expr, 2);
1285 op3 = TREE_OPERAND (expr, 3);
1286 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1287 maybe_const_itself, for_int_const);
1288 STRIP_TYPE_NOPS (op0);
1289 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1290 maybe_const_itself, for_int_const);
1291 STRIP_TYPE_NOPS (op1);
1292 op1 = decl_constant_value_for_optimization (op1);
1293 if (op0 != orig_op0 || op1 != orig_op1)
1294 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1297 TREE_READONLY (ret) = TREE_READONLY (expr);
1298 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1299 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1306 case PREDECREMENT_EXPR:
1307 case PREINCREMENT_EXPR:
1308 case POSTDECREMENT_EXPR:
1309 case POSTINCREMENT_EXPR:
1313 case POINTER_PLUS_EXPR:
1314 case TRUNC_DIV_EXPR:
1316 case FLOOR_DIV_EXPR:
1317 case TRUNC_MOD_EXPR:
1319 case EXACT_DIV_EXPR:
1332 case TRUTH_AND_EXPR:
1334 case TRUTH_XOR_EXPR:
1335 case UNORDERED_EXPR:
1342 /* Binary operations evaluating both arguments (increment and
1343 decrement are binary internally in GCC). */
1344 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1345 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1346 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1347 maybe_const_itself, for_int_const);
1348 STRIP_TYPE_NOPS (op0);
1349 if (code != MODIFY_EXPR
1350 && code != PREDECREMENT_EXPR
1351 && code != PREINCREMENT_EXPR
1352 && code != POSTDECREMENT_EXPR
1353 && code != POSTINCREMENT_EXPR)
1354 op0 = decl_constant_value_for_optimization (op0);
1355 /* The RHS of a MODIFY_EXPR was fully folded when building that
1356 expression for the sake of conversion warnings. */
1357 if (code != MODIFY_EXPR)
1358 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1359 maybe_const_itself, for_int_const);
1360 STRIP_TYPE_NOPS (op1);
1361 op1 = decl_constant_value_for_optimization (op1);
1363 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1364 || TREE_CODE (op1) != INTEGER_CST))
1367 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1369 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1370 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1373 if (TREE_OVERFLOW_P (ret)
1374 && !TREE_OVERFLOW_P (op0)
1375 && !TREE_OVERFLOW_P (op1))
1376 overflow_warning (EXPR_LOC_OR_LOC (expr, input_location), ret);
1377 if (code == LSHIFT_EXPR
1378 && TREE_CODE (orig_op0) != INTEGER_CST
1379 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1380 && TREE_CODE (op0) == INTEGER_CST
1381 && c_inhibit_evaluation_warnings == 0
1382 && tree_int_cst_sgn (op0) < 0)
1383 warning_at (loc, OPT_Wshift_negative_value,
1384 "left shift of negative value");
1385 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1386 && TREE_CODE (orig_op1) != INTEGER_CST
1387 && TREE_CODE (op1) == INTEGER_CST
1388 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1389 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1390 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1391 && c_inhibit_evaluation_warnings == 0)
1393 if (tree_int_cst_sgn (op1) < 0)
1394 warning_at (loc, OPT_Wshift_count_negative,
1395 (code == LSHIFT_EXPR
1396 ? G_("left shift count is negative")
1397 : G_("right shift count is negative")));
1398 else if (compare_tree_int (op1,
1399 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1401 warning_at (loc, OPT_Wshift_count_overflow,
1402 (code == LSHIFT_EXPR
1403 ? G_("left shift count >= width of type")
1404 : G_("right shift count >= width of type")));
1406 if (code == LSHIFT_EXPR
1407 /* If either OP0 has been folded to INTEGER_CST... */
1408 && ((TREE_CODE (orig_op0) != INTEGER_CST
1409 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1410 && TREE_CODE (op0) == INTEGER_CST)
1411 /* ...or if OP1 has been folded to INTEGER_CST... */
1412 || (TREE_CODE (orig_op1) != INTEGER_CST
1413 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1414 && TREE_CODE (op1) == INTEGER_CST))
1415 && c_inhibit_evaluation_warnings == 0)
1416 /* ...then maybe we can detect an overflow. */
1417 maybe_warn_shift_overflow (loc, op0, op1);
1418 if ((code == TRUNC_DIV_EXPR
1419 || code == CEIL_DIV_EXPR
1420 || code == FLOOR_DIV_EXPR
1421 || code == EXACT_DIV_EXPR
1422 || code == TRUNC_MOD_EXPR)
1423 && TREE_CODE (orig_op1) != INTEGER_CST
1424 && TREE_CODE (op1) == INTEGER_CST
1425 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1426 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1427 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1428 warn_for_div_by_zero (loc, op1);
1432 case FIX_TRUNC_EXPR:
1435 case ADDR_SPACE_CONVERT_EXPR:
1436 case VIEW_CONVERT_EXPR:
1437 case NON_LVALUE_EXPR:
1440 case TRUTH_NOT_EXPR:
1445 /* Unary operations. */
1446 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1447 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1448 maybe_const_itself, for_int_const);
1449 STRIP_TYPE_NOPS (op0);
1450 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1451 op0 = decl_constant_value_for_optimization (op0);
1453 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1456 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1457 not prepared to deal with them if they occur in initializers. */
1459 && code == ADDR_EXPR
1460 && (op1 = get_base_address (op0)) != NULL_TREE
1461 && INDIRECT_REF_P (op1)
1462 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1463 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1464 else if (op0 != orig_op0 || in_init)
1466 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1467 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1470 if (code == INDIRECT_REF
1472 && INDIRECT_REF_P (ret))
1474 TREE_READONLY (ret) = TREE_READONLY (expr);
1475 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1476 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1480 case FIX_TRUNC_EXPR:
1483 /* Don't warn about explicit conversions. We will already
1484 have warned about suspect implicit conversions. */
1488 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1489 overflow_warning (EXPR_LOCATION (expr), ret);
1494 case TRUTH_ANDIF_EXPR:
1495 case TRUTH_ORIF_EXPR:
1496 /* Binary operations not necessarily evaluating both
1498 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1499 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1500 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1502 STRIP_TYPE_NOPS (op0);
1504 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1505 ? truthvalue_false_node
1506 : truthvalue_true_node));
1507 c_disable_warnings (unused_p);
1508 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1510 STRIP_TYPE_NOPS (op1);
1511 c_enable_warnings (unused_p);
1514 && (TREE_CODE (op0) != INTEGER_CST
1515 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1516 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1519 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1521 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1522 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1525 *maybe_const_operands &= op0_const;
1526 *maybe_const_itself &= op0_const_self;
1530 && (code == TRUTH_ANDIF_EXPR
1531 ? op0 == truthvalue_false_node
1532 : op0 == truthvalue_true_node)))
1533 *maybe_const_operands &= op1_const;
1536 && (code == TRUTH_ANDIF_EXPR
1537 ? op0 == truthvalue_false_node
1538 : op0 == truthvalue_true_node)))
1539 *maybe_const_itself &= op1_const_self;
1543 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1544 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1545 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1546 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1549 STRIP_TYPE_NOPS (op0);
1550 c_disable_warnings (op0 == truthvalue_false_node);
1551 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1553 STRIP_TYPE_NOPS (op1);
1554 c_enable_warnings (op0 == truthvalue_false_node);
1556 c_disable_warnings (op0 == truthvalue_true_node);
1557 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1559 STRIP_TYPE_NOPS (op2);
1560 c_enable_warnings (op0 == truthvalue_true_node);
1563 && (TREE_CODE (op0) != INTEGER_CST
1564 /* Only the evaluated operand must be an INTEGER_CST. */
1565 || (op0 == truthvalue_true_node
1566 ? TREE_CODE (op1) != INTEGER_CST
1567 : TREE_CODE (op2) != INTEGER_CST)))
1570 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1571 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1574 *maybe_const_operands &= op0_const;
1575 *maybe_const_itself &= op0_const_self;
1579 && op0 == truthvalue_false_node))
1580 *maybe_const_operands &= op1_const;
1583 && op0 == truthvalue_false_node))
1584 *maybe_const_itself &= op1_const_self;
1588 && op0 == truthvalue_true_node))
1589 *maybe_const_operands &= op2_const;
1592 && op0 == truthvalue_true_node))
1593 *maybe_const_itself &= op2_const_self;
1596 case EXCESS_PRECISION_EXPR:
1597 /* Each case where an operand with excess precision may be
1598 encountered must remove the EXCESS_PRECISION_EXPR around
1599 inner operands and possibly put one around the whole
1600 expression or possibly convert to the semantic type (which
1601 c_fully_fold does); we cannot tell at this stage which is
1602 appropriate in any particular case. */
1606 /* Various codes may appear through folding built-in functions
1607 and their arguments. */
1612 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1613 have been done by this point, so remove them again. */
1614 nowarning |= TREE_NO_WARNING (ret);
1615 STRIP_TYPE_NOPS (ret);
1616 if (nowarning && !TREE_NO_WARNING (ret))
1618 if (!CAN_HAVE_LOCATION_P (ret))
1619 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1620 TREE_NO_WARNING (ret) = 1;
1623 protected_set_expr_location (ret, loc);
1627 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1628 return EXP. Otherwise, return either EXP or its known constant
1629 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1630 Is the BLKmode test appropriate? */
1633 decl_constant_value_for_optimization (tree exp)
1637 /* This function is only used by C, for c_fully_fold and other
1638 optimization, and may not be correct for C++. */
1639 if (c_dialect_cxx ())
1644 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1645 || DECL_MODE (exp) == BLKmode)
1648 ret = decl_constant_value (exp);
1649 /* Avoid unwanted tree sharing between the initializer and current
1650 function's body where the tree can be modified e.g. by the
1652 if (ret != exp && TREE_STATIC (exp))
1653 ret = unshare_expr (ret);
1657 /* Print a warning if a constant expression had overflow in folding.
1658 Invoke this function on every expression that the language
1659 requires to be a constant expression.
1660 Note the ANSI C standard says it is erroneous for a
1661 constant expression to overflow. */
1664 constant_expression_warning (tree value)
1666 if (warn_overflow && pedantic
1667 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1668 || TREE_CODE (value) == FIXED_CST
1669 || TREE_CODE (value) == VECTOR_CST
1670 || TREE_CODE (value) == COMPLEX_CST)
1671 && TREE_OVERFLOW (value))
1672 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1675 /* The same as above but print an unconditional error. */
1677 constant_expression_error (tree value)
1679 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1680 || TREE_CODE (value) == FIXED_CST
1681 || TREE_CODE (value) == VECTOR_CST
1682 || TREE_CODE (value) == COMPLEX_CST)
1683 && TREE_OVERFLOW (value))
1684 error ("overflow in constant expression");
1687 /* Print a warning if an expression had overflow in folding and its
1690 Invoke this function on every expression that
1691 (1) appears in the source code, and
1692 (2) is a constant expression that overflowed, and
1693 (3) is not already checked by convert_and_check;
1694 however, do not invoke this function on operands of explicit casts
1695 or when the expression is the result of an operator and any operand
1696 already overflowed. */
1699 overflow_warning (location_t loc, tree value)
1701 if (c_inhibit_evaluation_warnings != 0)
1704 switch (TREE_CODE (value))
1707 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1711 warning_at (loc, OPT_Woverflow,
1712 "floating point overflow in expression");
1716 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1720 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1724 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1725 warning_at (loc, OPT_Woverflow,
1726 "complex integer overflow in expression");
1727 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1728 warning_at (loc, OPT_Woverflow,
1729 "complex floating point overflow in expression");
1737 /* Warn about uses of logical || / && operator in a context where it
1738 is likely that the bitwise equivalent was intended by the
1739 programmer. We have seen an expression in which CODE is a binary
1740 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1741 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1743 warn_logical_operator (location_t location, enum tree_code code, tree type,
1744 enum tree_code code_left, tree op_left,
1745 enum tree_code ARG_UNUSED (code_right), tree op_right)
1747 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1748 int in0_p, in1_p, in_p;
1749 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1750 bool strict_overflow_p = false;
1752 if (code != TRUTH_ANDIF_EXPR
1753 && code != TRUTH_AND_EXPR
1754 && code != TRUTH_ORIF_EXPR
1755 && code != TRUTH_OR_EXPR)
1758 /* We don't want to warn if either operand comes from a macro
1759 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1761 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1762 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1765 /* Warn if &&/|| are being used in a context where it is
1766 likely that the bitwise equivalent was intended by the
1767 programmer. That is, an expression such as op && MASK
1768 where op should not be any boolean expression, nor a
1769 constant, and mask seems to be a non-boolean integer constant. */
1770 if (!truth_value_p (code_left)
1771 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1772 && !CONSTANT_CLASS_P (op_left)
1773 && !TREE_NO_WARNING (op_left)
1774 && TREE_CODE (op_right) == INTEGER_CST
1775 && !integer_zerop (op_right)
1776 && !integer_onep (op_right))
1779 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1780 " applied to non-boolean constant");
1782 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1783 " applied to non-boolean constant");
1784 TREE_NO_WARNING (op_left) = true;
1788 /* We do not warn for constants because they are typical of macro
1789 expansions that test for features. */
1790 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1793 /* This warning only makes sense with logical operands. */
1794 if (!(truth_value_p (TREE_CODE (op_left))
1795 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1796 || !(truth_value_p (TREE_CODE (op_right))
1797 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1800 /* The range computations only work with scalars. */
1801 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1802 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1805 /* We first test whether either side separately is trivially true
1806 (with OR) or trivially false (with AND). If so, do not warn.
1807 This is a common idiom for testing ranges of data types in
1809 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1812 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1813 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1815 /* If this is an OR operation, invert both sides; now, the result
1816 should be always false to get a warning. */
1820 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1821 if (tem && integer_zerop (tem))
1824 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1827 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1828 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1830 /* If this is an OR operation, invert both sides; now, the result
1831 should be always false to get a warning. */
1835 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1836 if (tem && integer_zerop (tem))
1839 /* If both expressions have the same operand, if we can merge the
1841 if (operand_equal_p (lhs, rhs, 0)
1842 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1843 in1_p, low1, high1))
1845 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1846 /* ... and if the range test is always false, then warn. */
1847 if (tem && integer_zerop (tem))
1850 warning_at (location, OPT_Wlogical_op,
1851 "logical %<or%> of collectively exhaustive tests is "
1854 warning_at (location, OPT_Wlogical_op,
1855 "logical %<and%> of mutually exclusive tests is "
1858 /* Or warn if the operands have exactly the same range, e.g.
1860 else if (tree_int_cst_equal (low0, low1)
1861 && tree_int_cst_equal (high0, high1))
1864 warning_at (location, OPT_Wlogical_op,
1865 "logical %<or%> of equal expressions");
1867 warning_at (location, OPT_Wlogical_op,
1868 "logical %<and%> of equal expressions");
1873 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1874 with constant indices. */
1877 find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1879 tree expr = *expr_p;
1881 if ((TREE_CODE (expr) == ARRAY_REF
1882 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1883 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1885 *(bool *) data = true;
1892 /* Warn if a self-comparison always evaluates to true or false. LOC
1893 is the location of the comparison with code CODE, LHS and RHS are
1894 operands of the comparison. */
1897 warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1899 if (TREE_CODE_CLASS (code) != tcc_comparison)
1902 /* Don't warn for various macro expansions. */
1903 if (from_macro_expansion_at (loc)
1904 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1905 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1908 /* We do not warn for constants because they are typical of macro
1909 expansions that test for features, sizeof, and similar. */
1910 if (CONSTANT_CLASS_P (lhs) || CONSTANT_CLASS_P (rhs))
1913 /* Don't warn for e.g.
1916 if (n == (long) n) ...
1918 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1919 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1922 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1923 It could be a NaN, and NaN never compares equal to anything, even
1925 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1928 if (operand_equal_p (lhs, rhs, 0))
1930 /* Don't warn about array references with constant indices;
1931 these are likely to come from a macro. */
1933 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1937 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1938 || code == GE_EXPR || code == UNLE_EXPR
1939 || code == UNGE_EXPR || code == UNEQ_EXPR);
1941 warning_at (loc, OPT_Wtautological_compare,
1942 "self-comparison always evaluates to true");
1944 warning_at (loc, OPT_Wtautological_compare,
1945 "self-comparison always evaluates to false");
1949 /* Warn about logical not used on the left hand side operand of a comparison.
1950 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1951 Do not warn if RHS is of a boolean type. */
1954 warn_logical_not_parentheses (location_t location, enum tree_code code,
1957 if (TREE_CODE_CLASS (code) != tcc_comparison
1958 || TREE_TYPE (rhs) == NULL_TREE
1959 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1962 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1963 !(x == 0) or !(y != 0). */
1964 if ((code == EQ_EXPR || code == NE_EXPR)
1965 && integer_zerop (rhs))
1968 warning_at (location, OPT_Wlogical_not_parentheses,
1969 "logical not is only applied to the left hand side of "
1973 /* Warn if EXP contains any computations whose results are not used.
1974 Return true if a warning is printed; false otherwise. LOCUS is the
1975 (potential) location of the expression. */
1978 warn_if_unused_value (const_tree exp, location_t locus)
1981 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1984 /* Don't warn about void constructs. This includes casting to void,
1985 void function calls, and statement expressions with a final cast
1987 if (VOID_TYPE_P (TREE_TYPE (exp)))
1990 if (EXPR_HAS_LOCATION (exp))
1991 locus = EXPR_LOCATION (exp);
1993 switch (TREE_CODE (exp))
1995 case PREINCREMENT_EXPR:
1996 case POSTINCREMENT_EXPR:
1997 case PREDECREMENT_EXPR:
1998 case POSTDECREMENT_EXPR:
2003 case TRY_CATCH_EXPR:
2004 case WITH_CLEANUP_EXPR:
2010 /* For a binding, warn if no side effect within it. */
2011 exp = BIND_EXPR_BODY (exp);
2015 case NON_LVALUE_EXPR:
2017 exp = TREE_OPERAND (exp, 0);
2020 case TRUTH_ORIF_EXPR:
2021 case TRUTH_ANDIF_EXPR:
2022 /* In && or ||, warn if 2nd operand has no side effect. */
2023 exp = TREE_OPERAND (exp, 1);
2027 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
2029 /* Let people do `(foo (), 0)' without a warning. */
2030 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
2032 exp = TREE_OPERAND (exp, 1);
2036 /* If this is an expression with side effects, don't warn; this
2037 case commonly appears in macro expansions. */
2038 if (TREE_SIDE_EFFECTS (exp))
2043 /* Don't warn about automatic dereferencing of references, since
2044 the user cannot control it. */
2045 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
2047 exp = TREE_OPERAND (exp, 0);
2053 /* Referencing a volatile value is a side effect, so don't warn. */
2054 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
2055 && TREE_THIS_VOLATILE (exp))
2058 /* If this is an expression which has no operands, there is no value
2059 to be unused. There are no such language-independent codes,
2060 but front ends may define such. */
2061 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
2065 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
2070 /* Print a warning about casts that might indicate violation
2071 of strict aliasing rules if -Wstrict-aliasing is used and
2072 strict aliasing mode is in effect. OTYPE is the original
2073 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
2076 strict_aliasing_warning (tree otype, tree type, tree expr)
2078 /* Strip pointer conversion chains and get to the correct original type. */
2080 otype = TREE_TYPE (expr);
2082 if (!(flag_strict_aliasing
2083 && POINTER_TYPE_P (type)
2084 && POINTER_TYPE_P (otype)
2085 && !VOID_TYPE_P (TREE_TYPE (type)))
2086 /* If the type we are casting to is a ref-all pointer
2087 dereferencing it is always valid. */
2088 || TYPE_REF_CAN_ALIAS_ALL (type))
2091 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
2092 && (DECL_P (TREE_OPERAND (expr, 0))
2093 || handled_component_p (TREE_OPERAND (expr, 0))))
2095 /* Casting the address of an object to non void pointer. Warn
2096 if the cast breaks type based aliasing. */
2097 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2099 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2100 "might break strict-aliasing rules");
2105 /* warn_strict_aliasing >= 3. This includes the default (3).
2106 Only warn if the cast is dereferenced immediately. */
2107 alias_set_type set1 =
2108 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
2109 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2111 if (set1 != set2 && set2 != 0
2112 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
2114 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2115 "pointer will break strict-aliasing rules");
2118 else if (warn_strict_aliasing == 2
2119 && !alias_sets_must_conflict_p (set1, set2))
2121 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2122 "pointer might break strict-aliasing rules");
2128 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2130 /* At this level, warn for any conversions, even if an address is
2131 not taken in the same statement. This will likely produce many
2132 false positives, but could be useful to pinpoint problems that
2133 are not revealed at higher levels. */
2134 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2135 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2136 if (!COMPLETE_TYPE_P (type)
2137 || !alias_sets_must_conflict_p (set1, set2))
2139 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2140 "pointer might break strict-aliasing rules");
2148 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2149 sizeof as last operand of certain builtins. */
2152 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
2153 vec<tree, va_gc> *params, tree *sizeof_arg,
2154 bool (*comp_types) (tree, tree))
2156 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
2157 bool strop = false, cmp = false;
2158 unsigned int idx = ~0;
2161 if (TREE_CODE (callee) != FUNCTION_DECL
2162 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2163 || vec_safe_length (params) <= 1)
2166 switch (DECL_FUNCTION_CODE (callee))
2168 case BUILT_IN_STRNCMP:
2169 case BUILT_IN_STRNCASECMP:
2172 case BUILT_IN_STRNCPY:
2173 case BUILT_IN_STRNCPY_CHK:
2174 case BUILT_IN_STRNCAT:
2175 case BUILT_IN_STRNCAT_CHK:
2176 case BUILT_IN_STPNCPY:
2177 case BUILT_IN_STPNCPY_CHK:
2180 case BUILT_IN_MEMCPY:
2181 case BUILT_IN_MEMCPY_CHK:
2182 case BUILT_IN_MEMMOVE:
2183 case BUILT_IN_MEMMOVE_CHK:
2184 if (params->length () < 3)
2187 dest = (*params)[0];
2190 case BUILT_IN_BCOPY:
2191 if (params->length () < 3)
2194 dest = (*params)[1];
2197 case BUILT_IN_MEMCMP:
2199 if (params->length () < 3)
2202 dest = (*params)[0];
2206 case BUILT_IN_MEMSET:
2207 case BUILT_IN_MEMSET_CHK:
2208 if (params->length () < 3)
2210 dest = (*params)[0];
2213 case BUILT_IN_BZERO:
2214 dest = (*params)[0];
2217 case BUILT_IN_STRNDUP:
2222 case BUILT_IN_MEMCHR:
2223 if (params->length () < 3)
2228 case BUILT_IN_SNPRINTF:
2229 case BUILT_IN_SNPRINTF_CHK:
2230 case BUILT_IN_VSNPRINTF:
2231 case BUILT_IN_VSNPRINTF_CHK:
2232 dest = (*params)[0];
2243 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2246 type = TYPE_P (sizeof_arg[idx])
2247 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2248 if (!POINTER_TYPE_P (type))
2252 && (tem = tree_strip_nop_conversions (dest))
2253 && POINTER_TYPE_P (TREE_TYPE (tem))
2254 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2258 && (tem = tree_strip_nop_conversions (src))
2259 && POINTER_TYPE_P (TREE_TYPE (tem))
2260 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2263 loc = sizeof_arg_loc[idx];
2267 if (!TYPE_P (sizeof_arg[idx])
2268 && operand_equal_p (dest, sizeof_arg[idx], 0)
2269 && comp_types (TREE_TYPE (dest), type))
2271 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2272 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2273 "argument to %<sizeof%> in %qD call is the same "
2274 "expression as the destination; did you mean to "
2275 "remove the addressof?", callee);
2276 else if ((TYPE_PRECISION (TREE_TYPE (type))
2277 == TYPE_PRECISION (char_type_node))
2279 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2280 "argument to %<sizeof%> in %qD call is the same "
2281 "expression as the destination; did you mean to "
2282 "provide an explicit length?", callee);
2284 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2285 "argument to %<sizeof%> in %qD call is the same "
2286 "expression as the destination; did you mean to "
2287 "dereference it?", callee);
2291 if (POINTER_TYPE_P (TREE_TYPE (dest))
2293 && comp_types (TREE_TYPE (dest), type)
2294 && !VOID_TYPE_P (TREE_TYPE (type)))
2296 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2297 "argument to %<sizeof%> in %qD call is the same "
2298 "pointer type %qT as the destination; expected %qT "
2299 "or an explicit length", callee, TREE_TYPE (dest),
2300 TREE_TYPE (TREE_TYPE (dest)));
2307 if (!TYPE_P (sizeof_arg[idx])
2308 && operand_equal_p (src, sizeof_arg[idx], 0)
2309 && comp_types (TREE_TYPE (src), type))
2311 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2312 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2313 "argument to %<sizeof%> in %qD call is the same "
2314 "expression as the source; did you mean to "
2315 "remove the addressof?", callee);
2316 else if ((TYPE_PRECISION (TREE_TYPE (type))
2317 == TYPE_PRECISION (char_type_node))
2319 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2320 "argument to %<sizeof%> in %qD call is the same "
2321 "expression as the source; did you mean to "
2322 "provide an explicit length?", callee);
2324 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2325 "argument to %<sizeof%> in %qD call is the same "
2326 "expression as the source; did you mean to "
2327 "dereference it?", callee);
2331 if (POINTER_TYPE_P (TREE_TYPE (src))
2333 && comp_types (TREE_TYPE (src), type)
2334 && !VOID_TYPE_P (TREE_TYPE (type)))
2336 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2337 "argument to %<sizeof%> in %qD call is the same "
2338 "pointer type %qT as the source; expected %qT "
2339 "or an explicit length", callee, TREE_TYPE (src),
2340 TREE_TYPE (TREE_TYPE (src)));
2347 if (!TYPE_P (sizeof_arg[idx])
2348 && operand_equal_p (dest, sizeof_arg[idx], 0)
2349 && comp_types (TREE_TYPE (dest), type))
2351 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2352 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2353 "argument to %<sizeof%> in %qD call is the same "
2354 "expression as the first source; did you mean to "
2355 "remove the addressof?", callee);
2356 else if ((TYPE_PRECISION (TREE_TYPE (type))
2357 == TYPE_PRECISION (char_type_node))
2359 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2360 "argument to %<sizeof%> in %qD call is the same "
2361 "expression as the first source; did you mean to "
2362 "provide an explicit length?", callee);
2364 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2365 "argument to %<sizeof%> in %qD call is the same "
2366 "expression as the first source; did you mean to "
2367 "dereference it?", callee);
2371 if (POINTER_TYPE_P (TREE_TYPE (dest))
2373 && comp_types (TREE_TYPE (dest), type)
2374 && !VOID_TYPE_P (TREE_TYPE (type)))
2376 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2377 "argument to %<sizeof%> in %qD call is the same "
2378 "pointer type %qT as the first source; expected %qT "
2379 "or an explicit length", callee, TREE_TYPE (dest),
2380 TREE_TYPE (TREE_TYPE (dest)));
2387 if (!TYPE_P (sizeof_arg[idx])
2388 && operand_equal_p (src, sizeof_arg[idx], 0)
2389 && comp_types (TREE_TYPE (src), type))
2391 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2392 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2393 "argument to %<sizeof%> in %qD call is the same "
2394 "expression as the second source; did you mean to "
2395 "remove the addressof?", callee);
2396 else if ((TYPE_PRECISION (TREE_TYPE (type))
2397 == TYPE_PRECISION (char_type_node))
2399 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2400 "argument to %<sizeof%> in %qD call is the same "
2401 "expression as the second source; did you mean to "
2402 "provide an explicit length?", callee);
2404 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2405 "argument to %<sizeof%> in %qD call is the same "
2406 "expression as the second source; did you mean to "
2407 "dereference it?", callee);
2411 if (POINTER_TYPE_P (TREE_TYPE (src))
2413 && comp_types (TREE_TYPE (src), type)
2414 && !VOID_TYPE_P (TREE_TYPE (type)))
2416 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2417 "argument to %<sizeof%> in %qD call is the same "
2418 "pointer type %qT as the second source; expected %qT "
2419 "or an explicit length", callee, TREE_TYPE (src),
2420 TREE_TYPE (TREE_TYPE (src)));
2427 /* Warn for unlikely, improbable, or stupid DECL declarations
2431 check_main_parameter_types (tree decl)
2433 function_args_iterator iter;
2437 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2439 /* XXX void_type_node belies the abstraction. */
2440 if (type == void_type_node || type == error_mark_node )
2444 if (TYPE_ATOMIC (t))
2445 pedwarn (input_location, OPT_Wmain,
2446 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2448 while (POINTER_TYPE_P (t))
2451 if (TYPE_ATOMIC (t))
2452 pedwarn (input_location, OPT_Wmain,
2453 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2461 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2462 pedwarn (input_location, OPT_Wmain,
2463 "first argument of %q+D should be %<int%>", decl);
2467 if (TREE_CODE (type) != POINTER_TYPE
2468 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2469 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2471 pedwarn (input_location, OPT_Wmain,
2472 "second argument of %q+D should be %<char **%>", decl);
2476 if (TREE_CODE (type) != POINTER_TYPE
2477 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2478 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2480 pedwarn (input_location, OPT_Wmain,
2481 "third argument of %q+D should probably be "
2482 "%<char **%>", decl);
2487 /* It is intentional that this message does not mention the third
2488 argument because it's only mentioned in an appendix of the
2490 if (argct > 0 && (argct < 2 || argct > 3))
2491 pedwarn (input_location, OPT_Wmain,
2492 "%q+D takes only zero or two arguments", decl);
2494 if (stdarg_p (TREE_TYPE (decl)))
2495 pedwarn (input_location, OPT_Wmain,
2496 "%q+D declared as variadic function", decl);
2499 /* vector_targets_convertible_p is used for vector pointer types. The
2500 callers perform various checks that the qualifiers are satisfactory,
2501 while OTOH vector_targets_convertible_p ignores the number of elements
2502 in the vectors. That's fine with vector pointers as we can consider,
2503 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2504 and that does not require and conversion of the pointer values.
2505 In contrast, vector_types_convertible_p and
2506 vector_types_compatible_elements_p are used for vector value types. */
2507 /* True if pointers to distinct types T1 and T2 can be converted to
2508 each other without an explicit cast. Only returns true for opaque
2511 vector_targets_convertible_p (const_tree t1, const_tree t2)
2513 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
2514 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2515 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2521 /* vector_types_convertible_p is used for vector value types.
2522 It could in principle call vector_targets_convertible_p as a subroutine,
2523 but then the check for vector type would be duplicated with its callers,
2524 and also the purpose of vector_targets_convertible_p would become
2526 Where vector_types_convertible_p returns true, a conversion might still be
2527 needed to make the types match.
2528 In contrast, vector_targets_convertible_p is used for vector pointer
2529 values, and vector_types_compatible_elements_p is used specifically
2530 in the context for binary operators, as a check if use is possible without
2532 /* True if vector types T1 and T2 can be converted to each other
2533 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2534 can only be converted with -flax-vector-conversions yet that is not
2535 in effect, emit a note telling the user about that option if such
2536 a note has not previously been emitted. */
2538 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2540 static bool emitted_lax_note = false;
2541 bool convertible_lax;
2543 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2544 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2548 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2549 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2550 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2551 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2552 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2554 if (!convertible_lax || flag_lax_vector_conversions)
2555 return convertible_lax;
2557 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2558 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2561 if (emit_lax_note && !emitted_lax_note)
2563 emitted_lax_note = true;
2564 inform (input_location, "use -flax-vector-conversions to permit "
2565 "conversions between vectors with differing "
2566 "element types or numbers of subparts");
2572 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2573 and have vector types, V0 has the same type as V1, and the number of
2574 elements of V0, V1, MASK is the same.
2576 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2577 called with two arguments. In this case implementation passes the
2578 first argument twice in order to share the same tree code. This fact
2579 could enable the mask-values being twice the vector length. This is
2580 an implementation accident and this semantics is not guaranteed to
2583 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2588 bool maybe_const = false;
2589 bool two_arguments = false;
2591 if (v1 == NULL_TREE)
2593 two_arguments = true;
2597 if (v0 == error_mark_node || v1 == error_mark_node
2598 || mask == error_mark_node)
2599 return error_mark_node;
2601 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
2604 error_at (loc, "__builtin_shuffle last argument must "
2605 "be an integer vector");
2606 return error_mark_node;
2609 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2610 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
2613 error_at (loc, "__builtin_shuffle arguments must be vectors");
2614 return error_mark_node;
2617 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2620 error_at (loc, "__builtin_shuffle argument vectors must be of "
2622 return error_mark_node;
2625 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2626 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2627 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2628 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2631 error_at (loc, "__builtin_shuffle number of elements of the "
2632 "argument vector(s) and the mask vector should "
2634 return error_mark_node;
2637 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2638 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2641 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2642 "must have the same size as inner type of the mask");
2643 return error_mark_node;
2646 if (!c_dialect_cxx ())
2648 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2649 v0 = c_fully_fold (v0, false, &maybe_const);
2650 wrap &= maybe_const;
2653 v1 = v0 = save_expr (v0);
2656 v1 = c_fully_fold (v1, false, &maybe_const);
2657 wrap &= maybe_const;
2660 mask = c_fully_fold (mask, false, &maybe_const);
2661 wrap &= maybe_const;
2663 else if (two_arguments)
2664 v1 = v0 = save_expr (v0);
2666 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2668 if (!c_dialect_cxx () && !wrap)
2669 ret = c_wrap_maybe_const (ret, true);
2674 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2675 to integral type. */
2678 c_common_get_narrower (tree op, int *unsignedp_ptr)
2680 op = get_narrower (op, unsignedp_ptr);
2682 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2683 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2685 /* C++0x scoped enumerations don't implicitly convert to integral
2686 type; if we stripped an explicit conversion to a larger type we
2687 need to replace it so common_type will still work. */
2688 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2689 TYPE_UNSIGNED (TREE_TYPE (op)));
2690 op = fold_convert (type, op);
2695 /* This is a helper function of build_binary_op.
2697 For certain operations if both args were extended from the same
2698 smaller type, do the arithmetic in that type and then extend.
2700 BITWISE indicates a bitwise operation.
2701 For them, this optimization is safe only if
2702 both args are zero-extended or both are sign-extended.
2703 Otherwise, we might change the result.
2704 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2705 but calculated in (unsigned short) it would be (unsigned short)-1.
2708 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2710 int unsigned0, unsigned1;
2715 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2716 excessive narrowing when we call get_narrower below. For
2717 example, suppose that OP0 is of unsigned int extended
2718 from signed char and that RESULT_TYPE is long long int.
2719 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2722 (long long int) (unsigned int) signed_char
2724 which get_narrower would narrow down to
2726 (unsigned int) signed char
2728 If we do not cast OP0 first, get_narrower would return
2729 signed_char, which is inconsistent with the case of the
2731 op0 = convert (result_type, op0);
2732 op1 = convert (result_type, op1);
2734 arg0 = c_common_get_narrower (op0, &unsigned0);
2735 arg1 = c_common_get_narrower (op1, &unsigned1);
2737 /* UNS is 1 if the operation to be done is an unsigned one. */
2738 uns = TYPE_UNSIGNED (result_type);
2740 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2741 but it *requires* conversion to FINAL_TYPE. */
2743 if ((TYPE_PRECISION (TREE_TYPE (op0))
2744 == TYPE_PRECISION (TREE_TYPE (arg0)))
2745 && TREE_TYPE (op0) != result_type)
2746 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2747 if ((TYPE_PRECISION (TREE_TYPE (op1))
2748 == TYPE_PRECISION (TREE_TYPE (arg1)))
2749 && TREE_TYPE (op1) != result_type)
2750 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2752 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2754 /* For bitwise operations, signedness of nominal type
2755 does not matter. Consider only how operands were extended. */
2759 /* Note that in all three cases below we refrain from optimizing
2760 an unsigned operation on sign-extended args.
2761 That would not be valid. */
2763 /* Both args variable: if both extended in same way
2764 from same width, do it in that width.
2765 Do it unsigned if args were zero-extended. */
2766 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2767 < TYPE_PRECISION (result_type))
2768 && (TYPE_PRECISION (TREE_TYPE (arg1))
2769 == TYPE_PRECISION (TREE_TYPE (arg0)))
2770 && unsigned0 == unsigned1
2771 && (unsigned0 || !uns))
2772 return c_common_signed_or_unsigned_type
2773 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2775 else if (TREE_CODE (arg0) == INTEGER_CST
2776 && (unsigned1 || !uns)
2777 && (TYPE_PRECISION (TREE_TYPE (arg1))
2778 < TYPE_PRECISION (result_type))
2780 = c_common_signed_or_unsigned_type (unsigned1,
2782 && !POINTER_TYPE_P (type)
2783 && int_fits_type_p (arg0, type))
2786 else if (TREE_CODE (arg1) == INTEGER_CST
2787 && (unsigned0 || !uns)
2788 && (TYPE_PRECISION (TREE_TYPE (arg0))
2789 < TYPE_PRECISION (result_type))
2791 = c_common_signed_or_unsigned_type (unsigned0,
2793 && !POINTER_TYPE_P (type)
2794 && int_fits_type_p (arg1, type))
2800 /* Returns true iff any integer value of type FROM_TYPE can be represented as
2801 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2804 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2806 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2807 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2808 REAL_VALUE_TYPE real_low_bound =
2809 real_value_from_int_cst (0, type_low_bound);
2810 REAL_VALUE_TYPE real_high_bound =
2811 real_value_from_int_cst (0, type_high_bound);
2813 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2814 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2817 /* Checks if expression EXPR of complex/real/integer type cannot be converted
2818 to the complex/real/integer type TYPE. Function returns non-zero when:
2819 * EXPR is a constant which cannot be exactly converted to TYPE.
2820 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2821 for EXPR type and TYPE being both integers or both real, or both
2823 * EXPR is not a constant of complex type and TYPE is a real or
2825 * EXPR is not a constant of real type and TYPE is an integer.
2826 * EXPR is not a constant of integer type which cannot be
2827 exactly converted to real type.
2829 Function allows conversions between types of different signedness and
2830 can return SAFE_CONVERSION (zero) in that case. Function can produce
2831 signedness warnings if PRODUCE_WARNS is true.
2833 Function allows conversions from complex constants to non-complex types,
2834 provided that imaginary part is zero and real part can be safely converted
2837 enum conversion_safety
2838 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2840 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2841 tree expr_type = TREE_TYPE (expr);
2842 loc = expansion_point_location_if_in_system_header (loc);
2844 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2846 /* If type is complex, we are interested in compatibility with
2848 if (TREE_CODE (type) == COMPLEX_TYPE)
2849 type = TREE_TYPE (type);
2851 /* Warn for real constant that is not an exact integer converted
2853 if (TREE_CODE (expr_type) == REAL_TYPE
2854 && TREE_CODE (type) == INTEGER_TYPE)
2856 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2857 give_warning = UNSAFE_REAL;
2859 /* Warn for an integer constant that does not fit into integer type. */
2860 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2861 && TREE_CODE (type) == INTEGER_TYPE
2862 && !int_fits_type_p (expr, type))
2864 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2865 && tree_int_cst_sgn (expr) < 0)
2868 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2869 " implicitly converted to unsigned type");
2871 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2874 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2875 " constant value to negative integer");
2878 give_warning = UNSAFE_OTHER;
2880 else if (TREE_CODE (type) == REAL_TYPE)
2882 /* Warn for an integer constant that does not fit into real type. */
2883 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2885 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2886 if (!exact_real_truncate (TYPE_MODE (type), &a))
2887 give_warning = UNSAFE_REAL;
2889 /* Warn for a real constant that does not fit into a smaller
2891 else if (TREE_CODE (expr_type) == REAL_TYPE
2892 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2894 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2895 if (!exact_real_truncate (TYPE_MODE (type), &a))
2896 give_warning = UNSAFE_REAL;
2901 else if (TREE_CODE (expr) == COMPLEX_CST)
2903 tree imag_part = TREE_IMAGPART (expr);
2904 /* Conversion from complex constant with zero imaginary part,
2905 perform check for conversion of real part. */
2906 if ((TREE_CODE (imag_part) == REAL_CST
2907 && real_zerop (imag_part))
2908 || (TREE_CODE (imag_part) == INTEGER_CST
2909 && integer_zerop (imag_part)))
2910 /* Note: in this branch we use recursive call to unsafe_conversion_p
2911 with different type of EXPR, but it is still safe, because when EXPR
2912 is a constant, it's type is not used in text of generated warnings
2913 (otherwise they could sound misleading). */
2914 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2916 /* Conversion from complex constant with non-zero imaginary part. */
2919 /* Conversion to complex type.
2920 Perform checks for both real and imaginary parts. */
2921 if (TREE_CODE (type) == COMPLEX_TYPE)
2923 /* Unfortunately, produce_warns must be false in two subsequent
2924 calls of unsafe_conversion_p, because otherwise we could
2925 produce strange "double" warnings, if both real and imaginary
2926 parts have conversion problems related to signedness.
2929 int32_t _Complex a = 0x80000000 + 0x80000000i;
2931 Possible solution: add a separate function for checking
2932 constants and combine result of two calls appropriately. */
2933 enum conversion_safety re_safety =
2934 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2935 enum conversion_safety im_safety =
2936 unsafe_conversion_p (loc, type, imag_part, false);
2938 /* Merge the results into appropriate single warning. */
2940 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2941 if (re_safety == im_safety)
2942 give_warning = re_safety;
2943 else if (!re_safety && im_safety)
2944 give_warning = im_safety;
2945 else if (re_safety && !im_safety)
2946 give_warning = re_safety;
2948 give_warning = UNSAFE_OTHER;
2950 /* Warn about conversion from complex to real or integer type. */
2952 give_warning = UNSAFE_IMAGINARY;
2956 /* Checks for remaining case: EXPR is not constant. */
2959 /* Warn for real types converted to integer types. */
2960 if (TREE_CODE (expr_type) == REAL_TYPE
2961 && TREE_CODE (type) == INTEGER_TYPE)
2962 give_warning = UNSAFE_REAL;
2964 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2965 && TREE_CODE (type) == INTEGER_TYPE)
2967 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2968 expr = get_unwidened (expr, 0);
2969 expr_type = TREE_TYPE (expr);
2971 /* Don't warn for short y; short x = ((int)y & 0xff); */
2972 if (TREE_CODE (expr) == BIT_AND_EXPR
2973 || TREE_CODE (expr) == BIT_IOR_EXPR
2974 || TREE_CODE (expr) == BIT_XOR_EXPR)
2976 /* If both args were extended from a shortest type,
2977 use that type if that is safe. */
2978 expr_type = shorten_binary_op (expr_type,
2979 TREE_OPERAND (expr, 0),
2980 TREE_OPERAND (expr, 1),
2983 if (TREE_CODE (expr) == BIT_AND_EXPR)
2985 tree op0 = TREE_OPERAND (expr, 0);
2986 tree op1 = TREE_OPERAND (expr, 1);
2987 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2988 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2990 /* If one of the operands is a non-negative constant
2991 that fits in the target type, then the type of the
2992 other operand does not matter. */
2993 if ((TREE_CODE (op0) == INTEGER_CST
2994 && int_fits_type_p (op0, c_common_signed_type (type))
2995 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2996 || (TREE_CODE (op1) == INTEGER_CST
2997 && int_fits_type_p (op1, c_common_signed_type (type))
2998 && int_fits_type_p (op1,
2999 c_common_unsigned_type (type))))
3000 return SAFE_CONVERSION;
3001 /* If constant is unsigned and fits in the target
3002 type, then the result will also fit. */
3003 else if ((TREE_CODE (op0) == INTEGER_CST
3005 && int_fits_type_p (op0, type))
3006 || (TREE_CODE (op1) == INTEGER_CST
3008 && int_fits_type_p (op1, type)))
3009 return SAFE_CONVERSION;
3012 /* Warn for integer types converted to smaller integer types. */
3013 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
3014 give_warning = UNSAFE_OTHER;
3016 /* When they are the same width but different signedness,
3017 then the value may change. */
3018 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
3019 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
3020 /* Even when converted to a bigger type, if the type is
3021 unsigned but expr is signed, then negative values
3023 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
3025 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
3026 "may change the sign of the result",
3030 /* Warn for integer types converted to real types if and only if
3031 all the range of values of the integer type cannot be
3032 represented by the real type. */
3033 else if (TREE_CODE (expr_type) == INTEGER_TYPE
3034 && TREE_CODE (type) == REAL_TYPE)
3036 /* Don't warn about char y = 0xff; float x = (int) y; */
3037 expr = get_unwidened (expr, 0);
3038 expr_type = TREE_TYPE (expr);
3040 if (!int_safely_convertible_to_real_p (expr_type, type))
3041 give_warning = UNSAFE_OTHER;
3044 /* Warn for real types converted to smaller real types. */
3045 else if (TREE_CODE (expr_type) == REAL_TYPE
3046 && TREE_CODE (type) == REAL_TYPE
3047 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
3048 give_warning = UNSAFE_REAL;
3050 /* Check conversion between two complex types. */
3051 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3052 && TREE_CODE (type) == COMPLEX_TYPE)
3054 /* Extract underlying types (i.e., type of real and imaginary
3055 parts) of expr_type and type. */
3056 tree from_type = TREE_TYPE (expr_type);
3057 tree to_type = TREE_TYPE (type);
3059 /* Warn for real types converted to integer types. */
3060 if (TREE_CODE (from_type) == REAL_TYPE
3061 && TREE_CODE (to_type) == INTEGER_TYPE)
3062 give_warning = UNSAFE_REAL;
3064 /* Warn for real types converted to smaller real types. */
3065 else if (TREE_CODE (from_type) == REAL_TYPE
3066 && TREE_CODE (to_type) == REAL_TYPE
3067 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
3068 give_warning = UNSAFE_REAL;
3070 /* Check conversion for complex integer types. Here implementation
3071 is simpler than for real-domain integers because it does not
3072 involve sophisticated cases, such as bitmasks, casts, etc. */
3073 else if (TREE_CODE (from_type) == INTEGER_TYPE
3074 && TREE_CODE (to_type) == INTEGER_TYPE)
3076 /* Warn for integer types converted to smaller integer types. */
3077 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
3078 give_warning = UNSAFE_OTHER;
3080 /* Check for different signedness, see case for real-domain
3081 integers (above) for a more detailed comment. */
3082 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
3083 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
3084 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
3086 warning_at (loc, OPT_Wsign_conversion,
3087 "conversion to %qT from %qT "
3088 "may change the sign of the result",
3091 else if (TREE_CODE (from_type) == INTEGER_TYPE
3092 && TREE_CODE (to_type) == REAL_TYPE
3093 && !int_safely_convertible_to_real_p (from_type, to_type))
3094 give_warning = UNSAFE_OTHER;
3097 /* Warn for complex types converted to real or integer types. */
3098 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3099 && TREE_CODE (type) != COMPLEX_TYPE)
3100 give_warning = UNSAFE_IMAGINARY;
3103 return give_warning;
3106 /* Warns if the conversion of EXPR to TYPE may alter a value.
3107 This is a helper function for warnings_for_convert_and_check. */
3110 conversion_warning (location_t loc, tree type, tree expr)
3112 tree expr_type = TREE_TYPE (expr);
3113 enum conversion_safety conversion_kind;
3115 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
3118 /* This may happen, because for LHS op= RHS we preevaluate
3119 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3120 means we could no longer see the code of the EXPR. */
3121 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3122 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3123 if (TREE_CODE (expr) == SAVE_EXPR)
3124 expr = TREE_OPERAND (expr, 0);
3126 switch (TREE_CODE (expr))
3134 case TRUTH_ANDIF_EXPR:
3135 case TRUTH_ORIF_EXPR:
3136 case TRUTH_AND_EXPR:
3138 case TRUTH_XOR_EXPR:
3139 case TRUTH_NOT_EXPR:
3140 /* Conversion from boolean to a signed:1 bit-field (which only
3141 can hold the values 0 and -1) doesn't lose information - but
3142 it does change the value. */
3143 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3144 warning_at (loc, OPT_Wconversion,
3145 "conversion to %qT from boolean expression", type);
3151 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
3152 if (conversion_kind == UNSAFE_REAL)
3153 warning_at (loc, OPT_Wfloat_conversion,
3154 "conversion to %qT alters %qT constant value",
3156 else if (conversion_kind)
3157 warning_at (loc, OPT_Wconversion,
3158 "conversion to %qT alters %qT constant value",
3164 /* In case of COND_EXPR, we do not care about the type of
3165 COND_EXPR, only about the conversion of each operand. */
3166 tree op1 = TREE_OPERAND (expr, 1);
3167 tree op2 = TREE_OPERAND (expr, 2);
3169 conversion_warning (loc, type, op1);
3170 conversion_warning (loc, type, op2);
3174 default: /* 'expr' is not a constant. */
3175 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
3176 if (conversion_kind == UNSAFE_REAL)
3177 warning_at (loc, OPT_Wfloat_conversion,
3178 "conversion to %qT from %qT may alter its value",
3180 else if (conversion_kind == UNSAFE_IMAGINARY)
3181 warning_at (loc, OPT_Wconversion,
3182 "conversion to %qT from %qT discards imaginary component",
3184 else if (conversion_kind)
3185 warning_at (loc, OPT_Wconversion,
3186 "conversion to %qT from %qT may alter its value",
3191 /* Produce warnings after a conversion. RESULT is the result of
3192 converting EXPR to TYPE. This is a helper function for
3193 convert_and_check and cp_convert_and_check. */
3196 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3199 loc = expansion_point_location_if_in_system_header (loc);
3201 if (TREE_CODE (expr) == INTEGER_CST
3202 && (TREE_CODE (type) == INTEGER_TYPE
3203 || TREE_CODE (type) == ENUMERAL_TYPE)
3204 && !int_fits_type_p (expr, type))
3206 /* Do not diagnose overflow in a constant expression merely
3207 because a conversion overflowed. */
3208 if (TREE_OVERFLOW (result))
3209 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3211 if (TYPE_UNSIGNED (type))
3213 /* This detects cases like converting -129 or 256 to
3215 if (!int_fits_type_p (expr, c_common_signed_type (type)))
3216 warning_at (loc, OPT_Woverflow,
3217 "large integer implicitly truncated to unsigned type");
3219 conversion_warning (loc, type, expr);
3221 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
3222 warning_at (loc, OPT_Woverflow,
3223 "overflow in implicit constant conversion");
3224 /* No warning for converting 0x80000000 to int. */
3226 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3227 || TYPE_PRECISION (TREE_TYPE (expr))
3228 != TYPE_PRECISION (type)))
3229 warning_at (loc, OPT_Woverflow,
3230 "overflow in implicit constant conversion");
3233 conversion_warning (loc, type, expr);
3235 else if ((TREE_CODE (result) == INTEGER_CST
3236 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
3237 warning_at (loc, OPT_Woverflow,
3238 "overflow in implicit constant conversion");
3240 conversion_warning (loc, type, expr);
3244 /* Convert EXPR to TYPE, warning about conversion problems with constants.
3245 Invoke this function on every expression that is converted implicitly,
3246 i.e. because of language rules and not because of an explicit cast. */
3249 convert_and_check (location_t loc, tree type, tree expr)
3252 tree expr_for_warning;
3254 /* Convert from a value with possible excess precision rather than
3255 via the semantic type, but do not warn about values not fitting
3256 exactly in the semantic type. */
3257 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3259 tree orig_type = TREE_TYPE (expr);
3260 expr = TREE_OPERAND (expr, 0);
3261 expr_for_warning = convert (orig_type, expr);
3262 if (orig_type == type)
3263 return expr_for_warning;
3266 expr_for_warning = expr;
3268 if (TREE_TYPE (expr) == type)
3271 result = convert (type, expr);
3273 if (c_inhibit_evaluation_warnings == 0
3274 && !TREE_OVERFLOW_P (expr)
3275 && result != error_mark_node)
3276 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
3281 /* A node in a list that describes references to variables (EXPR), which are
3282 either read accesses if WRITER is zero, or write accesses, in which case
3283 WRITER is the parent of EXPR. */
3290 /* Used to implement a cache the results of a call to verify_tree. We only
3291 use this for SAVE_EXPRs. */
3294 struct tlist_cache *next;
3295 struct tlist *cache_before_sp;
3296 struct tlist *cache_after_sp;
3300 /* Obstack to use when allocating tlist structures, and corresponding
3302 static struct obstack tlist_obstack;
3303 static char *tlist_firstobj = 0;
3305 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3307 static struct tlist *warned_ids;
3308 /* SAVE_EXPRs need special treatment. We process them only once and then
3309 cache the results. */
3310 static struct tlist_cache *save_expr_cache;
3312 static void add_tlist (struct tlist **, struct tlist *, tree, int);
3313 static void merge_tlist (struct tlist **, struct tlist *, int);
3314 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3315 static int warning_candidate_p (tree);
3316 static bool candidate_equal_p (const_tree, const_tree);
3317 static void warn_for_collisions (struct tlist *);
3318 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3319 static struct tlist *new_tlist (struct tlist *, tree, tree);
3321 /* Create a new struct tlist and fill in its fields. */
3322 static struct tlist *
3323 new_tlist (struct tlist *next, tree t, tree writer)
3326 l = XOBNEW (&tlist_obstack, struct tlist);
3333 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3334 is nonnull, we ignore any node we find which has a writer equal to it. */
3337 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3341 struct tlist *next = add->next;
3344 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3345 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3350 /* Merge the nodes of ADD into TO. This merging process is done so that for
3351 each variable that already exists in TO, no new node is added; however if
3352 there is a write access recorded in ADD, and an occurrence on TO is only
3353 a read access, then the occurrence in TO will be modified to record the
3357 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3359 struct tlist **end = to;
3362 end = &(*end)->next;
3368 struct tlist *next = add->next;
3370 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3371 if (candidate_equal_p (tmp2->expr, add->expr))
3375 tmp2->writer = add->writer;
3379 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3380 end = &(*end)->next;
3387 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3388 references in list LIST conflict with it, excluding reads if ONLY writers
3392 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3397 /* Avoid duplicate warnings. */
3398 for (tmp = warned_ids; tmp; tmp = tmp->next)
3399 if (candidate_equal_p (tmp->expr, written))
3404 if (candidate_equal_p (list->expr, written)
3405 && !candidate_equal_p (list->writer, writer)
3406 && (!only_writes || list->writer))
3408 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3409 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3410 OPT_Wsequence_point, "operation on %qE may be undefined",
3417 /* Given a list LIST of references to variables, find whether any of these
3418 can cause conflicts due to missing sequence points. */
3421 warn_for_collisions (struct tlist *list)
3425 for (tmp = list; tmp; tmp = tmp->next)
3428 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3432 /* Return nonzero if X is a tree that can be verified by the sequence point
3435 warning_candidate_p (tree x)
3437 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3440 if (TREE_CODE (x) == BLOCK)
3443 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3444 (lvalue_p) crash on TRY/CATCH. */
3445 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3451 /* No point to track non-const calls, they will never satisfy
3453 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3456 if (TREE_CODE (x) == STRING_CST)
3462 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3464 candidate_equal_p (const_tree x, const_tree y)
3466 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3469 /* Walk the tree X, and record accesses to variables. If X is written by the
3470 parent tree, WRITER is the parent.
3471 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3472 expression or its only operand forces a sequence point, then everything up
3473 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3475 Once we return, we will have emitted warnings if any subexpression before
3476 such a sequence point could be undefined. On a higher level, however, the
3477 sequence point may not be relevant, and we'll merge the two lists.
3479 Example: (b++, a) + b;
3480 The call that processes the COMPOUND_EXPR will store the increment of B
3481 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3482 processes the PLUS_EXPR will need to merge the two lists so that
3483 eventually, all accesses end up on the same list (and we'll warn about the
3484 unordered subexpressions b++ and b.
3486 A note on merging. If we modify the former example so that our expression
3489 care must be taken not simply to add all three expressions into the final
3490 PNO_SP list. The function merge_tlist takes care of that by merging the
3491 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3492 way, so that no more than one access to B is recorded. */
3495 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3498 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3499 enum tree_code code;
3500 enum tree_code_class cl;
3502 /* X may be NULL if it is the operand of an empty statement expression
3508 code = TREE_CODE (x);
3509 cl = TREE_CODE_CLASS (code);
3511 if (warning_candidate_p (x))
3512 *pno_sp = new_tlist (*pno_sp, x, writer);
3521 case TRUTH_ANDIF_EXPR:
3522 case TRUTH_ORIF_EXPR:
3523 tmp_before = tmp_nosp = tmp_list3 = 0;
3524 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3525 warn_for_collisions (tmp_nosp);
3526 merge_tlist (pbefore_sp, tmp_before, 0);
3527 merge_tlist (pbefore_sp, tmp_nosp, 0);
3528 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3529 merge_tlist (pbefore_sp, tmp_list3, 0);
3533 tmp_before = tmp_list2 = 0;
3534 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3535 warn_for_collisions (tmp_list2);
3536 merge_tlist (pbefore_sp, tmp_before, 0);
3537 merge_tlist (pbefore_sp, tmp_list2, 0);
3539 tmp_list3 = tmp_nosp = 0;
3540 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3541 warn_for_collisions (tmp_nosp);
3542 merge_tlist (pbefore_sp, tmp_list3, 0);
3544 tmp_list3 = tmp_list2 = 0;
3545 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3546 warn_for_collisions (tmp_list2);
3547 merge_tlist (pbefore_sp, tmp_list3, 0);
3548 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3549 two first, to avoid warning for (a ? b++ : b++). */
3550 merge_tlist (&tmp_nosp, tmp_list2, 0);
3551 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3554 case PREDECREMENT_EXPR:
3555 case PREINCREMENT_EXPR:
3556 case POSTDECREMENT_EXPR:
3557 case POSTINCREMENT_EXPR:
3558 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3562 tmp_before = tmp_nosp = tmp_list3 = 0;
3563 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3564 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3565 /* Expressions inside the LHS are not ordered wrt. the sequence points
3566 in the RHS. Example:
3568 Despite the fact that the modification of "a" is in the before_sp
3569 list (tmp_before), it conflicts with the use of "a" in the LHS.
3570 We can handle this by adding the contents of tmp_list3
3571 to those of tmp_before, and redoing the collision warnings for that
3573 add_tlist (&tmp_before, tmp_list3, x, 1);
3574 warn_for_collisions (tmp_before);
3575 /* Exclude the LHS itself here; we first have to merge it into the
3576 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3577 didn't exclude the LHS, we'd get it twice, once as a read and once
3579 add_tlist (pno_sp, tmp_list3, x, 0);
3580 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3582 merge_tlist (pbefore_sp, tmp_before, 0);
3583 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3584 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3585 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3589 /* We need to warn about conflicts among arguments and conflicts between
3590 args and the function address. Side effects of the function address,
3591 however, are not ordered by the sequence point of the call. */
3593 call_expr_arg_iterator iter;
3595 tmp_before = tmp_nosp = 0;
3596 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3597 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3599 tmp_list2 = tmp_list3 = 0;
3600 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3601 merge_tlist (&tmp_list3, tmp_list2, 0);
3602 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3604 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3605 warn_for_collisions (tmp_before);
3606 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3611 /* Scan all the list, e.g. indices of multi dimensional array. */
3614 tmp_before = tmp_nosp = 0;
3615 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3616 merge_tlist (&tmp_nosp, tmp_before, 0);
3617 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3624 struct tlist_cache *t;
3625 for (t = save_expr_cache; t; t = t->next)
3626 if (candidate_equal_p (t->expr, x))
3631 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3632 t->next = save_expr_cache;
3634 save_expr_cache = t;
3636 tmp_before = tmp_nosp = 0;
3637 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3638 warn_for_collisions (tmp_nosp);
3641 merge_tlist (&tmp_list3, tmp_nosp, 0);
3642 t->cache_before_sp = tmp_before;
3643 t->cache_after_sp = tmp_list3;
3645 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3646 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3651 x = TREE_OPERAND (x, 0);
3658 /* For other expressions, simply recurse on their operands.
3659 Manual tail recursion for unary expressions.
3660 Other non-expressions need not be processed. */
3661 if (cl == tcc_unary)
3663 x = TREE_OPERAND (x, 0);
3667 else if (IS_EXPR_CODE_CLASS (cl))
3670 int max = TREE_OPERAND_LENGTH (x);
3671 for (lp = 0; lp < max; lp++)
3673 tmp_before = tmp_nosp = 0;
3674 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3675 merge_tlist (&tmp_nosp, tmp_before, 0);
3676 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3683 /* Try to warn for undefined behavior in EXPR due to missing sequence
3687 verify_sequence_points (tree expr)
3689 struct tlist *before_sp = 0, *after_sp = 0;
3692 save_expr_cache = 0;
3693 if (tlist_firstobj == 0)
3695 gcc_obstack_init (&tlist_obstack);
3696 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3699 verify_tree (expr, &before_sp, &after_sp, 0);
3700 warn_for_collisions (after_sp);
3701 obstack_free (&tlist_obstack, tlist_firstobj);
3704 /* Validate the expression after `case' and apply default promotions. */
3707 check_case_value (location_t loc, tree value)
3709 if (value == NULL_TREE)
3712 if (TREE_CODE (value) == INTEGER_CST)
3713 /* Promote char or short to int. */
3714 value = perform_integral_promotions (value);
3715 else if (value != error_mark_node)
3717 error_at (loc, "case label does not reduce to an integer constant");
3718 value = error_mark_node;
3721 constant_expression_warning (value);
3726 /* See if the case values LOW and HIGH are in the range of the original
3727 type (i.e. before the default conversion to int) of the switch testing
3729 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3730 the type before promoting it. CASE_LOW_P is a pointer to the lower
3731 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3732 if the case is not a case range.
3733 The caller has to make sure that we are not called with NULL for
3734 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3735 was a case value that doesn't fit into the range of the ORIG_TYPE.
3736 Returns true if the case label is in range of ORIG_TYPE (saturated or
3737 untouched) or false if the label is out of range. */
3740 check_case_bounds (location_t loc, tree type, tree orig_type,
3741 tree *case_low_p, tree *case_high_p,
3742 bool *outside_range_p)
3744 tree min_value, max_value;
3745 tree case_low = *case_low_p;
3746 tree case_high = case_high_p ? *case_high_p : case_low;
3748 /* If there was a problem with the original type, do nothing. */
3749 if (orig_type == error_mark_node)
3752 min_value = TYPE_MIN_VALUE (orig_type);
3753 max_value = TYPE_MAX_VALUE (orig_type);
3755 /* Case label is less than minimum for type. */
3756 if (tree_int_cst_compare (case_low, min_value) < 0
3757 && tree_int_cst_compare (case_high, min_value) < 0)
3759 warning_at (loc, 0, "case label value is less than minimum value "
3761 *outside_range_p = true;
3765 /* Case value is greater than maximum for type. */
3766 if (tree_int_cst_compare (case_low, max_value) > 0
3767 && tree_int_cst_compare (case_high, max_value) > 0)
3769 warning_at (loc, 0, "case label value exceeds maximum value for type");
3770 *outside_range_p = true;
3774 /* Saturate lower case label value to minimum. */
3775 if (tree_int_cst_compare (case_high, min_value) >= 0
3776 && tree_int_cst_compare (case_low, min_value) < 0)
3778 warning_at (loc, 0, "lower value in case label range"
3779 " less than minimum value for type");
3780 *outside_range_p = true;
3781 case_low = min_value;
3784 /* Saturate upper case label value to maximum. */
3785 if (tree_int_cst_compare (case_low, max_value) <= 0
3786 && tree_int_cst_compare (case_high, max_value) > 0)
3788 warning_at (loc, 0, "upper value in case label range"
3789 " exceeds maximum value for type");
3790 *outside_range_p = true;
3791 case_high = max_value;
3794 if (*case_low_p != case_low)
3795 *case_low_p = convert (type, case_low);
3796 if (case_high_p && *case_high_p != case_high)
3797 *case_high_p = convert (type, case_high);
3802 /* Return an integer type with BITS bits of precision,
3803 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3806 c_common_type_for_size (unsigned int bits, int unsignedp)
3810 if (bits == TYPE_PRECISION (integer_type_node))
3811 return unsignedp ? unsigned_type_node : integer_type_node;
3813 if (bits == TYPE_PRECISION (signed_char_type_node))
3814 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3816 if (bits == TYPE_PRECISION (short_integer_type_node))
3817 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3819 if (bits == TYPE_PRECISION (long_integer_type_node))
3820 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3822 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3823 return (unsignedp ? long_long_unsigned_type_node
3824 : long_long_integer_type_node);
3826 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3827 if (int_n_enabled_p[i]
3828 && bits == int_n_data[i].bitsize)
3829 return (unsignedp ? int_n_trees[i].unsigned_type
3830 : int_n_trees[i].signed_type);
3832 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3833 return (unsignedp ? widest_unsigned_literal_type_node
3834 : widest_integer_literal_type_node);
3836 if (bits <= TYPE_PRECISION (intQI_type_node))
3837 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3839 if (bits <= TYPE_PRECISION (intHI_type_node))
3840 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3842 if (bits <= TYPE_PRECISION (intSI_type_node))
3843 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3845 if (bits <= TYPE_PRECISION (intDI_type_node))
3846 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3851 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3852 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3853 and saturating if SATP is nonzero, otherwise not saturating. */
3856 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3857 int unsignedp, int satp)
3861 mode = unsignedp ? UQQmode : QQmode;
3863 mode = unsignedp ? UHAmode : HAmode;
3865 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3866 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3869 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3871 sorry ("GCC cannot support operators with integer types and "
3872 "fixed-point types that have too many integral and "
3873 "fractional bits together");
3877 return c_common_type_for_mode (mode, satp);
3880 /* Used for communication between c_common_type_for_mode and
3881 c_register_builtin_type. */
3882 tree registered_builtin_types;
3884 /* Return a data type that has machine mode MODE.
3885 If the mode is an integer,
3886 then UNSIGNEDP selects between signed and unsigned types.
3887 If the mode is a fixed-point mode,
3888 then UNSIGNEDP selects between saturating and nonsaturating types. */
3891 c_common_type_for_mode (machine_mode mode, int unsignedp)
3896 if (mode == TYPE_MODE (integer_type_node))
3897 return unsignedp ? unsigned_type_node : integer_type_node;
3899 if (mode == TYPE_MODE (signed_char_type_node))
3900 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3902 if (mode == TYPE_MODE (short_integer_type_node))
3903 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3905 if (mode == TYPE_MODE (long_integer_type_node))
3906 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3908 if (mode == TYPE_MODE (long_long_integer_type_node))
3909 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3911 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3912 if (int_n_enabled_p[i]
3913 && mode == int_n_data[i].m)
3914 return (unsignedp ? int_n_trees[i].unsigned_type
3915 : int_n_trees[i].signed_type);
3917 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3918 return unsignedp ? widest_unsigned_literal_type_node
3919 : widest_integer_literal_type_node;
3922 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3925 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3928 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3931 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3933 #if HOST_BITS_PER_WIDE_INT >= 64
3934 if (mode == TYPE_MODE (intTI_type_node))
3935 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3938 if (mode == TYPE_MODE (float_type_node))
3939 return float_type_node;
3941 if (mode == TYPE_MODE (double_type_node))
3942 return double_type_node;
3944 if (mode == TYPE_MODE (long_double_type_node))
3945 return long_double_type_node;
3947 if (mode == TYPE_MODE (void_type_node))
3948 return void_type_node;
3950 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3952 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3953 : make_signed_type (GET_MODE_PRECISION (mode)));
3955 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3957 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3958 : make_signed_type (GET_MODE_PRECISION (mode)));
3960 if (COMPLEX_MODE_P (mode))
3962 machine_mode inner_mode;
3965 if (mode == TYPE_MODE (complex_float_type_node))
3966 return complex_float_type_node;
3967 if (mode == TYPE_MODE (complex_double_type_node))
3968 return complex_double_type_node;
3969 if (mode == TYPE_MODE (complex_long_double_type_node))
3970 return complex_long_double_type_node;
3972 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3973 return complex_integer_type_node;
3975 inner_mode = GET_MODE_INNER (mode);
3976 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3977 if (inner_type != NULL_TREE)
3978 return build_complex_type (inner_type);
3980 else if (VECTOR_MODE_P (mode))
3982 machine_mode inner_mode = GET_MODE_INNER (mode);
3983 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3984 if (inner_type != NULL_TREE)
3985 return build_vector_type_for_mode (inner_type, mode);
3988 if (mode == TYPE_MODE (dfloat32_type_node))
3989 return dfloat32_type_node;
3990 if (mode == TYPE_MODE (dfloat64_type_node))
3991 return dfloat64_type_node;
3992 if (mode == TYPE_MODE (dfloat128_type_node))
3993 return dfloat128_type_node;
3995 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3997 if (mode == TYPE_MODE (short_fract_type_node))
3998 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3999 if (mode == TYPE_MODE (fract_type_node))
4000 return unsignedp ? sat_fract_type_node : fract_type_node;
4001 if (mode == TYPE_MODE (long_fract_type_node))
4002 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
4003 if (mode == TYPE_MODE (long_long_fract_type_node))
4004 return unsignedp ? sat_long_long_fract_type_node
4005 : long_long_fract_type_node;
4007 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
4008 return unsignedp ? sat_unsigned_short_fract_type_node
4009 : unsigned_short_fract_type_node;
4010 if (mode == TYPE_MODE (unsigned_fract_type_node))
4011 return unsignedp ? sat_unsigned_fract_type_node
4012 : unsigned_fract_type_node;
4013 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
4014 return unsignedp ? sat_unsigned_long_fract_type_node
4015 : unsigned_long_fract_type_node;
4016 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
4017 return unsignedp ? sat_unsigned_long_long_fract_type_node
4018 : unsigned_long_long_fract_type_node;
4020 if (mode == TYPE_MODE (short_accum_type_node))
4021 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
4022 if (mode == TYPE_MODE (accum_type_node))
4023 return unsignedp ? sat_accum_type_node : accum_type_node;
4024 if (mode == TYPE_MODE (long_accum_type_node))
4025 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
4026 if (mode == TYPE_MODE (long_long_accum_type_node))
4027 return unsignedp ? sat_long_long_accum_type_node
4028 : long_long_accum_type_node;
4030 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
4031 return unsignedp ? sat_unsigned_short_accum_type_node
4032 : unsigned_short_accum_type_node;
4033 if (mode == TYPE_MODE (unsigned_accum_type_node))
4034 return unsignedp ? sat_unsigned_accum_type_node
4035 : unsigned_accum_type_node;
4036 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
4037 return unsignedp ? sat_unsigned_long_accum_type_node
4038 : unsigned_long_accum_type_node;
4039 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
4040 return unsignedp ? sat_unsigned_long_long_accum_type_node
4041 : unsigned_long_long_accum_type_node;
4044 return unsignedp ? sat_qq_type_node : qq_type_node;
4046 return unsignedp ? sat_hq_type_node : hq_type_node;
4048 return unsignedp ? sat_sq_type_node : sq_type_node;
4050 return unsignedp ? sat_dq_type_node : dq_type_node;
4052 return unsignedp ? sat_tq_type_node : tq_type_node;
4054 if (mode == UQQmode)
4055 return unsignedp ? sat_uqq_type_node : uqq_type_node;
4056 if (mode == UHQmode)
4057 return unsignedp ? sat_uhq_type_node : uhq_type_node;
4058 if (mode == USQmode)
4059 return unsignedp ? sat_usq_type_node : usq_type_node;
4060 if (mode == UDQmode)
4061 return unsignedp ? sat_udq_type_node : udq_type_node;
4062 if (mode == UTQmode)
4063 return unsignedp ? sat_utq_type_node : utq_type_node;
4066 return unsignedp ? sat_ha_type_node : ha_type_node;
4068 return unsignedp ? sat_sa_type_node : sa_type_node;
4070 return unsignedp ? sat_da_type_node : da_type_node;
4072 return unsignedp ? sat_ta_type_node : ta_type_node;
4074 if (mode == UHAmode)
4075 return unsignedp ? sat_uha_type_node : uha_type_node;
4076 if (mode == USAmode)
4077 return unsignedp ? sat_usa_type_node : usa_type_node;
4078 if (mode == UDAmode)
4079 return unsignedp ? sat_uda_type_node : uda_type_node;
4080 if (mode == UTAmode)
4081 return unsignedp ? sat_uta_type_node : uta_type_node;
4084 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
4085 if (TYPE_MODE (TREE_VALUE (t)) == mode
4086 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
4087 return TREE_VALUE (t);
4093 c_common_unsigned_type (tree type)
4095 return c_common_signed_or_unsigned_type (1, type);
4098 /* Return a signed type the same as TYPE in other respects. */
4101 c_common_signed_type (tree type)
4103 return c_common_signed_or_unsigned_type (0, type);
4106 /* Return a type the same as TYPE except unsigned or
4107 signed according to UNSIGNEDP. */
4110 c_common_signed_or_unsigned_type (int unsignedp, tree type)
4115 /* This block of code emulates the behavior of the old
4116 c_common_unsigned_type. In particular, it returns
4117 long_unsigned_type_node if passed a long, even when a int would
4118 have the same size. This is necessary for warnings to work
4119 correctly in archs where sizeof(int) == sizeof(long) */
4121 type1 = TYPE_MAIN_VARIANT (type);
4122 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4123 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4124 if (type1 == integer_type_node || type1 == unsigned_type_node)
4125 return unsignedp ? unsigned_type_node : integer_type_node;
4126 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4127 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4128 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4129 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4130 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4131 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
4133 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4134 if (int_n_enabled_p[i]
4135 && (type1 == int_n_trees[i].unsigned_type
4136 || type1 == int_n_trees[i].signed_type))
4137 return (unsignedp ? int_n_trees[i].unsigned_type
4138 : int_n_trees[i].signed_type);
4140 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4141 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4142 #if HOST_BITS_PER_WIDE_INT >= 64
4143 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4144 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4146 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4147 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4148 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4149 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4150 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4151 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4152 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4153 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4155 #define C_COMMON_FIXED_TYPES(NAME) \
4156 if (type1 == short_ ## NAME ## _type_node \
4157 || type1 == unsigned_short_ ## NAME ## _type_node) \
4158 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4159 : short_ ## NAME ## _type_node; \
4160 if (type1 == NAME ## _type_node \
4161 || type1 == unsigned_ ## NAME ## _type_node) \
4162 return unsignedp ? unsigned_ ## NAME ## _type_node \
4163 : NAME ## _type_node; \
4164 if (type1 == long_ ## NAME ## _type_node \
4165 || type1 == unsigned_long_ ## NAME ## _type_node) \
4166 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4167 : long_ ## NAME ## _type_node; \
4168 if (type1 == long_long_ ## NAME ## _type_node \
4169 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4170 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4171 : long_long_ ## NAME ## _type_node;
4173 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
4174 if (type1 == NAME ## _type_node \
4175 || type1 == u ## NAME ## _type_node) \
4176 return unsignedp ? u ## NAME ## _type_node \
4177 : NAME ## _type_node;
4179 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
4180 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4181 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4182 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4183 : sat_ ## short_ ## NAME ## _type_node; \
4184 if (type1 == sat_ ## NAME ## _type_node \
4185 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4186 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4187 : sat_ ## NAME ## _type_node; \
4188 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4189 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4190 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4191 : sat_ ## long_ ## NAME ## _type_node; \
4192 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4193 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4194 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4195 : sat_ ## long_long_ ## NAME ## _type_node;
4197 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4198 if (type1 == sat_ ## NAME ## _type_node \
4199 || type1 == sat_ ## u ## NAME ## _type_node) \
4200 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4201 : sat_ ## NAME ## _type_node;
4203 C_COMMON_FIXED_TYPES (fract);
4204 C_COMMON_FIXED_TYPES_SAT (fract);
4205 C_COMMON_FIXED_TYPES (accum);
4206 C_COMMON_FIXED_TYPES_SAT (accum);
4208 C_COMMON_FIXED_MODE_TYPES (qq);
4209 C_COMMON_FIXED_MODE_TYPES (hq);
4210 C_COMMON_FIXED_MODE_TYPES (sq);
4211 C_COMMON_FIXED_MODE_TYPES (dq);
4212 C_COMMON_FIXED_MODE_TYPES (tq);
4213 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4214 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4215 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4216 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4217 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4218 C_COMMON_FIXED_MODE_TYPES (ha);
4219 C_COMMON_FIXED_MODE_TYPES (sa);
4220 C_COMMON_FIXED_MODE_TYPES (da);
4221 C_COMMON_FIXED_MODE_TYPES (ta);
4222 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4223 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4224 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4225 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
4227 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4228 the precision; they have precision set to match their range, but
4229 may use a wider mode to match an ABI. If we change modes, we may
4230 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4231 the precision as well, so as to yield correct results for
4232 bit-field types. C++ does not have these separate bit-field
4233 types, and producing a signed or unsigned variant of an
4234 ENUMERAL_TYPE may cause other problems as well. */
4236 if (!INTEGRAL_TYPE_P (type)
4237 || TYPE_UNSIGNED (type) == unsignedp)
4240 #define TYPE_OK(node) \
4241 (TYPE_MODE (type) == TYPE_MODE (node) \
4242 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4243 if (TYPE_OK (signed_char_type_node))
4244 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4245 if (TYPE_OK (integer_type_node))
4246 return unsignedp ? unsigned_type_node : integer_type_node;
4247 if (TYPE_OK (short_integer_type_node))
4248 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4249 if (TYPE_OK (long_integer_type_node))
4250 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4251 if (TYPE_OK (long_long_integer_type_node))
4252 return (unsignedp ? long_long_unsigned_type_node
4253 : long_long_integer_type_node);
4255 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4256 if (int_n_enabled_p[i]
4257 && TYPE_MODE (type) == int_n_data[i].m
4258 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4259 return (unsignedp ? int_n_trees[i].unsigned_type
4260 : int_n_trees[i].signed_type);
4262 if (TYPE_OK (widest_integer_literal_type_node))
4263 return (unsignedp ? widest_unsigned_literal_type_node
4264 : widest_integer_literal_type_node);
4266 #if HOST_BITS_PER_WIDE_INT >= 64
4267 if (TYPE_OK (intTI_type_node))
4268 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4270 if (TYPE_OK (intDI_type_node))
4271 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4272 if (TYPE_OK (intSI_type_node))
4273 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4274 if (TYPE_OK (intHI_type_node))
4275 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4276 if (TYPE_OK (intQI_type_node))
4277 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4280 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4283 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4286 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4290 /* Extended integer types of the same width as a standard type have
4291 lesser rank, so those of the same width as int promote to int or
4292 unsigned int and are valid for printf formats expecting int or
4293 unsigned int. To avoid such special cases, avoid creating
4294 extended integer types for bit-fields if a standard integer type
4296 if (width == TYPE_PRECISION (integer_type_node))
4297 return unsignedp ? unsigned_type_node : integer_type_node;
4298 if (width == TYPE_PRECISION (signed_char_type_node))
4299 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4300 if (width == TYPE_PRECISION (short_integer_type_node))
4301 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4302 if (width == TYPE_PRECISION (long_integer_type_node))
4303 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4304 if (width == TYPE_PRECISION (long_long_integer_type_node))
4305 return (unsignedp ? long_long_unsigned_type_node
4306 : long_long_integer_type_node);
4307 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4308 if (int_n_enabled_p[i]
4309 && width == int_n_data[i].bitsize)
4310 return (unsignedp ? int_n_trees[i].unsigned_type
4311 : int_n_trees[i].signed_type);
4312 return build_nonstandard_integer_type (width, unsignedp);
4315 /* The C version of the register_builtin_type langhook. */
4318 c_register_builtin_type (tree type, const char* name)
4322 decl = build_decl (UNKNOWN_LOCATION,
4323 TYPE_DECL, get_identifier (name), type);
4324 DECL_ARTIFICIAL (decl) = 1;
4325 if (!TYPE_NAME (type))
4326 TYPE_NAME (type) = decl;
4329 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4332 /* Print an error message for invalid operands to arith operation
4333 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4334 LOCATION is the location of the message. */
4337 binary_op_error (location_t location, enum tree_code code,
4338 tree type0, tree type1)
4345 opname = "+"; break;
4347 opname = "-"; break;
4349 opname = "*"; break;
4351 opname = "max"; break;
4353 opname = "min"; break;
4355 opname = "=="; break;
4357 opname = "!="; break;
4359 opname = "<="; break;
4361 opname = ">="; break;
4363 opname = "<"; break;
4365 opname = ">"; break;
4367 opname = "<<"; break;
4369 opname = ">>"; break;
4370 case TRUNC_MOD_EXPR:
4371 case FLOOR_MOD_EXPR:
4372 opname = "%"; break;
4373 case TRUNC_DIV_EXPR:
4374 case FLOOR_DIV_EXPR:
4375 opname = "/"; break;
4377 opname = "&"; break;
4379 opname = "|"; break;
4380 case TRUTH_ANDIF_EXPR:
4381 opname = "&&"; break;
4382 case TRUTH_ORIF_EXPR:
4383 opname = "||"; break;
4385 opname = "^"; break;
4390 "invalid operands to binary %s (have %qT and %qT)", opname,
4394 /* Given an expression as a tree, return its original type. Do this
4395 by stripping any conversion that preserves the sign and precision. */
4397 expr_original_type (tree expr)
4399 STRIP_SIGN_NOPS (expr);
4400 return TREE_TYPE (expr);
4403 /* Subroutine of build_binary_op, used for comparison operations.
4404 See if the operands have both been converted from subword integer types
4405 and, if so, perhaps change them both back to their original type.
4406 This function is also responsible for converting the two operands
4407 to the proper common type for comparison.
4409 The arguments of this function are all pointers to local variables
4410 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4411 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4413 LOC is the location of the comparison.
4415 If this function returns nonzero, it means that the comparison has
4416 a constant value. What this function returns is an expression for
4420 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4421 tree *restype_ptr, enum tree_code *rescode_ptr)
4424 tree op0 = *op0_ptr;
4425 tree op1 = *op1_ptr;
4426 int unsignedp0, unsignedp1;
4428 tree primop0, primop1;
4429 enum tree_code code = *rescode_ptr;
4431 /* Throw away any conversions to wider types
4432 already present in the operands. */
4434 primop0 = c_common_get_narrower (op0, &unsignedp0);
4435 primop1 = c_common_get_narrower (op1, &unsignedp1);
4437 /* If primopN is first sign-extended from primopN's precision to opN's
4438 precision, then zero-extended from opN's precision to
4439 *restype_ptr precision, shortenings might be invalid. */
4440 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4441 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4443 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4445 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4446 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4448 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4451 /* Handle the case that OP0 does not *contain* a conversion
4452 but it *requires* conversion to FINAL_TYPE. */
4454 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4455 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4456 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4457 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4459 /* If one of the operands must be floated, we cannot optimize. */
4460 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4461 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4463 /* If first arg is constant, swap the args (changing operation
4464 so value is preserved), for canonicalization. Don't do this if
4465 the second arg is 0. */
4467 if (TREE_CONSTANT (primop0)
4468 && !integer_zerop (primop1) && !real_zerop (primop1)
4469 && !fixed_zerop (primop1))
4471 std::swap (primop0, primop1);
4472 std::swap (op0, op1);
4475 std::swap (unsignedp0, unsignedp1);
4476 std::swap (real1, real2);
4495 *rescode_ptr = code;
4498 /* If comparing an integer against a constant more bits wide,
4499 maybe we can deduce a value of 1 or 0 independent of the data.
4500 Or else truncate the constant now
4501 rather than extend the variable at run time.
4503 This is only interesting if the constant is the wider arg.
4504 Also, it is not safe if the constant is unsigned and the
4505 variable arg is signed, since in this case the variable
4506 would be sign-extended and then regarded as unsigned.
4507 Our technique fails in this case because the lowest/highest
4508 possible unsigned results don't follow naturally from the
4509 lowest/highest possible values of the variable operand.
4510 For just EQ_EXPR and NE_EXPR there is another technique that
4511 could be used: see if the constant can be faithfully represented
4512 in the other operand's type, by truncating it and reextending it
4513 and see if that preserves the constant's value. */
4515 if (!real1 && !real2
4516 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4517 && TREE_CODE (primop1) == INTEGER_CST
4518 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4520 int min_gt, max_gt, min_lt, max_lt;
4521 tree maxval, minval;
4522 /* 1 if comparison is nominally unsigned. */
4523 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4526 type = c_common_signed_or_unsigned_type (unsignedp0,
4527 TREE_TYPE (primop0));
4529 maxval = TYPE_MAX_VALUE (type);
4530 minval = TYPE_MIN_VALUE (type);
4532 if (unsignedp && !unsignedp0)
4533 *restype_ptr = c_common_signed_type (*restype_ptr);
4535 if (TREE_TYPE (primop1) != *restype_ptr)
4537 /* Convert primop1 to target type, but do not introduce
4538 additional overflow. We know primop1 is an int_cst. */
4539 primop1 = force_fit_type (*restype_ptr,
4542 TYPE_PRECISION (*restype_ptr),
4543 TYPE_SIGN (TREE_TYPE (primop1))),
4544 0, TREE_OVERFLOW (primop1));
4546 if (type != *restype_ptr)
4548 minval = convert (*restype_ptr, minval);
4549 maxval = convert (*restype_ptr, maxval);
4552 min_gt = tree_int_cst_lt (primop1, minval);
4553 max_gt = tree_int_cst_lt (primop1, maxval);
4554 min_lt = tree_int_cst_lt (minval, primop1);
4555 max_lt = tree_int_cst_lt (maxval, primop1);
4558 /* This used to be a switch, but Genix compiler can't handle that. */
4559 if (code == NE_EXPR)
4561 if (max_lt || min_gt)
4562 val = truthvalue_true_node;
4564 else if (code == EQ_EXPR)
4566 if (max_lt || min_gt)
4567 val = truthvalue_false_node;
4569 else if (code == LT_EXPR)
4572 val = truthvalue_true_node;
4574 val = truthvalue_false_node;
4576 else if (code == GT_EXPR)
4579 val = truthvalue_true_node;
4581 val = truthvalue_false_node;
4583 else if (code == LE_EXPR)
4586 val = truthvalue_true_node;
4588 val = truthvalue_false_node;
4590 else if (code == GE_EXPR)
4593 val = truthvalue_true_node;
4595 val = truthvalue_false_node;
4598 /* If primop0 was sign-extended and unsigned comparison specd,
4599 we did a signed comparison above using the signed type bounds.
4600 But the comparison we output must be unsigned.
4602 Also, for inequalities, VAL is no good; but if the signed
4603 comparison had *any* fixed result, it follows that the
4604 unsigned comparison just tests the sign in reverse
4605 (positive values are LE, negative ones GE).
4606 So we can generate an unsigned comparison
4607 against an extreme value of the signed type. */
4609 if (unsignedp && !unsignedp0)
4616 primop1 = TYPE_MIN_VALUE (type);
4622 primop1 = TYPE_MAX_VALUE (type);
4629 type = c_common_unsigned_type (type);
4632 if (TREE_CODE (primop0) != INTEGER_CST)
4634 if (val == truthvalue_false_node)
4635 warning_at (loc, OPT_Wtype_limits,
4636 "comparison is always false due to limited range of data type");
4637 if (val == truthvalue_true_node)
4638 warning_at (loc, OPT_Wtype_limits,
4639 "comparison is always true due to limited range of data type");
4644 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4645 if (TREE_SIDE_EFFECTS (primop0))
4646 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4650 /* Value is not predetermined, but do the comparison
4651 in the type of the operand that is not constant.
4652 TYPE is already properly set. */
4655 /* If either arg is decimal float and the other is float, find the
4656 proper common type to use for comparison. */
4657 else if (real1 && real2
4658 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4659 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4660 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4662 /* If either arg is decimal float and the other is float, fail. */
4663 else if (real1 && real2
4664 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4665 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4668 else if (real1 && real2
4669 && (TYPE_PRECISION (TREE_TYPE (primop0))
4670 == TYPE_PRECISION (TREE_TYPE (primop1))))
4671 type = TREE_TYPE (primop0);
4673 /* If args' natural types are both narrower than nominal type
4674 and both extend in the same manner, compare them
4675 in the type of the wider arg.
4676 Otherwise must actually extend both to the nominal
4677 common type lest different ways of extending
4679 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4681 else if (unsignedp0 == unsignedp1 && real1 == real2
4682 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4683 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4685 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4686 type = c_common_signed_or_unsigned_type (unsignedp0
4687 || TYPE_UNSIGNED (*restype_ptr),
4689 /* Make sure shorter operand is extended the right way
4690 to match the longer operand. */
4692 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4693 TREE_TYPE (primop0)),
4696 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4697 TREE_TYPE (primop1)),
4702 /* Here we must do the comparison on the nominal type
4703 using the args exactly as we received them. */
4704 type = *restype_ptr;
4708 if (!real1 && !real2 && integer_zerop (primop1)
4709 && TYPE_UNSIGNED (*restype_ptr))
4712 /* All unsigned values are >= 0, so we warn. However,
4713 if OP0 is a constant that is >= 0, the signedness of
4714 the comparison isn't an issue, so suppress the
4717 warn_type_limits && !in_system_header_at (loc)
4718 && !(TREE_CODE (primop0) == INTEGER_CST
4719 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4721 /* Do not warn for enumeration types. */
4722 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4728 warning_at (loc, OPT_Wtype_limits,
4729 "comparison of unsigned expression >= 0 is always true");
4730 value = truthvalue_true_node;
4735 warning_at (loc, OPT_Wtype_limits,
4736 "comparison of unsigned expression < 0 is always false");
4737 value = truthvalue_false_node;
4746 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4747 if (TREE_SIDE_EFFECTS (primop0))
4748 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4755 *op0_ptr = convert (type, primop0);
4756 *op1_ptr = convert (type, primop1);
4758 *restype_ptr = truthvalue_type_node;
4763 /* Return a tree for the sum or difference (RESULTCODE says which)
4764 of pointer PTROP and integer INTOP. */
4767 pointer_int_sum (location_t loc, enum tree_code resultcode,
4768 tree ptrop, tree intop, bool complain)
4772 /* The result is a pointer of the same type that is being added. */
4773 tree result_type = TREE_TYPE (ptrop);
4775 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4777 if (complain && warn_pointer_arith)
4778 pedwarn (loc, OPT_Wpointer_arith,
4779 "pointer of type %<void *%> used in arithmetic");
4781 return error_mark_node;
4782 size_exp = integer_one_node;
4784 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4786 if (complain && warn_pointer_arith)
4787 pedwarn (loc, OPT_Wpointer_arith,
4788 "pointer to a function used in arithmetic");
4790 return error_mark_node;
4791 size_exp = integer_one_node;
4794 size_exp = size_in_bytes (TREE_TYPE (result_type));
4796 /* We are manipulating pointer values, so we don't need to warn
4797 about relying on undefined signed overflow. We disable the
4798 warning here because we use integer types so fold won't know that
4799 they are really pointers. */
4800 fold_defer_overflow_warnings ();
4802 /* If what we are about to multiply by the size of the elements
4803 contains a constant term, apply distributive law
4804 and multiply that constant term separately.
4805 This helps produce common subexpressions. */
4806 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4807 && !TREE_CONSTANT (intop)
4808 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4809 && TREE_CONSTANT (size_exp)
4810 /* If the constant comes from pointer subtraction,
4811 skip this optimization--it would cause an error. */
4812 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4813 /* If the constant is unsigned, and smaller than the pointer size,
4814 then we must skip this optimization. This is because it could cause
4815 an overflow error if the constant is negative but INTOP is not. */
4816 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4817 || (TYPE_PRECISION (TREE_TYPE (intop))
4818 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4820 enum tree_code subcode = resultcode;
4821 tree int_type = TREE_TYPE (intop);
4822 if (TREE_CODE (intop) == MINUS_EXPR)
4823 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4824 /* Convert both subexpression types to the type of intop,
4825 because weird cases involving pointer arithmetic
4826 can result in a sum or difference with different type args. */
4827 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4829 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4830 intop = convert (int_type, TREE_OPERAND (intop, 0));
4833 /* Convert the integer argument to a type the same size as sizetype
4834 so the multiply won't overflow spuriously. */
4835 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4836 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4837 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4838 TYPE_UNSIGNED (sizetype)), intop);
4840 /* Replace the integer argument with a suitable product by the object size.
4841 Do this multiplication as signed, then convert to the appropriate type
4842 for the pointer operation and disregard an overflow that occurred only
4843 because of the sign-extension change in the latter conversion. */
4845 tree t = build_binary_op (loc,
4847 convert (TREE_TYPE (intop), size_exp), 1);
4848 intop = convert (sizetype, t);
4849 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4850 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4853 /* Create the sum or difference. */
4854 if (resultcode == MINUS_EXPR)
4855 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4857 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4859 fold_undefer_and_ignore_overflow_warnings ();
4864 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4865 and if NON_CONST is known not to be permitted in an evaluated part
4866 of a constant expression. */
4869 c_wrap_maybe_const (tree expr, bool non_const)
4871 bool nowarning = TREE_NO_WARNING (expr);
4872 location_t loc = EXPR_LOCATION (expr);
4874 /* This should never be called for C++. */
4875 if (c_dialect_cxx ())
4878 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4879 STRIP_TYPE_NOPS (expr);
4880 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4881 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4883 TREE_NO_WARNING (expr) = 1;
4884 protected_set_expr_location (expr, loc);
4889 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4890 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4891 around the SAVE_EXPR if needed so that c_fully_fold does not need
4892 to look inside SAVE_EXPRs. */
4895 c_save_expr (tree expr)
4897 bool maybe_const = true;
4898 if (c_dialect_cxx ())
4899 return save_expr (expr);
4900 expr = c_fully_fold (expr, false, &maybe_const);
4901 expr = save_expr (expr);
4903 expr = c_wrap_maybe_const (expr, true);
4907 /* Return whether EXPR is a declaration whose address can never be
4911 decl_with_nonnull_addr_p (const_tree expr)
4913 return (DECL_P (expr)
4914 && (TREE_CODE (expr) == PARM_DECL
4915 || TREE_CODE (expr) == LABEL_DECL
4916 || !DECL_WEAK (expr)));
4919 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4920 or for an `if' or `while' statement or ?..: exp. It should already
4921 have been validated to be of suitable type; otherwise, a bad
4922 diagnostic may result.
4924 The EXPR is located at LOCATION.
4926 This preparation consists of taking the ordinary
4927 representation of an expression expr and producing a valid tree
4928 boolean expression describing whether expr is nonzero. We could
4929 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4930 but we optimize comparisons, &&, ||, and !.
4932 The resulting type should always be `truthvalue_type_node'. */
4935 c_common_truthvalue_conversion (location_t location, tree expr)
4937 switch (TREE_CODE (expr))
4939 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4940 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4941 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4942 case ORDERED_EXPR: case UNORDERED_EXPR:
4943 if (TREE_TYPE (expr) == truthvalue_type_node)
4945 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4946 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4949 case TRUTH_ANDIF_EXPR:
4950 case TRUTH_ORIF_EXPR:
4951 case TRUTH_AND_EXPR:
4953 case TRUTH_XOR_EXPR:
4954 if (TREE_TYPE (expr) == truthvalue_type_node)
4956 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4957 c_common_truthvalue_conversion (location,
4958 TREE_OPERAND (expr, 0)),
4959 c_common_truthvalue_conversion (location,
4960 TREE_OPERAND (expr, 1)));
4963 case TRUTH_NOT_EXPR:
4964 if (TREE_TYPE (expr) == truthvalue_type_node)
4966 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4967 c_common_truthvalue_conversion (location,
4968 TREE_OPERAND (expr, 0)));
4975 return integer_zerop (expr) ? truthvalue_false_node
4976 : truthvalue_true_node;
4979 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4980 ? truthvalue_true_node
4981 : truthvalue_false_node;
4984 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4985 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4986 ? truthvalue_true_node
4987 : truthvalue_false_node;
4990 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4995 tree inner = TREE_OPERAND (expr, 0);
4996 if (decl_with_nonnull_addr_p (inner))
4998 /* Common Ada/Pascal programmer's mistake. */
4999 warning_at (location,
5001 "the address of %qD will always evaluate as %<true%>",
5003 return truthvalue_true_node;
5009 expr = build_binary_op (EXPR_LOCATION (expr),
5010 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
5011 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
5012 c_common_truthvalue_conversion (location,
5013 TREE_OPERAND (expr, 0)),
5014 c_common_truthvalue_conversion (location,
5015 TREE_OPERAND (expr, 1)),
5022 case EXCESS_PRECISION_EXPR:
5023 /* These don't change whether an object is nonzero or zero. */
5024 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
5028 /* These don't change whether an object is zero or nonzero, but
5029 we can't ignore them if their second arg has side-effects. */
5030 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
5032 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
5033 TREE_OPERAND (expr, 1),
5034 c_common_truthvalue_conversion
5035 (location, TREE_OPERAND (expr, 0)));
5039 return c_common_truthvalue_conversion (location,
5040 TREE_OPERAND (expr, 0));
5043 /* Distribute the conversion into the arms of a COND_EXPR. */
5044 if (c_dialect_cxx ())
5046 tree op1 = TREE_OPERAND (expr, 1);
5047 tree op2 = TREE_OPERAND (expr, 2);
5048 /* In C++ one of the arms might have void type if it is throw. */
5049 if (!VOID_TYPE_P (TREE_TYPE (op1)))
5050 op1 = c_common_truthvalue_conversion (location, op1);
5051 if (!VOID_TYPE_P (TREE_TYPE (op2)))
5052 op2 = c_common_truthvalue_conversion (location, op2);
5053 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
5054 TREE_OPERAND (expr, 0), op1, op2);
5059 /* Folding will happen later for C. */
5060 expr = build3 (COND_EXPR, truthvalue_type_node,
5061 TREE_OPERAND (expr, 0),
5062 c_common_truthvalue_conversion (location,
5063 TREE_OPERAND (expr, 1)),
5064 c_common_truthvalue_conversion (location,
5065 TREE_OPERAND (expr, 2)));
5071 tree totype = TREE_TYPE (expr);
5072 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
5074 if (POINTER_TYPE_P (totype)
5075 && TREE_CODE (fromtype) == REFERENCE_TYPE)
5081 warning_at (location,
5083 "the compiler can assume that the address of "
5084 "%qD will always evaluate to %<true%>",
5088 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
5089 since that affects how `default_conversion' will behave. */
5090 if (TREE_CODE (totype) == REFERENCE_TYPE
5091 || TREE_CODE (fromtype) == REFERENCE_TYPE)
5093 /* Don't strip a conversion from C++0x scoped enum, since they
5094 don't implicitly convert to other types. */
5095 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
5096 && ENUM_IS_SCOPED (fromtype))
5098 /* If this isn't narrowing the argument, we can ignore it. */
5099 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
5100 return c_common_truthvalue_conversion (location,
5101 TREE_OPERAND (expr, 0));
5106 if (!TREE_NO_WARNING (expr)
5107 && warn_parentheses)
5109 warning (OPT_Wparentheses,
5110 "suggest parentheses around assignment used as truth value");
5111 TREE_NO_WARNING (expr) = 1;
5119 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
5121 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
5122 expr = (build_binary_op
5123 (EXPR_LOCATION (expr),
5124 (TREE_SIDE_EFFECTS (expr)
5125 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
5126 c_common_truthvalue_conversion
5128 build_unary_op (location, REALPART_EXPR, t, 0)),
5129 c_common_truthvalue_conversion
5131 build_unary_op (location, IMAGPART_EXPR, t, 0)),
5136 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5138 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5140 (TREE_TYPE (expr))));
5141 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
5144 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
5147 protected_set_expr_location (expr, location);
5151 static void def_builtin_1 (enum built_in_function fncode,
5153 enum built_in_class fnclass,
5154 tree fntype, tree libtype,
5155 bool both_p, bool fallback_p, bool nonansi_p,
5156 tree fnattrs, bool implicit_p);
5159 /* Apply the TYPE_QUALS to the new DECL. */
5162 c_apply_type_quals_to_decl (int type_quals, tree decl)
5164 tree type = TREE_TYPE (decl);
5166 if (type == error_mark_node)
5169 if ((type_quals & TYPE_QUAL_CONST)
5170 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5171 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5172 constructor can produce constant init, so rely on cp_finish_decl to
5173 clear TREE_READONLY if the variable has non-constant init. */
5174 TREE_READONLY (decl) = 1;
5175 if (type_quals & TYPE_QUAL_VOLATILE)
5177 TREE_SIDE_EFFECTS (decl) = 1;
5178 TREE_THIS_VOLATILE (decl) = 1;
5180 if (type_quals & TYPE_QUAL_RESTRICT)
5182 while (type && TREE_CODE (type) == ARRAY_TYPE)
5183 /* Allow 'restrict' on arrays of pointers.
5184 FIXME currently we just ignore it. */
5185 type = TREE_TYPE (type);
5187 || !POINTER_TYPE_P (type)
5188 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
5189 error ("invalid use of %<restrict%>");
5193 struct c_type_hasher : ggc_ptr_hash<tree_node>
5195 static hashval_t hash (tree);
5196 static bool equal (tree, tree);
5199 /* Hash function for the problem of multiple type definitions in
5200 different files. This must hash all types that will compare
5201 equal via comptypes to the same value. In practice it hashes
5202 on some of the simple stuff and leaves the details to comptypes. */
5205 c_type_hasher::hash (tree t)
5210 switch (TREE_CODE (t))
5212 /* For pointers, hash on pointee type plus some swizzling. */
5214 return hash (TREE_TYPE (t)) ^ 0x3003003;
5215 /* Hash on number of elements and total size. */
5218 t2 = TYPE_VALUES (t);
5222 t2 = TYPE_FIELDS (t);
5224 case QUAL_UNION_TYPE:
5226 t2 = TYPE_FIELDS (t);
5230 t2 = TYPE_FIELDS (t);
5235 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5236 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5237 n_elements = list_length (t2);
5238 /* We might have a VLA here. */
5239 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5242 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
5243 return ((size << 24) | (n_elements << shift));
5247 c_type_hasher::equal (tree t1, tree t2)
5249 return lang_hooks.types_compatible_p (t1, t2);
5252 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
5254 /* Return the typed-based alias set for T, which may be an expression
5255 or a type. Return -1 if we don't do anything special. */
5258 c_common_get_alias_set (tree t)
5262 /* For VLAs, use the alias set of the element type rather than the
5263 default of alias set 0 for types compared structurally. */
5264 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5266 if (TREE_CODE (t) == ARRAY_TYPE)
5267 return get_alias_set (TREE_TYPE (t));
5271 /* Permit type-punning when accessing a union, provided the access
5272 is directly through the union. For example, this code does not
5273 permit taking the address of a union member and then storing
5274 through it. Even the type-punning allowed here is a GCC
5275 extension, albeit a common and useful one; the C standard says
5276 that such accesses have implementation-defined behavior. */
5278 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5279 u = TREE_OPERAND (u, 0))
5280 if (TREE_CODE (u) == COMPONENT_REF
5281 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5284 /* That's all the expressions we handle specially. */
5288 /* The C standard guarantees that any object may be accessed via an
5289 lvalue that has character type. */
5290 if (t == char_type_node
5291 || t == signed_char_type_node
5292 || t == unsigned_char_type_node)
5295 /* The C standard specifically allows aliasing between signed and
5296 unsigned variants of the same type. We treat the signed
5297 variant as canonical. */
5298 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
5300 tree t1 = c_common_signed_type (t);
5302 /* t1 == t can happen for boolean nodes which are always unsigned. */
5304 return get_alias_set (t1);
5307 /* Handle the case of multiple type nodes referring to "the same" type,
5308 which occurs with IMA. These share an alias set. FIXME: Currently only
5309 C90 is handled. (In C99 type compatibility is not transitive, which
5310 complicates things mightily. The alias set splay trees can theoretically
5311 represent this, but insertion is tricky when you consider all the
5312 different orders things might arrive in.) */
5314 if (c_language != clk_c || flag_isoc99)
5317 /* Save time if there's only one input file. */
5318 if (num_in_fnames == 1)
5321 /* Pointers need special handling if they point to any type that
5322 needs special handling (below). */
5323 if (TREE_CODE (t) == POINTER_TYPE)
5326 /* Find bottom type under any nested POINTERs. */
5327 for (t2 = TREE_TYPE (t);
5328 TREE_CODE (t2) == POINTER_TYPE;
5329 t2 = TREE_TYPE (t2))
5331 if (TREE_CODE (t2) != RECORD_TYPE
5332 && TREE_CODE (t2) != ENUMERAL_TYPE
5333 && TREE_CODE (t2) != QUAL_UNION_TYPE
5334 && TREE_CODE (t2) != UNION_TYPE)
5336 if (TYPE_SIZE (t2) == 0)
5339 /* These are the only cases that need special handling. */
5340 if (TREE_CODE (t) != RECORD_TYPE
5341 && TREE_CODE (t) != ENUMERAL_TYPE
5342 && TREE_CODE (t) != QUAL_UNION_TYPE
5343 && TREE_CODE (t) != UNION_TYPE
5344 && TREE_CODE (t) != POINTER_TYPE)
5347 if (TYPE_SIZE (t) == 0)
5350 /* Look up t in hash table. Only one of the compatible types within each
5351 alias set is recorded in the table. */
5352 if (!type_hash_table)
5353 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5354 tree *slot = type_hash_table->find_slot (t, INSERT);
5357 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5358 return TYPE_ALIAS_SET ((tree)*slot);
5361 /* Our caller will assign and record (in t) a new alias set; all we need
5362 to do is remember t in the hash table. */
5368 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5369 the IS_SIZEOF parameter indicates which operator is being applied.
5370 The COMPLAIN flag controls whether we should diagnose possibly
5371 ill-formed constructs or not. LOC is the location of the SIZEOF or
5372 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5373 a type in any context should be returned, rather than the normal
5374 alignment for that type. */
5377 c_sizeof_or_alignof_type (location_t loc,
5378 tree type, bool is_sizeof, bool min_alignof,
5381 const char *op_name;
5383 enum tree_code type_code = TREE_CODE (type);
5385 op_name = is_sizeof ? "sizeof" : "__alignof__";
5387 if (type_code == FUNCTION_TYPE)
5391 if (complain && warn_pointer_arith)
5392 pedwarn (loc, OPT_Wpointer_arith,
5393 "invalid application of %<sizeof%> to a function type");
5395 return error_mark_node;
5396 value = size_one_node;
5402 if (c_dialect_cxx ())
5403 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5404 "%<alignof%> applied to a function type");
5406 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5407 "%<_Alignof%> applied to a function type");
5409 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5412 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5414 if (type_code == VOID_TYPE
5415 && complain && warn_pointer_arith)
5416 pedwarn (loc, OPT_Wpointer_arith,
5417 "invalid application of %qs to a void type", op_name);
5419 return error_mark_node;
5420 value = size_one_node;
5422 else if (!COMPLETE_TYPE_P (type)
5423 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5426 error_at (loc, "invalid application of %qs to incomplete type %qT",
5428 return error_mark_node;
5430 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5431 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5434 error_at (loc, "invalid application of %qs to array type %qT of "
5435 "incomplete element type", op_name, type);
5436 return error_mark_node;
5441 /* Convert in case a char is more than one unit. */
5442 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5443 size_int (TYPE_PRECISION (char_type_node)
5445 else if (min_alignof)
5446 value = size_int (min_align_of_type (type));
5448 value = size_int (TYPE_ALIGN_UNIT (type));
5451 /* VALUE will have the middle-end integer type sizetype.
5452 However, we should really return a value of type `size_t',
5453 which is just a typedef for an ordinary integer type. */
5454 value = fold_convert_loc (loc, size_type_node, value);
5459 /* Implement the __alignof keyword: Return the minimum required
5460 alignment of EXPR, measured in bytes. For VAR_DECLs,
5461 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5462 from an "aligned" __attribute__ specification). LOC is the
5463 location of the ALIGNOF operator. */
5466 c_alignof_expr (location_t loc, tree expr)
5470 if (VAR_OR_FUNCTION_DECL_P (expr))
5471 t = size_int (DECL_ALIGN_UNIT (expr));
5473 else if (TREE_CODE (expr) == COMPONENT_REF
5474 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5476 error_at (loc, "%<__alignof%> applied to a bit-field");
5479 else if (TREE_CODE (expr) == COMPONENT_REF
5480 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5481 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5483 else if (INDIRECT_REF_P (expr))
5485 tree t = TREE_OPERAND (expr, 0);
5487 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5489 while (CONVERT_EXPR_P (t)
5490 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5494 t = TREE_OPERAND (t, 0);
5495 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5496 if (thisalign > bestalign)
5497 best = t, bestalign = thisalign;
5499 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5502 return c_alignof (loc, TREE_TYPE (expr));
5504 return fold_convert_loc (loc, size_type_node, t);
5507 /* Handle C and C++ default attributes. */
5509 enum built_in_attribute
5511 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5512 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5513 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5514 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5515 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5516 #include "builtin-attrs.def"
5517 #undef DEF_ATTR_NULL_TREE
5519 #undef DEF_ATTR_STRING
5520 #undef DEF_ATTR_IDENT
5521 #undef DEF_ATTR_TREE_LIST
5525 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5527 static void c_init_attributes (void);
5531 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5532 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5533 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5534 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5535 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5536 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5537 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5538 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5540 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5542 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5543 ARG6, ARG7, ARG8) NAME,
5544 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5545 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5546 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5547 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5548 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5549 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5551 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5553 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5555 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5556 #include "builtin-types.def"
5557 #undef DEF_PRIMITIVE_TYPE
5558 #undef DEF_FUNCTION_TYPE_0
5559 #undef DEF_FUNCTION_TYPE_1
5560 #undef DEF_FUNCTION_TYPE_2
5561 #undef DEF_FUNCTION_TYPE_3
5562 #undef DEF_FUNCTION_TYPE_4
5563 #undef DEF_FUNCTION_TYPE_5
5564 #undef DEF_FUNCTION_TYPE_6
5565 #undef DEF_FUNCTION_TYPE_7
5566 #undef DEF_FUNCTION_TYPE_8
5567 #undef DEF_FUNCTION_TYPE_VAR_0
5568 #undef DEF_FUNCTION_TYPE_VAR_1
5569 #undef DEF_FUNCTION_TYPE_VAR_2
5570 #undef DEF_FUNCTION_TYPE_VAR_3
5571 #undef DEF_FUNCTION_TYPE_VAR_4
5572 #undef DEF_FUNCTION_TYPE_VAR_5
5573 #undef DEF_FUNCTION_TYPE_VAR_6
5574 #undef DEF_FUNCTION_TYPE_VAR_7
5575 #undef DEF_POINTER_TYPE
5579 typedef enum c_builtin_type builtin_type;
5581 /* A temporary array for c_common_nodes_and_builtins. Used in
5582 communication with def_fn_type. */
5583 static tree builtin_types[(int) BT_LAST + 1];
5585 /* A helper function for c_common_nodes_and_builtins. Build function type
5586 for DEF with return type RET and N arguments. If VAR is true, then the
5587 function should be variadic after those N arguments.
5589 Takes special care not to ICE if any of the types involved are
5590 error_mark_node, which indicates that said type is not in fact available
5591 (see builtin_type_for_size). In which case the function type as a whole
5592 should be error_mark_node. */
5595 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5598 tree *args = XALLOCAVEC (tree, n);
5603 for (i = 0; i < n; ++i)
5605 builtin_type a = (builtin_type) va_arg (list, int);
5606 t = builtin_types[a];
5607 if (t == error_mark_node)
5612 t = builtin_types[ret];
5613 if (t == error_mark_node)
5616 t = build_varargs_function_type_array (t, n, args);
5618 t = build_function_type_array (t, n, args);
5621 builtin_types[def] = t;
5625 /* Build builtin functions common to both C and C++ language
5629 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5631 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5632 builtin_types[ENUM] = VALUE;
5633 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5634 def_fn_type (ENUM, RETURN, 0, 0);
5635 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5636 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5637 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5638 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5639 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5640 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5641 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5642 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5643 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5644 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5645 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5647 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5648 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5650 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5651 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5653 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5655 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5656 def_fn_type (ENUM, RETURN, 1, 0);
5657 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5658 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5659 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5660 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5661 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5662 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5663 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5664 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5665 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5666 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5667 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5669 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5670 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5672 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5673 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5674 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5676 #include "builtin-types.def"
5678 #undef DEF_PRIMITIVE_TYPE
5679 #undef DEF_FUNCTION_TYPE_0
5680 #undef DEF_FUNCTION_TYPE_1
5681 #undef DEF_FUNCTION_TYPE_2
5682 #undef DEF_FUNCTION_TYPE_3
5683 #undef DEF_FUNCTION_TYPE_4
5684 #undef DEF_FUNCTION_TYPE_5
5685 #undef DEF_FUNCTION_TYPE_6
5686 #undef DEF_FUNCTION_TYPE_7
5687 #undef DEF_FUNCTION_TYPE_8
5688 #undef DEF_FUNCTION_TYPE_VAR_0
5689 #undef DEF_FUNCTION_TYPE_VAR_1
5690 #undef DEF_FUNCTION_TYPE_VAR_2
5691 #undef DEF_FUNCTION_TYPE_VAR_3
5692 #undef DEF_FUNCTION_TYPE_VAR_4
5693 #undef DEF_FUNCTION_TYPE_VAR_5
5694 #undef DEF_FUNCTION_TYPE_VAR_6
5695 #undef DEF_FUNCTION_TYPE_VAR_7
5696 #undef DEF_POINTER_TYPE
5697 builtin_types[(int) BT_LAST] = NULL_TREE;
5699 c_init_attributes ();
5701 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5702 NONANSI_P, ATTRS, IMPLICIT, COND) \
5704 def_builtin_1 (ENUM, NAME, CLASS, \
5705 builtin_types[(int) TYPE], \
5706 builtin_types[(int) LIBTYPE], \
5707 BOTH_P, FALLBACK_P, NONANSI_P, \
5708 built_in_attributes[(int) ATTRS], IMPLICIT);
5709 #include "builtins.def"
5712 targetm.init_builtins ();
5714 build_common_builtin_nodes ();
5717 cilk_init_builtins ();
5720 /* Like get_identifier, but avoid warnings about null arguments when
5721 the argument may be NULL for targets where GCC lacks stdint.h type
5725 c_get_ident (const char *id)
5727 return get_identifier (id);
5730 /* Build tree nodes and builtin functions common to both C and C++ language
5734 c_common_nodes_and_builtins (void)
5736 int char16_type_size;
5737 int char32_type_size;
5738 int wchar_type_size;
5739 tree array_domain_type;
5740 tree va_list_ref_type_node;
5741 tree va_list_arg_type_node;
5744 build_common_tree_nodes (flag_signed_char, flag_short_double);
5746 /* Define `int' and `char' first so that dbx will output them first. */
5747 record_builtin_type (RID_INT, NULL, integer_type_node);
5748 record_builtin_type (RID_CHAR, "char", char_type_node);
5750 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5751 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5752 but not C. Are the conditionals here needed? */
5753 if (c_dialect_cxx ())
5754 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5755 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5756 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5757 record_builtin_type (RID_MAX, "long unsigned int",
5758 long_unsigned_type_node);
5760 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5764 sprintf (name, "__int%d", int_n_data[i].bitsize);
5765 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5766 int_n_trees[i].signed_type);
5767 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5768 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5771 if (c_dialect_cxx ())
5772 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5773 record_builtin_type (RID_MAX, "long long int",
5774 long_long_integer_type_node);
5775 record_builtin_type (RID_MAX, "long long unsigned int",
5776 long_long_unsigned_type_node);
5777 if (c_dialect_cxx ())
5778 record_builtin_type (RID_MAX, "long long unsigned",
5779 long_long_unsigned_type_node);
5780 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5781 record_builtin_type (RID_MAX, "short unsigned int",
5782 short_unsigned_type_node);
5783 if (c_dialect_cxx ())
5784 record_builtin_type (RID_MAX, "unsigned short",
5785 short_unsigned_type_node);
5787 /* Define both `signed char' and `unsigned char'. */
5788 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5789 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5791 /* These are types that c_common_type_for_size and
5792 c_common_type_for_mode use. */
5793 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5794 TYPE_DECL, NULL_TREE,
5796 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5797 TYPE_DECL, NULL_TREE,
5799 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5800 TYPE_DECL, NULL_TREE,
5802 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5803 TYPE_DECL, NULL_TREE,
5805 #if HOST_BITS_PER_WIDE_INT >= 64
5806 /* Note that this is different than the __int128 type that's part of
5807 the generic __intN support. */
5808 if (targetm.scalar_mode_supported_p (TImode))
5809 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5811 get_identifier ("__int128_t"),
5814 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5815 TYPE_DECL, NULL_TREE,
5816 unsigned_intQI_type_node));
5817 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5818 TYPE_DECL, NULL_TREE,
5819 unsigned_intHI_type_node));
5820 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5821 TYPE_DECL, NULL_TREE,
5822 unsigned_intSI_type_node));
5823 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5824 TYPE_DECL, NULL_TREE,
5825 unsigned_intDI_type_node));
5826 #if HOST_BITS_PER_WIDE_INT >= 64
5827 if (targetm.scalar_mode_supported_p (TImode))
5828 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5830 get_identifier ("__uint128_t"),
5831 unsigned_intTI_type_node));
5834 /* Create the widest literal types. */
5835 widest_integer_literal_type_node
5836 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5837 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5838 TYPE_DECL, NULL_TREE,
5839 widest_integer_literal_type_node));
5841 widest_unsigned_literal_type_node
5842 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5843 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5844 TYPE_DECL, NULL_TREE,
5845 widest_unsigned_literal_type_node));
5847 signed_size_type_node = c_common_signed_type (size_type_node);
5850 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5852 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5853 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5854 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5856 /* Only supported decimal floating point extension if the target
5857 actually supports underlying modes. */
5858 if (targetm.scalar_mode_supported_p (SDmode)
5859 && targetm.scalar_mode_supported_p (DDmode)
5860 && targetm.scalar_mode_supported_p (TDmode))
5862 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5863 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5864 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5867 if (targetm.fixed_point_supported_p ())
5869 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5870 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5871 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5872 record_builtin_type (RID_MAX, "long long _Fract",
5873 long_long_fract_type_node);
5874 record_builtin_type (RID_MAX, "unsigned short _Fract",
5875 unsigned_short_fract_type_node);
5876 record_builtin_type (RID_MAX, "unsigned _Fract",
5877 unsigned_fract_type_node);
5878 record_builtin_type (RID_MAX, "unsigned long _Fract",
5879 unsigned_long_fract_type_node);
5880 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5881 unsigned_long_long_fract_type_node);
5882 record_builtin_type (RID_MAX, "_Sat short _Fract",
5883 sat_short_fract_type_node);
5884 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5885 record_builtin_type (RID_MAX, "_Sat long _Fract",
5886 sat_long_fract_type_node);
5887 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5888 sat_long_long_fract_type_node);
5889 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5890 sat_unsigned_short_fract_type_node);
5891 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5892 sat_unsigned_fract_type_node);
5893 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5894 sat_unsigned_long_fract_type_node);
5895 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5896 sat_unsigned_long_long_fract_type_node);
5897 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5898 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5899 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5900 record_builtin_type (RID_MAX, "long long _Accum",
5901 long_long_accum_type_node);
5902 record_builtin_type (RID_MAX, "unsigned short _Accum",
5903 unsigned_short_accum_type_node);
5904 record_builtin_type (RID_MAX, "unsigned _Accum",
5905 unsigned_accum_type_node);
5906 record_builtin_type (RID_MAX, "unsigned long _Accum",
5907 unsigned_long_accum_type_node);
5908 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5909 unsigned_long_long_accum_type_node);
5910 record_builtin_type (RID_MAX, "_Sat short _Accum",
5911 sat_short_accum_type_node);
5912 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5913 record_builtin_type (RID_MAX, "_Sat long _Accum",
5914 sat_long_accum_type_node);
5915 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5916 sat_long_long_accum_type_node);
5917 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5918 sat_unsigned_short_accum_type_node);
5919 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5920 sat_unsigned_accum_type_node);
5921 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5922 sat_unsigned_long_accum_type_node);
5923 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5924 sat_unsigned_long_long_accum_type_node);
5928 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5930 get_identifier ("complex int"),
5931 complex_integer_type_node));
5932 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5934 get_identifier ("complex float"),
5935 complex_float_type_node));
5936 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5938 get_identifier ("complex double"),
5939 complex_double_type_node));
5940 lang_hooks.decls.pushdecl
5941 (build_decl (UNKNOWN_LOCATION,
5942 TYPE_DECL, get_identifier ("complex long double"),
5943 complex_long_double_type_node));
5945 if (c_dialect_cxx ())
5946 /* For C++, make fileptr_type_node a distinct void * type until
5947 FILE type is defined. */
5948 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5950 record_builtin_type (RID_VOID, NULL, void_type_node);
5952 /* Set the TYPE_NAME for any variants that were built before
5953 record_builtin_type gave names to the built-in types. */
5955 tree void_name = TYPE_NAME (void_type_node);
5956 TYPE_NAME (void_type_node) = NULL_TREE;
5957 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5959 TYPE_NAME (void_type_node) = void_name;
5962 void_list_node = build_void_list_node ();
5964 /* Make a type to be the domain of a few array types
5965 whose domains don't really matter.
5966 200 is small enough that it always fits in size_t
5967 and large enough that it can hold most function names for the
5968 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5969 array_domain_type = build_index_type (size_int (200));
5971 /* Make a type for arrays of characters.
5972 With luck nothing will ever really depend on the length of this
5974 char_array_type_node
5975 = build_array_type (char_type_node, array_domain_type);
5977 string_type_node = build_pointer_type (char_type_node);
5978 const_string_type_node
5979 = build_pointer_type (build_qualified_type
5980 (char_type_node, TYPE_QUAL_CONST));
5982 /* This is special for C++ so functions can be overloaded. */
5983 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5984 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5985 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5986 underlying_wchar_type_node = wchar_type_node;
5987 if (c_dialect_cxx ())
5989 if (TYPE_UNSIGNED (wchar_type_node))
5990 wchar_type_node = make_unsigned_type (wchar_type_size);
5992 wchar_type_node = make_signed_type (wchar_type_size);
5993 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5996 /* This is for wide string constants. */
5997 wchar_array_type_node
5998 = build_array_type (wchar_type_node, array_domain_type);
6000 /* Define 'char16_t'. */
6001 char16_type_node = get_identifier (CHAR16_TYPE);
6002 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
6003 char16_type_size = TYPE_PRECISION (char16_type_node);
6004 if (c_dialect_cxx ())
6006 char16_type_node = make_unsigned_type (char16_type_size);
6008 if (cxx_dialect >= cxx11)
6009 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
6012 /* This is for UTF-16 string constants. */
6013 char16_array_type_node
6014 = build_array_type (char16_type_node, array_domain_type);
6016 /* Define 'char32_t'. */
6017 char32_type_node = get_identifier (CHAR32_TYPE);
6018 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
6019 char32_type_size = TYPE_PRECISION (char32_type_node);
6020 if (c_dialect_cxx ())
6022 char32_type_node = make_unsigned_type (char32_type_size);
6024 if (cxx_dialect >= cxx11)
6025 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
6028 /* This is for UTF-32 string constants. */
6029 char32_array_type_node
6030 = build_array_type (char32_type_node, array_domain_type);
6033 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
6036 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
6038 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
6040 if (SIG_ATOMIC_TYPE)
6041 sig_atomic_type_node =
6042 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
6045 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
6048 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
6051 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
6054 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
6057 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
6059 c_uint16_type_node = uint16_type_node =
6060 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
6062 c_uint32_type_node = uint32_type_node =
6063 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
6065 c_uint64_type_node = uint64_type_node =
6066 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
6067 if (INT_LEAST8_TYPE)
6068 int_least8_type_node =
6069 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
6070 if (INT_LEAST16_TYPE)
6071 int_least16_type_node =
6072 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
6073 if (INT_LEAST32_TYPE)
6074 int_least32_type_node =
6075 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
6076 if (INT_LEAST64_TYPE)
6077 int_least64_type_node =
6078 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
6079 if (UINT_LEAST8_TYPE)
6080 uint_least8_type_node =
6081 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
6082 if (UINT_LEAST16_TYPE)
6083 uint_least16_type_node =
6084 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
6085 if (UINT_LEAST32_TYPE)
6086 uint_least32_type_node =
6087 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
6088 if (UINT_LEAST64_TYPE)
6089 uint_least64_type_node =
6090 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
6092 int_fast8_type_node =
6093 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
6094 if (INT_FAST16_TYPE)
6095 int_fast16_type_node =
6096 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
6097 if (INT_FAST32_TYPE)
6098 int_fast32_type_node =
6099 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
6100 if (INT_FAST64_TYPE)
6101 int_fast64_type_node =
6102 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
6103 if (UINT_FAST8_TYPE)
6104 uint_fast8_type_node =
6105 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
6106 if (UINT_FAST16_TYPE)
6107 uint_fast16_type_node =
6108 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
6109 if (UINT_FAST32_TYPE)
6110 uint_fast32_type_node =
6111 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
6112 if (UINT_FAST64_TYPE)
6113 uint_fast64_type_node =
6114 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
6117 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
6120 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
6122 default_function_type
6123 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6125 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
6126 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6128 lang_hooks.decls.pushdecl
6129 (build_decl (UNKNOWN_LOCATION,
6130 TYPE_DECL, get_identifier ("__builtin_va_list"),
6131 va_list_type_node));
6132 if (targetm.enum_va_list_p)
6138 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
6140 lang_hooks.decls.pushdecl
6141 (build_decl (UNKNOWN_LOCATION,
6142 TYPE_DECL, get_identifier (pname),
6148 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
6150 va_list_arg_type_node = va_list_ref_type_node =
6151 build_pointer_type (TREE_TYPE (va_list_type_node));
6155 va_list_arg_type_node = va_list_type_node;
6156 va_list_ref_type_node = build_reference_type (va_list_type_node);
6159 if (!flag_preprocess_only)
6160 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
6162 main_identifier_node = get_identifier ("main");
6164 /* Create the built-in __null node. It is important that this is
6166 null_node = make_int_cst (1, 1);
6167 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
6169 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6170 memset (builtin_types, 0, sizeof (builtin_types));
6173 /* The number of named compound-literals generated thus far. */
6174 static GTY(()) int compound_literal_number;
6176 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6179 set_compound_literal_name (tree decl)
6182 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6183 compound_literal_number);
6184 compound_literal_number++;
6185 DECL_NAME (decl) = get_identifier (name);
6188 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6189 TYPE and operand OP. */
6192 build_va_arg_1 (location_t loc, tree type, tree op)
6194 tree expr = build1 (VA_ARG_EXPR, type, op);
6195 SET_EXPR_LOCATION (expr, loc);
6199 /* Return a VA_ARG_EXPR corresponding to a source-level expression
6200 va_arg (EXPR, TYPE) at source location LOC. */
6203 build_va_arg (location_t loc, tree expr, tree type)
6205 tree va_type = TREE_TYPE (expr);
6206 tree canon_va_type = (va_type == error_mark_node
6208 : targetm.canonical_va_list_type (va_type));
6210 if (va_type == error_mark_node
6211 || canon_va_type == NULL_TREE)
6213 /* Let's handle things neutrallly, if expr:
6214 - has undeclared type, or
6215 - is not an va_list type. */
6216 return build_va_arg_1 (loc, type, expr);
6219 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6221 /* Case 1: Not an array type. */
6223 /* Take the address, to get '&ap'. */
6224 mark_addressable (expr);
6225 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6227 /* Verify that &ap is still recognized as having va_list type. */
6228 tree canon_expr_type
6229 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6230 gcc_assert (canon_expr_type != NULL_TREE);
6232 return build_va_arg_1 (loc, type, expr);
6235 /* Case 2: Array type.
6239 For contrast, let's start with the simple case (case 1). If
6240 canon_va_type is not an array type, but say a char *, then when
6241 passing-by-value a va_list, the type of the va_list param decl is
6242 the same as for another va_list decl (all ap's are char *):
6245 D.1815 = VA_ARG (&ap, 0B, 1);
6251 __builtin_va_start (&ap, 0);
6254 __builtin_va_end (&ap);
6258 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6259 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6260 the same as for another va_list decl (case 2a, struct ap[1]).
6263 D.1844 = VA_ARG (ap, 0B, 0);
6268 __builtin_va_start (&ap, 0);
6270 __builtin_va_end (&ap);
6274 Case 2b is different because:
6275 - on the callee side, the parm decl has declared type va_list, but
6276 grokdeclarator changes the type of the parm decl to a pointer to the
6278 - on the caller side, the pass-by-value uses &ap.
6280 We unify these two cases (case 2a: va_list is array type,
6281 case 2b: va_list is pointer to array elem type), by adding '&' for the
6282 array type case, such that we have a pointer to array elem in both
6285 if (TREE_CODE (va_type) == ARRAY_TYPE)
6287 /* Case 2a: va_list is array type. */
6289 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6291 mark_addressable (expr);
6292 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6295 /* Verify that &ap is still recognized as having va_list type. */
6296 tree canon_expr_type
6297 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6298 gcc_assert (canon_expr_type != NULL_TREE);
6302 /* Case 2b: va_list is pointer to array elem type. */
6303 gcc_assert (POINTER_TYPE_P (va_type));
6304 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6306 /* Don't take the address. We've already got '&ap'. */
6310 return build_va_arg_1 (loc, type, expr);
6314 /* Linked list of disabled built-in functions. */
6316 struct disabled_builtin
6319 struct disabled_builtin *next;
6321 static disabled_builtin *disabled_builtins = NULL;
6323 static bool builtin_function_disabled_p (const char *);
6325 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6326 begins with "__builtin_", give an error. */
6329 disable_builtin_function (const char *name)
6331 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
6332 error ("cannot disable built-in function %qs", name);
6335 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6336 new_disabled_builtin->name = name;
6337 new_disabled_builtin->next = disabled_builtins;
6338 disabled_builtins = new_disabled_builtin;
6343 /* Return true if the built-in function NAME has been disabled, false
6347 builtin_function_disabled_p (const char *name)
6349 disabled_builtin *p;
6350 for (p = disabled_builtins; p != NULL; p = p->next)
6352 if (strcmp (name, p->name) == 0)
6359 /* Worker for DEF_BUILTIN.
6360 Possibly define a builtin function with one or two names.
6361 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6362 nonansi_p and flag_no_nonansi_builtin. */
6365 def_builtin_1 (enum built_in_function fncode,
6367 enum built_in_class fnclass,
6368 tree fntype, tree libtype,
6369 bool both_p, bool fallback_p, bool nonansi_p,
6370 tree fnattrs, bool implicit_p)
6373 const char *libname;
6375 if (fntype == error_mark_node)
6378 gcc_assert ((!both_p && !fallback_p)
6379 || !strncmp (name, "__builtin_",
6380 strlen ("__builtin_")));
6382 libname = name + strlen ("__builtin_");
6383 decl = add_builtin_function (name, fntype, fncode, fnclass,
6384 (fallback_p ? libname : NULL),
6387 set_builtin_decl (fncode, decl, implicit_p);
6390 && !flag_no_builtin && !builtin_function_disabled_p (libname)
6391 && !(nonansi_p && flag_no_nonansi_builtin))
6392 add_builtin_function (libname, libtype, fncode, fnclass,
6396 /* Nonzero if the type T promotes to int. This is (nearly) the
6397 integral promotions defined in ISO C99 6.3.1.1/2. */
6400 c_promoting_integer_type_p (const_tree t)
6402 switch (TREE_CODE (t))
6405 return (TYPE_MAIN_VARIANT (t) == char_type_node
6406 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6407 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6408 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
6409 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6410 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
6413 /* ??? Technically all enumerations not larger than an int
6414 promote to an int. But this is used along code paths
6415 that only want to notice a size change. */
6416 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6426 /* Return 1 if PARMS specifies a fixed number of parameters
6427 and none of their types is affected by default promotions. */
6430 self_promoting_args_p (const_tree parms)
6433 for (t = parms; t; t = TREE_CHAIN (t))
6435 tree type = TREE_VALUE (t);
6437 if (type == error_mark_node)
6440 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6446 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6449 if (c_promoting_integer_type_p (type))
6455 /* Recursively remove any '*' or '&' operator from TYPE. */
6457 strip_pointer_operator (tree t)
6459 while (POINTER_TYPE_P (t))
6464 /* Recursively remove pointer or array type from TYPE. */
6466 strip_pointer_or_array_types (tree t)
6468 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6473 /* Used to compare case labels. K1 and K2 are actually tree nodes
6474 representing case labels, or NULL_TREE for a `default' label.
6475 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6476 K2, and 0 if K1 and K2 are equal. */
6479 case_compare (splay_tree_key k1, splay_tree_key k2)
6481 /* Consider a NULL key (such as arises with a `default' label) to be
6482 smaller than anything else. */
6488 return tree_int_cst_compare ((tree) k1, (tree) k2);
6491 /* Process a case label, located at LOC, for the range LOW_VALUE
6492 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6493 then this case label is actually a `default' label. If only
6494 HIGH_VALUE is NULL_TREE, then case label was declared using the
6495 usual C/C++ syntax, rather than the GNU case range extension.
6496 CASES is a tree containing all the case ranges processed so far;
6497 COND is the condition for the switch-statement itself.
6498 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6499 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6500 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
6503 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6504 tree low_value, tree high_value, bool *outside_range_p)
6509 splay_tree_node node;
6511 /* Create the LABEL_DECL itself. */
6512 label = create_artificial_label (loc);
6514 /* If there was an error processing the switch condition, bail now
6515 before we get more confused. */
6516 if (!cond || cond == error_mark_node)
6519 if ((low_value && TREE_TYPE (low_value)
6520 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6521 || (high_value && TREE_TYPE (high_value)
6522 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6524 error_at (loc, "pointers are not permitted as case values");
6528 /* Case ranges are a GNU extension. */
6530 pedwarn (loc, OPT_Wpedantic,
6531 "range expressions in switch statements are non-standard");
6533 type = TREE_TYPE (cond);
6536 low_value = check_case_value (loc, low_value);
6537 low_value = convert_and_check (loc, type, low_value);
6538 if (low_value == error_mark_node)
6543 high_value = check_case_value (loc, high_value);
6544 high_value = convert_and_check (loc, type, high_value);
6545 if (high_value == error_mark_node)
6549 if (low_value && high_value)
6551 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6552 really a case range, even though it was written that way.
6553 Remove the HIGH_VALUE to simplify later processing. */
6554 if (tree_int_cst_equal (low_value, high_value))
6555 high_value = NULL_TREE;
6556 else if (!tree_int_cst_lt (low_value, high_value))
6557 warning_at (loc, 0, "empty range specified");
6560 /* See if the case is in range of the type of the original testing
6561 expression. If both low_value and high_value are out of range,
6562 don't insert the case label and return NULL_TREE. */
6564 && !check_case_bounds (loc, type, orig_type,
6565 &low_value, high_value ? &high_value : NULL,
6569 /* Look up the LOW_VALUE in the table of case labels we already
6571 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6572 /* If there was not an exact match, check for overlapping ranges.
6573 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6574 that's a `default' label and the only overlap is an exact match. */
6575 if (!node && (low_value || high_value))
6577 splay_tree_node low_bound;
6578 splay_tree_node high_bound;
6580 /* Even though there wasn't an exact match, there might be an
6581 overlap between this case range and another case range.
6582 Since we've (inductively) not allowed any overlapping case
6583 ranges, we simply need to find the greatest low case label
6584 that is smaller that LOW_VALUE, and the smallest low case
6585 label that is greater than LOW_VALUE. If there is an overlap
6586 it will occur in one of these two ranges. */
6587 low_bound = splay_tree_predecessor (cases,
6588 (splay_tree_key) low_value);
6589 high_bound = splay_tree_successor (cases,
6590 (splay_tree_key) low_value);
6592 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6593 the LOW_VALUE, so there is no need to check unless the
6594 LOW_BOUND is in fact itself a case range. */
6596 && CASE_HIGH ((tree) low_bound->value)
6597 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6600 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6601 range is bigger than the low end of the current range, so we
6602 are only interested if the current range is a real range, and
6603 not an ordinary case label. */
6606 && (tree_int_cst_compare ((tree) high_bound->key,
6611 /* If there was an overlap, issue an error. */
6614 tree duplicate = CASE_LABEL ((tree) node->value);
6618 error_at (loc, "duplicate (or overlapping) case value");
6619 error_at (DECL_SOURCE_LOCATION (duplicate),
6620 "this is the first entry overlapping that value");
6624 error_at (loc, "duplicate case value") ;
6625 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6629 error_at (loc, "multiple default labels in one switch");
6630 error_at (DECL_SOURCE_LOCATION (duplicate),
6631 "this is the first default label");
6636 /* Add a CASE_LABEL to the statement-tree. */
6637 case_label = add_stmt (build_case_label (low_value, high_value, label));
6638 /* Register this case label in the splay tree. */
6639 splay_tree_insert (cases,
6640 (splay_tree_key) low_value,
6641 (splay_tree_value) case_label);
6646 /* Add a label so that the back-end doesn't think that the beginning of
6647 the switch is unreachable. Note that we do not add a case label, as
6648 that just leads to duplicates and thence to failure later on. */
6651 tree t = create_artificial_label (loc);
6652 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6654 return error_mark_node;
6657 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6658 Used to verify that case values match up with enumerator values. */
6661 match_case_to_enum_1 (tree key, tree type, tree label)
6663 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6665 if (tree_fits_uhwi_p (key))
6666 print_dec (key, buf, UNSIGNED);
6667 else if (tree_fits_shwi_p (key))
6668 print_dec (key, buf, SIGNED);
6670 print_hex (key, buf);
6672 if (TYPE_NAME (type) == 0)
6673 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6674 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6675 "case value %qs not in enumerated type",
6678 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6679 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6680 "case value %qs not in enumerated type %qT",
6684 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6685 Used to verify that case values match up with enumerator values. */
6688 match_case_to_enum (splay_tree_node node, void *data)
6690 tree label = (tree) node->value;
6691 tree type = (tree) data;
6693 /* Skip default case. */
6694 if (!CASE_LOW (label))
6697 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6698 when we did our enum->case scan. Reset our scratch bit after. */
6699 if (!CASE_LOW_SEEN (label))
6700 match_case_to_enum_1 (CASE_LOW (label), type, label);
6702 CASE_LOW_SEEN (label) = 0;
6704 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6705 not set, that means that CASE_HIGH did not appear when we did our
6706 enum->case scan. Reset our scratch bit after. */
6707 if (CASE_HIGH (label))
6709 if (!CASE_HIGH_SEEN (label))
6710 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6712 CASE_HIGH_SEEN (label) = 0;
6718 /* Handle -Wswitch*. Called from the front end after parsing the
6719 switch construct. */
6720 /* ??? Should probably be somewhere generic, since other languages
6721 besides C and C++ would want this. At the moment, however, C/C++
6722 are the only tree-ssa languages that support enumerations at all,
6723 so the point is moot. */
6726 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6727 tree type, tree cond, bool bool_cond_p,
6728 bool outside_range_p)
6730 splay_tree_node default_node;
6731 splay_tree_node node;
6734 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6735 && !warn_switch_bool)
6738 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6740 warning_at (switch_location, OPT_Wswitch_default,
6741 "switch missing default case");
6743 /* There are certain cases where -Wswitch-bool warnings aren't
6750 so be careful here. */
6751 if (warn_switch_bool && bool_cond_p)
6753 splay_tree_node min_node;
6754 /* If there's a default node, it's also the value with the minimal
6755 key. So look at the penultimate key (if any). */
6757 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6759 min_node = splay_tree_min (cases);
6760 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6762 splay_tree_node max_node = splay_tree_max (cases);
6763 /* This might be a case range, so look at the value with the
6764 maximal key and then check CASE_HIGH. */
6765 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6767 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6769 /* If there's a case value > 1 or < 0, that is outside bool
6772 || (max && wi::gts_p (max, 1))
6773 || (min && wi::lts_p (min, 0))
6781 case, where we want to warn. */
6783 && max && wi::eq_p (max, 1)
6784 && min && wi::eq_p (min, 0)))
6785 warning_at (switch_location, OPT_Wswitch_bool,
6786 "switch condition has boolean value");
6789 /* From here on, we only care about enumerated types. */
6790 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6793 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6794 if (!warn_switch_enum && !warn_switch)
6797 /* Check the cases. Warn about case values which are not members of
6798 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6799 there is no default case, check that exactly all enumeration
6800 literals are covered by the cases. */
6802 /* Clearing COND if it is not an integer constant simplifies
6803 the tests inside the loop below. */
6804 if (TREE_CODE (cond) != INTEGER_CST)
6807 /* The time complexity here is O(N*lg(N)) worst case, but for the
6808 common case of monotonically increasing enumerators, it is
6809 O(N), since the nature of the splay tree will keep the next
6810 element adjacent to the root at all times. */
6812 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6814 tree value = TREE_VALUE (chain);
6815 if (TREE_CODE (value) == CONST_DECL)
6816 value = DECL_INITIAL (value);
6817 node = splay_tree_lookup (cases, (splay_tree_key) value);
6820 /* Mark the CASE_LOW part of the case entry as seen. */
6821 tree label = (tree) node->value;
6822 CASE_LOW_SEEN (label) = 1;
6826 /* Even though there wasn't an exact match, there might be a
6827 case range which includes the enumerator's value. */
6828 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6829 if (node && CASE_HIGH ((tree) node->value))
6831 tree label = (tree) node->value;
6832 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6835 /* If we match the upper bound exactly, mark the CASE_HIGH
6836 part of the case entry as seen. */
6838 CASE_HIGH_SEEN (label) = 1;
6843 /* We've now determined that this enumerated literal isn't
6844 handled by the case labels of the switch statement. */
6846 /* If the switch expression is a constant, we only really care
6847 about whether that constant is handled by the switch. */
6848 if (cond && tree_int_cst_compare (cond, value))
6851 /* If there is a default_node, the only relevant option is
6852 Wswitch-enum. Otherwise, if both are enabled then we prefer
6853 to warn using -Wswitch because -Wswitch is enabled by -Wall
6854 while -Wswitch-enum is explicit. */
6855 warning_at (switch_location,
6856 (default_node || !warn_switch
6859 "enumeration value %qE not handled in switch",
6860 TREE_PURPOSE (chain));
6863 /* Warn if there are case expressions that don't correspond to
6864 enumerators. This can occur since C and C++ don't enforce
6865 type-checking of assignments to enumeration variables.
6867 The time complexity here is now always O(N) worst case, since
6868 we should have marked both the lower bound and upper bound of
6869 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6870 above. This scan also resets those fields. */
6872 splay_tree_foreach (cases, match_case_to_enum, type);
6875 /* Finish an expression taking the address of LABEL (an
6876 IDENTIFIER_NODE). Returns an expression for the address.
6878 LOC is the location for the expression returned. */
6881 finish_label_address_expr (tree label, location_t loc)
6885 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6887 if (label == error_mark_node)
6888 return error_mark_node;
6890 label = lookup_label (label);
6891 if (label == NULL_TREE)
6892 result = null_pointer_node;
6895 TREE_USED (label) = 1;
6896 result = build1 (ADDR_EXPR, ptr_type_node, label);
6897 /* The current function is not necessarily uninlinable.
6898 Computed gotos are incompatible with inlining, but the value
6899 here could be used only in a diagnostic, for example. */
6900 protected_set_expr_location (result, loc);
6907 /* Given a boolean expression ARG, return a tree representing an increment
6908 or decrement (as indicated by CODE) of ARG. The front end must check for
6909 invalid cases (e.g., decrement in C++). */
6911 boolean_increment (enum tree_code code, tree arg)
6914 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6916 arg = stabilize_reference (arg);
6919 case PREINCREMENT_EXPR:
6920 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6922 case POSTINCREMENT_EXPR:
6923 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6924 arg = save_expr (arg);
6925 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6926 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6928 case PREDECREMENT_EXPR:
6929 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6930 invert_truthvalue_loc (input_location, arg));
6932 case POSTDECREMENT_EXPR:
6933 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6934 invert_truthvalue_loc (input_location, arg));
6935 arg = save_expr (arg);
6936 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6937 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6942 TREE_SIDE_EFFECTS (val) = 1;
6946 /* Built-in macros for stddef.h and stdint.h, that require macros
6947 defined in this file. */
6949 c_stddef_cpp_builtins(void)
6951 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6952 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6953 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6954 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6955 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6956 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6957 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6958 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6959 if (SIG_ATOMIC_TYPE)
6960 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6962 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6964 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6966 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6968 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6970 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6972 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6974 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6976 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6977 if (INT_LEAST8_TYPE)
6978 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6979 if (INT_LEAST16_TYPE)
6980 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6981 if (INT_LEAST32_TYPE)
6982 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6983 if (INT_LEAST64_TYPE)
6984 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6985 if (UINT_LEAST8_TYPE)
6986 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6987 if (UINT_LEAST16_TYPE)
6988 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6989 if (UINT_LEAST32_TYPE)
6990 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6991 if (UINT_LEAST64_TYPE)
6992 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6994 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6995 if (INT_FAST16_TYPE)
6996 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6997 if (INT_FAST32_TYPE)
6998 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6999 if (INT_FAST64_TYPE)
7000 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
7001 if (UINT_FAST8_TYPE)
7002 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
7003 if (UINT_FAST16_TYPE)
7004 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
7005 if (UINT_FAST32_TYPE)
7006 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
7007 if (UINT_FAST64_TYPE)
7008 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
7010 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
7012 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
7016 c_init_attributes (void)
7018 /* Fill in the built_in_attributes array. */
7019 #define DEF_ATTR_NULL_TREE(ENUM) \
7020 built_in_attributes[(int) ENUM] = NULL_TREE;
7021 #define DEF_ATTR_INT(ENUM, VALUE) \
7022 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
7023 #define DEF_ATTR_STRING(ENUM, VALUE) \
7024 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7025 #define DEF_ATTR_IDENT(ENUM, STRING) \
7026 built_in_attributes[(int) ENUM] = get_identifier (STRING);
7027 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
7028 built_in_attributes[(int) ENUM] \
7029 = tree_cons (built_in_attributes[(int) PURPOSE], \
7030 built_in_attributes[(int) VALUE], \
7031 built_in_attributes[(int) CHAIN]);
7032 #include "builtin-attrs.def"
7033 #undef DEF_ATTR_NULL_TREE
7035 #undef DEF_ATTR_IDENT
7036 #undef DEF_ATTR_TREE_LIST
7039 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
7040 identifier as an argument, so the front end shouldn't look it up. */
7043 attribute_takes_identifier_p (const_tree attr_id)
7045 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
7047 /* Unknown attribute that we'll end up ignoring, return true so we
7048 don't complain about an identifier argument. */
7050 else if (!strcmp ("mode", spec->name)
7051 || !strcmp ("format", spec->name)
7052 || !strcmp ("cleanup", spec->name))
7055 return targetm.attribute_takes_identifier_p (attr_id);
7058 /* Attribute handlers common to C front ends. */
7060 /* Handle a "packed" attribute; arguments as in
7061 struct attribute_spec.handler. */
7064 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7065 int flags, bool *no_add_attrs)
7069 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7070 *node = build_variant_type_copy (*node);
7071 TYPE_PACKED (*node) = 1;
7073 else if (TREE_CODE (*node) == FIELD_DECL)
7075 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
7076 /* Still pack bitfields. */
7077 && ! DECL_INITIAL (*node))
7078 warning (OPT_Wattributes,
7079 "%qE attribute ignored for field of type %qT",
7080 name, TREE_TYPE (*node));
7082 DECL_PACKED (*node) = 1;
7084 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
7085 used for DECL_REGISTER. It wouldn't mean anything anyway.
7086 We can't set DECL_PACKED on the type of a TYPE_DECL, because
7087 that changes what the typedef is typing. */
7090 warning (OPT_Wattributes, "%qE attribute ignored", name);
7091 *no_add_attrs = true;
7097 /* Handle a "nocommon" attribute; arguments as in
7098 struct attribute_spec.handler. */
7101 handle_nocommon_attribute (tree *node, tree name,
7102 tree ARG_UNUSED (args),
7103 int ARG_UNUSED (flags), bool *no_add_attrs)
7106 DECL_COMMON (*node) = 0;
7109 warning (OPT_Wattributes, "%qE attribute ignored", name);
7110 *no_add_attrs = true;
7116 /* Handle a "common" attribute; arguments as in
7117 struct attribute_spec.handler. */
7120 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7121 int ARG_UNUSED (flags), bool *no_add_attrs)
7124 DECL_COMMON (*node) = 1;
7127 warning (OPT_Wattributes, "%qE attribute ignored", name);
7128 *no_add_attrs = true;
7134 /* Handle a "noreturn" attribute; arguments as in
7135 struct attribute_spec.handler. */
7138 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7139 int ARG_UNUSED (flags), bool *no_add_attrs)
7141 tree type = TREE_TYPE (*node);
7143 /* See FIXME comment in c_common_attribute_table. */
7144 if (TREE_CODE (*node) == FUNCTION_DECL
7145 || objc_method_decl (TREE_CODE (*node)))
7146 TREE_THIS_VOLATILE (*node) = 1;
7147 else if (TREE_CODE (type) == POINTER_TYPE
7148 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7150 = (build_qualified_type
7152 (build_type_variant (TREE_TYPE (type),
7153 TYPE_READONLY (TREE_TYPE (type)), 1)),
7154 TYPE_QUALS (type)));
7157 warning (OPT_Wattributes, "%qE attribute ignored", name);
7158 *no_add_attrs = true;
7164 /* Handle a "hot" and attribute; arguments as in
7165 struct attribute_spec.handler. */
7168 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7169 int ARG_UNUSED (flags), bool *no_add_attrs)
7171 if (TREE_CODE (*node) == FUNCTION_DECL
7172 || TREE_CODE (*node) == LABEL_DECL)
7174 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7176 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7177 "with attribute %qs", name, "cold");
7178 *no_add_attrs = true;
7180 /* Most of the rest of the hot processing is done later with
7181 lookup_attribute. */
7185 warning (OPT_Wattributes, "%qE attribute ignored", name);
7186 *no_add_attrs = true;
7192 /* Handle a "cold" and attribute; arguments as in
7193 struct attribute_spec.handler. */
7196 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7197 int ARG_UNUSED (flags), bool *no_add_attrs)
7199 if (TREE_CODE (*node) == FUNCTION_DECL
7200 || TREE_CODE (*node) == LABEL_DECL)
7202 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7204 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7205 "with attribute %qs", name, "hot");
7206 *no_add_attrs = true;
7208 /* Most of the rest of the cold processing is done later with
7209 lookup_attribute. */
7213 warning (OPT_Wattributes, "%qE attribute ignored", name);
7214 *no_add_attrs = true;
7220 /* Handle a "no_sanitize_address" attribute; arguments as in
7221 struct attribute_spec.handler. */
7224 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7227 if (TREE_CODE (*node) != FUNCTION_DECL)
7229 warning (OPT_Wattributes, "%qE attribute ignored", name);
7230 *no_add_attrs = true;
7236 /* Handle a "no_address_safety_analysis" attribute; arguments as in
7237 struct attribute_spec.handler. */
7240 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7243 if (TREE_CODE (*node) != FUNCTION_DECL)
7244 warning (OPT_Wattributes, "%qE attribute ignored", name);
7245 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7246 DECL_ATTRIBUTES (*node)
7247 = tree_cons (get_identifier ("no_sanitize_address"),
7248 NULL_TREE, DECL_ATTRIBUTES (*node));
7249 *no_add_attrs = true;
7253 /* Handle a "no_sanitize_undefined" attribute; arguments as in
7254 struct attribute_spec.handler. */
7257 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7260 if (TREE_CODE (*node) != FUNCTION_DECL)
7262 warning (OPT_Wattributes, "%qE attribute ignored", name);
7263 *no_add_attrs = true;
7269 /* Handle a "stack_protect" attribute; arguments as in
7270 struct attribute_spec.handler. */
7272 handle_stack_protect_attribute (tree *node, tree name, tree, int,
7275 if (TREE_CODE (*node) != FUNCTION_DECL)
7277 warning (OPT_Wattributes, "%qE attribute ignored", name);
7278 *no_add_attrs = true;
7281 DECL_ATTRIBUTES (*node)
7282 = tree_cons (get_identifier ("stack_protect"),
7283 NULL_TREE, DECL_ATTRIBUTES (*node));
7288 /* Handle a "noinline" attribute; arguments as in
7289 struct attribute_spec.handler. */
7292 handle_noinline_attribute (tree *node, tree name,
7293 tree ARG_UNUSED (args),
7294 int ARG_UNUSED (flags), bool *no_add_attrs)
7296 if (TREE_CODE (*node) == FUNCTION_DECL)
7298 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7300 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7301 "with attribute %qs", name, "always_inline");
7302 *no_add_attrs = true;
7305 DECL_UNINLINABLE (*node) = 1;
7309 warning (OPT_Wattributes, "%qE attribute ignored", name);
7310 *no_add_attrs = true;
7316 /* Handle a "noclone" attribute; arguments as in
7317 struct attribute_spec.handler. */
7320 handle_noclone_attribute (tree *node, tree name,
7321 tree ARG_UNUSED (args),
7322 int ARG_UNUSED (flags), bool *no_add_attrs)
7324 if (TREE_CODE (*node) != FUNCTION_DECL)
7326 warning (OPT_Wattributes, "%qE attribute ignored", name);
7327 *no_add_attrs = true;
7333 /* Handle a "no_icf" attribute; arguments as in
7334 struct attribute_spec.handler. */
7337 handle_noicf_attribute (tree *node, tree name,
7338 tree ARG_UNUSED (args),
7339 int ARG_UNUSED (flags), bool *no_add_attrs)
7341 if (TREE_CODE (*node) != FUNCTION_DECL)
7343 warning (OPT_Wattributes, "%qE attribute ignored", name);
7344 *no_add_attrs = true;
7351 /* Handle a "always_inline" attribute; arguments as in
7352 struct attribute_spec.handler. */
7355 handle_always_inline_attribute (tree *node, tree name,
7356 tree ARG_UNUSED (args),
7357 int ARG_UNUSED (flags),
7360 if (TREE_CODE (*node) == FUNCTION_DECL)
7362 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7364 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7365 "with %qs attribute", name, "noinline");
7366 *no_add_attrs = true;
7369 /* Set the attribute and mark it for disregarding inline
7371 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
7375 warning (OPT_Wattributes, "%qE attribute ignored", name);
7376 *no_add_attrs = true;
7382 /* Handle a "gnu_inline" attribute; arguments as in
7383 struct attribute_spec.handler. */
7386 handle_gnu_inline_attribute (tree *node, tree name,
7387 tree ARG_UNUSED (args),
7388 int ARG_UNUSED (flags),
7391 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7393 /* Do nothing else, just set the attribute. We'll get at
7394 it later with lookup_attribute. */
7398 warning (OPT_Wattributes, "%qE attribute ignored", name);
7399 *no_add_attrs = true;
7405 /* Handle a "leaf" attribute; arguments as in
7406 struct attribute_spec.handler. */
7409 handle_leaf_attribute (tree *node, tree name,
7410 tree ARG_UNUSED (args),
7411 int ARG_UNUSED (flags), bool *no_add_attrs)
7413 if (TREE_CODE (*node) != FUNCTION_DECL)
7415 warning (OPT_Wattributes, "%qE attribute ignored", name);
7416 *no_add_attrs = true;
7418 if (!TREE_PUBLIC (*node))
7420 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7421 *no_add_attrs = true;
7427 /* Handle an "artificial" attribute; arguments as in
7428 struct attribute_spec.handler. */
7431 handle_artificial_attribute (tree *node, tree name,
7432 tree ARG_UNUSED (args),
7433 int ARG_UNUSED (flags),
7436 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7438 /* Do nothing else, just set the attribute. We'll get at
7439 it later with lookup_attribute. */
7443 warning (OPT_Wattributes, "%qE attribute ignored", name);
7444 *no_add_attrs = true;
7450 /* Handle a "flatten" attribute; arguments as in
7451 struct attribute_spec.handler. */
7454 handle_flatten_attribute (tree *node, tree name,
7455 tree args ATTRIBUTE_UNUSED,
7456 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7458 if (TREE_CODE (*node) == FUNCTION_DECL)
7459 /* Do nothing else, just set the attribute. We'll get at
7460 it later with lookup_attribute. */
7464 warning (OPT_Wattributes, "%qE attribute ignored", name);
7465 *no_add_attrs = true;
7471 /* Handle a "warning" or "error" attribute; arguments as in
7472 struct attribute_spec.handler. */
7475 handle_error_attribute (tree *node, tree name, tree args,
7476 int ARG_UNUSED (flags), bool *no_add_attrs)
7478 if (TREE_CODE (*node) == FUNCTION_DECL
7479 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7480 /* Do nothing else, just set the attribute. We'll get at
7481 it later with lookup_attribute. */
7485 warning (OPT_Wattributes, "%qE attribute ignored", name);
7486 *no_add_attrs = true;
7492 /* Handle a "used" attribute; arguments as in
7493 struct attribute_spec.handler. */
7496 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7497 int ARG_UNUSED (flags), bool *no_add_attrs)
7501 if (TREE_CODE (node) == FUNCTION_DECL
7502 || (VAR_P (node) && TREE_STATIC (node))
7503 || (TREE_CODE (node) == TYPE_DECL))
7505 TREE_USED (node) = 1;
7506 DECL_PRESERVE_P (node) = 1;
7508 DECL_READ_P (node) = 1;
7512 warning (OPT_Wattributes, "%qE attribute ignored", name);
7513 *no_add_attrs = true;
7519 /* Handle a "unused" attribute; arguments as in
7520 struct attribute_spec.handler. */
7523 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7524 int flags, bool *no_add_attrs)
7530 if (TREE_CODE (decl) == PARM_DECL
7531 || VAR_OR_FUNCTION_DECL_P (decl)
7532 || TREE_CODE (decl) == LABEL_DECL
7533 || TREE_CODE (decl) == TYPE_DECL)
7535 TREE_USED (decl) = 1;
7536 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
7537 DECL_READ_P (decl) = 1;
7541 warning (OPT_Wattributes, "%qE attribute ignored", name);
7542 *no_add_attrs = true;
7547 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7548 *node = build_variant_type_copy (*node);
7549 TREE_USED (*node) = 1;
7555 /* Handle a "externally_visible" attribute; arguments as in
7556 struct attribute_spec.handler. */
7559 handle_externally_visible_attribute (tree *pnode, tree name,
7560 tree ARG_UNUSED (args),
7561 int ARG_UNUSED (flags),
7566 if (VAR_OR_FUNCTION_DECL_P (node))
7568 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7569 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7571 warning (OPT_Wattributes,
7572 "%qE attribute have effect only on public objects", name);
7573 *no_add_attrs = true;
7578 warning (OPT_Wattributes, "%qE attribute ignored", name);
7579 *no_add_attrs = true;
7585 /* Handle the "no_reorder" attribute. Arguments as in
7586 struct attribute_spec.handler. */
7589 handle_no_reorder_attribute (tree *pnode,
7597 if (!VAR_OR_FUNCTION_DECL_P (node)
7598 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7600 warning (OPT_Wattributes,
7601 "%qE attribute only affects top level objects",
7603 *no_add_attrs = true;
7609 /* Handle a "const" attribute; arguments as in
7610 struct attribute_spec.handler. */
7613 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7614 int ARG_UNUSED (flags), bool *no_add_attrs)
7616 tree type = TREE_TYPE (*node);
7618 /* See FIXME comment on noreturn in c_common_attribute_table. */
7619 if (TREE_CODE (*node) == FUNCTION_DECL)
7620 TREE_READONLY (*node) = 1;
7621 else if (TREE_CODE (type) == POINTER_TYPE
7622 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7624 = (build_qualified_type
7626 (build_type_variant (TREE_TYPE (type), 1,
7627 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7628 TYPE_QUALS (type)));
7631 warning (OPT_Wattributes, "%qE attribute ignored", name);
7632 *no_add_attrs = true;
7638 /* Handle a "transparent_union" attribute; arguments as in
7639 struct attribute_spec.handler. */
7642 handle_transparent_union_attribute (tree *node, tree name,
7643 tree ARG_UNUSED (args), int flags,
7648 *no_add_attrs = true;
7651 if (TREE_CODE (*node) == TYPE_DECL
7652 && ! (flags & ATTR_FLAG_CXX11))
7653 node = &TREE_TYPE (*node);
7656 if (TREE_CODE (type) == UNION_TYPE)
7658 /* Make sure that the first field will work for a transparent union.
7659 If the type isn't complete yet, leave the check to the code in
7661 if (TYPE_SIZE (type))
7663 tree first = first_field (type);
7664 if (first == NULL_TREE
7665 || DECL_ARTIFICIAL (first)
7666 || TYPE_MODE (type) != DECL_MODE (first))
7670 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7672 /* If the type isn't complete yet, setting the flag
7673 on a variant wouldn't ever be checked. */
7674 if (!TYPE_SIZE (type))
7677 /* build_duplicate_type doesn't work for C++. */
7678 if (c_dialect_cxx ())
7681 /* A type variant isn't good enough, since we don't a cast
7682 to such a type removed as a no-op. */
7683 *node = type = build_duplicate_type (type);
7686 TYPE_TRANSPARENT_AGGR (type) = 1;
7691 warning (OPT_Wattributes, "%qE attribute ignored", name);
7695 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7696 get the requested priority for a constructor or destructor,
7697 possibly issuing diagnostics for invalid or reserved
7700 static priority_type
7701 get_priority (tree args, bool is_destructor)
7707 return DEFAULT_INIT_PRIORITY;
7709 if (!SUPPORTS_INIT_PRIORITY)
7712 error ("destructor priorities are not supported");
7714 error ("constructor priorities are not supported");
7715 return DEFAULT_INIT_PRIORITY;
7718 arg = TREE_VALUE (args);
7719 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7721 if (arg == error_mark_node)
7722 return DEFAULT_INIT_PRIORITY;
7723 arg = default_conversion (arg);
7724 if (!tree_fits_shwi_p (arg)
7725 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7728 pri = tree_to_shwi (arg);
7729 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7732 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7736 "destructor priorities from 0 to %d are reserved "
7737 "for the implementation",
7738 MAX_RESERVED_INIT_PRIORITY);
7741 "constructor priorities from 0 to %d are reserved "
7742 "for the implementation",
7743 MAX_RESERVED_INIT_PRIORITY);
7749 error ("destructor priorities must be integers from 0 to %d inclusive",
7752 error ("constructor priorities must be integers from 0 to %d inclusive",
7754 return DEFAULT_INIT_PRIORITY;
7757 /* Handle a "constructor" attribute; arguments as in
7758 struct attribute_spec.handler. */
7761 handle_constructor_attribute (tree *node, tree name, tree args,
7762 int ARG_UNUSED (flags),
7766 tree type = TREE_TYPE (decl);
7768 if (TREE_CODE (decl) == FUNCTION_DECL
7769 && TREE_CODE (type) == FUNCTION_TYPE
7770 && decl_function_context (decl) == 0)
7772 priority_type priority;
7773 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7774 priority = get_priority (args, /*is_destructor=*/false);
7775 SET_DECL_INIT_PRIORITY (decl, priority);
7776 TREE_USED (decl) = 1;
7780 warning (OPT_Wattributes, "%qE attribute ignored", name);
7781 *no_add_attrs = true;
7787 /* Handle a "destructor" attribute; arguments as in
7788 struct attribute_spec.handler. */
7791 handle_destructor_attribute (tree *node, tree name, tree args,
7792 int ARG_UNUSED (flags),
7796 tree type = TREE_TYPE (decl);
7798 if (TREE_CODE (decl) == FUNCTION_DECL
7799 && TREE_CODE (type) == FUNCTION_TYPE
7800 && decl_function_context (decl) == 0)
7802 priority_type priority;
7803 DECL_STATIC_DESTRUCTOR (decl) = 1;
7804 priority = get_priority (args, /*is_destructor=*/true);
7805 SET_DECL_FINI_PRIORITY (decl, priority);
7806 TREE_USED (decl) = 1;
7810 warning (OPT_Wattributes, "%qE attribute ignored", name);
7811 *no_add_attrs = true;
7817 /* Nonzero if the mode is a valid vector mode for this architecture.
7818 This returns nonzero even if there is no hardware support for the
7819 vector mode, but we can emulate with narrower modes. */
7822 vector_mode_valid_p (machine_mode mode)
7824 enum mode_class mclass = GET_MODE_CLASS (mode);
7825 machine_mode innermode;
7827 /* Doh! What's going on? */
7828 if (mclass != MODE_VECTOR_INT
7829 && mclass != MODE_VECTOR_FLOAT
7830 && mclass != MODE_VECTOR_FRACT
7831 && mclass != MODE_VECTOR_UFRACT
7832 && mclass != MODE_VECTOR_ACCUM
7833 && mclass != MODE_VECTOR_UACCUM)
7836 /* Hardware support. Woo hoo! */
7837 if (targetm.vector_mode_supported_p (mode))
7840 innermode = GET_MODE_INNER (mode);
7842 /* We should probably return 1 if requesting V4DI and we have no DI,
7843 but we have V2DI, but this is probably very unlikely. */
7845 /* If we have support for the inner mode, we can safely emulate it.
7846 We may not have V2DI, but me can emulate with a pair of DIs. */
7847 return targetm.scalar_mode_supported_p (innermode);
7851 /* Handle a "mode" attribute; arguments as in
7852 struct attribute_spec.handler. */
7855 handle_mode_attribute (tree *node, tree name, tree args,
7856 int ARG_UNUSED (flags), bool *no_add_attrs)
7859 tree ident = TREE_VALUE (args);
7861 *no_add_attrs = true;
7863 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7864 warning (OPT_Wattributes, "%qE attribute ignored", name);
7868 const char *p = IDENTIFIER_POINTER (ident);
7869 int len = strlen (p);
7870 machine_mode mode = VOIDmode;
7874 if (len > 4 && p[0] == '_' && p[1] == '_'
7875 && p[len - 1] == '_' && p[len - 2] == '_')
7877 char *newp = (char *) alloca (len - 1);
7879 strcpy (newp, &p[2]);
7880 newp[len - 4] = '\0';
7884 /* Change this type to have a type with the specified mode.
7885 First check for the special modes. */
7886 if (!strcmp (p, "byte"))
7888 else if (!strcmp (p, "word"))
7890 else if (!strcmp (p, "pointer"))
7892 else if (!strcmp (p, "libgcc_cmp_return"))
7893 mode = targetm.libgcc_cmp_return_mode ();
7894 else if (!strcmp (p, "libgcc_shift_count"))
7895 mode = targetm.libgcc_shift_count_mode ();
7896 else if (!strcmp (p, "unwind_word"))
7897 mode = targetm.unwind_word_mode ();
7899 for (j = 0; j < NUM_MACHINE_MODES; j++)
7900 if (!strcmp (p, GET_MODE_NAME (j)))
7902 mode = (machine_mode) j;
7906 if (mode == VOIDmode)
7908 error ("unknown machine mode %qE", ident);
7913 switch (GET_MODE_CLASS (mode))
7916 case MODE_PARTIAL_INT:
7918 case MODE_DECIMAL_FLOAT:
7923 valid_mode = targetm.scalar_mode_supported_p (mode);
7926 case MODE_COMPLEX_INT:
7927 case MODE_COMPLEX_FLOAT:
7928 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7931 case MODE_VECTOR_INT:
7932 case MODE_VECTOR_FLOAT:
7933 case MODE_VECTOR_FRACT:
7934 case MODE_VECTOR_UFRACT:
7935 case MODE_VECTOR_ACCUM:
7936 case MODE_VECTOR_UACCUM:
7937 warning (OPT_Wattributes, "specifying vector types with "
7938 "__attribute__ ((mode)) is deprecated");
7939 warning (OPT_Wattributes,
7940 "use __attribute__ ((vector_size)) instead");
7941 valid_mode = vector_mode_valid_p (mode);
7949 error ("unable to emulate %qs", p);
7953 if (POINTER_TYPE_P (type))
7955 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7956 tree (*fn)(tree, machine_mode, bool);
7958 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7960 error ("invalid pointer mode %qs", p);
7964 if (TREE_CODE (type) == POINTER_TYPE)
7965 fn = build_pointer_type_for_mode;
7967 fn = build_reference_type_for_mode;
7968 typefm = fn (TREE_TYPE (type), mode, false);
7972 /* For fixed-point modes, we need to test if the signness of type
7973 and the machine mode are consistent. */
7974 if (ALL_FIXED_POINT_MODE_P (mode)
7975 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7977 error ("signedness of type and machine mode %qs don%'t match", p);
7980 /* For fixed-point modes, we need to pass saturating info. */
7981 typefm = lang_hooks.types.type_for_mode (mode,
7982 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7983 : TYPE_UNSIGNED (type));
7986 if (typefm == NULL_TREE)
7988 error ("no data type for mode %qs", p);
7991 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7993 /* For enumeral types, copy the precision from the integer
7994 type returned above. If not an INTEGER_TYPE, we can't use
7995 this mode for this type. */
7996 if (TREE_CODE (typefm) != INTEGER_TYPE)
7998 error ("cannot use mode %qs for enumeral types", p);
8002 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
8004 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
8009 /* We cannot build a type variant, as there's code that assumes
8010 that TYPE_MAIN_VARIANT has the same mode. This includes the
8011 debug generators. Instead, create a subrange type. This
8012 results in all of the enumeral values being emitted only once
8013 in the original, and the subtype gets them by reference. */
8014 if (TYPE_UNSIGNED (type))
8015 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
8017 typefm = make_signed_type (TYPE_PRECISION (typefm));
8018 TREE_TYPE (typefm) = type;
8021 else if (VECTOR_MODE_P (mode)
8022 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
8023 : TREE_CODE (type) != TREE_CODE (typefm))
8025 error ("mode %qs applied to inappropriate type", p);
8035 /* Handle a "section" attribute; arguments as in
8036 struct attribute_spec.handler. */
8039 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8040 int ARG_UNUSED (flags), bool *no_add_attrs)
8044 if (!targetm_common.have_named_sections)
8046 error_at (DECL_SOURCE_LOCATION (*node),
8047 "section attributes are not supported for this target");
8051 user_defined_section_attribute = true;
8053 if (!VAR_OR_FUNCTION_DECL_P (decl))
8055 error ("section attribute not allowed for %q+D", *node);
8059 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8061 error ("section attribute argument not a string constant");
8066 && current_function_decl != NULL_TREE
8067 && !TREE_STATIC (decl))
8069 error_at (DECL_SOURCE_LOCATION (decl),
8070 "section attribute cannot be specified for local variables");
8074 /* The decl may have already been given a section attribute
8075 from a previous declaration. Ensure they match. */
8076 if (DECL_SECTION_NAME (decl) != NULL
8077 && strcmp (DECL_SECTION_NAME (decl),
8078 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
8080 error ("section of %q+D conflicts with previous declaration", *node);
8085 && !targetm.have_tls && targetm.emutls.tmpl_section
8086 && DECL_THREAD_LOCAL_P (decl))
8088 error ("section of %q+D cannot be overridden", *node);
8092 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
8096 *no_add_attrs = true;
8100 /* Check whether ALIGN is a valid user-specified alignment. If so,
8101 return its base-2 log; if not, output an error and return -1. If
8102 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
8105 check_user_alignment (const_tree align, bool allow_zero)
8109 if (error_operand_p (align))
8111 if (TREE_CODE (align) != INTEGER_CST
8112 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
8114 error ("requested alignment is not an integer constant");
8117 else if (allow_zero && integer_zerop (align))
8119 else if (tree_int_cst_sgn (align) == -1
8120 || (i = tree_log2 (align)) == -1)
8122 error ("requested alignment is not a positive power of 2");
8125 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
8127 error ("requested alignment is too large");
8134 If in c++-11, check if the c++-11 alignment constraint with respect
8135 to fundamental alignment (in [dcl.align]) are satisfied. If not in
8136 c++-11 mode, does nothing.
8140 [* if the constant expression evaluates to a fundamental alignment,
8141 the alignment requirement of the declared entity shall be the
8142 specified fundamental alignment.
8144 * if the constant expression evaluates to an extended alignment
8145 and the implementation supports that alignment in the context
8146 of the declaration, the alignment of the declared entity shall
8149 * if the constant expression evaluates to an extended alignment
8150 and the implementation does not support that alignment in the
8151 context of the declaration, the program is ill-formed]. */
8154 check_cxx_fundamental_alignment_constraints (tree node,
8158 bool alignment_too_large_p = false;
8159 unsigned requested_alignment = 1U << align_log;
8160 unsigned max_align = 0;
8162 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8163 || (node == NULL_TREE || node == error_mark_node))
8166 if (cxx_fundamental_alignment_p (requested_alignment))
8171 if (TREE_STATIC (node))
8173 /* For file scope variables and static members, the target
8174 supports alignments that are at most
8175 MAX_OFILE_ALIGNMENT. */
8176 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8177 alignment_too_large_p = true;
8181 #ifdef BIGGEST_FIELD_ALIGNMENT
8182 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8184 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8186 /* For non-static members, the target supports either
8187 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8188 if it is defined or BIGGEST_ALIGNMENT. */
8189 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8190 if (TREE_CODE (node) == FIELD_DECL
8191 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8192 alignment_too_large_p = true;
8193 #undef MAX_TARGET_FIELD_ALIGNMENT
8194 /* For stack variables, the target supports at most
8195 MAX_STACK_ALIGNMENT. */
8196 else if (decl_function_context (node) != NULL
8197 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8198 alignment_too_large_p = true;
8201 else if (TYPE_P (node))
8203 /* Let's be liberal for types. */
8204 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8205 alignment_too_large_p = true;
8208 if (alignment_too_large_p)
8209 pedwarn (input_location, OPT_Wattributes,
8210 "requested alignment %d is larger than %d",
8211 requested_alignment, max_align);
8213 return !alignment_too_large_p;
8216 /* Handle a "aligned" attribute; arguments as in
8217 struct attribute_spec.handler. */
8220 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8221 int flags, bool *no_add_attrs)
8223 tree decl = NULL_TREE;
8231 align_expr = TREE_VALUE (args);
8232 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8233 && TREE_CODE (align_expr) != FUNCTION_DECL)
8234 align_expr = default_conversion (align_expr);
8237 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8242 type = &TREE_TYPE (decl);
8243 is_type = TREE_CODE (*node) == TYPE_DECL;
8245 else if (TYPE_P (*node))
8246 type = node, is_type = 1;
8248 if ((i = check_user_alignment (align_expr, false)) == -1
8249 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
8250 *no_add_attrs = true;
8253 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8254 /* OK, modify the type in place. */;
8255 /* If we have a TYPE_DECL, then copy the type, so that we
8256 don't accidentally modify a builtin type. See pushdecl. */
8257 else if (decl && TREE_TYPE (decl) != error_mark_node
8258 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
8260 tree tt = TREE_TYPE (decl);
8261 *type = build_variant_type_copy (*type);
8262 DECL_ORIGINAL_TYPE (decl) = tt;
8263 TYPE_NAME (*type) = decl;
8264 TREE_USED (*type) = TREE_USED (decl);
8265 TREE_TYPE (decl) = *type;
8268 *type = build_variant_type_copy (*type);
8270 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
8271 TYPE_USER_ALIGN (*type) = 1;
8273 else if (! VAR_OR_FUNCTION_DECL_P (decl)
8274 && TREE_CODE (decl) != FIELD_DECL)
8276 error ("alignment may not be specified for %q+D", decl);
8277 *no_add_attrs = true;
8279 else if (DECL_USER_ALIGN (decl)
8280 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8281 /* C++-11 [dcl.align/4]:
8283 When multiple alignment-specifiers are specified for an
8284 entity, the alignment requirement shall be set to the
8285 strictest specified alignment.
8287 This formally comes from the c++11 specification but we are
8288 doing it for the GNU attribute syntax as well. */
8289 *no_add_attrs = true;
8290 else if (TREE_CODE (decl) == FUNCTION_DECL
8291 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8293 if (DECL_USER_ALIGN (decl))
8294 error ("alignment for %q+D was previously specified as %d "
8295 "and may not be decreased", decl,
8296 DECL_ALIGN (decl) / BITS_PER_UNIT);
8298 error ("alignment for %q+D must be at least %d", decl,
8299 DECL_ALIGN (decl) / BITS_PER_UNIT);
8300 *no_add_attrs = true;
8304 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
8305 DECL_USER_ALIGN (decl) = 1;
8311 /* Handle a "weak" attribute; arguments as in
8312 struct attribute_spec.handler. */
8315 handle_weak_attribute (tree *node, tree name,
8316 tree ARG_UNUSED (args),
8317 int ARG_UNUSED (flags),
8318 bool * ARG_UNUSED (no_add_attrs))
8320 if (TREE_CODE (*node) == FUNCTION_DECL
8321 && DECL_DECLARED_INLINE_P (*node))
8323 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
8324 *no_add_attrs = true;
8326 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8328 error ("indirect function %q+D cannot be declared weak", *node);
8329 *no_add_attrs = true;
8332 else if (VAR_OR_FUNCTION_DECL_P (*node))
8333 declare_weak (*node);
8335 warning (OPT_Wattributes, "%qE attribute ignored", name);
8340 /* Handle a "noplt" attribute; arguments as in
8341 struct attribute_spec.handler. */
8344 handle_noplt_attribute (tree *node, tree name,
8345 tree ARG_UNUSED (args),
8346 int ARG_UNUSED (flags),
8347 bool * ARG_UNUSED (no_add_attrs))
8349 if (TREE_CODE (*node) != FUNCTION_DECL)
8351 warning (OPT_Wattributes,
8352 "%qE attribute is only applicable on functions", name);
8353 *no_add_attrs = true;
8359 /* Handle an "alias" or "ifunc" attribute; arguments as in
8360 struct attribute_spec.handler, except that IS_ALIAS tells us
8361 whether this is an alias as opposed to ifunc attribute. */
8364 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8369 if (TREE_CODE (decl) != FUNCTION_DECL
8370 && (!is_alias || !VAR_P (decl)))
8372 warning (OPT_Wattributes, "%qE attribute ignored", name);
8373 *no_add_attrs = true;
8375 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
8376 || (TREE_CODE (decl) != FUNCTION_DECL
8377 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8378 /* A static variable declaration is always a tentative definition,
8379 but the alias is a non-tentative definition which overrides. */
8380 || (TREE_CODE (decl) != FUNCTION_DECL
8381 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
8383 error ("%q+D defined both normally and as %qE attribute", decl, name);
8384 *no_add_attrs = true;
8388 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8389 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8391 error ("weak %q+D cannot be defined %qE", decl, name);
8392 *no_add_attrs = true;
8396 /* Note that the very first time we process a nested declaration,
8397 decl_function_context will not be set. Indeed, *would* never
8398 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8399 we do below. After such frobbery, pushdecl would set the context.
8400 In any case, this is never what we want. */
8401 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
8405 id = TREE_VALUE (args);
8406 if (TREE_CODE (id) != STRING_CST)
8408 error ("attribute %qE argument not a string", name);
8409 *no_add_attrs = true;
8412 id = get_identifier (TREE_STRING_POINTER (id));
8413 /* This counts as a use of the object pointed to. */
8416 if (TREE_CODE (decl) == FUNCTION_DECL)
8417 DECL_INITIAL (decl) = error_mark_node;
8419 TREE_STATIC (decl) = 1;
8422 /* ifuncs are also aliases, so set that attribute too. */
8423 DECL_ATTRIBUTES (decl)
8424 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8428 warning (OPT_Wattributes, "%qE attribute ignored", name);
8429 *no_add_attrs = true;
8432 if (decl_in_symtab_p (*node))
8434 struct symtab_node *n = symtab_node::get (decl);
8435 if (n && n->refuse_visibility_changes)
8438 error ("%+D declared alias after being used", decl);
8440 error ("%+D declared ifunc after being used", decl);
8448 /* Handle an "alias" or "ifunc" attribute; arguments as in
8449 struct attribute_spec.handler. */
8452 handle_ifunc_attribute (tree *node, tree name, tree args,
8453 int ARG_UNUSED (flags), bool *no_add_attrs)
8455 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8458 /* Handle an "alias" or "ifunc" attribute; arguments as in
8459 struct attribute_spec.handler. */
8462 handle_alias_attribute (tree *node, tree name, tree args,
8463 int ARG_UNUSED (flags), bool *no_add_attrs)
8465 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8468 /* Handle a "weakref" attribute; arguments as in struct
8469 attribute_spec.handler. */
8472 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8473 int flags, bool *no_add_attrs)
8475 tree attr = NULL_TREE;
8477 /* We must ignore the attribute when it is associated with
8478 local-scoped decls, since attribute alias is ignored and many
8479 such symbols do not even have a DECL_WEAK field. */
8480 if (decl_function_context (*node)
8481 || current_function_decl
8482 || !VAR_OR_FUNCTION_DECL_P (*node))
8484 warning (OPT_Wattributes, "%qE attribute ignored", name);
8485 *no_add_attrs = true;
8489 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8491 error ("indirect function %q+D cannot be declared weakref", *node);
8492 *no_add_attrs = true;
8496 /* The idea here is that `weakref("name")' mutates into `weakref,
8497 alias("name")', and weakref without arguments, in turn,
8498 implicitly adds weak. */
8502 attr = tree_cons (get_identifier ("alias"), args, attr);
8503 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8505 *no_add_attrs = true;
8507 decl_attributes (node, attr, flags);
8511 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8512 error_at (DECL_SOURCE_LOCATION (*node),
8513 "weakref attribute must appear before alias attribute");
8515 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8516 and that isn't supported; and because it wants to add it to
8517 the list of weak decls, which isn't helpful. */
8518 DECL_WEAK (*node) = 1;
8521 if (decl_in_symtab_p (*node))
8523 struct symtab_node *n = symtab_node::get (*node);
8524 if (n && n->refuse_visibility_changes)
8525 error ("%+D declared weakref after being used", *node);
8531 /* Handle an "visibility" attribute; arguments as in
8532 struct attribute_spec.handler. */
8535 handle_visibility_attribute (tree *node, tree name, tree args,
8536 int ARG_UNUSED (flags),
8537 bool *ARG_UNUSED (no_add_attrs))
8540 tree id = TREE_VALUE (args);
8541 enum symbol_visibility vis;
8545 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8547 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8549 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8553 else if (TYPE_FIELDS (*node))
8555 error ("%qE attribute ignored because %qT is already defined",
8560 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8562 warning (OPT_Wattributes, "%qE attribute ignored", name);
8566 if (TREE_CODE (id) != STRING_CST)
8568 error ("visibility argument not a string");
8572 /* If this is a type, set the visibility on the type decl. */
8575 decl = TYPE_NAME (decl);
8578 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8580 warning (OPT_Wattributes, "%qE attribute ignored on types",
8586 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8587 vis = VISIBILITY_DEFAULT;
8588 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8589 vis = VISIBILITY_INTERNAL;
8590 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8591 vis = VISIBILITY_HIDDEN;
8592 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8593 vis = VISIBILITY_PROTECTED;
8596 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8597 vis = VISIBILITY_DEFAULT;
8600 if (DECL_VISIBILITY_SPECIFIED (decl)
8601 && vis != DECL_VISIBILITY (decl))
8603 tree attributes = (TYPE_P (*node)
8604 ? TYPE_ATTRIBUTES (*node)
8605 : DECL_ATTRIBUTES (decl));
8606 if (lookup_attribute ("visibility", attributes))
8607 error ("%qD redeclared with different visibility", decl);
8608 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8609 && lookup_attribute ("dllimport", attributes))
8610 error ("%qD was declared %qs which implies default visibility",
8612 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8613 && lookup_attribute ("dllexport", attributes))
8614 error ("%qD was declared %qs which implies default visibility",
8618 DECL_VISIBILITY (decl) = vis;
8619 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8621 /* Go ahead and attach the attribute to the node as well. This is needed
8622 so we can determine whether we have VISIBILITY_DEFAULT because the
8623 visibility was not specified, or because it was explicitly overridden
8624 from the containing scope. */
8629 /* Determine the ELF symbol visibility for DECL, which is either a
8630 variable or a function. It is an error to use this function if a
8631 definition of DECL is not available in this translation unit.
8632 Returns true if the final visibility has been determined by this
8633 function; false if the caller is free to make additional
8637 c_determine_visibility (tree decl)
8639 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8641 /* If the user explicitly specified the visibility with an
8642 attribute, honor that. DECL_VISIBILITY will have been set during
8643 the processing of the attribute. We check for an explicit
8644 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8645 to distinguish the use of an attribute from the use of a "#pragma
8646 GCC visibility push(...)"; in the latter case we still want other
8647 considerations to be able to overrule the #pragma. */
8648 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8649 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8650 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8651 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8654 /* Set default visibility to whatever the user supplied with
8655 visibility_specified depending on #pragma GCC visibility. */
8656 if (!DECL_VISIBILITY_SPECIFIED (decl))
8658 if (visibility_options.inpragma
8659 || DECL_VISIBILITY (decl) != default_visibility)
8661 DECL_VISIBILITY (decl) = default_visibility;
8662 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8663 /* If visibility changed and DECL already has DECL_RTL, ensure
8664 symbol flags are updated. */
8665 if (((VAR_P (decl) && TREE_STATIC (decl))
8666 || TREE_CODE (decl) == FUNCTION_DECL)
8667 && DECL_RTL_SET_P (decl))
8668 make_decl_rtl (decl);
8674 /* Handle an "tls_model" attribute; arguments as in
8675 struct attribute_spec.handler. */
8678 handle_tls_model_attribute (tree *node, tree name, tree args,
8679 int ARG_UNUSED (flags), bool *no_add_attrs)
8683 enum tls_model kind;
8685 *no_add_attrs = true;
8687 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
8689 warning (OPT_Wattributes, "%qE attribute ignored", name);
8693 kind = DECL_TLS_MODEL (decl);
8694 id = TREE_VALUE (args);
8695 if (TREE_CODE (id) != STRING_CST)
8697 error ("tls_model argument not a string");
8701 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8702 kind = TLS_MODEL_LOCAL_EXEC;
8703 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8704 kind = TLS_MODEL_INITIAL_EXEC;
8705 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8706 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8707 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8708 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8710 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8712 set_decl_tls_model (decl, kind);
8716 /* Handle a "no_instrument_function" attribute; arguments as in
8717 struct attribute_spec.handler. */
8720 handle_no_instrument_function_attribute (tree *node, tree name,
8721 tree ARG_UNUSED (args),
8722 int ARG_UNUSED (flags),
8727 if (TREE_CODE (decl) != FUNCTION_DECL)
8729 error_at (DECL_SOURCE_LOCATION (decl),
8730 "%qE attribute applies only to functions", name);
8731 *no_add_attrs = true;
8734 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8739 /* Handle a "malloc" attribute; arguments as in
8740 struct attribute_spec.handler. */
8743 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8744 int ARG_UNUSED (flags), bool *no_add_attrs)
8746 if (TREE_CODE (*node) == FUNCTION_DECL
8747 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8748 DECL_IS_MALLOC (*node) = 1;
8751 warning (OPT_Wattributes, "%qE attribute ignored", name);
8752 *no_add_attrs = true;
8758 /* Handle a "alloc_size" attribute; arguments as in
8759 struct attribute_spec.handler. */
8762 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8763 int ARG_UNUSED (flags), bool *no_add_attrs)
8765 unsigned arg_count = type_num_arguments (*node);
8766 for (; args; args = TREE_CHAIN (args))
8768 tree position = TREE_VALUE (args);
8769 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8770 && TREE_CODE (position) != FUNCTION_DECL)
8771 position = default_conversion (position);
8773 if (!tree_fits_uhwi_p (position)
8775 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8777 warning (OPT_Wattributes,
8778 "alloc_size parameter outside range");
8779 *no_add_attrs = true;
8786 /* Handle a "alloc_align" attribute; arguments as in
8787 struct attribute_spec.handler. */
8790 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8793 unsigned arg_count = type_num_arguments (*node);
8794 tree position = TREE_VALUE (args);
8795 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8796 position = default_conversion (position);
8798 if (!tree_fits_uhwi_p (position)
8800 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8802 warning (OPT_Wattributes,
8803 "alloc_align parameter outside range");
8804 *no_add_attrs = true;
8810 /* Handle a "assume_aligned" attribute; arguments as in
8811 struct attribute_spec.handler. */
8814 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8817 for (; args; args = TREE_CHAIN (args))
8819 tree position = TREE_VALUE (args);
8820 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8821 && TREE_CODE (position) != FUNCTION_DECL)
8822 position = default_conversion (position);
8824 if (TREE_CODE (position) != INTEGER_CST)
8826 warning (OPT_Wattributes,
8827 "assume_aligned parameter not integer constant");
8828 *no_add_attrs = true;
8835 /* Handle a "fn spec" attribute; arguments as in
8836 struct attribute_spec.handler. */
8839 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8840 tree args, int ARG_UNUSED (flags),
8841 bool *no_add_attrs ATTRIBUTE_UNUSED)
8844 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8845 && !TREE_CHAIN (args));
8849 /* Handle a "bnd_variable_size" attribute; arguments as in
8850 struct attribute_spec.handler. */
8853 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8854 int ARG_UNUSED (flags), bool *no_add_attrs)
8856 if (TREE_CODE (*node) != FIELD_DECL)
8858 warning (OPT_Wattributes, "%qE attribute ignored", name);
8859 *no_add_attrs = true;
8865 /* Handle a "bnd_legacy" attribute; arguments as in
8866 struct attribute_spec.handler. */
8869 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8870 int ARG_UNUSED (flags), bool *no_add_attrs)
8872 if (TREE_CODE (*node) != FUNCTION_DECL)
8874 warning (OPT_Wattributes, "%qE attribute ignored", name);
8875 *no_add_attrs = true;
8881 /* Handle a "bnd_instrument" attribute; arguments as in
8882 struct attribute_spec.handler. */
8885 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8886 int ARG_UNUSED (flags), bool *no_add_attrs)
8888 if (TREE_CODE (*node) != FUNCTION_DECL)
8890 warning (OPT_Wattributes, "%qE attribute ignored", name);
8891 *no_add_attrs = true;
8897 /* Handle a "warn_unused" attribute; arguments as in
8898 struct attribute_spec.handler. */
8901 handle_warn_unused_attribute (tree *node, tree name,
8902 tree args ATTRIBUTE_UNUSED,
8903 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8906 /* Do nothing else, just set the attribute. We'll get at
8907 it later with lookup_attribute. */
8911 warning (OPT_Wattributes, "%qE attribute ignored", name);
8912 *no_add_attrs = true;
8918 /* Handle an "omp declare simd" attribute; arguments as in
8919 struct attribute_spec.handler. */
8922 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8927 /* Handle an "omp declare target" attribute; arguments as in
8928 struct attribute_spec.handler. */
8931 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8936 /* Handle a "returns_twice" attribute; arguments as in
8937 struct attribute_spec.handler. */
8940 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8941 int ARG_UNUSED (flags), bool *no_add_attrs)
8943 if (TREE_CODE (*node) == FUNCTION_DECL)
8944 DECL_IS_RETURNS_TWICE (*node) = 1;
8947 warning (OPT_Wattributes, "%qE attribute ignored", name);
8948 *no_add_attrs = true;
8954 /* Handle a "no_limit_stack" attribute; arguments as in
8955 struct attribute_spec.handler. */
8958 handle_no_limit_stack_attribute (tree *node, tree name,
8959 tree ARG_UNUSED (args),
8960 int ARG_UNUSED (flags),
8965 if (TREE_CODE (decl) != FUNCTION_DECL)
8967 error_at (DECL_SOURCE_LOCATION (decl),
8968 "%qE attribute applies only to functions", name);
8969 *no_add_attrs = true;
8971 else if (DECL_INITIAL (decl))
8973 error_at (DECL_SOURCE_LOCATION (decl),
8974 "can%'t set %qE attribute after definition", name);
8975 *no_add_attrs = true;
8978 DECL_NO_LIMIT_STACK (decl) = 1;
8983 /* Handle a "pure" attribute; arguments as in
8984 struct attribute_spec.handler. */
8987 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8988 int ARG_UNUSED (flags), bool *no_add_attrs)
8990 if (TREE_CODE (*node) == FUNCTION_DECL)
8991 DECL_PURE_P (*node) = 1;
8992 /* ??? TODO: Support types. */
8995 warning (OPT_Wattributes, "%qE attribute ignored", name);
8996 *no_add_attrs = true;
9002 /* Digest an attribute list destined for a transactional memory statement.
9003 ALLOWED is the set of attributes that are allowed for this statement;
9004 return the attribute we parsed. Multiple attributes are never allowed. */
9007 parse_tm_stmt_attr (tree attrs, int allowed)
9012 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
9014 tree a = TREE_PURPOSE (attrs);
9017 if (is_attribute_p ("outer", a))
9018 m = TM_STMT_ATTR_OUTER;
9020 if ((m & allowed) == 0)
9022 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
9031 else if (m_seen == m)
9032 warning (OPT_Wattributes, "%qE attribute duplicated", a);
9034 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
9040 /* Transform a TM attribute name into a maskable integer and back.
9041 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
9042 to how the lack of an attribute is treated. */
9045 tm_attr_to_mask (tree attr)
9049 if (is_attribute_p ("transaction_safe", attr))
9050 return TM_ATTR_SAFE;
9051 if (is_attribute_p ("transaction_callable", attr))
9052 return TM_ATTR_CALLABLE;
9053 if (is_attribute_p ("transaction_pure", attr))
9054 return TM_ATTR_PURE;
9055 if (is_attribute_p ("transaction_unsafe", attr))
9056 return TM_ATTR_IRREVOCABLE;
9057 if (is_attribute_p ("transaction_may_cancel_outer", attr))
9058 return TM_ATTR_MAY_CANCEL_OUTER;
9063 tm_mask_to_attr (int mask)
9069 str = "transaction_safe";
9071 case TM_ATTR_CALLABLE:
9072 str = "transaction_callable";
9075 str = "transaction_pure";
9077 case TM_ATTR_IRREVOCABLE:
9078 str = "transaction_unsafe";
9080 case TM_ATTR_MAY_CANCEL_OUTER:
9081 str = "transaction_may_cancel_outer";
9086 return get_identifier (str);
9089 /* Return the first TM attribute seen in LIST. */
9092 find_tm_attribute (tree list)
9094 for (; list ; list = TREE_CHAIN (list))
9096 tree name = TREE_PURPOSE (list);
9097 if (tm_attr_to_mask (name) != 0)
9103 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
9104 Here we accept only function types, and verify that none of the other
9105 function TM attributes are also applied. */
9106 /* ??? We need to accept class types for C++, but not C. This greatly
9107 complicates this function, since we can no longer rely on the extra
9108 processing given by function_type_required. */
9111 handle_tm_attribute (tree *node, tree name, tree args,
9112 int flags, bool *no_add_attrs)
9114 /* Only one path adds the attribute; others don't. */
9115 *no_add_attrs = true;
9117 switch (TREE_CODE (*node))
9121 /* Only tm_callable and tm_safe apply to classes. */
9122 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
9129 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
9130 if (old_name == name)
9132 else if (old_name != NULL_TREE)
9133 error ("type was previously declared %qE", old_name);
9135 *no_add_attrs = false;
9141 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
9142 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
9144 tree fn_tmp = TREE_TYPE (*node);
9145 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
9146 *node = build_pointer_type (fn_tmp);
9153 /* If a function is next, pass it on to be tried next. */
9154 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
9155 return tree_cons (name, args, NULL);
9158 warning (OPT_Wattributes, "%qE attribute ignored", name);
9165 /* Handle the TM_WRAP attribute; arguments as in
9166 struct attribute_spec.handler. */
9169 handle_tm_wrap_attribute (tree *node, tree name, tree args,
9170 int ARG_UNUSED (flags), bool *no_add_attrs)
9174 /* We don't need the attribute even on success, since we
9175 record the entry in an external table. */
9176 *no_add_attrs = true;
9178 if (TREE_CODE (decl) != FUNCTION_DECL)
9179 warning (OPT_Wattributes, "%qE attribute ignored", name);
9182 tree wrap_decl = TREE_VALUE (args);
9183 if (error_operand_p (wrap_decl))
9185 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
9186 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
9187 error ("%qE argument not an identifier", name);
9190 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9191 wrap_decl = lookup_name (wrap_decl);
9192 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9194 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9195 TREE_TYPE (wrap_decl)))
9196 record_tm_replacement (wrap_decl, decl);
9198 error ("%qD is not compatible with %qD", wrap_decl, decl);
9201 error ("%qE argument is not a function", name);
9208 /* Ignore the given attribute. Used when this attribute may be usefully
9209 overridden by the target, but is not used generically. */
9212 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9213 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9216 *no_add_attrs = true;
9220 /* Handle a "no vops" attribute; arguments as in
9221 struct attribute_spec.handler. */
9224 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9225 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9226 bool *ARG_UNUSED (no_add_attrs))
9228 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9229 DECL_IS_NOVOPS (*node) = 1;
9233 /* Handle a "deprecated" attribute; arguments as in
9234 struct attribute_spec.handler. */
9237 handle_deprecated_attribute (tree *node, tree name,
9238 tree args, int flags,
9241 tree type = NULL_TREE;
9243 tree what = NULL_TREE;
9246 *no_add_attrs = true;
9247 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9249 error ("deprecated message is not a string");
9250 *no_add_attrs = true;
9256 type = TREE_TYPE (decl);
9258 if (TREE_CODE (decl) == TYPE_DECL
9259 || TREE_CODE (decl) == PARM_DECL
9260 || VAR_OR_FUNCTION_DECL_P (decl)
9261 || TREE_CODE (decl) == FIELD_DECL
9262 || TREE_CODE (decl) == CONST_DECL
9263 || objc_method_decl (TREE_CODE (decl)))
9264 TREE_DEPRECATED (decl) = 1;
9268 else if (TYPE_P (*node))
9270 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
9271 *node = build_variant_type_copy (*node);
9272 TREE_DEPRECATED (*node) = 1;
9280 *no_add_attrs = true;
9281 if (type && TYPE_NAME (type))
9283 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
9284 what = TYPE_NAME (*node);
9285 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9286 && DECL_NAME (TYPE_NAME (type)))
9287 what = DECL_NAME (TYPE_NAME (type));
9290 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
9292 warning (OPT_Wattributes, "%qE attribute ignored", name);
9298 /* Handle a "vector_size" attribute; arguments as in
9299 struct attribute_spec.handler. */
9302 handle_vector_size_attribute (tree *node, tree name, tree args,
9303 int ARG_UNUSED (flags),
9306 unsigned HOST_WIDE_INT vecsize, nunits;
9307 machine_mode orig_mode;
9308 tree type = *node, new_type, size;
9310 *no_add_attrs = true;
9312 size = TREE_VALUE (args);
9313 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9314 && TREE_CODE (size) != FUNCTION_DECL)
9315 size = default_conversion (size);
9317 if (!tree_fits_uhwi_p (size))
9319 warning (OPT_Wattributes, "%qE attribute ignored", name);
9323 /* Get the vector size (in bytes). */
9324 vecsize = tree_to_uhwi (size);
9326 /* We need to provide for vector pointers, vector arrays, and
9327 functions returning vectors. For example:
9329 __attribute__((vector_size(16))) short *foo;
9331 In this case, the mode is SI, but the type being modified is
9332 HI, so we need to look further. */
9334 while (POINTER_TYPE_P (type)
9335 || TREE_CODE (type) == FUNCTION_TYPE
9336 || TREE_CODE (type) == METHOD_TYPE
9337 || TREE_CODE (type) == ARRAY_TYPE
9338 || TREE_CODE (type) == OFFSET_TYPE)
9339 type = TREE_TYPE (type);
9341 /* Get the mode of the type being modified. */
9342 orig_mode = TYPE_MODE (type);
9344 if ((!INTEGRAL_TYPE_P (type)
9345 && !SCALAR_FLOAT_TYPE_P (type)
9346 && !FIXED_POINT_TYPE_P (type))
9347 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9348 && GET_MODE_CLASS (orig_mode) != MODE_INT
9349 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
9350 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9351 || TREE_CODE (type) == BOOLEAN_TYPE)
9353 error ("invalid vector type for attribute %qE", name);
9357 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
9359 error ("vector size not an integral multiple of component size");
9365 error ("zero vector size");
9369 /* Calculate how many units fit in the vector. */
9370 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
9371 if (nunits & (nunits - 1))
9373 error ("number of components of the vector not a power of two");
9377 new_type = build_vector_type (type, nunits);
9379 /* Build back pointers if needed. */
9380 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9385 /* Handle the "nonnull" attribute. */
9387 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9388 tree args, int ARG_UNUSED (flags),
9392 unsigned HOST_WIDE_INT attr_arg_num;
9394 /* If no arguments are specified, all pointer arguments should be
9395 non-null. Verify a full prototype is given so that the arguments
9396 will have the correct types when we actually check them later. */
9399 if (!prototype_p (type))
9401 error ("nonnull attribute without arguments on a non-prototype");
9402 *no_add_attrs = true;
9407 /* Argument list specified. Verify that each argument number references
9408 a pointer argument. */
9409 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9411 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9413 tree arg = TREE_VALUE (args);
9414 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9415 && TREE_CODE (arg) != FUNCTION_DECL)
9416 arg = default_conversion (arg);
9418 if (!get_nonnull_operand (arg, &arg_num))
9420 error ("nonnull argument has invalid operand number (argument %lu)",
9421 (unsigned long) attr_arg_num);
9422 *no_add_attrs = true;
9426 if (prototype_p (type))
9428 function_args_iterator iter;
9431 function_args_iter_init (&iter, type);
9432 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9434 argument = function_args_iter_cond (&iter);
9435 if (argument == NULL_TREE || ck_num == arg_num)
9440 || TREE_CODE (argument) == VOID_TYPE)
9442 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9443 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9444 *no_add_attrs = true;
9448 if (TREE_CODE (argument) != POINTER_TYPE)
9450 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9451 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9452 *no_add_attrs = true;
9461 /* Check the argument list of a function call for null in argument slots
9462 that are marked as requiring a non-null pointer argument. The NARGS
9463 arguments are passed in the array ARGARRAY.
9467 check_function_nonnull (tree attrs, int nargs, tree *argarray)
9472 attrs = lookup_attribute ("nonnull", attrs);
9473 if (attrs == NULL_TREE)
9477 /* See if any of the nonnull attributes has no arguments. If so,
9478 then every pointer argument is checked (in which case the check
9479 for pointer type is done in check_nonnull_arg). */
9480 if (TREE_VALUE (a) != NULL_TREE)
9482 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9483 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9486 for (i = 0; i < nargs; i++)
9487 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9491 /* Walk the argument list. If we encounter an argument number we
9492 should check for non-null, do it. */
9493 for (i = 0; i < nargs; i++)
9495 for (a = attrs; ; a = TREE_CHAIN (a))
9497 a = lookup_attribute ("nonnull", a);
9498 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9503 check_function_arguments_recurse (check_nonnull_arg, NULL,
9504 argarray[i], i + 1);
9509 /* Check that the Nth argument of a function call (counting backwards
9510 from the end) is a (pointer)0. The NARGS arguments are passed in the
9514 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9516 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9523 function_args_iterator iter;
9526 /* Skip over the named arguments. */
9527 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9534 if (TREE_VALUE (attr))
9536 tree p = TREE_VALUE (TREE_VALUE (attr));
9537 pos = TREE_INT_CST_LOW (p);
9540 /* The sentinel must be one of the varargs, i.e.
9541 in position >= the number of fixed arguments. */
9542 if ((nargs - 1 - pos) < len)
9544 warning (OPT_Wformat_,
9545 "not enough variable arguments to fit a sentinel");
9549 /* Validate the sentinel. */
9550 sentinel = argarray[nargs - 1 - pos];
9551 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9552 || !integer_zerop (sentinel))
9553 /* Although __null (in C++) is only an integer we allow it
9554 nevertheless, as we are guaranteed that it's exactly
9555 as wide as a pointer, and we don't want to force
9556 users to cast the NULL they have written there.
9557 We warn with -Wstrict-null-sentinel, though. */
9558 && (warn_strict_null_sentinel || null_node != sentinel))
9559 warning (OPT_Wformat_, "missing sentinel in function call");
9563 /* Helper for check_function_nonnull; given a list of operands which
9564 must be non-null in ARGS, determine if operand PARAM_NUM should be
9568 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9570 unsigned HOST_WIDE_INT arg_num = 0;
9572 for (; args; args = TREE_CHAIN (args))
9574 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9578 if (arg_num == param_num)
9584 /* Check that the function argument PARAM (which is operand number
9585 PARAM_NUM) is non-null. This is called by check_function_nonnull
9586 via check_function_arguments_recurse. */
9589 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9590 unsigned HOST_WIDE_INT param_num)
9592 /* Just skip checking the argument if it's not a pointer. This can
9593 happen if the "nonnull" attribute was given without an operand
9594 list (which means to check every pointer argument). */
9596 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9599 if (integer_zerop (param))
9600 warning (OPT_Wnonnull, "null argument where non-null required "
9601 "(argument %lu)", (unsigned long) param_num);
9604 /* Helper for nonnull attribute handling; fetch the operand number
9605 from the attribute argument list. */
9608 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9610 /* Verify the arg number is a small constant. */
9611 if (tree_fits_uhwi_p (arg_num_expr))
9613 *valp = TREE_INT_CST_LOW (arg_num_expr);
9620 /* Handle a "nothrow" attribute; arguments as in
9621 struct attribute_spec.handler. */
9624 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9625 int ARG_UNUSED (flags), bool *no_add_attrs)
9627 if (TREE_CODE (*node) == FUNCTION_DECL)
9628 TREE_NOTHROW (*node) = 1;
9629 /* ??? TODO: Support types. */
9632 warning (OPT_Wattributes, "%qE attribute ignored", name);
9633 *no_add_attrs = true;
9639 /* Handle a "cleanup" attribute; arguments as in
9640 struct attribute_spec.handler. */
9643 handle_cleanup_attribute (tree *node, tree name, tree args,
9644 int ARG_UNUSED (flags), bool *no_add_attrs)
9647 tree cleanup_id, cleanup_decl;
9649 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9650 for global destructors in C++. This requires infrastructure that
9651 we don't have generically at the moment. It's also not a feature
9652 we'd be missing too much, since we do have attribute constructor. */
9653 if (!VAR_P (decl) || TREE_STATIC (decl))
9655 warning (OPT_Wattributes, "%qE attribute ignored", name);
9656 *no_add_attrs = true;
9660 /* Verify that the argument is a function in scope. */
9661 /* ??? We could support pointers to functions here as well, if
9662 that was considered desirable. */
9663 cleanup_id = TREE_VALUE (args);
9664 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9666 error ("cleanup argument not an identifier");
9667 *no_add_attrs = true;
9670 cleanup_decl = lookup_name (cleanup_id);
9671 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9673 error ("cleanup argument not a function");
9674 *no_add_attrs = true;
9678 /* That the function has proper type is checked with the
9679 eventual call to build_function_call. */
9684 /* Handle a "warn_unused_result" attribute. No special handling. */
9687 handle_warn_unused_result_attribute (tree *node, tree name,
9688 tree ARG_UNUSED (args),
9689 int ARG_UNUSED (flags), bool *no_add_attrs)
9691 /* Ignore the attribute for functions not returning any value. */
9692 if (VOID_TYPE_P (TREE_TYPE (*node)))
9694 warning (OPT_Wattributes, "%qE attribute ignored", name);
9695 *no_add_attrs = true;
9701 /* Handle a "sentinel" attribute. */
9704 handle_sentinel_attribute (tree *node, tree name, tree args,
9705 int ARG_UNUSED (flags), bool *no_add_attrs)
9707 if (!prototype_p (*node))
9709 warning (OPT_Wattributes,
9710 "%qE attribute requires prototypes with named arguments", name);
9711 *no_add_attrs = true;
9715 if (!stdarg_p (*node))
9717 warning (OPT_Wattributes,
9718 "%qE attribute only applies to variadic functions", name);
9719 *no_add_attrs = true;
9725 tree position = TREE_VALUE (args);
9726 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9727 && TREE_CODE (position) != FUNCTION_DECL)
9728 position = default_conversion (position);
9730 if (TREE_CODE (position) != INTEGER_CST
9731 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9733 warning (OPT_Wattributes,
9734 "requested position is not an integer constant");
9735 *no_add_attrs = true;
9739 if (tree_int_cst_lt (position, integer_zero_node))
9741 warning (OPT_Wattributes,
9742 "requested position is less than zero");
9743 *no_add_attrs = true;
9751 /* Handle a "type_generic" attribute. */
9754 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9755 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9756 bool * ARG_UNUSED (no_add_attrs))
9758 /* Ensure we have a function type. */
9759 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9761 /* Ensure we have a variadic function. */
9762 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9767 /* Handle a "target" attribute. */
9770 handle_target_attribute (tree *node, tree name, tree args, int flags,
9773 /* Ensure we have a function type. */
9774 if (TREE_CODE (*node) != FUNCTION_DECL)
9776 warning (OPT_Wattributes, "%qE attribute ignored", name);
9777 *no_add_attrs = true;
9779 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9781 *no_add_attrs = true;
9786 /* Arguments being collected for optimization. */
9787 typedef const char *const_char_p; /* For DEF_VEC_P. */
9788 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9791 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9792 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9793 false for #pragma GCC optimize. */
9796 parse_optimize_options (tree args, bool attr_p)
9801 int saved_flag_strict_aliasing;
9802 const char **opt_argv;
9803 struct cl_decoded_option *decoded_options;
9804 unsigned int decoded_options_count;
9807 /* Build up argv vector. Just in case the string is stored away, use garbage
9808 collected strings. */
9809 vec_safe_truncate (optimize_args, 0);
9810 vec_safe_push (optimize_args, (const char *) NULL);
9812 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9814 tree value = TREE_VALUE (ap);
9816 if (TREE_CODE (value) == INTEGER_CST)
9819 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9820 vec_safe_push (optimize_args, ggc_strdup (buffer));
9823 else if (TREE_CODE (value) == STRING_CST)
9825 /* Split string into multiple substrings. */
9826 size_t len = TREE_STRING_LENGTH (value);
9827 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9828 char *end = p + len;
9832 while (next_p != NULL)
9838 comma = strchr (p, ',');
9851 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9853 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9855 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9859 warning (OPT_Wattributes,
9860 "bad option %s to optimize attribute", p);
9862 warning (OPT_Wpragmas,
9863 "bad option %s to pragma attribute", p);
9871 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9872 itself is -Os, and any other switch begins with a -f. */
9873 if ((*p >= '0' && *p <= '9')
9874 || (p[0] == 's' && p[1] == '\0'))
9880 memcpy (r, p, len2);
9882 vec_safe_push (optimize_args, (const char *) q);
9888 opt_argc = optimize_args->length ();
9889 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9891 for (i = 1; i < opt_argc; i++)
9892 opt_argv[i] = (*optimize_args)[i];
9894 saved_flag_strict_aliasing = flag_strict_aliasing;
9896 /* Now parse the options. */
9897 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9899 &decoded_options_count);
9900 decode_options (&global_options, &global_options_set,
9901 decoded_options, decoded_options_count,
9902 input_location, global_dc);
9904 targetm.override_options_after_change();
9906 /* Don't allow changing -fstrict-aliasing. */
9907 flag_strict_aliasing = saved_flag_strict_aliasing;
9909 optimize_args->truncate (0);
9913 /* For handling "optimize" attribute. arguments as in
9914 struct attribute_spec.handler. */
9917 handle_optimize_attribute (tree *node, tree name, tree args,
9918 int ARG_UNUSED (flags), bool *no_add_attrs)
9920 /* Ensure we have a function type. */
9921 if (TREE_CODE (*node) != FUNCTION_DECL)
9923 warning (OPT_Wattributes, "%qE attribute ignored", name);
9924 *no_add_attrs = true;
9928 struct cl_optimization cur_opts;
9929 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9931 /* Save current options. */
9932 cl_optimization_save (&cur_opts, &global_options);
9934 /* If we previously had some optimization options, use them as the
9937 cl_optimization_restore (&global_options,
9938 TREE_OPTIMIZATION (old_opts));
9940 /* Parse options, and update the vector. */
9941 parse_optimize_options (args, true);
9942 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9943 = build_optimization_node (&global_options);
9945 /* Restore current options. */
9946 cl_optimization_restore (&global_options, &cur_opts);
9952 /* Handle a "no_split_stack" attribute. */
9955 handle_no_split_stack_attribute (tree *node, tree name,
9956 tree ARG_UNUSED (args),
9957 int ARG_UNUSED (flags),
9962 if (TREE_CODE (decl) != FUNCTION_DECL)
9964 error_at (DECL_SOURCE_LOCATION (decl),
9965 "%qE attribute applies only to functions", name);
9966 *no_add_attrs = true;
9968 else if (DECL_INITIAL (decl))
9970 error_at (DECL_SOURCE_LOCATION (decl),
9971 "can%'t set %qE attribute after definition", name);
9972 *no_add_attrs = true;
9978 /* Handle a "returns_nonnull" attribute; arguments as in
9979 struct attribute_spec.handler. */
9982 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9985 // Even without a prototype we still have a return type we can check.
9986 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9988 error ("returns_nonnull attribute on a function not returning a pointer");
9989 *no_add_attrs = true;
9994 /* Handle a "designated_init" attribute; arguments as in
9995 struct attribute_spec.handler. */
9998 handle_designated_init_attribute (tree *node, tree name, tree, int,
10001 if (TREE_CODE (*node) != RECORD_TYPE)
10003 error ("%qE attribute is only valid on %<struct%> type", name);
10004 *no_add_attrs = true;
10010 /* Check for valid arguments being passed to a function with FNTYPE.
10011 There are NARGS arguments in the array ARGARRAY. */
10013 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
10015 /* Check for null being passed in a pointer argument that must be
10016 non-null. We also need to do this if format checking is enabled. */
10019 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
10021 /* Check for errors in format strings. */
10023 if (warn_format || warn_suggest_attribute_format)
10024 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
10027 check_function_sentinel (fntype, nargs, argarray);
10030 /* Generic argument checking recursion routine. PARAM is the argument to
10031 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
10032 once the argument is resolved. CTX is context for the callback. */
10034 check_function_arguments_recurse (void (*callback)
10035 (void *, tree, unsigned HOST_WIDE_INT),
10036 void *ctx, tree param,
10037 unsigned HOST_WIDE_INT param_num)
10039 if (CONVERT_EXPR_P (param)
10040 && (TYPE_PRECISION (TREE_TYPE (param))
10041 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
10043 /* Strip coercion. */
10044 check_function_arguments_recurse (callback, ctx,
10045 TREE_OPERAND (param, 0), param_num);
10049 if (TREE_CODE (param) == CALL_EXPR)
10051 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
10053 bool found_format_arg = false;
10055 /* See if this is a call to a known internationalization function
10056 that modifies a format arg. Such a function may have multiple
10057 format_arg attributes (for example, ngettext). */
10059 for (attrs = TYPE_ATTRIBUTES (type);
10061 attrs = TREE_CHAIN (attrs))
10062 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
10065 tree format_num_expr;
10068 call_expr_arg_iterator iter;
10070 /* Extract the argument number, which was previously checked
10072 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
10074 format_num = tree_to_uhwi (format_num_expr);
10076 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
10078 inner_arg = next_call_expr_arg (&iter), i++)
10079 if (i == format_num)
10081 check_function_arguments_recurse (callback, ctx,
10082 inner_arg, param_num);
10083 found_format_arg = true;
10088 /* If we found a format_arg attribute and did a recursive check,
10089 we are done with checking this argument. Otherwise, we continue
10090 and this will be considered a non-literal. */
10091 if (found_format_arg)
10095 if (TREE_CODE (param) == COND_EXPR)
10097 /* Check both halves of the conditional expression. */
10098 check_function_arguments_recurse (callback, ctx,
10099 TREE_OPERAND (param, 1), param_num);
10100 check_function_arguments_recurse (callback, ctx,
10101 TREE_OPERAND (param, 2), param_num);
10105 (*callback) (ctx, param, param_num);
10108 /* Checks for a builtin function FNDECL that the number of arguments
10109 NARGS against the required number REQUIRED and issues an error if
10110 there is a mismatch. Returns true if the number of arguments is
10111 correct, otherwise false. */
10114 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
10116 if (nargs < required)
10118 error_at (input_location,
10119 "not enough arguments to function %qE", fndecl);
10122 else if (nargs > required)
10124 error_at (input_location,
10125 "too many arguments to function %qE", fndecl);
10131 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
10132 Returns false if there was an error, otherwise true. */
10135 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
10137 if (!DECL_BUILT_IN (fndecl)
10138 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
10141 switch (DECL_FUNCTION_CODE (fndecl))
10143 case BUILT_IN_CONSTANT_P:
10144 return builtin_function_validate_nargs (fndecl, nargs, 1);
10146 case BUILT_IN_ISFINITE:
10147 case BUILT_IN_ISINF:
10148 case BUILT_IN_ISINF_SIGN:
10149 case BUILT_IN_ISNAN:
10150 case BUILT_IN_ISNORMAL:
10151 case BUILT_IN_SIGNBIT:
10152 if (builtin_function_validate_nargs (fndecl, nargs, 1))
10154 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10156 error ("non-floating-point argument in call to "
10157 "function %qE", fndecl);
10164 case BUILT_IN_ISGREATER:
10165 case BUILT_IN_ISGREATEREQUAL:
10166 case BUILT_IN_ISLESS:
10167 case BUILT_IN_ISLESSEQUAL:
10168 case BUILT_IN_ISLESSGREATER:
10169 case BUILT_IN_ISUNORDERED:
10170 if (builtin_function_validate_nargs (fndecl, nargs, 2))
10172 enum tree_code code0, code1;
10173 code0 = TREE_CODE (TREE_TYPE (args[0]));
10174 code1 = TREE_CODE (TREE_TYPE (args[1]));
10175 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10176 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10177 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10179 error ("non-floating-point arguments in call to "
10180 "function %qE", fndecl);
10187 case BUILT_IN_FPCLASSIFY:
10188 if (builtin_function_validate_nargs (fndecl, nargs, 6))
10192 for (i=0; i<5; i++)
10193 if (TREE_CODE (args[i]) != INTEGER_CST)
10195 error ("non-const integer argument %u in call to function %qE",
10200 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10202 error ("non-floating-point argument in call to function %qE",
10210 case BUILT_IN_ASSUME_ALIGNED:
10211 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10213 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10215 error ("non-integer argument 3 in call to function %qE", fndecl);
10222 case BUILT_IN_ADD_OVERFLOW:
10223 case BUILT_IN_SUB_OVERFLOW:
10224 case BUILT_IN_MUL_OVERFLOW:
10225 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10228 for (i = 0; i < 2; i++)
10229 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10231 error ("argument %u in call to function %qE does not have "
10232 "integral type", i + 1, fndecl);
10235 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10236 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10238 error ("argument 3 in call to function %qE does not have "
10239 "pointer to integer type", fndecl);
10251 /* Function to help qsort sort FIELD_DECLs by name order. */
10254 field_decl_cmp (const void *x_p, const void *y_p)
10256 const tree *const x = (const tree *const) x_p;
10257 const tree *const y = (const tree *const) y_p;
10259 if (DECL_NAME (*x) == DECL_NAME (*y))
10260 /* A nontype is "greater" than a type. */
10261 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10262 if (DECL_NAME (*x) == NULL_TREE)
10264 if (DECL_NAME (*y) == NULL_TREE)
10266 if (DECL_NAME (*x) < DECL_NAME (*y))
10272 gt_pointer_operator new_value;
10276 /* This routine compares two fields like field_decl_cmp but using the
10277 pointer operator in resort_data. */
10280 resort_field_decl_cmp (const void *x_p, const void *y_p)
10282 const tree *const x = (const tree *const) x_p;
10283 const tree *const y = (const tree *const) y_p;
10285 if (DECL_NAME (*x) == DECL_NAME (*y))
10286 /* A nontype is "greater" than a type. */
10287 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10288 if (DECL_NAME (*x) == NULL_TREE)
10290 if (DECL_NAME (*y) == NULL_TREE)
10293 tree d1 = DECL_NAME (*x);
10294 tree d2 = DECL_NAME (*y);
10295 resort_data.new_value (&d1, resort_data.cookie);
10296 resort_data.new_value (&d2, resort_data.cookie);
10303 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10306 resort_sorted_fields (void *obj,
10307 void * ARG_UNUSED (orig_obj),
10308 gt_pointer_operator new_value,
10311 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10312 resort_data.new_value = new_value;
10313 resort_data.cookie = cookie;
10314 qsort (&sf->elts[0], sf->len, sizeof (tree),
10315 resort_field_decl_cmp);
10318 /* Subroutine of c_parse_error.
10319 Return the result of concatenating LHS and RHS. RHS is really
10320 a string literal, its first character is indicated by RHS_START and
10321 RHS_SIZE is its length (including the terminating NUL character).
10323 The caller is responsible for deleting the returned pointer. */
10326 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10328 const int lhs_size = strlen (lhs);
10329 char *result = XNEWVEC (char, lhs_size + rhs_size);
10330 strncpy (result, lhs, lhs_size);
10331 strncpy (result + lhs_size, rhs_start, rhs_size);
10335 /* Issue the error given by GMSGID, indicating that it occurred before
10336 TOKEN, which had the associated VALUE. */
10339 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
10340 tree value, unsigned char token_flags)
10342 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10344 char *message = NULL;
10346 if (token_type == CPP_EOF)
10347 message = catenate_messages (gmsgid, " at end of input");
10348 else if (token_type == CPP_CHAR
10349 || token_type == CPP_WCHAR
10350 || token_type == CPP_CHAR16
10351 || token_type == CPP_CHAR32
10352 || token_type == CPP_UTF8CHAR)
10354 unsigned int val = TREE_INT_CST_LOW (value);
10355 const char *prefix;
10357 switch (token_type)
10376 if (val <= UCHAR_MAX && ISGRAPH (val))
10377 message = catenate_messages (gmsgid, " before %s'%c'");
10379 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10381 error (message, prefix, val);
10385 else if (token_type == CPP_CHAR_USERDEF
10386 || token_type == CPP_WCHAR_USERDEF
10387 || token_type == CPP_CHAR16_USERDEF
10388 || token_type == CPP_CHAR32_USERDEF
10389 || token_type == CPP_UTF8CHAR_USERDEF)
10390 message = catenate_messages (gmsgid,
10391 " before user-defined character literal");
10392 else if (token_type == CPP_STRING_USERDEF
10393 || token_type == CPP_WSTRING_USERDEF
10394 || token_type == CPP_STRING16_USERDEF
10395 || token_type == CPP_STRING32_USERDEF
10396 || token_type == CPP_UTF8STRING_USERDEF)
10397 message = catenate_messages (gmsgid, " before user-defined string literal");
10398 else if (token_type == CPP_STRING
10399 || token_type == CPP_WSTRING
10400 || token_type == CPP_STRING16
10401 || token_type == CPP_STRING32
10402 || token_type == CPP_UTF8STRING)
10403 message = catenate_messages (gmsgid, " before string constant");
10404 else if (token_type == CPP_NUMBER)
10405 message = catenate_messages (gmsgid, " before numeric constant");
10406 else if (token_type == CPP_NAME)
10408 message = catenate_messages (gmsgid, " before %qE");
10409 error (message, value);
10413 else if (token_type == CPP_PRAGMA)
10414 message = catenate_messages (gmsgid, " before %<#pragma%>");
10415 else if (token_type == CPP_PRAGMA_EOL)
10416 message = catenate_messages (gmsgid, " before end of line");
10417 else if (token_type == CPP_DECLTYPE)
10418 message = catenate_messages (gmsgid, " before %<decltype%>");
10419 else if (token_type < N_TTYPES)
10421 message = catenate_messages (gmsgid, " before %qs token");
10422 error (message, cpp_type2name (token_type, token_flags));
10434 #undef catenate_messages
10437 /* Return the gcc option code associated with the reason for a cpp
10438 message, or 0 if none. */
10441 c_option_controlling_cpp_error (int reason)
10443 const struct cpp_reason_option_codes_t *entry;
10445 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10447 if (entry->reason == reason)
10448 return entry->option_code;
10453 /* Callback from cpp_error for PFILE to print diagnostics from the
10454 preprocessor. The diagnostic is of type LEVEL, with REASON set
10455 to the reason code if LEVEL is represents a warning, at location
10456 LOCATION unless this is after lexing and the compiler's location
10457 should be used instead, with column number possibly overridden by
10458 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10459 the arguments. Returns true if a diagnostic was emitted, false
10463 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10464 location_t location, unsigned int column_override,
10465 const char *msg, va_list *ap)
10467 diagnostic_info diagnostic;
10468 diagnostic_t dlevel;
10469 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10474 case CPP_DL_WARNING_SYSHDR:
10475 if (flag_no_output)
10477 global_dc->dc_warn_system_headers = 1;
10478 /* Fall through. */
10479 case CPP_DL_WARNING:
10480 if (flag_no_output)
10482 dlevel = DK_WARNING;
10484 case CPP_DL_PEDWARN:
10485 if (flag_no_output && !flag_pedantic_errors)
10487 dlevel = DK_PEDWARN;
10502 gcc_unreachable ();
10505 location = input_location;
10506 diagnostic_set_info_translated (&diagnostic, msg, ap,
10508 if (column_override)
10509 diagnostic_override_column (&diagnostic, column_override);
10510 diagnostic_override_option_index (&diagnostic,
10511 c_option_controlling_cpp_error (reason));
10512 ret = report_diagnostic (&diagnostic);
10513 if (level == CPP_DL_WARNING_SYSHDR)
10514 global_dc->dc_warn_system_headers = save_warn_system_headers;
10518 /* Convert a character from the host to the target execution character
10519 set. cpplib handles this, mostly. */
10522 c_common_to_target_charset (HOST_WIDE_INT c)
10524 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10525 zero-extended under -fno-signed-char. cpplib insists that characters
10526 and character constants are always unsigned. Hence we must convert
10528 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10530 uc = cpp_host_to_exec_charset (parse_in, uc);
10532 if (flag_signed_char)
10533 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10534 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10539 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10540 references with an INDIRECT_REF of a constant at the bottom; much like the
10541 traditional rendering of offsetof as a macro. Return the folded result. */
10544 fold_offsetof_1 (tree expr)
10548 switch (TREE_CODE (expr))
10554 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10555 return error_mark_node;
10559 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10560 return error_mark_node;
10564 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10566 error ("cannot apply %<offsetof%> to a non constant address");
10567 return error_mark_node;
10569 return TREE_OPERAND (expr, 0);
10571 case COMPONENT_REF:
10572 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10573 if (base == error_mark_node)
10576 t = TREE_OPERAND (expr, 1);
10577 if (DECL_C_BIT_FIELD (t))
10579 error ("attempt to take address of bit-field structure "
10581 return error_mark_node;
10583 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10584 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10589 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10590 if (base == error_mark_node)
10593 t = TREE_OPERAND (expr, 1);
10595 /* Check if the offset goes beyond the upper bound of the array. */
10596 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10598 tree upbound = array_ref_up_bound (expr);
10599 if (upbound != NULL_TREE
10600 && TREE_CODE (upbound) == INTEGER_CST
10601 && !tree_int_cst_equal (upbound,
10602 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10604 upbound = size_binop (PLUS_EXPR, upbound,
10605 build_int_cst (TREE_TYPE (upbound), 1));
10606 if (tree_int_cst_lt (upbound, t))
10610 for (v = TREE_OPERAND (expr, 0);
10611 TREE_CODE (v) == COMPONENT_REF;
10612 v = TREE_OPERAND (v, 0))
10613 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10616 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10617 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10618 if (TREE_CODE (fld_chain) == FIELD_DECL)
10624 /* Don't warn if the array might be considered a poor
10625 man's flexible array member with a very permissive
10626 definition thereof. */
10627 if (TREE_CODE (v) == ARRAY_REF
10628 || TREE_CODE (v) == COMPONENT_REF)
10629 warning (OPT_Warray_bounds,
10630 "index %E denotes an offset "
10631 "greater than size of %qT",
10632 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10637 t = convert (sizetype, t);
10638 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10641 case COMPOUND_EXPR:
10642 /* Handle static members of volatile structs. */
10643 t = TREE_OPERAND (expr, 1);
10644 gcc_assert (VAR_P (t));
10645 return fold_offsetof_1 (t);
10648 gcc_unreachable ();
10651 return fold_build_pointer_plus (base, off);
10654 /* Likewise, but convert it to the return type of offsetof. */
10657 fold_offsetof (tree expr)
10659 return convert (size_type_node, fold_offsetof_1 (expr));
10662 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10663 expression, because B will always be true. */
10666 warn_for_omitted_condop (location_t location, tree cond)
10668 if (truth_value_p (TREE_CODE (cond)))
10669 warning_at (location, OPT_Wparentheses,
10670 "the omitted middle operand in ?: will always be %<true%>, "
10671 "suggest explicit middle operand");
10674 /* Give an error for storing into ARG, which is 'const'. USE indicates
10675 how ARG was being used. */
10678 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10680 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10682 /* Using this macro rather than (for example) arrays of messages
10683 ensures that all the format strings are checked at compile
10685 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10686 : (use == lv_increment ? (I) \
10687 : (use == lv_decrement ? (D) : (AS))))
10688 if (TREE_CODE (arg) == COMPONENT_REF)
10690 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10691 error_at (loc, READONLY_MSG (G_("assignment of member "
10692 "%qD in read-only object"),
10693 G_("increment of member "
10694 "%qD in read-only object"),
10695 G_("decrement of member "
10696 "%qD in read-only object"),
10697 G_("member %qD in read-only object "
10698 "used as %<asm%> output")),
10699 TREE_OPERAND (arg, 1));
10701 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10702 G_("increment of read-only member %qD"),
10703 G_("decrement of read-only member %qD"),
10704 G_("read-only member %qD used as %<asm%> output")),
10705 TREE_OPERAND (arg, 1));
10707 else if (VAR_P (arg))
10708 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10709 G_("increment of read-only variable %qD"),
10710 G_("decrement of read-only variable %qD"),
10711 G_("read-only variable %qD used as %<asm%> output")),
10713 else if (TREE_CODE (arg) == PARM_DECL)
10714 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10715 G_("increment of read-only parameter %qD"),
10716 G_("decrement of read-only parameter %qD"),
10717 G_("read-only parameter %qD use as %<asm%> output")),
10719 else if (TREE_CODE (arg) == RESULT_DECL)
10721 gcc_assert (c_dialect_cxx ());
10722 error_at (loc, READONLY_MSG (G_("assignment of "
10723 "read-only named return value %qD"),
10725 "read-only named return value %qD"),
10727 "read-only named return value %qD"),
10728 G_("read-only named return value %qD "
10729 "used as %<asm%>output")),
10732 else if (TREE_CODE (arg) == FUNCTION_DECL)
10733 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10734 G_("increment of function %qD"),
10735 G_("decrement of function %qD"),
10736 G_("function %qD used as %<asm%> output")),
10739 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10740 G_("increment of read-only location %qE"),
10741 G_("decrement of read-only location %qE"),
10742 G_("read-only location %qE used as %<asm%> output")),
10746 /* Print an error message for an invalid lvalue. USE says
10747 how the lvalue is being used and so selects the error message. LOC
10748 is the location for the error. */
10751 lvalue_error (location_t loc, enum lvalue_use use)
10756 error_at (loc, "lvalue required as left operand of assignment");
10759 error_at (loc, "lvalue required as increment operand");
10762 error_at (loc, "lvalue required as decrement operand");
10765 error_at (loc, "lvalue required as unary %<&%> operand");
10768 error_at (loc, "lvalue required in asm statement");
10771 gcc_unreachable ();
10775 /* Print an error message for an invalid indirection of type TYPE.
10776 ERRSTRING is the name of the operator for the indirection. */
10779 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10784 gcc_assert (c_dialect_cxx ());
10785 error_at (loc, "invalid type argument (have %qT)", type);
10787 case RO_ARRAY_INDEXING:
10789 "invalid type argument of array indexing (have %qT)",
10792 case RO_UNARY_STAR:
10794 "invalid type argument of unary %<*%> (have %qT)",
10799 "invalid type argument of %<->%> (have %qT)",
10802 case RO_ARROW_STAR:
10804 "invalid type argument of %<->*%> (have %qT)",
10807 case RO_IMPLICIT_CONVERSION:
10809 "invalid type argument of implicit conversion (have %qT)",
10813 gcc_unreachable ();
10817 /* *PTYPE is an incomplete array. Complete it with a domain based on
10818 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10819 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10820 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10823 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10825 tree maxindex, type, main_type, elt, unqual_elt;
10826 int failure = 0, quals;
10827 hashval_t hashcode = 0;
10828 bool overflow_p = false;
10830 maxindex = size_zero_node;
10833 if (TREE_CODE (initial_value) == STRING_CST)
10836 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10837 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10839 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10841 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10843 if (vec_safe_is_empty (v))
10847 maxindex = ssize_int (-1);
10852 unsigned HOST_WIDE_INT cnt;
10853 constructor_elt *ce;
10854 bool fold_p = false;
10857 maxindex = (*v)[0].index, fold_p = true;
10859 curindex = maxindex;
10861 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10863 bool curfold_p = false;
10865 curindex = ce->index, curfold_p = true;
10870 /* Since we treat size types now as ordinary
10871 unsigned types, we need an explicit overflow
10873 tree orig = curindex;
10874 curindex = fold_convert (sizetype, curindex);
10875 overflow_p |= tree_int_cst_lt (curindex, orig);
10877 curindex = size_binop (PLUS_EXPR, curindex,
10880 if (tree_int_cst_lt (maxindex, curindex))
10881 maxindex = curindex, fold_p = curfold_p;
10885 tree orig = maxindex;
10886 maxindex = fold_convert (sizetype, maxindex);
10887 overflow_p |= tree_int_cst_lt (maxindex, orig);
10893 /* Make an error message unless that happened already. */
10894 if (initial_value != error_mark_node)
10906 elt = TREE_TYPE (type);
10907 quals = TYPE_QUALS (strip_array_types (elt));
10911 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10913 /* Using build_distinct_type_copy and modifying things afterward instead
10914 of using build_array_type to create a new type preserves all of the
10915 TYPE_LANG_FLAG_? bits that the front end may have set. */
10916 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10917 TREE_TYPE (main_type) = unqual_elt;
10918 TYPE_DOMAIN (main_type)
10919 = build_range_type (TREE_TYPE (maxindex),
10920 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10921 layout_type (main_type);
10923 /* Make sure we have the canonical MAIN_TYPE. */
10924 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10925 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10927 main_type = type_hash_canon (hashcode, main_type);
10929 /* Fix the canonical type. */
10930 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10931 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10932 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10933 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10934 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10935 != TYPE_DOMAIN (main_type)))
10936 TYPE_CANONICAL (main_type)
10937 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10938 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10940 TYPE_CANONICAL (main_type) = main_type;
10945 type = c_build_qualified_type (main_type, quals);
10947 if (COMPLETE_TYPE_P (type)
10948 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10949 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10951 error ("size of array is too large");
10952 /* If we proceed with the array type as it is, we'll eventually
10953 crash in tree_to_[su]hwi(). */
10954 type = error_mark_node;
10961 /* Like c_mark_addressable but don't check register qualifier. */
10963 c_common_mark_addressable_vec (tree t)
10965 while (handled_component_p (t))
10966 t = TREE_OPERAND (t, 0);
10967 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
10969 TREE_ADDRESSABLE (t) = 1;
10974 /* Used to help initialize the builtin-types.def table. When a type of
10975 the correct size doesn't exist, use error_mark_node instead of NULL.
10976 The later results in segfaults even when a decl using the type doesn't
10980 builtin_type_for_size (int size, bool unsignedp)
10982 tree type = c_common_type_for_size (size, unsignedp);
10983 return type ? type : error_mark_node;
10986 /* A helper function for resolve_overloaded_builtin in resolving the
10987 overloaded __sync_ builtins. Returns a positive power of 2 if the
10988 first operand of PARAMS is a pointer to a supported data type.
10989 Returns 0 if an error is encountered. */
10992 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10999 error ("too few arguments to function %qE", function);
11003 type = TREE_TYPE ((*params)[0]);
11004 if (TREE_CODE (type) == ARRAY_TYPE)
11006 /* Force array-to-pointer decay for C++. */
11007 gcc_assert (c_dialect_cxx());
11008 (*params)[0] = default_conversion ((*params)[0]);
11009 type = TREE_TYPE ((*params)[0]);
11011 if (TREE_CODE (type) != POINTER_TYPE)
11014 type = TREE_TYPE (type);
11015 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
11018 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
11019 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
11023 error ("incompatible type for argument %d of %qE", 1, function);
11027 /* A helper function for resolve_overloaded_builtin. Adds casts to
11028 PARAMS to make arguments match up with those of FUNCTION. Drops
11029 the variadic arguments at the end. Returns false if some error
11030 was encountered; true on success. */
11033 sync_resolve_params (location_t loc, tree orig_function, tree function,
11034 vec<tree, va_gc> *params, bool orig_format)
11036 function_args_iterator iter;
11038 unsigned int parmnum;
11040 function_args_iter_init (&iter, TREE_TYPE (function));
11041 /* We've declared the implementation functions to use "volatile void *"
11042 as the pointer parameter, so we shouldn't get any complaints from the
11043 call to check_function_arguments what ever type the user used. */
11044 function_args_iter_next (&iter);
11045 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
11046 ptype = TYPE_MAIN_VARIANT (ptype);
11048 /* For the rest of the values, we need to cast these to FTYPE, so that we
11049 don't get warnings for passing pointer types, etc. */
11053 tree val, arg_type;
11055 arg_type = function_args_iter_cond (&iter);
11056 /* XXX void_type_node belies the abstraction. */
11057 if (arg_type == void_type_node)
11061 if (params->length () <= parmnum)
11063 error_at (loc, "too few arguments to function %qE", orig_function);
11067 /* Only convert parameters if arg_type is unsigned integer type with
11068 new format sync routines, i.e. don't attempt to convert pointer
11069 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
11070 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
11072 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
11074 /* Ideally for the first conversion we'd use convert_for_assignment
11075 so that we get warnings for anything that doesn't match the pointer
11076 type. This isn't portable across the C and C++ front ends atm. */
11077 val = (*params)[parmnum];
11078 val = convert (ptype, val);
11079 val = convert (arg_type, val);
11080 (*params)[parmnum] = val;
11083 function_args_iter_next (&iter);
11086 /* __atomic routines are not variadic. */
11087 if (!orig_format && params->length () != parmnum + 1)
11089 error_at (loc, "too many arguments to function %qE", orig_function);
11093 /* The definition of these primitives is variadic, with the remaining
11094 being "an optional list of variables protected by the memory barrier".
11095 No clue what that's supposed to mean, precisely, but we consider all
11096 call-clobbered variables to be protected so we're safe. */
11097 params->truncate (parmnum + 1);
11102 /* A helper function for resolve_overloaded_builtin. Adds a cast to
11103 RESULT to make it match the type of the first pointer argument in
11107 sync_resolve_return (tree first_param, tree result, bool orig_format)
11109 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
11110 tree rtype = TREE_TYPE (result);
11111 ptype = TYPE_MAIN_VARIANT (ptype);
11113 /* New format doesn't require casting unless the types are the same size. */
11114 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
11115 return convert (ptype, result);
11120 /* This function verifies the PARAMS to generic atomic FUNCTION.
11121 It returns the size if all the parameters are the same size, otherwise
11122 0 is returned if the parameters are invalid. */
11125 get_atomic_generic_size (location_t loc, tree function,
11126 vec<tree, va_gc> *params)
11128 unsigned int n_param;
11129 unsigned int n_model;
11134 /* Determine the parameter makeup. */
11135 switch (DECL_FUNCTION_CODE (function))
11137 case BUILT_IN_ATOMIC_EXCHANGE:
11141 case BUILT_IN_ATOMIC_LOAD:
11142 case BUILT_IN_ATOMIC_STORE:
11146 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11151 gcc_unreachable ();
11154 if (vec_safe_length (params) != n_param)
11156 error_at (loc, "incorrect number of arguments to function %qE", function);
11160 /* Get type of first parameter, and determine its size. */
11161 type_0 = TREE_TYPE ((*params)[0]);
11162 if (TREE_CODE (type_0) == ARRAY_TYPE)
11164 /* Force array-to-pointer decay for C++. */
11165 gcc_assert (c_dialect_cxx());
11166 (*params)[0] = default_conversion ((*params)[0]);
11167 type_0 = TREE_TYPE ((*params)[0]);
11169 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11171 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11176 /* Types must be compile time constant sizes. */
11177 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
11180 "argument 1 of %qE must be a pointer to a constant size type",
11185 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
11187 /* Zero size objects are not allowed. */
11191 "argument 1 of %qE must be a pointer to a nonzero size object",
11196 /* Check each other parameter is a pointer and the same size. */
11197 for (x = 0; x < n_param - n_model; x++)
11200 tree type = TREE_TYPE ((*params)[x]);
11201 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
11202 if (n_param == 6 && x == 3)
11204 if (!POINTER_TYPE_P (type))
11206 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11210 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11211 size = type_size ? tree_to_uhwi (type_size) : 0;
11212 if (size != size_0)
11214 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11220 /* Check memory model parameters for validity. */
11221 for (x = n_param - n_model ; x < n_param; x++)
11223 tree p = (*params)[x];
11224 if (TREE_CODE (p) == INTEGER_CST)
11226 int i = tree_to_uhwi (p);
11227 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
11229 warning_at (loc, OPT_Winvalid_memory_model,
11230 "invalid memory model argument %d of %qE", x + 1,
11235 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11237 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11247 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11248 at the beginning of the parameter list PARAMS representing the size of the
11249 objects. This is to match the library ABI requirement. LOC is the location
11250 of the function call.
11251 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11252 returned to allow the external call to be constructed. */
11255 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
11256 vec<tree, va_gc> *params)
11260 /* Insert a SIZE_T parameter as the first param. If there isn't
11261 enough space, allocate a new vector and recursively re-build with that. */
11262 if (!params->space (1))
11264 unsigned int z, len;
11265 vec<tree, va_gc> *v;
11268 len = params->length ();
11269 vec_alloc (v, len + 1);
11270 v->quick_push (build_int_cst (size_type_node, n));
11271 for (z = 0; z < len; z++)
11272 v->quick_push ((*params)[z]);
11273 f = build_function_call_vec (loc, vNULL, function, v, NULL);
11278 /* Add the size parameter and leave as a function call for processing. */
11279 size_node = build_int_cst (size_type_node, n);
11280 params->quick_insert (0, size_node);
11285 /* Return whether atomic operations for naturally aligned N-byte
11286 arguments are supported, whether inline or through libatomic. */
11288 atomic_size_supported_p (int n)
11299 return targetm.scalar_mode_supported_p (TImode);
11306 /* This will process an __atomic_exchange function call, determine whether it
11307 needs to be mapped to the _N variation, or turned into a library call.
11308 LOC is the location of the builtin call.
11309 FUNCTION is the DECL that has been invoked;
11310 PARAMS is the argument list for the call. The return value is non-null
11311 TRUE is returned if it is translated into the proper format for a call to the
11312 external library, and NEW_RETURN is set the tree for that function.
11313 FALSE is returned if processing for the _N variation is required, and
11314 NEW_RETURN is set to the return value the result is copied into. */
11316 resolve_overloaded_atomic_exchange (location_t loc, tree function,
11317 vec<tree, va_gc> *params, tree *new_return)
11319 tree p0, p1, p2, p3;
11320 tree I_type, I_type_ptr;
11321 int n = get_atomic_generic_size (loc, function, params);
11323 /* Size of 0 is an error condition. */
11326 *new_return = error_mark_node;
11330 /* If not a lock-free size, change to the library generic format. */
11331 if (!atomic_size_supported_p (n))
11333 *new_return = add_atomic_size_parameter (n, loc, function, params);
11337 /* Otherwise there is a lockfree match, transform the call from:
11338 void fn(T* mem, T* desired, T* return, model)
11340 *return = (T) (fn (In* mem, (In) *desired, model)) */
11347 /* Create pointer to appropriate size. */
11348 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11349 I_type_ptr = build_pointer_type (I_type);
11351 /* Convert object pointer to required type. */
11352 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11354 /* Convert new value to required type, and dereference it. */
11355 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11356 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11359 /* Move memory model to the 3rd position, and end param list. */
11361 params->truncate (3);
11363 /* Convert return pointer and dereference it for later assignment. */
11364 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11370 /* This will process an __atomic_compare_exchange function call, determine
11371 whether it needs to be mapped to the _N variation, or turned into a lib call.
11372 LOC is the location of the builtin call.
11373 FUNCTION is the DECL that has been invoked;
11374 PARAMS is the argument list for the call. The return value is non-null
11375 TRUE is returned if it is translated into the proper format for a call to the
11376 external library, and NEW_RETURN is set the tree for that function.
11377 FALSE is returned if processing for the _N variation is required. */
11380 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11381 vec<tree, va_gc> *params,
11385 tree I_type, I_type_ptr;
11386 int n = get_atomic_generic_size (loc, function, params);
11388 /* Size of 0 is an error condition. */
11391 *new_return = error_mark_node;
11395 /* If not a lock-free size, change to the library generic format. */
11396 if (!atomic_size_supported_p (n))
11398 /* The library generic format does not have the weak parameter, so
11399 remove it from the param list. Since a parameter has been removed,
11400 we can be sure that there is room for the SIZE_T parameter, meaning
11401 there will not be a recursive rebuilding of the parameter list, so
11402 there is no danger this will be done twice. */
11405 (*params)[3] = (*params)[4];
11406 (*params)[4] = (*params)[5];
11407 params->truncate (5);
11409 *new_return = add_atomic_size_parameter (n, loc, function, params);
11413 /* Otherwise, there is a match, so the call needs to be transformed from:
11414 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11416 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11422 /* Create pointer to appropriate size. */
11423 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11424 I_type_ptr = build_pointer_type (I_type);
11426 /* Convert object pointer to required type. */
11427 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11430 /* Convert expected pointer to required type. */
11431 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11434 /* Convert desired value to required type, and dereference it. */
11435 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11436 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11439 /* The rest of the parameters are fine. NULL means no special return value
11441 *new_return = NULL;
11446 /* This will process an __atomic_load function call, determine whether it
11447 needs to be mapped to the _N variation, or turned into a library call.
11448 LOC is the location of the builtin call.
11449 FUNCTION is the DECL that has been invoked;
11450 PARAMS is the argument list for the call. The return value is non-null
11451 TRUE is returned if it is translated into the proper format for a call to the
11452 external library, and NEW_RETURN is set the tree for that function.
11453 FALSE is returned if processing for the _N variation is required, and
11454 NEW_RETURN is set to the return value the result is copied into. */
11457 resolve_overloaded_atomic_load (location_t loc, tree function,
11458 vec<tree, va_gc> *params, tree *new_return)
11461 tree I_type, I_type_ptr;
11462 int n = get_atomic_generic_size (loc, function, params);
11464 /* Size of 0 is an error condition. */
11467 *new_return = error_mark_node;
11471 /* If not a lock-free size, change to the library generic format. */
11472 if (!atomic_size_supported_p (n))
11474 *new_return = add_atomic_size_parameter (n, loc, function, params);
11478 /* Otherwise, there is a match, so the call needs to be transformed from:
11479 void fn(T* mem, T* return, model)
11481 *return = (T) (fn ((In *) mem, model)) */
11487 /* Create pointer to appropriate size. */
11488 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11489 I_type_ptr = build_pointer_type (I_type);
11491 /* Convert object pointer to required type. */
11492 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11495 /* Move memory model to the 2nd position, and end param list. */
11497 params->truncate (2);
11499 /* Convert return pointer and dereference it for later assignment. */
11500 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11506 /* This will process an __atomic_store function call, determine whether it
11507 needs to be mapped to the _N variation, or turned into a library call.
11508 LOC is the location of the builtin call.
11509 FUNCTION is the DECL that has been invoked;
11510 PARAMS is the argument list for the call. The return value is non-null
11511 TRUE is returned if it is translated into the proper format for a call to the
11512 external library, and NEW_RETURN is set the tree for that function.
11513 FALSE is returned if processing for the _N variation is required, and
11514 NEW_RETURN is set to the return value the result is copied into. */
11517 resolve_overloaded_atomic_store (location_t loc, tree function,
11518 vec<tree, va_gc> *params, tree *new_return)
11521 tree I_type, I_type_ptr;
11522 int n = get_atomic_generic_size (loc, function, params);
11524 /* Size of 0 is an error condition. */
11527 *new_return = error_mark_node;
11531 /* If not a lock-free size, change to the library generic format. */
11532 if (!atomic_size_supported_p (n))
11534 *new_return = add_atomic_size_parameter (n, loc, function, params);
11538 /* Otherwise, there is a match, so the call needs to be transformed from:
11539 void fn(T* mem, T* value, model)
11541 fn ((In *) mem, (In) *value, model) */
11546 /* Create pointer to appropriate size. */
11547 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11548 I_type_ptr = build_pointer_type (I_type);
11550 /* Convert object pointer to required type. */
11551 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11554 /* Convert new value to required type, and dereference it. */
11555 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11556 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11559 /* The memory model is in the right spot already. Return is void. */
11560 *new_return = NULL_TREE;
11566 /* Some builtin functions are placeholders for other expressions. This
11567 function should be called immediately after parsing the call expression
11568 before surrounding code has committed to the type of the expression.
11570 LOC is the location of the builtin call.
11572 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11573 PARAMS is the argument list for the call. The return value is non-null
11574 when expansion is complete, and null if normal processing should
11578 resolve_overloaded_builtin (location_t loc, tree function,
11579 vec<tree, va_gc> *params)
11581 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11582 bool orig_format = true;
11583 tree new_return = NULL_TREE;
11585 switch (DECL_BUILT_IN_CLASS (function))
11587 case BUILT_IN_NORMAL:
11590 if (targetm.resolve_overloaded_builtin)
11591 return targetm.resolve_overloaded_builtin (loc, function, params);
11598 /* Handle BUILT_IN_NORMAL here. */
11601 case BUILT_IN_ATOMIC_EXCHANGE:
11602 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11603 case BUILT_IN_ATOMIC_LOAD:
11604 case BUILT_IN_ATOMIC_STORE:
11606 /* Handle these 4 together so that they can fall through to the next
11607 case if the call is transformed to an _N variant. */
11610 case BUILT_IN_ATOMIC_EXCHANGE:
11612 if (resolve_overloaded_atomic_exchange (loc, function, params,
11615 /* Change to the _N variant. */
11616 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11620 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11622 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11626 /* Change to the _N variant. */
11627 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11630 case BUILT_IN_ATOMIC_LOAD:
11632 if (resolve_overloaded_atomic_load (loc, function, params,
11635 /* Change to the _N variant. */
11636 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11639 case BUILT_IN_ATOMIC_STORE:
11641 if (resolve_overloaded_atomic_store (loc, function, params,
11644 /* Change to the _N variant. */
11645 orig_code = BUILT_IN_ATOMIC_STORE_N;
11649 gcc_unreachable ();
11651 /* Fallthrough to the normal processing. */
11653 case BUILT_IN_ATOMIC_EXCHANGE_N:
11654 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11655 case BUILT_IN_ATOMIC_LOAD_N:
11656 case BUILT_IN_ATOMIC_STORE_N:
11657 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11658 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11659 case BUILT_IN_ATOMIC_AND_FETCH_N:
11660 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11661 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11662 case BUILT_IN_ATOMIC_OR_FETCH_N:
11663 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11664 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11665 case BUILT_IN_ATOMIC_FETCH_AND_N:
11666 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11667 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11668 case BUILT_IN_ATOMIC_FETCH_OR_N:
11670 orig_format = false;
11671 /* Fallthru for parameter processing. */
11673 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11674 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11675 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11676 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11677 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11678 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11679 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11680 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11681 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11682 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11683 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11684 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11685 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11686 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11687 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11688 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11690 int n = sync_resolve_size (function, params);
11691 tree new_function, first_param, result;
11692 enum built_in_function fncode;
11695 return error_mark_node;
11697 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11698 new_function = builtin_decl_explicit (fncode);
11699 if (!sync_resolve_params (loc, function, new_function, params,
11701 return error_mark_node;
11703 first_param = (*params)[0];
11704 result = build_function_call_vec (loc, vNULL, new_function, params,
11706 if (result == error_mark_node)
11708 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11709 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11710 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11711 result = sync_resolve_return (first_param, result, orig_format);
11713 /* If new_return is set, assign function to that expr and cast the
11714 result to void since the generic interface returned void. */
11717 /* Cast function result from I{1,2,4,8,16} to the required type. */
11718 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11719 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11721 TREE_SIDE_EFFECTS (result) = 1;
11722 protected_set_expr_location (result, loc);
11723 result = convert (void_type_node, result);
11733 /* vector_types_compatible_elements_p is used in type checks of vectors
11734 values used as operands of binary operators. Where it returns true, and
11735 the other checks of the caller succeed (being vector types in he first
11736 place, and matching number of elements), we can just treat the types
11737 as essentially the same.
11738 Contrast with vector_targets_convertible_p, which is used for vector
11739 pointer types, and vector_types_convertible_p, which will allow
11740 language-specific matches under the control of flag_lax_vector_conversions,
11741 and might still require a conversion. */
11742 /* True if vector types T1 and T2 can be inputs to the same binary
11743 operator without conversion.
11744 We don't check the overall vector size here because some of our callers
11745 want to give different error messages when the vectors are compatible
11746 except for the element count. */
11749 vector_types_compatible_elements_p (tree t1, tree t2)
11751 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11752 t1 = TREE_TYPE (t1);
11753 t2 = TREE_TYPE (t2);
11755 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11757 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11758 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11759 || c2 == FIXED_POINT_TYPE));
11761 t1 = c_common_signed_type (t1);
11762 t2 = c_common_signed_type (t2);
11763 /* Equality works here because c_common_signed_type uses
11764 TYPE_MAIN_VARIANT. */
11767 if (opaque && c1 == c2
11768 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11769 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11774 /* Check for missing format attributes on function pointers. LTYPE is
11775 the new type or left-hand side type. RTYPE is the old type or
11776 right-hand side type. Returns TRUE if LTYPE is missing the desired
11780 check_missing_format_attribute (tree ltype, tree rtype)
11782 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11785 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11786 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11791 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11792 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11800 /* Subscripting with type char is likely to lose on a machine where
11801 chars are signed. So warn on any machine, but optionally. Don't
11802 warn for unsigned char since that type is safe. Don't warn for
11803 signed char because anyone who uses that must have done so
11804 deliberately. Furthermore, we reduce the false positive load by
11805 warning only for non-constant value of type char. */
11808 warn_array_subscript_with_type_char (location_t loc, tree index)
11810 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11811 && TREE_CODE (index) != INTEGER_CST)
11812 warning_at (loc, OPT_Wchar_subscripts,
11813 "array subscript has type %<char%>");
11816 /* Implement -Wparentheses for the unexpected C precedence rules, to
11817 cover cases like x + y << z which readers are likely to
11818 misinterpret. We have seen an expression in which CODE is a binary
11819 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11820 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11821 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11822 expression was not formed using a binary or unary operator, or it
11823 was enclosed in parentheses. */
11826 warn_about_parentheses (location_t loc, enum tree_code code,
11827 enum tree_code code_left, tree arg_left,
11828 enum tree_code code_right, tree arg_right)
11830 if (!warn_parentheses)
11833 /* This macro tests that the expression ARG with original tree code
11834 CODE appears to be a boolean expression. or the result of folding a
11835 boolean expression. */
11836 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11837 (truth_value_p (TREE_CODE (ARG)) \
11838 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11839 /* Folding may create 0 or 1 integers from other expressions. */ \
11840 || ((CODE) != INTEGER_CST \
11841 && (integer_onep (ARG) || integer_zerop (ARG))))
11846 if (code_left == PLUS_EXPR)
11847 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11848 "suggest parentheses around %<+%> inside %<<<%>");
11849 else if (code_right == PLUS_EXPR)
11850 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11851 "suggest parentheses around %<+%> inside %<<<%>");
11852 else if (code_left == MINUS_EXPR)
11853 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11854 "suggest parentheses around %<-%> inside %<<<%>");
11855 else if (code_right == MINUS_EXPR)
11856 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11857 "suggest parentheses around %<-%> inside %<<<%>");
11861 if (code_left == PLUS_EXPR)
11862 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11863 "suggest parentheses around %<+%> inside %<>>%>");
11864 else if (code_right == PLUS_EXPR)
11865 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11866 "suggest parentheses around %<+%> inside %<>>%>");
11867 else if (code_left == MINUS_EXPR)
11868 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11869 "suggest parentheses around %<-%> inside %<>>%>");
11870 else if (code_right == MINUS_EXPR)
11871 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11872 "suggest parentheses around %<-%> inside %<>>%>");
11875 case TRUTH_ORIF_EXPR:
11876 if (code_left == TRUTH_ANDIF_EXPR)
11877 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11878 "suggest parentheses around %<&&%> within %<||%>");
11879 else if (code_right == TRUTH_ANDIF_EXPR)
11880 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11881 "suggest parentheses around %<&&%> within %<||%>");
11885 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11886 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11887 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11888 "suggest parentheses around arithmetic in operand of %<|%>");
11889 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11890 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11891 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11892 "suggest parentheses around arithmetic in operand of %<|%>");
11893 /* Check cases like x|y==z */
11894 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11895 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11896 "suggest parentheses around comparison in operand of %<|%>");
11897 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11898 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11899 "suggest parentheses around comparison in operand of %<|%>");
11900 /* Check cases like !x | y */
11901 else if (code_left == TRUTH_NOT_EXPR
11902 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11903 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11904 "suggest parentheses around operand of "
11905 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11909 if (code_left == BIT_AND_EXPR
11910 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11911 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11912 "suggest parentheses around arithmetic in operand of %<^%>");
11913 else if (code_right == BIT_AND_EXPR
11914 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11915 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11916 "suggest parentheses around arithmetic in operand of %<^%>");
11917 /* Check cases like x^y==z */
11918 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11919 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11920 "suggest parentheses around comparison in operand of %<^%>");
11921 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11922 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11923 "suggest parentheses around comparison in operand of %<^%>");
11927 if (code_left == PLUS_EXPR)
11928 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11929 "suggest parentheses around %<+%> in operand of %<&%>");
11930 else if (code_right == PLUS_EXPR)
11931 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11932 "suggest parentheses around %<+%> in operand of %<&%>");
11933 else if (code_left == MINUS_EXPR)
11934 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11935 "suggest parentheses around %<-%> in operand of %<&%>");
11936 else if (code_right == MINUS_EXPR)
11937 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11938 "suggest parentheses around %<-%> in operand of %<&%>");
11939 /* Check cases like x&y==z */
11940 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11941 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11942 "suggest parentheses around comparison in operand of %<&%>");
11943 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11944 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11945 "suggest parentheses around comparison in operand of %<&%>");
11946 /* Check cases like !x & y */
11947 else if (code_left == TRUTH_NOT_EXPR
11948 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11949 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11950 "suggest parentheses around operand of "
11951 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11955 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11956 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11957 "suggest parentheses around comparison in operand of %<==%>");
11958 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11959 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11960 "suggest parentheses around comparison in operand of %<==%>");
11963 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11964 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11965 "suggest parentheses around comparison in operand of %<!=%>");
11966 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11967 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11968 "suggest parentheses around comparison in operand of %<!=%>");
11972 if (TREE_CODE_CLASS (code) == tcc_comparison)
11974 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11975 && code_left != NE_EXPR && code_left != EQ_EXPR
11976 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11977 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11978 "comparisons like %<X<=Y<=Z%> do not "
11979 "have their mathematical meaning");
11980 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11981 && code_right != NE_EXPR && code_right != EQ_EXPR
11982 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11983 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11984 "comparisons like %<X<=Y<=Z%> do not "
11985 "have their mathematical meaning");
11989 #undef NOT_A_BOOLEAN_EXPR_P
11992 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11995 warn_for_unused_label (tree label)
11997 if (!TREE_USED (label))
11999 if (DECL_INITIAL (label))
12000 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
12002 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
12006 /* Warn for division by zero according to the value of DIVISOR. LOC
12007 is the location of the division operator. */
12010 warn_for_div_by_zero (location_t loc, tree divisor)
12012 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
12013 about division by zero. Do not issue a warning if DIVISOR has a
12014 floating-point type, since we consider 0.0/0.0 a valid way of
12015 generating a NaN. */
12016 if (c_inhibit_evaluation_warnings == 0
12017 && (integer_zerop (divisor) || fixed_zerop (divisor)))
12018 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
12021 /* Subroutine of build_binary_op. Give warnings for comparisons
12022 between signed and unsigned quantities that may fail. Do the
12023 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
12024 so that casts will be considered, but default promotions won't
12027 LOCATION is the location of the comparison operator.
12029 The arguments of this function map directly to local variables
12030 of build_binary_op. */
12033 warn_for_sign_compare (location_t location,
12034 tree orig_op0, tree orig_op1,
12035 tree op0, tree op1,
12036 tree result_type, enum tree_code resultcode)
12038 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
12039 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
12040 int unsignedp0, unsignedp1;
12042 /* In C++, check for comparison of different enum types. */
12043 if (c_dialect_cxx()
12044 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
12045 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
12046 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
12047 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
12049 warning_at (location,
12050 OPT_Wsign_compare, "comparison between types %qT and %qT",
12051 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
12054 /* Do not warn if the comparison is being done in a signed type,
12055 since the signed type will only be chosen if it can represent
12056 all the values of the unsigned type. */
12057 if (!TYPE_UNSIGNED (result_type))
12059 /* Do not warn if both operands are unsigned. */
12060 else if (op0_signed == op1_signed)
12064 tree sop, uop, base_type;
12068 sop = orig_op0, uop = orig_op1;
12070 sop = orig_op1, uop = orig_op0;
12072 STRIP_TYPE_NOPS (sop);
12073 STRIP_TYPE_NOPS (uop);
12074 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12075 ? TREE_TYPE (result_type) : result_type);
12077 /* Do not warn if the signed quantity is an unsuffixed integer
12078 literal (or some static constant expression involving such
12079 literals or a conditional expression involving such literals)
12080 and it is non-negative. */
12081 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12083 /* Do not warn if the comparison is an equality operation, the
12084 unsigned quantity is an integral constant, and it would fit
12085 in the result if the result were signed. */
12086 else if (TREE_CODE (uop) == INTEGER_CST
12087 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
12088 && int_fits_type_p (uop, c_common_signed_type (base_type)))
12090 /* In C, do not warn if the unsigned quantity is an enumeration
12091 constant and its maximum value would fit in the result if the
12092 result were signed. */
12093 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12094 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12095 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
12096 c_common_signed_type (base_type)))
12099 warning_at (location,
12101 "comparison between signed and unsigned integer expressions");
12104 /* Warn if two unsigned values are being compared in a size larger
12105 than their original size, and one (and only one) is the result of
12106 a `~' operator. This comparison will always fail.
12108 Also warn if one operand is a constant, and the constant does not
12109 have all bits set that are set in the ~ operand when it is
12112 op0 = c_common_get_narrower (op0, &unsignedp0);
12113 op1 = c_common_get_narrower (op1, &unsignedp1);
12115 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12116 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12118 if (TREE_CODE (op0) == BIT_NOT_EXPR)
12119 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
12120 if (TREE_CODE (op1) == BIT_NOT_EXPR)
12121 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
12123 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
12126 HOST_WIDE_INT constant, mask;
12130 if (tree_fits_shwi_p (op0))
12133 unsignedp = unsignedp1;
12134 constant = tree_to_shwi (op0);
12139 unsignedp = unsignedp0;
12140 constant = tree_to_shwi (op1);
12143 bits = TYPE_PRECISION (TREE_TYPE (primop));
12144 if (bits < TYPE_PRECISION (result_type)
12145 && bits < HOST_BITS_PER_LONG && unsignedp)
12147 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
12148 if ((mask & constant) != mask)
12151 warning_at (location, OPT_Wsign_compare,
12152 "promoted ~unsigned is always non-zero");
12154 warning_at (location, OPT_Wsign_compare,
12155 "comparison of promoted ~unsigned with constant");
12159 else if (unsignedp0 && unsignedp1
12160 && (TYPE_PRECISION (TREE_TYPE (op0))
12161 < TYPE_PRECISION (result_type))
12162 && (TYPE_PRECISION (TREE_TYPE (op1))
12163 < TYPE_PRECISION (result_type)))
12164 warning_at (location, OPT_Wsign_compare,
12165 "comparison of promoted ~unsigned with unsigned");
12169 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12170 type via c_common_type. If -Wdouble-promotion is in use, and the
12171 conditions for warning have been met, issue a warning. GMSGID is
12172 the warning message. It must have two %T specifiers for the type
12173 that was converted (generally "float") and the type to which it was
12174 converted (generally "double), respectively. LOC is the location
12175 to which the awrning should refer. */
12178 do_warn_double_promotion (tree result_type, tree type1, tree type2,
12179 const char *gmsgid, location_t loc)
12183 if (!warn_double_promotion)
12185 /* If the conversion will not occur at run-time, there is no need to
12187 if (c_inhibit_evaluation_warnings)
12189 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12190 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12192 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12193 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12194 source_type = type1;
12195 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12196 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12197 source_type = type2;
12200 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12203 /* Possibly warn about unused parameters. */
12206 do_warn_unused_parameter (tree fn)
12210 for (decl = DECL_ARGUMENTS (fn);
12211 decl; decl = DECL_CHAIN (decl))
12212 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12213 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12214 && !TREE_NO_WARNING (decl))
12215 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12216 "unused parameter %qD", decl);
12220 /* Setup a TYPE_DECL node as a typedef representation.
12222 X is a TYPE_DECL for a typedef statement. Create a brand new
12223 ..._TYPE node (which will be just a variant of the existing
12224 ..._TYPE node with identical properties) and then install X
12225 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12227 The whole point here is to end up with a situation where each
12228 and every ..._TYPE node the compiler creates will be uniquely
12229 associated with AT MOST one node representing a typedef name.
12230 This way, even though the compiler substitutes corresponding
12231 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12232 early on, later parts of the compiler can always do the reverse
12233 translation and get back the corresponding typedef name. For
12236 typedef struct S MY_TYPE;
12239 Later parts of the compiler might only know that `object' was of
12240 type `struct S' if it were not for code just below. With this
12241 code however, later parts of the compiler see something like:
12243 struct S' == struct S
12244 typedef struct S' MY_TYPE;
12247 And they can then deduce (from the node for type struct S') that
12248 the original object declaration was:
12252 Being able to do this is important for proper support of protoize,
12253 and also for generating precise symbolic debugging information
12254 which takes full account of the programmer's (typedef) vocabulary.
12256 Obviously, we don't want to generate a duplicate ..._TYPE node if
12257 the TYPE_DECL node that we are now processing really represents a
12258 standard built-in type. */
12261 set_underlying_type (tree x)
12263 if (x == error_mark_node)
12265 if (DECL_IS_BUILTIN (x))
12267 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12268 TYPE_NAME (TREE_TYPE (x)) = x;
12270 else if (TREE_TYPE (x) != error_mark_node
12271 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12273 tree tt = TREE_TYPE (x);
12274 DECL_ORIGINAL_TYPE (x) = tt;
12275 tt = build_variant_type_copy (tt);
12276 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12277 TYPE_NAME (tt) = x;
12278 TREE_USED (tt) = TREE_USED (x);
12279 TREE_TYPE (x) = tt;
12283 /* Record the types used by the current global variable declaration
12284 being parsed, so that we can decide later to emit their debug info.
12285 Those types are in types_used_by_cur_var_decl, and we are going to
12286 store them in the types_used_by_vars_hash hash table.
12287 DECL is the declaration of the global variable that has been parsed. */
12290 record_types_used_by_current_var_decl (tree decl)
12292 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12294 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
12296 tree type = types_used_by_cur_var_decl->pop ();
12297 types_used_by_var_decl_insert (type, decl);
12301 /* If DECL is a typedef that is declared in the current function,
12302 record it for the purpose of -Wunused-local-typedefs. */
12305 record_locally_defined_typedef (tree decl)
12307 struct c_language_function *l;
12309 if (!warn_unused_local_typedefs
12311 /* if this is not a locally defined typedef then we are not
12313 || !is_typedef_decl (decl)
12314 || !decl_function_context (decl))
12317 l = (struct c_language_function *) cfun->language;
12318 vec_safe_push (l->local_typedefs, decl);
12321 /* If T is a TYPE_DECL declared locally, mark it as used. */
12324 maybe_record_typedef_use (tree t)
12326 if (!is_typedef_decl (t))
12329 TREE_USED (t) = true;
12332 /* Warn if there are some unused locally defined typedefs in the
12333 current function. */
12336 maybe_warn_unused_local_typedefs (void)
12340 /* The number of times we have emitted -Wunused-local-typedefs
12341 warnings. If this is different from errorcount, that means some
12342 unrelated errors have been issued. In which case, we'll avoid
12343 emitting "unused-local-typedefs" warnings. */
12344 static int unused_local_typedefs_warn_count;
12345 struct c_language_function *l;
12350 if ((l = (struct c_language_function *) cfun->language) == NULL)
12353 if (warn_unused_local_typedefs
12354 && errorcount == unused_local_typedefs_warn_count)
12356 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
12357 if (!TREE_USED (decl))
12358 warning_at (DECL_SOURCE_LOCATION (decl),
12359 OPT_Wunused_local_typedefs,
12360 "typedef %qD locally defined but not used", decl);
12361 unused_local_typedefs_warn_count = errorcount;
12364 vec_free (l->local_typedefs);
12367 /* Warn about boolean expression compared with an integer value different
12368 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12369 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12370 are the operands of the comparison. The caller must ensure that
12371 either operand is a boolean expression. */
12374 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12377 if (TREE_CODE_CLASS (code) != tcc_comparison)
12380 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12381 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12385 if (!integer_zerop (cst) && !integer_onep (cst))
12387 int sign = (TREE_CODE (op0) == INTEGER_CST
12388 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
12389 if (code == EQ_EXPR
12390 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12391 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12392 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12393 "with boolean expression is always false", cst);
12395 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12396 "with boolean expression is always true", cst);
12398 else if (integer_zerop (cst) || integer_onep (cst))
12400 /* If the non-constant operand isn't of a boolean type, we
12401 don't want to warn here. */
12402 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12403 /* Handle booleans promoted to integers. */
12404 if (CONVERT_EXPR_P (noncst)
12405 && TREE_TYPE (noncst) == integer_type_node
12406 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12408 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12409 && !truth_value_p (TREE_CODE (noncst)))
12411 /* Do some magic to get the right diagnostics. */
12412 bool flag = TREE_CODE (op0) == INTEGER_CST;
12413 flag = integer_zerop (cst) ? flag : !flag;
12414 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12415 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12416 "with boolean expression is always true", cst);
12417 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12418 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12419 "with boolean expression is always false", cst);
12423 /* Warn if signed left shift overflows. We don't warn
12424 about left-shifting 1 into the sign bit in C++14; cf.
12425 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12426 LOC is a location of the shift; OP0 and OP1 are the operands.
12427 Return true if an overflow is detected, false otherwise. */
12430 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12432 if (TREE_CODE (op0) != INTEGER_CST
12433 || TREE_CODE (op1) != INTEGER_CST)
12436 tree type0 = TREE_TYPE (op0);
12437 unsigned int prec0 = TYPE_PRECISION (type0);
12439 /* Left-hand operand must be signed. */
12440 if (TYPE_UNSIGNED (type0))
12443 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12444 + TREE_INT_CST_LOW (op1));
12445 /* Handle the left-shifting 1 into the sign bit case. */
12446 if (min_prec == prec0 + 1)
12448 /* Never warn for C++14 onwards. */
12449 if (cxx_dialect >= cxx14)
12451 /* Otherwise only if -Wshift-overflow=2. But return
12452 true to signal an overflow for the sake of integer
12453 constant expressions. */
12454 if (warn_shift_overflow < 2)
12458 bool overflowed = min_prec > prec0;
12459 if (overflowed && c_inhibit_evaluation_warnings == 0)
12460 warning_at (loc, OPT_Wshift_overflow_,
12461 "result of %qE requires %u bits to represent, "
12462 "but %qT only has %u bits",
12463 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12464 min_prec, type0, prec0);
12469 /* The C and C++ parsers both use vectors to hold function arguments.
12470 For efficiency, we keep a cache of unused vectors. This is the
12473 typedef vec<tree, va_gc> *tree_gc_vec;
12474 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12476 /* Return a new vector from the cache. If the cache is empty,
12477 allocate a new vector. These vectors are GC'ed, so it is OK if the
12478 pointer is not released.. */
12481 make_tree_vector (void)
12483 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12484 return tree_vector_cache->pop ();
12487 /* Passing 0 to vec::alloc returns NULL, and our callers require
12488 that we always return a non-NULL value. The vector code uses
12489 4 when growing a NULL vector, so we do too. */
12490 vec<tree, va_gc> *v;
12496 /* Release a vector of trees back to the cache. */
12499 release_tree_vector (vec<tree, va_gc> *vec)
12504 vec_safe_push (tree_vector_cache, vec);
12508 /* Get a new tree vector holding a single tree. */
12511 make_tree_vector_single (tree t)
12513 vec<tree, va_gc> *ret = make_tree_vector ();
12514 ret->quick_push (t);
12518 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12521 make_tree_vector_from_list (tree list)
12523 vec<tree, va_gc> *ret = make_tree_vector ();
12524 for (; list; list = TREE_CHAIN (list))
12525 vec_safe_push (ret, TREE_VALUE (list));
12529 /* Get a new tree vector which is a copy of an existing one. */
12532 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12534 vec<tree, va_gc> *ret;
12538 ret = make_tree_vector ();
12539 vec_safe_reserve (ret, vec_safe_length (orig));
12540 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12541 ret->quick_push (t);
12545 /* Return true if KEYWORD starts a type specifier. */
12548 keyword_begins_type_specifier (enum rid keyword)
12552 case RID_AUTO_TYPE:
12564 case RID_DFLOAT128:
12580 if (keyword >= RID_FIRST_INT_N
12581 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12582 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12588 /* Return true if KEYWORD names a type qualifier. */
12591 keyword_is_type_qualifier (enum rid keyword)
12605 /* Return true if KEYWORD names a storage class specifier.
12607 RID_TYPEDEF is not included in this list despite `typedef' being
12608 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12609 such for syntactic convenience only. */
12612 keyword_is_storage_class_specifier (enum rid keyword)
12628 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12631 keyword_is_function_specifier (enum rid keyword)
12645 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12646 declaration-specifier (C99 6.7). */
12649 keyword_is_decl_specifier (enum rid keyword)
12651 if (keyword_is_storage_class_specifier (keyword)
12652 || keyword_is_type_qualifier (keyword)
12653 || keyword_is_function_specifier (keyword))
12660 case RID_CONSTEXPR:
12667 /* Initialize language-specific-bits of tree_contains_struct. */
12670 c_common_init_ts (void)
12672 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12673 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12674 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12677 /* Build a user-defined numeric literal out of an integer constant type VALUE
12678 with identifier SUFFIX. */
12681 build_userdef_literal (tree suffix_id, tree value,
12682 enum overflow_type overflow, tree num_string)
12684 tree literal = make_node (USERDEF_LITERAL);
12685 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12686 USERDEF_LITERAL_VALUE (literal) = value;
12687 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12688 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12692 /* For vector[index], convert the vector to a
12693 pointer of the underlying type. Return true if the resulting
12694 ARRAY_REF should not be an lvalue. */
12697 convert_vector_to_pointer_for_subscript (location_t loc,
12698 tree *vecp, tree index)
12701 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
12703 tree type = TREE_TYPE (*vecp);
12706 ret = !lvalue_p (*vecp);
12707 if (TREE_CODE (index) == INTEGER_CST)
12708 if (!tree_fits_uhwi_p (index)
12709 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12710 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12714 tree tmp = create_tmp_var_raw (type);
12715 DECL_SOURCE_LOCATION (tmp) = loc;
12716 *vecp = c_save_expr (*vecp);
12717 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12719 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12720 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12722 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12723 *vecp, NULL_TREE, NULL_TREE),
12727 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12728 NULL_TREE, NULL_TREE);
12729 SET_EXPR_LOCATION (*vecp, loc);
12730 c_common_mark_addressable_vec (tmp);
12733 c_common_mark_addressable_vec (*vecp);
12734 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12735 type1 = build_pointer_type (TREE_TYPE (*vecp));
12736 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12738 && !DECL_P (*vecp))
12740 /* If the original vector isn't declared may_alias and it
12741 isn't a bare vector look if the subscripting would
12742 alias the vector we subscript, and if not, force ref-all. */
12743 alias_set_type vecset = get_alias_set (*vecp);
12744 alias_set_type sset = get_alias_set (type);
12745 if (!alias_sets_must_conflict_p (sset, vecset)
12746 && !alias_set_subset_of (sset, vecset))
12749 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12750 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12751 *vecp = convert (type, *vecp);
12756 /* Determine which of the operands, if any, is a scalar that needs to be
12757 converted to a vector, for the range of operations. */
12759 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12762 tree type0 = TREE_TYPE (op0);
12763 tree type1 = TREE_TYPE (op1);
12764 bool integer_only_op = false;
12765 enum stv_conv ret = stv_firstarg;
12767 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
12770 /* Most GENERIC binary expressions require homogeneous arguments.
12771 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12772 argument that is a vector and a second one that is a scalar, so
12773 we never return stv_secondarg for them. */
12776 if (TREE_CODE (type0) == INTEGER_TYPE
12777 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12779 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12782 error_at (loc, "conversion of scalar %qT to vector %qT "
12783 "involves truncation", type0, type1);
12787 return stv_firstarg;
12794 integer_only_op = true;
12795 /* ... fall through ... */
12797 case VEC_COND_EXPR:
12802 case TRUNC_DIV_EXPR:
12803 case CEIL_DIV_EXPR:
12804 case FLOOR_DIV_EXPR:
12805 case ROUND_DIV_EXPR:
12806 case EXACT_DIV_EXPR:
12807 case TRUNC_MOD_EXPR:
12808 case FLOOR_MOD_EXPR:
12816 /* What about UNLT_EXPR? */
12817 if (VECTOR_TYPE_P (type0))
12819 ret = stv_secondarg;
12820 std::swap (type0, type1);
12821 std::swap (op0, op1);
12824 if (TREE_CODE (type0) == INTEGER_TYPE
12825 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12827 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12830 error_at (loc, "conversion of scalar %qT to vector %qT "
12831 "involves truncation", type0, type1);
12836 else if (!integer_only_op
12837 /* Allow integer --> real conversion if safe. */
12838 && (TREE_CODE (type0) == REAL_TYPE
12839 || TREE_CODE (type0) == INTEGER_TYPE)
12840 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12842 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12845 error_at (loc, "conversion of scalar %qT to vector %qT "
12846 "involves truncation", type0, type1);
12855 return stv_nothing;
12858 /* Return true iff ALIGN is an integral constant that is a fundamental
12859 alignment, as defined by [basic.align] in the c++-11
12864 [A fundamental alignment is represented by an alignment less than or
12865 equal to the greatest alignment supported by the implementation
12866 in all contexts, which is equal to
12867 alignof(max_align_t)]. */
12870 cxx_fundamental_alignment_p (unsigned align)
12872 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12873 TYPE_ALIGN (long_double_type_node)));
12876 /* Return true if T is a pointer to a zero-sized aggregate. */
12879 pointer_to_zero_sized_aggr_p (tree t)
12881 if (!POINTER_TYPE_P (t))
12884 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12887 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12888 with no library fallback or for an ADDR_EXPR whose operand is such type
12889 issues an error pointing to the location LOC.
12890 Returns true when the expression has been diagnosed and false
12893 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12895 if (TREE_CODE (expr) == ADDR_EXPR)
12896 expr = TREE_OPERAND (expr, 0);
12898 if (TREE_TYPE (expr)
12899 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12901 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12902 false positives for user-declared built-ins such as abs or
12903 strlen, and for C++ operators new and delete.
12904 The c_decl_implicit() test avoids false positives for implicitly
12905 declared built-ins with library fallbacks (such as abs). */
12906 && DECL_BUILT_IN (expr)
12907 && DECL_IS_BUILTIN (expr)
12908 && !c_decl_implicit (expr)
12909 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12911 if (loc == UNKNOWN_LOCATION)
12912 loc = EXPR_LOC_OR_LOC (expr, input_location);
12914 /* Reject arguments that are built-in functions with
12915 no library fallback. */
12916 error_at (loc, "built-in function %qE must be directly called", expr);
12924 /* If we're creating an if-else-if condition chain, first see if we
12925 already have this COND in the CHAIN. If so, warn and don't add COND
12926 into the vector, otherwise add the COND there. LOC is the location
12930 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12932 /* No chain has been created yet. Do nothing. */
12933 if (*chain == NULL)
12936 if (TREE_SIDE_EFFECTS (cond))
12938 /* Uh-oh! This condition has a side-effect, thus invalidates
12939 the whole chain. */
12947 bool found = false;
12948 FOR_EACH_VEC_ELT (**chain, ix, t)
12949 if (operand_equal_p (cond, t, 0))
12951 if (warning_at (loc, OPT_Wduplicated_cond,
12952 "duplicated %<if%> condition"))
12953 inform (EXPR_LOCATION (t), "previously used here");
12959 && !CONSTANT_CLASS_P (cond)
12960 /* Don't infinitely grow the chain. */
12961 && (*chain)->length () < 512)
12962 (*chain)->safe_push (cond);
12965 #include "gt-c-family-c-common.h"