1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2014 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/>. */
22 #include "coretypes.h"
26 #include "fold-const.h"
27 #include "stor-layout.h"
29 #include "stringpool.h"
32 #include "trans-mem.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
45 #include "diagnostic.h"
46 #include "tree-iterator.h"
50 #include "target-def.h"
53 cpp_reader *parse_in; /* Declared in c-pragma.h. */
55 /* The following symbols are subsumed in the c_global_trees array, and
56 listed here individually for documentation purposes.
58 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
60 tree short_integer_type_node;
61 tree long_integer_type_node;
62 tree long_long_integer_type_node;
63 tree int128_integer_type_node;
65 tree short_unsigned_type_node;
66 tree long_unsigned_type_node;
67 tree long_long_unsigned_type_node;
68 tree int128_unsigned_type_node;
70 tree truthvalue_type_node;
71 tree truthvalue_false_node;
72 tree truthvalue_true_node;
74 tree ptrdiff_type_node;
76 tree unsigned_char_type_node;
77 tree signed_char_type_node;
80 tree char16_type_node;
81 tree char32_type_node;
84 tree double_type_node;
85 tree long_double_type_node;
87 tree complex_integer_type_node;
88 tree complex_float_type_node;
89 tree complex_double_type_node;
90 tree complex_long_double_type_node;
92 tree dfloat32_type_node;
93 tree dfloat64_type_node;
94 tree_dfloat128_type_node;
100 tree intTI_type_node;
102 tree unsigned_intQI_type_node;
103 tree unsigned_intHI_type_node;
104 tree unsigned_intSI_type_node;
105 tree unsigned_intDI_type_node;
106 tree unsigned_intTI_type_node;
108 tree widest_integer_literal_type_node;
109 tree widest_unsigned_literal_type_node;
111 Nodes for types `void *' and `const void *'.
113 tree ptr_type_node, const_ptr_type_node;
115 Nodes for types `char *' and `const char *'.
117 tree string_type_node, const_string_type_node;
119 Type `char[SOMENUMBER]'.
120 Used when an array of char is needed and the size is irrelevant.
122 tree char_array_type_node;
124 Type `wchar_t[SOMENUMBER]' or something like it.
125 Used when a wide string literal is created.
127 tree wchar_array_type_node;
129 Type `char16_t[SOMENUMBER]' or something like it.
130 Used when a UTF-16 string literal is created.
132 tree char16_array_type_node;
134 Type `char32_t[SOMENUMBER]' or something like it.
135 Used when a UTF-32 string literal is created.
137 tree char32_array_type_node;
139 Type `int ()' -- used for implicit declaration of functions.
141 tree default_function_type;
143 A VOID_TYPE node, packaged in a TREE_LIST.
147 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
148 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
149 VAR_DECLS, but C++ does.)
151 tree function_name_decl_node;
152 tree pretty_function_name_decl_node;
153 tree c99_function_name_decl_node;
155 Stack of nested function name VAR_DECLs.
157 tree saved_function_name_decls;
161 tree c_global_trees[CTI_MAX];
163 /* Switches common to the C front ends. */
165 /* Nonzero means don't output line number information. */
167 char flag_no_line_commands;
169 /* Nonzero causes -E output not to be done, but directives such as
170 #define that have side effects are still obeyed. */
174 /* Nonzero means dump macros in some fashion. */
176 char flag_dump_macros;
178 /* Nonzero means pass #include lines through to the output. */
180 char flag_dump_includes;
182 /* Nonzero means process PCH files while preprocessing. */
184 bool flag_pch_preprocess;
186 /* The file name to which we should write a precompiled header, or
187 NULL if no header will be written in this compile. */
189 const char *pch_file;
191 /* Nonzero if an ISO standard was selected. It rejects macros in the
195 /* C/ObjC language option variables. */
198 /* Nonzero means allow type mismatches in conditional expressions;
199 just make their values `void'. */
201 int flag_cond_mismatch;
203 /* Nonzero means enable C89 Amendment 1 features. */
207 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
211 /* Nonzero means use the ISO C11 dialect of C. */
215 /* Nonzero means that we have builtin functions, and main is an int. */
220 /* ObjC language option variables. */
223 /* Tells the compiler that this is a special run. Do not perform any
224 compiling, instead we are to test some platform dependent features
225 and output a C header file with appropriate definitions. */
227 int print_struct_values;
229 /* Tells the compiler what is the constant string class for ObjC. */
231 const char *constant_string_class_name;
234 /* C++ language option variables. */
237 /* Nonzero means generate separate instantiation control files and
238 juggle them at link time. */
240 int flag_use_repository;
242 /* The C++ dialect being used. C++98 is the default. */
244 enum cxx_dialect cxx_dialect = cxx98;
246 /* Maximum template instantiation depth. This limit exists to limit the
247 time it takes to notice excessively recursive template instantiations.
249 The default is lower than the 1024 recommended by the C++0x standard
250 because G++ runs out of stack before 1024 with highly recursive template
251 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
253 int max_tinst_depth = 900;
255 /* The elements of `ridpointers' are identifier nodes for the reserved
256 type names and storage classes. It is indexed by a RID_... value. */
259 tree (*make_fname_decl) (location_t, tree, int);
261 /* Nonzero means don't warn about problems that occur when the code is
263 int c_inhibit_evaluation_warnings;
265 /* Whether we are building a boolean conversion inside
266 convert_for_assignment, or some other late binary operation. If
267 build_binary_op is called for C (from code shared by C and C++) in
268 this case, then the operands have already been folded and the
269 result will not be folded again, so C_MAYBE_CONST_EXPR should not
271 bool in_late_binary_op;
273 /* Whether lexing has been completed, so subsequent preprocessor
274 errors should use the compiler's input_location. */
275 bool done_lexing = false;
277 /* Information about how a function name is generated. */
280 tree *const decl; /* pointer to the VAR_DECL. */
281 const unsigned rid; /* RID number for the identifier. */
282 const int pretty; /* How pretty is it? */
285 /* The three ways of getting then name of the current function. */
287 const struct fname_var_t fname_vars[] =
289 /* C99 compliant __func__, must be first. */
290 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
291 /* GCC __FUNCTION__ compliant. */
292 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
293 /* GCC __PRETTY_FUNCTION__ compliant. */
294 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
298 /* Global visibility options. */
299 struct visibility_flags visibility_options;
301 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *);
302 static tree check_case_value (tree);
303 static bool check_case_bounds (tree, tree, tree *, tree *);
305 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
306 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
307 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
313 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
315 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
317 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
318 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
319 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_always_inline_attribute (tree *, tree, tree, int,
322 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
330 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_transparent_union_attribute (tree *, tree, tree,
333 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
334 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
339 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
340 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
343 static tree handle_visibility_attribute (tree *, tree, tree, int,
345 static tree handle_tls_model_attribute (tree *, tree, tree, int,
347 static tree handle_no_instrument_function_attribute (tree *, tree,
349 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
353 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
354 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_deprecated_attribute (tree *, tree, tree, int,
359 static tree handle_vector_size_attribute (tree *, tree, tree, int,
361 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
366 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
370 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
373 static tree ignore_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
380 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
383 static void check_function_nonnull (tree, int, tree *);
384 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
385 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
386 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
387 static int resort_field_decl_cmp (const void *, const void *);
389 /* Reserved words. The third field is a mask: keywords are disabled
390 if they match the mask.
393 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
394 C --std=c99: D_CXXONLY | D_OBJC
395 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
396 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
397 C++ --std=c0x: D_CONLY | D_OBJC
398 ObjC++ is like C++ except that D_OBJC is not set
400 If -fno-asm is used, D_ASM is added to the mask. If
401 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
402 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
403 In C with -Wc++-compat, we warn if D_CXXWARN is set.
405 Note the complication of the D_CXX_OBJC keywords. These are
406 reserved words such as 'class'. In C++, 'class' is a reserved
407 word. In Objective-C++ it is too. In Objective-C, it is a
408 reserved word too, but only if it follows an '@' sign.
410 const struct c_common_resword c_common_reswords[] =
412 { "_Alignas", RID_ALIGNAS, D_CONLY },
413 { "_Alignof", RID_ALIGNOF, D_CONLY },
414 { "_Atomic", RID_ATOMIC, D_CONLY },
415 { "_Bool", RID_BOOL, D_CONLY },
416 { "_Complex", RID_COMPLEX, 0 },
417 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
418 { "_Cilk_sync", RID_CILK_SYNC, 0 },
419 { "_Imaginary", RID_IMAGINARY, D_CONLY },
420 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
421 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
422 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
423 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
424 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
425 { "_Sat", RID_SAT, D_CONLY | D_EXT },
426 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
427 { "_Noreturn", RID_NORETURN, D_CONLY },
428 { "_Generic", RID_GENERIC, D_CONLY },
429 { "_Thread_local", RID_THREAD, D_CONLY },
430 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
431 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
432 { "__alignof", RID_ALIGNOF, 0 },
433 { "__alignof__", RID_ALIGNOF, 0 },
434 { "__asm", RID_ASM, 0 },
435 { "__asm__", RID_ASM, 0 },
436 { "__attribute", RID_ATTRIBUTE, 0 },
437 { "__attribute__", RID_ATTRIBUTE, 0 },
438 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
439 { "__bases", RID_BASES, D_CXXONLY },
440 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
441 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
442 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
443 { "__builtin_offsetof", RID_OFFSETOF, 0 },
444 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
445 { "__builtin_va_arg", RID_VA_ARG, 0 },
446 { "__complex", RID_COMPLEX, 0 },
447 { "__complex__", RID_COMPLEX, 0 },
448 { "__const", RID_CONST, 0 },
449 { "__const__", RID_CONST, 0 },
450 { "__decltype", RID_DECLTYPE, D_CXXONLY },
451 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
452 { "__extension__", RID_EXTENSION, 0 },
453 { "__func__", RID_C99_FUNCTION_NAME, 0 },
454 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
455 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
456 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
457 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
458 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
459 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
460 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
461 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
462 { "__imag", RID_IMAGPART, 0 },
463 { "__imag__", RID_IMAGPART, 0 },
464 { "__inline", RID_INLINE, 0 },
465 { "__inline__", RID_INLINE, 0 },
466 { "__int128", RID_INT128, 0 },
467 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
468 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
469 { "__is_class", RID_IS_CLASS, D_CXXONLY },
470 { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, D_CXXONLY },
471 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
472 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
473 { "__is_final", RID_IS_FINAL, D_CXXONLY },
474 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
475 { "__is_pod", RID_IS_POD, D_CXXONLY },
476 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
477 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
478 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
479 { "__is_union", RID_IS_UNION, D_CXXONLY },
480 { "__label__", RID_LABEL, 0 },
481 { "__null", RID_NULL, 0 },
482 { "__real", RID_REALPART, 0 },
483 { "__real__", RID_REALPART, 0 },
484 { "__restrict", RID_RESTRICT, 0 },
485 { "__restrict__", RID_RESTRICT, 0 },
486 { "__signed", RID_SIGNED, 0 },
487 { "__signed__", RID_SIGNED, 0 },
488 { "__thread", RID_THREAD, 0 },
489 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
490 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
491 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
492 { "__typeof", RID_TYPEOF, 0 },
493 { "__typeof__", RID_TYPEOF, 0 },
494 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
495 { "__volatile", RID_VOLATILE, 0 },
496 { "__volatile__", RID_VOLATILE, 0 },
497 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
498 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
499 { "asm", RID_ASM, D_ASM },
500 { "auto", RID_AUTO, 0 },
501 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
502 { "break", RID_BREAK, 0 },
503 { "case", RID_CASE, 0 },
504 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
505 { "char", RID_CHAR, 0 },
506 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
507 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
508 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
509 { "const", RID_CONST, 0 },
510 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
511 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
512 { "continue", RID_CONTINUE, 0 },
513 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
514 { "default", RID_DEFAULT, 0 },
515 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
517 { "double", RID_DOUBLE, 0 },
518 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
519 { "else", RID_ELSE, 0 },
520 { "enum", RID_ENUM, 0 },
521 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
522 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
523 { "extern", RID_EXTERN, 0 },
524 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
525 { "float", RID_FLOAT, 0 },
526 { "for", RID_FOR, 0 },
527 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
528 { "goto", RID_GOTO, 0 },
530 { "inline", RID_INLINE, D_EXT89 },
531 { "int", RID_INT, 0 },
532 { "long", RID_LONG, 0 },
533 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
534 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
535 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
536 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
537 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
538 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
539 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
540 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
541 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
542 { "register", RID_REGISTER, 0 },
543 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
544 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
545 { "return", RID_RETURN, 0 },
546 { "short", RID_SHORT, 0 },
547 { "signed", RID_SIGNED, 0 },
548 { "sizeof", RID_SIZEOF, 0 },
549 { "static", RID_STATIC, 0 },
550 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
551 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
552 { "struct", RID_STRUCT, 0 },
553 { "switch", RID_SWITCH, 0 },
554 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
555 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
556 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
557 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
558 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
559 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
560 { "typedef", RID_TYPEDEF, 0 },
561 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
562 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
563 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
564 { "union", RID_UNION, 0 },
565 { "unsigned", RID_UNSIGNED, 0 },
566 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
567 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
568 { "void", RID_VOID, 0 },
569 { "volatile", RID_VOLATILE, 0 },
570 { "wchar_t", RID_WCHAR, D_CXXONLY },
571 { "while", RID_WHILE, 0 },
572 /* These Objective-C keywords are recognized only immediately after
574 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
575 { "defs", RID_AT_DEFS, D_OBJC },
576 { "encode", RID_AT_ENCODE, D_OBJC },
577 { "end", RID_AT_END, D_OBJC },
578 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
579 { "interface", RID_AT_INTERFACE, D_OBJC },
580 { "protocol", RID_AT_PROTOCOL, D_OBJC },
581 { "selector", RID_AT_SELECTOR, D_OBJC },
582 { "finally", RID_AT_FINALLY, D_OBJC },
583 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
584 { "optional", RID_AT_OPTIONAL, D_OBJC },
585 { "required", RID_AT_REQUIRED, D_OBJC },
586 { "property", RID_AT_PROPERTY, D_OBJC },
587 { "package", RID_AT_PACKAGE, D_OBJC },
588 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
589 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
590 /* These are recognized only in protocol-qualifier context
592 { "bycopy", RID_BYCOPY, D_OBJC },
593 { "byref", RID_BYREF, D_OBJC },
594 { "in", RID_IN, D_OBJC },
595 { "inout", RID_INOUT, D_OBJC },
596 { "oneway", RID_ONEWAY, D_OBJC },
597 { "out", RID_OUT, D_OBJC },
598 /* These are recognized inside a property attribute list */
599 { "assign", RID_ASSIGN, D_OBJC },
600 { "copy", RID_COPY, D_OBJC },
601 { "getter", RID_GETTER, D_OBJC },
602 { "nonatomic", RID_NONATOMIC, D_OBJC },
603 { "readonly", RID_READONLY, D_OBJC },
604 { "readwrite", RID_READWRITE, D_OBJC },
605 { "retain", RID_RETAIN, D_OBJC },
606 { "setter", RID_SETTER, D_OBJC },
609 const unsigned int num_c_common_reswords =
610 sizeof c_common_reswords / sizeof (struct c_common_resword);
612 /* Table of machine-independent attributes common to all C-like languages. */
613 const struct attribute_spec c_common_attribute_table[] =
615 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
616 affects_type_identity } */
617 { "packed", 0, 0, false, false, false,
618 handle_packed_attribute , false},
619 { "nocommon", 0, 0, true, false, false,
620 handle_nocommon_attribute, false},
621 { "common", 0, 0, true, false, false,
622 handle_common_attribute, false },
623 /* FIXME: logically, noreturn attributes should be listed as
624 "false, true, true" and apply to function types. But implementing this
625 would require all the places in the compiler that use TREE_THIS_VOLATILE
626 on a decl to identify non-returning functions to be located and fixed
627 to check the function type instead. */
628 { "noreturn", 0, 0, true, false, false,
629 handle_noreturn_attribute, false },
630 { "volatile", 0, 0, true, false, false,
631 handle_noreturn_attribute, false },
632 { "noinline", 0, 0, true, false, false,
633 handle_noinline_attribute, false },
634 { "noclone", 0, 0, true, false, false,
635 handle_noclone_attribute, false },
636 { "leaf", 0, 0, true, false, false,
637 handle_leaf_attribute, false },
638 { "always_inline", 0, 0, true, false, false,
639 handle_always_inline_attribute, false },
640 { "gnu_inline", 0, 0, true, false, false,
641 handle_gnu_inline_attribute, false },
642 { "artificial", 0, 0, true, false, false,
643 handle_artificial_attribute, false },
644 { "flatten", 0, 0, true, false, false,
645 handle_flatten_attribute, false },
646 { "used", 0, 0, true, false, false,
647 handle_used_attribute, false },
648 { "unused", 0, 0, false, false, false,
649 handle_unused_attribute, false },
650 { "externally_visible", 0, 0, true, false, false,
651 handle_externally_visible_attribute, false },
652 /* The same comments as for noreturn attributes apply to const ones. */
653 { "const", 0, 0, true, false, false,
654 handle_const_attribute, false },
655 { "transparent_union", 0, 0, false, false, false,
656 handle_transparent_union_attribute, false },
657 { "constructor", 0, 1, true, false, false,
658 handle_constructor_attribute, false },
659 { "destructor", 0, 1, true, false, false,
660 handle_destructor_attribute, false },
661 { "mode", 1, 1, false, true, false,
662 handle_mode_attribute, false },
663 { "section", 1, 1, true, false, false,
664 handle_section_attribute, false },
665 { "aligned", 0, 1, false, false, false,
666 handle_aligned_attribute, false },
667 { "weak", 0, 0, true, false, false,
668 handle_weak_attribute, false },
669 { "ifunc", 1, 1, true, false, false,
670 handle_ifunc_attribute, false },
671 { "alias", 1, 1, true, false, false,
672 handle_alias_attribute, false },
673 { "weakref", 0, 1, true, false, false,
674 handle_weakref_attribute, false },
675 { "no_instrument_function", 0, 0, true, false, false,
676 handle_no_instrument_function_attribute,
678 { "malloc", 0, 0, true, false, false,
679 handle_malloc_attribute, false },
680 { "returns_twice", 0, 0, true, false, false,
681 handle_returns_twice_attribute, false },
682 { "no_stack_limit", 0, 0, true, false, false,
683 handle_no_limit_stack_attribute, false },
684 { "pure", 0, 0, true, false, false,
685 handle_pure_attribute, false },
686 { "transaction_callable", 0, 0, false, true, false,
687 handle_tm_attribute, false },
688 { "transaction_unsafe", 0, 0, false, true, false,
689 handle_tm_attribute, false },
690 { "transaction_safe", 0, 0, false, true, false,
691 handle_tm_attribute, false },
692 { "transaction_may_cancel_outer", 0, 0, false, true, false,
693 handle_tm_attribute, false },
694 /* ??? These two attributes didn't make the transition from the
695 Intel language document to the multi-vendor language document. */
696 { "transaction_pure", 0, 0, false, true, false,
697 handle_tm_attribute, false },
698 { "transaction_wrap", 1, 1, true, false, false,
699 handle_tm_wrap_attribute, false },
700 /* For internal use (marking of builtins) only. The name contains space
701 to prevent its usage in source code. */
702 { "no vops", 0, 0, true, false, false,
703 handle_novops_attribute, false },
704 { "deprecated", 0, 1, false, false, false,
705 handle_deprecated_attribute, false },
706 { "vector_size", 1, 1, false, true, false,
707 handle_vector_size_attribute, false },
708 { "visibility", 1, 1, false, false, false,
709 handle_visibility_attribute, false },
710 { "tls_model", 1, 1, true, false, false,
711 handle_tls_model_attribute, false },
712 { "nonnull", 0, -1, false, true, true,
713 handle_nonnull_attribute, false },
714 { "nothrow", 0, 0, true, false, false,
715 handle_nothrow_attribute, false },
716 { "may_alias", 0, 0, false, true, false, NULL, false },
717 { "cleanup", 1, 1, true, false, false,
718 handle_cleanup_attribute, false },
719 { "warn_unused_result", 0, 0, false, true, true,
720 handle_warn_unused_result_attribute, false },
721 { "sentinel", 0, 1, false, true, true,
722 handle_sentinel_attribute, false },
723 /* For internal use (marking of builtins) only. The name contains space
724 to prevent its usage in source code. */
725 { "type generic", 0, 0, false, true, true,
726 handle_type_generic_attribute, false },
727 { "alloc_size", 1, 2, false, true, true,
728 handle_alloc_size_attribute, false },
729 { "cold", 0, 0, true, false, false,
730 handle_cold_attribute, false },
731 { "hot", 0, 0, true, false, false,
732 handle_hot_attribute, false },
733 { "no_address_safety_analysis",
734 0, 0, true, false, false,
735 handle_no_address_safety_analysis_attribute,
737 { "no_sanitize_address", 0, 0, true, false, false,
738 handle_no_sanitize_address_attribute,
740 { "no_sanitize_undefined", 0, 0, true, false, false,
741 handle_no_sanitize_undefined_attribute,
743 { "warning", 1, 1, true, false, false,
744 handle_error_attribute, false },
745 { "error", 1, 1, true, false, false,
746 handle_error_attribute, false },
747 { "target", 1, -1, true, false, false,
748 handle_target_attribute, false },
749 { "optimize", 1, -1, true, false, false,
750 handle_optimize_attribute, false },
751 /* For internal use only. The leading '*' both prevents its usage in
752 source code and signals that it may be overridden by machine tables. */
753 { "*tm regparm", 0, 0, false, true, true,
754 ignore_attribute, false },
755 { "no_split_stack", 0, 0, true, false, false,
756 handle_no_split_stack_attribute, false },
757 /* For internal use (marking of builtins and runtime functions) only.
758 The name contains space to prevent its usage in source code. */
759 { "fn spec", 1, 1, false, true, true,
760 handle_fnspec_attribute, false },
761 { "warn_unused", 0, 0, false, false, false,
762 handle_warn_unused_attribute, false },
763 { "returns_nonnull", 0, 0, false, true, true,
764 handle_returns_nonnull_attribute, false },
765 { "omp declare simd", 0, -1, true, false, false,
766 handle_omp_declare_simd_attribute, false },
767 { "cilk simd function", 0, -1, true, false, false,
768 handle_omp_declare_simd_attribute, false },
769 { "omp declare target", 0, 0, true, false, false,
770 handle_omp_declare_target_attribute, false },
771 { "alloc_align", 1, 1, false, true, true,
772 handle_alloc_align_attribute, false },
773 { "assume_aligned", 1, 2, false, true, true,
774 handle_assume_aligned_attribute, false },
775 { NULL, 0, 0, false, false, false, NULL, false }
778 /* Give the specifications for the format attributes, used by C and all
781 const struct attribute_spec c_common_format_attribute_table[] =
783 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
784 affects_type_identity } */
785 { "format", 3, 3, false, true, true,
786 handle_format_attribute, false },
787 { "format_arg", 1, 1, false, true, true,
788 handle_format_arg_attribute, false },
789 { NULL, 0, 0, false, false, false, NULL, false }
792 /* Return identifier for address space AS. */
795 c_addr_space_name (addr_space_t as)
797 int rid = RID_FIRST_ADDR_SPACE + as;
798 gcc_assert (ridpointers [rid]);
799 return IDENTIFIER_POINTER (ridpointers [rid]);
802 /* Push current bindings for the function name VAR_DECLS. */
805 start_fname_decls (void)
808 tree saved = NULL_TREE;
810 for (ix = 0; fname_vars[ix].decl; ix++)
812 tree decl = *fname_vars[ix].decl;
816 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
818 *fname_vars[ix].decl = NULL_TREE;
821 if (saved || saved_function_name_decls)
822 /* Normally they'll have been NULL, so only push if we've got a
823 stack, or they are non-NULL. */
824 saved_function_name_decls = tree_cons (saved, NULL_TREE,
825 saved_function_name_decls);
828 /* Finish up the current bindings, adding them into the current function's
829 statement tree. This must be done _before_ finish_stmt_tree is called.
830 If there is no current function, we must be at file scope and no statements
831 are involved. Pop the previous bindings. */
834 finish_fname_decls (void)
837 tree stmts = NULL_TREE;
838 tree stack = saved_function_name_decls;
840 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
841 append_to_statement_list (TREE_VALUE (stack), &stmts);
845 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
847 if (TREE_CODE (*bodyp) == BIND_EXPR)
848 bodyp = &BIND_EXPR_BODY (*bodyp);
850 append_to_statement_list_force (*bodyp, &stmts);
854 for (ix = 0; fname_vars[ix].decl; ix++)
855 *fname_vars[ix].decl = NULL_TREE;
859 /* We had saved values, restore them. */
862 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
864 tree decl = TREE_PURPOSE (saved);
865 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
867 *fname_vars[ix].decl = decl;
869 stack = TREE_CHAIN (stack);
871 saved_function_name_decls = stack;
874 /* Return the text name of the current function, suitably prettified
875 by PRETTY_P. Return string must be freed by caller. */
878 fname_as_string (int pretty_p)
880 const char *name = "top level";
883 cpp_string cstr = { 0, 0 }, strname;
891 if (current_function_decl)
892 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
894 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
896 namep = XNEWVEC (char, len);
897 snprintf (namep, len, "\"%s\"", name);
898 strname.text = (unsigned char *) namep;
899 strname.len = len - 1;
901 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
904 return (const char *) cstr.text;
910 /* Return the VAR_DECL for a const char array naming the current
911 function. If the VAR_DECL has not yet been created, create it
912 now. RID indicates how it should be formatted and IDENTIFIER_NODE
913 ID is its name (unfortunately C and C++ hold the RID values of
914 keywords in different places, so we can't derive RID from ID in
915 this language independent code. LOC is the location of the
919 fname_decl (location_t loc, unsigned int rid, tree id)
922 tree decl = NULL_TREE;
924 for (ix = 0; fname_vars[ix].decl; ix++)
925 if (fname_vars[ix].rid == rid)
928 decl = *fname_vars[ix].decl;
931 /* If a tree is built here, it would normally have the lineno of
932 the current statement. Later this tree will be moved to the
933 beginning of the function and this line number will be wrong.
934 To avoid this problem set the lineno to 0 here; that prevents
935 it from appearing in the RTL. */
937 location_t saved_location = input_location;
938 input_location = UNKNOWN_LOCATION;
940 stmts = push_stmt_list ();
941 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
942 stmts = pop_stmt_list (stmts);
943 if (!IS_EMPTY_STMT (stmts))
944 saved_function_name_decls
945 = tree_cons (decl, stmts, saved_function_name_decls);
946 *fname_vars[ix].decl = decl;
947 input_location = saved_location;
949 if (!ix && !current_function_decl)
950 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
955 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
958 fix_string_type (tree value)
960 int length = TREE_STRING_LENGTH (value);
962 tree e_type, i_type, a_type;
964 /* Compute the number of elements, for the array type. */
965 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
968 e_type = char_type_node;
970 else if (TREE_TYPE (value) == char16_array_type_node)
972 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
973 e_type = char16_type_node;
975 else if (TREE_TYPE (value) == char32_array_type_node)
977 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
978 e_type = char32_type_node;
982 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
983 e_type = wchar_type_node;
986 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
987 limit in C++98 Annex B is very large (65536) and is not normative,
988 so we do not diagnose it (warn_overlength_strings is forced off
989 in c_common_post_options). */
990 if (warn_overlength_strings)
992 const int nchars_max = flag_isoc99 ? 4095 : 509;
993 const int relevant_std = flag_isoc99 ? 99 : 90;
994 if (nchars - 1 > nchars_max)
995 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
996 separate the %d from the 'C'. 'ISO' should not be
997 translated, but it may be moved after 'C%d' in languages
998 where modifiers follow nouns. */
999 pedwarn (input_location, OPT_Woverlength_strings,
1000 "string length %qd is greater than the length %qd "
1001 "ISO C%d compilers are required to support",
1002 nchars - 1, nchars_max, relevant_std);
1005 /* Create the array type for the string constant. The ISO C++
1006 standard says that a string literal has type `const char[N]' or
1007 `const wchar_t[N]'. We use the same logic when invoked as a C
1008 front-end with -Wwrite-strings.
1009 ??? We should change the type of an expression depending on the
1010 state of a warning flag. We should just be warning -- see how
1011 this is handled in the C++ front-end for the deprecated implicit
1012 conversion from string literals to `char*' or `wchar_t*'.
1014 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1015 array type being the unqualified version of that type.
1016 Therefore, if we are constructing an array of const char, we must
1017 construct the matching unqualified array type first. The C front
1018 end does not require this, but it does no harm, so we do it
1020 i_type = build_index_type (size_int (nchars - 1));
1021 a_type = build_array_type (e_type, i_type);
1022 if (c_dialect_cxx() || warn_write_strings)
1023 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1025 TREE_TYPE (value) = a_type;
1026 TREE_CONSTANT (value) = 1;
1027 TREE_READONLY (value) = 1;
1028 TREE_STATIC (value) = 1;
1032 /* If DISABLE is true, stop issuing warnings. This is used when
1033 parsing code that we know will not be executed. This function may
1034 be called multiple times, and works as a stack. */
1037 c_disable_warnings (bool disable)
1041 ++c_inhibit_evaluation_warnings;
1042 fold_defer_overflow_warnings ();
1046 /* If ENABLE is true, reenable issuing warnings. */
1049 c_enable_warnings (bool enable)
1053 --c_inhibit_evaluation_warnings;
1054 fold_undefer_and_ignore_overflow_warnings ();
1058 /* Fully fold EXPR, an expression that was not folded (beyond integer
1059 constant expressions and null pointer constants) when being built
1060 up. If IN_INIT, this is in a static initializer and certain
1061 changes are made to the folding done. Clear *MAYBE_CONST if
1062 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1063 expression because it contains an evaluated operator (in C99) or an
1064 operator outside of sizeof returning an integer constant (in C90)
1065 not permitted in constant expressions, or because it contains an
1066 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1067 set to true by callers before calling this function.) Return the
1068 folded expression. Function arguments have already been folded
1069 before calling this function, as have the contents of SAVE_EXPR,
1070 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1071 C_MAYBE_CONST_EXPR. */
1074 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1077 tree eptype = NULL_TREE;
1079 bool maybe_const_itself = true;
1080 location_t loc = EXPR_LOCATION (expr);
1082 /* This function is not relevant to C++ because C++ folds while
1083 parsing, and may need changes to be correct for C++ when C++
1084 stops folding while parsing. */
1085 if (c_dialect_cxx ())
1089 maybe_const = &dummy;
1090 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1092 eptype = TREE_TYPE (expr);
1093 expr = TREE_OPERAND (expr, 0);
1095 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1096 &maybe_const_itself);
1098 ret = fold_convert_loc (loc, eptype, ret);
1099 *maybe_const &= maybe_const_itself;
1103 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1104 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1105 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1106 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1107 both evaluated and unevaluated subexpressions while
1108 *MAYBE_CONST_ITSELF is carried from only evaluated
1112 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1113 bool *maybe_const_itself)
1116 enum tree_code code = TREE_CODE (expr);
1117 enum tree_code_class kind = TREE_CODE_CLASS (code);
1118 location_t loc = EXPR_LOCATION (expr);
1119 tree op0, op1, op2, op3;
1120 tree orig_op0, orig_op1, orig_op2;
1121 bool op0_const = true, op1_const = true, op2_const = true;
1122 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1123 bool nowarning = TREE_NO_WARNING (expr);
1126 /* This function is not relevant to C++ because C++ folds while
1127 parsing, and may need changes to be correct for C++ when C++
1128 stops folding while parsing. */
1129 if (c_dialect_cxx ())
1132 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1133 anything else not counted as an expression cannot usefully be
1134 folded further at this point. */
1135 if (!IS_EXPR_CODE_CLASS (kind)
1136 || kind == tcc_statement
1137 || code == SAVE_EXPR)
1140 /* Operands of variable-length expressions (function calls) have
1141 already been folded, as have __builtin_* function calls, and such
1142 expressions cannot occur in constant expressions. */
1143 if (kind == tcc_vl_exp)
1145 *maybe_const_operands = false;
1150 if (code == C_MAYBE_CONST_EXPR)
1152 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1153 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1154 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1155 *maybe_const_operands = false;
1156 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1157 *maybe_const_itself = false;
1158 if (pre && !in_init)
1159 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1165 /* Assignment, increment, decrement, function call and comma
1166 operators, and statement expressions, cannot occur in constant
1167 expressions if evaluated / outside of sizeof. (Function calls
1168 were handled above, though VA_ARG_EXPR is treated like a function
1169 call here, and statement expressions are handled through
1170 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1174 case PREDECREMENT_EXPR:
1175 case PREINCREMENT_EXPR:
1176 case POSTDECREMENT_EXPR:
1177 case POSTINCREMENT_EXPR:
1179 *maybe_const_operands = false;
1186 *maybe_const_operands = false;
1194 /* Fold individual tree codes as appropriate. */
1197 case COMPOUND_LITERAL_EXPR:
1198 /* Any non-constancy will have been marked in a containing
1199 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1203 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1204 op1 = TREE_OPERAND (expr, 1);
1205 op2 = TREE_OPERAND (expr, 2);
1206 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1207 maybe_const_itself);
1208 STRIP_TYPE_NOPS (op0);
1209 if (op0 != orig_op0)
1210 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1213 TREE_READONLY (ret) = TREE_READONLY (expr);
1214 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1219 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1220 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1221 op2 = TREE_OPERAND (expr, 2);
1222 op3 = TREE_OPERAND (expr, 3);
1223 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1224 maybe_const_itself);
1225 STRIP_TYPE_NOPS (op0);
1226 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1227 maybe_const_itself);
1228 STRIP_TYPE_NOPS (op1);
1229 op1 = decl_constant_value_for_optimization (op1);
1230 if (op0 != orig_op0 || op1 != orig_op1)
1231 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1234 TREE_READONLY (ret) = TREE_READONLY (expr);
1235 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1236 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1243 case PREDECREMENT_EXPR:
1244 case PREINCREMENT_EXPR:
1245 case POSTDECREMENT_EXPR:
1246 case POSTINCREMENT_EXPR:
1250 case POINTER_PLUS_EXPR:
1251 case TRUNC_DIV_EXPR:
1253 case FLOOR_DIV_EXPR:
1254 case TRUNC_MOD_EXPR:
1256 case EXACT_DIV_EXPR:
1269 case TRUTH_AND_EXPR:
1271 case TRUTH_XOR_EXPR:
1272 case UNORDERED_EXPR:
1279 /* Binary operations evaluating both arguments (increment and
1280 decrement are binary internally in GCC). */
1281 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1282 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1283 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1284 maybe_const_itself);
1285 STRIP_TYPE_NOPS (op0);
1286 if (code != MODIFY_EXPR
1287 && code != PREDECREMENT_EXPR
1288 && code != PREINCREMENT_EXPR
1289 && code != POSTDECREMENT_EXPR
1290 && code != POSTINCREMENT_EXPR)
1291 op0 = decl_constant_value_for_optimization (op0);
1292 /* The RHS of a MODIFY_EXPR was fully folded when building that
1293 expression for the sake of conversion warnings. */
1294 if (code != MODIFY_EXPR)
1295 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1296 maybe_const_itself);
1297 STRIP_TYPE_NOPS (op1);
1298 op1 = decl_constant_value_for_optimization (op1);
1299 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1301 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1302 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1305 if (TREE_OVERFLOW_P (ret)
1306 && !TREE_OVERFLOW_P (op0)
1307 && !TREE_OVERFLOW_P (op1))
1308 overflow_warning (EXPR_LOCATION (expr), ret);
1309 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1310 && TREE_CODE (orig_op1) != INTEGER_CST
1311 && TREE_CODE (op1) == INTEGER_CST
1312 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1313 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1314 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1315 && c_inhibit_evaluation_warnings == 0)
1317 if (tree_int_cst_sgn (op1) < 0)
1318 warning_at (loc, 0, (code == LSHIFT_EXPR
1319 ? G_("left shift count is negative")
1320 : G_("right shift count is negative")));
1321 else if (compare_tree_int (op1,
1322 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1324 warning_at (loc, 0, (code == LSHIFT_EXPR
1325 ? G_("left shift count >= width of type")
1326 : G_("right shift count >= width of type")));
1331 case FIX_TRUNC_EXPR:
1334 case ADDR_SPACE_CONVERT_EXPR:
1335 case VIEW_CONVERT_EXPR:
1336 case NON_LVALUE_EXPR:
1339 case TRUTH_NOT_EXPR:
1344 /* Unary operations. */
1345 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1346 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1347 maybe_const_itself);
1348 STRIP_TYPE_NOPS (op0);
1349 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1350 op0 = decl_constant_value_for_optimization (op0);
1351 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1352 not prepared to deal with them if they occur in initializers. */
1354 && code == ADDR_EXPR
1355 && (op1 = get_base_address (op0)) != NULL_TREE
1356 && TREE_CODE (op1) == INDIRECT_REF
1357 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1358 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1359 else if (op0 != orig_op0 || in_init)
1361 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1362 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1365 if (code == INDIRECT_REF
1367 && TREE_CODE (ret) == INDIRECT_REF)
1369 TREE_READONLY (ret) = TREE_READONLY (expr);
1370 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1371 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1375 case FIX_TRUNC_EXPR:
1378 /* Don't warn about explicit conversions. We will already
1379 have warned about suspect implicit conversions. */
1383 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1384 overflow_warning (EXPR_LOCATION (expr), ret);
1389 case TRUTH_ANDIF_EXPR:
1390 case TRUTH_ORIF_EXPR:
1391 /* Binary operations not necessarily evaluating both
1393 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1394 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1395 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1396 STRIP_TYPE_NOPS (op0);
1398 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1399 ? truthvalue_false_node
1400 : truthvalue_true_node));
1401 c_disable_warnings (unused_p);
1402 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1403 STRIP_TYPE_NOPS (op1);
1404 c_enable_warnings (unused_p);
1406 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1408 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1409 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1412 *maybe_const_operands &= op0_const;
1413 *maybe_const_itself &= op0_const_self;
1417 && (code == TRUTH_ANDIF_EXPR
1418 ? op0 == truthvalue_false_node
1419 : op0 == truthvalue_true_node)))
1420 *maybe_const_operands &= op1_const;
1423 && (code == TRUTH_ANDIF_EXPR
1424 ? op0 == truthvalue_false_node
1425 : op0 == truthvalue_true_node)))
1426 *maybe_const_itself &= op1_const_self;
1430 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1431 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1432 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1433 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self);
1435 STRIP_TYPE_NOPS (op0);
1436 c_disable_warnings (op0 == truthvalue_false_node);
1437 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self);
1438 STRIP_TYPE_NOPS (op1);
1439 c_enable_warnings (op0 == truthvalue_false_node);
1441 c_disable_warnings (op0 == truthvalue_true_node);
1442 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self);
1443 STRIP_TYPE_NOPS (op2);
1444 c_enable_warnings (op0 == truthvalue_true_node);
1446 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1447 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1450 *maybe_const_operands &= op0_const;
1451 *maybe_const_itself &= op0_const_self;
1455 && op0 == truthvalue_false_node))
1456 *maybe_const_operands &= op1_const;
1459 && op0 == truthvalue_false_node))
1460 *maybe_const_itself &= op1_const_self;
1464 && op0 == truthvalue_true_node))
1465 *maybe_const_operands &= op2_const;
1468 && op0 == truthvalue_true_node))
1469 *maybe_const_itself &= op2_const_self;
1472 case EXCESS_PRECISION_EXPR:
1473 /* Each case where an operand with excess precision may be
1474 encountered must remove the EXCESS_PRECISION_EXPR around
1475 inner operands and possibly put one around the whole
1476 expression or possibly convert to the semantic type (which
1477 c_fully_fold does); we cannot tell at this stage which is
1478 appropriate in any particular case. */
1482 /* Various codes may appear through folding built-in functions
1483 and their arguments. */
1488 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1489 have been done by this point, so remove them again. */
1490 nowarning |= TREE_NO_WARNING (ret);
1491 STRIP_TYPE_NOPS (ret);
1492 if (nowarning && !TREE_NO_WARNING (ret))
1494 if (!CAN_HAVE_LOCATION_P (ret))
1495 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1496 TREE_NO_WARNING (ret) = 1;
1499 protected_set_expr_location (ret, loc);
1503 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1504 return EXP. Otherwise, return either EXP or its known constant
1505 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1506 Is the BLKmode test appropriate? */
1509 decl_constant_value_for_optimization (tree exp)
1513 /* This function is only used by C, for c_fully_fold and other
1514 optimization, and may not be correct for C++. */
1515 if (c_dialect_cxx ())
1519 || TREE_CODE (exp) != VAR_DECL
1520 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1521 || DECL_MODE (exp) == BLKmode)
1524 ret = decl_constant_value (exp);
1525 /* Avoid unwanted tree sharing between the initializer and current
1526 function's body where the tree can be modified e.g. by the
1528 if (ret != exp && TREE_STATIC (exp))
1529 ret = unshare_expr (ret);
1533 /* Print a warning if a constant expression had overflow in folding.
1534 Invoke this function on every expression that the language
1535 requires to be a constant expression.
1536 Note the ANSI C standard says it is erroneous for a
1537 constant expression to overflow. */
1540 constant_expression_warning (tree value)
1542 if (warn_overflow && pedantic
1543 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1544 || TREE_CODE (value) == FIXED_CST
1545 || TREE_CODE (value) == VECTOR_CST
1546 || TREE_CODE (value) == COMPLEX_CST)
1547 && TREE_OVERFLOW (value))
1548 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1551 /* The same as above but print an unconditional error. */
1553 constant_expression_error (tree value)
1555 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1556 || TREE_CODE (value) == FIXED_CST
1557 || TREE_CODE (value) == VECTOR_CST
1558 || TREE_CODE (value) == COMPLEX_CST)
1559 && TREE_OVERFLOW (value))
1560 error ("overflow in constant expression");
1563 /* Print a warning if an expression had overflow in folding and its
1566 Invoke this function on every expression that
1567 (1) appears in the source code, and
1568 (2) is a constant expression that overflowed, and
1569 (3) is not already checked by convert_and_check;
1570 however, do not invoke this function on operands of explicit casts
1571 or when the expression is the result of an operator and any operand
1572 already overflowed. */
1575 overflow_warning (location_t loc, tree value)
1577 if (c_inhibit_evaluation_warnings != 0)
1580 switch (TREE_CODE (value))
1583 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1587 warning_at (loc, OPT_Woverflow,
1588 "floating point overflow in expression");
1592 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1596 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1600 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1601 warning_at (loc, OPT_Woverflow,
1602 "complex integer overflow in expression");
1603 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1604 warning_at (loc, OPT_Woverflow,
1605 "complex floating point overflow in expression");
1613 /* Warn about uses of logical || / && operator in a context where it
1614 is likely that the bitwise equivalent was intended by the
1615 programmer. We have seen an expression in which CODE is a binary
1616 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1617 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1619 warn_logical_operator (location_t location, enum tree_code code, tree type,
1620 enum tree_code code_left, tree op_left,
1621 enum tree_code ARG_UNUSED (code_right), tree op_right)
1623 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1624 int in0_p, in1_p, in_p;
1625 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1626 bool strict_overflow_p = false;
1628 if (code != TRUTH_ANDIF_EXPR
1629 && code != TRUTH_AND_EXPR
1630 && code != TRUTH_ORIF_EXPR
1631 && code != TRUTH_OR_EXPR)
1634 /* Warn if &&/|| are being used in a context where it is
1635 likely that the bitwise equivalent was intended by the
1636 programmer. That is, an expression such as op && MASK
1637 where op should not be any boolean expression, nor a
1638 constant, and mask seems to be a non-boolean integer constant. */
1639 if (!truth_value_p (code_left)
1640 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1641 && !CONSTANT_CLASS_P (op_left)
1642 && !TREE_NO_WARNING (op_left)
1643 && TREE_CODE (op_right) == INTEGER_CST
1644 && !integer_zerop (op_right)
1645 && !integer_onep (op_right))
1648 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1649 " applied to non-boolean constant");
1651 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1652 " applied to non-boolean constant");
1653 TREE_NO_WARNING (op_left) = true;
1657 /* We do not warn for constants because they are typical of macro
1658 expansions that test for features. */
1659 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1662 /* This warning only makes sense with logical operands. */
1663 if (!(truth_value_p (TREE_CODE (op_left))
1664 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1665 || !(truth_value_p (TREE_CODE (op_right))
1666 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1670 /* We first test whether either side separately is trivially true
1671 (with OR) or trivially false (with AND). If so, do not warn.
1672 This is a common idiom for testing ranges of data types in
1674 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1677 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1678 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1680 /* If this is an OR operation, invert both sides; now, the result
1681 should be always false to get a warning. */
1685 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1686 if (tem && integer_zerop (tem))
1689 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1692 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1693 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1695 /* If this is an OR operation, invert both sides; now, the result
1696 should be always false to get a warning. */
1700 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1701 if (tem && integer_zerop (tem))
1704 /* If both expressions have the same operand, if we can merge the
1705 ranges, and if the range test is always false, then warn. */
1706 if (operand_equal_p (lhs, rhs, 0)
1707 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1709 && 0 != (tem = build_range_check (UNKNOWN_LOCATION,
1710 type, lhs, in_p, low, high))
1711 && integer_zerop (tem))
1714 warning_at (location, OPT_Wlogical_op,
1716 "of collectively exhaustive tests is always true");
1718 warning_at (location, OPT_Wlogical_op,
1720 "of mutually exclusive tests is always false");
1725 /* Warn if EXP contains any computations whose results are not used.
1726 Return true if a warning is printed; false otherwise. LOCUS is the
1727 (potential) location of the expression. */
1730 warn_if_unused_value (const_tree exp, location_t locus)
1733 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1736 /* Don't warn about void constructs. This includes casting to void,
1737 void function calls, and statement expressions with a final cast
1739 if (VOID_TYPE_P (TREE_TYPE (exp)))
1742 if (EXPR_HAS_LOCATION (exp))
1743 locus = EXPR_LOCATION (exp);
1745 switch (TREE_CODE (exp))
1747 case PREINCREMENT_EXPR:
1748 case POSTINCREMENT_EXPR:
1749 case PREDECREMENT_EXPR:
1750 case POSTDECREMENT_EXPR:
1755 case TRY_CATCH_EXPR:
1756 case WITH_CLEANUP_EXPR:
1762 /* For a binding, warn if no side effect within it. */
1763 exp = BIND_EXPR_BODY (exp);
1767 case NON_LVALUE_EXPR:
1769 exp = TREE_OPERAND (exp, 0);
1772 case TRUTH_ORIF_EXPR:
1773 case TRUTH_ANDIF_EXPR:
1774 /* In && or ||, warn if 2nd operand has no side effect. */
1775 exp = TREE_OPERAND (exp, 1);
1779 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1781 /* Let people do `(foo (), 0)' without a warning. */
1782 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1784 exp = TREE_OPERAND (exp, 1);
1788 /* If this is an expression with side effects, don't warn; this
1789 case commonly appears in macro expansions. */
1790 if (TREE_SIDE_EFFECTS (exp))
1795 /* Don't warn about automatic dereferencing of references, since
1796 the user cannot control it. */
1797 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1799 exp = TREE_OPERAND (exp, 0);
1805 /* Referencing a volatile value is a side effect, so don't warn. */
1806 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1807 && TREE_THIS_VOLATILE (exp))
1810 /* If this is an expression which has no operands, there is no value
1811 to be unused. There are no such language-independent codes,
1812 but front ends may define such. */
1813 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1817 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1822 /* Print a warning about casts that might indicate violation
1823 of strict aliasing rules if -Wstrict-aliasing is used and
1824 strict aliasing mode is in effect. OTYPE is the original
1825 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1828 strict_aliasing_warning (tree otype, tree type, tree expr)
1830 /* Strip pointer conversion chains and get to the correct original type. */
1832 otype = TREE_TYPE (expr);
1834 if (!(flag_strict_aliasing
1835 && POINTER_TYPE_P (type)
1836 && POINTER_TYPE_P (otype)
1837 && !VOID_TYPE_P (TREE_TYPE (type)))
1838 /* If the type we are casting to is a ref-all pointer
1839 dereferencing it is always valid. */
1840 || TYPE_REF_CAN_ALIAS_ALL (type))
1843 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1844 && (DECL_P (TREE_OPERAND (expr, 0))
1845 || handled_component_p (TREE_OPERAND (expr, 0))))
1847 /* Casting the address of an object to non void pointer. Warn
1848 if the cast breaks type based aliasing. */
1849 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1851 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1852 "might break strict-aliasing rules");
1857 /* warn_strict_aliasing >= 3. This includes the default (3).
1858 Only warn if the cast is dereferenced immediately. */
1859 alias_set_type set1 =
1860 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1861 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1863 if (set1 != set2 && set2 != 0
1864 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
1866 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1867 "pointer will break strict-aliasing rules");
1870 else if (warn_strict_aliasing == 2
1871 && !alias_sets_must_conflict_p (set1, set2))
1873 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1874 "pointer might break strict-aliasing rules");
1880 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1882 /* At this level, warn for any conversions, even if an address is
1883 not taken in the same statement. This will likely produce many
1884 false positives, but could be useful to pinpoint problems that
1885 are not revealed at higher levels. */
1886 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1887 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1888 if (!COMPLETE_TYPE_P (type)
1889 || !alias_sets_must_conflict_p (set1, set2))
1891 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1892 "pointer might break strict-aliasing rules");
1900 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1901 sizeof as last operand of certain builtins. */
1904 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1905 vec<tree, va_gc> *params, tree *sizeof_arg,
1906 bool (*comp_types) (tree, tree))
1908 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1909 bool strop = false, cmp = false;
1910 unsigned int idx = ~0;
1913 if (TREE_CODE (callee) != FUNCTION_DECL
1914 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1915 || vec_safe_length (params) <= 1)
1918 switch (DECL_FUNCTION_CODE (callee))
1920 case BUILT_IN_STRNCMP:
1921 case BUILT_IN_STRNCASECMP:
1924 case BUILT_IN_STRNCPY:
1925 case BUILT_IN_STRNCPY_CHK:
1926 case BUILT_IN_STRNCAT:
1927 case BUILT_IN_STRNCAT_CHK:
1928 case BUILT_IN_STPNCPY:
1929 case BUILT_IN_STPNCPY_CHK:
1932 case BUILT_IN_MEMCPY:
1933 case BUILT_IN_MEMCPY_CHK:
1934 case BUILT_IN_MEMMOVE:
1935 case BUILT_IN_MEMMOVE_CHK:
1936 if (params->length () < 3)
1939 dest = (*params)[0];
1942 case BUILT_IN_BCOPY:
1943 if (params->length () < 3)
1946 dest = (*params)[1];
1949 case BUILT_IN_MEMCMP:
1951 if (params->length () < 3)
1954 dest = (*params)[0];
1958 case BUILT_IN_MEMSET:
1959 case BUILT_IN_MEMSET_CHK:
1960 if (params->length () < 3)
1962 dest = (*params)[0];
1965 case BUILT_IN_BZERO:
1966 dest = (*params)[0];
1969 case BUILT_IN_STRNDUP:
1974 case BUILT_IN_MEMCHR:
1975 if (params->length () < 3)
1980 case BUILT_IN_SNPRINTF:
1981 case BUILT_IN_SNPRINTF_CHK:
1982 case BUILT_IN_VSNPRINTF:
1983 case BUILT_IN_VSNPRINTF_CHK:
1984 dest = (*params)[0];
1995 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1998 type = TYPE_P (sizeof_arg[idx])
1999 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2000 if (!POINTER_TYPE_P (type))
2004 && (tem = tree_strip_nop_conversions (dest))
2005 && POINTER_TYPE_P (TREE_TYPE (tem))
2006 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2010 && (tem = tree_strip_nop_conversions (src))
2011 && POINTER_TYPE_P (TREE_TYPE (tem))
2012 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2015 loc = sizeof_arg_loc[idx];
2019 if (!TYPE_P (sizeof_arg[idx])
2020 && operand_equal_p (dest, sizeof_arg[idx], 0)
2021 && comp_types (TREE_TYPE (dest), type))
2023 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2024 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2025 "argument to %<sizeof%> in %qD call is the same "
2026 "expression as the destination; did you mean to "
2027 "remove the addressof?", callee);
2028 else if ((TYPE_PRECISION (TREE_TYPE (type))
2029 == TYPE_PRECISION (char_type_node))
2031 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2032 "argument to %<sizeof%> in %qD call is the same "
2033 "expression as the destination; did you mean to "
2034 "provide an explicit length?", callee);
2036 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2037 "argument to %<sizeof%> in %qD call is the same "
2038 "expression as the destination; did you mean to "
2039 "dereference it?", callee);
2043 if (POINTER_TYPE_P (TREE_TYPE (dest))
2045 && comp_types (TREE_TYPE (dest), type)
2046 && !VOID_TYPE_P (TREE_TYPE (type)))
2048 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2049 "argument to %<sizeof%> in %qD call is the same "
2050 "pointer type %qT as the destination; expected %qT "
2051 "or an explicit length", callee, TREE_TYPE (dest),
2052 TREE_TYPE (TREE_TYPE (dest)));
2059 if (!TYPE_P (sizeof_arg[idx])
2060 && operand_equal_p (src, sizeof_arg[idx], 0)
2061 && comp_types (TREE_TYPE (src), type))
2063 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2064 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2065 "argument to %<sizeof%> in %qD call is the same "
2066 "expression as the source; did you mean to "
2067 "remove the addressof?", callee);
2068 else if ((TYPE_PRECISION (TREE_TYPE (type))
2069 == TYPE_PRECISION (char_type_node))
2071 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2072 "argument to %<sizeof%> in %qD call is the same "
2073 "expression as the source; did you mean to "
2074 "provide an explicit length?", callee);
2076 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2077 "argument to %<sizeof%> in %qD call is the same "
2078 "expression as the source; did you mean to "
2079 "dereference it?", callee);
2083 if (POINTER_TYPE_P (TREE_TYPE (src))
2085 && comp_types (TREE_TYPE (src), type)
2086 && !VOID_TYPE_P (TREE_TYPE (type)))
2088 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2089 "argument to %<sizeof%> in %qD call is the same "
2090 "pointer type %qT as the source; expected %qT "
2091 "or an explicit length", callee, TREE_TYPE (src),
2092 TREE_TYPE (TREE_TYPE (src)));
2099 if (!TYPE_P (sizeof_arg[idx])
2100 && operand_equal_p (dest, sizeof_arg[idx], 0)
2101 && comp_types (TREE_TYPE (dest), type))
2103 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2104 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2105 "argument to %<sizeof%> in %qD call is the same "
2106 "expression as the first source; did you mean to "
2107 "remove the addressof?", callee);
2108 else if ((TYPE_PRECISION (TREE_TYPE (type))
2109 == TYPE_PRECISION (char_type_node))
2111 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2112 "argument to %<sizeof%> in %qD call is the same "
2113 "expression as the first source; did you mean to "
2114 "provide an explicit length?", callee);
2116 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2117 "argument to %<sizeof%> in %qD call is the same "
2118 "expression as the first source; did you mean to "
2119 "dereference it?", callee);
2123 if (POINTER_TYPE_P (TREE_TYPE (dest))
2125 && comp_types (TREE_TYPE (dest), type)
2126 && !VOID_TYPE_P (TREE_TYPE (type)))
2128 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2129 "argument to %<sizeof%> in %qD call is the same "
2130 "pointer type %qT as the first source; expected %qT "
2131 "or an explicit length", callee, TREE_TYPE (dest),
2132 TREE_TYPE (TREE_TYPE (dest)));
2139 if (!TYPE_P (sizeof_arg[idx])
2140 && operand_equal_p (src, sizeof_arg[idx], 0)
2141 && comp_types (TREE_TYPE (src), type))
2143 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2144 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2145 "argument to %<sizeof%> in %qD call is the same "
2146 "expression as the second source; did you mean to "
2147 "remove the addressof?", callee);
2148 else if ((TYPE_PRECISION (TREE_TYPE (type))
2149 == TYPE_PRECISION (char_type_node))
2151 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2152 "argument to %<sizeof%> in %qD call is the same "
2153 "expression as the second source; did you mean to "
2154 "provide an explicit length?", callee);
2156 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2157 "argument to %<sizeof%> in %qD call is the same "
2158 "expression as the second source; did you mean to "
2159 "dereference it?", callee);
2163 if (POINTER_TYPE_P (TREE_TYPE (src))
2165 && comp_types (TREE_TYPE (src), type)
2166 && !VOID_TYPE_P (TREE_TYPE (type)))
2168 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2169 "argument to %<sizeof%> in %qD call is the same "
2170 "pointer type %qT as the second source; expected %qT "
2171 "or an explicit length", callee, TREE_TYPE (src),
2172 TREE_TYPE (TREE_TYPE (src)));
2179 /* Warn for unlikely, improbable, or stupid DECL declarations
2183 check_main_parameter_types (tree decl)
2185 function_args_iterator iter;
2189 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2191 /* XXX void_type_node belies the abstraction. */
2192 if (type == void_type_node || type == error_mark_node )
2199 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2200 pedwarn (input_location, OPT_Wmain,
2201 "first argument of %q+D should be %<int%>", decl);
2205 if (TREE_CODE (type) != POINTER_TYPE
2206 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2207 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2209 pedwarn (input_location, OPT_Wmain,
2210 "second argument of %q+D should be %<char **%>", decl);
2214 if (TREE_CODE (type) != POINTER_TYPE
2215 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2216 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2218 pedwarn (input_location, OPT_Wmain,
2219 "third argument of %q+D should probably be "
2220 "%<char **%>", decl);
2225 /* It is intentional that this message does not mention the third
2226 argument because it's only mentioned in an appendix of the
2228 if (argct > 0 && (argct < 2 || argct > 3))
2229 pedwarn (input_location, OPT_Wmain,
2230 "%q+D takes only zero or two arguments", decl);
2233 /* vector_targets_convertible_p is used for vector pointer types. The
2234 callers perform various checks that the qualifiers are satisfactory,
2235 while OTOH vector_targets_convertible_p ignores the number of elements
2236 in the vectors. That's fine with vector pointers as we can consider,
2237 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2238 and that does not require and conversion of the pointer values.
2239 In contrast, vector_types_convertible_p and
2240 vector_types_compatible_elements_p are used for vector value types. */
2241 /* True if pointers to distinct types T1 and T2 can be converted to
2242 each other without an explicit cast. Only returns true for opaque
2245 vector_targets_convertible_p (const_tree t1, const_tree t2)
2247 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2248 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2249 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2255 /* vector_types_convertible_p is used for vector value types.
2256 It could in principle call vector_targets_convertible_p as a subroutine,
2257 but then the check for vector type would be duplicated with its callers,
2258 and also the purpose of vector_targets_convertible_p would become
2260 Where vector_types_convertible_p returns true, a conversion might still be
2261 needed to make the types match.
2262 In contrast, vector_targets_convertible_p is used for vector pointer
2263 values, and vector_types_compatible_elements_p is used specifically
2264 in the context for binary operators, as a check if use is possible without
2266 /* True if vector types T1 and T2 can be converted to each other
2267 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2268 can only be converted with -flax-vector-conversions yet that is not
2269 in effect, emit a note telling the user about that option if such
2270 a note has not previously been emitted. */
2272 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2274 static bool emitted_lax_note = false;
2275 bool convertible_lax;
2277 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2278 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2282 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2283 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2284 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2285 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2286 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2288 if (!convertible_lax || flag_lax_vector_conversions)
2289 return convertible_lax;
2291 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2292 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2295 if (emit_lax_note && !emitted_lax_note)
2297 emitted_lax_note = true;
2298 inform (input_location, "use -flax-vector-conversions to permit "
2299 "conversions between vectors with differing "
2300 "element types or numbers of subparts");
2306 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2307 and have vector types, V0 has the same type as V1, and the number of
2308 elements of V0, V1, MASK is the same.
2310 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2311 called with two arguments. In this case implementation passes the
2312 first argument twice in order to share the same tree code. This fact
2313 could enable the mask-values being twice the vector length. This is
2314 an implementation accident and this semantics is not guaranteed to
2317 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2322 bool maybe_const = false;
2323 bool two_arguments = false;
2325 if (v1 == NULL_TREE)
2327 two_arguments = true;
2331 if (v0 == error_mark_node || v1 == error_mark_node
2332 || mask == error_mark_node)
2333 return error_mark_node;
2335 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2336 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2339 error_at (loc, "__builtin_shuffle last argument must "
2340 "be an integer vector");
2341 return error_mark_node;
2344 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2345 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2348 error_at (loc, "__builtin_shuffle arguments must be vectors");
2349 return error_mark_node;
2352 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2355 error_at (loc, "__builtin_shuffle argument vectors must be of "
2357 return error_mark_node;
2360 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2361 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2362 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2363 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2366 error_at (loc, "__builtin_shuffle number of elements of the "
2367 "argument vector(s) and the mask vector should "
2369 return error_mark_node;
2372 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2373 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2376 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2377 "must have the same size as inner type of the mask");
2378 return error_mark_node;
2381 if (!c_dialect_cxx ())
2383 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2384 v0 = c_fully_fold (v0, false, &maybe_const);
2385 wrap &= maybe_const;
2388 v1 = v0 = save_expr (v0);
2391 v1 = c_fully_fold (v1, false, &maybe_const);
2392 wrap &= maybe_const;
2395 mask = c_fully_fold (mask, false, &maybe_const);
2396 wrap &= maybe_const;
2398 else if (two_arguments)
2399 v1 = v0 = save_expr (v0);
2401 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2403 if (!c_dialect_cxx () && !wrap)
2404 ret = c_wrap_maybe_const (ret, true);
2409 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2410 to integral type. */
2413 c_common_get_narrower (tree op, int *unsignedp_ptr)
2415 op = get_narrower (op, unsignedp_ptr);
2417 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2418 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2420 /* C++0x scoped enumerations don't implicitly convert to integral
2421 type; if we stripped an explicit conversion to a larger type we
2422 need to replace it so common_type will still work. */
2423 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2424 TYPE_UNSIGNED (TREE_TYPE (op)));
2425 op = fold_convert (type, op);
2430 /* This is a helper function of build_binary_op.
2432 For certain operations if both args were extended from the same
2433 smaller type, do the arithmetic in that type and then extend.
2435 BITWISE indicates a bitwise operation.
2436 For them, this optimization is safe only if
2437 both args are zero-extended or both are sign-extended.
2438 Otherwise, we might change the result.
2439 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2440 but calculated in (unsigned short) it would be (unsigned short)-1.
2443 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2445 int unsigned0, unsigned1;
2450 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2451 excessive narrowing when we call get_narrower below. For
2452 example, suppose that OP0 is of unsigned int extended
2453 from signed char and that RESULT_TYPE is long long int.
2454 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2457 (long long int) (unsigned int) signed_char
2459 which get_narrower would narrow down to
2461 (unsigned int) signed char
2463 If we do not cast OP0 first, get_narrower would return
2464 signed_char, which is inconsistent with the case of the
2466 op0 = convert (result_type, op0);
2467 op1 = convert (result_type, op1);
2469 arg0 = c_common_get_narrower (op0, &unsigned0);
2470 arg1 = c_common_get_narrower (op1, &unsigned1);
2472 /* UNS is 1 if the operation to be done is an unsigned one. */
2473 uns = TYPE_UNSIGNED (result_type);
2475 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2476 but it *requires* conversion to FINAL_TYPE. */
2478 if ((TYPE_PRECISION (TREE_TYPE (op0))
2479 == TYPE_PRECISION (TREE_TYPE (arg0)))
2480 && TREE_TYPE (op0) != result_type)
2481 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2482 if ((TYPE_PRECISION (TREE_TYPE (op1))
2483 == TYPE_PRECISION (TREE_TYPE (arg1)))
2484 && TREE_TYPE (op1) != result_type)
2485 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2487 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2489 /* For bitwise operations, signedness of nominal type
2490 does not matter. Consider only how operands were extended. */
2494 /* Note that in all three cases below we refrain from optimizing
2495 an unsigned operation on sign-extended args.
2496 That would not be valid. */
2498 /* Both args variable: if both extended in same way
2499 from same width, do it in that width.
2500 Do it unsigned if args were zero-extended. */
2501 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2502 < TYPE_PRECISION (result_type))
2503 && (TYPE_PRECISION (TREE_TYPE (arg1))
2504 == TYPE_PRECISION (TREE_TYPE (arg0)))
2505 && unsigned0 == unsigned1
2506 && (unsigned0 || !uns))
2507 return c_common_signed_or_unsigned_type
2508 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2510 else if (TREE_CODE (arg0) == INTEGER_CST
2511 && (unsigned1 || !uns)
2512 && (TYPE_PRECISION (TREE_TYPE (arg1))
2513 < TYPE_PRECISION (result_type))
2515 = c_common_signed_or_unsigned_type (unsigned1,
2517 && !POINTER_TYPE_P (type)
2518 && int_fits_type_p (arg0, type))
2521 else if (TREE_CODE (arg1) == INTEGER_CST
2522 && (unsigned0 || !uns)
2523 && (TYPE_PRECISION (TREE_TYPE (arg0))
2524 < TYPE_PRECISION (result_type))
2526 = c_common_signed_or_unsigned_type (unsigned0,
2528 && !POINTER_TYPE_P (type)
2529 && int_fits_type_p (arg1, type))
2535 /* Checks if expression EXPR of real/integer type cannot be converted
2536 to the real/integer type TYPE. Function returns non-zero when:
2537 * EXPR is a constant which cannot be exactly converted to TYPE.
2538 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2539 for EXPR type and TYPE being both integers or both real.
2540 * EXPR is not a constant of real type and TYPE is an integer.
2541 * EXPR is not a constant of integer type which cannot be
2542 exactly converted to real type.
2543 Function allows conversions between types of different signedness and
2544 can return SAFE_CONVERSION (zero) in that case. Function can produce
2545 signedness warnings if PRODUCE_WARNS is true. */
2547 enum conversion_safety
2548 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2550 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2551 tree expr_type = TREE_TYPE (expr);
2552 loc = expansion_point_location_if_in_system_header (loc);
2554 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2556 /* Warn for real constant that is not an exact integer converted
2558 if (TREE_CODE (expr_type) == REAL_TYPE
2559 && TREE_CODE (type) == INTEGER_TYPE)
2561 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2562 give_warning = UNSAFE_REAL;
2564 /* Warn for an integer constant that does not fit into integer type. */
2565 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2566 && TREE_CODE (type) == INTEGER_TYPE
2567 && !int_fits_type_p (expr, type))
2569 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2570 && tree_int_cst_sgn (expr) < 0)
2573 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2574 " implicitly converted to unsigned type");
2576 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2579 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2580 " constant value to negative integer");
2583 give_warning = UNSAFE_OTHER;
2585 else if (TREE_CODE (type) == REAL_TYPE)
2587 /* Warn for an integer constant that does not fit into real type. */
2588 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2590 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2591 if (!exact_real_truncate (TYPE_MODE (type), &a))
2592 give_warning = UNSAFE_REAL;
2594 /* Warn for a real constant that does not fit into a smaller
2596 else if (TREE_CODE (expr_type) == REAL_TYPE
2597 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2599 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2600 if (!exact_real_truncate (TYPE_MODE (type), &a))
2601 give_warning = UNSAFE_REAL;
2607 /* Warn for real types converted to integer types. */
2608 if (TREE_CODE (expr_type) == REAL_TYPE
2609 && TREE_CODE (type) == INTEGER_TYPE)
2610 give_warning = UNSAFE_REAL;
2612 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2613 && TREE_CODE (type) == INTEGER_TYPE)
2615 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2616 expr = get_unwidened (expr, 0);
2617 expr_type = TREE_TYPE (expr);
2619 /* Don't warn for short y; short x = ((int)y & 0xff); */
2620 if (TREE_CODE (expr) == BIT_AND_EXPR
2621 || TREE_CODE (expr) == BIT_IOR_EXPR
2622 || TREE_CODE (expr) == BIT_XOR_EXPR)
2624 /* If both args were extended from a shortest type,
2625 use that type if that is safe. */
2626 expr_type = shorten_binary_op (expr_type,
2627 TREE_OPERAND (expr, 0),
2628 TREE_OPERAND (expr, 1),
2631 if (TREE_CODE (expr) == BIT_AND_EXPR)
2633 tree op0 = TREE_OPERAND (expr, 0);
2634 tree op1 = TREE_OPERAND (expr, 1);
2635 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2636 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2638 /* If one of the operands is a non-negative constant
2639 that fits in the target type, then the type of the
2640 other operand does not matter. */
2641 if ((TREE_CODE (op0) == INTEGER_CST
2642 && int_fits_type_p (op0, c_common_signed_type (type))
2643 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2644 || (TREE_CODE (op1) == INTEGER_CST
2645 && int_fits_type_p (op1, c_common_signed_type (type))
2646 && int_fits_type_p (op1,
2647 c_common_unsigned_type (type))))
2648 return SAFE_CONVERSION;
2649 /* If constant is unsigned and fits in the target
2650 type, then the result will also fit. */
2651 else if ((TREE_CODE (op0) == INTEGER_CST
2653 && int_fits_type_p (op0, type))
2654 || (TREE_CODE (op1) == INTEGER_CST
2656 && int_fits_type_p (op1, type)))
2657 return SAFE_CONVERSION;
2660 /* Warn for integer types converted to smaller integer types. */
2661 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2662 give_warning = UNSAFE_OTHER;
2664 /* When they are the same width but different signedness,
2665 then the value may change. */
2666 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2667 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2668 /* Even when converted to a bigger type, if the type is
2669 unsigned but expr is signed, then negative values
2671 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2673 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2674 "may change the sign of the result",
2678 /* Warn for integer types converted to real types if and only if
2679 all the range of values of the integer type cannot be
2680 represented by the real type. */
2681 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2682 && TREE_CODE (type) == REAL_TYPE)
2684 tree type_low_bound, type_high_bound;
2685 REAL_VALUE_TYPE real_low_bound, real_high_bound;
2687 /* Don't warn about char y = 0xff; float x = (int) y; */
2688 expr = get_unwidened (expr, 0);
2689 expr_type = TREE_TYPE (expr);
2691 type_low_bound = TYPE_MIN_VALUE (expr_type);
2692 type_high_bound = TYPE_MAX_VALUE (expr_type);
2693 real_low_bound = real_value_from_int_cst (0, type_low_bound);
2694 real_high_bound = real_value_from_int_cst (0, type_high_bound);
2696 if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2697 || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2698 give_warning = UNSAFE_OTHER;
2701 /* Warn for real types converted to smaller real types. */
2702 else if (TREE_CODE (expr_type) == REAL_TYPE
2703 && TREE_CODE (type) == REAL_TYPE
2704 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2705 give_warning = UNSAFE_REAL;
2708 return give_warning;
2711 /* Warns if the conversion of EXPR to TYPE may alter a value.
2712 This is a helper function for warnings_for_convert_and_check. */
2715 conversion_warning (location_t loc, tree type, tree expr)
2717 tree expr_type = TREE_TYPE (expr);
2718 enum conversion_safety conversion_kind;
2720 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2723 /* This may happen, because for LHS op= RHS we preevaluate
2724 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2725 means we could no longer see the code of the EXPR. */
2726 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2727 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2728 if (TREE_CODE (expr) == SAVE_EXPR)
2729 expr = TREE_OPERAND (expr, 0);
2731 switch (TREE_CODE (expr))
2739 case TRUTH_ANDIF_EXPR:
2740 case TRUTH_ORIF_EXPR:
2741 case TRUTH_AND_EXPR:
2743 case TRUTH_XOR_EXPR:
2744 case TRUTH_NOT_EXPR:
2745 /* Conversion from boolean to a signed:1 bit-field (which only
2746 can hold the values 0 and -1) doesn't lose information - but
2747 it does change the value. */
2748 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2749 warning_at (loc, OPT_Wconversion,
2750 "conversion to %qT from boolean expression", type);
2755 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2756 if (conversion_kind == UNSAFE_REAL)
2757 warning_at (loc, OPT_Wfloat_conversion,
2758 "conversion to %qT alters %qT constant value",
2760 else if (conversion_kind)
2761 warning_at (loc, OPT_Wconversion,
2762 "conversion to %qT alters %qT constant value",
2768 /* In case of COND_EXPR, we do not care about the type of
2769 COND_EXPR, only about the conversion of each operand. */
2770 tree op1 = TREE_OPERAND (expr, 1);
2771 tree op2 = TREE_OPERAND (expr, 2);
2773 conversion_warning (loc, type, op1);
2774 conversion_warning (loc, type, op2);
2778 default: /* 'expr' is not a constant. */
2779 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2780 if (conversion_kind == UNSAFE_REAL)
2781 warning_at (loc, OPT_Wfloat_conversion,
2782 "conversion to %qT from %qT may alter its value",
2784 else if (conversion_kind)
2785 warning_at (loc, OPT_Wconversion,
2786 "conversion to %qT from %qT may alter its value",
2791 /* Produce warnings after a conversion. RESULT is the result of
2792 converting EXPR to TYPE. This is a helper function for
2793 convert_and_check and cp_convert_and_check. */
2796 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2799 loc = expansion_point_location_if_in_system_header (loc);
2801 if (TREE_CODE (expr) == INTEGER_CST
2802 && (TREE_CODE (type) == INTEGER_TYPE
2803 || TREE_CODE (type) == ENUMERAL_TYPE)
2804 && !int_fits_type_p (expr, type))
2806 /* Do not diagnose overflow in a constant expression merely
2807 because a conversion overflowed. */
2808 if (TREE_OVERFLOW (result))
2809 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2811 if (TYPE_UNSIGNED (type))
2813 /* This detects cases like converting -129 or 256 to
2815 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2816 warning_at (loc, OPT_Woverflow,
2817 "large integer implicitly truncated to unsigned type");
2819 conversion_warning (loc, type, expr);
2821 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2822 warning_at (loc, OPT_Woverflow,
2823 "overflow in implicit constant conversion");
2824 /* No warning for converting 0x80000000 to int. */
2826 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2827 || TYPE_PRECISION (TREE_TYPE (expr))
2828 != TYPE_PRECISION (type)))
2829 warning_at (loc, OPT_Woverflow,
2830 "overflow in implicit constant conversion");
2833 conversion_warning (loc, type, expr);
2835 else if ((TREE_CODE (result) == INTEGER_CST
2836 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2837 warning_at (loc, OPT_Woverflow,
2838 "overflow in implicit constant conversion");
2840 conversion_warning (loc, type, expr);
2844 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2845 Invoke this function on every expression that is converted implicitly,
2846 i.e. because of language rules and not because of an explicit cast. */
2849 convert_and_check (location_t loc, tree type, tree expr)
2852 tree expr_for_warning;
2854 /* Convert from a value with possible excess precision rather than
2855 via the semantic type, but do not warn about values not fitting
2856 exactly in the semantic type. */
2857 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2859 tree orig_type = TREE_TYPE (expr);
2860 expr = TREE_OPERAND (expr, 0);
2861 expr_for_warning = convert (orig_type, expr);
2862 if (orig_type == type)
2863 return expr_for_warning;
2866 expr_for_warning = expr;
2868 if (TREE_TYPE (expr) == type)
2871 result = convert (type, expr);
2873 if (c_inhibit_evaluation_warnings == 0
2874 && !TREE_OVERFLOW_P (expr)
2875 && result != error_mark_node)
2876 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2881 /* A node in a list that describes references to variables (EXPR), which are
2882 either read accesses if WRITER is zero, or write accesses, in which case
2883 WRITER is the parent of EXPR. */
2890 /* Used to implement a cache the results of a call to verify_tree. We only
2891 use this for SAVE_EXPRs. */
2894 struct tlist_cache *next;
2895 struct tlist *cache_before_sp;
2896 struct tlist *cache_after_sp;
2900 /* Obstack to use when allocating tlist structures, and corresponding
2902 static struct obstack tlist_obstack;
2903 static char *tlist_firstobj = 0;
2905 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2907 static struct tlist *warned_ids;
2908 /* SAVE_EXPRs need special treatment. We process them only once and then
2909 cache the results. */
2910 static struct tlist_cache *save_expr_cache;
2912 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2913 static void merge_tlist (struct tlist **, struct tlist *, int);
2914 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2915 static int warning_candidate_p (tree);
2916 static bool candidate_equal_p (const_tree, const_tree);
2917 static void warn_for_collisions (struct tlist *);
2918 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2919 static struct tlist *new_tlist (struct tlist *, tree, tree);
2921 /* Create a new struct tlist and fill in its fields. */
2922 static struct tlist *
2923 new_tlist (struct tlist *next, tree t, tree writer)
2926 l = XOBNEW (&tlist_obstack, struct tlist);
2933 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2934 is nonnull, we ignore any node we find which has a writer equal to it. */
2937 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2941 struct tlist *next = add->next;
2944 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2945 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2950 /* Merge the nodes of ADD into TO. This merging process is done so that for
2951 each variable that already exists in TO, no new node is added; however if
2952 there is a write access recorded in ADD, and an occurrence on TO is only
2953 a read access, then the occurrence in TO will be modified to record the
2957 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2959 struct tlist **end = to;
2962 end = &(*end)->next;
2968 struct tlist *next = add->next;
2970 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2971 if (candidate_equal_p (tmp2->expr, add->expr))
2975 tmp2->writer = add->writer;
2979 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
2980 end = &(*end)->next;
2987 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2988 references in list LIST conflict with it, excluding reads if ONLY writers
2992 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2997 /* Avoid duplicate warnings. */
2998 for (tmp = warned_ids; tmp; tmp = tmp->next)
2999 if (candidate_equal_p (tmp->expr, written))
3004 if (candidate_equal_p (list->expr, written)
3005 && !candidate_equal_p (list->writer, writer)
3006 && (!only_writes || list->writer))
3008 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3009 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3010 OPT_Wsequence_point, "operation on %qE may be undefined",
3017 /* Given a list LIST of references to variables, find whether any of these
3018 can cause conflicts due to missing sequence points. */
3021 warn_for_collisions (struct tlist *list)
3025 for (tmp = list; tmp; tmp = tmp->next)
3028 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3032 /* Return nonzero if X is a tree that can be verified by the sequence point
3035 warning_candidate_p (tree x)
3037 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3040 if (TREE_CODE (x) == BLOCK)
3043 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3044 (lvalue_p) crash on TRY/CATCH. */
3045 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3051 /* No point to track non-const calls, they will never satisfy
3053 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3056 if (TREE_CODE (x) == STRING_CST)
3062 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3064 candidate_equal_p (const_tree x, const_tree y)
3066 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3069 /* Walk the tree X, and record accesses to variables. If X is written by the
3070 parent tree, WRITER is the parent.
3071 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3072 expression or its only operand forces a sequence point, then everything up
3073 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3075 Once we return, we will have emitted warnings if any subexpression before
3076 such a sequence point could be undefined. On a higher level, however, the
3077 sequence point may not be relevant, and we'll merge the two lists.
3079 Example: (b++, a) + b;
3080 The call that processes the COMPOUND_EXPR will store the increment of B
3081 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3082 processes the PLUS_EXPR will need to merge the two lists so that
3083 eventually, all accesses end up on the same list (and we'll warn about the
3084 unordered subexpressions b++ and b.
3086 A note on merging. If we modify the former example so that our expression
3089 care must be taken not simply to add all three expressions into the final
3090 PNO_SP list. The function merge_tlist takes care of that by merging the
3091 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3092 way, so that no more than one access to B is recorded. */
3095 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3098 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3099 enum tree_code code;
3100 enum tree_code_class cl;
3102 /* X may be NULL if it is the operand of an empty statement expression
3108 code = TREE_CODE (x);
3109 cl = TREE_CODE_CLASS (code);
3111 if (warning_candidate_p (x))
3112 *pno_sp = new_tlist (*pno_sp, x, writer);
3121 case TRUTH_ANDIF_EXPR:
3122 case TRUTH_ORIF_EXPR:
3123 tmp_before = tmp_nosp = tmp_list3 = 0;
3124 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3125 warn_for_collisions (tmp_nosp);
3126 merge_tlist (pbefore_sp, tmp_before, 0);
3127 merge_tlist (pbefore_sp, tmp_nosp, 0);
3128 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3129 merge_tlist (pbefore_sp, tmp_list3, 0);
3133 tmp_before = tmp_list2 = 0;
3134 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3135 warn_for_collisions (tmp_list2);
3136 merge_tlist (pbefore_sp, tmp_before, 0);
3137 merge_tlist (pbefore_sp, tmp_list2, 0);
3139 tmp_list3 = tmp_nosp = 0;
3140 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3141 warn_for_collisions (tmp_nosp);
3142 merge_tlist (pbefore_sp, tmp_list3, 0);
3144 tmp_list3 = tmp_list2 = 0;
3145 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3146 warn_for_collisions (tmp_list2);
3147 merge_tlist (pbefore_sp, tmp_list3, 0);
3148 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3149 two first, to avoid warning for (a ? b++ : b++). */
3150 merge_tlist (&tmp_nosp, tmp_list2, 0);
3151 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3154 case PREDECREMENT_EXPR:
3155 case PREINCREMENT_EXPR:
3156 case POSTDECREMENT_EXPR:
3157 case POSTINCREMENT_EXPR:
3158 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3162 tmp_before = tmp_nosp = tmp_list3 = 0;
3163 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3164 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3165 /* Expressions inside the LHS are not ordered wrt. the sequence points
3166 in the RHS. Example:
3168 Despite the fact that the modification of "a" is in the before_sp
3169 list (tmp_before), it conflicts with the use of "a" in the LHS.
3170 We can handle this by adding the contents of tmp_list3
3171 to those of tmp_before, and redoing the collision warnings for that
3173 add_tlist (&tmp_before, tmp_list3, x, 1);
3174 warn_for_collisions (tmp_before);
3175 /* Exclude the LHS itself here; we first have to merge it into the
3176 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3177 didn't exclude the LHS, we'd get it twice, once as a read and once
3179 add_tlist (pno_sp, tmp_list3, x, 0);
3180 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3182 merge_tlist (pbefore_sp, tmp_before, 0);
3183 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3184 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3185 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3189 /* We need to warn about conflicts among arguments and conflicts between
3190 args and the function address. Side effects of the function address,
3191 however, are not ordered by the sequence point of the call. */
3193 call_expr_arg_iterator iter;
3195 tmp_before = tmp_nosp = 0;
3196 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3197 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3199 tmp_list2 = tmp_list3 = 0;
3200 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3201 merge_tlist (&tmp_list3, tmp_list2, 0);
3202 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3204 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3205 warn_for_collisions (tmp_before);
3206 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3211 /* Scan all the list, e.g. indices of multi dimensional array. */
3214 tmp_before = tmp_nosp = 0;
3215 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3216 merge_tlist (&tmp_nosp, tmp_before, 0);
3217 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3224 struct tlist_cache *t;
3225 for (t = save_expr_cache; t; t = t->next)
3226 if (candidate_equal_p (t->expr, x))
3231 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3232 t->next = save_expr_cache;
3234 save_expr_cache = t;
3236 tmp_before = tmp_nosp = 0;
3237 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3238 warn_for_collisions (tmp_nosp);
3241 merge_tlist (&tmp_list3, tmp_nosp, 0);
3242 t->cache_before_sp = tmp_before;
3243 t->cache_after_sp = tmp_list3;
3245 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3246 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3251 x = TREE_OPERAND (x, 0);
3258 /* For other expressions, simply recurse on their operands.
3259 Manual tail recursion for unary expressions.
3260 Other non-expressions need not be processed. */
3261 if (cl == tcc_unary)
3263 x = TREE_OPERAND (x, 0);
3267 else if (IS_EXPR_CODE_CLASS (cl))
3270 int max = TREE_OPERAND_LENGTH (x);
3271 for (lp = 0; lp < max; lp++)
3273 tmp_before = tmp_nosp = 0;
3274 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3275 merge_tlist (&tmp_nosp, tmp_before, 0);
3276 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3283 /* Try to warn for undefined behavior in EXPR due to missing sequence
3287 verify_sequence_points (tree expr)
3289 struct tlist *before_sp = 0, *after_sp = 0;
3292 save_expr_cache = 0;
3293 if (tlist_firstobj == 0)
3295 gcc_obstack_init (&tlist_obstack);
3296 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3299 verify_tree (expr, &before_sp, &after_sp, 0);
3300 warn_for_collisions (after_sp);
3301 obstack_free (&tlist_obstack, tlist_firstobj);
3304 /* Validate the expression after `case' and apply default promotions. */
3307 check_case_value (tree value)
3309 if (value == NULL_TREE)
3312 if (TREE_CODE (value) == INTEGER_CST)
3313 /* Promote char or short to int. */
3314 value = perform_integral_promotions (value);
3315 else if (value != error_mark_node)
3317 error ("case label does not reduce to an integer constant");
3318 value = error_mark_node;
3321 constant_expression_warning (value);
3326 /* See if the case values LOW and HIGH are in the range of the original
3327 type (i.e. before the default conversion to int) of the switch testing
3329 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3330 the type before promoting it. CASE_LOW_P is a pointer to the lower
3331 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3332 if the case is not a case range.
3333 The caller has to make sure that we are not called with NULL for
3334 CASE_LOW_P (i.e. the default case).
3335 Returns true if the case label is in range of ORIG_TYPE (saturated or
3336 untouched) or false if the label is out of range. */
3339 check_case_bounds (tree type, tree orig_type,
3340 tree *case_low_p, tree *case_high_p)
3342 tree min_value, max_value;
3343 tree case_low = *case_low_p;
3344 tree case_high = case_high_p ? *case_high_p : case_low;
3346 /* If there was a problem with the original type, do nothing. */
3347 if (orig_type == error_mark_node)
3350 min_value = TYPE_MIN_VALUE (orig_type);
3351 max_value = TYPE_MAX_VALUE (orig_type);
3353 /* Case label is less than minimum for type. */
3354 if (tree_int_cst_compare (case_low, min_value) < 0
3355 && tree_int_cst_compare (case_high, min_value) < 0)
3357 warning (0, "case label value is less than minimum value for type");
3361 /* Case value is greater than maximum for type. */
3362 if (tree_int_cst_compare (case_low, max_value) > 0
3363 && tree_int_cst_compare (case_high, max_value) > 0)
3365 warning (0, "case label value exceeds maximum value for type");
3369 /* Saturate lower case label value to minimum. */
3370 if (tree_int_cst_compare (case_high, min_value) >= 0
3371 && tree_int_cst_compare (case_low, min_value) < 0)
3373 warning (0, "lower value in case label range"
3374 " less than minimum value for type");
3375 case_low = min_value;
3378 /* Saturate upper case label value to maximum. */
3379 if (tree_int_cst_compare (case_low, max_value) <= 0
3380 && tree_int_cst_compare (case_high, max_value) > 0)
3382 warning (0, "upper value in case label range"
3383 " exceeds maximum value for type");
3384 case_high = max_value;
3387 if (*case_low_p != case_low)
3388 *case_low_p = convert (type, case_low);
3389 if (case_high_p && *case_high_p != case_high)
3390 *case_high_p = convert (type, case_high);
3395 /* Return an integer type with BITS bits of precision,
3396 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3399 c_common_type_for_size (unsigned int bits, int unsignedp)
3401 if (bits == TYPE_PRECISION (integer_type_node))
3402 return unsignedp ? unsigned_type_node : integer_type_node;
3404 if (bits == TYPE_PRECISION (signed_char_type_node))
3405 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3407 if (bits == TYPE_PRECISION (short_integer_type_node))
3408 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3410 if (bits == TYPE_PRECISION (long_integer_type_node))
3411 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3413 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3414 return (unsignedp ? long_long_unsigned_type_node
3415 : long_long_integer_type_node);
3417 if (int128_integer_type_node
3418 && bits == TYPE_PRECISION (int128_integer_type_node))
3419 return (unsignedp ? int128_unsigned_type_node
3420 : int128_integer_type_node);
3422 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3423 return (unsignedp ? widest_unsigned_literal_type_node
3424 : widest_integer_literal_type_node);
3426 if (bits <= TYPE_PRECISION (intQI_type_node))
3427 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3429 if (bits <= TYPE_PRECISION (intHI_type_node))
3430 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3432 if (bits <= TYPE_PRECISION (intSI_type_node))
3433 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3435 if (bits <= TYPE_PRECISION (intDI_type_node))
3436 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3441 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3442 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3443 and saturating if SATP is nonzero, otherwise not saturating. */
3446 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3447 int unsignedp, int satp)
3449 enum machine_mode mode;
3451 mode = unsignedp ? UQQmode : QQmode;
3453 mode = unsignedp ? UHAmode : HAmode;
3455 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3456 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3459 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3461 sorry ("GCC cannot support operators with integer types and "
3462 "fixed-point types that have too many integral and "
3463 "fractional bits together");
3467 return c_common_type_for_mode (mode, satp);
3470 /* Used for communication between c_common_type_for_mode and
3471 c_register_builtin_type. */
3472 static GTY(()) tree registered_builtin_types;
3474 /* Return a data type that has machine mode MODE.
3475 If the mode is an integer,
3476 then UNSIGNEDP selects between signed and unsigned types.
3477 If the mode is a fixed-point mode,
3478 then UNSIGNEDP selects between saturating and nonsaturating types. */
3481 c_common_type_for_mode (enum machine_mode mode, int unsignedp)
3485 if (mode == TYPE_MODE (integer_type_node))
3486 return unsignedp ? unsigned_type_node : integer_type_node;
3488 if (mode == TYPE_MODE (signed_char_type_node))
3489 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3491 if (mode == TYPE_MODE (short_integer_type_node))
3492 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3494 if (mode == TYPE_MODE (long_integer_type_node))
3495 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3497 if (mode == TYPE_MODE (long_long_integer_type_node))
3498 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3500 if (int128_integer_type_node
3501 && mode == TYPE_MODE (int128_integer_type_node))
3502 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3504 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3505 return unsignedp ? widest_unsigned_literal_type_node
3506 : widest_integer_literal_type_node;
3509 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3512 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3515 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3518 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3520 #if HOST_BITS_PER_WIDE_INT >= 64
3521 if (mode == TYPE_MODE (intTI_type_node))
3522 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3525 if (mode == TYPE_MODE (float_type_node))
3526 return float_type_node;
3528 if (mode == TYPE_MODE (double_type_node))
3529 return double_type_node;
3531 if (mode == TYPE_MODE (long_double_type_node))
3532 return long_double_type_node;
3534 if (mode == TYPE_MODE (void_type_node))
3535 return void_type_node;
3537 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3539 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3540 : make_signed_type (GET_MODE_PRECISION (mode)));
3542 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3544 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3545 : make_signed_type (GET_MODE_PRECISION (mode)));
3547 if (COMPLEX_MODE_P (mode))
3549 enum machine_mode inner_mode;
3552 if (mode == TYPE_MODE (complex_float_type_node))
3553 return complex_float_type_node;
3554 if (mode == TYPE_MODE (complex_double_type_node))
3555 return complex_double_type_node;
3556 if (mode == TYPE_MODE (complex_long_double_type_node))
3557 return complex_long_double_type_node;
3559 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3560 return complex_integer_type_node;
3562 inner_mode = GET_MODE_INNER (mode);
3563 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3564 if (inner_type != NULL_TREE)
3565 return build_complex_type (inner_type);
3567 else if (VECTOR_MODE_P (mode))
3569 enum machine_mode inner_mode = GET_MODE_INNER (mode);
3570 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3571 if (inner_type != NULL_TREE)
3572 return build_vector_type_for_mode (inner_type, mode);
3575 if (mode == TYPE_MODE (dfloat32_type_node))
3576 return dfloat32_type_node;
3577 if (mode == TYPE_MODE (dfloat64_type_node))
3578 return dfloat64_type_node;
3579 if (mode == TYPE_MODE (dfloat128_type_node))
3580 return dfloat128_type_node;
3582 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3584 if (mode == TYPE_MODE (short_fract_type_node))
3585 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3586 if (mode == TYPE_MODE (fract_type_node))
3587 return unsignedp ? sat_fract_type_node : fract_type_node;
3588 if (mode == TYPE_MODE (long_fract_type_node))
3589 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3590 if (mode == TYPE_MODE (long_long_fract_type_node))
3591 return unsignedp ? sat_long_long_fract_type_node
3592 : long_long_fract_type_node;
3594 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3595 return unsignedp ? sat_unsigned_short_fract_type_node
3596 : unsigned_short_fract_type_node;
3597 if (mode == TYPE_MODE (unsigned_fract_type_node))
3598 return unsignedp ? sat_unsigned_fract_type_node
3599 : unsigned_fract_type_node;
3600 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3601 return unsignedp ? sat_unsigned_long_fract_type_node
3602 : unsigned_long_fract_type_node;
3603 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3604 return unsignedp ? sat_unsigned_long_long_fract_type_node
3605 : unsigned_long_long_fract_type_node;
3607 if (mode == TYPE_MODE (short_accum_type_node))
3608 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3609 if (mode == TYPE_MODE (accum_type_node))
3610 return unsignedp ? sat_accum_type_node : accum_type_node;
3611 if (mode == TYPE_MODE (long_accum_type_node))
3612 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3613 if (mode == TYPE_MODE (long_long_accum_type_node))
3614 return unsignedp ? sat_long_long_accum_type_node
3615 : long_long_accum_type_node;
3617 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3618 return unsignedp ? sat_unsigned_short_accum_type_node
3619 : unsigned_short_accum_type_node;
3620 if (mode == TYPE_MODE (unsigned_accum_type_node))
3621 return unsignedp ? sat_unsigned_accum_type_node
3622 : unsigned_accum_type_node;
3623 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3624 return unsignedp ? sat_unsigned_long_accum_type_node
3625 : unsigned_long_accum_type_node;
3626 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3627 return unsignedp ? sat_unsigned_long_long_accum_type_node
3628 : unsigned_long_long_accum_type_node;
3631 return unsignedp ? sat_qq_type_node : qq_type_node;
3633 return unsignedp ? sat_hq_type_node : hq_type_node;
3635 return unsignedp ? sat_sq_type_node : sq_type_node;
3637 return unsignedp ? sat_dq_type_node : dq_type_node;
3639 return unsignedp ? sat_tq_type_node : tq_type_node;
3641 if (mode == UQQmode)
3642 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3643 if (mode == UHQmode)
3644 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3645 if (mode == USQmode)
3646 return unsignedp ? sat_usq_type_node : usq_type_node;
3647 if (mode == UDQmode)
3648 return unsignedp ? sat_udq_type_node : udq_type_node;
3649 if (mode == UTQmode)
3650 return unsignedp ? sat_utq_type_node : utq_type_node;
3653 return unsignedp ? sat_ha_type_node : ha_type_node;
3655 return unsignedp ? sat_sa_type_node : sa_type_node;
3657 return unsignedp ? sat_da_type_node : da_type_node;
3659 return unsignedp ? sat_ta_type_node : ta_type_node;
3661 if (mode == UHAmode)
3662 return unsignedp ? sat_uha_type_node : uha_type_node;
3663 if (mode == USAmode)
3664 return unsignedp ? sat_usa_type_node : usa_type_node;
3665 if (mode == UDAmode)
3666 return unsignedp ? sat_uda_type_node : uda_type_node;
3667 if (mode == UTAmode)
3668 return unsignedp ? sat_uta_type_node : uta_type_node;
3671 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3672 if (TYPE_MODE (TREE_VALUE (t)) == mode
3673 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3674 return TREE_VALUE (t);
3680 c_common_unsigned_type (tree type)
3682 return c_common_signed_or_unsigned_type (1, type);
3685 /* Return a signed type the same as TYPE in other respects. */
3688 c_common_signed_type (tree type)
3690 return c_common_signed_or_unsigned_type (0, type);
3693 /* Return a type the same as TYPE except unsigned or
3694 signed according to UNSIGNEDP. */
3697 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3701 /* This block of code emulates the behavior of the old
3702 c_common_unsigned_type. In particular, it returns
3703 long_unsigned_type_node if passed a long, even when a int would
3704 have the same size. This is necessary for warnings to work
3705 correctly in archs where sizeof(int) == sizeof(long) */
3707 type1 = TYPE_MAIN_VARIANT (type);
3708 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3709 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3710 if (type1 == integer_type_node || type1 == unsigned_type_node)
3711 return unsignedp ? unsigned_type_node : integer_type_node;
3712 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3713 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3714 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3715 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3716 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3717 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3718 if (int128_integer_type_node
3719 && (type1 == int128_integer_type_node
3720 || type1 == int128_unsigned_type_node))
3721 return unsignedp ? int128_unsigned_type_node : int128_integer_type_node;
3722 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3723 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3724 #if HOST_BITS_PER_WIDE_INT >= 64
3725 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3726 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3728 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3729 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3730 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3731 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3732 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3733 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3734 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3735 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3737 #define C_COMMON_FIXED_TYPES(NAME) \
3738 if (type1 == short_ ## NAME ## _type_node \
3739 || type1 == unsigned_short_ ## NAME ## _type_node) \
3740 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3741 : short_ ## NAME ## _type_node; \
3742 if (type1 == NAME ## _type_node \
3743 || type1 == unsigned_ ## NAME ## _type_node) \
3744 return unsignedp ? unsigned_ ## NAME ## _type_node \
3745 : NAME ## _type_node; \
3746 if (type1 == long_ ## NAME ## _type_node \
3747 || type1 == unsigned_long_ ## NAME ## _type_node) \
3748 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3749 : long_ ## NAME ## _type_node; \
3750 if (type1 == long_long_ ## NAME ## _type_node \
3751 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3752 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3753 : long_long_ ## NAME ## _type_node;
3755 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3756 if (type1 == NAME ## _type_node \
3757 || type1 == u ## NAME ## _type_node) \
3758 return unsignedp ? u ## NAME ## _type_node \
3759 : NAME ## _type_node;
3761 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3762 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3763 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3764 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3765 : sat_ ## short_ ## NAME ## _type_node; \
3766 if (type1 == sat_ ## NAME ## _type_node \
3767 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3768 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3769 : sat_ ## NAME ## _type_node; \
3770 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3771 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3772 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3773 : sat_ ## long_ ## NAME ## _type_node; \
3774 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3775 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3776 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3777 : sat_ ## long_long_ ## NAME ## _type_node;
3779 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3780 if (type1 == sat_ ## NAME ## _type_node \
3781 || type1 == sat_ ## u ## NAME ## _type_node) \
3782 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3783 : sat_ ## NAME ## _type_node;
3785 C_COMMON_FIXED_TYPES (fract);
3786 C_COMMON_FIXED_TYPES_SAT (fract);
3787 C_COMMON_FIXED_TYPES (accum);
3788 C_COMMON_FIXED_TYPES_SAT (accum);
3790 C_COMMON_FIXED_MODE_TYPES (qq);
3791 C_COMMON_FIXED_MODE_TYPES (hq);
3792 C_COMMON_FIXED_MODE_TYPES (sq);
3793 C_COMMON_FIXED_MODE_TYPES (dq);
3794 C_COMMON_FIXED_MODE_TYPES (tq);
3795 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3796 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3797 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3798 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3799 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3800 C_COMMON_FIXED_MODE_TYPES (ha);
3801 C_COMMON_FIXED_MODE_TYPES (sa);
3802 C_COMMON_FIXED_MODE_TYPES (da);
3803 C_COMMON_FIXED_MODE_TYPES (ta);
3804 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3805 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3806 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3807 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3809 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3810 the precision; they have precision set to match their range, but
3811 may use a wider mode to match an ABI. If we change modes, we may
3812 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3813 the precision as well, so as to yield correct results for
3814 bit-field types. C++ does not have these separate bit-field
3815 types, and producing a signed or unsigned variant of an
3816 ENUMERAL_TYPE may cause other problems as well. */
3818 if (!INTEGRAL_TYPE_P (type)
3819 || TYPE_UNSIGNED (type) == unsignedp)
3822 #define TYPE_OK(node) \
3823 (TYPE_MODE (type) == TYPE_MODE (node) \
3824 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3825 if (TYPE_OK (signed_char_type_node))
3826 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3827 if (TYPE_OK (integer_type_node))
3828 return unsignedp ? unsigned_type_node : integer_type_node;
3829 if (TYPE_OK (short_integer_type_node))
3830 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3831 if (TYPE_OK (long_integer_type_node))
3832 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3833 if (TYPE_OK (long_long_integer_type_node))
3834 return (unsignedp ? long_long_unsigned_type_node
3835 : long_long_integer_type_node);
3836 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3837 return (unsignedp ? int128_unsigned_type_node
3838 : int128_integer_type_node);
3839 if (TYPE_OK (widest_integer_literal_type_node))
3840 return (unsignedp ? widest_unsigned_literal_type_node
3841 : widest_integer_literal_type_node);
3843 #if HOST_BITS_PER_WIDE_INT >= 64
3844 if (TYPE_OK (intTI_type_node))
3845 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3847 if (TYPE_OK (intDI_type_node))
3848 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3849 if (TYPE_OK (intSI_type_node))
3850 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3851 if (TYPE_OK (intHI_type_node))
3852 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3853 if (TYPE_OK (intQI_type_node))
3854 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3857 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3860 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3863 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3865 /* Extended integer types of the same width as a standard type have
3866 lesser rank, so those of the same width as int promote to int or
3867 unsigned int and are valid for printf formats expecting int or
3868 unsigned int. To avoid such special cases, avoid creating
3869 extended integer types for bit-fields if a standard integer type
3871 if (width == TYPE_PRECISION (integer_type_node))
3872 return unsignedp ? unsigned_type_node : integer_type_node;
3873 if (width == TYPE_PRECISION (signed_char_type_node))
3874 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3875 if (width == TYPE_PRECISION (short_integer_type_node))
3876 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3877 if (width == TYPE_PRECISION (long_integer_type_node))
3878 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3879 if (width == TYPE_PRECISION (long_long_integer_type_node))
3880 return (unsignedp ? long_long_unsigned_type_node
3881 : long_long_integer_type_node);
3882 if (int128_integer_type_node
3883 && width == TYPE_PRECISION (int128_integer_type_node))
3884 return (unsignedp ? int128_unsigned_type_node
3885 : int128_integer_type_node);
3886 return build_nonstandard_integer_type (width, unsignedp);
3889 /* The C version of the register_builtin_type langhook. */
3892 c_register_builtin_type (tree type, const char* name)
3896 decl = build_decl (UNKNOWN_LOCATION,
3897 TYPE_DECL, get_identifier (name), type);
3898 DECL_ARTIFICIAL (decl) = 1;
3899 if (!TYPE_NAME (type))
3900 TYPE_NAME (type) = decl;
3903 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3906 /* Print an error message for invalid operands to arith operation
3907 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3908 LOCATION is the location of the message. */
3911 binary_op_error (location_t location, enum tree_code code,
3912 tree type0, tree type1)
3919 opname = "+"; break;
3921 opname = "-"; break;
3923 opname = "*"; break;
3925 opname = "max"; break;
3927 opname = "min"; break;
3929 opname = "=="; break;
3931 opname = "!="; break;
3933 opname = "<="; break;
3935 opname = ">="; break;
3937 opname = "<"; break;
3939 opname = ">"; break;
3941 opname = "<<"; break;
3943 opname = ">>"; break;
3944 case TRUNC_MOD_EXPR:
3945 case FLOOR_MOD_EXPR:
3946 opname = "%"; break;
3947 case TRUNC_DIV_EXPR:
3948 case FLOOR_DIV_EXPR:
3949 opname = "/"; break;
3951 opname = "&"; break;
3953 opname = "|"; break;
3954 case TRUTH_ANDIF_EXPR:
3955 opname = "&&"; break;
3956 case TRUTH_ORIF_EXPR:
3957 opname = "||"; break;
3959 opname = "^"; break;
3964 "invalid operands to binary %s (have %qT and %qT)", opname,
3968 /* Given an expression as a tree, return its original type. Do this
3969 by stripping any conversion that preserves the sign and precision. */
3971 expr_original_type (tree expr)
3973 STRIP_SIGN_NOPS (expr);
3974 return TREE_TYPE (expr);
3977 /* Subroutine of build_binary_op, used for comparison operations.
3978 See if the operands have both been converted from subword integer types
3979 and, if so, perhaps change them both back to their original type.
3980 This function is also responsible for converting the two operands
3981 to the proper common type for comparison.
3983 The arguments of this function are all pointers to local variables
3984 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3985 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3987 LOC is the location of the comparison.
3989 If this function returns nonzero, it means that the comparison has
3990 a constant value. What this function returns is an expression for
3994 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3995 tree *restype_ptr, enum tree_code *rescode_ptr)
3998 tree op0 = *op0_ptr;
3999 tree op1 = *op1_ptr;
4000 int unsignedp0, unsignedp1;
4002 tree primop0, primop1;
4003 enum tree_code code = *rescode_ptr;
4005 /* Throw away any conversions to wider types
4006 already present in the operands. */
4008 primop0 = c_common_get_narrower (op0, &unsignedp0);
4009 primop1 = c_common_get_narrower (op1, &unsignedp1);
4011 /* If primopN is first sign-extended from primopN's precision to opN's
4012 precision, then zero-extended from opN's precision to
4013 *restype_ptr precision, shortenings might be invalid. */
4014 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4015 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4017 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4019 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4020 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4022 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4025 /* Handle the case that OP0 does not *contain* a conversion
4026 but it *requires* conversion to FINAL_TYPE. */
4028 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4029 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4030 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4031 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4033 /* If one of the operands must be floated, we cannot optimize. */
4034 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4035 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4037 /* If first arg is constant, swap the args (changing operation
4038 so value is preserved), for canonicalization. Don't do this if
4039 the second arg is 0. */
4041 if (TREE_CONSTANT (primop0)
4042 && !integer_zerop (primop1) && !real_zerop (primop1)
4043 && !fixed_zerop (primop1))
4046 int temi = unsignedp0;
4054 unsignedp0 = unsignedp1;
4077 *rescode_ptr = code;
4080 /* If comparing an integer against a constant more bits wide,
4081 maybe we can deduce a value of 1 or 0 independent of the data.
4082 Or else truncate the constant now
4083 rather than extend the variable at run time.
4085 This is only interesting if the constant is the wider arg.
4086 Also, it is not safe if the constant is unsigned and the
4087 variable arg is signed, since in this case the variable
4088 would be sign-extended and then regarded as unsigned.
4089 Our technique fails in this case because the lowest/highest
4090 possible unsigned results don't follow naturally from the
4091 lowest/highest possible values of the variable operand.
4092 For just EQ_EXPR and NE_EXPR there is another technique that
4093 could be used: see if the constant can be faithfully represented
4094 in the other operand's type, by truncating it and reextending it
4095 and see if that preserves the constant's value. */
4097 if (!real1 && !real2
4098 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4099 && TREE_CODE (primop1) == INTEGER_CST
4100 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4102 int min_gt, max_gt, min_lt, max_lt;
4103 tree maxval, minval;
4104 /* 1 if comparison is nominally unsigned. */
4105 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4108 type = c_common_signed_or_unsigned_type (unsignedp0,
4109 TREE_TYPE (primop0));
4111 maxval = TYPE_MAX_VALUE (type);
4112 minval = TYPE_MIN_VALUE (type);
4114 if (unsignedp && !unsignedp0)
4115 *restype_ptr = c_common_signed_type (*restype_ptr);
4117 if (TREE_TYPE (primop1) != *restype_ptr)
4119 /* Convert primop1 to target type, but do not introduce
4120 additional overflow. We know primop1 is an int_cst. */
4121 primop1 = force_fit_type_double (*restype_ptr,
4122 tree_to_double_int (primop1),
4123 0, TREE_OVERFLOW (primop1));
4125 if (type != *restype_ptr)
4127 minval = convert (*restype_ptr, minval);
4128 maxval = convert (*restype_ptr, maxval);
4131 if (unsignedp && unsignedp0)
4133 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
4134 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
4135 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
4136 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
4140 min_gt = INT_CST_LT (primop1, minval);
4141 max_gt = INT_CST_LT (primop1, maxval);
4142 min_lt = INT_CST_LT (minval, primop1);
4143 max_lt = INT_CST_LT (maxval, primop1);
4147 /* This used to be a switch, but Genix compiler can't handle that. */
4148 if (code == NE_EXPR)
4150 if (max_lt || min_gt)
4151 val = truthvalue_true_node;
4153 else if (code == EQ_EXPR)
4155 if (max_lt || min_gt)
4156 val = truthvalue_false_node;
4158 else if (code == LT_EXPR)
4161 val = truthvalue_true_node;
4163 val = truthvalue_false_node;
4165 else if (code == GT_EXPR)
4168 val = truthvalue_true_node;
4170 val = truthvalue_false_node;
4172 else if (code == LE_EXPR)
4175 val = truthvalue_true_node;
4177 val = truthvalue_false_node;
4179 else if (code == GE_EXPR)
4182 val = truthvalue_true_node;
4184 val = truthvalue_false_node;
4187 /* If primop0 was sign-extended and unsigned comparison specd,
4188 we did a signed comparison above using the signed type bounds.
4189 But the comparison we output must be unsigned.
4191 Also, for inequalities, VAL is no good; but if the signed
4192 comparison had *any* fixed result, it follows that the
4193 unsigned comparison just tests the sign in reverse
4194 (positive values are LE, negative ones GE).
4195 So we can generate an unsigned comparison
4196 against an extreme value of the signed type. */
4198 if (unsignedp && !unsignedp0)
4205 primop1 = TYPE_MIN_VALUE (type);
4211 primop1 = TYPE_MAX_VALUE (type);
4218 type = c_common_unsigned_type (type);
4221 if (TREE_CODE (primop0) != INTEGER_CST
4222 && c_inhibit_evaluation_warnings == 0)
4224 if (val == truthvalue_false_node)
4225 warning_at (loc, OPT_Wtype_limits,
4226 "comparison is always false due to limited range of data type");
4227 if (val == truthvalue_true_node)
4228 warning_at (loc, OPT_Wtype_limits,
4229 "comparison is always true due to limited range of data type");
4234 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4235 if (TREE_SIDE_EFFECTS (primop0))
4236 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4240 /* Value is not predetermined, but do the comparison
4241 in the type of the operand that is not constant.
4242 TYPE is already properly set. */
4245 /* If either arg is decimal float and the other is float, find the
4246 proper common type to use for comparison. */
4247 else if (real1 && real2
4248 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4249 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4250 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4252 else if (real1 && real2
4253 && (TYPE_PRECISION (TREE_TYPE (primop0))
4254 == TYPE_PRECISION (TREE_TYPE (primop1))))
4255 type = TREE_TYPE (primop0);
4257 /* If args' natural types are both narrower than nominal type
4258 and both extend in the same manner, compare them
4259 in the type of the wider arg.
4260 Otherwise must actually extend both to the nominal
4261 common type lest different ways of extending
4263 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4265 else if (unsignedp0 == unsignedp1 && real1 == real2
4266 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4267 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4269 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4270 type = c_common_signed_or_unsigned_type (unsignedp0
4271 || TYPE_UNSIGNED (*restype_ptr),
4273 /* Make sure shorter operand is extended the right way
4274 to match the longer operand. */
4276 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4277 TREE_TYPE (primop0)),
4280 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4281 TREE_TYPE (primop1)),
4286 /* Here we must do the comparison on the nominal type
4287 using the args exactly as we received them. */
4288 type = *restype_ptr;
4292 if (!real1 && !real2 && integer_zerop (primop1)
4293 && TYPE_UNSIGNED (*restype_ptr))
4296 /* All unsigned values are >= 0, so we warn. However,
4297 if OP0 is a constant that is >= 0, the signedness of
4298 the comparison isn't an issue, so suppress the
4301 warn_type_limits && !in_system_header_at (loc)
4302 && c_inhibit_evaluation_warnings == 0
4303 && !(TREE_CODE (primop0) == INTEGER_CST
4304 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4306 /* Do not warn for enumeration types. */
4307 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4313 warning_at (loc, OPT_Wtype_limits,
4314 "comparison of unsigned expression >= 0 is always true");
4315 value = truthvalue_true_node;
4320 warning_at (loc, OPT_Wtype_limits,
4321 "comparison of unsigned expression < 0 is always false");
4322 value = truthvalue_false_node;
4331 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4332 if (TREE_SIDE_EFFECTS (primop0))
4333 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4340 *op0_ptr = convert (type, primop0);
4341 *op1_ptr = convert (type, primop1);
4343 *restype_ptr = truthvalue_type_node;
4348 /* Return a tree for the sum or difference (RESULTCODE says which)
4349 of pointer PTROP and integer INTOP. */
4352 pointer_int_sum (location_t loc, enum tree_code resultcode,
4353 tree ptrop, tree intop, bool complain)
4357 /* The result is a pointer of the same type that is being added. */
4358 tree result_type = TREE_TYPE (ptrop);
4360 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4362 if (complain && warn_pointer_arith)
4363 pedwarn (loc, OPT_Wpointer_arith,
4364 "pointer of type %<void *%> used in arithmetic");
4366 return error_mark_node;
4367 size_exp = integer_one_node;
4369 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4371 if (complain && warn_pointer_arith)
4372 pedwarn (loc, OPT_Wpointer_arith,
4373 "pointer to a function used in arithmetic");
4375 return error_mark_node;
4376 size_exp = integer_one_node;
4379 size_exp = size_in_bytes (TREE_TYPE (result_type));
4381 /* We are manipulating pointer values, so we don't need to warn
4382 about relying on undefined signed overflow. We disable the
4383 warning here because we use integer types so fold won't know that
4384 they are really pointers. */
4385 fold_defer_overflow_warnings ();
4387 /* If what we are about to multiply by the size of the elements
4388 contains a constant term, apply distributive law
4389 and multiply that constant term separately.
4390 This helps produce common subexpressions. */
4391 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4392 && !TREE_CONSTANT (intop)
4393 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4394 && TREE_CONSTANT (size_exp)
4395 /* If the constant comes from pointer subtraction,
4396 skip this optimization--it would cause an error. */
4397 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4398 /* If the constant is unsigned, and smaller than the pointer size,
4399 then we must skip this optimization. This is because it could cause
4400 an overflow error if the constant is negative but INTOP is not. */
4401 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4402 || (TYPE_PRECISION (TREE_TYPE (intop))
4403 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4405 enum tree_code subcode = resultcode;
4406 tree int_type = TREE_TYPE (intop);
4407 if (TREE_CODE (intop) == MINUS_EXPR)
4408 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4409 /* Convert both subexpression types to the type of intop,
4410 because weird cases involving pointer arithmetic
4411 can result in a sum or difference with different type args. */
4412 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4414 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4415 intop = convert (int_type, TREE_OPERAND (intop, 0));
4418 /* Convert the integer argument to a type the same size as sizetype
4419 so the multiply won't overflow spuriously. */
4420 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4421 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4422 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4423 TYPE_UNSIGNED (sizetype)), intop);
4425 /* Replace the integer argument with a suitable product by the object size.
4426 Do this multiplication as signed, then convert to the appropriate type
4427 for the pointer operation and disregard an overflow that occurred only
4428 because of the sign-extension change in the latter conversion. */
4430 tree t = build_binary_op (loc,
4432 convert (TREE_TYPE (intop), size_exp), 1);
4433 intop = convert (sizetype, t);
4434 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4435 intop = build_int_cst_wide (TREE_TYPE (intop), TREE_INT_CST_LOW (intop),
4436 TREE_INT_CST_HIGH (intop));
4439 /* Create the sum or difference. */
4440 if (resultcode == MINUS_EXPR)
4441 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4443 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4445 fold_undefer_and_ignore_overflow_warnings ();
4450 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4451 and if NON_CONST is known not to be permitted in an evaluated part
4452 of a constant expression. */
4455 c_wrap_maybe_const (tree expr, bool non_const)
4457 bool nowarning = TREE_NO_WARNING (expr);
4458 location_t loc = EXPR_LOCATION (expr);
4460 /* This should never be called for C++. */
4461 if (c_dialect_cxx ())
4464 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4465 STRIP_TYPE_NOPS (expr);
4466 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4467 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4469 TREE_NO_WARNING (expr) = 1;
4470 protected_set_expr_location (expr, loc);
4475 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4476 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4477 around the SAVE_EXPR if needed so that c_fully_fold does not need
4478 to look inside SAVE_EXPRs. */
4481 c_save_expr (tree expr)
4483 bool maybe_const = true;
4484 if (c_dialect_cxx ())
4485 return save_expr (expr);
4486 expr = c_fully_fold (expr, false, &maybe_const);
4487 expr = save_expr (expr);
4489 expr = c_wrap_maybe_const (expr, true);
4493 /* Return whether EXPR is a declaration whose address can never be
4497 decl_with_nonnull_addr_p (const_tree expr)
4499 return (DECL_P (expr)
4500 && (TREE_CODE (expr) == PARM_DECL
4501 || TREE_CODE (expr) == LABEL_DECL
4502 || !DECL_WEAK (expr)));
4505 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4506 or for an `if' or `while' statement or ?..: exp. It should already
4507 have been validated to be of suitable type; otherwise, a bad
4508 diagnostic may result.
4510 The EXPR is located at LOCATION.
4512 This preparation consists of taking the ordinary
4513 representation of an expression expr and producing a valid tree
4514 boolean expression describing whether expr is nonzero. We could
4515 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4516 but we optimize comparisons, &&, ||, and !.
4518 The resulting type should always be `truthvalue_type_node'. */
4521 c_common_truthvalue_conversion (location_t location, tree expr)
4523 switch (TREE_CODE (expr))
4525 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4526 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4527 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4528 case ORDERED_EXPR: case UNORDERED_EXPR:
4529 if (TREE_TYPE (expr) == truthvalue_type_node)
4531 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4532 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4535 case TRUTH_ANDIF_EXPR:
4536 case TRUTH_ORIF_EXPR:
4537 case TRUTH_AND_EXPR:
4539 case TRUTH_XOR_EXPR:
4540 if (TREE_TYPE (expr) == truthvalue_type_node)
4542 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4543 c_common_truthvalue_conversion (location,
4544 TREE_OPERAND (expr, 0)),
4545 c_common_truthvalue_conversion (location,
4546 TREE_OPERAND (expr, 1)));
4549 case TRUTH_NOT_EXPR:
4550 if (TREE_TYPE (expr) == truthvalue_type_node)
4552 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4553 c_common_truthvalue_conversion (location,
4554 TREE_OPERAND (expr, 0)));
4561 return integer_zerop (expr) ? truthvalue_false_node
4562 : truthvalue_true_node;
4565 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4566 ? truthvalue_true_node
4567 : truthvalue_false_node;
4570 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4571 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4572 ? truthvalue_true_node
4573 : truthvalue_false_node;
4576 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4581 tree inner = TREE_OPERAND (expr, 0);
4582 if (decl_with_nonnull_addr_p (inner))
4584 /* Common Ada/Pascal programmer's mistake. */
4585 warning_at (location,
4587 "the address of %qD will always evaluate as %<true%>",
4589 return truthvalue_true_node;
4595 expr = build_binary_op (EXPR_LOCATION (expr),
4596 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4597 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4598 c_common_truthvalue_conversion (location,
4599 TREE_OPERAND (expr, 0)),
4600 c_common_truthvalue_conversion (location,
4601 TREE_OPERAND (expr, 1)),
4608 case EXCESS_PRECISION_EXPR:
4609 /* These don't change whether an object is nonzero or zero. */
4610 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4614 /* These don't change whether an object is zero or nonzero, but
4615 we can't ignore them if their second arg has side-effects. */
4616 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4618 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4619 TREE_OPERAND (expr, 1),
4620 c_common_truthvalue_conversion
4621 (location, TREE_OPERAND (expr, 0)));
4625 return c_common_truthvalue_conversion (location,
4626 TREE_OPERAND (expr, 0));
4629 /* Distribute the conversion into the arms of a COND_EXPR. */
4630 if (c_dialect_cxx ())
4632 tree op1 = TREE_OPERAND (expr, 1);
4633 tree op2 = TREE_OPERAND (expr, 2);
4634 /* In C++ one of the arms might have void type if it is throw. */
4635 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4636 op1 = c_common_truthvalue_conversion (location, op1);
4637 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4638 op2 = c_common_truthvalue_conversion (location, op2);
4639 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4640 TREE_OPERAND (expr, 0), op1, op2);
4645 /* Folding will happen later for C. */
4646 expr = build3 (COND_EXPR, truthvalue_type_node,
4647 TREE_OPERAND (expr, 0),
4648 c_common_truthvalue_conversion (location,
4649 TREE_OPERAND (expr, 1)),
4650 c_common_truthvalue_conversion (location,
4651 TREE_OPERAND (expr, 2)));
4657 tree totype = TREE_TYPE (expr);
4658 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4660 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4661 since that affects how `default_conversion' will behave. */
4662 if (TREE_CODE (totype) == REFERENCE_TYPE
4663 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4665 /* Don't strip a conversion from C++0x scoped enum, since they
4666 don't implicitly convert to other types. */
4667 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4668 && ENUM_IS_SCOPED (fromtype))
4670 /* If this isn't narrowing the argument, we can ignore it. */
4671 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4672 return c_common_truthvalue_conversion (location,
4673 TREE_OPERAND (expr, 0));
4678 if (!TREE_NO_WARNING (expr)
4679 && warn_parentheses)
4681 warning (OPT_Wparentheses,
4682 "suggest parentheses around assignment used as truth value");
4683 TREE_NO_WARNING (expr) = 1;
4691 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4693 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4694 expr = (build_binary_op
4695 (EXPR_LOCATION (expr),
4696 (TREE_SIDE_EFFECTS (expr)
4697 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4698 c_common_truthvalue_conversion
4700 build_unary_op (location, REALPART_EXPR, t, 0)),
4701 c_common_truthvalue_conversion
4703 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4708 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4710 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4712 (TREE_TYPE (expr))));
4713 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4716 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4719 protected_set_expr_location (expr, location);
4723 static void def_builtin_1 (enum built_in_function fncode,
4725 enum built_in_class fnclass,
4726 tree fntype, tree libtype,
4727 bool both_p, bool fallback_p, bool nonansi_p,
4728 tree fnattrs, bool implicit_p);
4731 /* Apply the TYPE_QUALS to the new DECL. */
4734 c_apply_type_quals_to_decl (int type_quals, tree decl)
4736 tree type = TREE_TYPE (decl);
4738 if (type == error_mark_node)
4741 if ((type_quals & TYPE_QUAL_CONST)
4742 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4743 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4744 constructor can produce constant init, so rely on cp_finish_decl to
4745 clear TREE_READONLY if the variable has non-constant init. */
4746 TREE_READONLY (decl) = 1;
4747 if (type_quals & TYPE_QUAL_VOLATILE)
4749 TREE_SIDE_EFFECTS (decl) = 1;
4750 TREE_THIS_VOLATILE (decl) = 1;
4752 if (type_quals & TYPE_QUAL_RESTRICT)
4754 while (type && TREE_CODE (type) == ARRAY_TYPE)
4755 /* Allow 'restrict' on arrays of pointers.
4756 FIXME currently we just ignore it. */
4757 type = TREE_TYPE (type);
4759 || !POINTER_TYPE_P (type)
4760 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4761 error ("invalid use of %<restrict%>");
4765 /* Hash function for the problem of multiple type definitions in
4766 different files. This must hash all types that will compare
4767 equal via comptypes to the same value. In practice it hashes
4768 on some of the simple stuff and leaves the details to comptypes. */
4771 c_type_hash (const void *p)
4775 const_tree const t = (const_tree) p;
4777 switch (TREE_CODE (t))
4779 /* For pointers, hash on pointee type plus some swizzling. */
4781 return c_type_hash (TREE_TYPE (t)) ^ 0x3003003;
4782 /* Hash on number of elements and total size. */
4785 t2 = TYPE_VALUES (t);
4789 t2 = TYPE_FIELDS (t);
4791 case QUAL_UNION_TYPE:
4793 t2 = TYPE_FIELDS (t);
4797 t2 = TYPE_FIELDS (t);
4802 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4803 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4804 n_elements = list_length (t2);
4805 /* We might have a VLA here. */
4806 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4809 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4810 return ((size << 24) | (n_elements << shift));
4813 static GTY((param_is (union tree_node))) htab_t type_hash_table;
4815 /* Return the typed-based alias set for T, which may be an expression
4816 or a type. Return -1 if we don't do anything special. */
4819 c_common_get_alias_set (tree t)
4824 /* For VLAs, use the alias set of the element type rather than the
4825 default of alias set 0 for types compared structurally. */
4826 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4828 if (TREE_CODE (t) == ARRAY_TYPE)
4829 return get_alias_set (TREE_TYPE (t));
4833 /* Permit type-punning when accessing a union, provided the access
4834 is directly through the union. For example, this code does not
4835 permit taking the address of a union member and then storing
4836 through it. Even the type-punning allowed here is a GCC
4837 extension, albeit a common and useful one; the C standard says
4838 that such accesses have implementation-defined behavior. */
4840 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4841 u = TREE_OPERAND (u, 0))
4842 if (TREE_CODE (u) == COMPONENT_REF
4843 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4846 /* That's all the expressions we handle specially. */
4850 /* The C standard guarantees that any object may be accessed via an
4851 lvalue that has character type. */
4852 if (t == char_type_node
4853 || t == signed_char_type_node
4854 || t == unsigned_char_type_node)
4857 /* The C standard specifically allows aliasing between signed and
4858 unsigned variants of the same type. We treat the signed
4859 variant as canonical. */
4860 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4862 tree t1 = c_common_signed_type (t);
4864 /* t1 == t can happen for boolean nodes which are always unsigned. */
4866 return get_alias_set (t1);
4869 /* Handle the case of multiple type nodes referring to "the same" type,
4870 which occurs with IMA. These share an alias set. FIXME: Currently only
4871 C90 is handled. (In C99 type compatibility is not transitive, which
4872 complicates things mightily. The alias set splay trees can theoretically
4873 represent this, but insertion is tricky when you consider all the
4874 different orders things might arrive in.) */
4876 if (c_language != clk_c || flag_isoc99)
4879 /* Save time if there's only one input file. */
4880 if (num_in_fnames == 1)
4883 /* Pointers need special handling if they point to any type that
4884 needs special handling (below). */
4885 if (TREE_CODE (t) == POINTER_TYPE)
4888 /* Find bottom type under any nested POINTERs. */
4889 for (t2 = TREE_TYPE (t);
4890 TREE_CODE (t2) == POINTER_TYPE;
4891 t2 = TREE_TYPE (t2))
4893 if (TREE_CODE (t2) != RECORD_TYPE
4894 && TREE_CODE (t2) != ENUMERAL_TYPE
4895 && TREE_CODE (t2) != QUAL_UNION_TYPE
4896 && TREE_CODE (t2) != UNION_TYPE)
4898 if (TYPE_SIZE (t2) == 0)
4901 /* These are the only cases that need special handling. */
4902 if (TREE_CODE (t) != RECORD_TYPE
4903 && TREE_CODE (t) != ENUMERAL_TYPE
4904 && TREE_CODE (t) != QUAL_UNION_TYPE
4905 && TREE_CODE (t) != UNION_TYPE
4906 && TREE_CODE (t) != POINTER_TYPE)
4909 if (TYPE_SIZE (t) == 0)
4912 /* Look up t in hash table. Only one of the compatible types within each
4913 alias set is recorded in the table. */
4914 if (!type_hash_table)
4915 type_hash_table = htab_create_ggc (1021, c_type_hash,
4916 (htab_eq) lang_hooks.types_compatible_p,
4918 slot = htab_find_slot (type_hash_table, t, INSERT);
4921 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4922 return TYPE_ALIAS_SET ((tree)*slot);
4925 /* Our caller will assign and record (in t) a new alias set; all we need
4926 to do is remember t in the hash table. */
4932 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4933 the IS_SIZEOF parameter indicates which operator is being applied.
4934 The COMPLAIN flag controls whether we should diagnose possibly
4935 ill-formed constructs or not. LOC is the location of the SIZEOF or
4936 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4937 a type in any context should be returned, rather than the normal
4938 alignment for that type. */
4941 c_sizeof_or_alignof_type (location_t loc,
4942 tree type, bool is_sizeof, bool min_alignof,
4945 const char *op_name;
4947 enum tree_code type_code = TREE_CODE (type);
4949 op_name = is_sizeof ? "sizeof" : "__alignof__";
4951 if (type_code == FUNCTION_TYPE)
4955 if (complain && warn_pointer_arith)
4956 pedwarn (loc, OPT_Wpointer_arith,
4957 "invalid application of %<sizeof%> to a function type");
4959 return error_mark_node;
4960 value = size_one_node;
4966 if (c_dialect_cxx ())
4967 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4968 "%<alignof%> applied to a function type");
4970 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4971 "%<_Alignof%> applied to a function type");
4973 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4976 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4978 if (type_code == VOID_TYPE
4979 && complain && warn_pointer_arith)
4980 pedwarn (loc, OPT_Wpointer_arith,
4981 "invalid application of %qs to a void type", op_name);
4983 return error_mark_node;
4984 value = size_one_node;
4986 else if (!COMPLETE_TYPE_P (type)
4987 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4990 error_at (loc, "invalid application of %qs to incomplete type %qT",
4992 return error_mark_node;
4994 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4995 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4998 error_at (loc, "invalid application of %qs to array type %qT of "
4999 "incomplete element type", op_name, type);
5000 return error_mark_node;
5005 /* Convert in case a char is more than one unit. */
5006 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5007 size_int (TYPE_PRECISION (char_type_node)
5009 else if (min_alignof)
5011 unsigned int align = TYPE_ALIGN (type);
5012 align = MIN (align, BIGGEST_ALIGNMENT);
5013 #ifdef BIGGEST_FIELD_ALIGNMENT
5014 align = MIN (align, BIGGEST_FIELD_ALIGNMENT);
5016 unsigned int field_align = align;
5017 #ifdef ADJUST_FIELD_ALIGN
5018 tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL, NULL_TREE,
5020 field_align = ADJUST_FIELD_ALIGN (field, field_align);
5022 align = MIN (align, field_align);
5023 value = size_int (align / BITS_PER_UNIT);
5026 value = size_int (TYPE_ALIGN_UNIT (type));
5029 /* VALUE will have the middle-end integer type sizetype.
5030 However, we should really return a value of type `size_t',
5031 which is just a typedef for an ordinary integer type. */
5032 value = fold_convert_loc (loc, size_type_node, value);
5037 /* Implement the __alignof keyword: Return the minimum required
5038 alignment of EXPR, measured in bytes. For VAR_DECLs,
5039 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5040 from an "aligned" __attribute__ specification). LOC is the
5041 location of the ALIGNOF operator. */
5044 c_alignof_expr (location_t loc, tree expr)
5048 if (VAR_OR_FUNCTION_DECL_P (expr))
5049 t = size_int (DECL_ALIGN_UNIT (expr));
5051 else if (TREE_CODE (expr) == COMPONENT_REF
5052 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5054 error_at (loc, "%<__alignof%> applied to a bit-field");
5057 else if (TREE_CODE (expr) == COMPONENT_REF
5058 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5059 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5061 else if (TREE_CODE (expr) == INDIRECT_REF)
5063 tree t = TREE_OPERAND (expr, 0);
5065 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5067 while (CONVERT_EXPR_P (t)
5068 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5072 t = TREE_OPERAND (t, 0);
5073 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5074 if (thisalign > bestalign)
5075 best = t, bestalign = thisalign;
5077 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5080 return c_alignof (loc, TREE_TYPE (expr));
5082 return fold_convert_loc (loc, size_type_node, t);
5085 /* Handle C and C++ default attributes. */
5087 enum built_in_attribute
5089 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5090 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5091 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5092 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5093 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5094 #include "builtin-attrs.def"
5095 #undef DEF_ATTR_NULL_TREE
5097 #undef DEF_ATTR_STRING
5098 #undef DEF_ATTR_IDENT
5099 #undef DEF_ATTR_TREE_LIST
5103 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5105 static void c_init_attributes (void);
5109 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5110 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5111 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5112 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5113 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5114 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5115 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5116 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) NAME,
5117 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) NAME,
5118 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) NAME,
5119 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5120 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5121 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5122 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5123 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5124 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG6) \
5126 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5127 #include "builtin-types.def"
5128 #undef DEF_PRIMITIVE_TYPE
5129 #undef DEF_FUNCTION_TYPE_0
5130 #undef DEF_FUNCTION_TYPE_1
5131 #undef DEF_FUNCTION_TYPE_2
5132 #undef DEF_FUNCTION_TYPE_3
5133 #undef DEF_FUNCTION_TYPE_4
5134 #undef DEF_FUNCTION_TYPE_5
5135 #undef DEF_FUNCTION_TYPE_6
5136 #undef DEF_FUNCTION_TYPE_7
5137 #undef DEF_FUNCTION_TYPE_8
5138 #undef DEF_FUNCTION_TYPE_VAR_0
5139 #undef DEF_FUNCTION_TYPE_VAR_1
5140 #undef DEF_FUNCTION_TYPE_VAR_2
5141 #undef DEF_FUNCTION_TYPE_VAR_3
5142 #undef DEF_FUNCTION_TYPE_VAR_4
5143 #undef DEF_FUNCTION_TYPE_VAR_5
5144 #undef DEF_POINTER_TYPE
5148 typedef enum c_builtin_type builtin_type;
5150 /* A temporary array for c_common_nodes_and_builtins. Used in
5151 communication with def_fn_type. */
5152 static tree builtin_types[(int) BT_LAST + 1];
5154 /* A helper function for c_common_nodes_and_builtins. Build function type
5155 for DEF with return type RET and N arguments. If VAR is true, then the
5156 function should be variadic after those N arguments.
5158 Takes special care not to ICE if any of the types involved are
5159 error_mark_node, which indicates that said type is not in fact available
5160 (see builtin_type_for_size). In which case the function type as a whole
5161 should be error_mark_node. */
5164 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5167 tree *args = XALLOCAVEC (tree, n);
5172 for (i = 0; i < n; ++i)
5174 builtin_type a = (builtin_type) va_arg (list, int);
5175 t = builtin_types[a];
5176 if (t == error_mark_node)
5181 t = builtin_types[ret];
5182 if (t == error_mark_node)
5185 t = build_varargs_function_type_array (t, n, args);
5187 t = build_function_type_array (t, n, args);
5190 builtin_types[def] = t;
5194 /* Build builtin functions common to both C and C++ language
5198 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5200 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5201 builtin_types[ENUM] = VALUE;
5202 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5203 def_fn_type (ENUM, RETURN, 0, 0);
5204 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5205 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5206 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5207 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5208 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5209 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5210 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5211 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5212 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5213 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5214 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5216 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5217 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5219 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5220 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5222 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5224 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5225 def_fn_type (ENUM, RETURN, 1, 0);
5226 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5227 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5228 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5229 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5230 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5231 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5232 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5233 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5234 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5235 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5236 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5237 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5239 #include "builtin-types.def"
5241 #undef DEF_PRIMITIVE_TYPE
5242 #undef DEF_FUNCTION_TYPE_1
5243 #undef DEF_FUNCTION_TYPE_2
5244 #undef DEF_FUNCTION_TYPE_3
5245 #undef DEF_FUNCTION_TYPE_4
5246 #undef DEF_FUNCTION_TYPE_5
5247 #undef DEF_FUNCTION_TYPE_6
5248 #undef DEF_FUNCTION_TYPE_VAR_0
5249 #undef DEF_FUNCTION_TYPE_VAR_1
5250 #undef DEF_FUNCTION_TYPE_VAR_2
5251 #undef DEF_FUNCTION_TYPE_VAR_3
5252 #undef DEF_FUNCTION_TYPE_VAR_4
5253 #undef DEF_FUNCTION_TYPE_VAR_5
5254 #undef DEF_POINTER_TYPE
5255 builtin_types[(int) BT_LAST] = NULL_TREE;
5257 c_init_attributes ();
5259 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5260 NONANSI_P, ATTRS, IMPLICIT, COND) \
5262 def_builtin_1 (ENUM, NAME, CLASS, \
5263 builtin_types[(int) TYPE], \
5264 builtin_types[(int) LIBTYPE], \
5265 BOTH_P, FALLBACK_P, NONANSI_P, \
5266 built_in_attributes[(int) ATTRS], IMPLICIT);
5267 #include "builtins.def"
5270 targetm.init_builtins ();
5272 build_common_builtin_nodes ();
5275 cilk_init_builtins ();
5278 /* Like get_identifier, but avoid warnings about null arguments when
5279 the argument may be NULL for targets where GCC lacks stdint.h type
5283 c_get_ident (const char *id)
5285 return get_identifier (id);
5288 /* Build tree nodes and builtin functions common to both C and C++ language
5292 c_common_nodes_and_builtins (void)
5294 int char16_type_size;
5295 int char32_type_size;
5296 int wchar_type_size;
5297 tree array_domain_type;
5298 tree va_list_ref_type_node;
5299 tree va_list_arg_type_node;
5301 build_common_tree_nodes (flag_signed_char, flag_short_double);
5303 /* Define `int' and `char' first so that dbx will output them first. */
5304 record_builtin_type (RID_INT, NULL, integer_type_node);
5305 record_builtin_type (RID_CHAR, "char", char_type_node);
5307 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5308 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5309 but not C. Are the conditionals here needed? */
5310 if (c_dialect_cxx ())
5311 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5312 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5313 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5314 record_builtin_type (RID_MAX, "long unsigned int",
5315 long_unsigned_type_node);
5316 if (int128_integer_type_node != NULL_TREE)
5318 record_builtin_type (RID_INT128, "__int128",
5319 int128_integer_type_node);
5320 record_builtin_type (RID_MAX, "__int128 unsigned",
5321 int128_unsigned_type_node);
5323 if (c_dialect_cxx ())
5324 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5325 record_builtin_type (RID_MAX, "long long int",
5326 long_long_integer_type_node);
5327 record_builtin_type (RID_MAX, "long long unsigned int",
5328 long_long_unsigned_type_node);
5329 if (c_dialect_cxx ())
5330 record_builtin_type (RID_MAX, "long long unsigned",
5331 long_long_unsigned_type_node);
5332 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5333 record_builtin_type (RID_MAX, "short unsigned int",
5334 short_unsigned_type_node);
5335 if (c_dialect_cxx ())
5336 record_builtin_type (RID_MAX, "unsigned short",
5337 short_unsigned_type_node);
5339 /* Define both `signed char' and `unsigned char'. */
5340 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5341 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5343 /* These are types that c_common_type_for_size and
5344 c_common_type_for_mode use. */
5345 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5346 TYPE_DECL, NULL_TREE,
5348 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5349 TYPE_DECL, NULL_TREE,
5351 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5352 TYPE_DECL, NULL_TREE,
5354 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5355 TYPE_DECL, NULL_TREE,
5357 #if HOST_BITS_PER_WIDE_INT >= 64
5358 if (targetm.scalar_mode_supported_p (TImode))
5359 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5361 get_identifier ("__int128_t"),
5364 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5365 TYPE_DECL, NULL_TREE,
5366 unsigned_intQI_type_node));
5367 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5368 TYPE_DECL, NULL_TREE,
5369 unsigned_intHI_type_node));
5370 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5371 TYPE_DECL, NULL_TREE,
5372 unsigned_intSI_type_node));
5373 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5374 TYPE_DECL, NULL_TREE,
5375 unsigned_intDI_type_node));
5376 #if HOST_BITS_PER_WIDE_INT >= 64
5377 if (targetm.scalar_mode_supported_p (TImode))
5378 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5380 get_identifier ("__uint128_t"),
5381 unsigned_intTI_type_node));
5384 /* Create the widest literal types. */
5385 widest_integer_literal_type_node
5386 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5387 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5388 TYPE_DECL, NULL_TREE,
5389 widest_integer_literal_type_node));
5391 widest_unsigned_literal_type_node
5392 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5393 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5394 TYPE_DECL, NULL_TREE,
5395 widest_unsigned_literal_type_node));
5397 signed_size_type_node = c_common_signed_type (size_type_node);
5400 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5402 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5403 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5404 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5406 /* Only supported decimal floating point extension if the target
5407 actually supports underlying modes. */
5408 if (targetm.scalar_mode_supported_p (SDmode)
5409 && targetm.scalar_mode_supported_p (DDmode)
5410 && targetm.scalar_mode_supported_p (TDmode))
5412 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5413 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5414 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5417 if (targetm.fixed_point_supported_p ())
5419 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5420 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5421 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5422 record_builtin_type (RID_MAX, "long long _Fract",
5423 long_long_fract_type_node);
5424 record_builtin_type (RID_MAX, "unsigned short _Fract",
5425 unsigned_short_fract_type_node);
5426 record_builtin_type (RID_MAX, "unsigned _Fract",
5427 unsigned_fract_type_node);
5428 record_builtin_type (RID_MAX, "unsigned long _Fract",
5429 unsigned_long_fract_type_node);
5430 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5431 unsigned_long_long_fract_type_node);
5432 record_builtin_type (RID_MAX, "_Sat short _Fract",
5433 sat_short_fract_type_node);
5434 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5435 record_builtin_type (RID_MAX, "_Sat long _Fract",
5436 sat_long_fract_type_node);
5437 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5438 sat_long_long_fract_type_node);
5439 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5440 sat_unsigned_short_fract_type_node);
5441 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5442 sat_unsigned_fract_type_node);
5443 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5444 sat_unsigned_long_fract_type_node);
5445 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5446 sat_unsigned_long_long_fract_type_node);
5447 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5448 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5449 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5450 record_builtin_type (RID_MAX, "long long _Accum",
5451 long_long_accum_type_node);
5452 record_builtin_type (RID_MAX, "unsigned short _Accum",
5453 unsigned_short_accum_type_node);
5454 record_builtin_type (RID_MAX, "unsigned _Accum",
5455 unsigned_accum_type_node);
5456 record_builtin_type (RID_MAX, "unsigned long _Accum",
5457 unsigned_long_accum_type_node);
5458 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5459 unsigned_long_long_accum_type_node);
5460 record_builtin_type (RID_MAX, "_Sat short _Accum",
5461 sat_short_accum_type_node);
5462 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5463 record_builtin_type (RID_MAX, "_Sat long _Accum",
5464 sat_long_accum_type_node);
5465 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5466 sat_long_long_accum_type_node);
5467 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5468 sat_unsigned_short_accum_type_node);
5469 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5470 sat_unsigned_accum_type_node);
5471 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5472 sat_unsigned_long_accum_type_node);
5473 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5474 sat_unsigned_long_long_accum_type_node);
5478 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5480 get_identifier ("complex int"),
5481 complex_integer_type_node));
5482 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5484 get_identifier ("complex float"),
5485 complex_float_type_node));
5486 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5488 get_identifier ("complex double"),
5489 complex_double_type_node));
5490 lang_hooks.decls.pushdecl
5491 (build_decl (UNKNOWN_LOCATION,
5492 TYPE_DECL, get_identifier ("complex long double"),
5493 complex_long_double_type_node));
5495 if (c_dialect_cxx ())
5496 /* For C++, make fileptr_type_node a distinct void * type until
5497 FILE type is defined. */
5498 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5500 record_builtin_type (RID_VOID, NULL, void_type_node);
5502 /* Set the TYPE_NAME for any variants that were built before
5503 record_builtin_type gave names to the built-in types. */
5505 tree void_name = TYPE_NAME (void_type_node);
5506 TYPE_NAME (void_type_node) = NULL_TREE;
5507 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5509 TYPE_NAME (void_type_node) = void_name;
5512 /* This node must not be shared. */
5513 void_zero_node = make_node (INTEGER_CST);
5514 TREE_TYPE (void_zero_node) = void_type_node;
5516 void_list_node = build_void_list_node ();
5518 /* Make a type to be the domain of a few array types
5519 whose domains don't really matter.
5520 200 is small enough that it always fits in size_t
5521 and large enough that it can hold most function names for the
5522 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5523 array_domain_type = build_index_type (size_int (200));
5525 /* Make a type for arrays of characters.
5526 With luck nothing will ever really depend on the length of this
5528 char_array_type_node
5529 = build_array_type (char_type_node, array_domain_type);
5531 string_type_node = build_pointer_type (char_type_node);
5532 const_string_type_node
5533 = build_pointer_type (build_qualified_type
5534 (char_type_node, TYPE_QUAL_CONST));
5536 /* This is special for C++ so functions can be overloaded. */
5537 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5538 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5539 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5540 underlying_wchar_type_node = wchar_type_node;
5541 if (c_dialect_cxx ())
5543 if (TYPE_UNSIGNED (wchar_type_node))
5544 wchar_type_node = make_unsigned_type (wchar_type_size);
5546 wchar_type_node = make_signed_type (wchar_type_size);
5547 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5550 /* This is for wide string constants. */
5551 wchar_array_type_node
5552 = build_array_type (wchar_type_node, array_domain_type);
5554 /* Define 'char16_t'. */
5555 char16_type_node = get_identifier (CHAR16_TYPE);
5556 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5557 char16_type_size = TYPE_PRECISION (char16_type_node);
5558 if (c_dialect_cxx ())
5560 char16_type_node = make_unsigned_type (char16_type_size);
5562 if (cxx_dialect >= cxx11)
5563 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5566 /* This is for UTF-16 string constants. */
5567 char16_array_type_node
5568 = build_array_type (char16_type_node, array_domain_type);
5570 /* Define 'char32_t'. */
5571 char32_type_node = get_identifier (CHAR32_TYPE);
5572 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5573 char32_type_size = TYPE_PRECISION (char32_type_node);
5574 if (c_dialect_cxx ())
5576 char32_type_node = make_unsigned_type (char32_type_size);
5578 if (cxx_dialect >= cxx11)
5579 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5582 /* This is for UTF-32 string constants. */
5583 char32_array_type_node
5584 = build_array_type (char32_type_node, array_domain_type);
5587 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5590 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5592 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5594 if (SIG_ATOMIC_TYPE)
5595 sig_atomic_type_node =
5596 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5599 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5602 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5605 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5608 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5611 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5613 c_uint16_type_node =
5614 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5616 c_uint32_type_node =
5617 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5619 c_uint64_type_node =
5620 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5621 if (INT_LEAST8_TYPE)
5622 int_least8_type_node =
5623 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5624 if (INT_LEAST16_TYPE)
5625 int_least16_type_node =
5626 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5627 if (INT_LEAST32_TYPE)
5628 int_least32_type_node =
5629 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5630 if (INT_LEAST64_TYPE)
5631 int_least64_type_node =
5632 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5633 if (UINT_LEAST8_TYPE)
5634 uint_least8_type_node =
5635 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5636 if (UINT_LEAST16_TYPE)
5637 uint_least16_type_node =
5638 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5639 if (UINT_LEAST32_TYPE)
5640 uint_least32_type_node =
5641 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5642 if (UINT_LEAST64_TYPE)
5643 uint_least64_type_node =
5644 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5646 int_fast8_type_node =
5647 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5648 if (INT_FAST16_TYPE)
5649 int_fast16_type_node =
5650 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5651 if (INT_FAST32_TYPE)
5652 int_fast32_type_node =
5653 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5654 if (INT_FAST64_TYPE)
5655 int_fast64_type_node =
5656 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5657 if (UINT_FAST8_TYPE)
5658 uint_fast8_type_node =
5659 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5660 if (UINT_FAST16_TYPE)
5661 uint_fast16_type_node =
5662 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5663 if (UINT_FAST32_TYPE)
5664 uint_fast32_type_node =
5665 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5666 if (UINT_FAST64_TYPE)
5667 uint_fast64_type_node =
5668 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5671 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5674 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5676 default_function_type
5677 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5679 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5680 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5682 lang_hooks.decls.pushdecl
5683 (build_decl (UNKNOWN_LOCATION,
5684 TYPE_DECL, get_identifier ("__builtin_va_list"),
5685 va_list_type_node));
5686 if (targetm.enum_va_list_p)
5692 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5694 lang_hooks.decls.pushdecl
5695 (build_decl (UNKNOWN_LOCATION,
5696 TYPE_DECL, get_identifier (pname),
5702 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5704 va_list_arg_type_node = va_list_ref_type_node =
5705 build_pointer_type (TREE_TYPE (va_list_type_node));
5709 va_list_arg_type_node = va_list_type_node;
5710 va_list_ref_type_node = build_reference_type (va_list_type_node);
5713 if (!flag_preprocess_only)
5714 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5716 main_identifier_node = get_identifier ("main");
5718 /* Create the built-in __null node. It is important that this is
5720 null_node = make_node (INTEGER_CST);
5721 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5723 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5724 memset (builtin_types, 0, sizeof (builtin_types));
5727 /* The number of named compound-literals generated thus far. */
5728 static GTY(()) int compound_literal_number;
5730 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5733 set_compound_literal_name (tree decl)
5736 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5737 compound_literal_number);
5738 compound_literal_number++;
5739 DECL_NAME (decl) = get_identifier (name);
5743 build_va_arg (location_t loc, tree expr, tree type)
5745 expr = build1 (VA_ARG_EXPR, type, expr);
5746 SET_EXPR_LOCATION (expr, loc);
5751 /* Linked list of disabled built-in functions. */
5753 typedef struct disabled_builtin
5756 struct disabled_builtin *next;
5758 static disabled_builtin *disabled_builtins = NULL;
5760 static bool builtin_function_disabled_p (const char *);
5762 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5763 begins with "__builtin_", give an error. */
5766 disable_builtin_function (const char *name)
5768 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5769 error ("cannot disable built-in function %qs", name);
5772 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5773 new_disabled_builtin->name = name;
5774 new_disabled_builtin->next = disabled_builtins;
5775 disabled_builtins = new_disabled_builtin;
5780 /* Return true if the built-in function NAME has been disabled, false
5784 builtin_function_disabled_p (const char *name)
5786 disabled_builtin *p;
5787 for (p = disabled_builtins; p != NULL; p = p->next)
5789 if (strcmp (name, p->name) == 0)
5796 /* Worker for DEF_BUILTIN.
5797 Possibly define a builtin function with one or two names.
5798 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5799 nonansi_p and flag_no_nonansi_builtin. */
5802 def_builtin_1 (enum built_in_function fncode,
5804 enum built_in_class fnclass,
5805 tree fntype, tree libtype,
5806 bool both_p, bool fallback_p, bool nonansi_p,
5807 tree fnattrs, bool implicit_p)
5810 const char *libname;
5812 if (fntype == error_mark_node)
5815 gcc_assert ((!both_p && !fallback_p)
5816 || !strncmp (name, "__builtin_",
5817 strlen ("__builtin_")));
5819 libname = name + strlen ("__builtin_");
5820 decl = add_builtin_function (name, fntype, fncode, fnclass,
5821 (fallback_p ? libname : NULL),
5824 set_builtin_decl (fncode, decl, implicit_p);
5827 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5828 && !(nonansi_p && flag_no_nonansi_builtin))
5829 add_builtin_function (libname, libtype, fncode, fnclass,
5833 /* Nonzero if the type T promotes to int. This is (nearly) the
5834 integral promotions defined in ISO C99 6.3.1.1/2. */
5837 c_promoting_integer_type_p (const_tree t)
5839 switch (TREE_CODE (t))
5842 return (TYPE_MAIN_VARIANT (t) == char_type_node
5843 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5844 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5845 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5846 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5847 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5850 /* ??? Technically all enumerations not larger than an int
5851 promote to an int. But this is used along code paths
5852 that only want to notice a size change. */
5853 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5863 /* Return 1 if PARMS specifies a fixed number of parameters
5864 and none of their types is affected by default promotions. */
5867 self_promoting_args_p (const_tree parms)
5870 for (t = parms; t; t = TREE_CHAIN (t))
5872 tree type = TREE_VALUE (t);
5874 if (type == error_mark_node)
5877 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5883 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5886 if (c_promoting_integer_type_p (type))
5892 /* Recursively remove any '*' or '&' operator from TYPE. */
5894 strip_pointer_operator (tree t)
5896 while (POINTER_TYPE_P (t))
5901 /* Recursively remove pointer or array type from TYPE. */
5903 strip_pointer_or_array_types (tree t)
5905 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5910 /* Used to compare case labels. K1 and K2 are actually tree nodes
5911 representing case labels, or NULL_TREE for a `default' label.
5912 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5913 K2, and 0 if K1 and K2 are equal. */
5916 case_compare (splay_tree_key k1, splay_tree_key k2)
5918 /* Consider a NULL key (such as arises with a `default' label) to be
5919 smaller than anything else. */
5925 return tree_int_cst_compare ((tree) k1, (tree) k2);
5928 /* Process a case label, located at LOC, for the range LOW_VALUE
5929 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5930 then this case label is actually a `default' label. If only
5931 HIGH_VALUE is NULL_TREE, then case label was declared using the
5932 usual C/C++ syntax, rather than the GNU case range extension.
5933 CASES is a tree containing all the case ranges processed so far;
5934 COND is the condition for the switch-statement itself. Returns the
5935 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
5939 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
5940 tree low_value, tree high_value)
5945 splay_tree_node node;
5947 /* Create the LABEL_DECL itself. */
5948 label = create_artificial_label (loc);
5950 /* If there was an error processing the switch condition, bail now
5951 before we get more confused. */
5952 if (!cond || cond == error_mark_node)
5955 if ((low_value && TREE_TYPE (low_value)
5956 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5957 || (high_value && TREE_TYPE (high_value)
5958 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5960 error_at (loc, "pointers are not permitted as case values");
5964 /* Case ranges are a GNU extension. */
5966 pedwarn (loc, OPT_Wpedantic,
5967 "range expressions in switch statements are non-standard");
5969 type = TREE_TYPE (cond);
5972 low_value = check_case_value (low_value);
5973 low_value = convert_and_check (loc, type, low_value);
5974 if (low_value == error_mark_node)
5979 high_value = check_case_value (high_value);
5980 high_value = convert_and_check (loc, type, high_value);
5981 if (high_value == error_mark_node)
5985 if (low_value && high_value)
5987 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5988 really a case range, even though it was written that way.
5989 Remove the HIGH_VALUE to simplify later processing. */
5990 if (tree_int_cst_equal (low_value, high_value))
5991 high_value = NULL_TREE;
5992 else if (!tree_int_cst_lt (low_value, high_value))
5993 warning_at (loc, 0, "empty range specified");
5996 /* See if the case is in range of the type of the original testing
5997 expression. If both low_value and high_value are out of range,
5998 don't insert the case label and return NULL_TREE. */
6000 && !check_case_bounds (type, orig_type,
6001 &low_value, high_value ? &high_value : NULL))
6004 /* Look up the LOW_VALUE in the table of case labels we already
6006 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6007 /* If there was not an exact match, check for overlapping ranges.
6008 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6009 that's a `default' label and the only overlap is an exact match. */
6010 if (!node && (low_value || high_value))
6012 splay_tree_node low_bound;
6013 splay_tree_node high_bound;
6015 /* Even though there wasn't an exact match, there might be an
6016 overlap between this case range and another case range.
6017 Since we've (inductively) not allowed any overlapping case
6018 ranges, we simply need to find the greatest low case label
6019 that is smaller that LOW_VALUE, and the smallest low case
6020 label that is greater than LOW_VALUE. If there is an overlap
6021 it will occur in one of these two ranges. */
6022 low_bound = splay_tree_predecessor (cases,
6023 (splay_tree_key) low_value);
6024 high_bound = splay_tree_successor (cases,
6025 (splay_tree_key) low_value);
6027 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6028 the LOW_VALUE, so there is no need to check unless the
6029 LOW_BOUND is in fact itself a case range. */
6031 && CASE_HIGH ((tree) low_bound->value)
6032 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6035 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6036 range is bigger than the low end of the current range, so we
6037 are only interested if the current range is a real range, and
6038 not an ordinary case label. */
6041 && (tree_int_cst_compare ((tree) high_bound->key,
6046 /* If there was an overlap, issue an error. */
6049 tree duplicate = CASE_LABEL ((tree) node->value);
6053 error_at (loc, "duplicate (or overlapping) case value");
6054 error_at (DECL_SOURCE_LOCATION (duplicate),
6055 "this is the first entry overlapping that value");
6059 error_at (loc, "duplicate case value") ;
6060 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6064 error_at (loc, "multiple default labels in one switch");
6065 error_at (DECL_SOURCE_LOCATION (duplicate),
6066 "this is the first default label");
6071 /* Add a CASE_LABEL to the statement-tree. */
6072 case_label = add_stmt (build_case_label (low_value, high_value, label));
6073 /* Register this case label in the splay tree. */
6074 splay_tree_insert (cases,
6075 (splay_tree_key) low_value,
6076 (splay_tree_value) case_label);
6081 /* Add a label so that the back-end doesn't think that the beginning of
6082 the switch is unreachable. Note that we do not add a case label, as
6083 that just leads to duplicates and thence to failure later on. */
6086 tree t = create_artificial_label (loc);
6087 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6089 return error_mark_node;
6092 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6093 Used to verify that case values match up with enumerator values. */
6096 match_case_to_enum_1 (tree key, tree type, tree label)
6098 char buf[2 + 2*HOST_BITS_PER_WIDE_INT/4 + 1];
6100 /* ??? Not working too hard to print the double-word value.
6101 Should perhaps be done with %lwd in the diagnostic routines? */
6102 if (TREE_INT_CST_HIGH (key) == 0)
6103 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_UNSIGNED,
6104 TREE_INT_CST_LOW (key));
6105 else if (!TYPE_UNSIGNED (type)
6106 && TREE_INT_CST_HIGH (key) == -1
6107 && TREE_INT_CST_LOW (key) != 0)
6108 snprintf (buf, sizeof (buf), "-" HOST_WIDE_INT_PRINT_UNSIGNED,
6109 -TREE_INT_CST_LOW (key));
6111 snprintf (buf, sizeof (buf), HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6112 (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (key),
6113 (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (key));
6115 if (TYPE_NAME (type) == 0)
6116 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6117 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6118 "case value %qs not in enumerated type",
6121 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6122 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6123 "case value %qs not in enumerated type %qT",
6127 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6128 Used to verify that case values match up with enumerator values. */
6131 match_case_to_enum (splay_tree_node node, void *data)
6133 tree label = (tree) node->value;
6134 tree type = (tree) data;
6136 /* Skip default case. */
6137 if (!CASE_LOW (label))
6140 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6141 when we did our enum->case scan. Reset our scratch bit after. */
6142 if (!CASE_LOW_SEEN (label))
6143 match_case_to_enum_1 (CASE_LOW (label), type, label);
6145 CASE_LOW_SEEN (label) = 0;
6147 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6148 not set, that means that CASE_HIGH did not appear when we did our
6149 enum->case scan. Reset our scratch bit after. */
6150 if (CASE_HIGH (label))
6152 if (!CASE_HIGH_SEEN (label))
6153 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6155 CASE_HIGH_SEEN (label) = 0;
6161 /* Handle -Wswitch*. Called from the front end after parsing the
6162 switch construct. */
6163 /* ??? Should probably be somewhere generic, since other languages
6164 besides C and C++ would want this. At the moment, however, C/C++
6165 are the only tree-ssa languages that support enumerations at all,
6166 so the point is moot. */
6169 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6170 tree type, tree cond)
6172 splay_tree_node default_node;
6173 splay_tree_node node;
6176 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6179 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6181 warning_at (switch_location, OPT_Wswitch_default,
6182 "switch missing default case");
6184 /* From here on, we only care about about enumerated types. */
6185 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6188 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6189 if (!warn_switch_enum && !warn_switch)
6192 /* Check the cases. Warn about case values which are not members of
6193 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6194 there is no default case, check that exactly all enumeration
6195 literals are covered by the cases. */
6197 /* Clearing COND if it is not an integer constant simplifies
6198 the tests inside the loop below. */
6199 if (TREE_CODE (cond) != INTEGER_CST)
6202 /* The time complexity here is O(N*lg(N)) worst case, but for the
6203 common case of monotonically increasing enumerators, it is
6204 O(N), since the nature of the splay tree will keep the next
6205 element adjacent to the root at all times. */
6207 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6209 tree value = TREE_VALUE (chain);
6210 if (TREE_CODE (value) == CONST_DECL)
6211 value = DECL_INITIAL (value);
6212 node = splay_tree_lookup (cases, (splay_tree_key) value);
6215 /* Mark the CASE_LOW part of the case entry as seen. */
6216 tree label = (tree) node->value;
6217 CASE_LOW_SEEN (label) = 1;
6221 /* Even though there wasn't an exact match, there might be a
6222 case range which includes the enumerator's value. */
6223 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6224 if (node && CASE_HIGH ((tree) node->value))
6226 tree label = (tree) node->value;
6227 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6230 /* If we match the upper bound exactly, mark the CASE_HIGH
6231 part of the case entry as seen. */
6233 CASE_HIGH_SEEN (label) = 1;
6238 /* We've now determined that this enumerated literal isn't
6239 handled by the case labels of the switch statement. */
6241 /* If the switch expression is a constant, we only really care
6242 about whether that constant is handled by the switch. */
6243 if (cond && tree_int_cst_compare (cond, value))
6246 /* If there is a default_node, the only relevant option is
6247 Wswitch-enum. Otherwise, if both are enabled then we prefer
6248 to warn using -Wswitch because -Wswitch is enabled by -Wall
6249 while -Wswitch-enum is explicit. */
6250 warning_at (switch_location,
6251 (default_node || !warn_switch
6254 "enumeration value %qE not handled in switch",
6255 TREE_PURPOSE (chain));
6258 /* Warn if there are case expressions that don't correspond to
6259 enumerators. This can occur since C and C++ don't enforce
6260 type-checking of assignments to enumeration variables.
6262 The time complexity here is now always O(N) worst case, since
6263 we should have marked both the lower bound and upper bound of
6264 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6265 above. This scan also resets those fields. */
6267 splay_tree_foreach (cases, match_case_to_enum, type);
6270 /* Finish an expression taking the address of LABEL (an
6271 IDENTIFIER_NODE). Returns an expression for the address.
6273 LOC is the location for the expression returned. */
6276 finish_label_address_expr (tree label, location_t loc)
6280 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6282 if (label == error_mark_node)
6283 return error_mark_node;
6285 label = lookup_label (label);
6286 if (label == NULL_TREE)
6287 result = null_pointer_node;
6290 TREE_USED (label) = 1;
6291 result = build1 (ADDR_EXPR, ptr_type_node, label);
6292 /* The current function is not necessarily uninlinable.
6293 Computed gotos are incompatible with inlining, but the value
6294 here could be used only in a diagnostic, for example. */
6295 protected_set_expr_location (result, loc);
6302 /* Given a boolean expression ARG, return a tree representing an increment
6303 or decrement (as indicated by CODE) of ARG. The front end must check for
6304 invalid cases (e.g., decrement in C++). */
6306 boolean_increment (enum tree_code code, tree arg)
6309 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6311 arg = stabilize_reference (arg);
6314 case PREINCREMENT_EXPR:
6315 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6317 case POSTINCREMENT_EXPR:
6318 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6319 arg = save_expr (arg);
6320 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6321 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6323 case PREDECREMENT_EXPR:
6324 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6325 invert_truthvalue_loc (input_location, arg));
6327 case POSTDECREMENT_EXPR:
6328 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6329 invert_truthvalue_loc (input_location, arg));
6330 arg = save_expr (arg);
6331 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6332 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6337 TREE_SIDE_EFFECTS (val) = 1;
6341 /* Built-in macros for stddef.h and stdint.h, that require macros
6342 defined in this file. */
6344 c_stddef_cpp_builtins(void)
6346 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6347 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6348 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6349 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6350 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6351 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6352 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6353 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6354 if (SIG_ATOMIC_TYPE)
6355 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6357 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6359 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6361 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6363 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6365 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6367 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6369 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6371 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6372 if (INT_LEAST8_TYPE)
6373 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6374 if (INT_LEAST16_TYPE)
6375 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6376 if (INT_LEAST32_TYPE)
6377 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6378 if (INT_LEAST64_TYPE)
6379 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6380 if (UINT_LEAST8_TYPE)
6381 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6382 if (UINT_LEAST16_TYPE)
6383 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6384 if (UINT_LEAST32_TYPE)
6385 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6386 if (UINT_LEAST64_TYPE)
6387 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6389 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6390 if (INT_FAST16_TYPE)
6391 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6392 if (INT_FAST32_TYPE)
6393 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6394 if (INT_FAST64_TYPE)
6395 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6396 if (UINT_FAST8_TYPE)
6397 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6398 if (UINT_FAST16_TYPE)
6399 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6400 if (UINT_FAST32_TYPE)
6401 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6402 if (UINT_FAST64_TYPE)
6403 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6405 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6407 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6411 c_init_attributes (void)
6413 /* Fill in the built_in_attributes array. */
6414 #define DEF_ATTR_NULL_TREE(ENUM) \
6415 built_in_attributes[(int) ENUM] = NULL_TREE;
6416 #define DEF_ATTR_INT(ENUM, VALUE) \
6417 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6418 #define DEF_ATTR_STRING(ENUM, VALUE) \
6419 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6420 #define DEF_ATTR_IDENT(ENUM, STRING) \
6421 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6422 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6423 built_in_attributes[(int) ENUM] \
6424 = tree_cons (built_in_attributes[(int) PURPOSE], \
6425 built_in_attributes[(int) VALUE], \
6426 built_in_attributes[(int) CHAIN]);
6427 #include "builtin-attrs.def"
6428 #undef DEF_ATTR_NULL_TREE
6430 #undef DEF_ATTR_IDENT
6431 #undef DEF_ATTR_TREE_LIST
6434 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6435 identifier as an argument, so the front end shouldn't look it up. */
6438 attribute_takes_identifier_p (const_tree attr_id)
6440 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6442 /* Unknown attribute that we'll end up ignoring, return true so we
6443 don't complain about an identifier argument. */
6445 else if (!strcmp ("mode", spec->name)
6446 || !strcmp ("format", spec->name)
6447 || !strcmp ("cleanup", spec->name))
6450 return targetm.attribute_takes_identifier_p (attr_id);
6453 /* Attribute handlers common to C front ends. */
6455 /* Handle a "packed" attribute; arguments as in
6456 struct attribute_spec.handler. */
6459 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6460 int flags, bool *no_add_attrs)
6464 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6465 *node = build_variant_type_copy (*node);
6466 TYPE_PACKED (*node) = 1;
6468 else if (TREE_CODE (*node) == FIELD_DECL)
6470 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6471 /* Still pack bitfields. */
6472 && ! DECL_INITIAL (*node))
6473 warning (OPT_Wattributes,
6474 "%qE attribute ignored for field of type %qT",
6475 name, TREE_TYPE (*node));
6477 DECL_PACKED (*node) = 1;
6479 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6480 used for DECL_REGISTER. It wouldn't mean anything anyway.
6481 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6482 that changes what the typedef is typing. */
6485 warning (OPT_Wattributes, "%qE attribute ignored", name);
6486 *no_add_attrs = true;
6492 /* Handle a "nocommon" attribute; arguments as in
6493 struct attribute_spec.handler. */
6496 handle_nocommon_attribute (tree *node, tree name,
6497 tree ARG_UNUSED (args),
6498 int ARG_UNUSED (flags), bool *no_add_attrs)
6500 if (TREE_CODE (*node) == VAR_DECL)
6501 DECL_COMMON (*node) = 0;
6504 warning (OPT_Wattributes, "%qE attribute ignored", name);
6505 *no_add_attrs = true;
6511 /* Handle a "common" attribute; arguments as in
6512 struct attribute_spec.handler. */
6515 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6516 int ARG_UNUSED (flags), bool *no_add_attrs)
6518 if (TREE_CODE (*node) == VAR_DECL)
6519 DECL_COMMON (*node) = 1;
6522 warning (OPT_Wattributes, "%qE attribute ignored", name);
6523 *no_add_attrs = true;
6529 /* Handle a "noreturn" attribute; arguments as in
6530 struct attribute_spec.handler. */
6533 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6534 int ARG_UNUSED (flags), bool *no_add_attrs)
6536 tree type = TREE_TYPE (*node);
6538 /* See FIXME comment in c_common_attribute_table. */
6539 if (TREE_CODE (*node) == FUNCTION_DECL
6540 || objc_method_decl (TREE_CODE (*node)))
6541 TREE_THIS_VOLATILE (*node) = 1;
6542 else if (TREE_CODE (type) == POINTER_TYPE
6543 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6545 = build_pointer_type
6546 (build_type_variant (TREE_TYPE (type),
6547 TYPE_READONLY (TREE_TYPE (type)), 1));
6550 warning (OPT_Wattributes, "%qE attribute ignored", name);
6551 *no_add_attrs = true;
6557 /* Handle a "hot" and attribute; arguments as in
6558 struct attribute_spec.handler. */
6561 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6562 int ARG_UNUSED (flags), bool *no_add_attrs)
6564 if (TREE_CODE (*node) == FUNCTION_DECL
6565 || TREE_CODE (*node) == LABEL_DECL)
6567 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6569 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6571 *no_add_attrs = true;
6573 /* Most of the rest of the hot processing is done later with
6574 lookup_attribute. */
6578 warning (OPT_Wattributes, "%qE attribute ignored", name);
6579 *no_add_attrs = true;
6585 /* Handle a "cold" and attribute; arguments as in
6586 struct attribute_spec.handler. */
6589 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6590 int ARG_UNUSED (flags), bool *no_add_attrs)
6592 if (TREE_CODE (*node) == FUNCTION_DECL
6593 || TREE_CODE (*node) == LABEL_DECL)
6595 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6597 warning (OPT_Wattributes, "%qE attribute conflicts with attribute %s",
6599 *no_add_attrs = true;
6601 /* Most of the rest of the cold processing is done later with
6602 lookup_attribute. */
6606 warning (OPT_Wattributes, "%qE attribute ignored", name);
6607 *no_add_attrs = true;
6613 /* Handle a "no_sanitize_address" attribute; arguments as in
6614 struct attribute_spec.handler. */
6617 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6620 if (TREE_CODE (*node) != FUNCTION_DECL)
6622 warning (OPT_Wattributes, "%qE attribute ignored", name);
6623 *no_add_attrs = true;
6629 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6630 struct attribute_spec.handler. */
6633 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6636 if (TREE_CODE (*node) != FUNCTION_DECL)
6637 warning (OPT_Wattributes, "%qE attribute ignored", name);
6638 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6639 DECL_ATTRIBUTES (*node)
6640 = tree_cons (get_identifier ("no_sanitize_address"),
6641 NULL_TREE, DECL_ATTRIBUTES (*node));
6642 *no_add_attrs = true;
6646 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6647 struct attribute_spec.handler. */
6650 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6653 if (TREE_CODE (*node) != FUNCTION_DECL)
6655 warning (OPT_Wattributes, "%qE attribute ignored", name);
6656 *no_add_attrs = true;
6662 /* Handle a "noinline" attribute; arguments as in
6663 struct attribute_spec.handler. */
6666 handle_noinline_attribute (tree *node, tree name,
6667 tree ARG_UNUSED (args),
6668 int ARG_UNUSED (flags), bool *no_add_attrs)
6670 if (TREE_CODE (*node) == FUNCTION_DECL)
6671 DECL_UNINLINABLE (*node) = 1;
6674 warning (OPT_Wattributes, "%qE attribute ignored", name);
6675 *no_add_attrs = true;
6681 /* Handle a "noclone" attribute; arguments as in
6682 struct attribute_spec.handler. */
6685 handle_noclone_attribute (tree *node, tree name,
6686 tree ARG_UNUSED (args),
6687 int ARG_UNUSED (flags), bool *no_add_attrs)
6689 if (TREE_CODE (*node) != FUNCTION_DECL)
6691 warning (OPT_Wattributes, "%qE attribute ignored", name);
6692 *no_add_attrs = true;
6698 /* Handle a "always_inline" attribute; arguments as in
6699 struct attribute_spec.handler. */
6702 handle_always_inline_attribute (tree *node, tree name,
6703 tree ARG_UNUSED (args),
6704 int ARG_UNUSED (flags),
6707 if (TREE_CODE (*node) == FUNCTION_DECL)
6709 /* Set the attribute and mark it for disregarding inline
6711 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6715 warning (OPT_Wattributes, "%qE attribute ignored", name);
6716 *no_add_attrs = true;
6722 /* Handle a "gnu_inline" attribute; arguments as in
6723 struct attribute_spec.handler. */
6726 handle_gnu_inline_attribute (tree *node, tree name,
6727 tree ARG_UNUSED (args),
6728 int ARG_UNUSED (flags),
6731 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6733 /* Do nothing else, just set the attribute. We'll get at
6734 it later with lookup_attribute. */
6738 warning (OPT_Wattributes, "%qE attribute ignored", name);
6739 *no_add_attrs = true;
6745 /* Handle a "leaf" attribute; arguments as in
6746 struct attribute_spec.handler. */
6749 handle_leaf_attribute (tree *node, tree name,
6750 tree ARG_UNUSED (args),
6751 int ARG_UNUSED (flags), bool *no_add_attrs)
6753 if (TREE_CODE (*node) != FUNCTION_DECL)
6755 warning (OPT_Wattributes, "%qE attribute ignored", name);
6756 *no_add_attrs = true;
6758 if (!TREE_PUBLIC (*node))
6760 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6761 *no_add_attrs = true;
6767 /* Handle an "artificial" attribute; arguments as in
6768 struct attribute_spec.handler. */
6771 handle_artificial_attribute (tree *node, tree name,
6772 tree ARG_UNUSED (args),
6773 int ARG_UNUSED (flags),
6776 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6778 /* Do nothing else, just set the attribute. We'll get at
6779 it later with lookup_attribute. */
6783 warning (OPT_Wattributes, "%qE attribute ignored", name);
6784 *no_add_attrs = true;
6790 /* Handle a "flatten" attribute; arguments as in
6791 struct attribute_spec.handler. */
6794 handle_flatten_attribute (tree *node, tree name,
6795 tree args ATTRIBUTE_UNUSED,
6796 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
6798 if (TREE_CODE (*node) == FUNCTION_DECL)
6799 /* Do nothing else, just set the attribute. We'll get at
6800 it later with lookup_attribute. */
6804 warning (OPT_Wattributes, "%qE attribute ignored", name);
6805 *no_add_attrs = true;
6811 /* Handle a "warning" or "error" attribute; arguments as in
6812 struct attribute_spec.handler. */
6815 handle_error_attribute (tree *node, tree name, tree args,
6816 int ARG_UNUSED (flags), bool *no_add_attrs)
6818 if (TREE_CODE (*node) == FUNCTION_DECL
6819 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
6820 /* Do nothing else, just set the attribute. We'll get at
6821 it later with lookup_attribute. */
6825 warning (OPT_Wattributes, "%qE attribute ignored", name);
6826 *no_add_attrs = true;
6832 /* Handle a "used" attribute; arguments as in
6833 struct attribute_spec.handler. */
6836 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6837 int ARG_UNUSED (flags), bool *no_add_attrs)
6841 if (TREE_CODE (node) == FUNCTION_DECL
6842 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
6843 || (TREE_CODE (node) == TYPE_DECL))
6845 TREE_USED (node) = 1;
6846 DECL_PRESERVE_P (node) = 1;
6847 if (TREE_CODE (node) == VAR_DECL)
6848 DECL_READ_P (node) = 1;
6852 warning (OPT_Wattributes, "%qE attribute ignored", name);
6853 *no_add_attrs = true;
6859 /* Handle a "unused" attribute; arguments as in
6860 struct attribute_spec.handler. */
6863 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6864 int flags, bool *no_add_attrs)
6870 if (TREE_CODE (decl) == PARM_DECL
6871 || TREE_CODE (decl) == VAR_DECL
6872 || TREE_CODE (decl) == FUNCTION_DECL
6873 || TREE_CODE (decl) == LABEL_DECL
6874 || TREE_CODE (decl) == TYPE_DECL)
6876 TREE_USED (decl) = 1;
6877 if (TREE_CODE (decl) == VAR_DECL
6878 || TREE_CODE (decl) == PARM_DECL)
6879 DECL_READ_P (decl) = 1;
6883 warning (OPT_Wattributes, "%qE attribute ignored", name);
6884 *no_add_attrs = true;
6889 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6890 *node = build_variant_type_copy (*node);
6891 TREE_USED (*node) = 1;
6897 /* Handle a "externally_visible" attribute; arguments as in
6898 struct attribute_spec.handler. */
6901 handle_externally_visible_attribute (tree *pnode, tree name,
6902 tree ARG_UNUSED (args),
6903 int ARG_UNUSED (flags),
6908 if (TREE_CODE (node) == FUNCTION_DECL || TREE_CODE (node) == VAR_DECL)
6910 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
6911 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
6913 warning (OPT_Wattributes,
6914 "%qE attribute have effect only on public objects", name);
6915 *no_add_attrs = true;
6920 warning (OPT_Wattributes, "%qE attribute ignored", name);
6921 *no_add_attrs = true;
6927 /* Handle a "const" attribute; arguments as in
6928 struct attribute_spec.handler. */
6931 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6932 int ARG_UNUSED (flags), bool *no_add_attrs)
6934 tree type = TREE_TYPE (*node);
6936 /* See FIXME comment on noreturn in c_common_attribute_table. */
6937 if (TREE_CODE (*node) == FUNCTION_DECL)
6938 TREE_READONLY (*node) = 1;
6939 else if (TREE_CODE (type) == POINTER_TYPE
6940 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6942 = build_pointer_type
6943 (build_type_variant (TREE_TYPE (type), 1,
6944 TREE_THIS_VOLATILE (TREE_TYPE (type))));
6947 warning (OPT_Wattributes, "%qE attribute ignored", name);
6948 *no_add_attrs = true;
6954 /* Handle a "transparent_union" attribute; arguments as in
6955 struct attribute_spec.handler. */
6958 handle_transparent_union_attribute (tree *node, tree name,
6959 tree ARG_UNUSED (args), int flags,
6964 *no_add_attrs = true;
6967 if (TREE_CODE (*node) == TYPE_DECL
6968 && ! (flags & ATTR_FLAG_CXX11))
6969 node = &TREE_TYPE (*node);
6972 if (TREE_CODE (type) == UNION_TYPE)
6974 /* Make sure that the first field will work for a transparent union.
6975 If the type isn't complete yet, leave the check to the code in
6977 if (TYPE_SIZE (type))
6979 tree first = first_field (type);
6980 if (first == NULL_TREE
6981 || DECL_ARTIFICIAL (first)
6982 || TYPE_MODE (type) != DECL_MODE (first))
6986 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6988 /* If the type isn't complete yet, setting the flag
6989 on a variant wouldn't ever be checked. */
6990 if (!TYPE_SIZE (type))
6993 /* build_duplicate_type doesn't work for C++. */
6994 if (c_dialect_cxx ())
6997 /* A type variant isn't good enough, since we don't a cast
6998 to such a type removed as a no-op. */
6999 *node = type = build_duplicate_type (type);
7002 TYPE_TRANSPARENT_AGGR (type) = 1;
7007 warning (OPT_Wattributes, "%qE attribute ignored", name);
7011 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7012 get the requested priority for a constructor or destructor,
7013 possibly issuing diagnostics for invalid or reserved
7016 static priority_type
7017 get_priority (tree args, bool is_destructor)
7023 return DEFAULT_INIT_PRIORITY;
7025 if (!SUPPORTS_INIT_PRIORITY)
7028 error ("destructor priorities are not supported");
7030 error ("constructor priorities are not supported");
7031 return DEFAULT_INIT_PRIORITY;
7034 arg = TREE_VALUE (args);
7035 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7037 if (arg == error_mark_node)
7038 return DEFAULT_INIT_PRIORITY;
7039 arg = default_conversion (arg);
7040 if (!tree_fits_shwi_p (arg)
7041 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7044 pri = tree_to_shwi (arg);
7045 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7048 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7052 "destructor priorities from 0 to %d are reserved "
7053 "for the implementation",
7054 MAX_RESERVED_INIT_PRIORITY);
7057 "constructor priorities from 0 to %d are reserved "
7058 "for the implementation",
7059 MAX_RESERVED_INIT_PRIORITY);
7065 error ("destructor priorities must be integers from 0 to %d inclusive",
7068 error ("constructor priorities must be integers from 0 to %d inclusive",
7070 return DEFAULT_INIT_PRIORITY;
7073 /* Handle a "constructor" attribute; arguments as in
7074 struct attribute_spec.handler. */
7077 handle_constructor_attribute (tree *node, tree name, tree args,
7078 int ARG_UNUSED (flags),
7082 tree type = TREE_TYPE (decl);
7084 if (TREE_CODE (decl) == FUNCTION_DECL
7085 && TREE_CODE (type) == FUNCTION_TYPE
7086 && decl_function_context (decl) == 0)
7088 priority_type priority;
7089 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7090 priority = get_priority (args, /*is_destructor=*/false);
7091 SET_DECL_INIT_PRIORITY (decl, priority);
7092 TREE_USED (decl) = 1;
7096 warning (OPT_Wattributes, "%qE attribute ignored", name);
7097 *no_add_attrs = true;
7103 /* Handle a "destructor" attribute; arguments as in
7104 struct attribute_spec.handler. */
7107 handle_destructor_attribute (tree *node, tree name, tree args,
7108 int ARG_UNUSED (flags),
7112 tree type = TREE_TYPE (decl);
7114 if (TREE_CODE (decl) == FUNCTION_DECL
7115 && TREE_CODE (type) == FUNCTION_TYPE
7116 && decl_function_context (decl) == 0)
7118 priority_type priority;
7119 DECL_STATIC_DESTRUCTOR (decl) = 1;
7120 priority = get_priority (args, /*is_destructor=*/true);
7121 SET_DECL_FINI_PRIORITY (decl, priority);
7122 TREE_USED (decl) = 1;
7126 warning (OPT_Wattributes, "%qE attribute ignored", name);
7127 *no_add_attrs = true;
7133 /* Nonzero if the mode is a valid vector mode for this architecture.
7134 This returns nonzero even if there is no hardware support for the
7135 vector mode, but we can emulate with narrower modes. */
7138 vector_mode_valid_p (enum machine_mode mode)
7140 enum mode_class mclass = GET_MODE_CLASS (mode);
7141 enum machine_mode innermode;
7143 /* Doh! What's going on? */
7144 if (mclass != MODE_VECTOR_INT
7145 && mclass != MODE_VECTOR_FLOAT
7146 && mclass != MODE_VECTOR_FRACT
7147 && mclass != MODE_VECTOR_UFRACT
7148 && mclass != MODE_VECTOR_ACCUM
7149 && mclass != MODE_VECTOR_UACCUM)
7152 /* Hardware support. Woo hoo! */
7153 if (targetm.vector_mode_supported_p (mode))
7156 innermode = GET_MODE_INNER (mode);
7158 /* We should probably return 1 if requesting V4DI and we have no DI,
7159 but we have V2DI, but this is probably very unlikely. */
7161 /* If we have support for the inner mode, we can safely emulate it.
7162 We may not have V2DI, but me can emulate with a pair of DIs. */
7163 return targetm.scalar_mode_supported_p (innermode);
7167 /* Handle a "mode" attribute; arguments as in
7168 struct attribute_spec.handler. */
7171 handle_mode_attribute (tree *node, tree name, tree args,
7172 int ARG_UNUSED (flags), bool *no_add_attrs)
7175 tree ident = TREE_VALUE (args);
7177 *no_add_attrs = true;
7179 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7180 warning (OPT_Wattributes, "%qE attribute ignored", name);
7184 const char *p = IDENTIFIER_POINTER (ident);
7185 int len = strlen (p);
7186 enum machine_mode mode = VOIDmode;
7190 if (len > 4 && p[0] == '_' && p[1] == '_'
7191 && p[len - 1] == '_' && p[len - 2] == '_')
7193 char *newp = (char *) alloca (len - 1);
7195 strcpy (newp, &p[2]);
7196 newp[len - 4] = '\0';
7200 /* Change this type to have a type with the specified mode.
7201 First check for the special modes. */
7202 if (!strcmp (p, "byte"))
7204 else if (!strcmp (p, "word"))
7206 else if (!strcmp (p, "pointer"))
7208 else if (!strcmp (p, "libgcc_cmp_return"))
7209 mode = targetm.libgcc_cmp_return_mode ();
7210 else if (!strcmp (p, "libgcc_shift_count"))
7211 mode = targetm.libgcc_shift_count_mode ();
7212 else if (!strcmp (p, "unwind_word"))
7213 mode = targetm.unwind_word_mode ();
7215 for (j = 0; j < NUM_MACHINE_MODES; j++)
7216 if (!strcmp (p, GET_MODE_NAME (j)))
7218 mode = (enum machine_mode) j;
7222 if (mode == VOIDmode)
7224 error ("unknown machine mode %qE", ident);
7229 switch (GET_MODE_CLASS (mode))
7232 case MODE_PARTIAL_INT:
7234 case MODE_DECIMAL_FLOAT:
7239 valid_mode = targetm.scalar_mode_supported_p (mode);
7242 case MODE_COMPLEX_INT:
7243 case MODE_COMPLEX_FLOAT:
7244 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7247 case MODE_VECTOR_INT:
7248 case MODE_VECTOR_FLOAT:
7249 case MODE_VECTOR_FRACT:
7250 case MODE_VECTOR_UFRACT:
7251 case MODE_VECTOR_ACCUM:
7252 case MODE_VECTOR_UACCUM:
7253 warning (OPT_Wattributes, "specifying vector types with "
7254 "__attribute__ ((mode)) is deprecated");
7255 warning (OPT_Wattributes,
7256 "use __attribute__ ((vector_size)) instead");
7257 valid_mode = vector_mode_valid_p (mode);
7265 error ("unable to emulate %qs", p);
7269 if (POINTER_TYPE_P (type))
7271 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7272 tree (*fn)(tree, enum machine_mode, bool);
7274 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7276 error ("invalid pointer mode %qs", p);
7280 if (TREE_CODE (type) == POINTER_TYPE)
7281 fn = build_pointer_type_for_mode;
7283 fn = build_reference_type_for_mode;
7284 typefm = fn (TREE_TYPE (type), mode, false);
7288 /* For fixed-point modes, we need to test if the signness of type
7289 and the machine mode are consistent. */
7290 if (ALL_FIXED_POINT_MODE_P (mode)
7291 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7293 error ("signedness of type and machine mode %qs don%'t match", p);
7296 /* For fixed-point modes, we need to pass saturating info. */
7297 typefm = lang_hooks.types.type_for_mode (mode,
7298 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7299 : TYPE_UNSIGNED (type));
7302 if (typefm == NULL_TREE)
7304 error ("no data type for mode %qs", p);
7307 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7309 /* For enumeral types, copy the precision from the integer
7310 type returned above. If not an INTEGER_TYPE, we can't use
7311 this mode for this type. */
7312 if (TREE_CODE (typefm) != INTEGER_TYPE)
7314 error ("cannot use mode %qs for enumeral types", p);
7318 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7320 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7325 /* We cannot build a type variant, as there's code that assumes
7326 that TYPE_MAIN_VARIANT has the same mode. This includes the
7327 debug generators. Instead, create a subrange type. This
7328 results in all of the enumeral values being emitted only once
7329 in the original, and the subtype gets them by reference. */
7330 if (TYPE_UNSIGNED (type))
7331 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7333 typefm = make_signed_type (TYPE_PRECISION (typefm));
7334 TREE_TYPE (typefm) = type;
7337 else if (VECTOR_MODE_P (mode)
7338 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7339 : TREE_CODE (type) != TREE_CODE (typefm))
7341 error ("mode %qs applied to inappropriate type", p);
7351 /* Handle a "section" attribute; arguments as in
7352 struct attribute_spec.handler. */
7355 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7356 int ARG_UNUSED (flags), bool *no_add_attrs)
7360 if (targetm_common.have_named_sections)
7362 user_defined_section_attribute = true;
7364 if ((TREE_CODE (decl) == FUNCTION_DECL
7365 || TREE_CODE (decl) == VAR_DECL)
7366 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7368 if (TREE_CODE (decl) == VAR_DECL
7369 && current_function_decl != NULL_TREE
7370 && !TREE_STATIC (decl))
7372 error_at (DECL_SOURCE_LOCATION (decl),
7373 "section attribute cannot be specified for "
7375 *no_add_attrs = true;
7378 /* The decl may have already been given a section attribute
7379 from a previous declaration. Ensure they match. */
7380 else if (DECL_SECTION_NAME (decl) != NULL_TREE
7381 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
7382 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7384 error ("section of %q+D conflicts with previous declaration",
7386 *no_add_attrs = true;
7388 else if (TREE_CODE (decl) == VAR_DECL
7389 && !targetm.have_tls && targetm.emutls.tmpl_section
7390 && DECL_THREAD_LOCAL_P (decl))
7392 error ("section of %q+D cannot be overridden", *node);
7393 *no_add_attrs = true;
7396 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
7400 error ("section attribute not allowed for %q+D", *node);
7401 *no_add_attrs = true;
7406 error_at (DECL_SOURCE_LOCATION (*node),
7407 "section attributes are not supported for this target");
7408 *no_add_attrs = true;
7414 /* Check whether ALIGN is a valid user-specified alignment. If so,
7415 return its base-2 log; if not, output an error and return -1. If
7416 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7419 check_user_alignment (const_tree align, bool allow_zero)
7423 if (TREE_CODE (align) != INTEGER_CST
7424 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7426 error ("requested alignment is not an integer constant");
7429 else if (allow_zero && integer_zerop (align))
7431 else if (tree_int_cst_sgn (align) == -1
7432 || (i = tree_log2 (align)) == -1)
7434 error ("requested alignment is not a positive power of 2");
7437 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7439 error ("requested alignment is too large");
7446 If in c++-11, check if the c++-11 alignment constraint with respect
7447 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7448 c++-11 mode, does nothing.
7452 [* if the constant expression evaluates to a fundamental alignment,
7453 the alignment requirement of the declared entity shall be the
7454 specified fundamental alignment.
7456 * if the constant expression evaluates to an extended alignment
7457 and the implementation supports that alignment in the context
7458 of the declaration, the alignment of the declared entity shall
7461 * if the constant expression evaluates to an extended alignment
7462 and the implementation does not support that alignment in the
7463 context of the declaration, the program is ill-formed]. */
7466 check_cxx_fundamental_alignment_constraints (tree node,
7470 bool alignment_too_large_p = false;
7471 unsigned requested_alignment = 1U << align_log;
7472 unsigned max_align = 0;
7474 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7475 || (node == NULL_TREE || node == error_mark_node))
7478 if (cxx_fundamental_alignment_p (requested_alignment))
7483 if (TREE_STATIC (node))
7485 /* For file scope variables and static members, the target
7486 supports alignments that are at most
7487 MAX_OFILE_ALIGNMENT. */
7488 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7489 alignment_too_large_p = true;
7493 #ifdef BIGGEST_FIELD_ALIGNMENT
7494 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7496 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7498 /* For non-static members, the target supports either
7499 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7500 if it is defined or BIGGEST_ALIGNMENT. */
7501 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7502 if (TREE_CODE (node) == FIELD_DECL
7503 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7504 alignment_too_large_p = true;
7505 #undef MAX_TARGET_FIELD_ALIGNMENT
7506 /* For stack variables, the target supports at most
7507 MAX_STACK_ALIGNMENT. */
7508 else if (decl_function_context (node) != NULL
7509 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7510 alignment_too_large_p = true;
7513 else if (TYPE_P (node))
7515 /* Let's be liberal for types. */
7516 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7517 alignment_too_large_p = true;
7520 if (alignment_too_large_p)
7521 pedwarn (input_location, OPT_Wattributes,
7522 "requested alignment %d is larger than %d",
7523 requested_alignment, max_align);
7525 return !alignment_too_large_p;
7528 /* Handle a "aligned" attribute; arguments as in
7529 struct attribute_spec.handler. */
7532 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7533 int flags, bool *no_add_attrs)
7535 tree decl = NULL_TREE;
7543 align_expr = TREE_VALUE (args);
7544 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE)
7545 align_expr = default_conversion (align_expr);
7548 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7553 type = &TREE_TYPE (decl);
7554 is_type = TREE_CODE (*node) == TYPE_DECL;
7556 else if (TYPE_P (*node))
7557 type = node, is_type = 1;
7559 if ((i = check_user_alignment (align_expr, false)) == -1
7560 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7561 *no_add_attrs = true;
7564 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7565 /* OK, modify the type in place. */;
7566 /* If we have a TYPE_DECL, then copy the type, so that we
7567 don't accidentally modify a builtin type. See pushdecl. */
7568 else if (decl && TREE_TYPE (decl) != error_mark_node
7569 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7571 tree tt = TREE_TYPE (decl);
7572 *type = build_variant_type_copy (*type);
7573 DECL_ORIGINAL_TYPE (decl) = tt;
7574 TYPE_NAME (*type) = decl;
7575 TREE_USED (*type) = TREE_USED (decl);
7576 TREE_TYPE (decl) = *type;
7579 *type = build_variant_type_copy (*type);
7581 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
7582 TYPE_USER_ALIGN (*type) = 1;
7584 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7585 && TREE_CODE (decl) != FIELD_DECL)
7587 error ("alignment may not be specified for %q+D", decl);
7588 *no_add_attrs = true;
7590 else if (DECL_USER_ALIGN (decl)
7591 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7592 /* C++-11 [dcl.align/4]:
7594 When multiple alignment-specifiers are specified for an
7595 entity, the alignment requirement shall be set to the
7596 strictest specified alignment.
7598 This formally comes from the c++11 specification but we are
7599 doing it for the GNU attribute syntax as well. */
7600 *no_add_attrs = true;
7601 else if (TREE_CODE (decl) == FUNCTION_DECL
7602 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7604 if (DECL_USER_ALIGN (decl))
7605 error ("alignment for %q+D was previously specified as %d "
7606 "and may not be decreased", decl,
7607 DECL_ALIGN (decl) / BITS_PER_UNIT);
7609 error ("alignment for %q+D must be at least %d", decl,
7610 DECL_ALIGN (decl) / BITS_PER_UNIT);
7611 *no_add_attrs = true;
7615 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
7616 DECL_USER_ALIGN (decl) = 1;
7622 /* Handle a "weak" attribute; arguments as in
7623 struct attribute_spec.handler. */
7626 handle_weak_attribute (tree *node, tree name,
7627 tree ARG_UNUSED (args),
7628 int ARG_UNUSED (flags),
7629 bool * ARG_UNUSED (no_add_attrs))
7631 if (TREE_CODE (*node) == FUNCTION_DECL
7632 && DECL_DECLARED_INLINE_P (*node))
7634 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7635 *no_add_attrs = true;
7637 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7639 error ("indirect function %q+D cannot be declared weak", *node);
7640 *no_add_attrs = true;
7643 else if (TREE_CODE (*node) == FUNCTION_DECL
7644 || TREE_CODE (*node) == VAR_DECL)
7645 declare_weak (*node);
7647 warning (OPT_Wattributes, "%qE attribute ignored", name);
7652 /* Handle an "alias" or "ifunc" attribute; arguments as in
7653 struct attribute_spec.handler, except that IS_ALIAS tells us
7654 whether this is an alias as opposed to ifunc attribute. */
7657 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7662 if (TREE_CODE (decl) != FUNCTION_DECL
7663 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
7665 warning (OPT_Wattributes, "%qE attribute ignored", name);
7666 *no_add_attrs = true;
7668 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
7669 || (TREE_CODE (decl) != FUNCTION_DECL
7670 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7671 /* A static variable declaration is always a tentative definition,
7672 but the alias is a non-tentative definition which overrides. */
7673 || (TREE_CODE (decl) != FUNCTION_DECL
7674 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
7676 error ("%q+D defined both normally and as %qE attribute", decl, name);
7677 *no_add_attrs = true;
7681 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7682 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7684 error ("weak %q+D cannot be defined %qE", decl, name);
7685 *no_add_attrs = true;
7689 /* Note that the very first time we process a nested declaration,
7690 decl_function_context will not be set. Indeed, *would* never
7691 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7692 we do below. After such frobbery, pushdecl would set the context.
7693 In any case, this is never what we want. */
7694 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
7698 id = TREE_VALUE (args);
7699 if (TREE_CODE (id) != STRING_CST)
7701 error ("attribute %qE argument not a string", name);
7702 *no_add_attrs = true;
7705 id = get_identifier (TREE_STRING_POINTER (id));
7706 /* This counts as a use of the object pointed to. */
7709 if (TREE_CODE (decl) == FUNCTION_DECL)
7710 DECL_INITIAL (decl) = error_mark_node;
7712 TREE_STATIC (decl) = 1;
7715 /* ifuncs are also aliases, so set that attribute too. */
7716 DECL_ATTRIBUTES (decl)
7717 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
7721 warning (OPT_Wattributes, "%qE attribute ignored", name);
7722 *no_add_attrs = true;
7728 /* Handle an "alias" or "ifunc" attribute; arguments as in
7729 struct attribute_spec.handler. */
7732 handle_ifunc_attribute (tree *node, tree name, tree args,
7733 int ARG_UNUSED (flags), bool *no_add_attrs)
7735 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
7738 /* Handle an "alias" or "ifunc" attribute; arguments as in
7739 struct attribute_spec.handler. */
7742 handle_alias_attribute (tree *node, tree name, tree args,
7743 int ARG_UNUSED (flags), bool *no_add_attrs)
7745 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
7748 /* Handle a "weakref" attribute; arguments as in struct
7749 attribute_spec.handler. */
7752 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7753 int flags, bool *no_add_attrs)
7755 tree attr = NULL_TREE;
7757 /* We must ignore the attribute when it is associated with
7758 local-scoped decls, since attribute alias is ignored and many
7759 such symbols do not even have a DECL_WEAK field. */
7760 if (decl_function_context (*node)
7761 || current_function_decl
7762 || (TREE_CODE (*node) != VAR_DECL && TREE_CODE (*node) != FUNCTION_DECL))
7764 warning (OPT_Wattributes, "%qE attribute ignored", name);
7765 *no_add_attrs = true;
7769 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7771 error ("indirect function %q+D cannot be declared weakref", *node);
7772 *no_add_attrs = true;
7776 /* The idea here is that `weakref("name")' mutates into `weakref,
7777 alias("name")', and weakref without arguments, in turn,
7778 implicitly adds weak. */
7782 attr = tree_cons (get_identifier ("alias"), args, attr);
7783 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
7785 *no_add_attrs = true;
7787 decl_attributes (node, attr, flags);
7791 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
7792 error_at (DECL_SOURCE_LOCATION (*node),
7793 "weakref attribute must appear before alias attribute");
7795 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
7796 and that isn't supported; and because it wants to add it to
7797 the list of weak decls, which isn't helpful. */
7798 DECL_WEAK (*node) = 1;
7804 /* Handle an "visibility" attribute; arguments as in
7805 struct attribute_spec.handler. */
7808 handle_visibility_attribute (tree *node, tree name, tree args,
7809 int ARG_UNUSED (flags),
7810 bool *ARG_UNUSED (no_add_attrs))
7813 tree id = TREE_VALUE (args);
7814 enum symbol_visibility vis;
7818 if (TREE_CODE (*node) == ENUMERAL_TYPE)
7820 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
7822 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
7826 else if (TYPE_FIELDS (*node))
7828 error ("%qE attribute ignored because %qT is already defined",
7833 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
7835 warning (OPT_Wattributes, "%qE attribute ignored", name);
7839 if (TREE_CODE (id) != STRING_CST)
7841 error ("visibility argument not a string");
7845 /* If this is a type, set the visibility on the type decl. */
7848 decl = TYPE_NAME (decl);
7851 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7853 warning (OPT_Wattributes, "%qE attribute ignored on types",
7859 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
7860 vis = VISIBILITY_DEFAULT;
7861 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
7862 vis = VISIBILITY_INTERNAL;
7863 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
7864 vis = VISIBILITY_HIDDEN;
7865 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
7866 vis = VISIBILITY_PROTECTED;
7869 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
7870 vis = VISIBILITY_DEFAULT;
7873 if (DECL_VISIBILITY_SPECIFIED (decl)
7874 && vis != DECL_VISIBILITY (decl))
7876 tree attributes = (TYPE_P (*node)
7877 ? TYPE_ATTRIBUTES (*node)
7878 : DECL_ATTRIBUTES (decl));
7879 if (lookup_attribute ("visibility", attributes))
7880 error ("%qD redeclared with different visibility", decl);
7881 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7882 && lookup_attribute ("dllimport", attributes))
7883 error ("%qD was declared %qs which implies default visibility",
7885 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7886 && lookup_attribute ("dllexport", attributes))
7887 error ("%qD was declared %qs which implies default visibility",
7891 DECL_VISIBILITY (decl) = vis;
7892 DECL_VISIBILITY_SPECIFIED (decl) = 1;
7894 /* Go ahead and attach the attribute to the node as well. This is needed
7895 so we can determine whether we have VISIBILITY_DEFAULT because the
7896 visibility was not specified, or because it was explicitly overridden
7897 from the containing scope. */
7902 /* Determine the ELF symbol visibility for DECL, which is either a
7903 variable or a function. It is an error to use this function if a
7904 definition of DECL is not available in this translation unit.
7905 Returns true if the final visibility has been determined by this
7906 function; false if the caller is free to make additional
7910 c_determine_visibility (tree decl)
7912 gcc_assert (TREE_CODE (decl) == VAR_DECL
7913 || TREE_CODE (decl) == FUNCTION_DECL);
7915 /* If the user explicitly specified the visibility with an
7916 attribute, honor that. DECL_VISIBILITY will have been set during
7917 the processing of the attribute. We check for an explicit
7918 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
7919 to distinguish the use of an attribute from the use of a "#pragma
7920 GCC visibility push(...)"; in the latter case we still want other
7921 considerations to be able to overrule the #pragma. */
7922 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
7923 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7924 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
7925 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
7928 /* Set default visibility to whatever the user supplied with
7929 visibility_specified depending on #pragma GCC visibility. */
7930 if (!DECL_VISIBILITY_SPECIFIED (decl))
7932 if (visibility_options.inpragma
7933 || DECL_VISIBILITY (decl) != default_visibility)
7935 DECL_VISIBILITY (decl) = default_visibility;
7936 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
7937 /* If visibility changed and DECL already has DECL_RTL, ensure
7938 symbol flags are updated. */
7939 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
7940 || TREE_CODE (decl) == FUNCTION_DECL)
7941 && DECL_RTL_SET_P (decl))
7942 make_decl_rtl (decl);
7948 /* Handle an "tls_model" attribute; arguments as in
7949 struct attribute_spec.handler. */
7952 handle_tls_model_attribute (tree *node, tree name, tree args,
7953 int ARG_UNUSED (flags), bool *no_add_attrs)
7957 enum tls_model kind;
7959 *no_add_attrs = true;
7961 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
7963 warning (OPT_Wattributes, "%qE attribute ignored", name);
7967 kind = DECL_TLS_MODEL (decl);
7968 id = TREE_VALUE (args);
7969 if (TREE_CODE (id) != STRING_CST)
7971 error ("tls_model argument not a string");
7975 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
7976 kind = TLS_MODEL_LOCAL_EXEC;
7977 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
7978 kind = TLS_MODEL_INITIAL_EXEC;
7979 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
7980 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
7981 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
7982 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7984 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
7986 DECL_TLS_MODEL (decl) = kind;
7990 /* Handle a "no_instrument_function" attribute; arguments as in
7991 struct attribute_spec.handler. */
7994 handle_no_instrument_function_attribute (tree *node, tree name,
7995 tree ARG_UNUSED (args),
7996 int ARG_UNUSED (flags),
8001 if (TREE_CODE (decl) != FUNCTION_DECL)
8003 error_at (DECL_SOURCE_LOCATION (decl),
8004 "%qE attribute applies only to functions", name);
8005 *no_add_attrs = true;
8008 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8013 /* Handle a "malloc" attribute; arguments as in
8014 struct attribute_spec.handler. */
8017 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8018 int ARG_UNUSED (flags), bool *no_add_attrs)
8020 if (TREE_CODE (*node) == FUNCTION_DECL
8021 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8022 DECL_IS_MALLOC (*node) = 1;
8025 warning (OPT_Wattributes, "%qE attribute ignored", name);
8026 *no_add_attrs = true;
8032 /* Handle a "alloc_size" attribute; arguments as in
8033 struct attribute_spec.handler. */
8036 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8037 int ARG_UNUSED (flags), bool *no_add_attrs)
8039 unsigned arg_count = type_num_arguments (*node);
8040 for (; args; args = TREE_CHAIN (args))
8042 tree position = TREE_VALUE (args);
8043 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8044 && TREE_CODE (position) != FUNCTION_DECL)
8045 position = default_conversion (position);
8047 if (!tree_fits_uhwi_p (position)
8049 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8051 warning (OPT_Wattributes,
8052 "alloc_size parameter outside range");
8053 *no_add_attrs = true;
8060 /* Handle a "alloc_align" attribute; arguments as in
8061 struct attribute_spec.handler. */
8064 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8067 unsigned arg_count = type_num_arguments (*node);
8068 tree position = TREE_VALUE (args);
8069 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8070 position = default_conversion (position);
8072 if (!tree_fits_uhwi_p (position)
8074 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8076 warning (OPT_Wattributes,
8077 "alloc_align parameter outside range");
8078 *no_add_attrs = true;
8084 /* Handle a "assume_aligned" attribute; arguments as in
8085 struct attribute_spec.handler. */
8088 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8091 for (; args; args = TREE_CHAIN (args))
8093 tree position = TREE_VALUE (args);
8094 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8095 && TREE_CODE (position) != FUNCTION_DECL)
8096 position = default_conversion (position);
8098 if (TREE_CODE (position) != INTEGER_CST)
8100 warning (OPT_Wattributes,
8101 "assume_aligned parameter not integer constant");
8102 *no_add_attrs = true;
8109 /* Handle a "fn spec" attribute; arguments as in
8110 struct attribute_spec.handler. */
8113 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8114 tree args, int ARG_UNUSED (flags),
8115 bool *no_add_attrs ATTRIBUTE_UNUSED)
8118 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8119 && !TREE_CHAIN (args));
8123 /* Handle a "warn_unused" attribute; arguments as in
8124 struct attribute_spec.handler. */
8127 handle_warn_unused_attribute (tree *node, tree name,
8128 tree args ATTRIBUTE_UNUSED,
8129 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8132 /* Do nothing else, just set the attribute. We'll get at
8133 it later with lookup_attribute. */
8137 warning (OPT_Wattributes, "%qE attribute ignored", name);
8138 *no_add_attrs = true;
8144 /* Handle an "omp declare simd" attribute; arguments as in
8145 struct attribute_spec.handler. */
8148 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8153 /* Handle an "omp declare target" attribute; arguments as in
8154 struct attribute_spec.handler. */
8157 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8162 /* Handle a "returns_twice" attribute; arguments as in
8163 struct attribute_spec.handler. */
8166 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8167 int ARG_UNUSED (flags), bool *no_add_attrs)
8169 if (TREE_CODE (*node) == FUNCTION_DECL)
8170 DECL_IS_RETURNS_TWICE (*node) = 1;
8173 warning (OPT_Wattributes, "%qE attribute ignored", name);
8174 *no_add_attrs = true;
8180 /* Handle a "no_limit_stack" attribute; arguments as in
8181 struct attribute_spec.handler. */
8184 handle_no_limit_stack_attribute (tree *node, tree name,
8185 tree ARG_UNUSED (args),
8186 int ARG_UNUSED (flags),
8191 if (TREE_CODE (decl) != FUNCTION_DECL)
8193 error_at (DECL_SOURCE_LOCATION (decl),
8194 "%qE attribute applies only to functions", name);
8195 *no_add_attrs = true;
8197 else if (DECL_INITIAL (decl))
8199 error_at (DECL_SOURCE_LOCATION (decl),
8200 "can%'t set %qE attribute after definition", name);
8201 *no_add_attrs = true;
8204 DECL_NO_LIMIT_STACK (decl) = 1;
8209 /* Handle a "pure" attribute; arguments as in
8210 struct attribute_spec.handler. */
8213 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8214 int ARG_UNUSED (flags), bool *no_add_attrs)
8216 if (TREE_CODE (*node) == FUNCTION_DECL)
8217 DECL_PURE_P (*node) = 1;
8218 /* ??? TODO: Support types. */
8221 warning (OPT_Wattributes, "%qE attribute ignored", name);
8222 *no_add_attrs = true;
8228 /* Digest an attribute list destined for a transactional memory statement.
8229 ALLOWED is the set of attributes that are allowed for this statement;
8230 return the attribute we parsed. Multiple attributes are never allowed. */
8233 parse_tm_stmt_attr (tree attrs, int allowed)
8238 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8240 tree a = TREE_PURPOSE (attrs);
8243 if (is_attribute_p ("outer", a))
8244 m = TM_STMT_ATTR_OUTER;
8246 if ((m & allowed) == 0)
8248 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8257 else if (m_seen == m)
8258 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8260 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8266 /* Transform a TM attribute name into a maskable integer and back.
8267 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8268 to how the lack of an attribute is treated. */
8271 tm_attr_to_mask (tree attr)
8275 if (is_attribute_p ("transaction_safe", attr))
8276 return TM_ATTR_SAFE;
8277 if (is_attribute_p ("transaction_callable", attr))
8278 return TM_ATTR_CALLABLE;
8279 if (is_attribute_p ("transaction_pure", attr))
8280 return TM_ATTR_PURE;
8281 if (is_attribute_p ("transaction_unsafe", attr))
8282 return TM_ATTR_IRREVOCABLE;
8283 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8284 return TM_ATTR_MAY_CANCEL_OUTER;
8289 tm_mask_to_attr (int mask)
8295 str = "transaction_safe";
8297 case TM_ATTR_CALLABLE:
8298 str = "transaction_callable";
8301 str = "transaction_pure";
8303 case TM_ATTR_IRREVOCABLE:
8304 str = "transaction_unsafe";
8306 case TM_ATTR_MAY_CANCEL_OUTER:
8307 str = "transaction_may_cancel_outer";
8312 return get_identifier (str);
8315 /* Return the first TM attribute seen in LIST. */
8318 find_tm_attribute (tree list)
8320 for (; list ; list = TREE_CHAIN (list))
8322 tree name = TREE_PURPOSE (list);
8323 if (tm_attr_to_mask (name) != 0)
8329 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8330 Here we accept only function types, and verify that none of the other
8331 function TM attributes are also applied. */
8332 /* ??? We need to accept class types for C++, but not C. This greatly
8333 complicates this function, since we can no longer rely on the extra
8334 processing given by function_type_required. */
8337 handle_tm_attribute (tree *node, tree name, tree args,
8338 int flags, bool *no_add_attrs)
8340 /* Only one path adds the attribute; others don't. */
8341 *no_add_attrs = true;
8343 switch (TREE_CODE (*node))
8347 /* Only tm_callable and tm_safe apply to classes. */
8348 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8355 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8356 if (old_name == name)
8358 else if (old_name != NULL_TREE)
8359 error ("type was previously declared %qE", old_name);
8361 *no_add_attrs = false;
8367 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8368 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8370 tree fn_tmp = TREE_TYPE (*node);
8371 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8372 *node = build_pointer_type (fn_tmp);
8379 /* If a function is next, pass it on to be tried next. */
8380 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8381 return tree_cons (name, args, NULL);
8384 warning (OPT_Wattributes, "%qE attribute ignored", name);
8391 /* Handle the TM_WRAP attribute; arguments as in
8392 struct attribute_spec.handler. */
8395 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8396 int ARG_UNUSED (flags), bool *no_add_attrs)
8400 /* We don't need the attribute even on success, since we
8401 record the entry in an external table. */
8402 *no_add_attrs = true;
8404 if (TREE_CODE (decl) != FUNCTION_DECL)
8405 warning (OPT_Wattributes, "%qE attribute ignored", name);
8408 tree wrap_decl = TREE_VALUE (args);
8409 if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8410 && TREE_CODE (wrap_decl) != VAR_DECL
8411 && TREE_CODE (wrap_decl) != FUNCTION_DECL)
8412 error ("%qE argument not an identifier", name);
8415 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8416 wrap_decl = lookup_name (wrap_decl);
8417 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8419 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8420 TREE_TYPE (wrap_decl)))
8421 record_tm_replacement (wrap_decl, decl);
8423 error ("%qD is not compatible with %qD", wrap_decl, decl);
8426 error ("transaction_wrap argument is not a function");
8433 /* Ignore the given attribute. Used when this attribute may be usefully
8434 overridden by the target, but is not used generically. */
8437 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8438 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8441 *no_add_attrs = true;
8445 /* Handle a "no vops" attribute; arguments as in
8446 struct attribute_spec.handler. */
8449 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8450 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8451 bool *ARG_UNUSED (no_add_attrs))
8453 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8454 DECL_IS_NOVOPS (*node) = 1;
8458 /* Handle a "deprecated" attribute; arguments as in
8459 struct attribute_spec.handler. */
8462 handle_deprecated_attribute (tree *node, tree name,
8463 tree args, int flags,
8466 tree type = NULL_TREE;
8468 tree what = NULL_TREE;
8471 *no_add_attrs = true;
8472 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8474 error ("deprecated message is not a string");
8475 *no_add_attrs = true;
8481 type = TREE_TYPE (decl);
8483 if (TREE_CODE (decl) == TYPE_DECL
8484 || TREE_CODE (decl) == PARM_DECL
8485 || TREE_CODE (decl) == VAR_DECL
8486 || TREE_CODE (decl) == FUNCTION_DECL
8487 || TREE_CODE (decl) == FIELD_DECL
8488 || objc_method_decl (TREE_CODE (decl)))
8489 TREE_DEPRECATED (decl) = 1;
8493 else if (TYPE_P (*node))
8495 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8496 *node = build_variant_type_copy (*node);
8497 TREE_DEPRECATED (*node) = 1;
8505 *no_add_attrs = true;
8506 if (type && TYPE_NAME (type))
8508 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
8509 what = TYPE_NAME (*node);
8510 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8511 && DECL_NAME (TYPE_NAME (type)))
8512 what = DECL_NAME (TYPE_NAME (type));
8515 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
8517 warning (OPT_Wattributes, "%qE attribute ignored", name);
8523 /* Handle a "vector_size" attribute; arguments as in
8524 struct attribute_spec.handler. */
8527 handle_vector_size_attribute (tree *node, tree name, tree args,
8528 int ARG_UNUSED (flags),
8531 unsigned HOST_WIDE_INT vecsize, nunits;
8532 enum machine_mode orig_mode;
8533 tree type = *node, new_type, size;
8535 *no_add_attrs = true;
8537 size = TREE_VALUE (args);
8538 if (size && TREE_CODE (size) != IDENTIFIER_NODE)
8539 size = default_conversion (size);
8541 if (!tree_fits_uhwi_p (size))
8543 warning (OPT_Wattributes, "%qE attribute ignored", name);
8547 /* Get the vector size (in bytes). */
8548 vecsize = tree_to_uhwi (size);
8550 /* We need to provide for vector pointers, vector arrays, and
8551 functions returning vectors. For example:
8553 __attribute__((vector_size(16))) short *foo;
8555 In this case, the mode is SI, but the type being modified is
8556 HI, so we need to look further. */
8558 while (POINTER_TYPE_P (type)
8559 || TREE_CODE (type) == FUNCTION_TYPE
8560 || TREE_CODE (type) == METHOD_TYPE
8561 || TREE_CODE (type) == ARRAY_TYPE
8562 || TREE_CODE (type) == OFFSET_TYPE)
8563 type = TREE_TYPE (type);
8565 /* Get the mode of the type being modified. */
8566 orig_mode = TYPE_MODE (type);
8568 if ((!INTEGRAL_TYPE_P (type)
8569 && !SCALAR_FLOAT_TYPE_P (type)
8570 && !FIXED_POINT_TYPE_P (type))
8571 || (!SCALAR_FLOAT_MODE_P (orig_mode)
8572 && GET_MODE_CLASS (orig_mode) != MODE_INT
8573 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
8574 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8575 || TREE_CODE (type) == BOOLEAN_TYPE)
8577 error ("invalid vector type for attribute %qE", name);
8581 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
8583 error ("vector size not an integral multiple of component size");
8589 error ("zero vector size");
8593 /* Calculate how many units fit in the vector. */
8594 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
8595 if (nunits & (nunits - 1))
8597 error ("number of components of the vector not a power of two");
8601 new_type = build_vector_type (type, nunits);
8603 /* Build back pointers if needed. */
8604 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
8609 /* Handle the "nonnull" attribute. */
8611 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
8612 tree args, int ARG_UNUSED (flags),
8616 unsigned HOST_WIDE_INT attr_arg_num;
8618 /* If no arguments are specified, all pointer arguments should be
8619 non-null. Verify a full prototype is given so that the arguments
8620 will have the correct types when we actually check them later. */
8623 if (!prototype_p (type))
8625 error ("nonnull attribute without arguments on a non-prototype");
8626 *no_add_attrs = true;
8631 /* Argument list specified. Verify that each argument number references
8632 a pointer argument. */
8633 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
8635 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
8637 tree arg = TREE_VALUE (args);
8638 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
8639 && TREE_CODE (arg) != FUNCTION_DECL)
8640 arg = default_conversion (arg);
8642 if (!get_nonnull_operand (arg, &arg_num))
8644 error ("nonnull argument has invalid operand number (argument %lu)",
8645 (unsigned long) attr_arg_num);
8646 *no_add_attrs = true;
8650 if (prototype_p (type))
8652 function_args_iterator iter;
8655 function_args_iter_init (&iter, type);
8656 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
8658 argument = function_args_iter_cond (&iter);
8659 if (argument == NULL_TREE || ck_num == arg_num)
8664 || TREE_CODE (argument) == VOID_TYPE)
8666 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
8667 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8668 *no_add_attrs = true;
8672 if (TREE_CODE (argument) != POINTER_TYPE)
8674 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
8675 (unsigned long) attr_arg_num, (unsigned long) arg_num);
8676 *no_add_attrs = true;
8685 /* Check the argument list of a function call for null in argument slots
8686 that are marked as requiring a non-null pointer argument. The NARGS
8687 arguments are passed in the array ARGARRAY.
8691 check_function_nonnull (tree attrs, int nargs, tree *argarray)
8696 attrs = lookup_attribute ("nonnull", attrs);
8697 if (attrs == NULL_TREE)
8701 /* See if any of the nonnull attributes has no arguments. If so,
8702 then every pointer argument is checked (in which case the check
8703 for pointer type is done in check_nonnull_arg). */
8704 if (TREE_VALUE (a) != NULL_TREE)
8706 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
8707 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
8710 for (i = 0; i < nargs; i++)
8711 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
8715 /* Walk the argument list. If we encounter an argument number we
8716 should check for non-null, do it. */
8717 for (i = 0; i < nargs; i++)
8719 for (a = attrs; ; a = TREE_CHAIN (a))
8721 a = lookup_attribute ("nonnull", a);
8722 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
8727 check_function_arguments_recurse (check_nonnull_arg, NULL,
8728 argarray[i], i + 1);
8733 /* Check that the Nth argument of a function call (counting backwards
8734 from the end) is a (pointer)0. The NARGS arguments are passed in the
8738 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
8740 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
8747 function_args_iterator iter;
8750 /* Skip over the named arguments. */
8751 FOREACH_FUNCTION_ARGS (fntype, t, iter)
8758 if (TREE_VALUE (attr))
8760 tree p = TREE_VALUE (TREE_VALUE (attr));
8761 pos = TREE_INT_CST_LOW (p);
8764 /* The sentinel must be one of the varargs, i.e.
8765 in position >= the number of fixed arguments. */
8766 if ((nargs - 1 - pos) < len)
8768 warning (OPT_Wformat_,
8769 "not enough variable arguments to fit a sentinel");
8773 /* Validate the sentinel. */
8774 sentinel = argarray[nargs - 1 - pos];
8775 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
8776 || !integer_zerop (sentinel))
8777 /* Although __null (in C++) is only an integer we allow it
8778 nevertheless, as we are guaranteed that it's exactly
8779 as wide as a pointer, and we don't want to force
8780 users to cast the NULL they have written there.
8781 We warn with -Wstrict-null-sentinel, though. */
8782 && (warn_strict_null_sentinel || null_node != sentinel))
8783 warning (OPT_Wformat_, "missing sentinel in function call");
8787 /* Helper for check_function_nonnull; given a list of operands which
8788 must be non-null in ARGS, determine if operand PARAM_NUM should be
8792 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
8794 unsigned HOST_WIDE_INT arg_num = 0;
8796 for (; args; args = TREE_CHAIN (args))
8798 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
8802 if (arg_num == param_num)
8808 /* Check that the function argument PARAM (which is operand number
8809 PARAM_NUM) is non-null. This is called by check_function_nonnull
8810 via check_function_arguments_recurse. */
8813 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
8814 unsigned HOST_WIDE_INT param_num)
8816 /* Just skip checking the argument if it's not a pointer. This can
8817 happen if the "nonnull" attribute was given without an operand
8818 list (which means to check every pointer argument). */
8820 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
8823 if (integer_zerop (param))
8824 warning (OPT_Wnonnull, "null argument where non-null required "
8825 "(argument %lu)", (unsigned long) param_num);
8828 /* Helper for nonnull attribute handling; fetch the operand number
8829 from the attribute argument list. */
8832 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
8834 /* Verify the arg number is a constant. */
8835 if (TREE_CODE (arg_num_expr) != INTEGER_CST
8836 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
8839 *valp = TREE_INT_CST_LOW (arg_num_expr);
8843 /* Handle a "nothrow" attribute; arguments as in
8844 struct attribute_spec.handler. */
8847 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8848 int ARG_UNUSED (flags), bool *no_add_attrs)
8850 if (TREE_CODE (*node) == FUNCTION_DECL)
8851 TREE_NOTHROW (*node) = 1;
8852 /* ??? TODO: Support types. */
8855 warning (OPT_Wattributes, "%qE attribute ignored", name);
8856 *no_add_attrs = true;
8862 /* Handle a "cleanup" attribute; arguments as in
8863 struct attribute_spec.handler. */
8866 handle_cleanup_attribute (tree *node, tree name, tree args,
8867 int ARG_UNUSED (flags), bool *no_add_attrs)
8870 tree cleanup_id, cleanup_decl;
8872 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
8873 for global destructors in C++. This requires infrastructure that
8874 we don't have generically at the moment. It's also not a feature
8875 we'd be missing too much, since we do have attribute constructor. */
8876 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
8878 warning (OPT_Wattributes, "%qE attribute ignored", name);
8879 *no_add_attrs = true;
8883 /* Verify that the argument is a function in scope. */
8884 /* ??? We could support pointers to functions here as well, if
8885 that was considered desirable. */
8886 cleanup_id = TREE_VALUE (args);
8887 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
8889 error ("cleanup argument not an identifier");
8890 *no_add_attrs = true;
8893 cleanup_decl = lookup_name (cleanup_id);
8894 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
8896 error ("cleanup argument not a function");
8897 *no_add_attrs = true;
8901 /* That the function has proper type is checked with the
8902 eventual call to build_function_call. */
8907 /* Handle a "warn_unused_result" attribute. No special handling. */
8910 handle_warn_unused_result_attribute (tree *node, tree name,
8911 tree ARG_UNUSED (args),
8912 int ARG_UNUSED (flags), bool *no_add_attrs)
8914 /* Ignore the attribute for functions not returning any value. */
8915 if (VOID_TYPE_P (TREE_TYPE (*node)))
8917 warning (OPT_Wattributes, "%qE attribute ignored", name);
8918 *no_add_attrs = true;
8924 /* Handle a "sentinel" attribute. */
8927 handle_sentinel_attribute (tree *node, tree name, tree args,
8928 int ARG_UNUSED (flags), bool *no_add_attrs)
8930 if (!prototype_p (*node))
8932 warning (OPT_Wattributes,
8933 "%qE attribute requires prototypes with named arguments", name);
8934 *no_add_attrs = true;
8938 if (!stdarg_p (*node))
8940 warning (OPT_Wattributes,
8941 "%qE attribute only applies to variadic functions", name);
8942 *no_add_attrs = true;
8948 tree position = TREE_VALUE (args);
8950 if (TREE_CODE (position) != INTEGER_CST)
8952 warning (OPT_Wattributes,
8953 "requested position is not an integer constant");
8954 *no_add_attrs = true;
8958 if (tree_int_cst_lt (position, integer_zero_node))
8960 warning (OPT_Wattributes,
8961 "requested position is less than zero");
8962 *no_add_attrs = true;
8970 /* Handle a "type_generic" attribute. */
8973 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
8974 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8975 bool * ARG_UNUSED (no_add_attrs))
8977 /* Ensure we have a function type. */
8978 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
8980 /* Ensure we have a variadic function. */
8981 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
8986 /* Handle a "target" attribute. */
8989 handle_target_attribute (tree *node, tree name, tree args, int flags,
8992 /* Ensure we have a function type. */
8993 if (TREE_CODE (*node) != FUNCTION_DECL)
8995 warning (OPT_Wattributes, "%qE attribute ignored", name);
8996 *no_add_attrs = true;
8998 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9000 *no_add_attrs = true;
9005 /* Arguments being collected for optimization. */
9006 typedef const char *const_char_p; /* For DEF_VEC_P. */
9007 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9010 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9011 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9012 false for #pragma GCC optimize. */
9015 parse_optimize_options (tree args, bool attr_p)
9020 int saved_flag_strict_aliasing;
9021 const char **opt_argv;
9022 struct cl_decoded_option *decoded_options;
9023 unsigned int decoded_options_count;
9026 /* Build up argv vector. Just in case the string is stored away, use garbage
9027 collected strings. */
9028 vec_safe_truncate (optimize_args, 0);
9029 vec_safe_push (optimize_args, (const char *) NULL);
9031 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9033 tree value = TREE_VALUE (ap);
9035 if (TREE_CODE (value) == INTEGER_CST)
9038 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9039 vec_safe_push (optimize_args, ggc_strdup (buffer));
9042 else if (TREE_CODE (value) == STRING_CST)
9044 /* Split string into multiple substrings. */
9045 size_t len = TREE_STRING_LENGTH (value);
9046 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9047 char *end = p + len;
9051 while (next_p != NULL)
9057 comma = strchr (p, ',');
9070 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9072 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9074 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9078 warning (OPT_Wattributes,
9079 "bad option %s to optimize attribute", p);
9081 warning (OPT_Wpragmas,
9082 "bad option %s to pragma attribute", p);
9090 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9091 itself is -Os, and any other switch begins with a -f. */
9092 if ((*p >= '0' && *p <= '9')
9093 || (p[0] == 's' && p[1] == '\0'))
9099 memcpy (r, p, len2);
9101 vec_safe_push (optimize_args, (const char *) q);
9107 opt_argc = optimize_args->length ();
9108 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9110 for (i = 1; i < opt_argc; i++)
9111 opt_argv[i] = (*optimize_args)[i];
9113 saved_flag_strict_aliasing = flag_strict_aliasing;
9115 /* Now parse the options. */
9116 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9118 &decoded_options_count);
9119 decode_options (&global_options, &global_options_set,
9120 decoded_options, decoded_options_count,
9121 input_location, global_dc);
9123 targetm.override_options_after_change();
9125 /* Don't allow changing -fstrict-aliasing. */
9126 flag_strict_aliasing = saved_flag_strict_aliasing;
9128 optimize_args->truncate (0);
9132 /* For handling "optimize" attribute. arguments as in
9133 struct attribute_spec.handler. */
9136 handle_optimize_attribute (tree *node, tree name, tree args,
9137 int ARG_UNUSED (flags), bool *no_add_attrs)
9139 /* Ensure we have a function type. */
9140 if (TREE_CODE (*node) != FUNCTION_DECL)
9142 warning (OPT_Wattributes, "%qE attribute ignored", name);
9143 *no_add_attrs = true;
9147 struct cl_optimization cur_opts;
9148 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9150 /* Save current options. */
9151 cl_optimization_save (&cur_opts, &global_options);
9153 /* If we previously had some optimization options, use them as the
9156 cl_optimization_restore (&global_options,
9157 TREE_OPTIMIZATION (old_opts));
9159 /* Parse options, and update the vector. */
9160 parse_optimize_options (args, true);
9161 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9162 = build_optimization_node (&global_options);
9164 /* Restore current options. */
9165 cl_optimization_restore (&global_options, &cur_opts);
9171 /* Handle a "no_split_stack" attribute. */
9174 handle_no_split_stack_attribute (tree *node, tree name,
9175 tree ARG_UNUSED (args),
9176 int ARG_UNUSED (flags),
9181 if (TREE_CODE (decl) != FUNCTION_DECL)
9183 error_at (DECL_SOURCE_LOCATION (decl),
9184 "%qE attribute applies only to functions", name);
9185 *no_add_attrs = true;
9187 else if (DECL_INITIAL (decl))
9189 error_at (DECL_SOURCE_LOCATION (decl),
9190 "can%'t set %qE attribute after definition", name);
9191 *no_add_attrs = true;
9197 /* Handle a "returns_nonnull" attribute; arguments as in
9198 struct attribute_spec.handler. */
9201 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9204 // Even without a prototype we still have a return type we can check.
9205 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9207 error ("returns_nonnull attribute on a function not returning a pointer");
9208 *no_add_attrs = true;
9214 /* Check for valid arguments being passed to a function with FNTYPE.
9215 There are NARGS arguments in the array ARGARRAY. */
9217 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9219 /* Check for null being passed in a pointer argument that must be
9220 non-null. We also need to do this if format checking is enabled. */
9223 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9225 /* Check for errors in format strings. */
9227 if (warn_format || warn_suggest_attribute_format)
9228 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9231 check_function_sentinel (fntype, nargs, argarray);
9234 /* Generic argument checking recursion routine. PARAM is the argument to
9235 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9236 once the argument is resolved. CTX is context for the callback. */
9238 check_function_arguments_recurse (void (*callback)
9239 (void *, tree, unsigned HOST_WIDE_INT),
9240 void *ctx, tree param,
9241 unsigned HOST_WIDE_INT param_num)
9243 if (CONVERT_EXPR_P (param)
9244 && (TYPE_PRECISION (TREE_TYPE (param))
9245 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9247 /* Strip coercion. */
9248 check_function_arguments_recurse (callback, ctx,
9249 TREE_OPERAND (param, 0), param_num);
9253 if (TREE_CODE (param) == CALL_EXPR)
9255 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9257 bool found_format_arg = false;
9259 /* See if this is a call to a known internationalization function
9260 that modifies a format arg. Such a function may have multiple
9261 format_arg attributes (for example, ngettext). */
9263 for (attrs = TYPE_ATTRIBUTES (type);
9265 attrs = TREE_CHAIN (attrs))
9266 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9269 tree format_num_expr;
9272 call_expr_arg_iterator iter;
9274 /* Extract the argument number, which was previously checked
9276 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9278 format_num = tree_to_uhwi (format_num_expr);
9280 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9282 inner_arg = next_call_expr_arg (&iter), i++)
9283 if (i == format_num)
9285 check_function_arguments_recurse (callback, ctx,
9286 inner_arg, param_num);
9287 found_format_arg = true;
9292 /* If we found a format_arg attribute and did a recursive check,
9293 we are done with checking this argument. Otherwise, we continue
9294 and this will be considered a non-literal. */
9295 if (found_format_arg)
9299 if (TREE_CODE (param) == COND_EXPR)
9301 /* Check both halves of the conditional expression. */
9302 check_function_arguments_recurse (callback, ctx,
9303 TREE_OPERAND (param, 1), param_num);
9304 check_function_arguments_recurse (callback, ctx,
9305 TREE_OPERAND (param, 2), param_num);
9309 (*callback) (ctx, param, param_num);
9312 /* Checks for a builtin function FNDECL that the number of arguments
9313 NARGS against the required number REQUIRED and issues an error if
9314 there is a mismatch. Returns true if the number of arguments is
9315 correct, otherwise false. */
9318 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9320 if (nargs < required)
9322 error_at (input_location,
9323 "not enough arguments to function %qE", fndecl);
9326 else if (nargs > required)
9328 error_at (input_location,
9329 "too many arguments to function %qE", fndecl);
9335 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9336 Returns false if there was an error, otherwise true. */
9339 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9341 if (!DECL_BUILT_IN (fndecl)
9342 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9345 switch (DECL_FUNCTION_CODE (fndecl))
9347 case BUILT_IN_CONSTANT_P:
9348 return builtin_function_validate_nargs (fndecl, nargs, 1);
9350 case BUILT_IN_ISFINITE:
9351 case BUILT_IN_ISINF:
9352 case BUILT_IN_ISINF_SIGN:
9353 case BUILT_IN_ISNAN:
9354 case BUILT_IN_ISNORMAL:
9355 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9357 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9359 error ("non-floating-point argument in call to "
9360 "function %qE", fndecl);
9367 case BUILT_IN_ISGREATER:
9368 case BUILT_IN_ISGREATEREQUAL:
9369 case BUILT_IN_ISLESS:
9370 case BUILT_IN_ISLESSEQUAL:
9371 case BUILT_IN_ISLESSGREATER:
9372 case BUILT_IN_ISUNORDERED:
9373 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9375 enum tree_code code0, code1;
9376 code0 = TREE_CODE (TREE_TYPE (args[0]));
9377 code1 = TREE_CODE (TREE_TYPE (args[1]));
9378 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9379 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9380 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9382 error ("non-floating-point arguments in call to "
9383 "function %qE", fndecl);
9390 case BUILT_IN_FPCLASSIFY:
9391 if (builtin_function_validate_nargs (fndecl, nargs, 6))
9396 if (TREE_CODE (args[i]) != INTEGER_CST)
9398 error ("non-const integer argument %u in call to function %qE",
9403 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9405 error ("non-floating-point argument in call to function %qE",
9413 case BUILT_IN_ASSUME_ALIGNED:
9414 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9416 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9418 error ("non-integer argument 3 in call to function %qE", fndecl);
9430 /* Function to help qsort sort FIELD_DECLs by name order. */
9433 field_decl_cmp (const void *x_p, const void *y_p)
9435 const tree *const x = (const tree *const) x_p;
9436 const tree *const y = (const tree *const) y_p;
9438 if (DECL_NAME (*x) == DECL_NAME (*y))
9439 /* A nontype is "greater" than a type. */
9440 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9441 if (DECL_NAME (*x) == NULL_TREE)
9443 if (DECL_NAME (*y) == NULL_TREE)
9445 if (DECL_NAME (*x) < DECL_NAME (*y))
9451 gt_pointer_operator new_value;
9455 /* This routine compares two fields like field_decl_cmp but using the
9456 pointer operator in resort_data. */
9459 resort_field_decl_cmp (const void *x_p, const void *y_p)
9461 const tree *const x = (const tree *const) x_p;
9462 const tree *const y = (const tree *const) y_p;
9464 if (DECL_NAME (*x) == DECL_NAME (*y))
9465 /* A nontype is "greater" than a type. */
9466 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9467 if (DECL_NAME (*x) == NULL_TREE)
9469 if (DECL_NAME (*y) == NULL_TREE)
9472 tree d1 = DECL_NAME (*x);
9473 tree d2 = DECL_NAME (*y);
9474 resort_data.new_value (&d1, resort_data.cookie);
9475 resort_data.new_value (&d2, resort_data.cookie);
9482 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9485 resort_sorted_fields (void *obj,
9486 void * ARG_UNUSED (orig_obj),
9487 gt_pointer_operator new_value,
9490 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9491 resort_data.new_value = new_value;
9492 resort_data.cookie = cookie;
9493 qsort (&sf->elts[0], sf->len, sizeof (tree),
9494 resort_field_decl_cmp);
9497 /* Subroutine of c_parse_error.
9498 Return the result of concatenating LHS and RHS. RHS is really
9499 a string literal, its first character is indicated by RHS_START and
9500 RHS_SIZE is its length (including the terminating NUL character).
9502 The caller is responsible for deleting the returned pointer. */
9505 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
9507 const int lhs_size = strlen (lhs);
9508 char *result = XNEWVEC (char, lhs_size + rhs_size);
9509 strncpy (result, lhs, lhs_size);
9510 strncpy (result + lhs_size, rhs_start, rhs_size);
9514 /* Issue the error given by GMSGID, indicating that it occurred before
9515 TOKEN, which had the associated VALUE. */
9518 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
9519 tree value, unsigned char token_flags)
9521 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
9523 char *message = NULL;
9525 if (token_type == CPP_EOF)
9526 message = catenate_messages (gmsgid, " at end of input");
9527 else if (token_type == CPP_CHAR
9528 || token_type == CPP_WCHAR
9529 || token_type == CPP_CHAR16
9530 || token_type == CPP_CHAR32)
9532 unsigned int val = TREE_INT_CST_LOW (value);
9551 if (val <= UCHAR_MAX && ISGRAPH (val))
9552 message = catenate_messages (gmsgid, " before %s'%c'");
9554 message = catenate_messages (gmsgid, " before %s'\\x%x'");
9556 error (message, prefix, val);
9560 else if (token_type == CPP_CHAR_USERDEF
9561 || token_type == CPP_WCHAR_USERDEF
9562 || token_type == CPP_CHAR16_USERDEF
9563 || token_type == CPP_CHAR32_USERDEF)
9564 message = catenate_messages (gmsgid,
9565 " before user-defined character literal");
9566 else if (token_type == CPP_STRING_USERDEF
9567 || token_type == CPP_WSTRING_USERDEF
9568 || token_type == CPP_STRING16_USERDEF
9569 || token_type == CPP_STRING32_USERDEF
9570 || token_type == CPP_UTF8STRING_USERDEF)
9571 message = catenate_messages (gmsgid, " before user-defined string literal");
9572 else if (token_type == CPP_STRING
9573 || token_type == CPP_WSTRING
9574 || token_type == CPP_STRING16
9575 || token_type == CPP_STRING32
9576 || token_type == CPP_UTF8STRING)
9577 message = catenate_messages (gmsgid, " before string constant");
9578 else if (token_type == CPP_NUMBER)
9579 message = catenate_messages (gmsgid, " before numeric constant");
9580 else if (token_type == CPP_NAME)
9582 message = catenate_messages (gmsgid, " before %qE");
9583 error (message, value);
9587 else if (token_type == CPP_PRAGMA)
9588 message = catenate_messages (gmsgid, " before %<#pragma%>");
9589 else if (token_type == CPP_PRAGMA_EOL)
9590 message = catenate_messages (gmsgid, " before end of line");
9591 else if (token_type == CPP_DECLTYPE)
9592 message = catenate_messages (gmsgid, " before %<decltype%>");
9593 else if (token_type < N_TTYPES)
9595 message = catenate_messages (gmsgid, " before %qs token");
9596 error (message, cpp_type2name (token_type, token_flags));
9608 #undef catenate_messages
9611 /* Mapping for cpp message reasons to the options that enable them. */
9613 struct reason_option_codes_t
9615 const int reason; /* cpplib message reason. */
9616 const int option_code; /* gcc option that controls this message. */
9619 static const struct reason_option_codes_t option_codes[] = {
9620 {CPP_W_DEPRECATED, OPT_Wdeprecated},
9621 {CPP_W_COMMENTS, OPT_Wcomment},
9622 {CPP_W_TRIGRAPHS, OPT_Wtrigraphs},
9623 {CPP_W_MULTICHAR, OPT_Wmultichar},
9624 {CPP_W_TRADITIONAL, OPT_Wtraditional},
9625 {CPP_W_LONG_LONG, OPT_Wlong_long},
9626 {CPP_W_ENDIF_LABELS, OPT_Wendif_labels},
9627 {CPP_W_VARIADIC_MACROS, OPT_Wvariadic_macros},
9628 {CPP_W_BUILTIN_MACRO_REDEFINED, OPT_Wbuiltin_macro_redefined},
9629 {CPP_W_UNDEF, OPT_Wundef},
9630 {CPP_W_UNUSED_MACROS, OPT_Wunused_macros},
9631 {CPP_W_CXX_OPERATOR_NAMES, OPT_Wc___compat},
9632 {CPP_W_NORMALIZE, OPT_Wnormalized_},
9633 {CPP_W_INVALID_PCH, OPT_Winvalid_pch},
9634 {CPP_W_WARNING_DIRECTIVE, OPT_Wcpp},
9635 {CPP_W_LITERAL_SUFFIX, OPT_Wliteral_suffix},
9636 {CPP_W_DATE_TIME, OPT_Wdate_time},
9640 /* Return the gcc option code associated with the reason for a cpp
9641 message, or 0 if none. */
9644 c_option_controlling_cpp_error (int reason)
9646 const struct reason_option_codes_t *entry;
9648 for (entry = option_codes; entry->reason != CPP_W_NONE; entry++)
9650 if (entry->reason == reason)
9651 return entry->option_code;
9656 /* Callback from cpp_error for PFILE to print diagnostics from the
9657 preprocessor. The diagnostic is of type LEVEL, with REASON set
9658 to the reason code if LEVEL is represents a warning, at location
9659 LOCATION unless this is after lexing and the compiler's location
9660 should be used instead, with column number possibly overridden by
9661 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
9662 the arguments. Returns true if a diagnostic was emitted, false
9666 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
9667 location_t location, unsigned int column_override,
9668 const char *msg, va_list *ap)
9670 diagnostic_info diagnostic;
9671 diagnostic_t dlevel;
9672 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
9677 case CPP_DL_WARNING_SYSHDR:
9680 global_dc->dc_warn_system_headers = 1;
9682 case CPP_DL_WARNING:
9685 dlevel = DK_WARNING;
9687 case CPP_DL_PEDWARN:
9688 if (flag_no_output && !flag_pedantic_errors)
9690 dlevel = DK_PEDWARN;
9708 location = input_location;
9709 diagnostic_set_info_translated (&diagnostic, msg, ap,
9711 if (column_override)
9712 diagnostic_override_column (&diagnostic, column_override);
9713 diagnostic_override_option_index (&diagnostic,
9714 c_option_controlling_cpp_error (reason));
9715 ret = report_diagnostic (&diagnostic);
9716 if (level == CPP_DL_WARNING_SYSHDR)
9717 global_dc->dc_warn_system_headers = save_warn_system_headers;
9721 /* Convert a character from the host to the target execution character
9722 set. cpplib handles this, mostly. */
9725 c_common_to_target_charset (HOST_WIDE_INT c)
9727 /* Character constants in GCC proper are sign-extended under -fsigned-char,
9728 zero-extended under -fno-signed-char. cpplib insists that characters
9729 and character constants are always unsigned. Hence we must convert
9731 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
9733 uc = cpp_host_to_exec_charset (parse_in, uc);
9735 if (flag_signed_char)
9736 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
9737 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
9742 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
9743 references with an INDIRECT_REF of a constant at the bottom; much like the
9744 traditional rendering of offsetof as a macro. Return the folded result. */
9747 fold_offsetof_1 (tree expr)
9751 switch (TREE_CODE (expr))
9757 error ("cannot apply %<offsetof%> to static data member %qD", expr);
9758 return error_mark_node;
9762 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
9763 return error_mark_node;
9767 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
9769 error ("cannot apply %<offsetof%> to a non constant address");
9770 return error_mark_node;
9772 return TREE_OPERAND (expr, 0);
9775 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9776 if (base == error_mark_node)
9779 t = TREE_OPERAND (expr, 1);
9780 if (DECL_C_BIT_FIELD (t))
9782 error ("attempt to take address of bit-field structure "
9784 return error_mark_node;
9786 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
9787 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
9792 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
9793 if (base == error_mark_node)
9796 t = TREE_OPERAND (expr, 1);
9798 /* Check if the offset goes beyond the upper bound of the array. */
9799 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
9801 tree upbound = array_ref_up_bound (expr);
9802 if (upbound != NULL_TREE
9803 && TREE_CODE (upbound) == INTEGER_CST
9804 && !tree_int_cst_equal (upbound,
9805 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
9807 upbound = size_binop (PLUS_EXPR, upbound,
9808 build_int_cst (TREE_TYPE (upbound), 1));
9809 if (tree_int_cst_lt (upbound, t))
9813 for (v = TREE_OPERAND (expr, 0);
9814 TREE_CODE (v) == COMPONENT_REF;
9815 v = TREE_OPERAND (v, 0))
9816 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
9819 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
9820 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
9821 if (TREE_CODE (fld_chain) == FIELD_DECL)
9827 /* Don't warn if the array might be considered a poor
9828 man's flexible array member with a very permissive
9829 definition thereof. */
9830 if (TREE_CODE (v) == ARRAY_REF
9831 || TREE_CODE (v) == COMPONENT_REF)
9832 warning (OPT_Warray_bounds,
9833 "index %E denotes an offset "
9834 "greater than size of %qT",
9835 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
9840 t = convert (sizetype, t);
9841 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
9845 /* Handle static members of volatile structs. */
9846 t = TREE_OPERAND (expr, 1);
9847 gcc_assert (TREE_CODE (t) == VAR_DECL);
9848 return fold_offsetof_1 (t);
9854 return fold_build_pointer_plus (base, off);
9857 /* Likewise, but convert it to the return type of offsetof. */
9860 fold_offsetof (tree expr)
9862 return convert (size_type_node, fold_offsetof_1 (expr));
9865 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
9866 expression, because B will always be true. */
9869 warn_for_omitted_condop (location_t location, tree cond)
9871 if (truth_value_p (TREE_CODE (cond)))
9872 warning_at (location, OPT_Wparentheses,
9873 "the omitted middle operand in ?: will always be %<true%>, "
9874 "suggest explicit middle operand");
9877 /* Give an error for storing into ARG, which is 'const'. USE indicates
9878 how ARG was being used. */
9881 readonly_error (location_t loc, tree arg, enum lvalue_use use)
9883 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
9885 /* Using this macro rather than (for example) arrays of messages
9886 ensures that all the format strings are checked at compile
9888 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
9889 : (use == lv_increment ? (I) \
9890 : (use == lv_decrement ? (D) : (AS))))
9891 if (TREE_CODE (arg) == COMPONENT_REF)
9893 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
9894 error_at (loc, READONLY_MSG (G_("assignment of member "
9895 "%qD in read-only object"),
9896 G_("increment of member "
9897 "%qD in read-only object"),
9898 G_("decrement of member "
9899 "%qD in read-only object"),
9900 G_("member %qD in read-only object "
9901 "used as %<asm%> output")),
9902 TREE_OPERAND (arg, 1));
9904 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
9905 G_("increment of read-only member %qD"),
9906 G_("decrement of read-only member %qD"),
9907 G_("read-only member %qD used as %<asm%> output")),
9908 TREE_OPERAND (arg, 1));
9910 else if (TREE_CODE (arg) == VAR_DECL)
9911 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
9912 G_("increment of read-only variable %qD"),
9913 G_("decrement of read-only variable %qD"),
9914 G_("read-only variable %qD used as %<asm%> output")),
9916 else if (TREE_CODE (arg) == PARM_DECL)
9917 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
9918 G_("increment of read-only parameter %qD"),
9919 G_("decrement of read-only parameter %qD"),
9920 G_("read-only parameter %qD use as %<asm%> output")),
9922 else if (TREE_CODE (arg) == RESULT_DECL)
9924 gcc_assert (c_dialect_cxx ());
9925 error_at (loc, READONLY_MSG (G_("assignment of "
9926 "read-only named return value %qD"),
9928 "read-only named return value %qD"),
9930 "read-only named return value %qD"),
9931 G_("read-only named return value %qD "
9932 "used as %<asm%>output")),
9935 else if (TREE_CODE (arg) == FUNCTION_DECL)
9936 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
9937 G_("increment of function %qD"),
9938 G_("decrement of function %qD"),
9939 G_("function %qD used as %<asm%> output")),
9942 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
9943 G_("increment of read-only location %qE"),
9944 G_("decrement of read-only location %qE"),
9945 G_("read-only location %qE used as %<asm%> output")),
9949 /* Print an error message for an invalid lvalue. USE says
9950 how the lvalue is being used and so selects the error message. LOC
9951 is the location for the error. */
9954 lvalue_error (location_t loc, enum lvalue_use use)
9959 error_at (loc, "lvalue required as left operand of assignment");
9962 error_at (loc, "lvalue required as increment operand");
9965 error_at (loc, "lvalue required as decrement operand");
9968 error_at (loc, "lvalue required as unary %<&%> operand");
9971 error_at (loc, "lvalue required in asm statement");
9978 /* Print an error message for an invalid indirection of type TYPE.
9979 ERRSTRING is the name of the operator for the indirection. */
9982 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
9987 gcc_assert (c_dialect_cxx ());
9988 error_at (loc, "invalid type argument (have %qT)", type);
9990 case RO_ARRAY_INDEXING:
9992 "invalid type argument of array indexing (have %qT)",
9997 "invalid type argument of unary %<*%> (have %qT)",
10002 "invalid type argument of %<->%> (have %qT)",
10005 case RO_ARROW_STAR:
10007 "invalid type argument of %<->*%> (have %qT)",
10010 case RO_IMPLICIT_CONVERSION:
10012 "invalid type argument of implicit conversion (have %qT)",
10016 gcc_unreachable ();
10020 /* *PTYPE is an incomplete array. Complete it with a domain based on
10021 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10022 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10023 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10026 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10028 tree maxindex, type, main_type, elt, unqual_elt;
10029 int failure = 0, quals;
10030 hashval_t hashcode = 0;
10031 bool overflow_p = false;
10033 maxindex = size_zero_node;
10036 if (TREE_CODE (initial_value) == STRING_CST)
10039 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10040 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10042 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10044 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10046 if (vec_safe_is_empty (v))
10050 maxindex = ssize_int (-1);
10055 unsigned HOST_WIDE_INT cnt;
10056 constructor_elt *ce;
10057 bool fold_p = false;
10060 maxindex = (*v)[0].index, fold_p = true;
10062 curindex = maxindex;
10064 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10066 bool curfold_p = false;
10068 curindex = ce->index, curfold_p = true;
10073 /* Since we treat size types now as ordinary
10074 unsigned types, we need an explicit overflow
10076 tree orig = curindex;
10077 curindex = fold_convert (sizetype, curindex);
10078 overflow_p |= tree_int_cst_lt (curindex, orig);
10080 curindex = size_binop (PLUS_EXPR, curindex,
10083 if (tree_int_cst_lt (maxindex, curindex))
10084 maxindex = curindex, fold_p = curfold_p;
10088 tree orig = maxindex;
10089 maxindex = fold_convert (sizetype, maxindex);
10090 overflow_p |= tree_int_cst_lt (maxindex, orig);
10096 /* Make an error message unless that happened already. */
10097 if (initial_value != error_mark_node)
10109 elt = TREE_TYPE (type);
10110 quals = TYPE_QUALS (strip_array_types (elt));
10114 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10116 /* Using build_distinct_type_copy and modifying things afterward instead
10117 of using build_array_type to create a new type preserves all of the
10118 TYPE_LANG_FLAG_? bits that the front end may have set. */
10119 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10120 TREE_TYPE (main_type) = unqual_elt;
10121 TYPE_DOMAIN (main_type)
10122 = build_range_type (TREE_TYPE (maxindex),
10123 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10124 layout_type (main_type);
10126 /* Make sure we have the canonical MAIN_TYPE. */
10127 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10128 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10130 main_type = type_hash_canon (hashcode, main_type);
10132 /* Fix the canonical type. */
10133 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10134 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10135 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10136 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10137 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10138 != TYPE_DOMAIN (main_type)))
10139 TYPE_CANONICAL (main_type)
10140 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10141 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10143 TYPE_CANONICAL (main_type) = main_type;
10148 type = c_build_qualified_type (main_type, quals);
10150 if (COMPLETE_TYPE_P (type)
10151 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10152 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10154 error ("size of array is too large");
10155 /* If we proceed with the array type as it is, we'll eventually
10156 crash in tree_to_[su]hwi(). */
10157 type = error_mark_node;
10164 /* Like c_mark_addressable but don't check register qualifier. */
10166 c_common_mark_addressable_vec (tree t)
10168 while (handled_component_p (t))
10169 t = TREE_OPERAND (t, 0);
10170 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10172 TREE_ADDRESSABLE (t) = 1;
10177 /* Used to help initialize the builtin-types.def table. When a type of
10178 the correct size doesn't exist, use error_mark_node instead of NULL.
10179 The later results in segfaults even when a decl using the type doesn't
10183 builtin_type_for_size (int size, bool unsignedp)
10185 tree type = c_common_type_for_size (size, unsignedp);
10186 return type ? type : error_mark_node;
10189 /* A helper function for resolve_overloaded_builtin in resolving the
10190 overloaded __sync_ builtins. Returns a positive power of 2 if the
10191 first operand of PARAMS is a pointer to a supported data type.
10192 Returns 0 if an error is encountered. */
10195 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10202 error ("too few arguments to function %qE", function);
10206 type = TREE_TYPE ((*params)[0]);
10207 if (TREE_CODE (type) != POINTER_TYPE)
10210 type = TREE_TYPE (type);
10211 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10214 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10215 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10219 error ("incompatible type for argument %d of %qE", 1, function);
10223 /* A helper function for resolve_overloaded_builtin. Adds casts to
10224 PARAMS to make arguments match up with those of FUNCTION. Drops
10225 the variadic arguments at the end. Returns false if some error
10226 was encountered; true on success. */
10229 sync_resolve_params (location_t loc, tree orig_function, tree function,
10230 vec<tree, va_gc> *params, bool orig_format)
10232 function_args_iterator iter;
10234 unsigned int parmnum;
10236 function_args_iter_init (&iter, TREE_TYPE (function));
10237 /* We've declared the implementation functions to use "volatile void *"
10238 as the pointer parameter, so we shouldn't get any complaints from the
10239 call to check_function_arguments what ever type the user used. */
10240 function_args_iter_next (&iter);
10241 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10242 ptype = TYPE_MAIN_VARIANT (ptype);
10244 /* For the rest of the values, we need to cast these to FTYPE, so that we
10245 don't get warnings for passing pointer types, etc. */
10249 tree val, arg_type;
10251 arg_type = function_args_iter_cond (&iter);
10252 /* XXX void_type_node belies the abstraction. */
10253 if (arg_type == void_type_node)
10257 if (params->length () <= parmnum)
10259 error_at (loc, "too few arguments to function %qE", orig_function);
10263 /* Only convert parameters if arg_type is unsigned integer type with
10264 new format sync routines, i.e. don't attempt to convert pointer
10265 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10266 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10268 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10270 /* Ideally for the first conversion we'd use convert_for_assignment
10271 so that we get warnings for anything that doesn't match the pointer
10272 type. This isn't portable across the C and C++ front ends atm. */
10273 val = (*params)[parmnum];
10274 val = convert (ptype, val);
10275 val = convert (arg_type, val);
10276 (*params)[parmnum] = val;
10279 function_args_iter_next (&iter);
10282 /* __atomic routines are not variadic. */
10283 if (!orig_format && params->length () != parmnum + 1)
10285 error_at (loc, "too many arguments to function %qE", orig_function);
10289 /* The definition of these primitives is variadic, with the remaining
10290 being "an optional list of variables protected by the memory barrier".
10291 No clue what that's supposed to mean, precisely, but we consider all
10292 call-clobbered variables to be protected so we're safe. */
10293 params->truncate (parmnum + 1);
10298 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10299 RESULT to make it match the type of the first pointer argument in
10303 sync_resolve_return (tree first_param, tree result, bool orig_format)
10305 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10306 tree rtype = TREE_TYPE (result);
10307 ptype = TYPE_MAIN_VARIANT (ptype);
10309 /* New format doesn't require casting unless the types are the same size. */
10310 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10311 return convert (ptype, result);
10316 /* This function verifies the PARAMS to generic atomic FUNCTION.
10317 It returns the size if all the parameters are the same size, otherwise
10318 0 is returned if the parameters are invalid. */
10321 get_atomic_generic_size (location_t loc, tree function,
10322 vec<tree, va_gc> *params)
10324 unsigned int n_param;
10325 unsigned int n_model;
10330 /* Determine the parameter makeup. */
10331 switch (DECL_FUNCTION_CODE (function))
10333 case BUILT_IN_ATOMIC_EXCHANGE:
10337 case BUILT_IN_ATOMIC_LOAD:
10338 case BUILT_IN_ATOMIC_STORE:
10342 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10347 gcc_unreachable ();
10350 if (vec_safe_length (params) != n_param)
10352 error_at (loc, "incorrect number of arguments to function %qE", function);
10356 /* Get type of first parameter, and determine its size. */
10357 type_0 = TREE_TYPE ((*params)[0]);
10358 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10360 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10365 /* Types must be compile time constant sizes. */
10366 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
10369 "argument 1 of %qE must be a pointer to a constant size type",
10374 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
10376 /* Zero size objects are not allowed. */
10380 "argument 1 of %qE must be a pointer to a nonzero size object",
10385 /* Check each other parameter is a pointer and the same size. */
10386 for (x = 0; x < n_param - n_model; x++)
10389 tree type = TREE_TYPE ((*params)[x]);
10390 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
10391 if (n_param == 6 && x == 3)
10393 if (!POINTER_TYPE_P (type))
10395 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10399 size = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
10400 if (size != size_0)
10402 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10408 /* Check memory model parameters for validity. */
10409 for (x = n_param - n_model ; x < n_param; x++)
10411 tree p = (*params)[x];
10412 if (TREE_CODE (p) == INTEGER_CST)
10414 int i = tree_to_uhwi (p);
10415 if (i < 0 || (i & MEMMODEL_MASK) >= MEMMODEL_LAST)
10417 warning_at (loc, OPT_Winvalid_memory_model,
10418 "invalid memory model argument %d of %qE", x + 1,
10423 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10425 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10435 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10436 at the beginning of the parameter list PARAMS representing the size of the
10437 objects. This is to match the library ABI requirement. LOC is the location
10438 of the function call.
10439 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10440 returned to allow the external call to be constructed. */
10443 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
10444 vec<tree, va_gc> *params)
10448 /* Insert a SIZE_T parameter as the first param. If there isn't
10449 enough space, allocate a new vector and recursively re-build with that. */
10450 if (!params->space (1))
10452 unsigned int z, len;
10453 vec<tree, va_gc> *v;
10456 len = params->length ();
10457 vec_alloc (v, len + 1);
10458 for (z = 0; z < len; z++)
10459 v->quick_push ((*params)[z]);
10460 f = build_function_call_vec (loc, vNULL, function, v, NULL);
10465 /* Add the size parameter and leave as a function call for processing. */
10466 size_node = build_int_cst (size_type_node, n);
10467 params->quick_insert (0, size_node);
10472 /* Return whether atomic operations for naturally aligned N-byte
10473 arguments are supported, whether inline or through libatomic. */
10475 atomic_size_supported_p (int n)
10486 return targetm.scalar_mode_supported_p (TImode);
10493 /* This will process an __atomic_exchange function call, determine whether it
10494 needs to be mapped to the _N variation, or turned into a library call.
10495 LOC is the location of the builtin call.
10496 FUNCTION is the DECL that has been invoked;
10497 PARAMS is the argument list for the call. The return value is non-null
10498 TRUE is returned if it is translated into the proper format for a call to the
10499 external library, and NEW_RETURN is set the tree for that function.
10500 FALSE is returned if processing for the _N variation is required, and
10501 NEW_RETURN is set to the the return value the result is copied into. */
10503 resolve_overloaded_atomic_exchange (location_t loc, tree function,
10504 vec<tree, va_gc> *params, tree *new_return)
10506 tree p0, p1, p2, p3;
10507 tree I_type, I_type_ptr;
10508 int n = get_atomic_generic_size (loc, function, params);
10510 /* Size of 0 is an error condition. */
10513 *new_return = error_mark_node;
10517 /* If not a lock-free size, change to the library generic format. */
10518 if (!atomic_size_supported_p (n))
10520 *new_return = add_atomic_size_parameter (n, loc, function, params);
10524 /* Otherwise there is a lockfree match, transform the call from:
10525 void fn(T* mem, T* desired, T* return, model)
10527 *return = (T) (fn (In* mem, (In) *desired, model)) */
10534 /* Create pointer to appropriate size. */
10535 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10536 I_type_ptr = build_pointer_type (I_type);
10538 /* Convert object pointer to required type. */
10539 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10541 /* Convert new value to required type, and dereference it. */
10542 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10543 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10546 /* Move memory model to the 3rd position, and end param list. */
10548 params->truncate (3);
10550 /* Convert return pointer and dereference it for later assignment. */
10551 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10557 /* This will process an __atomic_compare_exchange function call, determine
10558 whether it needs to be mapped to the _N variation, or turned into a lib call.
10559 LOC is the location of the builtin call.
10560 FUNCTION is the DECL that has been invoked;
10561 PARAMS is the argument list for the call. The return value is non-null
10562 TRUE is returned if it is translated into the proper format for a call to the
10563 external library, and NEW_RETURN is set the tree for that function.
10564 FALSE is returned if processing for the _N variation is required. */
10567 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
10568 vec<tree, va_gc> *params,
10572 tree I_type, I_type_ptr;
10573 int n = get_atomic_generic_size (loc, function, params);
10575 /* Size of 0 is an error condition. */
10578 *new_return = error_mark_node;
10582 /* If not a lock-free size, change to the library generic format. */
10583 if (!atomic_size_supported_p (n))
10585 /* The library generic format does not have the weak parameter, so
10586 remove it from the param list. Since a parameter has been removed,
10587 we can be sure that there is room for the SIZE_T parameter, meaning
10588 there will not be a recursive rebuilding of the parameter list, so
10589 there is no danger this will be done twice. */
10592 (*params)[3] = (*params)[4];
10593 (*params)[4] = (*params)[5];
10594 params->truncate (5);
10596 *new_return = add_atomic_size_parameter (n, loc, function, params);
10600 /* Otherwise, there is a match, so the call needs to be transformed from:
10601 bool fn(T* mem, T* desired, T* return, weak, success, failure)
10603 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
10609 /* Create pointer to appropriate size. */
10610 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10611 I_type_ptr = build_pointer_type (I_type);
10613 /* Convert object pointer to required type. */
10614 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10617 /* Convert expected pointer to required type. */
10618 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
10621 /* Convert desired value to required type, and dereference it. */
10622 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
10623 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
10626 /* The rest of the parameters are fine. NULL means no special return value
10628 *new_return = NULL;
10633 /* This will process an __atomic_load function call, determine whether it
10634 needs to be mapped to the _N variation, or turned into a library call.
10635 LOC is the location of the builtin call.
10636 FUNCTION is the DECL that has been invoked;
10637 PARAMS is the argument list for the call. The return value is non-null
10638 TRUE is returned if it is translated into the proper format for a call to the
10639 external library, and NEW_RETURN is set the tree for that function.
10640 FALSE is returned if processing for the _N variation is required, and
10641 NEW_RETURN is set to the the return value the result is copied into. */
10644 resolve_overloaded_atomic_load (location_t loc, tree function,
10645 vec<tree, va_gc> *params, tree *new_return)
10648 tree I_type, I_type_ptr;
10649 int n = get_atomic_generic_size (loc, function, params);
10651 /* Size of 0 is an error condition. */
10654 *new_return = error_mark_node;
10658 /* If not a lock-free size, change to the library generic format. */
10659 if (!atomic_size_supported_p (n))
10661 *new_return = add_atomic_size_parameter (n, loc, function, params);
10665 /* Otherwise, there is a match, so the call needs to be transformed from:
10666 void fn(T* mem, T* return, model)
10668 *return = (T) (fn ((In *) mem, model)) */
10674 /* Create pointer to appropriate size. */
10675 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10676 I_type_ptr = build_pointer_type (I_type);
10678 /* Convert object pointer to required type. */
10679 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10682 /* Move memory model to the 2nd position, and end param list. */
10684 params->truncate (2);
10686 /* Convert return pointer and dereference it for later assignment. */
10687 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10693 /* This will process an __atomic_store function call, determine whether it
10694 needs to be mapped to the _N variation, or turned into a library call.
10695 LOC is the location of the builtin call.
10696 FUNCTION is the DECL that has been invoked;
10697 PARAMS is the argument list for the call. The return value is non-null
10698 TRUE is returned if it is translated into the proper format for a call to the
10699 external library, and NEW_RETURN is set the tree for that function.
10700 FALSE is returned if processing for the _N variation is required, and
10701 NEW_RETURN is set to the the return value the result is copied into. */
10704 resolve_overloaded_atomic_store (location_t loc, tree function,
10705 vec<tree, va_gc> *params, tree *new_return)
10708 tree I_type, I_type_ptr;
10709 int n = get_atomic_generic_size (loc, function, params);
10711 /* Size of 0 is an error condition. */
10714 *new_return = error_mark_node;
10718 /* If not a lock-free size, change to the library generic format. */
10719 if (!atomic_size_supported_p (n))
10721 *new_return = add_atomic_size_parameter (n, loc, function, params);
10725 /* Otherwise, there is a match, so the call needs to be transformed from:
10726 void fn(T* mem, T* value, model)
10728 fn ((In *) mem, (In) *value, model) */
10733 /* Create pointer to appropriate size. */
10734 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
10735 I_type_ptr = build_pointer_type (I_type);
10737 /* Convert object pointer to required type. */
10738 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
10741 /* Convert new value to required type, and dereference it. */
10742 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
10743 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
10746 /* The memory model is in the right spot already. Return is void. */
10747 *new_return = NULL_TREE;
10753 /* Some builtin functions are placeholders for other expressions. This
10754 function should be called immediately after parsing the call expression
10755 before surrounding code has committed to the type of the expression.
10757 LOC is the location of the builtin call.
10759 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
10760 PARAMS is the argument list for the call. The return value is non-null
10761 when expansion is complete, and null if normal processing should
10765 resolve_overloaded_builtin (location_t loc, tree function,
10766 vec<tree, va_gc> *params)
10768 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
10769 bool orig_format = true;
10770 tree new_return = NULL_TREE;
10772 switch (DECL_BUILT_IN_CLASS (function))
10774 case BUILT_IN_NORMAL:
10777 if (targetm.resolve_overloaded_builtin)
10778 return targetm.resolve_overloaded_builtin (loc, function, params);
10785 /* Handle BUILT_IN_NORMAL here. */
10788 case BUILT_IN_ATOMIC_EXCHANGE:
10789 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10790 case BUILT_IN_ATOMIC_LOAD:
10791 case BUILT_IN_ATOMIC_STORE:
10793 /* Handle these 4 together so that they can fall through to the next
10794 case if the call is transformed to an _N variant. */
10797 case BUILT_IN_ATOMIC_EXCHANGE:
10799 if (resolve_overloaded_atomic_exchange (loc, function, params,
10802 /* Change to the _N variant. */
10803 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
10807 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10809 if (resolve_overloaded_atomic_compare_exchange (loc, function,
10813 /* Change to the _N variant. */
10814 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
10817 case BUILT_IN_ATOMIC_LOAD:
10819 if (resolve_overloaded_atomic_load (loc, function, params,
10822 /* Change to the _N variant. */
10823 orig_code = BUILT_IN_ATOMIC_LOAD_N;
10826 case BUILT_IN_ATOMIC_STORE:
10828 if (resolve_overloaded_atomic_store (loc, function, params,
10831 /* Change to the _N variant. */
10832 orig_code = BUILT_IN_ATOMIC_STORE_N;
10836 gcc_unreachable ();
10838 /* Fallthrough to the normal processing. */
10840 case BUILT_IN_ATOMIC_EXCHANGE_N:
10841 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
10842 case BUILT_IN_ATOMIC_LOAD_N:
10843 case BUILT_IN_ATOMIC_STORE_N:
10844 case BUILT_IN_ATOMIC_ADD_FETCH_N:
10845 case BUILT_IN_ATOMIC_SUB_FETCH_N:
10846 case BUILT_IN_ATOMIC_AND_FETCH_N:
10847 case BUILT_IN_ATOMIC_NAND_FETCH_N:
10848 case BUILT_IN_ATOMIC_XOR_FETCH_N:
10849 case BUILT_IN_ATOMIC_OR_FETCH_N:
10850 case BUILT_IN_ATOMIC_FETCH_ADD_N:
10851 case BUILT_IN_ATOMIC_FETCH_SUB_N:
10852 case BUILT_IN_ATOMIC_FETCH_AND_N:
10853 case BUILT_IN_ATOMIC_FETCH_NAND_N:
10854 case BUILT_IN_ATOMIC_FETCH_XOR_N:
10855 case BUILT_IN_ATOMIC_FETCH_OR_N:
10857 orig_format = false;
10858 /* Fallthru for parameter processing. */
10860 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
10861 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
10862 case BUILT_IN_SYNC_FETCH_AND_OR_N:
10863 case BUILT_IN_SYNC_FETCH_AND_AND_N:
10864 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
10865 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
10866 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
10867 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
10868 case BUILT_IN_SYNC_OR_AND_FETCH_N:
10869 case BUILT_IN_SYNC_AND_AND_FETCH_N:
10870 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
10871 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
10872 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
10873 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
10874 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
10875 case BUILT_IN_SYNC_LOCK_RELEASE_N:
10877 int n = sync_resolve_size (function, params);
10878 tree new_function, first_param, result;
10879 enum built_in_function fncode;
10882 return error_mark_node;
10884 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
10885 new_function = builtin_decl_explicit (fncode);
10886 if (!sync_resolve_params (loc, function, new_function, params,
10888 return error_mark_node;
10890 first_param = (*params)[0];
10891 result = build_function_call_vec (loc, vNULL, new_function, params,
10893 if (result == error_mark_node)
10895 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
10896 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
10897 && orig_code != BUILT_IN_ATOMIC_STORE_N)
10898 result = sync_resolve_return (first_param, result, orig_format);
10900 /* If new_return is set, assign function to that expr and cast the
10901 result to void since the generic interface returned void. */
10904 /* Cast function result from I{1,2,4,8,16} to the required type. */
10905 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
10906 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
10908 TREE_SIDE_EFFECTS (result) = 1;
10909 protected_set_expr_location (result, loc);
10910 result = convert (void_type_node, result);
10920 /* vector_types_compatible_elements_p is used in type checks of vectors
10921 values used as operands of binary operators. Where it returns true, and
10922 the other checks of the caller succeed (being vector types in he first
10923 place, and matching number of elements), we can just treat the types
10924 as essentially the same.
10925 Contrast with vector_targets_convertible_p, which is used for vector
10926 pointer types, and vector_types_convertible_p, which will allow
10927 language-specific matches under the control of flag_lax_vector_conversions,
10928 and might still require a conversion. */
10929 /* True if vector types T1 and T2 can be inputs to the same binary
10930 operator without conversion.
10931 We don't check the overall vector size here because some of our callers
10932 want to give different error messages when the vectors are compatible
10933 except for the element count. */
10936 vector_types_compatible_elements_p (tree t1, tree t2)
10938 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
10939 t1 = TREE_TYPE (t1);
10940 t2 = TREE_TYPE (t2);
10942 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
10944 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
10945 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
10946 || c2 == FIXED_POINT_TYPE));
10948 t1 = c_common_signed_type (t1);
10949 t2 = c_common_signed_type (t2);
10950 /* Equality works here because c_common_signed_type uses
10951 TYPE_MAIN_VARIANT. */
10954 if (opaque && c1 == c2
10955 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
10956 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
10961 /* Check for missing format attributes on function pointers. LTYPE is
10962 the new type or left-hand side type. RTYPE is the old type or
10963 right-hand side type. Returns TRUE if LTYPE is missing the desired
10967 check_missing_format_attribute (tree ltype, tree rtype)
10969 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
10972 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
10973 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
10978 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
10979 if (is_attribute_p ("format", TREE_PURPOSE (la)))
10987 /* Subscripting with type char is likely to lose on a machine where
10988 chars are signed. So warn on any machine, but optionally. Don't
10989 warn for unsigned char since that type is safe. Don't warn for
10990 signed char because anyone who uses that must have done so
10991 deliberately. Furthermore, we reduce the false positive load by
10992 warning only for non-constant value of type char. */
10995 warn_array_subscript_with_type_char (tree index)
10997 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
10998 && TREE_CODE (index) != INTEGER_CST)
10999 warning (OPT_Wchar_subscripts, "array subscript has type %<char%>");
11002 /* Implement -Wparentheses for the unexpected C precedence rules, to
11003 cover cases like x + y << z which readers are likely to
11004 misinterpret. We have seen an expression in which CODE is a binary
11005 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11006 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11007 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11008 expression was not formed using a binary or unary operator, or it
11009 was enclosed in parentheses. */
11012 warn_about_parentheses (location_t loc, enum tree_code code,
11013 enum tree_code code_left, tree arg_left,
11014 enum tree_code code_right, tree arg_right)
11016 if (!warn_parentheses)
11019 /* This macro tests that the expression ARG with original tree code
11020 CODE appears to be a boolean expression. or the result of folding a
11021 boolean expression. */
11022 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11023 (truth_value_p (TREE_CODE (ARG)) \
11024 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11025 /* Folding may create 0 or 1 integers from other expressions. */ \
11026 || ((CODE) != INTEGER_CST \
11027 && (integer_onep (ARG) || integer_zerop (ARG))))
11032 if (code_left == PLUS_EXPR)
11033 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11034 "suggest parentheses around %<+%> inside %<<<%>");
11035 else if (code_right == PLUS_EXPR)
11036 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11037 "suggest parentheses around %<+%> inside %<<<%>");
11038 else if (code_left == MINUS_EXPR)
11039 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11040 "suggest parentheses around %<-%> inside %<<<%>");
11041 else if (code_right == MINUS_EXPR)
11042 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11043 "suggest parentheses around %<-%> inside %<<<%>");
11047 if (code_left == PLUS_EXPR)
11048 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11049 "suggest parentheses around %<+%> inside %<>>%>");
11050 else if (code_right == PLUS_EXPR)
11051 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11052 "suggest parentheses around %<+%> inside %<>>%>");
11053 else if (code_left == MINUS_EXPR)
11054 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11055 "suggest parentheses around %<-%> inside %<>>%>");
11056 else if (code_right == MINUS_EXPR)
11057 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11058 "suggest parentheses around %<-%> inside %<>>%>");
11061 case TRUTH_ORIF_EXPR:
11062 if (code_left == TRUTH_ANDIF_EXPR)
11063 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11064 "suggest parentheses around %<&&%> within %<||%>");
11065 else if (code_right == TRUTH_ANDIF_EXPR)
11066 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11067 "suggest parentheses around %<&&%> within %<||%>");
11071 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11072 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11073 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11074 "suggest parentheses around arithmetic in operand of %<|%>");
11075 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11076 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11077 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11078 "suggest parentheses around arithmetic in operand of %<|%>");
11079 /* Check cases like x|y==z */
11080 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11081 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11082 "suggest parentheses around comparison in operand of %<|%>");
11083 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11084 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11085 "suggest parentheses around comparison in operand of %<|%>");
11086 /* Check cases like !x | y */
11087 else if (code_left == TRUTH_NOT_EXPR
11088 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11089 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11090 "suggest parentheses around operand of "
11091 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11095 if (code_left == BIT_AND_EXPR
11096 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11097 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11098 "suggest parentheses around arithmetic in operand of %<^%>");
11099 else if (code_right == BIT_AND_EXPR
11100 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11101 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11102 "suggest parentheses around arithmetic in operand of %<^%>");
11103 /* Check cases like x^y==z */
11104 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11105 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11106 "suggest parentheses around comparison in operand of %<^%>");
11107 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11108 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11109 "suggest parentheses around comparison in operand of %<^%>");
11113 if (code_left == PLUS_EXPR)
11114 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11115 "suggest parentheses around %<+%> in operand of %<&%>");
11116 else if (code_right == PLUS_EXPR)
11117 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11118 "suggest parentheses around %<+%> in operand of %<&%>");
11119 else if (code_left == MINUS_EXPR)
11120 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11121 "suggest parentheses around %<-%> in operand of %<&%>");
11122 else if (code_right == MINUS_EXPR)
11123 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11124 "suggest parentheses around %<-%> in operand of %<&%>");
11125 /* Check cases like x&y==z */
11126 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11127 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11128 "suggest parentheses around comparison in operand of %<&%>");
11129 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11130 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11131 "suggest parentheses around comparison in operand of %<&%>");
11132 /* Check cases like !x & y */
11133 else if (code_left == TRUTH_NOT_EXPR
11134 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11135 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11136 "suggest parentheses around operand of "
11137 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11141 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11142 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11143 "suggest parentheses around comparison in operand of %<==%>");
11144 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11145 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11146 "suggest parentheses around comparison in operand of %<==%>");
11149 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11150 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11151 "suggest parentheses around comparison in operand of %<!=%>");
11152 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11153 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11154 "suggest parentheses around comparison in operand of %<!=%>");
11158 if (TREE_CODE_CLASS (code) == tcc_comparison)
11160 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11161 && code_left != NE_EXPR && code_left != EQ_EXPR
11162 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11163 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11164 "comparisons like %<X<=Y<=Z%> do not "
11165 "have their mathematical meaning");
11166 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11167 && code_right != NE_EXPR && code_right != EQ_EXPR
11168 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11169 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11170 "comparisons like %<X<=Y<=Z%> do not "
11171 "have their mathematical meaning");
11175 #undef NOT_A_BOOLEAN_EXPR_P
11178 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11181 warn_for_unused_label (tree label)
11183 if (!TREE_USED (label))
11185 if (DECL_INITIAL (label))
11186 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11188 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11192 /* Warn for division by zero according to the value of DIVISOR. LOC
11193 is the location of the division operator. */
11196 warn_for_div_by_zero (location_t loc, tree divisor)
11198 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11199 about division by zero. Do not issue a warning if DIVISOR has a
11200 floating-point type, since we consider 0.0/0.0 a valid way of
11201 generating a NaN. */
11202 if (c_inhibit_evaluation_warnings == 0
11203 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11204 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11207 /* Subroutine of build_binary_op. Give warnings for comparisons
11208 between signed and unsigned quantities that may fail. Do the
11209 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11210 so that casts will be considered, but default promotions won't
11213 LOCATION is the location of the comparison operator.
11215 The arguments of this function map directly to local variables
11216 of build_binary_op. */
11219 warn_for_sign_compare (location_t location,
11220 tree orig_op0, tree orig_op1,
11221 tree op0, tree op1,
11222 tree result_type, enum tree_code resultcode)
11224 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11225 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11226 int unsignedp0, unsignedp1;
11228 /* In C++, check for comparison of different enum types. */
11229 if (c_dialect_cxx()
11230 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11231 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11232 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11233 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11235 warning_at (location,
11236 OPT_Wsign_compare, "comparison between types %qT and %qT",
11237 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11240 /* Do not warn if the comparison is being done in a signed type,
11241 since the signed type will only be chosen if it can represent
11242 all the values of the unsigned type. */
11243 if (!TYPE_UNSIGNED (result_type))
11245 /* Do not warn if both operands are unsigned. */
11246 else if (op0_signed == op1_signed)
11250 tree sop, uop, base_type;
11254 sop = orig_op0, uop = orig_op1;
11256 sop = orig_op1, uop = orig_op0;
11258 STRIP_TYPE_NOPS (sop);
11259 STRIP_TYPE_NOPS (uop);
11260 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11261 ? TREE_TYPE (result_type) : result_type);
11263 /* Do not warn if the signed quantity is an unsuffixed integer
11264 literal (or some static constant expression involving such
11265 literals or a conditional expression involving such literals)
11266 and it is non-negative. */
11267 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11269 /* Do not warn if the comparison is an equality operation, the
11270 unsigned quantity is an integral constant, and it would fit
11271 in the result if the result were signed. */
11272 else if (TREE_CODE (uop) == INTEGER_CST
11273 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11274 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11276 /* In C, do not warn if the unsigned quantity is an enumeration
11277 constant and its maximum value would fit in the result if the
11278 result were signed. */
11279 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11280 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11281 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11282 c_common_signed_type (base_type)))
11285 warning_at (location,
11287 "comparison between signed and unsigned integer expressions");
11290 /* Warn if two unsigned values are being compared in a size larger
11291 than their original size, and one (and only one) is the result of
11292 a `~' operator. This comparison will always fail.
11294 Also warn if one operand is a constant, and the constant does not
11295 have all bits set that are set in the ~ operand when it is
11298 op0 = c_common_get_narrower (op0, &unsignedp0);
11299 op1 = c_common_get_narrower (op1, &unsignedp1);
11301 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11302 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11304 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11305 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11306 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11307 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11309 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11312 HOST_WIDE_INT constant, mask;
11316 if (tree_fits_shwi_p (op0))
11319 unsignedp = unsignedp1;
11320 constant = tree_to_shwi (op0);
11325 unsignedp = unsignedp0;
11326 constant = tree_to_shwi (op1);
11329 bits = TYPE_PRECISION (TREE_TYPE (primop));
11330 if (bits < TYPE_PRECISION (result_type)
11331 && bits < HOST_BITS_PER_LONG && unsignedp)
11333 mask = (~ (HOST_WIDE_INT) 0) << bits;
11334 if ((mask & constant) != mask)
11337 warning_at (location, OPT_Wsign_compare,
11338 "promoted ~unsigned is always non-zero");
11340 warning_at (location, OPT_Wsign_compare,
11341 "comparison of promoted ~unsigned with constant");
11345 else if (unsignedp0 && unsignedp1
11346 && (TYPE_PRECISION (TREE_TYPE (op0))
11347 < TYPE_PRECISION (result_type))
11348 && (TYPE_PRECISION (TREE_TYPE (op1))
11349 < TYPE_PRECISION (result_type)))
11350 warning_at (location, OPT_Wsign_compare,
11351 "comparison of promoted ~unsigned with unsigned");
11355 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11356 type via c_common_type. If -Wdouble-promotion is in use, and the
11357 conditions for warning have been met, issue a warning. GMSGID is
11358 the warning message. It must have two %T specifiers for the type
11359 that was converted (generally "float") and the type to which it was
11360 converted (generally "double), respectively. LOC is the location
11361 to which the awrning should refer. */
11364 do_warn_double_promotion (tree result_type, tree type1, tree type2,
11365 const char *gmsgid, location_t loc)
11369 if (!warn_double_promotion)
11371 /* If the conversion will not occur at run-time, there is no need to
11373 if (c_inhibit_evaluation_warnings)
11375 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11376 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11378 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11379 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11380 source_type = type1;
11381 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11382 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11383 source_type = type2;
11386 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11389 /* Setup a TYPE_DECL node as a typedef representation.
11391 X is a TYPE_DECL for a typedef statement. Create a brand new
11392 ..._TYPE node (which will be just a variant of the existing
11393 ..._TYPE node with identical properties) and then install X
11394 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11396 The whole point here is to end up with a situation where each
11397 and every ..._TYPE node the compiler creates will be uniquely
11398 associated with AT MOST one node representing a typedef name.
11399 This way, even though the compiler substitutes corresponding
11400 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11401 early on, later parts of the compiler can always do the reverse
11402 translation and get back the corresponding typedef name. For
11405 typedef struct S MY_TYPE;
11408 Later parts of the compiler might only know that `object' was of
11409 type `struct S' if it were not for code just below. With this
11410 code however, later parts of the compiler see something like:
11412 struct S' == struct S
11413 typedef struct S' MY_TYPE;
11416 And they can then deduce (from the node for type struct S') that
11417 the original object declaration was:
11421 Being able to do this is important for proper support of protoize,
11422 and also for generating precise symbolic debugging information
11423 which takes full account of the programmer's (typedef) vocabulary.
11425 Obviously, we don't want to generate a duplicate ..._TYPE node if
11426 the TYPE_DECL node that we are now processing really represents a
11427 standard built-in type. */
11430 set_underlying_type (tree x)
11432 if (x == error_mark_node)
11434 if (DECL_IS_BUILTIN (x))
11436 if (TYPE_NAME (TREE_TYPE (x)) == 0)
11437 TYPE_NAME (TREE_TYPE (x)) = x;
11439 else if (TREE_TYPE (x) != error_mark_node
11440 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
11442 tree tt = TREE_TYPE (x);
11443 DECL_ORIGINAL_TYPE (x) = tt;
11444 tt = build_variant_type_copy (tt);
11445 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
11446 TYPE_NAME (tt) = x;
11447 TREE_USED (tt) = TREE_USED (x);
11448 TREE_TYPE (x) = tt;
11452 /* Record the types used by the current global variable declaration
11453 being parsed, so that we can decide later to emit their debug info.
11454 Those types are in types_used_by_cur_var_decl, and we are going to
11455 store them in the types_used_by_vars_hash hash table.
11456 DECL is the declaration of the global variable that has been parsed. */
11459 record_types_used_by_current_var_decl (tree decl)
11461 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
11463 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
11465 tree type = types_used_by_cur_var_decl->pop ();
11466 types_used_by_var_decl_insert (type, decl);
11470 /* If DECL is a typedef that is declared in the current function,
11471 record it for the purpose of -Wunused-local-typedefs. */
11474 record_locally_defined_typedef (tree decl)
11476 struct c_language_function *l;
11478 if (!warn_unused_local_typedefs
11480 /* if this is not a locally defined typedef then we are not
11482 || !is_typedef_decl (decl)
11483 || !decl_function_context (decl))
11486 l = (struct c_language_function *) cfun->language;
11487 vec_safe_push (l->local_typedefs, decl);
11490 /* If T is a TYPE_DECL declared locally, mark it as used. */
11493 maybe_record_typedef_use (tree t)
11495 if (!is_typedef_decl (t))
11498 TREE_USED (t) = true;
11501 /* Warn if there are some unused locally defined typedefs in the
11502 current function. */
11505 maybe_warn_unused_local_typedefs (void)
11509 /* The number of times we have emitted -Wunused-local-typedefs
11510 warnings. If this is different from errorcount, that means some
11511 unrelated errors have been issued. In which case, we'll avoid
11512 emitting "unused-local-typedefs" warnings. */
11513 static int unused_local_typedefs_warn_count;
11514 struct c_language_function *l;
11519 if ((l = (struct c_language_function *) cfun->language) == NULL)
11522 if (warn_unused_local_typedefs
11523 && errorcount == unused_local_typedefs_warn_count)
11525 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
11526 if (!TREE_USED (decl))
11527 warning_at (DECL_SOURCE_LOCATION (decl),
11528 OPT_Wunused_local_typedefs,
11529 "typedef %qD locally defined but not used", decl);
11530 unused_local_typedefs_warn_count = errorcount;
11533 vec_free (l->local_typedefs);
11536 /* The C and C++ parsers both use vectors to hold function arguments.
11537 For efficiency, we keep a cache of unused vectors. This is the
11540 typedef vec<tree, va_gc> *tree_gc_vec;
11541 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
11543 /* Return a new vector from the cache. If the cache is empty,
11544 allocate a new vector. These vectors are GC'ed, so it is OK if the
11545 pointer is not released.. */
11548 make_tree_vector (void)
11550 if (tree_vector_cache && !tree_vector_cache->is_empty ())
11551 return tree_vector_cache->pop ();
11554 /* Passing 0 to vec::alloc returns NULL, and our callers require
11555 that we always return a non-NULL value. The vector code uses
11556 4 when growing a NULL vector, so we do too. */
11557 vec<tree, va_gc> *v;
11563 /* Release a vector of trees back to the cache. */
11566 release_tree_vector (vec<tree, va_gc> *vec)
11571 vec_safe_push (tree_vector_cache, vec);
11575 /* Get a new tree vector holding a single tree. */
11578 make_tree_vector_single (tree t)
11580 vec<tree, va_gc> *ret = make_tree_vector ();
11581 ret->quick_push (t);
11585 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
11588 make_tree_vector_from_list (tree list)
11590 vec<tree, va_gc> *ret = make_tree_vector ();
11591 for (; list; list = TREE_CHAIN (list))
11592 vec_safe_push (ret, TREE_VALUE (list));
11596 /* Get a new tree vector which is a copy of an existing one. */
11599 make_tree_vector_copy (const vec<tree, va_gc> *orig)
11601 vec<tree, va_gc> *ret;
11605 ret = make_tree_vector ();
11606 vec_safe_reserve (ret, vec_safe_length (orig));
11607 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
11608 ret->quick_push (t);
11612 /* Return true if KEYWORD starts a type specifier. */
11615 keyword_begins_type_specifier (enum rid keyword)
11619 case RID_AUTO_TYPE:
11632 case RID_DFLOAT128:
11652 /* Return true if KEYWORD names a type qualifier. */
11655 keyword_is_type_qualifier (enum rid keyword)
11669 /* Return true if KEYWORD names a storage class specifier.
11671 RID_TYPEDEF is not included in this list despite `typedef' being
11672 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
11673 such for syntactic convenience only. */
11676 keyword_is_storage_class_specifier (enum rid keyword)
11692 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
11695 keyword_is_function_specifier (enum rid keyword)
11709 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
11710 declaration-specifier (C99 6.7). */
11713 keyword_is_decl_specifier (enum rid keyword)
11715 if (keyword_is_storage_class_specifier (keyword)
11716 || keyword_is_type_qualifier (keyword)
11717 || keyword_is_function_specifier (keyword))
11724 case RID_CONSTEXPR:
11731 /* Initialize language-specific-bits of tree_contains_struct. */
11734 c_common_init_ts (void)
11736 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
11737 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
11738 MARK_TS_TYPED (ARRAY_NOTATION_REF);
11741 /* Build a user-defined numeric literal out of an integer constant type VALUE
11742 with identifier SUFFIX. */
11745 build_userdef_literal (tree suffix_id, tree value,
11746 enum overflow_type overflow, tree num_string)
11748 tree literal = make_node (USERDEF_LITERAL);
11749 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
11750 USERDEF_LITERAL_VALUE (literal) = value;
11751 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
11752 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
11756 /* For vector[index], convert the vector to a
11757 pointer of the underlying type. */
11759 convert_vector_to_pointer_for_subscript (location_t loc,
11760 tree* vecp, tree index)
11762 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
11764 tree type = TREE_TYPE (*vecp);
11767 if (TREE_CODE (index) == INTEGER_CST)
11768 if (!tree_fits_uhwi_p (index)
11769 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
11770 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
11772 c_common_mark_addressable_vec (*vecp);
11773 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
11774 type = build_pointer_type (type);
11775 type1 = build_pointer_type (TREE_TYPE (*vecp));
11776 *vecp = build1 (ADDR_EXPR, type1, *vecp);
11777 *vecp = convert (type, *vecp);
11781 /* Determine which of the operands, if any, is a scalar that needs to be
11782 converted to a vector, for the range of operations. */
11784 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
11787 tree type0 = TREE_TYPE (op0);
11788 tree type1 = TREE_TYPE (op1);
11789 bool integer_only_op = false;
11790 enum stv_conv ret = stv_firstarg;
11792 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
11793 || TREE_CODE (type1) == VECTOR_TYPE);
11796 /* Most GENERIC binary expressions require homogeneous arguments.
11797 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
11798 argument that is a vector and a second one that is a scalar, so
11799 we never return stv_secondarg for them. */
11802 if (TREE_CODE (type0) == INTEGER_TYPE
11803 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11805 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11808 error_at (loc, "conversion of scalar %qT to vector %qT "
11809 "involves truncation", type0, type1);
11813 return stv_firstarg;
11820 integer_only_op = true;
11821 /* ... fall through ... */
11823 case VEC_COND_EXPR:
11828 case TRUNC_DIV_EXPR:
11829 case CEIL_DIV_EXPR:
11830 case FLOOR_DIV_EXPR:
11831 case ROUND_DIV_EXPR:
11832 case EXACT_DIV_EXPR:
11833 case TRUNC_MOD_EXPR:
11834 case FLOOR_MOD_EXPR:
11842 /* What about UNLT_EXPR? */
11843 if (TREE_CODE (type0) == VECTOR_TYPE)
11846 ret = stv_secondarg;
11847 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
11848 tmp = type0; type0 = type1; type1 = tmp;
11849 tmp = op0; op0 = op1; op1 = tmp;
11852 if (TREE_CODE (type0) == INTEGER_TYPE
11853 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11855 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11858 error_at (loc, "conversion of scalar %qT to vector %qT "
11859 "involves truncation", type0, type1);
11864 else if (!integer_only_op
11865 /* Allow integer --> real conversion if safe. */
11866 && (TREE_CODE (type0) == REAL_TYPE
11867 || TREE_CODE (type0) == INTEGER_TYPE)
11868 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
11870 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
11873 error_at (loc, "conversion of scalar %qT to vector %qT "
11874 "involves truncation", type0, type1);
11883 return stv_nothing;
11886 /* Return true iff ALIGN is an integral constant that is a fundamental
11887 alignment, as defined by [basic.align] in the c++-11
11892 [A fundamental alignment is represented by an alignment less than or
11893 equal to the greatest alignment supported by the implementation
11894 in all contexts, which is equal to
11895 alignof(max_align_t)]. */
11898 cxx_fundamental_alignment_p (unsigned align)
11900 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
11901 TYPE_ALIGN (long_double_type_node)));
11904 /* Return true if T is a pointer to a zero-sized aggregate. */
11907 pointer_to_zero_sized_aggr_p (tree t)
11909 if (!POINTER_TYPE_P (t))
11912 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
11915 #include "gt-c-family-c-common.h"