Implement P0258R2 - helper for C++17
[official-gcc.git] / gcc / c-family / c-common.c
blobf518c20797f53923fa4a0b68e0d29a63f5277203
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 "target.h"
26 #include "function.h"
27 #include "tree.h"
28 #include "memmodel.h"
29 #include "c-common.h"
30 #include "gimple-expr.h"
31 #include "tm_p.h"
32 #include "stringpool.h"
33 #include "cgraph.h"
34 #include "diagnostic.h"
35 #include "intl.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "trans-mem.h"
41 #include "c-objc.h"
42 #include "common/common-target.h"
43 #include "langhooks.h"
44 #include "tree-inline.h"
45 #include "toplev.h"
46 #include "tree-iterator.h"
47 #include "opts.h"
48 #include "gimplify.h"
49 #include "substring-locations.h"
50 #include "spellcheck.h"
52 cpp_reader *parse_in; /* Declared in c-pragma.h. */
54 /* Mode used to build pointers (VOIDmode means ptr_mode). */
56 machine_mode c_default_pointer_mode = VOIDmode;
58 /* The following symbols are subsumed in the c_global_trees array, and
59 listed here individually for documentation purposes.
61 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
63 tree short_integer_type_node;
64 tree long_integer_type_node;
65 tree long_long_integer_type_node;
67 tree short_unsigned_type_node;
68 tree long_unsigned_type_node;
69 tree long_long_unsigned_type_node;
71 tree truthvalue_type_node;
72 tree truthvalue_false_node;
73 tree truthvalue_true_node;
75 tree ptrdiff_type_node;
77 tree unsigned_char_type_node;
78 tree signed_char_type_node;
79 tree wchar_type_node;
81 tree char16_type_node;
82 tree char32_type_node;
84 tree float_type_node;
85 tree double_type_node;
86 tree long_double_type_node;
88 tree complex_integer_type_node;
89 tree complex_float_type_node;
90 tree complex_double_type_node;
91 tree complex_long_double_type_node;
93 tree dfloat32_type_node;
94 tree dfloat64_type_node;
95 tree_dfloat128_type_node;
97 tree intQI_type_node;
98 tree intHI_type_node;
99 tree intSI_type_node;
100 tree intDI_type_node;
101 tree intTI_type_node;
103 tree unsigned_intQI_type_node;
104 tree unsigned_intHI_type_node;
105 tree unsigned_intSI_type_node;
106 tree unsigned_intDI_type_node;
107 tree unsigned_intTI_type_node;
109 tree widest_integer_literal_type_node;
110 tree widest_unsigned_literal_type_node;
112 Nodes for types `void *' and `const void *'.
114 tree ptr_type_node, const_ptr_type_node;
116 Nodes for types `char *' and `const char *'.
118 tree string_type_node, const_string_type_node;
120 Type `char[SOMENUMBER]'.
121 Used when an array of char is needed and the size is irrelevant.
123 tree char_array_type_node;
125 Type `wchar_t[SOMENUMBER]' or something like it.
126 Used when a wide string literal is created.
128 tree wchar_array_type_node;
130 Type `char16_t[SOMENUMBER]' or something like it.
131 Used when a UTF-16 string literal is created.
133 tree char16_array_type_node;
135 Type `char32_t[SOMENUMBER]' or something like it.
136 Used when a UTF-32 string literal is created.
138 tree char32_array_type_node;
140 Type `int ()' -- used for implicit declaration of functions.
142 tree default_function_type;
144 A VOID_TYPE node, packaged in a TREE_LIST.
146 tree void_list_node;
148 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
149 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
150 VAR_DECLS, but C++ does.)
152 tree function_name_decl_node;
153 tree pretty_function_name_decl_node;
154 tree c99_function_name_decl_node;
156 Stack of nested function name VAR_DECLs.
158 tree saved_function_name_decls;
162 tree c_global_trees[CTI_MAX];
164 /* Switches common to the C front ends. */
166 /* Nonzero means don't output line number information. */
168 char flag_no_line_commands;
170 /* Nonzero causes -E output not to be done, but directives such as
171 #define that have side effects are still obeyed. */
173 char flag_no_output;
175 /* Nonzero means dump macros in some fashion. */
177 char flag_dump_macros;
179 /* Nonzero means pass #include lines through to the output. */
181 char flag_dump_includes;
183 /* Nonzero means process PCH files while preprocessing. */
185 bool flag_pch_preprocess;
187 /* The file name to which we should write a precompiled header, or
188 NULL if no header will be written in this compile. */
190 const char *pch_file;
192 /* Nonzero if an ISO standard was selected. It rejects macros in the
193 user's namespace. */
194 int flag_iso;
196 /* C/ObjC language option variables. */
199 /* Nonzero means allow type mismatches in conditional expressions;
200 just make their values `void'. */
202 int flag_cond_mismatch;
204 /* Nonzero means enable C89 Amendment 1 features. */
206 int flag_isoc94;
208 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
210 int flag_isoc99;
212 /* Nonzero means use the ISO C11 dialect of C. */
214 int flag_isoc11;
216 /* Nonzero means that we have builtin functions, and main is an int. */
218 int flag_hosted = 1;
221 /* ObjC language option variables. */
224 /* Tells the compiler that this is a special run. Do not perform any
225 compiling, instead we are to test some platform dependent features
226 and output a C header file with appropriate definitions. */
228 int print_struct_values;
230 /* Tells the compiler what is the constant string class for ObjC. */
232 const char *constant_string_class_name;
235 /* C++ language option variables. */
237 /* The reference version of the ABI for -Wabi. */
239 int warn_abi_version = -1;
241 /* Nonzero means generate separate instantiation control files and
242 juggle them at link time. */
244 int flag_use_repository;
246 /* The C++ dialect being used. Default set in c_common_post_options. */
248 enum cxx_dialect cxx_dialect = cxx_unset;
250 /* Maximum template instantiation depth. This limit exists to limit the
251 time it takes to notice excessively recursive template instantiations.
253 The default is lower than the 1024 recommended by the C++0x standard
254 because G++ runs out of stack before 1024 with highly recursive template
255 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
257 int max_tinst_depth = 900;
259 /* The elements of `ridpointers' are identifier nodes for the reserved
260 type names and storage classes. It is indexed by a RID_... value. */
261 tree *ridpointers;
263 tree (*make_fname_decl) (location_t, tree, int);
265 /* Nonzero means don't warn about problems that occur when the code is
266 executed. */
267 int c_inhibit_evaluation_warnings;
269 /* Whether we are building a boolean conversion inside
270 convert_for_assignment, or some other late binary operation. If
271 build_binary_op is called for C (from code shared by C and C++) in
272 this case, then the operands have already been folded and the
273 result will not be folded again, so C_MAYBE_CONST_EXPR should not
274 be generated. */
275 bool in_late_binary_op;
277 /* Whether lexing has been completed, so subsequent preprocessor
278 errors should use the compiler's input_location. */
279 bool done_lexing = false;
281 /* Information about how a function name is generated. */
282 struct fname_var_t
284 tree *const decl; /* pointer to the VAR_DECL. */
285 const unsigned rid; /* RID number for the identifier. */
286 const int pretty; /* How pretty is it? */
289 /* The three ways of getting then name of the current function. */
291 const struct fname_var_t fname_vars[] =
293 /* C99 compliant __func__, must be first. */
294 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
295 /* GCC __FUNCTION__ compliant. */
296 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
297 /* GCC __PRETTY_FUNCTION__ compliant. */
298 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
299 {NULL, 0, 0},
302 /* Global visibility options. */
303 struct visibility_flags visibility_options;
305 static tree check_case_value (location_t, tree);
306 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
307 bool *);
309 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
312 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
313 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
314 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
315 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
316 int, bool *);
317 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
318 int, bool *);
319 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
320 bool *);
321 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
325 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
326 static tree handle_always_inline_attribute (tree *, tree, tree, int,
327 bool *);
328 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
332 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
333 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
334 bool *);
335 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
336 bool *);
337 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
338 static tree handle_transparent_union_attribute (tree *, tree, tree,
339 int, bool *);
340 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
341 int, bool *);
342 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
346 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
347 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
348 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
349 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
350 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
351 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
352 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
353 static tree handle_visibility_attribute (tree *, tree, tree, int,
354 bool *);
355 static tree handle_tls_model_attribute (tree *, tree, tree, int,
356 bool *);
357 static tree handle_no_instrument_function_attribute (tree *, tree,
358 tree, int, bool *);
359 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
360 tree, int, bool *);
361 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
362 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
363 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
364 bool *);
365 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
368 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
369 static tree handle_deprecated_attribute (tree *, tree, tree, int,
370 bool *);
371 static tree handle_vector_size_attribute (tree *, tree, tree, int,
372 bool *);
373 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
375 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
376 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
377 bool *);
378 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
384 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
386 static tree ignore_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
390 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
391 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
392 bool *);
393 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
394 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
395 bool *);
396 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
397 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
398 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
399 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
400 static tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *);
402 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
403 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
404 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
405 static int resort_field_decl_cmp (const void *, const void *);
407 /* Reserved words. The third field is a mask: keywords are disabled
408 if they match the mask.
410 Masks for languages:
411 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
412 C --std=c99: D_CXXONLY | D_OBJC
413 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
414 C++ --std=c++98: D_CONLY | D_CXX11 | D_OBJC
415 C++ --std=c++11: D_CONLY | D_OBJC
416 ObjC++ is like C++ except that D_OBJC is not set
418 If -fno-asm is used, D_ASM is added to the mask. If
419 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
420 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
421 In C with -Wc++-compat, we warn if D_CXXWARN is set.
423 Note the complication of the D_CXX_OBJC keywords. These are
424 reserved words such as 'class'. In C++, 'class' is a reserved
425 word. In Objective-C++ it is too. In Objective-C, it is a
426 reserved word too, but only if it follows an '@' sign.
428 const struct c_common_resword c_common_reswords[] =
430 { "_Alignas", RID_ALIGNAS, D_CONLY },
431 { "_Alignof", RID_ALIGNOF, D_CONLY },
432 { "_Atomic", RID_ATOMIC, D_CONLY },
433 { "_Bool", RID_BOOL, D_CONLY },
434 { "_Complex", RID_COMPLEX, 0 },
435 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
436 { "_Cilk_sync", RID_CILK_SYNC, 0 },
437 { "_Cilk_for", RID_CILK_FOR, 0 },
438 { "_Imaginary", RID_IMAGINARY, D_CONLY },
439 { "_Float16", RID_FLOAT16, D_CONLY },
440 { "_Float32", RID_FLOAT32, D_CONLY },
441 { "_Float64", RID_FLOAT64, D_CONLY },
442 { "_Float128", RID_FLOAT128, D_CONLY },
443 { "_Float32x", RID_FLOAT32X, D_CONLY },
444 { "_Float64x", RID_FLOAT64X, D_CONLY },
445 { "_Float128x", RID_FLOAT128X, 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_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
490 D_CXXONLY },
491 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
492 { "__imag", RID_IMAGPART, 0 },
493 { "__imag__", RID_IMAGPART, 0 },
494 { "__inline", RID_INLINE, 0 },
495 { "__inline__", RID_INLINE, 0 },
496 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
497 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
498 { "__is_class", RID_IS_CLASS, D_CXXONLY },
499 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
500 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
501 { "__is_final", RID_IS_FINAL, D_CXXONLY },
502 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
503 { "__is_pod", RID_IS_POD, D_CXXONLY },
504 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
505 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
506 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
507 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
508 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
509 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
510 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
511 { "__is_union", RID_IS_UNION, D_CXXONLY },
512 { "__label__", RID_LABEL, 0 },
513 { "__null", RID_NULL, 0 },
514 { "__real", RID_REALPART, 0 },
515 { "__real__", RID_REALPART, 0 },
516 { "__restrict", RID_RESTRICT, 0 },
517 { "__restrict__", RID_RESTRICT, 0 },
518 { "__signed", RID_SIGNED, 0 },
519 { "__signed__", RID_SIGNED, 0 },
520 { "__thread", RID_THREAD, 0 },
521 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
522 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
523 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
524 { "__typeof", RID_TYPEOF, 0 },
525 { "__typeof__", RID_TYPEOF, 0 },
526 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
527 { "__volatile", RID_VOLATILE, 0 },
528 { "__volatile__", RID_VOLATILE, 0 },
529 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
530 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
531 { "asm", RID_ASM, D_ASM },
532 { "auto", RID_AUTO, 0 },
533 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
534 { "break", RID_BREAK, 0 },
535 { "case", RID_CASE, 0 },
536 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
537 { "char", RID_CHAR, 0 },
538 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
539 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
540 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
541 { "const", RID_CONST, 0 },
542 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
543 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
544 { "continue", RID_CONTINUE, 0 },
545 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
546 { "default", RID_DEFAULT, 0 },
547 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
548 { "do", RID_DO, 0 },
549 { "double", RID_DOUBLE, 0 },
550 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
551 { "else", RID_ELSE, 0 },
552 { "enum", RID_ENUM, 0 },
553 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
554 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
555 { "extern", RID_EXTERN, 0 },
556 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
557 { "float", RID_FLOAT, 0 },
558 { "for", RID_FOR, 0 },
559 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
560 { "goto", RID_GOTO, 0 },
561 { "if", RID_IF, 0 },
562 { "inline", RID_INLINE, D_EXT89 },
563 { "int", RID_INT, 0 },
564 { "long", RID_LONG, 0 },
565 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
566 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
567 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
568 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
569 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
570 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
571 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
572 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
573 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
574 { "register", RID_REGISTER, 0 },
575 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
576 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
577 { "return", RID_RETURN, 0 },
578 { "short", RID_SHORT, 0 },
579 { "signed", RID_SIGNED, 0 },
580 { "sizeof", RID_SIZEOF, 0 },
581 { "static", RID_STATIC, 0 },
582 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
583 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
584 { "struct", RID_STRUCT, 0 },
585 { "switch", RID_SWITCH, 0 },
586 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
587 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
588 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
589 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
590 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
591 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
592 { "typedef", RID_TYPEDEF, 0 },
593 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
594 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
595 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
596 { "union", RID_UNION, 0 },
597 { "unsigned", RID_UNSIGNED, 0 },
598 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
599 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
600 { "void", RID_VOID, 0 },
601 { "volatile", RID_VOLATILE, 0 },
602 { "wchar_t", RID_WCHAR, D_CXXONLY },
603 { "while", RID_WHILE, 0 },
605 /* C++ transactional memory. */
606 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
607 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
608 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
609 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
611 /* Concepts-related keywords */
612 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
613 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
615 /* These Objective-C keywords are recognized only immediately after
616 an '@'. */
617 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
618 { "defs", RID_AT_DEFS, D_OBJC },
619 { "encode", RID_AT_ENCODE, D_OBJC },
620 { "end", RID_AT_END, D_OBJC },
621 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
622 { "interface", RID_AT_INTERFACE, D_OBJC },
623 { "protocol", RID_AT_PROTOCOL, D_OBJC },
624 { "selector", RID_AT_SELECTOR, D_OBJC },
625 { "finally", RID_AT_FINALLY, D_OBJC },
626 { "optional", RID_AT_OPTIONAL, D_OBJC },
627 { "required", RID_AT_REQUIRED, D_OBJC },
628 { "property", RID_AT_PROPERTY, D_OBJC },
629 { "package", RID_AT_PACKAGE, D_OBJC },
630 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
631 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
632 /* These are recognized only in protocol-qualifier context
633 (see above) */
634 { "bycopy", RID_BYCOPY, D_OBJC },
635 { "byref", RID_BYREF, D_OBJC },
636 { "in", RID_IN, D_OBJC },
637 { "inout", RID_INOUT, D_OBJC },
638 { "oneway", RID_ONEWAY, D_OBJC },
639 { "out", RID_OUT, D_OBJC },
640 /* These are recognized inside a property attribute list */
641 { "assign", RID_ASSIGN, D_OBJC },
642 { "copy", RID_COPY, D_OBJC },
643 { "getter", RID_GETTER, D_OBJC },
644 { "nonatomic", RID_NONATOMIC, D_OBJC },
645 { "readonly", RID_READONLY, D_OBJC },
646 { "readwrite", RID_READWRITE, D_OBJC },
647 { "retain", RID_RETAIN, D_OBJC },
648 { "setter", RID_SETTER, D_OBJC },
651 const unsigned int num_c_common_reswords =
652 sizeof c_common_reswords / sizeof (struct c_common_resword);
654 /* Table of machine-independent attributes common to all C-like languages.
656 All attributes referencing arguments should be additionally processed
657 in chkp_copy_function_type_adding_bounds for correct instrumentation
658 by Pointer Bounds Checker.
659 Current list of processed common attributes: nonnull. */
660 const struct attribute_spec c_common_attribute_table[] =
662 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
663 affects_type_identity } */
664 { "packed", 0, 0, false, false, false,
665 handle_packed_attribute , false},
666 { "nocommon", 0, 0, true, false, false,
667 handle_nocommon_attribute, false},
668 { "common", 0, 0, true, false, false,
669 handle_common_attribute, false },
670 /* FIXME: logically, noreturn attributes should be listed as
671 "false, true, true" and apply to function types. But implementing this
672 would require all the places in the compiler that use TREE_THIS_VOLATILE
673 on a decl to identify non-returning functions to be located and fixed
674 to check the function type instead. */
675 { "noreturn", 0, 0, true, false, false,
676 handle_noreturn_attribute, false },
677 { "volatile", 0, 0, true, false, false,
678 handle_noreturn_attribute, false },
679 { "stack_protect", 0, 0, true, false, false,
680 handle_stack_protect_attribute, false },
681 { "noinline", 0, 0, true, false, false,
682 handle_noinline_attribute, false },
683 { "noclone", 0, 0, true, false, false,
684 handle_noclone_attribute, false },
685 { "no_icf", 0, 0, true, false, false,
686 handle_noicf_attribute, false },
687 { "leaf", 0, 0, true, false, false,
688 handle_leaf_attribute, false },
689 { "always_inline", 0, 0, true, false, false,
690 handle_always_inline_attribute, false },
691 { "gnu_inline", 0, 0, true, false, false,
692 handle_gnu_inline_attribute, false },
693 { "artificial", 0, 0, true, false, false,
694 handle_artificial_attribute, false },
695 { "flatten", 0, 0, true, false, false,
696 handle_flatten_attribute, false },
697 { "used", 0, 0, true, false, false,
698 handle_used_attribute, false },
699 { "unused", 0, 0, false, false, false,
700 handle_unused_attribute, false },
701 { "externally_visible", 0, 0, true, false, false,
702 handle_externally_visible_attribute, false },
703 { "no_reorder", 0, 0, true, false, false,
704 handle_no_reorder_attribute, false },
705 /* The same comments as for noreturn attributes apply to const ones. */
706 { "const", 0, 0, true, false, false,
707 handle_const_attribute, false },
708 { "scalar_storage_order", 1, 1, false, false, false,
709 handle_scalar_storage_order_attribute, false },
710 { "transparent_union", 0, 0, false, false, false,
711 handle_transparent_union_attribute, false },
712 { "constructor", 0, 1, true, false, false,
713 handle_constructor_attribute, false },
714 { "destructor", 0, 1, true, false, false,
715 handle_destructor_attribute, false },
716 { "mode", 1, 1, false, true, false,
717 handle_mode_attribute, false },
718 { "section", 1, 1, true, false, false,
719 handle_section_attribute, false },
720 { "aligned", 0, 1, false, false, false,
721 handle_aligned_attribute, false },
722 { "weak", 0, 0, true, false, false,
723 handle_weak_attribute, false },
724 { "noplt", 0, 0, true, false, false,
725 handle_noplt_attribute, false },
726 { "ifunc", 1, 1, true, false, false,
727 handle_ifunc_attribute, false },
728 { "alias", 1, 1, true, false, false,
729 handle_alias_attribute, false },
730 { "weakref", 0, 1, true, false, false,
731 handle_weakref_attribute, false },
732 { "no_instrument_function", 0, 0, true, false, false,
733 handle_no_instrument_function_attribute,
734 false },
735 { "no_profile_instrument_function", 0, 0, true, false, false,
736 handle_no_profile_instrument_function_attribute,
737 false },
738 { "malloc", 0, 0, true, false, false,
739 handle_malloc_attribute, false },
740 { "returns_twice", 0, 0, true, false, false,
741 handle_returns_twice_attribute, false },
742 { "no_stack_limit", 0, 0, true, false, false,
743 handle_no_limit_stack_attribute, false },
744 { "pure", 0, 0, true, false, false,
745 handle_pure_attribute, false },
746 { "transaction_callable", 0, 0, false, true, false,
747 handle_tm_attribute, false },
748 { "transaction_unsafe", 0, 0, false, true, false,
749 handle_tm_attribute, true },
750 { "transaction_safe", 0, 0, false, true, false,
751 handle_tm_attribute, true },
752 { "transaction_safe_dynamic", 0, 0, true, false, false,
753 handle_tm_attribute, false },
754 { "transaction_may_cancel_outer", 0, 0, false, true, false,
755 handle_tm_attribute, false },
756 /* ??? These two attributes didn't make the transition from the
757 Intel language document to the multi-vendor language document. */
758 { "transaction_pure", 0, 0, false, true, false,
759 handle_tm_attribute, false },
760 { "transaction_wrap", 1, 1, true, false, false,
761 handle_tm_wrap_attribute, false },
762 /* For internal use (marking of builtins) only. The name contains space
763 to prevent its usage in source code. */
764 { "no vops", 0, 0, true, false, false,
765 handle_novops_attribute, false },
766 { "deprecated", 0, 1, false, false, false,
767 handle_deprecated_attribute, false },
768 { "vector_size", 1, 1, false, true, false,
769 handle_vector_size_attribute, true },
770 { "visibility", 1, 1, false, false, false,
771 handle_visibility_attribute, false },
772 { "tls_model", 1, 1, true, false, false,
773 handle_tls_model_attribute, false },
774 { "nonnull", 0, -1, false, true, true,
775 handle_nonnull_attribute, false },
776 { "nothrow", 0, 0, true, false, false,
777 handle_nothrow_attribute, false },
778 { "may_alias", 0, 0, false, true, false, NULL, false },
779 { "cleanup", 1, 1, true, false, false,
780 handle_cleanup_attribute, false },
781 { "warn_unused_result", 0, 0, false, true, true,
782 handle_warn_unused_result_attribute, false },
783 { "sentinel", 0, 1, false, true, true,
784 handle_sentinel_attribute, false },
785 /* For internal use (marking of builtins) only. The name contains space
786 to prevent its usage in source code. */
787 { "type generic", 0, 0, false, true, true,
788 handle_type_generic_attribute, false },
789 { "alloc_size", 1, 2, false, true, true,
790 handle_alloc_size_attribute, false },
791 { "cold", 0, 0, true, false, false,
792 handle_cold_attribute, false },
793 { "hot", 0, 0, true, false, false,
794 handle_hot_attribute, false },
795 { "no_address_safety_analysis",
796 0, 0, true, false, false,
797 handle_no_address_safety_analysis_attribute,
798 false },
799 { "no_sanitize_address", 0, 0, true, false, false,
800 handle_no_sanitize_address_attribute,
801 false },
802 { "no_sanitize_thread", 0, 0, true, false, false,
803 handle_no_sanitize_address_attribute,
804 false },
805 { "no_sanitize_undefined", 0, 0, true, false, false,
806 handle_no_sanitize_undefined_attribute,
807 false },
808 { "warning", 1, 1, true, false, false,
809 handle_error_attribute, false },
810 { "error", 1, 1, true, false, false,
811 handle_error_attribute, false },
812 { "target", 1, -1, true, false, false,
813 handle_target_attribute, false },
814 { "target_clones", 1, -1, true, false, false,
815 handle_target_clones_attribute, false },
816 { "optimize", 1, -1, true, false, false,
817 handle_optimize_attribute, false },
818 /* For internal use only. The leading '*' both prevents its usage in
819 source code and signals that it may be overridden by machine tables. */
820 { "*tm regparm", 0, 0, false, true, true,
821 ignore_attribute, false },
822 { "no_split_stack", 0, 0, true, false, false,
823 handle_no_split_stack_attribute, false },
824 /* For internal use (marking of builtins and runtime functions) only.
825 The name contains space to prevent its usage in source code. */
826 { "fn spec", 1, 1, false, true, true,
827 handle_fnspec_attribute, false },
828 { "warn_unused", 0, 0, false, false, false,
829 handle_warn_unused_attribute, false },
830 { "returns_nonnull", 0, 0, false, true, true,
831 handle_returns_nonnull_attribute, false },
832 { "omp declare simd", 0, -1, true, false, false,
833 handle_omp_declare_simd_attribute, false },
834 { "cilk simd function", 0, -1, true, false, false,
835 handle_omp_declare_simd_attribute, false },
836 { "simd", 0, 1, true, false, false,
837 handle_simd_attribute, false },
838 { "omp declare target", 0, 0, true, false, false,
839 handle_omp_declare_target_attribute, false },
840 { "omp declare target link", 0, 0, true, false, false,
841 handle_omp_declare_target_attribute, false },
842 { "alloc_align", 1, 1, false, true, true,
843 handle_alloc_align_attribute, false },
844 { "assume_aligned", 1, 2, false, true, true,
845 handle_assume_aligned_attribute, false },
846 { "designated_init", 0, 0, false, true, false,
847 handle_designated_init_attribute, false },
848 { "bnd_variable_size", 0, 0, true, false, false,
849 handle_bnd_variable_size_attribute, false },
850 { "bnd_legacy", 0, 0, true, false, false,
851 handle_bnd_legacy, false },
852 { "bnd_instrument", 0, 0, true, false, false,
853 handle_bnd_instrument, false },
854 { "fallthrough", 0, 0, false, false, false,
855 handle_fallthrough_attribute, false },
856 { NULL, 0, 0, false, false, false, NULL, false }
859 /* Give the specifications for the format attributes, used by C and all
860 descendants.
862 All attributes referencing arguments should be additionally processed
863 in chkp_copy_function_type_adding_bounds for correct instrumentation
864 by Pointer Bounds Checker.
865 Current list of processed format attributes: format, format_arg. */
866 const struct attribute_spec c_common_format_attribute_table[] =
868 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
869 affects_type_identity } */
870 { "format", 3, 3, false, true, true,
871 handle_format_attribute, false },
872 { "format_arg", 1, 1, false, true, true,
873 handle_format_arg_attribute, false },
874 { NULL, 0, 0, false, false, false, NULL, false }
877 /* Return identifier for address space AS. */
879 const char *
880 c_addr_space_name (addr_space_t as)
882 int rid = RID_FIRST_ADDR_SPACE + as;
883 gcc_assert (ridpointers [rid]);
884 return IDENTIFIER_POINTER (ridpointers [rid]);
887 /* Push current bindings for the function name VAR_DECLS. */
889 void
890 start_fname_decls (void)
892 unsigned ix;
893 tree saved = NULL_TREE;
895 for (ix = 0; fname_vars[ix].decl; ix++)
897 tree decl = *fname_vars[ix].decl;
899 if (decl)
901 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
902 saved);
903 *fname_vars[ix].decl = NULL_TREE;
906 if (saved || saved_function_name_decls)
907 /* Normally they'll have been NULL, so only push if we've got a
908 stack, or they are non-NULL. */
909 saved_function_name_decls = tree_cons (saved, NULL_TREE,
910 saved_function_name_decls);
913 /* Finish up the current bindings, adding them into the current function's
914 statement tree. This must be done _before_ finish_stmt_tree is called.
915 If there is no current function, we must be at file scope and no statements
916 are involved. Pop the previous bindings. */
918 void
919 finish_fname_decls (void)
921 unsigned ix;
922 tree stmts = NULL_TREE;
923 tree stack = saved_function_name_decls;
925 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
926 append_to_statement_list (TREE_VALUE (stack), &stmts);
928 if (stmts)
930 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
932 if (TREE_CODE (*bodyp) == BIND_EXPR)
933 bodyp = &BIND_EXPR_BODY (*bodyp);
935 append_to_statement_list_force (*bodyp, &stmts);
936 *bodyp = stmts;
939 for (ix = 0; fname_vars[ix].decl; ix++)
940 *fname_vars[ix].decl = NULL_TREE;
942 if (stack)
944 /* We had saved values, restore them. */
945 tree saved;
947 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
949 tree decl = TREE_PURPOSE (saved);
950 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
952 *fname_vars[ix].decl = decl;
954 stack = TREE_CHAIN (stack);
956 saved_function_name_decls = stack;
959 /* Return the text name of the current function, suitably prettified
960 by PRETTY_P. Return string must be freed by caller. */
962 const char *
963 fname_as_string (int pretty_p)
965 const char *name = "top level";
966 char *namep;
967 int vrb = 2, len;
968 cpp_string cstr = { 0, 0 }, strname;
970 if (!pretty_p)
972 name = "";
973 vrb = 0;
976 if (current_function_decl)
977 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
979 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
981 namep = XNEWVEC (char, len);
982 snprintf (namep, len, "\"%s\"", name);
983 strname.text = (unsigned char *) namep;
984 strname.len = len - 1;
986 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
988 XDELETEVEC (namep);
989 return (const char *) cstr.text;
992 return namep;
995 /* Return the VAR_DECL for a const char array naming the current
996 function. If the VAR_DECL has not yet been created, create it
997 now. RID indicates how it should be formatted and IDENTIFIER_NODE
998 ID is its name (unfortunately C and C++ hold the RID values of
999 keywords in different places, so we can't derive RID from ID in
1000 this language independent code. LOC is the location of the
1001 function. */
1003 tree
1004 fname_decl (location_t loc, unsigned int rid, tree id)
1006 unsigned ix;
1007 tree decl = NULL_TREE;
1009 for (ix = 0; fname_vars[ix].decl; ix++)
1010 if (fname_vars[ix].rid == rid)
1011 break;
1013 decl = *fname_vars[ix].decl;
1014 if (!decl)
1016 /* If a tree is built here, it would normally have the lineno of
1017 the current statement. Later this tree will be moved to the
1018 beginning of the function and this line number will be wrong.
1019 To avoid this problem set the lineno to 0 here; that prevents
1020 it from appearing in the RTL. */
1021 tree stmts;
1022 location_t saved_location = input_location;
1023 input_location = UNKNOWN_LOCATION;
1025 stmts = push_stmt_list ();
1026 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1027 stmts = pop_stmt_list (stmts);
1028 if (!IS_EMPTY_STMT (stmts))
1029 saved_function_name_decls
1030 = tree_cons (decl, stmts, saved_function_name_decls);
1031 *fname_vars[ix].decl = decl;
1032 input_location = saved_location;
1034 if (!ix && !current_function_decl)
1035 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1037 return decl;
1040 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1042 tree
1043 fix_string_type (tree value)
1045 int length = TREE_STRING_LENGTH (value);
1046 int nchars;
1047 tree e_type, i_type, a_type;
1049 /* Compute the number of elements, for the array type. */
1050 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1052 nchars = length;
1053 e_type = char_type_node;
1055 else if (TREE_TYPE (value) == char16_array_type_node)
1057 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1058 e_type = char16_type_node;
1060 else if (TREE_TYPE (value) == char32_array_type_node)
1062 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1063 e_type = char32_type_node;
1065 else
1067 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1068 e_type = wchar_type_node;
1071 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1072 limit in C++98 Annex B is very large (65536) and is not normative,
1073 so we do not diagnose it (warn_overlength_strings is forced off
1074 in c_common_post_options). */
1075 if (warn_overlength_strings)
1077 const int nchars_max = flag_isoc99 ? 4095 : 509;
1078 const int relevant_std = flag_isoc99 ? 99 : 90;
1079 if (nchars - 1 > nchars_max)
1080 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1081 separate the %d from the 'C'. 'ISO' should not be
1082 translated, but it may be moved after 'C%d' in languages
1083 where modifiers follow nouns. */
1084 pedwarn (input_location, OPT_Woverlength_strings,
1085 "string length %qd is greater than the length %qd "
1086 "ISO C%d compilers are required to support",
1087 nchars - 1, nchars_max, relevant_std);
1090 /* Create the array type for the string constant. The ISO C++
1091 standard says that a string literal has type `const char[N]' or
1092 `const wchar_t[N]'. We use the same logic when invoked as a C
1093 front-end with -Wwrite-strings.
1094 ??? We should change the type of an expression depending on the
1095 state of a warning flag. We should just be warning -- see how
1096 this is handled in the C++ front-end for the deprecated implicit
1097 conversion from string literals to `char*' or `wchar_t*'.
1099 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1100 array type being the unqualified version of that type.
1101 Therefore, if we are constructing an array of const char, we must
1102 construct the matching unqualified array type first. The C front
1103 end does not require this, but it does no harm, so we do it
1104 unconditionally. */
1105 i_type = build_index_type (size_int (nchars - 1));
1106 a_type = build_array_type (e_type, i_type);
1107 if (c_dialect_cxx() || warn_write_strings)
1108 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1110 TREE_TYPE (value) = a_type;
1111 TREE_CONSTANT (value) = 1;
1112 TREE_READONLY (value) = 1;
1113 TREE_STATIC (value) = 1;
1114 return value;
1117 /* Given a string of type STRING_TYPE, determine what kind of string
1118 token would give an equivalent execution encoding: CPP_STRING,
1119 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
1120 This may not be exactly the string token type that initially created
1121 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
1122 string type at this point.
1124 This effectively reverses part of the logic in lex_string and
1125 fix_string_type. */
1127 static enum cpp_ttype
1128 get_cpp_ttype_from_string_type (tree string_type)
1130 gcc_assert (string_type);
1131 if (TREE_CODE (string_type) == POINTER_TYPE)
1132 string_type = TREE_TYPE (string_type);
1134 if (TREE_CODE (string_type) != ARRAY_TYPE)
1135 return CPP_OTHER;
1137 tree element_type = TREE_TYPE (string_type);
1138 if (TREE_CODE (element_type) != INTEGER_TYPE)
1139 return CPP_OTHER;
1141 int bits_per_character = TYPE_PRECISION (element_type);
1142 switch (bits_per_character)
1144 case 8:
1145 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
1146 case 16:
1147 return CPP_STRING16;
1148 case 32:
1149 return CPP_STRING32;
1152 return CPP_OTHER;
1155 /* The global record of string concatentations, for use in
1156 extracting locations within string literals. */
1158 GTY(()) string_concat_db *g_string_concat_db;
1160 /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
1162 const char *
1163 c_get_substring_location (const substring_loc &substr_loc,
1164 location_t *out_loc)
1166 enum cpp_ttype tok_type
1167 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
1168 if (tok_type == CPP_OTHER)
1169 return "unrecognized string type";
1171 return get_source_location_for_substring (parse_in, g_string_concat_db,
1172 substr_loc.get_fmt_string_loc (),
1173 tok_type,
1174 substr_loc.get_caret_idx (),
1175 substr_loc.get_start_idx (),
1176 substr_loc.get_end_idx (),
1177 out_loc);
1181 /* Fold X for consideration by one of the warning functions when checking
1182 whether an expression has a constant value. */
1184 static tree
1185 fold_for_warn (tree x)
1187 if (c_dialect_cxx ())
1188 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1189 else
1190 /* The C front-end has already folded X appropriately. */
1191 return x;
1194 /* Print a warning if a constant expression had overflow in folding.
1195 Invoke this function on every expression that the language
1196 requires to be a constant expression.
1197 Note the ANSI C standard says it is erroneous for a
1198 constant expression to overflow. */
1200 void
1201 constant_expression_warning (tree value)
1203 if (warn_overflow && pedantic
1204 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1205 || TREE_CODE (value) == FIXED_CST
1206 || TREE_CODE (value) == VECTOR_CST
1207 || TREE_CODE (value) == COMPLEX_CST)
1208 && TREE_OVERFLOW (value))
1209 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1212 /* The same as above but print an unconditional error. */
1213 void
1214 constant_expression_error (tree value)
1216 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1217 || TREE_CODE (value) == FIXED_CST
1218 || TREE_CODE (value) == VECTOR_CST
1219 || TREE_CODE (value) == COMPLEX_CST)
1220 && TREE_OVERFLOW (value))
1221 error ("overflow in constant expression");
1224 /* Print a warning if an expression had overflow in folding and its
1225 operands hadn't.
1227 Invoke this function on every expression that
1228 (1) appears in the source code, and
1229 (2) is a constant expression that overflowed, and
1230 (3) is not already checked by convert_and_check;
1231 however, do not invoke this function on operands of explicit casts
1232 or when the expression is the result of an operator and any operand
1233 already overflowed. */
1235 void
1236 overflow_warning (location_t loc, tree value)
1238 if (c_inhibit_evaluation_warnings != 0)
1239 return;
1241 switch (TREE_CODE (value))
1243 case INTEGER_CST:
1244 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1245 break;
1247 case REAL_CST:
1248 warning_at (loc, OPT_Woverflow,
1249 "floating point overflow in expression");
1250 break;
1252 case FIXED_CST:
1253 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1254 break;
1256 case VECTOR_CST:
1257 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1258 break;
1260 case COMPLEX_CST:
1261 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1262 warning_at (loc, OPT_Woverflow,
1263 "complex integer overflow in expression");
1264 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1265 warning_at (loc, OPT_Woverflow,
1266 "complex floating point overflow in expression");
1267 break;
1269 default:
1270 break;
1274 /* Warn about uses of logical || / && operator in a context where it
1275 is likely that the bitwise equivalent was intended by the
1276 programmer. We have seen an expression in which CODE is a binary
1277 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1278 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1279 void
1280 warn_logical_operator (location_t location, enum tree_code code, tree type,
1281 enum tree_code code_left, tree op_left,
1282 enum tree_code ARG_UNUSED (code_right), tree op_right)
1284 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1285 int in0_p, in1_p, in_p;
1286 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1287 bool strict_overflow_p = false;
1289 if (code != TRUTH_ANDIF_EXPR
1290 && code != TRUTH_AND_EXPR
1291 && code != TRUTH_ORIF_EXPR
1292 && code != TRUTH_OR_EXPR)
1293 return;
1295 /* We don't want to warn if either operand comes from a macro
1296 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1297 see PR61534. */
1298 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1299 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1300 return;
1302 /* Warn if &&/|| are being used in a context where it is
1303 likely that the bitwise equivalent was intended by the
1304 programmer. That is, an expression such as op && MASK
1305 where op should not be any boolean expression, nor a
1306 constant, and mask seems to be a non-boolean integer constant. */
1307 if (TREE_CODE (op_right) == CONST_DECL)
1308 /* An enumerator counts as a constant. */
1309 op_right = DECL_INITIAL (op_right);
1310 if (!truth_value_p (code_left)
1311 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1312 && !CONSTANT_CLASS_P (op_left)
1313 && !TREE_NO_WARNING (op_left)
1314 && TREE_CODE (op_right) == INTEGER_CST
1315 && !integer_zerop (op_right)
1316 && !integer_onep (op_right))
1318 if (or_op)
1319 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1320 " applied to non-boolean constant");
1321 else
1322 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1323 " applied to non-boolean constant");
1324 TREE_NO_WARNING (op_left) = true;
1325 return;
1328 /* We do not warn for constants because they are typical of macro
1329 expansions that test for features. */
1330 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1331 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
1332 return;
1334 /* This warning only makes sense with logical operands. */
1335 if (!(truth_value_p (TREE_CODE (op_left))
1336 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1337 || !(truth_value_p (TREE_CODE (op_right))
1338 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1339 return;
1341 /* The range computations only work with scalars. */
1342 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1343 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1344 return;
1346 /* We first test whether either side separately is trivially true
1347 (with OR) or trivially false (with AND). If so, do not warn.
1348 This is a common idiom for testing ranges of data types in
1349 portable code. */
1350 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1351 if (!lhs)
1352 return;
1353 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1354 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1356 /* If this is an OR operation, invert both sides; now, the result
1357 should be always false to get a warning. */
1358 if (or_op)
1359 in0_p = !in0_p;
1361 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1362 if (tem && integer_zerop (tem))
1363 return;
1365 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1366 if (!rhs)
1367 return;
1368 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1369 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1371 /* If this is an OR operation, invert both sides; now, the result
1372 should be always false to get a warning. */
1373 if (or_op)
1374 in1_p = !in1_p;
1376 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1377 if (tem && integer_zerop (tem))
1378 return;
1380 /* If both expressions have the same operand, if we can merge the
1381 ranges, ... */
1382 if (operand_equal_p (lhs, rhs, 0)
1383 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1384 in1_p, low1, high1))
1386 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1387 /* ... and if the range test is always false, then warn. */
1388 if (tem && integer_zerop (tem))
1390 if (or_op)
1391 warning_at (location, OPT_Wlogical_op,
1392 "logical %<or%> of collectively exhaustive tests is "
1393 "always true");
1394 else
1395 warning_at (location, OPT_Wlogical_op,
1396 "logical %<and%> of mutually exclusive tests is "
1397 "always false");
1399 /* Or warn if the operands have exactly the same range, e.g.
1400 A > 0 && A > 0. */
1401 else if (tree_int_cst_equal (low0, low1)
1402 && tree_int_cst_equal (high0, high1))
1404 if (or_op)
1405 warning_at (location, OPT_Wlogical_op,
1406 "logical %<or%> of equal expressions");
1407 else
1408 warning_at (location, OPT_Wlogical_op,
1409 "logical %<and%> of equal expressions");
1414 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1415 with constant indices. */
1417 static tree
1418 find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1420 tree expr = *expr_p;
1422 if ((TREE_CODE (expr) == ARRAY_REF
1423 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1424 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1426 *(bool *) data = true;
1427 *walk_subtrees = 0;
1430 return NULL_TREE;
1433 /* Warn if a self-comparison always evaluates to true or false. LOC
1434 is the location of the comparison with code CODE, LHS and RHS are
1435 operands of the comparison. */
1437 void
1438 warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1440 if (TREE_CODE_CLASS (code) != tcc_comparison)
1441 return;
1443 /* Don't warn for various macro expansions. */
1444 if (from_macro_expansion_at (loc)
1445 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1446 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1447 return;
1449 /* We do not warn for constants because they are typical of macro
1450 expansions that test for features, sizeof, and similar. */
1451 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1452 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
1453 return;
1455 /* Don't warn for e.g.
1456 HOST_WIDE_INT n;
1458 if (n == (long) n) ...
1460 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1461 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1462 return;
1464 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1465 It could be a NaN, and NaN never compares equal to anything, even
1466 itself. */
1467 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1468 return;
1470 if (operand_equal_p (lhs, rhs, 0))
1472 /* Don't warn about array references with constant indices;
1473 these are likely to come from a macro. */
1474 bool found = false;
1475 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1476 &found);
1477 if (found)
1478 return;
1479 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1480 || code == GE_EXPR || code == UNLE_EXPR
1481 || code == UNGE_EXPR || code == UNEQ_EXPR);
1482 if (always_true)
1483 warning_at (loc, OPT_Wtautological_compare,
1484 "self-comparison always evaluates to true");
1485 else
1486 warning_at (loc, OPT_Wtautological_compare,
1487 "self-comparison always evaluates to false");
1491 /* Return true iff T is a boolean promoted to int. */
1493 static bool
1494 bool_promoted_to_int_p (tree t)
1496 return (CONVERT_EXPR_P (t)
1497 && TREE_TYPE (t) == integer_type_node
1498 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
1501 /* Return true iff EXPR only contains boolean operands, or comparisons. */
1503 static bool
1504 expr_has_boolean_operands_p (tree expr)
1506 STRIP_NOPS (expr);
1508 if (CONVERT_EXPR_P (expr))
1509 return bool_promoted_to_int_p (expr);
1510 else if (UNARY_CLASS_P (expr))
1511 return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0));
1512 else if (BINARY_CLASS_P (expr))
1513 return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0))
1514 && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1)));
1515 else if (COMPARISON_CLASS_P (expr))
1516 return true;
1517 else
1518 return false;
1521 /* Warn about logical not used on the left hand side operand of a comparison.
1522 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1523 Do not warn if RHS is of a boolean type, a logical operator, or
1524 a comparison. */
1526 void
1527 warn_logical_not_parentheses (location_t location, enum tree_code code,
1528 tree lhs, tree rhs)
1530 if (TREE_CODE_CLASS (code) != tcc_comparison
1531 || TREE_TYPE (rhs) == NULL_TREE
1532 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE
1533 || truth_value_p (TREE_CODE (rhs)))
1534 return;
1536 /* Don't warn for expression like !x == ~(bool1 | bool2). */
1537 if (expr_has_boolean_operands_p (rhs))
1538 return;
1540 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1541 !(x == 0) or !(y != 0). */
1542 if ((code == EQ_EXPR || code == NE_EXPR)
1543 && integer_zerop (rhs))
1544 return;
1546 if (warning_at (location, OPT_Wlogical_not_parentheses,
1547 "logical not is only applied to the left hand side of "
1548 "comparison")
1549 && EXPR_HAS_LOCATION (lhs))
1551 location_t lhs_loc = EXPR_LOCATION (lhs);
1552 rich_location richloc (line_table, lhs_loc);
1553 richloc.add_fixit_insert_before (lhs_loc, "(");
1554 richloc.add_fixit_insert_after (lhs_loc, ")");
1555 inform_at_rich_loc (&richloc, "add parentheses around left hand side "
1556 "expression to silence this warning");
1560 /* Warn if EXP contains any computations whose results are not used.
1561 Return true if a warning is printed; false otherwise. LOCUS is the
1562 (potential) location of the expression. */
1564 bool
1565 warn_if_unused_value (const_tree exp, location_t locus)
1567 restart:
1568 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1569 return false;
1571 /* Don't warn about void constructs. This includes casting to void,
1572 void function calls, and statement expressions with a final cast
1573 to void. */
1574 if (VOID_TYPE_P (TREE_TYPE (exp)))
1575 return false;
1577 if (EXPR_HAS_LOCATION (exp))
1578 locus = EXPR_LOCATION (exp);
1580 switch (TREE_CODE (exp))
1582 case PREINCREMENT_EXPR:
1583 case POSTINCREMENT_EXPR:
1584 case PREDECREMENT_EXPR:
1585 case POSTDECREMENT_EXPR:
1586 case MODIFY_EXPR:
1587 case INIT_EXPR:
1588 case TARGET_EXPR:
1589 case CALL_EXPR:
1590 case TRY_CATCH_EXPR:
1591 case WITH_CLEANUP_EXPR:
1592 case EXIT_EXPR:
1593 case VA_ARG_EXPR:
1594 return false;
1596 case BIND_EXPR:
1597 /* For a binding, warn if no side effect within it. */
1598 exp = BIND_EXPR_BODY (exp);
1599 goto restart;
1601 case SAVE_EXPR:
1602 case NON_LVALUE_EXPR:
1603 case NOP_EXPR:
1604 exp = TREE_OPERAND (exp, 0);
1605 goto restart;
1607 case TRUTH_ORIF_EXPR:
1608 case TRUTH_ANDIF_EXPR:
1609 /* In && or ||, warn if 2nd operand has no side effect. */
1610 exp = TREE_OPERAND (exp, 1);
1611 goto restart;
1613 case COMPOUND_EXPR:
1614 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1615 return true;
1616 /* Let people do `(foo (), 0)' without a warning. */
1617 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1618 return false;
1619 exp = TREE_OPERAND (exp, 1);
1620 goto restart;
1622 case COND_EXPR:
1623 /* If this is an expression with side effects, don't warn; this
1624 case commonly appears in macro expansions. */
1625 if (TREE_SIDE_EFFECTS (exp))
1626 return false;
1627 goto warn;
1629 case INDIRECT_REF:
1630 /* Don't warn about automatic dereferencing of references, since
1631 the user cannot control it. */
1632 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1634 exp = TREE_OPERAND (exp, 0);
1635 goto restart;
1637 /* Fall through. */
1639 default:
1640 /* Referencing a volatile value is a side effect, so don't warn. */
1641 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1642 && TREE_THIS_VOLATILE (exp))
1643 return false;
1645 /* If this is an expression which has no operands, there is no value
1646 to be unused. There are no such language-independent codes,
1647 but front ends may define such. */
1648 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1649 return false;
1651 warn:
1652 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1657 /* Print a warning about casts that might indicate violation
1658 of strict aliasing rules if -Wstrict-aliasing is used and
1659 strict aliasing mode is in effect. OTYPE is the original
1660 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1662 bool
1663 strict_aliasing_warning (tree otype, tree type, tree expr)
1665 /* Strip pointer conversion chains and get to the correct original type. */
1666 STRIP_NOPS (expr);
1667 otype = TREE_TYPE (expr);
1669 if (!(flag_strict_aliasing
1670 && POINTER_TYPE_P (type)
1671 && POINTER_TYPE_P (otype)
1672 && !VOID_TYPE_P (TREE_TYPE (type)))
1673 /* If the type we are casting to is a ref-all pointer
1674 dereferencing it is always valid. */
1675 || TYPE_REF_CAN_ALIAS_ALL (type))
1676 return false;
1678 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1679 && (DECL_P (TREE_OPERAND (expr, 0))
1680 || handled_component_p (TREE_OPERAND (expr, 0))))
1682 /* Casting the address of an object to non void pointer. Warn
1683 if the cast breaks type based aliasing. */
1684 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1686 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1687 "might break strict-aliasing rules");
1688 return true;
1690 else
1692 /* warn_strict_aliasing >= 3. This includes the default (3).
1693 Only warn if the cast is dereferenced immediately. */
1694 alias_set_type set1 =
1695 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1696 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1698 if (set1 != set2 && set2 != 0
1699 && (set1 == 0
1700 || (!alias_set_subset_of (set2, set1)
1701 && !alias_sets_conflict_p (set1, set2))))
1703 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1704 "pointer will break strict-aliasing rules");
1705 return true;
1707 else if (warn_strict_aliasing == 2
1708 && !alias_sets_must_conflict_p (set1, set2))
1710 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1711 "pointer might break strict-aliasing rules");
1712 return true;
1716 else
1717 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1719 /* At this level, warn for any conversions, even if an address is
1720 not taken in the same statement. This will likely produce many
1721 false positives, but could be useful to pinpoint problems that
1722 are not revealed at higher levels. */
1723 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1724 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1725 if (!COMPLETE_TYPE_P (type)
1726 || !alias_sets_must_conflict_p (set1, set2))
1728 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1729 "pointer might break strict-aliasing rules");
1730 return true;
1734 return false;
1737 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1738 sizeof as last operand of certain builtins. */
1740 void
1741 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1742 vec<tree, va_gc> *params, tree *sizeof_arg,
1743 bool (*comp_types) (tree, tree))
1745 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1746 bool strop = false, cmp = false;
1747 unsigned int idx = ~0;
1748 location_t loc;
1750 if (TREE_CODE (callee) != FUNCTION_DECL
1751 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1752 || vec_safe_length (params) <= 1)
1753 return;
1755 switch (DECL_FUNCTION_CODE (callee))
1757 case BUILT_IN_STRNCMP:
1758 case BUILT_IN_STRNCASECMP:
1759 cmp = true;
1760 /* FALLTHRU */
1761 case BUILT_IN_STRNCPY:
1762 case BUILT_IN_STRNCPY_CHK:
1763 case BUILT_IN_STRNCAT:
1764 case BUILT_IN_STRNCAT_CHK:
1765 case BUILT_IN_STPNCPY:
1766 case BUILT_IN_STPNCPY_CHK:
1767 strop = true;
1768 /* FALLTHRU */
1769 case BUILT_IN_MEMCPY:
1770 case BUILT_IN_MEMCPY_CHK:
1771 case BUILT_IN_MEMMOVE:
1772 case BUILT_IN_MEMMOVE_CHK:
1773 if (params->length () < 3)
1774 return;
1775 src = (*params)[1];
1776 dest = (*params)[0];
1777 idx = 2;
1778 break;
1779 case BUILT_IN_BCOPY:
1780 if (params->length () < 3)
1781 return;
1782 src = (*params)[0];
1783 dest = (*params)[1];
1784 idx = 2;
1785 break;
1786 case BUILT_IN_MEMCMP:
1787 case BUILT_IN_BCMP:
1788 if (params->length () < 3)
1789 return;
1790 src = (*params)[1];
1791 dest = (*params)[0];
1792 idx = 2;
1793 cmp = true;
1794 break;
1795 case BUILT_IN_MEMSET:
1796 case BUILT_IN_MEMSET_CHK:
1797 if (params->length () < 3)
1798 return;
1799 dest = (*params)[0];
1800 idx = 2;
1801 break;
1802 case BUILT_IN_BZERO:
1803 dest = (*params)[0];
1804 idx = 1;
1805 break;
1806 case BUILT_IN_STRNDUP:
1807 src = (*params)[0];
1808 strop = true;
1809 idx = 1;
1810 break;
1811 case BUILT_IN_MEMCHR:
1812 if (params->length () < 3)
1813 return;
1814 src = (*params)[0];
1815 idx = 2;
1816 break;
1817 case BUILT_IN_SNPRINTF:
1818 case BUILT_IN_SNPRINTF_CHK:
1819 case BUILT_IN_VSNPRINTF:
1820 case BUILT_IN_VSNPRINTF_CHK:
1821 dest = (*params)[0];
1822 idx = 1;
1823 strop = true;
1824 break;
1825 default:
1826 break;
1829 if (idx >= 3)
1830 return;
1832 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1833 return;
1835 type = TYPE_P (sizeof_arg[idx])
1836 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1837 if (!POINTER_TYPE_P (type))
1838 return;
1840 if (dest
1841 && (tem = tree_strip_nop_conversions (dest))
1842 && POINTER_TYPE_P (TREE_TYPE (tem))
1843 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1844 return;
1846 if (src
1847 && (tem = tree_strip_nop_conversions (src))
1848 && POINTER_TYPE_P (TREE_TYPE (tem))
1849 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1850 return;
1852 loc = sizeof_arg_loc[idx];
1854 if (dest && !cmp)
1856 if (!TYPE_P (sizeof_arg[idx])
1857 && operand_equal_p (dest, sizeof_arg[idx], 0)
1858 && comp_types (TREE_TYPE (dest), type))
1860 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1861 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1862 "argument to %<sizeof%> in %qD call is the same "
1863 "expression as the destination; did you mean to "
1864 "remove the addressof?", callee);
1865 else if ((TYPE_PRECISION (TREE_TYPE (type))
1866 == TYPE_PRECISION (char_type_node))
1867 || strop)
1868 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1869 "argument to %<sizeof%> in %qD call is the same "
1870 "expression as the destination; did you mean to "
1871 "provide an explicit length?", callee);
1872 else
1873 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1874 "argument to %<sizeof%> in %qD call is the same "
1875 "expression as the destination; did you mean to "
1876 "dereference it?", callee);
1877 return;
1880 if (POINTER_TYPE_P (TREE_TYPE (dest))
1881 && !strop
1882 && comp_types (TREE_TYPE (dest), type)
1883 && !VOID_TYPE_P (TREE_TYPE (type)))
1885 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1886 "argument to %<sizeof%> in %qD call is the same "
1887 "pointer type %qT as the destination; expected %qT "
1888 "or an explicit length", callee, TREE_TYPE (dest),
1889 TREE_TYPE (TREE_TYPE (dest)));
1890 return;
1894 if (src && !cmp)
1896 if (!TYPE_P (sizeof_arg[idx])
1897 && operand_equal_p (src, sizeof_arg[idx], 0)
1898 && comp_types (TREE_TYPE (src), type))
1900 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1901 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1902 "argument to %<sizeof%> in %qD call is the same "
1903 "expression as the source; did you mean to "
1904 "remove the addressof?", callee);
1905 else if ((TYPE_PRECISION (TREE_TYPE (type))
1906 == TYPE_PRECISION (char_type_node))
1907 || strop)
1908 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1909 "argument to %<sizeof%> in %qD call is the same "
1910 "expression as the source; did you mean to "
1911 "provide an explicit length?", callee);
1912 else
1913 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1914 "argument to %<sizeof%> in %qD call is the same "
1915 "expression as the source; did you mean to "
1916 "dereference it?", callee);
1917 return;
1920 if (POINTER_TYPE_P (TREE_TYPE (src))
1921 && !strop
1922 && comp_types (TREE_TYPE (src), type)
1923 && !VOID_TYPE_P (TREE_TYPE (type)))
1925 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1926 "argument to %<sizeof%> in %qD call is the same "
1927 "pointer type %qT as the source; expected %qT "
1928 "or an explicit length", callee, TREE_TYPE (src),
1929 TREE_TYPE (TREE_TYPE (src)));
1930 return;
1934 if (dest)
1936 if (!TYPE_P (sizeof_arg[idx])
1937 && operand_equal_p (dest, sizeof_arg[idx], 0)
1938 && comp_types (TREE_TYPE (dest), type))
1940 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1941 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1942 "argument to %<sizeof%> in %qD call is the same "
1943 "expression as the first source; did you mean to "
1944 "remove the addressof?", callee);
1945 else if ((TYPE_PRECISION (TREE_TYPE (type))
1946 == TYPE_PRECISION (char_type_node))
1947 || strop)
1948 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1949 "argument to %<sizeof%> in %qD call is the same "
1950 "expression as the first source; did you mean to "
1951 "provide an explicit length?", callee);
1952 else
1953 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1954 "argument to %<sizeof%> in %qD call is the same "
1955 "expression as the first source; did you mean to "
1956 "dereference it?", callee);
1957 return;
1960 if (POINTER_TYPE_P (TREE_TYPE (dest))
1961 && !strop
1962 && comp_types (TREE_TYPE (dest), type)
1963 && !VOID_TYPE_P (TREE_TYPE (type)))
1965 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1966 "argument to %<sizeof%> in %qD call is the same "
1967 "pointer type %qT as the first source; expected %qT "
1968 "or an explicit length", callee, TREE_TYPE (dest),
1969 TREE_TYPE (TREE_TYPE (dest)));
1970 return;
1974 if (src)
1976 if (!TYPE_P (sizeof_arg[idx])
1977 && operand_equal_p (src, sizeof_arg[idx], 0)
1978 && comp_types (TREE_TYPE (src), type))
1980 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1981 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1982 "argument to %<sizeof%> in %qD call is the same "
1983 "expression as the second source; did you mean to "
1984 "remove the addressof?", callee);
1985 else if ((TYPE_PRECISION (TREE_TYPE (type))
1986 == TYPE_PRECISION (char_type_node))
1987 || strop)
1988 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1989 "argument to %<sizeof%> in %qD call is the same "
1990 "expression as the second source; did you mean to "
1991 "provide an explicit length?", callee);
1992 else
1993 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1994 "argument to %<sizeof%> in %qD call is the same "
1995 "expression as the second source; did you mean to "
1996 "dereference it?", callee);
1997 return;
2000 if (POINTER_TYPE_P (TREE_TYPE (src))
2001 && !strop
2002 && comp_types (TREE_TYPE (src), type)
2003 && !VOID_TYPE_P (TREE_TYPE (type)))
2005 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2006 "argument to %<sizeof%> in %qD call is the same "
2007 "pointer type %qT as the second source; expected %qT "
2008 "or an explicit length", callee, TREE_TYPE (src),
2009 TREE_TYPE (TREE_TYPE (src)));
2010 return;
2016 /* Warn for unlikely, improbable, or stupid DECL declarations
2017 of `main'. */
2019 void
2020 check_main_parameter_types (tree decl)
2022 function_args_iterator iter;
2023 tree type;
2024 int argct = 0;
2026 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2028 /* XXX void_type_node belies the abstraction. */
2029 if (type == void_type_node || type == error_mark_node )
2030 break;
2032 tree t = type;
2033 if (TYPE_ATOMIC (t))
2034 pedwarn (input_location, OPT_Wmain,
2035 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2036 type, decl);
2037 while (POINTER_TYPE_P (t))
2039 t = TREE_TYPE (t);
2040 if (TYPE_ATOMIC (t))
2041 pedwarn (input_location, OPT_Wmain,
2042 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2043 type, decl);
2046 ++argct;
2047 switch (argct)
2049 case 1:
2050 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2051 pedwarn (input_location, OPT_Wmain,
2052 "first argument of %q+D should be %<int%>", decl);
2053 break;
2055 case 2:
2056 if (TREE_CODE (type) != POINTER_TYPE
2057 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2058 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2059 != char_type_node))
2060 pedwarn (input_location, OPT_Wmain,
2061 "second argument of %q+D should be %<char **%>", decl);
2062 break;
2064 case 3:
2065 if (TREE_CODE (type) != POINTER_TYPE
2066 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2067 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2068 != char_type_node))
2069 pedwarn (input_location, OPT_Wmain,
2070 "third argument of %q+D should probably be "
2071 "%<char **%>", decl);
2072 break;
2076 /* It is intentional that this message does not mention the third
2077 argument because it's only mentioned in an appendix of the
2078 standard. */
2079 if (argct > 0 && (argct < 2 || argct > 3))
2080 pedwarn (input_location, OPT_Wmain,
2081 "%q+D takes only zero or two arguments", decl);
2083 if (stdarg_p (TREE_TYPE (decl)))
2084 pedwarn (input_location, OPT_Wmain,
2085 "%q+D declared as variadic function", decl);
2088 /* vector_targets_convertible_p is used for vector pointer types. The
2089 callers perform various checks that the qualifiers are satisfactory,
2090 while OTOH vector_targets_convertible_p ignores the number of elements
2091 in the vectors. That's fine with vector pointers as we can consider,
2092 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2093 and that does not require and conversion of the pointer values.
2094 In contrast, vector_types_convertible_p and
2095 vector_types_compatible_elements_p are used for vector value types. */
2096 /* True if pointers to distinct types T1 and T2 can be converted to
2097 each other without an explicit cast. Only returns true for opaque
2098 vector types. */
2099 bool
2100 vector_targets_convertible_p (const_tree t1, const_tree t2)
2102 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
2103 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2104 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2105 return true;
2107 return false;
2110 /* vector_types_convertible_p is used for vector value types.
2111 It could in principle call vector_targets_convertible_p as a subroutine,
2112 but then the check for vector type would be duplicated with its callers,
2113 and also the purpose of vector_targets_convertible_p would become
2114 muddled.
2115 Where vector_types_convertible_p returns true, a conversion might still be
2116 needed to make the types match.
2117 In contrast, vector_targets_convertible_p is used for vector pointer
2118 values, and vector_types_compatible_elements_p is used specifically
2119 in the context for binary operators, as a check if use is possible without
2120 conversion. */
2121 /* True if vector types T1 and T2 can be converted to each other
2122 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2123 can only be converted with -flax-vector-conversions yet that is not
2124 in effect, emit a note telling the user about that option if such
2125 a note has not previously been emitted. */
2126 bool
2127 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2129 static bool emitted_lax_note = false;
2130 bool convertible_lax;
2132 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2133 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2134 return true;
2136 convertible_lax =
2137 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2138 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2139 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2140 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2141 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2143 if (!convertible_lax || flag_lax_vector_conversions)
2144 return convertible_lax;
2146 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2147 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2148 return true;
2150 if (emit_lax_note && !emitted_lax_note)
2152 emitted_lax_note = true;
2153 inform (input_location, "use -flax-vector-conversions to permit "
2154 "conversions between vectors with differing "
2155 "element types or numbers of subparts");
2158 return false;
2161 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2162 and have vector types, V0 has the same type as V1, and the number of
2163 elements of V0, V1, MASK is the same.
2165 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2166 called with two arguments. In this case implementation passes the
2167 first argument twice in order to share the same tree code. This fact
2168 could enable the mask-values being twice the vector length. This is
2169 an implementation accident and this semantics is not guaranteed to
2170 the user. */
2171 tree
2172 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2173 bool complain)
2175 tree ret;
2176 bool wrap = true;
2177 bool maybe_const = false;
2178 bool two_arguments = false;
2180 if (v1 == NULL_TREE)
2182 two_arguments = true;
2183 v1 = v0;
2186 if (v0 == error_mark_node || v1 == error_mark_node
2187 || mask == error_mark_node)
2188 return error_mark_node;
2190 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
2192 if (complain)
2193 error_at (loc, "__builtin_shuffle last argument must "
2194 "be an integer vector");
2195 return error_mark_node;
2198 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2199 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
2201 if (complain)
2202 error_at (loc, "__builtin_shuffle arguments must be vectors");
2203 return error_mark_node;
2206 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2208 if (complain)
2209 error_at (loc, "__builtin_shuffle argument vectors must be of "
2210 "the same type");
2211 return error_mark_node;
2214 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2215 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2216 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2217 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2219 if (complain)
2220 error_at (loc, "__builtin_shuffle number of elements of the "
2221 "argument vector(s) and the mask vector should "
2222 "be the same");
2223 return error_mark_node;
2226 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2227 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2229 if (complain)
2230 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2231 "must have the same size as inner type of the mask");
2232 return error_mark_node;
2235 if (!c_dialect_cxx ())
2237 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2238 v0 = c_fully_fold (v0, false, &maybe_const);
2239 wrap &= maybe_const;
2241 if (two_arguments)
2242 v1 = v0 = save_expr (v0);
2243 else
2245 v1 = c_fully_fold (v1, false, &maybe_const);
2246 wrap &= maybe_const;
2249 mask = c_fully_fold (mask, false, &maybe_const);
2250 wrap &= maybe_const;
2252 else if (two_arguments)
2253 v1 = v0 = save_expr (v0);
2255 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2257 if (!c_dialect_cxx () && !wrap)
2258 ret = c_wrap_maybe_const (ret, true);
2260 return ret;
2263 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2264 to integral type. */
2266 static tree
2267 c_common_get_narrower (tree op, int *unsignedp_ptr)
2269 op = get_narrower (op, unsignedp_ptr);
2271 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2272 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2274 /* C++0x scoped enumerations don't implicitly convert to integral
2275 type; if we stripped an explicit conversion to a larger type we
2276 need to replace it so common_type will still work. */
2277 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2278 TYPE_UNSIGNED (TREE_TYPE (op)));
2279 op = fold_convert (type, op);
2281 return op;
2284 /* This is a helper function of build_binary_op.
2286 For certain operations if both args were extended from the same
2287 smaller type, do the arithmetic in that type and then extend.
2289 BITWISE indicates a bitwise operation.
2290 For them, this optimization is safe only if
2291 both args are zero-extended or both are sign-extended.
2292 Otherwise, we might change the result.
2293 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2294 but calculated in (unsigned short) it would be (unsigned short)-1.
2296 tree
2297 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2299 int unsigned0, unsigned1;
2300 tree arg0, arg1;
2301 int uns;
2302 tree type;
2304 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2305 excessive narrowing when we call get_narrower below. For
2306 example, suppose that OP0 is of unsigned int extended
2307 from signed char and that RESULT_TYPE is long long int.
2308 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2309 like
2311 (long long int) (unsigned int) signed_char
2313 which get_narrower would narrow down to
2315 (unsigned int) signed char
2317 If we do not cast OP0 first, get_narrower would return
2318 signed_char, which is inconsistent with the case of the
2319 explicit cast. */
2320 op0 = convert (result_type, op0);
2321 op1 = convert (result_type, op1);
2323 arg0 = c_common_get_narrower (op0, &unsigned0);
2324 arg1 = c_common_get_narrower (op1, &unsigned1);
2326 /* UNS is 1 if the operation to be done is an unsigned one. */
2327 uns = TYPE_UNSIGNED (result_type);
2329 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2330 but it *requires* conversion to FINAL_TYPE. */
2332 if ((TYPE_PRECISION (TREE_TYPE (op0))
2333 == TYPE_PRECISION (TREE_TYPE (arg0)))
2334 && TREE_TYPE (op0) != result_type)
2335 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2336 if ((TYPE_PRECISION (TREE_TYPE (op1))
2337 == TYPE_PRECISION (TREE_TYPE (arg1)))
2338 && TREE_TYPE (op1) != result_type)
2339 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2341 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2343 /* For bitwise operations, signedness of nominal type
2344 does not matter. Consider only how operands were extended. */
2345 if (bitwise)
2346 uns = unsigned0;
2348 /* Note that in all three cases below we refrain from optimizing
2349 an unsigned operation on sign-extended args.
2350 That would not be valid. */
2352 /* Both args variable: if both extended in same way
2353 from same width, do it in that width.
2354 Do it unsigned if args were zero-extended. */
2355 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2356 < TYPE_PRECISION (result_type))
2357 && (TYPE_PRECISION (TREE_TYPE (arg1))
2358 == TYPE_PRECISION (TREE_TYPE (arg0)))
2359 && unsigned0 == unsigned1
2360 && (unsigned0 || !uns))
2361 return c_common_signed_or_unsigned_type
2362 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2364 else if (TREE_CODE (arg0) == INTEGER_CST
2365 && (unsigned1 || !uns)
2366 && (TYPE_PRECISION (TREE_TYPE (arg1))
2367 < TYPE_PRECISION (result_type))
2368 && (type
2369 = c_common_signed_or_unsigned_type (unsigned1,
2370 TREE_TYPE (arg1)))
2371 && !POINTER_TYPE_P (type)
2372 && int_fits_type_p (arg0, type))
2373 return type;
2375 else if (TREE_CODE (arg1) == INTEGER_CST
2376 && (unsigned0 || !uns)
2377 && (TYPE_PRECISION (TREE_TYPE (arg0))
2378 < TYPE_PRECISION (result_type))
2379 && (type
2380 = c_common_signed_or_unsigned_type (unsigned0,
2381 TREE_TYPE (arg0)))
2382 && !POINTER_TYPE_P (type)
2383 && int_fits_type_p (arg1, type))
2384 return type;
2386 return result_type;
2389 /* Returns true iff any integer value of type FROM_TYPE can be represented as
2390 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2392 static bool
2393 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2395 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2396 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2397 REAL_VALUE_TYPE real_low_bound =
2398 real_value_from_int_cst (0, type_low_bound);
2399 REAL_VALUE_TYPE real_high_bound =
2400 real_value_from_int_cst (0, type_high_bound);
2402 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2403 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2406 /* Checks if expression EXPR of complex/real/integer type cannot be converted
2407 to the complex/real/integer type TYPE. Function returns non-zero when:
2408 * EXPR is a constant which cannot be exactly converted to TYPE.
2409 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2410 for EXPR type and TYPE being both integers or both real, or both
2411 complex.
2412 * EXPR is not a constant of complex type and TYPE is a real or
2413 an integer.
2414 * EXPR is not a constant of real type and TYPE is an integer.
2415 * EXPR is not a constant of integer type which cannot be
2416 exactly converted to real type.
2418 Function allows conversions between types of different signedness and
2419 can return SAFE_CONVERSION (zero) in that case. Function can produce
2420 signedness warnings if PRODUCE_WARNS is true.
2422 Function allows conversions from complex constants to non-complex types,
2423 provided that imaginary part is zero and real part can be safely converted
2424 to TYPE. */
2426 enum conversion_safety
2427 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2429 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2430 tree expr_type = TREE_TYPE (expr);
2431 loc = expansion_point_location_if_in_system_header (loc);
2433 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2435 /* If type is complex, we are interested in compatibility with
2436 underlying type. */
2437 if (TREE_CODE (type) == COMPLEX_TYPE)
2438 type = TREE_TYPE (type);
2440 /* Warn for real constant that is not an exact integer converted
2441 to integer type. */
2442 if (TREE_CODE (expr_type) == REAL_TYPE
2443 && TREE_CODE (type) == INTEGER_TYPE)
2445 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2446 give_warning = UNSAFE_REAL;
2448 /* Warn for an integer constant that does not fit into integer type. */
2449 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2450 && TREE_CODE (type) == INTEGER_TYPE
2451 && !int_fits_type_p (expr, type))
2453 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2454 && tree_int_cst_sgn (expr) < 0)
2456 if (produce_warns)
2457 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2458 " implicitly converted to unsigned type");
2460 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2462 if (produce_warns)
2463 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2464 " constant value to negative integer");
2466 else
2467 give_warning = UNSAFE_OTHER;
2469 else if (TREE_CODE (type) == REAL_TYPE)
2471 /* Warn for an integer constant that does not fit into real type. */
2472 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2474 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2475 if (!exact_real_truncate (TYPE_MODE (type), &a))
2476 give_warning = UNSAFE_REAL;
2478 /* Warn for a real constant that does not fit into a smaller
2479 real type. */
2480 else if (TREE_CODE (expr_type) == REAL_TYPE
2481 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2483 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2484 if (!exact_real_truncate (TYPE_MODE (type), &a))
2485 give_warning = UNSAFE_REAL;
2490 else if (TREE_CODE (expr) == COMPLEX_CST)
2492 tree imag_part = TREE_IMAGPART (expr);
2493 /* Conversion from complex constant with zero imaginary part,
2494 perform check for conversion of real part. */
2495 if ((TREE_CODE (imag_part) == REAL_CST
2496 && real_zerop (imag_part))
2497 || (TREE_CODE (imag_part) == INTEGER_CST
2498 && integer_zerop (imag_part)))
2499 /* Note: in this branch we use recursive call to unsafe_conversion_p
2500 with different type of EXPR, but it is still safe, because when EXPR
2501 is a constant, it's type is not used in text of generated warnings
2502 (otherwise they could sound misleading). */
2503 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2504 produce_warns);
2505 /* Conversion from complex constant with non-zero imaginary part. */
2506 else
2508 /* Conversion to complex type.
2509 Perform checks for both real and imaginary parts. */
2510 if (TREE_CODE (type) == COMPLEX_TYPE)
2512 /* Unfortunately, produce_warns must be false in two subsequent
2513 calls of unsafe_conversion_p, because otherwise we could
2514 produce strange "double" warnings, if both real and imaginary
2515 parts have conversion problems related to signedness.
2517 For example:
2518 int32_t _Complex a = 0x80000000 + 0x80000000i;
2520 Possible solution: add a separate function for checking
2521 constants and combine result of two calls appropriately. */
2522 enum conversion_safety re_safety =
2523 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2524 enum conversion_safety im_safety =
2525 unsafe_conversion_p (loc, type, imag_part, false);
2527 /* Merge the results into appropriate single warning. */
2529 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2530 if (re_safety == im_safety)
2531 give_warning = re_safety;
2532 else if (!re_safety && im_safety)
2533 give_warning = im_safety;
2534 else if (re_safety && !im_safety)
2535 give_warning = re_safety;
2536 else
2537 give_warning = UNSAFE_OTHER;
2539 /* Warn about conversion from complex to real or integer type. */
2540 else
2541 give_warning = UNSAFE_IMAGINARY;
2545 /* Checks for remaining case: EXPR is not constant. */
2546 else
2548 /* Warn for real types converted to integer types. */
2549 if (TREE_CODE (expr_type) == REAL_TYPE
2550 && TREE_CODE (type) == INTEGER_TYPE)
2551 give_warning = UNSAFE_REAL;
2553 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2554 && TREE_CODE (type) == INTEGER_TYPE)
2556 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2557 expr = get_unwidened (expr, 0);
2558 expr_type = TREE_TYPE (expr);
2560 /* Don't warn for short y; short x = ((int)y & 0xff); */
2561 if (TREE_CODE (expr) == BIT_AND_EXPR
2562 || TREE_CODE (expr) == BIT_IOR_EXPR
2563 || TREE_CODE (expr) == BIT_XOR_EXPR)
2565 /* If both args were extended from a shortest type,
2566 use that type if that is safe. */
2567 expr_type = shorten_binary_op (expr_type,
2568 TREE_OPERAND (expr, 0),
2569 TREE_OPERAND (expr, 1),
2570 /* bitwise */1);
2572 if (TREE_CODE (expr) == BIT_AND_EXPR)
2574 tree op0 = TREE_OPERAND (expr, 0);
2575 tree op1 = TREE_OPERAND (expr, 1);
2576 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2577 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2579 /* If one of the operands is a non-negative constant
2580 that fits in the target type, then the type of the
2581 other operand does not matter. */
2582 if ((TREE_CODE (op0) == INTEGER_CST
2583 && int_fits_type_p (op0, c_common_signed_type (type))
2584 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2585 || (TREE_CODE (op1) == INTEGER_CST
2586 && int_fits_type_p (op1, c_common_signed_type (type))
2587 && int_fits_type_p (op1,
2588 c_common_unsigned_type (type))))
2589 return SAFE_CONVERSION;
2590 /* If constant is unsigned and fits in the target
2591 type, then the result will also fit. */
2592 else if ((TREE_CODE (op0) == INTEGER_CST
2593 && unsigned0
2594 && int_fits_type_p (op0, type))
2595 || (TREE_CODE (op1) == INTEGER_CST
2596 && unsigned1
2597 && int_fits_type_p (op1, type)))
2598 return SAFE_CONVERSION;
2601 /* Warn for integer types converted to smaller integer types. */
2602 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2603 give_warning = UNSAFE_OTHER;
2605 /* When they are the same width but different signedness,
2606 then the value may change. */
2607 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2608 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2609 /* Even when converted to a bigger type, if the type is
2610 unsigned but expr is signed, then negative values
2611 will be changed. */
2612 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2613 && produce_warns)
2614 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2615 "may change the sign of the result",
2616 type, expr_type);
2619 /* Warn for integer types converted to real types if and only if
2620 all the range of values of the integer type cannot be
2621 represented by the real type. */
2622 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2623 && TREE_CODE (type) == REAL_TYPE)
2625 /* Don't warn about char y = 0xff; float x = (int) y; */
2626 expr = get_unwidened (expr, 0);
2627 expr_type = TREE_TYPE (expr);
2629 if (!int_safely_convertible_to_real_p (expr_type, type))
2630 give_warning = UNSAFE_OTHER;
2633 /* Warn for real types converted to smaller real types. */
2634 else if (TREE_CODE (expr_type) == REAL_TYPE
2635 && TREE_CODE (type) == REAL_TYPE
2636 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2637 give_warning = UNSAFE_REAL;
2639 /* Check conversion between two complex types. */
2640 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2641 && TREE_CODE (type) == COMPLEX_TYPE)
2643 /* Extract underlying types (i.e., type of real and imaginary
2644 parts) of expr_type and type. */
2645 tree from_type = TREE_TYPE (expr_type);
2646 tree to_type = TREE_TYPE (type);
2648 /* Warn for real types converted to integer types. */
2649 if (TREE_CODE (from_type) == REAL_TYPE
2650 && TREE_CODE (to_type) == INTEGER_TYPE)
2651 give_warning = UNSAFE_REAL;
2653 /* Warn for real types converted to smaller real types. */
2654 else if (TREE_CODE (from_type) == REAL_TYPE
2655 && TREE_CODE (to_type) == REAL_TYPE
2656 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2657 give_warning = UNSAFE_REAL;
2659 /* Check conversion for complex integer types. Here implementation
2660 is simpler than for real-domain integers because it does not
2661 involve sophisticated cases, such as bitmasks, casts, etc. */
2662 else if (TREE_CODE (from_type) == INTEGER_TYPE
2663 && TREE_CODE (to_type) == INTEGER_TYPE)
2665 /* Warn for integer types converted to smaller integer types. */
2666 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2667 give_warning = UNSAFE_OTHER;
2669 /* Check for different signedness, see case for real-domain
2670 integers (above) for a more detailed comment. */
2671 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2672 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2673 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2674 && produce_warns)
2675 warning_at (loc, OPT_Wsign_conversion,
2676 "conversion to %qT from %qT "
2677 "may change the sign of the result",
2678 type, expr_type);
2680 else if (TREE_CODE (from_type) == INTEGER_TYPE
2681 && TREE_CODE (to_type) == REAL_TYPE
2682 && !int_safely_convertible_to_real_p (from_type, to_type))
2683 give_warning = UNSAFE_OTHER;
2686 /* Warn for complex types converted to real or integer types. */
2687 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2688 && TREE_CODE (type) != COMPLEX_TYPE)
2689 give_warning = UNSAFE_IMAGINARY;
2692 return give_warning;
2695 /* Warns if the conversion of EXPR to TYPE may alter a value.
2696 This is a helper function for warnings_for_convert_and_check. */
2698 static void
2699 conversion_warning (location_t loc, tree type, tree expr)
2701 tree expr_type = TREE_TYPE (expr);
2702 enum conversion_safety conversion_kind;
2704 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2705 return;
2707 /* This may happen, because for LHS op= RHS we preevaluate
2708 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2709 means we could no longer see the code of the EXPR. */
2710 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2711 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2712 if (TREE_CODE (expr) == SAVE_EXPR)
2713 expr = TREE_OPERAND (expr, 0);
2715 switch (TREE_CODE (expr))
2717 case EQ_EXPR:
2718 case NE_EXPR:
2719 case LE_EXPR:
2720 case GE_EXPR:
2721 case LT_EXPR:
2722 case GT_EXPR:
2723 case TRUTH_ANDIF_EXPR:
2724 case TRUTH_ORIF_EXPR:
2725 case TRUTH_AND_EXPR:
2726 case TRUTH_OR_EXPR:
2727 case TRUTH_XOR_EXPR:
2728 case TRUTH_NOT_EXPR:
2729 /* Conversion from boolean to a signed:1 bit-field (which only
2730 can hold the values 0 and -1) doesn't lose information - but
2731 it does change the value. */
2732 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2733 warning_at (loc, OPT_Wconversion,
2734 "conversion to %qT from boolean expression", type);
2735 return;
2737 case REAL_CST:
2738 case INTEGER_CST:
2739 case COMPLEX_CST:
2740 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2741 if (conversion_kind == UNSAFE_REAL)
2742 warning_at (loc, OPT_Wfloat_conversion,
2743 "conversion to %qT alters %qT constant value",
2744 type, expr_type);
2745 else if (conversion_kind)
2746 warning_at (loc, OPT_Wconversion,
2747 "conversion to %qT alters %qT constant value",
2748 type, expr_type);
2749 return;
2751 case COND_EXPR:
2753 /* In case of COND_EXPR, we do not care about the type of
2754 COND_EXPR, only about the conversion of each operand. */
2755 tree op1 = TREE_OPERAND (expr, 1);
2756 tree op2 = TREE_OPERAND (expr, 2);
2758 conversion_warning (loc, type, op1);
2759 conversion_warning (loc, type, op2);
2760 return;
2763 default: /* 'expr' is not a constant. */
2764 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2765 if (conversion_kind == UNSAFE_REAL)
2766 warning_at (loc, OPT_Wfloat_conversion,
2767 "conversion to %qT from %qT may alter its value",
2768 type, expr_type);
2769 else if (conversion_kind == UNSAFE_IMAGINARY)
2770 warning_at (loc, OPT_Wconversion,
2771 "conversion to %qT from %qT discards imaginary component",
2772 type, expr_type);
2773 else if (conversion_kind)
2774 warning_at (loc, OPT_Wconversion,
2775 "conversion to %qT from %qT may alter its value",
2776 type, expr_type);
2780 /* Produce warnings after a conversion. RESULT is the result of
2781 converting EXPR to TYPE. This is a helper function for
2782 convert_and_check and cp_convert_and_check. */
2784 void
2785 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2786 tree result)
2788 loc = expansion_point_location_if_in_system_header (loc);
2790 if (TREE_CODE (expr) == INTEGER_CST
2791 && (TREE_CODE (type) == INTEGER_TYPE
2792 || TREE_CODE (type) == ENUMERAL_TYPE)
2793 && !int_fits_type_p (expr, type))
2795 /* Do not diagnose overflow in a constant expression merely
2796 because a conversion overflowed. */
2797 if (TREE_OVERFLOW (result))
2798 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2800 if (TYPE_UNSIGNED (type))
2802 /* This detects cases like converting -129 or 256 to
2803 unsigned char. */
2804 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2805 warning_at (loc, OPT_Woverflow,
2806 "large integer implicitly truncated to unsigned type");
2807 else
2808 conversion_warning (loc, type, expr);
2810 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2811 warning_at (loc, OPT_Woverflow,
2812 "overflow in implicit constant conversion");
2813 /* No warning for converting 0x80000000 to int. */
2814 else if (pedantic
2815 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2816 || TYPE_PRECISION (TREE_TYPE (expr))
2817 != TYPE_PRECISION (type)))
2818 warning_at (loc, OPT_Woverflow,
2819 "overflow in implicit constant conversion");
2821 else
2822 conversion_warning (loc, type, expr);
2824 else if ((TREE_CODE (result) == INTEGER_CST
2825 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2826 warning_at (loc, OPT_Woverflow,
2827 "overflow in implicit constant conversion");
2828 else
2829 conversion_warning (loc, type, expr);
2833 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2834 Invoke this function on every expression that is converted implicitly,
2835 i.e. because of language rules and not because of an explicit cast. */
2837 tree
2838 convert_and_check (location_t loc, tree type, tree expr)
2840 tree result;
2841 tree expr_for_warning;
2843 /* Convert from a value with possible excess precision rather than
2844 via the semantic type, but do not warn about values not fitting
2845 exactly in the semantic type. */
2846 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2848 tree orig_type = TREE_TYPE (expr);
2849 expr = TREE_OPERAND (expr, 0);
2850 expr_for_warning = convert (orig_type, expr);
2851 if (orig_type == type)
2852 return expr_for_warning;
2854 else
2855 expr_for_warning = expr;
2857 if (TREE_TYPE (expr) == type)
2858 return expr;
2860 result = convert (type, expr);
2862 if (c_inhibit_evaluation_warnings == 0
2863 && !TREE_OVERFLOW_P (expr)
2864 && result != error_mark_node)
2865 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2867 return result;
2870 /* A node in a list that describes references to variables (EXPR), which are
2871 either read accesses if WRITER is zero, or write accesses, in which case
2872 WRITER is the parent of EXPR. */
2873 struct tlist
2875 struct tlist *next;
2876 tree expr, writer;
2879 /* Used to implement a cache the results of a call to verify_tree. We only
2880 use this for SAVE_EXPRs. */
2881 struct tlist_cache
2883 struct tlist_cache *next;
2884 struct tlist *cache_before_sp;
2885 struct tlist *cache_after_sp;
2886 tree expr;
2889 /* Obstack to use when allocating tlist structures, and corresponding
2890 firstobj. */
2891 static struct obstack tlist_obstack;
2892 static char *tlist_firstobj = 0;
2894 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2895 warnings. */
2896 static struct tlist *warned_ids;
2897 /* SAVE_EXPRs need special treatment. We process them only once and then
2898 cache the results. */
2899 static struct tlist_cache *save_expr_cache;
2901 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2902 static void merge_tlist (struct tlist **, struct tlist *, int);
2903 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2904 static int warning_candidate_p (tree);
2905 static bool candidate_equal_p (const_tree, const_tree);
2906 static void warn_for_collisions (struct tlist *);
2907 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2908 static struct tlist *new_tlist (struct tlist *, tree, tree);
2910 /* Create a new struct tlist and fill in its fields. */
2911 static struct tlist *
2912 new_tlist (struct tlist *next, tree t, tree writer)
2914 struct tlist *l;
2915 l = XOBNEW (&tlist_obstack, struct tlist);
2916 l->next = next;
2917 l->expr = t;
2918 l->writer = writer;
2919 return l;
2922 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2923 is nonnull, we ignore any node we find which has a writer equal to it. */
2925 static void
2926 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2928 while (add)
2930 struct tlist *next = add->next;
2931 if (!copy)
2932 add->next = *to;
2933 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2934 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2935 add = next;
2939 /* Merge the nodes of ADD into TO. This merging process is done so that for
2940 each variable that already exists in TO, no new node is added; however if
2941 there is a write access recorded in ADD, and an occurrence on TO is only
2942 a read access, then the occurrence in TO will be modified to record the
2943 write. */
2945 static void
2946 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2948 struct tlist **end = to;
2950 while (*end)
2951 end = &(*end)->next;
2953 while (add)
2955 int found = 0;
2956 struct tlist *tmp2;
2957 struct tlist *next = add->next;
2959 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2960 if (candidate_equal_p (tmp2->expr, add->expr))
2962 found = 1;
2963 if (!tmp2->writer)
2964 tmp2->writer = add->writer;
2966 if (!found)
2968 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
2969 end = &(*end)->next;
2970 *end = 0;
2972 add = next;
2976 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2977 references in list LIST conflict with it, excluding reads if ONLY writers
2978 is nonzero. */
2980 static void
2981 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2982 int only_writes)
2984 struct tlist *tmp;
2986 /* Avoid duplicate warnings. */
2987 for (tmp = warned_ids; tmp; tmp = tmp->next)
2988 if (candidate_equal_p (tmp->expr, written))
2989 return;
2991 while (list)
2993 if (candidate_equal_p (list->expr, written)
2994 && !candidate_equal_p (list->writer, writer)
2995 && (!only_writes || list->writer))
2997 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2998 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
2999 OPT_Wsequence_point, "operation on %qE may be undefined",
3000 list->expr);
3002 list = list->next;
3006 /* Given a list LIST of references to variables, find whether any of these
3007 can cause conflicts due to missing sequence points. */
3009 static void
3010 warn_for_collisions (struct tlist *list)
3012 struct tlist *tmp;
3014 for (tmp = list; tmp; tmp = tmp->next)
3016 if (tmp->writer)
3017 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3021 /* Return nonzero if X is a tree that can be verified by the sequence point
3022 warnings. */
3023 static int
3024 warning_candidate_p (tree x)
3026 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3027 return 0;
3029 if (TREE_CODE (x) == BLOCK)
3030 return 0;
3032 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
3033 (lvalue_p) crash on TRY/CATCH. */
3034 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3035 return 0;
3037 if (!lvalue_p (x))
3038 return 0;
3040 /* No point to track non-const calls, they will never satisfy
3041 operand_equal_p. */
3042 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3043 return 0;
3045 if (TREE_CODE (x) == STRING_CST)
3046 return 0;
3048 return 1;
3051 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3052 static bool
3053 candidate_equal_p (const_tree x, const_tree y)
3055 return (x == y) || (x && y && operand_equal_p (x, y, 0));
3058 /* Walk the tree X, and record accesses to variables. If X is written by the
3059 parent tree, WRITER is the parent.
3060 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3061 expression or its only operand forces a sequence point, then everything up
3062 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3063 in PNO_SP.
3064 Once we return, we will have emitted warnings if any subexpression before
3065 such a sequence point could be undefined. On a higher level, however, the
3066 sequence point may not be relevant, and we'll merge the two lists.
3068 Example: (b++, a) + b;
3069 The call that processes the COMPOUND_EXPR will store the increment of B
3070 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3071 processes the PLUS_EXPR will need to merge the two lists so that
3072 eventually, all accesses end up on the same list (and we'll warn about the
3073 unordered subexpressions b++ and b.
3075 A note on merging. If we modify the former example so that our expression
3076 becomes
3077 (b++, b) + a
3078 care must be taken not simply to add all three expressions into the final
3079 PNO_SP list. The function merge_tlist takes care of that by merging the
3080 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3081 way, so that no more than one access to B is recorded. */
3083 static void
3084 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3085 tree writer)
3087 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3088 enum tree_code code;
3089 enum tree_code_class cl;
3091 /* X may be NULL if it is the operand of an empty statement expression
3092 ({ }). */
3093 if (x == NULL)
3094 return;
3096 restart:
3097 code = TREE_CODE (x);
3098 cl = TREE_CODE_CLASS (code);
3100 if (warning_candidate_p (x))
3101 *pno_sp = new_tlist (*pno_sp, x, writer);
3103 switch (code)
3105 case CONSTRUCTOR:
3106 case SIZEOF_EXPR:
3107 return;
3109 case COMPOUND_EXPR:
3110 case TRUTH_ANDIF_EXPR:
3111 case TRUTH_ORIF_EXPR:
3112 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
3113 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3114 warn_for_collisions (tmp_nosp);
3115 merge_tlist (pbefore_sp, tmp_before, 0);
3116 merge_tlist (pbefore_sp, tmp_nosp, 0);
3117 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
3118 warn_for_collisions (tmp_list2);
3119 merge_tlist (pbefore_sp, tmp_list3, 0);
3120 merge_tlist (pno_sp, tmp_list2, 0);
3121 return;
3123 case COND_EXPR:
3124 tmp_before = tmp_list2 = 0;
3125 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3126 warn_for_collisions (tmp_list2);
3127 merge_tlist (pbefore_sp, tmp_before, 0);
3128 merge_tlist (pbefore_sp, tmp_list2, 0);
3130 tmp_list3 = tmp_nosp = 0;
3131 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3132 warn_for_collisions (tmp_nosp);
3133 merge_tlist (pbefore_sp, tmp_list3, 0);
3135 tmp_list3 = tmp_list2 = 0;
3136 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3137 warn_for_collisions (tmp_list2);
3138 merge_tlist (pbefore_sp, tmp_list3, 0);
3139 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3140 two first, to avoid warning for (a ? b++ : b++). */
3141 merge_tlist (&tmp_nosp, tmp_list2, 0);
3142 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3143 return;
3145 case PREDECREMENT_EXPR:
3146 case PREINCREMENT_EXPR:
3147 case POSTDECREMENT_EXPR:
3148 case POSTINCREMENT_EXPR:
3149 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3150 return;
3152 case MODIFY_EXPR:
3153 tmp_before = tmp_nosp = tmp_list3 = 0;
3154 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3155 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3156 /* Expressions inside the LHS are not ordered wrt. the sequence points
3157 in the RHS. Example:
3158 *a = (a++, 2)
3159 Despite the fact that the modification of "a" is in the before_sp
3160 list (tmp_before), it conflicts with the use of "a" in the LHS.
3161 We can handle this by adding the contents of tmp_list3
3162 to those of tmp_before, and redoing the collision warnings for that
3163 list. */
3164 add_tlist (&tmp_before, tmp_list3, x, 1);
3165 warn_for_collisions (tmp_before);
3166 /* Exclude the LHS itself here; we first have to merge it into the
3167 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3168 didn't exclude the LHS, we'd get it twice, once as a read and once
3169 as a write. */
3170 add_tlist (pno_sp, tmp_list3, x, 0);
3171 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3173 merge_tlist (pbefore_sp, tmp_before, 0);
3174 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3175 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3176 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3177 return;
3179 case CALL_EXPR:
3180 /* We need to warn about conflicts among arguments and conflicts between
3181 args and the function address. Side effects of the function address,
3182 however, are not ordered by the sequence point of the call. */
3184 call_expr_arg_iterator iter;
3185 tree arg;
3186 tmp_before = tmp_nosp = 0;
3187 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3188 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3190 tmp_list2 = tmp_list3 = 0;
3191 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3192 merge_tlist (&tmp_list3, tmp_list2, 0);
3193 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3195 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3196 warn_for_collisions (tmp_before);
3197 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3198 return;
3201 case TREE_LIST:
3202 /* Scan all the list, e.g. indices of multi dimensional array. */
3203 while (x)
3205 tmp_before = tmp_nosp = 0;
3206 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3207 merge_tlist (&tmp_nosp, tmp_before, 0);
3208 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3209 x = TREE_CHAIN (x);
3211 return;
3213 case SAVE_EXPR:
3215 struct tlist_cache *t;
3216 for (t = save_expr_cache; t; t = t->next)
3217 if (candidate_equal_p (t->expr, x))
3218 break;
3220 if (!t)
3222 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3223 t->next = save_expr_cache;
3224 t->expr = x;
3225 save_expr_cache = t;
3227 tmp_before = tmp_nosp = 0;
3228 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3229 warn_for_collisions (tmp_nosp);
3231 tmp_list3 = 0;
3232 merge_tlist (&tmp_list3, tmp_nosp, 0);
3233 t->cache_before_sp = tmp_before;
3234 t->cache_after_sp = tmp_list3;
3236 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3237 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3238 return;
3241 case ADDR_EXPR:
3242 x = TREE_OPERAND (x, 0);
3243 if (DECL_P (x))
3244 return;
3245 writer = 0;
3246 goto restart;
3248 default:
3249 /* For other expressions, simply recurse on their operands.
3250 Manual tail recursion for unary expressions.
3251 Other non-expressions need not be processed. */
3252 if (cl == tcc_unary)
3254 x = TREE_OPERAND (x, 0);
3255 writer = 0;
3256 goto restart;
3258 else if (IS_EXPR_CODE_CLASS (cl))
3260 int lp;
3261 int max = TREE_OPERAND_LENGTH (x);
3262 for (lp = 0; lp < max; lp++)
3264 tmp_before = tmp_nosp = 0;
3265 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3266 merge_tlist (&tmp_nosp, tmp_before, 0);
3267 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3270 return;
3274 /* Try to warn for undefined behavior in EXPR due to missing sequence
3275 points. */
3277 DEBUG_FUNCTION void
3278 verify_sequence_points (tree expr)
3280 struct tlist *before_sp = 0, *after_sp = 0;
3282 warned_ids = 0;
3283 save_expr_cache = 0;
3284 if (tlist_firstobj == 0)
3286 gcc_obstack_init (&tlist_obstack);
3287 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3290 verify_tree (expr, &before_sp, &after_sp, 0);
3291 warn_for_collisions (after_sp);
3292 obstack_free (&tlist_obstack, tlist_firstobj);
3295 /* Validate the expression after `case' and apply default promotions. */
3297 static tree
3298 check_case_value (location_t loc, tree value)
3300 if (value == NULL_TREE)
3301 return value;
3303 if (TREE_CODE (value) == INTEGER_CST)
3304 /* Promote char or short to int. */
3305 value = perform_integral_promotions (value);
3306 else if (value != error_mark_node)
3308 error_at (loc, "case label does not reduce to an integer constant");
3309 value = error_mark_node;
3312 constant_expression_warning (value);
3314 return value;
3317 /* See if the case values LOW and HIGH are in the range of the original
3318 type (i.e. before the default conversion to int) of the switch testing
3319 expression.
3320 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3321 the type before promoting it. CASE_LOW_P is a pointer to the lower
3322 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3323 if the case is not a case range.
3324 The caller has to make sure that we are not called with NULL for
3325 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3326 was a case value that doesn't fit into the range of the ORIG_TYPE.
3327 Returns true if the case label is in range of ORIG_TYPE (saturated or
3328 untouched) or false if the label is out of range. */
3330 static bool
3331 check_case_bounds (location_t loc, tree type, tree orig_type,
3332 tree *case_low_p, tree *case_high_p,
3333 bool *outside_range_p)
3335 tree min_value, max_value;
3336 tree case_low = *case_low_p;
3337 tree case_high = case_high_p ? *case_high_p : case_low;
3339 /* If there was a problem with the original type, do nothing. */
3340 if (orig_type == error_mark_node)
3341 return true;
3343 min_value = TYPE_MIN_VALUE (orig_type);
3344 max_value = TYPE_MAX_VALUE (orig_type);
3346 /* We'll really need integer constants here. */
3347 case_low = fold (case_low);
3348 case_high = fold (case_high);
3350 /* Case label is less than minimum for type. */
3351 if (tree_int_cst_compare (case_low, min_value) < 0
3352 && tree_int_cst_compare (case_high, min_value) < 0)
3354 warning_at (loc, 0, "case label value is less than minimum value "
3355 "for type");
3356 *outside_range_p = true;
3357 return false;
3360 /* Case value is greater than maximum for type. */
3361 if (tree_int_cst_compare (case_low, max_value) > 0
3362 && tree_int_cst_compare (case_high, max_value) > 0)
3364 warning_at (loc, 0, "case label value exceeds maximum value for type");
3365 *outside_range_p = true;
3366 return false;
3369 /* Saturate lower case label value to minimum. */
3370 if (tree_int_cst_compare (case_high, min_value) >= 0
3371 && tree_int_cst_compare (case_low, min_value) < 0)
3373 warning_at (loc, 0, "lower value in case label range"
3374 " less than minimum value for type");
3375 *outside_range_p = true;
3376 case_low = min_value;
3379 /* Saturate upper case label value to maximum. */
3380 if (tree_int_cst_compare (case_low, max_value) <= 0
3381 && tree_int_cst_compare (case_high, max_value) > 0)
3383 warning_at (loc, 0, "upper value in case label range"
3384 " exceeds maximum value for type");
3385 *outside_range_p = true;
3386 case_high = max_value;
3389 if (*case_low_p != case_low)
3390 *case_low_p = convert (type, case_low);
3391 if (case_high_p && *case_high_p != case_high)
3392 *case_high_p = convert (type, case_high);
3394 return true;
3397 /* Return an integer type with BITS bits of precision,
3398 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3400 tree
3401 c_common_type_for_size (unsigned int bits, int unsignedp)
3403 int i;
3405 if (bits == TYPE_PRECISION (integer_type_node))
3406 return unsignedp ? unsigned_type_node : integer_type_node;
3408 if (bits == TYPE_PRECISION (signed_char_type_node))
3409 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3411 if (bits == TYPE_PRECISION (short_integer_type_node))
3412 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3414 if (bits == TYPE_PRECISION (long_integer_type_node))
3415 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3417 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3418 return (unsignedp ? long_long_unsigned_type_node
3419 : long_long_integer_type_node);
3421 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3422 if (int_n_enabled_p[i]
3423 && bits == int_n_data[i].bitsize)
3424 return (unsignedp ? int_n_trees[i].unsigned_type
3425 : int_n_trees[i].signed_type);
3427 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3428 return (unsignedp ? widest_unsigned_literal_type_node
3429 : widest_integer_literal_type_node);
3431 if (bits <= TYPE_PRECISION (intQI_type_node))
3432 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3434 if (bits <= TYPE_PRECISION (intHI_type_node))
3435 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3437 if (bits <= TYPE_PRECISION (intSI_type_node))
3438 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3440 if (bits <= TYPE_PRECISION (intDI_type_node))
3441 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3443 return 0;
3446 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3447 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3448 and saturating if SATP is nonzero, otherwise not saturating. */
3450 tree
3451 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3452 int unsignedp, int satp)
3454 machine_mode mode;
3455 if (ibit == 0)
3456 mode = unsignedp ? UQQmode : QQmode;
3457 else
3458 mode = unsignedp ? UHAmode : HAmode;
3460 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3461 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3462 break;
3464 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3466 sorry ("GCC cannot support operators with integer types and "
3467 "fixed-point types that have too many integral and "
3468 "fractional bits together");
3469 return 0;
3472 return c_common_type_for_mode (mode, satp);
3475 /* Used for communication between c_common_type_for_mode and
3476 c_register_builtin_type. */
3477 tree registered_builtin_types;
3479 /* Return a data type that has machine mode MODE.
3480 If the mode is an integer,
3481 then UNSIGNEDP selects between signed and unsigned types.
3482 If the mode is a fixed-point mode,
3483 then UNSIGNEDP selects between saturating and nonsaturating types. */
3485 tree
3486 c_common_type_for_mode (machine_mode mode, int unsignedp)
3488 tree t;
3489 int i;
3491 if (mode == TYPE_MODE (integer_type_node))
3492 return unsignedp ? unsigned_type_node : integer_type_node;
3494 if (mode == TYPE_MODE (signed_char_type_node))
3495 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3497 if (mode == TYPE_MODE (short_integer_type_node))
3498 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3500 if (mode == TYPE_MODE (long_integer_type_node))
3501 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3503 if (mode == TYPE_MODE (long_long_integer_type_node))
3504 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3506 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3507 if (int_n_enabled_p[i]
3508 && mode == int_n_data[i].m)
3509 return (unsignedp ? int_n_trees[i].unsigned_type
3510 : int_n_trees[i].signed_type);
3512 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3513 return unsignedp ? widest_unsigned_literal_type_node
3514 : widest_integer_literal_type_node;
3516 if (mode == QImode)
3517 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3519 if (mode == HImode)
3520 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3522 if (mode == SImode)
3523 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3525 if (mode == DImode)
3526 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3528 #if HOST_BITS_PER_WIDE_INT >= 64
3529 if (mode == TYPE_MODE (intTI_type_node))
3530 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3531 #endif
3533 if (mode == TYPE_MODE (float_type_node))
3534 return float_type_node;
3536 if (mode == TYPE_MODE (double_type_node))
3537 return double_type_node;
3539 if (mode == TYPE_MODE (long_double_type_node))
3540 return long_double_type_node;
3542 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3543 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
3544 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
3545 return FLOATN_NX_TYPE_NODE (i);
3547 if (mode == TYPE_MODE (void_type_node))
3548 return void_type_node;
3550 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3551 return (unsignedp
3552 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3553 : make_signed_type (GET_MODE_PRECISION (mode)));
3555 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3556 return (unsignedp
3557 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3558 : make_signed_type (GET_MODE_PRECISION (mode)));
3560 if (COMPLEX_MODE_P (mode))
3562 machine_mode inner_mode;
3563 tree inner_type;
3565 if (mode == TYPE_MODE (complex_float_type_node))
3566 return complex_float_type_node;
3567 if (mode == TYPE_MODE (complex_double_type_node))
3568 return complex_double_type_node;
3569 if (mode == TYPE_MODE (complex_long_double_type_node))
3570 return complex_long_double_type_node;
3572 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3573 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
3574 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
3575 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
3577 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3578 return complex_integer_type_node;
3580 inner_mode = GET_MODE_INNER (mode);
3581 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3582 if (inner_type != NULL_TREE)
3583 return build_complex_type (inner_type);
3585 else if (VECTOR_MODE_P (mode))
3587 machine_mode inner_mode = GET_MODE_INNER (mode);
3588 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3589 if (inner_type != NULL_TREE)
3590 return build_vector_type_for_mode (inner_type, mode);
3593 if (mode == TYPE_MODE (dfloat32_type_node))
3594 return dfloat32_type_node;
3595 if (mode == TYPE_MODE (dfloat64_type_node))
3596 return dfloat64_type_node;
3597 if (mode == TYPE_MODE (dfloat128_type_node))
3598 return dfloat128_type_node;
3600 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3602 if (mode == TYPE_MODE (short_fract_type_node))
3603 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3604 if (mode == TYPE_MODE (fract_type_node))
3605 return unsignedp ? sat_fract_type_node : fract_type_node;
3606 if (mode == TYPE_MODE (long_fract_type_node))
3607 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3608 if (mode == TYPE_MODE (long_long_fract_type_node))
3609 return unsignedp ? sat_long_long_fract_type_node
3610 : long_long_fract_type_node;
3612 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3613 return unsignedp ? sat_unsigned_short_fract_type_node
3614 : unsigned_short_fract_type_node;
3615 if (mode == TYPE_MODE (unsigned_fract_type_node))
3616 return unsignedp ? sat_unsigned_fract_type_node
3617 : unsigned_fract_type_node;
3618 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3619 return unsignedp ? sat_unsigned_long_fract_type_node
3620 : unsigned_long_fract_type_node;
3621 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3622 return unsignedp ? sat_unsigned_long_long_fract_type_node
3623 : unsigned_long_long_fract_type_node;
3625 if (mode == TYPE_MODE (short_accum_type_node))
3626 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3627 if (mode == TYPE_MODE (accum_type_node))
3628 return unsignedp ? sat_accum_type_node : accum_type_node;
3629 if (mode == TYPE_MODE (long_accum_type_node))
3630 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3631 if (mode == TYPE_MODE (long_long_accum_type_node))
3632 return unsignedp ? sat_long_long_accum_type_node
3633 : long_long_accum_type_node;
3635 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3636 return unsignedp ? sat_unsigned_short_accum_type_node
3637 : unsigned_short_accum_type_node;
3638 if (mode == TYPE_MODE (unsigned_accum_type_node))
3639 return unsignedp ? sat_unsigned_accum_type_node
3640 : unsigned_accum_type_node;
3641 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3642 return unsignedp ? sat_unsigned_long_accum_type_node
3643 : unsigned_long_accum_type_node;
3644 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3645 return unsignedp ? sat_unsigned_long_long_accum_type_node
3646 : unsigned_long_long_accum_type_node;
3648 if (mode == QQmode)
3649 return unsignedp ? sat_qq_type_node : qq_type_node;
3650 if (mode == HQmode)
3651 return unsignedp ? sat_hq_type_node : hq_type_node;
3652 if (mode == SQmode)
3653 return unsignedp ? sat_sq_type_node : sq_type_node;
3654 if (mode == DQmode)
3655 return unsignedp ? sat_dq_type_node : dq_type_node;
3656 if (mode == TQmode)
3657 return unsignedp ? sat_tq_type_node : tq_type_node;
3659 if (mode == UQQmode)
3660 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3661 if (mode == UHQmode)
3662 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3663 if (mode == USQmode)
3664 return unsignedp ? sat_usq_type_node : usq_type_node;
3665 if (mode == UDQmode)
3666 return unsignedp ? sat_udq_type_node : udq_type_node;
3667 if (mode == UTQmode)
3668 return unsignedp ? sat_utq_type_node : utq_type_node;
3670 if (mode == HAmode)
3671 return unsignedp ? sat_ha_type_node : ha_type_node;
3672 if (mode == SAmode)
3673 return unsignedp ? sat_sa_type_node : sa_type_node;
3674 if (mode == DAmode)
3675 return unsignedp ? sat_da_type_node : da_type_node;
3676 if (mode == TAmode)
3677 return unsignedp ? sat_ta_type_node : ta_type_node;
3679 if (mode == UHAmode)
3680 return unsignedp ? sat_uha_type_node : uha_type_node;
3681 if (mode == USAmode)
3682 return unsignedp ? sat_usa_type_node : usa_type_node;
3683 if (mode == UDAmode)
3684 return unsignedp ? sat_uda_type_node : uda_type_node;
3685 if (mode == UTAmode)
3686 return unsignedp ? sat_uta_type_node : uta_type_node;
3689 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3690 if (TYPE_MODE (TREE_VALUE (t)) == mode
3691 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3692 return TREE_VALUE (t);
3694 return 0;
3697 tree
3698 c_common_unsigned_type (tree type)
3700 return c_common_signed_or_unsigned_type (1, type);
3703 /* Return a signed type the same as TYPE in other respects. */
3705 tree
3706 c_common_signed_type (tree type)
3708 return c_common_signed_or_unsigned_type (0, type);
3711 /* Return a type the same as TYPE except unsigned or
3712 signed according to UNSIGNEDP. */
3714 tree
3715 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3717 tree type1;
3718 int i;
3720 /* This block of code emulates the behavior of the old
3721 c_common_unsigned_type. In particular, it returns
3722 long_unsigned_type_node if passed a long, even when a int would
3723 have the same size. This is necessary for warnings to work
3724 correctly in archs where sizeof(int) == sizeof(long) */
3726 type1 = TYPE_MAIN_VARIANT (type);
3727 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3728 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3729 if (type1 == integer_type_node || type1 == unsigned_type_node)
3730 return unsignedp ? unsigned_type_node : integer_type_node;
3731 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3732 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3733 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3734 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3735 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3736 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3738 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3739 if (int_n_enabled_p[i]
3740 && (type1 == int_n_trees[i].unsigned_type
3741 || type1 == int_n_trees[i].signed_type))
3742 return (unsignedp ? int_n_trees[i].unsigned_type
3743 : int_n_trees[i].signed_type);
3745 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3746 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3747 #if HOST_BITS_PER_WIDE_INT >= 64
3748 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3749 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3750 #endif
3751 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3752 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3753 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3754 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3755 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3756 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3757 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3758 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3760 #define C_COMMON_FIXED_TYPES(NAME) \
3761 if (type1 == short_ ## NAME ## _type_node \
3762 || type1 == unsigned_short_ ## NAME ## _type_node) \
3763 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3764 : short_ ## NAME ## _type_node; \
3765 if (type1 == NAME ## _type_node \
3766 || type1 == unsigned_ ## NAME ## _type_node) \
3767 return unsignedp ? unsigned_ ## NAME ## _type_node \
3768 : NAME ## _type_node; \
3769 if (type1 == long_ ## NAME ## _type_node \
3770 || type1 == unsigned_long_ ## NAME ## _type_node) \
3771 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3772 : long_ ## NAME ## _type_node; \
3773 if (type1 == long_long_ ## NAME ## _type_node \
3774 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3775 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3776 : long_long_ ## NAME ## _type_node;
3778 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3779 if (type1 == NAME ## _type_node \
3780 || type1 == u ## NAME ## _type_node) \
3781 return unsignedp ? u ## NAME ## _type_node \
3782 : NAME ## _type_node;
3784 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3785 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3786 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3787 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3788 : sat_ ## short_ ## NAME ## _type_node; \
3789 if (type1 == sat_ ## NAME ## _type_node \
3790 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3791 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3792 : sat_ ## NAME ## _type_node; \
3793 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3794 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3795 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3796 : sat_ ## long_ ## NAME ## _type_node; \
3797 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3798 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3799 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3800 : sat_ ## long_long_ ## NAME ## _type_node;
3802 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3803 if (type1 == sat_ ## NAME ## _type_node \
3804 || type1 == sat_ ## u ## NAME ## _type_node) \
3805 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3806 : sat_ ## NAME ## _type_node;
3808 C_COMMON_FIXED_TYPES (fract);
3809 C_COMMON_FIXED_TYPES_SAT (fract);
3810 C_COMMON_FIXED_TYPES (accum);
3811 C_COMMON_FIXED_TYPES_SAT (accum);
3813 C_COMMON_FIXED_MODE_TYPES (qq);
3814 C_COMMON_FIXED_MODE_TYPES (hq);
3815 C_COMMON_FIXED_MODE_TYPES (sq);
3816 C_COMMON_FIXED_MODE_TYPES (dq);
3817 C_COMMON_FIXED_MODE_TYPES (tq);
3818 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3819 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3820 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3821 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3822 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3823 C_COMMON_FIXED_MODE_TYPES (ha);
3824 C_COMMON_FIXED_MODE_TYPES (sa);
3825 C_COMMON_FIXED_MODE_TYPES (da);
3826 C_COMMON_FIXED_MODE_TYPES (ta);
3827 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3828 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3829 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3830 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3832 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3833 the precision; they have precision set to match their range, but
3834 may use a wider mode to match an ABI. If we change modes, we may
3835 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3836 the precision as well, so as to yield correct results for
3837 bit-field types. C++ does not have these separate bit-field
3838 types, and producing a signed or unsigned variant of an
3839 ENUMERAL_TYPE may cause other problems as well. */
3841 if (!INTEGRAL_TYPE_P (type)
3842 || TYPE_UNSIGNED (type) == unsignedp)
3843 return type;
3845 #define TYPE_OK(node) \
3846 (TYPE_MODE (type) == TYPE_MODE (node) \
3847 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3848 if (TYPE_OK (signed_char_type_node))
3849 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3850 if (TYPE_OK (integer_type_node))
3851 return unsignedp ? unsigned_type_node : integer_type_node;
3852 if (TYPE_OK (short_integer_type_node))
3853 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3854 if (TYPE_OK (long_integer_type_node))
3855 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3856 if (TYPE_OK (long_long_integer_type_node))
3857 return (unsignedp ? long_long_unsigned_type_node
3858 : long_long_integer_type_node);
3860 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3861 if (int_n_enabled_p[i]
3862 && TYPE_MODE (type) == int_n_data[i].m
3863 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3864 return (unsignedp ? int_n_trees[i].unsigned_type
3865 : int_n_trees[i].signed_type);
3867 if (TYPE_OK (widest_integer_literal_type_node))
3868 return (unsignedp ? widest_unsigned_literal_type_node
3869 : widest_integer_literal_type_node);
3871 #if HOST_BITS_PER_WIDE_INT >= 64
3872 if (TYPE_OK (intTI_type_node))
3873 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3874 #endif
3875 if (TYPE_OK (intDI_type_node))
3876 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3877 if (TYPE_OK (intSI_type_node))
3878 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3879 if (TYPE_OK (intHI_type_node))
3880 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3881 if (TYPE_OK (intQI_type_node))
3882 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3883 #undef TYPE_OK
3885 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3888 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3890 tree
3891 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3893 int i;
3895 /* Extended integer types of the same width as a standard type have
3896 lesser rank, so those of the same width as int promote to int or
3897 unsigned int and are valid for printf formats expecting int or
3898 unsigned int. To avoid such special cases, avoid creating
3899 extended integer types for bit-fields if a standard integer type
3900 is available. */
3901 if (width == TYPE_PRECISION (integer_type_node))
3902 return unsignedp ? unsigned_type_node : integer_type_node;
3903 if (width == TYPE_PRECISION (signed_char_type_node))
3904 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3905 if (width == TYPE_PRECISION (short_integer_type_node))
3906 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3907 if (width == TYPE_PRECISION (long_integer_type_node))
3908 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3909 if (width == TYPE_PRECISION (long_long_integer_type_node))
3910 return (unsignedp ? long_long_unsigned_type_node
3911 : long_long_integer_type_node);
3912 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3913 if (int_n_enabled_p[i]
3914 && width == int_n_data[i].bitsize)
3915 return (unsignedp ? int_n_trees[i].unsigned_type
3916 : int_n_trees[i].signed_type);
3917 return build_nonstandard_integer_type (width, unsignedp);
3920 /* The C version of the register_builtin_type langhook. */
3922 void
3923 c_register_builtin_type (tree type, const char* name)
3925 tree decl;
3927 decl = build_decl (UNKNOWN_LOCATION,
3928 TYPE_DECL, get_identifier (name), type);
3929 DECL_ARTIFICIAL (decl) = 1;
3930 if (!TYPE_NAME (type))
3931 TYPE_NAME (type) = decl;
3932 pushdecl (decl);
3934 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3937 /* Print an error message for invalid operands to arith operation
3938 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3939 RICHLOC is a rich location for the message, containing either
3940 three separate locations for each of the operator and operands
3942 lhs op rhs
3943 ~~~ ^~ ~~~
3945 (C FE), or one location ranging over all over them
3947 lhs op rhs
3948 ~~~~^~~~~~
3950 (C++ FE). */
3952 void
3953 binary_op_error (rich_location *richloc, enum tree_code code,
3954 tree type0, tree type1)
3956 const char *opname;
3958 switch (code)
3960 case PLUS_EXPR:
3961 opname = "+"; break;
3962 case MINUS_EXPR:
3963 opname = "-"; break;
3964 case MULT_EXPR:
3965 opname = "*"; break;
3966 case MAX_EXPR:
3967 opname = "max"; break;
3968 case MIN_EXPR:
3969 opname = "min"; break;
3970 case EQ_EXPR:
3971 opname = "=="; break;
3972 case NE_EXPR:
3973 opname = "!="; break;
3974 case LE_EXPR:
3975 opname = "<="; break;
3976 case GE_EXPR:
3977 opname = ">="; break;
3978 case LT_EXPR:
3979 opname = "<"; break;
3980 case GT_EXPR:
3981 opname = ">"; break;
3982 case LSHIFT_EXPR:
3983 opname = "<<"; break;
3984 case RSHIFT_EXPR:
3985 opname = ">>"; break;
3986 case TRUNC_MOD_EXPR:
3987 case FLOOR_MOD_EXPR:
3988 opname = "%"; break;
3989 case TRUNC_DIV_EXPR:
3990 case FLOOR_DIV_EXPR:
3991 opname = "/"; break;
3992 case BIT_AND_EXPR:
3993 opname = "&"; break;
3994 case BIT_IOR_EXPR:
3995 opname = "|"; break;
3996 case TRUTH_ANDIF_EXPR:
3997 opname = "&&"; break;
3998 case TRUTH_ORIF_EXPR:
3999 opname = "||"; break;
4000 case BIT_XOR_EXPR:
4001 opname = "^"; break;
4002 default:
4003 gcc_unreachable ();
4005 error_at_rich_loc (richloc,
4006 "invalid operands to binary %s (have %qT and %qT)",
4007 opname, type0, type1);
4010 /* Given an expression as a tree, return its original type. Do this
4011 by stripping any conversion that preserves the sign and precision. */
4012 static tree
4013 expr_original_type (tree expr)
4015 STRIP_SIGN_NOPS (expr);
4016 return TREE_TYPE (expr);
4019 /* Subroutine of build_binary_op, used for comparison operations.
4020 See if the operands have both been converted from subword integer types
4021 and, if so, perhaps change them both back to their original type.
4022 This function is also responsible for converting the two operands
4023 to the proper common type for comparison.
4025 The arguments of this function are all pointers to local variables
4026 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4027 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4029 LOC is the location of the comparison.
4031 If this function returns nonzero, it means that the comparison has
4032 a constant value. What this function returns is an expression for
4033 that value. */
4035 tree
4036 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4037 tree *restype_ptr, enum tree_code *rescode_ptr)
4039 tree type;
4040 tree op0 = *op0_ptr;
4041 tree op1 = *op1_ptr;
4042 int unsignedp0, unsignedp1;
4043 int real1, real2;
4044 tree primop0, primop1;
4045 enum tree_code code = *rescode_ptr;
4047 /* Throw away any conversions to wider types
4048 already present in the operands. */
4050 primop0 = c_common_get_narrower (op0, &unsignedp0);
4051 primop1 = c_common_get_narrower (op1, &unsignedp1);
4053 /* If primopN is first sign-extended from primopN's precision to opN's
4054 precision, then zero-extended from opN's precision to
4055 *restype_ptr precision, shortenings might be invalid. */
4056 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4057 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4058 && !unsignedp0
4059 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4060 primop0 = op0;
4061 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4062 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4063 && !unsignedp1
4064 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4065 primop1 = op1;
4067 /* Handle the case that OP0 does not *contain* a conversion
4068 but it *requires* conversion to FINAL_TYPE. */
4070 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
4071 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4072 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4073 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4075 /* If one of the operands must be floated, we cannot optimize. */
4076 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4077 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4079 /* If first arg is constant, swap the args (changing operation
4080 so value is preserved), for canonicalization. Don't do this if
4081 the second arg is 0. */
4083 if (TREE_CONSTANT (primop0)
4084 && !integer_zerop (primop1) && !real_zerop (primop1)
4085 && !fixed_zerop (primop1))
4087 std::swap (primop0, primop1);
4088 std::swap (op0, op1);
4089 *op0_ptr = op0;
4090 *op1_ptr = op1;
4091 std::swap (unsignedp0, unsignedp1);
4092 std::swap (real1, real2);
4094 switch (code)
4096 case LT_EXPR:
4097 code = GT_EXPR;
4098 break;
4099 case GT_EXPR:
4100 code = LT_EXPR;
4101 break;
4102 case LE_EXPR:
4103 code = GE_EXPR;
4104 break;
4105 case GE_EXPR:
4106 code = LE_EXPR;
4107 break;
4108 default:
4109 break;
4111 *rescode_ptr = code;
4114 /* If comparing an integer against a constant more bits wide,
4115 maybe we can deduce a value of 1 or 0 independent of the data.
4116 Or else truncate the constant now
4117 rather than extend the variable at run time.
4119 This is only interesting if the constant is the wider arg.
4120 Also, it is not safe if the constant is unsigned and the
4121 variable arg is signed, since in this case the variable
4122 would be sign-extended and then regarded as unsigned.
4123 Our technique fails in this case because the lowest/highest
4124 possible unsigned results don't follow naturally from the
4125 lowest/highest possible values of the variable operand.
4126 For just EQ_EXPR and NE_EXPR there is another technique that
4127 could be used: see if the constant can be faithfully represented
4128 in the other operand's type, by truncating it and reextending it
4129 and see if that preserves the constant's value. */
4131 if (!real1 && !real2
4132 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4133 && TREE_CODE (primop1) == INTEGER_CST
4134 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4136 int min_gt, max_gt, min_lt, max_lt;
4137 tree maxval, minval;
4138 /* 1 if comparison is nominally unsigned. */
4139 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4140 tree val;
4142 type = c_common_signed_or_unsigned_type (unsignedp0,
4143 TREE_TYPE (primop0));
4145 maxval = TYPE_MAX_VALUE (type);
4146 minval = TYPE_MIN_VALUE (type);
4148 if (unsignedp && !unsignedp0)
4149 *restype_ptr = c_common_signed_type (*restype_ptr);
4151 if (TREE_TYPE (primop1) != *restype_ptr)
4153 /* Convert primop1 to target type, but do not introduce
4154 additional overflow. We know primop1 is an int_cst. */
4155 primop1 = force_fit_type (*restype_ptr,
4156 wi::to_wide
4157 (primop1,
4158 TYPE_PRECISION (*restype_ptr)),
4159 0, TREE_OVERFLOW (primop1));
4161 if (type != *restype_ptr)
4163 minval = convert (*restype_ptr, minval);
4164 maxval = convert (*restype_ptr, maxval);
4167 min_gt = tree_int_cst_lt (primop1, minval);
4168 max_gt = tree_int_cst_lt (primop1, maxval);
4169 min_lt = tree_int_cst_lt (minval, primop1);
4170 max_lt = tree_int_cst_lt (maxval, primop1);
4172 val = 0;
4173 /* This used to be a switch, but Genix compiler can't handle that. */
4174 if (code == NE_EXPR)
4176 if (max_lt || min_gt)
4177 val = truthvalue_true_node;
4179 else if (code == EQ_EXPR)
4181 if (max_lt || min_gt)
4182 val = truthvalue_false_node;
4184 else if (code == LT_EXPR)
4186 if (max_lt)
4187 val = truthvalue_true_node;
4188 if (!min_lt)
4189 val = truthvalue_false_node;
4191 else if (code == GT_EXPR)
4193 if (min_gt)
4194 val = truthvalue_true_node;
4195 if (!max_gt)
4196 val = truthvalue_false_node;
4198 else if (code == LE_EXPR)
4200 if (!max_gt)
4201 val = truthvalue_true_node;
4202 if (min_gt)
4203 val = truthvalue_false_node;
4205 else if (code == GE_EXPR)
4207 if (!min_lt)
4208 val = truthvalue_true_node;
4209 if (max_lt)
4210 val = truthvalue_false_node;
4213 /* If primop0 was sign-extended and unsigned comparison specd,
4214 we did a signed comparison above using the signed type bounds.
4215 But the comparison we output must be unsigned.
4217 Also, for inequalities, VAL is no good; but if the signed
4218 comparison had *any* fixed result, it follows that the
4219 unsigned comparison just tests the sign in reverse
4220 (positive values are LE, negative ones GE).
4221 So we can generate an unsigned comparison
4222 against an extreme value of the signed type. */
4224 if (unsignedp && !unsignedp0)
4226 if (val != 0)
4227 switch (code)
4229 case LT_EXPR:
4230 case GE_EXPR:
4231 primop1 = TYPE_MIN_VALUE (type);
4232 val = 0;
4233 break;
4235 case LE_EXPR:
4236 case GT_EXPR:
4237 primop1 = TYPE_MAX_VALUE (type);
4238 val = 0;
4239 break;
4241 default:
4242 break;
4244 type = c_common_unsigned_type (type);
4247 if (TREE_CODE (primop0) != INTEGER_CST
4248 /* Don't warn if it's from a (non-system) macro. */
4249 && !(from_macro_expansion_at
4250 (expansion_point_location_if_in_system_header
4251 (EXPR_LOCATION (primop0)))))
4253 if (val == truthvalue_false_node)
4254 warning_at (loc, OPT_Wtype_limits,
4255 "comparison is always false due to limited range of data type");
4256 if (val == truthvalue_true_node)
4257 warning_at (loc, OPT_Wtype_limits,
4258 "comparison is always true due to limited range of data type");
4261 if (val != 0)
4263 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4264 if (TREE_SIDE_EFFECTS (primop0))
4265 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4266 return val;
4269 /* Value is not predetermined, but do the comparison
4270 in the type of the operand that is not constant.
4271 TYPE is already properly set. */
4274 /* If either arg is decimal float and the other is float, find the
4275 proper common type to use for comparison. */
4276 else if (real1 && real2
4277 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4278 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4279 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4281 /* If either arg is decimal float and the other is float, fail. */
4282 else if (real1 && real2
4283 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4284 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4285 return 0;
4287 else if (real1 && real2
4288 && (TYPE_PRECISION (TREE_TYPE (primop0))
4289 == TYPE_PRECISION (TREE_TYPE (primop1))))
4290 type = TREE_TYPE (primop0);
4292 /* If args' natural types are both narrower than nominal type
4293 and both extend in the same manner, compare them
4294 in the type of the wider arg.
4295 Otherwise must actually extend both to the nominal
4296 common type lest different ways of extending
4297 alter the result.
4298 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4300 else if (unsignedp0 == unsignedp1 && real1 == real2
4301 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4302 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4304 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4305 type = c_common_signed_or_unsigned_type (unsignedp0
4306 || TYPE_UNSIGNED (*restype_ptr),
4307 type);
4308 /* Make sure shorter operand is extended the right way
4309 to match the longer operand. */
4310 primop0
4311 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4312 TREE_TYPE (primop0)),
4313 primop0);
4314 primop1
4315 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4316 TREE_TYPE (primop1)),
4317 primop1);
4319 else
4321 /* Here we must do the comparison on the nominal type
4322 using the args exactly as we received them. */
4323 type = *restype_ptr;
4324 primop0 = op0;
4325 primop1 = op1;
4327 if (!real1 && !real2 && integer_zerop (primop1)
4328 && TYPE_UNSIGNED (*restype_ptr))
4330 tree value = 0;
4331 /* All unsigned values are >= 0, so we warn. However,
4332 if OP0 is a constant that is >= 0, the signedness of
4333 the comparison isn't an issue, so suppress the
4334 warning. */
4335 bool warn =
4336 warn_type_limits && !in_system_header_at (loc)
4337 && !(TREE_CODE (primop0) == INTEGER_CST
4338 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4339 primop0)))
4340 /* Do not warn for enumeration types. */
4341 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4343 switch (code)
4345 case GE_EXPR:
4346 if (warn)
4347 warning_at (loc, OPT_Wtype_limits,
4348 "comparison of unsigned expression >= 0 is always true");
4349 value = truthvalue_true_node;
4350 break;
4352 case LT_EXPR:
4353 if (warn)
4354 warning_at (loc, OPT_Wtype_limits,
4355 "comparison of unsigned expression < 0 is always false");
4356 value = truthvalue_false_node;
4357 break;
4359 default:
4360 break;
4363 if (value != 0)
4365 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4366 if (TREE_SIDE_EFFECTS (primop0))
4367 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4368 primop0, value);
4369 return value;
4374 *op0_ptr = convert (type, primop0);
4375 *op1_ptr = convert (type, primop1);
4377 *restype_ptr = truthvalue_type_node;
4379 return 0;
4382 /* Return a tree for the sum or difference (RESULTCODE says which)
4383 of pointer PTROP and integer INTOP. */
4385 tree
4386 pointer_int_sum (location_t loc, enum tree_code resultcode,
4387 tree ptrop, tree intop, bool complain)
4389 tree size_exp, ret;
4391 /* The result is a pointer of the same type that is being added. */
4392 tree result_type = TREE_TYPE (ptrop);
4394 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4396 if (complain && warn_pointer_arith)
4397 pedwarn (loc, OPT_Wpointer_arith,
4398 "pointer of type %<void *%> used in arithmetic");
4399 else if (!complain)
4400 return error_mark_node;
4401 size_exp = integer_one_node;
4403 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4405 if (complain && warn_pointer_arith)
4406 pedwarn (loc, OPT_Wpointer_arith,
4407 "pointer to a function used in arithmetic");
4408 else if (!complain)
4409 return error_mark_node;
4410 size_exp = integer_one_node;
4412 else
4413 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
4415 /* We are manipulating pointer values, so we don't need to warn
4416 about relying on undefined signed overflow. We disable the
4417 warning here because we use integer types so fold won't know that
4418 they are really pointers. */
4419 fold_defer_overflow_warnings ();
4421 /* If what we are about to multiply by the size of the elements
4422 contains a constant term, apply distributive law
4423 and multiply that constant term separately.
4424 This helps produce common subexpressions. */
4425 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4426 && !TREE_CONSTANT (intop)
4427 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4428 && TREE_CONSTANT (size_exp)
4429 /* If the constant comes from pointer subtraction,
4430 skip this optimization--it would cause an error. */
4431 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4432 /* If the constant is unsigned, and smaller than the pointer size,
4433 then we must skip this optimization. This is because it could cause
4434 an overflow error if the constant is negative but INTOP is not. */
4435 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4436 || (TYPE_PRECISION (TREE_TYPE (intop))
4437 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4439 enum tree_code subcode = resultcode;
4440 tree int_type = TREE_TYPE (intop);
4441 if (TREE_CODE (intop) == MINUS_EXPR)
4442 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4443 /* Convert both subexpression types to the type of intop,
4444 because weird cases involving pointer arithmetic
4445 can result in a sum or difference with different type args. */
4446 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4447 subcode, ptrop,
4448 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4449 intop = convert (int_type, TREE_OPERAND (intop, 0));
4452 /* Convert the integer argument to a type the same size as sizetype
4453 so the multiply won't overflow spuriously. */
4454 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4455 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4456 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4457 TYPE_UNSIGNED (sizetype)), intop);
4459 /* Replace the integer argument with a suitable product by the object size.
4460 Do this multiplication as signed, then convert to the appropriate type
4461 for the pointer operation and disregard an overflow that occurred only
4462 because of the sign-extension change in the latter conversion. */
4464 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4465 convert (TREE_TYPE (intop), size_exp));
4466 intop = convert (sizetype, t);
4467 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4468 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4471 /* Create the sum or difference. */
4472 if (resultcode == MINUS_EXPR)
4473 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4475 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4477 fold_undefer_and_ignore_overflow_warnings ();
4479 return ret;
4482 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4483 and if NON_CONST is known not to be permitted in an evaluated part
4484 of a constant expression. */
4486 tree
4487 c_wrap_maybe_const (tree expr, bool non_const)
4489 bool nowarning = TREE_NO_WARNING (expr);
4490 location_t loc = EXPR_LOCATION (expr);
4492 /* This should never be called for C++. */
4493 if (c_dialect_cxx ())
4494 gcc_unreachable ();
4496 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4497 STRIP_TYPE_NOPS (expr);
4498 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4499 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4500 if (nowarning)
4501 TREE_NO_WARNING (expr) = 1;
4502 protected_set_expr_location (expr, loc);
4504 return expr;
4507 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4508 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4509 around the SAVE_EXPR if needed so that c_fully_fold does not need
4510 to look inside SAVE_EXPRs. */
4512 tree
4513 c_save_expr (tree expr)
4515 bool maybe_const = true;
4516 if (c_dialect_cxx ())
4517 return save_expr (expr);
4518 expr = c_fully_fold (expr, false, &maybe_const);
4519 expr = save_expr (expr);
4520 if (!maybe_const)
4521 expr = c_wrap_maybe_const (expr, true);
4522 return expr;
4525 /* Return whether EXPR is a declaration whose address can never be
4526 NULL. */
4528 bool
4529 decl_with_nonnull_addr_p (const_tree expr)
4531 return (DECL_P (expr)
4532 && (TREE_CODE (expr) == PARM_DECL
4533 || TREE_CODE (expr) == LABEL_DECL
4534 || !DECL_WEAK (expr)));
4537 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4538 or for an `if' or `while' statement or ?..: exp. It should already
4539 have been validated to be of suitable type; otherwise, a bad
4540 diagnostic may result.
4542 The EXPR is located at LOCATION.
4544 This preparation consists of taking the ordinary
4545 representation of an expression expr and producing a valid tree
4546 boolean expression describing whether expr is nonzero. We could
4547 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4548 but we optimize comparisons, &&, ||, and !.
4550 The resulting type should always be `truthvalue_type_node'. */
4552 tree
4553 c_common_truthvalue_conversion (location_t location, tree expr)
4555 switch (TREE_CODE (expr))
4557 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4558 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4559 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4560 case ORDERED_EXPR: case UNORDERED_EXPR:
4561 if (TREE_TYPE (expr) == truthvalue_type_node)
4562 return expr;
4563 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4564 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4565 goto ret;
4567 case TRUTH_ANDIF_EXPR:
4568 case TRUTH_ORIF_EXPR:
4569 case TRUTH_AND_EXPR:
4570 case TRUTH_OR_EXPR:
4571 case TRUTH_XOR_EXPR:
4572 if (TREE_TYPE (expr) == truthvalue_type_node)
4573 return expr;
4574 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4575 c_common_truthvalue_conversion (location,
4576 TREE_OPERAND (expr, 0)),
4577 c_common_truthvalue_conversion (location,
4578 TREE_OPERAND (expr, 1)));
4579 goto ret;
4581 case TRUTH_NOT_EXPR:
4582 if (TREE_TYPE (expr) == truthvalue_type_node)
4583 return expr;
4584 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4585 c_common_truthvalue_conversion (location,
4586 TREE_OPERAND (expr, 0)));
4587 goto ret;
4589 case ERROR_MARK:
4590 return expr;
4592 case INTEGER_CST:
4593 return integer_zerop (expr) ? truthvalue_false_node
4594 : truthvalue_true_node;
4596 case REAL_CST:
4597 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4598 ? truthvalue_true_node
4599 : truthvalue_false_node;
4601 case FIXED_CST:
4602 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4603 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4604 ? truthvalue_true_node
4605 : truthvalue_false_node;
4607 case FUNCTION_DECL:
4608 expr = build_unary_op (location, ADDR_EXPR, expr, false);
4609 /* Fall through. */
4611 case ADDR_EXPR:
4613 tree inner = TREE_OPERAND (expr, 0);
4614 if (decl_with_nonnull_addr_p (inner))
4616 /* Common Ada/Pascal programmer's mistake. */
4617 warning_at (location,
4618 OPT_Waddress,
4619 "the address of %qD will always evaluate as %<true%>",
4620 inner);
4621 return truthvalue_true_node;
4623 break;
4626 case COMPLEX_EXPR:
4627 expr = build_binary_op (EXPR_LOCATION (expr),
4628 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4629 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4630 c_common_truthvalue_conversion (location,
4631 TREE_OPERAND (expr, 0)),
4632 c_common_truthvalue_conversion (location,
4633 TREE_OPERAND (expr, 1)),
4635 goto ret;
4637 case NEGATE_EXPR:
4638 case ABS_EXPR:
4639 case FLOAT_EXPR:
4640 case EXCESS_PRECISION_EXPR:
4641 /* These don't change whether an object is nonzero or zero. */
4642 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4644 case LROTATE_EXPR:
4645 case RROTATE_EXPR:
4646 /* These don't change whether an object is zero or nonzero, but
4647 we can't ignore them if their second arg has side-effects. */
4648 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4650 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4651 TREE_OPERAND (expr, 1),
4652 c_common_truthvalue_conversion
4653 (location, TREE_OPERAND (expr, 0)));
4654 goto ret;
4656 else
4657 return c_common_truthvalue_conversion (location,
4658 TREE_OPERAND (expr, 0));
4660 case LSHIFT_EXPR:
4661 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
4662 "<< in boolean context, did you mean '<' ?");
4663 break;
4665 case COND_EXPR:
4666 if (warn_int_in_bool_context
4667 && !from_macro_definition_at (EXPR_LOCATION (expr)))
4669 tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
4670 tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
4671 if (TREE_CODE (val1) == INTEGER_CST
4672 && TREE_CODE (val2) == INTEGER_CST
4673 && !integer_zerop (val1)
4674 && !integer_zerop (val2)
4675 && (!integer_onep (val1)
4676 || !integer_onep (val2)))
4677 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
4678 "?: using integer constants in boolean context, "
4679 "the expression will always evaluate to %<true%>");
4680 else if ((TREE_CODE (val1) == INTEGER_CST
4681 && !integer_zerop (val1)
4682 && !integer_onep (val1))
4683 || (TREE_CODE (val2) == INTEGER_CST
4684 && !integer_zerop (val2)
4685 && !integer_onep (val2)))
4686 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
4687 "?: using integer constants in boolean context");
4689 /* Distribute the conversion into the arms of a COND_EXPR. */
4690 if (c_dialect_cxx ())
4692 tree op1 = TREE_OPERAND (expr, 1);
4693 tree op2 = TREE_OPERAND (expr, 2);
4694 int w = warn_int_in_bool_context;
4695 warn_int_in_bool_context = 0;
4696 /* In C++ one of the arms might have void type if it is throw. */
4697 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4698 op1 = c_common_truthvalue_conversion (location, op1);
4699 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4700 op2 = c_common_truthvalue_conversion (location, op2);
4701 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4702 TREE_OPERAND (expr, 0), op1, op2);
4703 warn_int_in_bool_context = w;
4704 goto ret;
4706 else
4708 int w = warn_int_in_bool_context;
4709 warn_int_in_bool_context = 0;
4710 /* Folding will happen later for C. */
4711 expr = build3 (COND_EXPR, truthvalue_type_node,
4712 TREE_OPERAND (expr, 0),
4713 c_common_truthvalue_conversion (location,
4714 TREE_OPERAND (expr, 1)),
4715 c_common_truthvalue_conversion (location,
4716 TREE_OPERAND (expr, 2)));
4717 warn_int_in_bool_context = w;
4718 goto ret;
4721 CASE_CONVERT:
4723 tree totype = TREE_TYPE (expr);
4724 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4726 if (POINTER_TYPE_P (totype)
4727 && !c_inhibit_evaluation_warnings
4728 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4730 tree inner = expr;
4731 STRIP_NOPS (inner);
4733 if (DECL_P (inner))
4734 warning_at (location,
4735 OPT_Waddress,
4736 "the compiler can assume that the address of "
4737 "%qD will always evaluate to %<true%>",
4738 inner);
4741 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4742 since that affects how `default_conversion' will behave. */
4743 if (TREE_CODE (totype) == REFERENCE_TYPE
4744 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4745 break;
4746 /* Don't strip a conversion from C++0x scoped enum, since they
4747 don't implicitly convert to other types. */
4748 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4749 && ENUM_IS_SCOPED (fromtype))
4750 break;
4751 /* If this isn't narrowing the argument, we can ignore it. */
4752 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4753 return c_common_truthvalue_conversion (location,
4754 TREE_OPERAND (expr, 0));
4756 break;
4758 case MODIFY_EXPR:
4759 if (!TREE_NO_WARNING (expr)
4760 && warn_parentheses)
4762 warning_at (location, OPT_Wparentheses,
4763 "suggest parentheses around assignment used as "
4764 "truth value");
4765 TREE_NO_WARNING (expr) = 1;
4767 break;
4769 default:
4770 break;
4773 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4775 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4776 expr = (build_binary_op
4777 (EXPR_LOCATION (expr),
4778 (TREE_SIDE_EFFECTS (expr)
4779 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4780 c_common_truthvalue_conversion
4781 (location,
4782 build_unary_op (location, REALPART_EXPR, t, false)),
4783 c_common_truthvalue_conversion
4784 (location,
4785 build_unary_op (location, IMAGPART_EXPR, t, false)),
4786 0));
4787 goto ret;
4790 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4792 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4793 FCONST0 (TYPE_MODE
4794 (TREE_TYPE (expr))));
4795 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4797 else
4798 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4800 ret:
4801 protected_set_expr_location (expr, location);
4802 return expr;
4805 static void def_builtin_1 (enum built_in_function fncode,
4806 const char *name,
4807 enum built_in_class fnclass,
4808 tree fntype, tree libtype,
4809 bool both_p, bool fallback_p, bool nonansi_p,
4810 tree fnattrs, bool implicit_p);
4813 /* Apply the TYPE_QUALS to the new DECL. */
4815 void
4816 c_apply_type_quals_to_decl (int type_quals, tree decl)
4818 tree type = TREE_TYPE (decl);
4820 if (type == error_mark_node)
4821 return;
4823 if ((type_quals & TYPE_QUAL_CONST)
4824 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4825 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4826 constructor can produce constant init, so rely on cp_finish_decl to
4827 clear TREE_READONLY if the variable has non-constant init. */
4828 TREE_READONLY (decl) = 1;
4829 if (type_quals & TYPE_QUAL_VOLATILE)
4831 TREE_SIDE_EFFECTS (decl) = 1;
4832 TREE_THIS_VOLATILE (decl) = 1;
4834 if (type_quals & TYPE_QUAL_RESTRICT)
4836 while (type && TREE_CODE (type) == ARRAY_TYPE)
4837 /* Allow 'restrict' on arrays of pointers.
4838 FIXME currently we just ignore it. */
4839 type = TREE_TYPE (type);
4840 if (!type
4841 || !POINTER_TYPE_P (type)
4842 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4843 error ("invalid use of %<restrict%>");
4847 struct c_type_hasher : ggc_ptr_hash<tree_node>
4849 static hashval_t hash (tree);
4850 static bool equal (tree, tree);
4853 /* Hash function for the problem of multiple type definitions in
4854 different files. This must hash all types that will compare
4855 equal via comptypes to the same value. In practice it hashes
4856 on some of the simple stuff and leaves the details to comptypes. */
4858 hashval_t
4859 c_type_hasher::hash (tree t)
4861 int n_elements;
4862 int shift, size;
4863 tree t2;
4864 switch (TREE_CODE (t))
4866 /* For pointers, hash on pointee type plus some swizzling. */
4867 case POINTER_TYPE:
4868 return hash (TREE_TYPE (t)) ^ 0x3003003;
4869 /* Hash on number of elements and total size. */
4870 case ENUMERAL_TYPE:
4871 shift = 3;
4872 t2 = TYPE_VALUES (t);
4873 break;
4874 case RECORD_TYPE:
4875 shift = 0;
4876 t2 = TYPE_FIELDS (t);
4877 break;
4878 case QUAL_UNION_TYPE:
4879 shift = 1;
4880 t2 = TYPE_FIELDS (t);
4881 break;
4882 case UNION_TYPE:
4883 shift = 2;
4884 t2 = TYPE_FIELDS (t);
4885 break;
4886 default:
4887 gcc_unreachable ();
4889 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4890 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4891 n_elements = list_length (t2);
4892 /* We might have a VLA here. */
4893 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4894 size = 0;
4895 else
4896 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4897 return ((size << 24) | (n_elements << shift));
4900 bool
4901 c_type_hasher::equal (tree t1, tree t2)
4903 return lang_hooks.types_compatible_p (t1, t2);
4906 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4908 /* Return the typed-based alias set for T, which may be an expression
4909 or a type. Return -1 if we don't do anything special. */
4911 alias_set_type
4912 c_common_get_alias_set (tree t)
4914 /* For VLAs, use the alias set of the element type rather than the
4915 default of alias set 0 for types compared structurally. */
4916 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4918 if (TREE_CODE (t) == ARRAY_TYPE)
4919 return get_alias_set (TREE_TYPE (t));
4920 return -1;
4923 /* That's all the expressions we handle specially. */
4924 if (!TYPE_P (t))
4925 return -1;
4927 /* The C standard guarantees that any object may be accessed via an
4928 lvalue that has character type. */
4929 if (t == char_type_node
4930 || t == signed_char_type_node
4931 || t == unsigned_char_type_node)
4932 return 0;
4934 /* The C standard specifically allows aliasing between signed and
4935 unsigned variants of the same type. We treat the signed
4936 variant as canonical. */
4937 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4939 tree t1 = c_common_signed_type (t);
4941 /* t1 == t can happen for boolean nodes which are always unsigned. */
4942 if (t1 != t)
4943 return get_alias_set (t1);
4946 /* Handle the case of multiple type nodes referring to "the same" type,
4947 which occurs with IMA. These share an alias set. FIXME: Currently only
4948 C90 is handled. (In C99 type compatibility is not transitive, which
4949 complicates things mightily. The alias set splay trees can theoretically
4950 represent this, but insertion is tricky when you consider all the
4951 different orders things might arrive in.) */
4953 if (c_language != clk_c || flag_isoc99)
4954 return -1;
4956 /* Save time if there's only one input file. */
4957 if (num_in_fnames == 1)
4958 return -1;
4960 /* Pointers need special handling if they point to any type that
4961 needs special handling (below). */
4962 if (TREE_CODE (t) == POINTER_TYPE)
4964 tree t2;
4965 /* Find bottom type under any nested POINTERs. */
4966 for (t2 = TREE_TYPE (t);
4967 TREE_CODE (t2) == POINTER_TYPE;
4968 t2 = TREE_TYPE (t2))
4970 if (!RECORD_OR_UNION_TYPE_P (t2)
4971 && TREE_CODE (t2) != ENUMERAL_TYPE)
4972 return -1;
4973 if (TYPE_SIZE (t2) == 0)
4974 return -1;
4976 /* These are the only cases that need special handling. */
4977 if (!RECORD_OR_UNION_TYPE_P (t)
4978 && TREE_CODE (t) != ENUMERAL_TYPE
4979 && TREE_CODE (t) != POINTER_TYPE)
4980 return -1;
4981 /* Undefined? */
4982 if (TYPE_SIZE (t) == 0)
4983 return -1;
4985 /* Look up t in hash table. Only one of the compatible types within each
4986 alias set is recorded in the table. */
4987 if (!type_hash_table)
4988 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4989 tree *slot = type_hash_table->find_slot (t, INSERT);
4990 if (*slot != NULL)
4992 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4993 return TYPE_ALIAS_SET ((tree)*slot);
4995 else
4996 /* Our caller will assign and record (in t) a new alias set; all we need
4997 to do is remember t in the hash table. */
4998 *slot = t;
5000 return -1;
5003 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
5004 the IS_SIZEOF parameter indicates which operator is being applied.
5005 The COMPLAIN flag controls whether we should diagnose possibly
5006 ill-formed constructs or not. LOC is the location of the SIZEOF or
5007 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5008 a type in any context should be returned, rather than the normal
5009 alignment for that type. */
5011 tree
5012 c_sizeof_or_alignof_type (location_t loc,
5013 tree type, bool is_sizeof, bool min_alignof,
5014 int complain)
5016 const char *op_name;
5017 tree value = NULL;
5018 enum tree_code type_code = TREE_CODE (type);
5020 op_name = is_sizeof ? "sizeof" : "__alignof__";
5022 if (type_code == FUNCTION_TYPE)
5024 if (is_sizeof)
5026 if (complain && warn_pointer_arith)
5027 pedwarn (loc, OPT_Wpointer_arith,
5028 "invalid application of %<sizeof%> to a function type");
5029 else if (!complain)
5030 return error_mark_node;
5031 value = size_one_node;
5033 else
5035 if (complain)
5037 if (c_dialect_cxx ())
5038 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
5039 "%<alignof%> applied to a function type");
5040 else
5041 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
5042 "%<_Alignof%> applied to a function type");
5044 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5047 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5049 if (type_code == VOID_TYPE
5050 && complain && warn_pointer_arith)
5051 pedwarn (loc, OPT_Wpointer_arith,
5052 "invalid application of %qs to a void type", op_name);
5053 else if (!complain)
5054 return error_mark_node;
5055 value = size_one_node;
5057 else if (!COMPLETE_TYPE_P (type)
5058 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
5060 if (complain)
5061 error_at (loc, "invalid application of %qs to incomplete type %qT",
5062 op_name, type);
5063 return error_mark_node;
5065 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5066 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5068 if (complain)
5069 error_at (loc, "invalid application of %qs to array type %qT of "
5070 "incomplete element type", op_name, type);
5071 return error_mark_node;
5073 else
5075 if (is_sizeof)
5076 /* Convert in case a char is more than one unit. */
5077 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5078 size_int (TYPE_PRECISION (char_type_node)
5079 / BITS_PER_UNIT));
5080 else if (min_alignof)
5081 value = size_int (min_align_of_type (type));
5082 else
5083 value = size_int (TYPE_ALIGN_UNIT (type));
5086 /* VALUE will have the middle-end integer type sizetype.
5087 However, we should really return a value of type `size_t',
5088 which is just a typedef for an ordinary integer type. */
5089 value = fold_convert_loc (loc, size_type_node, value);
5091 return value;
5094 /* Implement the __alignof keyword: Return the minimum required
5095 alignment of EXPR, measured in bytes. For VAR_DECLs,
5096 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
5097 from an "aligned" __attribute__ specification). LOC is the
5098 location of the ALIGNOF operator. */
5100 tree
5101 c_alignof_expr (location_t loc, tree expr)
5103 tree t;
5105 if (VAR_OR_FUNCTION_DECL_P (expr))
5106 t = size_int (DECL_ALIGN_UNIT (expr));
5108 else if (TREE_CODE (expr) == COMPONENT_REF
5109 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5111 error_at (loc, "%<__alignof%> applied to a bit-field");
5112 t = size_one_node;
5114 else if (TREE_CODE (expr) == COMPONENT_REF
5115 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5116 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5118 else if (INDIRECT_REF_P (expr))
5120 tree t = TREE_OPERAND (expr, 0);
5121 tree best = t;
5122 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5124 while (CONVERT_EXPR_P (t)
5125 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5127 int thisalign;
5129 t = TREE_OPERAND (t, 0);
5130 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5131 if (thisalign > bestalign)
5132 best = t, bestalign = thisalign;
5134 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5136 else
5137 return c_alignof (loc, TREE_TYPE (expr));
5139 return fold_convert_loc (loc, size_type_node, t);
5142 /* Handle C and C++ default attributes. */
5144 enum built_in_attribute
5146 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5147 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5148 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5149 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5150 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5151 #include "builtin-attrs.def"
5152 #undef DEF_ATTR_NULL_TREE
5153 #undef DEF_ATTR_INT
5154 #undef DEF_ATTR_STRING
5155 #undef DEF_ATTR_IDENT
5156 #undef DEF_ATTR_TREE_LIST
5157 ATTR_LAST
5160 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5162 static void c_init_attributes (void);
5164 enum c_builtin_type
5166 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5167 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5168 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5169 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5170 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5171 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5172 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5173 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5174 ARG6) NAME,
5175 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5176 ARG6, ARG7) NAME,
5177 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5178 ARG6, ARG7, ARG8) NAME,
5179 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5180 ARG6, ARG7, ARG8, ARG9) NAME,
5181 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5182 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5183 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5184 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5185 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5186 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5187 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5188 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5189 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5190 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5191 NAME,
5192 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5193 ARG6) NAME,
5194 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5195 ARG6, ARG7) NAME,
5196 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5197 #include "builtin-types.def"
5198 #undef DEF_PRIMITIVE_TYPE
5199 #undef DEF_FUNCTION_TYPE_0
5200 #undef DEF_FUNCTION_TYPE_1
5201 #undef DEF_FUNCTION_TYPE_2
5202 #undef DEF_FUNCTION_TYPE_3
5203 #undef DEF_FUNCTION_TYPE_4
5204 #undef DEF_FUNCTION_TYPE_5
5205 #undef DEF_FUNCTION_TYPE_6
5206 #undef DEF_FUNCTION_TYPE_7
5207 #undef DEF_FUNCTION_TYPE_8
5208 #undef DEF_FUNCTION_TYPE_9
5209 #undef DEF_FUNCTION_TYPE_10
5210 #undef DEF_FUNCTION_TYPE_11
5211 #undef DEF_FUNCTION_TYPE_VAR_0
5212 #undef DEF_FUNCTION_TYPE_VAR_1
5213 #undef DEF_FUNCTION_TYPE_VAR_2
5214 #undef DEF_FUNCTION_TYPE_VAR_3
5215 #undef DEF_FUNCTION_TYPE_VAR_4
5216 #undef DEF_FUNCTION_TYPE_VAR_5
5217 #undef DEF_FUNCTION_TYPE_VAR_6
5218 #undef DEF_FUNCTION_TYPE_VAR_7
5219 #undef DEF_POINTER_TYPE
5220 BT_LAST
5223 typedef enum c_builtin_type builtin_type;
5225 /* A temporary array for c_common_nodes_and_builtins. Used in
5226 communication with def_fn_type. */
5227 static tree builtin_types[(int) BT_LAST + 1];
5229 /* A helper function for c_common_nodes_and_builtins. Build function type
5230 for DEF with return type RET and N arguments. If VAR is true, then the
5231 function should be variadic after those N arguments.
5233 Takes special care not to ICE if any of the types involved are
5234 error_mark_node, which indicates that said type is not in fact available
5235 (see builtin_type_for_size). In which case the function type as a whole
5236 should be error_mark_node. */
5238 static void
5239 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5241 tree t;
5242 tree *args = XALLOCAVEC (tree, n);
5243 va_list list;
5244 int i;
5246 va_start (list, n);
5247 for (i = 0; i < n; ++i)
5249 builtin_type a = (builtin_type) va_arg (list, int);
5250 t = builtin_types[a];
5251 if (t == error_mark_node)
5252 goto egress;
5253 args[i] = t;
5256 t = builtin_types[ret];
5257 if (t == error_mark_node)
5258 goto egress;
5259 if (var)
5260 t = build_varargs_function_type_array (t, n, args);
5261 else
5262 t = build_function_type_array (t, n, args);
5264 egress:
5265 builtin_types[def] = t;
5266 va_end (list);
5269 /* Build builtin functions common to both C and C++ language
5270 frontends. */
5272 static void
5273 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5275 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5276 builtin_types[ENUM] = VALUE;
5277 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5278 def_fn_type (ENUM, RETURN, 0, 0);
5279 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5280 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5281 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5282 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5283 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5284 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5285 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5286 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5287 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5288 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5289 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5290 ARG6) \
5291 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5292 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5293 ARG6, ARG7) \
5294 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5295 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5296 ARG6, ARG7, ARG8) \
5297 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5298 ARG7, ARG8);
5299 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5300 ARG6, ARG7, ARG8, ARG9) \
5301 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5302 ARG7, ARG8, ARG9);
5303 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5304 ARG6, ARG7, ARG8, ARG9, ARG10) \
5305 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5306 ARG7, ARG8, ARG9, ARG10);
5307 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5308 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5309 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5310 ARG7, ARG8, ARG9, ARG10, ARG11);
5311 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5312 def_fn_type (ENUM, RETURN, 1, 0);
5313 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5314 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5315 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5316 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5317 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5318 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5319 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5320 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5321 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5322 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5323 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5324 ARG6) \
5325 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5326 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5327 ARG6, ARG7) \
5328 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5329 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5330 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5332 #include "builtin-types.def"
5334 #undef DEF_PRIMITIVE_TYPE
5335 #undef DEF_FUNCTION_TYPE_0
5336 #undef DEF_FUNCTION_TYPE_1
5337 #undef DEF_FUNCTION_TYPE_2
5338 #undef DEF_FUNCTION_TYPE_3
5339 #undef DEF_FUNCTION_TYPE_4
5340 #undef DEF_FUNCTION_TYPE_5
5341 #undef DEF_FUNCTION_TYPE_6
5342 #undef DEF_FUNCTION_TYPE_7
5343 #undef DEF_FUNCTION_TYPE_8
5344 #undef DEF_FUNCTION_TYPE_9
5345 #undef DEF_FUNCTION_TYPE_10
5346 #undef DEF_FUNCTION_TYPE_11
5347 #undef DEF_FUNCTION_TYPE_VAR_0
5348 #undef DEF_FUNCTION_TYPE_VAR_1
5349 #undef DEF_FUNCTION_TYPE_VAR_2
5350 #undef DEF_FUNCTION_TYPE_VAR_3
5351 #undef DEF_FUNCTION_TYPE_VAR_4
5352 #undef DEF_FUNCTION_TYPE_VAR_5
5353 #undef DEF_FUNCTION_TYPE_VAR_6
5354 #undef DEF_FUNCTION_TYPE_VAR_7
5355 #undef DEF_POINTER_TYPE
5356 builtin_types[(int) BT_LAST] = NULL_TREE;
5358 c_init_attributes ();
5360 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5361 NONANSI_P, ATTRS, IMPLICIT, COND) \
5362 if (NAME && COND) \
5363 def_builtin_1 (ENUM, NAME, CLASS, \
5364 builtin_types[(int) TYPE], \
5365 builtin_types[(int) LIBTYPE], \
5366 BOTH_P, FALLBACK_P, NONANSI_P, \
5367 built_in_attributes[(int) ATTRS], IMPLICIT);
5368 #include "builtins.def"
5370 targetm.init_builtins ();
5372 build_common_builtin_nodes ();
5374 if (flag_cilkplus)
5375 cilk_init_builtins ();
5378 /* Like get_identifier, but avoid warnings about null arguments when
5379 the argument may be NULL for targets where GCC lacks stdint.h type
5380 information. */
5382 static inline tree
5383 c_get_ident (const char *id)
5385 return get_identifier (id);
5388 /* Build tree nodes and builtin functions common to both C and C++ language
5389 frontends. */
5391 void
5392 c_common_nodes_and_builtins (void)
5394 int char16_type_size;
5395 int char32_type_size;
5396 int wchar_type_size;
5397 tree array_domain_type;
5398 tree va_list_ref_type_node;
5399 tree va_list_arg_type_node;
5400 int i;
5402 build_common_tree_nodes (flag_signed_char);
5404 /* Define `int' and `char' first so that dbx will output them first. */
5405 record_builtin_type (RID_INT, NULL, integer_type_node);
5406 record_builtin_type (RID_CHAR, "char", char_type_node);
5408 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5409 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5410 but not C. Are the conditionals here needed? */
5411 if (c_dialect_cxx ())
5412 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5413 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5414 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5415 record_builtin_type (RID_MAX, "long unsigned int",
5416 long_unsigned_type_node);
5418 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5420 char name[25];
5422 sprintf (name, "__int%d", int_n_data[i].bitsize);
5423 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5424 int_n_trees[i].signed_type);
5425 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5426 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5429 if (c_dialect_cxx ())
5430 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5431 record_builtin_type (RID_MAX, "long long int",
5432 long_long_integer_type_node);
5433 record_builtin_type (RID_MAX, "long long unsigned int",
5434 long_long_unsigned_type_node);
5435 if (c_dialect_cxx ())
5436 record_builtin_type (RID_MAX, "long long unsigned",
5437 long_long_unsigned_type_node);
5438 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5439 record_builtin_type (RID_MAX, "short unsigned int",
5440 short_unsigned_type_node);
5441 if (c_dialect_cxx ())
5442 record_builtin_type (RID_MAX, "unsigned short",
5443 short_unsigned_type_node);
5445 /* Define both `signed char' and `unsigned char'. */
5446 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5447 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5449 /* These are types that c_common_type_for_size and
5450 c_common_type_for_mode use. */
5451 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5452 TYPE_DECL, NULL_TREE,
5453 intQI_type_node));
5454 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5455 TYPE_DECL, NULL_TREE,
5456 intHI_type_node));
5457 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5458 TYPE_DECL, NULL_TREE,
5459 intSI_type_node));
5460 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5461 TYPE_DECL, NULL_TREE,
5462 intDI_type_node));
5463 #if HOST_BITS_PER_WIDE_INT >= 64
5464 /* Note that this is different than the __int128 type that's part of
5465 the generic __intN support. */
5466 if (targetm.scalar_mode_supported_p (TImode))
5467 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5468 TYPE_DECL,
5469 get_identifier ("__int128_t"),
5470 intTI_type_node));
5471 #endif
5472 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5473 TYPE_DECL, NULL_TREE,
5474 unsigned_intQI_type_node));
5475 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5476 TYPE_DECL, NULL_TREE,
5477 unsigned_intHI_type_node));
5478 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5479 TYPE_DECL, NULL_TREE,
5480 unsigned_intSI_type_node));
5481 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5482 TYPE_DECL, NULL_TREE,
5483 unsigned_intDI_type_node));
5484 #if HOST_BITS_PER_WIDE_INT >= 64
5485 if (targetm.scalar_mode_supported_p (TImode))
5486 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5487 TYPE_DECL,
5488 get_identifier ("__uint128_t"),
5489 unsigned_intTI_type_node));
5490 #endif
5492 /* Create the widest literal types. */
5493 widest_integer_literal_type_node
5494 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5495 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5496 TYPE_DECL, NULL_TREE,
5497 widest_integer_literal_type_node));
5499 widest_unsigned_literal_type_node
5500 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5501 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5502 TYPE_DECL, NULL_TREE,
5503 widest_unsigned_literal_type_node));
5505 signed_size_type_node = c_common_signed_type (size_type_node);
5507 pid_type_node =
5508 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5510 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5511 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5512 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5514 if (!c_dialect_cxx ())
5515 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
5516 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
5517 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
5518 FLOATN_NX_TYPE_NODE (i));
5520 /* Only supported decimal floating point extension if the target
5521 actually supports underlying modes. */
5522 if (targetm.scalar_mode_supported_p (SDmode)
5523 && targetm.scalar_mode_supported_p (DDmode)
5524 && targetm.scalar_mode_supported_p (TDmode))
5526 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5527 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5528 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5531 if (targetm.fixed_point_supported_p ())
5533 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5534 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5535 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5536 record_builtin_type (RID_MAX, "long long _Fract",
5537 long_long_fract_type_node);
5538 record_builtin_type (RID_MAX, "unsigned short _Fract",
5539 unsigned_short_fract_type_node);
5540 record_builtin_type (RID_MAX, "unsigned _Fract",
5541 unsigned_fract_type_node);
5542 record_builtin_type (RID_MAX, "unsigned long _Fract",
5543 unsigned_long_fract_type_node);
5544 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5545 unsigned_long_long_fract_type_node);
5546 record_builtin_type (RID_MAX, "_Sat short _Fract",
5547 sat_short_fract_type_node);
5548 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5549 record_builtin_type (RID_MAX, "_Sat long _Fract",
5550 sat_long_fract_type_node);
5551 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5552 sat_long_long_fract_type_node);
5553 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5554 sat_unsigned_short_fract_type_node);
5555 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5556 sat_unsigned_fract_type_node);
5557 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5558 sat_unsigned_long_fract_type_node);
5559 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5560 sat_unsigned_long_long_fract_type_node);
5561 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5562 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5563 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5564 record_builtin_type (RID_MAX, "long long _Accum",
5565 long_long_accum_type_node);
5566 record_builtin_type (RID_MAX, "unsigned short _Accum",
5567 unsigned_short_accum_type_node);
5568 record_builtin_type (RID_MAX, "unsigned _Accum",
5569 unsigned_accum_type_node);
5570 record_builtin_type (RID_MAX, "unsigned long _Accum",
5571 unsigned_long_accum_type_node);
5572 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5573 unsigned_long_long_accum_type_node);
5574 record_builtin_type (RID_MAX, "_Sat short _Accum",
5575 sat_short_accum_type_node);
5576 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5577 record_builtin_type (RID_MAX, "_Sat long _Accum",
5578 sat_long_accum_type_node);
5579 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5580 sat_long_long_accum_type_node);
5581 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5582 sat_unsigned_short_accum_type_node);
5583 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5584 sat_unsigned_accum_type_node);
5585 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5586 sat_unsigned_long_accum_type_node);
5587 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5588 sat_unsigned_long_long_accum_type_node);
5592 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5593 TYPE_DECL,
5594 get_identifier ("complex int"),
5595 complex_integer_type_node));
5596 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5597 TYPE_DECL,
5598 get_identifier ("complex float"),
5599 complex_float_type_node));
5600 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5601 TYPE_DECL,
5602 get_identifier ("complex double"),
5603 complex_double_type_node));
5604 lang_hooks.decls.pushdecl
5605 (build_decl (UNKNOWN_LOCATION,
5606 TYPE_DECL, get_identifier ("complex long double"),
5607 complex_long_double_type_node));
5609 if (!c_dialect_cxx ())
5610 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
5611 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
5613 char buf[30];
5614 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
5615 floatn_nx_types[i].extended ? "x" : "");
5616 lang_hooks.decls.pushdecl
5617 (build_decl (UNKNOWN_LOCATION,
5618 TYPE_DECL,
5619 get_identifier (buf),
5620 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
5623 if (c_dialect_cxx ())
5624 /* For C++, make fileptr_type_node a distinct void * type until
5625 FILE type is defined. */
5626 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5628 record_builtin_type (RID_VOID, NULL, void_type_node);
5630 /* Set the TYPE_NAME for any variants that were built before
5631 record_builtin_type gave names to the built-in types. */
5633 tree void_name = TYPE_NAME (void_type_node);
5634 TYPE_NAME (void_type_node) = NULL_TREE;
5635 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5636 = void_name;
5637 TYPE_NAME (void_type_node) = void_name;
5640 void_list_node = build_void_list_node ();
5642 /* Make a type to be the domain of a few array types
5643 whose domains don't really matter.
5644 200 is small enough that it always fits in size_t
5645 and large enough that it can hold most function names for the
5646 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5647 array_domain_type = build_index_type (size_int (200));
5649 /* Make a type for arrays of characters.
5650 With luck nothing will ever really depend on the length of this
5651 array type. */
5652 char_array_type_node
5653 = build_array_type (char_type_node, array_domain_type);
5655 string_type_node = build_pointer_type (char_type_node);
5656 const_string_type_node
5657 = build_pointer_type (build_qualified_type
5658 (char_type_node, TYPE_QUAL_CONST));
5660 /* This is special for C++ so functions can be overloaded. */
5661 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5662 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5663 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5664 underlying_wchar_type_node = wchar_type_node;
5665 if (c_dialect_cxx ())
5667 if (TYPE_UNSIGNED (wchar_type_node))
5668 wchar_type_node = make_unsigned_type (wchar_type_size);
5669 else
5670 wchar_type_node = make_signed_type (wchar_type_size);
5671 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5674 /* This is for wide string constants. */
5675 wchar_array_type_node
5676 = build_array_type (wchar_type_node, array_domain_type);
5678 /* Define 'char16_t'. */
5679 char16_type_node = get_identifier (CHAR16_TYPE);
5680 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5681 char16_type_size = TYPE_PRECISION (char16_type_node);
5682 if (c_dialect_cxx ())
5684 char16_type_node = make_unsigned_type (char16_type_size);
5686 if (cxx_dialect >= cxx11)
5687 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5690 /* This is for UTF-16 string constants. */
5691 char16_array_type_node
5692 = build_array_type (char16_type_node, array_domain_type);
5694 /* Define 'char32_t'. */
5695 char32_type_node = get_identifier (CHAR32_TYPE);
5696 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5697 char32_type_size = TYPE_PRECISION (char32_type_node);
5698 if (c_dialect_cxx ())
5700 char32_type_node = make_unsigned_type (char32_type_size);
5702 if (cxx_dialect >= cxx11)
5703 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5706 /* This is for UTF-32 string constants. */
5707 char32_array_type_node
5708 = build_array_type (char32_type_node, array_domain_type);
5710 wint_type_node =
5711 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5713 intmax_type_node =
5714 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5715 uintmax_type_node =
5716 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5718 if (SIG_ATOMIC_TYPE)
5719 sig_atomic_type_node =
5720 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5721 if (INT8_TYPE)
5722 int8_type_node =
5723 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5724 if (INT16_TYPE)
5725 int16_type_node =
5726 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5727 if (INT32_TYPE)
5728 int32_type_node =
5729 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5730 if (INT64_TYPE)
5731 int64_type_node =
5732 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5733 if (UINT8_TYPE)
5734 uint8_type_node =
5735 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5736 if (UINT16_TYPE)
5737 c_uint16_type_node = uint16_type_node =
5738 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5739 if (UINT32_TYPE)
5740 c_uint32_type_node = uint32_type_node =
5741 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5742 if (UINT64_TYPE)
5743 c_uint64_type_node = uint64_type_node =
5744 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5745 if (INT_LEAST8_TYPE)
5746 int_least8_type_node =
5747 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5748 if (INT_LEAST16_TYPE)
5749 int_least16_type_node =
5750 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5751 if (INT_LEAST32_TYPE)
5752 int_least32_type_node =
5753 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5754 if (INT_LEAST64_TYPE)
5755 int_least64_type_node =
5756 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5757 if (UINT_LEAST8_TYPE)
5758 uint_least8_type_node =
5759 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5760 if (UINT_LEAST16_TYPE)
5761 uint_least16_type_node =
5762 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5763 if (UINT_LEAST32_TYPE)
5764 uint_least32_type_node =
5765 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5766 if (UINT_LEAST64_TYPE)
5767 uint_least64_type_node =
5768 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5769 if (INT_FAST8_TYPE)
5770 int_fast8_type_node =
5771 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5772 if (INT_FAST16_TYPE)
5773 int_fast16_type_node =
5774 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5775 if (INT_FAST32_TYPE)
5776 int_fast32_type_node =
5777 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5778 if (INT_FAST64_TYPE)
5779 int_fast64_type_node =
5780 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5781 if (UINT_FAST8_TYPE)
5782 uint_fast8_type_node =
5783 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5784 if (UINT_FAST16_TYPE)
5785 uint_fast16_type_node =
5786 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5787 if (UINT_FAST32_TYPE)
5788 uint_fast32_type_node =
5789 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5790 if (UINT_FAST64_TYPE)
5791 uint_fast64_type_node =
5792 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5793 if (INTPTR_TYPE)
5794 intptr_type_node =
5795 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5796 if (UINTPTR_TYPE)
5797 uintptr_type_node =
5798 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5800 default_function_type
5801 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5802 ptrdiff_type_node
5803 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5804 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5806 lang_hooks.decls.pushdecl
5807 (build_decl (UNKNOWN_LOCATION,
5808 TYPE_DECL, get_identifier ("__builtin_va_list"),
5809 va_list_type_node));
5810 if (targetm.enum_va_list_p)
5812 int l;
5813 const char *pname;
5814 tree ptype;
5816 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5818 lang_hooks.decls.pushdecl
5819 (build_decl (UNKNOWN_LOCATION,
5820 TYPE_DECL, get_identifier (pname),
5821 ptype));
5826 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5828 va_list_arg_type_node = va_list_ref_type_node =
5829 build_pointer_type (TREE_TYPE (va_list_type_node));
5831 else
5833 va_list_arg_type_node = va_list_type_node;
5834 va_list_ref_type_node = build_reference_type (va_list_type_node);
5837 if (!flag_preprocess_only)
5838 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5840 main_identifier_node = get_identifier ("main");
5842 /* Create the built-in __null node. It is important that this is
5843 not shared. */
5844 null_node = make_int_cst (1, 1);
5845 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5847 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5848 memset (builtin_types, 0, sizeof (builtin_types));
5851 /* The number of named compound-literals generated thus far. */
5852 static GTY(()) int compound_literal_number;
5854 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5856 void
5857 set_compound_literal_name (tree decl)
5859 char *name;
5860 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5861 compound_literal_number);
5862 compound_literal_number++;
5863 DECL_NAME (decl) = get_identifier (name);
5866 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5867 TYPE and operand OP. */
5869 static tree
5870 build_va_arg_1 (location_t loc, tree type, tree op)
5872 tree expr = build1 (VA_ARG_EXPR, type, op);
5873 SET_EXPR_LOCATION (expr, loc);
5874 return expr;
5877 /* Return a VA_ARG_EXPR corresponding to a source-level expression
5878 va_arg (EXPR, TYPE) at source location LOC. */
5880 tree
5881 build_va_arg (location_t loc, tree expr, tree type)
5883 tree va_type = TREE_TYPE (expr);
5884 tree canon_va_type = (va_type == error_mark_node
5885 ? error_mark_node
5886 : targetm.canonical_va_list_type (va_type));
5888 if (va_type == error_mark_node
5889 || canon_va_type == NULL_TREE)
5891 if (canon_va_type == NULL_TREE)
5892 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
5894 /* Let's handle things neutrallly, if expr:
5895 - has undeclared type, or
5896 - is not an va_list type. */
5897 return build_va_arg_1 (loc, type, error_mark_node);
5900 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5902 /* Case 1: Not an array type. */
5904 /* Take the address, to get '&ap'. Note that &ap is not a va_list
5905 type. */
5906 mark_addressable (expr);
5907 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5909 return build_va_arg_1 (loc, type, expr);
5912 /* Case 2: Array type.
5914 Background:
5916 For contrast, let's start with the simple case (case 1). If
5917 canon_va_type is not an array type, but say a char *, then when
5918 passing-by-value a va_list, the type of the va_list param decl is
5919 the same as for another va_list decl (all ap's are char *):
5921 f2_1 (char * ap)
5922 D.1815 = VA_ARG (&ap, 0B, 1);
5923 return D.1815;
5925 f2 (int i)
5926 char * ap.0;
5927 char * ap;
5928 __builtin_va_start (&ap, 0);
5929 ap.0 = ap;
5930 res = f2_1 (ap.0);
5931 __builtin_va_end (&ap);
5932 D.1812 = res;
5933 return D.1812;
5935 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5936 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5937 the same as for another va_list decl (case 2a, struct ap[1]).
5939 f2_1 (struct * ap)
5940 D.1844 = VA_ARG (ap, 0B, 0);
5941 return D.1844;
5943 f2 (int i)
5944 struct ap[1];
5945 __builtin_va_start (&ap, 0);
5946 res = f2_1 (&ap);
5947 __builtin_va_end (&ap);
5948 D.1841 = res;
5949 return D.1841;
5951 Case 2b is different because:
5952 - on the callee side, the parm decl has declared type va_list, but
5953 grokdeclarator changes the type of the parm decl to a pointer to the
5954 array elem type.
5955 - on the caller side, the pass-by-value uses &ap.
5957 We unify these two cases (case 2a: va_list is array type,
5958 case 2b: va_list is pointer to array elem type), by adding '&' for the
5959 array type case, such that we have a pointer to array elem in both
5960 cases. */
5962 if (TREE_CODE (va_type) == ARRAY_TYPE)
5964 /* Case 2a: va_list is array type. */
5966 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5967 elem type. */
5968 mark_addressable (expr);
5969 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5970 expr);
5972 /* Verify that &ap is still recognized as having va_list type. */
5973 tree canon_expr_type
5974 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5975 gcc_assert (canon_expr_type != NULL_TREE);
5977 else
5979 /* Case 2b: va_list is pointer to array elem type. */
5980 gcc_assert (POINTER_TYPE_P (va_type));
5982 /* Comparison as in std_canonical_va_list_type. */
5983 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
5984 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
5986 /* Don't take the address. We've already got '&ap'. */
5990 return build_va_arg_1 (loc, type, expr);
5994 /* Linked list of disabled built-in functions. */
5996 struct disabled_builtin
5998 const char *name;
5999 struct disabled_builtin *next;
6001 static disabled_builtin *disabled_builtins = NULL;
6003 static bool builtin_function_disabled_p (const char *);
6005 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6006 begins with "__builtin_", give an error. */
6008 void
6009 disable_builtin_function (const char *name)
6011 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
6012 error ("cannot disable built-in function %qs", name);
6013 else
6015 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6016 new_disabled_builtin->name = name;
6017 new_disabled_builtin->next = disabled_builtins;
6018 disabled_builtins = new_disabled_builtin;
6023 /* Return true if the built-in function NAME has been disabled, false
6024 otherwise. */
6026 static bool
6027 builtin_function_disabled_p (const char *name)
6029 disabled_builtin *p;
6030 for (p = disabled_builtins; p != NULL; p = p->next)
6032 if (strcmp (name, p->name) == 0)
6033 return true;
6035 return false;
6039 /* Worker for DEF_BUILTIN.
6040 Possibly define a builtin function with one or two names.
6041 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6042 nonansi_p and flag_no_nonansi_builtin. */
6044 static void
6045 def_builtin_1 (enum built_in_function fncode,
6046 const char *name,
6047 enum built_in_class fnclass,
6048 tree fntype, tree libtype,
6049 bool both_p, bool fallback_p, bool nonansi_p,
6050 tree fnattrs, bool implicit_p)
6052 tree decl;
6053 const char *libname;
6055 if (fntype == error_mark_node)
6056 return;
6058 gcc_assert ((!both_p && !fallback_p)
6059 || !strncmp (name, "__builtin_",
6060 strlen ("__builtin_")));
6062 libname = name + strlen ("__builtin_");
6063 decl = add_builtin_function (name, fntype, fncode, fnclass,
6064 (fallback_p ? libname : NULL),
6065 fnattrs);
6067 set_builtin_decl (fncode, decl, implicit_p);
6069 if (both_p
6070 && !flag_no_builtin && !builtin_function_disabled_p (libname)
6071 && !(nonansi_p && flag_no_nonansi_builtin))
6072 add_builtin_function (libname, libtype, fncode, fnclass,
6073 NULL, fnattrs);
6076 /* Nonzero if the type T promotes to int. This is (nearly) the
6077 integral promotions defined in ISO C99 6.3.1.1/2. */
6079 bool
6080 c_promoting_integer_type_p (const_tree t)
6082 switch (TREE_CODE (t))
6084 case INTEGER_TYPE:
6085 return (TYPE_MAIN_VARIANT (t) == char_type_node
6086 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6087 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6088 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
6089 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6090 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
6092 case ENUMERAL_TYPE:
6093 /* ??? Technically all enumerations not larger than an int
6094 promote to an int. But this is used along code paths
6095 that only want to notice a size change. */
6096 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6098 case BOOLEAN_TYPE:
6099 return 1;
6101 default:
6102 return 0;
6106 /* Return 1 if PARMS specifies a fixed number of parameters
6107 and none of their types is affected by default promotions. */
6110 self_promoting_args_p (const_tree parms)
6112 const_tree t;
6113 for (t = parms; t; t = TREE_CHAIN (t))
6115 tree type = TREE_VALUE (t);
6117 if (type == error_mark_node)
6118 continue;
6120 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6121 return 0;
6123 if (type == 0)
6124 return 0;
6126 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6127 return 0;
6129 if (c_promoting_integer_type_p (type))
6130 return 0;
6132 return 1;
6135 /* Recursively remove any '*' or '&' operator from TYPE. */
6136 tree
6137 strip_pointer_operator (tree t)
6139 while (POINTER_TYPE_P (t))
6140 t = TREE_TYPE (t);
6141 return t;
6144 /* Recursively remove pointer or array type from TYPE. */
6145 tree
6146 strip_pointer_or_array_types (tree t)
6148 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6149 t = TREE_TYPE (t);
6150 return t;
6153 /* Used to compare case labels. K1 and K2 are actually tree nodes
6154 representing case labels, or NULL_TREE for a `default' label.
6155 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6156 K2, and 0 if K1 and K2 are equal. */
6159 case_compare (splay_tree_key k1, splay_tree_key k2)
6161 /* Consider a NULL key (such as arises with a `default' label) to be
6162 smaller than anything else. */
6163 if (!k1)
6164 return k2 ? -1 : 0;
6165 else if (!k2)
6166 return k1 ? 1 : 0;
6168 return tree_int_cst_compare ((tree) k1, (tree) k2);
6171 /* Process a case label, located at LOC, for the range LOW_VALUE
6172 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6173 then this case label is actually a `default' label. If only
6174 HIGH_VALUE is NULL_TREE, then case label was declared using the
6175 usual C/C++ syntax, rather than the GNU case range extension.
6176 CASES is a tree containing all the case ranges processed so far;
6177 COND is the condition for the switch-statement itself.
6178 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6179 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6180 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
6182 tree
6183 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6184 tree low_value, tree high_value, bool *outside_range_p)
6186 tree type;
6187 tree label;
6188 tree case_label;
6189 splay_tree_node node;
6191 /* Create the LABEL_DECL itself. */
6192 label = create_artificial_label (loc);
6194 /* If there was an error processing the switch condition, bail now
6195 before we get more confused. */
6196 if (!cond || cond == error_mark_node)
6197 goto error_out;
6199 if ((low_value && TREE_TYPE (low_value)
6200 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6201 || (high_value && TREE_TYPE (high_value)
6202 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6204 error_at (loc, "pointers are not permitted as case values");
6205 goto error_out;
6208 /* Case ranges are a GNU extension. */
6209 if (high_value)
6210 pedwarn (loc, OPT_Wpedantic,
6211 "range expressions in switch statements are non-standard");
6213 type = TREE_TYPE (cond);
6214 if (low_value)
6216 low_value = check_case_value (loc, low_value);
6217 low_value = convert_and_check (loc, type, low_value);
6218 if (low_value == error_mark_node)
6219 goto error_out;
6221 if (high_value)
6223 high_value = check_case_value (loc, high_value);
6224 high_value = convert_and_check (loc, type, high_value);
6225 if (high_value == error_mark_node)
6226 goto error_out;
6229 if (low_value && high_value)
6231 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6232 really a case range, even though it was written that way.
6233 Remove the HIGH_VALUE to simplify later processing. */
6234 if (tree_int_cst_equal (low_value, high_value))
6235 high_value = NULL_TREE;
6236 else if (!tree_int_cst_lt (low_value, high_value))
6237 warning_at (loc, 0, "empty range specified");
6240 /* See if the case is in range of the type of the original testing
6241 expression. If both low_value and high_value are out of range,
6242 don't insert the case label and return NULL_TREE. */
6243 if (low_value
6244 && !check_case_bounds (loc, type, orig_type,
6245 &low_value, high_value ? &high_value : NULL,
6246 outside_range_p))
6247 return NULL_TREE;
6249 /* Look up the LOW_VALUE in the table of case labels we already
6250 have. */
6251 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6252 /* If there was not an exact match, check for overlapping ranges.
6253 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6254 that's a `default' label and the only overlap is an exact match. */
6255 if (!node && (low_value || high_value))
6257 splay_tree_node low_bound;
6258 splay_tree_node high_bound;
6260 /* Even though there wasn't an exact match, there might be an
6261 overlap between this case range and another case range.
6262 Since we've (inductively) not allowed any overlapping case
6263 ranges, we simply need to find the greatest low case label
6264 that is smaller that LOW_VALUE, and the smallest low case
6265 label that is greater than LOW_VALUE. If there is an overlap
6266 it will occur in one of these two ranges. */
6267 low_bound = splay_tree_predecessor (cases,
6268 (splay_tree_key) low_value);
6269 high_bound = splay_tree_successor (cases,
6270 (splay_tree_key) low_value);
6272 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6273 the LOW_VALUE, so there is no need to check unless the
6274 LOW_BOUND is in fact itself a case range. */
6275 if (low_bound
6276 && CASE_HIGH ((tree) low_bound->value)
6277 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6278 low_value) >= 0)
6279 node = low_bound;
6280 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6281 range is bigger than the low end of the current range, so we
6282 are only interested if the current range is a real range, and
6283 not an ordinary case label. */
6284 else if (high_bound
6285 && high_value
6286 && (tree_int_cst_compare ((tree) high_bound->key,
6287 high_value)
6288 <= 0))
6289 node = high_bound;
6291 /* If there was an overlap, issue an error. */
6292 if (node)
6294 tree duplicate = CASE_LABEL ((tree) node->value);
6296 if (high_value)
6298 error_at (loc, "duplicate (or overlapping) case value");
6299 error_at (DECL_SOURCE_LOCATION (duplicate),
6300 "this is the first entry overlapping that value");
6302 else if (low_value)
6304 error_at (loc, "duplicate case value") ;
6305 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6307 else
6309 error_at (loc, "multiple default labels in one switch");
6310 error_at (DECL_SOURCE_LOCATION (duplicate),
6311 "this is the first default label");
6313 goto error_out;
6316 /* Add a CASE_LABEL to the statement-tree. */
6317 case_label = add_stmt (build_case_label (low_value, high_value, label));
6318 /* Register this case label in the splay tree. */
6319 splay_tree_insert (cases,
6320 (splay_tree_key) low_value,
6321 (splay_tree_value) case_label);
6323 return case_label;
6325 error_out:
6326 /* Add a label so that the back-end doesn't think that the beginning of
6327 the switch is unreachable. Note that we do not add a case label, as
6328 that just leads to duplicates and thence to failure later on. */
6329 if (!cases->root)
6331 tree t = create_artificial_label (loc);
6332 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6334 return error_mark_node;
6337 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6338 Used to verify that case values match up with enumerator values. */
6340 static void
6341 match_case_to_enum_1 (tree key, tree type, tree label)
6343 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6345 if (tree_fits_uhwi_p (key))
6346 print_dec (key, buf, UNSIGNED);
6347 else if (tree_fits_shwi_p (key))
6348 print_dec (key, buf, SIGNED);
6349 else
6350 print_hex (key, buf);
6352 if (TYPE_NAME (type) == 0)
6353 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6354 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6355 "case value %qs not in enumerated type",
6356 buf);
6357 else
6358 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6359 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6360 "case value %qs not in enumerated type %qT",
6361 buf, type);
6364 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6365 Used to verify that case values match up with enumerator values. */
6367 static int
6368 match_case_to_enum (splay_tree_node node, void *data)
6370 tree label = (tree) node->value;
6371 tree type = (tree) data;
6373 /* Skip default case. */
6374 if (!CASE_LOW (label))
6375 return 0;
6377 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6378 when we did our enum->case scan. Reset our scratch bit after. */
6379 if (!CASE_LOW_SEEN (label))
6380 match_case_to_enum_1 (CASE_LOW (label), type, label);
6381 else
6382 CASE_LOW_SEEN (label) = 0;
6384 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6385 not set, that means that CASE_HIGH did not appear when we did our
6386 enum->case scan. Reset our scratch bit after. */
6387 if (CASE_HIGH (label))
6389 if (!CASE_HIGH_SEEN (label))
6390 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6391 else
6392 CASE_HIGH_SEEN (label) = 0;
6395 return 0;
6398 /* Handle -Wswitch*. Called from the front end after parsing the
6399 switch construct. */
6400 /* ??? Should probably be somewhere generic, since other languages
6401 besides C and C++ would want this. At the moment, however, C/C++
6402 are the only tree-ssa languages that support enumerations at all,
6403 so the point is moot. */
6405 void
6406 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6407 tree type, tree cond, bool bool_cond_p,
6408 bool outside_range_p)
6410 splay_tree_node default_node;
6411 splay_tree_node node;
6412 tree chain;
6414 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6415 && !warn_switch_bool)
6416 return;
6418 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6419 if (!default_node)
6420 warning_at (switch_location, OPT_Wswitch_default,
6421 "switch missing default case");
6423 /* There are certain cases where -Wswitch-bool warnings aren't
6424 desirable, such as
6425 switch (boolean)
6427 case true: ...
6428 case false: ...
6430 so be careful here. */
6431 if (warn_switch_bool && bool_cond_p)
6433 splay_tree_node min_node;
6434 /* If there's a default node, it's also the value with the minimal
6435 key. So look at the penultimate key (if any). */
6436 if (default_node)
6437 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6438 else
6439 min_node = splay_tree_min (cases);
6440 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6442 splay_tree_node max_node = splay_tree_max (cases);
6443 /* This might be a case range, so look at the value with the
6444 maximal key and then check CASE_HIGH. */
6445 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6446 if (max)
6447 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6449 /* If there's a case value > 1 or < 0, that is outside bool
6450 range, warn. */
6451 if (outside_range_p
6452 || (max && wi::gts_p (max, 1))
6453 || (min && wi::lts_p (min, 0))
6454 /* And handle the
6455 switch (boolean)
6457 case true: ...
6458 case false: ...
6459 default: ...
6461 case, where we want to warn. */
6462 || (default_node
6463 && max && wi::eq_p (max, 1)
6464 && min && wi::eq_p (min, 0)))
6465 warning_at (switch_location, OPT_Wswitch_bool,
6466 "switch condition has boolean value");
6469 /* From here on, we only care about enumerated types. */
6470 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6471 return;
6473 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6474 if (!warn_switch_enum && !warn_switch)
6475 return;
6477 /* Check the cases. Warn about case values which are not members of
6478 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6479 there is no default case, check that exactly all enumeration
6480 literals are covered by the cases. */
6482 /* Clearing COND if it is not an integer constant simplifies
6483 the tests inside the loop below. */
6484 if (TREE_CODE (cond) != INTEGER_CST)
6485 cond = NULL_TREE;
6487 /* The time complexity here is O(N*lg(N)) worst case, but for the
6488 common case of monotonically increasing enumerators, it is
6489 O(N), since the nature of the splay tree will keep the next
6490 element adjacent to the root at all times. */
6492 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6494 tree value = TREE_VALUE (chain);
6495 if (TREE_CODE (value) == CONST_DECL)
6496 value = DECL_INITIAL (value);
6497 node = splay_tree_lookup (cases, (splay_tree_key) value);
6498 if (node)
6500 /* Mark the CASE_LOW part of the case entry as seen. */
6501 tree label = (tree) node->value;
6502 CASE_LOW_SEEN (label) = 1;
6503 continue;
6506 /* Even though there wasn't an exact match, there might be a
6507 case range which includes the enumerator's value. */
6508 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6509 if (node && CASE_HIGH ((tree) node->value))
6511 tree label = (tree) node->value;
6512 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6513 if (cmp >= 0)
6515 /* If we match the upper bound exactly, mark the CASE_HIGH
6516 part of the case entry as seen. */
6517 if (cmp == 0)
6518 CASE_HIGH_SEEN (label) = 1;
6519 continue;
6523 /* We've now determined that this enumerated literal isn't
6524 handled by the case labels of the switch statement. */
6526 /* If the switch expression is a constant, we only really care
6527 about whether that constant is handled by the switch. */
6528 if (cond && tree_int_cst_compare (cond, value))
6529 continue;
6531 /* If there is a default_node, the only relevant option is
6532 Wswitch-enum. Otherwise, if both are enabled then we prefer
6533 to warn using -Wswitch because -Wswitch is enabled by -Wall
6534 while -Wswitch-enum is explicit. */
6535 warning_at (switch_location,
6536 (default_node || !warn_switch
6537 ? OPT_Wswitch_enum
6538 : OPT_Wswitch),
6539 "enumeration value %qE not handled in switch",
6540 TREE_PURPOSE (chain));
6543 /* Warn if there are case expressions that don't correspond to
6544 enumerators. This can occur since C and C++ don't enforce
6545 type-checking of assignments to enumeration variables.
6547 The time complexity here is now always O(N) worst case, since
6548 we should have marked both the lower bound and upper bound of
6549 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6550 above. This scan also resets those fields. */
6552 splay_tree_foreach (cases, match_case_to_enum, type);
6555 /* Finish an expression taking the address of LABEL (an
6556 IDENTIFIER_NODE). Returns an expression for the address.
6558 LOC is the location for the expression returned. */
6560 tree
6561 finish_label_address_expr (tree label, location_t loc)
6563 tree result;
6565 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6567 if (label == error_mark_node)
6568 return error_mark_node;
6570 label = lookup_label (label);
6571 if (label == NULL_TREE)
6572 result = null_pointer_node;
6573 else
6575 TREE_USED (label) = 1;
6576 result = build1 (ADDR_EXPR, ptr_type_node, label);
6577 /* The current function is not necessarily uninlinable.
6578 Computed gotos are incompatible with inlining, but the value
6579 here could be used only in a diagnostic, for example. */
6580 protected_set_expr_location (result, loc);
6583 return result;
6587 /* Given a boolean expression ARG, return a tree representing an increment
6588 or decrement (as indicated by CODE) of ARG. The front end must check for
6589 invalid cases (e.g., decrement in C++). */
6590 tree
6591 boolean_increment (enum tree_code code, tree arg)
6593 tree val;
6594 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6596 arg = stabilize_reference (arg);
6597 switch (code)
6599 case PREINCREMENT_EXPR:
6600 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6601 break;
6602 case POSTINCREMENT_EXPR:
6603 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6604 arg = save_expr (arg);
6605 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6606 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6607 break;
6608 case PREDECREMENT_EXPR:
6609 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6610 invert_truthvalue_loc (input_location, arg));
6611 break;
6612 case POSTDECREMENT_EXPR:
6613 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6614 invert_truthvalue_loc (input_location, arg));
6615 arg = save_expr (arg);
6616 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6617 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6618 break;
6619 default:
6620 gcc_unreachable ();
6622 TREE_SIDE_EFFECTS (val) = 1;
6623 return val;
6626 /* Built-in macros for stddef.h and stdint.h, that require macros
6627 defined in this file. */
6628 void
6629 c_stddef_cpp_builtins(void)
6631 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6632 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6633 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6634 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6635 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6636 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6637 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6638 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6639 if (SIG_ATOMIC_TYPE)
6640 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6641 if (INT8_TYPE)
6642 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6643 if (INT16_TYPE)
6644 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6645 if (INT32_TYPE)
6646 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6647 if (INT64_TYPE)
6648 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6649 if (UINT8_TYPE)
6650 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6651 if (UINT16_TYPE)
6652 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6653 if (UINT32_TYPE)
6654 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6655 if (UINT64_TYPE)
6656 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6657 if (INT_LEAST8_TYPE)
6658 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6659 if (INT_LEAST16_TYPE)
6660 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6661 if (INT_LEAST32_TYPE)
6662 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6663 if (INT_LEAST64_TYPE)
6664 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6665 if (UINT_LEAST8_TYPE)
6666 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6667 if (UINT_LEAST16_TYPE)
6668 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6669 if (UINT_LEAST32_TYPE)
6670 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6671 if (UINT_LEAST64_TYPE)
6672 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6673 if (INT_FAST8_TYPE)
6674 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6675 if (INT_FAST16_TYPE)
6676 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6677 if (INT_FAST32_TYPE)
6678 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6679 if (INT_FAST64_TYPE)
6680 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6681 if (UINT_FAST8_TYPE)
6682 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6683 if (UINT_FAST16_TYPE)
6684 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6685 if (UINT_FAST32_TYPE)
6686 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6687 if (UINT_FAST64_TYPE)
6688 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6689 if (INTPTR_TYPE)
6690 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6691 if (UINTPTR_TYPE)
6692 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6695 static void
6696 c_init_attributes (void)
6698 /* Fill in the built_in_attributes array. */
6699 #define DEF_ATTR_NULL_TREE(ENUM) \
6700 built_in_attributes[(int) ENUM] = NULL_TREE;
6701 #define DEF_ATTR_INT(ENUM, VALUE) \
6702 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6703 #define DEF_ATTR_STRING(ENUM, VALUE) \
6704 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6705 #define DEF_ATTR_IDENT(ENUM, STRING) \
6706 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6707 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6708 built_in_attributes[(int) ENUM] \
6709 = tree_cons (built_in_attributes[(int) PURPOSE], \
6710 built_in_attributes[(int) VALUE], \
6711 built_in_attributes[(int) CHAIN]);
6712 #include "builtin-attrs.def"
6713 #undef DEF_ATTR_NULL_TREE
6714 #undef DEF_ATTR_INT
6715 #undef DEF_ATTR_IDENT
6716 #undef DEF_ATTR_TREE_LIST
6719 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6720 identifier as an argument, so the front end shouldn't look it up. */
6722 bool
6723 attribute_takes_identifier_p (const_tree attr_id)
6725 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6726 if (spec == NULL)
6727 /* Unknown attribute that we'll end up ignoring, return true so we
6728 don't complain about an identifier argument. */
6729 return true;
6730 else if (!strcmp ("mode", spec->name)
6731 || !strcmp ("format", spec->name)
6732 || !strcmp ("cleanup", spec->name))
6733 return true;
6734 else
6735 return targetm.attribute_takes_identifier_p (attr_id);
6738 /* Attribute handlers common to C front ends. */
6740 /* Handle a "packed" attribute; arguments as in
6741 struct attribute_spec.handler. */
6743 static tree
6744 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6745 int flags, bool *no_add_attrs)
6747 if (TYPE_P (*node))
6749 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6750 *node = build_variant_type_copy (*node);
6751 TYPE_PACKED (*node) = 1;
6753 else if (TREE_CODE (*node) == FIELD_DECL)
6755 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6756 /* Still pack bitfields. */
6757 && ! DECL_INITIAL (*node))
6758 warning (OPT_Wattributes,
6759 "%qE attribute ignored for field of type %qT",
6760 name, TREE_TYPE (*node));
6761 else
6762 DECL_PACKED (*node) = 1;
6764 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6765 used for DECL_REGISTER. It wouldn't mean anything anyway.
6766 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6767 that changes what the typedef is typing. */
6768 else
6770 warning (OPT_Wattributes, "%qE attribute ignored", name);
6771 *no_add_attrs = true;
6774 return NULL_TREE;
6777 /* Handle a "nocommon" attribute; arguments as in
6778 struct attribute_spec.handler. */
6780 static tree
6781 handle_nocommon_attribute (tree *node, tree name,
6782 tree ARG_UNUSED (args),
6783 int ARG_UNUSED (flags), bool *no_add_attrs)
6785 if (VAR_P (*node))
6786 DECL_COMMON (*node) = 0;
6787 else
6789 warning (OPT_Wattributes, "%qE attribute ignored", name);
6790 *no_add_attrs = true;
6793 return NULL_TREE;
6796 /* Handle a "common" attribute; arguments as in
6797 struct attribute_spec.handler. */
6799 static tree
6800 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6801 int ARG_UNUSED (flags), bool *no_add_attrs)
6803 if (VAR_P (*node))
6804 DECL_COMMON (*node) = 1;
6805 else
6807 warning (OPT_Wattributes, "%qE attribute ignored", name);
6808 *no_add_attrs = true;
6811 return NULL_TREE;
6814 /* Handle a "noreturn" attribute; arguments as in
6815 struct attribute_spec.handler. */
6817 static tree
6818 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6819 int ARG_UNUSED (flags), bool *no_add_attrs)
6821 tree type = TREE_TYPE (*node);
6823 /* See FIXME comment in c_common_attribute_table. */
6824 if (TREE_CODE (*node) == FUNCTION_DECL
6825 || objc_method_decl (TREE_CODE (*node)))
6826 TREE_THIS_VOLATILE (*node) = 1;
6827 else if (TREE_CODE (type) == POINTER_TYPE
6828 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6829 TREE_TYPE (*node)
6830 = (build_qualified_type
6831 (build_pointer_type
6832 (build_type_variant (TREE_TYPE (type),
6833 TYPE_READONLY (TREE_TYPE (type)), 1)),
6834 TYPE_QUALS (type)));
6835 else
6837 warning (OPT_Wattributes, "%qE attribute ignored", name);
6838 *no_add_attrs = true;
6841 return NULL_TREE;
6844 /* Handle a "hot" and attribute; arguments as in
6845 struct attribute_spec.handler. */
6847 static tree
6848 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6849 int ARG_UNUSED (flags), bool *no_add_attrs)
6851 if (TREE_CODE (*node) == FUNCTION_DECL
6852 || TREE_CODE (*node) == LABEL_DECL)
6854 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6856 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6857 "with attribute %qs", name, "cold");
6858 *no_add_attrs = true;
6860 /* Most of the rest of the hot processing is done later with
6861 lookup_attribute. */
6863 else
6865 warning (OPT_Wattributes, "%qE attribute ignored", name);
6866 *no_add_attrs = true;
6869 return NULL_TREE;
6872 /* Handle a "cold" and attribute; arguments as in
6873 struct attribute_spec.handler. */
6875 static tree
6876 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6877 int ARG_UNUSED (flags), bool *no_add_attrs)
6879 if (TREE_CODE (*node) == FUNCTION_DECL
6880 || TREE_CODE (*node) == LABEL_DECL)
6882 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6884 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6885 "with attribute %qs", name, "hot");
6886 *no_add_attrs = true;
6888 /* Most of the rest of the cold processing is done later with
6889 lookup_attribute. */
6891 else
6893 warning (OPT_Wattributes, "%qE attribute ignored", name);
6894 *no_add_attrs = true;
6897 return NULL_TREE;
6900 /* Handle a "no_sanitize_address" attribute; arguments as in
6901 struct attribute_spec.handler. */
6903 static tree
6904 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6905 bool *no_add_attrs)
6907 if (TREE_CODE (*node) != FUNCTION_DECL)
6909 warning (OPT_Wattributes, "%qE attribute ignored", name);
6910 *no_add_attrs = true;
6913 return NULL_TREE;
6916 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6917 struct attribute_spec.handler. */
6919 static tree
6920 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6921 bool *no_add_attrs)
6923 if (TREE_CODE (*node) != FUNCTION_DECL)
6924 warning (OPT_Wattributes, "%qE attribute ignored", name);
6925 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6926 DECL_ATTRIBUTES (*node)
6927 = tree_cons (get_identifier ("no_sanitize_address"),
6928 NULL_TREE, DECL_ATTRIBUTES (*node));
6929 *no_add_attrs = true;
6930 return NULL_TREE;
6933 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6934 struct attribute_spec.handler. */
6936 static tree
6937 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6938 bool *no_add_attrs)
6940 if (TREE_CODE (*node) != FUNCTION_DECL)
6942 warning (OPT_Wattributes, "%qE attribute ignored", name);
6943 *no_add_attrs = true;
6946 return NULL_TREE;
6949 /* Handle a "stack_protect" attribute; arguments as in
6950 struct attribute_spec.handler. */
6951 static tree
6952 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6953 bool *no_add_attrs)
6955 if (TREE_CODE (*node) != FUNCTION_DECL)
6957 warning (OPT_Wattributes, "%qE attribute ignored", name);
6958 *no_add_attrs = true;
6960 else
6961 DECL_ATTRIBUTES (*node)
6962 = tree_cons (get_identifier ("stack_protect"),
6963 NULL_TREE, DECL_ATTRIBUTES (*node));
6965 return NULL_TREE;
6968 /* Handle a "noinline" attribute; arguments as in
6969 struct attribute_spec.handler. */
6971 static tree
6972 handle_noinline_attribute (tree *node, tree name,
6973 tree ARG_UNUSED (args),
6974 int ARG_UNUSED (flags), bool *no_add_attrs)
6976 if (TREE_CODE (*node) == FUNCTION_DECL)
6978 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6980 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6981 "with attribute %qs", name, "always_inline");
6982 *no_add_attrs = true;
6984 else
6985 DECL_UNINLINABLE (*node) = 1;
6987 else
6989 warning (OPT_Wattributes, "%qE attribute ignored", name);
6990 *no_add_attrs = true;
6993 return NULL_TREE;
6996 /* Handle a "noclone" attribute; arguments as in
6997 struct attribute_spec.handler. */
6999 static tree
7000 handle_noclone_attribute (tree *node, tree name,
7001 tree ARG_UNUSED (args),
7002 int ARG_UNUSED (flags), bool *no_add_attrs)
7004 if (TREE_CODE (*node) != FUNCTION_DECL)
7006 warning (OPT_Wattributes, "%qE attribute ignored", name);
7007 *no_add_attrs = true;
7010 return NULL_TREE;
7013 /* Handle a "no_icf" attribute; arguments as in
7014 struct attribute_spec.handler. */
7016 static tree
7017 handle_noicf_attribute (tree *node, tree name,
7018 tree ARG_UNUSED (args),
7019 int ARG_UNUSED (flags), bool *no_add_attrs)
7021 if (TREE_CODE (*node) != FUNCTION_DECL)
7023 warning (OPT_Wattributes, "%qE attribute ignored", name);
7024 *no_add_attrs = true;
7027 return NULL_TREE;
7031 /* Handle a "always_inline" attribute; arguments as in
7032 struct attribute_spec.handler. */
7034 static tree
7035 handle_always_inline_attribute (tree *node, tree name,
7036 tree ARG_UNUSED (args),
7037 int ARG_UNUSED (flags),
7038 bool *no_add_attrs)
7040 if (TREE_CODE (*node) == FUNCTION_DECL)
7042 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7044 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7045 "with %qs attribute", name, "noinline");
7046 *no_add_attrs = true;
7048 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
7050 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7051 "with %qs attribute", name, "target_clones");
7052 *no_add_attrs = true;
7054 else
7055 /* Set the attribute and mark it for disregarding inline
7056 limits. */
7057 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
7059 else
7061 warning (OPT_Wattributes, "%qE attribute ignored", name);
7062 *no_add_attrs = true;
7065 return NULL_TREE;
7068 /* Handle a "gnu_inline" attribute; arguments as in
7069 struct attribute_spec.handler. */
7071 static tree
7072 handle_gnu_inline_attribute (tree *node, tree name,
7073 tree ARG_UNUSED (args),
7074 int ARG_UNUSED (flags),
7075 bool *no_add_attrs)
7077 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7079 /* Do nothing else, just set the attribute. We'll get at
7080 it later with lookup_attribute. */
7082 else
7084 warning (OPT_Wattributes, "%qE attribute ignored", name);
7085 *no_add_attrs = true;
7088 return NULL_TREE;
7091 /* Handle a "leaf" attribute; arguments as in
7092 struct attribute_spec.handler. */
7094 static tree
7095 handle_leaf_attribute (tree *node, tree name,
7096 tree ARG_UNUSED (args),
7097 int ARG_UNUSED (flags), bool *no_add_attrs)
7099 if (TREE_CODE (*node) != FUNCTION_DECL)
7101 warning (OPT_Wattributes, "%qE attribute ignored", name);
7102 *no_add_attrs = true;
7104 if (!TREE_PUBLIC (*node))
7106 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7107 *no_add_attrs = true;
7110 return NULL_TREE;
7113 /* Handle an "artificial" attribute; arguments as in
7114 struct attribute_spec.handler. */
7116 static tree
7117 handle_artificial_attribute (tree *node, tree name,
7118 tree ARG_UNUSED (args),
7119 int ARG_UNUSED (flags),
7120 bool *no_add_attrs)
7122 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7124 /* Do nothing else, just set the attribute. We'll get at
7125 it later with lookup_attribute. */
7127 else
7129 warning (OPT_Wattributes, "%qE attribute ignored", name);
7130 *no_add_attrs = true;
7133 return NULL_TREE;
7136 /* Handle a "flatten" attribute; arguments as in
7137 struct attribute_spec.handler. */
7139 static tree
7140 handle_flatten_attribute (tree *node, tree name,
7141 tree args ATTRIBUTE_UNUSED,
7142 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7144 if (TREE_CODE (*node) == FUNCTION_DECL)
7145 /* Do nothing else, just set the attribute. We'll get at
7146 it later with lookup_attribute. */
7148 else
7150 warning (OPT_Wattributes, "%qE attribute ignored", name);
7151 *no_add_attrs = true;
7154 return NULL_TREE;
7157 /* Handle a "warning" or "error" attribute; arguments as in
7158 struct attribute_spec.handler. */
7160 static tree
7161 handle_error_attribute (tree *node, tree name, tree args,
7162 int ARG_UNUSED (flags), bool *no_add_attrs)
7164 if (TREE_CODE (*node) == FUNCTION_DECL
7165 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7166 /* Do nothing else, just set the attribute. We'll get at
7167 it later with lookup_attribute. */
7169 else
7171 warning (OPT_Wattributes, "%qE attribute ignored", name);
7172 *no_add_attrs = true;
7175 return NULL_TREE;
7178 /* Handle a "used" attribute; arguments as in
7179 struct attribute_spec.handler. */
7181 static tree
7182 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7183 int ARG_UNUSED (flags), bool *no_add_attrs)
7185 tree node = *pnode;
7187 if (TREE_CODE (node) == FUNCTION_DECL
7188 || (VAR_P (node) && TREE_STATIC (node))
7189 || (TREE_CODE (node) == TYPE_DECL))
7191 TREE_USED (node) = 1;
7192 DECL_PRESERVE_P (node) = 1;
7193 if (VAR_P (node))
7194 DECL_READ_P (node) = 1;
7196 else
7198 warning (OPT_Wattributes, "%qE attribute ignored", name);
7199 *no_add_attrs = true;
7202 return NULL_TREE;
7205 /* Handle a "unused" attribute; arguments as in
7206 struct attribute_spec.handler. */
7208 tree
7209 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7210 int flags, bool *no_add_attrs)
7212 if (DECL_P (*node))
7214 tree decl = *node;
7216 if (TREE_CODE (decl) == PARM_DECL
7217 || VAR_OR_FUNCTION_DECL_P (decl)
7218 || TREE_CODE (decl) == LABEL_DECL
7219 || TREE_CODE (decl) == CONST_DECL
7220 || TREE_CODE (decl) == TYPE_DECL)
7222 TREE_USED (decl) = 1;
7223 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
7224 DECL_READ_P (decl) = 1;
7226 else
7228 warning (OPT_Wattributes, "%qE attribute ignored", name);
7229 *no_add_attrs = true;
7232 else
7234 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7235 *node = build_variant_type_copy (*node);
7236 TREE_USED (*node) = 1;
7239 return NULL_TREE;
7242 /* Handle a "externally_visible" attribute; arguments as in
7243 struct attribute_spec.handler. */
7245 static tree
7246 handle_externally_visible_attribute (tree *pnode, tree name,
7247 tree ARG_UNUSED (args),
7248 int ARG_UNUSED (flags),
7249 bool *no_add_attrs)
7251 tree node = *pnode;
7253 if (VAR_OR_FUNCTION_DECL_P (node))
7255 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7256 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7258 warning (OPT_Wattributes,
7259 "%qE attribute have effect only on public objects", name);
7260 *no_add_attrs = true;
7263 else
7265 warning (OPT_Wattributes, "%qE attribute ignored", name);
7266 *no_add_attrs = true;
7269 return NULL_TREE;
7272 /* Handle the "no_reorder" attribute. Arguments as in
7273 struct attribute_spec.handler. */
7275 static tree
7276 handle_no_reorder_attribute (tree *pnode,
7277 tree name,
7278 tree,
7279 int,
7280 bool *no_add_attrs)
7282 tree node = *pnode;
7284 if (!VAR_OR_FUNCTION_DECL_P (node)
7285 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7287 warning (OPT_Wattributes,
7288 "%qE attribute only affects top level objects",
7289 name);
7290 *no_add_attrs = true;
7293 return NULL_TREE;
7296 /* Handle a "const" attribute; arguments as in
7297 struct attribute_spec.handler. */
7299 static tree
7300 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7301 int ARG_UNUSED (flags), bool *no_add_attrs)
7303 tree type = TREE_TYPE (*node);
7305 /* See FIXME comment on noreturn in c_common_attribute_table. */
7306 if (TREE_CODE (*node) == FUNCTION_DECL)
7307 TREE_READONLY (*node) = 1;
7308 else if (TREE_CODE (type) == POINTER_TYPE
7309 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7310 TREE_TYPE (*node)
7311 = (build_qualified_type
7312 (build_pointer_type
7313 (build_type_variant (TREE_TYPE (type), 1,
7314 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7315 TYPE_QUALS (type)));
7316 else
7318 warning (OPT_Wattributes, "%qE attribute ignored", name);
7319 *no_add_attrs = true;
7322 return NULL_TREE;
7325 /* Handle a "scalar_storage_order" attribute; arguments as in
7326 struct attribute_spec.handler. */
7328 static tree
7329 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7330 int flags, bool *no_add_attrs)
7332 tree id = TREE_VALUE (args);
7333 tree type;
7335 if (TREE_CODE (*node) == TYPE_DECL
7336 && ! (flags & ATTR_FLAG_CXX11))
7337 node = &TREE_TYPE (*node);
7338 type = *node;
7340 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7342 error ("scalar_storage_order is not supported because endianness "
7343 "is not uniform");
7344 return NULL_TREE;
7347 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7349 bool reverse = false;
7351 if (TREE_CODE (id) == STRING_CST
7352 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7353 reverse = !BYTES_BIG_ENDIAN;
7354 else if (TREE_CODE (id) == STRING_CST
7355 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7356 reverse = BYTES_BIG_ENDIAN;
7357 else
7359 error ("scalar_storage_order argument must be one of \"big-endian\""
7360 " or \"little-endian\"");
7361 return NULL_TREE;
7364 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7366 if (reverse)
7367 /* A type variant isn't good enough, since we don't want a cast
7368 to such a type to be removed as a no-op. */
7369 *node = type = build_duplicate_type (type);
7372 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7373 return NULL_TREE;
7376 warning (OPT_Wattributes, "%qE attribute ignored", name);
7377 *no_add_attrs = true;
7378 return NULL_TREE;
7381 /* Handle a "transparent_union" attribute; arguments as in
7382 struct attribute_spec.handler. */
7384 static tree
7385 handle_transparent_union_attribute (tree *node, tree name,
7386 tree ARG_UNUSED (args), int flags,
7387 bool *no_add_attrs)
7389 tree type;
7391 *no_add_attrs = true;
7393 if (TREE_CODE (*node) == TYPE_DECL
7394 && ! (flags & ATTR_FLAG_CXX11))
7395 node = &TREE_TYPE (*node);
7396 type = *node;
7398 if (TREE_CODE (type) == UNION_TYPE)
7400 /* Make sure that the first field will work for a transparent union.
7401 If the type isn't complete yet, leave the check to the code in
7402 finish_struct. */
7403 if (TYPE_SIZE (type))
7405 tree first = first_field (type);
7406 if (first == NULL_TREE
7407 || DECL_ARTIFICIAL (first)
7408 || TYPE_MODE (type) != DECL_MODE (first))
7409 goto ignored;
7412 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7414 /* If the type isn't complete yet, setting the flag
7415 on a variant wouldn't ever be checked. */
7416 if (!TYPE_SIZE (type))
7417 goto ignored;
7419 /* build_duplicate_type doesn't work for C++. */
7420 if (c_dialect_cxx ())
7421 goto ignored;
7423 /* A type variant isn't good enough, since we don't want a cast
7424 to such a type to be removed as a no-op. */
7425 *node = type = build_duplicate_type (type);
7428 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7429 TYPE_TRANSPARENT_AGGR (t) = 1;
7430 return NULL_TREE;
7433 ignored:
7434 warning (OPT_Wattributes, "%qE attribute ignored", name);
7435 return NULL_TREE;
7438 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7439 get the requested priority for a constructor or destructor,
7440 possibly issuing diagnostics for invalid or reserved
7441 priorities. */
7443 static priority_type
7444 get_priority (tree args, bool is_destructor)
7446 HOST_WIDE_INT pri;
7447 tree arg;
7449 if (!args)
7450 return DEFAULT_INIT_PRIORITY;
7452 if (!SUPPORTS_INIT_PRIORITY)
7454 if (is_destructor)
7455 error ("destructor priorities are not supported");
7456 else
7457 error ("constructor priorities are not supported");
7458 return DEFAULT_INIT_PRIORITY;
7461 arg = TREE_VALUE (args);
7462 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7463 goto invalid;
7464 if (arg == error_mark_node)
7465 return DEFAULT_INIT_PRIORITY;
7466 arg = default_conversion (arg);
7467 if (!tree_fits_shwi_p (arg)
7468 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7469 goto invalid;
7471 pri = tree_to_shwi (arg);
7472 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7473 goto invalid;
7475 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7477 if (is_destructor)
7478 warning (0,
7479 "destructor priorities from 0 to %d are reserved "
7480 "for the implementation",
7481 MAX_RESERVED_INIT_PRIORITY);
7482 else
7483 warning (0,
7484 "constructor priorities from 0 to %d are reserved "
7485 "for the implementation",
7486 MAX_RESERVED_INIT_PRIORITY);
7488 return pri;
7490 invalid:
7491 if (is_destructor)
7492 error ("destructor priorities must be integers from 0 to %d inclusive",
7493 MAX_INIT_PRIORITY);
7494 else
7495 error ("constructor priorities must be integers from 0 to %d inclusive",
7496 MAX_INIT_PRIORITY);
7497 return DEFAULT_INIT_PRIORITY;
7500 /* Handle a "constructor" attribute; arguments as in
7501 struct attribute_spec.handler. */
7503 static tree
7504 handle_constructor_attribute (tree *node, tree name, tree args,
7505 int ARG_UNUSED (flags),
7506 bool *no_add_attrs)
7508 tree decl = *node;
7509 tree type = TREE_TYPE (decl);
7511 if (TREE_CODE (decl) == FUNCTION_DECL
7512 && TREE_CODE (type) == FUNCTION_TYPE
7513 && decl_function_context (decl) == 0)
7515 priority_type priority;
7516 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7517 priority = get_priority (args, /*is_destructor=*/false);
7518 SET_DECL_INIT_PRIORITY (decl, priority);
7519 TREE_USED (decl) = 1;
7521 else
7523 warning (OPT_Wattributes, "%qE attribute ignored", name);
7524 *no_add_attrs = true;
7527 return NULL_TREE;
7530 /* Handle a "destructor" attribute; arguments as in
7531 struct attribute_spec.handler. */
7533 static tree
7534 handle_destructor_attribute (tree *node, tree name, tree args,
7535 int ARG_UNUSED (flags),
7536 bool *no_add_attrs)
7538 tree decl = *node;
7539 tree type = TREE_TYPE (decl);
7541 if (TREE_CODE (decl) == FUNCTION_DECL
7542 && TREE_CODE (type) == FUNCTION_TYPE
7543 && decl_function_context (decl) == 0)
7545 priority_type priority;
7546 DECL_STATIC_DESTRUCTOR (decl) = 1;
7547 priority = get_priority (args, /*is_destructor=*/true);
7548 SET_DECL_FINI_PRIORITY (decl, priority);
7549 TREE_USED (decl) = 1;
7551 else
7553 warning (OPT_Wattributes, "%qE attribute ignored", name);
7554 *no_add_attrs = true;
7557 return NULL_TREE;
7560 /* Nonzero if the mode is a valid vector mode for this architecture.
7561 This returns nonzero even if there is no hardware support for the
7562 vector mode, but we can emulate with narrower modes. */
7564 static int
7565 vector_mode_valid_p (machine_mode mode)
7567 enum mode_class mclass = GET_MODE_CLASS (mode);
7568 machine_mode innermode;
7570 /* Doh! What's going on? */
7571 if (mclass != MODE_VECTOR_INT
7572 && mclass != MODE_VECTOR_FLOAT
7573 && mclass != MODE_VECTOR_FRACT
7574 && mclass != MODE_VECTOR_UFRACT
7575 && mclass != MODE_VECTOR_ACCUM
7576 && mclass != MODE_VECTOR_UACCUM)
7577 return 0;
7579 /* Hardware support. Woo hoo! */
7580 if (targetm.vector_mode_supported_p (mode))
7581 return 1;
7583 innermode = GET_MODE_INNER (mode);
7585 /* We should probably return 1 if requesting V4DI and we have no DI,
7586 but we have V2DI, but this is probably very unlikely. */
7588 /* If we have support for the inner mode, we can safely emulate it.
7589 We may not have V2DI, but me can emulate with a pair of DIs. */
7590 return targetm.scalar_mode_supported_p (innermode);
7594 /* Handle a "mode" attribute; arguments as in
7595 struct attribute_spec.handler. */
7597 static tree
7598 handle_mode_attribute (tree *node, tree name, tree args,
7599 int ARG_UNUSED (flags), bool *no_add_attrs)
7601 tree type = *node;
7602 tree ident = TREE_VALUE (args);
7604 *no_add_attrs = true;
7606 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7607 warning (OPT_Wattributes, "%qE attribute ignored", name);
7608 else
7610 int j;
7611 const char *p = IDENTIFIER_POINTER (ident);
7612 int len = strlen (p);
7613 machine_mode mode = VOIDmode;
7614 tree typefm;
7615 bool valid_mode;
7617 if (len > 4 && p[0] == '_' && p[1] == '_'
7618 && p[len - 1] == '_' && p[len - 2] == '_')
7620 char *newp = (char *) alloca (len - 1);
7622 strcpy (newp, &p[2]);
7623 newp[len - 4] = '\0';
7624 p = newp;
7627 /* Change this type to have a type with the specified mode.
7628 First check for the special modes. */
7629 if (!strcmp (p, "byte"))
7630 mode = byte_mode;
7631 else if (!strcmp (p, "word"))
7632 mode = word_mode;
7633 else if (!strcmp (p, "pointer"))
7634 mode = ptr_mode;
7635 else if (!strcmp (p, "libgcc_cmp_return"))
7636 mode = targetm.libgcc_cmp_return_mode ();
7637 else if (!strcmp (p, "libgcc_shift_count"))
7638 mode = targetm.libgcc_shift_count_mode ();
7639 else if (!strcmp (p, "unwind_word"))
7640 mode = targetm.unwind_word_mode ();
7641 else
7642 for (j = 0; j < NUM_MACHINE_MODES; j++)
7643 if (!strcmp (p, GET_MODE_NAME (j)))
7645 mode = (machine_mode) j;
7646 break;
7649 if (mode == VOIDmode)
7651 error ("unknown machine mode %qE", ident);
7652 return NULL_TREE;
7655 valid_mode = false;
7656 switch (GET_MODE_CLASS (mode))
7658 case MODE_INT:
7659 case MODE_PARTIAL_INT:
7660 case MODE_FLOAT:
7661 case MODE_DECIMAL_FLOAT:
7662 case MODE_FRACT:
7663 case MODE_UFRACT:
7664 case MODE_ACCUM:
7665 case MODE_UACCUM:
7666 valid_mode = targetm.scalar_mode_supported_p (mode);
7667 break;
7669 case MODE_COMPLEX_INT:
7670 case MODE_COMPLEX_FLOAT:
7671 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7672 break;
7674 case MODE_VECTOR_INT:
7675 case MODE_VECTOR_FLOAT:
7676 case MODE_VECTOR_FRACT:
7677 case MODE_VECTOR_UFRACT:
7678 case MODE_VECTOR_ACCUM:
7679 case MODE_VECTOR_UACCUM:
7680 warning (OPT_Wattributes, "specifying vector types with "
7681 "__attribute__ ((mode)) is deprecated");
7682 warning (OPT_Wattributes,
7683 "use __attribute__ ((vector_size)) instead");
7684 valid_mode = vector_mode_valid_p (mode);
7685 break;
7687 default:
7688 break;
7690 if (!valid_mode)
7692 error ("unable to emulate %qs", p);
7693 return NULL_TREE;
7696 if (POINTER_TYPE_P (type))
7698 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7699 tree (*fn)(tree, machine_mode, bool);
7701 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7703 error ("invalid pointer mode %qs", p);
7704 return NULL_TREE;
7707 if (TREE_CODE (type) == POINTER_TYPE)
7708 fn = build_pointer_type_for_mode;
7709 else
7710 fn = build_reference_type_for_mode;
7711 typefm = fn (TREE_TYPE (type), mode, false);
7713 else
7715 /* For fixed-point modes, we need to test if the signness of type
7716 and the machine mode are consistent. */
7717 if (ALL_FIXED_POINT_MODE_P (mode)
7718 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7720 error ("signedness of type and machine mode %qs don%'t match", p);
7721 return NULL_TREE;
7723 /* For fixed-point modes, we need to pass saturating info. */
7724 typefm = lang_hooks.types.type_for_mode (mode,
7725 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7726 : TYPE_UNSIGNED (type));
7729 if (typefm == NULL_TREE)
7731 error ("no data type for mode %qs", p);
7732 return NULL_TREE;
7734 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7736 /* For enumeral types, copy the precision from the integer
7737 type returned above. If not an INTEGER_TYPE, we can't use
7738 this mode for this type. */
7739 if (TREE_CODE (typefm) != INTEGER_TYPE)
7741 error ("cannot use mode %qs for enumeral types", p);
7742 return NULL_TREE;
7745 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7747 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7748 typefm = type;
7750 else
7752 /* We cannot build a type variant, as there's code that assumes
7753 that TYPE_MAIN_VARIANT has the same mode. This includes the
7754 debug generators. Instead, create a subrange type. This
7755 results in all of the enumeral values being emitted only once
7756 in the original, and the subtype gets them by reference. */
7757 if (TYPE_UNSIGNED (type))
7758 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7759 else
7760 typefm = make_signed_type (TYPE_PRECISION (typefm));
7761 TREE_TYPE (typefm) = type;
7764 else if (VECTOR_MODE_P (mode)
7765 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7766 : TREE_CODE (type) != TREE_CODE (typefm))
7768 error ("mode %qs applied to inappropriate type", p);
7769 return NULL_TREE;
7772 *node = typefm;
7775 return NULL_TREE;
7778 /* Handle a "section" attribute; arguments as in
7779 struct attribute_spec.handler. */
7781 static tree
7782 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7783 int ARG_UNUSED (flags), bool *no_add_attrs)
7785 tree decl = *node;
7787 if (!targetm_common.have_named_sections)
7789 error_at (DECL_SOURCE_LOCATION (*node),
7790 "section attributes are not supported for this target");
7791 goto fail;
7794 user_defined_section_attribute = true;
7796 if (!VAR_OR_FUNCTION_DECL_P (decl))
7798 error ("section attribute not allowed for %q+D", *node);
7799 goto fail;
7802 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7804 error ("section attribute argument not a string constant");
7805 goto fail;
7808 if (VAR_P (decl)
7809 && current_function_decl != NULL_TREE
7810 && !TREE_STATIC (decl))
7812 error_at (DECL_SOURCE_LOCATION (decl),
7813 "section attribute cannot be specified for local variables");
7814 goto fail;
7817 /* The decl may have already been given a section attribute
7818 from a previous declaration. Ensure they match. */
7819 if (DECL_SECTION_NAME (decl) != NULL
7820 && strcmp (DECL_SECTION_NAME (decl),
7821 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7823 error ("section of %q+D conflicts with previous declaration", *node);
7824 goto fail;
7827 if (VAR_P (decl)
7828 && !targetm.have_tls && targetm.emutls.tmpl_section
7829 && DECL_THREAD_LOCAL_P (decl))
7831 error ("section of %q+D cannot be overridden", *node);
7832 goto fail;
7835 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7836 return NULL_TREE;
7838 fail:
7839 *no_add_attrs = true;
7840 return NULL_TREE;
7843 /* Check whether ALIGN is a valid user-specified alignment. If so,
7844 return its base-2 log; if not, output an error and return -1. If
7845 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7846 no error. */
7848 check_user_alignment (const_tree align, bool allow_zero)
7850 int i;
7852 if (error_operand_p (align))
7853 return -1;
7854 if (TREE_CODE (align) != INTEGER_CST
7855 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7857 error ("requested alignment is not an integer constant");
7858 return -1;
7860 else if (allow_zero && integer_zerop (align))
7861 return -1;
7862 else if (tree_int_cst_sgn (align) == -1
7863 || (i = tree_log2 (align)) == -1)
7865 error ("requested alignment is not a positive power of 2");
7866 return -1;
7868 else if (i >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
7870 error ("requested alignment is too large");
7871 return -1;
7873 return i;
7876 /* If in c++-11, check if the c++-11 alignment constraint with respect
7877 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7878 c++-11 mode, does nothing.
7880 [dcl.align]2/ says:
7882 [* if the constant expression evaluates to a fundamental alignment,
7883 the alignment requirement of the declared entity shall be the
7884 specified fundamental alignment.
7886 * if the constant expression evaluates to an extended alignment
7887 and the implementation supports that alignment in the context
7888 of the declaration, the alignment of the declared entity shall
7889 be that alignment
7891 * if the constant expression evaluates to an extended alignment
7892 and the implementation does not support that alignment in the
7893 context of the declaration, the program is ill-formed]. */
7895 static bool
7896 check_cxx_fundamental_alignment_constraints (tree node,
7897 unsigned align_log,
7898 int flags)
7900 bool alignment_too_large_p = false;
7901 unsigned requested_alignment = (1U << align_log) * BITS_PER_UNIT;
7902 unsigned max_align = 0;
7904 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7905 || (node == NULL_TREE || node == error_mark_node))
7906 return true;
7908 if (cxx_fundamental_alignment_p (requested_alignment))
7909 return true;
7911 if (VAR_P (node))
7913 if (TREE_STATIC (node) || DECL_EXTERNAL (node))
7914 /* For file scope variables and static members, the target supports
7915 alignments that are at most MAX_OFILE_ALIGNMENT. */
7916 max_align = MAX_OFILE_ALIGNMENT;
7917 else
7918 /* For stack variables, the target supports at most
7919 MAX_STACK_ALIGNMENT. */
7920 max_align = MAX_STACK_ALIGNMENT;
7921 if (requested_alignment > max_align)
7922 alignment_too_large_p = true;
7924 /* Let's be liberal for types and fields; don't limit their alignment any
7925 more than check_user_alignment already did. */
7927 if (alignment_too_large_p)
7928 pedwarn (input_location, OPT_Wattributes,
7929 "requested alignment %d is larger than %d",
7930 requested_alignment / BITS_PER_UNIT, max_align / BITS_PER_UNIT);
7932 return !alignment_too_large_p;
7935 /* Handle a "aligned" attribute; arguments as in
7936 struct attribute_spec.handler. */
7938 static tree
7939 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7940 int flags, bool *no_add_attrs)
7942 tree decl = NULL_TREE;
7943 tree *type = NULL;
7944 int is_type = 0;
7945 tree align_expr;
7946 int i;
7948 if (args)
7950 align_expr = TREE_VALUE (args);
7951 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7952 && TREE_CODE (align_expr) != FUNCTION_DECL)
7953 align_expr = default_conversion (align_expr);
7955 else
7956 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7958 if (DECL_P (*node))
7960 decl = *node;
7961 type = &TREE_TYPE (decl);
7962 is_type = TREE_CODE (*node) == TYPE_DECL;
7964 else if (TYPE_P (*node))
7965 type = node, is_type = 1;
7967 if ((i = check_user_alignment (align_expr, true)) == -1
7968 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7969 *no_add_attrs = true;
7970 else if (is_type)
7972 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7973 /* OK, modify the type in place. */;
7974 /* If we have a TYPE_DECL, then copy the type, so that we
7975 don't accidentally modify a builtin type. See pushdecl. */
7976 else if (decl && TREE_TYPE (decl) != error_mark_node
7977 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7979 tree tt = TREE_TYPE (decl);
7980 *type = build_variant_type_copy (*type);
7981 DECL_ORIGINAL_TYPE (decl) = tt;
7982 TYPE_NAME (*type) = decl;
7983 TREE_USED (*type) = TREE_USED (decl);
7984 TREE_TYPE (decl) = *type;
7986 else
7987 *type = build_variant_type_copy (*type);
7989 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
7990 TYPE_USER_ALIGN (*type) = 1;
7992 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7993 && TREE_CODE (decl) != FIELD_DECL)
7995 error ("alignment may not be specified for %q+D", decl);
7996 *no_add_attrs = true;
7998 else if (DECL_USER_ALIGN (decl)
7999 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8000 /* C++-11 [dcl.align/4]:
8002 When multiple alignment-specifiers are specified for an
8003 entity, the alignment requirement shall be set to the
8004 strictest specified alignment.
8006 This formally comes from the c++11 specification but we are
8007 doing it for the GNU attribute syntax as well. */
8008 *no_add_attrs = true;
8009 else if (TREE_CODE (decl) == FUNCTION_DECL
8010 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8012 if (DECL_USER_ALIGN (decl))
8013 error ("alignment for %q+D was previously specified as %d "
8014 "and may not be decreased", decl,
8015 DECL_ALIGN (decl) / BITS_PER_UNIT);
8016 else
8017 error ("alignment for %q+D must be at least %d", decl,
8018 DECL_ALIGN (decl) / BITS_PER_UNIT);
8019 *no_add_attrs = true;
8021 else
8023 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
8024 DECL_USER_ALIGN (decl) = 1;
8027 return NULL_TREE;
8030 /* Handle a "weak" attribute; arguments as in
8031 struct attribute_spec.handler. */
8033 static tree
8034 handle_weak_attribute (tree *node, tree name,
8035 tree ARG_UNUSED (args),
8036 int ARG_UNUSED (flags),
8037 bool * ARG_UNUSED (no_add_attrs))
8039 if (TREE_CODE (*node) == FUNCTION_DECL
8040 && DECL_DECLARED_INLINE_P (*node))
8042 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
8043 *no_add_attrs = true;
8045 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8047 error ("indirect function %q+D cannot be declared weak", *node);
8048 *no_add_attrs = true;
8049 return NULL_TREE;
8051 else if (VAR_OR_FUNCTION_DECL_P (*node))
8052 declare_weak (*node);
8053 else
8054 warning (OPT_Wattributes, "%qE attribute ignored", name);
8056 return NULL_TREE;
8059 /* Handle a "noplt" attribute; arguments as in
8060 struct attribute_spec.handler. */
8062 static tree
8063 handle_noplt_attribute (tree *node, tree name,
8064 tree ARG_UNUSED (args),
8065 int ARG_UNUSED (flags),
8066 bool * ARG_UNUSED (no_add_attrs))
8068 if (TREE_CODE (*node) != FUNCTION_DECL)
8070 warning (OPT_Wattributes,
8071 "%qE attribute is only applicable on functions", name);
8072 *no_add_attrs = true;
8073 return NULL_TREE;
8075 return NULL_TREE;
8078 /* Handle an "alias" or "ifunc" attribute; arguments as in
8079 struct attribute_spec.handler, except that IS_ALIAS tells us
8080 whether this is an alias as opposed to ifunc attribute. */
8082 static tree
8083 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8084 bool *no_add_attrs)
8086 tree decl = *node;
8088 if (TREE_CODE (decl) != FUNCTION_DECL
8089 && (!is_alias || !VAR_P (decl)))
8091 warning (OPT_Wattributes, "%qE attribute ignored", name);
8092 *no_add_attrs = true;
8094 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
8095 || (TREE_CODE (decl) != FUNCTION_DECL
8096 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8097 /* A static variable declaration is always a tentative definition,
8098 but the alias is a non-tentative definition which overrides. */
8099 || (TREE_CODE (decl) != FUNCTION_DECL
8100 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
8102 error ("%q+D defined both normally and as %qE attribute", decl, name);
8103 *no_add_attrs = true;
8104 return NULL_TREE;
8106 else if (!is_alias
8107 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8108 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8110 error ("weak %q+D cannot be defined %qE", decl, name);
8111 *no_add_attrs = true;
8112 return NULL_TREE;
8115 /* Note that the very first time we process a nested declaration,
8116 decl_function_context will not be set. Indeed, *would* never
8117 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8118 we do below. After such frobbery, pushdecl would set the context.
8119 In any case, this is never what we want. */
8120 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
8122 tree id;
8124 id = TREE_VALUE (args);
8125 if (TREE_CODE (id) != STRING_CST)
8127 error ("attribute %qE argument not a string", name);
8128 *no_add_attrs = true;
8129 return NULL_TREE;
8131 id = get_identifier (TREE_STRING_POINTER (id));
8132 /* This counts as a use of the object pointed to. */
8133 TREE_USED (id) = 1;
8135 if (TREE_CODE (decl) == FUNCTION_DECL)
8136 DECL_INITIAL (decl) = error_mark_node;
8137 else
8138 TREE_STATIC (decl) = 1;
8140 if (!is_alias)
8141 /* ifuncs are also aliases, so set that attribute too. */
8142 DECL_ATTRIBUTES (decl)
8143 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8145 else
8147 warning (OPT_Wattributes, "%qE attribute ignored", name);
8148 *no_add_attrs = true;
8151 if (decl_in_symtab_p (*node))
8153 struct symtab_node *n = symtab_node::get (decl);
8154 if (n && n->refuse_visibility_changes)
8156 if (is_alias)
8157 error ("%+D declared alias after being used", decl);
8158 else
8159 error ("%+D declared ifunc after being used", decl);
8164 return NULL_TREE;
8167 /* Handle an "alias" or "ifunc" attribute; arguments as in
8168 struct attribute_spec.handler. */
8170 static tree
8171 handle_ifunc_attribute (tree *node, tree name, tree args,
8172 int ARG_UNUSED (flags), bool *no_add_attrs)
8174 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8177 /* Handle an "alias" or "ifunc" attribute; arguments as in
8178 struct attribute_spec.handler. */
8180 static tree
8181 handle_alias_attribute (tree *node, tree name, tree args,
8182 int ARG_UNUSED (flags), bool *no_add_attrs)
8184 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8187 /* Handle a "weakref" attribute; arguments as in struct
8188 attribute_spec.handler. */
8190 static tree
8191 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8192 int flags, bool *no_add_attrs)
8194 tree attr = NULL_TREE;
8196 /* We must ignore the attribute when it is associated with
8197 local-scoped decls, since attribute alias is ignored and many
8198 such symbols do not even have a DECL_WEAK field. */
8199 if (decl_function_context (*node)
8200 || current_function_decl
8201 || !VAR_OR_FUNCTION_DECL_P (*node))
8203 warning (OPT_Wattributes, "%qE attribute ignored", name);
8204 *no_add_attrs = true;
8205 return NULL_TREE;
8208 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8210 error ("indirect function %q+D cannot be declared weakref", *node);
8211 *no_add_attrs = true;
8212 return NULL_TREE;
8215 /* The idea here is that `weakref("name")' mutates into `weakref,
8216 alias("name")', and weakref without arguments, in turn,
8217 implicitly adds weak. */
8219 if (args)
8221 attr = tree_cons (get_identifier ("alias"), args, attr);
8222 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8224 *no_add_attrs = true;
8226 decl_attributes (node, attr, flags);
8228 else
8230 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8231 error_at (DECL_SOURCE_LOCATION (*node),
8232 "weakref attribute must appear before alias attribute");
8234 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8235 and that isn't supported; and because it wants to add it to
8236 the list of weak decls, which isn't helpful. */
8237 DECL_WEAK (*node) = 1;
8240 if (decl_in_symtab_p (*node))
8242 struct symtab_node *n = symtab_node::get (*node);
8243 if (n && n->refuse_visibility_changes)
8244 error ("%+D declared weakref after being used", *node);
8247 return NULL_TREE;
8250 /* Handle an "visibility" attribute; arguments as in
8251 struct attribute_spec.handler. */
8253 static tree
8254 handle_visibility_attribute (tree *node, tree name, tree args,
8255 int ARG_UNUSED (flags),
8256 bool *ARG_UNUSED (no_add_attrs))
8258 tree decl = *node;
8259 tree id = TREE_VALUE (args);
8260 enum symbol_visibility vis;
8262 if (TYPE_P (*node))
8264 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8265 /* OK */;
8266 else if (!RECORD_OR_UNION_TYPE_P (*node))
8268 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8269 name);
8270 return NULL_TREE;
8272 else if (TYPE_FIELDS (*node))
8274 error ("%qE attribute ignored because %qT is already defined",
8275 name, *node);
8276 return NULL_TREE;
8279 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8281 warning (OPT_Wattributes, "%qE attribute ignored", name);
8282 return NULL_TREE;
8285 if (TREE_CODE (id) != STRING_CST)
8287 error ("visibility argument not a string");
8288 return NULL_TREE;
8291 /* If this is a type, set the visibility on the type decl. */
8292 if (TYPE_P (decl))
8294 decl = TYPE_NAME (decl);
8295 if (!decl)
8296 return NULL_TREE;
8297 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8299 warning (OPT_Wattributes, "%qE attribute ignored on types",
8300 name);
8301 return NULL_TREE;
8305 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8306 vis = VISIBILITY_DEFAULT;
8307 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8308 vis = VISIBILITY_INTERNAL;
8309 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8310 vis = VISIBILITY_HIDDEN;
8311 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8312 vis = VISIBILITY_PROTECTED;
8313 else
8315 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8316 vis = VISIBILITY_DEFAULT;
8319 if (DECL_VISIBILITY_SPECIFIED (decl)
8320 && vis != DECL_VISIBILITY (decl))
8322 tree attributes = (TYPE_P (*node)
8323 ? TYPE_ATTRIBUTES (*node)
8324 : DECL_ATTRIBUTES (decl));
8325 if (lookup_attribute ("visibility", attributes))
8326 error ("%qD redeclared with different visibility", decl);
8327 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8328 && lookup_attribute ("dllimport", attributes))
8329 error ("%qD was declared %qs which implies default visibility",
8330 decl, "dllimport");
8331 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8332 && lookup_attribute ("dllexport", attributes))
8333 error ("%qD was declared %qs which implies default visibility",
8334 decl, "dllexport");
8337 DECL_VISIBILITY (decl) = vis;
8338 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8340 /* Go ahead and attach the attribute to the node as well. This is needed
8341 so we can determine whether we have VISIBILITY_DEFAULT because the
8342 visibility was not specified, or because it was explicitly overridden
8343 from the containing scope. */
8345 return NULL_TREE;
8348 /* Determine the ELF symbol visibility for DECL, which is either a
8349 variable or a function. It is an error to use this function if a
8350 definition of DECL is not available in this translation unit.
8351 Returns true if the final visibility has been determined by this
8352 function; false if the caller is free to make additional
8353 modifications. */
8355 bool
8356 c_determine_visibility (tree decl)
8358 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8360 /* If the user explicitly specified the visibility with an
8361 attribute, honor that. DECL_VISIBILITY will have been set during
8362 the processing of the attribute. We check for an explicit
8363 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8364 to distinguish the use of an attribute from the use of a "#pragma
8365 GCC visibility push(...)"; in the latter case we still want other
8366 considerations to be able to overrule the #pragma. */
8367 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8368 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8369 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8370 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8371 return true;
8373 /* Set default visibility to whatever the user supplied with
8374 visibility_specified depending on #pragma GCC visibility. */
8375 if (!DECL_VISIBILITY_SPECIFIED (decl))
8377 if (visibility_options.inpragma
8378 || DECL_VISIBILITY (decl) != default_visibility)
8380 DECL_VISIBILITY (decl) = default_visibility;
8381 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8382 /* If visibility changed and DECL already has DECL_RTL, ensure
8383 symbol flags are updated. */
8384 if (((VAR_P (decl) && TREE_STATIC (decl))
8385 || TREE_CODE (decl) == FUNCTION_DECL)
8386 && DECL_RTL_SET_P (decl))
8387 make_decl_rtl (decl);
8390 return false;
8393 /* Handle an "tls_model" attribute; arguments as in
8394 struct attribute_spec.handler. */
8396 static tree
8397 handle_tls_model_attribute (tree *node, tree name, tree args,
8398 int ARG_UNUSED (flags), bool *no_add_attrs)
8400 tree id;
8401 tree decl = *node;
8402 enum tls_model kind;
8404 *no_add_attrs = true;
8406 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
8408 warning (OPT_Wattributes, "%qE attribute ignored", name);
8409 return NULL_TREE;
8412 kind = DECL_TLS_MODEL (decl);
8413 id = TREE_VALUE (args);
8414 if (TREE_CODE (id) != STRING_CST)
8416 error ("tls_model argument not a string");
8417 return NULL_TREE;
8420 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8421 kind = TLS_MODEL_LOCAL_EXEC;
8422 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8423 kind = TLS_MODEL_INITIAL_EXEC;
8424 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8425 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8426 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8427 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8428 else
8429 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8431 set_decl_tls_model (decl, kind);
8432 return NULL_TREE;
8435 /* Handle a "no_instrument_function" attribute; arguments as in
8436 struct attribute_spec.handler. */
8438 static tree
8439 handle_no_instrument_function_attribute (tree *node, tree name,
8440 tree ARG_UNUSED (args),
8441 int ARG_UNUSED (flags),
8442 bool *no_add_attrs)
8444 tree decl = *node;
8446 if (TREE_CODE (decl) != FUNCTION_DECL)
8448 error_at (DECL_SOURCE_LOCATION (decl),
8449 "%qE attribute applies only to functions", name);
8450 *no_add_attrs = true;
8452 else
8453 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8455 return NULL_TREE;
8458 /* Handle a "no_profile_instrument_function" attribute; arguments as in
8459 struct attribute_spec.handler. */
8461 static tree
8462 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
8463 int, bool *no_add_attrs)
8465 if (TREE_CODE (*node) != FUNCTION_DECL)
8467 warning (OPT_Wattributes, "%qE attribute ignored", name);
8468 *no_add_attrs = true;
8471 return NULL_TREE;
8474 /* Handle a "malloc" attribute; arguments as in
8475 struct attribute_spec.handler. */
8477 static tree
8478 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8479 int ARG_UNUSED (flags), bool *no_add_attrs)
8481 if (TREE_CODE (*node) == FUNCTION_DECL
8482 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8483 DECL_IS_MALLOC (*node) = 1;
8484 else
8486 warning (OPT_Wattributes, "%qE attribute ignored", name);
8487 *no_add_attrs = true;
8490 return NULL_TREE;
8493 /* Handle a "alloc_size" attribute; arguments as in
8494 struct attribute_spec.handler. */
8496 static tree
8497 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8498 int ARG_UNUSED (flags), bool *no_add_attrs)
8500 unsigned arg_count = type_num_arguments (*node);
8501 for (; args; args = TREE_CHAIN (args))
8503 tree position = TREE_VALUE (args);
8504 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8505 && TREE_CODE (position) != FUNCTION_DECL)
8506 position = default_conversion (position);
8508 if (!tree_fits_uhwi_p (position)
8509 || !arg_count
8510 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8512 warning (OPT_Wattributes,
8513 "alloc_size parameter outside range");
8514 *no_add_attrs = true;
8515 return NULL_TREE;
8518 return NULL_TREE;
8521 /* Handle a "alloc_align" attribute; arguments as in
8522 struct attribute_spec.handler. */
8524 static tree
8525 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8526 bool *no_add_attrs)
8528 unsigned arg_count = type_num_arguments (*node);
8529 tree position = TREE_VALUE (args);
8530 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8531 && TREE_CODE (position) != FUNCTION_DECL)
8532 position = default_conversion (position);
8534 if (!tree_fits_uhwi_p (position)
8535 || !arg_count
8536 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8538 warning (OPT_Wattributes,
8539 "alloc_align parameter outside range");
8540 *no_add_attrs = true;
8541 return NULL_TREE;
8543 return NULL_TREE;
8546 /* Handle a "assume_aligned" attribute; arguments as in
8547 struct attribute_spec.handler. */
8549 static tree
8550 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8551 bool *no_add_attrs)
8553 for (; args; args = TREE_CHAIN (args))
8555 tree position = TREE_VALUE (args);
8556 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8557 && TREE_CODE (position) != FUNCTION_DECL)
8558 position = default_conversion (position);
8560 if (TREE_CODE (position) != INTEGER_CST)
8562 warning (OPT_Wattributes,
8563 "assume_aligned parameter not integer constant");
8564 *no_add_attrs = true;
8565 return NULL_TREE;
8568 return NULL_TREE;
8571 /* Handle a "fn spec" attribute; arguments as in
8572 struct attribute_spec.handler. */
8574 static tree
8575 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8576 tree args, int ARG_UNUSED (flags),
8577 bool *no_add_attrs ATTRIBUTE_UNUSED)
8579 gcc_assert (args
8580 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8581 && !TREE_CHAIN (args));
8582 return NULL_TREE;
8585 /* Handle a "bnd_variable_size" attribute; arguments as in
8586 struct attribute_spec.handler. */
8588 static tree
8589 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8590 int ARG_UNUSED (flags), bool *no_add_attrs)
8592 if (TREE_CODE (*node) != FIELD_DECL)
8594 warning (OPT_Wattributes, "%qE attribute ignored", name);
8595 *no_add_attrs = true;
8598 return NULL_TREE;
8601 /* Handle a "bnd_legacy" attribute; arguments as in
8602 struct attribute_spec.handler. */
8604 static tree
8605 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8606 int ARG_UNUSED (flags), bool *no_add_attrs)
8608 if (TREE_CODE (*node) != FUNCTION_DECL)
8610 warning (OPT_Wattributes, "%qE attribute ignored", name);
8611 *no_add_attrs = true;
8614 return NULL_TREE;
8617 /* Handle a "bnd_instrument" attribute; arguments as in
8618 struct attribute_spec.handler. */
8620 static tree
8621 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8622 int ARG_UNUSED (flags), bool *no_add_attrs)
8624 if (TREE_CODE (*node) != FUNCTION_DECL)
8626 warning (OPT_Wattributes, "%qE attribute ignored", name);
8627 *no_add_attrs = true;
8630 return NULL_TREE;
8633 /* Handle a "warn_unused" attribute; arguments as in
8634 struct attribute_spec.handler. */
8636 static tree
8637 handle_warn_unused_attribute (tree *node, tree name,
8638 tree args ATTRIBUTE_UNUSED,
8639 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8641 if (TYPE_P (*node))
8642 /* Do nothing else, just set the attribute. We'll get at
8643 it later with lookup_attribute. */
8645 else
8647 warning (OPT_Wattributes, "%qE attribute ignored", name);
8648 *no_add_attrs = true;
8651 return NULL_TREE;
8654 /* Handle an "omp declare simd" attribute; arguments as in
8655 struct attribute_spec.handler. */
8657 static tree
8658 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8660 return NULL_TREE;
8663 /* Handle a "simd" attribute. */
8665 static tree
8666 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
8668 if (TREE_CODE (*node) == FUNCTION_DECL)
8670 if (lookup_attribute ("cilk simd function",
8671 DECL_ATTRIBUTES (*node)) != NULL)
8673 error_at (DECL_SOURCE_LOCATION (*node),
8674 "%<__simd__%> attribute cannot be used in the same "
8675 "function marked as a Cilk Plus SIMD-enabled function");
8676 *no_add_attrs = true;
8678 else
8680 tree t = get_identifier ("omp declare simd");
8681 tree attr = NULL_TREE;
8682 if (args)
8684 tree id = TREE_VALUE (args);
8686 if (TREE_CODE (id) != STRING_CST)
8688 error ("attribute %qE argument not a string", name);
8689 *no_add_attrs = true;
8690 return NULL_TREE;
8693 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8694 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8695 OMP_CLAUSE_NOTINBRANCH);
8696 else
8697 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8698 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8699 OMP_CLAUSE_INBRANCH);
8700 else
8702 error ("only %<inbranch%> and %<notinbranch%> flags are "
8703 "allowed for %<__simd__%> attribute");
8704 *no_add_attrs = true;
8705 return NULL_TREE;
8709 DECL_ATTRIBUTES (*node) = tree_cons (t,
8710 build_tree_list (NULL_TREE,
8711 attr),
8712 DECL_ATTRIBUTES (*node));
8715 else
8717 warning (OPT_Wattributes, "%qE attribute ignored", name);
8718 *no_add_attrs = true;
8721 return NULL_TREE;
8724 /* Handle an "omp declare target" attribute; arguments as in
8725 struct attribute_spec.handler. */
8727 static tree
8728 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8730 return NULL_TREE;
8733 /* Handle a "returns_twice" attribute; arguments as in
8734 struct attribute_spec.handler. */
8736 static tree
8737 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8738 int ARG_UNUSED (flags), bool *no_add_attrs)
8740 if (TREE_CODE (*node) == FUNCTION_DECL)
8741 DECL_IS_RETURNS_TWICE (*node) = 1;
8742 else
8744 warning (OPT_Wattributes, "%qE attribute ignored", name);
8745 *no_add_attrs = true;
8748 return NULL_TREE;
8751 /* Handle a "no_limit_stack" attribute; arguments as in
8752 struct attribute_spec.handler. */
8754 static tree
8755 handle_no_limit_stack_attribute (tree *node, tree name,
8756 tree ARG_UNUSED (args),
8757 int ARG_UNUSED (flags),
8758 bool *no_add_attrs)
8760 tree decl = *node;
8762 if (TREE_CODE (decl) != FUNCTION_DECL)
8764 error_at (DECL_SOURCE_LOCATION (decl),
8765 "%qE attribute applies only to functions", name);
8766 *no_add_attrs = true;
8768 else if (DECL_INITIAL (decl))
8770 error_at (DECL_SOURCE_LOCATION (decl),
8771 "can%'t set %qE attribute after definition", name);
8772 *no_add_attrs = true;
8774 else
8775 DECL_NO_LIMIT_STACK (decl) = 1;
8777 return NULL_TREE;
8780 /* Handle a "pure" attribute; arguments as in
8781 struct attribute_spec.handler. */
8783 static tree
8784 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8785 int ARG_UNUSED (flags), bool *no_add_attrs)
8787 if (TREE_CODE (*node) == FUNCTION_DECL)
8788 DECL_PURE_P (*node) = 1;
8789 /* ??? TODO: Support types. */
8790 else
8792 warning (OPT_Wattributes, "%qE attribute ignored", name);
8793 *no_add_attrs = true;
8796 return NULL_TREE;
8799 /* Digest an attribute list destined for a transactional memory statement.
8800 ALLOWED is the set of attributes that are allowed for this statement;
8801 return the attribute we parsed. Multiple attributes are never allowed. */
8804 parse_tm_stmt_attr (tree attrs, int allowed)
8806 tree a_seen = NULL;
8807 int m_seen = 0;
8809 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8811 tree a = TREE_PURPOSE (attrs);
8812 int m = 0;
8814 if (is_attribute_p ("outer", a))
8815 m = TM_STMT_ATTR_OUTER;
8817 if ((m & allowed) == 0)
8819 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8820 continue;
8823 if (m_seen == 0)
8825 a_seen = a;
8826 m_seen = m;
8828 else if (m_seen == m)
8829 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8830 else
8831 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8834 return m_seen;
8837 /* Transform a TM attribute name into a maskable integer and back.
8838 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8839 to how the lack of an attribute is treated. */
8842 tm_attr_to_mask (tree attr)
8844 if (attr == NULL)
8845 return 0;
8846 if (is_attribute_p ("transaction_safe", attr))
8847 return TM_ATTR_SAFE;
8848 if (is_attribute_p ("transaction_callable", attr))
8849 return TM_ATTR_CALLABLE;
8850 if (is_attribute_p ("transaction_pure", attr))
8851 return TM_ATTR_PURE;
8852 if (is_attribute_p ("transaction_unsafe", attr))
8853 return TM_ATTR_IRREVOCABLE;
8854 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8855 return TM_ATTR_MAY_CANCEL_OUTER;
8856 return 0;
8859 tree
8860 tm_mask_to_attr (int mask)
8862 const char *str;
8863 switch (mask)
8865 case TM_ATTR_SAFE:
8866 str = "transaction_safe";
8867 break;
8868 case TM_ATTR_CALLABLE:
8869 str = "transaction_callable";
8870 break;
8871 case TM_ATTR_PURE:
8872 str = "transaction_pure";
8873 break;
8874 case TM_ATTR_IRREVOCABLE:
8875 str = "transaction_unsafe";
8876 break;
8877 case TM_ATTR_MAY_CANCEL_OUTER:
8878 str = "transaction_may_cancel_outer";
8879 break;
8880 default:
8881 gcc_unreachable ();
8883 return get_identifier (str);
8886 /* Return the first TM attribute seen in LIST. */
8888 tree
8889 find_tm_attribute (tree list)
8891 for (; list ; list = TREE_CHAIN (list))
8893 tree name = TREE_PURPOSE (list);
8894 if (tm_attr_to_mask (name) != 0)
8895 return name;
8897 return NULL_TREE;
8900 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8901 Here we accept only function types, and verify that none of the other
8902 function TM attributes are also applied. */
8903 /* ??? We need to accept class types for C++, but not C. This greatly
8904 complicates this function, since we can no longer rely on the extra
8905 processing given by function_type_required. */
8907 static tree
8908 handle_tm_attribute (tree *node, tree name, tree args,
8909 int flags, bool *no_add_attrs)
8911 /* Only one path adds the attribute; others don't. */
8912 *no_add_attrs = true;
8914 switch (TREE_CODE (*node))
8916 case RECORD_TYPE:
8917 case UNION_TYPE:
8918 /* Only tm_callable and tm_safe apply to classes. */
8919 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8920 goto ignored;
8921 /* FALLTHRU */
8923 case FUNCTION_TYPE:
8924 case METHOD_TYPE:
8926 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8927 if (old_name == name)
8929 else if (old_name != NULL_TREE)
8930 error ("type was previously declared %qE", old_name);
8931 else
8932 *no_add_attrs = false;
8934 break;
8936 case FUNCTION_DECL:
8938 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8939 want to set transaction_safe on the type. */
8940 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8941 if (!TYPE_P (DECL_CONTEXT (*node)))
8942 error_at (DECL_SOURCE_LOCATION (*node),
8943 "%<transaction_safe_dynamic%> may only be specified for "
8944 "a virtual function");
8945 *no_add_attrs = false;
8946 decl_attributes (&TREE_TYPE (*node),
8947 build_tree_list (get_identifier ("transaction_safe"),
8948 NULL_TREE),
8950 break;
8953 case POINTER_TYPE:
8955 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8956 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8958 tree fn_tmp = TREE_TYPE (*node);
8959 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8960 *node = build_pointer_type (fn_tmp);
8961 break;
8964 /* FALLTHRU */
8966 default:
8967 /* If a function is next, pass it on to be tried next. */
8968 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8969 return tree_cons (name, args, NULL);
8971 ignored:
8972 warning (OPT_Wattributes, "%qE attribute ignored", name);
8973 break;
8976 return NULL_TREE;
8979 /* Handle the TM_WRAP attribute; arguments as in
8980 struct attribute_spec.handler. */
8982 static tree
8983 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8984 int ARG_UNUSED (flags), bool *no_add_attrs)
8986 tree decl = *node;
8988 /* We don't need the attribute even on success, since we
8989 record the entry in an external table. */
8990 *no_add_attrs = true;
8992 if (TREE_CODE (decl) != FUNCTION_DECL)
8993 warning (OPT_Wattributes, "%qE attribute ignored", name);
8994 else
8996 tree wrap_decl = TREE_VALUE (args);
8997 if (error_operand_p (wrap_decl))
8999 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
9000 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
9001 error ("%qE argument not an identifier", name);
9002 else
9004 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9005 wrap_decl = lookup_name (wrap_decl);
9006 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9008 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9009 TREE_TYPE (wrap_decl)))
9010 record_tm_replacement (wrap_decl, decl);
9011 else
9012 error ("%qD is not compatible with %qD", wrap_decl, decl);
9014 else
9015 error ("%qE argument is not a function", name);
9019 return NULL_TREE;
9022 /* Ignore the given attribute. Used when this attribute may be usefully
9023 overridden by the target, but is not used generically. */
9025 static tree
9026 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9027 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9028 bool *no_add_attrs)
9030 *no_add_attrs = true;
9031 return NULL_TREE;
9034 /* Handle a "no vops" attribute; arguments as in
9035 struct attribute_spec.handler. */
9037 static tree
9038 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9039 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9040 bool *ARG_UNUSED (no_add_attrs))
9042 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9043 DECL_IS_NOVOPS (*node) = 1;
9044 return NULL_TREE;
9047 /* Handle a "deprecated" attribute; arguments as in
9048 struct attribute_spec.handler. */
9050 static tree
9051 handle_deprecated_attribute (tree *node, tree name,
9052 tree args, int flags,
9053 bool *no_add_attrs)
9055 tree type = NULL_TREE;
9056 int warn = 0;
9057 tree what = NULL_TREE;
9059 if (!args)
9060 *no_add_attrs = true;
9061 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9063 error ("deprecated message is not a string");
9064 *no_add_attrs = true;
9067 if (DECL_P (*node))
9069 tree decl = *node;
9070 type = TREE_TYPE (decl);
9072 if (TREE_CODE (decl) == TYPE_DECL
9073 || TREE_CODE (decl) == PARM_DECL
9074 || VAR_OR_FUNCTION_DECL_P (decl)
9075 || TREE_CODE (decl) == FIELD_DECL
9076 || TREE_CODE (decl) == CONST_DECL
9077 || objc_method_decl (TREE_CODE (decl)))
9078 TREE_DEPRECATED (decl) = 1;
9079 else
9080 warn = 1;
9082 else if (TYPE_P (*node))
9084 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
9085 *node = build_variant_type_copy (*node);
9086 TREE_DEPRECATED (*node) = 1;
9087 type = *node;
9089 else
9090 warn = 1;
9092 if (warn)
9094 *no_add_attrs = true;
9095 if (type && TYPE_NAME (type))
9097 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
9098 what = TYPE_NAME (*node);
9099 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9100 && DECL_NAME (TYPE_NAME (type)))
9101 what = DECL_NAME (TYPE_NAME (type));
9103 if (what)
9104 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
9105 else
9106 warning (OPT_Wattributes, "%qE attribute ignored", name);
9109 return NULL_TREE;
9112 /* Handle a "vector_size" attribute; arguments as in
9113 struct attribute_spec.handler. */
9115 static tree
9116 handle_vector_size_attribute (tree *node, tree name, tree args,
9117 int ARG_UNUSED (flags),
9118 bool *no_add_attrs)
9120 unsigned HOST_WIDE_INT vecsize, nunits;
9121 machine_mode orig_mode;
9122 tree type = *node, new_type, size;
9124 *no_add_attrs = true;
9126 size = TREE_VALUE (args);
9127 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9128 && TREE_CODE (size) != FUNCTION_DECL)
9129 size = default_conversion (size);
9131 if (!tree_fits_uhwi_p (size))
9133 warning (OPT_Wattributes, "%qE attribute ignored", name);
9134 return NULL_TREE;
9137 /* Get the vector size (in bytes). */
9138 vecsize = tree_to_uhwi (size);
9140 /* We need to provide for vector pointers, vector arrays, and
9141 functions returning vectors. For example:
9143 __attribute__((vector_size(16))) short *foo;
9145 In this case, the mode is SI, but the type being modified is
9146 HI, so we need to look further. */
9148 while (POINTER_TYPE_P (type)
9149 || TREE_CODE (type) == FUNCTION_TYPE
9150 || TREE_CODE (type) == METHOD_TYPE
9151 || TREE_CODE (type) == ARRAY_TYPE
9152 || TREE_CODE (type) == OFFSET_TYPE)
9153 type = TREE_TYPE (type);
9155 /* Get the mode of the type being modified. */
9156 orig_mode = TYPE_MODE (type);
9158 if ((!INTEGRAL_TYPE_P (type)
9159 && !SCALAR_FLOAT_TYPE_P (type)
9160 && !FIXED_POINT_TYPE_P (type))
9161 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9162 && GET_MODE_CLASS (orig_mode) != MODE_INT
9163 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
9164 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9165 || TREE_CODE (type) == BOOLEAN_TYPE)
9167 error ("invalid vector type for attribute %qE", name);
9168 return NULL_TREE;
9171 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
9173 error ("vector size not an integral multiple of component size");
9174 return NULL;
9177 if (vecsize == 0)
9179 error ("zero vector size");
9180 return NULL;
9183 /* Calculate how many units fit in the vector. */
9184 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
9185 if (nunits & (nunits - 1))
9187 error ("number of components of the vector not a power of two");
9188 return NULL_TREE;
9191 new_type = build_vector_type (type, nunits);
9193 /* Build back pointers if needed. */
9194 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9196 return NULL_TREE;
9199 /* Handle the "nonnull" attribute. */
9200 static tree
9201 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9202 tree args, int ARG_UNUSED (flags),
9203 bool *no_add_attrs)
9205 tree type = *node;
9206 unsigned HOST_WIDE_INT attr_arg_num;
9208 /* If no arguments are specified, all pointer arguments should be
9209 non-null. Verify a full prototype is given so that the arguments
9210 will have the correct types when we actually check them later.
9211 Avoid diagnosing type-generic built-ins since those have no
9212 prototype. */
9213 if (!args)
9215 if (!prototype_p (type)
9216 && (!TYPE_ATTRIBUTES (type)
9217 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
9219 error ("nonnull attribute without arguments on a non-prototype");
9220 *no_add_attrs = true;
9222 return NULL_TREE;
9225 /* Argument list specified. Verify that each argument number references
9226 a pointer argument. */
9227 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9229 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9231 tree arg = TREE_VALUE (args);
9232 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9233 && TREE_CODE (arg) != FUNCTION_DECL)
9234 arg = default_conversion (arg);
9236 if (!get_nonnull_operand (arg, &arg_num))
9238 error ("nonnull argument has invalid operand number (argument %lu)",
9239 (unsigned long) attr_arg_num);
9240 *no_add_attrs = true;
9241 return NULL_TREE;
9244 if (prototype_p (type))
9246 function_args_iterator iter;
9247 tree argument;
9249 function_args_iter_init (&iter, type);
9250 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9252 argument = function_args_iter_cond (&iter);
9253 if (argument == NULL_TREE || ck_num == arg_num)
9254 break;
9257 if (!argument
9258 || TREE_CODE (argument) == VOID_TYPE)
9260 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9261 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9262 *no_add_attrs = true;
9263 return NULL_TREE;
9266 if (TREE_CODE (argument) != POINTER_TYPE)
9268 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9269 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9270 *no_add_attrs = true;
9271 return NULL_TREE;
9276 return NULL_TREE;
9279 /* Check the argument list of a function call for null in argument slots
9280 that are marked as requiring a non-null pointer argument. The NARGS
9281 arguments are passed in the array ARGARRAY. */
9283 static void
9284 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
9286 tree a;
9287 int i;
9289 attrs = lookup_attribute ("nonnull", attrs);
9290 if (attrs == NULL_TREE)
9291 return;
9293 a = attrs;
9294 /* See if any of the nonnull attributes has no arguments. If so,
9295 then every pointer argument is checked (in which case the check
9296 for pointer type is done in check_nonnull_arg). */
9297 if (TREE_VALUE (a) != NULL_TREE)
9299 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9300 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9302 if (a != NULL_TREE)
9303 for (i = 0; i < nargs; i++)
9304 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
9305 i + 1);
9306 else
9308 /* Walk the argument list. If we encounter an argument number we
9309 should check for non-null, do it. */
9310 for (i = 0; i < nargs; i++)
9312 for (a = attrs; ; a = TREE_CHAIN (a))
9314 a = lookup_attribute ("nonnull", a);
9315 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9316 break;
9319 if (a != NULL_TREE)
9320 check_function_arguments_recurse (check_nonnull_arg, &loc,
9321 argarray[i], i + 1);
9326 /* Check that the Nth argument of a function call (counting backwards
9327 from the end) is a (pointer)0. The NARGS arguments are passed in the
9328 array ARGARRAY. */
9330 static void
9331 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9333 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9335 if (attr)
9337 int len = 0;
9338 int pos = 0;
9339 tree sentinel;
9340 function_args_iterator iter;
9341 tree t;
9343 /* Skip over the named arguments. */
9344 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9346 if (len == nargs)
9347 break;
9348 len++;
9351 if (TREE_VALUE (attr))
9353 tree p = TREE_VALUE (TREE_VALUE (attr));
9354 pos = TREE_INT_CST_LOW (p);
9357 /* The sentinel must be one of the varargs, i.e.
9358 in position >= the number of fixed arguments. */
9359 if ((nargs - 1 - pos) < len)
9361 warning (OPT_Wformat_,
9362 "not enough variable arguments to fit a sentinel");
9363 return;
9366 /* Validate the sentinel. */
9367 sentinel = argarray[nargs - 1 - pos];
9368 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9369 || !integer_zerop (sentinel))
9370 /* Although __null (in C++) is only an integer we allow it
9371 nevertheless, as we are guaranteed that it's exactly
9372 as wide as a pointer, and we don't want to force
9373 users to cast the NULL they have written there.
9374 We warn with -Wstrict-null-sentinel, though. */
9375 && (warn_strict_null_sentinel || null_node != sentinel))
9376 warning (OPT_Wformat_, "missing sentinel in function call");
9380 /* Helper for check_function_nonnull; given a list of operands which
9381 must be non-null in ARGS, determine if operand PARAM_NUM should be
9382 checked. */
9384 static bool
9385 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9387 unsigned HOST_WIDE_INT arg_num = 0;
9389 for (; args; args = TREE_CHAIN (args))
9391 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9393 gcc_assert (found);
9395 if (arg_num == param_num)
9396 return true;
9398 return false;
9401 /* Check that the function argument PARAM (which is operand number
9402 PARAM_NUM) is non-null. This is called by check_function_nonnull
9403 via check_function_arguments_recurse. */
9405 static void
9406 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
9408 location_t *ploc = (location_t *) ctx;
9410 /* Just skip checking the argument if it's not a pointer. This can
9411 happen if the "nonnull" attribute was given without an operand
9412 list (which means to check every pointer argument). */
9414 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9415 return;
9417 if (integer_zerop (param))
9418 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9419 "(argument %lu)", (unsigned long) param_num);
9422 /* Helper for nonnull attribute handling; fetch the operand number
9423 from the attribute argument list. */
9425 static bool
9426 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9428 /* Verify the arg number is a small constant. */
9429 if (tree_fits_uhwi_p (arg_num_expr))
9431 *valp = TREE_INT_CST_LOW (arg_num_expr);
9432 return true;
9434 else
9435 return false;
9438 /* Handle a "nothrow" attribute; arguments as in
9439 struct attribute_spec.handler. */
9441 static tree
9442 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9443 int ARG_UNUSED (flags), bool *no_add_attrs)
9445 if (TREE_CODE (*node) == FUNCTION_DECL)
9446 TREE_NOTHROW (*node) = 1;
9447 /* ??? TODO: Support types. */
9448 else
9450 warning (OPT_Wattributes, "%qE attribute ignored", name);
9451 *no_add_attrs = true;
9454 return NULL_TREE;
9457 /* Handle a "cleanup" attribute; arguments as in
9458 struct attribute_spec.handler. */
9460 static tree
9461 handle_cleanup_attribute (tree *node, tree name, tree args,
9462 int ARG_UNUSED (flags), bool *no_add_attrs)
9464 tree decl = *node;
9465 tree cleanup_id, cleanup_decl;
9467 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9468 for global destructors in C++. This requires infrastructure that
9469 we don't have generically at the moment. It's also not a feature
9470 we'd be missing too much, since we do have attribute constructor. */
9471 if (!VAR_P (decl) || TREE_STATIC (decl))
9473 warning (OPT_Wattributes, "%qE attribute ignored", name);
9474 *no_add_attrs = true;
9475 return NULL_TREE;
9478 /* Verify that the argument is a function in scope. */
9479 /* ??? We could support pointers to functions here as well, if
9480 that was considered desirable. */
9481 cleanup_id = TREE_VALUE (args);
9482 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9484 error ("cleanup argument not an identifier");
9485 *no_add_attrs = true;
9486 return NULL_TREE;
9488 cleanup_decl = lookup_name (cleanup_id);
9489 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9491 error ("cleanup argument not a function");
9492 *no_add_attrs = true;
9493 return NULL_TREE;
9496 /* That the function has proper type is checked with the
9497 eventual call to build_function_call. */
9499 return NULL_TREE;
9502 /* Handle a "warn_unused_result" attribute. No special handling. */
9504 static tree
9505 handle_warn_unused_result_attribute (tree *node, tree name,
9506 tree ARG_UNUSED (args),
9507 int ARG_UNUSED (flags), bool *no_add_attrs)
9509 /* Ignore the attribute for functions not returning any value. */
9510 if (VOID_TYPE_P (TREE_TYPE (*node)))
9512 warning (OPT_Wattributes, "%qE attribute ignored", name);
9513 *no_add_attrs = true;
9516 return NULL_TREE;
9519 /* Handle a "sentinel" attribute. */
9521 static tree
9522 handle_sentinel_attribute (tree *node, tree name, tree args,
9523 int ARG_UNUSED (flags), bool *no_add_attrs)
9525 if (!prototype_p (*node))
9527 warning (OPT_Wattributes,
9528 "%qE attribute requires prototypes with named arguments", name);
9529 *no_add_attrs = true;
9531 else
9533 if (!stdarg_p (*node))
9535 warning (OPT_Wattributes,
9536 "%qE attribute only applies to variadic functions", name);
9537 *no_add_attrs = true;
9541 if (args)
9543 tree position = TREE_VALUE (args);
9544 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9545 && TREE_CODE (position) != FUNCTION_DECL)
9546 position = default_conversion (position);
9548 if (TREE_CODE (position) != INTEGER_CST
9549 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9551 warning (OPT_Wattributes,
9552 "requested position is not an integer constant");
9553 *no_add_attrs = true;
9555 else
9557 if (tree_int_cst_lt (position, integer_zero_node))
9559 warning (OPT_Wattributes,
9560 "requested position is less than zero");
9561 *no_add_attrs = true;
9566 return NULL_TREE;
9569 /* Handle a "type_generic" attribute. */
9571 static tree
9572 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9573 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9574 bool * ARG_UNUSED (no_add_attrs))
9576 /* Ensure we have a function type. */
9577 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9579 /* Ensure we have a variadic function. */
9580 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9582 return NULL_TREE;
9585 /* Handle a "target" attribute. */
9587 static tree
9588 handle_target_attribute (tree *node, tree name, tree args, int flags,
9589 bool *no_add_attrs)
9591 /* Ensure we have a function type. */
9592 if (TREE_CODE (*node) != FUNCTION_DECL)
9594 warning (OPT_Wattributes, "%qE attribute ignored", name);
9595 *no_add_attrs = true;
9597 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9599 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9600 "with %qs attribute", name, "target_clones");
9601 *no_add_attrs = true;
9603 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9604 flags))
9605 *no_add_attrs = true;
9607 return NULL_TREE;
9610 /* Handle a "target_clones" attribute. */
9612 static tree
9613 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9614 int ARG_UNUSED (flags), bool *no_add_attrs)
9616 /* Ensure we have a function type. */
9617 if (TREE_CODE (*node) == FUNCTION_DECL)
9619 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9621 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9622 "with %qs attribute", name, "always_inline");
9623 *no_add_attrs = true;
9625 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9627 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9628 "with %qs attribute", name, "target");
9629 *no_add_attrs = true;
9631 else
9632 /* Do not inline functions with multiple clone targets. */
9633 DECL_UNINLINABLE (*node) = 1;
9635 else
9637 warning (OPT_Wattributes, "%qE attribute ignored", name);
9638 *no_add_attrs = true;
9640 return NULL_TREE;
9643 /* Arguments being collected for optimization. */
9644 typedef const char *const_char_p; /* For DEF_VEC_P. */
9645 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9648 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9649 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9650 false for #pragma GCC optimize. */
9652 bool
9653 parse_optimize_options (tree args, bool attr_p)
9655 bool ret = true;
9656 unsigned opt_argc;
9657 unsigned i;
9658 const char **opt_argv;
9659 struct cl_decoded_option *decoded_options;
9660 unsigned int decoded_options_count;
9661 tree ap;
9663 /* Build up argv vector. Just in case the string is stored away, use garbage
9664 collected strings. */
9665 vec_safe_truncate (optimize_args, 0);
9666 vec_safe_push (optimize_args, (const char *) NULL);
9668 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9670 tree value = TREE_VALUE (ap);
9672 if (TREE_CODE (value) == INTEGER_CST)
9674 char buffer[20];
9675 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9676 vec_safe_push (optimize_args, ggc_strdup (buffer));
9679 else if (TREE_CODE (value) == STRING_CST)
9681 /* Split string into multiple substrings. */
9682 size_t len = TREE_STRING_LENGTH (value);
9683 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9684 char *end = p + len;
9685 char *comma;
9686 char *next_p = p;
9688 while (next_p != NULL)
9690 size_t len2;
9691 char *q, *r;
9693 p = next_p;
9694 comma = strchr (p, ',');
9695 if (comma)
9697 len2 = comma - p;
9698 *comma = '\0';
9699 next_p = comma+1;
9701 else
9703 len2 = end - p;
9704 next_p = NULL;
9707 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9709 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9710 options. */
9711 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9713 ret = false;
9714 if (attr_p)
9715 warning (OPT_Wattributes,
9716 "bad option %qs to attribute %<optimize%>", p);
9717 else
9718 warning (OPT_Wpragmas,
9719 "bad option %qs to pragma %<optimize%>", p);
9720 continue;
9723 if (*p != '-')
9725 *r++ = '-';
9727 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9728 itself is -Os, and any other switch begins with a -f. */
9729 if ((*p >= '0' && *p <= '9')
9730 || (p[0] == 's' && p[1] == '\0'))
9731 *r++ = 'O';
9732 else if (*p != 'O')
9733 *r++ = 'f';
9736 memcpy (r, p, len2);
9737 r[len2] = '\0';
9738 vec_safe_push (optimize_args, (const char *) q);
9744 opt_argc = optimize_args->length ();
9745 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9747 for (i = 1; i < opt_argc; i++)
9748 opt_argv[i] = (*optimize_args)[i];
9750 /* Now parse the options. */
9751 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9752 &decoded_options,
9753 &decoded_options_count);
9754 /* Drop non-Optimization options. */
9755 unsigned j = 1;
9756 for (i = 1; i < decoded_options_count; ++i)
9758 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
9760 ret = false;
9761 if (attr_p)
9762 warning (OPT_Wattributes,
9763 "bad option %qs to attribute %<optimize%>",
9764 decoded_options[i].orig_option_with_args_text);
9765 else
9766 warning (OPT_Wpragmas,
9767 "bad option %qs to pragma %<optimize%>",
9768 decoded_options[i].orig_option_with_args_text);
9769 continue;
9771 if (i != j)
9772 decoded_options[j] = decoded_options[i];
9773 j++;
9775 decoded_options_count = j;
9776 /* And apply them. */
9777 decode_options (&global_options, &global_options_set,
9778 decoded_options, decoded_options_count,
9779 input_location, global_dc);
9781 targetm.override_options_after_change();
9783 optimize_args->truncate (0);
9784 return ret;
9787 /* For handling "optimize" attribute. arguments as in
9788 struct attribute_spec.handler. */
9790 static tree
9791 handle_optimize_attribute (tree *node, tree name, tree args,
9792 int ARG_UNUSED (flags), bool *no_add_attrs)
9794 /* Ensure we have a function type. */
9795 if (TREE_CODE (*node) != FUNCTION_DECL)
9797 warning (OPT_Wattributes, "%qE attribute ignored", name);
9798 *no_add_attrs = true;
9800 else
9802 struct cl_optimization cur_opts;
9803 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9805 /* Save current options. */
9806 cl_optimization_save (&cur_opts, &global_options);
9808 /* If we previously had some optimization options, use them as the
9809 default. */
9810 if (old_opts)
9811 cl_optimization_restore (&global_options,
9812 TREE_OPTIMIZATION (old_opts));
9814 /* Parse options, and update the vector. */
9815 parse_optimize_options (args, true);
9816 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9817 = build_optimization_node (&global_options);
9819 /* Restore current options. */
9820 cl_optimization_restore (&global_options, &cur_opts);
9823 return NULL_TREE;
9826 /* Handle a "no_split_stack" attribute. */
9828 static tree
9829 handle_no_split_stack_attribute (tree *node, tree name,
9830 tree ARG_UNUSED (args),
9831 int ARG_UNUSED (flags),
9832 bool *no_add_attrs)
9834 tree decl = *node;
9836 if (TREE_CODE (decl) != FUNCTION_DECL)
9838 error_at (DECL_SOURCE_LOCATION (decl),
9839 "%qE attribute applies only to functions", name);
9840 *no_add_attrs = true;
9842 else if (DECL_INITIAL (decl))
9844 error_at (DECL_SOURCE_LOCATION (decl),
9845 "can%'t set %qE attribute after definition", name);
9846 *no_add_attrs = true;
9849 return NULL_TREE;
9852 /* Handle a "returns_nonnull" attribute; arguments as in
9853 struct attribute_spec.handler. */
9855 static tree
9856 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9857 bool *no_add_attrs)
9859 // Even without a prototype we still have a return type we can check.
9860 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9862 error ("returns_nonnull attribute on a function not returning a pointer");
9863 *no_add_attrs = true;
9865 return NULL_TREE;
9868 /* Handle a "designated_init" attribute; arguments as in
9869 struct attribute_spec.handler. */
9871 static tree
9872 handle_designated_init_attribute (tree *node, tree name, tree, int,
9873 bool *no_add_attrs)
9875 if (TREE_CODE (*node) != RECORD_TYPE)
9877 error ("%qE attribute is only valid on %<struct%> type", name);
9878 *no_add_attrs = true;
9880 return NULL_TREE;
9884 /* Handle a "fallthrough" attribute; arguments as in struct
9885 attribute_spec.handler. */
9887 static tree
9888 handle_fallthrough_attribute (tree *, tree name, tree, int,
9889 bool *no_add_attrs)
9891 warning (OPT_Wattributes, "%qE attribute ignored", name);
9892 *no_add_attrs = true;
9893 return NULL_TREE;
9896 /* Check whether ATTR is a valid attribute fallthrough. */
9898 bool
9899 attribute_fallthrough_p (tree attr)
9901 tree t = lookup_attribute ("fallthrough", attr);
9902 if (t == NULL_TREE)
9903 return false;
9904 /* This attribute shall appear at most once in each attribute-list. */
9905 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
9906 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
9907 "times");
9908 /* No attribute-argument-clause shall be present. */
9909 else if (TREE_VALUE (t) != NULL_TREE)
9910 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
9911 "a parameter");
9912 /* Warn if other attributes are found. */
9913 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
9915 tree name = get_attribute_name (t);
9916 if (!is_attribute_p ("fallthrough", name))
9917 warning (OPT_Wattributes, "%qE attribute ignored", name);
9919 return true;
9923 /* Check for valid arguments being passed to a function with FNTYPE.
9924 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9925 diagnostics. */
9926 void
9927 check_function_arguments (location_t loc, const_tree fntype, int nargs,
9928 tree *argarray)
9930 /* Check for null being passed in a pointer argument that must be
9931 non-null. We also need to do this if format checking is enabled. */
9933 if (warn_nonnull)
9934 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
9936 /* Check for errors in format strings. */
9938 if (warn_format || warn_suggest_attribute_format)
9939 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9941 if (warn_format)
9942 check_function_sentinel (fntype, nargs, argarray);
9945 /* Generic argument checking recursion routine. PARAM is the argument to
9946 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9947 once the argument is resolved. CTX is context for the callback. */
9948 void
9949 check_function_arguments_recurse (void (*callback)
9950 (void *, tree, unsigned HOST_WIDE_INT),
9951 void *ctx, tree param,
9952 unsigned HOST_WIDE_INT param_num)
9954 if (CONVERT_EXPR_P (param)
9955 && (TYPE_PRECISION (TREE_TYPE (param))
9956 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9958 /* Strip coercion. */
9959 check_function_arguments_recurse (callback, ctx,
9960 TREE_OPERAND (param, 0), param_num);
9961 return;
9964 if (TREE_CODE (param) == CALL_EXPR)
9966 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9967 tree attrs;
9968 bool found_format_arg = false;
9970 /* See if this is a call to a known internationalization function
9971 that modifies a format arg. Such a function may have multiple
9972 format_arg attributes (for example, ngettext). */
9974 for (attrs = TYPE_ATTRIBUTES (type);
9975 attrs;
9976 attrs = TREE_CHAIN (attrs))
9977 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9979 tree inner_arg;
9980 tree format_num_expr;
9981 int format_num;
9982 int i;
9983 call_expr_arg_iterator iter;
9985 /* Extract the argument number, which was previously checked
9986 to be valid. */
9987 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9989 format_num = tree_to_uhwi (format_num_expr);
9991 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9992 inner_arg != 0;
9993 inner_arg = next_call_expr_arg (&iter), i++)
9994 if (i == format_num)
9996 check_function_arguments_recurse (callback, ctx,
9997 inner_arg, param_num);
9998 found_format_arg = true;
9999 break;
10003 /* If we found a format_arg attribute and did a recursive check,
10004 we are done with checking this argument. Otherwise, we continue
10005 and this will be considered a non-literal. */
10006 if (found_format_arg)
10007 return;
10010 if (TREE_CODE (param) == COND_EXPR)
10012 /* Simplify to avoid warning for an impossible case. */
10013 param = fold_for_warn (param);
10014 if (TREE_CODE (param) == COND_EXPR)
10016 /* Check both halves of the conditional expression. */
10017 check_function_arguments_recurse (callback, ctx,
10018 TREE_OPERAND (param, 1),
10019 param_num);
10020 check_function_arguments_recurse (callback, ctx,
10021 TREE_OPERAND (param, 2),
10022 param_num);
10023 return;
10027 (*callback) (ctx, param, param_num);
10030 /* Checks for a builtin function FNDECL that the number of arguments
10031 NARGS against the required number REQUIRED and issues an error if
10032 there is a mismatch. Returns true if the number of arguments is
10033 correct, otherwise false. LOC is the location of FNDECL. */
10035 static bool
10036 builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
10037 int required)
10039 if (nargs < required)
10041 error_at (loc, "too few arguments to function %qE", fndecl);
10042 return false;
10044 else if (nargs > required)
10046 error_at (loc, "too many arguments to function %qE", fndecl);
10047 return false;
10049 return true;
10052 /* Helper macro for check_builtin_function_arguments. */
10053 #define ARG_LOCATION(N) \
10054 (arg_loc.is_empty () \
10055 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
10056 : expansion_point_location (arg_loc[(N)]))
10058 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
10059 Returns false if there was an error, otherwise true. LOC is the
10060 location of the function; ARG_LOC is a vector of locations of the
10061 arguments. */
10063 bool
10064 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
10065 tree fndecl, int nargs, tree *args)
10067 if (!DECL_BUILT_IN (fndecl)
10068 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
10069 return true;
10071 switch (DECL_FUNCTION_CODE (fndecl))
10073 case BUILT_IN_ALLOCA_WITH_ALIGN:
10075 /* Get the requested alignment (in bits) if it's a constant
10076 integer expression. */
10077 unsigned HOST_WIDE_INT align
10078 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
10080 /* Determine if the requested alignment is a power of 2. */
10081 if ((align & (align - 1)))
10082 align = 0;
10084 /* The maximum alignment in bits corresponding to the same
10085 maximum in bytes enforced in check_user_alignment(). */
10086 unsigned maxalign = (UINT_MAX >> 1) + 1;
10088 /* Reject invalid alignments. */
10089 if (align < BITS_PER_UNIT || maxalign < align)
10091 error_at (ARG_LOCATION (1),
10092 "second argument to function %qE must be a constant "
10093 "integer power of 2 between %qi and %qu bits",
10094 fndecl, BITS_PER_UNIT, maxalign);
10095 return false;
10097 return true;
10100 case BUILT_IN_CONSTANT_P:
10101 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
10103 case BUILT_IN_ISFINITE:
10104 case BUILT_IN_ISINF:
10105 case BUILT_IN_ISINF_SIGN:
10106 case BUILT_IN_ISNAN:
10107 case BUILT_IN_ISNORMAL:
10108 case BUILT_IN_SIGNBIT:
10109 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
10111 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10113 error_at (ARG_LOCATION (0), "non-floating-point argument in "
10114 "call to function %qE", fndecl);
10115 return false;
10117 return true;
10119 return false;
10121 case BUILT_IN_ISGREATER:
10122 case BUILT_IN_ISGREATEREQUAL:
10123 case BUILT_IN_ISLESS:
10124 case BUILT_IN_ISLESSEQUAL:
10125 case BUILT_IN_ISLESSGREATER:
10126 case BUILT_IN_ISUNORDERED:
10127 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
10129 enum tree_code code0, code1;
10130 code0 = TREE_CODE (TREE_TYPE (args[0]));
10131 code1 = TREE_CODE (TREE_TYPE (args[1]));
10132 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10133 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10134 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10136 error_at (loc, "non-floating-point arguments in call to "
10137 "function %qE", fndecl);
10138 return false;
10140 return true;
10142 return false;
10144 case BUILT_IN_FPCLASSIFY:
10145 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
10147 for (unsigned int i = 0; i < 5; i++)
10148 if (TREE_CODE (args[i]) != INTEGER_CST)
10150 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
10151 "call to function %qE", i + 1, fndecl);
10152 return false;
10155 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10157 error_at (ARG_LOCATION (5), "non-floating-point argument in "
10158 "call to function %qE", fndecl);
10159 return false;
10161 return true;
10163 return false;
10165 case BUILT_IN_ASSUME_ALIGNED:
10166 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
10168 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10170 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
10171 "function %qE", fndecl);
10172 return false;
10174 return true;
10176 return false;
10178 case BUILT_IN_ADD_OVERFLOW:
10179 case BUILT_IN_SUB_OVERFLOW:
10180 case BUILT_IN_MUL_OVERFLOW:
10181 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
10183 unsigned i;
10184 for (i = 0; i < 2; i++)
10185 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10187 error_at (ARG_LOCATION (i), "argument %u in call to function "
10188 "%qE does not have integral type", i + 1, fndecl);
10189 return false;
10191 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10192 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
10194 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10195 "does not have pointer to integral type", fndecl);
10196 return false;
10198 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
10200 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10201 "has pointer to enumerated type", fndecl);
10202 return false;
10204 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
10206 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10207 "has pointer to boolean type", fndecl);
10208 return false;
10210 return true;
10212 return false;
10214 case BUILT_IN_ADD_OVERFLOW_P:
10215 case BUILT_IN_SUB_OVERFLOW_P:
10216 case BUILT_IN_MUL_OVERFLOW_P:
10217 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
10219 unsigned i;
10220 for (i = 0; i < 3; i++)
10221 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10223 error_at (ARG_LOCATION (i), "argument %u in call to function "
10224 "%qE does not have integral type", i + 1, fndecl);
10225 return false;
10227 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
10229 error_at (ARG_LOCATION (2), "argument 3 in call to function "
10230 "%qE has enumerated type", fndecl);
10231 return false;
10233 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
10235 error_at (ARG_LOCATION (2), "argument 3 in call to function "
10236 "%qE has boolean type", fndecl);
10237 return false;
10239 return true;
10241 return false;
10243 default:
10244 return true;
10248 /* Function to help qsort sort FIELD_DECLs by name order. */
10251 field_decl_cmp (const void *x_p, const void *y_p)
10253 const tree *const x = (const tree *const) x_p;
10254 const tree *const y = (const tree *const) y_p;
10256 if (DECL_NAME (*x) == DECL_NAME (*y))
10257 /* A nontype is "greater" than a type. */
10258 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10259 if (DECL_NAME (*x) == NULL_TREE)
10260 return -1;
10261 if (DECL_NAME (*y) == NULL_TREE)
10262 return 1;
10263 if (DECL_NAME (*x) < DECL_NAME (*y))
10264 return -1;
10265 return 1;
10268 static struct {
10269 gt_pointer_operator new_value;
10270 void *cookie;
10271 } resort_data;
10273 /* This routine compares two fields like field_decl_cmp but using the
10274 pointer operator in resort_data. */
10276 static int
10277 resort_field_decl_cmp (const void *x_p, const void *y_p)
10279 const tree *const x = (const tree *const) x_p;
10280 const tree *const y = (const tree *const) y_p;
10282 if (DECL_NAME (*x) == DECL_NAME (*y))
10283 /* A nontype is "greater" than a type. */
10284 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10285 if (DECL_NAME (*x) == NULL_TREE)
10286 return -1;
10287 if (DECL_NAME (*y) == NULL_TREE)
10288 return 1;
10290 tree d1 = DECL_NAME (*x);
10291 tree d2 = DECL_NAME (*y);
10292 resort_data.new_value (&d1, resort_data.cookie);
10293 resort_data.new_value (&d2, resort_data.cookie);
10294 if (d1 < d2)
10295 return -1;
10297 return 1;
10300 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10302 void
10303 resort_sorted_fields (void *obj,
10304 void * ARG_UNUSED (orig_obj),
10305 gt_pointer_operator new_value,
10306 void *cookie)
10308 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10309 resort_data.new_value = new_value;
10310 resort_data.cookie = cookie;
10311 qsort (&sf->elts[0], sf->len, sizeof (tree),
10312 resort_field_decl_cmp);
10315 /* Subroutine of c_parse_error.
10316 Return the result of concatenating LHS and RHS. RHS is really
10317 a string literal, its first character is indicated by RHS_START and
10318 RHS_SIZE is its length (including the terminating NUL character).
10320 The caller is responsible for deleting the returned pointer. */
10322 static char *
10323 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10325 const int lhs_size = strlen (lhs);
10326 char *result = XNEWVEC (char, lhs_size + rhs_size);
10327 strncpy (result, lhs, lhs_size);
10328 strncpy (result + lhs_size, rhs_start, rhs_size);
10329 return result;
10332 /* Issue the error given by GMSGID, indicating that it occurred before
10333 TOKEN, which had the associated VALUE. */
10335 void
10336 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
10337 tree value, unsigned char token_flags)
10339 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10341 char *message = NULL;
10343 if (token_type == CPP_EOF)
10344 message = catenate_messages (gmsgid, " at end of input");
10345 else if (token_type == CPP_CHAR
10346 || token_type == CPP_WCHAR
10347 || token_type == CPP_CHAR16
10348 || token_type == CPP_CHAR32
10349 || token_type == CPP_UTF8CHAR)
10351 unsigned int val = TREE_INT_CST_LOW (value);
10352 const char *prefix;
10354 switch (token_type)
10356 default:
10357 prefix = "";
10358 break;
10359 case CPP_WCHAR:
10360 prefix = "L";
10361 break;
10362 case CPP_CHAR16:
10363 prefix = "u";
10364 break;
10365 case CPP_CHAR32:
10366 prefix = "U";
10367 break;
10368 case CPP_UTF8CHAR:
10369 prefix = "u8";
10370 break;
10373 if (val <= UCHAR_MAX && ISGRAPH (val))
10374 message = catenate_messages (gmsgid, " before %s'%c'");
10375 else
10376 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10378 error (message, prefix, val);
10379 free (message);
10380 message = NULL;
10382 else if (token_type == CPP_CHAR_USERDEF
10383 || token_type == CPP_WCHAR_USERDEF
10384 || token_type == CPP_CHAR16_USERDEF
10385 || token_type == CPP_CHAR32_USERDEF
10386 || token_type == CPP_UTF8CHAR_USERDEF)
10387 message = catenate_messages (gmsgid,
10388 " before user-defined character literal");
10389 else if (token_type == CPP_STRING_USERDEF
10390 || token_type == CPP_WSTRING_USERDEF
10391 || token_type == CPP_STRING16_USERDEF
10392 || token_type == CPP_STRING32_USERDEF
10393 || token_type == CPP_UTF8STRING_USERDEF)
10394 message = catenate_messages (gmsgid, " before user-defined string literal");
10395 else if (token_type == CPP_STRING
10396 || token_type == CPP_WSTRING
10397 || token_type == CPP_STRING16
10398 || token_type == CPP_STRING32
10399 || token_type == CPP_UTF8STRING)
10400 message = catenate_messages (gmsgid, " before string constant");
10401 else if (token_type == CPP_NUMBER)
10402 message = catenate_messages (gmsgid, " before numeric constant");
10403 else if (token_type == CPP_NAME)
10405 message = catenate_messages (gmsgid, " before %qE");
10406 error (message, value);
10407 free (message);
10408 message = NULL;
10410 else if (token_type == CPP_PRAGMA)
10411 message = catenate_messages (gmsgid, " before %<#pragma%>");
10412 else if (token_type == CPP_PRAGMA_EOL)
10413 message = catenate_messages (gmsgid, " before end of line");
10414 else if (token_type == CPP_DECLTYPE)
10415 message = catenate_messages (gmsgid, " before %<decltype%>");
10416 else if (token_type < N_TTYPES)
10418 message = catenate_messages (gmsgid, " before %qs token");
10419 error (message, cpp_type2name (token_type, token_flags));
10420 free (message);
10421 message = NULL;
10423 else
10424 error (gmsgid);
10426 if (message)
10428 error (message);
10429 free (message);
10431 #undef catenate_messages
10434 /* Return the gcc option code associated with the reason for a cpp
10435 message, or 0 if none. */
10437 static int
10438 c_option_controlling_cpp_error (int reason)
10440 const struct cpp_reason_option_codes_t *entry;
10442 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10444 if (entry->reason == reason)
10445 return entry->option_code;
10447 return 0;
10450 /* Callback from cpp_error for PFILE to print diagnostics from the
10451 preprocessor. The diagnostic is of type LEVEL, with REASON set
10452 to the reason code if LEVEL is represents a warning, at location
10453 RICHLOC unless this is after lexing and the compiler's location
10454 should be used instead; MSG is the translated message and AP
10455 the arguments. Returns true if a diagnostic was emitted, false
10456 otherwise. */
10458 bool
10459 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10460 rich_location *richloc,
10461 const char *msg, va_list *ap)
10463 diagnostic_info diagnostic;
10464 diagnostic_t dlevel;
10465 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10466 bool ret;
10468 switch (level)
10470 case CPP_DL_WARNING_SYSHDR:
10471 if (flag_no_output)
10472 return false;
10473 global_dc->dc_warn_system_headers = 1;
10474 /* Fall through. */
10475 case CPP_DL_WARNING:
10476 if (flag_no_output)
10477 return false;
10478 dlevel = DK_WARNING;
10479 break;
10480 case CPP_DL_PEDWARN:
10481 if (flag_no_output && !flag_pedantic_errors)
10482 return false;
10483 dlevel = DK_PEDWARN;
10484 break;
10485 case CPP_DL_ERROR:
10486 dlevel = DK_ERROR;
10487 break;
10488 case CPP_DL_ICE:
10489 dlevel = DK_ICE;
10490 break;
10491 case CPP_DL_NOTE:
10492 dlevel = DK_NOTE;
10493 break;
10494 case CPP_DL_FATAL:
10495 dlevel = DK_FATAL;
10496 break;
10497 default:
10498 gcc_unreachable ();
10500 if (done_lexing)
10501 richloc->set_range (line_table, 0, input_location, true);
10502 diagnostic_set_info_translated (&diagnostic, msg, ap,
10503 richloc, dlevel);
10504 diagnostic_override_option_index (&diagnostic,
10505 c_option_controlling_cpp_error (reason));
10506 ret = report_diagnostic (&diagnostic);
10507 if (level == CPP_DL_WARNING_SYSHDR)
10508 global_dc->dc_warn_system_headers = save_warn_system_headers;
10509 return ret;
10512 /* Convert a character from the host to the target execution character
10513 set. cpplib handles this, mostly. */
10515 HOST_WIDE_INT
10516 c_common_to_target_charset (HOST_WIDE_INT c)
10518 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10519 zero-extended under -fno-signed-char. cpplib insists that characters
10520 and character constants are always unsigned. Hence we must convert
10521 back and forth. */
10522 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10524 uc = cpp_host_to_exec_charset (parse_in, uc);
10526 if (flag_signed_char)
10527 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10528 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10529 else
10530 return uc;
10533 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10534 references with an INDIRECT_REF of a constant at the bottom; much like the
10535 traditional rendering of offsetof as a macro. Return the folded result. */
10537 tree
10538 fold_offsetof_1 (tree expr, enum tree_code ctx)
10540 tree base, off, t;
10541 tree_code code = TREE_CODE (expr);
10542 switch (code)
10544 case ERROR_MARK:
10545 return expr;
10547 case VAR_DECL:
10548 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10549 return error_mark_node;
10551 case CALL_EXPR:
10552 case TARGET_EXPR:
10553 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10554 return error_mark_node;
10556 case NOP_EXPR:
10557 case INDIRECT_REF:
10558 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10560 error ("cannot apply %<offsetof%> to a non constant address");
10561 return error_mark_node;
10563 return TREE_OPERAND (expr, 0);
10565 case COMPONENT_REF:
10566 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10567 if (base == error_mark_node)
10568 return base;
10570 t = TREE_OPERAND (expr, 1);
10571 if (DECL_C_BIT_FIELD (t))
10573 error ("attempt to take address of bit-field structure "
10574 "member %qD", t);
10575 return error_mark_node;
10577 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10578 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10579 / BITS_PER_UNIT));
10580 break;
10582 case ARRAY_REF:
10583 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10584 if (base == error_mark_node)
10585 return base;
10587 t = TREE_OPERAND (expr, 1);
10589 /* Check if the offset goes beyond the upper bound of the array. */
10590 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10592 tree upbound = array_ref_up_bound (expr);
10593 if (upbound != NULL_TREE
10594 && TREE_CODE (upbound) == INTEGER_CST
10595 && !tree_int_cst_equal (upbound,
10596 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10598 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10599 upbound = size_binop (PLUS_EXPR, upbound,
10600 build_int_cst (TREE_TYPE (upbound), 1));
10601 if (tree_int_cst_lt (upbound, t))
10603 tree v;
10605 for (v = TREE_OPERAND (expr, 0);
10606 TREE_CODE (v) == COMPONENT_REF;
10607 v = TREE_OPERAND (v, 0))
10608 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10609 == RECORD_TYPE)
10611 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10612 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10613 if (TREE_CODE (fld_chain) == FIELD_DECL)
10614 break;
10616 if (fld_chain)
10617 break;
10619 /* Don't warn if the array might be considered a poor
10620 man's flexible array member with a very permissive
10621 definition thereof. */
10622 if (TREE_CODE (v) == ARRAY_REF
10623 || TREE_CODE (v) == COMPONENT_REF)
10624 warning (OPT_Warray_bounds,
10625 "index %E denotes an offset "
10626 "greater than size of %qT",
10627 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10632 t = convert (sizetype, t);
10633 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10634 break;
10636 case COMPOUND_EXPR:
10637 /* Handle static members of volatile structs. */
10638 t = TREE_OPERAND (expr, 1);
10639 gcc_assert (VAR_P (t));
10640 return fold_offsetof_1 (t);
10642 default:
10643 gcc_unreachable ();
10646 return fold_build_pointer_plus (base, off);
10649 /* Likewise, but convert it to the return type of offsetof. */
10651 tree
10652 fold_offsetof (tree expr)
10654 return convert (size_type_node, fold_offsetof_1 (expr));
10657 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10658 expression, because B will always be true. */
10660 void
10661 warn_for_omitted_condop (location_t location, tree cond)
10663 /* In C++ template declarations it can happen that the type is dependent
10664 and not yet known, thus TREE_TYPE (cond) == NULL_TREE. */
10665 if (truth_value_p (TREE_CODE (cond))
10666 || (TREE_TYPE (cond) != NULL_TREE
10667 && TREE_CODE (TREE_TYPE (cond)) == BOOLEAN_TYPE))
10668 warning_at (location, OPT_Wparentheses,
10669 "the omitted middle operand in ?: will always be %<true%>, "
10670 "suggest explicit middle operand");
10673 /* Give an error for storing into ARG, which is 'const'. USE indicates
10674 how ARG was being used. */
10676 void
10677 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10679 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10680 || use == lv_asm);
10681 /* Using this macro rather than (for example) arrays of messages
10682 ensures that all the format strings are checked at compile
10683 time. */
10684 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10685 : (use == lv_increment ? (I) \
10686 : (use == lv_decrement ? (D) : (AS))))
10687 if (TREE_CODE (arg) == COMPONENT_REF)
10689 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10690 error_at (loc, READONLY_MSG (G_("assignment of member "
10691 "%qD in read-only object"),
10692 G_("increment of member "
10693 "%qD in read-only object"),
10694 G_("decrement of member "
10695 "%qD in read-only object"),
10696 G_("member %qD in read-only object "
10697 "used as %<asm%> output")),
10698 TREE_OPERAND (arg, 1));
10699 else
10700 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10701 G_("increment of read-only member %qD"),
10702 G_("decrement of read-only member %qD"),
10703 G_("read-only member %qD used as %<asm%> output")),
10704 TREE_OPERAND (arg, 1));
10706 else if (VAR_P (arg))
10707 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10708 G_("increment of read-only variable %qD"),
10709 G_("decrement of read-only variable %qD"),
10710 G_("read-only variable %qD used as %<asm%> output")),
10711 arg);
10712 else if (TREE_CODE (arg) == PARM_DECL)
10713 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10714 G_("increment of read-only parameter %qD"),
10715 G_("decrement of read-only parameter %qD"),
10716 G_("read-only parameter %qD use as %<asm%> output")),
10717 arg);
10718 else if (TREE_CODE (arg) == RESULT_DECL)
10720 gcc_assert (c_dialect_cxx ());
10721 error_at (loc, READONLY_MSG (G_("assignment of "
10722 "read-only named return value %qD"),
10723 G_("increment of "
10724 "read-only named return value %qD"),
10725 G_("decrement of "
10726 "read-only named return value %qD"),
10727 G_("read-only named return value %qD "
10728 "used as %<asm%>output")),
10729 arg);
10731 else if (TREE_CODE (arg) == FUNCTION_DECL)
10732 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10733 G_("increment of function %qD"),
10734 G_("decrement of function %qD"),
10735 G_("function %qD used as %<asm%> output")),
10736 arg);
10737 else
10738 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10739 G_("increment of read-only location %qE"),
10740 G_("decrement of read-only location %qE"),
10741 G_("read-only location %qE used as %<asm%> output")),
10742 arg);
10745 /* Print an error message for an invalid lvalue. USE says
10746 how the lvalue is being used and so selects the error message. LOC
10747 is the location for the error. */
10749 void
10750 lvalue_error (location_t loc, enum lvalue_use use)
10752 switch (use)
10754 case lv_assign:
10755 error_at (loc, "lvalue required as left operand of assignment");
10756 break;
10757 case lv_increment:
10758 error_at (loc, "lvalue required as increment operand");
10759 break;
10760 case lv_decrement:
10761 error_at (loc, "lvalue required as decrement operand");
10762 break;
10763 case lv_addressof:
10764 error_at (loc, "lvalue required as unary %<&%> operand");
10765 break;
10766 case lv_asm:
10767 error_at (loc, "lvalue required in asm statement");
10768 break;
10769 default:
10770 gcc_unreachable ();
10774 /* Print an error message for an invalid indirection of type TYPE.
10775 ERRSTRING is the name of the operator for the indirection. */
10777 void
10778 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10780 switch (errstring)
10782 case RO_NULL:
10783 gcc_assert (c_dialect_cxx ());
10784 error_at (loc, "invalid type argument (have %qT)", type);
10785 break;
10786 case RO_ARRAY_INDEXING:
10787 error_at (loc,
10788 "invalid type argument of array indexing (have %qT)",
10789 type);
10790 break;
10791 case RO_UNARY_STAR:
10792 error_at (loc,
10793 "invalid type argument of unary %<*%> (have %qT)",
10794 type);
10795 break;
10796 case RO_ARROW:
10797 error_at (loc,
10798 "invalid type argument of %<->%> (have %qT)",
10799 type);
10800 break;
10801 case RO_ARROW_STAR:
10802 error_at (loc,
10803 "invalid type argument of %<->*%> (have %qT)",
10804 type);
10805 break;
10806 case RO_IMPLICIT_CONVERSION:
10807 error_at (loc,
10808 "invalid type argument of implicit conversion (have %qT)",
10809 type);
10810 break;
10811 default:
10812 gcc_unreachable ();
10816 /* *PTYPE is an incomplete array. Complete it with a domain based on
10817 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10818 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10819 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10822 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10824 tree maxindex, type, main_type, elt, unqual_elt;
10825 int failure = 0, quals;
10826 hashval_t hashcode = 0;
10827 bool overflow_p = false;
10829 maxindex = size_zero_node;
10830 if (initial_value)
10832 if (TREE_CODE (initial_value) == STRING_CST)
10834 int eltsize
10835 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10836 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10838 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10840 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10842 if (vec_safe_is_empty (v))
10844 if (pedantic)
10845 failure = 3;
10846 maxindex = ssize_int (-1);
10848 else
10850 tree curindex;
10851 unsigned HOST_WIDE_INT cnt;
10852 constructor_elt *ce;
10853 bool fold_p = false;
10855 if ((*v)[0].index)
10856 maxindex = (*v)[0].index, fold_p = true;
10858 curindex = maxindex;
10860 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10862 bool curfold_p = false;
10863 if (ce->index)
10864 curindex = ce->index, curfold_p = true;
10865 else
10867 if (fold_p)
10869 /* Since we treat size types now as ordinary
10870 unsigned types, we need an explicit overflow
10871 check. */
10872 tree orig = curindex;
10873 curindex = fold_convert (sizetype, curindex);
10874 overflow_p |= tree_int_cst_lt (curindex, orig);
10876 curindex = size_binop (PLUS_EXPR, curindex,
10877 size_one_node);
10879 if (tree_int_cst_lt (maxindex, curindex))
10880 maxindex = curindex, fold_p = curfold_p;
10882 if (fold_p)
10884 tree orig = maxindex;
10885 maxindex = fold_convert (sizetype, maxindex);
10886 overflow_p |= tree_int_cst_lt (maxindex, orig);
10890 else
10892 /* Make an error message unless that happened already. */
10893 if (initial_value != error_mark_node)
10894 failure = 1;
10897 else
10899 failure = 2;
10900 if (!do_default)
10901 return failure;
10904 type = *ptype;
10905 elt = TREE_TYPE (type);
10906 quals = TYPE_QUALS (strip_array_types (elt));
10907 if (quals == 0)
10908 unqual_elt = elt;
10909 else
10910 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10912 /* Using build_distinct_type_copy and modifying things afterward instead
10913 of using build_array_type to create a new type preserves all of the
10914 TYPE_LANG_FLAG_? bits that the front end may have set. */
10915 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10916 TREE_TYPE (main_type) = unqual_elt;
10917 TYPE_DOMAIN (main_type)
10918 = build_range_type (TREE_TYPE (maxindex),
10919 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10920 layout_type (main_type);
10922 /* Make sure we have the canonical MAIN_TYPE. */
10923 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10924 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10925 hashcode);
10926 main_type = type_hash_canon (hashcode, main_type);
10928 /* Fix the canonical type. */
10929 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10930 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10931 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10932 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10933 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10934 != TYPE_DOMAIN (main_type)))
10935 TYPE_CANONICAL (main_type)
10936 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10937 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10938 else
10939 TYPE_CANONICAL (main_type) = main_type;
10941 if (quals == 0)
10942 type = main_type;
10943 else
10944 type = c_build_qualified_type (main_type, quals);
10946 if (COMPLETE_TYPE_P (type)
10947 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10948 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10950 error ("size of array is too large");
10951 /* If we proceed with the array type as it is, we'll eventually
10952 crash in tree_to_[su]hwi(). */
10953 type = error_mark_node;
10956 *ptype = type;
10957 return failure;
10960 /* Like c_mark_addressable but don't check register qualifier. */
10961 void
10962 c_common_mark_addressable_vec (tree t)
10964 while (handled_component_p (t))
10965 t = TREE_OPERAND (t, 0);
10966 if (!VAR_P (t)
10967 && TREE_CODE (t) != PARM_DECL
10968 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
10969 return;
10970 TREE_ADDRESSABLE (t) = 1;
10975 /* Used to help initialize the builtin-types.def table. When a type of
10976 the correct size doesn't exist, use error_mark_node instead of NULL.
10977 The later results in segfaults even when a decl using the type doesn't
10978 get invoked. */
10980 tree
10981 builtin_type_for_size (int size, bool unsignedp)
10983 tree type = c_common_type_for_size (size, unsignedp);
10984 return type ? type : error_mark_node;
10987 /* A helper function for resolve_overloaded_builtin in resolving the
10988 overloaded __sync_ builtins. Returns a positive power of 2 if the
10989 first operand of PARAMS is a pointer to a supported data type.
10990 Returns 0 if an error is encountered.
10991 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10992 built-ins. */
10994 static int
10995 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
10997 /* Type of the argument. */
10998 tree argtype;
10999 /* Type the argument points to. */
11000 tree type;
11001 int size;
11003 if (vec_safe_is_empty (params))
11005 error ("too few arguments to function %qE", function);
11006 return 0;
11009 argtype = type = TREE_TYPE ((*params)[0]);
11010 if (TREE_CODE (type) == ARRAY_TYPE)
11012 /* Force array-to-pointer decay for C++. */
11013 gcc_assert (c_dialect_cxx());
11014 (*params)[0] = default_conversion ((*params)[0]);
11015 type = TREE_TYPE ((*params)[0]);
11017 if (TREE_CODE (type) != POINTER_TYPE)
11018 goto incompatible;
11020 type = TREE_TYPE (type);
11021 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
11022 goto incompatible;
11024 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
11025 goto incompatible;
11027 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
11028 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
11029 return size;
11031 incompatible:
11032 /* Issue the diagnostic only if the argument is valid, otherwise
11033 it would be redundant at best and could be misleading. */
11034 if (argtype != error_mark_node)
11035 error ("operand type %qT is incompatible with argument %d of %qE",
11036 argtype, 1, function);
11037 return 0;
11040 /* A helper function for resolve_overloaded_builtin. Adds casts to
11041 PARAMS to make arguments match up with those of FUNCTION. Drops
11042 the variadic arguments at the end. Returns false if some error
11043 was encountered; true on success. */
11045 static bool
11046 sync_resolve_params (location_t loc, tree orig_function, tree function,
11047 vec<tree, va_gc> *params, bool orig_format)
11049 function_args_iterator iter;
11050 tree ptype;
11051 unsigned int parmnum;
11053 function_args_iter_init (&iter, TREE_TYPE (function));
11054 /* We've declared the implementation functions to use "volatile void *"
11055 as the pointer parameter, so we shouldn't get any complaints from the
11056 call to check_function_arguments what ever type the user used. */
11057 function_args_iter_next (&iter);
11058 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
11059 ptype = TYPE_MAIN_VARIANT (ptype);
11061 /* For the rest of the values, we need to cast these to FTYPE, so that we
11062 don't get warnings for passing pointer types, etc. */
11063 parmnum = 0;
11064 while (1)
11066 tree val, arg_type;
11068 arg_type = function_args_iter_cond (&iter);
11069 /* XXX void_type_node belies the abstraction. */
11070 if (arg_type == void_type_node)
11071 break;
11073 ++parmnum;
11074 if (params->length () <= parmnum)
11076 error_at (loc, "too few arguments to function %qE", orig_function);
11077 return false;
11080 /* Only convert parameters if arg_type is unsigned integer type with
11081 new format sync routines, i.e. don't attempt to convert pointer
11082 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
11083 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
11084 kinds). */
11085 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
11087 /* Ideally for the first conversion we'd use convert_for_assignment
11088 so that we get warnings for anything that doesn't match the pointer
11089 type. This isn't portable across the C and C++ front ends atm. */
11090 val = (*params)[parmnum];
11091 val = convert (ptype, val);
11092 val = convert (arg_type, val);
11093 (*params)[parmnum] = val;
11096 function_args_iter_next (&iter);
11099 /* __atomic routines are not variadic. */
11100 if (!orig_format && params->length () != parmnum + 1)
11102 error_at (loc, "too many arguments to function %qE", orig_function);
11103 return false;
11106 /* The definition of these primitives is variadic, with the remaining
11107 being "an optional list of variables protected by the memory barrier".
11108 No clue what that's supposed to mean, precisely, but we consider all
11109 call-clobbered variables to be protected so we're safe. */
11110 params->truncate (parmnum + 1);
11112 return true;
11115 /* A helper function for resolve_overloaded_builtin. Adds a cast to
11116 RESULT to make it match the type of the first pointer argument in
11117 PARAMS. */
11119 static tree
11120 sync_resolve_return (tree first_param, tree result, bool orig_format)
11122 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
11123 tree rtype = TREE_TYPE (result);
11124 ptype = TYPE_MAIN_VARIANT (ptype);
11126 /* New format doesn't require casting unless the types are the same size. */
11127 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
11128 return convert (ptype, result);
11129 else
11130 return result;
11133 /* This function verifies the PARAMS to generic atomic FUNCTION.
11134 It returns the size if all the parameters are the same size, otherwise
11135 0 is returned if the parameters are invalid. */
11137 static int
11138 get_atomic_generic_size (location_t loc, tree function,
11139 vec<tree, va_gc> *params)
11141 unsigned int n_param;
11142 unsigned int n_model;
11143 unsigned int x;
11144 int size_0;
11145 tree type_0;
11147 /* Determine the parameter makeup. */
11148 switch (DECL_FUNCTION_CODE (function))
11150 case BUILT_IN_ATOMIC_EXCHANGE:
11151 n_param = 4;
11152 n_model = 1;
11153 break;
11154 case BUILT_IN_ATOMIC_LOAD:
11155 case BUILT_IN_ATOMIC_STORE:
11156 n_param = 3;
11157 n_model = 1;
11158 break;
11159 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11160 n_param = 6;
11161 n_model = 2;
11162 break;
11163 default:
11164 gcc_unreachable ();
11167 if (vec_safe_length (params) != n_param)
11169 error_at (loc, "incorrect number of arguments to function %qE", function);
11170 return 0;
11173 /* Get type of first parameter, and determine its size. */
11174 type_0 = TREE_TYPE ((*params)[0]);
11175 if (TREE_CODE (type_0) == ARRAY_TYPE)
11177 /* Force array-to-pointer decay for C++. */
11178 gcc_assert (c_dialect_cxx());
11179 (*params)[0] = default_conversion ((*params)[0]);
11180 type_0 = TREE_TYPE ((*params)[0]);
11182 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11184 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11185 function);
11186 return 0;
11189 /* Types must be compile time constant sizes. */
11190 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
11192 error_at (loc,
11193 "argument 1 of %qE must be a pointer to a constant size type",
11194 function);
11195 return 0;
11198 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
11200 /* Zero size objects are not allowed. */
11201 if (size_0 == 0)
11203 error_at (loc,
11204 "argument 1 of %qE must be a pointer to a nonzero size object",
11205 function);
11206 return 0;
11209 /* Check each other parameter is a pointer and the same size. */
11210 for (x = 0; x < n_param - n_model; x++)
11212 int size;
11213 tree type = TREE_TYPE ((*params)[x]);
11214 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
11215 if (n_param == 6 && x == 3)
11216 continue;
11217 if (!POINTER_TYPE_P (type))
11219 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11220 function);
11221 return 0;
11223 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
11224 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
11225 != INTEGER_CST)
11227 error_at (loc, "argument %d of %qE must be a pointer to a constant "
11228 "size type", x + 1, function);
11229 return 0;
11231 else if (FUNCTION_POINTER_TYPE_P (type))
11233 error_at (loc, "argument %d of %qE must not be a pointer to a "
11234 "function", x + 1, function);
11235 return 0;
11237 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11238 size = type_size ? tree_to_uhwi (type_size) : 0;
11239 if (size != size_0)
11241 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11242 function);
11243 return 0;
11247 /* Check memory model parameters for validity. */
11248 for (x = n_param - n_model ; x < n_param; x++)
11250 tree p = (*params)[x];
11251 if (TREE_CODE (p) == INTEGER_CST)
11253 int i = tree_to_uhwi (p);
11254 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
11256 warning_at (loc, OPT_Winvalid_memory_model,
11257 "invalid memory model argument %d of %qE", x + 1,
11258 function);
11261 else
11262 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11264 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11265 function);
11266 return 0;
11270 return size_0;
11274 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11275 at the beginning of the parameter list PARAMS representing the size of the
11276 objects. This is to match the library ABI requirement. LOC is the location
11277 of the function call.
11278 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11279 returned to allow the external call to be constructed. */
11281 static tree
11282 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
11283 vec<tree, va_gc> *params)
11285 tree size_node;
11287 /* Insert a SIZE_T parameter as the first param. If there isn't
11288 enough space, allocate a new vector and recursively re-build with that. */
11289 if (!params->space (1))
11291 unsigned int z, len;
11292 vec<tree, va_gc> *v;
11293 tree f;
11295 len = params->length ();
11296 vec_alloc (v, len + 1);
11297 v->quick_push (build_int_cst (size_type_node, n));
11298 for (z = 0; z < len; z++)
11299 v->quick_push ((*params)[z]);
11300 f = build_function_call_vec (loc, vNULL, function, v, NULL);
11301 vec_free (v);
11302 return f;
11305 /* Add the size parameter and leave as a function call for processing. */
11306 size_node = build_int_cst (size_type_node, n);
11307 params->quick_insert (0, size_node);
11308 return NULL_TREE;
11312 /* Return whether atomic operations for naturally aligned N-byte
11313 arguments are supported, whether inline or through libatomic. */
11314 static bool
11315 atomic_size_supported_p (int n)
11317 switch (n)
11319 case 1:
11320 case 2:
11321 case 4:
11322 case 8:
11323 return true;
11325 case 16:
11326 return targetm.scalar_mode_supported_p (TImode);
11328 default:
11329 return false;
11333 /* This will process an __atomic_exchange function call, determine whether it
11334 needs to be mapped to the _N variation, or turned into a library call.
11335 LOC is the location of the builtin call.
11336 FUNCTION is the DECL that has been invoked;
11337 PARAMS is the argument list for the call. The return value is non-null
11338 TRUE is returned if it is translated into the proper format for a call to the
11339 external library, and NEW_RETURN is set the tree for that function.
11340 FALSE is returned if processing for the _N variation is required, and
11341 NEW_RETURN is set to the return value the result is copied into. */
11342 static bool
11343 resolve_overloaded_atomic_exchange (location_t loc, tree function,
11344 vec<tree, va_gc> *params, tree *new_return)
11346 tree p0, p1, p2, p3;
11347 tree I_type, I_type_ptr;
11348 int n = get_atomic_generic_size (loc, function, params);
11350 /* Size of 0 is an error condition. */
11351 if (n == 0)
11353 *new_return = error_mark_node;
11354 return true;
11357 /* If not a lock-free size, change to the library generic format. */
11358 if (!atomic_size_supported_p (n))
11360 *new_return = add_atomic_size_parameter (n, loc, function, params);
11361 return true;
11364 /* Otherwise there is a lockfree match, transform the call from:
11365 void fn(T* mem, T* desired, T* return, model)
11366 into
11367 *return = (T) (fn (In* mem, (In) *desired, model)) */
11369 p0 = (*params)[0];
11370 p1 = (*params)[1];
11371 p2 = (*params)[2];
11372 p3 = (*params)[3];
11374 /* Create pointer to appropriate size. */
11375 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11376 I_type_ptr = build_pointer_type (I_type);
11378 /* Convert object pointer to required type. */
11379 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11380 (*params)[0] = p0;
11381 /* Convert new value to required type, and dereference it. */
11382 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11383 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11384 (*params)[1] = p1;
11386 /* Move memory model to the 3rd position, and end param list. */
11387 (*params)[2] = p3;
11388 params->truncate (3);
11390 /* Convert return pointer and dereference it for later assignment. */
11391 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11393 return false;
11397 /* This will process an __atomic_compare_exchange function call, determine
11398 whether it needs to be mapped to the _N variation, or turned into a lib call.
11399 LOC is the location of the builtin call.
11400 FUNCTION is the DECL that has been invoked;
11401 PARAMS is the argument list for the call. The return value is non-null
11402 TRUE is returned if it is translated into the proper format for a call to the
11403 external library, and NEW_RETURN is set the tree for that function.
11404 FALSE is returned if processing for the _N variation is required. */
11406 static bool
11407 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11408 vec<tree, va_gc> *params,
11409 tree *new_return)
11411 tree p0, p1, p2;
11412 tree I_type, I_type_ptr;
11413 int n = get_atomic_generic_size (loc, function, params);
11415 /* Size of 0 is an error condition. */
11416 if (n == 0)
11418 *new_return = error_mark_node;
11419 return true;
11422 /* If not a lock-free size, change to the library generic format. */
11423 if (!atomic_size_supported_p (n))
11425 /* The library generic format does not have the weak parameter, so
11426 remove it from the param list. Since a parameter has been removed,
11427 we can be sure that there is room for the SIZE_T parameter, meaning
11428 there will not be a recursive rebuilding of the parameter list, so
11429 there is no danger this will be done twice. */
11430 if (n > 0)
11432 (*params)[3] = (*params)[4];
11433 (*params)[4] = (*params)[5];
11434 params->truncate (5);
11436 *new_return = add_atomic_size_parameter (n, loc, function, params);
11437 return true;
11440 /* Otherwise, there is a match, so the call needs to be transformed from:
11441 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11442 into
11443 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11445 p0 = (*params)[0];
11446 p1 = (*params)[1];
11447 p2 = (*params)[2];
11449 /* Create pointer to appropriate size. */
11450 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11451 I_type_ptr = build_pointer_type (I_type);
11453 /* Convert object pointer to required type. */
11454 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11455 (*params)[0] = p0;
11457 /* Convert expected pointer to required type. */
11458 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11459 (*params)[1] = p1;
11461 /* Convert desired value to required type, and dereference it. */
11462 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11463 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11464 (*params)[2] = p2;
11466 /* The rest of the parameters are fine. NULL means no special return value
11467 processing.*/
11468 *new_return = NULL;
11469 return false;
11473 /* This will process an __atomic_load function call, determine whether it
11474 needs to be mapped to the _N variation, or turned into a library call.
11475 LOC is the location of the builtin call.
11476 FUNCTION is the DECL that has been invoked;
11477 PARAMS is the argument list for the call. The return value is non-null
11478 TRUE is returned if it is translated into the proper format for a call to the
11479 external library, and NEW_RETURN is set the tree for that function.
11480 FALSE is returned if processing for the _N variation is required, and
11481 NEW_RETURN is set to the return value the result is copied into. */
11483 static bool
11484 resolve_overloaded_atomic_load (location_t loc, tree function,
11485 vec<tree, va_gc> *params, tree *new_return)
11487 tree p0, p1, p2;
11488 tree I_type, I_type_ptr;
11489 int n = get_atomic_generic_size (loc, function, params);
11491 /* Size of 0 is an error condition. */
11492 if (n == 0)
11494 *new_return = error_mark_node;
11495 return true;
11498 /* If not a lock-free size, change to the library generic format. */
11499 if (!atomic_size_supported_p (n))
11501 *new_return = add_atomic_size_parameter (n, loc, function, params);
11502 return true;
11505 /* Otherwise, there is a match, so the call needs to be transformed from:
11506 void fn(T* mem, T* return, model)
11507 into
11508 *return = (T) (fn ((In *) mem, model)) */
11510 p0 = (*params)[0];
11511 p1 = (*params)[1];
11512 p2 = (*params)[2];
11514 /* Create pointer to appropriate size. */
11515 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11516 I_type_ptr = build_pointer_type (I_type);
11518 /* Convert object pointer to required type. */
11519 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11520 (*params)[0] = p0;
11522 /* Move memory model to the 2nd position, and end param list. */
11523 (*params)[1] = p2;
11524 params->truncate (2);
11526 /* Convert return pointer and dereference it for later assignment. */
11527 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11529 return false;
11533 /* This will process an __atomic_store function call, determine whether it
11534 needs to be mapped to the _N variation, or turned into a library call.
11535 LOC is the location of the builtin call.
11536 FUNCTION is the DECL that has been invoked;
11537 PARAMS is the argument list for the call. The return value is non-null
11538 TRUE is returned if it is translated into the proper format for a call to the
11539 external library, and NEW_RETURN is set the tree for that function.
11540 FALSE is returned if processing for the _N variation is required, and
11541 NEW_RETURN is set to the return value the result is copied into. */
11543 static bool
11544 resolve_overloaded_atomic_store (location_t loc, tree function,
11545 vec<tree, va_gc> *params, tree *new_return)
11547 tree p0, p1;
11548 tree I_type, I_type_ptr;
11549 int n = get_atomic_generic_size (loc, function, params);
11551 /* Size of 0 is an error condition. */
11552 if (n == 0)
11554 *new_return = error_mark_node;
11555 return true;
11558 /* If not a lock-free size, change to the library generic format. */
11559 if (!atomic_size_supported_p (n))
11561 *new_return = add_atomic_size_parameter (n, loc, function, params);
11562 return true;
11565 /* Otherwise, there is a match, so the call needs to be transformed from:
11566 void fn(T* mem, T* value, model)
11567 into
11568 fn ((In *) mem, (In) *value, model) */
11570 p0 = (*params)[0];
11571 p1 = (*params)[1];
11573 /* Create pointer to appropriate size. */
11574 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11575 I_type_ptr = build_pointer_type (I_type);
11577 /* Convert object pointer to required type. */
11578 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11579 (*params)[0] = p0;
11581 /* Convert new value to required type, and dereference it. */
11582 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11583 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11584 (*params)[1] = p1;
11586 /* The memory model is in the right spot already. Return is void. */
11587 *new_return = NULL_TREE;
11589 return false;
11593 /* Some builtin functions are placeholders for other expressions. This
11594 function should be called immediately after parsing the call expression
11595 before surrounding code has committed to the type of the expression.
11597 LOC is the location of the builtin call.
11599 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11600 PARAMS is the argument list for the call. The return value is non-null
11601 when expansion is complete, and null if normal processing should
11602 continue. */
11604 tree
11605 resolve_overloaded_builtin (location_t loc, tree function,
11606 vec<tree, va_gc> *params)
11608 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11610 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11611 Those are not valid to call with a pointer to _Bool (or C++ bool)
11612 and so must be rejected. */
11613 bool fetch_op = true;
11614 bool orig_format = true;
11615 tree new_return = NULL_TREE;
11617 switch (DECL_BUILT_IN_CLASS (function))
11619 case BUILT_IN_NORMAL:
11620 break;
11621 case BUILT_IN_MD:
11622 if (targetm.resolve_overloaded_builtin)
11623 return targetm.resolve_overloaded_builtin (loc, function, params);
11624 else
11625 return NULL_TREE;
11626 default:
11627 return NULL_TREE;
11630 /* Handle BUILT_IN_NORMAL here. */
11631 switch (orig_code)
11633 case BUILT_IN_ATOMIC_EXCHANGE:
11634 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11635 case BUILT_IN_ATOMIC_LOAD:
11636 case BUILT_IN_ATOMIC_STORE:
11638 /* Handle these 4 together so that they can fall through to the next
11639 case if the call is transformed to an _N variant. */
11640 switch (orig_code)
11642 case BUILT_IN_ATOMIC_EXCHANGE:
11644 if (resolve_overloaded_atomic_exchange (loc, function, params,
11645 &new_return))
11646 return new_return;
11647 /* Change to the _N variant. */
11648 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11649 break;
11652 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11654 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11655 params,
11656 &new_return))
11657 return new_return;
11658 /* Change to the _N variant. */
11659 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11660 break;
11662 case BUILT_IN_ATOMIC_LOAD:
11664 if (resolve_overloaded_atomic_load (loc, function, params,
11665 &new_return))
11666 return new_return;
11667 /* Change to the _N variant. */
11668 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11669 break;
11671 case BUILT_IN_ATOMIC_STORE:
11673 if (resolve_overloaded_atomic_store (loc, function, params,
11674 &new_return))
11675 return new_return;
11676 /* Change to the _N variant. */
11677 orig_code = BUILT_IN_ATOMIC_STORE_N;
11678 break;
11680 default:
11681 gcc_unreachable ();
11684 /* FALLTHRU */
11685 case BUILT_IN_ATOMIC_EXCHANGE_N:
11686 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11687 case BUILT_IN_ATOMIC_LOAD_N:
11688 case BUILT_IN_ATOMIC_STORE_N:
11689 fetch_op = false;
11690 /* FALLTHRU */
11691 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11692 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11693 case BUILT_IN_ATOMIC_AND_FETCH_N:
11694 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11695 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11696 case BUILT_IN_ATOMIC_OR_FETCH_N:
11697 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11698 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11699 case BUILT_IN_ATOMIC_FETCH_AND_N:
11700 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11701 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11702 case BUILT_IN_ATOMIC_FETCH_OR_N:
11703 orig_format = false;
11704 /* FALLTHRU */
11705 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11706 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11707 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11708 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11709 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11710 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11711 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11712 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11713 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11714 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11715 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11716 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11717 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11718 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11719 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11720 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11722 /* The following are not _FETCH_OPs and must be accepted with
11723 pointers to _Bool (or C++ bool). */
11724 if (fetch_op)
11725 fetch_op =
11726 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11727 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11728 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11729 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11731 int n = sync_resolve_size (function, params, fetch_op);
11732 tree new_function, first_param, result;
11733 enum built_in_function fncode;
11735 if (n == 0)
11736 return error_mark_node;
11738 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11739 new_function = builtin_decl_explicit (fncode);
11740 if (!sync_resolve_params (loc, function, new_function, params,
11741 orig_format))
11742 return error_mark_node;
11744 first_param = (*params)[0];
11745 result = build_function_call_vec (loc, vNULL, new_function, params,
11746 NULL);
11747 if (result == error_mark_node)
11748 return result;
11749 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11750 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11751 && orig_code != BUILT_IN_ATOMIC_STORE_N
11752 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
11753 result = sync_resolve_return (first_param, result, orig_format);
11755 if (fetch_op)
11756 /* Prevent -Wunused-value warning. */
11757 TREE_USED (result) = true;
11759 /* If new_return is set, assign function to that expr and cast the
11760 result to void since the generic interface returned void. */
11761 if (new_return)
11763 /* Cast function result from I{1,2,4,8,16} to the required type. */
11764 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11765 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11766 result);
11767 TREE_SIDE_EFFECTS (result) = 1;
11768 protected_set_expr_location (result, loc);
11769 result = convert (void_type_node, result);
11771 return result;
11774 default:
11775 return NULL_TREE;
11779 /* vector_types_compatible_elements_p is used in type checks of vectors
11780 values used as operands of binary operators. Where it returns true, and
11781 the other checks of the caller succeed (being vector types in he first
11782 place, and matching number of elements), we can just treat the types
11783 as essentially the same.
11784 Contrast with vector_targets_convertible_p, which is used for vector
11785 pointer types, and vector_types_convertible_p, which will allow
11786 language-specific matches under the control of flag_lax_vector_conversions,
11787 and might still require a conversion. */
11788 /* True if vector types T1 and T2 can be inputs to the same binary
11789 operator without conversion.
11790 We don't check the overall vector size here because some of our callers
11791 want to give different error messages when the vectors are compatible
11792 except for the element count. */
11794 bool
11795 vector_types_compatible_elements_p (tree t1, tree t2)
11797 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11798 t1 = TREE_TYPE (t1);
11799 t2 = TREE_TYPE (t2);
11801 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11803 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11804 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11805 || c2 == FIXED_POINT_TYPE));
11807 t1 = c_common_signed_type (t1);
11808 t2 = c_common_signed_type (t2);
11809 /* Equality works here because c_common_signed_type uses
11810 TYPE_MAIN_VARIANT. */
11811 if (t1 == t2)
11812 return true;
11813 if (opaque && c1 == c2
11814 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11815 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11816 return true;
11817 return false;
11820 /* Check for missing format attributes on function pointers. LTYPE is
11821 the new type or left-hand side type. RTYPE is the old type or
11822 right-hand side type. Returns TRUE if LTYPE is missing the desired
11823 attribute. */
11825 bool
11826 check_missing_format_attribute (tree ltype, tree rtype)
11828 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11829 tree ra;
11831 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11832 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11833 break;
11834 if (ra)
11836 tree la;
11837 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11838 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11839 break;
11840 return !la;
11842 else
11843 return false;
11846 /* Subscripting with type char is likely to lose on a machine where
11847 chars are signed. So warn on any machine, but optionally. Don't
11848 warn for unsigned char since that type is safe. Don't warn for
11849 signed char because anyone who uses that must have done so
11850 deliberately. Furthermore, we reduce the false positive load by
11851 warning only for non-constant value of type char. */
11853 void
11854 warn_array_subscript_with_type_char (location_t loc, tree index)
11856 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11857 && TREE_CODE (index) != INTEGER_CST)
11858 warning_at (loc, OPT_Wchar_subscripts,
11859 "array subscript has type %<char%>");
11862 /* Implement -Wparentheses for the unexpected C precedence rules, to
11863 cover cases like x + y << z which readers are likely to
11864 misinterpret. We have seen an expression in which CODE is a binary
11865 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11866 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11867 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11868 expression was not formed using a binary or unary operator, or it
11869 was enclosed in parentheses. */
11871 void
11872 warn_about_parentheses (location_t loc, enum tree_code code,
11873 enum tree_code code_left, tree arg_left,
11874 enum tree_code code_right, tree arg_right)
11876 if (!warn_parentheses)
11877 return;
11879 /* This macro tests that the expression ARG with original tree code
11880 CODE appears to be a boolean expression. or the result of folding a
11881 boolean expression. */
11882 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11883 (truth_value_p (TREE_CODE (ARG)) \
11884 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11885 /* Folding may create 0 or 1 integers from other expressions. */ \
11886 || ((CODE) != INTEGER_CST \
11887 && (integer_onep (ARG) || integer_zerop (ARG))))
11889 switch (code)
11891 case LSHIFT_EXPR:
11892 if (code_left == PLUS_EXPR)
11893 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11894 "suggest parentheses around %<+%> inside %<<<%>");
11895 else if (code_right == PLUS_EXPR)
11896 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11897 "suggest parentheses around %<+%> inside %<<<%>");
11898 else if (code_left == MINUS_EXPR)
11899 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11900 "suggest parentheses around %<-%> inside %<<<%>");
11901 else if (code_right == MINUS_EXPR)
11902 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11903 "suggest parentheses around %<-%> inside %<<<%>");
11904 return;
11906 case RSHIFT_EXPR:
11907 if (code_left == PLUS_EXPR)
11908 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11909 "suggest parentheses around %<+%> inside %<>>%>");
11910 else if (code_right == PLUS_EXPR)
11911 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11912 "suggest parentheses around %<+%> inside %<>>%>");
11913 else if (code_left == MINUS_EXPR)
11914 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11915 "suggest parentheses around %<-%> inside %<>>%>");
11916 else if (code_right == MINUS_EXPR)
11917 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11918 "suggest parentheses around %<-%> inside %<>>%>");
11919 return;
11921 case TRUTH_ORIF_EXPR:
11922 if (code_left == TRUTH_ANDIF_EXPR)
11923 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11924 "suggest parentheses around %<&&%> within %<||%>");
11925 else if (code_right == TRUTH_ANDIF_EXPR)
11926 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11927 "suggest parentheses around %<&&%> within %<||%>");
11928 return;
11930 case BIT_IOR_EXPR:
11931 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11932 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11933 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11934 "suggest parentheses around arithmetic in operand of %<|%>");
11935 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11936 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11937 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11938 "suggest parentheses around arithmetic in operand of %<|%>");
11939 /* Check cases like x|y==z */
11940 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11941 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11942 "suggest parentheses around comparison in operand of %<|%>");
11943 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11944 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11945 "suggest parentheses around comparison in operand of %<|%>");
11946 /* Check cases like !x | y */
11947 else if (code_left == TRUTH_NOT_EXPR
11948 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11949 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11950 "suggest parentheses around operand of "
11951 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11952 return;
11954 case BIT_XOR_EXPR:
11955 if (code_left == BIT_AND_EXPR
11956 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11957 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11958 "suggest parentheses around arithmetic in operand of %<^%>");
11959 else if (code_right == BIT_AND_EXPR
11960 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11961 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11962 "suggest parentheses around arithmetic in operand of %<^%>");
11963 /* Check cases like x^y==z */
11964 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11965 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11966 "suggest parentheses around comparison in operand of %<^%>");
11967 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11968 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11969 "suggest parentheses around comparison in operand of %<^%>");
11970 return;
11972 case BIT_AND_EXPR:
11973 if (code_left == PLUS_EXPR)
11974 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11975 "suggest parentheses around %<+%> in operand of %<&%>");
11976 else if (code_right == PLUS_EXPR)
11977 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11978 "suggest parentheses around %<+%> in operand of %<&%>");
11979 else if (code_left == MINUS_EXPR)
11980 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11981 "suggest parentheses around %<-%> in operand of %<&%>");
11982 else if (code_right == MINUS_EXPR)
11983 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11984 "suggest parentheses around %<-%> in operand of %<&%>");
11985 /* Check cases like x&y==z */
11986 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11987 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11988 "suggest parentheses around comparison in operand of %<&%>");
11989 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11990 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11991 "suggest parentheses around comparison in operand of %<&%>");
11992 /* Check cases like !x & y */
11993 else if (code_left == TRUTH_NOT_EXPR
11994 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11995 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11996 "suggest parentheses around operand of "
11997 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11998 return;
12000 case EQ_EXPR:
12001 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
12002 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12003 "suggest parentheses around comparison in operand of %<==%>");
12004 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
12005 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12006 "suggest parentheses around comparison in operand of %<==%>");
12007 return;
12008 case NE_EXPR:
12009 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
12010 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12011 "suggest parentheses around comparison in operand of %<!=%>");
12012 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
12013 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12014 "suggest parentheses around comparison in operand of %<!=%>");
12015 return;
12017 default:
12018 if (TREE_CODE_CLASS (code) == tcc_comparison)
12020 if (TREE_CODE_CLASS (code_left) == tcc_comparison
12021 && code_left != NE_EXPR && code_left != EQ_EXPR
12022 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
12023 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12024 "comparisons like %<X<=Y<=Z%> do not "
12025 "have their mathematical meaning");
12026 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
12027 && code_right != NE_EXPR && code_right != EQ_EXPR
12028 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
12029 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12030 "comparisons like %<X<=Y<=Z%> do not "
12031 "have their mathematical meaning");
12033 return;
12035 #undef NOT_A_BOOLEAN_EXPR_P
12038 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
12040 void
12041 warn_for_unused_label (tree label)
12043 if (!TREE_USED (label))
12045 if (DECL_INITIAL (label))
12046 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
12047 else
12048 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
12052 /* Warn for division by zero according to the value of DIVISOR. LOC
12053 is the location of the division operator. */
12055 void
12056 warn_for_div_by_zero (location_t loc, tree divisor)
12058 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
12059 about division by zero. Do not issue a warning if DIVISOR has a
12060 floating-point type, since we consider 0.0/0.0 a valid way of
12061 generating a NaN. */
12062 if (c_inhibit_evaluation_warnings == 0
12063 && (integer_zerop (divisor) || fixed_zerop (divisor)))
12064 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
12067 /* Warn for patterns where memset appears to be used incorrectly. The
12068 warning location should be LOC. ARG0, and ARG2 are the first and
12069 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
12070 each argument that was a literal zero. */
12072 void
12073 warn_for_memset (location_t loc, tree arg0, tree arg2,
12074 int literal_zero_mask)
12076 if (warn_memset_transposed_args
12077 && integer_zerop (arg2)
12078 && (literal_zero_mask & (1 << 2)) != 0
12079 && (literal_zero_mask & (1 << 1)) == 0)
12080 warning_at (loc, OPT_Wmemset_transposed_args,
12081 "%<memset%> used with constant zero length "
12082 "parameter; this could be due to transposed "
12083 "parameters");
12085 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
12087 STRIP_NOPS (arg0);
12088 if (TREE_CODE (arg0) == ADDR_EXPR)
12089 arg0 = TREE_OPERAND (arg0, 0);
12090 tree type = TREE_TYPE (arg0);
12091 if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE)
12093 tree elt_type = TREE_TYPE (type);
12094 tree domain = TYPE_DOMAIN (type);
12095 if (!integer_onep (TYPE_SIZE_UNIT (elt_type))
12096 && domain != NULL_TREE
12097 && TYPE_MAXVAL (domain)
12098 && TYPE_MINVAL (domain)
12099 && integer_zerop (TYPE_MINVAL (domain))
12100 && integer_onep (fold_build2 (MINUS_EXPR, domain,
12101 arg2,
12102 TYPE_MAXVAL (domain))))
12103 warning_at (loc, OPT_Wmemset_elt_size,
12104 "%<memset%> used with length equal to "
12105 "number of elements without multiplication "
12106 "by element size");
12111 /* Subroutine of build_binary_op. Give warnings for comparisons
12112 between signed and unsigned quantities that may fail. Do the
12113 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
12114 so that casts will be considered, but default promotions won't
12117 LOCATION is the location of the comparison operator.
12119 The arguments of this function map directly to local variables
12120 of build_binary_op. */
12122 void
12123 warn_for_sign_compare (location_t location,
12124 tree orig_op0, tree orig_op1,
12125 tree op0, tree op1,
12126 tree result_type, enum tree_code resultcode)
12128 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
12129 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
12130 int unsignedp0, unsignedp1;
12132 /* In C++, check for comparison of different enum types. */
12133 if (c_dialect_cxx()
12134 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
12135 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
12136 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
12137 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
12139 warning_at (location,
12140 OPT_Wsign_compare, "comparison between types %qT and %qT",
12141 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
12144 /* Do not warn if the comparison is being done in a signed type,
12145 since the signed type will only be chosen if it can represent
12146 all the values of the unsigned type. */
12147 if (!TYPE_UNSIGNED (result_type))
12148 /* OK */;
12149 /* Do not warn if both operands are unsigned. */
12150 else if (op0_signed == op1_signed)
12151 /* OK */;
12152 else
12154 tree sop, uop, base_type;
12155 bool ovf;
12157 if (op0_signed)
12158 sop = orig_op0, uop = orig_op1;
12159 else
12160 sop = orig_op1, uop = orig_op0;
12162 STRIP_TYPE_NOPS (sop);
12163 STRIP_TYPE_NOPS (uop);
12164 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12165 ? TREE_TYPE (result_type) : result_type);
12167 /* Do not warn if the signed quantity is an unsuffixed integer
12168 literal (or some static constant expression involving such
12169 literals or a conditional expression involving such literals)
12170 and it is non-negative. */
12171 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12172 /* OK */;
12173 /* Do not warn if the comparison is an equality operation, the
12174 unsigned quantity is an integral constant, and it would fit
12175 in the result if the result were signed. */
12176 else if (TREE_CODE (uop) == INTEGER_CST
12177 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
12178 && int_fits_type_p (uop, c_common_signed_type (base_type)))
12179 /* OK */;
12180 /* In C, do not warn if the unsigned quantity is an enumeration
12181 constant and its maximum value would fit in the result if the
12182 result were signed. */
12183 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12184 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12185 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
12186 c_common_signed_type (base_type)))
12187 /* OK */;
12188 else
12189 warning_at (location,
12190 OPT_Wsign_compare,
12191 "comparison between signed and unsigned integer expressions");
12194 /* Warn if two unsigned values are being compared in a size larger
12195 than their original size, and one (and only one) is the result of
12196 a `~' operator. This comparison will always fail.
12198 Also warn if one operand is a constant, and the constant does not
12199 have all bits set that are set in the ~ operand when it is
12200 extended. */
12202 op0 = c_common_get_narrower (op0, &unsignedp0);
12203 op1 = c_common_get_narrower (op1, &unsignedp1);
12205 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12206 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12208 if (TREE_CODE (op0) == BIT_NOT_EXPR)
12209 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
12210 if (TREE_CODE (op1) == BIT_NOT_EXPR)
12211 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
12213 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
12215 tree primop;
12216 HOST_WIDE_INT constant, mask;
12217 int unsignedp;
12218 unsigned int bits;
12220 if (tree_fits_shwi_p (op0))
12222 primop = op1;
12223 unsignedp = unsignedp1;
12224 constant = tree_to_shwi (op0);
12226 else
12228 primop = op0;
12229 unsignedp = unsignedp0;
12230 constant = tree_to_shwi (op1);
12233 bits = TYPE_PRECISION (TREE_TYPE (primop));
12234 if (bits < TYPE_PRECISION (result_type)
12235 && bits < HOST_BITS_PER_LONG && unsignedp)
12237 mask = HOST_WIDE_INT_M1U << bits;
12238 if ((mask & constant) != mask)
12240 if (constant == 0)
12241 warning_at (location, OPT_Wsign_compare,
12242 "promoted ~unsigned is always non-zero");
12243 else
12244 warning_at (location, OPT_Wsign_compare,
12245 "comparison of promoted ~unsigned with constant");
12249 else if (unsignedp0 && unsignedp1
12250 && (TYPE_PRECISION (TREE_TYPE (op0))
12251 < TYPE_PRECISION (result_type))
12252 && (TYPE_PRECISION (TREE_TYPE (op1))
12253 < TYPE_PRECISION (result_type)))
12254 warning_at (location, OPT_Wsign_compare,
12255 "comparison of promoted ~unsigned with unsigned");
12259 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12260 type via c_common_type. If -Wdouble-promotion is in use, and the
12261 conditions for warning have been met, issue a warning. GMSGID is
12262 the warning message. It must have two %T specifiers for the type
12263 that was converted (generally "float") and the type to which it was
12264 converted (generally "double), respectively. LOC is the location
12265 to which the awrning should refer. */
12267 void
12268 do_warn_double_promotion (tree result_type, tree type1, tree type2,
12269 const char *gmsgid, location_t loc)
12271 tree source_type;
12273 if (!warn_double_promotion)
12274 return;
12275 /* If the conversion will not occur at run-time, there is no need to
12276 warn about it. */
12277 if (c_inhibit_evaluation_warnings)
12278 return;
12279 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12280 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12281 return;
12282 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12283 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12284 source_type = type1;
12285 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12286 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12287 source_type = type2;
12288 else
12289 return;
12290 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12293 /* Possibly warn about unused parameters. */
12295 void
12296 do_warn_unused_parameter (tree fn)
12298 tree decl;
12300 for (decl = DECL_ARGUMENTS (fn);
12301 decl; decl = DECL_CHAIN (decl))
12302 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12303 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12304 && !TREE_NO_WARNING (decl))
12305 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12306 "unused parameter %qD", decl);
12310 /* Setup a TYPE_DECL node as a typedef representation.
12312 X is a TYPE_DECL for a typedef statement. Create a brand new
12313 ..._TYPE node (which will be just a variant of the existing
12314 ..._TYPE node with identical properties) and then install X
12315 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12317 The whole point here is to end up with a situation where each
12318 and every ..._TYPE node the compiler creates will be uniquely
12319 associated with AT MOST one node representing a typedef name.
12320 This way, even though the compiler substitutes corresponding
12321 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12322 early on, later parts of the compiler can always do the reverse
12323 translation and get back the corresponding typedef name. For
12324 example, given:
12326 typedef struct S MY_TYPE;
12327 MY_TYPE object;
12329 Later parts of the compiler might only know that `object' was of
12330 type `struct S' if it were not for code just below. With this
12331 code however, later parts of the compiler see something like:
12333 struct S' == struct S
12334 typedef struct S' MY_TYPE;
12335 struct S' object;
12337 And they can then deduce (from the node for type struct S') that
12338 the original object declaration was:
12340 MY_TYPE object;
12342 Being able to do this is important for proper support of protoize,
12343 and also for generating precise symbolic debugging information
12344 which takes full account of the programmer's (typedef) vocabulary.
12346 Obviously, we don't want to generate a duplicate ..._TYPE node if
12347 the TYPE_DECL node that we are now processing really represents a
12348 standard built-in type. */
12350 void
12351 set_underlying_type (tree x)
12353 if (x == error_mark_node)
12354 return;
12355 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
12357 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12358 TYPE_NAME (TREE_TYPE (x)) = x;
12360 else if (TREE_TYPE (x) != error_mark_node
12361 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12363 tree tt = TREE_TYPE (x);
12364 DECL_ORIGINAL_TYPE (x) = tt;
12365 tt = build_variant_type_copy (tt);
12366 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12367 TYPE_NAME (tt) = x;
12368 TREE_USED (tt) = TREE_USED (x);
12369 TREE_TYPE (x) = tt;
12373 /* Record the types used by the current global variable declaration
12374 being parsed, so that we can decide later to emit their debug info.
12375 Those types are in types_used_by_cur_var_decl, and we are going to
12376 store them in the types_used_by_vars_hash hash table.
12377 DECL is the declaration of the global variable that has been parsed. */
12379 void
12380 record_types_used_by_current_var_decl (tree decl)
12382 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12384 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
12386 tree type = types_used_by_cur_var_decl->pop ();
12387 types_used_by_var_decl_insert (type, decl);
12391 /* If DECL is a typedef that is declared in the current function,
12392 record it for the purpose of -Wunused-local-typedefs. */
12394 void
12395 record_locally_defined_typedef (tree decl)
12397 struct c_language_function *l;
12399 if (!warn_unused_local_typedefs
12400 || cfun == NULL
12401 /* if this is not a locally defined typedef then we are not
12402 interested. */
12403 || !is_typedef_decl (decl)
12404 || !decl_function_context (decl))
12405 return;
12407 l = (struct c_language_function *) cfun->language;
12408 vec_safe_push (l->local_typedefs, decl);
12411 /* If T is a TYPE_DECL declared locally, mark it as used. */
12413 void
12414 maybe_record_typedef_use (tree t)
12416 if (!is_typedef_decl (t))
12417 return;
12419 TREE_USED (t) = true;
12422 /* Warn if there are some unused locally defined typedefs in the
12423 current function. */
12425 void
12426 maybe_warn_unused_local_typedefs (void)
12428 int i;
12429 tree decl;
12430 /* The number of times we have emitted -Wunused-local-typedefs
12431 warnings. If this is different from errorcount, that means some
12432 unrelated errors have been issued. In which case, we'll avoid
12433 emitting "unused-local-typedefs" warnings. */
12434 static int unused_local_typedefs_warn_count;
12435 struct c_language_function *l;
12437 if (cfun == NULL)
12438 return;
12440 if ((l = (struct c_language_function *) cfun->language) == NULL)
12441 return;
12443 if (warn_unused_local_typedefs
12444 && errorcount == unused_local_typedefs_warn_count)
12446 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
12447 if (!TREE_USED (decl))
12448 warning_at (DECL_SOURCE_LOCATION (decl),
12449 OPT_Wunused_local_typedefs,
12450 "typedef %qD locally defined but not used", decl);
12451 unused_local_typedefs_warn_count = errorcount;
12454 vec_free (l->local_typedefs);
12457 /* Warn about boolean expression compared with an integer value different
12458 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12459 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12460 are the operands of the comparison. The caller must ensure that
12461 either operand is a boolean expression. */
12463 void
12464 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12465 tree op1)
12467 if (TREE_CODE_CLASS (code) != tcc_comparison)
12468 return;
12470 tree f, cst;
12471 if (f = fold_for_warn (op0),
12472 TREE_CODE (f) == INTEGER_CST)
12473 cst = op0 = f;
12474 else if (f = fold_for_warn (op1),
12475 TREE_CODE (f) == INTEGER_CST)
12476 cst = op1 = f;
12477 else
12478 return;
12480 if (!integer_zerop (cst) && !integer_onep (cst))
12482 int sign = (TREE_CODE (op0) == INTEGER_CST
12483 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
12484 if (code == EQ_EXPR
12485 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12486 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12487 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12488 "with boolean expression is always false", cst);
12489 else
12490 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12491 "with boolean expression is always true", cst);
12493 else if (integer_zerop (cst) || integer_onep (cst))
12495 /* If the non-constant operand isn't of a boolean type, we
12496 don't want to warn here. */
12497 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12498 /* Handle booleans promoted to integers. */
12499 if (bool_promoted_to_int_p (noncst))
12500 /* Warn. */;
12501 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12502 && !truth_value_p (TREE_CODE (noncst)))
12503 return;
12504 /* Do some magic to get the right diagnostics. */
12505 bool flag = TREE_CODE (op0) == INTEGER_CST;
12506 flag = integer_zerop (cst) ? flag : !flag;
12507 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12508 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12509 "with boolean expression is always true", cst);
12510 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12511 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12512 "with boolean expression is always false", cst);
12516 /* Warn if signed left shift overflows. We don't warn
12517 about left-shifting 1 into the sign bit in C++14; cf.
12518 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12519 LOC is a location of the shift; OP0 and OP1 are the operands.
12520 Return true if an overflow is detected, false otherwise. */
12522 bool
12523 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12525 if (TREE_CODE (op0) != INTEGER_CST
12526 || TREE_CODE (op1) != INTEGER_CST)
12527 return false;
12529 tree type0 = TREE_TYPE (op0);
12530 unsigned int prec0 = TYPE_PRECISION (type0);
12532 /* Left-hand operand must be signed. */
12533 if (TYPE_UNSIGNED (type0))
12534 return false;
12536 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12537 + TREE_INT_CST_LOW (op1));
12538 /* Handle the case of left-shifting 1 into the sign bit.
12539 * However, shifting 1 _out_ of the sign bit, as in
12540 * INT_MIN << 1, is considered an overflow.
12542 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
12544 /* Never warn for C++14 onwards. */
12545 if (cxx_dialect >= cxx14)
12546 return false;
12547 /* Otherwise only if -Wshift-overflow=2. But return
12548 true to signal an overflow for the sake of integer
12549 constant expressions. */
12550 if (warn_shift_overflow < 2)
12551 return true;
12554 bool overflowed = min_prec > prec0;
12555 if (overflowed && c_inhibit_evaluation_warnings == 0)
12556 warning_at (loc, OPT_Wshift_overflow_,
12557 "result of %qE requires %u bits to represent, "
12558 "but %qT only has %u bits",
12559 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12560 min_prec, type0, prec0);
12562 return overflowed;
12565 /* The C and C++ parsers both use vectors to hold function arguments.
12566 For efficiency, we keep a cache of unused vectors. This is the
12567 cache. */
12569 typedef vec<tree, va_gc> *tree_gc_vec;
12570 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12572 /* Return a new vector from the cache. If the cache is empty,
12573 allocate a new vector. These vectors are GC'ed, so it is OK if the
12574 pointer is not released.. */
12576 vec<tree, va_gc> *
12577 make_tree_vector (void)
12579 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12580 return tree_vector_cache->pop ();
12581 else
12583 /* Passing 0 to vec::alloc returns NULL, and our callers require
12584 that we always return a non-NULL value. The vector code uses
12585 4 when growing a NULL vector, so we do too. */
12586 vec<tree, va_gc> *v;
12587 vec_alloc (v, 4);
12588 return v;
12592 /* Release a vector of trees back to the cache. */
12594 void
12595 release_tree_vector (vec<tree, va_gc> *vec)
12597 if (vec != NULL)
12599 vec->truncate (0);
12600 vec_safe_push (tree_vector_cache, vec);
12604 /* Get a new tree vector holding a single tree. */
12606 vec<tree, va_gc> *
12607 make_tree_vector_single (tree t)
12609 vec<tree, va_gc> *ret = make_tree_vector ();
12610 ret->quick_push (t);
12611 return ret;
12614 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12616 vec<tree, va_gc> *
12617 make_tree_vector_from_list (tree list)
12619 vec<tree, va_gc> *ret = make_tree_vector ();
12620 for (; list; list = TREE_CHAIN (list))
12621 vec_safe_push (ret, TREE_VALUE (list));
12622 return ret;
12625 /* Get a new tree vector of the values of a CONSTRUCTOR. */
12627 vec<tree, va_gc> *
12628 make_tree_vector_from_ctor (tree ctor)
12630 vec<tree,va_gc> *ret = make_tree_vector ();
12631 vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
12632 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
12633 ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
12634 return ret;
12637 /* Get a new tree vector which is a copy of an existing one. */
12639 vec<tree, va_gc> *
12640 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12642 vec<tree, va_gc> *ret;
12643 unsigned int ix;
12644 tree t;
12646 ret = make_tree_vector ();
12647 vec_safe_reserve (ret, vec_safe_length (orig));
12648 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12649 ret->quick_push (t);
12650 return ret;
12653 /* Return true if KEYWORD starts a type specifier. */
12655 bool
12656 keyword_begins_type_specifier (enum rid keyword)
12658 switch (keyword)
12660 case RID_AUTO_TYPE:
12661 case RID_INT:
12662 case RID_CHAR:
12663 case RID_FLOAT:
12664 case RID_DOUBLE:
12665 case RID_VOID:
12666 case RID_UNSIGNED:
12667 case RID_LONG:
12668 case RID_SHORT:
12669 case RID_SIGNED:
12670 CASE_RID_FLOATN_NX:
12671 case RID_DFLOAT32:
12672 case RID_DFLOAT64:
12673 case RID_DFLOAT128:
12674 case RID_FRACT:
12675 case RID_ACCUM:
12676 case RID_BOOL:
12677 case RID_WCHAR:
12678 case RID_CHAR16:
12679 case RID_CHAR32:
12680 case RID_SAT:
12681 case RID_COMPLEX:
12682 case RID_TYPEOF:
12683 case RID_STRUCT:
12684 case RID_CLASS:
12685 case RID_UNION:
12686 case RID_ENUM:
12687 return true;
12688 default:
12689 if (keyword >= RID_FIRST_INT_N
12690 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12691 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12692 return true;
12693 return false;
12697 /* Return true if KEYWORD names a type qualifier. */
12699 bool
12700 keyword_is_type_qualifier (enum rid keyword)
12702 switch (keyword)
12704 case RID_CONST:
12705 case RID_VOLATILE:
12706 case RID_RESTRICT:
12707 case RID_ATOMIC:
12708 return true;
12709 default:
12710 return false;
12714 /* Return true if KEYWORD names a storage class specifier.
12716 RID_TYPEDEF is not included in this list despite `typedef' being
12717 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12718 such for syntactic convenience only. */
12720 bool
12721 keyword_is_storage_class_specifier (enum rid keyword)
12723 switch (keyword)
12725 case RID_STATIC:
12726 case RID_EXTERN:
12727 case RID_REGISTER:
12728 case RID_AUTO:
12729 case RID_MUTABLE:
12730 case RID_THREAD:
12731 return true;
12732 default:
12733 return false;
12737 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12739 static bool
12740 keyword_is_function_specifier (enum rid keyword)
12742 switch (keyword)
12744 case RID_INLINE:
12745 case RID_NORETURN:
12746 case RID_VIRTUAL:
12747 case RID_EXPLICIT:
12748 return true;
12749 default:
12750 return false;
12754 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12755 declaration-specifier (C99 6.7). */
12757 bool
12758 keyword_is_decl_specifier (enum rid keyword)
12760 if (keyword_is_storage_class_specifier (keyword)
12761 || keyword_is_type_qualifier (keyword)
12762 || keyword_is_function_specifier (keyword))
12763 return true;
12765 switch (keyword)
12767 case RID_TYPEDEF:
12768 case RID_FRIEND:
12769 case RID_CONSTEXPR:
12770 return true;
12771 default:
12772 return false;
12776 /* Initialize language-specific-bits of tree_contains_struct. */
12778 void
12779 c_common_init_ts (void)
12781 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12782 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12783 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12786 /* Build a user-defined numeric literal out of an integer constant type VALUE
12787 with identifier SUFFIX. */
12789 tree
12790 build_userdef_literal (tree suffix_id, tree value,
12791 enum overflow_type overflow, tree num_string)
12793 tree literal = make_node (USERDEF_LITERAL);
12794 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12795 USERDEF_LITERAL_VALUE (literal) = value;
12796 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12797 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12798 return literal;
12801 /* For vector[index], convert the vector to an array of the underlying type.
12802 Return true if the resulting ARRAY_REF should not be an lvalue. */
12804 bool
12805 convert_vector_to_array_for_subscript (location_t loc,
12806 tree *vecp, tree index)
12808 bool ret = false;
12809 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
12811 tree type = TREE_TYPE (*vecp);
12813 ret = !lvalue_p (*vecp);
12815 if (TREE_CODE (index) == INTEGER_CST)
12816 if (!tree_fits_uhwi_p (index)
12817 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12818 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12820 /* We are building an ARRAY_REF so mark the vector as addressable
12821 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
12822 for function parameters. */
12823 c_common_mark_addressable_vec (*vecp);
12825 *vecp = build1 (VIEW_CONVERT_EXPR,
12826 build_array_type_nelts (TREE_TYPE (type),
12827 TYPE_VECTOR_SUBPARTS (type)),
12828 *vecp);
12830 return ret;
12833 /* Determine which of the operands, if any, is a scalar that needs to be
12834 converted to a vector, for the range of operations. */
12835 enum stv_conv
12836 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12837 bool complain)
12839 tree type0 = TREE_TYPE (op0);
12840 tree type1 = TREE_TYPE (op1);
12841 bool integer_only_op = false;
12842 enum stv_conv ret = stv_firstarg;
12844 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
12845 switch (code)
12847 /* Most GENERIC binary expressions require homogeneous arguments.
12848 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12849 argument that is a vector and a second one that is a scalar, so
12850 we never return stv_secondarg for them. */
12851 case RSHIFT_EXPR:
12852 case LSHIFT_EXPR:
12853 if (TREE_CODE (type0) == INTEGER_TYPE
12854 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12856 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12858 if (complain)
12859 error_at (loc, "conversion of scalar %qT to vector %qT "
12860 "involves truncation", type0, type1);
12861 return stv_error;
12863 else
12864 return stv_firstarg;
12866 break;
12868 case BIT_IOR_EXPR:
12869 case BIT_XOR_EXPR:
12870 case BIT_AND_EXPR:
12871 integer_only_op = true;
12872 /* fall through */
12874 case VEC_COND_EXPR:
12876 case PLUS_EXPR:
12877 case MINUS_EXPR:
12878 case MULT_EXPR:
12879 case TRUNC_DIV_EXPR:
12880 case CEIL_DIV_EXPR:
12881 case FLOOR_DIV_EXPR:
12882 case ROUND_DIV_EXPR:
12883 case EXACT_DIV_EXPR:
12884 case TRUNC_MOD_EXPR:
12885 case FLOOR_MOD_EXPR:
12886 case RDIV_EXPR:
12887 case EQ_EXPR:
12888 case NE_EXPR:
12889 case LE_EXPR:
12890 case GE_EXPR:
12891 case LT_EXPR:
12892 case GT_EXPR:
12893 /* What about UNLT_EXPR? */
12894 if (VECTOR_TYPE_P (type0))
12896 ret = stv_secondarg;
12897 std::swap (type0, type1);
12898 std::swap (op0, op1);
12901 if (TREE_CODE (type0) == INTEGER_TYPE
12902 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12904 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12906 if (complain)
12907 error_at (loc, "conversion of scalar %qT to vector %qT "
12908 "involves truncation", type0, type1);
12909 return stv_error;
12911 return ret;
12913 else if (!integer_only_op
12914 /* Allow integer --> real conversion if safe. */
12915 && (TREE_CODE (type0) == REAL_TYPE
12916 || TREE_CODE (type0) == INTEGER_TYPE)
12917 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12919 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12921 if (complain)
12922 error_at (loc, "conversion of scalar %qT to vector %qT "
12923 "involves truncation", type0, type1);
12924 return stv_error;
12926 return ret;
12928 default:
12929 break;
12932 return stv_nothing;
12935 /* Return the alignment of std::max_align_t.
12937 [support.types.layout] The type max_align_t is a POD type whose alignment
12938 requirement is at least as great as that of every scalar type, and whose
12939 alignment requirement is supported in every context. */
12941 unsigned
12942 max_align_t_align ()
12944 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
12945 TYPE_ALIGN (long_double_type_node));
12946 if (float128_type_node != NULL_TREE)
12947 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
12948 return max_align;
12951 /* Return true iff ALIGN is an integral constant that is a fundamental
12952 alignment, as defined by [basic.align] in the c++-11
12953 specifications.
12955 That is:
12957 [A fundamental alignment is represented by an alignment less than or
12958 equal to the greatest alignment supported by the implementation
12959 in all contexts, which is equal to alignof(max_align_t)]. */
12961 bool
12962 cxx_fundamental_alignment_p (unsigned align)
12964 return (align <= max_align_t_align ());
12967 /* Return true if T is a pointer to a zero-sized aggregate. */
12969 bool
12970 pointer_to_zero_sized_aggr_p (tree t)
12972 if (!POINTER_TYPE_P (t))
12973 return false;
12974 t = TREE_TYPE (t);
12975 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12978 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12979 with no library fallback or for an ADDR_EXPR whose operand is such type
12980 issues an error pointing to the location LOC.
12981 Returns true when the expression has been diagnosed and false
12982 otherwise. */
12983 bool
12984 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12986 if (TREE_CODE (expr) == ADDR_EXPR)
12987 expr = TREE_OPERAND (expr, 0);
12989 if (TREE_TYPE (expr)
12990 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12991 && TREE_CODE (expr) == FUNCTION_DECL
12992 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12993 false positives for user-declared built-ins such as abs or
12994 strlen, and for C++ operators new and delete.
12995 The c_decl_implicit() test avoids false positives for implicitly
12996 declared built-ins with library fallbacks (such as abs). */
12997 && DECL_BUILT_IN (expr)
12998 && DECL_IS_BUILTIN (expr)
12999 && !c_decl_implicit (expr)
13000 && !DECL_ASSEMBLER_NAME_SET_P (expr))
13002 if (loc == UNKNOWN_LOCATION)
13003 loc = EXPR_LOC_OR_LOC (expr, input_location);
13005 /* Reject arguments that are built-in functions with
13006 no library fallback. */
13007 error_at (loc, "built-in function %qE must be directly called", expr);
13009 return true;
13012 return false;
13015 /* If we're creating an if-else-if condition chain, first see if we
13016 already have this COND in the CHAIN. If so, warn and don't add COND
13017 into the vector, otherwise add the COND there. LOC is the location
13018 of COND. */
13020 void
13021 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
13023 /* No chain has been created yet. Do nothing. */
13024 if (*chain == NULL)
13025 return;
13027 if (TREE_SIDE_EFFECTS (cond))
13029 /* Uh-oh! This condition has a side-effect, thus invalidates
13030 the whole chain. */
13031 delete *chain;
13032 *chain = NULL;
13033 return;
13036 unsigned int ix;
13037 tree t;
13038 bool found = false;
13039 FOR_EACH_VEC_ELT (**chain, ix, t)
13040 if (operand_equal_p (cond, t, 0))
13042 if (warning_at (loc, OPT_Wduplicated_cond,
13043 "duplicated %<if%> condition"))
13044 inform (EXPR_LOCATION (t), "previously used here");
13045 found = true;
13046 break;
13049 if (!found
13050 && !CONSTANT_CLASS_P (cond)
13051 /* Don't infinitely grow the chain. */
13052 && (*chain)->length () < 512)
13053 (*chain)->safe_push (cond);
13056 /* Check if array size calculations overflow or if the array covers more
13057 than half of the address space. Return true if the size of the array
13058 is valid, false otherwise. TYPE is the type of the array and NAME is
13059 the name of the array, or NULL_TREE for unnamed arrays. */
13061 bool
13062 valid_array_size_p (location_t loc, tree type, tree name)
13064 if (type != error_mark_node
13065 && COMPLETE_TYPE_P (type)
13066 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
13067 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
13069 if (name)
13070 error_at (loc, "size of array %qE is too large", name);
13071 else
13072 error_at (loc, "size of unnamed array is too large");
13073 return false;
13075 return true;
13078 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
13079 timestamp to replace embedded current dates to get reproducible
13080 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
13082 time_t
13083 cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
13085 char *source_date_epoch;
13086 int64_t epoch;
13087 char *endptr;
13089 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
13090 if (!source_date_epoch)
13091 return (time_t) -1;
13093 errno = 0;
13094 #if defined(INT64_T_IS_LONG)
13095 epoch = strtol (source_date_epoch, &endptr, 10);
13096 #else
13097 epoch = strtoll (source_date_epoch, &endptr, 10);
13098 #endif
13099 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
13100 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
13102 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
13103 "expand to a non-negative integer less than or equal to %wd",
13104 MAX_SOURCE_DATE_EPOCH);
13105 return (time_t) -1;
13108 return (time_t) epoch;
13111 /* Callback for libcpp for offering spelling suggestions for misspelled
13112 directives. GOAL is an unrecognized string; CANDIDATES is a
13113 NULL-terminated array of candidate strings. Return the closest
13114 match to GOAL within CANDIDATES, or NULL if none are good
13115 suggestions. */
13117 const char *
13118 cb_get_suggestion (cpp_reader *, const char *goal,
13119 const char *const *candidates)
13121 best_match<const char *, const char *> bm (goal);
13122 while (*candidates)
13123 bm.consider (*candidates++);
13124 return bm.get_best_meaningful_candidate ();
13127 /* Check and possibly warn if two declarations have contradictory
13128 attributes, such as always_inline vs. noinline. */
13130 bool
13131 diagnose_mismatched_attributes (tree olddecl, tree newdecl)
13133 bool warned = false;
13135 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
13136 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
13137 /* An optimization attribute applied on a declaration after the
13138 definition is likely not what the user wanted. */
13139 if (a2 != NULL_TREE
13140 && DECL_SAVED_TREE (olddecl) != NULL_TREE
13141 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
13142 warned |= warning (OPT_Wattributes,
13143 "optimization attribute on %qD follows "
13144 "definition but the attribute doesn%'t match",
13145 newdecl);
13147 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
13148 if (DECL_DECLARED_INLINE_P (newdecl)
13149 && DECL_UNINLINABLE (olddecl)
13150 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
13151 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
13152 "declaration with attribute noinline", newdecl);
13153 else if (DECL_DECLARED_INLINE_P (olddecl)
13154 && DECL_UNINLINABLE (newdecl)
13155 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
13156 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13157 "noinline follows inline declaration ", newdecl);
13158 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
13159 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
13160 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13161 "%qs follows declaration with attribute %qs",
13162 newdecl, "noinline", "always_inline");
13163 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
13164 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
13165 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13166 "%qs follows declaration with attribute %qs",
13167 newdecl, "always_inline", "noinline");
13168 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
13169 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
13170 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13171 "%qs follows declaration with attribute %qs",
13172 newdecl, "cold", "hot");
13173 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
13174 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
13175 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13176 "%qs follows declaration with attribute %qs",
13177 newdecl, "hot", "cold");
13178 return warned;
13181 #include "gt-c-family-c-common.h"