* c-pragma.c: Use VAR_OR_FUNCTION_DECL_P throughout.
[official-gcc.git] / gcc / c-family / c-common.c
bloba2b37931911c0516f9be3cf03a6c269dcbb07fcc
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #define GCC_C_COMMON_C
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "input.h"
26 #include "c-common.h"
27 #include "tm.h"
28 #include "intl.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "stor-layout.h"
32 #include "calls.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "varasm.h"
36 #include "trans-mem.h"
37 #include "flags.h"
38 #include "c-pragma.h"
39 #include "c-objc.h"
40 #include "tm_p.h"
41 #include "obstack.h"
42 #include "cpplib.h"
43 #include "target.h"
44 #include "common/common-target.h"
45 #include "langhooks.h"
46 #include "tree-inline.h"
47 #include "toplev.h"
48 #include "diagnostic.h"
49 #include "tree-iterator.h"
50 #include "hashtab.h"
51 #include "opts.h"
52 #include "hash-map.h"
53 #include "is-a.h"
54 #include "plugin-api.h"
55 #include "vec.h"
56 #include "hash-set.h"
57 #include "machmode.h"
58 #include "hard-reg-set.h"
59 #include "input.h"
60 #include "function.h"
61 #include "ipa-ref.h"
62 #include "cgraph.h"
63 #include "target-def.h"
64 #include "gimplify.h"
65 #include "wide-int-print.h"
66 #include "gimple-expr.h"
68 cpp_reader *parse_in; /* Declared in c-pragma.h. */
70 /* Mode used to build pointers (VOIDmode means ptr_mode). */
72 machine_mode c_default_pointer_mode = VOIDmode;
74 /* The following symbols are subsumed in the c_global_trees array, and
75 listed here individually for documentation purposes.
77 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
79 tree short_integer_type_node;
80 tree long_integer_type_node;
81 tree long_long_integer_type_node;
83 tree short_unsigned_type_node;
84 tree long_unsigned_type_node;
85 tree long_long_unsigned_type_node;
87 tree truthvalue_type_node;
88 tree truthvalue_false_node;
89 tree truthvalue_true_node;
91 tree ptrdiff_type_node;
93 tree unsigned_char_type_node;
94 tree signed_char_type_node;
95 tree wchar_type_node;
97 tree char16_type_node;
98 tree char32_type_node;
100 tree float_type_node;
101 tree double_type_node;
102 tree long_double_type_node;
104 tree complex_integer_type_node;
105 tree complex_float_type_node;
106 tree complex_double_type_node;
107 tree complex_long_double_type_node;
109 tree dfloat32_type_node;
110 tree dfloat64_type_node;
111 tree_dfloat128_type_node;
113 tree intQI_type_node;
114 tree intHI_type_node;
115 tree intSI_type_node;
116 tree intDI_type_node;
117 tree intTI_type_node;
119 tree unsigned_intQI_type_node;
120 tree unsigned_intHI_type_node;
121 tree unsigned_intSI_type_node;
122 tree unsigned_intDI_type_node;
123 tree unsigned_intTI_type_node;
125 tree widest_integer_literal_type_node;
126 tree widest_unsigned_literal_type_node;
128 Nodes for types `void *' and `const void *'.
130 tree ptr_type_node, const_ptr_type_node;
132 Nodes for types `char *' and `const char *'.
134 tree string_type_node, const_string_type_node;
136 Type `char[SOMENUMBER]'.
137 Used when an array of char is needed and the size is irrelevant.
139 tree char_array_type_node;
141 Type `wchar_t[SOMENUMBER]' or something like it.
142 Used when a wide string literal is created.
144 tree wchar_array_type_node;
146 Type `char16_t[SOMENUMBER]' or something like it.
147 Used when a UTF-16 string literal is created.
149 tree char16_array_type_node;
151 Type `char32_t[SOMENUMBER]' or something like it.
152 Used when a UTF-32 string literal is created.
154 tree char32_array_type_node;
156 Type `int ()' -- used for implicit declaration of functions.
158 tree default_function_type;
160 A VOID_TYPE node, packaged in a TREE_LIST.
162 tree void_list_node;
164 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
165 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
166 VAR_DECLS, but C++ does.)
168 tree function_name_decl_node;
169 tree pretty_function_name_decl_node;
170 tree c99_function_name_decl_node;
172 Stack of nested function name VAR_DECLs.
174 tree saved_function_name_decls;
178 tree c_global_trees[CTI_MAX];
180 /* Switches common to the C front ends. */
182 /* Nonzero means don't output line number information. */
184 char flag_no_line_commands;
186 /* Nonzero causes -E output not to be done, but directives such as
187 #define that have side effects are still obeyed. */
189 char flag_no_output;
191 /* Nonzero means dump macros in some fashion. */
193 char flag_dump_macros;
195 /* Nonzero means pass #include lines through to the output. */
197 char flag_dump_includes;
199 /* Nonzero means process PCH files while preprocessing. */
201 bool flag_pch_preprocess;
203 /* The file name to which we should write a precompiled header, or
204 NULL if no header will be written in this compile. */
206 const char *pch_file;
208 /* Nonzero if an ISO standard was selected. It rejects macros in the
209 user's namespace. */
210 int flag_iso;
212 /* C/ObjC language option variables. */
215 /* Nonzero means allow type mismatches in conditional expressions;
216 just make their values `void'. */
218 int flag_cond_mismatch;
220 /* Nonzero means enable C89 Amendment 1 features. */
222 int flag_isoc94;
224 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
226 int flag_isoc99;
228 /* Nonzero means use the ISO C11 dialect of C. */
230 int flag_isoc11;
232 /* Nonzero means that we have builtin functions, and main is an int. */
234 int flag_hosted = 1;
237 /* ObjC language option variables. */
240 /* Tells the compiler that this is a special run. Do not perform any
241 compiling, instead we are to test some platform dependent features
242 and output a C header file with appropriate definitions. */
244 int print_struct_values;
246 /* Tells the compiler what is the constant string class for ObjC. */
248 const char *constant_string_class_name;
251 /* C++ language option variables. */
254 /* Nonzero means generate separate instantiation control files and
255 juggle them at link time. */
257 int flag_use_repository;
259 /* The C++ dialect being used. Default set in c_common_post_options. */
261 enum cxx_dialect cxx_dialect = cxx_unset;
263 /* Maximum template instantiation depth. This limit exists to limit the
264 time it takes to notice excessively recursive template instantiations.
266 The default is lower than the 1024 recommended by the C++0x standard
267 because G++ runs out of stack before 1024 with highly recursive template
268 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
270 int max_tinst_depth = 900;
272 /* The elements of `ridpointers' are identifier nodes for the reserved
273 type names and storage classes. It is indexed by a RID_... value. */
274 tree *ridpointers;
276 tree (*make_fname_decl) (location_t, tree, int);
278 /* Nonzero means don't warn about problems that occur when the code is
279 executed. */
280 int c_inhibit_evaluation_warnings;
282 /* Whether we are building a boolean conversion inside
283 convert_for_assignment, or some other late binary operation. If
284 build_binary_op is called for C (from code shared by C and C++) in
285 this case, then the operands have already been folded and the
286 result will not be folded again, so C_MAYBE_CONST_EXPR should not
287 be generated. */
288 bool in_late_binary_op;
290 /* Whether lexing has been completed, so subsequent preprocessor
291 errors should use the compiler's input_location. */
292 bool done_lexing = false;
294 /* Information about how a function name is generated. */
295 struct fname_var_t
297 tree *const decl; /* pointer to the VAR_DECL. */
298 const unsigned rid; /* RID number for the identifier. */
299 const int pretty; /* How pretty is it? */
302 /* The three ways of getting then name of the current function. */
304 const struct fname_var_t fname_vars[] =
306 /* C99 compliant __func__, must be first. */
307 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
308 /* GCC __FUNCTION__ compliant. */
309 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
310 /* GCC __PRETTY_FUNCTION__ compliant. */
311 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
312 {NULL, 0, 0},
315 /* Global visibility options. */
316 struct visibility_flags visibility_options;
318 static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
319 static tree check_case_value (location_t, tree);
320 static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
322 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
329 int, bool *);
330 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
331 int, bool *);
332 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
333 bool *);
334 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
335 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
337 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
339 static tree handle_always_inline_attribute (tree *, tree, tree, int,
340 bool *);
341 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
348 bool *);
349 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
350 bool *);
351 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_transparent_union_attribute (tree *, tree, tree,
353 int, bool *);
354 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
355 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
356 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
357 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
358 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
359 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
360 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
361 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
364 static tree handle_visibility_attribute (tree *, tree, tree, int,
365 bool *);
366 static tree handle_tls_model_attribute (tree *, tree, tree, int,
367 bool *);
368 static tree handle_no_instrument_function_attribute (tree *, tree,
369 tree, int, bool *);
370 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
371 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
373 bool *);
374 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_deprecated_attribute (tree *, tree, tree, int,
379 bool *);
380 static tree handle_vector_size_attribute (tree *, tree, tree, int,
381 bool *);
382 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
386 bool *);
387 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
392 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
393 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
394 static tree ignore_attribute (tree *, tree, tree, int, bool *);
395 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
396 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
397 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
398 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
399 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
400 bool *);
401 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
402 bool *);
403 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
404 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
405 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
406 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
408 static void check_function_nonnull (tree, int, tree *);
409 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
410 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
411 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
412 static int resort_field_decl_cmp (const void *, const void *);
414 /* Reserved words. The third field is a mask: keywords are disabled
415 if they match the mask.
417 Masks for languages:
418 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
419 C --std=c99: D_CXXONLY | D_OBJC
420 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
421 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
422 C++ --std=c0x: D_CONLY | D_OBJC
423 ObjC++ is like C++ except that D_OBJC is not set
425 If -fno-asm is used, D_ASM is added to the mask. If
426 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
427 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
428 In C with -Wc++-compat, we warn if D_CXXWARN is set.
430 Note the complication of the D_CXX_OBJC keywords. These are
431 reserved words such as 'class'. In C++, 'class' is a reserved
432 word. In Objective-C++ it is too. In Objective-C, it is a
433 reserved word too, but only if it follows an '@' sign.
435 const struct c_common_resword c_common_reswords[] =
437 { "_Alignas", RID_ALIGNAS, D_CONLY },
438 { "_Alignof", RID_ALIGNOF, D_CONLY },
439 { "_Atomic", RID_ATOMIC, D_CONLY },
440 { "_Bool", RID_BOOL, D_CONLY },
441 { "_Complex", RID_COMPLEX, 0 },
442 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
443 { "_Cilk_sync", RID_CILK_SYNC, 0 },
444 { "_Cilk_for", RID_CILK_FOR, 0 },
445 { "_Imaginary", RID_IMAGINARY, D_CONLY },
446 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
447 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
448 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
449 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
450 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
451 { "_Sat", RID_SAT, D_CONLY | D_EXT },
452 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
453 { "_Noreturn", RID_NORETURN, D_CONLY },
454 { "_Generic", RID_GENERIC, D_CONLY },
455 { "_Thread_local", RID_THREAD, D_CONLY },
456 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
457 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
458 { "__alignof", RID_ALIGNOF, 0 },
459 { "__alignof__", RID_ALIGNOF, 0 },
460 { "__asm", RID_ASM, 0 },
461 { "__asm__", RID_ASM, 0 },
462 { "__attribute", RID_ATTRIBUTE, 0 },
463 { "__attribute__", RID_ATTRIBUTE, 0 },
464 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
465 { "__bases", RID_BASES, D_CXXONLY },
466 { "__builtin_call_with_static_chain",
467 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
468 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
469 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
470 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
471 { "__builtin_offsetof", RID_OFFSETOF, 0 },
472 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
473 { "__builtin_va_arg", RID_VA_ARG, 0 },
474 { "__complex", RID_COMPLEX, 0 },
475 { "__complex__", RID_COMPLEX, 0 },
476 { "__const", RID_CONST, 0 },
477 { "__const__", RID_CONST, 0 },
478 { "__decltype", RID_DECLTYPE, D_CXXONLY },
479 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
480 { "__extension__", RID_EXTENSION, 0 },
481 { "__func__", RID_C99_FUNCTION_NAME, 0 },
482 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
483 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
484 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
485 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
486 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
487 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
488 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
489 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
490 { "__imag", RID_IMAGPART, 0 },
491 { "__imag__", RID_IMAGPART, 0 },
492 { "__inline", RID_INLINE, 0 },
493 { "__inline__", RID_INLINE, 0 },
494 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
495 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
496 { "__is_class", RID_IS_CLASS, D_CXXONLY },
497 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
498 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
499 { "__is_final", RID_IS_FINAL, D_CXXONLY },
500 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
501 { "__is_pod", RID_IS_POD, D_CXXONLY },
502 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
503 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
504 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
505 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
506 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
507 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
508 { "__is_union", RID_IS_UNION, D_CXXONLY },
509 { "__label__", RID_LABEL, 0 },
510 { "__null", RID_NULL, 0 },
511 { "__real", RID_REALPART, 0 },
512 { "__real__", RID_REALPART, 0 },
513 { "__restrict", RID_RESTRICT, 0 },
514 { "__restrict__", RID_RESTRICT, 0 },
515 { "__signed", RID_SIGNED, 0 },
516 { "__signed__", RID_SIGNED, 0 },
517 { "__thread", RID_THREAD, 0 },
518 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
519 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
520 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
521 { "__typeof", RID_TYPEOF, 0 },
522 { "__typeof__", RID_TYPEOF, 0 },
523 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
524 { "__volatile", RID_VOLATILE, 0 },
525 { "__volatile__", RID_VOLATILE, 0 },
526 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
527 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
528 { "asm", RID_ASM, D_ASM },
529 { "auto", RID_AUTO, 0 },
530 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
531 { "break", RID_BREAK, 0 },
532 { "case", RID_CASE, 0 },
533 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
534 { "char", RID_CHAR, 0 },
535 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
536 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
537 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
538 { "const", RID_CONST, 0 },
539 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
540 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
541 { "continue", RID_CONTINUE, 0 },
542 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
543 { "default", RID_DEFAULT, 0 },
544 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
545 { "do", RID_DO, 0 },
546 { "double", RID_DOUBLE, 0 },
547 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
548 { "else", RID_ELSE, 0 },
549 { "enum", RID_ENUM, 0 },
550 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
551 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
552 { "extern", RID_EXTERN, 0 },
553 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
554 { "float", RID_FLOAT, 0 },
555 { "for", RID_FOR, 0 },
556 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
557 { "goto", RID_GOTO, 0 },
558 { "if", RID_IF, 0 },
559 { "inline", RID_INLINE, D_EXT89 },
560 { "int", RID_INT, 0 },
561 { "long", RID_LONG, 0 },
562 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
563 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
564 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
565 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
566 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
567 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
568 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
569 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
570 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
571 { "register", RID_REGISTER, 0 },
572 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
573 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
574 { "return", RID_RETURN, 0 },
575 { "short", RID_SHORT, 0 },
576 { "signed", RID_SIGNED, 0 },
577 { "sizeof", RID_SIZEOF, 0 },
578 { "static", RID_STATIC, 0 },
579 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
580 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
581 { "struct", RID_STRUCT, 0 },
582 { "switch", RID_SWITCH, 0 },
583 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
584 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
585 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
586 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
587 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
588 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
589 { "typedef", RID_TYPEDEF, 0 },
590 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
591 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
592 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
593 { "union", RID_UNION, 0 },
594 { "unsigned", RID_UNSIGNED, 0 },
595 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
596 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
597 { "void", RID_VOID, 0 },
598 { "volatile", RID_VOLATILE, 0 },
599 { "wchar_t", RID_WCHAR, D_CXXONLY },
600 { "while", RID_WHILE, 0 },
601 /* These Objective-C keywords are recognized only immediately after
602 an '@'. */
603 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
604 { "defs", RID_AT_DEFS, D_OBJC },
605 { "encode", RID_AT_ENCODE, D_OBJC },
606 { "end", RID_AT_END, D_OBJC },
607 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
608 { "interface", RID_AT_INTERFACE, D_OBJC },
609 { "protocol", RID_AT_PROTOCOL, D_OBJC },
610 { "selector", RID_AT_SELECTOR, D_OBJC },
611 { "finally", RID_AT_FINALLY, D_OBJC },
612 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
613 { "optional", RID_AT_OPTIONAL, D_OBJC },
614 { "required", RID_AT_REQUIRED, D_OBJC },
615 { "property", RID_AT_PROPERTY, D_OBJC },
616 { "package", RID_AT_PACKAGE, D_OBJC },
617 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
618 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
619 /* These are recognized only in protocol-qualifier context
620 (see above) */
621 { "bycopy", RID_BYCOPY, D_OBJC },
622 { "byref", RID_BYREF, D_OBJC },
623 { "in", RID_IN, D_OBJC },
624 { "inout", RID_INOUT, D_OBJC },
625 { "oneway", RID_ONEWAY, D_OBJC },
626 { "out", RID_OUT, D_OBJC },
627 /* These are recognized inside a property attribute list */
628 { "assign", RID_ASSIGN, D_OBJC },
629 { "copy", RID_COPY, D_OBJC },
630 { "getter", RID_GETTER, D_OBJC },
631 { "nonatomic", RID_NONATOMIC, D_OBJC },
632 { "readonly", RID_READONLY, D_OBJC },
633 { "readwrite", RID_READWRITE, D_OBJC },
634 { "retain", RID_RETAIN, D_OBJC },
635 { "setter", RID_SETTER, D_OBJC },
638 const unsigned int num_c_common_reswords =
639 sizeof c_common_reswords / sizeof (struct c_common_resword);
641 /* Table of machine-independent attributes common to all C-like languages.
643 All attributes referencing arguments should be additionally processed
644 in chkp_copy_function_type_adding_bounds for correct instrumentation
645 by Pointer Bounds Checker.
646 Current list of processed common attributes: nonnull. */
647 const struct attribute_spec c_common_attribute_table[] =
649 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
650 affects_type_identity } */
651 { "packed", 0, 0, false, false, false,
652 handle_packed_attribute , false},
653 { "nocommon", 0, 0, true, false, false,
654 handle_nocommon_attribute, false},
655 { "common", 0, 0, true, false, false,
656 handle_common_attribute, false },
657 /* FIXME: logically, noreturn attributes should be listed as
658 "false, true, true" and apply to function types. But implementing this
659 would require all the places in the compiler that use TREE_THIS_VOLATILE
660 on a decl to identify non-returning functions to be located and fixed
661 to check the function type instead. */
662 { "noreturn", 0, 0, true, false, false,
663 handle_noreturn_attribute, false },
664 { "volatile", 0, 0, true, false, false,
665 handle_noreturn_attribute, false },
666 { "stack_protect", 0, 0, true, false, false,
667 handle_stack_protect_attribute, false },
668 { "noinline", 0, 0, true, false, false,
669 handle_noinline_attribute, false },
670 { "noclone", 0, 0, true, false, false,
671 handle_noclone_attribute, false },
672 { "no_icf", 0, 0, true, false, false,
673 handle_noicf_attribute, false },
674 { "leaf", 0, 0, true, false, false,
675 handle_leaf_attribute, false },
676 { "always_inline", 0, 0, true, false, false,
677 handle_always_inline_attribute, false },
678 { "gnu_inline", 0, 0, true, false, false,
679 handle_gnu_inline_attribute, false },
680 { "artificial", 0, 0, true, false, false,
681 handle_artificial_attribute, false },
682 { "flatten", 0, 0, true, false, false,
683 handle_flatten_attribute, false },
684 { "used", 0, 0, true, false, false,
685 handle_used_attribute, false },
686 { "unused", 0, 0, false, false, false,
687 handle_unused_attribute, false },
688 { "externally_visible", 0, 0, true, false, false,
689 handle_externally_visible_attribute, false },
690 { "no_reorder", 0, 0, true, false, false,
691 handle_no_reorder_attribute, false },
692 /* The same comments as for noreturn attributes apply to const ones. */
693 { "const", 0, 0, true, false, false,
694 handle_const_attribute, false },
695 { "transparent_union", 0, 0, false, false, false,
696 handle_transparent_union_attribute, false },
697 { "constructor", 0, 1, true, false, false,
698 handle_constructor_attribute, false },
699 { "destructor", 0, 1, true, false, false,
700 handle_destructor_attribute, false },
701 { "mode", 1, 1, false, true, false,
702 handle_mode_attribute, false },
703 { "section", 1, 1, true, false, false,
704 handle_section_attribute, false },
705 { "aligned", 0, 1, false, false, false,
706 handle_aligned_attribute, false },
707 { "weak", 0, 0, true, false, false,
708 handle_weak_attribute, false },
709 { "ifunc", 1, 1, true, false, false,
710 handle_ifunc_attribute, false },
711 { "alias", 1, 1, true, false, false,
712 handle_alias_attribute, false },
713 { "weakref", 0, 1, true, false, false,
714 handle_weakref_attribute, false },
715 { "no_instrument_function", 0, 0, true, false, false,
716 handle_no_instrument_function_attribute,
717 false },
718 { "malloc", 0, 0, true, false, false,
719 handle_malloc_attribute, false },
720 { "returns_twice", 0, 0, true, false, false,
721 handle_returns_twice_attribute, false },
722 { "no_stack_limit", 0, 0, true, false, false,
723 handle_no_limit_stack_attribute, false },
724 { "pure", 0, 0, true, false, false,
725 handle_pure_attribute, false },
726 { "transaction_callable", 0, 0, false, true, false,
727 handle_tm_attribute, false },
728 { "transaction_unsafe", 0, 0, false, true, false,
729 handle_tm_attribute, false },
730 { "transaction_safe", 0, 0, false, true, false,
731 handle_tm_attribute, false },
732 { "transaction_may_cancel_outer", 0, 0, false, true, false,
733 handle_tm_attribute, false },
734 /* ??? These two attributes didn't make the transition from the
735 Intel language document to the multi-vendor language document. */
736 { "transaction_pure", 0, 0, false, true, false,
737 handle_tm_attribute, false },
738 { "transaction_wrap", 1, 1, true, false, false,
739 handle_tm_wrap_attribute, false },
740 /* For internal use (marking of builtins) only. The name contains space
741 to prevent its usage in source code. */
742 { "no vops", 0, 0, true, false, false,
743 handle_novops_attribute, false },
744 { "deprecated", 0, 1, false, false, false,
745 handle_deprecated_attribute, false },
746 { "vector_size", 1, 1, false, true, false,
747 handle_vector_size_attribute, false },
748 { "visibility", 1, 1, false, false, false,
749 handle_visibility_attribute, false },
750 { "tls_model", 1, 1, true, false, false,
751 handle_tls_model_attribute, false },
752 { "nonnull", 0, -1, false, true, true,
753 handle_nonnull_attribute, false },
754 { "nothrow", 0, 0, true, false, false,
755 handle_nothrow_attribute, false },
756 { "may_alias", 0, 0, false, true, false, NULL, false },
757 { "cleanup", 1, 1, true, false, false,
758 handle_cleanup_attribute, false },
759 { "warn_unused_result", 0, 0, false, true, true,
760 handle_warn_unused_result_attribute, false },
761 { "sentinel", 0, 1, false, true, true,
762 handle_sentinel_attribute, false },
763 /* For internal use (marking of builtins) only. The name contains space
764 to prevent its usage in source code. */
765 { "type generic", 0, 0, false, true, true,
766 handle_type_generic_attribute, false },
767 { "alloc_size", 1, 2, false, true, true,
768 handle_alloc_size_attribute, false },
769 { "cold", 0, 0, true, false, false,
770 handle_cold_attribute, false },
771 { "hot", 0, 0, true, false, false,
772 handle_hot_attribute, false },
773 { "no_address_safety_analysis",
774 0, 0, true, false, false,
775 handle_no_address_safety_analysis_attribute,
776 false },
777 { "no_sanitize_address", 0, 0, true, false, false,
778 handle_no_sanitize_address_attribute,
779 false },
780 { "no_sanitize_thread", 0, 0, true, false, false,
781 handle_no_sanitize_address_attribute,
782 false },
783 { "no_sanitize_undefined", 0, 0, true, false, false,
784 handle_no_sanitize_undefined_attribute,
785 false },
786 { "warning", 1, 1, true, false, false,
787 handle_error_attribute, false },
788 { "error", 1, 1, true, false, false,
789 handle_error_attribute, false },
790 { "target", 1, -1, true, false, false,
791 handle_target_attribute, false },
792 { "optimize", 1, -1, true, false, false,
793 handle_optimize_attribute, false },
794 /* For internal use only. The leading '*' both prevents its usage in
795 source code and signals that it may be overridden by machine tables. */
796 { "*tm regparm", 0, 0, false, true, true,
797 ignore_attribute, false },
798 { "no_split_stack", 0, 0, true, false, false,
799 handle_no_split_stack_attribute, false },
800 /* For internal use (marking of builtins and runtime functions) only.
801 The name contains space to prevent its usage in source code. */
802 { "fn spec", 1, 1, false, true, true,
803 handle_fnspec_attribute, false },
804 { "warn_unused", 0, 0, false, false, false,
805 handle_warn_unused_attribute, false },
806 { "returns_nonnull", 0, 0, false, true, true,
807 handle_returns_nonnull_attribute, false },
808 { "omp declare simd", 0, -1, true, false, false,
809 handle_omp_declare_simd_attribute, false },
810 { "cilk simd function", 0, -1, true, false, false,
811 handle_omp_declare_simd_attribute, false },
812 { "omp declare target", 0, 0, true, false, false,
813 handle_omp_declare_target_attribute, false },
814 { "alloc_align", 1, 1, false, true, true,
815 handle_alloc_align_attribute, false },
816 { "assume_aligned", 1, 2, false, true, true,
817 handle_assume_aligned_attribute, false },
818 { "designated_init", 0, 0, false, true, false,
819 handle_designated_init_attribute, false },
820 { "bnd_variable_size", 0, 0, true, false, false,
821 handle_bnd_variable_size_attribute, false },
822 { "bnd_legacy", 0, 0, true, false, false,
823 handle_bnd_legacy, false },
824 { "bnd_instrument", 0, 0, true, false, false,
825 handle_bnd_instrument, false },
826 { NULL, 0, 0, false, false, false, NULL, false }
829 /* Give the specifications for the format attributes, used by C and all
830 descendants.
832 All attributes referencing arguments should be additionally processed
833 in chkp_copy_function_type_adding_bounds for correct instrumentation
834 by Pointer Bounds Checker.
835 Current list of processed format attributes: format, format_arg. */
836 const struct attribute_spec c_common_format_attribute_table[] =
838 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
839 affects_type_identity } */
840 { "format", 3, 3, false, true, true,
841 handle_format_attribute, false },
842 { "format_arg", 1, 1, false, true, true,
843 handle_format_arg_attribute, false },
844 { NULL, 0, 0, false, false, false, NULL, false }
847 /* Return identifier for address space AS. */
849 const char *
850 c_addr_space_name (addr_space_t as)
852 int rid = RID_FIRST_ADDR_SPACE + as;
853 gcc_assert (ridpointers [rid]);
854 return IDENTIFIER_POINTER (ridpointers [rid]);
857 /* Push current bindings for the function name VAR_DECLS. */
859 void
860 start_fname_decls (void)
862 unsigned ix;
863 tree saved = NULL_TREE;
865 for (ix = 0; fname_vars[ix].decl; ix++)
867 tree decl = *fname_vars[ix].decl;
869 if (decl)
871 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
872 saved);
873 *fname_vars[ix].decl = NULL_TREE;
876 if (saved || saved_function_name_decls)
877 /* Normally they'll have been NULL, so only push if we've got a
878 stack, or they are non-NULL. */
879 saved_function_name_decls = tree_cons (saved, NULL_TREE,
880 saved_function_name_decls);
883 /* Finish up the current bindings, adding them into the current function's
884 statement tree. This must be done _before_ finish_stmt_tree is called.
885 If there is no current function, we must be at file scope and no statements
886 are involved. Pop the previous bindings. */
888 void
889 finish_fname_decls (void)
891 unsigned ix;
892 tree stmts = NULL_TREE;
893 tree stack = saved_function_name_decls;
895 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
896 append_to_statement_list (TREE_VALUE (stack), &stmts);
898 if (stmts)
900 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
902 if (TREE_CODE (*bodyp) == BIND_EXPR)
903 bodyp = &BIND_EXPR_BODY (*bodyp);
905 append_to_statement_list_force (*bodyp, &stmts);
906 *bodyp = stmts;
909 for (ix = 0; fname_vars[ix].decl; ix++)
910 *fname_vars[ix].decl = NULL_TREE;
912 if (stack)
914 /* We had saved values, restore them. */
915 tree saved;
917 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
919 tree decl = TREE_PURPOSE (saved);
920 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
922 *fname_vars[ix].decl = decl;
924 stack = TREE_CHAIN (stack);
926 saved_function_name_decls = stack;
929 /* Return the text name of the current function, suitably prettified
930 by PRETTY_P. Return string must be freed by caller. */
932 const char *
933 fname_as_string (int pretty_p)
935 const char *name = "top level";
936 char *namep;
937 int vrb = 2, len;
938 cpp_string cstr = { 0, 0 }, strname;
940 if (!pretty_p)
942 name = "";
943 vrb = 0;
946 if (current_function_decl)
947 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
949 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
951 namep = XNEWVEC (char, len);
952 snprintf (namep, len, "\"%s\"", name);
953 strname.text = (unsigned char *) namep;
954 strname.len = len - 1;
956 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
958 XDELETEVEC (namep);
959 return (const char *) cstr.text;
962 return namep;
965 /* Return the VAR_DECL for a const char array naming the current
966 function. If the VAR_DECL has not yet been created, create it
967 now. RID indicates how it should be formatted and IDENTIFIER_NODE
968 ID is its name (unfortunately C and C++ hold the RID values of
969 keywords in different places, so we can't derive RID from ID in
970 this language independent code. LOC is the location of the
971 function. */
973 tree
974 fname_decl (location_t loc, unsigned int rid, tree id)
976 unsigned ix;
977 tree decl = NULL_TREE;
979 for (ix = 0; fname_vars[ix].decl; ix++)
980 if (fname_vars[ix].rid == rid)
981 break;
983 decl = *fname_vars[ix].decl;
984 if (!decl)
986 /* If a tree is built here, it would normally have the lineno of
987 the current statement. Later this tree will be moved to the
988 beginning of the function and this line number will be wrong.
989 To avoid this problem set the lineno to 0 here; that prevents
990 it from appearing in the RTL. */
991 tree stmts;
992 location_t saved_location = input_location;
993 input_location = UNKNOWN_LOCATION;
995 stmts = push_stmt_list ();
996 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
997 stmts = pop_stmt_list (stmts);
998 if (!IS_EMPTY_STMT (stmts))
999 saved_function_name_decls
1000 = tree_cons (decl, stmts, saved_function_name_decls);
1001 *fname_vars[ix].decl = decl;
1002 input_location = saved_location;
1004 if (!ix && !current_function_decl)
1005 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1007 return decl;
1010 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1012 tree
1013 fix_string_type (tree value)
1015 int length = TREE_STRING_LENGTH (value);
1016 int nchars;
1017 tree e_type, i_type, a_type;
1019 /* Compute the number of elements, for the array type. */
1020 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1022 nchars = length;
1023 e_type = char_type_node;
1025 else if (TREE_TYPE (value) == char16_array_type_node)
1027 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1028 e_type = char16_type_node;
1030 else if (TREE_TYPE (value) == char32_array_type_node)
1032 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1033 e_type = char32_type_node;
1035 else
1037 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1038 e_type = wchar_type_node;
1041 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1042 limit in C++98 Annex B is very large (65536) and is not normative,
1043 so we do not diagnose it (warn_overlength_strings is forced off
1044 in c_common_post_options). */
1045 if (warn_overlength_strings)
1047 const int nchars_max = flag_isoc99 ? 4095 : 509;
1048 const int relevant_std = flag_isoc99 ? 99 : 90;
1049 if (nchars - 1 > nchars_max)
1050 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1051 separate the %d from the 'C'. 'ISO' should not be
1052 translated, but it may be moved after 'C%d' in languages
1053 where modifiers follow nouns. */
1054 pedwarn (input_location, OPT_Woverlength_strings,
1055 "string length %qd is greater than the length %qd "
1056 "ISO C%d compilers are required to support",
1057 nchars - 1, nchars_max, relevant_std);
1060 /* Create the array type for the string constant. The ISO C++
1061 standard says that a string literal has type `const char[N]' or
1062 `const wchar_t[N]'. We use the same logic when invoked as a C
1063 front-end with -Wwrite-strings.
1064 ??? We should change the type of an expression depending on the
1065 state of a warning flag. We should just be warning -- see how
1066 this is handled in the C++ front-end for the deprecated implicit
1067 conversion from string literals to `char*' or `wchar_t*'.
1069 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1070 array type being the unqualified version of that type.
1071 Therefore, if we are constructing an array of const char, we must
1072 construct the matching unqualified array type first. The C front
1073 end does not require this, but it does no harm, so we do it
1074 unconditionally. */
1075 i_type = build_index_type (size_int (nchars - 1));
1076 a_type = build_array_type (e_type, i_type);
1077 if (c_dialect_cxx() || warn_write_strings)
1078 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1080 TREE_TYPE (value) = a_type;
1081 TREE_CONSTANT (value) = 1;
1082 TREE_READONLY (value) = 1;
1083 TREE_STATIC (value) = 1;
1084 return value;
1087 /* If DISABLE is true, stop issuing warnings. This is used when
1088 parsing code that we know will not be executed. This function may
1089 be called multiple times, and works as a stack. */
1091 static void
1092 c_disable_warnings (bool disable)
1094 if (disable)
1096 ++c_inhibit_evaluation_warnings;
1097 fold_defer_overflow_warnings ();
1101 /* If ENABLE is true, reenable issuing warnings. */
1103 static void
1104 c_enable_warnings (bool enable)
1106 if (enable)
1108 --c_inhibit_evaluation_warnings;
1109 fold_undefer_and_ignore_overflow_warnings ();
1113 /* Fully fold EXPR, an expression that was not folded (beyond integer
1114 constant expressions and null pointer constants) when being built
1115 up. If IN_INIT, this is in a static initializer and certain
1116 changes are made to the folding done. Clear *MAYBE_CONST if
1117 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1118 expression because it contains an evaluated operator (in C99) or an
1119 operator outside of sizeof returning an integer constant (in C90)
1120 not permitted in constant expressions, or because it contains an
1121 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1122 set to true by callers before calling this function.) Return the
1123 folded expression. Function arguments have already been folded
1124 before calling this function, as have the contents of SAVE_EXPR,
1125 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1126 C_MAYBE_CONST_EXPR. */
1128 tree
1129 c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1131 tree ret;
1132 tree eptype = NULL_TREE;
1133 bool dummy = true;
1134 bool maybe_const_itself = true;
1135 location_t loc = EXPR_LOCATION (expr);
1137 /* This function is not relevant to C++ because C++ folds while
1138 parsing, and may need changes to be correct for C++ when C++
1139 stops folding while parsing. */
1140 if (c_dialect_cxx ())
1141 gcc_unreachable ();
1143 if (!maybe_const)
1144 maybe_const = &dummy;
1145 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1147 eptype = TREE_TYPE (expr);
1148 expr = TREE_OPERAND (expr, 0);
1150 ret = c_fully_fold_internal (expr, in_init, maybe_const,
1151 &maybe_const_itself, false);
1152 if (eptype)
1153 ret = fold_convert_loc (loc, eptype, ret);
1154 *maybe_const &= maybe_const_itself;
1155 return ret;
1158 /* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1159 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1160 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1161 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1162 both evaluated and unevaluated subexpressions while
1163 *MAYBE_CONST_ITSELF is carried from only evaluated
1164 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1165 with integer constant operands, and if any of the operands doesn't
1166 get folded to an integer constant, don't fold the expression itself. */
1168 static tree
1169 c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
1170 bool *maybe_const_itself, bool for_int_const)
1172 tree ret = expr;
1173 enum tree_code code = TREE_CODE (expr);
1174 enum tree_code_class kind = TREE_CODE_CLASS (code);
1175 location_t loc = EXPR_LOCATION (expr);
1176 tree op0, op1, op2, op3;
1177 tree orig_op0, orig_op1, orig_op2;
1178 bool op0_const = true, op1_const = true, op2_const = true;
1179 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1180 bool nowarning = TREE_NO_WARNING (expr);
1181 bool unused_p;
1183 /* This function is not relevant to C++ because C++ folds while
1184 parsing, and may need changes to be correct for C++ when C++
1185 stops folding while parsing. */
1186 if (c_dialect_cxx ())
1187 gcc_unreachable ();
1189 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1190 anything else not counted as an expression cannot usefully be
1191 folded further at this point. */
1192 if (!IS_EXPR_CODE_CLASS (kind)
1193 || kind == tcc_statement
1194 || code == SAVE_EXPR)
1195 return expr;
1197 /* Operands of variable-length expressions (function calls) have
1198 already been folded, as have __builtin_* function calls, and such
1199 expressions cannot occur in constant expressions. */
1200 if (kind == tcc_vl_exp)
1202 *maybe_const_operands = false;
1203 ret = fold (expr);
1204 goto out;
1207 if (code == C_MAYBE_CONST_EXPR)
1209 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1210 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1211 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1212 *maybe_const_operands = false;
1213 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
1215 *maybe_const_itself = false;
1216 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1217 maybe_const_itself, true);
1219 if (pre && !in_init)
1220 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1221 else
1222 ret = inner;
1223 goto out;
1226 /* Assignment, increment, decrement, function call and comma
1227 operators, and statement expressions, cannot occur in constant
1228 expressions if evaluated / outside of sizeof. (Function calls
1229 were handled above, though VA_ARG_EXPR is treated like a function
1230 call here, and statement expressions are handled through
1231 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1232 switch (code)
1234 case MODIFY_EXPR:
1235 case PREDECREMENT_EXPR:
1236 case PREINCREMENT_EXPR:
1237 case POSTDECREMENT_EXPR:
1238 case POSTINCREMENT_EXPR:
1239 case COMPOUND_EXPR:
1240 *maybe_const_operands = false;
1241 break;
1243 case VA_ARG_EXPR:
1244 case TARGET_EXPR:
1245 case BIND_EXPR:
1246 case OBJ_TYPE_REF:
1247 *maybe_const_operands = false;
1248 ret = fold (expr);
1249 goto out;
1251 default:
1252 break;
1255 /* Fold individual tree codes as appropriate. */
1256 switch (code)
1258 case COMPOUND_LITERAL_EXPR:
1259 /* Any non-constancy will have been marked in a containing
1260 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1261 goto out;
1263 case COMPONENT_REF:
1264 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1265 op1 = TREE_OPERAND (expr, 1);
1266 op2 = TREE_OPERAND (expr, 2);
1267 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1268 maybe_const_itself, for_int_const);
1269 STRIP_TYPE_NOPS (op0);
1270 if (op0 != orig_op0)
1271 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1272 if (ret != expr)
1274 TREE_READONLY (ret) = TREE_READONLY (expr);
1275 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1277 goto out;
1279 case ARRAY_REF:
1280 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1281 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1282 op2 = TREE_OPERAND (expr, 2);
1283 op3 = TREE_OPERAND (expr, 3);
1284 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1285 maybe_const_itself, for_int_const);
1286 STRIP_TYPE_NOPS (op0);
1287 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1288 maybe_const_itself, for_int_const);
1289 STRIP_TYPE_NOPS (op1);
1290 op1 = decl_constant_value_for_optimization (op1);
1291 if (op0 != orig_op0 || op1 != orig_op1)
1292 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1293 if (ret != expr)
1295 TREE_READONLY (ret) = TREE_READONLY (expr);
1296 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1297 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1299 ret = fold (ret);
1300 goto out;
1302 case COMPOUND_EXPR:
1303 case MODIFY_EXPR:
1304 case PREDECREMENT_EXPR:
1305 case PREINCREMENT_EXPR:
1306 case POSTDECREMENT_EXPR:
1307 case POSTINCREMENT_EXPR:
1308 case PLUS_EXPR:
1309 case MINUS_EXPR:
1310 case MULT_EXPR:
1311 case POINTER_PLUS_EXPR:
1312 case TRUNC_DIV_EXPR:
1313 case CEIL_DIV_EXPR:
1314 case FLOOR_DIV_EXPR:
1315 case TRUNC_MOD_EXPR:
1316 case RDIV_EXPR:
1317 case EXACT_DIV_EXPR:
1318 case LSHIFT_EXPR:
1319 case RSHIFT_EXPR:
1320 case BIT_IOR_EXPR:
1321 case BIT_XOR_EXPR:
1322 case BIT_AND_EXPR:
1323 case LT_EXPR:
1324 case LE_EXPR:
1325 case GT_EXPR:
1326 case GE_EXPR:
1327 case EQ_EXPR:
1328 case NE_EXPR:
1329 case COMPLEX_EXPR:
1330 case TRUTH_AND_EXPR:
1331 case TRUTH_OR_EXPR:
1332 case TRUTH_XOR_EXPR:
1333 case UNORDERED_EXPR:
1334 case ORDERED_EXPR:
1335 case UNLT_EXPR:
1336 case UNLE_EXPR:
1337 case UNGT_EXPR:
1338 case UNGE_EXPR:
1339 case UNEQ_EXPR:
1340 /* Binary operations evaluating both arguments (increment and
1341 decrement are binary internally in GCC). */
1342 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1343 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1344 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1345 maybe_const_itself, for_int_const);
1346 STRIP_TYPE_NOPS (op0);
1347 if (code != MODIFY_EXPR
1348 && code != PREDECREMENT_EXPR
1349 && code != PREINCREMENT_EXPR
1350 && code != POSTDECREMENT_EXPR
1351 && code != POSTINCREMENT_EXPR)
1352 op0 = decl_constant_value_for_optimization (op0);
1353 /* The RHS of a MODIFY_EXPR was fully folded when building that
1354 expression for the sake of conversion warnings. */
1355 if (code != MODIFY_EXPR)
1356 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
1357 maybe_const_itself, for_int_const);
1358 STRIP_TYPE_NOPS (op1);
1359 op1 = decl_constant_value_for_optimization (op1);
1361 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1362 || TREE_CODE (op1) != INTEGER_CST))
1363 goto out;
1365 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1366 ret = in_init
1367 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1368 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1369 else
1370 ret = fold (expr);
1371 if (TREE_OVERFLOW_P (ret)
1372 && !TREE_OVERFLOW_P (op0)
1373 && !TREE_OVERFLOW_P (op1))
1374 overflow_warning (EXPR_LOCATION (expr), ret);
1375 if (code == LSHIFT_EXPR
1376 && TREE_CODE (orig_op0) != INTEGER_CST
1377 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1378 && TREE_CODE (op0) == INTEGER_CST
1379 && c_inhibit_evaluation_warnings == 0
1380 && tree_int_cst_sgn (op0) < 0)
1381 warning_at (loc, OPT_Wshift_negative_value,
1382 "left shift of negative value");
1383 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1384 && TREE_CODE (orig_op1) != INTEGER_CST
1385 && TREE_CODE (op1) == INTEGER_CST
1386 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1387 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1388 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1389 && c_inhibit_evaluation_warnings == 0)
1391 if (tree_int_cst_sgn (op1) < 0)
1392 warning_at (loc, OPT_Wshift_count_negative,
1393 (code == LSHIFT_EXPR
1394 ? G_("left shift count is negative")
1395 : G_("right shift count is negative")));
1396 else if (compare_tree_int (op1,
1397 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1398 >= 0)
1399 warning_at (loc, OPT_Wshift_count_overflow,
1400 (code == LSHIFT_EXPR
1401 ? G_("left shift count >= width of type")
1402 : G_("right shift count >= width of type")));
1404 if ((code == TRUNC_DIV_EXPR
1405 || code == CEIL_DIV_EXPR
1406 || code == FLOOR_DIV_EXPR
1407 || code == EXACT_DIV_EXPR
1408 || code == TRUNC_MOD_EXPR)
1409 && TREE_CODE (orig_op1) != INTEGER_CST
1410 && TREE_CODE (op1) == INTEGER_CST
1411 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1412 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1413 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1414 warn_for_div_by_zero (loc, op1);
1415 goto out;
1417 case INDIRECT_REF:
1418 case FIX_TRUNC_EXPR:
1419 case FLOAT_EXPR:
1420 CASE_CONVERT:
1421 case ADDR_SPACE_CONVERT_EXPR:
1422 case VIEW_CONVERT_EXPR:
1423 case NON_LVALUE_EXPR:
1424 case NEGATE_EXPR:
1425 case BIT_NOT_EXPR:
1426 case TRUTH_NOT_EXPR:
1427 case ADDR_EXPR:
1428 case CONJ_EXPR:
1429 case REALPART_EXPR:
1430 case IMAGPART_EXPR:
1431 /* Unary operations. */
1432 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1433 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
1434 maybe_const_itself, for_int_const);
1435 STRIP_TYPE_NOPS (op0);
1436 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1437 op0 = decl_constant_value_for_optimization (op0);
1439 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1440 goto out;
1442 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1443 not prepared to deal with them if they occur in initializers. */
1444 if (op0 != orig_op0
1445 && code == ADDR_EXPR
1446 && (op1 = get_base_address (op0)) != NULL_TREE
1447 && TREE_CODE (op1) == INDIRECT_REF
1448 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
1449 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
1450 else if (op0 != orig_op0 || in_init)
1451 ret = in_init
1452 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1453 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
1454 else
1455 ret = fold (expr);
1456 if (code == INDIRECT_REF
1457 && ret != expr
1458 && TREE_CODE (ret) == INDIRECT_REF)
1460 TREE_READONLY (ret) = TREE_READONLY (expr);
1461 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1462 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1464 switch (code)
1466 case FIX_TRUNC_EXPR:
1467 case FLOAT_EXPR:
1468 CASE_CONVERT:
1469 /* Don't warn about explicit conversions. We will already
1470 have warned about suspect implicit conversions. */
1471 break;
1473 default:
1474 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1475 overflow_warning (EXPR_LOCATION (expr), ret);
1476 break;
1478 goto out;
1480 case TRUTH_ANDIF_EXPR:
1481 case TRUTH_ORIF_EXPR:
1482 /* Binary operations not necessarily evaluating both
1483 arguments. */
1484 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1485 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1486 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1487 for_int_const);
1488 STRIP_TYPE_NOPS (op0);
1490 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1491 ? truthvalue_false_node
1492 : truthvalue_true_node));
1493 c_disable_warnings (unused_p);
1494 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1495 for_int_const);
1496 STRIP_TYPE_NOPS (op1);
1497 c_enable_warnings (unused_p);
1499 if (for_int_const
1500 && (TREE_CODE (op0) != INTEGER_CST
1501 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1502 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1503 goto out;
1505 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1506 ret = in_init
1507 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1508 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
1509 else
1510 ret = fold (expr);
1511 *maybe_const_operands &= op0_const;
1512 *maybe_const_itself &= op0_const_self;
1513 if (!(flag_isoc99
1514 && op0_const
1515 && op0_const_self
1516 && (code == TRUTH_ANDIF_EXPR
1517 ? op0 == truthvalue_false_node
1518 : op0 == truthvalue_true_node)))
1519 *maybe_const_operands &= op1_const;
1520 if (!(op0_const
1521 && op0_const_self
1522 && (code == TRUTH_ANDIF_EXPR
1523 ? op0 == truthvalue_false_node
1524 : op0 == truthvalue_true_node)))
1525 *maybe_const_itself &= op1_const_self;
1526 goto out;
1528 case COND_EXPR:
1529 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1530 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1531 orig_op2 = op2 = TREE_OPERAND (expr, 2);
1532 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1533 for_int_const);
1535 STRIP_TYPE_NOPS (op0);
1536 c_disable_warnings (op0 == truthvalue_false_node);
1537 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1538 for_int_const);
1539 STRIP_TYPE_NOPS (op1);
1540 c_enable_warnings (op0 == truthvalue_false_node);
1542 c_disable_warnings (op0 == truthvalue_true_node);
1543 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1544 for_int_const);
1545 STRIP_TYPE_NOPS (op2);
1546 c_enable_warnings (op0 == truthvalue_true_node);
1548 if (for_int_const
1549 && (TREE_CODE (op0) != INTEGER_CST
1550 /* Only the evaluated operand must be an INTEGER_CST. */
1551 || (op0 == truthvalue_true_node
1552 ? TREE_CODE (op1) != INTEGER_CST
1553 : TREE_CODE (op2) != INTEGER_CST)))
1554 goto out;
1556 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
1557 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
1558 else
1559 ret = fold (expr);
1560 *maybe_const_operands &= op0_const;
1561 *maybe_const_itself &= op0_const_self;
1562 if (!(flag_isoc99
1563 && op0_const
1564 && op0_const_self
1565 && op0 == truthvalue_false_node))
1566 *maybe_const_operands &= op1_const;
1567 if (!(op0_const
1568 && op0_const_self
1569 && op0 == truthvalue_false_node))
1570 *maybe_const_itself &= op1_const_self;
1571 if (!(flag_isoc99
1572 && op0_const
1573 && op0_const_self
1574 && op0 == truthvalue_true_node))
1575 *maybe_const_operands &= op2_const;
1576 if (!(op0_const
1577 && op0_const_self
1578 && op0 == truthvalue_true_node))
1579 *maybe_const_itself &= op2_const_self;
1580 goto out;
1582 case EXCESS_PRECISION_EXPR:
1583 /* Each case where an operand with excess precision may be
1584 encountered must remove the EXCESS_PRECISION_EXPR around
1585 inner operands and possibly put one around the whole
1586 expression or possibly convert to the semantic type (which
1587 c_fully_fold does); we cannot tell at this stage which is
1588 appropriate in any particular case. */
1589 gcc_unreachable ();
1591 default:
1592 /* Various codes may appear through folding built-in functions
1593 and their arguments. */
1594 goto out;
1597 out:
1598 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1599 have been done by this point, so remove them again. */
1600 nowarning |= TREE_NO_WARNING (ret);
1601 STRIP_TYPE_NOPS (ret);
1602 if (nowarning && !TREE_NO_WARNING (ret))
1604 if (!CAN_HAVE_LOCATION_P (ret))
1605 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1606 TREE_NO_WARNING (ret) = 1;
1608 if (ret != expr)
1609 protected_set_expr_location (ret, loc);
1610 return ret;
1613 /* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1614 return EXP. Otherwise, return either EXP or its known constant
1615 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1616 Is the BLKmode test appropriate? */
1618 tree
1619 decl_constant_value_for_optimization (tree exp)
1621 tree ret;
1623 /* This function is only used by C, for c_fully_fold and other
1624 optimization, and may not be correct for C++. */
1625 if (c_dialect_cxx ())
1626 gcc_unreachable ();
1628 if (!optimize
1629 || TREE_CODE (exp) != VAR_DECL
1630 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1631 || DECL_MODE (exp) == BLKmode)
1632 return exp;
1634 ret = decl_constant_value (exp);
1635 /* Avoid unwanted tree sharing between the initializer and current
1636 function's body where the tree can be modified e.g. by the
1637 gimplifier. */
1638 if (ret != exp && TREE_STATIC (exp))
1639 ret = unshare_expr (ret);
1640 return ret;
1643 /* Print a warning if a constant expression had overflow in folding.
1644 Invoke this function on every expression that the language
1645 requires to be a constant expression.
1646 Note the ANSI C standard says it is erroneous for a
1647 constant expression to overflow. */
1649 void
1650 constant_expression_warning (tree value)
1652 if (warn_overflow && pedantic
1653 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1654 || TREE_CODE (value) == FIXED_CST
1655 || TREE_CODE (value) == VECTOR_CST
1656 || TREE_CODE (value) == COMPLEX_CST)
1657 && TREE_OVERFLOW (value))
1658 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1661 /* The same as above but print an unconditional error. */
1662 void
1663 constant_expression_error (tree value)
1665 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1666 || TREE_CODE (value) == FIXED_CST
1667 || TREE_CODE (value) == VECTOR_CST
1668 || TREE_CODE (value) == COMPLEX_CST)
1669 && TREE_OVERFLOW (value))
1670 error ("overflow in constant expression");
1673 /* Print a warning if an expression had overflow in folding and its
1674 operands hadn't.
1676 Invoke this function on every expression that
1677 (1) appears in the source code, and
1678 (2) is a constant expression that overflowed, and
1679 (3) is not already checked by convert_and_check;
1680 however, do not invoke this function on operands of explicit casts
1681 or when the expression is the result of an operator and any operand
1682 already overflowed. */
1684 void
1685 overflow_warning (location_t loc, tree value)
1687 if (c_inhibit_evaluation_warnings != 0)
1688 return;
1690 switch (TREE_CODE (value))
1692 case INTEGER_CST:
1693 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1694 break;
1696 case REAL_CST:
1697 warning_at (loc, OPT_Woverflow,
1698 "floating point overflow in expression");
1699 break;
1701 case FIXED_CST:
1702 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1703 break;
1705 case VECTOR_CST:
1706 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1707 break;
1709 case COMPLEX_CST:
1710 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1711 warning_at (loc, OPT_Woverflow,
1712 "complex integer overflow in expression");
1713 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1714 warning_at (loc, OPT_Woverflow,
1715 "complex floating point overflow in expression");
1716 break;
1718 default:
1719 break;
1723 /* Warn about uses of logical || / && operator in a context where it
1724 is likely that the bitwise equivalent was intended by the
1725 programmer. We have seen an expression in which CODE is a binary
1726 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1727 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1728 void
1729 warn_logical_operator (location_t location, enum tree_code code, tree type,
1730 enum tree_code code_left, tree op_left,
1731 enum tree_code ARG_UNUSED (code_right), tree op_right)
1733 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1734 int in0_p, in1_p, in_p;
1735 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1736 bool strict_overflow_p = false;
1738 if (code != TRUTH_ANDIF_EXPR
1739 && code != TRUTH_AND_EXPR
1740 && code != TRUTH_ORIF_EXPR
1741 && code != TRUTH_OR_EXPR)
1742 return;
1744 /* We don't want to warn if either operand comes from a macro
1745 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1746 see PR61534. */
1747 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1748 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1749 return;
1751 /* Warn if &&/|| are being used in a context where it is
1752 likely that the bitwise equivalent was intended by the
1753 programmer. That is, an expression such as op && MASK
1754 where op should not be any boolean expression, nor a
1755 constant, and mask seems to be a non-boolean integer constant. */
1756 if (!truth_value_p (code_left)
1757 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1758 && !CONSTANT_CLASS_P (op_left)
1759 && !TREE_NO_WARNING (op_left)
1760 && TREE_CODE (op_right) == INTEGER_CST
1761 && !integer_zerop (op_right)
1762 && !integer_onep (op_right))
1764 if (or_op)
1765 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1766 " applied to non-boolean constant");
1767 else
1768 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1769 " applied to non-boolean constant");
1770 TREE_NO_WARNING (op_left) = true;
1771 return;
1774 /* We do not warn for constants because they are typical of macro
1775 expansions that test for features. */
1776 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1777 return;
1779 /* This warning only makes sense with logical operands. */
1780 if (!(truth_value_p (TREE_CODE (op_left))
1781 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1782 || !(truth_value_p (TREE_CODE (op_right))
1783 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1784 return;
1786 /* The range computations only work with scalars. */
1787 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1788 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1789 return;
1791 /* We first test whether either side separately is trivially true
1792 (with OR) or trivially false (with AND). If so, do not warn.
1793 This is a common idiom for testing ranges of data types in
1794 portable code. */
1795 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1796 if (!lhs)
1797 return;
1798 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1799 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1801 /* If this is an OR operation, invert both sides; now, the result
1802 should be always false to get a warning. */
1803 if (or_op)
1804 in0_p = !in0_p;
1806 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1807 if (tem && integer_zerop (tem))
1808 return;
1810 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1811 if (!rhs)
1812 return;
1813 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1814 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1816 /* If this is an OR operation, invert both sides; now, the result
1817 should be always false to get a warning. */
1818 if (or_op)
1819 in1_p = !in1_p;
1821 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1822 if (tem && integer_zerop (tem))
1823 return;
1825 /* If both expressions have the same operand, if we can merge the
1826 ranges, ... */
1827 if (operand_equal_p (lhs, rhs, 0)
1828 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1829 in1_p, low1, high1))
1831 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1832 /* ... and if the range test is always false, then warn. */
1833 if (tem && integer_zerop (tem))
1835 if (or_op)
1836 warning_at (location, OPT_Wlogical_op,
1837 "logical %<or%> of collectively exhaustive tests is "
1838 "always true");
1839 else
1840 warning_at (location, OPT_Wlogical_op,
1841 "logical %<and%> of mutually exclusive tests is "
1842 "always false");
1844 /* Or warn if the operands have exactly the same range, e.g.
1845 A > 0 && A > 0. */
1846 else if (low0 == low1 && high0 == high1)
1848 if (or_op)
1849 warning_at (location, OPT_Wlogical_op,
1850 "logical %<or%> of equal expressions");
1851 else
1852 warning_at (location, OPT_Wlogical_op,
1853 "logical %<and%> of equal expressions");
1858 /* Warn about logical not used on the left hand side operand of a comparison.
1859 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1860 Do not warn if RHS is of a boolean type. */
1862 void
1863 warn_logical_not_parentheses (location_t location, enum tree_code code,
1864 tree rhs)
1866 if (TREE_CODE_CLASS (code) != tcc_comparison
1867 || TREE_TYPE (rhs) == NULL_TREE
1868 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1869 return;
1871 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1872 !(x == 0) or !(y != 0). */
1873 if ((code == EQ_EXPR || code == NE_EXPR)
1874 && integer_zerop (rhs))
1875 return;
1877 warning_at (location, OPT_Wlogical_not_parentheses,
1878 "logical not is only applied to the left hand side of "
1879 "comparison");
1882 /* Warn if EXP contains any computations whose results are not used.
1883 Return true if a warning is printed; false otherwise. LOCUS is the
1884 (potential) location of the expression. */
1886 bool
1887 warn_if_unused_value (const_tree exp, location_t locus)
1889 restart:
1890 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1891 return false;
1893 /* Don't warn about void constructs. This includes casting to void,
1894 void function calls, and statement expressions with a final cast
1895 to void. */
1896 if (VOID_TYPE_P (TREE_TYPE (exp)))
1897 return false;
1899 if (EXPR_HAS_LOCATION (exp))
1900 locus = EXPR_LOCATION (exp);
1902 switch (TREE_CODE (exp))
1904 case PREINCREMENT_EXPR:
1905 case POSTINCREMENT_EXPR:
1906 case PREDECREMENT_EXPR:
1907 case POSTDECREMENT_EXPR:
1908 case MODIFY_EXPR:
1909 case INIT_EXPR:
1910 case TARGET_EXPR:
1911 case CALL_EXPR:
1912 case TRY_CATCH_EXPR:
1913 case WITH_CLEANUP_EXPR:
1914 case EXIT_EXPR:
1915 case VA_ARG_EXPR:
1916 return false;
1918 case BIND_EXPR:
1919 /* For a binding, warn if no side effect within it. */
1920 exp = BIND_EXPR_BODY (exp);
1921 goto restart;
1923 case SAVE_EXPR:
1924 case NON_LVALUE_EXPR:
1925 case NOP_EXPR:
1926 exp = TREE_OPERAND (exp, 0);
1927 goto restart;
1929 case TRUTH_ORIF_EXPR:
1930 case TRUTH_ANDIF_EXPR:
1931 /* In && or ||, warn if 2nd operand has no side effect. */
1932 exp = TREE_OPERAND (exp, 1);
1933 goto restart;
1935 case COMPOUND_EXPR:
1936 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1937 return true;
1938 /* Let people do `(foo (), 0)' without a warning. */
1939 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1940 return false;
1941 exp = TREE_OPERAND (exp, 1);
1942 goto restart;
1944 case COND_EXPR:
1945 /* If this is an expression with side effects, don't warn; this
1946 case commonly appears in macro expansions. */
1947 if (TREE_SIDE_EFFECTS (exp))
1948 return false;
1949 goto warn;
1951 case INDIRECT_REF:
1952 /* Don't warn about automatic dereferencing of references, since
1953 the user cannot control it. */
1954 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1956 exp = TREE_OPERAND (exp, 0);
1957 goto restart;
1959 /* Fall through. */
1961 default:
1962 /* Referencing a volatile value is a side effect, so don't warn. */
1963 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1964 && TREE_THIS_VOLATILE (exp))
1965 return false;
1967 /* If this is an expression which has no operands, there is no value
1968 to be unused. There are no such language-independent codes,
1969 but front ends may define such. */
1970 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1971 return false;
1973 warn:
1974 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1979 /* Print a warning about casts that might indicate violation
1980 of strict aliasing rules if -Wstrict-aliasing is used and
1981 strict aliasing mode is in effect. OTYPE is the original
1982 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1984 bool
1985 strict_aliasing_warning (tree otype, tree type, tree expr)
1987 /* Strip pointer conversion chains and get to the correct original type. */
1988 STRIP_NOPS (expr);
1989 otype = TREE_TYPE (expr);
1991 if (!(flag_strict_aliasing
1992 && POINTER_TYPE_P (type)
1993 && POINTER_TYPE_P (otype)
1994 && !VOID_TYPE_P (TREE_TYPE (type)))
1995 /* If the type we are casting to is a ref-all pointer
1996 dereferencing it is always valid. */
1997 || TYPE_REF_CAN_ALIAS_ALL (type))
1998 return false;
2000 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
2001 && (DECL_P (TREE_OPERAND (expr, 0))
2002 || handled_component_p (TREE_OPERAND (expr, 0))))
2004 /* Casting the address of an object to non void pointer. Warn
2005 if the cast breaks type based aliasing. */
2006 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2008 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2009 "might break strict-aliasing rules");
2010 return true;
2012 else
2014 /* warn_strict_aliasing >= 3. This includes the default (3).
2015 Only warn if the cast is dereferenced immediately. */
2016 alias_set_type set1 =
2017 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
2018 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2020 if (set1 != set2 && set2 != 0
2021 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
2023 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2024 "pointer will break strict-aliasing rules");
2025 return true;
2027 else if (warn_strict_aliasing == 2
2028 && !alias_sets_must_conflict_p (set1, set2))
2030 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2031 "pointer might break strict-aliasing rules");
2032 return true;
2036 else
2037 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2039 /* At this level, warn for any conversions, even if an address is
2040 not taken in the same statement. This will likely produce many
2041 false positives, but could be useful to pinpoint problems that
2042 are not revealed at higher levels. */
2043 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2044 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2045 if (!COMPLETE_TYPE_P (type)
2046 || !alias_sets_must_conflict_p (set1, set2))
2048 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2049 "pointer might break strict-aliasing rules");
2050 return true;
2054 return false;
2057 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2058 sizeof as last operand of certain builtins. */
2060 void
2061 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
2062 vec<tree, va_gc> *params, tree *sizeof_arg,
2063 bool (*comp_types) (tree, tree))
2065 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
2066 bool strop = false, cmp = false;
2067 unsigned int idx = ~0;
2068 location_t loc;
2070 if (TREE_CODE (callee) != FUNCTION_DECL
2071 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
2072 || vec_safe_length (params) <= 1)
2073 return;
2075 switch (DECL_FUNCTION_CODE (callee))
2077 case BUILT_IN_STRNCMP:
2078 case BUILT_IN_STRNCASECMP:
2079 cmp = true;
2080 /* FALLTHRU */
2081 case BUILT_IN_STRNCPY:
2082 case BUILT_IN_STRNCPY_CHK:
2083 case BUILT_IN_STRNCAT:
2084 case BUILT_IN_STRNCAT_CHK:
2085 case BUILT_IN_STPNCPY:
2086 case BUILT_IN_STPNCPY_CHK:
2087 strop = true;
2088 /* FALLTHRU */
2089 case BUILT_IN_MEMCPY:
2090 case BUILT_IN_MEMCPY_CHK:
2091 case BUILT_IN_MEMMOVE:
2092 case BUILT_IN_MEMMOVE_CHK:
2093 if (params->length () < 3)
2094 return;
2095 src = (*params)[1];
2096 dest = (*params)[0];
2097 idx = 2;
2098 break;
2099 case BUILT_IN_BCOPY:
2100 if (params->length () < 3)
2101 return;
2102 src = (*params)[0];
2103 dest = (*params)[1];
2104 idx = 2;
2105 break;
2106 case BUILT_IN_MEMCMP:
2107 case BUILT_IN_BCMP:
2108 if (params->length () < 3)
2109 return;
2110 src = (*params)[1];
2111 dest = (*params)[0];
2112 idx = 2;
2113 cmp = true;
2114 break;
2115 case BUILT_IN_MEMSET:
2116 case BUILT_IN_MEMSET_CHK:
2117 if (params->length () < 3)
2118 return;
2119 dest = (*params)[0];
2120 idx = 2;
2121 break;
2122 case BUILT_IN_BZERO:
2123 dest = (*params)[0];
2124 idx = 1;
2125 break;
2126 case BUILT_IN_STRNDUP:
2127 src = (*params)[0];
2128 strop = true;
2129 idx = 1;
2130 break;
2131 case BUILT_IN_MEMCHR:
2132 if (params->length () < 3)
2133 return;
2134 src = (*params)[0];
2135 idx = 2;
2136 break;
2137 case BUILT_IN_SNPRINTF:
2138 case BUILT_IN_SNPRINTF_CHK:
2139 case BUILT_IN_VSNPRINTF:
2140 case BUILT_IN_VSNPRINTF_CHK:
2141 dest = (*params)[0];
2142 idx = 1;
2143 strop = true;
2144 break;
2145 default:
2146 break;
2149 if (idx >= 3)
2150 return;
2152 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2153 return;
2155 type = TYPE_P (sizeof_arg[idx])
2156 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2157 if (!POINTER_TYPE_P (type))
2158 return;
2160 if (dest
2161 && (tem = tree_strip_nop_conversions (dest))
2162 && POINTER_TYPE_P (TREE_TYPE (tem))
2163 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2164 return;
2166 if (src
2167 && (tem = tree_strip_nop_conversions (src))
2168 && POINTER_TYPE_P (TREE_TYPE (tem))
2169 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2170 return;
2172 loc = sizeof_arg_loc[idx];
2174 if (dest && !cmp)
2176 if (!TYPE_P (sizeof_arg[idx])
2177 && operand_equal_p (dest, sizeof_arg[idx], 0)
2178 && comp_types (TREE_TYPE (dest), type))
2180 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2181 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2182 "argument to %<sizeof%> in %qD call is the same "
2183 "expression as the destination; did you mean to "
2184 "remove the addressof?", callee);
2185 else if ((TYPE_PRECISION (TREE_TYPE (type))
2186 == TYPE_PRECISION (char_type_node))
2187 || strop)
2188 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2189 "argument to %<sizeof%> in %qD call is the same "
2190 "expression as the destination; did you mean to "
2191 "provide an explicit length?", callee);
2192 else
2193 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2194 "argument to %<sizeof%> in %qD call is the same "
2195 "expression as the destination; did you mean to "
2196 "dereference it?", callee);
2197 return;
2200 if (POINTER_TYPE_P (TREE_TYPE (dest))
2201 && !strop
2202 && comp_types (TREE_TYPE (dest), type)
2203 && !VOID_TYPE_P (TREE_TYPE (type)))
2205 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2206 "argument to %<sizeof%> in %qD call is the same "
2207 "pointer type %qT as the destination; expected %qT "
2208 "or an explicit length", callee, TREE_TYPE (dest),
2209 TREE_TYPE (TREE_TYPE (dest)));
2210 return;
2214 if (src && !cmp)
2216 if (!TYPE_P (sizeof_arg[idx])
2217 && operand_equal_p (src, sizeof_arg[idx], 0)
2218 && comp_types (TREE_TYPE (src), type))
2220 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2221 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2222 "argument to %<sizeof%> in %qD call is the same "
2223 "expression as the source; did you mean to "
2224 "remove the addressof?", callee);
2225 else if ((TYPE_PRECISION (TREE_TYPE (type))
2226 == TYPE_PRECISION (char_type_node))
2227 || strop)
2228 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2229 "argument to %<sizeof%> in %qD call is the same "
2230 "expression as the source; did you mean to "
2231 "provide an explicit length?", callee);
2232 else
2233 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2234 "argument to %<sizeof%> in %qD call is the same "
2235 "expression as the source; did you mean to "
2236 "dereference it?", callee);
2237 return;
2240 if (POINTER_TYPE_P (TREE_TYPE (src))
2241 && !strop
2242 && comp_types (TREE_TYPE (src), type)
2243 && !VOID_TYPE_P (TREE_TYPE (type)))
2245 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2246 "argument to %<sizeof%> in %qD call is the same "
2247 "pointer type %qT as the source; expected %qT "
2248 "or an explicit length", callee, TREE_TYPE (src),
2249 TREE_TYPE (TREE_TYPE (src)));
2250 return;
2254 if (dest)
2256 if (!TYPE_P (sizeof_arg[idx])
2257 && operand_equal_p (dest, sizeof_arg[idx], 0)
2258 && comp_types (TREE_TYPE (dest), type))
2260 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2261 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2262 "argument to %<sizeof%> in %qD call is the same "
2263 "expression as the first source; did you mean to "
2264 "remove the addressof?", callee);
2265 else if ((TYPE_PRECISION (TREE_TYPE (type))
2266 == TYPE_PRECISION (char_type_node))
2267 || strop)
2268 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2269 "argument to %<sizeof%> in %qD call is the same "
2270 "expression as the first source; did you mean to "
2271 "provide an explicit length?", callee);
2272 else
2273 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2274 "argument to %<sizeof%> in %qD call is the same "
2275 "expression as the first source; did you mean to "
2276 "dereference it?", callee);
2277 return;
2280 if (POINTER_TYPE_P (TREE_TYPE (dest))
2281 && !strop
2282 && comp_types (TREE_TYPE (dest), type)
2283 && !VOID_TYPE_P (TREE_TYPE (type)))
2285 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2286 "argument to %<sizeof%> in %qD call is the same "
2287 "pointer type %qT as the first source; expected %qT "
2288 "or an explicit length", callee, TREE_TYPE (dest),
2289 TREE_TYPE (TREE_TYPE (dest)));
2290 return;
2294 if (src)
2296 if (!TYPE_P (sizeof_arg[idx])
2297 && operand_equal_p (src, sizeof_arg[idx], 0)
2298 && comp_types (TREE_TYPE (src), type))
2300 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2301 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2302 "argument to %<sizeof%> in %qD call is the same "
2303 "expression as the second source; did you mean to "
2304 "remove the addressof?", callee);
2305 else if ((TYPE_PRECISION (TREE_TYPE (type))
2306 == TYPE_PRECISION (char_type_node))
2307 || strop)
2308 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2309 "argument to %<sizeof%> in %qD call is the same "
2310 "expression as the second source; did you mean to "
2311 "provide an explicit length?", callee);
2312 else
2313 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2314 "argument to %<sizeof%> in %qD call is the same "
2315 "expression as the second source; did you mean to "
2316 "dereference it?", callee);
2317 return;
2320 if (POINTER_TYPE_P (TREE_TYPE (src))
2321 && !strop
2322 && comp_types (TREE_TYPE (src), type)
2323 && !VOID_TYPE_P (TREE_TYPE (type)))
2325 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2326 "argument to %<sizeof%> in %qD call is the same "
2327 "pointer type %qT as the second source; expected %qT "
2328 "or an explicit length", callee, TREE_TYPE (src),
2329 TREE_TYPE (TREE_TYPE (src)));
2330 return;
2336 /* Warn for unlikely, improbable, or stupid DECL declarations
2337 of `main'. */
2339 void
2340 check_main_parameter_types (tree decl)
2342 function_args_iterator iter;
2343 tree type;
2344 int argct = 0;
2346 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2348 /* XXX void_type_node belies the abstraction. */
2349 if (type == void_type_node || type == error_mark_node )
2350 break;
2352 tree t = type;
2353 if (TYPE_ATOMIC (t))
2354 pedwarn (input_location, OPT_Wmain,
2355 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2356 type, decl);
2357 while (POINTER_TYPE_P (t))
2359 t = TREE_TYPE (t);
2360 if (TYPE_ATOMIC (t))
2361 pedwarn (input_location, OPT_Wmain,
2362 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2363 type, decl);
2366 ++argct;
2367 switch (argct)
2369 case 1:
2370 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2371 pedwarn (input_location, OPT_Wmain,
2372 "first argument of %q+D should be %<int%>", decl);
2373 break;
2375 case 2:
2376 if (TREE_CODE (type) != POINTER_TYPE
2377 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2378 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2379 != char_type_node))
2380 pedwarn (input_location, OPT_Wmain,
2381 "second argument of %q+D should be %<char **%>", decl);
2382 break;
2384 case 3:
2385 if (TREE_CODE (type) != POINTER_TYPE
2386 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2387 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2388 != char_type_node))
2389 pedwarn (input_location, OPT_Wmain,
2390 "third argument of %q+D should probably be "
2391 "%<char **%>", decl);
2392 break;
2396 /* It is intentional that this message does not mention the third
2397 argument because it's only mentioned in an appendix of the
2398 standard. */
2399 if (argct > 0 && (argct < 2 || argct > 3))
2400 pedwarn (input_location, OPT_Wmain,
2401 "%q+D takes only zero or two arguments", decl);
2403 if (stdarg_p (TREE_TYPE (decl)))
2404 pedwarn (input_location, OPT_Wmain,
2405 "%q+D declared as variadic function", decl);
2408 /* vector_targets_convertible_p is used for vector pointer types. The
2409 callers perform various checks that the qualifiers are satisfactory,
2410 while OTOH vector_targets_convertible_p ignores the number of elements
2411 in the vectors. That's fine with vector pointers as we can consider,
2412 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2413 and that does not require and conversion of the pointer values.
2414 In contrast, vector_types_convertible_p and
2415 vector_types_compatible_elements_p are used for vector value types. */
2416 /* True if pointers to distinct types T1 and T2 can be converted to
2417 each other without an explicit cast. Only returns true for opaque
2418 vector types. */
2419 bool
2420 vector_targets_convertible_p (const_tree t1, const_tree t2)
2422 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
2423 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2424 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2425 return true;
2427 return false;
2430 /* vector_types_convertible_p is used for vector value types.
2431 It could in principle call vector_targets_convertible_p as a subroutine,
2432 but then the check for vector type would be duplicated with its callers,
2433 and also the purpose of vector_targets_convertible_p would become
2434 muddled.
2435 Where vector_types_convertible_p returns true, a conversion might still be
2436 needed to make the types match.
2437 In contrast, vector_targets_convertible_p is used for vector pointer
2438 values, and vector_types_compatible_elements_p is used specifically
2439 in the context for binary operators, as a check if use is possible without
2440 conversion. */
2441 /* True if vector types T1 and T2 can be converted to each other
2442 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2443 can only be converted with -flax-vector-conversions yet that is not
2444 in effect, emit a note telling the user about that option if such
2445 a note has not previously been emitted. */
2446 bool
2447 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2449 static bool emitted_lax_note = false;
2450 bool convertible_lax;
2452 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2453 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2454 return true;
2456 convertible_lax =
2457 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2458 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2459 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2460 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2461 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2463 if (!convertible_lax || flag_lax_vector_conversions)
2464 return convertible_lax;
2466 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2467 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2468 return true;
2470 if (emit_lax_note && !emitted_lax_note)
2472 emitted_lax_note = true;
2473 inform (input_location, "use -flax-vector-conversions to permit "
2474 "conversions between vectors with differing "
2475 "element types or numbers of subparts");
2478 return false;
2481 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2482 and have vector types, V0 has the same type as V1, and the number of
2483 elements of V0, V1, MASK is the same.
2485 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2486 called with two arguments. In this case implementation passes the
2487 first argument twice in order to share the same tree code. This fact
2488 could enable the mask-values being twice the vector length. This is
2489 an implementation accident and this semantics is not guaranteed to
2490 the user. */
2491 tree
2492 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2493 bool complain)
2495 tree ret;
2496 bool wrap = true;
2497 bool maybe_const = false;
2498 bool two_arguments = false;
2500 if (v1 == NULL_TREE)
2502 two_arguments = true;
2503 v1 = v0;
2506 if (v0 == error_mark_node || v1 == error_mark_node
2507 || mask == error_mark_node)
2508 return error_mark_node;
2510 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2511 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2513 if (complain)
2514 error_at (loc, "__builtin_shuffle last argument must "
2515 "be an integer vector");
2516 return error_mark_node;
2519 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2520 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2522 if (complain)
2523 error_at (loc, "__builtin_shuffle arguments must be vectors");
2524 return error_mark_node;
2527 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2529 if (complain)
2530 error_at (loc, "__builtin_shuffle argument vectors must be of "
2531 "the same type");
2532 return error_mark_node;
2535 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2536 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2537 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2538 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2540 if (complain)
2541 error_at (loc, "__builtin_shuffle number of elements of the "
2542 "argument vector(s) and the mask vector should "
2543 "be the same");
2544 return error_mark_node;
2547 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2548 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2550 if (complain)
2551 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2552 "must have the same size as inner type of the mask");
2553 return error_mark_node;
2556 if (!c_dialect_cxx ())
2558 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2559 v0 = c_fully_fold (v0, false, &maybe_const);
2560 wrap &= maybe_const;
2562 if (two_arguments)
2563 v1 = v0 = save_expr (v0);
2564 else
2566 v1 = c_fully_fold (v1, false, &maybe_const);
2567 wrap &= maybe_const;
2570 mask = c_fully_fold (mask, false, &maybe_const);
2571 wrap &= maybe_const;
2573 else if (two_arguments)
2574 v1 = v0 = save_expr (v0);
2576 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2578 if (!c_dialect_cxx () && !wrap)
2579 ret = c_wrap_maybe_const (ret, true);
2581 return ret;
2584 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2585 to integral type. */
2587 static tree
2588 c_common_get_narrower (tree op, int *unsignedp_ptr)
2590 op = get_narrower (op, unsignedp_ptr);
2592 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2593 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2595 /* C++0x scoped enumerations don't implicitly convert to integral
2596 type; if we stripped an explicit conversion to a larger type we
2597 need to replace it so common_type will still work. */
2598 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2599 TYPE_UNSIGNED (TREE_TYPE (op)));
2600 op = fold_convert (type, op);
2602 return op;
2605 /* This is a helper function of build_binary_op.
2607 For certain operations if both args were extended from the same
2608 smaller type, do the arithmetic in that type and then extend.
2610 BITWISE indicates a bitwise operation.
2611 For them, this optimization is safe only if
2612 both args are zero-extended or both are sign-extended.
2613 Otherwise, we might change the result.
2614 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2615 but calculated in (unsigned short) it would be (unsigned short)-1.
2617 tree
2618 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2620 int unsigned0, unsigned1;
2621 tree arg0, arg1;
2622 int uns;
2623 tree type;
2625 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2626 excessive narrowing when we call get_narrower below. For
2627 example, suppose that OP0 is of unsigned int extended
2628 from signed char and that RESULT_TYPE is long long int.
2629 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2630 like
2632 (long long int) (unsigned int) signed_char
2634 which get_narrower would narrow down to
2636 (unsigned int) signed char
2638 If we do not cast OP0 first, get_narrower would return
2639 signed_char, which is inconsistent with the case of the
2640 explicit cast. */
2641 op0 = convert (result_type, op0);
2642 op1 = convert (result_type, op1);
2644 arg0 = c_common_get_narrower (op0, &unsigned0);
2645 arg1 = c_common_get_narrower (op1, &unsigned1);
2647 /* UNS is 1 if the operation to be done is an unsigned one. */
2648 uns = TYPE_UNSIGNED (result_type);
2650 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2651 but it *requires* conversion to FINAL_TYPE. */
2653 if ((TYPE_PRECISION (TREE_TYPE (op0))
2654 == TYPE_PRECISION (TREE_TYPE (arg0)))
2655 && TREE_TYPE (op0) != result_type)
2656 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2657 if ((TYPE_PRECISION (TREE_TYPE (op1))
2658 == TYPE_PRECISION (TREE_TYPE (arg1)))
2659 && TREE_TYPE (op1) != result_type)
2660 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2662 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2664 /* For bitwise operations, signedness of nominal type
2665 does not matter. Consider only how operands were extended. */
2666 if (bitwise)
2667 uns = unsigned0;
2669 /* Note that in all three cases below we refrain from optimizing
2670 an unsigned operation on sign-extended args.
2671 That would not be valid. */
2673 /* Both args variable: if both extended in same way
2674 from same width, do it in that width.
2675 Do it unsigned if args were zero-extended. */
2676 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2677 < TYPE_PRECISION (result_type))
2678 && (TYPE_PRECISION (TREE_TYPE (arg1))
2679 == TYPE_PRECISION (TREE_TYPE (arg0)))
2680 && unsigned0 == unsigned1
2681 && (unsigned0 || !uns))
2682 return c_common_signed_or_unsigned_type
2683 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2685 else if (TREE_CODE (arg0) == INTEGER_CST
2686 && (unsigned1 || !uns)
2687 && (TYPE_PRECISION (TREE_TYPE (arg1))
2688 < TYPE_PRECISION (result_type))
2689 && (type
2690 = c_common_signed_or_unsigned_type (unsigned1,
2691 TREE_TYPE (arg1)))
2692 && !POINTER_TYPE_P (type)
2693 && int_fits_type_p (arg0, type))
2694 return type;
2696 else if (TREE_CODE (arg1) == INTEGER_CST
2697 && (unsigned0 || !uns)
2698 && (TYPE_PRECISION (TREE_TYPE (arg0))
2699 < TYPE_PRECISION (result_type))
2700 && (type
2701 = c_common_signed_or_unsigned_type (unsigned0,
2702 TREE_TYPE (arg0)))
2703 && !POINTER_TYPE_P (type)
2704 && int_fits_type_p (arg1, type))
2705 return type;
2707 return result_type;
2710 /* Returns true iff any integer value of type FROM_TYPE can be represented as
2711 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2713 static bool
2714 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2716 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2717 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2718 REAL_VALUE_TYPE real_low_bound =
2719 real_value_from_int_cst (0, type_low_bound);
2720 REAL_VALUE_TYPE real_high_bound =
2721 real_value_from_int_cst (0, type_high_bound);
2723 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2724 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2727 /* Checks if expression EXPR of complex/real/integer type cannot be converted
2728 to the complex/real/integer type TYPE. Function returns non-zero when:
2729 * EXPR is a constant which cannot be exactly converted to TYPE.
2730 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2731 for EXPR type and TYPE being both integers or both real, or both
2732 complex.
2733 * EXPR is not a constant of complex type and TYPE is a real or
2734 an integer.
2735 * EXPR is not a constant of real type and TYPE is an integer.
2736 * EXPR is not a constant of integer type which cannot be
2737 exactly converted to real type.
2739 Function allows conversions between types of different signedness and
2740 can return SAFE_CONVERSION (zero) in that case. Function can produce
2741 signedness warnings if PRODUCE_WARNS is true.
2743 Function allows conversions from complex constants to non-complex types,
2744 provided that imaginary part is zero and real part can be safely converted
2745 to TYPE. */
2747 enum conversion_safety
2748 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2750 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2751 tree expr_type = TREE_TYPE (expr);
2752 loc = expansion_point_location_if_in_system_header (loc);
2754 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2756 /* If type is complex, we are interested in compatibility with
2757 underlying type. */
2758 if (TREE_CODE (type) == COMPLEX_TYPE)
2759 type = TREE_TYPE (type);
2761 /* Warn for real constant that is not an exact integer converted
2762 to integer type. */
2763 if (TREE_CODE (expr_type) == REAL_TYPE
2764 && TREE_CODE (type) == INTEGER_TYPE)
2766 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2767 give_warning = UNSAFE_REAL;
2769 /* Warn for an integer constant that does not fit into integer type. */
2770 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2771 && TREE_CODE (type) == INTEGER_TYPE
2772 && !int_fits_type_p (expr, type))
2774 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2775 && tree_int_cst_sgn (expr) < 0)
2777 if (produce_warns)
2778 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2779 " implicitly converted to unsigned type");
2781 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2783 if (produce_warns)
2784 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2785 " constant value to negative integer");
2787 else
2788 give_warning = UNSAFE_OTHER;
2790 else if (TREE_CODE (type) == REAL_TYPE)
2792 /* Warn for an integer constant that does not fit into real type. */
2793 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2795 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2796 if (!exact_real_truncate (TYPE_MODE (type), &a))
2797 give_warning = UNSAFE_REAL;
2799 /* Warn for a real constant that does not fit into a smaller
2800 real type. */
2801 else if (TREE_CODE (expr_type) == REAL_TYPE
2802 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2804 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2805 if (!exact_real_truncate (TYPE_MODE (type), &a))
2806 give_warning = UNSAFE_REAL;
2811 else if (TREE_CODE (expr) == COMPLEX_CST)
2813 tree imag_part = TREE_IMAGPART (expr);
2814 /* Conversion from complex constant with zero imaginary part,
2815 perform check for conversion of real part. */
2816 if ((TREE_CODE (imag_part) == REAL_CST
2817 && real_zerop (imag_part))
2818 || (TREE_CODE (imag_part) == INTEGER_CST
2819 && integer_zerop (imag_part)))
2820 /* Note: in this branch we use recursive call to unsafe_conversion_p
2821 with different type of EXPR, but it is still safe, because when EXPR
2822 is a constant, it's type is not used in text of generated warnings
2823 (otherwise they could sound misleading). */
2824 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2825 produce_warns);
2826 /* Conversion from complex constant with non-zero imaginary part. */
2827 else
2829 /* Conversion to complex type.
2830 Perform checks for both real and imaginary parts. */
2831 if (TREE_CODE (type) == COMPLEX_TYPE)
2833 /* Unfortunately, produce_warns must be false in two subsequent
2834 calls of unsafe_conversion_p, because otherwise we could
2835 produce strange "double" warnings, if both real and imaginary
2836 parts have conversion problems related to signedness.
2838 For example:
2839 int32_t _Complex a = 0x80000000 + 0x80000000i;
2841 Possible solution: add a separate function for checking
2842 constants and combine result of two calls appropriately. */
2843 enum conversion_safety re_safety =
2844 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2845 enum conversion_safety im_safety =
2846 unsafe_conversion_p (loc, type, imag_part, false);
2848 /* Merge the results into appropriate single warning. */
2850 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2851 if (re_safety == im_safety)
2852 give_warning = re_safety;
2853 else if (!re_safety && im_safety)
2854 give_warning = im_safety;
2855 else if (re_safety && !im_safety)
2856 give_warning = re_safety;
2857 else
2858 give_warning = UNSAFE_OTHER;
2860 /* Warn about conversion from complex to real or integer type. */
2861 else
2862 give_warning = UNSAFE_IMAGINARY;
2866 /* Checks for remaining case: EXPR is not constant. */
2867 else
2869 /* Warn for real types converted to integer types. */
2870 if (TREE_CODE (expr_type) == REAL_TYPE
2871 && TREE_CODE (type) == INTEGER_TYPE)
2872 give_warning = UNSAFE_REAL;
2874 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2875 && TREE_CODE (type) == INTEGER_TYPE)
2877 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2878 expr = get_unwidened (expr, 0);
2879 expr_type = TREE_TYPE (expr);
2881 /* Don't warn for short y; short x = ((int)y & 0xff); */
2882 if (TREE_CODE (expr) == BIT_AND_EXPR
2883 || TREE_CODE (expr) == BIT_IOR_EXPR
2884 || TREE_CODE (expr) == BIT_XOR_EXPR)
2886 /* If both args were extended from a shortest type,
2887 use that type if that is safe. */
2888 expr_type = shorten_binary_op (expr_type,
2889 TREE_OPERAND (expr, 0),
2890 TREE_OPERAND (expr, 1),
2891 /* bitwise */1);
2893 if (TREE_CODE (expr) == BIT_AND_EXPR)
2895 tree op0 = TREE_OPERAND (expr, 0);
2896 tree op1 = TREE_OPERAND (expr, 1);
2897 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2898 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2900 /* If one of the operands is a non-negative constant
2901 that fits in the target type, then the type of the
2902 other operand does not matter. */
2903 if ((TREE_CODE (op0) == INTEGER_CST
2904 && int_fits_type_p (op0, c_common_signed_type (type))
2905 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2906 || (TREE_CODE (op1) == INTEGER_CST
2907 && int_fits_type_p (op1, c_common_signed_type (type))
2908 && int_fits_type_p (op1,
2909 c_common_unsigned_type (type))))
2910 return SAFE_CONVERSION;
2911 /* If constant is unsigned and fits in the target
2912 type, then the result will also fit. */
2913 else if ((TREE_CODE (op0) == INTEGER_CST
2914 && unsigned0
2915 && int_fits_type_p (op0, type))
2916 || (TREE_CODE (op1) == INTEGER_CST
2917 && unsigned1
2918 && int_fits_type_p (op1, type)))
2919 return SAFE_CONVERSION;
2922 /* Warn for integer types converted to smaller integer types. */
2923 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2924 give_warning = UNSAFE_OTHER;
2926 /* When they are the same width but different signedness,
2927 then the value may change. */
2928 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2929 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2930 /* Even when converted to a bigger type, if the type is
2931 unsigned but expr is signed, then negative values
2932 will be changed. */
2933 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2934 && produce_warns)
2935 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2936 "may change the sign of the result",
2937 type, expr_type);
2940 /* Warn for integer types converted to real types if and only if
2941 all the range of values of the integer type cannot be
2942 represented by the real type. */
2943 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2944 && TREE_CODE (type) == REAL_TYPE)
2946 /* Don't warn about char y = 0xff; float x = (int) y; */
2947 expr = get_unwidened (expr, 0);
2948 expr_type = TREE_TYPE (expr);
2950 if (!int_safely_convertible_to_real_p (expr_type, type))
2951 give_warning = UNSAFE_OTHER;
2954 /* Warn for real types converted to smaller real types. */
2955 else if (TREE_CODE (expr_type) == REAL_TYPE
2956 && TREE_CODE (type) == REAL_TYPE
2957 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2958 give_warning = UNSAFE_REAL;
2960 /* Check conversion between two complex types. */
2961 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2962 && TREE_CODE (type) == COMPLEX_TYPE)
2964 /* Extract underlying types (i.e., type of real and imaginary
2965 parts) of expr_type and type. */
2966 tree from_type = TREE_TYPE (expr_type);
2967 tree to_type = TREE_TYPE (type);
2969 /* Warn for real types converted to integer types. */
2970 if (TREE_CODE (from_type) == REAL_TYPE
2971 && TREE_CODE (to_type) == INTEGER_TYPE)
2972 give_warning = UNSAFE_REAL;
2974 /* Warn for real types converted to smaller real types. */
2975 else if (TREE_CODE (from_type) == REAL_TYPE
2976 && TREE_CODE (to_type) == REAL_TYPE
2977 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2978 give_warning = UNSAFE_REAL;
2980 /* Check conversion for complex integer types. Here implementation
2981 is simpler than for real-domain integers because it does not
2982 involve sophisticated cases, such as bitmasks, casts, etc. */
2983 else if (TREE_CODE (from_type) == INTEGER_TYPE
2984 && TREE_CODE (to_type) == INTEGER_TYPE)
2986 /* Warn for integer types converted to smaller integer types. */
2987 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2988 give_warning = UNSAFE_OTHER;
2990 /* Check for different signedness, see case for real-domain
2991 integers (above) for a more detailed comment. */
2992 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2993 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2994 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2995 && produce_warns)
2996 warning_at (loc, OPT_Wsign_conversion,
2997 "conversion to %qT from %qT "
2998 "may change the sign of the result",
2999 type, expr_type);
3001 else if (TREE_CODE (from_type) == INTEGER_TYPE
3002 && TREE_CODE (to_type) == REAL_TYPE
3003 && !int_safely_convertible_to_real_p (from_type, to_type))
3004 give_warning = UNSAFE_OTHER;
3007 /* Warn for complex types converted to real or integer types. */
3008 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3009 && TREE_CODE (type) != COMPLEX_TYPE)
3010 give_warning = UNSAFE_IMAGINARY;
3013 return give_warning;
3016 /* Warns if the conversion of EXPR to TYPE may alter a value.
3017 This is a helper function for warnings_for_convert_and_check. */
3019 static void
3020 conversion_warning (location_t loc, tree type, tree expr)
3022 tree expr_type = TREE_TYPE (expr);
3023 enum conversion_safety conversion_kind;
3025 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
3026 return;
3028 /* This may happen, because for LHS op= RHS we preevaluate
3029 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3030 means we could no longer see the code of the EXPR. */
3031 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3032 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3033 if (TREE_CODE (expr) == SAVE_EXPR)
3034 expr = TREE_OPERAND (expr, 0);
3036 switch (TREE_CODE (expr))
3038 case EQ_EXPR:
3039 case NE_EXPR:
3040 case LE_EXPR:
3041 case GE_EXPR:
3042 case LT_EXPR:
3043 case GT_EXPR:
3044 case TRUTH_ANDIF_EXPR:
3045 case TRUTH_ORIF_EXPR:
3046 case TRUTH_AND_EXPR:
3047 case TRUTH_OR_EXPR:
3048 case TRUTH_XOR_EXPR:
3049 case TRUTH_NOT_EXPR:
3050 /* Conversion from boolean to a signed:1 bit-field (which only
3051 can hold the values 0 and -1) doesn't lose information - but
3052 it does change the value. */
3053 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3054 warning_at (loc, OPT_Wconversion,
3055 "conversion to %qT from boolean expression", type);
3056 return;
3058 case REAL_CST:
3059 case INTEGER_CST:
3060 case COMPLEX_CST:
3061 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
3062 if (conversion_kind == UNSAFE_REAL)
3063 warning_at (loc, OPT_Wfloat_conversion,
3064 "conversion to %qT alters %qT constant value",
3065 type, expr_type);
3066 else if (conversion_kind)
3067 warning_at (loc, OPT_Wconversion,
3068 "conversion to %qT alters %qT constant value",
3069 type, expr_type);
3070 return;
3072 case COND_EXPR:
3074 /* In case of COND_EXPR, we do not care about the type of
3075 COND_EXPR, only about the conversion of each operand. */
3076 tree op1 = TREE_OPERAND (expr, 1);
3077 tree op2 = TREE_OPERAND (expr, 2);
3079 conversion_warning (loc, type, op1);
3080 conversion_warning (loc, type, op2);
3081 return;
3084 default: /* 'expr' is not a constant. */
3085 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
3086 if (conversion_kind == UNSAFE_REAL)
3087 warning_at (loc, OPT_Wfloat_conversion,
3088 "conversion to %qT from %qT may alter its value",
3089 type, expr_type);
3090 else if (conversion_kind == UNSAFE_IMAGINARY)
3091 warning_at (loc, OPT_Wconversion,
3092 "conversion to %qT from %qT discards imaginary component",
3093 type, expr_type);
3094 else if (conversion_kind)
3095 warning_at (loc, OPT_Wconversion,
3096 "conversion to %qT from %qT may alter its value",
3097 type, expr_type);
3101 /* Produce warnings after a conversion. RESULT is the result of
3102 converting EXPR to TYPE. This is a helper function for
3103 convert_and_check and cp_convert_and_check. */
3105 void
3106 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3107 tree result)
3109 loc = expansion_point_location_if_in_system_header (loc);
3111 if (TREE_CODE (expr) == INTEGER_CST
3112 && (TREE_CODE (type) == INTEGER_TYPE
3113 || TREE_CODE (type) == ENUMERAL_TYPE)
3114 && !int_fits_type_p (expr, type))
3116 /* Do not diagnose overflow in a constant expression merely
3117 because a conversion overflowed. */
3118 if (TREE_OVERFLOW (result))
3119 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3121 if (TYPE_UNSIGNED (type))
3123 /* This detects cases like converting -129 or 256 to
3124 unsigned char. */
3125 if (!int_fits_type_p (expr, c_common_signed_type (type)))
3126 warning_at (loc, OPT_Woverflow,
3127 "large integer implicitly truncated to unsigned type");
3128 else
3129 conversion_warning (loc, type, expr);
3131 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
3132 warning_at (loc, OPT_Woverflow,
3133 "overflow in implicit constant conversion");
3134 /* No warning for converting 0x80000000 to int. */
3135 else if (pedantic
3136 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3137 || TYPE_PRECISION (TREE_TYPE (expr))
3138 != TYPE_PRECISION (type)))
3139 warning_at (loc, OPT_Woverflow,
3140 "overflow in implicit constant conversion");
3142 else
3143 conversion_warning (loc, type, expr);
3145 else if ((TREE_CODE (result) == INTEGER_CST
3146 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
3147 warning_at (loc, OPT_Woverflow,
3148 "overflow in implicit constant conversion");
3149 else
3150 conversion_warning (loc, type, expr);
3154 /* Convert EXPR to TYPE, warning about conversion problems with constants.
3155 Invoke this function on every expression that is converted implicitly,
3156 i.e. because of language rules and not because of an explicit cast. */
3158 tree
3159 convert_and_check (location_t loc, tree type, tree expr)
3161 tree result;
3162 tree expr_for_warning;
3164 /* Convert from a value with possible excess precision rather than
3165 via the semantic type, but do not warn about values not fitting
3166 exactly in the semantic type. */
3167 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3169 tree orig_type = TREE_TYPE (expr);
3170 expr = TREE_OPERAND (expr, 0);
3171 expr_for_warning = convert (orig_type, expr);
3172 if (orig_type == type)
3173 return expr_for_warning;
3175 else
3176 expr_for_warning = expr;
3178 if (TREE_TYPE (expr) == type)
3179 return expr;
3181 result = convert (type, expr);
3183 if (c_inhibit_evaluation_warnings == 0
3184 && !TREE_OVERFLOW_P (expr)
3185 && result != error_mark_node)
3186 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
3188 return result;
3191 /* A node in a list that describes references to variables (EXPR), which are
3192 either read accesses if WRITER is zero, or write accesses, in which case
3193 WRITER is the parent of EXPR. */
3194 struct tlist
3196 struct tlist *next;
3197 tree expr, writer;
3200 /* Used to implement a cache the results of a call to verify_tree. We only
3201 use this for SAVE_EXPRs. */
3202 struct tlist_cache
3204 struct tlist_cache *next;
3205 struct tlist *cache_before_sp;
3206 struct tlist *cache_after_sp;
3207 tree expr;
3210 /* Obstack to use when allocating tlist structures, and corresponding
3211 firstobj. */
3212 static struct obstack tlist_obstack;
3213 static char *tlist_firstobj = 0;
3215 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
3216 warnings. */
3217 static struct tlist *warned_ids;
3218 /* SAVE_EXPRs need special treatment. We process them only once and then
3219 cache the results. */
3220 static struct tlist_cache *save_expr_cache;
3222 static void add_tlist (struct tlist **, struct tlist *, tree, int);
3223 static void merge_tlist (struct tlist **, struct tlist *, int);
3224 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3225 static int warning_candidate_p (tree);
3226 static bool candidate_equal_p (const_tree, const_tree);
3227 static void warn_for_collisions (struct tlist *);
3228 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3229 static struct tlist *new_tlist (struct tlist *, tree, tree);
3231 /* Create a new struct tlist and fill in its fields. */
3232 static struct tlist *
3233 new_tlist (struct tlist *next, tree t, tree writer)
3235 struct tlist *l;
3236 l = XOBNEW (&tlist_obstack, struct tlist);
3237 l->next = next;
3238 l->expr = t;
3239 l->writer = writer;
3240 return l;
3243 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3244 is nonnull, we ignore any node we find which has a writer equal to it. */
3246 static void
3247 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
3249 while (add)
3251 struct tlist *next = add->next;
3252 if (!copy)
3253 add->next = *to;
3254 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
3255 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3256 add = next;
3260 /* Merge the nodes of ADD into TO. This merging process is done so that for
3261 each variable that already exists in TO, no new node is added; however if
3262 there is a write access recorded in ADD, and an occurrence on TO is only
3263 a read access, then the occurrence in TO will be modified to record the
3264 write. */
3266 static void
3267 merge_tlist (struct tlist **to, struct tlist *add, int copy)
3269 struct tlist **end = to;
3271 while (*end)
3272 end = &(*end)->next;
3274 while (add)
3276 int found = 0;
3277 struct tlist *tmp2;
3278 struct tlist *next = add->next;
3280 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
3281 if (candidate_equal_p (tmp2->expr, add->expr))
3283 found = 1;
3284 if (!tmp2->writer)
3285 tmp2->writer = add->writer;
3287 if (!found)
3289 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
3290 end = &(*end)->next;
3291 *end = 0;
3293 add = next;
3297 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3298 references in list LIST conflict with it, excluding reads if ONLY writers
3299 is nonzero. */
3301 static void
3302 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3303 int only_writes)
3305 struct tlist *tmp;
3307 /* Avoid duplicate warnings. */
3308 for (tmp = warned_ids; tmp; tmp = tmp->next)
3309 if (candidate_equal_p (tmp->expr, written))
3310 return;
3312 while (list)
3314 if (candidate_equal_p (list->expr, written)
3315 && !candidate_equal_p (list->writer, writer)
3316 && (!only_writes || list->writer))
3318 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3319 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
3320 OPT_Wsequence_point, "operation on %qE may be undefined",
3321 list->expr);
3323 list = list->next;
3327 /* Given a list LIST of references to variables, find whether any of these
3328 can cause conflicts due to missing sequence points. */
3330 static void
3331 warn_for_collisions (struct tlist *list)
3333 struct tlist *tmp;
3335 for (tmp = list; tmp; tmp = tmp->next)
3337 if (tmp->writer)
3338 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3342 /* Return nonzero if X is a tree that can be verified by the sequence point
3343 warnings. */
3344 static int
3345 warning_candidate_p (tree x)
3347 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3348 return 0;
3350 if (TREE_CODE (x) == BLOCK)
3351 return 0;
3353 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3354 (lvalue_p) crash on TRY/CATCH. */
3355 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3356 return 0;
3358 if (!lvalue_p (x))
3359 return 0;
3361 /* No point to track non-const calls, they will never satisfy
3362 operand_equal_p. */
3363 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3364 return 0;
3366 if (TREE_CODE (x) == STRING_CST)
3367 return 0;
3369 return 1;
3372 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3373 static bool
3374 candidate_equal_p (const_tree x, const_tree y)
3376 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3379 /* Walk the tree X, and record accesses to variables. If X is written by the
3380 parent tree, WRITER is the parent.
3381 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3382 expression or its only operand forces a sequence point, then everything up
3383 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3384 in PNO_SP.
3385 Once we return, we will have emitted warnings if any subexpression before
3386 such a sequence point could be undefined. On a higher level, however, the
3387 sequence point may not be relevant, and we'll merge the two lists.
3389 Example: (b++, a) + b;
3390 The call that processes the COMPOUND_EXPR will store the increment of B
3391 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3392 processes the PLUS_EXPR will need to merge the two lists so that
3393 eventually, all accesses end up on the same list (and we'll warn about the
3394 unordered subexpressions b++ and b.
3396 A note on merging. If we modify the former example so that our expression
3397 becomes
3398 (b++, b) + a
3399 care must be taken not simply to add all three expressions into the final
3400 PNO_SP list. The function merge_tlist takes care of that by merging the
3401 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3402 way, so that no more than one access to B is recorded. */
3404 static void
3405 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3406 tree writer)
3408 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3409 enum tree_code code;
3410 enum tree_code_class cl;
3412 /* X may be NULL if it is the operand of an empty statement expression
3413 ({ }). */
3414 if (x == NULL)
3415 return;
3417 restart:
3418 code = TREE_CODE (x);
3419 cl = TREE_CODE_CLASS (code);
3421 if (warning_candidate_p (x))
3422 *pno_sp = new_tlist (*pno_sp, x, writer);
3424 switch (code)
3426 case CONSTRUCTOR:
3427 case SIZEOF_EXPR:
3428 return;
3430 case COMPOUND_EXPR:
3431 case TRUTH_ANDIF_EXPR:
3432 case TRUTH_ORIF_EXPR:
3433 tmp_before = tmp_nosp = tmp_list3 = 0;
3434 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3435 warn_for_collisions (tmp_nosp);
3436 merge_tlist (pbefore_sp, tmp_before, 0);
3437 merge_tlist (pbefore_sp, tmp_nosp, 0);
3438 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3439 merge_tlist (pbefore_sp, tmp_list3, 0);
3440 return;
3442 case COND_EXPR:
3443 tmp_before = tmp_list2 = 0;
3444 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3445 warn_for_collisions (tmp_list2);
3446 merge_tlist (pbefore_sp, tmp_before, 0);
3447 merge_tlist (pbefore_sp, tmp_list2, 0);
3449 tmp_list3 = tmp_nosp = 0;
3450 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3451 warn_for_collisions (tmp_nosp);
3452 merge_tlist (pbefore_sp, tmp_list3, 0);
3454 tmp_list3 = tmp_list2 = 0;
3455 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3456 warn_for_collisions (tmp_list2);
3457 merge_tlist (pbefore_sp, tmp_list3, 0);
3458 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3459 two first, to avoid warning for (a ? b++ : b++). */
3460 merge_tlist (&tmp_nosp, tmp_list2, 0);
3461 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3462 return;
3464 case PREDECREMENT_EXPR:
3465 case PREINCREMENT_EXPR:
3466 case POSTDECREMENT_EXPR:
3467 case POSTINCREMENT_EXPR:
3468 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3469 return;
3471 case MODIFY_EXPR:
3472 tmp_before = tmp_nosp = tmp_list3 = 0;
3473 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3474 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3475 /* Expressions inside the LHS are not ordered wrt. the sequence points
3476 in the RHS. Example:
3477 *a = (a++, 2)
3478 Despite the fact that the modification of "a" is in the before_sp
3479 list (tmp_before), it conflicts with the use of "a" in the LHS.
3480 We can handle this by adding the contents of tmp_list3
3481 to those of tmp_before, and redoing the collision warnings for that
3482 list. */
3483 add_tlist (&tmp_before, tmp_list3, x, 1);
3484 warn_for_collisions (tmp_before);
3485 /* Exclude the LHS itself here; we first have to merge it into the
3486 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3487 didn't exclude the LHS, we'd get it twice, once as a read and once
3488 as a write. */
3489 add_tlist (pno_sp, tmp_list3, x, 0);
3490 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3492 merge_tlist (pbefore_sp, tmp_before, 0);
3493 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3494 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3495 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3496 return;
3498 case CALL_EXPR:
3499 /* We need to warn about conflicts among arguments and conflicts between
3500 args and the function address. Side effects of the function address,
3501 however, are not ordered by the sequence point of the call. */
3503 call_expr_arg_iterator iter;
3504 tree arg;
3505 tmp_before = tmp_nosp = 0;
3506 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3507 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3509 tmp_list2 = tmp_list3 = 0;
3510 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3511 merge_tlist (&tmp_list3, tmp_list2, 0);
3512 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3514 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3515 warn_for_collisions (tmp_before);
3516 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3517 return;
3520 case TREE_LIST:
3521 /* Scan all the list, e.g. indices of multi dimensional array. */
3522 while (x)
3524 tmp_before = tmp_nosp = 0;
3525 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3526 merge_tlist (&tmp_nosp, tmp_before, 0);
3527 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3528 x = TREE_CHAIN (x);
3530 return;
3532 case SAVE_EXPR:
3534 struct tlist_cache *t;
3535 for (t = save_expr_cache; t; t = t->next)
3536 if (candidate_equal_p (t->expr, x))
3537 break;
3539 if (!t)
3541 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3542 t->next = save_expr_cache;
3543 t->expr = x;
3544 save_expr_cache = t;
3546 tmp_before = tmp_nosp = 0;
3547 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3548 warn_for_collisions (tmp_nosp);
3550 tmp_list3 = 0;
3551 merge_tlist (&tmp_list3, tmp_nosp, 0);
3552 t->cache_before_sp = tmp_before;
3553 t->cache_after_sp = tmp_list3;
3555 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3556 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3557 return;
3560 case ADDR_EXPR:
3561 x = TREE_OPERAND (x, 0);
3562 if (DECL_P (x))
3563 return;
3564 writer = 0;
3565 goto restart;
3567 default:
3568 /* For other expressions, simply recurse on their operands.
3569 Manual tail recursion for unary expressions.
3570 Other non-expressions need not be processed. */
3571 if (cl == tcc_unary)
3573 x = TREE_OPERAND (x, 0);
3574 writer = 0;
3575 goto restart;
3577 else if (IS_EXPR_CODE_CLASS (cl))
3579 int lp;
3580 int max = TREE_OPERAND_LENGTH (x);
3581 for (lp = 0; lp < max; lp++)
3583 tmp_before = tmp_nosp = 0;
3584 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3585 merge_tlist (&tmp_nosp, tmp_before, 0);
3586 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3589 return;
3593 /* Try to warn for undefined behavior in EXPR due to missing sequence
3594 points. */
3596 DEBUG_FUNCTION void
3597 verify_sequence_points (tree expr)
3599 struct tlist *before_sp = 0, *after_sp = 0;
3601 warned_ids = 0;
3602 save_expr_cache = 0;
3603 if (tlist_firstobj == 0)
3605 gcc_obstack_init (&tlist_obstack);
3606 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3609 verify_tree (expr, &before_sp, &after_sp, 0);
3610 warn_for_collisions (after_sp);
3611 obstack_free (&tlist_obstack, tlist_firstobj);
3614 /* Validate the expression after `case' and apply default promotions. */
3616 static tree
3617 check_case_value (location_t loc, tree value)
3619 if (value == NULL_TREE)
3620 return value;
3622 if (TREE_CODE (value) == INTEGER_CST)
3623 /* Promote char or short to int. */
3624 value = perform_integral_promotions (value);
3625 else if (value != error_mark_node)
3627 error_at (loc, "case label does not reduce to an integer constant");
3628 value = error_mark_node;
3631 constant_expression_warning (value);
3633 return value;
3636 /* See if the case values LOW and HIGH are in the range of the original
3637 type (i.e. before the default conversion to int) of the switch testing
3638 expression.
3639 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3640 the type before promoting it. CASE_LOW_P is a pointer to the lower
3641 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3642 if the case is not a case range.
3643 The caller has to make sure that we are not called with NULL for
3644 CASE_LOW_P (i.e. the default case).
3645 Returns true if the case label is in range of ORIG_TYPE (saturated or
3646 untouched) or false if the label is out of range. */
3648 static bool
3649 check_case_bounds (location_t loc, tree type, tree orig_type,
3650 tree *case_low_p, tree *case_high_p)
3652 tree min_value, max_value;
3653 tree case_low = *case_low_p;
3654 tree case_high = case_high_p ? *case_high_p : case_low;
3656 /* If there was a problem with the original type, do nothing. */
3657 if (orig_type == error_mark_node)
3658 return true;
3660 min_value = TYPE_MIN_VALUE (orig_type);
3661 max_value = TYPE_MAX_VALUE (orig_type);
3663 /* Case label is less than minimum for type. */
3664 if (tree_int_cst_compare (case_low, min_value) < 0
3665 && tree_int_cst_compare (case_high, min_value) < 0)
3667 warning_at (loc, 0, "case label value is less than minimum value "
3668 "for type");
3669 return false;
3672 /* Case value is greater than maximum for type. */
3673 if (tree_int_cst_compare (case_low, max_value) > 0
3674 && tree_int_cst_compare (case_high, max_value) > 0)
3676 warning_at (loc, 0, "case label value exceeds maximum value for type");
3677 return false;
3680 /* Saturate lower case label value to minimum. */
3681 if (tree_int_cst_compare (case_high, min_value) >= 0
3682 && tree_int_cst_compare (case_low, min_value) < 0)
3684 warning_at (loc, 0, "lower value in case label range"
3685 " less than minimum value for type");
3686 case_low = min_value;
3689 /* Saturate upper case label value to maximum. */
3690 if (tree_int_cst_compare (case_low, max_value) <= 0
3691 && tree_int_cst_compare (case_high, max_value) > 0)
3693 warning_at (loc, 0, "upper value in case label range"
3694 " exceeds maximum value for type");
3695 case_high = max_value;
3698 if (*case_low_p != case_low)
3699 *case_low_p = convert (type, case_low);
3700 if (case_high_p && *case_high_p != case_high)
3701 *case_high_p = convert (type, case_high);
3703 return true;
3706 /* Return an integer type with BITS bits of precision,
3707 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3709 tree
3710 c_common_type_for_size (unsigned int bits, int unsignedp)
3712 int i;
3714 if (bits == TYPE_PRECISION (integer_type_node))
3715 return unsignedp ? unsigned_type_node : integer_type_node;
3717 if (bits == TYPE_PRECISION (signed_char_type_node))
3718 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3720 if (bits == TYPE_PRECISION (short_integer_type_node))
3721 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3723 if (bits == TYPE_PRECISION (long_integer_type_node))
3724 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3726 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3727 return (unsignedp ? long_long_unsigned_type_node
3728 : long_long_integer_type_node);
3730 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3731 if (int_n_enabled_p[i]
3732 && bits == int_n_data[i].bitsize)
3733 return (unsignedp ? int_n_trees[i].unsigned_type
3734 : int_n_trees[i].signed_type);
3736 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3737 return (unsignedp ? widest_unsigned_literal_type_node
3738 : widest_integer_literal_type_node);
3740 if (bits <= TYPE_PRECISION (intQI_type_node))
3741 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3743 if (bits <= TYPE_PRECISION (intHI_type_node))
3744 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3746 if (bits <= TYPE_PRECISION (intSI_type_node))
3747 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3749 if (bits <= TYPE_PRECISION (intDI_type_node))
3750 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3752 return 0;
3755 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3756 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3757 and saturating if SATP is nonzero, otherwise not saturating. */
3759 tree
3760 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3761 int unsignedp, int satp)
3763 machine_mode mode;
3764 if (ibit == 0)
3765 mode = unsignedp ? UQQmode : QQmode;
3766 else
3767 mode = unsignedp ? UHAmode : HAmode;
3769 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3770 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3771 break;
3773 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3775 sorry ("GCC cannot support operators with integer types and "
3776 "fixed-point types that have too many integral and "
3777 "fractional bits together");
3778 return 0;
3781 return c_common_type_for_mode (mode, satp);
3784 /* Used for communication between c_common_type_for_mode and
3785 c_register_builtin_type. */
3786 tree registered_builtin_types;
3788 /* Return a data type that has machine mode MODE.
3789 If the mode is an integer,
3790 then UNSIGNEDP selects between signed and unsigned types.
3791 If the mode is a fixed-point mode,
3792 then UNSIGNEDP selects between saturating and nonsaturating types. */
3794 tree
3795 c_common_type_for_mode (machine_mode mode, int unsignedp)
3797 tree t;
3798 int i;
3800 if (mode == TYPE_MODE (integer_type_node))
3801 return unsignedp ? unsigned_type_node : integer_type_node;
3803 if (mode == TYPE_MODE (signed_char_type_node))
3804 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3806 if (mode == TYPE_MODE (short_integer_type_node))
3807 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3809 if (mode == TYPE_MODE (long_integer_type_node))
3810 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3812 if (mode == TYPE_MODE (long_long_integer_type_node))
3813 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3815 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3816 if (int_n_enabled_p[i]
3817 && mode == int_n_data[i].m)
3818 return (unsignedp ? int_n_trees[i].unsigned_type
3819 : int_n_trees[i].signed_type);
3821 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3822 return unsignedp ? widest_unsigned_literal_type_node
3823 : widest_integer_literal_type_node;
3825 if (mode == QImode)
3826 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3828 if (mode == HImode)
3829 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3831 if (mode == SImode)
3832 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3834 if (mode == DImode)
3835 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3837 #if HOST_BITS_PER_WIDE_INT >= 64
3838 if (mode == TYPE_MODE (intTI_type_node))
3839 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3840 #endif
3842 if (mode == TYPE_MODE (float_type_node))
3843 return float_type_node;
3845 if (mode == TYPE_MODE (double_type_node))
3846 return double_type_node;
3848 if (mode == TYPE_MODE (long_double_type_node))
3849 return long_double_type_node;
3851 if (mode == TYPE_MODE (void_type_node))
3852 return void_type_node;
3854 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3855 return (unsignedp
3856 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3857 : make_signed_type (GET_MODE_PRECISION (mode)));
3859 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3860 return (unsignedp
3861 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3862 : make_signed_type (GET_MODE_PRECISION (mode)));
3864 if (COMPLEX_MODE_P (mode))
3866 machine_mode inner_mode;
3867 tree inner_type;
3869 if (mode == TYPE_MODE (complex_float_type_node))
3870 return complex_float_type_node;
3871 if (mode == TYPE_MODE (complex_double_type_node))
3872 return complex_double_type_node;
3873 if (mode == TYPE_MODE (complex_long_double_type_node))
3874 return complex_long_double_type_node;
3876 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3877 return complex_integer_type_node;
3879 inner_mode = GET_MODE_INNER (mode);
3880 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3881 if (inner_type != NULL_TREE)
3882 return build_complex_type (inner_type);
3884 else if (VECTOR_MODE_P (mode))
3886 machine_mode inner_mode = GET_MODE_INNER (mode);
3887 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3888 if (inner_type != NULL_TREE)
3889 return build_vector_type_for_mode (inner_type, mode);
3892 if (mode == TYPE_MODE (dfloat32_type_node))
3893 return dfloat32_type_node;
3894 if (mode == TYPE_MODE (dfloat64_type_node))
3895 return dfloat64_type_node;
3896 if (mode == TYPE_MODE (dfloat128_type_node))
3897 return dfloat128_type_node;
3899 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3901 if (mode == TYPE_MODE (short_fract_type_node))
3902 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3903 if (mode == TYPE_MODE (fract_type_node))
3904 return unsignedp ? sat_fract_type_node : fract_type_node;
3905 if (mode == TYPE_MODE (long_fract_type_node))
3906 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3907 if (mode == TYPE_MODE (long_long_fract_type_node))
3908 return unsignedp ? sat_long_long_fract_type_node
3909 : long_long_fract_type_node;
3911 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3912 return unsignedp ? sat_unsigned_short_fract_type_node
3913 : unsigned_short_fract_type_node;
3914 if (mode == TYPE_MODE (unsigned_fract_type_node))
3915 return unsignedp ? sat_unsigned_fract_type_node
3916 : unsigned_fract_type_node;
3917 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3918 return unsignedp ? sat_unsigned_long_fract_type_node
3919 : unsigned_long_fract_type_node;
3920 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3921 return unsignedp ? sat_unsigned_long_long_fract_type_node
3922 : unsigned_long_long_fract_type_node;
3924 if (mode == TYPE_MODE (short_accum_type_node))
3925 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3926 if (mode == TYPE_MODE (accum_type_node))
3927 return unsignedp ? sat_accum_type_node : accum_type_node;
3928 if (mode == TYPE_MODE (long_accum_type_node))
3929 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3930 if (mode == TYPE_MODE (long_long_accum_type_node))
3931 return unsignedp ? sat_long_long_accum_type_node
3932 : long_long_accum_type_node;
3934 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3935 return unsignedp ? sat_unsigned_short_accum_type_node
3936 : unsigned_short_accum_type_node;
3937 if (mode == TYPE_MODE (unsigned_accum_type_node))
3938 return unsignedp ? sat_unsigned_accum_type_node
3939 : unsigned_accum_type_node;
3940 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3941 return unsignedp ? sat_unsigned_long_accum_type_node
3942 : unsigned_long_accum_type_node;
3943 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3944 return unsignedp ? sat_unsigned_long_long_accum_type_node
3945 : unsigned_long_long_accum_type_node;
3947 if (mode == QQmode)
3948 return unsignedp ? sat_qq_type_node : qq_type_node;
3949 if (mode == HQmode)
3950 return unsignedp ? sat_hq_type_node : hq_type_node;
3951 if (mode == SQmode)
3952 return unsignedp ? sat_sq_type_node : sq_type_node;
3953 if (mode == DQmode)
3954 return unsignedp ? sat_dq_type_node : dq_type_node;
3955 if (mode == TQmode)
3956 return unsignedp ? sat_tq_type_node : tq_type_node;
3958 if (mode == UQQmode)
3959 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3960 if (mode == UHQmode)
3961 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3962 if (mode == USQmode)
3963 return unsignedp ? sat_usq_type_node : usq_type_node;
3964 if (mode == UDQmode)
3965 return unsignedp ? sat_udq_type_node : udq_type_node;
3966 if (mode == UTQmode)
3967 return unsignedp ? sat_utq_type_node : utq_type_node;
3969 if (mode == HAmode)
3970 return unsignedp ? sat_ha_type_node : ha_type_node;
3971 if (mode == SAmode)
3972 return unsignedp ? sat_sa_type_node : sa_type_node;
3973 if (mode == DAmode)
3974 return unsignedp ? sat_da_type_node : da_type_node;
3975 if (mode == TAmode)
3976 return unsignedp ? sat_ta_type_node : ta_type_node;
3978 if (mode == UHAmode)
3979 return unsignedp ? sat_uha_type_node : uha_type_node;
3980 if (mode == USAmode)
3981 return unsignedp ? sat_usa_type_node : usa_type_node;
3982 if (mode == UDAmode)
3983 return unsignedp ? sat_uda_type_node : uda_type_node;
3984 if (mode == UTAmode)
3985 return unsignedp ? sat_uta_type_node : uta_type_node;
3988 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3989 if (TYPE_MODE (TREE_VALUE (t)) == mode
3990 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3991 return TREE_VALUE (t);
3993 return 0;
3996 tree
3997 c_common_unsigned_type (tree type)
3999 return c_common_signed_or_unsigned_type (1, type);
4002 /* Return a signed type the same as TYPE in other respects. */
4004 tree
4005 c_common_signed_type (tree type)
4007 return c_common_signed_or_unsigned_type (0, type);
4010 /* Return a type the same as TYPE except unsigned or
4011 signed according to UNSIGNEDP. */
4013 tree
4014 c_common_signed_or_unsigned_type (int unsignedp, tree type)
4016 tree type1;
4017 int i;
4019 /* This block of code emulates the behavior of the old
4020 c_common_unsigned_type. In particular, it returns
4021 long_unsigned_type_node if passed a long, even when a int would
4022 have the same size. This is necessary for warnings to work
4023 correctly in archs where sizeof(int) == sizeof(long) */
4025 type1 = TYPE_MAIN_VARIANT (type);
4026 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4027 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4028 if (type1 == integer_type_node || type1 == unsigned_type_node)
4029 return unsignedp ? unsigned_type_node : integer_type_node;
4030 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4031 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4032 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4033 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4034 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4035 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
4037 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4038 if (int_n_enabled_p[i]
4039 && (type1 == int_n_trees[i].unsigned_type
4040 || type1 == int_n_trees[i].signed_type))
4041 return (unsignedp ? int_n_trees[i].unsigned_type
4042 : int_n_trees[i].signed_type);
4044 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4045 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4046 #if HOST_BITS_PER_WIDE_INT >= 64
4047 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4048 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4049 #endif
4050 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4051 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4052 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4053 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4054 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4055 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4056 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4057 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4059 #define C_COMMON_FIXED_TYPES(NAME) \
4060 if (type1 == short_ ## NAME ## _type_node \
4061 || type1 == unsigned_short_ ## NAME ## _type_node) \
4062 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4063 : short_ ## NAME ## _type_node; \
4064 if (type1 == NAME ## _type_node \
4065 || type1 == unsigned_ ## NAME ## _type_node) \
4066 return unsignedp ? unsigned_ ## NAME ## _type_node \
4067 : NAME ## _type_node; \
4068 if (type1 == long_ ## NAME ## _type_node \
4069 || type1 == unsigned_long_ ## NAME ## _type_node) \
4070 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4071 : long_ ## NAME ## _type_node; \
4072 if (type1 == long_long_ ## NAME ## _type_node \
4073 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4074 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4075 : long_long_ ## NAME ## _type_node;
4077 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
4078 if (type1 == NAME ## _type_node \
4079 || type1 == u ## NAME ## _type_node) \
4080 return unsignedp ? u ## NAME ## _type_node \
4081 : NAME ## _type_node;
4083 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
4084 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4085 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4086 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4087 : sat_ ## short_ ## NAME ## _type_node; \
4088 if (type1 == sat_ ## NAME ## _type_node \
4089 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4090 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4091 : sat_ ## NAME ## _type_node; \
4092 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4093 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4094 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4095 : sat_ ## long_ ## NAME ## _type_node; \
4096 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4097 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4098 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4099 : sat_ ## long_long_ ## NAME ## _type_node;
4101 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4102 if (type1 == sat_ ## NAME ## _type_node \
4103 || type1 == sat_ ## u ## NAME ## _type_node) \
4104 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4105 : sat_ ## NAME ## _type_node;
4107 C_COMMON_FIXED_TYPES (fract);
4108 C_COMMON_FIXED_TYPES_SAT (fract);
4109 C_COMMON_FIXED_TYPES (accum);
4110 C_COMMON_FIXED_TYPES_SAT (accum);
4112 C_COMMON_FIXED_MODE_TYPES (qq);
4113 C_COMMON_FIXED_MODE_TYPES (hq);
4114 C_COMMON_FIXED_MODE_TYPES (sq);
4115 C_COMMON_FIXED_MODE_TYPES (dq);
4116 C_COMMON_FIXED_MODE_TYPES (tq);
4117 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4118 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4119 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4120 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4121 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4122 C_COMMON_FIXED_MODE_TYPES (ha);
4123 C_COMMON_FIXED_MODE_TYPES (sa);
4124 C_COMMON_FIXED_MODE_TYPES (da);
4125 C_COMMON_FIXED_MODE_TYPES (ta);
4126 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4127 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4128 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4129 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
4131 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4132 the precision; they have precision set to match their range, but
4133 may use a wider mode to match an ABI. If we change modes, we may
4134 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4135 the precision as well, so as to yield correct results for
4136 bit-field types. C++ does not have these separate bit-field
4137 types, and producing a signed or unsigned variant of an
4138 ENUMERAL_TYPE may cause other problems as well. */
4140 if (!INTEGRAL_TYPE_P (type)
4141 || TYPE_UNSIGNED (type) == unsignedp)
4142 return type;
4144 #define TYPE_OK(node) \
4145 (TYPE_MODE (type) == TYPE_MODE (node) \
4146 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4147 if (TYPE_OK (signed_char_type_node))
4148 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4149 if (TYPE_OK (integer_type_node))
4150 return unsignedp ? unsigned_type_node : integer_type_node;
4151 if (TYPE_OK (short_integer_type_node))
4152 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4153 if (TYPE_OK (long_integer_type_node))
4154 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4155 if (TYPE_OK (long_long_integer_type_node))
4156 return (unsignedp ? long_long_unsigned_type_node
4157 : long_long_integer_type_node);
4159 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4160 if (int_n_enabled_p[i]
4161 && TYPE_MODE (type) == int_n_data[i].m
4162 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4163 return (unsignedp ? int_n_trees[i].unsigned_type
4164 : int_n_trees[i].signed_type);
4166 if (TYPE_OK (widest_integer_literal_type_node))
4167 return (unsignedp ? widest_unsigned_literal_type_node
4168 : widest_integer_literal_type_node);
4170 #if HOST_BITS_PER_WIDE_INT >= 64
4171 if (TYPE_OK (intTI_type_node))
4172 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4173 #endif
4174 if (TYPE_OK (intDI_type_node))
4175 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4176 if (TYPE_OK (intSI_type_node))
4177 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4178 if (TYPE_OK (intHI_type_node))
4179 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4180 if (TYPE_OK (intQI_type_node))
4181 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4182 #undef TYPE_OK
4184 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
4187 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4189 tree
4190 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4192 int i;
4194 /* Extended integer types of the same width as a standard type have
4195 lesser rank, so those of the same width as int promote to int or
4196 unsigned int and are valid for printf formats expecting int or
4197 unsigned int. To avoid such special cases, avoid creating
4198 extended integer types for bit-fields if a standard integer type
4199 is available. */
4200 if (width == TYPE_PRECISION (integer_type_node))
4201 return unsignedp ? unsigned_type_node : integer_type_node;
4202 if (width == TYPE_PRECISION (signed_char_type_node))
4203 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4204 if (width == TYPE_PRECISION (short_integer_type_node))
4205 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4206 if (width == TYPE_PRECISION (long_integer_type_node))
4207 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4208 if (width == TYPE_PRECISION (long_long_integer_type_node))
4209 return (unsignedp ? long_long_unsigned_type_node
4210 : long_long_integer_type_node);
4211 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4212 if (int_n_enabled_p[i]
4213 && width == int_n_data[i].bitsize)
4214 return (unsignedp ? int_n_trees[i].unsigned_type
4215 : int_n_trees[i].signed_type);
4216 return build_nonstandard_integer_type (width, unsignedp);
4219 /* The C version of the register_builtin_type langhook. */
4221 void
4222 c_register_builtin_type (tree type, const char* name)
4224 tree decl;
4226 decl = build_decl (UNKNOWN_LOCATION,
4227 TYPE_DECL, get_identifier (name), type);
4228 DECL_ARTIFICIAL (decl) = 1;
4229 if (!TYPE_NAME (type))
4230 TYPE_NAME (type) = decl;
4231 pushdecl (decl);
4233 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
4236 /* Print an error message for invalid operands to arith operation
4237 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4238 LOCATION is the location of the message. */
4240 void
4241 binary_op_error (location_t location, enum tree_code code,
4242 tree type0, tree type1)
4244 const char *opname;
4246 switch (code)
4248 case PLUS_EXPR:
4249 opname = "+"; break;
4250 case MINUS_EXPR:
4251 opname = "-"; break;
4252 case MULT_EXPR:
4253 opname = "*"; break;
4254 case MAX_EXPR:
4255 opname = "max"; break;
4256 case MIN_EXPR:
4257 opname = "min"; break;
4258 case EQ_EXPR:
4259 opname = "=="; break;
4260 case NE_EXPR:
4261 opname = "!="; break;
4262 case LE_EXPR:
4263 opname = "<="; break;
4264 case GE_EXPR:
4265 opname = ">="; break;
4266 case LT_EXPR:
4267 opname = "<"; break;
4268 case GT_EXPR:
4269 opname = ">"; break;
4270 case LSHIFT_EXPR:
4271 opname = "<<"; break;
4272 case RSHIFT_EXPR:
4273 opname = ">>"; break;
4274 case TRUNC_MOD_EXPR:
4275 case FLOOR_MOD_EXPR:
4276 opname = "%"; break;
4277 case TRUNC_DIV_EXPR:
4278 case FLOOR_DIV_EXPR:
4279 opname = "/"; break;
4280 case BIT_AND_EXPR:
4281 opname = "&"; break;
4282 case BIT_IOR_EXPR:
4283 opname = "|"; break;
4284 case TRUTH_ANDIF_EXPR:
4285 opname = "&&"; break;
4286 case TRUTH_ORIF_EXPR:
4287 opname = "||"; break;
4288 case BIT_XOR_EXPR:
4289 opname = "^"; break;
4290 default:
4291 gcc_unreachable ();
4293 error_at (location,
4294 "invalid operands to binary %s (have %qT and %qT)", opname,
4295 type0, type1);
4298 /* Given an expression as a tree, return its original type. Do this
4299 by stripping any conversion that preserves the sign and precision. */
4300 static tree
4301 expr_original_type (tree expr)
4303 STRIP_SIGN_NOPS (expr);
4304 return TREE_TYPE (expr);
4307 /* Subroutine of build_binary_op, used for comparison operations.
4308 See if the operands have both been converted from subword integer types
4309 and, if so, perhaps change them both back to their original type.
4310 This function is also responsible for converting the two operands
4311 to the proper common type for comparison.
4313 The arguments of this function are all pointers to local variables
4314 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4315 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4317 LOC is the location of the comparison.
4319 If this function returns nonzero, it means that the comparison has
4320 a constant value. What this function returns is an expression for
4321 that value. */
4323 tree
4324 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4325 tree *restype_ptr, enum tree_code *rescode_ptr)
4327 tree type;
4328 tree op0 = *op0_ptr;
4329 tree op1 = *op1_ptr;
4330 int unsignedp0, unsignedp1;
4331 int real1, real2;
4332 tree primop0, primop1;
4333 enum tree_code code = *rescode_ptr;
4335 /* Throw away any conversions to wider types
4336 already present in the operands. */
4338 primop0 = c_common_get_narrower (op0, &unsignedp0);
4339 primop1 = c_common_get_narrower (op1, &unsignedp1);
4341 /* If primopN is first sign-extended from primopN's precision to opN's
4342 precision, then zero-extended from opN's precision to
4343 *restype_ptr precision, shortenings might be invalid. */
4344 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4345 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4346 && !unsignedp0
4347 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4348 primop0 = op0;
4349 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4350 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4351 && !unsignedp1
4352 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4353 primop1 = op1;
4355 /* Handle the case that OP0 does not *contain* a conversion
4356 but it *requires* conversion to FINAL_TYPE. */
4358 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4359 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4360 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4361 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4363 /* If one of the operands must be floated, we cannot optimize. */
4364 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4365 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4367 /* If first arg is constant, swap the args (changing operation
4368 so value is preserved), for canonicalization. Don't do this if
4369 the second arg is 0. */
4371 if (TREE_CONSTANT (primop0)
4372 && !integer_zerop (primop1) && !real_zerop (primop1)
4373 && !fixed_zerop (primop1))
4375 std::swap (primop0, primop1);
4376 std::swap (op0, op1);
4377 *op0_ptr = op0;
4378 *op1_ptr = op1;
4379 std::swap (unsignedp0, unsignedp1);
4380 std::swap (real1, real2);
4382 switch (code)
4384 case LT_EXPR:
4385 code = GT_EXPR;
4386 break;
4387 case GT_EXPR:
4388 code = LT_EXPR;
4389 break;
4390 case LE_EXPR:
4391 code = GE_EXPR;
4392 break;
4393 case GE_EXPR:
4394 code = LE_EXPR;
4395 break;
4396 default:
4397 break;
4399 *rescode_ptr = code;
4402 /* If comparing an integer against a constant more bits wide,
4403 maybe we can deduce a value of 1 or 0 independent of the data.
4404 Or else truncate the constant now
4405 rather than extend the variable at run time.
4407 This is only interesting if the constant is the wider arg.
4408 Also, it is not safe if the constant is unsigned and the
4409 variable arg is signed, since in this case the variable
4410 would be sign-extended and then regarded as unsigned.
4411 Our technique fails in this case because the lowest/highest
4412 possible unsigned results don't follow naturally from the
4413 lowest/highest possible values of the variable operand.
4414 For just EQ_EXPR and NE_EXPR there is another technique that
4415 could be used: see if the constant can be faithfully represented
4416 in the other operand's type, by truncating it and reextending it
4417 and see if that preserves the constant's value. */
4419 if (!real1 && !real2
4420 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4421 && TREE_CODE (primop1) == INTEGER_CST
4422 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4424 int min_gt, max_gt, min_lt, max_lt;
4425 tree maxval, minval;
4426 /* 1 if comparison is nominally unsigned. */
4427 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4428 tree val;
4430 type = c_common_signed_or_unsigned_type (unsignedp0,
4431 TREE_TYPE (primop0));
4433 maxval = TYPE_MAX_VALUE (type);
4434 minval = TYPE_MIN_VALUE (type);
4436 if (unsignedp && !unsignedp0)
4437 *restype_ptr = c_common_signed_type (*restype_ptr);
4439 if (TREE_TYPE (primop1) != *restype_ptr)
4441 /* Convert primop1 to target type, but do not introduce
4442 additional overflow. We know primop1 is an int_cst. */
4443 primop1 = force_fit_type (*restype_ptr,
4444 wide_int::from
4445 (primop1,
4446 TYPE_PRECISION (*restype_ptr),
4447 TYPE_SIGN (TREE_TYPE (primop1))),
4448 0, TREE_OVERFLOW (primop1));
4450 if (type != *restype_ptr)
4452 minval = convert (*restype_ptr, minval);
4453 maxval = convert (*restype_ptr, maxval);
4456 min_gt = tree_int_cst_lt (primop1, minval);
4457 max_gt = tree_int_cst_lt (primop1, maxval);
4458 min_lt = tree_int_cst_lt (minval, primop1);
4459 max_lt = tree_int_cst_lt (maxval, primop1);
4461 val = 0;
4462 /* This used to be a switch, but Genix compiler can't handle that. */
4463 if (code == NE_EXPR)
4465 if (max_lt || min_gt)
4466 val = truthvalue_true_node;
4468 else if (code == EQ_EXPR)
4470 if (max_lt || min_gt)
4471 val = truthvalue_false_node;
4473 else if (code == LT_EXPR)
4475 if (max_lt)
4476 val = truthvalue_true_node;
4477 if (!min_lt)
4478 val = truthvalue_false_node;
4480 else if (code == GT_EXPR)
4482 if (min_gt)
4483 val = truthvalue_true_node;
4484 if (!max_gt)
4485 val = truthvalue_false_node;
4487 else if (code == LE_EXPR)
4489 if (!max_gt)
4490 val = truthvalue_true_node;
4491 if (min_gt)
4492 val = truthvalue_false_node;
4494 else if (code == GE_EXPR)
4496 if (!min_lt)
4497 val = truthvalue_true_node;
4498 if (max_lt)
4499 val = truthvalue_false_node;
4502 /* If primop0 was sign-extended and unsigned comparison specd,
4503 we did a signed comparison above using the signed type bounds.
4504 But the comparison we output must be unsigned.
4506 Also, for inequalities, VAL is no good; but if the signed
4507 comparison had *any* fixed result, it follows that the
4508 unsigned comparison just tests the sign in reverse
4509 (positive values are LE, negative ones GE).
4510 So we can generate an unsigned comparison
4511 against an extreme value of the signed type. */
4513 if (unsignedp && !unsignedp0)
4515 if (val != 0)
4516 switch (code)
4518 case LT_EXPR:
4519 case GE_EXPR:
4520 primop1 = TYPE_MIN_VALUE (type);
4521 val = 0;
4522 break;
4524 case LE_EXPR:
4525 case GT_EXPR:
4526 primop1 = TYPE_MAX_VALUE (type);
4527 val = 0;
4528 break;
4530 default:
4531 break;
4533 type = c_common_unsigned_type (type);
4536 if (TREE_CODE (primop0) != INTEGER_CST)
4538 if (val == truthvalue_false_node)
4539 warning_at (loc, OPT_Wtype_limits,
4540 "comparison is always false due to limited range of data type");
4541 if (val == truthvalue_true_node)
4542 warning_at (loc, OPT_Wtype_limits,
4543 "comparison is always true due to limited range of data type");
4546 if (val != 0)
4548 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4549 if (TREE_SIDE_EFFECTS (primop0))
4550 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4551 return val;
4554 /* Value is not predetermined, but do the comparison
4555 in the type of the operand that is not constant.
4556 TYPE is already properly set. */
4559 /* If either arg is decimal float and the other is float, find the
4560 proper common type to use for comparison. */
4561 else if (real1 && real2
4562 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4563 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4564 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4566 /* If either arg is decimal float and the other is float, fail. */
4567 else if (real1 && real2
4568 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4569 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4570 return 0;
4572 else if (real1 && real2
4573 && (TYPE_PRECISION (TREE_TYPE (primop0))
4574 == TYPE_PRECISION (TREE_TYPE (primop1))))
4575 type = TREE_TYPE (primop0);
4577 /* If args' natural types are both narrower than nominal type
4578 and both extend in the same manner, compare them
4579 in the type of the wider arg.
4580 Otherwise must actually extend both to the nominal
4581 common type lest different ways of extending
4582 alter the result.
4583 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4585 else if (unsignedp0 == unsignedp1 && real1 == real2
4586 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4587 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4589 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4590 type = c_common_signed_or_unsigned_type (unsignedp0
4591 || TYPE_UNSIGNED (*restype_ptr),
4592 type);
4593 /* Make sure shorter operand is extended the right way
4594 to match the longer operand. */
4595 primop0
4596 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4597 TREE_TYPE (primop0)),
4598 primop0);
4599 primop1
4600 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4601 TREE_TYPE (primop1)),
4602 primop1);
4604 else
4606 /* Here we must do the comparison on the nominal type
4607 using the args exactly as we received them. */
4608 type = *restype_ptr;
4609 primop0 = op0;
4610 primop1 = op1;
4612 if (!real1 && !real2 && integer_zerop (primop1)
4613 && TYPE_UNSIGNED (*restype_ptr))
4615 tree value = 0;
4616 /* All unsigned values are >= 0, so we warn. However,
4617 if OP0 is a constant that is >= 0, the signedness of
4618 the comparison isn't an issue, so suppress the
4619 warning. */
4620 bool warn =
4621 warn_type_limits && !in_system_header_at (loc)
4622 && !(TREE_CODE (primop0) == INTEGER_CST
4623 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4624 primop0)))
4625 /* Do not warn for enumeration types. */
4626 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4628 switch (code)
4630 case GE_EXPR:
4631 if (warn)
4632 warning_at (loc, OPT_Wtype_limits,
4633 "comparison of unsigned expression >= 0 is always true");
4634 value = truthvalue_true_node;
4635 break;
4637 case LT_EXPR:
4638 if (warn)
4639 warning_at (loc, OPT_Wtype_limits,
4640 "comparison of unsigned expression < 0 is always false");
4641 value = truthvalue_false_node;
4642 break;
4644 default:
4645 break;
4648 if (value != 0)
4650 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4651 if (TREE_SIDE_EFFECTS (primop0))
4652 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4653 primop0, value);
4654 return value;
4659 *op0_ptr = convert (type, primop0);
4660 *op1_ptr = convert (type, primop1);
4662 *restype_ptr = truthvalue_type_node;
4664 return 0;
4667 /* Return a tree for the sum or difference (RESULTCODE says which)
4668 of pointer PTROP and integer INTOP. */
4670 tree
4671 pointer_int_sum (location_t loc, enum tree_code resultcode,
4672 tree ptrop, tree intop, bool complain)
4674 tree size_exp, ret;
4676 /* The result is a pointer of the same type that is being added. */
4677 tree result_type = TREE_TYPE (ptrop);
4679 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4681 if (complain && warn_pointer_arith)
4682 pedwarn (loc, OPT_Wpointer_arith,
4683 "pointer of type %<void *%> used in arithmetic");
4684 else if (!complain)
4685 return error_mark_node;
4686 size_exp = integer_one_node;
4688 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4690 if (complain && warn_pointer_arith)
4691 pedwarn (loc, OPT_Wpointer_arith,
4692 "pointer to a function used in arithmetic");
4693 else if (!complain)
4694 return error_mark_node;
4695 size_exp = integer_one_node;
4697 else
4698 size_exp = size_in_bytes (TREE_TYPE (result_type));
4700 /* We are manipulating pointer values, so we don't need to warn
4701 about relying on undefined signed overflow. We disable the
4702 warning here because we use integer types so fold won't know that
4703 they are really pointers. */
4704 fold_defer_overflow_warnings ();
4706 /* If what we are about to multiply by the size of the elements
4707 contains a constant term, apply distributive law
4708 and multiply that constant term separately.
4709 This helps produce common subexpressions. */
4710 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4711 && !TREE_CONSTANT (intop)
4712 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4713 && TREE_CONSTANT (size_exp)
4714 /* If the constant comes from pointer subtraction,
4715 skip this optimization--it would cause an error. */
4716 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4717 /* If the constant is unsigned, and smaller than the pointer size,
4718 then we must skip this optimization. This is because it could cause
4719 an overflow error if the constant is negative but INTOP is not. */
4720 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4721 || (TYPE_PRECISION (TREE_TYPE (intop))
4722 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4724 enum tree_code subcode = resultcode;
4725 tree int_type = TREE_TYPE (intop);
4726 if (TREE_CODE (intop) == MINUS_EXPR)
4727 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4728 /* Convert both subexpression types to the type of intop,
4729 because weird cases involving pointer arithmetic
4730 can result in a sum or difference with different type args. */
4731 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4732 subcode, ptrop,
4733 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4734 intop = convert (int_type, TREE_OPERAND (intop, 0));
4737 /* Convert the integer argument to a type the same size as sizetype
4738 so the multiply won't overflow spuriously. */
4739 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4740 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4741 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4742 TYPE_UNSIGNED (sizetype)), intop);
4744 /* Replace the integer argument with a suitable product by the object size.
4745 Do this multiplication as signed, then convert to the appropriate type
4746 for the pointer operation and disregard an overflow that occurred only
4747 because of the sign-extension change in the latter conversion. */
4749 tree t = build_binary_op (loc,
4750 MULT_EXPR, intop,
4751 convert (TREE_TYPE (intop), size_exp), 1);
4752 intop = convert (sizetype, t);
4753 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4754 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4757 /* Create the sum or difference. */
4758 if (resultcode == MINUS_EXPR)
4759 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4761 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4763 fold_undefer_and_ignore_overflow_warnings ();
4765 return ret;
4768 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4769 and if NON_CONST is known not to be permitted in an evaluated part
4770 of a constant expression. */
4772 tree
4773 c_wrap_maybe_const (tree expr, bool non_const)
4775 bool nowarning = TREE_NO_WARNING (expr);
4776 location_t loc = EXPR_LOCATION (expr);
4778 /* This should never be called for C++. */
4779 if (c_dialect_cxx ())
4780 gcc_unreachable ();
4782 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4783 STRIP_TYPE_NOPS (expr);
4784 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4785 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4786 if (nowarning)
4787 TREE_NO_WARNING (expr) = 1;
4788 protected_set_expr_location (expr, loc);
4790 return expr;
4793 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4794 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4795 around the SAVE_EXPR if needed so that c_fully_fold does not need
4796 to look inside SAVE_EXPRs. */
4798 tree
4799 c_save_expr (tree expr)
4801 bool maybe_const = true;
4802 if (c_dialect_cxx ())
4803 return save_expr (expr);
4804 expr = c_fully_fold (expr, false, &maybe_const);
4805 expr = save_expr (expr);
4806 if (!maybe_const)
4807 expr = c_wrap_maybe_const (expr, true);
4808 return expr;
4811 /* Return whether EXPR is a declaration whose address can never be
4812 NULL. */
4814 bool
4815 decl_with_nonnull_addr_p (const_tree expr)
4817 return (DECL_P (expr)
4818 && (TREE_CODE (expr) == PARM_DECL
4819 || TREE_CODE (expr) == LABEL_DECL
4820 || !DECL_WEAK (expr)));
4823 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4824 or for an `if' or `while' statement or ?..: exp. It should already
4825 have been validated to be of suitable type; otherwise, a bad
4826 diagnostic may result.
4828 The EXPR is located at LOCATION.
4830 This preparation consists of taking the ordinary
4831 representation of an expression expr and producing a valid tree
4832 boolean expression describing whether expr is nonzero. We could
4833 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4834 but we optimize comparisons, &&, ||, and !.
4836 The resulting type should always be `truthvalue_type_node'. */
4838 tree
4839 c_common_truthvalue_conversion (location_t location, tree expr)
4841 switch (TREE_CODE (expr))
4843 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4844 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4845 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4846 case ORDERED_EXPR: case UNORDERED_EXPR:
4847 if (TREE_TYPE (expr) == truthvalue_type_node)
4848 return expr;
4849 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4850 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4851 goto ret;
4853 case TRUTH_ANDIF_EXPR:
4854 case TRUTH_ORIF_EXPR:
4855 case TRUTH_AND_EXPR:
4856 case TRUTH_OR_EXPR:
4857 case TRUTH_XOR_EXPR:
4858 if (TREE_TYPE (expr) == truthvalue_type_node)
4859 return expr;
4860 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4861 c_common_truthvalue_conversion (location,
4862 TREE_OPERAND (expr, 0)),
4863 c_common_truthvalue_conversion (location,
4864 TREE_OPERAND (expr, 1)));
4865 goto ret;
4867 case TRUTH_NOT_EXPR:
4868 if (TREE_TYPE (expr) == truthvalue_type_node)
4869 return expr;
4870 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4871 c_common_truthvalue_conversion (location,
4872 TREE_OPERAND (expr, 0)));
4873 goto ret;
4875 case ERROR_MARK:
4876 return expr;
4878 case INTEGER_CST:
4879 return integer_zerop (expr) ? truthvalue_false_node
4880 : truthvalue_true_node;
4882 case REAL_CST:
4883 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4884 ? truthvalue_true_node
4885 : truthvalue_false_node;
4887 case FIXED_CST:
4888 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4889 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4890 ? truthvalue_true_node
4891 : truthvalue_false_node;
4893 case FUNCTION_DECL:
4894 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4895 /* Fall through. */
4897 case ADDR_EXPR:
4899 tree inner = TREE_OPERAND (expr, 0);
4900 if (decl_with_nonnull_addr_p (inner))
4902 /* Common Ada/Pascal programmer's mistake. */
4903 warning_at (location,
4904 OPT_Waddress,
4905 "the address of %qD will always evaluate as %<true%>",
4906 inner);
4907 return truthvalue_true_node;
4909 break;
4912 case COMPLEX_EXPR:
4913 expr = build_binary_op (EXPR_LOCATION (expr),
4914 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4915 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4916 c_common_truthvalue_conversion (location,
4917 TREE_OPERAND (expr, 0)),
4918 c_common_truthvalue_conversion (location,
4919 TREE_OPERAND (expr, 1)),
4921 goto ret;
4923 case NEGATE_EXPR:
4924 case ABS_EXPR:
4925 case FLOAT_EXPR:
4926 case EXCESS_PRECISION_EXPR:
4927 /* These don't change whether an object is nonzero or zero. */
4928 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4930 case LROTATE_EXPR:
4931 case RROTATE_EXPR:
4932 /* These don't change whether an object is zero or nonzero, but
4933 we can't ignore them if their second arg has side-effects. */
4934 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4936 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4937 TREE_OPERAND (expr, 1),
4938 c_common_truthvalue_conversion
4939 (location, TREE_OPERAND (expr, 0)));
4940 goto ret;
4942 else
4943 return c_common_truthvalue_conversion (location,
4944 TREE_OPERAND (expr, 0));
4946 case COND_EXPR:
4947 /* Distribute the conversion into the arms of a COND_EXPR. */
4948 if (c_dialect_cxx ())
4950 tree op1 = TREE_OPERAND (expr, 1);
4951 tree op2 = TREE_OPERAND (expr, 2);
4952 /* In C++ one of the arms might have void type if it is throw. */
4953 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4954 op1 = c_common_truthvalue_conversion (location, op1);
4955 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4956 op2 = c_common_truthvalue_conversion (location, op2);
4957 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4958 TREE_OPERAND (expr, 0), op1, op2);
4959 goto ret;
4961 else
4963 /* Folding will happen later for C. */
4964 expr = build3 (COND_EXPR, truthvalue_type_node,
4965 TREE_OPERAND (expr, 0),
4966 c_common_truthvalue_conversion (location,
4967 TREE_OPERAND (expr, 1)),
4968 c_common_truthvalue_conversion (location,
4969 TREE_OPERAND (expr, 2)));
4970 goto ret;
4973 CASE_CONVERT:
4975 tree totype = TREE_TYPE (expr);
4976 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4978 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4979 since that affects how `default_conversion' will behave. */
4980 if (TREE_CODE (totype) == REFERENCE_TYPE
4981 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4982 break;
4983 /* Don't strip a conversion from C++0x scoped enum, since they
4984 don't implicitly convert to other types. */
4985 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4986 && ENUM_IS_SCOPED (fromtype))
4987 break;
4988 /* If this isn't narrowing the argument, we can ignore it. */
4989 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4990 return c_common_truthvalue_conversion (location,
4991 TREE_OPERAND (expr, 0));
4993 break;
4995 case MODIFY_EXPR:
4996 if (!TREE_NO_WARNING (expr)
4997 && warn_parentheses)
4999 warning (OPT_Wparentheses,
5000 "suggest parentheses around assignment used as truth value");
5001 TREE_NO_WARNING (expr) = 1;
5003 break;
5005 default:
5006 break;
5009 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
5011 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
5012 expr = (build_binary_op
5013 (EXPR_LOCATION (expr),
5014 (TREE_SIDE_EFFECTS (expr)
5015 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
5016 c_common_truthvalue_conversion
5017 (location,
5018 build_unary_op (location, REALPART_EXPR, t, 0)),
5019 c_common_truthvalue_conversion
5020 (location,
5021 build_unary_op (location, IMAGPART_EXPR, t, 0)),
5022 0));
5023 goto ret;
5026 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5028 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5029 FCONST0 (TYPE_MODE
5030 (TREE_TYPE (expr))));
5031 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
5033 else
5034 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
5036 ret:
5037 protected_set_expr_location (expr, location);
5038 return expr;
5041 static void def_builtin_1 (enum built_in_function fncode,
5042 const char *name,
5043 enum built_in_class fnclass,
5044 tree fntype, tree libtype,
5045 bool both_p, bool fallback_p, bool nonansi_p,
5046 tree fnattrs, bool implicit_p);
5049 /* Apply the TYPE_QUALS to the new DECL. */
5051 void
5052 c_apply_type_quals_to_decl (int type_quals, tree decl)
5054 tree type = TREE_TYPE (decl);
5056 if (type == error_mark_node)
5057 return;
5059 if ((type_quals & TYPE_QUAL_CONST)
5060 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5061 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5062 constructor can produce constant init, so rely on cp_finish_decl to
5063 clear TREE_READONLY if the variable has non-constant init. */
5064 TREE_READONLY (decl) = 1;
5065 if (type_quals & TYPE_QUAL_VOLATILE)
5067 TREE_SIDE_EFFECTS (decl) = 1;
5068 TREE_THIS_VOLATILE (decl) = 1;
5070 if (type_quals & TYPE_QUAL_RESTRICT)
5072 while (type && TREE_CODE (type) == ARRAY_TYPE)
5073 /* Allow 'restrict' on arrays of pointers.
5074 FIXME currently we just ignore it. */
5075 type = TREE_TYPE (type);
5076 if (!type
5077 || !POINTER_TYPE_P (type)
5078 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
5079 error ("invalid use of %<restrict%>");
5083 struct c_type_hasher : ggc_hasher<tree>
5085 static hashval_t hash (tree);
5086 static bool equal (tree, tree);
5089 /* Hash function for the problem of multiple type definitions in
5090 different files. This must hash all types that will compare
5091 equal via comptypes to the same value. In practice it hashes
5092 on some of the simple stuff and leaves the details to comptypes. */
5094 hashval_t
5095 c_type_hasher::hash (tree t)
5097 int n_elements;
5098 int shift, size;
5099 tree t2;
5100 switch (TREE_CODE (t))
5102 /* For pointers, hash on pointee type plus some swizzling. */
5103 case POINTER_TYPE:
5104 return hash (TREE_TYPE (t)) ^ 0x3003003;
5105 /* Hash on number of elements and total size. */
5106 case ENUMERAL_TYPE:
5107 shift = 3;
5108 t2 = TYPE_VALUES (t);
5109 break;
5110 case RECORD_TYPE:
5111 shift = 0;
5112 t2 = TYPE_FIELDS (t);
5113 break;
5114 case QUAL_UNION_TYPE:
5115 shift = 1;
5116 t2 = TYPE_FIELDS (t);
5117 break;
5118 case UNION_TYPE:
5119 shift = 2;
5120 t2 = TYPE_FIELDS (t);
5121 break;
5122 default:
5123 gcc_unreachable ();
5125 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5126 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5127 n_elements = list_length (t2);
5128 /* We might have a VLA here. */
5129 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5130 size = 0;
5131 else
5132 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
5133 return ((size << 24) | (n_elements << shift));
5136 bool
5137 c_type_hasher::equal (tree t1, tree t2)
5139 return lang_hooks.types_compatible_p (t1, t2);
5142 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
5144 /* Return the typed-based alias set for T, which may be an expression
5145 or a type. Return -1 if we don't do anything special. */
5147 alias_set_type
5148 c_common_get_alias_set (tree t)
5150 tree u;
5152 /* For VLAs, use the alias set of the element type rather than the
5153 default of alias set 0 for types compared structurally. */
5154 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5156 if (TREE_CODE (t) == ARRAY_TYPE)
5157 return get_alias_set (TREE_TYPE (t));
5158 return -1;
5161 /* Permit type-punning when accessing a union, provided the access
5162 is directly through the union. For example, this code does not
5163 permit taking the address of a union member and then storing
5164 through it. Even the type-punning allowed here is a GCC
5165 extension, albeit a common and useful one; the C standard says
5166 that such accesses have implementation-defined behavior. */
5167 for (u = t;
5168 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5169 u = TREE_OPERAND (u, 0))
5170 if (TREE_CODE (u) == COMPONENT_REF
5171 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5172 return 0;
5174 /* That's all the expressions we handle specially. */
5175 if (!TYPE_P (t))
5176 return -1;
5178 /* The C standard guarantees that any object may be accessed via an
5179 lvalue that has character type. */
5180 if (t == char_type_node
5181 || t == signed_char_type_node
5182 || t == unsigned_char_type_node)
5183 return 0;
5185 /* The C standard specifically allows aliasing between signed and
5186 unsigned variants of the same type. We treat the signed
5187 variant as canonical. */
5188 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
5190 tree t1 = c_common_signed_type (t);
5192 /* t1 == t can happen for boolean nodes which are always unsigned. */
5193 if (t1 != t)
5194 return get_alias_set (t1);
5197 /* Handle the case of multiple type nodes referring to "the same" type,
5198 which occurs with IMA. These share an alias set. FIXME: Currently only
5199 C90 is handled. (In C99 type compatibility is not transitive, which
5200 complicates things mightily. The alias set splay trees can theoretically
5201 represent this, but insertion is tricky when you consider all the
5202 different orders things might arrive in.) */
5204 if (c_language != clk_c || flag_isoc99)
5205 return -1;
5207 /* Save time if there's only one input file. */
5208 if (num_in_fnames == 1)
5209 return -1;
5211 /* Pointers need special handling if they point to any type that
5212 needs special handling (below). */
5213 if (TREE_CODE (t) == POINTER_TYPE)
5215 tree t2;
5216 /* Find bottom type under any nested POINTERs. */
5217 for (t2 = TREE_TYPE (t);
5218 TREE_CODE (t2) == POINTER_TYPE;
5219 t2 = TREE_TYPE (t2))
5221 if (TREE_CODE (t2) != RECORD_TYPE
5222 && TREE_CODE (t2) != ENUMERAL_TYPE
5223 && TREE_CODE (t2) != QUAL_UNION_TYPE
5224 && TREE_CODE (t2) != UNION_TYPE)
5225 return -1;
5226 if (TYPE_SIZE (t2) == 0)
5227 return -1;
5229 /* These are the only cases that need special handling. */
5230 if (TREE_CODE (t) != RECORD_TYPE
5231 && TREE_CODE (t) != ENUMERAL_TYPE
5232 && TREE_CODE (t) != QUAL_UNION_TYPE
5233 && TREE_CODE (t) != UNION_TYPE
5234 && TREE_CODE (t) != POINTER_TYPE)
5235 return -1;
5236 /* Undefined? */
5237 if (TYPE_SIZE (t) == 0)
5238 return -1;
5240 /* Look up t in hash table. Only one of the compatible types within each
5241 alias set is recorded in the table. */
5242 if (!type_hash_table)
5243 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5244 tree *slot = type_hash_table->find_slot (t, INSERT);
5245 if (*slot != NULL)
5247 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5248 return TYPE_ALIAS_SET ((tree)*slot);
5250 else
5251 /* Our caller will assign and record (in t) a new alias set; all we need
5252 to do is remember t in the hash table. */
5253 *slot = t;
5255 return -1;
5258 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5259 the IS_SIZEOF parameter indicates which operator is being applied.
5260 The COMPLAIN flag controls whether we should diagnose possibly
5261 ill-formed constructs or not. LOC is the location of the SIZEOF or
5262 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5263 a type in any context should be returned, rather than the normal
5264 alignment for that type. */
5266 tree
5267 c_sizeof_or_alignof_type (location_t loc,
5268 tree type, bool is_sizeof, bool min_alignof,
5269 int complain)
5271 const char *op_name;
5272 tree value = NULL;
5273 enum tree_code type_code = TREE_CODE (type);
5275 op_name = is_sizeof ? "sizeof" : "__alignof__";
5277 if (type_code == FUNCTION_TYPE)
5279 if (is_sizeof)
5281 if (complain && warn_pointer_arith)
5282 pedwarn (loc, OPT_Wpointer_arith,
5283 "invalid application of %<sizeof%> to a function type");
5284 else if (!complain)
5285 return error_mark_node;
5286 value = size_one_node;
5288 else
5290 if (complain)
5292 if (c_dialect_cxx ())
5293 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5294 "%<alignof%> applied to a function type");
5295 else
5296 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5297 "%<_Alignof%> applied to a function type");
5299 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5302 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5304 if (type_code == VOID_TYPE
5305 && complain && warn_pointer_arith)
5306 pedwarn (loc, OPT_Wpointer_arith,
5307 "invalid application of %qs to a void type", op_name);
5308 else if (!complain)
5309 return error_mark_node;
5310 value = size_one_node;
5312 else if (!COMPLETE_TYPE_P (type)
5313 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5315 if (complain)
5316 error_at (loc, "invalid application of %qs to incomplete type %qT",
5317 op_name, type);
5318 return error_mark_node;
5320 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5321 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5323 if (complain)
5324 error_at (loc, "invalid application of %qs to array type %qT of "
5325 "incomplete element type", op_name, type);
5326 return error_mark_node;
5328 else
5330 if (is_sizeof)
5331 /* Convert in case a char is more than one unit. */
5332 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5333 size_int (TYPE_PRECISION (char_type_node)
5334 / BITS_PER_UNIT));
5335 else if (min_alignof)
5336 value = size_int (min_align_of_type (type));
5337 else
5338 value = size_int (TYPE_ALIGN_UNIT (type));
5341 /* VALUE will have the middle-end integer type sizetype.
5342 However, we should really return a value of type `size_t',
5343 which is just a typedef for an ordinary integer type. */
5344 value = fold_convert_loc (loc, size_type_node, value);
5346 return value;
5349 /* Implement the __alignof keyword: Return the minimum required
5350 alignment of EXPR, measured in bytes. For VAR_DECLs,
5351 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5352 from an "aligned" __attribute__ specification). LOC is the
5353 location of the ALIGNOF operator. */
5355 tree
5356 c_alignof_expr (location_t loc, tree expr)
5358 tree t;
5360 if (VAR_OR_FUNCTION_DECL_P (expr))
5361 t = size_int (DECL_ALIGN_UNIT (expr));
5363 else if (TREE_CODE (expr) == COMPONENT_REF
5364 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5366 error_at (loc, "%<__alignof%> applied to a bit-field");
5367 t = size_one_node;
5369 else if (TREE_CODE (expr) == COMPONENT_REF
5370 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5371 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5373 else if (TREE_CODE (expr) == INDIRECT_REF)
5375 tree t = TREE_OPERAND (expr, 0);
5376 tree best = t;
5377 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5379 while (CONVERT_EXPR_P (t)
5380 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5382 int thisalign;
5384 t = TREE_OPERAND (t, 0);
5385 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5386 if (thisalign > bestalign)
5387 best = t, bestalign = thisalign;
5389 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5391 else
5392 return c_alignof (loc, TREE_TYPE (expr));
5394 return fold_convert_loc (loc, size_type_node, t);
5397 /* Handle C and C++ default attributes. */
5399 enum built_in_attribute
5401 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5402 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5403 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5404 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5405 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5406 #include "builtin-attrs.def"
5407 #undef DEF_ATTR_NULL_TREE
5408 #undef DEF_ATTR_INT
5409 #undef DEF_ATTR_STRING
5410 #undef DEF_ATTR_IDENT
5411 #undef DEF_ATTR_TREE_LIST
5412 ATTR_LAST
5415 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5417 static void c_init_attributes (void);
5419 enum c_builtin_type
5421 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5422 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5423 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5424 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5425 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5426 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5427 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5428 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5429 ARG6) NAME,
5430 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5431 ARG6, ARG7) NAME,
5432 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5433 ARG6, ARG7, ARG8) NAME,
5434 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5435 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5436 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5437 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5438 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5439 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5440 NAME,
5441 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5442 ARG6, ARG7) NAME,
5443 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5444 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5445 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5446 #include "builtin-types.def"
5447 #undef DEF_PRIMITIVE_TYPE
5448 #undef DEF_FUNCTION_TYPE_0
5449 #undef DEF_FUNCTION_TYPE_1
5450 #undef DEF_FUNCTION_TYPE_2
5451 #undef DEF_FUNCTION_TYPE_3
5452 #undef DEF_FUNCTION_TYPE_4
5453 #undef DEF_FUNCTION_TYPE_5
5454 #undef DEF_FUNCTION_TYPE_6
5455 #undef DEF_FUNCTION_TYPE_7
5456 #undef DEF_FUNCTION_TYPE_8
5457 #undef DEF_FUNCTION_TYPE_VAR_0
5458 #undef DEF_FUNCTION_TYPE_VAR_1
5459 #undef DEF_FUNCTION_TYPE_VAR_2
5460 #undef DEF_FUNCTION_TYPE_VAR_3
5461 #undef DEF_FUNCTION_TYPE_VAR_4
5462 #undef DEF_FUNCTION_TYPE_VAR_5
5463 #undef DEF_FUNCTION_TYPE_VAR_7
5464 #undef DEF_FUNCTION_TYPE_VAR_11
5465 #undef DEF_POINTER_TYPE
5466 BT_LAST
5469 typedef enum c_builtin_type builtin_type;
5471 /* A temporary array for c_common_nodes_and_builtins. Used in
5472 communication with def_fn_type. */
5473 static tree builtin_types[(int) BT_LAST + 1];
5475 /* A helper function for c_common_nodes_and_builtins. Build function type
5476 for DEF with return type RET and N arguments. If VAR is true, then the
5477 function should be variadic after those N arguments.
5479 Takes special care not to ICE if any of the types involved are
5480 error_mark_node, which indicates that said type is not in fact available
5481 (see builtin_type_for_size). In which case the function type as a whole
5482 should be error_mark_node. */
5484 static void
5485 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5487 tree t;
5488 tree *args = XALLOCAVEC (tree, n);
5489 va_list list;
5490 int i;
5492 va_start (list, n);
5493 for (i = 0; i < n; ++i)
5495 builtin_type a = (builtin_type) va_arg (list, int);
5496 t = builtin_types[a];
5497 if (t == error_mark_node)
5498 goto egress;
5499 args[i] = t;
5502 t = builtin_types[ret];
5503 if (t == error_mark_node)
5504 goto egress;
5505 if (var)
5506 t = build_varargs_function_type_array (t, n, args);
5507 else
5508 t = build_function_type_array (t, n, args);
5510 egress:
5511 builtin_types[def] = t;
5512 va_end (list);
5515 /* Build builtin functions common to both C and C++ language
5516 frontends. */
5518 static void
5519 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5521 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5522 builtin_types[ENUM] = VALUE;
5523 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5524 def_fn_type (ENUM, RETURN, 0, 0);
5525 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5526 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5527 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5528 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5529 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5530 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5531 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5532 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5533 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5534 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5535 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5536 ARG6) \
5537 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5538 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5539 ARG6, ARG7) \
5540 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5541 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5542 ARG6, ARG7, ARG8) \
5543 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5544 ARG7, ARG8);
5545 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5546 def_fn_type (ENUM, RETURN, 1, 0);
5547 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5548 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5549 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5550 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5551 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5552 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5553 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5554 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5555 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5556 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5557 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5558 ARG6, ARG7) \
5559 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5560 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5561 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5562 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5563 ARG7, ARG8, ARG9, ARG10, ARG11);
5564 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5565 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5567 #include "builtin-types.def"
5569 #undef DEF_PRIMITIVE_TYPE
5570 #undef DEF_FUNCTION_TYPE_0
5571 #undef DEF_FUNCTION_TYPE_1
5572 #undef DEF_FUNCTION_TYPE_2
5573 #undef DEF_FUNCTION_TYPE_3
5574 #undef DEF_FUNCTION_TYPE_4
5575 #undef DEF_FUNCTION_TYPE_5
5576 #undef DEF_FUNCTION_TYPE_6
5577 #undef DEF_FUNCTION_TYPE_7
5578 #undef DEF_FUNCTION_TYPE_8
5579 #undef DEF_FUNCTION_TYPE_VAR_0
5580 #undef DEF_FUNCTION_TYPE_VAR_1
5581 #undef DEF_FUNCTION_TYPE_VAR_2
5582 #undef DEF_FUNCTION_TYPE_VAR_3
5583 #undef DEF_FUNCTION_TYPE_VAR_4
5584 #undef DEF_FUNCTION_TYPE_VAR_5
5585 #undef DEF_FUNCTION_TYPE_VAR_7
5586 #undef DEF_FUNCTION_TYPE_VAR_11
5587 #undef DEF_POINTER_TYPE
5588 builtin_types[(int) BT_LAST] = NULL_TREE;
5590 c_init_attributes ();
5592 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5593 NONANSI_P, ATTRS, IMPLICIT, COND) \
5594 if (NAME && COND) \
5595 def_builtin_1 (ENUM, NAME, CLASS, \
5596 builtin_types[(int) TYPE], \
5597 builtin_types[(int) LIBTYPE], \
5598 BOTH_P, FALLBACK_P, NONANSI_P, \
5599 built_in_attributes[(int) ATTRS], IMPLICIT);
5600 #include "builtins.def"
5601 #undef DEF_BUILTIN
5603 targetm.init_builtins ();
5605 build_common_builtin_nodes ();
5607 if (flag_cilkplus)
5608 cilk_init_builtins ();
5611 /* Like get_identifier, but avoid warnings about null arguments when
5612 the argument may be NULL for targets where GCC lacks stdint.h type
5613 information. */
5615 static inline tree
5616 c_get_ident (const char *id)
5618 return get_identifier (id);
5621 /* Build tree nodes and builtin functions common to both C and C++ language
5622 frontends. */
5624 void
5625 c_common_nodes_and_builtins (void)
5627 int char16_type_size;
5628 int char32_type_size;
5629 int wchar_type_size;
5630 tree array_domain_type;
5631 tree va_list_ref_type_node;
5632 tree va_list_arg_type_node;
5633 int i;
5635 build_common_tree_nodes (flag_signed_char, flag_short_double);
5637 /* Define `int' and `char' first so that dbx will output them first. */
5638 record_builtin_type (RID_INT, NULL, integer_type_node);
5639 record_builtin_type (RID_CHAR, "char", char_type_node);
5641 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5642 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5643 but not C. Are the conditionals here needed? */
5644 if (c_dialect_cxx ())
5645 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5646 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5647 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5648 record_builtin_type (RID_MAX, "long unsigned int",
5649 long_unsigned_type_node);
5651 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5653 char name[25];
5655 sprintf (name, "__int%d", int_n_data[i].bitsize);
5656 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5657 int_n_trees[i].signed_type);
5658 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5659 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5662 if (c_dialect_cxx ())
5663 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5664 record_builtin_type (RID_MAX, "long long int",
5665 long_long_integer_type_node);
5666 record_builtin_type (RID_MAX, "long long unsigned int",
5667 long_long_unsigned_type_node);
5668 if (c_dialect_cxx ())
5669 record_builtin_type (RID_MAX, "long long unsigned",
5670 long_long_unsigned_type_node);
5671 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5672 record_builtin_type (RID_MAX, "short unsigned int",
5673 short_unsigned_type_node);
5674 if (c_dialect_cxx ())
5675 record_builtin_type (RID_MAX, "unsigned short",
5676 short_unsigned_type_node);
5678 /* Define both `signed char' and `unsigned char'. */
5679 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5680 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5682 /* These are types that c_common_type_for_size and
5683 c_common_type_for_mode use. */
5684 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5685 TYPE_DECL, NULL_TREE,
5686 intQI_type_node));
5687 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5688 TYPE_DECL, NULL_TREE,
5689 intHI_type_node));
5690 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5691 TYPE_DECL, NULL_TREE,
5692 intSI_type_node));
5693 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5694 TYPE_DECL, NULL_TREE,
5695 intDI_type_node));
5696 #if HOST_BITS_PER_WIDE_INT >= 64
5697 /* Note that this is different than the __int128 type that's part of
5698 the generic __intN support. */
5699 if (targetm.scalar_mode_supported_p (TImode))
5700 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5701 TYPE_DECL,
5702 get_identifier ("__int128_t"),
5703 intTI_type_node));
5704 #endif
5705 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5706 TYPE_DECL, NULL_TREE,
5707 unsigned_intQI_type_node));
5708 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5709 TYPE_DECL, NULL_TREE,
5710 unsigned_intHI_type_node));
5711 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5712 TYPE_DECL, NULL_TREE,
5713 unsigned_intSI_type_node));
5714 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5715 TYPE_DECL, NULL_TREE,
5716 unsigned_intDI_type_node));
5717 #if HOST_BITS_PER_WIDE_INT >= 64
5718 if (targetm.scalar_mode_supported_p (TImode))
5719 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5720 TYPE_DECL,
5721 get_identifier ("__uint128_t"),
5722 unsigned_intTI_type_node));
5723 #endif
5725 /* Create the widest literal types. */
5726 widest_integer_literal_type_node
5727 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5728 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5729 TYPE_DECL, NULL_TREE,
5730 widest_integer_literal_type_node));
5732 widest_unsigned_literal_type_node
5733 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5734 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5735 TYPE_DECL, NULL_TREE,
5736 widest_unsigned_literal_type_node));
5738 signed_size_type_node = c_common_signed_type (size_type_node);
5740 pid_type_node =
5741 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5743 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5744 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5745 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5747 /* Only supported decimal floating point extension if the target
5748 actually supports underlying modes. */
5749 if (targetm.scalar_mode_supported_p (SDmode)
5750 && targetm.scalar_mode_supported_p (DDmode)
5751 && targetm.scalar_mode_supported_p (TDmode))
5753 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5754 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5755 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5758 if (targetm.fixed_point_supported_p ())
5760 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5761 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5762 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5763 record_builtin_type (RID_MAX, "long long _Fract",
5764 long_long_fract_type_node);
5765 record_builtin_type (RID_MAX, "unsigned short _Fract",
5766 unsigned_short_fract_type_node);
5767 record_builtin_type (RID_MAX, "unsigned _Fract",
5768 unsigned_fract_type_node);
5769 record_builtin_type (RID_MAX, "unsigned long _Fract",
5770 unsigned_long_fract_type_node);
5771 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5772 unsigned_long_long_fract_type_node);
5773 record_builtin_type (RID_MAX, "_Sat short _Fract",
5774 sat_short_fract_type_node);
5775 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5776 record_builtin_type (RID_MAX, "_Sat long _Fract",
5777 sat_long_fract_type_node);
5778 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5779 sat_long_long_fract_type_node);
5780 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5781 sat_unsigned_short_fract_type_node);
5782 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5783 sat_unsigned_fract_type_node);
5784 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5785 sat_unsigned_long_fract_type_node);
5786 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5787 sat_unsigned_long_long_fract_type_node);
5788 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5789 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5790 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5791 record_builtin_type (RID_MAX, "long long _Accum",
5792 long_long_accum_type_node);
5793 record_builtin_type (RID_MAX, "unsigned short _Accum",
5794 unsigned_short_accum_type_node);
5795 record_builtin_type (RID_MAX, "unsigned _Accum",
5796 unsigned_accum_type_node);
5797 record_builtin_type (RID_MAX, "unsigned long _Accum",
5798 unsigned_long_accum_type_node);
5799 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5800 unsigned_long_long_accum_type_node);
5801 record_builtin_type (RID_MAX, "_Sat short _Accum",
5802 sat_short_accum_type_node);
5803 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5804 record_builtin_type (RID_MAX, "_Sat long _Accum",
5805 sat_long_accum_type_node);
5806 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5807 sat_long_long_accum_type_node);
5808 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5809 sat_unsigned_short_accum_type_node);
5810 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5811 sat_unsigned_accum_type_node);
5812 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5813 sat_unsigned_long_accum_type_node);
5814 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5815 sat_unsigned_long_long_accum_type_node);
5819 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5820 TYPE_DECL,
5821 get_identifier ("complex int"),
5822 complex_integer_type_node));
5823 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5824 TYPE_DECL,
5825 get_identifier ("complex float"),
5826 complex_float_type_node));
5827 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5828 TYPE_DECL,
5829 get_identifier ("complex double"),
5830 complex_double_type_node));
5831 lang_hooks.decls.pushdecl
5832 (build_decl (UNKNOWN_LOCATION,
5833 TYPE_DECL, get_identifier ("complex long double"),
5834 complex_long_double_type_node));
5836 if (c_dialect_cxx ())
5837 /* For C++, make fileptr_type_node a distinct void * type until
5838 FILE type is defined. */
5839 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5841 record_builtin_type (RID_VOID, NULL, void_type_node);
5843 /* Set the TYPE_NAME for any variants that were built before
5844 record_builtin_type gave names to the built-in types. */
5846 tree void_name = TYPE_NAME (void_type_node);
5847 TYPE_NAME (void_type_node) = NULL_TREE;
5848 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5849 = void_name;
5850 TYPE_NAME (void_type_node) = void_name;
5853 void_list_node = build_void_list_node ();
5855 /* Make a type to be the domain of a few array types
5856 whose domains don't really matter.
5857 200 is small enough that it always fits in size_t
5858 and large enough that it can hold most function names for the
5859 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5860 array_domain_type = build_index_type (size_int (200));
5862 /* Make a type for arrays of characters.
5863 With luck nothing will ever really depend on the length of this
5864 array type. */
5865 char_array_type_node
5866 = build_array_type (char_type_node, array_domain_type);
5868 string_type_node = build_pointer_type (char_type_node);
5869 const_string_type_node
5870 = build_pointer_type (build_qualified_type
5871 (char_type_node, TYPE_QUAL_CONST));
5873 /* This is special for C++ so functions can be overloaded. */
5874 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5875 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5876 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5877 underlying_wchar_type_node = wchar_type_node;
5878 if (c_dialect_cxx ())
5880 if (TYPE_UNSIGNED (wchar_type_node))
5881 wchar_type_node = make_unsigned_type (wchar_type_size);
5882 else
5883 wchar_type_node = make_signed_type (wchar_type_size);
5884 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5887 /* This is for wide string constants. */
5888 wchar_array_type_node
5889 = build_array_type (wchar_type_node, array_domain_type);
5891 /* Define 'char16_t'. */
5892 char16_type_node = get_identifier (CHAR16_TYPE);
5893 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5894 char16_type_size = TYPE_PRECISION (char16_type_node);
5895 if (c_dialect_cxx ())
5897 char16_type_node = make_unsigned_type (char16_type_size);
5899 if (cxx_dialect >= cxx11)
5900 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5903 /* This is for UTF-16 string constants. */
5904 char16_array_type_node
5905 = build_array_type (char16_type_node, array_domain_type);
5907 /* Define 'char32_t'. */
5908 char32_type_node = get_identifier (CHAR32_TYPE);
5909 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5910 char32_type_size = TYPE_PRECISION (char32_type_node);
5911 if (c_dialect_cxx ())
5913 char32_type_node = make_unsigned_type (char32_type_size);
5915 if (cxx_dialect >= cxx11)
5916 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5919 /* This is for UTF-32 string constants. */
5920 char32_array_type_node
5921 = build_array_type (char32_type_node, array_domain_type);
5923 wint_type_node =
5924 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5926 intmax_type_node =
5927 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5928 uintmax_type_node =
5929 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5931 if (SIG_ATOMIC_TYPE)
5932 sig_atomic_type_node =
5933 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5934 if (INT8_TYPE)
5935 int8_type_node =
5936 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5937 if (INT16_TYPE)
5938 int16_type_node =
5939 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5940 if (INT32_TYPE)
5941 int32_type_node =
5942 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5943 if (INT64_TYPE)
5944 int64_type_node =
5945 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5946 if (UINT8_TYPE)
5947 uint8_type_node =
5948 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5949 if (UINT16_TYPE)
5950 c_uint16_type_node = uint16_type_node =
5951 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5952 if (UINT32_TYPE)
5953 c_uint32_type_node = uint32_type_node =
5954 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5955 if (UINT64_TYPE)
5956 c_uint64_type_node = uint64_type_node =
5957 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5958 if (INT_LEAST8_TYPE)
5959 int_least8_type_node =
5960 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5961 if (INT_LEAST16_TYPE)
5962 int_least16_type_node =
5963 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5964 if (INT_LEAST32_TYPE)
5965 int_least32_type_node =
5966 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5967 if (INT_LEAST64_TYPE)
5968 int_least64_type_node =
5969 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5970 if (UINT_LEAST8_TYPE)
5971 uint_least8_type_node =
5972 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5973 if (UINT_LEAST16_TYPE)
5974 uint_least16_type_node =
5975 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5976 if (UINT_LEAST32_TYPE)
5977 uint_least32_type_node =
5978 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5979 if (UINT_LEAST64_TYPE)
5980 uint_least64_type_node =
5981 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5982 if (INT_FAST8_TYPE)
5983 int_fast8_type_node =
5984 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5985 if (INT_FAST16_TYPE)
5986 int_fast16_type_node =
5987 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5988 if (INT_FAST32_TYPE)
5989 int_fast32_type_node =
5990 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5991 if (INT_FAST64_TYPE)
5992 int_fast64_type_node =
5993 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5994 if (UINT_FAST8_TYPE)
5995 uint_fast8_type_node =
5996 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5997 if (UINT_FAST16_TYPE)
5998 uint_fast16_type_node =
5999 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
6000 if (UINT_FAST32_TYPE)
6001 uint_fast32_type_node =
6002 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
6003 if (UINT_FAST64_TYPE)
6004 uint_fast64_type_node =
6005 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
6006 if (INTPTR_TYPE)
6007 intptr_type_node =
6008 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
6009 if (UINTPTR_TYPE)
6010 uintptr_type_node =
6011 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
6013 default_function_type
6014 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6015 ptrdiff_type_node
6016 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
6017 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6019 lang_hooks.decls.pushdecl
6020 (build_decl (UNKNOWN_LOCATION,
6021 TYPE_DECL, get_identifier ("__builtin_va_list"),
6022 va_list_type_node));
6023 if (targetm.enum_va_list_p)
6025 int l;
6026 const char *pname;
6027 tree ptype;
6029 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
6031 lang_hooks.decls.pushdecl
6032 (build_decl (UNKNOWN_LOCATION,
6033 TYPE_DECL, get_identifier (pname),
6034 ptype));
6039 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
6041 va_list_arg_type_node = va_list_ref_type_node =
6042 build_pointer_type (TREE_TYPE (va_list_type_node));
6044 else
6046 va_list_arg_type_node = va_list_type_node;
6047 va_list_ref_type_node = build_reference_type (va_list_type_node);
6050 if (!flag_preprocess_only)
6051 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
6053 main_identifier_node = get_identifier ("main");
6055 /* Create the built-in __null node. It is important that this is
6056 not shared. */
6057 null_node = make_int_cst (1, 1);
6058 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
6060 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6061 memset (builtin_types, 0, sizeof (builtin_types));
6064 /* The number of named compound-literals generated thus far. */
6065 static GTY(()) int compound_literal_number;
6067 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6069 void
6070 set_compound_literal_name (tree decl)
6072 char *name;
6073 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6074 compound_literal_number);
6075 compound_literal_number++;
6076 DECL_NAME (decl) = get_identifier (name);
6079 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6080 TYPE and operand OP. */
6082 static tree
6083 build_va_arg_1 (location_t loc, tree type, tree op)
6085 tree expr = build1 (VA_ARG_EXPR, type, op);
6086 SET_EXPR_LOCATION (expr, loc);
6087 return expr;
6090 /* Return a VA_ARG_EXPR corresponding to a source-level expression
6091 va_arg (EXPR, TYPE) at source location LOC. */
6093 tree
6094 build_va_arg (location_t loc, tree expr, tree type)
6096 tree va_type = TREE_TYPE (expr);
6097 tree canon_va_type = (va_type == error_mark_node
6098 ? NULL_TREE
6099 : targetm.canonical_va_list_type (va_type));
6101 if (va_type == error_mark_node
6102 || canon_va_type == NULL_TREE)
6104 /* Let's handle things neutrallly, if expr:
6105 - has undeclared type, or
6106 - is not an va_list type. */
6107 return build_va_arg_1 (loc, type, expr);
6110 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6112 /* Case 1: Not an array type. */
6114 /* Take the address, to get '&ap'. */
6115 mark_addressable (expr);
6116 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6118 /* Verify that &ap is still recognized as having va_list type. */
6119 tree canon_expr_type
6120 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6121 gcc_assert (canon_expr_type != NULL_TREE);
6123 return build_va_arg_1 (loc, type, expr);
6126 /* Case 2: Array type.
6128 Background:
6130 For contrast, let's start with the simple case (case 1). If
6131 canon_va_type is not an array type, but say a char *, then when
6132 passing-by-value a va_list, the type of the va_list param decl is
6133 the same as for another va_list decl (all ap's are char *):
6135 f2_1 (char * ap)
6136 D.1815 = VA_ARG (&ap, 0B, 1);
6137 return D.1815;
6139 f2 (int i)
6140 char * ap.0;
6141 char * ap;
6142 __builtin_va_start (&ap, 0);
6143 ap.0 = ap;
6144 res = f2_1 (ap.0);
6145 __builtin_va_end (&ap);
6146 D.1812 = res;
6147 return D.1812;
6149 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6150 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6151 the same as for another va_list decl (case 2a, struct ap[1]).
6153 f2_1 (struct * ap)
6154 D.1844 = VA_ARG (ap, 0B, 0);
6155 return D.1844;
6157 f2 (int i)
6158 struct ap[1];
6159 __builtin_va_start (&ap, 0);
6160 res = f2_1 (&ap);
6161 __builtin_va_end (&ap);
6162 D.1841 = res;
6163 return D.1841;
6165 Case 2b is different because:
6166 - on the callee side, the parm decl has declared type va_list, but
6167 grokdeclarator changes the type of the parm decl to a pointer to the
6168 array elem type.
6169 - on the caller side, the pass-by-value uses &ap.
6171 We unify these two cases (case 2a: va_list is array type,
6172 case 2b: va_list is pointer to array elem type), by adding '&' for the
6173 array type case, such that we have a pointer to array elem in both
6174 cases. */
6176 if (TREE_CODE (va_type) == ARRAY_TYPE)
6178 /* Case 2a: va_list is array type. */
6180 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6181 elem type. */
6182 mark_addressable (expr);
6183 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6184 expr);
6186 /* Verify that &ap is still recognized as having va_list type. */
6187 tree canon_expr_type
6188 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6189 gcc_assert (canon_expr_type != NULL_TREE);
6191 else
6193 /* Case 2b: va_list is pointer to array elem type. */
6194 gcc_assert (POINTER_TYPE_P (va_type));
6195 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6197 /* Don't take the address. We've already got '&ap'. */
6201 return build_va_arg_1 (loc, type, expr);
6205 /* Linked list of disabled built-in functions. */
6207 typedef struct disabled_builtin
6209 const char *name;
6210 struct disabled_builtin *next;
6211 } disabled_builtin;
6212 static disabled_builtin *disabled_builtins = NULL;
6214 static bool builtin_function_disabled_p (const char *);
6216 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6217 begins with "__builtin_", give an error. */
6219 void
6220 disable_builtin_function (const char *name)
6222 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
6223 error ("cannot disable built-in function %qs", name);
6224 else
6226 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6227 new_disabled_builtin->name = name;
6228 new_disabled_builtin->next = disabled_builtins;
6229 disabled_builtins = new_disabled_builtin;
6234 /* Return true if the built-in function NAME has been disabled, false
6235 otherwise. */
6237 static bool
6238 builtin_function_disabled_p (const char *name)
6240 disabled_builtin *p;
6241 for (p = disabled_builtins; p != NULL; p = p->next)
6243 if (strcmp (name, p->name) == 0)
6244 return true;
6246 return false;
6250 /* Worker for DEF_BUILTIN.
6251 Possibly define a builtin function with one or two names.
6252 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6253 nonansi_p and flag_no_nonansi_builtin. */
6255 static void
6256 def_builtin_1 (enum built_in_function fncode,
6257 const char *name,
6258 enum built_in_class fnclass,
6259 tree fntype, tree libtype,
6260 bool both_p, bool fallback_p, bool nonansi_p,
6261 tree fnattrs, bool implicit_p)
6263 tree decl;
6264 const char *libname;
6266 if (fntype == error_mark_node)
6267 return;
6269 gcc_assert ((!both_p && !fallback_p)
6270 || !strncmp (name, "__builtin_",
6271 strlen ("__builtin_")));
6273 libname = name + strlen ("__builtin_");
6274 decl = add_builtin_function (name, fntype, fncode, fnclass,
6275 (fallback_p ? libname : NULL),
6276 fnattrs);
6278 set_builtin_decl (fncode, decl, implicit_p);
6280 if (both_p
6281 && !flag_no_builtin && !builtin_function_disabled_p (libname)
6282 && !(nonansi_p && flag_no_nonansi_builtin))
6283 add_builtin_function (libname, libtype, fncode, fnclass,
6284 NULL, fnattrs);
6287 /* Nonzero if the type T promotes to int. This is (nearly) the
6288 integral promotions defined in ISO C99 6.3.1.1/2. */
6290 bool
6291 c_promoting_integer_type_p (const_tree t)
6293 switch (TREE_CODE (t))
6295 case INTEGER_TYPE:
6296 return (TYPE_MAIN_VARIANT (t) == char_type_node
6297 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6298 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6299 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
6300 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6301 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
6303 case ENUMERAL_TYPE:
6304 /* ??? Technically all enumerations not larger than an int
6305 promote to an int. But this is used along code paths
6306 that only want to notice a size change. */
6307 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6309 case BOOLEAN_TYPE:
6310 return 1;
6312 default:
6313 return 0;
6317 /* Return 1 if PARMS specifies a fixed number of parameters
6318 and none of their types is affected by default promotions. */
6321 self_promoting_args_p (const_tree parms)
6323 const_tree t;
6324 for (t = parms; t; t = TREE_CHAIN (t))
6326 tree type = TREE_VALUE (t);
6328 if (type == error_mark_node)
6329 continue;
6331 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6332 return 0;
6334 if (type == 0)
6335 return 0;
6337 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6338 return 0;
6340 if (c_promoting_integer_type_p (type))
6341 return 0;
6343 return 1;
6346 /* Recursively remove any '*' or '&' operator from TYPE. */
6347 tree
6348 strip_pointer_operator (tree t)
6350 while (POINTER_TYPE_P (t))
6351 t = TREE_TYPE (t);
6352 return t;
6355 /* Recursively remove pointer or array type from TYPE. */
6356 tree
6357 strip_pointer_or_array_types (tree t)
6359 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6360 t = TREE_TYPE (t);
6361 return t;
6364 /* Used to compare case labels. K1 and K2 are actually tree nodes
6365 representing case labels, or NULL_TREE for a `default' label.
6366 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6367 K2, and 0 if K1 and K2 are equal. */
6370 case_compare (splay_tree_key k1, splay_tree_key k2)
6372 /* Consider a NULL key (such as arises with a `default' label) to be
6373 smaller than anything else. */
6374 if (!k1)
6375 return k2 ? -1 : 0;
6376 else if (!k2)
6377 return k1 ? 1 : 0;
6379 return tree_int_cst_compare ((tree) k1, (tree) k2);
6382 /* Process a case label, located at LOC, for the range LOW_VALUE
6383 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6384 then this case label is actually a `default' label. If only
6385 HIGH_VALUE is NULL_TREE, then case label was declared using the
6386 usual C/C++ syntax, rather than the GNU case range extension.
6387 CASES is a tree containing all the case ranges processed so far;
6388 COND is the condition for the switch-statement itself. Returns the
6389 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6390 is created. */
6392 tree
6393 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6394 tree low_value, tree high_value)
6396 tree type;
6397 tree label;
6398 tree case_label;
6399 splay_tree_node node;
6401 /* Create the LABEL_DECL itself. */
6402 label = create_artificial_label (loc);
6404 /* If there was an error processing the switch condition, bail now
6405 before we get more confused. */
6406 if (!cond || cond == error_mark_node)
6407 goto error_out;
6409 if ((low_value && TREE_TYPE (low_value)
6410 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6411 || (high_value && TREE_TYPE (high_value)
6412 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6414 error_at (loc, "pointers are not permitted as case values");
6415 goto error_out;
6418 /* Case ranges are a GNU extension. */
6419 if (high_value)
6420 pedwarn (loc, OPT_Wpedantic,
6421 "range expressions in switch statements are non-standard");
6423 type = TREE_TYPE (cond);
6424 if (low_value)
6426 low_value = check_case_value (loc, low_value);
6427 low_value = convert_and_check (loc, type, low_value);
6428 if (low_value == error_mark_node)
6429 goto error_out;
6431 if (high_value)
6433 high_value = check_case_value (loc, high_value);
6434 high_value = convert_and_check (loc, type, high_value);
6435 if (high_value == error_mark_node)
6436 goto error_out;
6439 if (low_value && high_value)
6441 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6442 really a case range, even though it was written that way.
6443 Remove the HIGH_VALUE to simplify later processing. */
6444 if (tree_int_cst_equal (low_value, high_value))
6445 high_value = NULL_TREE;
6446 else if (!tree_int_cst_lt (low_value, high_value))
6447 warning_at (loc, 0, "empty range specified");
6450 /* See if the case is in range of the type of the original testing
6451 expression. If both low_value and high_value are out of range,
6452 don't insert the case label and return NULL_TREE. */
6453 if (low_value
6454 && !check_case_bounds (loc, type, orig_type,
6455 &low_value, high_value ? &high_value : NULL))
6456 return NULL_TREE;
6458 /* Look up the LOW_VALUE in the table of case labels we already
6459 have. */
6460 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6461 /* If there was not an exact match, check for overlapping ranges.
6462 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6463 that's a `default' label and the only overlap is an exact match. */
6464 if (!node && (low_value || high_value))
6466 splay_tree_node low_bound;
6467 splay_tree_node high_bound;
6469 /* Even though there wasn't an exact match, there might be an
6470 overlap between this case range and another case range.
6471 Since we've (inductively) not allowed any overlapping case
6472 ranges, we simply need to find the greatest low case label
6473 that is smaller that LOW_VALUE, and the smallest low case
6474 label that is greater than LOW_VALUE. If there is an overlap
6475 it will occur in one of these two ranges. */
6476 low_bound = splay_tree_predecessor (cases,
6477 (splay_tree_key) low_value);
6478 high_bound = splay_tree_successor (cases,
6479 (splay_tree_key) low_value);
6481 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6482 the LOW_VALUE, so there is no need to check unless the
6483 LOW_BOUND is in fact itself a case range. */
6484 if (low_bound
6485 && CASE_HIGH ((tree) low_bound->value)
6486 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6487 low_value) >= 0)
6488 node = low_bound;
6489 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6490 range is bigger than the low end of the current range, so we
6491 are only interested if the current range is a real range, and
6492 not an ordinary case label. */
6493 else if (high_bound
6494 && high_value
6495 && (tree_int_cst_compare ((tree) high_bound->key,
6496 high_value)
6497 <= 0))
6498 node = high_bound;
6500 /* If there was an overlap, issue an error. */
6501 if (node)
6503 tree duplicate = CASE_LABEL ((tree) node->value);
6505 if (high_value)
6507 error_at (loc, "duplicate (or overlapping) case value");
6508 error_at (DECL_SOURCE_LOCATION (duplicate),
6509 "this is the first entry overlapping that value");
6511 else if (low_value)
6513 error_at (loc, "duplicate case value") ;
6514 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6516 else
6518 error_at (loc, "multiple default labels in one switch");
6519 error_at (DECL_SOURCE_LOCATION (duplicate),
6520 "this is the first default label");
6522 goto error_out;
6525 /* Add a CASE_LABEL to the statement-tree. */
6526 case_label = add_stmt (build_case_label (low_value, high_value, label));
6527 /* Register this case label in the splay tree. */
6528 splay_tree_insert (cases,
6529 (splay_tree_key) low_value,
6530 (splay_tree_value) case_label);
6532 return case_label;
6534 error_out:
6535 /* Add a label so that the back-end doesn't think that the beginning of
6536 the switch is unreachable. Note that we do not add a case label, as
6537 that just leads to duplicates and thence to failure later on. */
6538 if (!cases->root)
6540 tree t = create_artificial_label (loc);
6541 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6543 return error_mark_node;
6546 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6547 Used to verify that case values match up with enumerator values. */
6549 static void
6550 match_case_to_enum_1 (tree key, tree type, tree label)
6552 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6554 if (tree_fits_uhwi_p (key))
6555 print_dec (key, buf, UNSIGNED);
6556 else if (tree_fits_shwi_p (key))
6557 print_dec (key, buf, SIGNED);
6558 else
6559 print_hex (key, buf);
6561 if (TYPE_NAME (type) == 0)
6562 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6563 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6564 "case value %qs not in enumerated type",
6565 buf);
6566 else
6567 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6568 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6569 "case value %qs not in enumerated type %qT",
6570 buf, type);
6573 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6574 Used to verify that case values match up with enumerator values. */
6576 static int
6577 match_case_to_enum (splay_tree_node node, void *data)
6579 tree label = (tree) node->value;
6580 tree type = (tree) data;
6582 /* Skip default case. */
6583 if (!CASE_LOW (label))
6584 return 0;
6586 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6587 when we did our enum->case scan. Reset our scratch bit after. */
6588 if (!CASE_LOW_SEEN (label))
6589 match_case_to_enum_1 (CASE_LOW (label), type, label);
6590 else
6591 CASE_LOW_SEEN (label) = 0;
6593 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6594 not set, that means that CASE_HIGH did not appear when we did our
6595 enum->case scan. Reset our scratch bit after. */
6596 if (CASE_HIGH (label))
6598 if (!CASE_HIGH_SEEN (label))
6599 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6600 else
6601 CASE_HIGH_SEEN (label) = 0;
6604 return 0;
6607 /* Handle -Wswitch*. Called from the front end after parsing the
6608 switch construct. */
6609 /* ??? Should probably be somewhere generic, since other languages
6610 besides C and C++ would want this. At the moment, however, C/C++
6611 are the only tree-ssa languages that support enumerations at all,
6612 so the point is moot. */
6614 void
6615 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6616 tree type, tree cond)
6618 splay_tree_node default_node;
6619 splay_tree_node node;
6620 tree chain;
6622 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6623 return;
6625 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6626 if (!default_node)
6627 warning_at (switch_location, OPT_Wswitch_default,
6628 "switch missing default case");
6630 /* From here on, we only care about about enumerated types. */
6631 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6632 return;
6634 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6635 if (!warn_switch_enum && !warn_switch)
6636 return;
6638 /* Check the cases. Warn about case values which are not members of
6639 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6640 there is no default case, check that exactly all enumeration
6641 literals are covered by the cases. */
6643 /* Clearing COND if it is not an integer constant simplifies
6644 the tests inside the loop below. */
6645 if (TREE_CODE (cond) != INTEGER_CST)
6646 cond = NULL_TREE;
6648 /* The time complexity here is O(N*lg(N)) worst case, but for the
6649 common case of monotonically increasing enumerators, it is
6650 O(N), since the nature of the splay tree will keep the next
6651 element adjacent to the root at all times. */
6653 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6655 tree value = TREE_VALUE (chain);
6656 if (TREE_CODE (value) == CONST_DECL)
6657 value = DECL_INITIAL (value);
6658 node = splay_tree_lookup (cases, (splay_tree_key) value);
6659 if (node)
6661 /* Mark the CASE_LOW part of the case entry as seen. */
6662 tree label = (tree) node->value;
6663 CASE_LOW_SEEN (label) = 1;
6664 continue;
6667 /* Even though there wasn't an exact match, there might be a
6668 case range which includes the enumerator's value. */
6669 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6670 if (node && CASE_HIGH ((tree) node->value))
6672 tree label = (tree) node->value;
6673 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6674 if (cmp >= 0)
6676 /* If we match the upper bound exactly, mark the CASE_HIGH
6677 part of the case entry as seen. */
6678 if (cmp == 0)
6679 CASE_HIGH_SEEN (label) = 1;
6680 continue;
6684 /* We've now determined that this enumerated literal isn't
6685 handled by the case labels of the switch statement. */
6687 /* If the switch expression is a constant, we only really care
6688 about whether that constant is handled by the switch. */
6689 if (cond && tree_int_cst_compare (cond, value))
6690 continue;
6692 /* If there is a default_node, the only relevant option is
6693 Wswitch-enum. Otherwise, if both are enabled then we prefer
6694 to warn using -Wswitch because -Wswitch is enabled by -Wall
6695 while -Wswitch-enum is explicit. */
6696 warning_at (switch_location,
6697 (default_node || !warn_switch
6698 ? OPT_Wswitch_enum
6699 : OPT_Wswitch),
6700 "enumeration value %qE not handled in switch",
6701 TREE_PURPOSE (chain));
6704 /* Warn if there are case expressions that don't correspond to
6705 enumerators. This can occur since C and C++ don't enforce
6706 type-checking of assignments to enumeration variables.
6708 The time complexity here is now always O(N) worst case, since
6709 we should have marked both the lower bound and upper bound of
6710 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6711 above. This scan also resets those fields. */
6713 splay_tree_foreach (cases, match_case_to_enum, type);
6716 /* Finish an expression taking the address of LABEL (an
6717 IDENTIFIER_NODE). Returns an expression for the address.
6719 LOC is the location for the expression returned. */
6721 tree
6722 finish_label_address_expr (tree label, location_t loc)
6724 tree result;
6726 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6728 if (label == error_mark_node)
6729 return error_mark_node;
6731 label = lookup_label (label);
6732 if (label == NULL_TREE)
6733 result = null_pointer_node;
6734 else
6736 TREE_USED (label) = 1;
6737 result = build1 (ADDR_EXPR, ptr_type_node, label);
6738 /* The current function is not necessarily uninlinable.
6739 Computed gotos are incompatible with inlining, but the value
6740 here could be used only in a diagnostic, for example. */
6741 protected_set_expr_location (result, loc);
6744 return result;
6748 /* Given a boolean expression ARG, return a tree representing an increment
6749 or decrement (as indicated by CODE) of ARG. The front end must check for
6750 invalid cases (e.g., decrement in C++). */
6751 tree
6752 boolean_increment (enum tree_code code, tree arg)
6754 tree val;
6755 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6757 arg = stabilize_reference (arg);
6758 switch (code)
6760 case PREINCREMENT_EXPR:
6761 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6762 break;
6763 case POSTINCREMENT_EXPR:
6764 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6765 arg = save_expr (arg);
6766 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6767 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6768 break;
6769 case PREDECREMENT_EXPR:
6770 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6771 invert_truthvalue_loc (input_location, arg));
6772 break;
6773 case POSTDECREMENT_EXPR:
6774 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6775 invert_truthvalue_loc (input_location, arg));
6776 arg = save_expr (arg);
6777 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6778 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6779 break;
6780 default:
6781 gcc_unreachable ();
6783 TREE_SIDE_EFFECTS (val) = 1;
6784 return val;
6787 /* Built-in macros for stddef.h and stdint.h, that require macros
6788 defined in this file. */
6789 void
6790 c_stddef_cpp_builtins(void)
6792 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6793 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6794 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6795 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6796 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6797 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6798 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6799 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6800 if (SIG_ATOMIC_TYPE)
6801 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6802 if (INT8_TYPE)
6803 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6804 if (INT16_TYPE)
6805 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6806 if (INT32_TYPE)
6807 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6808 if (INT64_TYPE)
6809 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6810 if (UINT8_TYPE)
6811 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6812 if (UINT16_TYPE)
6813 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6814 if (UINT32_TYPE)
6815 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6816 if (UINT64_TYPE)
6817 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6818 if (INT_LEAST8_TYPE)
6819 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6820 if (INT_LEAST16_TYPE)
6821 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6822 if (INT_LEAST32_TYPE)
6823 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6824 if (INT_LEAST64_TYPE)
6825 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6826 if (UINT_LEAST8_TYPE)
6827 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6828 if (UINT_LEAST16_TYPE)
6829 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6830 if (UINT_LEAST32_TYPE)
6831 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6832 if (UINT_LEAST64_TYPE)
6833 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6834 if (INT_FAST8_TYPE)
6835 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6836 if (INT_FAST16_TYPE)
6837 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6838 if (INT_FAST32_TYPE)
6839 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6840 if (INT_FAST64_TYPE)
6841 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6842 if (UINT_FAST8_TYPE)
6843 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6844 if (UINT_FAST16_TYPE)
6845 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6846 if (UINT_FAST32_TYPE)
6847 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6848 if (UINT_FAST64_TYPE)
6849 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6850 if (INTPTR_TYPE)
6851 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6852 if (UINTPTR_TYPE)
6853 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6856 static void
6857 c_init_attributes (void)
6859 /* Fill in the built_in_attributes array. */
6860 #define DEF_ATTR_NULL_TREE(ENUM) \
6861 built_in_attributes[(int) ENUM] = NULL_TREE;
6862 #define DEF_ATTR_INT(ENUM, VALUE) \
6863 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6864 #define DEF_ATTR_STRING(ENUM, VALUE) \
6865 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6866 #define DEF_ATTR_IDENT(ENUM, STRING) \
6867 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6868 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6869 built_in_attributes[(int) ENUM] \
6870 = tree_cons (built_in_attributes[(int) PURPOSE], \
6871 built_in_attributes[(int) VALUE], \
6872 built_in_attributes[(int) CHAIN]);
6873 #include "builtin-attrs.def"
6874 #undef DEF_ATTR_NULL_TREE
6875 #undef DEF_ATTR_INT
6876 #undef DEF_ATTR_IDENT
6877 #undef DEF_ATTR_TREE_LIST
6880 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6881 identifier as an argument, so the front end shouldn't look it up. */
6883 bool
6884 attribute_takes_identifier_p (const_tree attr_id)
6886 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6887 if (spec == NULL)
6888 /* Unknown attribute that we'll end up ignoring, return true so we
6889 don't complain about an identifier argument. */
6890 return true;
6891 else if (!strcmp ("mode", spec->name)
6892 || !strcmp ("format", spec->name)
6893 || !strcmp ("cleanup", spec->name))
6894 return true;
6895 else
6896 return targetm.attribute_takes_identifier_p (attr_id);
6899 /* Attribute handlers common to C front ends. */
6901 /* Handle a "packed" attribute; arguments as in
6902 struct attribute_spec.handler. */
6904 static tree
6905 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6906 int flags, bool *no_add_attrs)
6908 if (TYPE_P (*node))
6910 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6911 *node = build_variant_type_copy (*node);
6912 TYPE_PACKED (*node) = 1;
6914 else if (TREE_CODE (*node) == FIELD_DECL)
6916 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6917 /* Still pack bitfields. */
6918 && ! DECL_INITIAL (*node))
6919 warning (OPT_Wattributes,
6920 "%qE attribute ignored for field of type %qT",
6921 name, TREE_TYPE (*node));
6922 else
6923 DECL_PACKED (*node) = 1;
6925 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6926 used for DECL_REGISTER. It wouldn't mean anything anyway.
6927 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6928 that changes what the typedef is typing. */
6929 else
6931 warning (OPT_Wattributes, "%qE attribute ignored", name);
6932 *no_add_attrs = true;
6935 return NULL_TREE;
6938 /* Handle a "nocommon" attribute; arguments as in
6939 struct attribute_spec.handler. */
6941 static tree
6942 handle_nocommon_attribute (tree *node, tree name,
6943 tree ARG_UNUSED (args),
6944 int ARG_UNUSED (flags), bool *no_add_attrs)
6946 if (TREE_CODE (*node) == VAR_DECL)
6947 DECL_COMMON (*node) = 0;
6948 else
6950 warning (OPT_Wattributes, "%qE attribute ignored", name);
6951 *no_add_attrs = true;
6954 return NULL_TREE;
6957 /* Handle a "common" attribute; arguments as in
6958 struct attribute_spec.handler. */
6960 static tree
6961 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6962 int ARG_UNUSED (flags), bool *no_add_attrs)
6964 if (TREE_CODE (*node) == VAR_DECL)
6965 DECL_COMMON (*node) = 1;
6966 else
6968 warning (OPT_Wattributes, "%qE attribute ignored", name);
6969 *no_add_attrs = true;
6972 return NULL_TREE;
6975 /* Handle a "noreturn" attribute; arguments as in
6976 struct attribute_spec.handler. */
6978 static tree
6979 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6980 int ARG_UNUSED (flags), bool *no_add_attrs)
6982 tree type = TREE_TYPE (*node);
6984 /* See FIXME comment in c_common_attribute_table. */
6985 if (TREE_CODE (*node) == FUNCTION_DECL
6986 || objc_method_decl (TREE_CODE (*node)))
6987 TREE_THIS_VOLATILE (*node) = 1;
6988 else if (TREE_CODE (type) == POINTER_TYPE
6989 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6990 TREE_TYPE (*node)
6991 = (build_qualified_type
6992 (build_pointer_type
6993 (build_type_variant (TREE_TYPE (type),
6994 TYPE_READONLY (TREE_TYPE (type)), 1)),
6995 TYPE_QUALS (type)));
6996 else
6998 warning (OPT_Wattributes, "%qE attribute ignored", name);
6999 *no_add_attrs = true;
7002 return NULL_TREE;
7005 /* Handle a "hot" and attribute; arguments as in
7006 struct attribute_spec.handler. */
7008 static tree
7009 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7010 int ARG_UNUSED (flags), bool *no_add_attrs)
7012 if (TREE_CODE (*node) == FUNCTION_DECL
7013 || TREE_CODE (*node) == LABEL_DECL)
7015 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7017 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7018 "with attribute %qs", name, "cold");
7019 *no_add_attrs = true;
7021 /* Most of the rest of the hot processing is done later with
7022 lookup_attribute. */
7024 else
7026 warning (OPT_Wattributes, "%qE attribute ignored", name);
7027 *no_add_attrs = true;
7030 return NULL_TREE;
7033 /* Handle a "cold" and attribute; arguments as in
7034 struct attribute_spec.handler. */
7036 static tree
7037 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7038 int ARG_UNUSED (flags), bool *no_add_attrs)
7040 if (TREE_CODE (*node) == FUNCTION_DECL
7041 || TREE_CODE (*node) == LABEL_DECL)
7043 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7045 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7046 "with attribute %qs", name, "hot");
7047 *no_add_attrs = true;
7049 /* Most of the rest of the cold processing is done later with
7050 lookup_attribute. */
7052 else
7054 warning (OPT_Wattributes, "%qE attribute ignored", name);
7055 *no_add_attrs = true;
7058 return NULL_TREE;
7061 /* Handle a "no_sanitize_address" attribute; arguments as in
7062 struct attribute_spec.handler. */
7064 static tree
7065 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7066 bool *no_add_attrs)
7068 if (TREE_CODE (*node) != FUNCTION_DECL)
7070 warning (OPT_Wattributes, "%qE attribute ignored", name);
7071 *no_add_attrs = true;
7074 return NULL_TREE;
7077 /* Handle a "no_address_safety_analysis" attribute; arguments as in
7078 struct attribute_spec.handler. */
7080 static tree
7081 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7082 bool *no_add_attrs)
7084 if (TREE_CODE (*node) != FUNCTION_DECL)
7085 warning (OPT_Wattributes, "%qE attribute ignored", name);
7086 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7087 DECL_ATTRIBUTES (*node)
7088 = tree_cons (get_identifier ("no_sanitize_address"),
7089 NULL_TREE, DECL_ATTRIBUTES (*node));
7090 *no_add_attrs = true;
7091 return NULL_TREE;
7094 /* Handle a "no_sanitize_undefined" attribute; arguments as in
7095 struct attribute_spec.handler. */
7097 static tree
7098 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7099 bool *no_add_attrs)
7101 if (TREE_CODE (*node) != FUNCTION_DECL)
7103 warning (OPT_Wattributes, "%qE attribute ignored", name);
7104 *no_add_attrs = true;
7107 return NULL_TREE;
7110 /* Handle a "stack_protect" attribute; arguments as in
7111 struct attribute_spec.handler. */
7112 static tree
7113 handle_stack_protect_attribute (tree *node, tree name, tree, int,
7114 bool *no_add_attrs)
7116 if (TREE_CODE (*node) != FUNCTION_DECL)
7118 warning (OPT_Wattributes, "%qE attribute ignored", name);
7119 *no_add_attrs = true;
7121 else
7122 DECL_ATTRIBUTES (*node)
7123 = tree_cons (get_identifier ("stack_protect"),
7124 NULL_TREE, DECL_ATTRIBUTES (*node));
7126 return NULL_TREE;
7129 /* Handle a "noinline" attribute; arguments as in
7130 struct attribute_spec.handler. */
7132 static tree
7133 handle_noinline_attribute (tree *node, tree name,
7134 tree ARG_UNUSED (args),
7135 int ARG_UNUSED (flags), bool *no_add_attrs)
7137 if (TREE_CODE (*node) == FUNCTION_DECL)
7139 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7141 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7142 "with attribute %qs", name, "always_inline");
7143 *no_add_attrs = true;
7145 else
7146 DECL_UNINLINABLE (*node) = 1;
7148 else
7150 warning (OPT_Wattributes, "%qE attribute ignored", name);
7151 *no_add_attrs = true;
7154 return NULL_TREE;
7157 /* Handle a "noclone" attribute; arguments as in
7158 struct attribute_spec.handler. */
7160 static tree
7161 handle_noclone_attribute (tree *node, tree name,
7162 tree ARG_UNUSED (args),
7163 int ARG_UNUSED (flags), bool *no_add_attrs)
7165 if (TREE_CODE (*node) != FUNCTION_DECL)
7167 warning (OPT_Wattributes, "%qE attribute ignored", name);
7168 *no_add_attrs = true;
7171 return NULL_TREE;
7174 /* Handle a "no_icf" attribute; arguments as in
7175 struct attribute_spec.handler. */
7177 static tree
7178 handle_noicf_attribute (tree *node, tree name,
7179 tree ARG_UNUSED (args),
7180 int ARG_UNUSED (flags), bool *no_add_attrs)
7182 if (TREE_CODE (*node) != FUNCTION_DECL)
7184 warning (OPT_Wattributes, "%qE attribute ignored", name);
7185 *no_add_attrs = true;
7188 return NULL_TREE;
7192 /* Handle a "always_inline" attribute; arguments as in
7193 struct attribute_spec.handler. */
7195 static tree
7196 handle_always_inline_attribute (tree *node, tree name,
7197 tree ARG_UNUSED (args),
7198 int ARG_UNUSED (flags),
7199 bool *no_add_attrs)
7201 if (TREE_CODE (*node) == FUNCTION_DECL)
7203 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7205 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7206 "with %qs attribute", name, "noinline");
7207 *no_add_attrs = true;
7209 else
7210 /* Set the attribute and mark it for disregarding inline
7211 limits. */
7212 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
7214 else
7216 warning (OPT_Wattributes, "%qE attribute ignored", name);
7217 *no_add_attrs = true;
7220 return NULL_TREE;
7223 /* Handle a "gnu_inline" attribute; arguments as in
7224 struct attribute_spec.handler. */
7226 static tree
7227 handle_gnu_inline_attribute (tree *node, tree name,
7228 tree ARG_UNUSED (args),
7229 int ARG_UNUSED (flags),
7230 bool *no_add_attrs)
7232 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7234 /* Do nothing else, just set the attribute. We'll get at
7235 it later with lookup_attribute. */
7237 else
7239 warning (OPT_Wattributes, "%qE attribute ignored", name);
7240 *no_add_attrs = true;
7243 return NULL_TREE;
7246 /* Handle a "leaf" attribute; arguments as in
7247 struct attribute_spec.handler. */
7249 static tree
7250 handle_leaf_attribute (tree *node, tree name,
7251 tree ARG_UNUSED (args),
7252 int ARG_UNUSED (flags), bool *no_add_attrs)
7254 if (TREE_CODE (*node) != FUNCTION_DECL)
7256 warning (OPT_Wattributes, "%qE attribute ignored", name);
7257 *no_add_attrs = true;
7259 if (!TREE_PUBLIC (*node))
7261 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7262 *no_add_attrs = true;
7265 return NULL_TREE;
7268 /* Handle an "artificial" attribute; arguments as in
7269 struct attribute_spec.handler. */
7271 static tree
7272 handle_artificial_attribute (tree *node, tree name,
7273 tree ARG_UNUSED (args),
7274 int ARG_UNUSED (flags),
7275 bool *no_add_attrs)
7277 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7279 /* Do nothing else, just set the attribute. We'll get at
7280 it later with lookup_attribute. */
7282 else
7284 warning (OPT_Wattributes, "%qE attribute ignored", name);
7285 *no_add_attrs = true;
7288 return NULL_TREE;
7291 /* Handle a "flatten" attribute; arguments as in
7292 struct attribute_spec.handler. */
7294 static tree
7295 handle_flatten_attribute (tree *node, tree name,
7296 tree args ATTRIBUTE_UNUSED,
7297 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7299 if (TREE_CODE (*node) == FUNCTION_DECL)
7300 /* Do nothing else, just set the attribute. We'll get at
7301 it later with lookup_attribute. */
7303 else
7305 warning (OPT_Wattributes, "%qE attribute ignored", name);
7306 *no_add_attrs = true;
7309 return NULL_TREE;
7312 /* Handle a "warning" or "error" attribute; arguments as in
7313 struct attribute_spec.handler. */
7315 static tree
7316 handle_error_attribute (tree *node, tree name, tree args,
7317 int ARG_UNUSED (flags), bool *no_add_attrs)
7319 if (TREE_CODE (*node) == FUNCTION_DECL
7320 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7321 /* Do nothing else, just set the attribute. We'll get at
7322 it later with lookup_attribute. */
7324 else
7326 warning (OPT_Wattributes, "%qE attribute ignored", name);
7327 *no_add_attrs = true;
7330 return NULL_TREE;
7333 /* Handle a "used" attribute; arguments as in
7334 struct attribute_spec.handler. */
7336 static tree
7337 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7338 int ARG_UNUSED (flags), bool *no_add_attrs)
7340 tree node = *pnode;
7342 if (TREE_CODE (node) == FUNCTION_DECL
7343 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7344 || (TREE_CODE (node) == TYPE_DECL))
7346 TREE_USED (node) = 1;
7347 DECL_PRESERVE_P (node) = 1;
7348 if (TREE_CODE (node) == VAR_DECL)
7349 DECL_READ_P (node) = 1;
7351 else
7353 warning (OPT_Wattributes, "%qE attribute ignored", name);
7354 *no_add_attrs = true;
7357 return NULL_TREE;
7360 /* Handle a "unused" attribute; arguments as in
7361 struct attribute_spec.handler. */
7363 static tree
7364 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7365 int flags, bool *no_add_attrs)
7367 if (DECL_P (*node))
7369 tree decl = *node;
7371 if (TREE_CODE (decl) == PARM_DECL
7372 || TREE_CODE (decl) == VAR_DECL
7373 || TREE_CODE (decl) == FUNCTION_DECL
7374 || TREE_CODE (decl) == LABEL_DECL
7375 || TREE_CODE (decl) == TYPE_DECL)
7377 TREE_USED (decl) = 1;
7378 if (TREE_CODE (decl) == VAR_DECL
7379 || TREE_CODE (decl) == PARM_DECL)
7380 DECL_READ_P (decl) = 1;
7382 else
7384 warning (OPT_Wattributes, "%qE attribute ignored", name);
7385 *no_add_attrs = true;
7388 else
7390 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7391 *node = build_variant_type_copy (*node);
7392 TREE_USED (*node) = 1;
7395 return NULL_TREE;
7398 /* Handle a "externally_visible" attribute; arguments as in
7399 struct attribute_spec.handler. */
7401 static tree
7402 handle_externally_visible_attribute (tree *pnode, tree name,
7403 tree ARG_UNUSED (args),
7404 int ARG_UNUSED (flags),
7405 bool *no_add_attrs)
7407 tree node = *pnode;
7409 if (VAR_OR_FUNCTION_DECL_P (node))
7411 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7412 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7414 warning (OPT_Wattributes,
7415 "%qE attribute have effect only on public objects", name);
7416 *no_add_attrs = true;
7419 else
7421 warning (OPT_Wattributes, "%qE attribute ignored", name);
7422 *no_add_attrs = true;
7425 return NULL_TREE;
7428 /* Handle the "no_reorder" attribute. Arguments as in
7429 struct attribute_spec.handler. */
7431 static tree
7432 handle_no_reorder_attribute (tree *pnode,
7433 tree name,
7434 tree,
7435 int,
7436 bool *no_add_attrs)
7438 tree node = *pnode;
7440 if (!VAR_OR_FUNCTION_DECL_P (node)
7441 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7443 warning (OPT_Wattributes,
7444 "%qE attribute only affects top level objects",
7445 name);
7446 *no_add_attrs = true;
7449 return NULL_TREE;
7452 /* Handle a "const" attribute; arguments as in
7453 struct attribute_spec.handler. */
7455 static tree
7456 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7457 int ARG_UNUSED (flags), bool *no_add_attrs)
7459 tree type = TREE_TYPE (*node);
7461 /* See FIXME comment on noreturn in c_common_attribute_table. */
7462 if (TREE_CODE (*node) == FUNCTION_DECL)
7463 TREE_READONLY (*node) = 1;
7464 else if (TREE_CODE (type) == POINTER_TYPE
7465 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7466 TREE_TYPE (*node)
7467 = (build_qualified_type
7468 (build_pointer_type
7469 (build_type_variant (TREE_TYPE (type), 1,
7470 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7471 TYPE_QUALS (type)));
7472 else
7474 warning (OPT_Wattributes, "%qE attribute ignored", name);
7475 *no_add_attrs = true;
7478 return NULL_TREE;
7481 /* Handle a "transparent_union" attribute; arguments as in
7482 struct attribute_spec.handler. */
7484 static tree
7485 handle_transparent_union_attribute (tree *node, tree name,
7486 tree ARG_UNUSED (args), int flags,
7487 bool *no_add_attrs)
7489 tree type;
7491 *no_add_attrs = true;
7494 if (TREE_CODE (*node) == TYPE_DECL
7495 && ! (flags & ATTR_FLAG_CXX11))
7496 node = &TREE_TYPE (*node);
7497 type = *node;
7499 if (TREE_CODE (type) == UNION_TYPE)
7501 /* Make sure that the first field will work for a transparent union.
7502 If the type isn't complete yet, leave the check to the code in
7503 finish_struct. */
7504 if (TYPE_SIZE (type))
7506 tree first = first_field (type);
7507 if (first == NULL_TREE
7508 || DECL_ARTIFICIAL (first)
7509 || TYPE_MODE (type) != DECL_MODE (first))
7510 goto ignored;
7513 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7515 /* If the type isn't complete yet, setting the flag
7516 on a variant wouldn't ever be checked. */
7517 if (!TYPE_SIZE (type))
7518 goto ignored;
7520 /* build_duplicate_type doesn't work for C++. */
7521 if (c_dialect_cxx ())
7522 goto ignored;
7524 /* A type variant isn't good enough, since we don't a cast
7525 to such a type removed as a no-op. */
7526 *node = type = build_duplicate_type (type);
7529 TYPE_TRANSPARENT_AGGR (type) = 1;
7530 return NULL_TREE;
7533 ignored:
7534 warning (OPT_Wattributes, "%qE attribute ignored", name);
7535 return NULL_TREE;
7538 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7539 get the requested priority for a constructor or destructor,
7540 possibly issuing diagnostics for invalid or reserved
7541 priorities. */
7543 static priority_type
7544 get_priority (tree args, bool is_destructor)
7546 HOST_WIDE_INT pri;
7547 tree arg;
7549 if (!args)
7550 return DEFAULT_INIT_PRIORITY;
7552 if (!SUPPORTS_INIT_PRIORITY)
7554 if (is_destructor)
7555 error ("destructor priorities are not supported");
7556 else
7557 error ("constructor priorities are not supported");
7558 return DEFAULT_INIT_PRIORITY;
7561 arg = TREE_VALUE (args);
7562 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7563 goto invalid;
7564 if (arg == error_mark_node)
7565 return DEFAULT_INIT_PRIORITY;
7566 arg = default_conversion (arg);
7567 if (!tree_fits_shwi_p (arg)
7568 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7569 goto invalid;
7571 pri = tree_to_shwi (arg);
7572 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7573 goto invalid;
7575 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7577 if (is_destructor)
7578 warning (0,
7579 "destructor priorities from 0 to %d are reserved "
7580 "for the implementation",
7581 MAX_RESERVED_INIT_PRIORITY);
7582 else
7583 warning (0,
7584 "constructor priorities from 0 to %d are reserved "
7585 "for the implementation",
7586 MAX_RESERVED_INIT_PRIORITY);
7588 return pri;
7590 invalid:
7591 if (is_destructor)
7592 error ("destructor priorities must be integers from 0 to %d inclusive",
7593 MAX_INIT_PRIORITY);
7594 else
7595 error ("constructor priorities must be integers from 0 to %d inclusive",
7596 MAX_INIT_PRIORITY);
7597 return DEFAULT_INIT_PRIORITY;
7600 /* Handle a "constructor" attribute; arguments as in
7601 struct attribute_spec.handler. */
7603 static tree
7604 handle_constructor_attribute (tree *node, tree name, tree args,
7605 int ARG_UNUSED (flags),
7606 bool *no_add_attrs)
7608 tree decl = *node;
7609 tree type = TREE_TYPE (decl);
7611 if (TREE_CODE (decl) == FUNCTION_DECL
7612 && TREE_CODE (type) == FUNCTION_TYPE
7613 && decl_function_context (decl) == 0)
7615 priority_type priority;
7616 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7617 priority = get_priority (args, /*is_destructor=*/false);
7618 SET_DECL_INIT_PRIORITY (decl, priority);
7619 TREE_USED (decl) = 1;
7621 else
7623 warning (OPT_Wattributes, "%qE attribute ignored", name);
7624 *no_add_attrs = true;
7627 return NULL_TREE;
7630 /* Handle a "destructor" attribute; arguments as in
7631 struct attribute_spec.handler. */
7633 static tree
7634 handle_destructor_attribute (tree *node, tree name, tree args,
7635 int ARG_UNUSED (flags),
7636 bool *no_add_attrs)
7638 tree decl = *node;
7639 tree type = TREE_TYPE (decl);
7641 if (TREE_CODE (decl) == FUNCTION_DECL
7642 && TREE_CODE (type) == FUNCTION_TYPE
7643 && decl_function_context (decl) == 0)
7645 priority_type priority;
7646 DECL_STATIC_DESTRUCTOR (decl) = 1;
7647 priority = get_priority (args, /*is_destructor=*/true);
7648 SET_DECL_FINI_PRIORITY (decl, priority);
7649 TREE_USED (decl) = 1;
7651 else
7653 warning (OPT_Wattributes, "%qE attribute ignored", name);
7654 *no_add_attrs = true;
7657 return NULL_TREE;
7660 /* Nonzero if the mode is a valid vector mode for this architecture.
7661 This returns nonzero even if there is no hardware support for the
7662 vector mode, but we can emulate with narrower modes. */
7664 static int
7665 vector_mode_valid_p (machine_mode mode)
7667 enum mode_class mclass = GET_MODE_CLASS (mode);
7668 machine_mode innermode;
7670 /* Doh! What's going on? */
7671 if (mclass != MODE_VECTOR_INT
7672 && mclass != MODE_VECTOR_FLOAT
7673 && mclass != MODE_VECTOR_FRACT
7674 && mclass != MODE_VECTOR_UFRACT
7675 && mclass != MODE_VECTOR_ACCUM
7676 && mclass != MODE_VECTOR_UACCUM)
7677 return 0;
7679 /* Hardware support. Woo hoo! */
7680 if (targetm.vector_mode_supported_p (mode))
7681 return 1;
7683 innermode = GET_MODE_INNER (mode);
7685 /* We should probably return 1 if requesting V4DI and we have no DI,
7686 but we have V2DI, but this is probably very unlikely. */
7688 /* If we have support for the inner mode, we can safely emulate it.
7689 We may not have V2DI, but me can emulate with a pair of DIs. */
7690 return targetm.scalar_mode_supported_p (innermode);
7694 /* Handle a "mode" attribute; arguments as in
7695 struct attribute_spec.handler. */
7697 static tree
7698 handle_mode_attribute (tree *node, tree name, tree args,
7699 int ARG_UNUSED (flags), bool *no_add_attrs)
7701 tree type = *node;
7702 tree ident = TREE_VALUE (args);
7704 *no_add_attrs = true;
7706 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7707 warning (OPT_Wattributes, "%qE attribute ignored", name);
7708 else
7710 int j;
7711 const char *p = IDENTIFIER_POINTER (ident);
7712 int len = strlen (p);
7713 machine_mode mode = VOIDmode;
7714 tree typefm;
7715 bool valid_mode;
7717 if (len > 4 && p[0] == '_' && p[1] == '_'
7718 && p[len - 1] == '_' && p[len - 2] == '_')
7720 char *newp = (char *) alloca (len - 1);
7722 strcpy (newp, &p[2]);
7723 newp[len - 4] = '\0';
7724 p = newp;
7727 /* Change this type to have a type with the specified mode.
7728 First check for the special modes. */
7729 if (!strcmp (p, "byte"))
7730 mode = byte_mode;
7731 else if (!strcmp (p, "word"))
7732 mode = word_mode;
7733 else if (!strcmp (p, "pointer"))
7734 mode = ptr_mode;
7735 else if (!strcmp (p, "libgcc_cmp_return"))
7736 mode = targetm.libgcc_cmp_return_mode ();
7737 else if (!strcmp (p, "libgcc_shift_count"))
7738 mode = targetm.libgcc_shift_count_mode ();
7739 else if (!strcmp (p, "unwind_word"))
7740 mode = targetm.unwind_word_mode ();
7741 else
7742 for (j = 0; j < NUM_MACHINE_MODES; j++)
7743 if (!strcmp (p, GET_MODE_NAME (j)))
7745 mode = (machine_mode) j;
7746 break;
7749 if (mode == VOIDmode)
7751 error ("unknown machine mode %qE", ident);
7752 return NULL_TREE;
7755 valid_mode = false;
7756 switch (GET_MODE_CLASS (mode))
7758 case MODE_INT:
7759 case MODE_PARTIAL_INT:
7760 case MODE_FLOAT:
7761 case MODE_DECIMAL_FLOAT:
7762 case MODE_FRACT:
7763 case MODE_UFRACT:
7764 case MODE_ACCUM:
7765 case MODE_UACCUM:
7766 valid_mode = targetm.scalar_mode_supported_p (mode);
7767 break;
7769 case MODE_COMPLEX_INT:
7770 case MODE_COMPLEX_FLOAT:
7771 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7772 break;
7774 case MODE_VECTOR_INT:
7775 case MODE_VECTOR_FLOAT:
7776 case MODE_VECTOR_FRACT:
7777 case MODE_VECTOR_UFRACT:
7778 case MODE_VECTOR_ACCUM:
7779 case MODE_VECTOR_UACCUM:
7780 warning (OPT_Wattributes, "specifying vector types with "
7781 "__attribute__ ((mode)) is deprecated");
7782 warning (OPT_Wattributes,
7783 "use __attribute__ ((vector_size)) instead");
7784 valid_mode = vector_mode_valid_p (mode);
7785 break;
7787 default:
7788 break;
7790 if (!valid_mode)
7792 error ("unable to emulate %qs", p);
7793 return NULL_TREE;
7796 if (POINTER_TYPE_P (type))
7798 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7799 tree (*fn)(tree, machine_mode, bool);
7801 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7803 error ("invalid pointer mode %qs", p);
7804 return NULL_TREE;
7807 if (TREE_CODE (type) == POINTER_TYPE)
7808 fn = build_pointer_type_for_mode;
7809 else
7810 fn = build_reference_type_for_mode;
7811 typefm = fn (TREE_TYPE (type), mode, false);
7813 else
7815 /* For fixed-point modes, we need to test if the signness of type
7816 and the machine mode are consistent. */
7817 if (ALL_FIXED_POINT_MODE_P (mode)
7818 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7820 error ("signedness of type and machine mode %qs don%'t match", p);
7821 return NULL_TREE;
7823 /* For fixed-point modes, we need to pass saturating info. */
7824 typefm = lang_hooks.types.type_for_mode (mode,
7825 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7826 : TYPE_UNSIGNED (type));
7829 if (typefm == NULL_TREE)
7831 error ("no data type for mode %qs", p);
7832 return NULL_TREE;
7834 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7836 /* For enumeral types, copy the precision from the integer
7837 type returned above. If not an INTEGER_TYPE, we can't use
7838 this mode for this type. */
7839 if (TREE_CODE (typefm) != INTEGER_TYPE)
7841 error ("cannot use mode %qs for enumeral types", p);
7842 return NULL_TREE;
7845 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7847 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7848 typefm = type;
7850 else
7852 /* We cannot build a type variant, as there's code that assumes
7853 that TYPE_MAIN_VARIANT has the same mode. This includes the
7854 debug generators. Instead, create a subrange type. This
7855 results in all of the enumeral values being emitted only once
7856 in the original, and the subtype gets them by reference. */
7857 if (TYPE_UNSIGNED (type))
7858 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7859 else
7860 typefm = make_signed_type (TYPE_PRECISION (typefm));
7861 TREE_TYPE (typefm) = type;
7864 else if (VECTOR_MODE_P (mode)
7865 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7866 : TREE_CODE (type) != TREE_CODE (typefm))
7868 error ("mode %qs applied to inappropriate type", p);
7869 return NULL_TREE;
7872 *node = typefm;
7875 return NULL_TREE;
7878 /* Handle a "section" attribute; arguments as in
7879 struct attribute_spec.handler. */
7881 static tree
7882 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7883 int ARG_UNUSED (flags), bool *no_add_attrs)
7885 tree decl = *node;
7887 if (!targetm_common.have_named_sections)
7889 error_at (DECL_SOURCE_LOCATION (*node),
7890 "section attributes are not supported for this target");
7891 goto fail;
7894 user_defined_section_attribute = true;
7896 if (!VAR_OR_FUNCTION_DECL_P (decl))
7898 error ("section attribute not allowed for %q+D", *node);
7899 goto fail;
7902 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7904 error ("section attribute argument not a string constant");
7905 goto fail;
7908 if (TREE_CODE (decl) == VAR_DECL
7909 && current_function_decl != NULL_TREE
7910 && !TREE_STATIC (decl))
7912 error_at (DECL_SOURCE_LOCATION (decl),
7913 "section attribute cannot be specified for local variables");
7914 goto fail;
7917 /* The decl may have already been given a section attribute
7918 from a previous declaration. Ensure they match. */
7919 if (DECL_SECTION_NAME (decl) != NULL
7920 && strcmp (DECL_SECTION_NAME (decl),
7921 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7923 error ("section of %q+D conflicts with previous declaration", *node);
7924 goto fail;
7927 if (TREE_CODE (decl) == VAR_DECL
7928 && !targetm.have_tls && targetm.emutls.tmpl_section
7929 && DECL_THREAD_LOCAL_P (decl))
7931 error ("section of %q+D cannot be overridden", *node);
7932 goto fail;
7935 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7936 return NULL_TREE;
7938 fail:
7939 *no_add_attrs = true;
7940 return NULL_TREE;
7943 /* Check whether ALIGN is a valid user-specified alignment. If so,
7944 return its base-2 log; if not, output an error and return -1. If
7945 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7946 no error. */
7948 check_user_alignment (const_tree align, bool allow_zero)
7950 int i;
7952 if (error_operand_p (align))
7953 return -1;
7954 if (TREE_CODE (align) != INTEGER_CST
7955 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7957 error ("requested alignment is not an integer constant");
7958 return -1;
7960 else if (allow_zero && integer_zerop (align))
7961 return -1;
7962 else if (tree_int_cst_sgn (align) == -1
7963 || (i = tree_log2 (align)) == -1)
7965 error ("requested alignment is not a positive power of 2");
7966 return -1;
7968 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7970 error ("requested alignment is too large");
7971 return -1;
7973 return i;
7977 If in c++-11, check if the c++-11 alignment constraint with respect
7978 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7979 c++-11 mode, does nothing.
7981 [dcl.align]2/ says:
7983 [* if the constant expression evaluates to a fundamental alignment,
7984 the alignment requirement of the declared entity shall be the
7985 specified fundamental alignment.
7987 * if the constant expression evaluates to an extended alignment
7988 and the implementation supports that alignment in the context
7989 of the declaration, the alignment of the declared entity shall
7990 be that alignment
7992 * if the constant expression evaluates to an extended alignment
7993 and the implementation does not support that alignment in the
7994 context of the declaration, the program is ill-formed]. */
7996 static bool
7997 check_cxx_fundamental_alignment_constraints (tree node,
7998 unsigned align_log,
7999 int flags)
8001 bool alignment_too_large_p = false;
8002 unsigned requested_alignment = 1U << align_log;
8003 unsigned max_align = 0;
8005 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8006 || (node == NULL_TREE || node == error_mark_node))
8007 return true;
8009 if (cxx_fundamental_alignment_p (requested_alignment))
8010 return true;
8012 if (DECL_P (node))
8014 if (TREE_STATIC (node))
8016 /* For file scope variables and static members, the target
8017 supports alignments that are at most
8018 MAX_OFILE_ALIGNMENT. */
8019 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8020 alignment_too_large_p = true;
8022 else
8024 #ifdef BIGGEST_FIELD_ALIGNMENT
8025 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8026 #else
8027 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8028 #endif
8029 /* For non-static members, the target supports either
8030 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8031 if it is defined or BIGGEST_ALIGNMENT. */
8032 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8033 if (TREE_CODE (node) == FIELD_DECL
8034 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8035 alignment_too_large_p = true;
8036 #undef MAX_TARGET_FIELD_ALIGNMENT
8037 /* For stack variables, the target supports at most
8038 MAX_STACK_ALIGNMENT. */
8039 else if (decl_function_context (node) != NULL
8040 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8041 alignment_too_large_p = true;
8044 else if (TYPE_P (node))
8046 /* Let's be liberal for types. */
8047 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8048 alignment_too_large_p = true;
8051 if (alignment_too_large_p)
8052 pedwarn (input_location, OPT_Wattributes,
8053 "requested alignment %d is larger than %d",
8054 requested_alignment, max_align);
8056 return !alignment_too_large_p;
8059 /* Handle a "aligned" attribute; arguments as in
8060 struct attribute_spec.handler. */
8062 static tree
8063 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8064 int flags, bool *no_add_attrs)
8066 tree decl = NULL_TREE;
8067 tree *type = NULL;
8068 int is_type = 0;
8069 tree align_expr;
8070 int i;
8072 if (args)
8074 align_expr = TREE_VALUE (args);
8075 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8076 && TREE_CODE (align_expr) != FUNCTION_DECL)
8077 align_expr = default_conversion (align_expr);
8079 else
8080 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8082 if (DECL_P (*node))
8084 decl = *node;
8085 type = &TREE_TYPE (decl);
8086 is_type = TREE_CODE (*node) == TYPE_DECL;
8088 else if (TYPE_P (*node))
8089 type = node, is_type = 1;
8091 if ((i = check_user_alignment (align_expr, false)) == -1
8092 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
8093 *no_add_attrs = true;
8094 else if (is_type)
8096 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8097 /* OK, modify the type in place. */;
8098 /* If we have a TYPE_DECL, then copy the type, so that we
8099 don't accidentally modify a builtin type. See pushdecl. */
8100 else if (decl && TREE_TYPE (decl) != error_mark_node
8101 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
8103 tree tt = TREE_TYPE (decl);
8104 *type = build_variant_type_copy (*type);
8105 DECL_ORIGINAL_TYPE (decl) = tt;
8106 TYPE_NAME (*type) = decl;
8107 TREE_USED (*type) = TREE_USED (decl);
8108 TREE_TYPE (decl) = *type;
8110 else
8111 *type = build_variant_type_copy (*type);
8113 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
8114 TYPE_USER_ALIGN (*type) = 1;
8116 else if (! VAR_OR_FUNCTION_DECL_P (decl)
8117 && TREE_CODE (decl) != FIELD_DECL)
8119 error ("alignment may not be specified for %q+D", decl);
8120 *no_add_attrs = true;
8122 else if (DECL_USER_ALIGN (decl)
8123 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8124 /* C++-11 [dcl.align/4]:
8126 When multiple alignment-specifiers are specified for an
8127 entity, the alignment requirement shall be set to the
8128 strictest specified alignment.
8130 This formally comes from the c++11 specification but we are
8131 doing it for the GNU attribute syntax as well. */
8132 *no_add_attrs = true;
8133 else if (TREE_CODE (decl) == FUNCTION_DECL
8134 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8136 if (DECL_USER_ALIGN (decl))
8137 error ("alignment for %q+D was previously specified as %d "
8138 "and may not be decreased", decl,
8139 DECL_ALIGN (decl) / BITS_PER_UNIT);
8140 else
8141 error ("alignment for %q+D must be at least %d", decl,
8142 DECL_ALIGN (decl) / BITS_PER_UNIT);
8143 *no_add_attrs = true;
8145 else
8147 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
8148 DECL_USER_ALIGN (decl) = 1;
8151 return NULL_TREE;
8154 /* Handle a "weak" attribute; arguments as in
8155 struct attribute_spec.handler. */
8157 static tree
8158 handle_weak_attribute (tree *node, tree name,
8159 tree ARG_UNUSED (args),
8160 int ARG_UNUSED (flags),
8161 bool * ARG_UNUSED (no_add_attrs))
8163 if (TREE_CODE (*node) == FUNCTION_DECL
8164 && DECL_DECLARED_INLINE_P (*node))
8166 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
8167 *no_add_attrs = true;
8169 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8171 error ("indirect function %q+D cannot be declared weak", *node);
8172 *no_add_attrs = true;
8173 return NULL_TREE;
8175 else if (VAR_OR_FUNCTION_DECL_P (*node))
8177 struct symtab_node *n = symtab_node::get (*node);
8178 if (n && n->refuse_visibility_changes)
8179 error ("%+D declared weak after being used", *node);
8180 declare_weak (*node);
8182 else
8183 warning (OPT_Wattributes, "%qE attribute ignored", name);
8185 return NULL_TREE;
8188 /* Handle an "alias" or "ifunc" attribute; arguments as in
8189 struct attribute_spec.handler, except that IS_ALIAS tells us
8190 whether this is an alias as opposed to ifunc attribute. */
8192 static tree
8193 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8194 bool *no_add_attrs)
8196 tree decl = *node;
8198 if (TREE_CODE (decl) != FUNCTION_DECL
8199 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
8201 warning (OPT_Wattributes, "%qE attribute ignored", name);
8202 *no_add_attrs = true;
8204 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
8205 || (TREE_CODE (decl) != FUNCTION_DECL
8206 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8207 /* A static variable declaration is always a tentative definition,
8208 but the alias is a non-tentative definition which overrides. */
8209 || (TREE_CODE (decl) != FUNCTION_DECL
8210 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
8212 error ("%q+D defined both normally and as %qE attribute", decl, name);
8213 *no_add_attrs = true;
8214 return NULL_TREE;
8216 else if (!is_alias
8217 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8218 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8220 error ("weak %q+D cannot be defined %qE", decl, name);
8221 *no_add_attrs = true;
8222 return NULL_TREE;
8225 /* Note that the very first time we process a nested declaration,
8226 decl_function_context will not be set. Indeed, *would* never
8227 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8228 we do below. After such frobbery, pushdecl would set the context.
8229 In any case, this is never what we want. */
8230 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
8232 tree id;
8234 id = TREE_VALUE (args);
8235 if (TREE_CODE (id) != STRING_CST)
8237 error ("attribute %qE argument not a string", name);
8238 *no_add_attrs = true;
8239 return NULL_TREE;
8241 id = get_identifier (TREE_STRING_POINTER (id));
8242 /* This counts as a use of the object pointed to. */
8243 TREE_USED (id) = 1;
8245 if (TREE_CODE (decl) == FUNCTION_DECL)
8246 DECL_INITIAL (decl) = error_mark_node;
8247 else
8248 TREE_STATIC (decl) = 1;
8250 if (!is_alias)
8251 /* ifuncs are also aliases, so set that attribute too. */
8252 DECL_ATTRIBUTES (decl)
8253 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8255 else
8257 warning (OPT_Wattributes, "%qE attribute ignored", name);
8258 *no_add_attrs = true;
8261 if (decl_in_symtab_p (*node))
8263 struct symtab_node *n = symtab_node::get (decl);
8264 if (n && n->refuse_visibility_changes)
8266 if (is_alias)
8267 error ("%+D declared alias after being used", decl);
8268 else
8269 error ("%+D declared ifunc after being used", decl);
8274 return NULL_TREE;
8277 /* Handle an "alias" or "ifunc" attribute; arguments as in
8278 struct attribute_spec.handler. */
8280 static tree
8281 handle_ifunc_attribute (tree *node, tree name, tree args,
8282 int ARG_UNUSED (flags), bool *no_add_attrs)
8284 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8287 /* Handle an "alias" or "ifunc" attribute; arguments as in
8288 struct attribute_spec.handler. */
8290 static tree
8291 handle_alias_attribute (tree *node, tree name, tree args,
8292 int ARG_UNUSED (flags), bool *no_add_attrs)
8294 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8297 /* Handle a "weakref" attribute; arguments as in struct
8298 attribute_spec.handler. */
8300 static tree
8301 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8302 int flags, bool *no_add_attrs)
8304 tree attr = NULL_TREE;
8306 /* We must ignore the attribute when it is associated with
8307 local-scoped decls, since attribute alias is ignored and many
8308 such symbols do not even have a DECL_WEAK field. */
8309 if (decl_function_context (*node)
8310 || current_function_decl
8311 || !VAR_OR_FUNCTION_DECL_P (*node))
8313 warning (OPT_Wattributes, "%qE attribute ignored", name);
8314 *no_add_attrs = true;
8315 return NULL_TREE;
8318 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8320 error ("indirect function %q+D cannot be declared weakref", *node);
8321 *no_add_attrs = true;
8322 return NULL_TREE;
8325 /* The idea here is that `weakref("name")' mutates into `weakref,
8326 alias("name")', and weakref without arguments, in turn,
8327 implicitly adds weak. */
8329 if (args)
8331 attr = tree_cons (get_identifier ("alias"), args, attr);
8332 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8334 *no_add_attrs = true;
8336 decl_attributes (node, attr, flags);
8338 else
8340 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8341 error_at (DECL_SOURCE_LOCATION (*node),
8342 "weakref attribute must appear before alias attribute");
8344 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8345 and that isn't supported; and because it wants to add it to
8346 the list of weak decls, which isn't helpful. */
8347 DECL_WEAK (*node) = 1;
8350 if (decl_in_symtab_p (*node))
8352 struct symtab_node *n = symtab_node::get (*node);
8353 if (n && n->refuse_visibility_changes)
8354 error ("%+D declared weakref after being used", *node);
8357 return NULL_TREE;
8360 /* Handle an "visibility" attribute; arguments as in
8361 struct attribute_spec.handler. */
8363 static tree
8364 handle_visibility_attribute (tree *node, tree name, tree args,
8365 int ARG_UNUSED (flags),
8366 bool *ARG_UNUSED (no_add_attrs))
8368 tree decl = *node;
8369 tree id = TREE_VALUE (args);
8370 enum symbol_visibility vis;
8372 if (TYPE_P (*node))
8374 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8375 /* OK */;
8376 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8378 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8379 name);
8380 return NULL_TREE;
8382 else if (TYPE_FIELDS (*node))
8384 error ("%qE attribute ignored because %qT is already defined",
8385 name, *node);
8386 return NULL_TREE;
8389 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8391 warning (OPT_Wattributes, "%qE attribute ignored", name);
8392 return NULL_TREE;
8395 if (TREE_CODE (id) != STRING_CST)
8397 error ("visibility argument not a string");
8398 return NULL_TREE;
8401 /* If this is a type, set the visibility on the type decl. */
8402 if (TYPE_P (decl))
8404 decl = TYPE_NAME (decl);
8405 if (!decl)
8406 return NULL_TREE;
8407 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8409 warning (OPT_Wattributes, "%qE attribute ignored on types",
8410 name);
8411 return NULL_TREE;
8415 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8416 vis = VISIBILITY_DEFAULT;
8417 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8418 vis = VISIBILITY_INTERNAL;
8419 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8420 vis = VISIBILITY_HIDDEN;
8421 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8422 vis = VISIBILITY_PROTECTED;
8423 else
8425 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8426 vis = VISIBILITY_DEFAULT;
8429 if (DECL_VISIBILITY_SPECIFIED (decl)
8430 && vis != DECL_VISIBILITY (decl))
8432 tree attributes = (TYPE_P (*node)
8433 ? TYPE_ATTRIBUTES (*node)
8434 : DECL_ATTRIBUTES (decl));
8435 if (lookup_attribute ("visibility", attributes))
8436 error ("%qD redeclared with different visibility", decl);
8437 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8438 && lookup_attribute ("dllimport", attributes))
8439 error ("%qD was declared %qs which implies default visibility",
8440 decl, "dllimport");
8441 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8442 && lookup_attribute ("dllexport", attributes))
8443 error ("%qD was declared %qs which implies default visibility",
8444 decl, "dllexport");
8447 DECL_VISIBILITY (decl) = vis;
8448 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8450 /* Go ahead and attach the attribute to the node as well. This is needed
8451 so we can determine whether we have VISIBILITY_DEFAULT because the
8452 visibility was not specified, or because it was explicitly overridden
8453 from the containing scope. */
8455 return NULL_TREE;
8458 /* Determine the ELF symbol visibility for DECL, which is either a
8459 variable or a function. It is an error to use this function if a
8460 definition of DECL is not available in this translation unit.
8461 Returns true if the final visibility has been determined by this
8462 function; false if the caller is free to make additional
8463 modifications. */
8465 bool
8466 c_determine_visibility (tree decl)
8468 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8470 /* If the user explicitly specified the visibility with an
8471 attribute, honor that. DECL_VISIBILITY will have been set during
8472 the processing of the attribute. We check for an explicit
8473 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8474 to distinguish the use of an attribute from the use of a "#pragma
8475 GCC visibility push(...)"; in the latter case we still want other
8476 considerations to be able to overrule the #pragma. */
8477 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8478 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8479 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8480 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8481 return true;
8483 /* Set default visibility to whatever the user supplied with
8484 visibility_specified depending on #pragma GCC visibility. */
8485 if (!DECL_VISIBILITY_SPECIFIED (decl))
8487 if (visibility_options.inpragma
8488 || DECL_VISIBILITY (decl) != default_visibility)
8490 DECL_VISIBILITY (decl) = default_visibility;
8491 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8492 /* If visibility changed and DECL already has DECL_RTL, ensure
8493 symbol flags are updated. */
8494 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8495 || TREE_CODE (decl) == FUNCTION_DECL)
8496 && DECL_RTL_SET_P (decl))
8497 make_decl_rtl (decl);
8500 return false;
8503 /* Handle an "tls_model" attribute; arguments as in
8504 struct attribute_spec.handler. */
8506 static tree
8507 handle_tls_model_attribute (tree *node, tree name, tree args,
8508 int ARG_UNUSED (flags), bool *no_add_attrs)
8510 tree id;
8511 tree decl = *node;
8512 enum tls_model kind;
8514 *no_add_attrs = true;
8516 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
8518 warning (OPT_Wattributes, "%qE attribute ignored", name);
8519 return NULL_TREE;
8522 kind = DECL_TLS_MODEL (decl);
8523 id = TREE_VALUE (args);
8524 if (TREE_CODE (id) != STRING_CST)
8526 error ("tls_model argument not a string");
8527 return NULL_TREE;
8530 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8531 kind = TLS_MODEL_LOCAL_EXEC;
8532 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8533 kind = TLS_MODEL_INITIAL_EXEC;
8534 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8535 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8536 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8537 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8538 else
8539 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8541 set_decl_tls_model (decl, kind);
8542 return NULL_TREE;
8545 /* Handle a "no_instrument_function" attribute; arguments as in
8546 struct attribute_spec.handler. */
8548 static tree
8549 handle_no_instrument_function_attribute (tree *node, tree name,
8550 tree ARG_UNUSED (args),
8551 int ARG_UNUSED (flags),
8552 bool *no_add_attrs)
8554 tree decl = *node;
8556 if (TREE_CODE (decl) != FUNCTION_DECL)
8558 error_at (DECL_SOURCE_LOCATION (decl),
8559 "%qE attribute applies only to functions", name);
8560 *no_add_attrs = true;
8562 else
8563 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8565 return NULL_TREE;
8568 /* Handle a "malloc" attribute; arguments as in
8569 struct attribute_spec.handler. */
8571 static tree
8572 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8573 int ARG_UNUSED (flags), bool *no_add_attrs)
8575 if (TREE_CODE (*node) == FUNCTION_DECL
8576 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8577 DECL_IS_MALLOC (*node) = 1;
8578 else
8580 warning (OPT_Wattributes, "%qE attribute ignored", name);
8581 *no_add_attrs = true;
8584 return NULL_TREE;
8587 /* Handle a "alloc_size" attribute; arguments as in
8588 struct attribute_spec.handler. */
8590 static tree
8591 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8592 int ARG_UNUSED (flags), bool *no_add_attrs)
8594 unsigned arg_count = type_num_arguments (*node);
8595 for (; args; args = TREE_CHAIN (args))
8597 tree position = TREE_VALUE (args);
8598 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8599 && TREE_CODE (position) != FUNCTION_DECL)
8600 position = default_conversion (position);
8602 if (!tree_fits_uhwi_p (position)
8603 || !arg_count
8604 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8606 warning (OPT_Wattributes,
8607 "alloc_size parameter outside range");
8608 *no_add_attrs = true;
8609 return NULL_TREE;
8612 return NULL_TREE;
8615 /* Handle a "alloc_align" attribute; arguments as in
8616 struct attribute_spec.handler. */
8618 static tree
8619 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8620 bool *no_add_attrs)
8622 unsigned arg_count = type_num_arguments (*node);
8623 tree position = TREE_VALUE (args);
8624 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8625 position = default_conversion (position);
8627 if (!tree_fits_uhwi_p (position)
8628 || !arg_count
8629 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8631 warning (OPT_Wattributes,
8632 "alloc_align parameter outside range");
8633 *no_add_attrs = true;
8634 return NULL_TREE;
8636 return NULL_TREE;
8639 /* Handle a "assume_aligned" attribute; arguments as in
8640 struct attribute_spec.handler. */
8642 static tree
8643 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8644 bool *no_add_attrs)
8646 for (; args; args = TREE_CHAIN (args))
8648 tree position = TREE_VALUE (args);
8649 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8650 && TREE_CODE (position) != FUNCTION_DECL)
8651 position = default_conversion (position);
8653 if (TREE_CODE (position) != INTEGER_CST)
8655 warning (OPT_Wattributes,
8656 "assume_aligned parameter not integer constant");
8657 *no_add_attrs = true;
8658 return NULL_TREE;
8661 return NULL_TREE;
8664 /* Handle a "fn spec" attribute; arguments as in
8665 struct attribute_spec.handler. */
8667 static tree
8668 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8669 tree args, int ARG_UNUSED (flags),
8670 bool *no_add_attrs ATTRIBUTE_UNUSED)
8672 gcc_assert (args
8673 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8674 && !TREE_CHAIN (args));
8675 return NULL_TREE;
8678 /* Handle a "bnd_variable_size" attribute; arguments as in
8679 struct attribute_spec.handler. */
8681 static tree
8682 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8683 int ARG_UNUSED (flags), bool *no_add_attrs)
8685 if (TREE_CODE (*node) != FIELD_DECL)
8687 warning (OPT_Wattributes, "%qE attribute ignored", name);
8688 *no_add_attrs = true;
8691 return NULL_TREE;
8694 /* Handle a "bnd_legacy" attribute; arguments as in
8695 struct attribute_spec.handler. */
8697 static tree
8698 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8699 int ARG_UNUSED (flags), bool *no_add_attrs)
8701 if (TREE_CODE (*node) != FUNCTION_DECL)
8703 warning (OPT_Wattributes, "%qE attribute ignored", name);
8704 *no_add_attrs = true;
8707 return NULL_TREE;
8710 /* Handle a "bnd_instrument" attribute; arguments as in
8711 struct attribute_spec.handler. */
8713 static tree
8714 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8715 int ARG_UNUSED (flags), bool *no_add_attrs)
8717 if (TREE_CODE (*node) != FUNCTION_DECL)
8719 warning (OPT_Wattributes, "%qE attribute ignored", name);
8720 *no_add_attrs = true;
8723 return NULL_TREE;
8726 /* Handle a "warn_unused" attribute; arguments as in
8727 struct attribute_spec.handler. */
8729 static tree
8730 handle_warn_unused_attribute (tree *node, tree name,
8731 tree args ATTRIBUTE_UNUSED,
8732 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8734 if (TYPE_P (*node))
8735 /* Do nothing else, just set the attribute. We'll get at
8736 it later with lookup_attribute. */
8738 else
8740 warning (OPT_Wattributes, "%qE attribute ignored", name);
8741 *no_add_attrs = true;
8744 return NULL_TREE;
8747 /* Handle an "omp declare simd" attribute; arguments as in
8748 struct attribute_spec.handler. */
8750 static tree
8751 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8753 return NULL_TREE;
8756 /* Handle an "omp declare target" attribute; arguments as in
8757 struct attribute_spec.handler. */
8759 static tree
8760 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8762 return NULL_TREE;
8765 /* Handle a "returns_twice" attribute; arguments as in
8766 struct attribute_spec.handler. */
8768 static tree
8769 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8770 int ARG_UNUSED (flags), bool *no_add_attrs)
8772 if (TREE_CODE (*node) == FUNCTION_DECL)
8773 DECL_IS_RETURNS_TWICE (*node) = 1;
8774 else
8776 warning (OPT_Wattributes, "%qE attribute ignored", name);
8777 *no_add_attrs = true;
8780 return NULL_TREE;
8783 /* Handle a "no_limit_stack" attribute; arguments as in
8784 struct attribute_spec.handler. */
8786 static tree
8787 handle_no_limit_stack_attribute (tree *node, tree name,
8788 tree ARG_UNUSED (args),
8789 int ARG_UNUSED (flags),
8790 bool *no_add_attrs)
8792 tree decl = *node;
8794 if (TREE_CODE (decl) != FUNCTION_DECL)
8796 error_at (DECL_SOURCE_LOCATION (decl),
8797 "%qE attribute applies only to functions", name);
8798 *no_add_attrs = true;
8800 else if (DECL_INITIAL (decl))
8802 error_at (DECL_SOURCE_LOCATION (decl),
8803 "can%'t set %qE attribute after definition", name);
8804 *no_add_attrs = true;
8806 else
8807 DECL_NO_LIMIT_STACK (decl) = 1;
8809 return NULL_TREE;
8812 /* Handle a "pure" attribute; arguments as in
8813 struct attribute_spec.handler. */
8815 static tree
8816 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8817 int ARG_UNUSED (flags), bool *no_add_attrs)
8819 if (TREE_CODE (*node) == FUNCTION_DECL)
8820 DECL_PURE_P (*node) = 1;
8821 /* ??? TODO: Support types. */
8822 else
8824 warning (OPT_Wattributes, "%qE attribute ignored", name);
8825 *no_add_attrs = true;
8828 return NULL_TREE;
8831 /* Digest an attribute list destined for a transactional memory statement.
8832 ALLOWED is the set of attributes that are allowed for this statement;
8833 return the attribute we parsed. Multiple attributes are never allowed. */
8836 parse_tm_stmt_attr (tree attrs, int allowed)
8838 tree a_seen = NULL;
8839 int m_seen = 0;
8841 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8843 tree a = TREE_PURPOSE (attrs);
8844 int m = 0;
8846 if (is_attribute_p ("outer", a))
8847 m = TM_STMT_ATTR_OUTER;
8849 if ((m & allowed) == 0)
8851 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8852 continue;
8855 if (m_seen == 0)
8857 a_seen = a;
8858 m_seen = m;
8860 else if (m_seen == m)
8861 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8862 else
8863 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8866 return m_seen;
8869 /* Transform a TM attribute name into a maskable integer and back.
8870 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8871 to how the lack of an attribute is treated. */
8874 tm_attr_to_mask (tree attr)
8876 if (attr == NULL)
8877 return 0;
8878 if (is_attribute_p ("transaction_safe", attr))
8879 return TM_ATTR_SAFE;
8880 if (is_attribute_p ("transaction_callable", attr))
8881 return TM_ATTR_CALLABLE;
8882 if (is_attribute_p ("transaction_pure", attr))
8883 return TM_ATTR_PURE;
8884 if (is_attribute_p ("transaction_unsafe", attr))
8885 return TM_ATTR_IRREVOCABLE;
8886 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8887 return TM_ATTR_MAY_CANCEL_OUTER;
8888 return 0;
8891 tree
8892 tm_mask_to_attr (int mask)
8894 const char *str;
8895 switch (mask)
8897 case TM_ATTR_SAFE:
8898 str = "transaction_safe";
8899 break;
8900 case TM_ATTR_CALLABLE:
8901 str = "transaction_callable";
8902 break;
8903 case TM_ATTR_PURE:
8904 str = "transaction_pure";
8905 break;
8906 case TM_ATTR_IRREVOCABLE:
8907 str = "transaction_unsafe";
8908 break;
8909 case TM_ATTR_MAY_CANCEL_OUTER:
8910 str = "transaction_may_cancel_outer";
8911 break;
8912 default:
8913 gcc_unreachable ();
8915 return get_identifier (str);
8918 /* Return the first TM attribute seen in LIST. */
8920 tree
8921 find_tm_attribute (tree list)
8923 for (; list ; list = TREE_CHAIN (list))
8925 tree name = TREE_PURPOSE (list);
8926 if (tm_attr_to_mask (name) != 0)
8927 return name;
8929 return NULL_TREE;
8932 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8933 Here we accept only function types, and verify that none of the other
8934 function TM attributes are also applied. */
8935 /* ??? We need to accept class types for C++, but not C. This greatly
8936 complicates this function, since we can no longer rely on the extra
8937 processing given by function_type_required. */
8939 static tree
8940 handle_tm_attribute (tree *node, tree name, tree args,
8941 int flags, bool *no_add_attrs)
8943 /* Only one path adds the attribute; others don't. */
8944 *no_add_attrs = true;
8946 switch (TREE_CODE (*node))
8948 case RECORD_TYPE:
8949 case UNION_TYPE:
8950 /* Only tm_callable and tm_safe apply to classes. */
8951 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8952 goto ignored;
8953 /* FALLTHRU */
8955 case FUNCTION_TYPE:
8956 case METHOD_TYPE:
8958 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8959 if (old_name == name)
8961 else if (old_name != NULL_TREE)
8962 error ("type was previously declared %qE", old_name);
8963 else
8964 *no_add_attrs = false;
8966 break;
8968 case POINTER_TYPE:
8970 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8971 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8973 tree fn_tmp = TREE_TYPE (*node);
8974 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8975 *node = build_pointer_type (fn_tmp);
8976 break;
8979 /* FALLTHRU */
8981 default:
8982 /* If a function is next, pass it on to be tried next. */
8983 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8984 return tree_cons (name, args, NULL);
8986 ignored:
8987 warning (OPT_Wattributes, "%qE attribute ignored", name);
8988 break;
8991 return NULL_TREE;
8994 /* Handle the TM_WRAP attribute; arguments as in
8995 struct attribute_spec.handler. */
8997 static tree
8998 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8999 int ARG_UNUSED (flags), bool *no_add_attrs)
9001 tree decl = *node;
9003 /* We don't need the attribute even on success, since we
9004 record the entry in an external table. */
9005 *no_add_attrs = true;
9007 if (TREE_CODE (decl) != FUNCTION_DECL)
9008 warning (OPT_Wattributes, "%qE attribute ignored", name);
9009 else
9011 tree wrap_decl = TREE_VALUE (args);
9012 if (error_operand_p (wrap_decl))
9014 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
9015 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
9016 error ("%qE argument not an identifier", name);
9017 else
9019 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9020 wrap_decl = lookup_name (wrap_decl);
9021 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9023 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9024 TREE_TYPE (wrap_decl)))
9025 record_tm_replacement (wrap_decl, decl);
9026 else
9027 error ("%qD is not compatible with %qD", wrap_decl, decl);
9029 else
9030 error ("%qE argument is not a function", name);
9034 return NULL_TREE;
9037 /* Ignore the given attribute. Used when this attribute may be usefully
9038 overridden by the target, but is not used generically. */
9040 static tree
9041 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9042 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9043 bool *no_add_attrs)
9045 *no_add_attrs = true;
9046 return NULL_TREE;
9049 /* Handle a "no vops" attribute; arguments as in
9050 struct attribute_spec.handler. */
9052 static tree
9053 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9054 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9055 bool *ARG_UNUSED (no_add_attrs))
9057 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9058 DECL_IS_NOVOPS (*node) = 1;
9059 return NULL_TREE;
9062 /* Handle a "deprecated" attribute; arguments as in
9063 struct attribute_spec.handler. */
9065 static tree
9066 handle_deprecated_attribute (tree *node, tree name,
9067 tree args, int flags,
9068 bool *no_add_attrs)
9070 tree type = NULL_TREE;
9071 int warn = 0;
9072 tree what = NULL_TREE;
9074 if (!args)
9075 *no_add_attrs = true;
9076 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9078 error ("deprecated message is not a string");
9079 *no_add_attrs = true;
9082 if (DECL_P (*node))
9084 tree decl = *node;
9085 type = TREE_TYPE (decl);
9087 if (TREE_CODE (decl) == TYPE_DECL
9088 || TREE_CODE (decl) == PARM_DECL
9089 || VAR_OR_FUNCTION_DECL_P (decl)
9090 || TREE_CODE (decl) == FIELD_DECL
9091 || objc_method_decl (TREE_CODE (decl)))
9092 TREE_DEPRECATED (decl) = 1;
9093 else
9094 warn = 1;
9096 else if (TYPE_P (*node))
9098 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
9099 *node = build_variant_type_copy (*node);
9100 TREE_DEPRECATED (*node) = 1;
9101 type = *node;
9103 else
9104 warn = 1;
9106 if (warn)
9108 *no_add_attrs = true;
9109 if (type && TYPE_NAME (type))
9111 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
9112 what = TYPE_NAME (*node);
9113 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9114 && DECL_NAME (TYPE_NAME (type)))
9115 what = DECL_NAME (TYPE_NAME (type));
9117 if (what)
9118 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
9119 else
9120 warning (OPT_Wattributes, "%qE attribute ignored", name);
9123 return NULL_TREE;
9126 /* Handle a "vector_size" attribute; arguments as in
9127 struct attribute_spec.handler. */
9129 static tree
9130 handle_vector_size_attribute (tree *node, tree name, tree args,
9131 int ARG_UNUSED (flags),
9132 bool *no_add_attrs)
9134 unsigned HOST_WIDE_INT vecsize, nunits;
9135 machine_mode orig_mode;
9136 tree type = *node, new_type, size;
9138 *no_add_attrs = true;
9140 size = TREE_VALUE (args);
9141 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9142 && TREE_CODE (size) != FUNCTION_DECL)
9143 size = default_conversion (size);
9145 if (!tree_fits_uhwi_p (size))
9147 warning (OPT_Wattributes, "%qE attribute ignored", name);
9148 return NULL_TREE;
9151 /* Get the vector size (in bytes). */
9152 vecsize = tree_to_uhwi (size);
9154 /* We need to provide for vector pointers, vector arrays, and
9155 functions returning vectors. For example:
9157 __attribute__((vector_size(16))) short *foo;
9159 In this case, the mode is SI, but the type being modified is
9160 HI, so we need to look further. */
9162 while (POINTER_TYPE_P (type)
9163 || TREE_CODE (type) == FUNCTION_TYPE
9164 || TREE_CODE (type) == METHOD_TYPE
9165 || TREE_CODE (type) == ARRAY_TYPE
9166 || TREE_CODE (type) == OFFSET_TYPE)
9167 type = TREE_TYPE (type);
9169 /* Get the mode of the type being modified. */
9170 orig_mode = TYPE_MODE (type);
9172 if ((!INTEGRAL_TYPE_P (type)
9173 && !SCALAR_FLOAT_TYPE_P (type)
9174 && !FIXED_POINT_TYPE_P (type))
9175 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9176 && GET_MODE_CLASS (orig_mode) != MODE_INT
9177 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
9178 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9179 || TREE_CODE (type) == BOOLEAN_TYPE)
9181 error ("invalid vector type for attribute %qE", name);
9182 return NULL_TREE;
9185 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
9187 error ("vector size not an integral multiple of component size");
9188 return NULL;
9191 if (vecsize == 0)
9193 error ("zero vector size");
9194 return NULL;
9197 /* Calculate how many units fit in the vector. */
9198 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
9199 if (nunits & (nunits - 1))
9201 error ("number of components of the vector not a power of two");
9202 return NULL_TREE;
9205 new_type = build_vector_type (type, nunits);
9207 /* Build back pointers if needed. */
9208 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9210 return NULL_TREE;
9213 /* Handle the "nonnull" attribute. */
9214 static tree
9215 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9216 tree args, int ARG_UNUSED (flags),
9217 bool *no_add_attrs)
9219 tree type = *node;
9220 unsigned HOST_WIDE_INT attr_arg_num;
9222 /* If no arguments are specified, all pointer arguments should be
9223 non-null. Verify a full prototype is given so that the arguments
9224 will have the correct types when we actually check them later. */
9225 if (!args)
9227 if (!prototype_p (type))
9229 error ("nonnull attribute without arguments on a non-prototype");
9230 *no_add_attrs = true;
9232 return NULL_TREE;
9235 /* Argument list specified. Verify that each argument number references
9236 a pointer argument. */
9237 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9239 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9241 tree arg = TREE_VALUE (args);
9242 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9243 && TREE_CODE (arg) != FUNCTION_DECL)
9244 arg = default_conversion (arg);
9246 if (!get_nonnull_operand (arg, &arg_num))
9248 error ("nonnull argument has invalid operand number (argument %lu)",
9249 (unsigned long) attr_arg_num);
9250 *no_add_attrs = true;
9251 return NULL_TREE;
9254 if (prototype_p (type))
9256 function_args_iterator iter;
9257 tree argument;
9259 function_args_iter_init (&iter, type);
9260 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9262 argument = function_args_iter_cond (&iter);
9263 if (argument == NULL_TREE || ck_num == arg_num)
9264 break;
9267 if (!argument
9268 || TREE_CODE (argument) == VOID_TYPE)
9270 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9271 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9272 *no_add_attrs = true;
9273 return NULL_TREE;
9276 if (TREE_CODE (argument) != POINTER_TYPE)
9278 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9279 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9280 *no_add_attrs = true;
9281 return NULL_TREE;
9286 return NULL_TREE;
9289 /* Check the argument list of a function call for null in argument slots
9290 that are marked as requiring a non-null pointer argument. The NARGS
9291 arguments are passed in the array ARGARRAY.
9294 static void
9295 check_function_nonnull (tree attrs, int nargs, tree *argarray)
9297 tree a;
9298 int i;
9300 attrs = lookup_attribute ("nonnull", attrs);
9301 if (attrs == NULL_TREE)
9302 return;
9304 a = attrs;
9305 /* See if any of the nonnull attributes has no arguments. If so,
9306 then every pointer argument is checked (in which case the check
9307 for pointer type is done in check_nonnull_arg). */
9308 if (TREE_VALUE (a) != NULL_TREE)
9310 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9311 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9313 if (a != NULL_TREE)
9314 for (i = 0; i < nargs; i++)
9315 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9316 i + 1);
9317 else
9319 /* Walk the argument list. If we encounter an argument number we
9320 should check for non-null, do it. */
9321 for (i = 0; i < nargs; i++)
9323 for (a = attrs; ; a = TREE_CHAIN (a))
9325 a = lookup_attribute ("nonnull", a);
9326 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9327 break;
9330 if (a != NULL_TREE)
9331 check_function_arguments_recurse (check_nonnull_arg, NULL,
9332 argarray[i], i + 1);
9337 /* Check that the Nth argument of a function call (counting backwards
9338 from the end) is a (pointer)0. The NARGS arguments are passed in the
9339 array ARGARRAY. */
9341 static void
9342 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9344 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9346 if (attr)
9348 int len = 0;
9349 int pos = 0;
9350 tree sentinel;
9351 function_args_iterator iter;
9352 tree t;
9354 /* Skip over the named arguments. */
9355 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9357 if (len == nargs)
9358 break;
9359 len++;
9362 if (TREE_VALUE (attr))
9364 tree p = TREE_VALUE (TREE_VALUE (attr));
9365 pos = TREE_INT_CST_LOW (p);
9368 /* The sentinel must be one of the varargs, i.e.
9369 in position >= the number of fixed arguments. */
9370 if ((nargs - 1 - pos) < len)
9372 warning (OPT_Wformat_,
9373 "not enough variable arguments to fit a sentinel");
9374 return;
9377 /* Validate the sentinel. */
9378 sentinel = argarray[nargs - 1 - pos];
9379 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9380 || !integer_zerop (sentinel))
9381 /* Although __null (in C++) is only an integer we allow it
9382 nevertheless, as we are guaranteed that it's exactly
9383 as wide as a pointer, and we don't want to force
9384 users to cast the NULL they have written there.
9385 We warn with -Wstrict-null-sentinel, though. */
9386 && (warn_strict_null_sentinel || null_node != sentinel))
9387 warning (OPT_Wformat_, "missing sentinel in function call");
9391 /* Helper for check_function_nonnull; given a list of operands which
9392 must be non-null in ARGS, determine if operand PARAM_NUM should be
9393 checked. */
9395 static bool
9396 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9398 unsigned HOST_WIDE_INT arg_num = 0;
9400 for (; args; args = TREE_CHAIN (args))
9402 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9404 gcc_assert (found);
9406 if (arg_num == param_num)
9407 return true;
9409 return false;
9412 /* Check that the function argument PARAM (which is operand number
9413 PARAM_NUM) is non-null. This is called by check_function_nonnull
9414 via check_function_arguments_recurse. */
9416 static void
9417 check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
9418 unsigned HOST_WIDE_INT param_num)
9420 /* Just skip checking the argument if it's not a pointer. This can
9421 happen if the "nonnull" attribute was given without an operand
9422 list (which means to check every pointer argument). */
9424 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9425 return;
9427 if (integer_zerop (param))
9428 warning (OPT_Wnonnull, "null argument where non-null required "
9429 "(argument %lu)", (unsigned long) param_num);
9432 /* Helper for nonnull attribute handling; fetch the operand number
9433 from the attribute argument list. */
9435 static bool
9436 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9438 /* Verify the arg number is a small constant. */
9439 if (tree_fits_uhwi_p (arg_num_expr))
9441 *valp = TREE_INT_CST_LOW (arg_num_expr);
9442 return true;
9444 else
9445 return false;
9448 /* Handle a "nothrow" attribute; arguments as in
9449 struct attribute_spec.handler. */
9451 static tree
9452 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9453 int ARG_UNUSED (flags), bool *no_add_attrs)
9455 if (TREE_CODE (*node) == FUNCTION_DECL)
9456 TREE_NOTHROW (*node) = 1;
9457 /* ??? TODO: Support types. */
9458 else
9460 warning (OPT_Wattributes, "%qE attribute ignored", name);
9461 *no_add_attrs = true;
9464 return NULL_TREE;
9467 /* Handle a "cleanup" attribute; arguments as in
9468 struct attribute_spec.handler. */
9470 static tree
9471 handle_cleanup_attribute (tree *node, tree name, tree args,
9472 int ARG_UNUSED (flags), bool *no_add_attrs)
9474 tree decl = *node;
9475 tree cleanup_id, cleanup_decl;
9477 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9478 for global destructors in C++. This requires infrastructure that
9479 we don't have generically at the moment. It's also not a feature
9480 we'd be missing too much, since we do have attribute constructor. */
9481 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9483 warning (OPT_Wattributes, "%qE attribute ignored", name);
9484 *no_add_attrs = true;
9485 return NULL_TREE;
9488 /* Verify that the argument is a function in scope. */
9489 /* ??? We could support pointers to functions here as well, if
9490 that was considered desirable. */
9491 cleanup_id = TREE_VALUE (args);
9492 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9494 error ("cleanup argument not an identifier");
9495 *no_add_attrs = true;
9496 return NULL_TREE;
9498 cleanup_decl = lookup_name (cleanup_id);
9499 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9501 error ("cleanup argument not a function");
9502 *no_add_attrs = true;
9503 return NULL_TREE;
9506 /* That the function has proper type is checked with the
9507 eventual call to build_function_call. */
9509 return NULL_TREE;
9512 /* Handle a "warn_unused_result" attribute. No special handling. */
9514 static tree
9515 handle_warn_unused_result_attribute (tree *node, tree name,
9516 tree ARG_UNUSED (args),
9517 int ARG_UNUSED (flags), bool *no_add_attrs)
9519 /* Ignore the attribute for functions not returning any value. */
9520 if (VOID_TYPE_P (TREE_TYPE (*node)))
9522 warning (OPT_Wattributes, "%qE attribute ignored", name);
9523 *no_add_attrs = true;
9526 return NULL_TREE;
9529 /* Handle a "sentinel" attribute. */
9531 static tree
9532 handle_sentinel_attribute (tree *node, tree name, tree args,
9533 int ARG_UNUSED (flags), bool *no_add_attrs)
9535 if (!prototype_p (*node))
9537 warning (OPT_Wattributes,
9538 "%qE attribute requires prototypes with named arguments", name);
9539 *no_add_attrs = true;
9541 else
9543 if (!stdarg_p (*node))
9545 warning (OPT_Wattributes,
9546 "%qE attribute only applies to variadic functions", name);
9547 *no_add_attrs = true;
9551 if (args)
9553 tree position = TREE_VALUE (args);
9554 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9555 && TREE_CODE (position) != FUNCTION_DECL)
9556 position = default_conversion (position);
9558 if (TREE_CODE (position) != INTEGER_CST
9559 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9561 warning (OPT_Wattributes,
9562 "requested position is not an integer constant");
9563 *no_add_attrs = true;
9565 else
9567 if (tree_int_cst_lt (position, integer_zero_node))
9569 warning (OPT_Wattributes,
9570 "requested position is less than zero");
9571 *no_add_attrs = true;
9576 return NULL_TREE;
9579 /* Handle a "type_generic" attribute. */
9581 static tree
9582 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9583 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9584 bool * ARG_UNUSED (no_add_attrs))
9586 /* Ensure we have a function type. */
9587 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9589 /* Ensure we have a variadic function. */
9590 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9592 return NULL_TREE;
9595 /* Handle a "target" attribute. */
9597 static tree
9598 handle_target_attribute (tree *node, tree name, tree args, int flags,
9599 bool *no_add_attrs)
9601 /* Ensure we have a function type. */
9602 if (TREE_CODE (*node) != FUNCTION_DECL)
9604 warning (OPT_Wattributes, "%qE attribute ignored", name);
9605 *no_add_attrs = true;
9607 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9608 flags))
9609 *no_add_attrs = true;
9611 return NULL_TREE;
9614 /* Arguments being collected for optimization. */
9615 typedef const char *const_char_p; /* For DEF_VEC_P. */
9616 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9619 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9620 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9621 false for #pragma GCC optimize. */
9623 bool
9624 parse_optimize_options (tree args, bool attr_p)
9626 bool ret = true;
9627 unsigned opt_argc;
9628 unsigned i;
9629 int saved_flag_strict_aliasing;
9630 const char **opt_argv;
9631 struct cl_decoded_option *decoded_options;
9632 unsigned int decoded_options_count;
9633 tree ap;
9635 /* Build up argv vector. Just in case the string is stored away, use garbage
9636 collected strings. */
9637 vec_safe_truncate (optimize_args, 0);
9638 vec_safe_push (optimize_args, (const char *) NULL);
9640 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9642 tree value = TREE_VALUE (ap);
9644 if (TREE_CODE (value) == INTEGER_CST)
9646 char buffer[20];
9647 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9648 vec_safe_push (optimize_args, ggc_strdup (buffer));
9651 else if (TREE_CODE (value) == STRING_CST)
9653 /* Split string into multiple substrings. */
9654 size_t len = TREE_STRING_LENGTH (value);
9655 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9656 char *end = p + len;
9657 char *comma;
9658 char *next_p = p;
9660 while (next_p != NULL)
9662 size_t len2;
9663 char *q, *r;
9665 p = next_p;
9666 comma = strchr (p, ',');
9667 if (comma)
9669 len2 = comma - p;
9670 *comma = '\0';
9671 next_p = comma+1;
9673 else
9675 len2 = end - p;
9676 next_p = NULL;
9679 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9681 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9682 options. */
9683 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9685 ret = false;
9686 if (attr_p)
9687 warning (OPT_Wattributes,
9688 "bad option %s to optimize attribute", p);
9689 else
9690 warning (OPT_Wpragmas,
9691 "bad option %s to pragma attribute", p);
9692 continue;
9695 if (*p != '-')
9697 *r++ = '-';
9699 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9700 itself is -Os, and any other switch begins with a -f. */
9701 if ((*p >= '0' && *p <= '9')
9702 || (p[0] == 's' && p[1] == '\0'))
9703 *r++ = 'O';
9704 else if (*p != 'O')
9705 *r++ = 'f';
9708 memcpy (r, p, len2);
9709 r[len2] = '\0';
9710 vec_safe_push (optimize_args, (const char *) q);
9716 opt_argc = optimize_args->length ();
9717 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9719 for (i = 1; i < opt_argc; i++)
9720 opt_argv[i] = (*optimize_args)[i];
9722 saved_flag_strict_aliasing = flag_strict_aliasing;
9724 /* Now parse the options. */
9725 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9726 &decoded_options,
9727 &decoded_options_count);
9728 decode_options (&global_options, &global_options_set,
9729 decoded_options, decoded_options_count,
9730 input_location, global_dc);
9732 targetm.override_options_after_change();
9734 /* Don't allow changing -fstrict-aliasing. */
9735 flag_strict_aliasing = saved_flag_strict_aliasing;
9737 optimize_args->truncate (0);
9738 return ret;
9741 /* For handling "optimize" attribute. arguments as in
9742 struct attribute_spec.handler. */
9744 static tree
9745 handle_optimize_attribute (tree *node, tree name, tree args,
9746 int ARG_UNUSED (flags), bool *no_add_attrs)
9748 /* Ensure we have a function type. */
9749 if (TREE_CODE (*node) != FUNCTION_DECL)
9751 warning (OPT_Wattributes, "%qE attribute ignored", name);
9752 *no_add_attrs = true;
9754 else
9756 struct cl_optimization cur_opts;
9757 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9759 /* Save current options. */
9760 cl_optimization_save (&cur_opts, &global_options);
9762 /* If we previously had some optimization options, use them as the
9763 default. */
9764 if (old_opts)
9765 cl_optimization_restore (&global_options,
9766 TREE_OPTIMIZATION (old_opts));
9768 /* Parse options, and update the vector. */
9769 parse_optimize_options (args, true);
9770 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9771 = build_optimization_node (&global_options);
9773 /* Restore current options. */
9774 cl_optimization_restore (&global_options, &cur_opts);
9777 return NULL_TREE;
9780 /* Handle a "no_split_stack" attribute. */
9782 static tree
9783 handle_no_split_stack_attribute (tree *node, tree name,
9784 tree ARG_UNUSED (args),
9785 int ARG_UNUSED (flags),
9786 bool *no_add_attrs)
9788 tree decl = *node;
9790 if (TREE_CODE (decl) != FUNCTION_DECL)
9792 error_at (DECL_SOURCE_LOCATION (decl),
9793 "%qE attribute applies only to functions", name);
9794 *no_add_attrs = true;
9796 else if (DECL_INITIAL (decl))
9798 error_at (DECL_SOURCE_LOCATION (decl),
9799 "can%'t set %qE attribute after definition", name);
9800 *no_add_attrs = true;
9803 return NULL_TREE;
9806 /* Handle a "returns_nonnull" attribute; arguments as in
9807 struct attribute_spec.handler. */
9809 static tree
9810 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9811 bool *no_add_attrs)
9813 // Even without a prototype we still have a return type we can check.
9814 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9816 error ("returns_nonnull attribute on a function not returning a pointer");
9817 *no_add_attrs = true;
9819 return NULL_TREE;
9822 /* Handle a "designated_init" attribute; arguments as in
9823 struct attribute_spec.handler. */
9825 static tree
9826 handle_designated_init_attribute (tree *node, tree name, tree, int,
9827 bool *no_add_attrs)
9829 if (TREE_CODE (*node) != RECORD_TYPE)
9831 error ("%qE attribute is only valid on %<struct%> type", name);
9832 *no_add_attrs = true;
9834 return NULL_TREE;
9838 /* Check for valid arguments being passed to a function with FNTYPE.
9839 There are NARGS arguments in the array ARGARRAY. */
9840 void
9841 check_function_arguments (const_tree fntype, int nargs, tree *argarray)
9843 /* Check for null being passed in a pointer argument that must be
9844 non-null. We also need to do this if format checking is enabled. */
9846 if (warn_nonnull)
9847 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9849 /* Check for errors in format strings. */
9851 if (warn_format || warn_suggest_attribute_format)
9852 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9854 if (warn_format)
9855 check_function_sentinel (fntype, nargs, argarray);
9858 /* Generic argument checking recursion routine. PARAM is the argument to
9859 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9860 once the argument is resolved. CTX is context for the callback. */
9861 void
9862 check_function_arguments_recurse (void (*callback)
9863 (void *, tree, unsigned HOST_WIDE_INT),
9864 void *ctx, tree param,
9865 unsigned HOST_WIDE_INT param_num)
9867 if (CONVERT_EXPR_P (param)
9868 && (TYPE_PRECISION (TREE_TYPE (param))
9869 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9871 /* Strip coercion. */
9872 check_function_arguments_recurse (callback, ctx,
9873 TREE_OPERAND (param, 0), param_num);
9874 return;
9877 if (TREE_CODE (param) == CALL_EXPR)
9879 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9880 tree attrs;
9881 bool found_format_arg = false;
9883 /* See if this is a call to a known internationalization function
9884 that modifies a format arg. Such a function may have multiple
9885 format_arg attributes (for example, ngettext). */
9887 for (attrs = TYPE_ATTRIBUTES (type);
9888 attrs;
9889 attrs = TREE_CHAIN (attrs))
9890 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9892 tree inner_arg;
9893 tree format_num_expr;
9894 int format_num;
9895 int i;
9896 call_expr_arg_iterator iter;
9898 /* Extract the argument number, which was previously checked
9899 to be valid. */
9900 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9902 format_num = tree_to_uhwi (format_num_expr);
9904 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9905 inner_arg != 0;
9906 inner_arg = next_call_expr_arg (&iter), i++)
9907 if (i == format_num)
9909 check_function_arguments_recurse (callback, ctx,
9910 inner_arg, param_num);
9911 found_format_arg = true;
9912 break;
9916 /* If we found a format_arg attribute and did a recursive check,
9917 we are done with checking this argument. Otherwise, we continue
9918 and this will be considered a non-literal. */
9919 if (found_format_arg)
9920 return;
9923 if (TREE_CODE (param) == COND_EXPR)
9925 /* Check both halves of the conditional expression. */
9926 check_function_arguments_recurse (callback, ctx,
9927 TREE_OPERAND (param, 1), param_num);
9928 check_function_arguments_recurse (callback, ctx,
9929 TREE_OPERAND (param, 2), param_num);
9930 return;
9933 (*callback) (ctx, param, param_num);
9936 /* Checks for a builtin function FNDECL that the number of arguments
9937 NARGS against the required number REQUIRED and issues an error if
9938 there is a mismatch. Returns true if the number of arguments is
9939 correct, otherwise false. */
9941 static bool
9942 builtin_function_validate_nargs (tree fndecl, int nargs, int required)
9944 if (nargs < required)
9946 error_at (input_location,
9947 "not enough arguments to function %qE", fndecl);
9948 return false;
9950 else if (nargs > required)
9952 error_at (input_location,
9953 "too many arguments to function %qE", fndecl);
9954 return false;
9956 return true;
9959 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9960 Returns false if there was an error, otherwise true. */
9962 bool
9963 check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9965 if (!DECL_BUILT_IN (fndecl)
9966 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9967 return true;
9969 switch (DECL_FUNCTION_CODE (fndecl))
9971 case BUILT_IN_CONSTANT_P:
9972 return builtin_function_validate_nargs (fndecl, nargs, 1);
9974 case BUILT_IN_ISFINITE:
9975 case BUILT_IN_ISINF:
9976 case BUILT_IN_ISINF_SIGN:
9977 case BUILT_IN_ISNAN:
9978 case BUILT_IN_ISNORMAL:
9979 if (builtin_function_validate_nargs (fndecl, nargs, 1))
9981 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9983 error ("non-floating-point argument in call to "
9984 "function %qE", fndecl);
9985 return false;
9987 return true;
9989 return false;
9991 case BUILT_IN_ISGREATER:
9992 case BUILT_IN_ISGREATEREQUAL:
9993 case BUILT_IN_ISLESS:
9994 case BUILT_IN_ISLESSEQUAL:
9995 case BUILT_IN_ISLESSGREATER:
9996 case BUILT_IN_ISUNORDERED:
9997 if (builtin_function_validate_nargs (fndecl, nargs, 2))
9999 enum tree_code code0, code1;
10000 code0 = TREE_CODE (TREE_TYPE (args[0]));
10001 code1 = TREE_CODE (TREE_TYPE (args[1]));
10002 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10003 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10004 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10006 error ("non-floating-point arguments in call to "
10007 "function %qE", fndecl);
10008 return false;
10010 return true;
10012 return false;
10014 case BUILT_IN_FPCLASSIFY:
10015 if (builtin_function_validate_nargs (fndecl, nargs, 6))
10017 unsigned i;
10019 for (i=0; i<5; i++)
10020 if (TREE_CODE (args[i]) != INTEGER_CST)
10022 error ("non-const integer argument %u in call to function %qE",
10023 i+1, fndecl);
10024 return false;
10027 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10029 error ("non-floating-point argument in call to function %qE",
10030 fndecl);
10031 return false;
10033 return true;
10035 return false;
10037 case BUILT_IN_ASSUME_ALIGNED:
10038 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10040 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10042 error ("non-integer argument 3 in call to function %qE", fndecl);
10043 return false;
10045 return true;
10047 return false;
10049 case BUILT_IN_ADD_OVERFLOW:
10050 case BUILT_IN_SUB_OVERFLOW:
10051 case BUILT_IN_MUL_OVERFLOW:
10052 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10054 unsigned i;
10055 for (i = 0; i < 2; i++)
10056 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10058 error ("argument %u in call to function %qE does not have "
10059 "integral type", i + 1, fndecl);
10060 return false;
10062 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10063 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10065 error ("argument 3 in call to function %qE does not have "
10066 "pointer to integer type", fndecl);
10067 return false;
10069 return true;
10071 return false;
10073 default:
10074 return true;
10078 /* Function to help qsort sort FIELD_DECLs by name order. */
10081 field_decl_cmp (const void *x_p, const void *y_p)
10083 const tree *const x = (const tree *const) x_p;
10084 const tree *const y = (const tree *const) y_p;
10086 if (DECL_NAME (*x) == DECL_NAME (*y))
10087 /* A nontype is "greater" than a type. */
10088 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10089 if (DECL_NAME (*x) == NULL_TREE)
10090 return -1;
10091 if (DECL_NAME (*y) == NULL_TREE)
10092 return 1;
10093 if (DECL_NAME (*x) < DECL_NAME (*y))
10094 return -1;
10095 return 1;
10098 static struct {
10099 gt_pointer_operator new_value;
10100 void *cookie;
10101 } resort_data;
10103 /* This routine compares two fields like field_decl_cmp but using the
10104 pointer operator in resort_data. */
10106 static int
10107 resort_field_decl_cmp (const void *x_p, const void *y_p)
10109 const tree *const x = (const tree *const) x_p;
10110 const tree *const y = (const tree *const) y_p;
10112 if (DECL_NAME (*x) == DECL_NAME (*y))
10113 /* A nontype is "greater" than a type. */
10114 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10115 if (DECL_NAME (*x) == NULL_TREE)
10116 return -1;
10117 if (DECL_NAME (*y) == NULL_TREE)
10118 return 1;
10120 tree d1 = DECL_NAME (*x);
10121 tree d2 = DECL_NAME (*y);
10122 resort_data.new_value (&d1, resort_data.cookie);
10123 resort_data.new_value (&d2, resort_data.cookie);
10124 if (d1 < d2)
10125 return -1;
10127 return 1;
10130 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10132 void
10133 resort_sorted_fields (void *obj,
10134 void * ARG_UNUSED (orig_obj),
10135 gt_pointer_operator new_value,
10136 void *cookie)
10138 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10139 resort_data.new_value = new_value;
10140 resort_data.cookie = cookie;
10141 qsort (&sf->elts[0], sf->len, sizeof (tree),
10142 resort_field_decl_cmp);
10145 /* Subroutine of c_parse_error.
10146 Return the result of concatenating LHS and RHS. RHS is really
10147 a string literal, its first character is indicated by RHS_START and
10148 RHS_SIZE is its length (including the terminating NUL character).
10150 The caller is responsible for deleting the returned pointer. */
10152 static char *
10153 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10155 const int lhs_size = strlen (lhs);
10156 char *result = XNEWVEC (char, lhs_size + rhs_size);
10157 strncpy (result, lhs, lhs_size);
10158 strncpy (result + lhs_size, rhs_start, rhs_size);
10159 return result;
10162 /* Issue the error given by GMSGID, indicating that it occurred before
10163 TOKEN, which had the associated VALUE. */
10165 void
10166 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
10167 tree value, unsigned char token_flags)
10169 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10171 char *message = NULL;
10173 if (token_type == CPP_EOF)
10174 message = catenate_messages (gmsgid, " at end of input");
10175 else if (token_type == CPP_CHAR
10176 || token_type == CPP_WCHAR
10177 || token_type == CPP_CHAR16
10178 || token_type == CPP_CHAR32)
10180 unsigned int val = TREE_INT_CST_LOW (value);
10181 const char *prefix;
10183 switch (token_type)
10185 default:
10186 prefix = "";
10187 break;
10188 case CPP_WCHAR:
10189 prefix = "L";
10190 break;
10191 case CPP_CHAR16:
10192 prefix = "u";
10193 break;
10194 case CPP_CHAR32:
10195 prefix = "U";
10196 break;
10199 if (val <= UCHAR_MAX && ISGRAPH (val))
10200 message = catenate_messages (gmsgid, " before %s'%c'");
10201 else
10202 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10204 error (message, prefix, val);
10205 free (message);
10206 message = NULL;
10208 else if (token_type == CPP_CHAR_USERDEF
10209 || token_type == CPP_WCHAR_USERDEF
10210 || token_type == CPP_CHAR16_USERDEF
10211 || token_type == CPP_CHAR32_USERDEF)
10212 message = catenate_messages (gmsgid,
10213 " before user-defined character literal");
10214 else if (token_type == CPP_STRING_USERDEF
10215 || token_type == CPP_WSTRING_USERDEF
10216 || token_type == CPP_STRING16_USERDEF
10217 || token_type == CPP_STRING32_USERDEF
10218 || token_type == CPP_UTF8STRING_USERDEF)
10219 message = catenate_messages (gmsgid, " before user-defined string literal");
10220 else if (token_type == CPP_STRING
10221 || token_type == CPP_WSTRING
10222 || token_type == CPP_STRING16
10223 || token_type == CPP_STRING32
10224 || token_type == CPP_UTF8STRING)
10225 message = catenate_messages (gmsgid, " before string constant");
10226 else if (token_type == CPP_NUMBER)
10227 message = catenate_messages (gmsgid, " before numeric constant");
10228 else if (token_type == CPP_NAME)
10230 message = catenate_messages (gmsgid, " before %qE");
10231 error (message, value);
10232 free (message);
10233 message = NULL;
10235 else if (token_type == CPP_PRAGMA)
10236 message = catenate_messages (gmsgid, " before %<#pragma%>");
10237 else if (token_type == CPP_PRAGMA_EOL)
10238 message = catenate_messages (gmsgid, " before end of line");
10239 else if (token_type == CPP_DECLTYPE)
10240 message = catenate_messages (gmsgid, " before %<decltype%>");
10241 else if (token_type < N_TTYPES)
10243 message = catenate_messages (gmsgid, " before %qs token");
10244 error (message, cpp_type2name (token_type, token_flags));
10245 free (message);
10246 message = NULL;
10248 else
10249 error (gmsgid);
10251 if (message)
10253 error (message);
10254 free (message);
10256 #undef catenate_messages
10259 /* Return the gcc option code associated with the reason for a cpp
10260 message, or 0 if none. */
10262 static int
10263 c_option_controlling_cpp_error (int reason)
10265 const struct cpp_reason_option_codes_t *entry;
10267 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10269 if (entry->reason == reason)
10270 return entry->option_code;
10272 return 0;
10275 /* Callback from cpp_error for PFILE to print diagnostics from the
10276 preprocessor. The diagnostic is of type LEVEL, with REASON set
10277 to the reason code if LEVEL is represents a warning, at location
10278 LOCATION unless this is after lexing and the compiler's location
10279 should be used instead, with column number possibly overridden by
10280 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10281 the arguments. Returns true if a diagnostic was emitted, false
10282 otherwise. */
10284 bool
10285 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10286 location_t location, unsigned int column_override,
10287 const char *msg, va_list *ap)
10289 diagnostic_info diagnostic;
10290 diagnostic_t dlevel;
10291 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10292 bool ret;
10294 switch (level)
10296 case CPP_DL_WARNING_SYSHDR:
10297 if (flag_no_output)
10298 return false;
10299 global_dc->dc_warn_system_headers = 1;
10300 /* Fall through. */
10301 case CPP_DL_WARNING:
10302 if (flag_no_output)
10303 return false;
10304 dlevel = DK_WARNING;
10305 break;
10306 case CPP_DL_PEDWARN:
10307 if (flag_no_output && !flag_pedantic_errors)
10308 return false;
10309 dlevel = DK_PEDWARN;
10310 break;
10311 case CPP_DL_ERROR:
10312 dlevel = DK_ERROR;
10313 break;
10314 case CPP_DL_ICE:
10315 dlevel = DK_ICE;
10316 break;
10317 case CPP_DL_NOTE:
10318 dlevel = DK_NOTE;
10319 break;
10320 case CPP_DL_FATAL:
10321 dlevel = DK_FATAL;
10322 break;
10323 default:
10324 gcc_unreachable ();
10326 if (done_lexing)
10327 location = input_location;
10328 diagnostic_set_info_translated (&diagnostic, msg, ap,
10329 location, dlevel);
10330 if (column_override)
10331 diagnostic_override_column (&diagnostic, column_override);
10332 diagnostic_override_option_index (&diagnostic,
10333 c_option_controlling_cpp_error (reason));
10334 ret = report_diagnostic (&diagnostic);
10335 if (level == CPP_DL_WARNING_SYSHDR)
10336 global_dc->dc_warn_system_headers = save_warn_system_headers;
10337 return ret;
10340 /* Convert a character from the host to the target execution character
10341 set. cpplib handles this, mostly. */
10343 HOST_WIDE_INT
10344 c_common_to_target_charset (HOST_WIDE_INT c)
10346 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10347 zero-extended under -fno-signed-char. cpplib insists that characters
10348 and character constants are always unsigned. Hence we must convert
10349 back and forth. */
10350 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10352 uc = cpp_host_to_exec_charset (parse_in, uc);
10354 if (flag_signed_char)
10355 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10356 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10357 else
10358 return uc;
10361 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10362 references with an INDIRECT_REF of a constant at the bottom; much like the
10363 traditional rendering of offsetof as a macro. Return the folded result. */
10365 tree
10366 fold_offsetof_1 (tree expr)
10368 tree base, off, t;
10370 switch (TREE_CODE (expr))
10372 case ERROR_MARK:
10373 return expr;
10375 case VAR_DECL:
10376 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10377 return error_mark_node;
10379 case CALL_EXPR:
10380 case TARGET_EXPR:
10381 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10382 return error_mark_node;
10384 case NOP_EXPR:
10385 case INDIRECT_REF:
10386 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10388 error ("cannot apply %<offsetof%> to a non constant address");
10389 return error_mark_node;
10391 return TREE_OPERAND (expr, 0);
10393 case COMPONENT_REF:
10394 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10395 if (base == error_mark_node)
10396 return base;
10398 t = TREE_OPERAND (expr, 1);
10399 if (DECL_C_BIT_FIELD (t))
10401 error ("attempt to take address of bit-field structure "
10402 "member %qD", t);
10403 return error_mark_node;
10405 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10406 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10407 / BITS_PER_UNIT));
10408 break;
10410 case ARRAY_REF:
10411 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
10412 if (base == error_mark_node)
10413 return base;
10415 t = TREE_OPERAND (expr, 1);
10417 /* Check if the offset goes beyond the upper bound of the array. */
10418 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10420 tree upbound = array_ref_up_bound (expr);
10421 if (upbound != NULL_TREE
10422 && TREE_CODE (upbound) == INTEGER_CST
10423 && !tree_int_cst_equal (upbound,
10424 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10426 upbound = size_binop (PLUS_EXPR, upbound,
10427 build_int_cst (TREE_TYPE (upbound), 1));
10428 if (tree_int_cst_lt (upbound, t))
10430 tree v;
10432 for (v = TREE_OPERAND (expr, 0);
10433 TREE_CODE (v) == COMPONENT_REF;
10434 v = TREE_OPERAND (v, 0))
10435 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10436 == RECORD_TYPE)
10438 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10439 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10440 if (TREE_CODE (fld_chain) == FIELD_DECL)
10441 break;
10443 if (fld_chain)
10444 break;
10446 /* Don't warn if the array might be considered a poor
10447 man's flexible array member with a very permissive
10448 definition thereof. */
10449 if (TREE_CODE (v) == ARRAY_REF
10450 || TREE_CODE (v) == COMPONENT_REF)
10451 warning (OPT_Warray_bounds,
10452 "index %E denotes an offset "
10453 "greater than size of %qT",
10454 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10459 t = convert (sizetype, t);
10460 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10461 break;
10463 case COMPOUND_EXPR:
10464 /* Handle static members of volatile structs. */
10465 t = TREE_OPERAND (expr, 1);
10466 gcc_assert (TREE_CODE (t) == VAR_DECL);
10467 return fold_offsetof_1 (t);
10469 default:
10470 gcc_unreachable ();
10473 return fold_build_pointer_plus (base, off);
10476 /* Likewise, but convert it to the return type of offsetof. */
10478 tree
10479 fold_offsetof (tree expr)
10481 return convert (size_type_node, fold_offsetof_1 (expr));
10484 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10485 expression, because B will always be true. */
10487 void
10488 warn_for_omitted_condop (location_t location, tree cond)
10490 if (truth_value_p (TREE_CODE (cond)))
10491 warning_at (location, OPT_Wparentheses,
10492 "the omitted middle operand in ?: will always be %<true%>, "
10493 "suggest explicit middle operand");
10496 /* Give an error for storing into ARG, which is 'const'. USE indicates
10497 how ARG was being used. */
10499 void
10500 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10502 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10503 || use == lv_asm);
10504 /* Using this macro rather than (for example) arrays of messages
10505 ensures that all the format strings are checked at compile
10506 time. */
10507 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10508 : (use == lv_increment ? (I) \
10509 : (use == lv_decrement ? (D) : (AS))))
10510 if (TREE_CODE (arg) == COMPONENT_REF)
10512 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10513 error_at (loc, READONLY_MSG (G_("assignment of member "
10514 "%qD in read-only object"),
10515 G_("increment of member "
10516 "%qD in read-only object"),
10517 G_("decrement of member "
10518 "%qD in read-only object"),
10519 G_("member %qD in read-only object "
10520 "used as %<asm%> output")),
10521 TREE_OPERAND (arg, 1));
10522 else
10523 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10524 G_("increment of read-only member %qD"),
10525 G_("decrement of read-only member %qD"),
10526 G_("read-only member %qD used as %<asm%> output")),
10527 TREE_OPERAND (arg, 1));
10529 else if (TREE_CODE (arg) == VAR_DECL)
10530 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10531 G_("increment of read-only variable %qD"),
10532 G_("decrement of read-only variable %qD"),
10533 G_("read-only variable %qD used as %<asm%> output")),
10534 arg);
10535 else if (TREE_CODE (arg) == PARM_DECL)
10536 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10537 G_("increment of read-only parameter %qD"),
10538 G_("decrement of read-only parameter %qD"),
10539 G_("read-only parameter %qD use as %<asm%> output")),
10540 arg);
10541 else if (TREE_CODE (arg) == RESULT_DECL)
10543 gcc_assert (c_dialect_cxx ());
10544 error_at (loc, READONLY_MSG (G_("assignment of "
10545 "read-only named return value %qD"),
10546 G_("increment of "
10547 "read-only named return value %qD"),
10548 G_("decrement of "
10549 "read-only named return value %qD"),
10550 G_("read-only named return value %qD "
10551 "used as %<asm%>output")),
10552 arg);
10554 else if (TREE_CODE (arg) == FUNCTION_DECL)
10555 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10556 G_("increment of function %qD"),
10557 G_("decrement of function %qD"),
10558 G_("function %qD used as %<asm%> output")),
10559 arg);
10560 else
10561 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10562 G_("increment of read-only location %qE"),
10563 G_("decrement of read-only location %qE"),
10564 G_("read-only location %qE used as %<asm%> output")),
10565 arg);
10568 /* Print an error message for an invalid lvalue. USE says
10569 how the lvalue is being used and so selects the error message. LOC
10570 is the location for the error. */
10572 void
10573 lvalue_error (location_t loc, enum lvalue_use use)
10575 switch (use)
10577 case lv_assign:
10578 error_at (loc, "lvalue required as left operand of assignment");
10579 break;
10580 case lv_increment:
10581 error_at (loc, "lvalue required as increment operand");
10582 break;
10583 case lv_decrement:
10584 error_at (loc, "lvalue required as decrement operand");
10585 break;
10586 case lv_addressof:
10587 error_at (loc, "lvalue required as unary %<&%> operand");
10588 break;
10589 case lv_asm:
10590 error_at (loc, "lvalue required in asm statement");
10591 break;
10592 default:
10593 gcc_unreachable ();
10597 /* Print an error message for an invalid indirection of type TYPE.
10598 ERRSTRING is the name of the operator for the indirection. */
10600 void
10601 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10603 switch (errstring)
10605 case RO_NULL:
10606 gcc_assert (c_dialect_cxx ());
10607 error_at (loc, "invalid type argument (have %qT)", type);
10608 break;
10609 case RO_ARRAY_INDEXING:
10610 error_at (loc,
10611 "invalid type argument of array indexing (have %qT)",
10612 type);
10613 break;
10614 case RO_UNARY_STAR:
10615 error_at (loc,
10616 "invalid type argument of unary %<*%> (have %qT)",
10617 type);
10618 break;
10619 case RO_ARROW:
10620 error_at (loc,
10621 "invalid type argument of %<->%> (have %qT)",
10622 type);
10623 break;
10624 case RO_ARROW_STAR:
10625 error_at (loc,
10626 "invalid type argument of %<->*%> (have %qT)",
10627 type);
10628 break;
10629 case RO_IMPLICIT_CONVERSION:
10630 error_at (loc,
10631 "invalid type argument of implicit conversion (have %qT)",
10632 type);
10633 break;
10634 default:
10635 gcc_unreachable ();
10639 /* *PTYPE is an incomplete array. Complete it with a domain based on
10640 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10641 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10642 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10645 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10647 tree maxindex, type, main_type, elt, unqual_elt;
10648 int failure = 0, quals;
10649 hashval_t hashcode = 0;
10650 bool overflow_p = false;
10652 maxindex = size_zero_node;
10653 if (initial_value)
10655 if (TREE_CODE (initial_value) == STRING_CST)
10657 int eltsize
10658 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10659 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10661 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10663 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10665 if (vec_safe_is_empty (v))
10667 if (pedantic)
10668 failure = 3;
10669 maxindex = ssize_int (-1);
10671 else
10673 tree curindex;
10674 unsigned HOST_WIDE_INT cnt;
10675 constructor_elt *ce;
10676 bool fold_p = false;
10678 if ((*v)[0].index)
10679 maxindex = (*v)[0].index, fold_p = true;
10681 curindex = maxindex;
10683 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10685 bool curfold_p = false;
10686 if (ce->index)
10687 curindex = ce->index, curfold_p = true;
10688 else
10690 if (fold_p)
10692 /* Since we treat size types now as ordinary
10693 unsigned types, we need an explicit overflow
10694 check. */
10695 tree orig = curindex;
10696 curindex = fold_convert (sizetype, curindex);
10697 overflow_p |= tree_int_cst_lt (curindex, orig);
10699 curindex = size_binop (PLUS_EXPR, curindex,
10700 size_one_node);
10702 if (tree_int_cst_lt (maxindex, curindex))
10703 maxindex = curindex, fold_p = curfold_p;
10705 if (fold_p)
10707 tree orig = maxindex;
10708 maxindex = fold_convert (sizetype, maxindex);
10709 overflow_p |= tree_int_cst_lt (maxindex, orig);
10713 else
10715 /* Make an error message unless that happened already. */
10716 if (initial_value != error_mark_node)
10717 failure = 1;
10720 else
10722 failure = 2;
10723 if (!do_default)
10724 return failure;
10727 type = *ptype;
10728 elt = TREE_TYPE (type);
10729 quals = TYPE_QUALS (strip_array_types (elt));
10730 if (quals == 0)
10731 unqual_elt = elt;
10732 else
10733 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10735 /* Using build_distinct_type_copy and modifying things afterward instead
10736 of using build_array_type to create a new type preserves all of the
10737 TYPE_LANG_FLAG_? bits that the front end may have set. */
10738 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10739 TREE_TYPE (main_type) = unqual_elt;
10740 TYPE_DOMAIN (main_type)
10741 = build_range_type (TREE_TYPE (maxindex),
10742 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10743 layout_type (main_type);
10745 /* Make sure we have the canonical MAIN_TYPE. */
10746 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10747 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10748 hashcode);
10749 main_type = type_hash_canon (hashcode, main_type);
10751 /* Fix the canonical type. */
10752 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10753 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10754 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10755 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10756 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10757 != TYPE_DOMAIN (main_type)))
10758 TYPE_CANONICAL (main_type)
10759 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10760 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10761 else
10762 TYPE_CANONICAL (main_type) = main_type;
10764 if (quals == 0)
10765 type = main_type;
10766 else
10767 type = c_build_qualified_type (main_type, quals);
10769 if (COMPLETE_TYPE_P (type)
10770 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10771 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10773 error ("size of array is too large");
10774 /* If we proceed with the array type as it is, we'll eventually
10775 crash in tree_to_[su]hwi(). */
10776 type = error_mark_node;
10779 *ptype = type;
10780 return failure;
10783 /* Like c_mark_addressable but don't check register qualifier. */
10784 void
10785 c_common_mark_addressable_vec (tree t)
10787 while (handled_component_p (t))
10788 t = TREE_OPERAND (t, 0);
10789 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10790 return;
10791 TREE_ADDRESSABLE (t) = 1;
10796 /* Used to help initialize the builtin-types.def table. When a type of
10797 the correct size doesn't exist, use error_mark_node instead of NULL.
10798 The later results in segfaults even when a decl using the type doesn't
10799 get invoked. */
10801 tree
10802 builtin_type_for_size (int size, bool unsignedp)
10804 tree type = c_common_type_for_size (size, unsignedp);
10805 return type ? type : error_mark_node;
10808 /* A helper function for resolve_overloaded_builtin in resolving the
10809 overloaded __sync_ builtins. Returns a positive power of 2 if the
10810 first operand of PARAMS is a pointer to a supported data type.
10811 Returns 0 if an error is encountered. */
10813 static int
10814 sync_resolve_size (tree function, vec<tree, va_gc> *params)
10816 tree type;
10817 int size;
10819 if (!params)
10821 error ("too few arguments to function %qE", function);
10822 return 0;
10825 type = TREE_TYPE ((*params)[0]);
10826 if (TREE_CODE (type) == ARRAY_TYPE)
10828 /* Force array-to-pointer decay for C++. */
10829 gcc_assert (c_dialect_cxx());
10830 (*params)[0] = default_conversion ((*params)[0]);
10831 type = TREE_TYPE ((*params)[0]);
10833 if (TREE_CODE (type) != POINTER_TYPE)
10834 goto incompatible;
10836 type = TREE_TYPE (type);
10837 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10838 goto incompatible;
10840 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10841 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10842 return size;
10844 incompatible:
10845 error ("incompatible type for argument %d of %qE", 1, function);
10846 return 0;
10849 /* A helper function for resolve_overloaded_builtin. Adds casts to
10850 PARAMS to make arguments match up with those of FUNCTION. Drops
10851 the variadic arguments at the end. Returns false if some error
10852 was encountered; true on success. */
10854 static bool
10855 sync_resolve_params (location_t loc, tree orig_function, tree function,
10856 vec<tree, va_gc> *params, bool orig_format)
10858 function_args_iterator iter;
10859 tree ptype;
10860 unsigned int parmnum;
10862 function_args_iter_init (&iter, TREE_TYPE (function));
10863 /* We've declared the implementation functions to use "volatile void *"
10864 as the pointer parameter, so we shouldn't get any complaints from the
10865 call to check_function_arguments what ever type the user used. */
10866 function_args_iter_next (&iter);
10867 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10868 ptype = TYPE_MAIN_VARIANT (ptype);
10870 /* For the rest of the values, we need to cast these to FTYPE, so that we
10871 don't get warnings for passing pointer types, etc. */
10872 parmnum = 0;
10873 while (1)
10875 tree val, arg_type;
10877 arg_type = function_args_iter_cond (&iter);
10878 /* XXX void_type_node belies the abstraction. */
10879 if (arg_type == void_type_node)
10880 break;
10882 ++parmnum;
10883 if (params->length () <= parmnum)
10885 error_at (loc, "too few arguments to function %qE", orig_function);
10886 return false;
10889 /* Only convert parameters if arg_type is unsigned integer type with
10890 new format sync routines, i.e. don't attempt to convert pointer
10891 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10892 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10893 kinds). */
10894 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10896 /* Ideally for the first conversion we'd use convert_for_assignment
10897 so that we get warnings for anything that doesn't match the pointer
10898 type. This isn't portable across the C and C++ front ends atm. */
10899 val = (*params)[parmnum];
10900 val = convert (ptype, val);
10901 val = convert (arg_type, val);
10902 (*params)[parmnum] = val;
10905 function_args_iter_next (&iter);
10908 /* __atomic routines are not variadic. */
10909 if (!orig_format && params->length () != parmnum + 1)
10911 error_at (loc, "too many arguments to function %qE", orig_function);
10912 return false;
10915 /* The definition of these primitives is variadic, with the remaining
10916 being "an optional list of variables protected by the memory barrier".
10917 No clue what that's supposed to mean, precisely, but we consider all
10918 call-clobbered variables to be protected so we're safe. */
10919 params->truncate (parmnum + 1);
10921 return true;
10924 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10925 RESULT to make it match the type of the first pointer argument in
10926 PARAMS. */
10928 static tree
10929 sync_resolve_return (tree first_param, tree result, bool orig_format)
10931 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10932 tree rtype = TREE_TYPE (result);
10933 ptype = TYPE_MAIN_VARIANT (ptype);
10935 /* New format doesn't require casting unless the types are the same size. */
10936 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10937 return convert (ptype, result);
10938 else
10939 return result;
10942 /* This function verifies the PARAMS to generic atomic FUNCTION.
10943 It returns the size if all the parameters are the same size, otherwise
10944 0 is returned if the parameters are invalid. */
10946 static int
10947 get_atomic_generic_size (location_t loc, tree function,
10948 vec<tree, va_gc> *params)
10950 unsigned int n_param;
10951 unsigned int n_model;
10952 unsigned int x;
10953 int size_0;
10954 tree type_0;
10956 /* Determine the parameter makeup. */
10957 switch (DECL_FUNCTION_CODE (function))
10959 case BUILT_IN_ATOMIC_EXCHANGE:
10960 n_param = 4;
10961 n_model = 1;
10962 break;
10963 case BUILT_IN_ATOMIC_LOAD:
10964 case BUILT_IN_ATOMIC_STORE:
10965 n_param = 3;
10966 n_model = 1;
10967 break;
10968 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10969 n_param = 6;
10970 n_model = 2;
10971 break;
10972 default:
10973 gcc_unreachable ();
10976 if (vec_safe_length (params) != n_param)
10978 error_at (loc, "incorrect number of arguments to function %qE", function);
10979 return 0;
10982 /* Get type of first parameter, and determine its size. */
10983 type_0 = TREE_TYPE ((*params)[0]);
10984 if (TREE_CODE (type_0) == ARRAY_TYPE)
10986 /* Force array-to-pointer decay for C++. */
10987 gcc_assert (c_dialect_cxx());
10988 (*params)[0] = default_conversion ((*params)[0]);
10989 type_0 = TREE_TYPE ((*params)[0]);
10991 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10993 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10994 function);
10995 return 0;
10998 /* Types must be compile time constant sizes. */
10999 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
11001 error_at (loc,
11002 "argument 1 of %qE must be a pointer to a constant size type",
11003 function);
11004 return 0;
11007 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
11009 /* Zero size objects are not allowed. */
11010 if (size_0 == 0)
11012 error_at (loc,
11013 "argument 1 of %qE must be a pointer to a nonzero size object",
11014 function);
11015 return 0;
11018 /* Check each other parameter is a pointer and the same size. */
11019 for (x = 0; x < n_param - n_model; x++)
11021 int size;
11022 tree type = TREE_TYPE ((*params)[x]);
11023 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
11024 if (n_param == 6 && x == 3)
11025 continue;
11026 if (!POINTER_TYPE_P (type))
11028 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11029 function);
11030 return 0;
11032 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11033 size = type_size ? tree_to_uhwi (type_size) : 0;
11034 if (size != size_0)
11036 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11037 function);
11038 return 0;
11042 /* Check memory model parameters for validity. */
11043 for (x = n_param - n_model ; x < n_param; x++)
11045 tree p = (*params)[x];
11046 if (TREE_CODE (p) == INTEGER_CST)
11048 int i = tree_to_uhwi (p);
11049 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
11051 warning_at (loc, OPT_Winvalid_memory_model,
11052 "invalid memory model argument %d of %qE", x + 1,
11053 function);
11056 else
11057 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11059 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11060 function);
11061 return 0;
11065 return size_0;
11069 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11070 at the beginning of the parameter list PARAMS representing the size of the
11071 objects. This is to match the library ABI requirement. LOC is the location
11072 of the function call.
11073 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11074 returned to allow the external call to be constructed. */
11076 static tree
11077 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
11078 vec<tree, va_gc> *params)
11080 tree size_node;
11082 /* Insert a SIZE_T parameter as the first param. If there isn't
11083 enough space, allocate a new vector and recursively re-build with that. */
11084 if (!params->space (1))
11086 unsigned int z, len;
11087 vec<tree, va_gc> *v;
11088 tree f;
11090 len = params->length ();
11091 vec_alloc (v, len + 1);
11092 v->quick_push (build_int_cst (size_type_node, n));
11093 for (z = 0; z < len; z++)
11094 v->quick_push ((*params)[z]);
11095 f = build_function_call_vec (loc, vNULL, function, v, NULL);
11096 vec_free (v);
11097 return f;
11100 /* Add the size parameter and leave as a function call for processing. */
11101 size_node = build_int_cst (size_type_node, n);
11102 params->quick_insert (0, size_node);
11103 return NULL_TREE;
11107 /* Return whether atomic operations for naturally aligned N-byte
11108 arguments are supported, whether inline or through libatomic. */
11109 static bool
11110 atomic_size_supported_p (int n)
11112 switch (n)
11114 case 1:
11115 case 2:
11116 case 4:
11117 case 8:
11118 return true;
11120 case 16:
11121 return targetm.scalar_mode_supported_p (TImode);
11123 default:
11124 return false;
11128 /* This will process an __atomic_exchange function call, determine whether it
11129 needs to be mapped to the _N variation, or turned into a library call.
11130 LOC is the location of the builtin call.
11131 FUNCTION is the DECL that has been invoked;
11132 PARAMS is the argument list for the call. The return value is non-null
11133 TRUE is returned if it is translated into the proper format for a call to the
11134 external library, and NEW_RETURN is set the tree for that function.
11135 FALSE is returned if processing for the _N variation is required, and
11136 NEW_RETURN is set to the the return value the result is copied into. */
11137 static bool
11138 resolve_overloaded_atomic_exchange (location_t loc, tree function,
11139 vec<tree, va_gc> *params, tree *new_return)
11141 tree p0, p1, p2, p3;
11142 tree I_type, I_type_ptr;
11143 int n = get_atomic_generic_size (loc, function, params);
11145 /* Size of 0 is an error condition. */
11146 if (n == 0)
11148 *new_return = error_mark_node;
11149 return true;
11152 /* If not a lock-free size, change to the library generic format. */
11153 if (!atomic_size_supported_p (n))
11155 *new_return = add_atomic_size_parameter (n, loc, function, params);
11156 return true;
11159 /* Otherwise there is a lockfree match, transform the call from:
11160 void fn(T* mem, T* desired, T* return, model)
11161 into
11162 *return = (T) (fn (In* mem, (In) *desired, model)) */
11164 p0 = (*params)[0];
11165 p1 = (*params)[1];
11166 p2 = (*params)[2];
11167 p3 = (*params)[3];
11169 /* Create pointer to appropriate size. */
11170 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11171 I_type_ptr = build_pointer_type (I_type);
11173 /* Convert object pointer to required type. */
11174 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11175 (*params)[0] = p0;
11176 /* Convert new value to required type, and dereference it. */
11177 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11178 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11179 (*params)[1] = p1;
11181 /* Move memory model to the 3rd position, and end param list. */
11182 (*params)[2] = p3;
11183 params->truncate (3);
11185 /* Convert return pointer and dereference it for later assignment. */
11186 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11188 return false;
11192 /* This will process an __atomic_compare_exchange function call, determine
11193 whether it needs to be mapped to the _N variation, or turned into a lib call.
11194 LOC is the location of the builtin call.
11195 FUNCTION is the DECL that has been invoked;
11196 PARAMS is the argument list for the call. The return value is non-null
11197 TRUE is returned if it is translated into the proper format for a call to the
11198 external library, and NEW_RETURN is set the tree for that function.
11199 FALSE is returned if processing for the _N variation is required. */
11201 static bool
11202 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11203 vec<tree, va_gc> *params,
11204 tree *new_return)
11206 tree p0, p1, p2;
11207 tree I_type, I_type_ptr;
11208 int n = get_atomic_generic_size (loc, function, params);
11210 /* Size of 0 is an error condition. */
11211 if (n == 0)
11213 *new_return = error_mark_node;
11214 return true;
11217 /* If not a lock-free size, change to the library generic format. */
11218 if (!atomic_size_supported_p (n))
11220 /* The library generic format does not have the weak parameter, so
11221 remove it from the param list. Since a parameter has been removed,
11222 we can be sure that there is room for the SIZE_T parameter, meaning
11223 there will not be a recursive rebuilding of the parameter list, so
11224 there is no danger this will be done twice. */
11225 if (n > 0)
11227 (*params)[3] = (*params)[4];
11228 (*params)[4] = (*params)[5];
11229 params->truncate (5);
11231 *new_return = add_atomic_size_parameter (n, loc, function, params);
11232 return true;
11235 /* Otherwise, there is a match, so the call needs to be transformed from:
11236 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11237 into
11238 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11240 p0 = (*params)[0];
11241 p1 = (*params)[1];
11242 p2 = (*params)[2];
11244 /* Create pointer to appropriate size. */
11245 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11246 I_type_ptr = build_pointer_type (I_type);
11248 /* Convert object pointer to required type. */
11249 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11250 (*params)[0] = p0;
11252 /* Convert expected pointer to required type. */
11253 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11254 (*params)[1] = p1;
11256 /* Convert desired value to required type, and dereference it. */
11257 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11258 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11259 (*params)[2] = p2;
11261 /* The rest of the parameters are fine. NULL means no special return value
11262 processing.*/
11263 *new_return = NULL;
11264 return false;
11268 /* This will process an __atomic_load function call, determine whether it
11269 needs to be mapped to the _N variation, or turned into a library call.
11270 LOC is the location of the builtin call.
11271 FUNCTION is the DECL that has been invoked;
11272 PARAMS is the argument list for the call. The return value is non-null
11273 TRUE is returned if it is translated into the proper format for a call to the
11274 external library, and NEW_RETURN is set the tree for that function.
11275 FALSE is returned if processing for the _N variation is required, and
11276 NEW_RETURN is set to the the return value the result is copied into. */
11278 static bool
11279 resolve_overloaded_atomic_load (location_t loc, tree function,
11280 vec<tree, va_gc> *params, tree *new_return)
11282 tree p0, p1, p2;
11283 tree I_type, I_type_ptr;
11284 int n = get_atomic_generic_size (loc, function, params);
11286 /* Size of 0 is an error condition. */
11287 if (n == 0)
11289 *new_return = error_mark_node;
11290 return true;
11293 /* If not a lock-free size, change to the library generic format. */
11294 if (!atomic_size_supported_p (n))
11296 *new_return = add_atomic_size_parameter (n, loc, function, params);
11297 return true;
11300 /* Otherwise, there is a match, so the call needs to be transformed from:
11301 void fn(T* mem, T* return, model)
11302 into
11303 *return = (T) (fn ((In *) mem, model)) */
11305 p0 = (*params)[0];
11306 p1 = (*params)[1];
11307 p2 = (*params)[2];
11309 /* Create pointer to appropriate size. */
11310 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11311 I_type_ptr = build_pointer_type (I_type);
11313 /* Convert object pointer to required type. */
11314 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11315 (*params)[0] = p0;
11317 /* Move memory model to the 2nd position, and end param list. */
11318 (*params)[1] = p2;
11319 params->truncate (2);
11321 /* Convert return pointer and dereference it for later assignment. */
11322 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11324 return false;
11328 /* This will process an __atomic_store function call, determine whether it
11329 needs to be mapped to the _N variation, or turned into a library call.
11330 LOC is the location of the builtin call.
11331 FUNCTION is the DECL that has been invoked;
11332 PARAMS is the argument list for the call. The return value is non-null
11333 TRUE is returned if it is translated into the proper format for a call to the
11334 external library, and NEW_RETURN is set the tree for that function.
11335 FALSE is returned if processing for the _N variation is required, and
11336 NEW_RETURN is set to the the return value the result is copied into. */
11338 static bool
11339 resolve_overloaded_atomic_store (location_t loc, tree function,
11340 vec<tree, va_gc> *params, tree *new_return)
11342 tree p0, p1;
11343 tree I_type, I_type_ptr;
11344 int n = get_atomic_generic_size (loc, function, params);
11346 /* Size of 0 is an error condition. */
11347 if (n == 0)
11349 *new_return = error_mark_node;
11350 return true;
11353 /* If not a lock-free size, change to the library generic format. */
11354 if (!atomic_size_supported_p (n))
11356 *new_return = add_atomic_size_parameter (n, loc, function, params);
11357 return true;
11360 /* Otherwise, there is a match, so the call needs to be transformed from:
11361 void fn(T* mem, T* value, model)
11362 into
11363 fn ((In *) mem, (In) *value, model) */
11365 p0 = (*params)[0];
11366 p1 = (*params)[1];
11368 /* Create pointer to appropriate size. */
11369 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11370 I_type_ptr = build_pointer_type (I_type);
11372 /* Convert object pointer to required type. */
11373 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11374 (*params)[0] = p0;
11376 /* Convert new value to required type, and dereference it. */
11377 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11378 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11379 (*params)[1] = p1;
11381 /* The memory model is in the right spot already. Return is void. */
11382 *new_return = NULL_TREE;
11384 return false;
11388 /* Some builtin functions are placeholders for other expressions. This
11389 function should be called immediately after parsing the call expression
11390 before surrounding code has committed to the type of the expression.
11392 LOC is the location of the builtin call.
11394 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11395 PARAMS is the argument list for the call. The return value is non-null
11396 when expansion is complete, and null if normal processing should
11397 continue. */
11399 tree
11400 resolve_overloaded_builtin (location_t loc, tree function,
11401 vec<tree, va_gc> *params)
11403 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11404 bool orig_format = true;
11405 tree new_return = NULL_TREE;
11407 switch (DECL_BUILT_IN_CLASS (function))
11409 case BUILT_IN_NORMAL:
11410 break;
11411 case BUILT_IN_MD:
11412 if (targetm.resolve_overloaded_builtin)
11413 return targetm.resolve_overloaded_builtin (loc, function, params);
11414 else
11415 return NULL_TREE;
11416 default:
11417 return NULL_TREE;
11420 /* Handle BUILT_IN_NORMAL here. */
11421 switch (orig_code)
11423 case BUILT_IN_ATOMIC_EXCHANGE:
11424 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11425 case BUILT_IN_ATOMIC_LOAD:
11426 case BUILT_IN_ATOMIC_STORE:
11428 /* Handle these 4 together so that they can fall through to the next
11429 case if the call is transformed to an _N variant. */
11430 switch (orig_code)
11432 case BUILT_IN_ATOMIC_EXCHANGE:
11434 if (resolve_overloaded_atomic_exchange (loc, function, params,
11435 &new_return))
11436 return new_return;
11437 /* Change to the _N variant. */
11438 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11439 break;
11442 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11444 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11445 params,
11446 &new_return))
11447 return new_return;
11448 /* Change to the _N variant. */
11449 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11450 break;
11452 case BUILT_IN_ATOMIC_LOAD:
11454 if (resolve_overloaded_atomic_load (loc, function, params,
11455 &new_return))
11456 return new_return;
11457 /* Change to the _N variant. */
11458 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11459 break;
11461 case BUILT_IN_ATOMIC_STORE:
11463 if (resolve_overloaded_atomic_store (loc, function, params,
11464 &new_return))
11465 return new_return;
11466 /* Change to the _N variant. */
11467 orig_code = BUILT_IN_ATOMIC_STORE_N;
11468 break;
11470 default:
11471 gcc_unreachable ();
11473 /* Fallthrough to the normal processing. */
11475 case BUILT_IN_ATOMIC_EXCHANGE_N:
11476 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11477 case BUILT_IN_ATOMIC_LOAD_N:
11478 case BUILT_IN_ATOMIC_STORE_N:
11479 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11480 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11481 case BUILT_IN_ATOMIC_AND_FETCH_N:
11482 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11483 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11484 case BUILT_IN_ATOMIC_OR_FETCH_N:
11485 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11486 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11487 case BUILT_IN_ATOMIC_FETCH_AND_N:
11488 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11489 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11490 case BUILT_IN_ATOMIC_FETCH_OR_N:
11492 orig_format = false;
11493 /* Fallthru for parameter processing. */
11495 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11496 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11497 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11498 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11499 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11500 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11501 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11502 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11503 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11504 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11505 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11506 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11507 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11508 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11509 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11510 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11512 int n = sync_resolve_size (function, params);
11513 tree new_function, first_param, result;
11514 enum built_in_function fncode;
11516 if (n == 0)
11517 return error_mark_node;
11519 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11520 new_function = builtin_decl_explicit (fncode);
11521 if (!sync_resolve_params (loc, function, new_function, params,
11522 orig_format))
11523 return error_mark_node;
11525 first_param = (*params)[0];
11526 result = build_function_call_vec (loc, vNULL, new_function, params,
11527 NULL);
11528 if (result == error_mark_node)
11529 return result;
11530 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11531 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11532 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11533 result = sync_resolve_return (first_param, result, orig_format);
11535 /* If new_return is set, assign function to that expr and cast the
11536 result to void since the generic interface returned void. */
11537 if (new_return)
11539 /* Cast function result from I{1,2,4,8,16} to the required type. */
11540 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11541 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11542 result);
11543 TREE_SIDE_EFFECTS (result) = 1;
11544 protected_set_expr_location (result, loc);
11545 result = convert (void_type_node, result);
11547 return result;
11550 default:
11551 return NULL_TREE;
11555 /* vector_types_compatible_elements_p is used in type checks of vectors
11556 values used as operands of binary operators. Where it returns true, and
11557 the other checks of the caller succeed (being vector types in he first
11558 place, and matching number of elements), we can just treat the types
11559 as essentially the same.
11560 Contrast with vector_targets_convertible_p, which is used for vector
11561 pointer types, and vector_types_convertible_p, which will allow
11562 language-specific matches under the control of flag_lax_vector_conversions,
11563 and might still require a conversion. */
11564 /* True if vector types T1 and T2 can be inputs to the same binary
11565 operator without conversion.
11566 We don't check the overall vector size here because some of our callers
11567 want to give different error messages when the vectors are compatible
11568 except for the element count. */
11570 bool
11571 vector_types_compatible_elements_p (tree t1, tree t2)
11573 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11574 t1 = TREE_TYPE (t1);
11575 t2 = TREE_TYPE (t2);
11577 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11579 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11580 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11581 || c2 == FIXED_POINT_TYPE));
11583 t1 = c_common_signed_type (t1);
11584 t2 = c_common_signed_type (t2);
11585 /* Equality works here because c_common_signed_type uses
11586 TYPE_MAIN_VARIANT. */
11587 if (t1 == t2)
11588 return true;
11589 if (opaque && c1 == c2
11590 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11591 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11592 return true;
11593 return false;
11596 /* Check for missing format attributes on function pointers. LTYPE is
11597 the new type or left-hand side type. RTYPE is the old type or
11598 right-hand side type. Returns TRUE if LTYPE is missing the desired
11599 attribute. */
11601 bool
11602 check_missing_format_attribute (tree ltype, tree rtype)
11604 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11605 tree ra;
11607 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11608 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11609 break;
11610 if (ra)
11612 tree la;
11613 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11614 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11615 break;
11616 return !la;
11618 else
11619 return false;
11622 /* Subscripting with type char is likely to lose on a machine where
11623 chars are signed. So warn on any machine, but optionally. Don't
11624 warn for unsigned char since that type is safe. Don't warn for
11625 signed char because anyone who uses that must have done so
11626 deliberately. Furthermore, we reduce the false positive load by
11627 warning only for non-constant value of type char. */
11629 void
11630 warn_array_subscript_with_type_char (location_t loc, tree index)
11632 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11633 && TREE_CODE (index) != INTEGER_CST)
11634 warning_at (loc, OPT_Wchar_subscripts,
11635 "array subscript has type %<char%>");
11638 /* Implement -Wparentheses for the unexpected C precedence rules, to
11639 cover cases like x + y << z which readers are likely to
11640 misinterpret. We have seen an expression in which CODE is a binary
11641 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11642 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11643 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11644 expression was not formed using a binary or unary operator, or it
11645 was enclosed in parentheses. */
11647 void
11648 warn_about_parentheses (location_t loc, enum tree_code code,
11649 enum tree_code code_left, tree arg_left,
11650 enum tree_code code_right, tree arg_right)
11652 if (!warn_parentheses)
11653 return;
11655 /* This macro tests that the expression ARG with original tree code
11656 CODE appears to be a boolean expression. or the result of folding a
11657 boolean expression. */
11658 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11659 (truth_value_p (TREE_CODE (ARG)) \
11660 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11661 /* Folding may create 0 or 1 integers from other expressions. */ \
11662 || ((CODE) != INTEGER_CST \
11663 && (integer_onep (ARG) || integer_zerop (ARG))))
11665 switch (code)
11667 case LSHIFT_EXPR:
11668 if (code_left == PLUS_EXPR)
11669 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11670 "suggest parentheses around %<+%> inside %<<<%>");
11671 else if (code_right == PLUS_EXPR)
11672 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11673 "suggest parentheses around %<+%> inside %<<<%>");
11674 else if (code_left == MINUS_EXPR)
11675 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11676 "suggest parentheses around %<-%> inside %<<<%>");
11677 else if (code_right == MINUS_EXPR)
11678 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11679 "suggest parentheses around %<-%> inside %<<<%>");
11680 return;
11682 case RSHIFT_EXPR:
11683 if (code_left == PLUS_EXPR)
11684 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11685 "suggest parentheses around %<+%> inside %<>>%>");
11686 else if (code_right == PLUS_EXPR)
11687 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11688 "suggest parentheses around %<+%> inside %<>>%>");
11689 else if (code_left == MINUS_EXPR)
11690 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11691 "suggest parentheses around %<-%> inside %<>>%>");
11692 else if (code_right == MINUS_EXPR)
11693 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11694 "suggest parentheses around %<-%> inside %<>>%>");
11695 return;
11697 case TRUTH_ORIF_EXPR:
11698 if (code_left == TRUTH_ANDIF_EXPR)
11699 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11700 "suggest parentheses around %<&&%> within %<||%>");
11701 else if (code_right == TRUTH_ANDIF_EXPR)
11702 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11703 "suggest parentheses around %<&&%> within %<||%>");
11704 return;
11706 case BIT_IOR_EXPR:
11707 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11708 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11709 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11710 "suggest parentheses around arithmetic in operand of %<|%>");
11711 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11712 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11713 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11714 "suggest parentheses around arithmetic in operand of %<|%>");
11715 /* Check cases like x|y==z */
11716 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11717 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11718 "suggest parentheses around comparison in operand of %<|%>");
11719 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11720 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11721 "suggest parentheses around comparison in operand of %<|%>");
11722 /* Check cases like !x | y */
11723 else if (code_left == TRUTH_NOT_EXPR
11724 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11725 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11726 "suggest parentheses around operand of "
11727 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11728 return;
11730 case BIT_XOR_EXPR:
11731 if (code_left == BIT_AND_EXPR
11732 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11733 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11734 "suggest parentheses around arithmetic in operand of %<^%>");
11735 else if (code_right == BIT_AND_EXPR
11736 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11737 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11738 "suggest parentheses around arithmetic in operand of %<^%>");
11739 /* Check cases like x^y==z */
11740 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11741 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11742 "suggest parentheses around comparison in operand of %<^%>");
11743 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11744 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11745 "suggest parentheses around comparison in operand of %<^%>");
11746 return;
11748 case BIT_AND_EXPR:
11749 if (code_left == PLUS_EXPR)
11750 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11751 "suggest parentheses around %<+%> in operand of %<&%>");
11752 else if (code_right == PLUS_EXPR)
11753 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11754 "suggest parentheses around %<+%> in operand of %<&%>");
11755 else if (code_left == MINUS_EXPR)
11756 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11757 "suggest parentheses around %<-%> in operand of %<&%>");
11758 else if (code_right == MINUS_EXPR)
11759 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11760 "suggest parentheses around %<-%> in operand of %<&%>");
11761 /* Check cases like x&y==z */
11762 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11763 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11764 "suggest parentheses around comparison in operand of %<&%>");
11765 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11766 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11767 "suggest parentheses around comparison in operand of %<&%>");
11768 /* Check cases like !x & y */
11769 else if (code_left == TRUTH_NOT_EXPR
11770 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11771 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11772 "suggest parentheses around operand of "
11773 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11774 return;
11776 case EQ_EXPR:
11777 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11778 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11779 "suggest parentheses around comparison in operand of %<==%>");
11780 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11781 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11782 "suggest parentheses around comparison in operand of %<==%>");
11783 return;
11784 case NE_EXPR:
11785 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11786 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11787 "suggest parentheses around comparison in operand of %<!=%>");
11788 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11789 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11790 "suggest parentheses around comparison in operand of %<!=%>");
11791 return;
11793 default:
11794 if (TREE_CODE_CLASS (code) == tcc_comparison)
11796 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11797 && code_left != NE_EXPR && code_left != EQ_EXPR
11798 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11799 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11800 "comparisons like %<X<=Y<=Z%> do not "
11801 "have their mathematical meaning");
11802 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11803 && code_right != NE_EXPR && code_right != EQ_EXPR
11804 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11805 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11806 "comparisons like %<X<=Y<=Z%> do not "
11807 "have their mathematical meaning");
11809 return;
11811 #undef NOT_A_BOOLEAN_EXPR_P
11814 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11816 void
11817 warn_for_unused_label (tree label)
11819 if (!TREE_USED (label))
11821 if (DECL_INITIAL (label))
11822 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11823 else
11824 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11828 /* Warn for division by zero according to the value of DIVISOR. LOC
11829 is the location of the division operator. */
11831 void
11832 warn_for_div_by_zero (location_t loc, tree divisor)
11834 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11835 about division by zero. Do not issue a warning if DIVISOR has a
11836 floating-point type, since we consider 0.0/0.0 a valid way of
11837 generating a NaN. */
11838 if (c_inhibit_evaluation_warnings == 0
11839 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11840 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11843 /* Subroutine of build_binary_op. Give warnings for comparisons
11844 between signed and unsigned quantities that may fail. Do the
11845 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11846 so that casts will be considered, but default promotions won't
11849 LOCATION is the location of the comparison operator.
11851 The arguments of this function map directly to local variables
11852 of build_binary_op. */
11854 void
11855 warn_for_sign_compare (location_t location,
11856 tree orig_op0, tree orig_op1,
11857 tree op0, tree op1,
11858 tree result_type, enum tree_code resultcode)
11860 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11861 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11862 int unsignedp0, unsignedp1;
11864 /* In C++, check for comparison of different enum types. */
11865 if (c_dialect_cxx()
11866 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11867 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11868 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11869 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11871 warning_at (location,
11872 OPT_Wsign_compare, "comparison between types %qT and %qT",
11873 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11876 /* Do not warn if the comparison is being done in a signed type,
11877 since the signed type will only be chosen if it can represent
11878 all the values of the unsigned type. */
11879 if (!TYPE_UNSIGNED (result_type))
11880 /* OK */;
11881 /* Do not warn if both operands are unsigned. */
11882 else if (op0_signed == op1_signed)
11883 /* OK */;
11884 else
11886 tree sop, uop, base_type;
11887 bool ovf;
11889 if (op0_signed)
11890 sop = orig_op0, uop = orig_op1;
11891 else
11892 sop = orig_op1, uop = orig_op0;
11894 STRIP_TYPE_NOPS (sop);
11895 STRIP_TYPE_NOPS (uop);
11896 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11897 ? TREE_TYPE (result_type) : result_type);
11899 /* Do not warn if the signed quantity is an unsuffixed integer
11900 literal (or some static constant expression involving such
11901 literals or a conditional expression involving such literals)
11902 and it is non-negative. */
11903 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11904 /* OK */;
11905 /* Do not warn if the comparison is an equality operation, the
11906 unsigned quantity is an integral constant, and it would fit
11907 in the result if the result were signed. */
11908 else if (TREE_CODE (uop) == INTEGER_CST
11909 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
11910 && int_fits_type_p (uop, c_common_signed_type (base_type)))
11911 /* OK */;
11912 /* In C, do not warn if the unsigned quantity is an enumeration
11913 constant and its maximum value would fit in the result if the
11914 result were signed. */
11915 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11916 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11917 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
11918 c_common_signed_type (base_type)))
11919 /* OK */;
11920 else
11921 warning_at (location,
11922 OPT_Wsign_compare,
11923 "comparison between signed and unsigned integer expressions");
11926 /* Warn if two unsigned values are being compared in a size larger
11927 than their original size, and one (and only one) is the result of
11928 a `~' operator. This comparison will always fail.
11930 Also warn if one operand is a constant, and the constant does not
11931 have all bits set that are set in the ~ operand when it is
11932 extended. */
11934 op0 = c_common_get_narrower (op0, &unsignedp0);
11935 op1 = c_common_get_narrower (op1, &unsignedp1);
11937 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11938 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11940 if (TREE_CODE (op0) == BIT_NOT_EXPR)
11941 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
11942 if (TREE_CODE (op1) == BIT_NOT_EXPR)
11943 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
11945 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
11947 tree primop;
11948 HOST_WIDE_INT constant, mask;
11949 int unsignedp;
11950 unsigned int bits;
11952 if (tree_fits_shwi_p (op0))
11954 primop = op1;
11955 unsignedp = unsignedp1;
11956 constant = tree_to_shwi (op0);
11958 else
11960 primop = op0;
11961 unsignedp = unsignedp0;
11962 constant = tree_to_shwi (op1);
11965 bits = TYPE_PRECISION (TREE_TYPE (primop));
11966 if (bits < TYPE_PRECISION (result_type)
11967 && bits < HOST_BITS_PER_LONG && unsignedp)
11969 mask = (~ (HOST_WIDE_INT) 0) << bits;
11970 if ((mask & constant) != mask)
11972 if (constant == 0)
11973 warning_at (location, OPT_Wsign_compare,
11974 "promoted ~unsigned is always non-zero");
11975 else
11976 warning_at (location, OPT_Wsign_compare,
11977 "comparison of promoted ~unsigned with constant");
11981 else if (unsignedp0 && unsignedp1
11982 && (TYPE_PRECISION (TREE_TYPE (op0))
11983 < TYPE_PRECISION (result_type))
11984 && (TYPE_PRECISION (TREE_TYPE (op1))
11985 < TYPE_PRECISION (result_type)))
11986 warning_at (location, OPT_Wsign_compare,
11987 "comparison of promoted ~unsigned with unsigned");
11991 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11992 type via c_common_type. If -Wdouble-promotion is in use, and the
11993 conditions for warning have been met, issue a warning. GMSGID is
11994 the warning message. It must have two %T specifiers for the type
11995 that was converted (generally "float") and the type to which it was
11996 converted (generally "double), respectively. LOC is the location
11997 to which the awrning should refer. */
11999 void
12000 do_warn_double_promotion (tree result_type, tree type1, tree type2,
12001 const char *gmsgid, location_t loc)
12003 tree source_type;
12005 if (!warn_double_promotion)
12006 return;
12007 /* If the conversion will not occur at run-time, there is no need to
12008 warn about it. */
12009 if (c_inhibit_evaluation_warnings)
12010 return;
12011 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12012 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12013 return;
12014 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12015 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12016 source_type = type1;
12017 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12018 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12019 source_type = type2;
12020 else
12021 return;
12022 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12025 /* Setup a TYPE_DECL node as a typedef representation.
12027 X is a TYPE_DECL for a typedef statement. Create a brand new
12028 ..._TYPE node (which will be just a variant of the existing
12029 ..._TYPE node with identical properties) and then install X
12030 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12032 The whole point here is to end up with a situation where each
12033 and every ..._TYPE node the compiler creates will be uniquely
12034 associated with AT MOST one node representing a typedef name.
12035 This way, even though the compiler substitutes corresponding
12036 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12037 early on, later parts of the compiler can always do the reverse
12038 translation and get back the corresponding typedef name. For
12039 example, given:
12041 typedef struct S MY_TYPE;
12042 MY_TYPE object;
12044 Later parts of the compiler might only know that `object' was of
12045 type `struct S' if it were not for code just below. With this
12046 code however, later parts of the compiler see something like:
12048 struct S' == struct S
12049 typedef struct S' MY_TYPE;
12050 struct S' object;
12052 And they can then deduce (from the node for type struct S') that
12053 the original object declaration was:
12055 MY_TYPE object;
12057 Being able to do this is important for proper support of protoize,
12058 and also for generating precise symbolic debugging information
12059 which takes full account of the programmer's (typedef) vocabulary.
12061 Obviously, we don't want to generate a duplicate ..._TYPE node if
12062 the TYPE_DECL node that we are now processing really represents a
12063 standard built-in type. */
12065 void
12066 set_underlying_type (tree x)
12068 if (x == error_mark_node)
12069 return;
12070 if (DECL_IS_BUILTIN (x))
12072 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12073 TYPE_NAME (TREE_TYPE (x)) = x;
12075 else if (TREE_TYPE (x) != error_mark_node
12076 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12078 tree tt = TREE_TYPE (x);
12079 DECL_ORIGINAL_TYPE (x) = tt;
12080 tt = build_variant_type_copy (tt);
12081 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12082 TYPE_NAME (tt) = x;
12083 TREE_USED (tt) = TREE_USED (x);
12084 TREE_TYPE (x) = tt;
12088 /* Record the types used by the current global variable declaration
12089 being parsed, so that we can decide later to emit their debug info.
12090 Those types are in types_used_by_cur_var_decl, and we are going to
12091 store them in the types_used_by_vars_hash hash table.
12092 DECL is the declaration of the global variable that has been parsed. */
12094 void
12095 record_types_used_by_current_var_decl (tree decl)
12097 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12099 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
12101 tree type = types_used_by_cur_var_decl->pop ();
12102 types_used_by_var_decl_insert (type, decl);
12106 /* If DECL is a typedef that is declared in the current function,
12107 record it for the purpose of -Wunused-local-typedefs. */
12109 void
12110 record_locally_defined_typedef (tree decl)
12112 struct c_language_function *l;
12114 if (!warn_unused_local_typedefs
12115 || cfun == NULL
12116 /* if this is not a locally defined typedef then we are not
12117 interested. */
12118 || !is_typedef_decl (decl)
12119 || !decl_function_context (decl))
12120 return;
12122 l = (struct c_language_function *) cfun->language;
12123 vec_safe_push (l->local_typedefs, decl);
12126 /* If T is a TYPE_DECL declared locally, mark it as used. */
12128 void
12129 maybe_record_typedef_use (tree t)
12131 if (!is_typedef_decl (t))
12132 return;
12134 TREE_USED (t) = true;
12137 /* Warn if there are some unused locally defined typedefs in the
12138 current function. */
12140 void
12141 maybe_warn_unused_local_typedefs (void)
12143 int i;
12144 tree decl;
12145 /* The number of times we have emitted -Wunused-local-typedefs
12146 warnings. If this is different from errorcount, that means some
12147 unrelated errors have been issued. In which case, we'll avoid
12148 emitting "unused-local-typedefs" warnings. */
12149 static int unused_local_typedefs_warn_count;
12150 struct c_language_function *l;
12152 if (cfun == NULL)
12153 return;
12155 if ((l = (struct c_language_function *) cfun->language) == NULL)
12156 return;
12158 if (warn_unused_local_typedefs
12159 && errorcount == unused_local_typedefs_warn_count)
12161 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
12162 if (!TREE_USED (decl))
12163 warning_at (DECL_SOURCE_LOCATION (decl),
12164 OPT_Wunused_local_typedefs,
12165 "typedef %qD locally defined but not used", decl);
12166 unused_local_typedefs_warn_count = errorcount;
12169 vec_free (l->local_typedefs);
12172 /* Warn about boolean expression compared with an integer value different
12173 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12174 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12175 are the operands of the comparison. The caller must ensure that
12176 either operand is a boolean expression. */
12178 void
12179 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12180 tree op1)
12182 if (TREE_CODE_CLASS (code) != tcc_comparison)
12183 return;
12185 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12186 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12187 if (!cst)
12188 return;
12190 if (!integer_zerop (cst) && !integer_onep (cst))
12192 int sign = (TREE_CODE (op0) == INTEGER_CST
12193 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
12194 if (code == EQ_EXPR
12195 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12196 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12197 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12198 "with boolean expression is always false", cst);
12199 else
12200 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12201 "with boolean expression is always true", cst);
12203 else if (integer_zerop (cst) || integer_onep (cst))
12205 /* If the non-constant operand isn't of a boolean type, we
12206 don't want to warn here. */
12207 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12208 /* Handle booleans promoted to integers. */
12209 if (CONVERT_EXPR_P (noncst)
12210 && TREE_TYPE (noncst) == integer_type_node
12211 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12212 /* Warn. */;
12213 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12214 && !truth_value_p (TREE_CODE (noncst)))
12215 return;
12216 /* Do some magic to get the right diagnostics. */
12217 bool flag = TREE_CODE (op0) == INTEGER_CST;
12218 flag = integer_zerop (cst) ? flag : !flag;
12219 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12220 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12221 "with boolean expression is always true", cst);
12222 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12223 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12224 "with boolean expression is always false", cst);
12228 /* The C and C++ parsers both use vectors to hold function arguments.
12229 For efficiency, we keep a cache of unused vectors. This is the
12230 cache. */
12232 typedef vec<tree, va_gc> *tree_gc_vec;
12233 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12235 /* Return a new vector from the cache. If the cache is empty,
12236 allocate a new vector. These vectors are GC'ed, so it is OK if the
12237 pointer is not released.. */
12239 vec<tree, va_gc> *
12240 make_tree_vector (void)
12242 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12243 return tree_vector_cache->pop ();
12244 else
12246 /* Passing 0 to vec::alloc returns NULL, and our callers require
12247 that we always return a non-NULL value. The vector code uses
12248 4 when growing a NULL vector, so we do too. */
12249 vec<tree, va_gc> *v;
12250 vec_alloc (v, 4);
12251 return v;
12255 /* Release a vector of trees back to the cache. */
12257 void
12258 release_tree_vector (vec<tree, va_gc> *vec)
12260 if (vec != NULL)
12262 vec->truncate (0);
12263 vec_safe_push (tree_vector_cache, vec);
12267 /* Get a new tree vector holding a single tree. */
12269 vec<tree, va_gc> *
12270 make_tree_vector_single (tree t)
12272 vec<tree, va_gc> *ret = make_tree_vector ();
12273 ret->quick_push (t);
12274 return ret;
12277 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12279 vec<tree, va_gc> *
12280 make_tree_vector_from_list (tree list)
12282 vec<tree, va_gc> *ret = make_tree_vector ();
12283 for (; list; list = TREE_CHAIN (list))
12284 vec_safe_push (ret, TREE_VALUE (list));
12285 return ret;
12288 /* Get a new tree vector which is a copy of an existing one. */
12290 vec<tree, va_gc> *
12291 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12293 vec<tree, va_gc> *ret;
12294 unsigned int ix;
12295 tree t;
12297 ret = make_tree_vector ();
12298 vec_safe_reserve (ret, vec_safe_length (orig));
12299 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12300 ret->quick_push (t);
12301 return ret;
12304 /* Return true if KEYWORD starts a type specifier. */
12306 bool
12307 keyword_begins_type_specifier (enum rid keyword)
12309 switch (keyword)
12311 case RID_AUTO_TYPE:
12312 case RID_INT:
12313 case RID_CHAR:
12314 case RID_FLOAT:
12315 case RID_DOUBLE:
12316 case RID_VOID:
12317 case RID_UNSIGNED:
12318 case RID_LONG:
12319 case RID_SHORT:
12320 case RID_SIGNED:
12321 case RID_DFLOAT32:
12322 case RID_DFLOAT64:
12323 case RID_DFLOAT128:
12324 case RID_FRACT:
12325 case RID_ACCUM:
12326 case RID_BOOL:
12327 case RID_WCHAR:
12328 case RID_CHAR16:
12329 case RID_CHAR32:
12330 case RID_SAT:
12331 case RID_COMPLEX:
12332 case RID_TYPEOF:
12333 case RID_STRUCT:
12334 case RID_CLASS:
12335 case RID_UNION:
12336 case RID_ENUM:
12337 return true;
12338 default:
12339 if (keyword >= RID_FIRST_INT_N
12340 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12341 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12342 return true;
12343 return false;
12347 /* Return true if KEYWORD names a type qualifier. */
12349 bool
12350 keyword_is_type_qualifier (enum rid keyword)
12352 switch (keyword)
12354 case RID_CONST:
12355 case RID_VOLATILE:
12356 case RID_RESTRICT:
12357 case RID_ATOMIC:
12358 return true;
12359 default:
12360 return false;
12364 /* Return true if KEYWORD names a storage class specifier.
12366 RID_TYPEDEF is not included in this list despite `typedef' being
12367 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12368 such for syntactic convenience only. */
12370 bool
12371 keyword_is_storage_class_specifier (enum rid keyword)
12373 switch (keyword)
12375 case RID_STATIC:
12376 case RID_EXTERN:
12377 case RID_REGISTER:
12378 case RID_AUTO:
12379 case RID_MUTABLE:
12380 case RID_THREAD:
12381 return true;
12382 default:
12383 return false;
12387 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12389 static bool
12390 keyword_is_function_specifier (enum rid keyword)
12392 switch (keyword)
12394 case RID_INLINE:
12395 case RID_NORETURN:
12396 case RID_VIRTUAL:
12397 case RID_EXPLICIT:
12398 return true;
12399 default:
12400 return false;
12404 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12405 declaration-specifier (C99 6.7). */
12407 bool
12408 keyword_is_decl_specifier (enum rid keyword)
12410 if (keyword_is_storage_class_specifier (keyword)
12411 || keyword_is_type_qualifier (keyword)
12412 || keyword_is_function_specifier (keyword))
12413 return true;
12415 switch (keyword)
12417 case RID_TYPEDEF:
12418 case RID_FRIEND:
12419 case RID_CONSTEXPR:
12420 return true;
12421 default:
12422 return false;
12426 /* Initialize language-specific-bits of tree_contains_struct. */
12428 void
12429 c_common_init_ts (void)
12431 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12432 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12433 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12436 /* Build a user-defined numeric literal out of an integer constant type VALUE
12437 with identifier SUFFIX. */
12439 tree
12440 build_userdef_literal (tree suffix_id, tree value,
12441 enum overflow_type overflow, tree num_string)
12443 tree literal = make_node (USERDEF_LITERAL);
12444 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12445 USERDEF_LITERAL_VALUE (literal) = value;
12446 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12447 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12448 return literal;
12451 /* For vector[index], convert the vector to a
12452 pointer of the underlying type. Return true if the resulting
12453 ARRAY_REF should not be an lvalue. */
12455 bool
12456 convert_vector_to_pointer_for_subscript (location_t loc,
12457 tree *vecp, tree index)
12459 bool ret = false;
12460 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12462 tree type = TREE_TYPE (*vecp);
12463 tree type1;
12465 ret = !lvalue_p (*vecp);
12466 if (TREE_CODE (index) == INTEGER_CST)
12467 if (!tree_fits_uhwi_p (index)
12468 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12469 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12471 if (ret)
12473 tree tmp = create_tmp_var_raw (type);
12474 DECL_SOURCE_LOCATION (tmp) = loc;
12475 *vecp = c_save_expr (*vecp);
12476 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12478 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12479 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12480 *vecp
12481 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12482 *vecp, NULL_TREE, NULL_TREE),
12483 non_const);
12485 else
12486 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12487 NULL_TREE, NULL_TREE);
12488 SET_EXPR_LOCATION (*vecp, loc);
12489 c_common_mark_addressable_vec (tmp);
12491 else
12492 c_common_mark_addressable_vec (*vecp);
12493 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
12494 type1 = build_pointer_type (TREE_TYPE (*vecp));
12495 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12496 if (!ref_all
12497 && !DECL_P (*vecp))
12499 /* If the original vector isn't declared may_alias and it
12500 isn't a bare vector look if the subscripting would
12501 alias the vector we subscript, and if not, force ref-all. */
12502 alias_set_type vecset = get_alias_set (*vecp);
12503 alias_set_type sset = get_alias_set (type);
12504 if (!alias_sets_must_conflict_p (sset, vecset)
12505 && !alias_set_subset_of (sset, vecset))
12506 ref_all = true;
12508 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
12509 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12510 *vecp = convert (type, *vecp);
12512 return ret;
12515 /* Determine which of the operands, if any, is a scalar that needs to be
12516 converted to a vector, for the range of operations. */
12517 enum stv_conv
12518 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12519 bool complain)
12521 tree type0 = TREE_TYPE (op0);
12522 tree type1 = TREE_TYPE (op1);
12523 bool integer_only_op = false;
12524 enum stv_conv ret = stv_firstarg;
12526 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12527 || TREE_CODE (type1) == VECTOR_TYPE);
12528 switch (code)
12530 /* Most GENERIC binary expressions require homogeneous arguments.
12531 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12532 argument that is a vector and a second one that is a scalar, so
12533 we never return stv_secondarg for them. */
12534 case RSHIFT_EXPR:
12535 case LSHIFT_EXPR:
12536 if (TREE_CODE (type0) == INTEGER_TYPE
12537 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12539 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12541 if (complain)
12542 error_at (loc, "conversion of scalar %qT to vector %qT "
12543 "involves truncation", type0, type1);
12544 return stv_error;
12546 else
12547 return stv_firstarg;
12549 break;
12551 case BIT_IOR_EXPR:
12552 case BIT_XOR_EXPR:
12553 case BIT_AND_EXPR:
12554 integer_only_op = true;
12555 /* ... fall through ... */
12557 case VEC_COND_EXPR:
12559 case PLUS_EXPR:
12560 case MINUS_EXPR:
12561 case MULT_EXPR:
12562 case TRUNC_DIV_EXPR:
12563 case CEIL_DIV_EXPR:
12564 case FLOOR_DIV_EXPR:
12565 case ROUND_DIV_EXPR:
12566 case EXACT_DIV_EXPR:
12567 case TRUNC_MOD_EXPR:
12568 case FLOOR_MOD_EXPR:
12569 case RDIV_EXPR:
12570 case EQ_EXPR:
12571 case NE_EXPR:
12572 case LE_EXPR:
12573 case GE_EXPR:
12574 case LT_EXPR:
12575 case GT_EXPR:
12576 /* What about UNLT_EXPR? */
12577 if (TREE_CODE (type0) == VECTOR_TYPE)
12579 tree tmp;
12580 ret = stv_secondarg;
12581 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12582 tmp = type0; type0 = type1; type1 = tmp;
12583 tmp = op0; op0 = op1; op1 = tmp;
12586 if (TREE_CODE (type0) == INTEGER_TYPE
12587 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12589 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12591 if (complain)
12592 error_at (loc, "conversion of scalar %qT to vector %qT "
12593 "involves truncation", type0, type1);
12594 return stv_error;
12596 return ret;
12598 else if (!integer_only_op
12599 /* Allow integer --> real conversion if safe. */
12600 && (TREE_CODE (type0) == REAL_TYPE
12601 || TREE_CODE (type0) == INTEGER_TYPE)
12602 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12604 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12606 if (complain)
12607 error_at (loc, "conversion of scalar %qT to vector %qT "
12608 "involves truncation", type0, type1);
12609 return stv_error;
12611 return ret;
12613 default:
12614 break;
12617 return stv_nothing;
12620 /* Return true iff ALIGN is an integral constant that is a fundamental
12621 alignment, as defined by [basic.align] in the c++-11
12622 specifications.
12624 That is:
12626 [A fundamental alignment is represented by an alignment less than or
12627 equal to the greatest alignment supported by the implementation
12628 in all contexts, which is equal to
12629 alignof(max_align_t)]. */
12631 bool
12632 cxx_fundamental_alignment_p (unsigned align)
12634 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12635 TYPE_ALIGN (long_double_type_node)));
12638 /* Return true if T is a pointer to a zero-sized aggregate. */
12640 bool
12641 pointer_to_zero_sized_aggr_p (tree t)
12643 if (!POINTER_TYPE_P (t))
12644 return false;
12645 t = TREE_TYPE (t);
12646 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12649 #include "gt-c-family-c-common.h"