[RS6000] TOC refs generated during reload
[official-gcc.git] / gcc / c-family / c-common.c
blob569f000150f277b2356318ac199ad7a2769578a2
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 "c-common.h"
29 #include "gimple-expr.h"
30 #include "tm_p.h"
31 #include "stringpool.h"
32 #include "cgraph.h"
33 #include "diagnostic.h"
34 #include "intl.h"
35 #include "stor-layout.h"
36 #include "calls.h"
37 #include "attribs.h"
38 #include "varasm.h"
39 #include "trans-mem.h"
40 #include "c-objc.h"
41 #include "common/common-target.h"
42 #include "langhooks.h"
43 #include "tree-inline.h"
44 #include "toplev.h"
45 #include "tree-iterator.h"
46 #include "opts.h"
47 #include "gimplify.h"
48 #include "substring-locations.h"
50 cpp_reader *parse_in; /* Declared in c-pragma.h. */
52 /* Mode used to build pointers (VOIDmode means ptr_mode). */
54 machine_mode c_default_pointer_mode = VOIDmode;
56 /* The following symbols are subsumed in the c_global_trees array, and
57 listed here individually for documentation purposes.
59 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
61 tree short_integer_type_node;
62 tree long_integer_type_node;
63 tree long_long_integer_type_node;
65 tree short_unsigned_type_node;
66 tree long_unsigned_type_node;
67 tree long_long_unsigned_type_node;
69 tree truthvalue_type_node;
70 tree truthvalue_false_node;
71 tree truthvalue_true_node;
73 tree ptrdiff_type_node;
75 tree unsigned_char_type_node;
76 tree signed_char_type_node;
77 tree wchar_type_node;
79 tree char16_type_node;
80 tree char32_type_node;
82 tree float_type_node;
83 tree double_type_node;
84 tree long_double_type_node;
86 tree complex_integer_type_node;
87 tree complex_float_type_node;
88 tree complex_double_type_node;
89 tree complex_long_double_type_node;
91 tree dfloat32_type_node;
92 tree dfloat64_type_node;
93 tree_dfloat128_type_node;
95 tree intQI_type_node;
96 tree intHI_type_node;
97 tree intSI_type_node;
98 tree intDI_type_node;
99 tree intTI_type_node;
101 tree unsigned_intQI_type_node;
102 tree unsigned_intHI_type_node;
103 tree unsigned_intSI_type_node;
104 tree unsigned_intDI_type_node;
105 tree unsigned_intTI_type_node;
107 tree widest_integer_literal_type_node;
108 tree widest_unsigned_literal_type_node;
110 Nodes for types `void *' and `const void *'.
112 tree ptr_type_node, const_ptr_type_node;
114 Nodes for types `char *' and `const char *'.
116 tree string_type_node, const_string_type_node;
118 Type `char[SOMENUMBER]'.
119 Used when an array of char is needed and the size is irrelevant.
121 tree char_array_type_node;
123 Type `wchar_t[SOMENUMBER]' or something like it.
124 Used when a wide string literal is created.
126 tree wchar_array_type_node;
128 Type `char16_t[SOMENUMBER]' or something like it.
129 Used when a UTF-16 string literal is created.
131 tree char16_array_type_node;
133 Type `char32_t[SOMENUMBER]' or something like it.
134 Used when a UTF-32 string literal is created.
136 tree char32_array_type_node;
138 Type `int ()' -- used for implicit declaration of functions.
140 tree default_function_type;
142 A VOID_TYPE node, packaged in a TREE_LIST.
144 tree void_list_node;
146 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
147 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
148 VAR_DECLS, but C++ does.)
150 tree function_name_decl_node;
151 tree pretty_function_name_decl_node;
152 tree c99_function_name_decl_node;
154 Stack of nested function name VAR_DECLs.
156 tree saved_function_name_decls;
160 tree c_global_trees[CTI_MAX];
162 /* Switches common to the C front ends. */
164 /* Nonzero means don't output line number information. */
166 char flag_no_line_commands;
168 /* Nonzero causes -E output not to be done, but directives such as
169 #define that have side effects are still obeyed. */
171 char flag_no_output;
173 /* Nonzero means dump macros in some fashion. */
175 char flag_dump_macros;
177 /* Nonzero means pass #include lines through to the output. */
179 char flag_dump_includes;
181 /* Nonzero means process PCH files while preprocessing. */
183 bool flag_pch_preprocess;
185 /* The file name to which we should write a precompiled header, or
186 NULL if no header will be written in this compile. */
188 const char *pch_file;
190 /* Nonzero if an ISO standard was selected. It rejects macros in the
191 user's namespace. */
192 int flag_iso;
194 /* C/ObjC language option variables. */
197 /* Nonzero means allow type mismatches in conditional expressions;
198 just make their values `void'. */
200 int flag_cond_mismatch;
202 /* Nonzero means enable C89 Amendment 1 features. */
204 int flag_isoc94;
206 /* Nonzero means use the ISO C99 (or C11) dialect of C. */
208 int flag_isoc99;
210 /* Nonzero means use the ISO C11 dialect of C. */
212 int flag_isoc11;
214 /* Nonzero means that we have builtin functions, and main is an int. */
216 int flag_hosted = 1;
219 /* ObjC language option variables. */
222 /* Tells the compiler that this is a special run. Do not perform any
223 compiling, instead we are to test some platform dependent features
224 and output a C header file with appropriate definitions. */
226 int print_struct_values;
228 /* Tells the compiler what is the constant string class for ObjC. */
230 const char *constant_string_class_name;
233 /* C++ language option variables. */
235 /* The reference version of the ABI for -Wabi. */
237 int warn_abi_version = -1;
239 /* Nonzero means generate separate instantiation control files and
240 juggle them at link time. */
242 int flag_use_repository;
244 /* The C++ dialect being used. Default set in c_common_post_options. */
246 enum cxx_dialect cxx_dialect = cxx_unset;
248 /* Maximum template instantiation depth. This limit exists to limit the
249 time it takes to notice excessively recursive template instantiations.
251 The default is lower than the 1024 recommended by the C++0x standard
252 because G++ runs out of stack before 1024 with highly recursive template
253 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
255 int max_tinst_depth = 900;
257 /* The elements of `ridpointers' are identifier nodes for the reserved
258 type names and storage classes. It is indexed by a RID_... value. */
259 tree *ridpointers;
261 tree (*make_fname_decl) (location_t, tree, int);
263 /* Nonzero means don't warn about problems that occur when the code is
264 executed. */
265 int c_inhibit_evaluation_warnings;
267 /* Whether we are building a boolean conversion inside
268 convert_for_assignment, or some other late binary operation. If
269 build_binary_op is called for C (from code shared by C and C++) in
270 this case, then the operands have already been folded and the
271 result will not be folded again, so C_MAYBE_CONST_EXPR should not
272 be generated. */
273 bool in_late_binary_op;
275 /* Whether lexing has been completed, so subsequent preprocessor
276 errors should use the compiler's input_location. */
277 bool done_lexing = false;
279 /* Information about how a function name is generated. */
280 struct fname_var_t
282 tree *const decl; /* pointer to the VAR_DECL. */
283 const unsigned rid; /* RID number for the identifier. */
284 const int pretty; /* How pretty is it? */
287 /* The three ways of getting then name of the current function. */
289 const struct fname_var_t fname_vars[] =
291 /* C99 compliant __func__, must be first. */
292 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
293 /* GCC __FUNCTION__ compliant. */
294 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
295 /* GCC __PRETTY_FUNCTION__ compliant. */
296 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
297 {NULL, 0, 0},
300 /* Global visibility options. */
301 struct visibility_flags visibility_options;
303 static tree check_case_value (location_t, tree);
304 static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
305 bool *);
307 static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
308 static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
309 static tree handle_common_attribute (tree *, tree, tree, int, bool *);
310 static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
311 static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
312 static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
313 static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
314 int, bool *);
315 static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
316 int, bool *);
317 static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
318 bool *);
319 static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
320 static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
321 static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
322 static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
323 static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
324 static tree handle_always_inline_attribute (tree *, tree, tree, int,
325 bool *);
326 static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
327 static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
328 static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
329 static tree handle_error_attribute (tree *, tree, tree, int, bool *);
330 static tree handle_used_attribute (tree *, tree, tree, int, bool *);
331 static tree handle_externally_visible_attribute (tree *, tree, tree, int,
332 bool *);
333 static tree handle_no_reorder_attribute (tree *, tree, tree, int,
334 bool *);
335 static tree handle_const_attribute (tree *, tree, tree, int, bool *);
336 static tree handle_transparent_union_attribute (tree *, tree, tree,
337 int, bool *);
338 static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
339 int, bool *);
340 static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
341 static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
342 static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
343 static tree handle_section_attribute (tree *, tree, tree, int, bool *);
344 static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
345 static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
346 static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
347 static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
348 static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
349 static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
350 static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
351 static tree handle_visibility_attribute (tree *, tree, tree, int,
352 bool *);
353 static tree handle_tls_model_attribute (tree *, tree, tree, int,
354 bool *);
355 static tree handle_no_instrument_function_attribute (tree *, tree,
356 tree, int, bool *);
357 static tree handle_no_profile_instrument_function_attribute (tree *, tree,
358 tree, int, bool *);
359 static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
360 static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
361 static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
362 bool *);
363 static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
364 static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
365 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
366 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
367 static tree handle_deprecated_attribute (tree *, tree, tree, int,
368 bool *);
369 static tree handle_vector_size_attribute (tree *, tree, tree, int,
370 bool *);
371 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
372 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
373 static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
374 static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
375 bool *);
376 static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
377 static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
378 static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
379 static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
380 static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
381 static tree handle_target_attribute (tree *, tree, tree, int, bool *);
382 static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
383 static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
384 static tree ignore_attribute (tree *, tree, tree, int, bool *);
385 static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
386 static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
387 static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
388 static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
389 static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
390 bool *);
391 static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
392 static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
393 bool *);
394 static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
395 static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
396 static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
397 static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
399 static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
400 static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
401 static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
402 static int resort_field_decl_cmp (const void *, const void *);
404 /* Reserved words. The third field is a mask: keywords are disabled
405 if they match the mask.
407 Masks for languages:
408 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
409 C --std=c99: D_CXXONLY | D_OBJC
410 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
411 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
412 C++ --std=c0x: D_CONLY | D_OBJC
413 ObjC++ is like C++ except that D_OBJC is not set
415 If -fno-asm is used, D_ASM is added to the mask. If
416 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
417 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
418 In C with -Wc++-compat, we warn if D_CXXWARN is set.
420 Note the complication of the D_CXX_OBJC keywords. These are
421 reserved words such as 'class'. In C++, 'class' is a reserved
422 word. In Objective-C++ it is too. In Objective-C, it is a
423 reserved word too, but only if it follows an '@' sign.
425 const struct c_common_resword c_common_reswords[] =
427 { "_Alignas", RID_ALIGNAS, D_CONLY },
428 { "_Alignof", RID_ALIGNOF, D_CONLY },
429 { "_Atomic", RID_ATOMIC, D_CONLY },
430 { "_Bool", RID_BOOL, D_CONLY },
431 { "_Complex", RID_COMPLEX, 0 },
432 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
433 { "_Cilk_sync", RID_CILK_SYNC, 0 },
434 { "_Cilk_for", RID_CILK_FOR, 0 },
435 { "_Imaginary", RID_IMAGINARY, D_CONLY },
436 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
437 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
438 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
439 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
440 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
441 { "_Sat", RID_SAT, D_CONLY | D_EXT },
442 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
443 { "_Noreturn", RID_NORETURN, D_CONLY },
444 { "_Generic", RID_GENERIC, D_CONLY },
445 { "_Thread_local", RID_THREAD, D_CONLY },
446 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
447 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
448 { "__alignof", RID_ALIGNOF, 0 },
449 { "__alignof__", RID_ALIGNOF, 0 },
450 { "__asm", RID_ASM, 0 },
451 { "__asm__", RID_ASM, 0 },
452 { "__attribute", RID_ATTRIBUTE, 0 },
453 { "__attribute__", RID_ATTRIBUTE, 0 },
454 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
455 { "__bases", RID_BASES, D_CXXONLY },
456 { "__builtin_call_with_static_chain",
457 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
458 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
459 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
460 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
461 { "__builtin_offsetof", RID_OFFSETOF, 0 },
462 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
463 { "__builtin_va_arg", RID_VA_ARG, 0 },
464 { "__complex", RID_COMPLEX, 0 },
465 { "__complex__", RID_COMPLEX, 0 },
466 { "__const", RID_CONST, 0 },
467 { "__const__", RID_CONST, 0 },
468 { "__decltype", RID_DECLTYPE, D_CXXONLY },
469 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
470 { "__extension__", RID_EXTENSION, 0 },
471 { "__func__", RID_C99_FUNCTION_NAME, 0 },
472 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
473 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
474 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
475 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
476 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
477 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
478 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
479 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
480 { "__imag", RID_IMAGPART, 0 },
481 { "__imag__", RID_IMAGPART, 0 },
482 { "__inline", RID_INLINE, 0 },
483 { "__inline__", RID_INLINE, 0 },
484 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
485 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
486 { "__is_class", RID_IS_CLASS, D_CXXONLY },
487 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
488 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
489 { "__is_final", RID_IS_FINAL, D_CXXONLY },
490 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
491 { "__is_pod", RID_IS_POD, D_CXXONLY },
492 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
493 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
494 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
495 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
496 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
497 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
498 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
499 { "__is_union", RID_IS_UNION, D_CXXONLY },
500 { "__label__", RID_LABEL, 0 },
501 { "__null", RID_NULL, 0 },
502 { "__real", RID_REALPART, 0 },
503 { "__real__", RID_REALPART, 0 },
504 { "__restrict", RID_RESTRICT, 0 },
505 { "__restrict__", RID_RESTRICT, 0 },
506 { "__signed", RID_SIGNED, 0 },
507 { "__signed__", RID_SIGNED, 0 },
508 { "__thread", RID_THREAD, 0 },
509 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
510 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
511 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
512 { "__typeof", RID_TYPEOF, 0 },
513 { "__typeof__", RID_TYPEOF, 0 },
514 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
515 { "__volatile", RID_VOLATILE, 0 },
516 { "__volatile__", RID_VOLATILE, 0 },
517 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
518 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
519 { "asm", RID_ASM, D_ASM },
520 { "auto", RID_AUTO, 0 },
521 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
522 { "break", RID_BREAK, 0 },
523 { "case", RID_CASE, 0 },
524 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
525 { "char", RID_CHAR, 0 },
526 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
527 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
528 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
529 { "const", RID_CONST, 0 },
530 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
531 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
532 { "continue", RID_CONTINUE, 0 },
533 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
534 { "default", RID_DEFAULT, 0 },
535 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
536 { "do", RID_DO, 0 },
537 { "double", RID_DOUBLE, 0 },
538 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
539 { "else", RID_ELSE, 0 },
540 { "enum", RID_ENUM, 0 },
541 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
542 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
543 { "extern", RID_EXTERN, 0 },
544 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
545 { "float", RID_FLOAT, 0 },
546 { "for", RID_FOR, 0 },
547 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
548 { "goto", RID_GOTO, 0 },
549 { "if", RID_IF, 0 },
550 { "inline", RID_INLINE, D_EXT89 },
551 { "int", RID_INT, 0 },
552 { "long", RID_LONG, 0 },
553 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
554 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
555 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
556 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
557 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
558 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
559 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
560 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
561 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
562 { "register", RID_REGISTER, 0 },
563 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
564 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
565 { "return", RID_RETURN, 0 },
566 { "short", RID_SHORT, 0 },
567 { "signed", RID_SIGNED, 0 },
568 { "sizeof", RID_SIZEOF, 0 },
569 { "static", RID_STATIC, 0 },
570 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
571 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
572 { "struct", RID_STRUCT, 0 },
573 { "switch", RID_SWITCH, 0 },
574 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
575 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
576 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
577 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
578 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
579 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
580 { "typedef", RID_TYPEDEF, 0 },
581 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
582 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
583 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
584 { "union", RID_UNION, 0 },
585 { "unsigned", RID_UNSIGNED, 0 },
586 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
587 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
588 { "void", RID_VOID, 0 },
589 { "volatile", RID_VOLATILE, 0 },
590 { "wchar_t", RID_WCHAR, D_CXXONLY },
591 { "while", RID_WHILE, 0 },
593 /* C++ transactional memory. */
594 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
595 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
596 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
597 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
599 /* Concepts-related keywords */
600 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
601 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
603 /* These Objective-C keywords are recognized only immediately after
604 an '@'. */
605 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
606 { "defs", RID_AT_DEFS, D_OBJC },
607 { "encode", RID_AT_ENCODE, D_OBJC },
608 { "end", RID_AT_END, D_OBJC },
609 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
610 { "interface", RID_AT_INTERFACE, D_OBJC },
611 { "protocol", RID_AT_PROTOCOL, D_OBJC },
612 { "selector", RID_AT_SELECTOR, D_OBJC },
613 { "finally", RID_AT_FINALLY, D_OBJC },
614 { "optional", RID_AT_OPTIONAL, D_OBJC },
615 { "required", RID_AT_REQUIRED, D_OBJC },
616 { "property", RID_AT_PROPERTY, D_OBJC },
617 { "package", RID_AT_PACKAGE, D_OBJC },
618 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
619 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
620 /* These are recognized only in protocol-qualifier context
621 (see above) */
622 { "bycopy", RID_BYCOPY, D_OBJC },
623 { "byref", RID_BYREF, D_OBJC },
624 { "in", RID_IN, D_OBJC },
625 { "inout", RID_INOUT, D_OBJC },
626 { "oneway", RID_ONEWAY, D_OBJC },
627 { "out", RID_OUT, D_OBJC },
628 /* These are recognized inside a property attribute list */
629 { "assign", RID_ASSIGN, D_OBJC },
630 { "copy", RID_COPY, D_OBJC },
631 { "getter", RID_GETTER, D_OBJC },
632 { "nonatomic", RID_NONATOMIC, D_OBJC },
633 { "readonly", RID_READONLY, D_OBJC },
634 { "readwrite", RID_READWRITE, D_OBJC },
635 { "retain", RID_RETAIN, D_OBJC },
636 { "setter", RID_SETTER, D_OBJC },
639 const unsigned int num_c_common_reswords =
640 sizeof c_common_reswords / sizeof (struct c_common_resword);
642 /* Table of machine-independent attributes common to all C-like languages.
644 All attributes referencing arguments should be additionally processed
645 in chkp_copy_function_type_adding_bounds for correct instrumentation
646 by Pointer Bounds Checker.
647 Current list of processed common attributes: nonnull. */
648 const struct attribute_spec c_common_attribute_table[] =
650 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
651 affects_type_identity } */
652 { "packed", 0, 0, false, false, false,
653 handle_packed_attribute , false},
654 { "nocommon", 0, 0, true, false, false,
655 handle_nocommon_attribute, false},
656 { "common", 0, 0, true, false, false,
657 handle_common_attribute, false },
658 /* FIXME: logically, noreturn attributes should be listed as
659 "false, true, true" and apply to function types. But implementing this
660 would require all the places in the compiler that use TREE_THIS_VOLATILE
661 on a decl to identify non-returning functions to be located and fixed
662 to check the function type instead. */
663 { "noreturn", 0, 0, true, false, false,
664 handle_noreturn_attribute, false },
665 { "volatile", 0, 0, true, false, false,
666 handle_noreturn_attribute, false },
667 { "stack_protect", 0, 0, true, false, false,
668 handle_stack_protect_attribute, false },
669 { "noinline", 0, 0, true, false, false,
670 handle_noinline_attribute, false },
671 { "noclone", 0, 0, true, false, false,
672 handle_noclone_attribute, false },
673 { "no_icf", 0, 0, true, false, false,
674 handle_noicf_attribute, false },
675 { "leaf", 0, 0, true, false, false,
676 handle_leaf_attribute, false },
677 { "always_inline", 0, 0, true, false, false,
678 handle_always_inline_attribute, false },
679 { "gnu_inline", 0, 0, true, false, false,
680 handle_gnu_inline_attribute, false },
681 { "artificial", 0, 0, true, false, false,
682 handle_artificial_attribute, false },
683 { "flatten", 0, 0, true, false, false,
684 handle_flatten_attribute, false },
685 { "used", 0, 0, true, false, false,
686 handle_used_attribute, false },
687 { "unused", 0, 0, false, false, false,
688 handle_unused_attribute, false },
689 { "externally_visible", 0, 0, true, false, false,
690 handle_externally_visible_attribute, false },
691 { "no_reorder", 0, 0, true, false, false,
692 handle_no_reorder_attribute, false },
693 /* The same comments as for noreturn attributes apply to const ones. */
694 { "const", 0, 0, true, false, false,
695 handle_const_attribute, false },
696 { "scalar_storage_order", 1, 1, false, false, false,
697 handle_scalar_storage_order_attribute, false },
698 { "transparent_union", 0, 0, false, false, false,
699 handle_transparent_union_attribute, false },
700 { "constructor", 0, 1, true, false, false,
701 handle_constructor_attribute, false },
702 { "destructor", 0, 1, true, false, false,
703 handle_destructor_attribute, false },
704 { "mode", 1, 1, false, true, false,
705 handle_mode_attribute, false },
706 { "section", 1, 1, true, false, false,
707 handle_section_attribute, false },
708 { "aligned", 0, 1, false, false, false,
709 handle_aligned_attribute, false },
710 { "weak", 0, 0, true, false, false,
711 handle_weak_attribute, false },
712 { "noplt", 0, 0, true, false, false,
713 handle_noplt_attribute, false },
714 { "ifunc", 1, 1, true, false, false,
715 handle_ifunc_attribute, false },
716 { "alias", 1, 1, true, false, false,
717 handle_alias_attribute, false },
718 { "weakref", 0, 1, true, false, false,
719 handle_weakref_attribute, false },
720 { "no_instrument_function", 0, 0, true, false, false,
721 handle_no_instrument_function_attribute,
722 false },
723 { "no_profile_instrument_function", 0, 0, true, false, false,
724 handle_no_profile_instrument_function_attribute,
725 false },
726 { "malloc", 0, 0, true, false, false,
727 handle_malloc_attribute, false },
728 { "returns_twice", 0, 0, true, false, false,
729 handle_returns_twice_attribute, false },
730 { "no_stack_limit", 0, 0, true, false, false,
731 handle_no_limit_stack_attribute, false },
732 { "pure", 0, 0, true, false, false,
733 handle_pure_attribute, false },
734 { "transaction_callable", 0, 0, false, true, false,
735 handle_tm_attribute, false },
736 { "transaction_unsafe", 0, 0, false, true, false,
737 handle_tm_attribute, true },
738 { "transaction_safe", 0, 0, false, true, false,
739 handle_tm_attribute, true },
740 { "transaction_safe_dynamic", 0, 0, true, false, false,
741 handle_tm_attribute, false },
742 { "transaction_may_cancel_outer", 0, 0, false, true, false,
743 handle_tm_attribute, false },
744 /* ??? These two attributes didn't make the transition from the
745 Intel language document to the multi-vendor language document. */
746 { "transaction_pure", 0, 0, false, true, false,
747 handle_tm_attribute, false },
748 { "transaction_wrap", 1, 1, true, false, false,
749 handle_tm_wrap_attribute, false },
750 /* For internal use (marking of builtins) only. The name contains space
751 to prevent its usage in source code. */
752 { "no vops", 0, 0, true, false, false,
753 handle_novops_attribute, false },
754 { "deprecated", 0, 1, false, false, false,
755 handle_deprecated_attribute, false },
756 { "vector_size", 1, 1, false, true, false,
757 handle_vector_size_attribute, false },
758 { "visibility", 1, 1, false, false, false,
759 handle_visibility_attribute, false },
760 { "tls_model", 1, 1, true, false, false,
761 handle_tls_model_attribute, false },
762 { "nonnull", 0, -1, false, true, true,
763 handle_nonnull_attribute, false },
764 { "nothrow", 0, 0, true, false, false,
765 handle_nothrow_attribute, false },
766 { "may_alias", 0, 0, false, true, false, NULL, false },
767 { "cleanup", 1, 1, true, false, false,
768 handle_cleanup_attribute, false },
769 { "warn_unused_result", 0, 0, false, true, true,
770 handle_warn_unused_result_attribute, false },
771 { "sentinel", 0, 1, false, true, true,
772 handle_sentinel_attribute, false },
773 /* For internal use (marking of builtins) only. The name contains space
774 to prevent its usage in source code. */
775 { "type generic", 0, 0, false, true, true,
776 handle_type_generic_attribute, false },
777 { "alloc_size", 1, 2, false, true, true,
778 handle_alloc_size_attribute, false },
779 { "cold", 0, 0, true, false, false,
780 handle_cold_attribute, false },
781 { "hot", 0, 0, true, false, false,
782 handle_hot_attribute, false },
783 { "no_address_safety_analysis",
784 0, 0, true, false, false,
785 handle_no_address_safety_analysis_attribute,
786 false },
787 { "no_sanitize_address", 0, 0, true, false, false,
788 handle_no_sanitize_address_attribute,
789 false },
790 { "no_sanitize_thread", 0, 0, true, false, false,
791 handle_no_sanitize_address_attribute,
792 false },
793 { "no_sanitize_undefined", 0, 0, true, false, false,
794 handle_no_sanitize_undefined_attribute,
795 false },
796 { "warning", 1, 1, true, false, false,
797 handle_error_attribute, false },
798 { "error", 1, 1, true, false, false,
799 handle_error_attribute, false },
800 { "target", 1, -1, true, false, false,
801 handle_target_attribute, false },
802 { "target_clones", 1, -1, true, false, false,
803 handle_target_clones_attribute, false },
804 { "optimize", 1, -1, true, false, false,
805 handle_optimize_attribute, false },
806 /* For internal use only. The leading '*' both prevents its usage in
807 source code and signals that it may be overridden by machine tables. */
808 { "*tm regparm", 0, 0, false, true, true,
809 ignore_attribute, false },
810 { "no_split_stack", 0, 0, true, false, false,
811 handle_no_split_stack_attribute, false },
812 /* For internal use (marking of builtins and runtime functions) only.
813 The name contains space to prevent its usage in source code. */
814 { "fn spec", 1, 1, false, true, true,
815 handle_fnspec_attribute, false },
816 { "warn_unused", 0, 0, false, false, false,
817 handle_warn_unused_attribute, false },
818 { "returns_nonnull", 0, 0, false, true, true,
819 handle_returns_nonnull_attribute, false },
820 { "omp declare simd", 0, -1, true, false, false,
821 handle_omp_declare_simd_attribute, false },
822 { "cilk simd function", 0, -1, true, false, false,
823 handle_omp_declare_simd_attribute, false },
824 { "simd", 0, 1, true, false, false,
825 handle_simd_attribute, false },
826 { "omp declare target", 0, 0, true, false, false,
827 handle_omp_declare_target_attribute, false },
828 { "omp declare target link", 0, 0, true, false, false,
829 handle_omp_declare_target_attribute, false },
830 { "alloc_align", 1, 1, false, true, true,
831 handle_alloc_align_attribute, false },
832 { "assume_aligned", 1, 2, false, true, true,
833 handle_assume_aligned_attribute, false },
834 { "designated_init", 0, 0, false, true, false,
835 handle_designated_init_attribute, false },
836 { "bnd_variable_size", 0, 0, true, false, false,
837 handle_bnd_variable_size_attribute, false },
838 { "bnd_legacy", 0, 0, true, false, false,
839 handle_bnd_legacy, false },
840 { "bnd_instrument", 0, 0, true, false, false,
841 handle_bnd_instrument, false },
842 { NULL, 0, 0, false, false, false, NULL, false }
845 /* Give the specifications for the format attributes, used by C and all
846 descendants.
848 All attributes referencing arguments should be additionally processed
849 in chkp_copy_function_type_adding_bounds for correct instrumentation
850 by Pointer Bounds Checker.
851 Current list of processed format attributes: format, format_arg. */
852 const struct attribute_spec c_common_format_attribute_table[] =
854 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
855 affects_type_identity } */
856 { "format", 3, 3, false, true, true,
857 handle_format_attribute, false },
858 { "format_arg", 1, 1, false, true, true,
859 handle_format_arg_attribute, false },
860 { NULL, 0, 0, false, false, false, NULL, false }
863 /* Return identifier for address space AS. */
865 const char *
866 c_addr_space_name (addr_space_t as)
868 int rid = RID_FIRST_ADDR_SPACE + as;
869 gcc_assert (ridpointers [rid]);
870 return IDENTIFIER_POINTER (ridpointers [rid]);
873 /* Push current bindings for the function name VAR_DECLS. */
875 void
876 start_fname_decls (void)
878 unsigned ix;
879 tree saved = NULL_TREE;
881 for (ix = 0; fname_vars[ix].decl; ix++)
883 tree decl = *fname_vars[ix].decl;
885 if (decl)
887 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
888 saved);
889 *fname_vars[ix].decl = NULL_TREE;
892 if (saved || saved_function_name_decls)
893 /* Normally they'll have been NULL, so only push if we've got a
894 stack, or they are non-NULL. */
895 saved_function_name_decls = tree_cons (saved, NULL_TREE,
896 saved_function_name_decls);
899 /* Finish up the current bindings, adding them into the current function's
900 statement tree. This must be done _before_ finish_stmt_tree is called.
901 If there is no current function, we must be at file scope and no statements
902 are involved. Pop the previous bindings. */
904 void
905 finish_fname_decls (void)
907 unsigned ix;
908 tree stmts = NULL_TREE;
909 tree stack = saved_function_name_decls;
911 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
912 append_to_statement_list (TREE_VALUE (stack), &stmts);
914 if (stmts)
916 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
918 if (TREE_CODE (*bodyp) == BIND_EXPR)
919 bodyp = &BIND_EXPR_BODY (*bodyp);
921 append_to_statement_list_force (*bodyp, &stmts);
922 *bodyp = stmts;
925 for (ix = 0; fname_vars[ix].decl; ix++)
926 *fname_vars[ix].decl = NULL_TREE;
928 if (stack)
930 /* We had saved values, restore them. */
931 tree saved;
933 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
935 tree decl = TREE_PURPOSE (saved);
936 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
938 *fname_vars[ix].decl = decl;
940 stack = TREE_CHAIN (stack);
942 saved_function_name_decls = stack;
945 /* Return the text name of the current function, suitably prettified
946 by PRETTY_P. Return string must be freed by caller. */
948 const char *
949 fname_as_string (int pretty_p)
951 const char *name = "top level";
952 char *namep;
953 int vrb = 2, len;
954 cpp_string cstr = { 0, 0 }, strname;
956 if (!pretty_p)
958 name = "";
959 vrb = 0;
962 if (current_function_decl)
963 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
965 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
967 namep = XNEWVEC (char, len);
968 snprintf (namep, len, "\"%s\"", name);
969 strname.text = (unsigned char *) namep;
970 strname.len = len - 1;
972 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
974 XDELETEVEC (namep);
975 return (const char *) cstr.text;
978 return namep;
981 /* Return the VAR_DECL for a const char array naming the current
982 function. If the VAR_DECL has not yet been created, create it
983 now. RID indicates how it should be formatted and IDENTIFIER_NODE
984 ID is its name (unfortunately C and C++ hold the RID values of
985 keywords in different places, so we can't derive RID from ID in
986 this language independent code. LOC is the location of the
987 function. */
989 tree
990 fname_decl (location_t loc, unsigned int rid, tree id)
992 unsigned ix;
993 tree decl = NULL_TREE;
995 for (ix = 0; fname_vars[ix].decl; ix++)
996 if (fname_vars[ix].rid == rid)
997 break;
999 decl = *fname_vars[ix].decl;
1000 if (!decl)
1002 /* If a tree is built here, it would normally have the lineno of
1003 the current statement. Later this tree will be moved to the
1004 beginning of the function and this line number will be wrong.
1005 To avoid this problem set the lineno to 0 here; that prevents
1006 it from appearing in the RTL. */
1007 tree stmts;
1008 location_t saved_location = input_location;
1009 input_location = UNKNOWN_LOCATION;
1011 stmts = push_stmt_list ();
1012 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
1013 stmts = pop_stmt_list (stmts);
1014 if (!IS_EMPTY_STMT (stmts))
1015 saved_function_name_decls
1016 = tree_cons (decl, stmts, saved_function_name_decls);
1017 *fname_vars[ix].decl = decl;
1018 input_location = saved_location;
1020 if (!ix && !current_function_decl)
1021 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
1023 return decl;
1026 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1028 tree
1029 fix_string_type (tree value)
1031 int length = TREE_STRING_LENGTH (value);
1032 int nchars;
1033 tree e_type, i_type, a_type;
1035 /* Compute the number of elements, for the array type. */
1036 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1038 nchars = length;
1039 e_type = char_type_node;
1041 else if (TREE_TYPE (value) == char16_array_type_node)
1043 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1044 e_type = char16_type_node;
1046 else if (TREE_TYPE (value) == char32_array_type_node)
1048 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1049 e_type = char32_type_node;
1051 else
1053 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1054 e_type = wchar_type_node;
1057 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1058 limit in C++98 Annex B is very large (65536) and is not normative,
1059 so we do not diagnose it (warn_overlength_strings is forced off
1060 in c_common_post_options). */
1061 if (warn_overlength_strings)
1063 const int nchars_max = flag_isoc99 ? 4095 : 509;
1064 const int relevant_std = flag_isoc99 ? 99 : 90;
1065 if (nchars - 1 > nchars_max)
1066 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1067 separate the %d from the 'C'. 'ISO' should not be
1068 translated, but it may be moved after 'C%d' in languages
1069 where modifiers follow nouns. */
1070 pedwarn (input_location, OPT_Woverlength_strings,
1071 "string length %qd is greater than the length %qd "
1072 "ISO C%d compilers are required to support",
1073 nchars - 1, nchars_max, relevant_std);
1076 /* Create the array type for the string constant. The ISO C++
1077 standard says that a string literal has type `const char[N]' or
1078 `const wchar_t[N]'. We use the same logic when invoked as a C
1079 front-end with -Wwrite-strings.
1080 ??? We should change the type of an expression depending on the
1081 state of a warning flag. We should just be warning -- see how
1082 this is handled in the C++ front-end for the deprecated implicit
1083 conversion from string literals to `char*' or `wchar_t*'.
1085 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1086 array type being the unqualified version of that type.
1087 Therefore, if we are constructing an array of const char, we must
1088 construct the matching unqualified array type first. The C front
1089 end does not require this, but it does no harm, so we do it
1090 unconditionally. */
1091 i_type = build_index_type (size_int (nchars - 1));
1092 a_type = build_array_type (e_type, i_type);
1093 if (c_dialect_cxx() || warn_write_strings)
1094 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
1096 TREE_TYPE (value) = a_type;
1097 TREE_CONSTANT (value) = 1;
1098 TREE_READONLY (value) = 1;
1099 TREE_STATIC (value) = 1;
1100 return value;
1103 /* Given a string of type STRING_TYPE, determine what kind of string
1104 token would give an equivalent execution encoding: CPP_STRING,
1105 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
1106 This may not be exactly the string token type that initially created
1107 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
1108 string type at this point.
1110 This effectively reverses part of the logic in lex_string and
1111 fix_string_type. */
1113 static enum cpp_ttype
1114 get_cpp_ttype_from_string_type (tree string_type)
1116 gcc_assert (string_type);
1117 if (TREE_CODE (string_type) != ARRAY_TYPE)
1118 return CPP_OTHER;
1120 tree element_type = TREE_TYPE (string_type);
1121 if (TREE_CODE (element_type) != INTEGER_TYPE)
1122 return CPP_OTHER;
1124 int bits_per_character = TYPE_PRECISION (element_type);
1125 switch (bits_per_character)
1127 case 8:
1128 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
1129 case 16:
1130 return CPP_STRING16;
1131 case 32:
1132 return CPP_STRING32;
1135 return CPP_OTHER;
1138 /* The global record of string concatentations, for use in
1139 extracting locations within string literals. */
1141 GTY(()) string_concat_db *g_string_concat_db;
1143 /* Attempt to determine the source range of the substring.
1144 If successful, return NULL and write the source range to *OUT_RANGE.
1145 Otherwise return an error message. Error messages are intended
1146 for GCC developers (to help debugging) rather than for end-users. */
1148 const char *
1149 substring_loc::get_range (source_range *out_range) const
1151 gcc_assert (out_range);
1153 enum cpp_ttype tok_type = get_cpp_ttype_from_string_type (m_string_type);
1154 if (tok_type == CPP_OTHER)
1155 return "unrecognized string type";
1157 return get_source_range_for_substring (parse_in, g_string_concat_db,
1158 m_fmt_string_loc, tok_type,
1159 m_start_idx, m_end_idx,
1160 out_range);
1164 /* Fold X for consideration by one of the warning functions when checking
1165 whether an expression has a constant value. */
1167 static tree
1168 fold_for_warn (tree x)
1170 if (c_dialect_cxx ())
1171 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1172 else
1173 /* The C front-end has already folded X appropriately. */
1174 return x;
1177 /* Print a warning if a constant expression had overflow in folding.
1178 Invoke this function on every expression that the language
1179 requires to be a constant expression.
1180 Note the ANSI C standard says it is erroneous for a
1181 constant expression to overflow. */
1183 void
1184 constant_expression_warning (tree value)
1186 if (warn_overflow && pedantic
1187 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1188 || TREE_CODE (value) == FIXED_CST
1189 || TREE_CODE (value) == VECTOR_CST
1190 || TREE_CODE (value) == COMPLEX_CST)
1191 && TREE_OVERFLOW (value))
1192 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
1195 /* The same as above but print an unconditional error. */
1196 void
1197 constant_expression_error (tree value)
1199 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1200 || TREE_CODE (value) == FIXED_CST
1201 || TREE_CODE (value) == VECTOR_CST
1202 || TREE_CODE (value) == COMPLEX_CST)
1203 && TREE_OVERFLOW (value))
1204 error ("overflow in constant expression");
1207 /* Print a warning if an expression had overflow in folding and its
1208 operands hadn't.
1210 Invoke this function on every expression that
1211 (1) appears in the source code, and
1212 (2) is a constant expression that overflowed, and
1213 (3) is not already checked by convert_and_check;
1214 however, do not invoke this function on operands of explicit casts
1215 or when the expression is the result of an operator and any operand
1216 already overflowed. */
1218 void
1219 overflow_warning (location_t loc, tree value)
1221 if (c_inhibit_evaluation_warnings != 0)
1222 return;
1224 switch (TREE_CODE (value))
1226 case INTEGER_CST:
1227 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
1228 break;
1230 case REAL_CST:
1231 warning_at (loc, OPT_Woverflow,
1232 "floating point overflow in expression");
1233 break;
1235 case FIXED_CST:
1236 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
1237 break;
1239 case VECTOR_CST:
1240 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
1241 break;
1243 case COMPLEX_CST:
1244 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
1245 warning_at (loc, OPT_Woverflow,
1246 "complex integer overflow in expression");
1247 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
1248 warning_at (loc, OPT_Woverflow,
1249 "complex floating point overflow in expression");
1250 break;
1252 default:
1253 break;
1257 /* Warn about uses of logical || / && operator in a context where it
1258 is likely that the bitwise equivalent was intended by the
1259 programmer. We have seen an expression in which CODE is a binary
1260 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1261 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
1262 void
1263 warn_logical_operator (location_t location, enum tree_code code, tree type,
1264 enum tree_code code_left, tree op_left,
1265 enum tree_code ARG_UNUSED (code_right), tree op_right)
1267 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1268 int in0_p, in1_p, in_p;
1269 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1270 bool strict_overflow_p = false;
1272 if (code != TRUTH_ANDIF_EXPR
1273 && code != TRUTH_AND_EXPR
1274 && code != TRUTH_ORIF_EXPR
1275 && code != TRUTH_OR_EXPR)
1276 return;
1278 /* We don't want to warn if either operand comes from a macro
1279 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1280 see PR61534. */
1281 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1282 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1283 return;
1285 /* Warn if &&/|| are being used in a context where it is
1286 likely that the bitwise equivalent was intended by the
1287 programmer. That is, an expression such as op && MASK
1288 where op should not be any boolean expression, nor a
1289 constant, and mask seems to be a non-boolean integer constant. */
1290 if (TREE_CODE (op_right) == CONST_DECL)
1291 /* An enumerator counts as a constant. */
1292 op_right = DECL_INITIAL (op_right);
1293 if (!truth_value_p (code_left)
1294 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1295 && !CONSTANT_CLASS_P (op_left)
1296 && !TREE_NO_WARNING (op_left)
1297 && TREE_CODE (op_right) == INTEGER_CST
1298 && !integer_zerop (op_right)
1299 && !integer_onep (op_right))
1301 if (or_op)
1302 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1303 " applied to non-boolean constant");
1304 else
1305 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1306 " applied to non-boolean constant");
1307 TREE_NO_WARNING (op_left) = true;
1308 return;
1311 /* We do not warn for constants because they are typical of macro
1312 expansions that test for features. */
1313 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1314 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
1315 return;
1317 /* This warning only makes sense with logical operands. */
1318 if (!(truth_value_p (TREE_CODE (op_left))
1319 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1320 || !(truth_value_p (TREE_CODE (op_right))
1321 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1322 return;
1324 /* The range computations only work with scalars. */
1325 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1326 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1327 return;
1329 /* We first test whether either side separately is trivially true
1330 (with OR) or trivially false (with AND). If so, do not warn.
1331 This is a common idiom for testing ranges of data types in
1332 portable code. */
1333 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1334 if (!lhs)
1335 return;
1336 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
1337 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1339 /* If this is an OR operation, invert both sides; now, the result
1340 should be always false to get a warning. */
1341 if (or_op)
1342 in0_p = !in0_p;
1344 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1345 if (tem && integer_zerop (tem))
1346 return;
1348 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1349 if (!rhs)
1350 return;
1351 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
1352 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
1354 /* If this is an OR operation, invert both sides; now, the result
1355 should be always false to get a warning. */
1356 if (or_op)
1357 in1_p = !in1_p;
1359 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1360 if (tem && integer_zerop (tem))
1361 return;
1363 /* If both expressions have the same operand, if we can merge the
1364 ranges, ... */
1365 if (operand_equal_p (lhs, rhs, 0)
1366 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
1367 in1_p, low1, high1))
1369 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1370 /* ... and if the range test is always false, then warn. */
1371 if (tem && integer_zerop (tem))
1373 if (or_op)
1374 warning_at (location, OPT_Wlogical_op,
1375 "logical %<or%> of collectively exhaustive tests is "
1376 "always true");
1377 else
1378 warning_at (location, OPT_Wlogical_op,
1379 "logical %<and%> of mutually exclusive tests is "
1380 "always false");
1382 /* Or warn if the operands have exactly the same range, e.g.
1383 A > 0 && A > 0. */
1384 else if (tree_int_cst_equal (low0, low1)
1385 && tree_int_cst_equal (high0, high1))
1387 if (or_op)
1388 warning_at (location, OPT_Wlogical_op,
1389 "logical %<or%> of equal expressions");
1390 else
1391 warning_at (location, OPT_Wlogical_op,
1392 "logical %<and%> of equal expressions");
1397 /* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1398 with constant indices. */
1400 static tree
1401 find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1403 tree expr = *expr_p;
1405 if ((TREE_CODE (expr) == ARRAY_REF
1406 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1407 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1409 *(bool *) data = true;
1410 *walk_subtrees = 0;
1413 return NULL_TREE;
1416 /* Warn if a self-comparison always evaluates to true or false. LOC
1417 is the location of the comparison with code CODE, LHS and RHS are
1418 operands of the comparison. */
1420 void
1421 warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1423 if (TREE_CODE_CLASS (code) != tcc_comparison)
1424 return;
1426 /* Don't warn for various macro expansions. */
1427 if (from_macro_expansion_at (loc)
1428 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1429 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1430 return;
1432 /* We do not warn for constants because they are typical of macro
1433 expansions that test for features, sizeof, and similar. */
1434 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1435 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
1436 return;
1438 /* Don't warn for e.g.
1439 HOST_WIDE_INT n;
1441 if (n == (long) n) ...
1443 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1444 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1445 return;
1447 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1448 It could be a NaN, and NaN never compares equal to anything, even
1449 itself. */
1450 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1451 return;
1453 if (operand_equal_p (lhs, rhs, 0))
1455 /* Don't warn about array references with constant indices;
1456 these are likely to come from a macro. */
1457 bool found = false;
1458 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1459 &found);
1460 if (found)
1461 return;
1462 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1463 || code == GE_EXPR || code == UNLE_EXPR
1464 || code == UNGE_EXPR || code == UNEQ_EXPR);
1465 if (always_true)
1466 warning_at (loc, OPT_Wtautological_compare,
1467 "self-comparison always evaluates to true");
1468 else
1469 warning_at (loc, OPT_Wtautological_compare,
1470 "self-comparison always evaluates to false");
1474 /* Warn about logical not used on the left hand side operand of a comparison.
1475 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
1476 Do not warn if RHS is of a boolean type. */
1478 void
1479 warn_logical_not_parentheses (location_t location, enum tree_code code,
1480 tree rhs)
1482 if (TREE_CODE_CLASS (code) != tcc_comparison
1483 || TREE_TYPE (rhs) == NULL_TREE
1484 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
1485 return;
1487 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1488 !(x == 0) or !(y != 0). */
1489 if ((code == EQ_EXPR || code == NE_EXPR)
1490 && integer_zerop (rhs))
1491 return;
1493 warning_at (location, OPT_Wlogical_not_parentheses,
1494 "logical not is only applied to the left hand side of "
1495 "comparison");
1498 /* Warn if EXP contains any computations whose results are not used.
1499 Return true if a warning is printed; false otherwise. LOCUS is the
1500 (potential) location of the expression. */
1502 bool
1503 warn_if_unused_value (const_tree exp, location_t locus)
1505 restart:
1506 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1507 return false;
1509 /* Don't warn about void constructs. This includes casting to void,
1510 void function calls, and statement expressions with a final cast
1511 to void. */
1512 if (VOID_TYPE_P (TREE_TYPE (exp)))
1513 return false;
1515 if (EXPR_HAS_LOCATION (exp))
1516 locus = EXPR_LOCATION (exp);
1518 switch (TREE_CODE (exp))
1520 case PREINCREMENT_EXPR:
1521 case POSTINCREMENT_EXPR:
1522 case PREDECREMENT_EXPR:
1523 case POSTDECREMENT_EXPR:
1524 case MODIFY_EXPR:
1525 case INIT_EXPR:
1526 case TARGET_EXPR:
1527 case CALL_EXPR:
1528 case TRY_CATCH_EXPR:
1529 case WITH_CLEANUP_EXPR:
1530 case EXIT_EXPR:
1531 case VA_ARG_EXPR:
1532 return false;
1534 case BIND_EXPR:
1535 /* For a binding, warn if no side effect within it. */
1536 exp = BIND_EXPR_BODY (exp);
1537 goto restart;
1539 case SAVE_EXPR:
1540 case NON_LVALUE_EXPR:
1541 case NOP_EXPR:
1542 exp = TREE_OPERAND (exp, 0);
1543 goto restart;
1545 case TRUTH_ORIF_EXPR:
1546 case TRUTH_ANDIF_EXPR:
1547 /* In && or ||, warn if 2nd operand has no side effect. */
1548 exp = TREE_OPERAND (exp, 1);
1549 goto restart;
1551 case COMPOUND_EXPR:
1552 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1553 return true;
1554 /* Let people do `(foo (), 0)' without a warning. */
1555 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1556 return false;
1557 exp = TREE_OPERAND (exp, 1);
1558 goto restart;
1560 case COND_EXPR:
1561 /* If this is an expression with side effects, don't warn; this
1562 case commonly appears in macro expansions. */
1563 if (TREE_SIDE_EFFECTS (exp))
1564 return false;
1565 goto warn;
1567 case INDIRECT_REF:
1568 /* Don't warn about automatic dereferencing of references, since
1569 the user cannot control it. */
1570 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1572 exp = TREE_OPERAND (exp, 0);
1573 goto restart;
1575 /* Fall through. */
1577 default:
1578 /* Referencing a volatile value is a side effect, so don't warn. */
1579 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1580 && TREE_THIS_VOLATILE (exp))
1581 return false;
1583 /* If this is an expression which has no operands, there is no value
1584 to be unused. There are no such language-independent codes,
1585 but front ends may define such. */
1586 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1587 return false;
1589 warn:
1590 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1595 /* Print a warning about casts that might indicate violation
1596 of strict aliasing rules if -Wstrict-aliasing is used and
1597 strict aliasing mode is in effect. OTYPE is the original
1598 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
1600 bool
1601 strict_aliasing_warning (tree otype, tree type, tree expr)
1603 /* Strip pointer conversion chains and get to the correct original type. */
1604 STRIP_NOPS (expr);
1605 otype = TREE_TYPE (expr);
1607 if (!(flag_strict_aliasing
1608 && POINTER_TYPE_P (type)
1609 && POINTER_TYPE_P (otype)
1610 && !VOID_TYPE_P (TREE_TYPE (type)))
1611 /* If the type we are casting to is a ref-all pointer
1612 dereferencing it is always valid. */
1613 || TYPE_REF_CAN_ALIAS_ALL (type))
1614 return false;
1616 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
1617 && (DECL_P (TREE_OPERAND (expr, 0))
1618 || handled_component_p (TREE_OPERAND (expr, 0))))
1620 /* Casting the address of an object to non void pointer. Warn
1621 if the cast breaks type based aliasing. */
1622 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1624 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1625 "might break strict-aliasing rules");
1626 return true;
1628 else
1630 /* warn_strict_aliasing >= 3. This includes the default (3).
1631 Only warn if the cast is dereferenced immediately. */
1632 alias_set_type set1 =
1633 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
1634 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1636 if (set1 != set2 && set2 != 0
1637 && (set1 == 0
1638 || (!alias_set_subset_of (set2, set1)
1639 && !alias_sets_conflict_p (set1, set2))))
1641 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1642 "pointer will break strict-aliasing rules");
1643 return true;
1645 else if (warn_strict_aliasing == 2
1646 && !alias_sets_must_conflict_p (set1, set2))
1648 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1649 "pointer might break strict-aliasing rules");
1650 return true;
1654 else
1655 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1657 /* At this level, warn for any conversions, even if an address is
1658 not taken in the same statement. This will likely produce many
1659 false positives, but could be useful to pinpoint problems that
1660 are not revealed at higher levels. */
1661 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1662 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1663 if (!COMPLETE_TYPE_P (type)
1664 || !alias_sets_must_conflict_p (set1, set2))
1666 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1667 "pointer might break strict-aliasing rules");
1668 return true;
1672 return false;
1675 /* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1676 sizeof as last operand of certain builtins. */
1678 void
1679 sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
1680 vec<tree, va_gc> *params, tree *sizeof_arg,
1681 bool (*comp_types) (tree, tree))
1683 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
1684 bool strop = false, cmp = false;
1685 unsigned int idx = ~0;
1686 location_t loc;
1688 if (TREE_CODE (callee) != FUNCTION_DECL
1689 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
1690 || vec_safe_length (params) <= 1)
1691 return;
1693 switch (DECL_FUNCTION_CODE (callee))
1695 case BUILT_IN_STRNCMP:
1696 case BUILT_IN_STRNCASECMP:
1697 cmp = true;
1698 /* FALLTHRU */
1699 case BUILT_IN_STRNCPY:
1700 case BUILT_IN_STRNCPY_CHK:
1701 case BUILT_IN_STRNCAT:
1702 case BUILT_IN_STRNCAT_CHK:
1703 case BUILT_IN_STPNCPY:
1704 case BUILT_IN_STPNCPY_CHK:
1705 strop = true;
1706 /* FALLTHRU */
1707 case BUILT_IN_MEMCPY:
1708 case BUILT_IN_MEMCPY_CHK:
1709 case BUILT_IN_MEMMOVE:
1710 case BUILT_IN_MEMMOVE_CHK:
1711 if (params->length () < 3)
1712 return;
1713 src = (*params)[1];
1714 dest = (*params)[0];
1715 idx = 2;
1716 break;
1717 case BUILT_IN_BCOPY:
1718 if (params->length () < 3)
1719 return;
1720 src = (*params)[0];
1721 dest = (*params)[1];
1722 idx = 2;
1723 break;
1724 case BUILT_IN_MEMCMP:
1725 case BUILT_IN_BCMP:
1726 if (params->length () < 3)
1727 return;
1728 src = (*params)[1];
1729 dest = (*params)[0];
1730 idx = 2;
1731 cmp = true;
1732 break;
1733 case BUILT_IN_MEMSET:
1734 case BUILT_IN_MEMSET_CHK:
1735 if (params->length () < 3)
1736 return;
1737 dest = (*params)[0];
1738 idx = 2;
1739 break;
1740 case BUILT_IN_BZERO:
1741 dest = (*params)[0];
1742 idx = 1;
1743 break;
1744 case BUILT_IN_STRNDUP:
1745 src = (*params)[0];
1746 strop = true;
1747 idx = 1;
1748 break;
1749 case BUILT_IN_MEMCHR:
1750 if (params->length () < 3)
1751 return;
1752 src = (*params)[0];
1753 idx = 2;
1754 break;
1755 case BUILT_IN_SNPRINTF:
1756 case BUILT_IN_SNPRINTF_CHK:
1757 case BUILT_IN_VSNPRINTF:
1758 case BUILT_IN_VSNPRINTF_CHK:
1759 dest = (*params)[0];
1760 idx = 1;
1761 strop = true;
1762 break;
1763 default:
1764 break;
1767 if (idx >= 3)
1768 return;
1770 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1771 return;
1773 type = TYPE_P (sizeof_arg[idx])
1774 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1775 if (!POINTER_TYPE_P (type))
1776 return;
1778 if (dest
1779 && (tem = tree_strip_nop_conversions (dest))
1780 && POINTER_TYPE_P (TREE_TYPE (tem))
1781 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1782 return;
1784 if (src
1785 && (tem = tree_strip_nop_conversions (src))
1786 && POINTER_TYPE_P (TREE_TYPE (tem))
1787 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1788 return;
1790 loc = sizeof_arg_loc[idx];
1792 if (dest && !cmp)
1794 if (!TYPE_P (sizeof_arg[idx])
1795 && operand_equal_p (dest, sizeof_arg[idx], 0)
1796 && comp_types (TREE_TYPE (dest), type))
1798 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1799 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1800 "argument to %<sizeof%> in %qD call is the same "
1801 "expression as the destination; did you mean to "
1802 "remove the addressof?", callee);
1803 else if ((TYPE_PRECISION (TREE_TYPE (type))
1804 == TYPE_PRECISION (char_type_node))
1805 || strop)
1806 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1807 "argument to %<sizeof%> in %qD call is the same "
1808 "expression as the destination; did you mean to "
1809 "provide an explicit length?", callee);
1810 else
1811 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1812 "argument to %<sizeof%> in %qD call is the same "
1813 "expression as the destination; did you mean to "
1814 "dereference it?", callee);
1815 return;
1818 if (POINTER_TYPE_P (TREE_TYPE (dest))
1819 && !strop
1820 && comp_types (TREE_TYPE (dest), type)
1821 && !VOID_TYPE_P (TREE_TYPE (type)))
1823 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1824 "argument to %<sizeof%> in %qD call is the same "
1825 "pointer type %qT as the destination; expected %qT "
1826 "or an explicit length", callee, TREE_TYPE (dest),
1827 TREE_TYPE (TREE_TYPE (dest)));
1828 return;
1832 if (src && !cmp)
1834 if (!TYPE_P (sizeof_arg[idx])
1835 && operand_equal_p (src, sizeof_arg[idx], 0)
1836 && comp_types (TREE_TYPE (src), type))
1838 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1839 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1840 "argument to %<sizeof%> in %qD call is the same "
1841 "expression as the source; did you mean to "
1842 "remove the addressof?", callee);
1843 else if ((TYPE_PRECISION (TREE_TYPE (type))
1844 == TYPE_PRECISION (char_type_node))
1845 || strop)
1846 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1847 "argument to %<sizeof%> in %qD call is the same "
1848 "expression as the source; did you mean to "
1849 "provide an explicit length?", callee);
1850 else
1851 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1852 "argument to %<sizeof%> in %qD call is the same "
1853 "expression as the source; did you mean to "
1854 "dereference it?", callee);
1855 return;
1858 if (POINTER_TYPE_P (TREE_TYPE (src))
1859 && !strop
1860 && comp_types (TREE_TYPE (src), type)
1861 && !VOID_TYPE_P (TREE_TYPE (type)))
1863 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1864 "argument to %<sizeof%> in %qD call is the same "
1865 "pointer type %qT as the source; expected %qT "
1866 "or an explicit length", callee, TREE_TYPE (src),
1867 TREE_TYPE (TREE_TYPE (src)));
1868 return;
1872 if (dest)
1874 if (!TYPE_P (sizeof_arg[idx])
1875 && operand_equal_p (dest, sizeof_arg[idx], 0)
1876 && comp_types (TREE_TYPE (dest), type))
1878 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1879 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1880 "argument to %<sizeof%> in %qD call is the same "
1881 "expression as the first source; did you mean to "
1882 "remove the addressof?", callee);
1883 else if ((TYPE_PRECISION (TREE_TYPE (type))
1884 == TYPE_PRECISION (char_type_node))
1885 || strop)
1886 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1887 "argument to %<sizeof%> in %qD call is the same "
1888 "expression as the first source; did you mean to "
1889 "provide an explicit length?", callee);
1890 else
1891 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1892 "argument to %<sizeof%> in %qD call is the same "
1893 "expression as the first source; did you mean to "
1894 "dereference it?", callee);
1895 return;
1898 if (POINTER_TYPE_P (TREE_TYPE (dest))
1899 && !strop
1900 && comp_types (TREE_TYPE (dest), type)
1901 && !VOID_TYPE_P (TREE_TYPE (type)))
1903 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1904 "argument to %<sizeof%> in %qD call is the same "
1905 "pointer type %qT as the first source; expected %qT "
1906 "or an explicit length", callee, TREE_TYPE (dest),
1907 TREE_TYPE (TREE_TYPE (dest)));
1908 return;
1912 if (src)
1914 if (!TYPE_P (sizeof_arg[idx])
1915 && operand_equal_p (src, sizeof_arg[idx], 0)
1916 && comp_types (TREE_TYPE (src), type))
1918 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1919 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1920 "argument to %<sizeof%> in %qD call is the same "
1921 "expression as the second source; did you mean to "
1922 "remove the addressof?", callee);
1923 else if ((TYPE_PRECISION (TREE_TYPE (type))
1924 == TYPE_PRECISION (char_type_node))
1925 || strop)
1926 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1927 "argument to %<sizeof%> in %qD call is the same "
1928 "expression as the second source; did you mean to "
1929 "provide an explicit length?", callee);
1930 else
1931 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1932 "argument to %<sizeof%> in %qD call is the same "
1933 "expression as the second source; did you mean to "
1934 "dereference it?", callee);
1935 return;
1938 if (POINTER_TYPE_P (TREE_TYPE (src))
1939 && !strop
1940 && comp_types (TREE_TYPE (src), type)
1941 && !VOID_TYPE_P (TREE_TYPE (type)))
1943 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1944 "argument to %<sizeof%> in %qD call is the same "
1945 "pointer type %qT as the second source; expected %qT "
1946 "or an explicit length", callee, TREE_TYPE (src),
1947 TREE_TYPE (TREE_TYPE (src)));
1948 return;
1954 /* Warn for unlikely, improbable, or stupid DECL declarations
1955 of `main'. */
1957 void
1958 check_main_parameter_types (tree decl)
1960 function_args_iterator iter;
1961 tree type;
1962 int argct = 0;
1964 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1966 /* XXX void_type_node belies the abstraction. */
1967 if (type == void_type_node || type == error_mark_node )
1968 break;
1970 tree t = type;
1971 if (TYPE_ATOMIC (t))
1972 pedwarn (input_location, OPT_Wmain,
1973 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1974 type, decl);
1975 while (POINTER_TYPE_P (t))
1977 t = TREE_TYPE (t);
1978 if (TYPE_ATOMIC (t))
1979 pedwarn (input_location, OPT_Wmain,
1980 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1981 type, decl);
1984 ++argct;
1985 switch (argct)
1987 case 1:
1988 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1989 pedwarn (input_location, OPT_Wmain,
1990 "first argument of %q+D should be %<int%>", decl);
1991 break;
1993 case 2:
1994 if (TREE_CODE (type) != POINTER_TYPE
1995 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1996 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1997 != char_type_node))
1998 pedwarn (input_location, OPT_Wmain,
1999 "second argument of %q+D should be %<char **%>", decl);
2000 break;
2002 case 3:
2003 if (TREE_CODE (type) != POINTER_TYPE
2004 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2005 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2006 != char_type_node))
2007 pedwarn (input_location, OPT_Wmain,
2008 "third argument of %q+D should probably be "
2009 "%<char **%>", decl);
2010 break;
2014 /* It is intentional that this message does not mention the third
2015 argument because it's only mentioned in an appendix of the
2016 standard. */
2017 if (argct > 0 && (argct < 2 || argct > 3))
2018 pedwarn (input_location, OPT_Wmain,
2019 "%q+D takes only zero or two arguments", decl);
2021 if (stdarg_p (TREE_TYPE (decl)))
2022 pedwarn (input_location, OPT_Wmain,
2023 "%q+D declared as variadic function", decl);
2026 /* vector_targets_convertible_p is used for vector pointer types. The
2027 callers perform various checks that the qualifiers are satisfactory,
2028 while OTOH vector_targets_convertible_p ignores the number of elements
2029 in the vectors. That's fine with vector pointers as we can consider,
2030 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2031 and that does not require and conversion of the pointer values.
2032 In contrast, vector_types_convertible_p and
2033 vector_types_compatible_elements_p are used for vector value types. */
2034 /* True if pointers to distinct types T1 and T2 can be converted to
2035 each other without an explicit cast. Only returns true for opaque
2036 vector types. */
2037 bool
2038 vector_targets_convertible_p (const_tree t1, const_tree t2)
2040 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
2041 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2042 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2043 return true;
2045 return false;
2048 /* vector_types_convertible_p is used for vector value types.
2049 It could in principle call vector_targets_convertible_p as a subroutine,
2050 but then the check for vector type would be duplicated with its callers,
2051 and also the purpose of vector_targets_convertible_p would become
2052 muddled.
2053 Where vector_types_convertible_p returns true, a conversion might still be
2054 needed to make the types match.
2055 In contrast, vector_targets_convertible_p is used for vector pointer
2056 values, and vector_types_compatible_elements_p is used specifically
2057 in the context for binary operators, as a check if use is possible without
2058 conversion. */
2059 /* True if vector types T1 and T2 can be converted to each other
2060 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2061 can only be converted with -flax-vector-conversions yet that is not
2062 in effect, emit a note telling the user about that option if such
2063 a note has not previously been emitted. */
2064 bool
2065 vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
2067 static bool emitted_lax_note = false;
2068 bool convertible_lax;
2070 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
2071 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2072 return true;
2074 convertible_lax =
2075 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2076 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
2077 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
2078 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2079 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
2081 if (!convertible_lax || flag_lax_vector_conversions)
2082 return convertible_lax;
2084 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
2085 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2086 return true;
2088 if (emit_lax_note && !emitted_lax_note)
2090 emitted_lax_note = true;
2091 inform (input_location, "use -flax-vector-conversions to permit "
2092 "conversions between vectors with differing "
2093 "element types or numbers of subparts");
2096 return false;
2099 /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2100 and have vector types, V0 has the same type as V1, and the number of
2101 elements of V0, V1, MASK is the same.
2103 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2104 called with two arguments. In this case implementation passes the
2105 first argument twice in order to share the same tree code. This fact
2106 could enable the mask-values being twice the vector length. This is
2107 an implementation accident and this semantics is not guaranteed to
2108 the user. */
2109 tree
2110 c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2111 bool complain)
2113 tree ret;
2114 bool wrap = true;
2115 bool maybe_const = false;
2116 bool two_arguments = false;
2118 if (v1 == NULL_TREE)
2120 two_arguments = true;
2121 v1 = v0;
2124 if (v0 == error_mark_node || v1 == error_mark_node
2125 || mask == error_mark_node)
2126 return error_mark_node;
2128 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
2130 if (complain)
2131 error_at (loc, "__builtin_shuffle last argument must "
2132 "be an integer vector");
2133 return error_mark_node;
2136 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2137 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
2139 if (complain)
2140 error_at (loc, "__builtin_shuffle arguments must be vectors");
2141 return error_mark_node;
2144 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2146 if (complain)
2147 error_at (loc, "__builtin_shuffle argument vectors must be of "
2148 "the same type");
2149 return error_mark_node;
2152 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2153 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2154 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2155 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2157 if (complain)
2158 error_at (loc, "__builtin_shuffle number of elements of the "
2159 "argument vector(s) and the mask vector should "
2160 "be the same");
2161 return error_mark_node;
2164 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2165 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2167 if (complain)
2168 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2169 "must have the same size as inner type of the mask");
2170 return error_mark_node;
2173 if (!c_dialect_cxx ())
2175 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2176 v0 = c_fully_fold (v0, false, &maybe_const);
2177 wrap &= maybe_const;
2179 if (two_arguments)
2180 v1 = v0 = save_expr (v0);
2181 else
2183 v1 = c_fully_fold (v1, false, &maybe_const);
2184 wrap &= maybe_const;
2187 mask = c_fully_fold (mask, false, &maybe_const);
2188 wrap &= maybe_const;
2190 else if (two_arguments)
2191 v1 = v0 = save_expr (v0);
2193 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2195 if (!c_dialect_cxx () && !wrap)
2196 ret = c_wrap_maybe_const (ret, true);
2198 return ret;
2201 /* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2202 to integral type. */
2204 static tree
2205 c_common_get_narrower (tree op, int *unsignedp_ptr)
2207 op = get_narrower (op, unsignedp_ptr);
2209 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2210 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2212 /* C++0x scoped enumerations don't implicitly convert to integral
2213 type; if we stripped an explicit conversion to a larger type we
2214 need to replace it so common_type will still work. */
2215 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2216 TYPE_UNSIGNED (TREE_TYPE (op)));
2217 op = fold_convert (type, op);
2219 return op;
2222 /* This is a helper function of build_binary_op.
2224 For certain operations if both args were extended from the same
2225 smaller type, do the arithmetic in that type and then extend.
2227 BITWISE indicates a bitwise operation.
2228 For them, this optimization is safe only if
2229 both args are zero-extended or both are sign-extended.
2230 Otherwise, we might change the result.
2231 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2232 but calculated in (unsigned short) it would be (unsigned short)-1.
2234 tree
2235 shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2237 int unsigned0, unsigned1;
2238 tree arg0, arg1;
2239 int uns;
2240 tree type;
2242 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2243 excessive narrowing when we call get_narrower below. For
2244 example, suppose that OP0 is of unsigned int extended
2245 from signed char and that RESULT_TYPE is long long int.
2246 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2247 like
2249 (long long int) (unsigned int) signed_char
2251 which get_narrower would narrow down to
2253 (unsigned int) signed char
2255 If we do not cast OP0 first, get_narrower would return
2256 signed_char, which is inconsistent with the case of the
2257 explicit cast. */
2258 op0 = convert (result_type, op0);
2259 op1 = convert (result_type, op1);
2261 arg0 = c_common_get_narrower (op0, &unsigned0);
2262 arg1 = c_common_get_narrower (op1, &unsigned1);
2264 /* UNS is 1 if the operation to be done is an unsigned one. */
2265 uns = TYPE_UNSIGNED (result_type);
2267 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2268 but it *requires* conversion to FINAL_TYPE. */
2270 if ((TYPE_PRECISION (TREE_TYPE (op0))
2271 == TYPE_PRECISION (TREE_TYPE (arg0)))
2272 && TREE_TYPE (op0) != result_type)
2273 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2274 if ((TYPE_PRECISION (TREE_TYPE (op1))
2275 == TYPE_PRECISION (TREE_TYPE (arg1)))
2276 && TREE_TYPE (op1) != result_type)
2277 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2279 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2281 /* For bitwise operations, signedness of nominal type
2282 does not matter. Consider only how operands were extended. */
2283 if (bitwise)
2284 uns = unsigned0;
2286 /* Note that in all three cases below we refrain from optimizing
2287 an unsigned operation on sign-extended args.
2288 That would not be valid. */
2290 /* Both args variable: if both extended in same way
2291 from same width, do it in that width.
2292 Do it unsigned if args were zero-extended. */
2293 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2294 < TYPE_PRECISION (result_type))
2295 && (TYPE_PRECISION (TREE_TYPE (arg1))
2296 == TYPE_PRECISION (TREE_TYPE (arg0)))
2297 && unsigned0 == unsigned1
2298 && (unsigned0 || !uns))
2299 return c_common_signed_or_unsigned_type
2300 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2302 else if (TREE_CODE (arg0) == INTEGER_CST
2303 && (unsigned1 || !uns)
2304 && (TYPE_PRECISION (TREE_TYPE (arg1))
2305 < TYPE_PRECISION (result_type))
2306 && (type
2307 = c_common_signed_or_unsigned_type (unsigned1,
2308 TREE_TYPE (arg1)))
2309 && !POINTER_TYPE_P (type)
2310 && int_fits_type_p (arg0, type))
2311 return type;
2313 else if (TREE_CODE (arg1) == INTEGER_CST
2314 && (unsigned0 || !uns)
2315 && (TYPE_PRECISION (TREE_TYPE (arg0))
2316 < TYPE_PRECISION (result_type))
2317 && (type
2318 = c_common_signed_or_unsigned_type (unsigned0,
2319 TREE_TYPE (arg0)))
2320 && !POINTER_TYPE_P (type)
2321 && int_fits_type_p (arg1, type))
2322 return type;
2324 return result_type;
2327 /* Returns true iff any integer value of type FROM_TYPE can be represented as
2328 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2330 static bool
2331 int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2333 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2334 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2335 REAL_VALUE_TYPE real_low_bound =
2336 real_value_from_int_cst (0, type_low_bound);
2337 REAL_VALUE_TYPE real_high_bound =
2338 real_value_from_int_cst (0, type_high_bound);
2340 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2341 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2344 /* Checks if expression EXPR of complex/real/integer type cannot be converted
2345 to the complex/real/integer type TYPE. Function returns non-zero when:
2346 * EXPR is a constant which cannot be exactly converted to TYPE.
2347 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
2348 for EXPR type and TYPE being both integers or both real, or both
2349 complex.
2350 * EXPR is not a constant of complex type and TYPE is a real or
2351 an integer.
2352 * EXPR is not a constant of real type and TYPE is an integer.
2353 * EXPR is not a constant of integer type which cannot be
2354 exactly converted to real type.
2356 Function allows conversions between types of different signedness and
2357 can return SAFE_CONVERSION (zero) in that case. Function can produce
2358 signedness warnings if PRODUCE_WARNS is true.
2360 Function allows conversions from complex constants to non-complex types,
2361 provided that imaginary part is zero and real part can be safely converted
2362 to TYPE. */
2364 enum conversion_safety
2365 unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
2367 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2368 tree expr_type = TREE_TYPE (expr);
2369 loc = expansion_point_location_if_in_system_header (loc);
2371 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
2373 /* If type is complex, we are interested in compatibility with
2374 underlying type. */
2375 if (TREE_CODE (type) == COMPLEX_TYPE)
2376 type = TREE_TYPE (type);
2378 /* Warn for real constant that is not an exact integer converted
2379 to integer type. */
2380 if (TREE_CODE (expr_type) == REAL_TYPE
2381 && TREE_CODE (type) == INTEGER_TYPE)
2383 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2384 give_warning = UNSAFE_REAL;
2386 /* Warn for an integer constant that does not fit into integer type. */
2387 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2388 && TREE_CODE (type) == INTEGER_TYPE
2389 && !int_fits_type_p (expr, type))
2391 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
2392 && tree_int_cst_sgn (expr) < 0)
2394 if (produce_warns)
2395 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2396 " implicitly converted to unsigned type");
2398 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2400 if (produce_warns)
2401 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2402 " constant value to negative integer");
2404 else
2405 give_warning = UNSAFE_OTHER;
2407 else if (TREE_CODE (type) == REAL_TYPE)
2409 /* Warn for an integer constant that does not fit into real type. */
2410 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2412 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2413 if (!exact_real_truncate (TYPE_MODE (type), &a))
2414 give_warning = UNSAFE_REAL;
2416 /* Warn for a real constant that does not fit into a smaller
2417 real type. */
2418 else if (TREE_CODE (expr_type) == REAL_TYPE
2419 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2421 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2422 if (!exact_real_truncate (TYPE_MODE (type), &a))
2423 give_warning = UNSAFE_REAL;
2428 else if (TREE_CODE (expr) == COMPLEX_CST)
2430 tree imag_part = TREE_IMAGPART (expr);
2431 /* Conversion from complex constant with zero imaginary part,
2432 perform check for conversion of real part. */
2433 if ((TREE_CODE (imag_part) == REAL_CST
2434 && real_zerop (imag_part))
2435 || (TREE_CODE (imag_part) == INTEGER_CST
2436 && integer_zerop (imag_part)))
2437 /* Note: in this branch we use recursive call to unsafe_conversion_p
2438 with different type of EXPR, but it is still safe, because when EXPR
2439 is a constant, it's type is not used in text of generated warnings
2440 (otherwise they could sound misleading). */
2441 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2442 produce_warns);
2443 /* Conversion from complex constant with non-zero imaginary part. */
2444 else
2446 /* Conversion to complex type.
2447 Perform checks for both real and imaginary parts. */
2448 if (TREE_CODE (type) == COMPLEX_TYPE)
2450 /* Unfortunately, produce_warns must be false in two subsequent
2451 calls of unsafe_conversion_p, because otherwise we could
2452 produce strange "double" warnings, if both real and imaginary
2453 parts have conversion problems related to signedness.
2455 For example:
2456 int32_t _Complex a = 0x80000000 + 0x80000000i;
2458 Possible solution: add a separate function for checking
2459 constants and combine result of two calls appropriately. */
2460 enum conversion_safety re_safety =
2461 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2462 enum conversion_safety im_safety =
2463 unsafe_conversion_p (loc, type, imag_part, false);
2465 /* Merge the results into appropriate single warning. */
2467 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2468 if (re_safety == im_safety)
2469 give_warning = re_safety;
2470 else if (!re_safety && im_safety)
2471 give_warning = im_safety;
2472 else if (re_safety && !im_safety)
2473 give_warning = re_safety;
2474 else
2475 give_warning = UNSAFE_OTHER;
2477 /* Warn about conversion from complex to real or integer type. */
2478 else
2479 give_warning = UNSAFE_IMAGINARY;
2483 /* Checks for remaining case: EXPR is not constant. */
2484 else
2486 /* Warn for real types converted to integer types. */
2487 if (TREE_CODE (expr_type) == REAL_TYPE
2488 && TREE_CODE (type) == INTEGER_TYPE)
2489 give_warning = UNSAFE_REAL;
2491 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2492 && TREE_CODE (type) == INTEGER_TYPE)
2494 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
2495 expr = get_unwidened (expr, 0);
2496 expr_type = TREE_TYPE (expr);
2498 /* Don't warn for short y; short x = ((int)y & 0xff); */
2499 if (TREE_CODE (expr) == BIT_AND_EXPR
2500 || TREE_CODE (expr) == BIT_IOR_EXPR
2501 || TREE_CODE (expr) == BIT_XOR_EXPR)
2503 /* If both args were extended from a shortest type,
2504 use that type if that is safe. */
2505 expr_type = shorten_binary_op (expr_type,
2506 TREE_OPERAND (expr, 0),
2507 TREE_OPERAND (expr, 1),
2508 /* bitwise */1);
2510 if (TREE_CODE (expr) == BIT_AND_EXPR)
2512 tree op0 = TREE_OPERAND (expr, 0);
2513 tree op1 = TREE_OPERAND (expr, 1);
2514 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2515 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2517 /* If one of the operands is a non-negative constant
2518 that fits in the target type, then the type of the
2519 other operand does not matter. */
2520 if ((TREE_CODE (op0) == INTEGER_CST
2521 && int_fits_type_p (op0, c_common_signed_type (type))
2522 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2523 || (TREE_CODE (op1) == INTEGER_CST
2524 && int_fits_type_p (op1, c_common_signed_type (type))
2525 && int_fits_type_p (op1,
2526 c_common_unsigned_type (type))))
2527 return SAFE_CONVERSION;
2528 /* If constant is unsigned and fits in the target
2529 type, then the result will also fit. */
2530 else if ((TREE_CODE (op0) == INTEGER_CST
2531 && unsigned0
2532 && int_fits_type_p (op0, type))
2533 || (TREE_CODE (op1) == INTEGER_CST
2534 && unsigned1
2535 && int_fits_type_p (op1, type)))
2536 return SAFE_CONVERSION;
2539 /* Warn for integer types converted to smaller integer types. */
2540 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2541 give_warning = UNSAFE_OTHER;
2543 /* When they are the same width but different signedness,
2544 then the value may change. */
2545 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2546 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
2547 /* Even when converted to a bigger type, if the type is
2548 unsigned but expr is signed, then negative values
2549 will be changed. */
2550 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2551 && produce_warns)
2552 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2553 "may change the sign of the result",
2554 type, expr_type);
2557 /* Warn for integer types converted to real types if and only if
2558 all the range of values of the integer type cannot be
2559 represented by the real type. */
2560 else if (TREE_CODE (expr_type) == INTEGER_TYPE
2561 && TREE_CODE (type) == REAL_TYPE)
2563 /* Don't warn about char y = 0xff; float x = (int) y; */
2564 expr = get_unwidened (expr, 0);
2565 expr_type = TREE_TYPE (expr);
2567 if (!int_safely_convertible_to_real_p (expr_type, type))
2568 give_warning = UNSAFE_OTHER;
2571 /* Warn for real types converted to smaller real types. */
2572 else if (TREE_CODE (expr_type) == REAL_TYPE
2573 && TREE_CODE (type) == REAL_TYPE
2574 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2575 give_warning = UNSAFE_REAL;
2577 /* Check conversion between two complex types. */
2578 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2579 && TREE_CODE (type) == COMPLEX_TYPE)
2581 /* Extract underlying types (i.e., type of real and imaginary
2582 parts) of expr_type and type. */
2583 tree from_type = TREE_TYPE (expr_type);
2584 tree to_type = TREE_TYPE (type);
2586 /* Warn for real types converted to integer types. */
2587 if (TREE_CODE (from_type) == REAL_TYPE
2588 && TREE_CODE (to_type) == INTEGER_TYPE)
2589 give_warning = UNSAFE_REAL;
2591 /* Warn for real types converted to smaller real types. */
2592 else if (TREE_CODE (from_type) == REAL_TYPE
2593 && TREE_CODE (to_type) == REAL_TYPE
2594 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2595 give_warning = UNSAFE_REAL;
2597 /* Check conversion for complex integer types. Here implementation
2598 is simpler than for real-domain integers because it does not
2599 involve sophisticated cases, such as bitmasks, casts, etc. */
2600 else if (TREE_CODE (from_type) == INTEGER_TYPE
2601 && TREE_CODE (to_type) == INTEGER_TYPE)
2603 /* Warn for integer types converted to smaller integer types. */
2604 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2605 give_warning = UNSAFE_OTHER;
2607 /* Check for different signedness, see case for real-domain
2608 integers (above) for a more detailed comment. */
2609 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2610 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2611 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2612 && produce_warns)
2613 warning_at (loc, OPT_Wsign_conversion,
2614 "conversion to %qT from %qT "
2615 "may change the sign of the result",
2616 type, expr_type);
2618 else if (TREE_CODE (from_type) == INTEGER_TYPE
2619 && TREE_CODE (to_type) == REAL_TYPE
2620 && !int_safely_convertible_to_real_p (from_type, to_type))
2621 give_warning = UNSAFE_OTHER;
2624 /* Warn for complex types converted to real or integer types. */
2625 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2626 && TREE_CODE (type) != COMPLEX_TYPE)
2627 give_warning = UNSAFE_IMAGINARY;
2630 return give_warning;
2633 /* Warns if the conversion of EXPR to TYPE may alter a value.
2634 This is a helper function for warnings_for_convert_and_check. */
2636 static void
2637 conversion_warning (location_t loc, tree type, tree expr)
2639 tree expr_type = TREE_TYPE (expr);
2640 enum conversion_safety conversion_kind;
2642 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2643 return;
2645 /* This may happen, because for LHS op= RHS we preevaluate
2646 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2647 means we could no longer see the code of the EXPR. */
2648 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2649 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2650 if (TREE_CODE (expr) == SAVE_EXPR)
2651 expr = TREE_OPERAND (expr, 0);
2653 switch (TREE_CODE (expr))
2655 case EQ_EXPR:
2656 case NE_EXPR:
2657 case LE_EXPR:
2658 case GE_EXPR:
2659 case LT_EXPR:
2660 case GT_EXPR:
2661 case TRUTH_ANDIF_EXPR:
2662 case TRUTH_ORIF_EXPR:
2663 case TRUTH_AND_EXPR:
2664 case TRUTH_OR_EXPR:
2665 case TRUTH_XOR_EXPR:
2666 case TRUTH_NOT_EXPR:
2667 /* Conversion from boolean to a signed:1 bit-field (which only
2668 can hold the values 0 and -1) doesn't lose information - but
2669 it does change the value. */
2670 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2671 warning_at (loc, OPT_Wconversion,
2672 "conversion to %qT from boolean expression", type);
2673 return;
2675 case REAL_CST:
2676 case INTEGER_CST:
2677 case COMPLEX_CST:
2678 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2679 if (conversion_kind == UNSAFE_REAL)
2680 warning_at (loc, OPT_Wfloat_conversion,
2681 "conversion to %qT alters %qT constant value",
2682 type, expr_type);
2683 else if (conversion_kind)
2684 warning_at (loc, OPT_Wconversion,
2685 "conversion to %qT alters %qT constant value",
2686 type, expr_type);
2687 return;
2689 case COND_EXPR:
2691 /* In case of COND_EXPR, we do not care about the type of
2692 COND_EXPR, only about the conversion of each operand. */
2693 tree op1 = TREE_OPERAND (expr, 1);
2694 tree op2 = TREE_OPERAND (expr, 2);
2696 conversion_warning (loc, type, op1);
2697 conversion_warning (loc, type, op2);
2698 return;
2701 default: /* 'expr' is not a constant. */
2702 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
2703 if (conversion_kind == UNSAFE_REAL)
2704 warning_at (loc, OPT_Wfloat_conversion,
2705 "conversion to %qT from %qT may alter its value",
2706 type, expr_type);
2707 else if (conversion_kind == UNSAFE_IMAGINARY)
2708 warning_at (loc, OPT_Wconversion,
2709 "conversion to %qT from %qT discards imaginary component",
2710 type, expr_type);
2711 else if (conversion_kind)
2712 warning_at (loc, OPT_Wconversion,
2713 "conversion to %qT from %qT may alter its value",
2714 type, expr_type);
2718 /* Produce warnings after a conversion. RESULT is the result of
2719 converting EXPR to TYPE. This is a helper function for
2720 convert_and_check and cp_convert_and_check. */
2722 void
2723 warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2724 tree result)
2726 loc = expansion_point_location_if_in_system_header (loc);
2728 if (TREE_CODE (expr) == INTEGER_CST
2729 && (TREE_CODE (type) == INTEGER_TYPE
2730 || TREE_CODE (type) == ENUMERAL_TYPE)
2731 && !int_fits_type_p (expr, type))
2733 /* Do not diagnose overflow in a constant expression merely
2734 because a conversion overflowed. */
2735 if (TREE_OVERFLOW (result))
2736 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2738 if (TYPE_UNSIGNED (type))
2740 /* This detects cases like converting -129 or 256 to
2741 unsigned char. */
2742 if (!int_fits_type_p (expr, c_common_signed_type (type)))
2743 warning_at (loc, OPT_Woverflow,
2744 "large integer implicitly truncated to unsigned type");
2745 else
2746 conversion_warning (loc, type, expr);
2748 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
2749 warning_at (loc, OPT_Woverflow,
2750 "overflow in implicit constant conversion");
2751 /* No warning for converting 0x80000000 to int. */
2752 else if (pedantic
2753 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2754 || TYPE_PRECISION (TREE_TYPE (expr))
2755 != TYPE_PRECISION (type)))
2756 warning_at (loc, OPT_Woverflow,
2757 "overflow in implicit constant conversion");
2759 else
2760 conversion_warning (loc, type, expr);
2762 else if ((TREE_CODE (result) == INTEGER_CST
2763 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
2764 warning_at (loc, OPT_Woverflow,
2765 "overflow in implicit constant conversion");
2766 else
2767 conversion_warning (loc, type, expr);
2771 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2772 Invoke this function on every expression that is converted implicitly,
2773 i.e. because of language rules and not because of an explicit cast. */
2775 tree
2776 convert_and_check (location_t loc, tree type, tree expr)
2778 tree result;
2779 tree expr_for_warning;
2781 /* Convert from a value with possible excess precision rather than
2782 via the semantic type, but do not warn about values not fitting
2783 exactly in the semantic type. */
2784 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2786 tree orig_type = TREE_TYPE (expr);
2787 expr = TREE_OPERAND (expr, 0);
2788 expr_for_warning = convert (orig_type, expr);
2789 if (orig_type == type)
2790 return expr_for_warning;
2792 else
2793 expr_for_warning = expr;
2795 if (TREE_TYPE (expr) == type)
2796 return expr;
2798 result = convert (type, expr);
2800 if (c_inhibit_evaluation_warnings == 0
2801 && !TREE_OVERFLOW_P (expr)
2802 && result != error_mark_node)
2803 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
2805 return result;
2808 /* A node in a list that describes references to variables (EXPR), which are
2809 either read accesses if WRITER is zero, or write accesses, in which case
2810 WRITER is the parent of EXPR. */
2811 struct tlist
2813 struct tlist *next;
2814 tree expr, writer;
2817 /* Used to implement a cache the results of a call to verify_tree. We only
2818 use this for SAVE_EXPRs. */
2819 struct tlist_cache
2821 struct tlist_cache *next;
2822 struct tlist *cache_before_sp;
2823 struct tlist *cache_after_sp;
2824 tree expr;
2827 /* Obstack to use when allocating tlist structures, and corresponding
2828 firstobj. */
2829 static struct obstack tlist_obstack;
2830 static char *tlist_firstobj = 0;
2832 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
2833 warnings. */
2834 static struct tlist *warned_ids;
2835 /* SAVE_EXPRs need special treatment. We process them only once and then
2836 cache the results. */
2837 static struct tlist_cache *save_expr_cache;
2839 static void add_tlist (struct tlist **, struct tlist *, tree, int);
2840 static void merge_tlist (struct tlist **, struct tlist *, int);
2841 static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2842 static int warning_candidate_p (tree);
2843 static bool candidate_equal_p (const_tree, const_tree);
2844 static void warn_for_collisions (struct tlist *);
2845 static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2846 static struct tlist *new_tlist (struct tlist *, tree, tree);
2848 /* Create a new struct tlist and fill in its fields. */
2849 static struct tlist *
2850 new_tlist (struct tlist *next, tree t, tree writer)
2852 struct tlist *l;
2853 l = XOBNEW (&tlist_obstack, struct tlist);
2854 l->next = next;
2855 l->expr = t;
2856 l->writer = writer;
2857 return l;
2860 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2861 is nonnull, we ignore any node we find which has a writer equal to it. */
2863 static void
2864 add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
2866 while (add)
2868 struct tlist *next = add->next;
2869 if (!copy)
2870 add->next = *to;
2871 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
2872 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2873 add = next;
2877 /* Merge the nodes of ADD into TO. This merging process is done so that for
2878 each variable that already exists in TO, no new node is added; however if
2879 there is a write access recorded in ADD, and an occurrence on TO is only
2880 a read access, then the occurrence in TO will be modified to record the
2881 write. */
2883 static void
2884 merge_tlist (struct tlist **to, struct tlist *add, int copy)
2886 struct tlist **end = to;
2888 while (*end)
2889 end = &(*end)->next;
2891 while (add)
2893 int found = 0;
2894 struct tlist *tmp2;
2895 struct tlist *next = add->next;
2897 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
2898 if (candidate_equal_p (tmp2->expr, add->expr))
2900 found = 1;
2901 if (!tmp2->writer)
2902 tmp2->writer = add->writer;
2904 if (!found)
2906 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
2907 end = &(*end)->next;
2908 *end = 0;
2910 add = next;
2914 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2915 references in list LIST conflict with it, excluding reads if ONLY writers
2916 is nonzero. */
2918 static void
2919 warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2920 int only_writes)
2922 struct tlist *tmp;
2924 /* Avoid duplicate warnings. */
2925 for (tmp = warned_ids; tmp; tmp = tmp->next)
2926 if (candidate_equal_p (tmp->expr, written))
2927 return;
2929 while (list)
2931 if (candidate_equal_p (list->expr, written)
2932 && !candidate_equal_p (list->writer, writer)
2933 && (!only_writes || list->writer))
2935 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
2936 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
2937 OPT_Wsequence_point, "operation on %qE may be undefined",
2938 list->expr);
2940 list = list->next;
2944 /* Given a list LIST of references to variables, find whether any of these
2945 can cause conflicts due to missing sequence points. */
2947 static void
2948 warn_for_collisions (struct tlist *list)
2950 struct tlist *tmp;
2952 for (tmp = list; tmp; tmp = tmp->next)
2954 if (tmp->writer)
2955 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2959 /* Return nonzero if X is a tree that can be verified by the sequence point
2960 warnings. */
2961 static int
2962 warning_candidate_p (tree x)
2964 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2965 return 0;
2967 if (TREE_CODE (x) == BLOCK)
2968 return 0;
2970 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
2971 (lvalue_p) crash on TRY/CATCH. */
2972 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2973 return 0;
2975 if (!lvalue_p (x))
2976 return 0;
2978 /* No point to track non-const calls, they will never satisfy
2979 operand_equal_p. */
2980 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2981 return 0;
2983 if (TREE_CODE (x) == STRING_CST)
2984 return 0;
2986 return 1;
2989 /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2990 static bool
2991 candidate_equal_p (const_tree x, const_tree y)
2993 return (x == y) || (x && y && operand_equal_p (x, y, 0));
2996 /* Walk the tree X, and record accesses to variables. If X is written by the
2997 parent tree, WRITER is the parent.
2998 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2999 expression or its only operand forces a sequence point, then everything up
3000 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3001 in PNO_SP.
3002 Once we return, we will have emitted warnings if any subexpression before
3003 such a sequence point could be undefined. On a higher level, however, the
3004 sequence point may not be relevant, and we'll merge the two lists.
3006 Example: (b++, a) + b;
3007 The call that processes the COMPOUND_EXPR will store the increment of B
3008 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3009 processes the PLUS_EXPR will need to merge the two lists so that
3010 eventually, all accesses end up on the same list (and we'll warn about the
3011 unordered subexpressions b++ and b.
3013 A note on merging. If we modify the former example so that our expression
3014 becomes
3015 (b++, b) + a
3016 care must be taken not simply to add all three expressions into the final
3017 PNO_SP list. The function merge_tlist takes care of that by merging the
3018 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3019 way, so that no more than one access to B is recorded. */
3021 static void
3022 verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3023 tree writer)
3025 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3026 enum tree_code code;
3027 enum tree_code_class cl;
3029 /* X may be NULL if it is the operand of an empty statement expression
3030 ({ }). */
3031 if (x == NULL)
3032 return;
3034 restart:
3035 code = TREE_CODE (x);
3036 cl = TREE_CODE_CLASS (code);
3038 if (warning_candidate_p (x))
3039 *pno_sp = new_tlist (*pno_sp, x, writer);
3041 switch (code)
3043 case CONSTRUCTOR:
3044 case SIZEOF_EXPR:
3045 return;
3047 case COMPOUND_EXPR:
3048 case TRUTH_ANDIF_EXPR:
3049 case TRUTH_ORIF_EXPR:
3050 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
3051 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3052 warn_for_collisions (tmp_nosp);
3053 merge_tlist (pbefore_sp, tmp_before, 0);
3054 merge_tlist (pbefore_sp, tmp_nosp, 0);
3055 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
3056 warn_for_collisions (tmp_list2);
3057 merge_tlist (pbefore_sp, tmp_list3, 0);
3058 merge_tlist (pno_sp, tmp_list2, 0);
3059 return;
3061 case COND_EXPR:
3062 tmp_before = tmp_list2 = 0;
3063 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3064 warn_for_collisions (tmp_list2);
3065 merge_tlist (pbefore_sp, tmp_before, 0);
3066 merge_tlist (pbefore_sp, tmp_list2, 0);
3068 tmp_list3 = tmp_nosp = 0;
3069 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3070 warn_for_collisions (tmp_nosp);
3071 merge_tlist (pbefore_sp, tmp_list3, 0);
3073 tmp_list3 = tmp_list2 = 0;
3074 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3075 warn_for_collisions (tmp_list2);
3076 merge_tlist (pbefore_sp, tmp_list3, 0);
3077 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3078 two first, to avoid warning for (a ? b++ : b++). */
3079 merge_tlist (&tmp_nosp, tmp_list2, 0);
3080 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3081 return;
3083 case PREDECREMENT_EXPR:
3084 case PREINCREMENT_EXPR:
3085 case POSTDECREMENT_EXPR:
3086 case POSTINCREMENT_EXPR:
3087 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3088 return;
3090 case MODIFY_EXPR:
3091 tmp_before = tmp_nosp = tmp_list3 = 0;
3092 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3093 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3094 /* Expressions inside the LHS are not ordered wrt. the sequence points
3095 in the RHS. Example:
3096 *a = (a++, 2)
3097 Despite the fact that the modification of "a" is in the before_sp
3098 list (tmp_before), it conflicts with the use of "a" in the LHS.
3099 We can handle this by adding the contents of tmp_list3
3100 to those of tmp_before, and redoing the collision warnings for that
3101 list. */
3102 add_tlist (&tmp_before, tmp_list3, x, 1);
3103 warn_for_collisions (tmp_before);
3104 /* Exclude the LHS itself here; we first have to merge it into the
3105 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3106 didn't exclude the LHS, we'd get it twice, once as a read and once
3107 as a write. */
3108 add_tlist (pno_sp, tmp_list3, x, 0);
3109 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3111 merge_tlist (pbefore_sp, tmp_before, 0);
3112 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3113 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3114 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3115 return;
3117 case CALL_EXPR:
3118 /* We need to warn about conflicts among arguments and conflicts between
3119 args and the function address. Side effects of the function address,
3120 however, are not ordered by the sequence point of the call. */
3122 call_expr_arg_iterator iter;
3123 tree arg;
3124 tmp_before = tmp_nosp = 0;
3125 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3126 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3128 tmp_list2 = tmp_list3 = 0;
3129 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3130 merge_tlist (&tmp_list3, tmp_list2, 0);
3131 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3133 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3134 warn_for_collisions (tmp_before);
3135 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3136 return;
3139 case TREE_LIST:
3140 /* Scan all the list, e.g. indices of multi dimensional array. */
3141 while (x)
3143 tmp_before = tmp_nosp = 0;
3144 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3145 merge_tlist (&tmp_nosp, tmp_before, 0);
3146 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3147 x = TREE_CHAIN (x);
3149 return;
3151 case SAVE_EXPR:
3153 struct tlist_cache *t;
3154 for (t = save_expr_cache; t; t = t->next)
3155 if (candidate_equal_p (t->expr, x))
3156 break;
3158 if (!t)
3160 t = XOBNEW (&tlist_obstack, struct tlist_cache);
3161 t->next = save_expr_cache;
3162 t->expr = x;
3163 save_expr_cache = t;
3165 tmp_before = tmp_nosp = 0;
3166 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3167 warn_for_collisions (tmp_nosp);
3169 tmp_list3 = 0;
3170 merge_tlist (&tmp_list3, tmp_nosp, 0);
3171 t->cache_before_sp = tmp_before;
3172 t->cache_after_sp = tmp_list3;
3174 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3175 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3176 return;
3179 case ADDR_EXPR:
3180 x = TREE_OPERAND (x, 0);
3181 if (DECL_P (x))
3182 return;
3183 writer = 0;
3184 goto restart;
3186 default:
3187 /* For other expressions, simply recurse on their operands.
3188 Manual tail recursion for unary expressions.
3189 Other non-expressions need not be processed. */
3190 if (cl == tcc_unary)
3192 x = TREE_OPERAND (x, 0);
3193 writer = 0;
3194 goto restart;
3196 else if (IS_EXPR_CODE_CLASS (cl))
3198 int lp;
3199 int max = TREE_OPERAND_LENGTH (x);
3200 for (lp = 0; lp < max; lp++)
3202 tmp_before = tmp_nosp = 0;
3203 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3204 merge_tlist (&tmp_nosp, tmp_before, 0);
3205 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3208 return;
3212 /* Try to warn for undefined behavior in EXPR due to missing sequence
3213 points. */
3215 DEBUG_FUNCTION void
3216 verify_sequence_points (tree expr)
3218 struct tlist *before_sp = 0, *after_sp = 0;
3220 warned_ids = 0;
3221 save_expr_cache = 0;
3222 if (tlist_firstobj == 0)
3224 gcc_obstack_init (&tlist_obstack);
3225 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
3228 verify_tree (expr, &before_sp, &after_sp, 0);
3229 warn_for_collisions (after_sp);
3230 obstack_free (&tlist_obstack, tlist_firstobj);
3233 /* Validate the expression after `case' and apply default promotions. */
3235 static tree
3236 check_case_value (location_t loc, tree value)
3238 if (value == NULL_TREE)
3239 return value;
3241 if (TREE_CODE (value) == INTEGER_CST)
3242 /* Promote char or short to int. */
3243 value = perform_integral_promotions (value);
3244 else if (value != error_mark_node)
3246 error_at (loc, "case label does not reduce to an integer constant");
3247 value = error_mark_node;
3250 constant_expression_warning (value);
3252 return value;
3255 /* See if the case values LOW and HIGH are in the range of the original
3256 type (i.e. before the default conversion to int) of the switch testing
3257 expression.
3258 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
3259 the type before promoting it. CASE_LOW_P is a pointer to the lower
3260 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3261 if the case is not a case range.
3262 The caller has to make sure that we are not called with NULL for
3263 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3264 was a case value that doesn't fit into the range of the ORIG_TYPE.
3265 Returns true if the case label is in range of ORIG_TYPE (saturated or
3266 untouched) or false if the label is out of range. */
3268 static bool
3269 check_case_bounds (location_t loc, tree type, tree orig_type,
3270 tree *case_low_p, tree *case_high_p,
3271 bool *outside_range_p)
3273 tree min_value, max_value;
3274 tree case_low = *case_low_p;
3275 tree case_high = case_high_p ? *case_high_p : case_low;
3277 /* If there was a problem with the original type, do nothing. */
3278 if (orig_type == error_mark_node)
3279 return true;
3281 min_value = TYPE_MIN_VALUE (orig_type);
3282 max_value = TYPE_MAX_VALUE (orig_type);
3284 /* We'll really need integer constants here. */
3285 case_low = fold (case_low);
3286 case_high = fold (case_high);
3288 /* Case label is less than minimum for type. */
3289 if (tree_int_cst_compare (case_low, min_value) < 0
3290 && tree_int_cst_compare (case_high, min_value) < 0)
3292 warning_at (loc, 0, "case label value is less than minimum value "
3293 "for type");
3294 *outside_range_p = true;
3295 return false;
3298 /* Case value is greater than maximum for type. */
3299 if (tree_int_cst_compare (case_low, max_value) > 0
3300 && tree_int_cst_compare (case_high, max_value) > 0)
3302 warning_at (loc, 0, "case label value exceeds maximum value for type");
3303 *outside_range_p = true;
3304 return false;
3307 /* Saturate lower case label value to minimum. */
3308 if (tree_int_cst_compare (case_high, min_value) >= 0
3309 && tree_int_cst_compare (case_low, min_value) < 0)
3311 warning_at (loc, 0, "lower value in case label range"
3312 " less than minimum value for type");
3313 *outside_range_p = true;
3314 case_low = min_value;
3317 /* Saturate upper case label value to maximum. */
3318 if (tree_int_cst_compare (case_low, max_value) <= 0
3319 && tree_int_cst_compare (case_high, max_value) > 0)
3321 warning_at (loc, 0, "upper value in case label range"
3322 " exceeds maximum value for type");
3323 *outside_range_p = true;
3324 case_high = max_value;
3327 if (*case_low_p != case_low)
3328 *case_low_p = convert (type, case_low);
3329 if (case_high_p && *case_high_p != case_high)
3330 *case_high_p = convert (type, case_high);
3332 return true;
3335 /* Return an integer type with BITS bits of precision,
3336 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3338 tree
3339 c_common_type_for_size (unsigned int bits, int unsignedp)
3341 int i;
3343 if (bits == TYPE_PRECISION (integer_type_node))
3344 return unsignedp ? unsigned_type_node : integer_type_node;
3346 if (bits == TYPE_PRECISION (signed_char_type_node))
3347 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3349 if (bits == TYPE_PRECISION (short_integer_type_node))
3350 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3352 if (bits == TYPE_PRECISION (long_integer_type_node))
3353 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3355 if (bits == TYPE_PRECISION (long_long_integer_type_node))
3356 return (unsignedp ? long_long_unsigned_type_node
3357 : long_long_integer_type_node);
3359 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3360 if (int_n_enabled_p[i]
3361 && bits == int_n_data[i].bitsize)
3362 return (unsignedp ? int_n_trees[i].unsigned_type
3363 : int_n_trees[i].signed_type);
3365 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3366 return (unsignedp ? widest_unsigned_literal_type_node
3367 : widest_integer_literal_type_node);
3369 if (bits <= TYPE_PRECISION (intQI_type_node))
3370 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3372 if (bits <= TYPE_PRECISION (intHI_type_node))
3373 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3375 if (bits <= TYPE_PRECISION (intSI_type_node))
3376 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3378 if (bits <= TYPE_PRECISION (intDI_type_node))
3379 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3381 return 0;
3384 /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3385 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3386 and saturating if SATP is nonzero, otherwise not saturating. */
3388 tree
3389 c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3390 int unsignedp, int satp)
3392 machine_mode mode;
3393 if (ibit == 0)
3394 mode = unsignedp ? UQQmode : QQmode;
3395 else
3396 mode = unsignedp ? UHAmode : HAmode;
3398 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3399 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3400 break;
3402 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3404 sorry ("GCC cannot support operators with integer types and "
3405 "fixed-point types that have too many integral and "
3406 "fractional bits together");
3407 return 0;
3410 return c_common_type_for_mode (mode, satp);
3413 /* Used for communication between c_common_type_for_mode and
3414 c_register_builtin_type. */
3415 tree registered_builtin_types;
3417 /* Return a data type that has machine mode MODE.
3418 If the mode is an integer,
3419 then UNSIGNEDP selects between signed and unsigned types.
3420 If the mode is a fixed-point mode,
3421 then UNSIGNEDP selects between saturating and nonsaturating types. */
3423 tree
3424 c_common_type_for_mode (machine_mode mode, int unsignedp)
3426 tree t;
3427 int i;
3429 if (mode == TYPE_MODE (integer_type_node))
3430 return unsignedp ? unsigned_type_node : integer_type_node;
3432 if (mode == TYPE_MODE (signed_char_type_node))
3433 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3435 if (mode == TYPE_MODE (short_integer_type_node))
3436 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3438 if (mode == TYPE_MODE (long_integer_type_node))
3439 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3441 if (mode == TYPE_MODE (long_long_integer_type_node))
3442 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3444 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3445 if (int_n_enabled_p[i]
3446 && mode == int_n_data[i].m)
3447 return (unsignedp ? int_n_trees[i].unsigned_type
3448 : int_n_trees[i].signed_type);
3450 if (mode == TYPE_MODE (widest_integer_literal_type_node))
3451 return unsignedp ? widest_unsigned_literal_type_node
3452 : widest_integer_literal_type_node;
3454 if (mode == QImode)
3455 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3457 if (mode == HImode)
3458 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3460 if (mode == SImode)
3461 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3463 if (mode == DImode)
3464 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3466 #if HOST_BITS_PER_WIDE_INT >= 64
3467 if (mode == TYPE_MODE (intTI_type_node))
3468 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3469 #endif
3471 if (mode == TYPE_MODE (float_type_node))
3472 return float_type_node;
3474 if (mode == TYPE_MODE (double_type_node))
3475 return double_type_node;
3477 if (mode == TYPE_MODE (long_double_type_node))
3478 return long_double_type_node;
3480 if (mode == TYPE_MODE (void_type_node))
3481 return void_type_node;
3483 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3484 return (unsignedp
3485 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3486 : make_signed_type (GET_MODE_PRECISION (mode)));
3488 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3489 return (unsignedp
3490 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3491 : make_signed_type (GET_MODE_PRECISION (mode)));
3493 if (COMPLEX_MODE_P (mode))
3495 machine_mode inner_mode;
3496 tree inner_type;
3498 if (mode == TYPE_MODE (complex_float_type_node))
3499 return complex_float_type_node;
3500 if (mode == TYPE_MODE (complex_double_type_node))
3501 return complex_double_type_node;
3502 if (mode == TYPE_MODE (complex_long_double_type_node))
3503 return complex_long_double_type_node;
3505 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3506 return complex_integer_type_node;
3508 inner_mode = GET_MODE_INNER (mode);
3509 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3510 if (inner_type != NULL_TREE)
3511 return build_complex_type (inner_type);
3513 else if (VECTOR_MODE_P (mode))
3515 machine_mode inner_mode = GET_MODE_INNER (mode);
3516 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3517 if (inner_type != NULL_TREE)
3518 return build_vector_type_for_mode (inner_type, mode);
3521 if (mode == TYPE_MODE (dfloat32_type_node))
3522 return dfloat32_type_node;
3523 if (mode == TYPE_MODE (dfloat64_type_node))
3524 return dfloat64_type_node;
3525 if (mode == TYPE_MODE (dfloat128_type_node))
3526 return dfloat128_type_node;
3528 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3530 if (mode == TYPE_MODE (short_fract_type_node))
3531 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3532 if (mode == TYPE_MODE (fract_type_node))
3533 return unsignedp ? sat_fract_type_node : fract_type_node;
3534 if (mode == TYPE_MODE (long_fract_type_node))
3535 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3536 if (mode == TYPE_MODE (long_long_fract_type_node))
3537 return unsignedp ? sat_long_long_fract_type_node
3538 : long_long_fract_type_node;
3540 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3541 return unsignedp ? sat_unsigned_short_fract_type_node
3542 : unsigned_short_fract_type_node;
3543 if (mode == TYPE_MODE (unsigned_fract_type_node))
3544 return unsignedp ? sat_unsigned_fract_type_node
3545 : unsigned_fract_type_node;
3546 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3547 return unsignedp ? sat_unsigned_long_fract_type_node
3548 : unsigned_long_fract_type_node;
3549 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3550 return unsignedp ? sat_unsigned_long_long_fract_type_node
3551 : unsigned_long_long_fract_type_node;
3553 if (mode == TYPE_MODE (short_accum_type_node))
3554 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3555 if (mode == TYPE_MODE (accum_type_node))
3556 return unsignedp ? sat_accum_type_node : accum_type_node;
3557 if (mode == TYPE_MODE (long_accum_type_node))
3558 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3559 if (mode == TYPE_MODE (long_long_accum_type_node))
3560 return unsignedp ? sat_long_long_accum_type_node
3561 : long_long_accum_type_node;
3563 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3564 return unsignedp ? sat_unsigned_short_accum_type_node
3565 : unsigned_short_accum_type_node;
3566 if (mode == TYPE_MODE (unsigned_accum_type_node))
3567 return unsignedp ? sat_unsigned_accum_type_node
3568 : unsigned_accum_type_node;
3569 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3570 return unsignedp ? sat_unsigned_long_accum_type_node
3571 : unsigned_long_accum_type_node;
3572 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3573 return unsignedp ? sat_unsigned_long_long_accum_type_node
3574 : unsigned_long_long_accum_type_node;
3576 if (mode == QQmode)
3577 return unsignedp ? sat_qq_type_node : qq_type_node;
3578 if (mode == HQmode)
3579 return unsignedp ? sat_hq_type_node : hq_type_node;
3580 if (mode == SQmode)
3581 return unsignedp ? sat_sq_type_node : sq_type_node;
3582 if (mode == DQmode)
3583 return unsignedp ? sat_dq_type_node : dq_type_node;
3584 if (mode == TQmode)
3585 return unsignedp ? sat_tq_type_node : tq_type_node;
3587 if (mode == UQQmode)
3588 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3589 if (mode == UHQmode)
3590 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3591 if (mode == USQmode)
3592 return unsignedp ? sat_usq_type_node : usq_type_node;
3593 if (mode == UDQmode)
3594 return unsignedp ? sat_udq_type_node : udq_type_node;
3595 if (mode == UTQmode)
3596 return unsignedp ? sat_utq_type_node : utq_type_node;
3598 if (mode == HAmode)
3599 return unsignedp ? sat_ha_type_node : ha_type_node;
3600 if (mode == SAmode)
3601 return unsignedp ? sat_sa_type_node : sa_type_node;
3602 if (mode == DAmode)
3603 return unsignedp ? sat_da_type_node : da_type_node;
3604 if (mode == TAmode)
3605 return unsignedp ? sat_ta_type_node : ta_type_node;
3607 if (mode == UHAmode)
3608 return unsignedp ? sat_uha_type_node : uha_type_node;
3609 if (mode == USAmode)
3610 return unsignedp ? sat_usa_type_node : usa_type_node;
3611 if (mode == UDAmode)
3612 return unsignedp ? sat_uda_type_node : uda_type_node;
3613 if (mode == UTAmode)
3614 return unsignedp ? sat_uta_type_node : uta_type_node;
3617 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
3618 if (TYPE_MODE (TREE_VALUE (t)) == mode
3619 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
3620 return TREE_VALUE (t);
3622 return 0;
3625 tree
3626 c_common_unsigned_type (tree type)
3628 return c_common_signed_or_unsigned_type (1, type);
3631 /* Return a signed type the same as TYPE in other respects. */
3633 tree
3634 c_common_signed_type (tree type)
3636 return c_common_signed_or_unsigned_type (0, type);
3639 /* Return a type the same as TYPE except unsigned or
3640 signed according to UNSIGNEDP. */
3642 tree
3643 c_common_signed_or_unsigned_type (int unsignedp, tree type)
3645 tree type1;
3646 int i;
3648 /* This block of code emulates the behavior of the old
3649 c_common_unsigned_type. In particular, it returns
3650 long_unsigned_type_node if passed a long, even when a int would
3651 have the same size. This is necessary for warnings to work
3652 correctly in archs where sizeof(int) == sizeof(long) */
3654 type1 = TYPE_MAIN_VARIANT (type);
3655 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3656 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3657 if (type1 == integer_type_node || type1 == unsigned_type_node)
3658 return unsignedp ? unsigned_type_node : integer_type_node;
3659 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3660 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3661 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3662 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3663 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3664 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3666 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3667 if (int_n_enabled_p[i]
3668 && (type1 == int_n_trees[i].unsigned_type
3669 || type1 == int_n_trees[i].signed_type))
3670 return (unsignedp ? int_n_trees[i].unsigned_type
3671 : int_n_trees[i].signed_type);
3673 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3674 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3675 #if HOST_BITS_PER_WIDE_INT >= 64
3676 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3677 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3678 #endif
3679 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3680 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3681 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3682 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3683 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3684 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3685 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3686 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3688 #define C_COMMON_FIXED_TYPES(NAME) \
3689 if (type1 == short_ ## NAME ## _type_node \
3690 || type1 == unsigned_short_ ## NAME ## _type_node) \
3691 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3692 : short_ ## NAME ## _type_node; \
3693 if (type1 == NAME ## _type_node \
3694 || type1 == unsigned_ ## NAME ## _type_node) \
3695 return unsignedp ? unsigned_ ## NAME ## _type_node \
3696 : NAME ## _type_node; \
3697 if (type1 == long_ ## NAME ## _type_node \
3698 || type1 == unsigned_long_ ## NAME ## _type_node) \
3699 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3700 : long_ ## NAME ## _type_node; \
3701 if (type1 == long_long_ ## NAME ## _type_node \
3702 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3703 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3704 : long_long_ ## NAME ## _type_node;
3706 #define C_COMMON_FIXED_MODE_TYPES(NAME) \
3707 if (type1 == NAME ## _type_node \
3708 || type1 == u ## NAME ## _type_node) \
3709 return unsignedp ? u ## NAME ## _type_node \
3710 : NAME ## _type_node;
3712 #define C_COMMON_FIXED_TYPES_SAT(NAME) \
3713 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3714 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3715 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3716 : sat_ ## short_ ## NAME ## _type_node; \
3717 if (type1 == sat_ ## NAME ## _type_node \
3718 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3719 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3720 : sat_ ## NAME ## _type_node; \
3721 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3722 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3723 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3724 : sat_ ## long_ ## NAME ## _type_node; \
3725 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3726 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3727 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3728 : sat_ ## long_long_ ## NAME ## _type_node;
3730 #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3731 if (type1 == sat_ ## NAME ## _type_node \
3732 || type1 == sat_ ## u ## NAME ## _type_node) \
3733 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3734 : sat_ ## NAME ## _type_node;
3736 C_COMMON_FIXED_TYPES (fract);
3737 C_COMMON_FIXED_TYPES_SAT (fract);
3738 C_COMMON_FIXED_TYPES (accum);
3739 C_COMMON_FIXED_TYPES_SAT (accum);
3741 C_COMMON_FIXED_MODE_TYPES (qq);
3742 C_COMMON_FIXED_MODE_TYPES (hq);
3743 C_COMMON_FIXED_MODE_TYPES (sq);
3744 C_COMMON_FIXED_MODE_TYPES (dq);
3745 C_COMMON_FIXED_MODE_TYPES (tq);
3746 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3747 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3748 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3749 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3750 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3751 C_COMMON_FIXED_MODE_TYPES (ha);
3752 C_COMMON_FIXED_MODE_TYPES (sa);
3753 C_COMMON_FIXED_MODE_TYPES (da);
3754 C_COMMON_FIXED_MODE_TYPES (ta);
3755 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3756 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3757 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3758 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
3760 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3761 the precision; they have precision set to match their range, but
3762 may use a wider mode to match an ABI. If we change modes, we may
3763 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3764 the precision as well, so as to yield correct results for
3765 bit-field types. C++ does not have these separate bit-field
3766 types, and producing a signed or unsigned variant of an
3767 ENUMERAL_TYPE may cause other problems as well. */
3769 if (!INTEGRAL_TYPE_P (type)
3770 || TYPE_UNSIGNED (type) == unsignedp)
3771 return type;
3773 #define TYPE_OK(node) \
3774 (TYPE_MODE (type) == TYPE_MODE (node) \
3775 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3776 if (TYPE_OK (signed_char_type_node))
3777 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3778 if (TYPE_OK (integer_type_node))
3779 return unsignedp ? unsigned_type_node : integer_type_node;
3780 if (TYPE_OK (short_integer_type_node))
3781 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3782 if (TYPE_OK (long_integer_type_node))
3783 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3784 if (TYPE_OK (long_long_integer_type_node))
3785 return (unsignedp ? long_long_unsigned_type_node
3786 : long_long_integer_type_node);
3788 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3789 if (int_n_enabled_p[i]
3790 && TYPE_MODE (type) == int_n_data[i].m
3791 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3792 return (unsignedp ? int_n_trees[i].unsigned_type
3793 : int_n_trees[i].signed_type);
3795 if (TYPE_OK (widest_integer_literal_type_node))
3796 return (unsignedp ? widest_unsigned_literal_type_node
3797 : widest_integer_literal_type_node);
3799 #if HOST_BITS_PER_WIDE_INT >= 64
3800 if (TYPE_OK (intTI_type_node))
3801 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3802 #endif
3803 if (TYPE_OK (intDI_type_node))
3804 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3805 if (TYPE_OK (intSI_type_node))
3806 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3807 if (TYPE_OK (intHI_type_node))
3808 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3809 if (TYPE_OK (intQI_type_node))
3810 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3811 #undef TYPE_OK
3813 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3816 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3818 tree
3819 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3821 int i;
3823 /* Extended integer types of the same width as a standard type have
3824 lesser rank, so those of the same width as int promote to int or
3825 unsigned int and are valid for printf formats expecting int or
3826 unsigned int. To avoid such special cases, avoid creating
3827 extended integer types for bit-fields if a standard integer type
3828 is available. */
3829 if (width == TYPE_PRECISION (integer_type_node))
3830 return unsignedp ? unsigned_type_node : integer_type_node;
3831 if (width == TYPE_PRECISION (signed_char_type_node))
3832 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3833 if (width == TYPE_PRECISION (short_integer_type_node))
3834 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3835 if (width == TYPE_PRECISION (long_integer_type_node))
3836 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3837 if (width == TYPE_PRECISION (long_long_integer_type_node))
3838 return (unsignedp ? long_long_unsigned_type_node
3839 : long_long_integer_type_node);
3840 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3841 if (int_n_enabled_p[i]
3842 && width == int_n_data[i].bitsize)
3843 return (unsignedp ? int_n_trees[i].unsigned_type
3844 : int_n_trees[i].signed_type);
3845 return build_nonstandard_integer_type (width, unsignedp);
3848 /* The C version of the register_builtin_type langhook. */
3850 void
3851 c_register_builtin_type (tree type, const char* name)
3853 tree decl;
3855 decl = build_decl (UNKNOWN_LOCATION,
3856 TYPE_DECL, get_identifier (name), type);
3857 DECL_ARTIFICIAL (decl) = 1;
3858 if (!TYPE_NAME (type))
3859 TYPE_NAME (type) = decl;
3860 pushdecl (decl);
3862 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
3865 /* Print an error message for invalid operands to arith operation
3866 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
3867 RICHLOC is a rich location for the message, containing either
3868 three separate locations for each of the operator and operands
3870 lhs op rhs
3871 ~~~ ^~ ~~~
3873 (C FE), or one location ranging over all over them
3875 lhs op rhs
3876 ~~~~^~~~~~
3878 (C++ FE). */
3880 void
3881 binary_op_error (rich_location *richloc, enum tree_code code,
3882 tree type0, tree type1)
3884 const char *opname;
3886 switch (code)
3888 case PLUS_EXPR:
3889 opname = "+"; break;
3890 case MINUS_EXPR:
3891 opname = "-"; break;
3892 case MULT_EXPR:
3893 opname = "*"; break;
3894 case MAX_EXPR:
3895 opname = "max"; break;
3896 case MIN_EXPR:
3897 opname = "min"; break;
3898 case EQ_EXPR:
3899 opname = "=="; break;
3900 case NE_EXPR:
3901 opname = "!="; break;
3902 case LE_EXPR:
3903 opname = "<="; break;
3904 case GE_EXPR:
3905 opname = ">="; break;
3906 case LT_EXPR:
3907 opname = "<"; break;
3908 case GT_EXPR:
3909 opname = ">"; break;
3910 case LSHIFT_EXPR:
3911 opname = "<<"; break;
3912 case RSHIFT_EXPR:
3913 opname = ">>"; break;
3914 case TRUNC_MOD_EXPR:
3915 case FLOOR_MOD_EXPR:
3916 opname = "%"; break;
3917 case TRUNC_DIV_EXPR:
3918 case FLOOR_DIV_EXPR:
3919 opname = "/"; break;
3920 case BIT_AND_EXPR:
3921 opname = "&"; break;
3922 case BIT_IOR_EXPR:
3923 opname = "|"; break;
3924 case TRUTH_ANDIF_EXPR:
3925 opname = "&&"; break;
3926 case TRUTH_ORIF_EXPR:
3927 opname = "||"; break;
3928 case BIT_XOR_EXPR:
3929 opname = "^"; break;
3930 default:
3931 gcc_unreachable ();
3933 error_at_rich_loc (richloc,
3934 "invalid operands to binary %s (have %qT and %qT)",
3935 opname, type0, type1);
3938 /* Given an expression as a tree, return its original type. Do this
3939 by stripping any conversion that preserves the sign and precision. */
3940 static tree
3941 expr_original_type (tree expr)
3943 STRIP_SIGN_NOPS (expr);
3944 return TREE_TYPE (expr);
3947 /* Subroutine of build_binary_op, used for comparison operations.
3948 See if the operands have both been converted from subword integer types
3949 and, if so, perhaps change them both back to their original type.
3950 This function is also responsible for converting the two operands
3951 to the proper common type for comparison.
3953 The arguments of this function are all pointers to local variables
3954 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3955 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3957 LOC is the location of the comparison.
3959 If this function returns nonzero, it means that the comparison has
3960 a constant value. What this function returns is an expression for
3961 that value. */
3963 tree
3964 shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3965 tree *restype_ptr, enum tree_code *rescode_ptr)
3967 tree type;
3968 tree op0 = *op0_ptr;
3969 tree op1 = *op1_ptr;
3970 int unsignedp0, unsignedp1;
3971 int real1, real2;
3972 tree primop0, primop1;
3973 enum tree_code code = *rescode_ptr;
3975 /* Throw away any conversions to wider types
3976 already present in the operands. */
3978 primop0 = c_common_get_narrower (op0, &unsignedp0);
3979 primop1 = c_common_get_narrower (op1, &unsignedp1);
3981 /* If primopN is first sign-extended from primopN's precision to opN's
3982 precision, then zero-extended from opN's precision to
3983 *restype_ptr precision, shortenings might be invalid. */
3984 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3985 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3986 && !unsignedp0
3987 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3988 primop0 = op0;
3989 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3990 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3991 && !unsignedp1
3992 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3993 primop1 = op1;
3995 /* Handle the case that OP0 does not *contain* a conversion
3996 but it *requires* conversion to FINAL_TYPE. */
3998 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3999 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
4000 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
4001 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
4003 /* If one of the operands must be floated, we cannot optimize. */
4004 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4005 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4007 /* If first arg is constant, swap the args (changing operation
4008 so value is preserved), for canonicalization. Don't do this if
4009 the second arg is 0. */
4011 if (TREE_CONSTANT (primop0)
4012 && !integer_zerop (primop1) && !real_zerop (primop1)
4013 && !fixed_zerop (primop1))
4015 std::swap (primop0, primop1);
4016 std::swap (op0, op1);
4017 *op0_ptr = op0;
4018 *op1_ptr = op1;
4019 std::swap (unsignedp0, unsignedp1);
4020 std::swap (real1, real2);
4022 switch (code)
4024 case LT_EXPR:
4025 code = GT_EXPR;
4026 break;
4027 case GT_EXPR:
4028 code = LT_EXPR;
4029 break;
4030 case LE_EXPR:
4031 code = GE_EXPR;
4032 break;
4033 case GE_EXPR:
4034 code = LE_EXPR;
4035 break;
4036 default:
4037 break;
4039 *rescode_ptr = code;
4042 /* If comparing an integer against a constant more bits wide,
4043 maybe we can deduce a value of 1 or 0 independent of the data.
4044 Or else truncate the constant now
4045 rather than extend the variable at run time.
4047 This is only interesting if the constant is the wider arg.
4048 Also, it is not safe if the constant is unsigned and the
4049 variable arg is signed, since in this case the variable
4050 would be sign-extended and then regarded as unsigned.
4051 Our technique fails in this case because the lowest/highest
4052 possible unsigned results don't follow naturally from the
4053 lowest/highest possible values of the variable operand.
4054 For just EQ_EXPR and NE_EXPR there is another technique that
4055 could be used: see if the constant can be faithfully represented
4056 in the other operand's type, by truncating it and reextending it
4057 and see if that preserves the constant's value. */
4059 if (!real1 && !real2
4060 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
4061 && TREE_CODE (primop1) == INTEGER_CST
4062 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4064 int min_gt, max_gt, min_lt, max_lt;
4065 tree maxval, minval;
4066 /* 1 if comparison is nominally unsigned. */
4067 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
4068 tree val;
4070 type = c_common_signed_or_unsigned_type (unsignedp0,
4071 TREE_TYPE (primop0));
4073 maxval = TYPE_MAX_VALUE (type);
4074 minval = TYPE_MIN_VALUE (type);
4076 if (unsignedp && !unsignedp0)
4077 *restype_ptr = c_common_signed_type (*restype_ptr);
4079 if (TREE_TYPE (primop1) != *restype_ptr)
4081 /* Convert primop1 to target type, but do not introduce
4082 additional overflow. We know primop1 is an int_cst. */
4083 primop1 = force_fit_type (*restype_ptr,
4084 wi::to_wide
4085 (primop1,
4086 TYPE_PRECISION (*restype_ptr)),
4087 0, TREE_OVERFLOW (primop1));
4089 if (type != *restype_ptr)
4091 minval = convert (*restype_ptr, minval);
4092 maxval = convert (*restype_ptr, maxval);
4095 min_gt = tree_int_cst_lt (primop1, minval);
4096 max_gt = tree_int_cst_lt (primop1, maxval);
4097 min_lt = tree_int_cst_lt (minval, primop1);
4098 max_lt = tree_int_cst_lt (maxval, primop1);
4100 val = 0;
4101 /* This used to be a switch, but Genix compiler can't handle that. */
4102 if (code == NE_EXPR)
4104 if (max_lt || min_gt)
4105 val = truthvalue_true_node;
4107 else if (code == EQ_EXPR)
4109 if (max_lt || min_gt)
4110 val = truthvalue_false_node;
4112 else if (code == LT_EXPR)
4114 if (max_lt)
4115 val = truthvalue_true_node;
4116 if (!min_lt)
4117 val = truthvalue_false_node;
4119 else if (code == GT_EXPR)
4121 if (min_gt)
4122 val = truthvalue_true_node;
4123 if (!max_gt)
4124 val = truthvalue_false_node;
4126 else if (code == LE_EXPR)
4128 if (!max_gt)
4129 val = truthvalue_true_node;
4130 if (min_gt)
4131 val = truthvalue_false_node;
4133 else if (code == GE_EXPR)
4135 if (!min_lt)
4136 val = truthvalue_true_node;
4137 if (max_lt)
4138 val = truthvalue_false_node;
4141 /* If primop0 was sign-extended and unsigned comparison specd,
4142 we did a signed comparison above using the signed type bounds.
4143 But the comparison we output must be unsigned.
4145 Also, for inequalities, VAL is no good; but if the signed
4146 comparison had *any* fixed result, it follows that the
4147 unsigned comparison just tests the sign in reverse
4148 (positive values are LE, negative ones GE).
4149 So we can generate an unsigned comparison
4150 against an extreme value of the signed type. */
4152 if (unsignedp && !unsignedp0)
4154 if (val != 0)
4155 switch (code)
4157 case LT_EXPR:
4158 case GE_EXPR:
4159 primop1 = TYPE_MIN_VALUE (type);
4160 val = 0;
4161 break;
4163 case LE_EXPR:
4164 case GT_EXPR:
4165 primop1 = TYPE_MAX_VALUE (type);
4166 val = 0;
4167 break;
4169 default:
4170 break;
4172 type = c_common_unsigned_type (type);
4175 if (TREE_CODE (primop0) != INTEGER_CST
4176 /* Don't warn if it's from a (non-system) macro. */
4177 && !(from_macro_expansion_at
4178 (expansion_point_location_if_in_system_header
4179 (EXPR_LOCATION (primop0)))))
4181 if (val == truthvalue_false_node)
4182 warning_at (loc, OPT_Wtype_limits,
4183 "comparison is always false due to limited range of data type");
4184 if (val == truthvalue_true_node)
4185 warning_at (loc, OPT_Wtype_limits,
4186 "comparison is always true due to limited range of data type");
4189 if (val != 0)
4191 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4192 if (TREE_SIDE_EFFECTS (primop0))
4193 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
4194 return val;
4197 /* Value is not predetermined, but do the comparison
4198 in the type of the operand that is not constant.
4199 TYPE is already properly set. */
4202 /* If either arg is decimal float and the other is float, find the
4203 proper common type to use for comparison. */
4204 else if (real1 && real2
4205 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4206 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4207 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4209 /* If either arg is decimal float and the other is float, fail. */
4210 else if (real1 && real2
4211 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4212 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
4213 return 0;
4215 else if (real1 && real2
4216 && (TYPE_PRECISION (TREE_TYPE (primop0))
4217 == TYPE_PRECISION (TREE_TYPE (primop1))))
4218 type = TREE_TYPE (primop0);
4220 /* If args' natural types are both narrower than nominal type
4221 and both extend in the same manner, compare them
4222 in the type of the wider arg.
4223 Otherwise must actually extend both to the nominal
4224 common type lest different ways of extending
4225 alter the result.
4226 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4228 else if (unsignedp0 == unsignedp1 && real1 == real2
4229 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4230 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4232 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4233 type = c_common_signed_or_unsigned_type (unsignedp0
4234 || TYPE_UNSIGNED (*restype_ptr),
4235 type);
4236 /* Make sure shorter operand is extended the right way
4237 to match the longer operand. */
4238 primop0
4239 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4240 TREE_TYPE (primop0)),
4241 primop0);
4242 primop1
4243 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4244 TREE_TYPE (primop1)),
4245 primop1);
4247 else
4249 /* Here we must do the comparison on the nominal type
4250 using the args exactly as we received them. */
4251 type = *restype_ptr;
4252 primop0 = op0;
4253 primop1 = op1;
4255 if (!real1 && !real2 && integer_zerop (primop1)
4256 && TYPE_UNSIGNED (*restype_ptr))
4258 tree value = 0;
4259 /* All unsigned values are >= 0, so we warn. However,
4260 if OP0 is a constant that is >= 0, the signedness of
4261 the comparison isn't an issue, so suppress the
4262 warning. */
4263 bool warn =
4264 warn_type_limits && !in_system_header_at (loc)
4265 && !(TREE_CODE (primop0) == INTEGER_CST
4266 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4267 primop0)))
4268 /* Do not warn for enumeration types. */
4269 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4271 switch (code)
4273 case GE_EXPR:
4274 if (warn)
4275 warning_at (loc, OPT_Wtype_limits,
4276 "comparison of unsigned expression >= 0 is always true");
4277 value = truthvalue_true_node;
4278 break;
4280 case LT_EXPR:
4281 if (warn)
4282 warning_at (loc, OPT_Wtype_limits,
4283 "comparison of unsigned expression < 0 is always false");
4284 value = truthvalue_false_node;
4285 break;
4287 default:
4288 break;
4291 if (value != 0)
4293 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4294 if (TREE_SIDE_EFFECTS (primop0))
4295 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4296 primop0, value);
4297 return value;
4302 *op0_ptr = convert (type, primop0);
4303 *op1_ptr = convert (type, primop1);
4305 *restype_ptr = truthvalue_type_node;
4307 return 0;
4310 /* Return a tree for the sum or difference (RESULTCODE says which)
4311 of pointer PTROP and integer INTOP. */
4313 tree
4314 pointer_int_sum (location_t loc, enum tree_code resultcode,
4315 tree ptrop, tree intop, bool complain)
4317 tree size_exp, ret;
4319 /* The result is a pointer of the same type that is being added. */
4320 tree result_type = TREE_TYPE (ptrop);
4322 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4324 if (complain && warn_pointer_arith)
4325 pedwarn (loc, OPT_Wpointer_arith,
4326 "pointer of type %<void *%> used in arithmetic");
4327 else if (!complain)
4328 return error_mark_node;
4329 size_exp = integer_one_node;
4331 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4333 if (complain && warn_pointer_arith)
4334 pedwarn (loc, OPT_Wpointer_arith,
4335 "pointer to a function used in arithmetic");
4336 else if (!complain)
4337 return error_mark_node;
4338 size_exp = integer_one_node;
4340 else
4341 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
4343 /* We are manipulating pointer values, so we don't need to warn
4344 about relying on undefined signed overflow. We disable the
4345 warning here because we use integer types so fold won't know that
4346 they are really pointers. */
4347 fold_defer_overflow_warnings ();
4349 /* If what we are about to multiply by the size of the elements
4350 contains a constant term, apply distributive law
4351 and multiply that constant term separately.
4352 This helps produce common subexpressions. */
4353 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
4354 && !TREE_CONSTANT (intop)
4355 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4356 && TREE_CONSTANT (size_exp)
4357 /* If the constant comes from pointer subtraction,
4358 skip this optimization--it would cause an error. */
4359 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4360 /* If the constant is unsigned, and smaller than the pointer size,
4361 then we must skip this optimization. This is because it could cause
4362 an overflow error if the constant is negative but INTOP is not. */
4363 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
4364 || (TYPE_PRECISION (TREE_TYPE (intop))
4365 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4367 enum tree_code subcode = resultcode;
4368 tree int_type = TREE_TYPE (intop);
4369 if (TREE_CODE (intop) == MINUS_EXPR)
4370 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4371 /* Convert both subexpression types to the type of intop,
4372 because weird cases involving pointer arithmetic
4373 can result in a sum or difference with different type args. */
4374 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4375 subcode, ptrop,
4376 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4377 intop = convert (int_type, TREE_OPERAND (intop, 0));
4380 /* Convert the integer argument to a type the same size as sizetype
4381 so the multiply won't overflow spuriously. */
4382 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
4383 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
4384 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
4385 TYPE_UNSIGNED (sizetype)), intop);
4387 /* Replace the integer argument with a suitable product by the object size.
4388 Do this multiplication as signed, then convert to the appropriate type
4389 for the pointer operation and disregard an overflow that occurred only
4390 because of the sign-extension change in the latter conversion. */
4392 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4393 convert (TREE_TYPE (intop), size_exp));
4394 intop = convert (sizetype, t);
4395 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
4396 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
4399 /* Create the sum or difference. */
4400 if (resultcode == MINUS_EXPR)
4401 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
4403 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
4405 fold_undefer_and_ignore_overflow_warnings ();
4407 return ret;
4410 /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4411 and if NON_CONST is known not to be permitted in an evaluated part
4412 of a constant expression. */
4414 tree
4415 c_wrap_maybe_const (tree expr, bool non_const)
4417 bool nowarning = TREE_NO_WARNING (expr);
4418 location_t loc = EXPR_LOCATION (expr);
4420 /* This should never be called for C++. */
4421 if (c_dialect_cxx ())
4422 gcc_unreachable ();
4424 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4425 STRIP_TYPE_NOPS (expr);
4426 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4427 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4428 if (nowarning)
4429 TREE_NO_WARNING (expr) = 1;
4430 protected_set_expr_location (expr, loc);
4432 return expr;
4435 /* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4436 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4437 around the SAVE_EXPR if needed so that c_fully_fold does not need
4438 to look inside SAVE_EXPRs. */
4440 tree
4441 c_save_expr (tree expr)
4443 bool maybe_const = true;
4444 if (c_dialect_cxx ())
4445 return save_expr (expr);
4446 expr = c_fully_fold (expr, false, &maybe_const);
4447 expr = save_expr (expr);
4448 if (!maybe_const)
4449 expr = c_wrap_maybe_const (expr, true);
4450 return expr;
4453 /* Return whether EXPR is a declaration whose address can never be
4454 NULL. */
4456 bool
4457 decl_with_nonnull_addr_p (const_tree expr)
4459 return (DECL_P (expr)
4460 && (TREE_CODE (expr) == PARM_DECL
4461 || TREE_CODE (expr) == LABEL_DECL
4462 || !DECL_WEAK (expr)));
4465 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
4466 or for an `if' or `while' statement or ?..: exp. It should already
4467 have been validated to be of suitable type; otherwise, a bad
4468 diagnostic may result.
4470 The EXPR is located at LOCATION.
4472 This preparation consists of taking the ordinary
4473 representation of an expression expr and producing a valid tree
4474 boolean expression describing whether expr is nonzero. We could
4475 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
4476 but we optimize comparisons, &&, ||, and !.
4478 The resulting type should always be `truthvalue_type_node'. */
4480 tree
4481 c_common_truthvalue_conversion (location_t location, tree expr)
4483 switch (TREE_CODE (expr))
4485 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
4486 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4487 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4488 case ORDERED_EXPR: case UNORDERED_EXPR:
4489 if (TREE_TYPE (expr) == truthvalue_type_node)
4490 return expr;
4491 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4492 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
4493 goto ret;
4495 case TRUTH_ANDIF_EXPR:
4496 case TRUTH_ORIF_EXPR:
4497 case TRUTH_AND_EXPR:
4498 case TRUTH_OR_EXPR:
4499 case TRUTH_XOR_EXPR:
4500 if (TREE_TYPE (expr) == truthvalue_type_node)
4501 return expr;
4502 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
4503 c_common_truthvalue_conversion (location,
4504 TREE_OPERAND (expr, 0)),
4505 c_common_truthvalue_conversion (location,
4506 TREE_OPERAND (expr, 1)));
4507 goto ret;
4509 case TRUTH_NOT_EXPR:
4510 if (TREE_TYPE (expr) == truthvalue_type_node)
4511 return expr;
4512 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4513 c_common_truthvalue_conversion (location,
4514 TREE_OPERAND (expr, 0)));
4515 goto ret;
4517 case ERROR_MARK:
4518 return expr;
4520 case INTEGER_CST:
4521 return integer_zerop (expr) ? truthvalue_false_node
4522 : truthvalue_true_node;
4524 case REAL_CST:
4525 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4526 ? truthvalue_true_node
4527 : truthvalue_false_node;
4529 case FIXED_CST:
4530 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4531 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4532 ? truthvalue_true_node
4533 : truthvalue_false_node;
4535 case FUNCTION_DECL:
4536 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
4537 /* Fall through. */
4539 case ADDR_EXPR:
4541 tree inner = TREE_OPERAND (expr, 0);
4542 if (decl_with_nonnull_addr_p (inner))
4544 /* Common Ada/Pascal programmer's mistake. */
4545 warning_at (location,
4546 OPT_Waddress,
4547 "the address of %qD will always evaluate as %<true%>",
4548 inner);
4549 return truthvalue_true_node;
4551 break;
4554 case COMPLEX_EXPR:
4555 expr = build_binary_op (EXPR_LOCATION (expr),
4556 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
4557 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4558 c_common_truthvalue_conversion (location,
4559 TREE_OPERAND (expr, 0)),
4560 c_common_truthvalue_conversion (location,
4561 TREE_OPERAND (expr, 1)),
4563 goto ret;
4565 case NEGATE_EXPR:
4566 case ABS_EXPR:
4567 case FLOAT_EXPR:
4568 case EXCESS_PRECISION_EXPR:
4569 /* These don't change whether an object is nonzero or zero. */
4570 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
4572 case LROTATE_EXPR:
4573 case RROTATE_EXPR:
4574 /* These don't change whether an object is zero or nonzero, but
4575 we can't ignore them if their second arg has side-effects. */
4576 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
4578 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4579 TREE_OPERAND (expr, 1),
4580 c_common_truthvalue_conversion
4581 (location, TREE_OPERAND (expr, 0)));
4582 goto ret;
4584 else
4585 return c_common_truthvalue_conversion (location,
4586 TREE_OPERAND (expr, 0));
4588 case COND_EXPR:
4589 /* Distribute the conversion into the arms of a COND_EXPR. */
4590 if (c_dialect_cxx ())
4592 tree op1 = TREE_OPERAND (expr, 1);
4593 tree op2 = TREE_OPERAND (expr, 2);
4594 /* In C++ one of the arms might have void type if it is throw. */
4595 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4596 op1 = c_common_truthvalue_conversion (location, op1);
4597 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4598 op2 = c_common_truthvalue_conversion (location, op2);
4599 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4600 TREE_OPERAND (expr, 0), op1, op2);
4601 goto ret;
4603 else
4605 /* Folding will happen later for C. */
4606 expr = build3 (COND_EXPR, truthvalue_type_node,
4607 TREE_OPERAND (expr, 0),
4608 c_common_truthvalue_conversion (location,
4609 TREE_OPERAND (expr, 1)),
4610 c_common_truthvalue_conversion (location,
4611 TREE_OPERAND (expr, 2)));
4612 goto ret;
4615 CASE_CONVERT:
4617 tree totype = TREE_TYPE (expr);
4618 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4620 if (POINTER_TYPE_P (totype)
4621 && !c_inhibit_evaluation_warnings
4622 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4624 tree inner = expr;
4625 STRIP_NOPS (inner);
4627 if (DECL_P (inner))
4628 warning_at (location,
4629 OPT_Waddress,
4630 "the compiler can assume that the address of "
4631 "%qD will always evaluate to %<true%>",
4632 inner);
4635 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4636 since that affects how `default_conversion' will behave. */
4637 if (TREE_CODE (totype) == REFERENCE_TYPE
4638 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4639 break;
4640 /* Don't strip a conversion from C++0x scoped enum, since they
4641 don't implicitly convert to other types. */
4642 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4643 && ENUM_IS_SCOPED (fromtype))
4644 break;
4645 /* If this isn't narrowing the argument, we can ignore it. */
4646 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4647 return c_common_truthvalue_conversion (location,
4648 TREE_OPERAND (expr, 0));
4650 break;
4652 case MODIFY_EXPR:
4653 if (!TREE_NO_WARNING (expr)
4654 && warn_parentheses)
4656 warning_at (location, OPT_Wparentheses,
4657 "suggest parentheses around assignment used as "
4658 "truth value");
4659 TREE_NO_WARNING (expr) = 1;
4661 break;
4663 default:
4664 break;
4667 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
4669 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
4670 expr = (build_binary_op
4671 (EXPR_LOCATION (expr),
4672 (TREE_SIDE_EFFECTS (expr)
4673 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4674 c_common_truthvalue_conversion
4675 (location,
4676 build_unary_op (location, REALPART_EXPR, t, 0)),
4677 c_common_truthvalue_conversion
4678 (location,
4679 build_unary_op (location, IMAGPART_EXPR, t, 0)),
4680 0));
4681 goto ret;
4684 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4686 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4687 FCONST0 (TYPE_MODE
4688 (TREE_TYPE (expr))));
4689 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
4691 else
4692 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
4694 ret:
4695 protected_set_expr_location (expr, location);
4696 return expr;
4699 static void def_builtin_1 (enum built_in_function fncode,
4700 const char *name,
4701 enum built_in_class fnclass,
4702 tree fntype, tree libtype,
4703 bool both_p, bool fallback_p, bool nonansi_p,
4704 tree fnattrs, bool implicit_p);
4707 /* Apply the TYPE_QUALS to the new DECL. */
4709 void
4710 c_apply_type_quals_to_decl (int type_quals, tree decl)
4712 tree type = TREE_TYPE (decl);
4714 if (type == error_mark_node)
4715 return;
4717 if ((type_quals & TYPE_QUAL_CONST)
4718 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4719 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4720 constructor can produce constant init, so rely on cp_finish_decl to
4721 clear TREE_READONLY if the variable has non-constant init. */
4722 TREE_READONLY (decl) = 1;
4723 if (type_quals & TYPE_QUAL_VOLATILE)
4725 TREE_SIDE_EFFECTS (decl) = 1;
4726 TREE_THIS_VOLATILE (decl) = 1;
4728 if (type_quals & TYPE_QUAL_RESTRICT)
4730 while (type && TREE_CODE (type) == ARRAY_TYPE)
4731 /* Allow 'restrict' on arrays of pointers.
4732 FIXME currently we just ignore it. */
4733 type = TREE_TYPE (type);
4734 if (!type
4735 || !POINTER_TYPE_P (type)
4736 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
4737 error ("invalid use of %<restrict%>");
4741 struct c_type_hasher : ggc_ptr_hash<tree_node>
4743 static hashval_t hash (tree);
4744 static bool equal (tree, tree);
4747 /* Hash function for the problem of multiple type definitions in
4748 different files. This must hash all types that will compare
4749 equal via comptypes to the same value. In practice it hashes
4750 on some of the simple stuff and leaves the details to comptypes. */
4752 hashval_t
4753 c_type_hasher::hash (tree t)
4755 int n_elements;
4756 int shift, size;
4757 tree t2;
4758 switch (TREE_CODE (t))
4760 /* For pointers, hash on pointee type plus some swizzling. */
4761 case POINTER_TYPE:
4762 return hash (TREE_TYPE (t)) ^ 0x3003003;
4763 /* Hash on number of elements and total size. */
4764 case ENUMERAL_TYPE:
4765 shift = 3;
4766 t2 = TYPE_VALUES (t);
4767 break;
4768 case RECORD_TYPE:
4769 shift = 0;
4770 t2 = TYPE_FIELDS (t);
4771 break;
4772 case QUAL_UNION_TYPE:
4773 shift = 1;
4774 t2 = TYPE_FIELDS (t);
4775 break;
4776 case UNION_TYPE:
4777 shift = 2;
4778 t2 = TYPE_FIELDS (t);
4779 break;
4780 default:
4781 gcc_unreachable ();
4783 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4784 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4785 n_elements = list_length (t2);
4786 /* We might have a VLA here. */
4787 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4788 size = 0;
4789 else
4790 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
4791 return ((size << 24) | (n_elements << shift));
4794 bool
4795 c_type_hasher::equal (tree t1, tree t2)
4797 return lang_hooks.types_compatible_p (t1, t2);
4800 static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4802 /* Return the typed-based alias set for T, which may be an expression
4803 or a type. Return -1 if we don't do anything special. */
4805 alias_set_type
4806 c_common_get_alias_set (tree t)
4808 /* For VLAs, use the alias set of the element type rather than the
4809 default of alias set 0 for types compared structurally. */
4810 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4812 if (TREE_CODE (t) == ARRAY_TYPE)
4813 return get_alias_set (TREE_TYPE (t));
4814 return -1;
4817 /* That's all the expressions we handle specially. */
4818 if (!TYPE_P (t))
4819 return -1;
4821 /* The C standard guarantees that any object may be accessed via an
4822 lvalue that has character type. */
4823 if (t == char_type_node
4824 || t == signed_char_type_node
4825 || t == unsigned_char_type_node)
4826 return 0;
4828 /* The C standard specifically allows aliasing between signed and
4829 unsigned variants of the same type. We treat the signed
4830 variant as canonical. */
4831 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
4833 tree t1 = c_common_signed_type (t);
4835 /* t1 == t can happen for boolean nodes which are always unsigned. */
4836 if (t1 != t)
4837 return get_alias_set (t1);
4840 /* Handle the case of multiple type nodes referring to "the same" type,
4841 which occurs with IMA. These share an alias set. FIXME: Currently only
4842 C90 is handled. (In C99 type compatibility is not transitive, which
4843 complicates things mightily. The alias set splay trees can theoretically
4844 represent this, but insertion is tricky when you consider all the
4845 different orders things might arrive in.) */
4847 if (c_language != clk_c || flag_isoc99)
4848 return -1;
4850 /* Save time if there's only one input file. */
4851 if (num_in_fnames == 1)
4852 return -1;
4854 /* Pointers need special handling if they point to any type that
4855 needs special handling (below). */
4856 if (TREE_CODE (t) == POINTER_TYPE)
4858 tree t2;
4859 /* Find bottom type under any nested POINTERs. */
4860 for (t2 = TREE_TYPE (t);
4861 TREE_CODE (t2) == POINTER_TYPE;
4862 t2 = TREE_TYPE (t2))
4864 if (!RECORD_OR_UNION_TYPE_P (t2)
4865 && TREE_CODE (t2) != ENUMERAL_TYPE)
4866 return -1;
4867 if (TYPE_SIZE (t2) == 0)
4868 return -1;
4870 /* These are the only cases that need special handling. */
4871 if (!RECORD_OR_UNION_TYPE_P (t)
4872 && TREE_CODE (t) != ENUMERAL_TYPE
4873 && TREE_CODE (t) != POINTER_TYPE)
4874 return -1;
4875 /* Undefined? */
4876 if (TYPE_SIZE (t) == 0)
4877 return -1;
4879 /* Look up t in hash table. Only one of the compatible types within each
4880 alias set is recorded in the table. */
4881 if (!type_hash_table)
4882 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4883 tree *slot = type_hash_table->find_slot (t, INSERT);
4884 if (*slot != NULL)
4886 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4887 return TYPE_ALIAS_SET ((tree)*slot);
4889 else
4890 /* Our caller will assign and record (in t) a new alias set; all we need
4891 to do is remember t in the hash table. */
4892 *slot = t;
4894 return -1;
4897 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
4898 the IS_SIZEOF parameter indicates which operator is being applied.
4899 The COMPLAIN flag controls whether we should diagnose possibly
4900 ill-formed constructs or not. LOC is the location of the SIZEOF or
4901 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4902 a type in any context should be returned, rather than the normal
4903 alignment for that type. */
4905 tree
4906 c_sizeof_or_alignof_type (location_t loc,
4907 tree type, bool is_sizeof, bool min_alignof,
4908 int complain)
4910 const char *op_name;
4911 tree value = NULL;
4912 enum tree_code type_code = TREE_CODE (type);
4914 op_name = is_sizeof ? "sizeof" : "__alignof__";
4916 if (type_code == FUNCTION_TYPE)
4918 if (is_sizeof)
4920 if (complain && warn_pointer_arith)
4921 pedwarn (loc, OPT_Wpointer_arith,
4922 "invalid application of %<sizeof%> to a function type");
4923 else if (!complain)
4924 return error_mark_node;
4925 value = size_one_node;
4927 else
4929 if (complain)
4931 if (c_dialect_cxx ())
4932 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4933 "%<alignof%> applied to a function type");
4934 else
4935 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4936 "%<_Alignof%> applied to a function type");
4938 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4941 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4943 if (type_code == VOID_TYPE
4944 && complain && warn_pointer_arith)
4945 pedwarn (loc, OPT_Wpointer_arith,
4946 "invalid application of %qs to a void type", op_name);
4947 else if (!complain)
4948 return error_mark_node;
4949 value = size_one_node;
4951 else if (!COMPLETE_TYPE_P (type)
4952 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
4954 if (complain)
4955 error_at (loc, "invalid application of %qs to incomplete type %qT",
4956 op_name, type);
4957 return error_mark_node;
4959 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4960 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4962 if (complain)
4963 error_at (loc, "invalid application of %qs to array type %qT of "
4964 "incomplete element type", op_name, type);
4965 return error_mark_node;
4967 else
4969 if (is_sizeof)
4970 /* Convert in case a char is more than one unit. */
4971 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4972 size_int (TYPE_PRECISION (char_type_node)
4973 / BITS_PER_UNIT));
4974 else if (min_alignof)
4975 value = size_int (min_align_of_type (type));
4976 else
4977 value = size_int (TYPE_ALIGN_UNIT (type));
4980 /* VALUE will have the middle-end integer type sizetype.
4981 However, we should really return a value of type `size_t',
4982 which is just a typedef for an ordinary integer type. */
4983 value = fold_convert_loc (loc, size_type_node, value);
4985 return value;
4988 /* Implement the __alignof keyword: Return the minimum required
4989 alignment of EXPR, measured in bytes. For VAR_DECLs,
4990 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4991 from an "aligned" __attribute__ specification). LOC is the
4992 location of the ALIGNOF operator. */
4994 tree
4995 c_alignof_expr (location_t loc, tree expr)
4997 tree t;
4999 if (VAR_OR_FUNCTION_DECL_P (expr))
5000 t = size_int (DECL_ALIGN_UNIT (expr));
5002 else if (TREE_CODE (expr) == COMPONENT_REF
5003 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5005 error_at (loc, "%<__alignof%> applied to a bit-field");
5006 t = size_one_node;
5008 else if (TREE_CODE (expr) == COMPONENT_REF
5009 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
5010 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
5012 else if (INDIRECT_REF_P (expr))
5014 tree t = TREE_OPERAND (expr, 0);
5015 tree best = t;
5016 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5018 while (CONVERT_EXPR_P (t)
5019 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
5021 int thisalign;
5023 t = TREE_OPERAND (t, 0);
5024 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5025 if (thisalign > bestalign)
5026 best = t, bestalign = thisalign;
5028 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
5030 else
5031 return c_alignof (loc, TREE_TYPE (expr));
5033 return fold_convert_loc (loc, size_type_node, t);
5036 /* Handle C and C++ default attributes. */
5038 enum built_in_attribute
5040 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5041 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
5042 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
5043 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5044 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
5045 #include "builtin-attrs.def"
5046 #undef DEF_ATTR_NULL_TREE
5047 #undef DEF_ATTR_INT
5048 #undef DEF_ATTR_STRING
5049 #undef DEF_ATTR_IDENT
5050 #undef DEF_ATTR_TREE_LIST
5051 ATTR_LAST
5054 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5056 static void c_init_attributes (void);
5058 enum c_builtin_type
5060 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5061 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5062 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5063 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5064 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5065 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5066 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
5067 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5068 ARG6) NAME,
5069 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5070 ARG6, ARG7) NAME,
5071 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5072 ARG6, ARG7, ARG8) NAME,
5073 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5074 ARG6, ARG7, ARG8, ARG9) NAME,
5075 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5076 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5077 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5078 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
5079 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5080 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5081 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
5082 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5083 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
5084 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5085 NAME,
5086 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5087 ARG6) NAME,
5088 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5089 ARG6, ARG7) NAME,
5090 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5091 #include "builtin-types.def"
5092 #undef DEF_PRIMITIVE_TYPE
5093 #undef DEF_FUNCTION_TYPE_0
5094 #undef DEF_FUNCTION_TYPE_1
5095 #undef DEF_FUNCTION_TYPE_2
5096 #undef DEF_FUNCTION_TYPE_3
5097 #undef DEF_FUNCTION_TYPE_4
5098 #undef DEF_FUNCTION_TYPE_5
5099 #undef DEF_FUNCTION_TYPE_6
5100 #undef DEF_FUNCTION_TYPE_7
5101 #undef DEF_FUNCTION_TYPE_8
5102 #undef DEF_FUNCTION_TYPE_9
5103 #undef DEF_FUNCTION_TYPE_10
5104 #undef DEF_FUNCTION_TYPE_11
5105 #undef DEF_FUNCTION_TYPE_VAR_0
5106 #undef DEF_FUNCTION_TYPE_VAR_1
5107 #undef DEF_FUNCTION_TYPE_VAR_2
5108 #undef DEF_FUNCTION_TYPE_VAR_3
5109 #undef DEF_FUNCTION_TYPE_VAR_4
5110 #undef DEF_FUNCTION_TYPE_VAR_5
5111 #undef DEF_FUNCTION_TYPE_VAR_6
5112 #undef DEF_FUNCTION_TYPE_VAR_7
5113 #undef DEF_POINTER_TYPE
5114 BT_LAST
5117 typedef enum c_builtin_type builtin_type;
5119 /* A temporary array for c_common_nodes_and_builtins. Used in
5120 communication with def_fn_type. */
5121 static tree builtin_types[(int) BT_LAST + 1];
5123 /* A helper function for c_common_nodes_and_builtins. Build function type
5124 for DEF with return type RET and N arguments. If VAR is true, then the
5125 function should be variadic after those N arguments.
5127 Takes special care not to ICE if any of the types involved are
5128 error_mark_node, which indicates that said type is not in fact available
5129 (see builtin_type_for_size). In which case the function type as a whole
5130 should be error_mark_node. */
5132 static void
5133 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5135 tree t;
5136 tree *args = XALLOCAVEC (tree, n);
5137 va_list list;
5138 int i;
5140 va_start (list, n);
5141 for (i = 0; i < n; ++i)
5143 builtin_type a = (builtin_type) va_arg (list, int);
5144 t = builtin_types[a];
5145 if (t == error_mark_node)
5146 goto egress;
5147 args[i] = t;
5150 t = builtin_types[ret];
5151 if (t == error_mark_node)
5152 goto egress;
5153 if (var)
5154 t = build_varargs_function_type_array (t, n, args);
5155 else
5156 t = build_function_type_array (t, n, args);
5158 egress:
5159 builtin_types[def] = t;
5160 va_end (list);
5163 /* Build builtin functions common to both C and C++ language
5164 frontends. */
5166 static void
5167 c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5169 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5170 builtin_types[ENUM] = VALUE;
5171 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5172 def_fn_type (ENUM, RETURN, 0, 0);
5173 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5174 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5175 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5176 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5177 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5178 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5179 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5180 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5181 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5182 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5183 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5184 ARG6) \
5185 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5186 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5187 ARG6, ARG7) \
5188 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5189 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5190 ARG6, ARG7, ARG8) \
5191 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5192 ARG7, ARG8);
5193 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5194 ARG6, ARG7, ARG8, ARG9) \
5195 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5196 ARG7, ARG8, ARG9);
5197 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5198 ARG6, ARG7, ARG8, ARG9, ARG10) \
5199 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5200 ARG7, ARG8, ARG9, ARG10);
5201 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5202 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5203 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5204 ARG7, ARG8, ARG9, ARG10, ARG11);
5205 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5206 def_fn_type (ENUM, RETURN, 1, 0);
5207 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5208 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5209 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5210 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5211 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5212 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5213 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5214 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5215 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5216 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5217 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5218 ARG6) \
5219 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5220 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5221 ARG6, ARG7) \
5222 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5223 #define DEF_POINTER_TYPE(ENUM, TYPE) \
5224 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5226 #include "builtin-types.def"
5228 #undef DEF_PRIMITIVE_TYPE
5229 #undef DEF_FUNCTION_TYPE_0
5230 #undef DEF_FUNCTION_TYPE_1
5231 #undef DEF_FUNCTION_TYPE_2
5232 #undef DEF_FUNCTION_TYPE_3
5233 #undef DEF_FUNCTION_TYPE_4
5234 #undef DEF_FUNCTION_TYPE_5
5235 #undef DEF_FUNCTION_TYPE_6
5236 #undef DEF_FUNCTION_TYPE_7
5237 #undef DEF_FUNCTION_TYPE_8
5238 #undef DEF_FUNCTION_TYPE_9
5239 #undef DEF_FUNCTION_TYPE_10
5240 #undef DEF_FUNCTION_TYPE_11
5241 #undef DEF_FUNCTION_TYPE_VAR_0
5242 #undef DEF_FUNCTION_TYPE_VAR_1
5243 #undef DEF_FUNCTION_TYPE_VAR_2
5244 #undef DEF_FUNCTION_TYPE_VAR_3
5245 #undef DEF_FUNCTION_TYPE_VAR_4
5246 #undef DEF_FUNCTION_TYPE_VAR_5
5247 #undef DEF_FUNCTION_TYPE_VAR_6
5248 #undef DEF_FUNCTION_TYPE_VAR_7
5249 #undef DEF_POINTER_TYPE
5250 builtin_types[(int) BT_LAST] = NULL_TREE;
5252 c_init_attributes ();
5254 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5255 NONANSI_P, ATTRS, IMPLICIT, COND) \
5256 if (NAME && COND) \
5257 def_builtin_1 (ENUM, NAME, CLASS, \
5258 builtin_types[(int) TYPE], \
5259 builtin_types[(int) LIBTYPE], \
5260 BOTH_P, FALLBACK_P, NONANSI_P, \
5261 built_in_attributes[(int) ATTRS], IMPLICIT);
5262 #include "builtins.def"
5264 targetm.init_builtins ();
5266 build_common_builtin_nodes ();
5268 if (flag_cilkplus)
5269 cilk_init_builtins ();
5272 /* Like get_identifier, but avoid warnings about null arguments when
5273 the argument may be NULL for targets where GCC lacks stdint.h type
5274 information. */
5276 static inline tree
5277 c_get_ident (const char *id)
5279 return get_identifier (id);
5282 /* Build tree nodes and builtin functions common to both C and C++ language
5283 frontends. */
5285 void
5286 c_common_nodes_and_builtins (void)
5288 int char16_type_size;
5289 int char32_type_size;
5290 int wchar_type_size;
5291 tree array_domain_type;
5292 tree va_list_ref_type_node;
5293 tree va_list_arg_type_node;
5294 int i;
5296 build_common_tree_nodes (flag_signed_char);
5298 /* Define `int' and `char' first so that dbx will output them first. */
5299 record_builtin_type (RID_INT, NULL, integer_type_node);
5300 record_builtin_type (RID_CHAR, "char", char_type_node);
5302 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5303 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5304 but not C. Are the conditionals here needed? */
5305 if (c_dialect_cxx ())
5306 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
5307 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5308 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5309 record_builtin_type (RID_MAX, "long unsigned int",
5310 long_unsigned_type_node);
5312 for (i = 0; i < NUM_INT_N_ENTS; i ++)
5314 char name[25];
5316 sprintf (name, "__int%d", int_n_data[i].bitsize);
5317 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
5318 int_n_trees[i].signed_type);
5319 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
5320 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
5323 if (c_dialect_cxx ())
5324 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5325 record_builtin_type (RID_MAX, "long long int",
5326 long_long_integer_type_node);
5327 record_builtin_type (RID_MAX, "long long unsigned int",
5328 long_long_unsigned_type_node);
5329 if (c_dialect_cxx ())
5330 record_builtin_type (RID_MAX, "long long unsigned",
5331 long_long_unsigned_type_node);
5332 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5333 record_builtin_type (RID_MAX, "short unsigned int",
5334 short_unsigned_type_node);
5335 if (c_dialect_cxx ())
5336 record_builtin_type (RID_MAX, "unsigned short",
5337 short_unsigned_type_node);
5339 /* Define both `signed char' and `unsigned char'. */
5340 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5341 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5343 /* These are types that c_common_type_for_size and
5344 c_common_type_for_mode use. */
5345 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5346 TYPE_DECL, NULL_TREE,
5347 intQI_type_node));
5348 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5349 TYPE_DECL, NULL_TREE,
5350 intHI_type_node));
5351 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5352 TYPE_DECL, NULL_TREE,
5353 intSI_type_node));
5354 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5355 TYPE_DECL, NULL_TREE,
5356 intDI_type_node));
5357 #if HOST_BITS_PER_WIDE_INT >= 64
5358 /* Note that this is different than the __int128 type that's part of
5359 the generic __intN support. */
5360 if (targetm.scalar_mode_supported_p (TImode))
5361 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5362 TYPE_DECL,
5363 get_identifier ("__int128_t"),
5364 intTI_type_node));
5365 #endif
5366 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5367 TYPE_DECL, NULL_TREE,
5368 unsigned_intQI_type_node));
5369 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5370 TYPE_DECL, NULL_TREE,
5371 unsigned_intHI_type_node));
5372 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5373 TYPE_DECL, NULL_TREE,
5374 unsigned_intSI_type_node));
5375 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5376 TYPE_DECL, NULL_TREE,
5377 unsigned_intDI_type_node));
5378 #if HOST_BITS_PER_WIDE_INT >= 64
5379 if (targetm.scalar_mode_supported_p (TImode))
5380 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5381 TYPE_DECL,
5382 get_identifier ("__uint128_t"),
5383 unsigned_intTI_type_node));
5384 #endif
5386 /* Create the widest literal types. */
5387 widest_integer_literal_type_node
5388 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
5389 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5390 TYPE_DECL, NULL_TREE,
5391 widest_integer_literal_type_node));
5393 widest_unsigned_literal_type_node
5394 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
5395 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5396 TYPE_DECL, NULL_TREE,
5397 widest_unsigned_literal_type_node));
5399 signed_size_type_node = c_common_signed_type (size_type_node);
5401 pid_type_node =
5402 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5404 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5405 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
5406 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5408 /* Only supported decimal floating point extension if the target
5409 actually supports underlying modes. */
5410 if (targetm.scalar_mode_supported_p (SDmode)
5411 && targetm.scalar_mode_supported_p (DDmode)
5412 && targetm.scalar_mode_supported_p (TDmode))
5414 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5415 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5416 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5419 if (targetm.fixed_point_supported_p ())
5421 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5422 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5423 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5424 record_builtin_type (RID_MAX, "long long _Fract",
5425 long_long_fract_type_node);
5426 record_builtin_type (RID_MAX, "unsigned short _Fract",
5427 unsigned_short_fract_type_node);
5428 record_builtin_type (RID_MAX, "unsigned _Fract",
5429 unsigned_fract_type_node);
5430 record_builtin_type (RID_MAX, "unsigned long _Fract",
5431 unsigned_long_fract_type_node);
5432 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5433 unsigned_long_long_fract_type_node);
5434 record_builtin_type (RID_MAX, "_Sat short _Fract",
5435 sat_short_fract_type_node);
5436 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5437 record_builtin_type (RID_MAX, "_Sat long _Fract",
5438 sat_long_fract_type_node);
5439 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5440 sat_long_long_fract_type_node);
5441 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5442 sat_unsigned_short_fract_type_node);
5443 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5444 sat_unsigned_fract_type_node);
5445 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5446 sat_unsigned_long_fract_type_node);
5447 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5448 sat_unsigned_long_long_fract_type_node);
5449 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5450 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5451 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5452 record_builtin_type (RID_MAX, "long long _Accum",
5453 long_long_accum_type_node);
5454 record_builtin_type (RID_MAX, "unsigned short _Accum",
5455 unsigned_short_accum_type_node);
5456 record_builtin_type (RID_MAX, "unsigned _Accum",
5457 unsigned_accum_type_node);
5458 record_builtin_type (RID_MAX, "unsigned long _Accum",
5459 unsigned_long_accum_type_node);
5460 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5461 unsigned_long_long_accum_type_node);
5462 record_builtin_type (RID_MAX, "_Sat short _Accum",
5463 sat_short_accum_type_node);
5464 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5465 record_builtin_type (RID_MAX, "_Sat long _Accum",
5466 sat_long_accum_type_node);
5467 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5468 sat_long_long_accum_type_node);
5469 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5470 sat_unsigned_short_accum_type_node);
5471 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5472 sat_unsigned_accum_type_node);
5473 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5474 sat_unsigned_long_accum_type_node);
5475 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5476 sat_unsigned_long_long_accum_type_node);
5480 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5481 TYPE_DECL,
5482 get_identifier ("complex int"),
5483 complex_integer_type_node));
5484 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5485 TYPE_DECL,
5486 get_identifier ("complex float"),
5487 complex_float_type_node));
5488 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5489 TYPE_DECL,
5490 get_identifier ("complex double"),
5491 complex_double_type_node));
5492 lang_hooks.decls.pushdecl
5493 (build_decl (UNKNOWN_LOCATION,
5494 TYPE_DECL, get_identifier ("complex long double"),
5495 complex_long_double_type_node));
5497 if (c_dialect_cxx ())
5498 /* For C++, make fileptr_type_node a distinct void * type until
5499 FILE type is defined. */
5500 fileptr_type_node = build_variant_type_copy (ptr_type_node);
5502 record_builtin_type (RID_VOID, NULL, void_type_node);
5504 /* Set the TYPE_NAME for any variants that were built before
5505 record_builtin_type gave names to the built-in types. */
5507 tree void_name = TYPE_NAME (void_type_node);
5508 TYPE_NAME (void_type_node) = NULL_TREE;
5509 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5510 = void_name;
5511 TYPE_NAME (void_type_node) = void_name;
5514 void_list_node = build_void_list_node ();
5516 /* Make a type to be the domain of a few array types
5517 whose domains don't really matter.
5518 200 is small enough that it always fits in size_t
5519 and large enough that it can hold most function names for the
5520 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5521 array_domain_type = build_index_type (size_int (200));
5523 /* Make a type for arrays of characters.
5524 With luck nothing will ever really depend on the length of this
5525 array type. */
5526 char_array_type_node
5527 = build_array_type (char_type_node, array_domain_type);
5529 string_type_node = build_pointer_type (char_type_node);
5530 const_string_type_node
5531 = build_pointer_type (build_qualified_type
5532 (char_type_node, TYPE_QUAL_CONST));
5534 /* This is special for C++ so functions can be overloaded. */
5535 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
5536 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5537 wchar_type_size = TYPE_PRECISION (wchar_type_node);
5538 underlying_wchar_type_node = wchar_type_node;
5539 if (c_dialect_cxx ())
5541 if (TYPE_UNSIGNED (wchar_type_node))
5542 wchar_type_node = make_unsigned_type (wchar_type_size);
5543 else
5544 wchar_type_node = make_signed_type (wchar_type_size);
5545 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5548 /* This is for wide string constants. */
5549 wchar_array_type_node
5550 = build_array_type (wchar_type_node, array_domain_type);
5552 /* Define 'char16_t'. */
5553 char16_type_node = get_identifier (CHAR16_TYPE);
5554 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5555 char16_type_size = TYPE_PRECISION (char16_type_node);
5556 if (c_dialect_cxx ())
5558 char16_type_node = make_unsigned_type (char16_type_size);
5560 if (cxx_dialect >= cxx11)
5561 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5564 /* This is for UTF-16 string constants. */
5565 char16_array_type_node
5566 = build_array_type (char16_type_node, array_domain_type);
5568 /* Define 'char32_t'. */
5569 char32_type_node = get_identifier (CHAR32_TYPE);
5570 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5571 char32_type_size = TYPE_PRECISION (char32_type_node);
5572 if (c_dialect_cxx ())
5574 char32_type_node = make_unsigned_type (char32_type_size);
5576 if (cxx_dialect >= cxx11)
5577 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5580 /* This is for UTF-32 string constants. */
5581 char32_array_type_node
5582 = build_array_type (char32_type_node, array_domain_type);
5584 wint_type_node =
5585 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5587 intmax_type_node =
5588 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5589 uintmax_type_node =
5590 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5592 if (SIG_ATOMIC_TYPE)
5593 sig_atomic_type_node =
5594 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
5595 if (INT8_TYPE)
5596 int8_type_node =
5597 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
5598 if (INT16_TYPE)
5599 int16_type_node =
5600 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
5601 if (INT32_TYPE)
5602 int32_type_node =
5603 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
5604 if (INT64_TYPE)
5605 int64_type_node =
5606 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
5607 if (UINT8_TYPE)
5608 uint8_type_node =
5609 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
5610 if (UINT16_TYPE)
5611 c_uint16_type_node = uint16_type_node =
5612 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
5613 if (UINT32_TYPE)
5614 c_uint32_type_node = uint32_type_node =
5615 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
5616 if (UINT64_TYPE)
5617 c_uint64_type_node = uint64_type_node =
5618 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
5619 if (INT_LEAST8_TYPE)
5620 int_least8_type_node =
5621 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
5622 if (INT_LEAST16_TYPE)
5623 int_least16_type_node =
5624 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
5625 if (INT_LEAST32_TYPE)
5626 int_least32_type_node =
5627 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
5628 if (INT_LEAST64_TYPE)
5629 int_least64_type_node =
5630 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
5631 if (UINT_LEAST8_TYPE)
5632 uint_least8_type_node =
5633 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
5634 if (UINT_LEAST16_TYPE)
5635 uint_least16_type_node =
5636 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
5637 if (UINT_LEAST32_TYPE)
5638 uint_least32_type_node =
5639 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
5640 if (UINT_LEAST64_TYPE)
5641 uint_least64_type_node =
5642 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
5643 if (INT_FAST8_TYPE)
5644 int_fast8_type_node =
5645 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
5646 if (INT_FAST16_TYPE)
5647 int_fast16_type_node =
5648 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
5649 if (INT_FAST32_TYPE)
5650 int_fast32_type_node =
5651 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
5652 if (INT_FAST64_TYPE)
5653 int_fast64_type_node =
5654 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
5655 if (UINT_FAST8_TYPE)
5656 uint_fast8_type_node =
5657 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
5658 if (UINT_FAST16_TYPE)
5659 uint_fast16_type_node =
5660 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
5661 if (UINT_FAST32_TYPE)
5662 uint_fast32_type_node =
5663 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
5664 if (UINT_FAST64_TYPE)
5665 uint_fast64_type_node =
5666 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
5667 if (INTPTR_TYPE)
5668 intptr_type_node =
5669 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
5670 if (UINTPTR_TYPE)
5671 uintptr_type_node =
5672 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
5674 default_function_type
5675 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5676 ptrdiff_type_node
5677 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
5678 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5680 lang_hooks.decls.pushdecl
5681 (build_decl (UNKNOWN_LOCATION,
5682 TYPE_DECL, get_identifier ("__builtin_va_list"),
5683 va_list_type_node));
5684 if (targetm.enum_va_list_p)
5686 int l;
5687 const char *pname;
5688 tree ptype;
5690 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
5692 lang_hooks.decls.pushdecl
5693 (build_decl (UNKNOWN_LOCATION,
5694 TYPE_DECL, get_identifier (pname),
5695 ptype));
5700 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
5702 va_list_arg_type_node = va_list_ref_type_node =
5703 build_pointer_type (TREE_TYPE (va_list_type_node));
5705 else
5707 va_list_arg_type_node = va_list_type_node;
5708 va_list_ref_type_node = build_reference_type (va_list_type_node);
5711 if (!flag_preprocess_only)
5712 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
5714 main_identifier_node = get_identifier ("main");
5716 /* Create the built-in __null node. It is important that this is
5717 not shared. */
5718 null_node = make_int_cst (1, 1);
5719 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
5721 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5722 memset (builtin_types, 0, sizeof (builtin_types));
5725 /* The number of named compound-literals generated thus far. */
5726 static GTY(()) int compound_literal_number;
5728 /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5730 void
5731 set_compound_literal_name (tree decl)
5733 char *name;
5734 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5735 compound_literal_number);
5736 compound_literal_number++;
5737 DECL_NAME (decl) = get_identifier (name);
5740 /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5741 TYPE and operand OP. */
5743 static tree
5744 build_va_arg_1 (location_t loc, tree type, tree op)
5746 tree expr = build1 (VA_ARG_EXPR, type, op);
5747 SET_EXPR_LOCATION (expr, loc);
5748 return expr;
5751 /* Return a VA_ARG_EXPR corresponding to a source-level expression
5752 va_arg (EXPR, TYPE) at source location LOC. */
5754 tree
5755 build_va_arg (location_t loc, tree expr, tree type)
5757 tree va_type = TREE_TYPE (expr);
5758 tree canon_va_type = (va_type == error_mark_node
5759 ? NULL_TREE
5760 : targetm.canonical_va_list_type (va_type));
5762 if (va_type == error_mark_node
5763 || canon_va_type == NULL_TREE)
5765 /* Let's handle things neutrallly, if expr:
5766 - has undeclared type, or
5767 - is not an va_list type. */
5768 return build_va_arg_1 (loc, type, expr);
5771 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5773 /* Case 1: Not an array type. */
5775 /* Take the address, to get '&ap'. */
5776 mark_addressable (expr);
5777 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5779 /* Verify that &ap is still recognized as having va_list type. */
5780 tree canon_expr_type
5781 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5782 if (canon_expr_type == NULL_TREE)
5784 error_at (loc,
5785 "first argument to %<va_arg%> not of type %<va_list%>");
5786 return error_mark_node;
5789 return build_va_arg_1 (loc, type, expr);
5792 /* Case 2: Array type.
5794 Background:
5796 For contrast, let's start with the simple case (case 1). If
5797 canon_va_type is not an array type, but say a char *, then when
5798 passing-by-value a va_list, the type of the va_list param decl is
5799 the same as for another va_list decl (all ap's are char *):
5801 f2_1 (char * ap)
5802 D.1815 = VA_ARG (&ap, 0B, 1);
5803 return D.1815;
5805 f2 (int i)
5806 char * ap.0;
5807 char * ap;
5808 __builtin_va_start (&ap, 0);
5809 ap.0 = ap;
5810 res = f2_1 (ap.0);
5811 __builtin_va_end (&ap);
5812 D.1812 = res;
5813 return D.1812;
5815 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5816 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5817 the same as for another va_list decl (case 2a, struct ap[1]).
5819 f2_1 (struct * ap)
5820 D.1844 = VA_ARG (ap, 0B, 0);
5821 return D.1844;
5823 f2 (int i)
5824 struct ap[1];
5825 __builtin_va_start (&ap, 0);
5826 res = f2_1 (&ap);
5827 __builtin_va_end (&ap);
5828 D.1841 = res;
5829 return D.1841;
5831 Case 2b is different because:
5832 - on the callee side, the parm decl has declared type va_list, but
5833 grokdeclarator changes the type of the parm decl to a pointer to the
5834 array elem type.
5835 - on the caller side, the pass-by-value uses &ap.
5837 We unify these two cases (case 2a: va_list is array type,
5838 case 2b: va_list is pointer to array elem type), by adding '&' for the
5839 array type case, such that we have a pointer to array elem in both
5840 cases. */
5842 if (TREE_CODE (va_type) == ARRAY_TYPE)
5844 /* Case 2a: va_list is array type. */
5846 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5847 elem type. */
5848 mark_addressable (expr);
5849 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5850 expr);
5852 /* Verify that &ap is still recognized as having va_list type. */
5853 tree canon_expr_type
5854 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5855 if (canon_expr_type == NULL_TREE)
5857 error_at (loc,
5858 "first argument to %<va_arg%> not of type %<va_list%>");
5859 return error_mark_node;
5862 else
5864 /* Case 2b: va_list is pointer to array elem type. */
5865 gcc_assert (POINTER_TYPE_P (va_type));
5866 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
5868 /* Don't take the address. We've already got '&ap'. */
5872 return build_va_arg_1 (loc, type, expr);
5876 /* Linked list of disabled built-in functions. */
5878 struct disabled_builtin
5880 const char *name;
5881 struct disabled_builtin *next;
5883 static disabled_builtin *disabled_builtins = NULL;
5885 static bool builtin_function_disabled_p (const char *);
5887 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5888 begins with "__builtin_", give an error. */
5890 void
5891 disable_builtin_function (const char *name)
5893 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
5894 error ("cannot disable built-in function %qs", name);
5895 else
5897 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5898 new_disabled_builtin->name = name;
5899 new_disabled_builtin->next = disabled_builtins;
5900 disabled_builtins = new_disabled_builtin;
5905 /* Return true if the built-in function NAME has been disabled, false
5906 otherwise. */
5908 static bool
5909 builtin_function_disabled_p (const char *name)
5911 disabled_builtin *p;
5912 for (p = disabled_builtins; p != NULL; p = p->next)
5914 if (strcmp (name, p->name) == 0)
5915 return true;
5917 return false;
5921 /* Worker for DEF_BUILTIN.
5922 Possibly define a builtin function with one or two names.
5923 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5924 nonansi_p and flag_no_nonansi_builtin. */
5926 static void
5927 def_builtin_1 (enum built_in_function fncode,
5928 const char *name,
5929 enum built_in_class fnclass,
5930 tree fntype, tree libtype,
5931 bool both_p, bool fallback_p, bool nonansi_p,
5932 tree fnattrs, bool implicit_p)
5934 tree decl;
5935 const char *libname;
5937 if (fntype == error_mark_node)
5938 return;
5940 gcc_assert ((!both_p && !fallback_p)
5941 || !strncmp (name, "__builtin_",
5942 strlen ("__builtin_")));
5944 libname = name + strlen ("__builtin_");
5945 decl = add_builtin_function (name, fntype, fncode, fnclass,
5946 (fallback_p ? libname : NULL),
5947 fnattrs);
5949 set_builtin_decl (fncode, decl, implicit_p);
5951 if (both_p
5952 && !flag_no_builtin && !builtin_function_disabled_p (libname)
5953 && !(nonansi_p && flag_no_nonansi_builtin))
5954 add_builtin_function (libname, libtype, fncode, fnclass,
5955 NULL, fnattrs);
5958 /* Nonzero if the type T promotes to int. This is (nearly) the
5959 integral promotions defined in ISO C99 6.3.1.1/2. */
5961 bool
5962 c_promoting_integer_type_p (const_tree t)
5964 switch (TREE_CODE (t))
5966 case INTEGER_TYPE:
5967 return (TYPE_MAIN_VARIANT (t) == char_type_node
5968 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5969 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5970 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5971 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5972 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5974 case ENUMERAL_TYPE:
5975 /* ??? Technically all enumerations not larger than an int
5976 promote to an int. But this is used along code paths
5977 that only want to notice a size change. */
5978 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5980 case BOOLEAN_TYPE:
5981 return 1;
5983 default:
5984 return 0;
5988 /* Return 1 if PARMS specifies a fixed number of parameters
5989 and none of their types is affected by default promotions. */
5992 self_promoting_args_p (const_tree parms)
5994 const_tree t;
5995 for (t = parms; t; t = TREE_CHAIN (t))
5997 tree type = TREE_VALUE (t);
5999 if (type == error_mark_node)
6000 continue;
6002 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6003 return 0;
6005 if (type == 0)
6006 return 0;
6008 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6009 return 0;
6011 if (c_promoting_integer_type_p (type))
6012 return 0;
6014 return 1;
6017 /* Recursively remove any '*' or '&' operator from TYPE. */
6018 tree
6019 strip_pointer_operator (tree t)
6021 while (POINTER_TYPE_P (t))
6022 t = TREE_TYPE (t);
6023 return t;
6026 /* Recursively remove pointer or array type from TYPE. */
6027 tree
6028 strip_pointer_or_array_types (tree t)
6030 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6031 t = TREE_TYPE (t);
6032 return t;
6035 /* Used to compare case labels. K1 and K2 are actually tree nodes
6036 representing case labels, or NULL_TREE for a `default' label.
6037 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6038 K2, and 0 if K1 and K2 are equal. */
6041 case_compare (splay_tree_key k1, splay_tree_key k2)
6043 /* Consider a NULL key (such as arises with a `default' label) to be
6044 smaller than anything else. */
6045 if (!k1)
6046 return k2 ? -1 : 0;
6047 else if (!k2)
6048 return k1 ? 1 : 0;
6050 return tree_int_cst_compare ((tree) k1, (tree) k2);
6053 /* Process a case label, located at LOC, for the range LOW_VALUE
6054 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6055 then this case label is actually a `default' label. If only
6056 HIGH_VALUE is NULL_TREE, then case label was declared using the
6057 usual C/C++ syntax, rather than the GNU case range extension.
6058 CASES is a tree containing all the case ranges processed so far;
6059 COND is the condition for the switch-statement itself.
6060 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6061 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6062 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
6064 tree
6065 c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
6066 tree low_value, tree high_value, bool *outside_range_p)
6068 tree type;
6069 tree label;
6070 tree case_label;
6071 splay_tree_node node;
6073 /* Create the LABEL_DECL itself. */
6074 label = create_artificial_label (loc);
6076 /* If there was an error processing the switch condition, bail now
6077 before we get more confused. */
6078 if (!cond || cond == error_mark_node)
6079 goto error_out;
6081 if ((low_value && TREE_TYPE (low_value)
6082 && POINTER_TYPE_P (TREE_TYPE (low_value)))
6083 || (high_value && TREE_TYPE (high_value)
6084 && POINTER_TYPE_P (TREE_TYPE (high_value))))
6086 error_at (loc, "pointers are not permitted as case values");
6087 goto error_out;
6090 /* Case ranges are a GNU extension. */
6091 if (high_value)
6092 pedwarn (loc, OPT_Wpedantic,
6093 "range expressions in switch statements are non-standard");
6095 type = TREE_TYPE (cond);
6096 if (low_value)
6098 low_value = check_case_value (loc, low_value);
6099 low_value = convert_and_check (loc, type, low_value);
6100 if (low_value == error_mark_node)
6101 goto error_out;
6103 if (high_value)
6105 high_value = check_case_value (loc, high_value);
6106 high_value = convert_and_check (loc, type, high_value);
6107 if (high_value == error_mark_node)
6108 goto error_out;
6111 if (low_value && high_value)
6113 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
6114 really a case range, even though it was written that way.
6115 Remove the HIGH_VALUE to simplify later processing. */
6116 if (tree_int_cst_equal (low_value, high_value))
6117 high_value = NULL_TREE;
6118 else if (!tree_int_cst_lt (low_value, high_value))
6119 warning_at (loc, 0, "empty range specified");
6122 /* See if the case is in range of the type of the original testing
6123 expression. If both low_value and high_value are out of range,
6124 don't insert the case label and return NULL_TREE. */
6125 if (low_value
6126 && !check_case_bounds (loc, type, orig_type,
6127 &low_value, high_value ? &high_value : NULL,
6128 outside_range_p))
6129 return NULL_TREE;
6131 /* Look up the LOW_VALUE in the table of case labels we already
6132 have. */
6133 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6134 /* If there was not an exact match, check for overlapping ranges.
6135 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6136 that's a `default' label and the only overlap is an exact match. */
6137 if (!node && (low_value || high_value))
6139 splay_tree_node low_bound;
6140 splay_tree_node high_bound;
6142 /* Even though there wasn't an exact match, there might be an
6143 overlap between this case range and another case range.
6144 Since we've (inductively) not allowed any overlapping case
6145 ranges, we simply need to find the greatest low case label
6146 that is smaller that LOW_VALUE, and the smallest low case
6147 label that is greater than LOW_VALUE. If there is an overlap
6148 it will occur in one of these two ranges. */
6149 low_bound = splay_tree_predecessor (cases,
6150 (splay_tree_key) low_value);
6151 high_bound = splay_tree_successor (cases,
6152 (splay_tree_key) low_value);
6154 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6155 the LOW_VALUE, so there is no need to check unless the
6156 LOW_BOUND is in fact itself a case range. */
6157 if (low_bound
6158 && CASE_HIGH ((tree) low_bound->value)
6159 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6160 low_value) >= 0)
6161 node = low_bound;
6162 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6163 range is bigger than the low end of the current range, so we
6164 are only interested if the current range is a real range, and
6165 not an ordinary case label. */
6166 else if (high_bound
6167 && high_value
6168 && (tree_int_cst_compare ((tree) high_bound->key,
6169 high_value)
6170 <= 0))
6171 node = high_bound;
6173 /* If there was an overlap, issue an error. */
6174 if (node)
6176 tree duplicate = CASE_LABEL ((tree) node->value);
6178 if (high_value)
6180 error_at (loc, "duplicate (or overlapping) case value");
6181 error_at (DECL_SOURCE_LOCATION (duplicate),
6182 "this is the first entry overlapping that value");
6184 else if (low_value)
6186 error_at (loc, "duplicate case value") ;
6187 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
6189 else
6191 error_at (loc, "multiple default labels in one switch");
6192 error_at (DECL_SOURCE_LOCATION (duplicate),
6193 "this is the first default label");
6195 goto error_out;
6198 /* Add a CASE_LABEL to the statement-tree. */
6199 case_label = add_stmt (build_case_label (low_value, high_value, label));
6200 /* Register this case label in the splay tree. */
6201 splay_tree_insert (cases,
6202 (splay_tree_key) low_value,
6203 (splay_tree_value) case_label);
6205 return case_label;
6207 error_out:
6208 /* Add a label so that the back-end doesn't think that the beginning of
6209 the switch is unreachable. Note that we do not add a case label, as
6210 that just leads to duplicates and thence to failure later on. */
6211 if (!cases->root)
6213 tree t = create_artificial_label (loc);
6214 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6216 return error_mark_node;
6219 /* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6220 Used to verify that case values match up with enumerator values. */
6222 static void
6223 match_case_to_enum_1 (tree key, tree type, tree label)
6225 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6227 if (tree_fits_uhwi_p (key))
6228 print_dec (key, buf, UNSIGNED);
6229 else if (tree_fits_shwi_p (key))
6230 print_dec (key, buf, SIGNED);
6231 else
6232 print_hex (key, buf);
6234 if (TYPE_NAME (type) == 0)
6235 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6236 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6237 "case value %qs not in enumerated type",
6238 buf);
6239 else
6240 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6241 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6242 "case value %qs not in enumerated type %qT",
6243 buf, type);
6246 /* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6247 Used to verify that case values match up with enumerator values. */
6249 static int
6250 match_case_to_enum (splay_tree_node node, void *data)
6252 tree label = (tree) node->value;
6253 tree type = (tree) data;
6255 /* Skip default case. */
6256 if (!CASE_LOW (label))
6257 return 0;
6259 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6260 when we did our enum->case scan. Reset our scratch bit after. */
6261 if (!CASE_LOW_SEEN (label))
6262 match_case_to_enum_1 (CASE_LOW (label), type, label);
6263 else
6264 CASE_LOW_SEEN (label) = 0;
6266 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6267 not set, that means that CASE_HIGH did not appear when we did our
6268 enum->case scan. Reset our scratch bit after. */
6269 if (CASE_HIGH (label))
6271 if (!CASE_HIGH_SEEN (label))
6272 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6273 else
6274 CASE_HIGH_SEEN (label) = 0;
6277 return 0;
6280 /* Handle -Wswitch*. Called from the front end after parsing the
6281 switch construct. */
6282 /* ??? Should probably be somewhere generic, since other languages
6283 besides C and C++ would want this. At the moment, however, C/C++
6284 are the only tree-ssa languages that support enumerations at all,
6285 so the point is moot. */
6287 void
6288 c_do_switch_warnings (splay_tree cases, location_t switch_location,
6289 tree type, tree cond, bool bool_cond_p,
6290 bool outside_range_p)
6292 splay_tree_node default_node;
6293 splay_tree_node node;
6294 tree chain;
6296 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6297 && !warn_switch_bool)
6298 return;
6300 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
6301 if (!default_node)
6302 warning_at (switch_location, OPT_Wswitch_default,
6303 "switch missing default case");
6305 /* There are certain cases where -Wswitch-bool warnings aren't
6306 desirable, such as
6307 switch (boolean)
6309 case true: ...
6310 case false: ...
6312 so be careful here. */
6313 if (warn_switch_bool && bool_cond_p)
6315 splay_tree_node min_node;
6316 /* If there's a default node, it's also the value with the minimal
6317 key. So look at the penultimate key (if any). */
6318 if (default_node)
6319 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6320 else
6321 min_node = splay_tree_min (cases);
6322 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6324 splay_tree_node max_node = splay_tree_max (cases);
6325 /* This might be a case range, so look at the value with the
6326 maximal key and then check CASE_HIGH. */
6327 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6328 if (max)
6329 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6331 /* If there's a case value > 1 or < 0, that is outside bool
6332 range, warn. */
6333 if (outside_range_p
6334 || (max && wi::gts_p (max, 1))
6335 || (min && wi::lts_p (min, 0))
6336 /* And handle the
6337 switch (boolean)
6339 case true: ...
6340 case false: ...
6341 default: ...
6343 case, where we want to warn. */
6344 || (default_node
6345 && max && wi::eq_p (max, 1)
6346 && min && wi::eq_p (min, 0)))
6347 warning_at (switch_location, OPT_Wswitch_bool,
6348 "switch condition has boolean value");
6351 /* From here on, we only care about enumerated types. */
6352 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6353 return;
6355 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6356 if (!warn_switch_enum && !warn_switch)
6357 return;
6359 /* Check the cases. Warn about case values which are not members of
6360 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6361 there is no default case, check that exactly all enumeration
6362 literals are covered by the cases. */
6364 /* Clearing COND if it is not an integer constant simplifies
6365 the tests inside the loop below. */
6366 if (TREE_CODE (cond) != INTEGER_CST)
6367 cond = NULL_TREE;
6369 /* The time complexity here is O(N*lg(N)) worst case, but for the
6370 common case of monotonically increasing enumerators, it is
6371 O(N), since the nature of the splay tree will keep the next
6372 element adjacent to the root at all times. */
6374 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6376 tree value = TREE_VALUE (chain);
6377 if (TREE_CODE (value) == CONST_DECL)
6378 value = DECL_INITIAL (value);
6379 node = splay_tree_lookup (cases, (splay_tree_key) value);
6380 if (node)
6382 /* Mark the CASE_LOW part of the case entry as seen. */
6383 tree label = (tree) node->value;
6384 CASE_LOW_SEEN (label) = 1;
6385 continue;
6388 /* Even though there wasn't an exact match, there might be a
6389 case range which includes the enumerator's value. */
6390 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6391 if (node && CASE_HIGH ((tree) node->value))
6393 tree label = (tree) node->value;
6394 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6395 if (cmp >= 0)
6397 /* If we match the upper bound exactly, mark the CASE_HIGH
6398 part of the case entry as seen. */
6399 if (cmp == 0)
6400 CASE_HIGH_SEEN (label) = 1;
6401 continue;
6405 /* We've now determined that this enumerated literal isn't
6406 handled by the case labels of the switch statement. */
6408 /* If the switch expression is a constant, we only really care
6409 about whether that constant is handled by the switch. */
6410 if (cond && tree_int_cst_compare (cond, value))
6411 continue;
6413 /* If there is a default_node, the only relevant option is
6414 Wswitch-enum. Otherwise, if both are enabled then we prefer
6415 to warn using -Wswitch because -Wswitch is enabled by -Wall
6416 while -Wswitch-enum is explicit. */
6417 warning_at (switch_location,
6418 (default_node || !warn_switch
6419 ? OPT_Wswitch_enum
6420 : OPT_Wswitch),
6421 "enumeration value %qE not handled in switch",
6422 TREE_PURPOSE (chain));
6425 /* Warn if there are case expressions that don't correspond to
6426 enumerators. This can occur since C and C++ don't enforce
6427 type-checking of assignments to enumeration variables.
6429 The time complexity here is now always O(N) worst case, since
6430 we should have marked both the lower bound and upper bound of
6431 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6432 above. This scan also resets those fields. */
6434 splay_tree_foreach (cases, match_case_to_enum, type);
6437 /* Finish an expression taking the address of LABEL (an
6438 IDENTIFIER_NODE). Returns an expression for the address.
6440 LOC is the location for the expression returned. */
6442 tree
6443 finish_label_address_expr (tree label, location_t loc)
6445 tree result;
6447 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
6449 if (label == error_mark_node)
6450 return error_mark_node;
6452 label = lookup_label (label);
6453 if (label == NULL_TREE)
6454 result = null_pointer_node;
6455 else
6457 TREE_USED (label) = 1;
6458 result = build1 (ADDR_EXPR, ptr_type_node, label);
6459 /* The current function is not necessarily uninlinable.
6460 Computed gotos are incompatible with inlining, but the value
6461 here could be used only in a diagnostic, for example. */
6462 protected_set_expr_location (result, loc);
6465 return result;
6469 /* Given a boolean expression ARG, return a tree representing an increment
6470 or decrement (as indicated by CODE) of ARG. The front end must check for
6471 invalid cases (e.g., decrement in C++). */
6472 tree
6473 boolean_increment (enum tree_code code, tree arg)
6475 tree val;
6476 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
6478 arg = stabilize_reference (arg);
6479 switch (code)
6481 case PREINCREMENT_EXPR:
6482 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6483 break;
6484 case POSTINCREMENT_EXPR:
6485 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
6486 arg = save_expr (arg);
6487 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6488 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6489 break;
6490 case PREDECREMENT_EXPR:
6491 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6492 invert_truthvalue_loc (input_location, arg));
6493 break;
6494 case POSTDECREMENT_EXPR:
6495 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
6496 invert_truthvalue_loc (input_location, arg));
6497 arg = save_expr (arg);
6498 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6499 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
6500 break;
6501 default:
6502 gcc_unreachable ();
6504 TREE_SIDE_EFFECTS (val) = 1;
6505 return val;
6508 /* Built-in macros for stddef.h and stdint.h, that require macros
6509 defined in this file. */
6510 void
6511 c_stddef_cpp_builtins(void)
6513 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6514 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6515 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6516 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
6517 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6518 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
6519 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6520 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
6521 if (SIG_ATOMIC_TYPE)
6522 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6523 if (INT8_TYPE)
6524 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6525 if (INT16_TYPE)
6526 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6527 if (INT32_TYPE)
6528 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6529 if (INT64_TYPE)
6530 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6531 if (UINT8_TYPE)
6532 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6533 if (UINT16_TYPE)
6534 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6535 if (UINT32_TYPE)
6536 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6537 if (UINT64_TYPE)
6538 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6539 if (INT_LEAST8_TYPE)
6540 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6541 if (INT_LEAST16_TYPE)
6542 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6543 if (INT_LEAST32_TYPE)
6544 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6545 if (INT_LEAST64_TYPE)
6546 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6547 if (UINT_LEAST8_TYPE)
6548 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6549 if (UINT_LEAST16_TYPE)
6550 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6551 if (UINT_LEAST32_TYPE)
6552 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6553 if (UINT_LEAST64_TYPE)
6554 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6555 if (INT_FAST8_TYPE)
6556 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6557 if (INT_FAST16_TYPE)
6558 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6559 if (INT_FAST32_TYPE)
6560 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6561 if (INT_FAST64_TYPE)
6562 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6563 if (UINT_FAST8_TYPE)
6564 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6565 if (UINT_FAST16_TYPE)
6566 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6567 if (UINT_FAST32_TYPE)
6568 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6569 if (UINT_FAST64_TYPE)
6570 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6571 if (INTPTR_TYPE)
6572 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6573 if (UINTPTR_TYPE)
6574 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
6577 static void
6578 c_init_attributes (void)
6580 /* Fill in the built_in_attributes array. */
6581 #define DEF_ATTR_NULL_TREE(ENUM) \
6582 built_in_attributes[(int) ENUM] = NULL_TREE;
6583 #define DEF_ATTR_INT(ENUM, VALUE) \
6584 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
6585 #define DEF_ATTR_STRING(ENUM, VALUE) \
6586 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6587 #define DEF_ATTR_IDENT(ENUM, STRING) \
6588 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6589 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6590 built_in_attributes[(int) ENUM] \
6591 = tree_cons (built_in_attributes[(int) PURPOSE], \
6592 built_in_attributes[(int) VALUE], \
6593 built_in_attributes[(int) CHAIN]);
6594 #include "builtin-attrs.def"
6595 #undef DEF_ATTR_NULL_TREE
6596 #undef DEF_ATTR_INT
6597 #undef DEF_ATTR_IDENT
6598 #undef DEF_ATTR_TREE_LIST
6601 /* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6602 identifier as an argument, so the front end shouldn't look it up. */
6604 bool
6605 attribute_takes_identifier_p (const_tree attr_id)
6607 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
6608 if (spec == NULL)
6609 /* Unknown attribute that we'll end up ignoring, return true so we
6610 don't complain about an identifier argument. */
6611 return true;
6612 else if (!strcmp ("mode", spec->name)
6613 || !strcmp ("format", spec->name)
6614 || !strcmp ("cleanup", spec->name))
6615 return true;
6616 else
6617 return targetm.attribute_takes_identifier_p (attr_id);
6620 /* Attribute handlers common to C front ends. */
6622 /* Handle a "packed" attribute; arguments as in
6623 struct attribute_spec.handler. */
6625 static tree
6626 handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6627 int flags, bool *no_add_attrs)
6629 if (TYPE_P (*node))
6631 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6632 *node = build_variant_type_copy (*node);
6633 TYPE_PACKED (*node) = 1;
6635 else if (TREE_CODE (*node) == FIELD_DECL)
6637 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6638 /* Still pack bitfields. */
6639 && ! DECL_INITIAL (*node))
6640 warning (OPT_Wattributes,
6641 "%qE attribute ignored for field of type %qT",
6642 name, TREE_TYPE (*node));
6643 else
6644 DECL_PACKED (*node) = 1;
6646 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
6647 used for DECL_REGISTER. It wouldn't mean anything anyway.
6648 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6649 that changes what the typedef is typing. */
6650 else
6652 warning (OPT_Wattributes, "%qE attribute ignored", name);
6653 *no_add_attrs = true;
6656 return NULL_TREE;
6659 /* Handle a "nocommon" attribute; arguments as in
6660 struct attribute_spec.handler. */
6662 static tree
6663 handle_nocommon_attribute (tree *node, tree name,
6664 tree ARG_UNUSED (args),
6665 int ARG_UNUSED (flags), bool *no_add_attrs)
6667 if (VAR_P (*node))
6668 DECL_COMMON (*node) = 0;
6669 else
6671 warning (OPT_Wattributes, "%qE attribute ignored", name);
6672 *no_add_attrs = true;
6675 return NULL_TREE;
6678 /* Handle a "common" attribute; arguments as in
6679 struct attribute_spec.handler. */
6681 static tree
6682 handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6683 int ARG_UNUSED (flags), bool *no_add_attrs)
6685 if (VAR_P (*node))
6686 DECL_COMMON (*node) = 1;
6687 else
6689 warning (OPT_Wattributes, "%qE attribute ignored", name);
6690 *no_add_attrs = true;
6693 return NULL_TREE;
6696 /* Handle a "noreturn" attribute; arguments as in
6697 struct attribute_spec.handler. */
6699 static tree
6700 handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6701 int ARG_UNUSED (flags), bool *no_add_attrs)
6703 tree type = TREE_TYPE (*node);
6705 /* See FIXME comment in c_common_attribute_table. */
6706 if (TREE_CODE (*node) == FUNCTION_DECL
6707 || objc_method_decl (TREE_CODE (*node)))
6708 TREE_THIS_VOLATILE (*node) = 1;
6709 else if (TREE_CODE (type) == POINTER_TYPE
6710 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6711 TREE_TYPE (*node)
6712 = (build_qualified_type
6713 (build_pointer_type
6714 (build_type_variant (TREE_TYPE (type),
6715 TYPE_READONLY (TREE_TYPE (type)), 1)),
6716 TYPE_QUALS (type)));
6717 else
6719 warning (OPT_Wattributes, "%qE attribute ignored", name);
6720 *no_add_attrs = true;
6723 return NULL_TREE;
6726 /* Handle a "hot" and attribute; arguments as in
6727 struct attribute_spec.handler. */
6729 static tree
6730 handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6731 int ARG_UNUSED (flags), bool *no_add_attrs)
6733 if (TREE_CODE (*node) == FUNCTION_DECL
6734 || TREE_CODE (*node) == LABEL_DECL)
6736 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6738 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6739 "with attribute %qs", name, "cold");
6740 *no_add_attrs = true;
6742 /* Most of the rest of the hot processing is done later with
6743 lookup_attribute. */
6745 else
6747 warning (OPT_Wattributes, "%qE attribute ignored", name);
6748 *no_add_attrs = true;
6751 return NULL_TREE;
6754 /* Handle a "cold" and attribute; arguments as in
6755 struct attribute_spec.handler. */
6757 static tree
6758 handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6759 int ARG_UNUSED (flags), bool *no_add_attrs)
6761 if (TREE_CODE (*node) == FUNCTION_DECL
6762 || TREE_CODE (*node) == LABEL_DECL)
6764 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6766 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6767 "with attribute %qs", name, "hot");
6768 *no_add_attrs = true;
6770 /* Most of the rest of the cold processing is done later with
6771 lookup_attribute. */
6773 else
6775 warning (OPT_Wattributes, "%qE attribute ignored", name);
6776 *no_add_attrs = true;
6779 return NULL_TREE;
6782 /* Handle a "no_sanitize_address" attribute; arguments as in
6783 struct attribute_spec.handler. */
6785 static tree
6786 handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6787 bool *no_add_attrs)
6789 if (TREE_CODE (*node) != FUNCTION_DECL)
6791 warning (OPT_Wattributes, "%qE attribute ignored", name);
6792 *no_add_attrs = true;
6795 return NULL_TREE;
6798 /* Handle a "no_address_safety_analysis" attribute; arguments as in
6799 struct attribute_spec.handler. */
6801 static tree
6802 handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6803 bool *no_add_attrs)
6805 if (TREE_CODE (*node) != FUNCTION_DECL)
6806 warning (OPT_Wattributes, "%qE attribute ignored", name);
6807 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6808 DECL_ATTRIBUTES (*node)
6809 = tree_cons (get_identifier ("no_sanitize_address"),
6810 NULL_TREE, DECL_ATTRIBUTES (*node));
6811 *no_add_attrs = true;
6812 return NULL_TREE;
6815 /* Handle a "no_sanitize_undefined" attribute; arguments as in
6816 struct attribute_spec.handler. */
6818 static tree
6819 handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6820 bool *no_add_attrs)
6822 if (TREE_CODE (*node) != FUNCTION_DECL)
6824 warning (OPT_Wattributes, "%qE attribute ignored", name);
6825 *no_add_attrs = true;
6828 return NULL_TREE;
6831 /* Handle a "stack_protect" attribute; arguments as in
6832 struct attribute_spec.handler. */
6833 static tree
6834 handle_stack_protect_attribute (tree *node, tree name, tree, int,
6835 bool *no_add_attrs)
6837 if (TREE_CODE (*node) != FUNCTION_DECL)
6839 warning (OPT_Wattributes, "%qE attribute ignored", name);
6840 *no_add_attrs = true;
6842 else
6843 DECL_ATTRIBUTES (*node)
6844 = tree_cons (get_identifier ("stack_protect"),
6845 NULL_TREE, DECL_ATTRIBUTES (*node));
6847 return NULL_TREE;
6850 /* Handle a "noinline" attribute; arguments as in
6851 struct attribute_spec.handler. */
6853 static tree
6854 handle_noinline_attribute (tree *node, tree name,
6855 tree ARG_UNUSED (args),
6856 int ARG_UNUSED (flags), bool *no_add_attrs)
6858 if (TREE_CODE (*node) == FUNCTION_DECL)
6860 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6862 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6863 "with attribute %qs", name, "always_inline");
6864 *no_add_attrs = true;
6866 else
6867 DECL_UNINLINABLE (*node) = 1;
6869 else
6871 warning (OPT_Wattributes, "%qE attribute ignored", name);
6872 *no_add_attrs = true;
6875 return NULL_TREE;
6878 /* Handle a "noclone" attribute; arguments as in
6879 struct attribute_spec.handler. */
6881 static tree
6882 handle_noclone_attribute (tree *node, tree name,
6883 tree ARG_UNUSED (args),
6884 int ARG_UNUSED (flags), bool *no_add_attrs)
6886 if (TREE_CODE (*node) != FUNCTION_DECL)
6888 warning (OPT_Wattributes, "%qE attribute ignored", name);
6889 *no_add_attrs = true;
6892 return NULL_TREE;
6895 /* Handle a "no_icf" attribute; arguments as in
6896 struct attribute_spec.handler. */
6898 static tree
6899 handle_noicf_attribute (tree *node, tree name,
6900 tree ARG_UNUSED (args),
6901 int ARG_UNUSED (flags), bool *no_add_attrs)
6903 if (TREE_CODE (*node) != FUNCTION_DECL)
6905 warning (OPT_Wattributes, "%qE attribute ignored", name);
6906 *no_add_attrs = true;
6909 return NULL_TREE;
6913 /* Handle a "always_inline" attribute; arguments as in
6914 struct attribute_spec.handler. */
6916 static tree
6917 handle_always_inline_attribute (tree *node, tree name,
6918 tree ARG_UNUSED (args),
6919 int ARG_UNUSED (flags),
6920 bool *no_add_attrs)
6922 if (TREE_CODE (*node) == FUNCTION_DECL)
6924 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6926 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6927 "with %qs attribute", name, "noinline");
6928 *no_add_attrs = true;
6930 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
6932 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6933 "with %qs attribute", name, "target_clones");
6934 *no_add_attrs = true;
6936 else
6937 /* Set the attribute and mark it for disregarding inline
6938 limits. */
6939 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
6941 else
6943 warning (OPT_Wattributes, "%qE attribute ignored", name);
6944 *no_add_attrs = true;
6947 return NULL_TREE;
6950 /* Handle a "gnu_inline" attribute; arguments as in
6951 struct attribute_spec.handler. */
6953 static tree
6954 handle_gnu_inline_attribute (tree *node, tree name,
6955 tree ARG_UNUSED (args),
6956 int ARG_UNUSED (flags),
6957 bool *no_add_attrs)
6959 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6961 /* Do nothing else, just set the attribute. We'll get at
6962 it later with lookup_attribute. */
6964 else
6966 warning (OPT_Wattributes, "%qE attribute ignored", name);
6967 *no_add_attrs = true;
6970 return NULL_TREE;
6973 /* Handle a "leaf" attribute; arguments as in
6974 struct attribute_spec.handler. */
6976 static tree
6977 handle_leaf_attribute (tree *node, tree name,
6978 tree ARG_UNUSED (args),
6979 int ARG_UNUSED (flags), bool *no_add_attrs)
6981 if (TREE_CODE (*node) != FUNCTION_DECL)
6983 warning (OPT_Wattributes, "%qE attribute ignored", name);
6984 *no_add_attrs = true;
6986 if (!TREE_PUBLIC (*node))
6988 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6989 *no_add_attrs = true;
6992 return NULL_TREE;
6995 /* Handle an "artificial" attribute; arguments as in
6996 struct attribute_spec.handler. */
6998 static tree
6999 handle_artificial_attribute (tree *node, tree name,
7000 tree ARG_UNUSED (args),
7001 int ARG_UNUSED (flags),
7002 bool *no_add_attrs)
7004 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7006 /* Do nothing else, just set the attribute. We'll get at
7007 it later with lookup_attribute. */
7009 else
7011 warning (OPT_Wattributes, "%qE attribute ignored", name);
7012 *no_add_attrs = true;
7015 return NULL_TREE;
7018 /* Handle a "flatten" attribute; arguments as in
7019 struct attribute_spec.handler. */
7021 static tree
7022 handle_flatten_attribute (tree *node, tree name,
7023 tree args ATTRIBUTE_UNUSED,
7024 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
7026 if (TREE_CODE (*node) == FUNCTION_DECL)
7027 /* Do nothing else, just set the attribute. We'll get at
7028 it later with lookup_attribute. */
7030 else
7032 warning (OPT_Wattributes, "%qE attribute ignored", name);
7033 *no_add_attrs = true;
7036 return NULL_TREE;
7039 /* Handle a "warning" or "error" attribute; arguments as in
7040 struct attribute_spec.handler. */
7042 static tree
7043 handle_error_attribute (tree *node, tree name, tree args,
7044 int ARG_UNUSED (flags), bool *no_add_attrs)
7046 if (TREE_CODE (*node) == FUNCTION_DECL
7047 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
7048 /* Do nothing else, just set the attribute. We'll get at
7049 it later with lookup_attribute. */
7051 else
7053 warning (OPT_Wattributes, "%qE attribute ignored", name);
7054 *no_add_attrs = true;
7057 return NULL_TREE;
7060 /* Handle a "used" attribute; arguments as in
7061 struct attribute_spec.handler. */
7063 static tree
7064 handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7065 int ARG_UNUSED (flags), bool *no_add_attrs)
7067 tree node = *pnode;
7069 if (TREE_CODE (node) == FUNCTION_DECL
7070 || (VAR_P (node) && TREE_STATIC (node))
7071 || (TREE_CODE (node) == TYPE_DECL))
7073 TREE_USED (node) = 1;
7074 DECL_PRESERVE_P (node) = 1;
7075 if (VAR_P (node))
7076 DECL_READ_P (node) = 1;
7078 else
7080 warning (OPT_Wattributes, "%qE attribute ignored", name);
7081 *no_add_attrs = true;
7084 return NULL_TREE;
7087 /* Handle a "unused" attribute; arguments as in
7088 struct attribute_spec.handler. */
7090 tree
7091 handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7092 int flags, bool *no_add_attrs)
7094 if (DECL_P (*node))
7096 tree decl = *node;
7098 if (TREE_CODE (decl) == PARM_DECL
7099 || VAR_OR_FUNCTION_DECL_P (decl)
7100 || TREE_CODE (decl) == LABEL_DECL
7101 || TREE_CODE (decl) == CONST_DECL
7102 || TREE_CODE (decl) == TYPE_DECL)
7104 TREE_USED (decl) = 1;
7105 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
7106 DECL_READ_P (decl) = 1;
7108 else
7110 warning (OPT_Wattributes, "%qE attribute ignored", name);
7111 *no_add_attrs = true;
7114 else
7116 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7117 *node = build_variant_type_copy (*node);
7118 TREE_USED (*node) = 1;
7121 return NULL_TREE;
7124 /* Handle a "externally_visible" attribute; arguments as in
7125 struct attribute_spec.handler. */
7127 static tree
7128 handle_externally_visible_attribute (tree *pnode, tree name,
7129 tree ARG_UNUSED (args),
7130 int ARG_UNUSED (flags),
7131 bool *no_add_attrs)
7133 tree node = *pnode;
7135 if (VAR_OR_FUNCTION_DECL_P (node))
7137 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7138 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7140 warning (OPT_Wattributes,
7141 "%qE attribute have effect only on public objects", name);
7142 *no_add_attrs = true;
7145 else
7147 warning (OPT_Wattributes, "%qE attribute ignored", name);
7148 *no_add_attrs = true;
7151 return NULL_TREE;
7154 /* Handle the "no_reorder" attribute. Arguments as in
7155 struct attribute_spec.handler. */
7157 static tree
7158 handle_no_reorder_attribute (tree *pnode,
7159 tree name,
7160 tree,
7161 int,
7162 bool *no_add_attrs)
7164 tree node = *pnode;
7166 if (!VAR_OR_FUNCTION_DECL_P (node)
7167 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7169 warning (OPT_Wattributes,
7170 "%qE attribute only affects top level objects",
7171 name);
7172 *no_add_attrs = true;
7175 return NULL_TREE;
7178 /* Handle a "const" attribute; arguments as in
7179 struct attribute_spec.handler. */
7181 static tree
7182 handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7183 int ARG_UNUSED (flags), bool *no_add_attrs)
7185 tree type = TREE_TYPE (*node);
7187 /* See FIXME comment on noreturn in c_common_attribute_table. */
7188 if (TREE_CODE (*node) == FUNCTION_DECL)
7189 TREE_READONLY (*node) = 1;
7190 else if (TREE_CODE (type) == POINTER_TYPE
7191 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7192 TREE_TYPE (*node)
7193 = (build_qualified_type
7194 (build_pointer_type
7195 (build_type_variant (TREE_TYPE (type), 1,
7196 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7197 TYPE_QUALS (type)));
7198 else
7200 warning (OPT_Wattributes, "%qE attribute ignored", name);
7201 *no_add_attrs = true;
7204 return NULL_TREE;
7207 /* Handle a "scalar_storage_order" attribute; arguments as in
7208 struct attribute_spec.handler. */
7210 static tree
7211 handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7212 int flags, bool *no_add_attrs)
7214 tree id = TREE_VALUE (args);
7215 tree type;
7217 if (TREE_CODE (*node) == TYPE_DECL
7218 && ! (flags & ATTR_FLAG_CXX11))
7219 node = &TREE_TYPE (*node);
7220 type = *node;
7222 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7224 error ("scalar_storage_order is not supported because endianness "
7225 "is not uniform");
7226 return NULL_TREE;
7229 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7231 bool reverse = false;
7233 if (TREE_CODE (id) == STRING_CST
7234 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7235 reverse = !BYTES_BIG_ENDIAN;
7236 else if (TREE_CODE (id) == STRING_CST
7237 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7238 reverse = BYTES_BIG_ENDIAN;
7239 else
7241 error ("scalar_storage_order argument must be one of \"big-endian\""
7242 " or \"little-endian\"");
7243 return NULL_TREE;
7246 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7248 if (reverse)
7249 /* A type variant isn't good enough, since we don't want a cast
7250 to such a type to be removed as a no-op. */
7251 *node = type = build_duplicate_type (type);
7254 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7255 return NULL_TREE;
7258 warning (OPT_Wattributes, "%qE attribute ignored", name);
7259 *no_add_attrs = true;
7260 return NULL_TREE;
7263 /* Handle a "transparent_union" attribute; arguments as in
7264 struct attribute_spec.handler. */
7266 static tree
7267 handle_transparent_union_attribute (tree *node, tree name,
7268 tree ARG_UNUSED (args), int flags,
7269 bool *no_add_attrs)
7271 tree type;
7273 *no_add_attrs = true;
7275 if (TREE_CODE (*node) == TYPE_DECL
7276 && ! (flags & ATTR_FLAG_CXX11))
7277 node = &TREE_TYPE (*node);
7278 type = *node;
7280 if (TREE_CODE (type) == UNION_TYPE)
7282 /* Make sure that the first field will work for a transparent union.
7283 If the type isn't complete yet, leave the check to the code in
7284 finish_struct. */
7285 if (TYPE_SIZE (type))
7287 tree first = first_field (type);
7288 if (first == NULL_TREE
7289 || DECL_ARTIFICIAL (first)
7290 || TYPE_MODE (type) != DECL_MODE (first))
7291 goto ignored;
7294 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7296 /* If the type isn't complete yet, setting the flag
7297 on a variant wouldn't ever be checked. */
7298 if (!TYPE_SIZE (type))
7299 goto ignored;
7301 /* build_duplicate_type doesn't work for C++. */
7302 if (c_dialect_cxx ())
7303 goto ignored;
7305 /* A type variant isn't good enough, since we don't want a cast
7306 to such a type to be removed as a no-op. */
7307 *node = type = build_duplicate_type (type);
7310 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7311 TYPE_TRANSPARENT_AGGR (t) = 1;
7312 return NULL_TREE;
7315 ignored:
7316 warning (OPT_Wattributes, "%qE attribute ignored", name);
7317 return NULL_TREE;
7320 /* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7321 get the requested priority for a constructor or destructor,
7322 possibly issuing diagnostics for invalid or reserved
7323 priorities. */
7325 static priority_type
7326 get_priority (tree args, bool is_destructor)
7328 HOST_WIDE_INT pri;
7329 tree arg;
7331 if (!args)
7332 return DEFAULT_INIT_PRIORITY;
7334 if (!SUPPORTS_INIT_PRIORITY)
7336 if (is_destructor)
7337 error ("destructor priorities are not supported");
7338 else
7339 error ("constructor priorities are not supported");
7340 return DEFAULT_INIT_PRIORITY;
7343 arg = TREE_VALUE (args);
7344 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7345 goto invalid;
7346 if (arg == error_mark_node)
7347 return DEFAULT_INIT_PRIORITY;
7348 arg = default_conversion (arg);
7349 if (!tree_fits_shwi_p (arg)
7350 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
7351 goto invalid;
7353 pri = tree_to_shwi (arg);
7354 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7355 goto invalid;
7357 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7359 if (is_destructor)
7360 warning (0,
7361 "destructor priorities from 0 to %d are reserved "
7362 "for the implementation",
7363 MAX_RESERVED_INIT_PRIORITY);
7364 else
7365 warning (0,
7366 "constructor priorities from 0 to %d are reserved "
7367 "for the implementation",
7368 MAX_RESERVED_INIT_PRIORITY);
7370 return pri;
7372 invalid:
7373 if (is_destructor)
7374 error ("destructor priorities must be integers from 0 to %d inclusive",
7375 MAX_INIT_PRIORITY);
7376 else
7377 error ("constructor priorities must be integers from 0 to %d inclusive",
7378 MAX_INIT_PRIORITY);
7379 return DEFAULT_INIT_PRIORITY;
7382 /* Handle a "constructor" attribute; arguments as in
7383 struct attribute_spec.handler. */
7385 static tree
7386 handle_constructor_attribute (tree *node, tree name, tree args,
7387 int ARG_UNUSED (flags),
7388 bool *no_add_attrs)
7390 tree decl = *node;
7391 tree type = TREE_TYPE (decl);
7393 if (TREE_CODE (decl) == FUNCTION_DECL
7394 && TREE_CODE (type) == FUNCTION_TYPE
7395 && decl_function_context (decl) == 0)
7397 priority_type priority;
7398 DECL_STATIC_CONSTRUCTOR (decl) = 1;
7399 priority = get_priority (args, /*is_destructor=*/false);
7400 SET_DECL_INIT_PRIORITY (decl, priority);
7401 TREE_USED (decl) = 1;
7403 else
7405 warning (OPT_Wattributes, "%qE attribute ignored", name);
7406 *no_add_attrs = true;
7409 return NULL_TREE;
7412 /* Handle a "destructor" attribute; arguments as in
7413 struct attribute_spec.handler. */
7415 static tree
7416 handle_destructor_attribute (tree *node, tree name, tree args,
7417 int ARG_UNUSED (flags),
7418 bool *no_add_attrs)
7420 tree decl = *node;
7421 tree type = TREE_TYPE (decl);
7423 if (TREE_CODE (decl) == FUNCTION_DECL
7424 && TREE_CODE (type) == FUNCTION_TYPE
7425 && decl_function_context (decl) == 0)
7427 priority_type priority;
7428 DECL_STATIC_DESTRUCTOR (decl) = 1;
7429 priority = get_priority (args, /*is_destructor=*/true);
7430 SET_DECL_FINI_PRIORITY (decl, priority);
7431 TREE_USED (decl) = 1;
7433 else
7435 warning (OPT_Wattributes, "%qE attribute ignored", name);
7436 *no_add_attrs = true;
7439 return NULL_TREE;
7442 /* Nonzero if the mode is a valid vector mode for this architecture.
7443 This returns nonzero even if there is no hardware support for the
7444 vector mode, but we can emulate with narrower modes. */
7446 static int
7447 vector_mode_valid_p (machine_mode mode)
7449 enum mode_class mclass = GET_MODE_CLASS (mode);
7450 machine_mode innermode;
7452 /* Doh! What's going on? */
7453 if (mclass != MODE_VECTOR_INT
7454 && mclass != MODE_VECTOR_FLOAT
7455 && mclass != MODE_VECTOR_FRACT
7456 && mclass != MODE_VECTOR_UFRACT
7457 && mclass != MODE_VECTOR_ACCUM
7458 && mclass != MODE_VECTOR_UACCUM)
7459 return 0;
7461 /* Hardware support. Woo hoo! */
7462 if (targetm.vector_mode_supported_p (mode))
7463 return 1;
7465 innermode = GET_MODE_INNER (mode);
7467 /* We should probably return 1 if requesting V4DI and we have no DI,
7468 but we have V2DI, but this is probably very unlikely. */
7470 /* If we have support for the inner mode, we can safely emulate it.
7471 We may not have V2DI, but me can emulate with a pair of DIs. */
7472 return targetm.scalar_mode_supported_p (innermode);
7476 /* Handle a "mode" attribute; arguments as in
7477 struct attribute_spec.handler. */
7479 static tree
7480 handle_mode_attribute (tree *node, tree name, tree args,
7481 int ARG_UNUSED (flags), bool *no_add_attrs)
7483 tree type = *node;
7484 tree ident = TREE_VALUE (args);
7486 *no_add_attrs = true;
7488 if (TREE_CODE (ident) != IDENTIFIER_NODE)
7489 warning (OPT_Wattributes, "%qE attribute ignored", name);
7490 else
7492 int j;
7493 const char *p = IDENTIFIER_POINTER (ident);
7494 int len = strlen (p);
7495 machine_mode mode = VOIDmode;
7496 tree typefm;
7497 bool valid_mode;
7499 if (len > 4 && p[0] == '_' && p[1] == '_'
7500 && p[len - 1] == '_' && p[len - 2] == '_')
7502 char *newp = (char *) alloca (len - 1);
7504 strcpy (newp, &p[2]);
7505 newp[len - 4] = '\0';
7506 p = newp;
7509 /* Change this type to have a type with the specified mode.
7510 First check for the special modes. */
7511 if (!strcmp (p, "byte"))
7512 mode = byte_mode;
7513 else if (!strcmp (p, "word"))
7514 mode = word_mode;
7515 else if (!strcmp (p, "pointer"))
7516 mode = ptr_mode;
7517 else if (!strcmp (p, "libgcc_cmp_return"))
7518 mode = targetm.libgcc_cmp_return_mode ();
7519 else if (!strcmp (p, "libgcc_shift_count"))
7520 mode = targetm.libgcc_shift_count_mode ();
7521 else if (!strcmp (p, "unwind_word"))
7522 mode = targetm.unwind_word_mode ();
7523 else
7524 for (j = 0; j < NUM_MACHINE_MODES; j++)
7525 if (!strcmp (p, GET_MODE_NAME (j)))
7527 mode = (machine_mode) j;
7528 break;
7531 if (mode == VOIDmode)
7533 error ("unknown machine mode %qE", ident);
7534 return NULL_TREE;
7537 valid_mode = false;
7538 switch (GET_MODE_CLASS (mode))
7540 case MODE_INT:
7541 case MODE_PARTIAL_INT:
7542 case MODE_FLOAT:
7543 case MODE_DECIMAL_FLOAT:
7544 case MODE_FRACT:
7545 case MODE_UFRACT:
7546 case MODE_ACCUM:
7547 case MODE_UACCUM:
7548 valid_mode = targetm.scalar_mode_supported_p (mode);
7549 break;
7551 case MODE_COMPLEX_INT:
7552 case MODE_COMPLEX_FLOAT:
7553 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7554 break;
7556 case MODE_VECTOR_INT:
7557 case MODE_VECTOR_FLOAT:
7558 case MODE_VECTOR_FRACT:
7559 case MODE_VECTOR_UFRACT:
7560 case MODE_VECTOR_ACCUM:
7561 case MODE_VECTOR_UACCUM:
7562 warning (OPT_Wattributes, "specifying vector types with "
7563 "__attribute__ ((mode)) is deprecated");
7564 warning (OPT_Wattributes,
7565 "use __attribute__ ((vector_size)) instead");
7566 valid_mode = vector_mode_valid_p (mode);
7567 break;
7569 default:
7570 break;
7572 if (!valid_mode)
7574 error ("unable to emulate %qs", p);
7575 return NULL_TREE;
7578 if (POINTER_TYPE_P (type))
7580 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
7581 tree (*fn)(tree, machine_mode, bool);
7583 if (!targetm.addr_space.valid_pointer_mode (mode, as))
7585 error ("invalid pointer mode %qs", p);
7586 return NULL_TREE;
7589 if (TREE_CODE (type) == POINTER_TYPE)
7590 fn = build_pointer_type_for_mode;
7591 else
7592 fn = build_reference_type_for_mode;
7593 typefm = fn (TREE_TYPE (type), mode, false);
7595 else
7597 /* For fixed-point modes, we need to test if the signness of type
7598 and the machine mode are consistent. */
7599 if (ALL_FIXED_POINT_MODE_P (mode)
7600 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7602 error ("signedness of type and machine mode %qs don%'t match", p);
7603 return NULL_TREE;
7605 /* For fixed-point modes, we need to pass saturating info. */
7606 typefm = lang_hooks.types.type_for_mode (mode,
7607 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7608 : TYPE_UNSIGNED (type));
7611 if (typefm == NULL_TREE)
7613 error ("no data type for mode %qs", p);
7614 return NULL_TREE;
7616 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7618 /* For enumeral types, copy the precision from the integer
7619 type returned above. If not an INTEGER_TYPE, we can't use
7620 this mode for this type. */
7621 if (TREE_CODE (typefm) != INTEGER_TYPE)
7623 error ("cannot use mode %qs for enumeral types", p);
7624 return NULL_TREE;
7627 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7629 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7630 typefm = type;
7632 else
7634 /* We cannot build a type variant, as there's code that assumes
7635 that TYPE_MAIN_VARIANT has the same mode. This includes the
7636 debug generators. Instead, create a subrange type. This
7637 results in all of the enumeral values being emitted only once
7638 in the original, and the subtype gets them by reference. */
7639 if (TYPE_UNSIGNED (type))
7640 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7641 else
7642 typefm = make_signed_type (TYPE_PRECISION (typefm));
7643 TREE_TYPE (typefm) = type;
7646 else if (VECTOR_MODE_P (mode)
7647 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7648 : TREE_CODE (type) != TREE_CODE (typefm))
7650 error ("mode %qs applied to inappropriate type", p);
7651 return NULL_TREE;
7654 *node = typefm;
7657 return NULL_TREE;
7660 /* Handle a "section" attribute; arguments as in
7661 struct attribute_spec.handler. */
7663 static tree
7664 handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7665 int ARG_UNUSED (flags), bool *no_add_attrs)
7667 tree decl = *node;
7669 if (!targetm_common.have_named_sections)
7671 error_at (DECL_SOURCE_LOCATION (*node),
7672 "section attributes are not supported for this target");
7673 goto fail;
7676 user_defined_section_attribute = true;
7678 if (!VAR_OR_FUNCTION_DECL_P (decl))
7680 error ("section attribute not allowed for %q+D", *node);
7681 goto fail;
7684 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
7686 error ("section attribute argument not a string constant");
7687 goto fail;
7690 if (VAR_P (decl)
7691 && current_function_decl != NULL_TREE
7692 && !TREE_STATIC (decl))
7694 error_at (DECL_SOURCE_LOCATION (decl),
7695 "section attribute cannot be specified for local variables");
7696 goto fail;
7699 /* The decl may have already been given a section attribute
7700 from a previous declaration. Ensure they match. */
7701 if (DECL_SECTION_NAME (decl) != NULL
7702 && strcmp (DECL_SECTION_NAME (decl),
7703 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7705 error ("section of %q+D conflicts with previous declaration", *node);
7706 goto fail;
7709 if (VAR_P (decl)
7710 && !targetm.have_tls && targetm.emutls.tmpl_section
7711 && DECL_THREAD_LOCAL_P (decl))
7713 error ("section of %q+D cannot be overridden", *node);
7714 goto fail;
7717 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7718 return NULL_TREE;
7720 fail:
7721 *no_add_attrs = true;
7722 return NULL_TREE;
7725 /* Check whether ALIGN is a valid user-specified alignment. If so,
7726 return its base-2 log; if not, output an error and return -1. If
7727 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7728 no error. */
7730 check_user_alignment (const_tree align, bool allow_zero)
7732 int i;
7734 if (error_operand_p (align))
7735 return -1;
7736 if (TREE_CODE (align) != INTEGER_CST
7737 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7739 error ("requested alignment is not an integer constant");
7740 return -1;
7742 else if (allow_zero && integer_zerop (align))
7743 return -1;
7744 else if (tree_int_cst_sgn (align) == -1
7745 || (i = tree_log2 (align)) == -1)
7747 error ("requested alignment is not a positive power of 2");
7748 return -1;
7750 else if (i >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
7752 error ("requested alignment is too large");
7753 return -1;
7755 return i;
7759 If in c++-11, check if the c++-11 alignment constraint with respect
7760 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7761 c++-11 mode, does nothing.
7763 [dcl.align]2/ says:
7765 [* if the constant expression evaluates to a fundamental alignment,
7766 the alignment requirement of the declared entity shall be the
7767 specified fundamental alignment.
7769 * if the constant expression evaluates to an extended alignment
7770 and the implementation supports that alignment in the context
7771 of the declaration, the alignment of the declared entity shall
7772 be that alignment
7774 * if the constant expression evaluates to an extended alignment
7775 and the implementation does not support that alignment in the
7776 context of the declaration, the program is ill-formed]. */
7778 static bool
7779 check_cxx_fundamental_alignment_constraints (tree node,
7780 unsigned align_log,
7781 int flags)
7783 bool alignment_too_large_p = false;
7784 unsigned requested_alignment = 1U << align_log;
7785 unsigned max_align = 0;
7787 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7788 || (node == NULL_TREE || node == error_mark_node))
7789 return true;
7791 if (cxx_fundamental_alignment_p (requested_alignment))
7792 return true;
7794 if (DECL_P (node))
7796 if (TREE_STATIC (node))
7798 /* For file scope variables and static members, the target
7799 supports alignments that are at most
7800 MAX_OFILE_ALIGNMENT. */
7801 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7802 alignment_too_large_p = true;
7804 else
7806 #ifdef BIGGEST_FIELD_ALIGNMENT
7807 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7808 #else
7809 #define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7810 #endif
7811 /* For non-static members, the target supports either
7812 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7813 if it is defined or BIGGEST_ALIGNMENT. */
7814 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7815 if (TREE_CODE (node) == FIELD_DECL
7816 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7817 alignment_too_large_p = true;
7818 #undef MAX_TARGET_FIELD_ALIGNMENT
7819 /* For stack variables, the target supports at most
7820 MAX_STACK_ALIGNMENT. */
7821 else if (decl_function_context (node) != NULL
7822 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7823 alignment_too_large_p = true;
7826 else if (TYPE_P (node))
7828 /* Let's be liberal for types. */
7829 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7830 alignment_too_large_p = true;
7833 if (alignment_too_large_p)
7834 pedwarn (input_location, OPT_Wattributes,
7835 "requested alignment %d is larger than %d",
7836 requested_alignment, max_align);
7838 return !alignment_too_large_p;
7841 /* Handle a "aligned" attribute; arguments as in
7842 struct attribute_spec.handler. */
7844 static tree
7845 handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7846 int flags, bool *no_add_attrs)
7848 tree decl = NULL_TREE;
7849 tree *type = NULL;
7850 int is_type = 0;
7851 tree align_expr;
7852 int i;
7854 if (args)
7856 align_expr = TREE_VALUE (args);
7857 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7858 && TREE_CODE (align_expr) != FUNCTION_DECL)
7859 align_expr = default_conversion (align_expr);
7861 else
7862 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7864 if (DECL_P (*node))
7866 decl = *node;
7867 type = &TREE_TYPE (decl);
7868 is_type = TREE_CODE (*node) == TYPE_DECL;
7870 else if (TYPE_P (*node))
7871 type = node, is_type = 1;
7873 if ((i = check_user_alignment (align_expr, true)) == -1
7874 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
7875 *no_add_attrs = true;
7876 else if (is_type)
7878 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7879 /* OK, modify the type in place. */;
7880 /* If we have a TYPE_DECL, then copy the type, so that we
7881 don't accidentally modify a builtin type. See pushdecl. */
7882 else if (decl && TREE_TYPE (decl) != error_mark_node
7883 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
7885 tree tt = TREE_TYPE (decl);
7886 *type = build_variant_type_copy (*type);
7887 DECL_ORIGINAL_TYPE (decl) = tt;
7888 TYPE_NAME (*type) = decl;
7889 TREE_USED (*type) = TREE_USED (decl);
7890 TREE_TYPE (decl) = *type;
7892 else
7893 *type = build_variant_type_copy (*type);
7895 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
7896 TYPE_USER_ALIGN (*type) = 1;
7898 else if (! VAR_OR_FUNCTION_DECL_P (decl)
7899 && TREE_CODE (decl) != FIELD_DECL)
7901 error ("alignment may not be specified for %q+D", decl);
7902 *no_add_attrs = true;
7904 else if (DECL_USER_ALIGN (decl)
7905 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7906 /* C++-11 [dcl.align/4]:
7908 When multiple alignment-specifiers are specified for an
7909 entity, the alignment requirement shall be set to the
7910 strictest specified alignment.
7912 This formally comes from the c++11 specification but we are
7913 doing it for the GNU attribute syntax as well. */
7914 *no_add_attrs = true;
7915 else if (TREE_CODE (decl) == FUNCTION_DECL
7916 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7918 if (DECL_USER_ALIGN (decl))
7919 error ("alignment for %q+D was previously specified as %d "
7920 "and may not be decreased", decl,
7921 DECL_ALIGN (decl) / BITS_PER_UNIT);
7922 else
7923 error ("alignment for %q+D must be at least %d", decl,
7924 DECL_ALIGN (decl) / BITS_PER_UNIT);
7925 *no_add_attrs = true;
7927 else
7929 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
7930 DECL_USER_ALIGN (decl) = 1;
7933 return NULL_TREE;
7936 /* Handle a "weak" attribute; arguments as in
7937 struct attribute_spec.handler. */
7939 static tree
7940 handle_weak_attribute (tree *node, tree name,
7941 tree ARG_UNUSED (args),
7942 int ARG_UNUSED (flags),
7943 bool * ARG_UNUSED (no_add_attrs))
7945 if (TREE_CODE (*node) == FUNCTION_DECL
7946 && DECL_DECLARED_INLINE_P (*node))
7948 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
7949 *no_add_attrs = true;
7951 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7953 error ("indirect function %q+D cannot be declared weak", *node);
7954 *no_add_attrs = true;
7955 return NULL_TREE;
7957 else if (VAR_OR_FUNCTION_DECL_P (*node))
7958 declare_weak (*node);
7959 else
7960 warning (OPT_Wattributes, "%qE attribute ignored", name);
7962 return NULL_TREE;
7965 /* Handle a "noplt" attribute; arguments as in
7966 struct attribute_spec.handler. */
7968 static tree
7969 handle_noplt_attribute (tree *node, tree name,
7970 tree ARG_UNUSED (args),
7971 int ARG_UNUSED (flags),
7972 bool * ARG_UNUSED (no_add_attrs))
7974 if (TREE_CODE (*node) != FUNCTION_DECL)
7976 warning (OPT_Wattributes,
7977 "%qE attribute is only applicable on functions", name);
7978 *no_add_attrs = true;
7979 return NULL_TREE;
7981 return NULL_TREE;
7984 /* Handle an "alias" or "ifunc" attribute; arguments as in
7985 struct attribute_spec.handler, except that IS_ALIAS tells us
7986 whether this is an alias as opposed to ifunc attribute. */
7988 static tree
7989 handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7990 bool *no_add_attrs)
7992 tree decl = *node;
7994 if (TREE_CODE (decl) != FUNCTION_DECL
7995 && (!is_alias || !VAR_P (decl)))
7997 warning (OPT_Wattributes, "%qE attribute ignored", name);
7998 *no_add_attrs = true;
8000 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
8001 || (TREE_CODE (decl) != FUNCTION_DECL
8002 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8003 /* A static variable declaration is always a tentative definition,
8004 but the alias is a non-tentative definition which overrides. */
8005 || (TREE_CODE (decl) != FUNCTION_DECL
8006 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
8008 error ("%q+D defined both normally and as %qE attribute", decl, name);
8009 *no_add_attrs = true;
8010 return NULL_TREE;
8012 else if (!is_alias
8013 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8014 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8016 error ("weak %q+D cannot be defined %qE", decl, name);
8017 *no_add_attrs = true;
8018 return NULL_TREE;
8021 /* Note that the very first time we process a nested declaration,
8022 decl_function_context will not be set. Indeed, *would* never
8023 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8024 we do below. After such frobbery, pushdecl would set the context.
8025 In any case, this is never what we want. */
8026 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
8028 tree id;
8030 id = TREE_VALUE (args);
8031 if (TREE_CODE (id) != STRING_CST)
8033 error ("attribute %qE argument not a string", name);
8034 *no_add_attrs = true;
8035 return NULL_TREE;
8037 id = get_identifier (TREE_STRING_POINTER (id));
8038 /* This counts as a use of the object pointed to. */
8039 TREE_USED (id) = 1;
8041 if (TREE_CODE (decl) == FUNCTION_DECL)
8042 DECL_INITIAL (decl) = error_mark_node;
8043 else
8044 TREE_STATIC (decl) = 1;
8046 if (!is_alias)
8047 /* ifuncs are also aliases, so set that attribute too. */
8048 DECL_ATTRIBUTES (decl)
8049 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
8051 else
8053 warning (OPT_Wattributes, "%qE attribute ignored", name);
8054 *no_add_attrs = true;
8057 if (decl_in_symtab_p (*node))
8059 struct symtab_node *n = symtab_node::get (decl);
8060 if (n && n->refuse_visibility_changes)
8062 if (is_alias)
8063 error ("%+D declared alias after being used", decl);
8064 else
8065 error ("%+D declared ifunc after being used", decl);
8070 return NULL_TREE;
8073 /* Handle an "alias" or "ifunc" attribute; arguments as in
8074 struct attribute_spec.handler. */
8076 static tree
8077 handle_ifunc_attribute (tree *node, tree name, tree args,
8078 int ARG_UNUSED (flags), bool *no_add_attrs)
8080 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8083 /* Handle an "alias" or "ifunc" attribute; arguments as in
8084 struct attribute_spec.handler. */
8086 static tree
8087 handle_alias_attribute (tree *node, tree name, tree args,
8088 int ARG_UNUSED (flags), bool *no_add_attrs)
8090 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8093 /* Handle a "weakref" attribute; arguments as in struct
8094 attribute_spec.handler. */
8096 static tree
8097 handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8098 int flags, bool *no_add_attrs)
8100 tree attr = NULL_TREE;
8102 /* We must ignore the attribute when it is associated with
8103 local-scoped decls, since attribute alias is ignored and many
8104 such symbols do not even have a DECL_WEAK field. */
8105 if (decl_function_context (*node)
8106 || current_function_decl
8107 || !VAR_OR_FUNCTION_DECL_P (*node))
8109 warning (OPT_Wattributes, "%qE attribute ignored", name);
8110 *no_add_attrs = true;
8111 return NULL_TREE;
8114 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8116 error ("indirect function %q+D cannot be declared weakref", *node);
8117 *no_add_attrs = true;
8118 return NULL_TREE;
8121 /* The idea here is that `weakref("name")' mutates into `weakref,
8122 alias("name")', and weakref without arguments, in turn,
8123 implicitly adds weak. */
8125 if (args)
8127 attr = tree_cons (get_identifier ("alias"), args, attr);
8128 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8130 *no_add_attrs = true;
8132 decl_attributes (node, attr, flags);
8134 else
8136 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
8137 error_at (DECL_SOURCE_LOCATION (*node),
8138 "weakref attribute must appear before alias attribute");
8140 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8141 and that isn't supported; and because it wants to add it to
8142 the list of weak decls, which isn't helpful. */
8143 DECL_WEAK (*node) = 1;
8146 if (decl_in_symtab_p (*node))
8148 struct symtab_node *n = symtab_node::get (*node);
8149 if (n && n->refuse_visibility_changes)
8150 error ("%+D declared weakref after being used", *node);
8153 return NULL_TREE;
8156 /* Handle an "visibility" attribute; arguments as in
8157 struct attribute_spec.handler. */
8159 static tree
8160 handle_visibility_attribute (tree *node, tree name, tree args,
8161 int ARG_UNUSED (flags),
8162 bool *ARG_UNUSED (no_add_attrs))
8164 tree decl = *node;
8165 tree id = TREE_VALUE (args);
8166 enum symbol_visibility vis;
8168 if (TYPE_P (*node))
8170 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8171 /* OK */;
8172 else if (!RECORD_OR_UNION_TYPE_P (*node))
8174 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8175 name);
8176 return NULL_TREE;
8178 else if (TYPE_FIELDS (*node))
8180 error ("%qE attribute ignored because %qT is already defined",
8181 name, *node);
8182 return NULL_TREE;
8185 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
8187 warning (OPT_Wattributes, "%qE attribute ignored", name);
8188 return NULL_TREE;
8191 if (TREE_CODE (id) != STRING_CST)
8193 error ("visibility argument not a string");
8194 return NULL_TREE;
8197 /* If this is a type, set the visibility on the type decl. */
8198 if (TYPE_P (decl))
8200 decl = TYPE_NAME (decl);
8201 if (!decl)
8202 return NULL_TREE;
8203 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8205 warning (OPT_Wattributes, "%qE attribute ignored on types",
8206 name);
8207 return NULL_TREE;
8211 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
8212 vis = VISIBILITY_DEFAULT;
8213 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
8214 vis = VISIBILITY_INTERNAL;
8215 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
8216 vis = VISIBILITY_HIDDEN;
8217 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
8218 vis = VISIBILITY_PROTECTED;
8219 else
8221 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8222 vis = VISIBILITY_DEFAULT;
8225 if (DECL_VISIBILITY_SPECIFIED (decl)
8226 && vis != DECL_VISIBILITY (decl))
8228 tree attributes = (TYPE_P (*node)
8229 ? TYPE_ATTRIBUTES (*node)
8230 : DECL_ATTRIBUTES (decl));
8231 if (lookup_attribute ("visibility", attributes))
8232 error ("%qD redeclared with different visibility", decl);
8233 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8234 && lookup_attribute ("dllimport", attributes))
8235 error ("%qD was declared %qs which implies default visibility",
8236 decl, "dllimport");
8237 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8238 && lookup_attribute ("dllexport", attributes))
8239 error ("%qD was declared %qs which implies default visibility",
8240 decl, "dllexport");
8243 DECL_VISIBILITY (decl) = vis;
8244 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8246 /* Go ahead and attach the attribute to the node as well. This is needed
8247 so we can determine whether we have VISIBILITY_DEFAULT because the
8248 visibility was not specified, or because it was explicitly overridden
8249 from the containing scope. */
8251 return NULL_TREE;
8254 /* Determine the ELF symbol visibility for DECL, which is either a
8255 variable or a function. It is an error to use this function if a
8256 definition of DECL is not available in this translation unit.
8257 Returns true if the final visibility has been determined by this
8258 function; false if the caller is free to make additional
8259 modifications. */
8261 bool
8262 c_determine_visibility (tree decl)
8264 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
8266 /* If the user explicitly specified the visibility with an
8267 attribute, honor that. DECL_VISIBILITY will have been set during
8268 the processing of the attribute. We check for an explicit
8269 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8270 to distinguish the use of an attribute from the use of a "#pragma
8271 GCC visibility push(...)"; in the latter case we still want other
8272 considerations to be able to overrule the #pragma. */
8273 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8274 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8275 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8276 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
8277 return true;
8279 /* Set default visibility to whatever the user supplied with
8280 visibility_specified depending on #pragma GCC visibility. */
8281 if (!DECL_VISIBILITY_SPECIFIED (decl))
8283 if (visibility_options.inpragma
8284 || DECL_VISIBILITY (decl) != default_visibility)
8286 DECL_VISIBILITY (decl) = default_visibility;
8287 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8288 /* If visibility changed and DECL already has DECL_RTL, ensure
8289 symbol flags are updated. */
8290 if (((VAR_P (decl) && TREE_STATIC (decl))
8291 || TREE_CODE (decl) == FUNCTION_DECL)
8292 && DECL_RTL_SET_P (decl))
8293 make_decl_rtl (decl);
8296 return false;
8299 /* Handle an "tls_model" attribute; arguments as in
8300 struct attribute_spec.handler. */
8302 static tree
8303 handle_tls_model_attribute (tree *node, tree name, tree args,
8304 int ARG_UNUSED (flags), bool *no_add_attrs)
8306 tree id;
8307 tree decl = *node;
8308 enum tls_model kind;
8310 *no_add_attrs = true;
8312 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
8314 warning (OPT_Wattributes, "%qE attribute ignored", name);
8315 return NULL_TREE;
8318 kind = DECL_TLS_MODEL (decl);
8319 id = TREE_VALUE (args);
8320 if (TREE_CODE (id) != STRING_CST)
8322 error ("tls_model argument not a string");
8323 return NULL_TREE;
8326 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8327 kind = TLS_MODEL_LOCAL_EXEC;
8328 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8329 kind = TLS_MODEL_INITIAL_EXEC;
8330 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8331 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8332 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8333 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8334 else
8335 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8337 set_decl_tls_model (decl, kind);
8338 return NULL_TREE;
8341 /* Handle a "no_instrument_function" attribute; arguments as in
8342 struct attribute_spec.handler. */
8344 static tree
8345 handle_no_instrument_function_attribute (tree *node, tree name,
8346 tree ARG_UNUSED (args),
8347 int ARG_UNUSED (flags),
8348 bool *no_add_attrs)
8350 tree decl = *node;
8352 if (TREE_CODE (decl) != FUNCTION_DECL)
8354 error_at (DECL_SOURCE_LOCATION (decl),
8355 "%qE attribute applies only to functions", name);
8356 *no_add_attrs = true;
8358 else
8359 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8361 return NULL_TREE;
8364 /* Handle a "no_profile_instrument_function" attribute; arguments as in
8365 struct attribute_spec.handler. */
8367 static tree
8368 handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
8369 int, bool *no_add_attrs)
8371 if (TREE_CODE (*node) != FUNCTION_DECL)
8373 warning (OPT_Wattributes, "%qE attribute ignored", name);
8374 *no_add_attrs = true;
8377 return NULL_TREE;
8380 /* Handle a "malloc" attribute; arguments as in
8381 struct attribute_spec.handler. */
8383 static tree
8384 handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8385 int ARG_UNUSED (flags), bool *no_add_attrs)
8387 if (TREE_CODE (*node) == FUNCTION_DECL
8388 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
8389 DECL_IS_MALLOC (*node) = 1;
8390 else
8392 warning (OPT_Wattributes, "%qE attribute ignored", name);
8393 *no_add_attrs = true;
8396 return NULL_TREE;
8399 /* Handle a "alloc_size" attribute; arguments as in
8400 struct attribute_spec.handler. */
8402 static tree
8403 handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8404 int ARG_UNUSED (flags), bool *no_add_attrs)
8406 unsigned arg_count = type_num_arguments (*node);
8407 for (; args; args = TREE_CHAIN (args))
8409 tree position = TREE_VALUE (args);
8410 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8411 && TREE_CODE (position) != FUNCTION_DECL)
8412 position = default_conversion (position);
8414 if (!tree_fits_uhwi_p (position)
8415 || !arg_count
8416 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8418 warning (OPT_Wattributes,
8419 "alloc_size parameter outside range");
8420 *no_add_attrs = true;
8421 return NULL_TREE;
8424 return NULL_TREE;
8427 /* Handle a "alloc_align" attribute; arguments as in
8428 struct attribute_spec.handler. */
8430 static tree
8431 handle_alloc_align_attribute (tree *node, tree, tree args, int,
8432 bool *no_add_attrs)
8434 unsigned arg_count = type_num_arguments (*node);
8435 tree position = TREE_VALUE (args);
8436 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8437 && TREE_CODE (position) != FUNCTION_DECL)
8438 position = default_conversion (position);
8440 if (!tree_fits_uhwi_p (position)
8441 || !arg_count
8442 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8444 warning (OPT_Wattributes,
8445 "alloc_align parameter outside range");
8446 *no_add_attrs = true;
8447 return NULL_TREE;
8449 return NULL_TREE;
8452 /* Handle a "assume_aligned" attribute; arguments as in
8453 struct attribute_spec.handler. */
8455 static tree
8456 handle_assume_aligned_attribute (tree *, tree, tree args, int,
8457 bool *no_add_attrs)
8459 for (; args; args = TREE_CHAIN (args))
8461 tree position = TREE_VALUE (args);
8462 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8463 && TREE_CODE (position) != FUNCTION_DECL)
8464 position = default_conversion (position);
8466 if (TREE_CODE (position) != INTEGER_CST)
8468 warning (OPT_Wattributes,
8469 "assume_aligned parameter not integer constant");
8470 *no_add_attrs = true;
8471 return NULL_TREE;
8474 return NULL_TREE;
8477 /* Handle a "fn spec" attribute; arguments as in
8478 struct attribute_spec.handler. */
8480 static tree
8481 handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8482 tree args, int ARG_UNUSED (flags),
8483 bool *no_add_attrs ATTRIBUTE_UNUSED)
8485 gcc_assert (args
8486 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8487 && !TREE_CHAIN (args));
8488 return NULL_TREE;
8491 /* Handle a "bnd_variable_size" attribute; arguments as in
8492 struct attribute_spec.handler. */
8494 static tree
8495 handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8496 int ARG_UNUSED (flags), bool *no_add_attrs)
8498 if (TREE_CODE (*node) != FIELD_DECL)
8500 warning (OPT_Wattributes, "%qE attribute ignored", name);
8501 *no_add_attrs = true;
8504 return NULL_TREE;
8507 /* Handle a "bnd_legacy" attribute; arguments as in
8508 struct attribute_spec.handler. */
8510 static tree
8511 handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8512 int ARG_UNUSED (flags), bool *no_add_attrs)
8514 if (TREE_CODE (*node) != FUNCTION_DECL)
8516 warning (OPT_Wattributes, "%qE attribute ignored", name);
8517 *no_add_attrs = true;
8520 return NULL_TREE;
8523 /* Handle a "bnd_instrument" attribute; arguments as in
8524 struct attribute_spec.handler. */
8526 static tree
8527 handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8528 int ARG_UNUSED (flags), bool *no_add_attrs)
8530 if (TREE_CODE (*node) != FUNCTION_DECL)
8532 warning (OPT_Wattributes, "%qE attribute ignored", name);
8533 *no_add_attrs = true;
8536 return NULL_TREE;
8539 /* Handle a "warn_unused" attribute; arguments as in
8540 struct attribute_spec.handler. */
8542 static tree
8543 handle_warn_unused_attribute (tree *node, tree name,
8544 tree args ATTRIBUTE_UNUSED,
8545 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8547 if (TYPE_P (*node))
8548 /* Do nothing else, just set the attribute. We'll get at
8549 it later with lookup_attribute. */
8551 else
8553 warning (OPT_Wattributes, "%qE attribute ignored", name);
8554 *no_add_attrs = true;
8557 return NULL_TREE;
8560 /* Handle an "omp declare simd" attribute; arguments as in
8561 struct attribute_spec.handler. */
8563 static tree
8564 handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8566 return NULL_TREE;
8569 /* Handle a "simd" attribute. */
8571 static tree
8572 handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
8574 if (TREE_CODE (*node) == FUNCTION_DECL)
8576 if (lookup_attribute ("cilk simd function",
8577 DECL_ATTRIBUTES (*node)) != NULL)
8579 error_at (DECL_SOURCE_LOCATION (*node),
8580 "%<__simd__%> attribute cannot be used in the same "
8581 "function marked as a Cilk Plus SIMD-enabled function");
8582 *no_add_attrs = true;
8584 else
8586 tree t = get_identifier ("omp declare simd");
8587 tree attr = NULL_TREE;
8588 if (args)
8590 tree id = TREE_VALUE (args);
8592 if (TREE_CODE (id) != STRING_CST)
8594 error ("attribute %qE argument not a string", name);
8595 *no_add_attrs = true;
8596 return NULL_TREE;
8599 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8600 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8601 OMP_CLAUSE_NOTINBRANCH);
8602 else
8603 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8604 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8605 OMP_CLAUSE_INBRANCH);
8606 else
8608 error ("only %<inbranch%> and %<notinbranch%> flags are "
8609 "allowed for %<__simd__%> attribute");
8610 *no_add_attrs = true;
8611 return NULL_TREE;
8615 DECL_ATTRIBUTES (*node) = tree_cons (t,
8616 build_tree_list (NULL_TREE,
8617 attr),
8618 DECL_ATTRIBUTES (*node));
8621 else
8623 warning (OPT_Wattributes, "%qE attribute ignored", name);
8624 *no_add_attrs = true;
8627 return NULL_TREE;
8630 /* Handle an "omp declare target" attribute; arguments as in
8631 struct attribute_spec.handler. */
8633 static tree
8634 handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8636 return NULL_TREE;
8639 /* Handle a "returns_twice" attribute; arguments as in
8640 struct attribute_spec.handler. */
8642 static tree
8643 handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8644 int ARG_UNUSED (flags), bool *no_add_attrs)
8646 if (TREE_CODE (*node) == FUNCTION_DECL)
8647 DECL_IS_RETURNS_TWICE (*node) = 1;
8648 else
8650 warning (OPT_Wattributes, "%qE attribute ignored", name);
8651 *no_add_attrs = true;
8654 return NULL_TREE;
8657 /* Handle a "no_limit_stack" attribute; arguments as in
8658 struct attribute_spec.handler. */
8660 static tree
8661 handle_no_limit_stack_attribute (tree *node, tree name,
8662 tree ARG_UNUSED (args),
8663 int ARG_UNUSED (flags),
8664 bool *no_add_attrs)
8666 tree decl = *node;
8668 if (TREE_CODE (decl) != FUNCTION_DECL)
8670 error_at (DECL_SOURCE_LOCATION (decl),
8671 "%qE attribute applies only to functions", name);
8672 *no_add_attrs = true;
8674 else if (DECL_INITIAL (decl))
8676 error_at (DECL_SOURCE_LOCATION (decl),
8677 "can%'t set %qE attribute after definition", name);
8678 *no_add_attrs = true;
8680 else
8681 DECL_NO_LIMIT_STACK (decl) = 1;
8683 return NULL_TREE;
8686 /* Handle a "pure" attribute; arguments as in
8687 struct attribute_spec.handler. */
8689 static tree
8690 handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8691 int ARG_UNUSED (flags), bool *no_add_attrs)
8693 if (TREE_CODE (*node) == FUNCTION_DECL)
8694 DECL_PURE_P (*node) = 1;
8695 /* ??? TODO: Support types. */
8696 else
8698 warning (OPT_Wattributes, "%qE attribute ignored", name);
8699 *no_add_attrs = true;
8702 return NULL_TREE;
8705 /* Digest an attribute list destined for a transactional memory statement.
8706 ALLOWED is the set of attributes that are allowed for this statement;
8707 return the attribute we parsed. Multiple attributes are never allowed. */
8710 parse_tm_stmt_attr (tree attrs, int allowed)
8712 tree a_seen = NULL;
8713 int m_seen = 0;
8715 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8717 tree a = TREE_PURPOSE (attrs);
8718 int m = 0;
8720 if (is_attribute_p ("outer", a))
8721 m = TM_STMT_ATTR_OUTER;
8723 if ((m & allowed) == 0)
8725 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8726 continue;
8729 if (m_seen == 0)
8731 a_seen = a;
8732 m_seen = m;
8734 else if (m_seen == m)
8735 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8736 else
8737 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8740 return m_seen;
8743 /* Transform a TM attribute name into a maskable integer and back.
8744 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8745 to how the lack of an attribute is treated. */
8748 tm_attr_to_mask (tree attr)
8750 if (attr == NULL)
8751 return 0;
8752 if (is_attribute_p ("transaction_safe", attr))
8753 return TM_ATTR_SAFE;
8754 if (is_attribute_p ("transaction_callable", attr))
8755 return TM_ATTR_CALLABLE;
8756 if (is_attribute_p ("transaction_pure", attr))
8757 return TM_ATTR_PURE;
8758 if (is_attribute_p ("transaction_unsafe", attr))
8759 return TM_ATTR_IRREVOCABLE;
8760 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8761 return TM_ATTR_MAY_CANCEL_OUTER;
8762 return 0;
8765 tree
8766 tm_mask_to_attr (int mask)
8768 const char *str;
8769 switch (mask)
8771 case TM_ATTR_SAFE:
8772 str = "transaction_safe";
8773 break;
8774 case TM_ATTR_CALLABLE:
8775 str = "transaction_callable";
8776 break;
8777 case TM_ATTR_PURE:
8778 str = "transaction_pure";
8779 break;
8780 case TM_ATTR_IRREVOCABLE:
8781 str = "transaction_unsafe";
8782 break;
8783 case TM_ATTR_MAY_CANCEL_OUTER:
8784 str = "transaction_may_cancel_outer";
8785 break;
8786 default:
8787 gcc_unreachable ();
8789 return get_identifier (str);
8792 /* Return the first TM attribute seen in LIST. */
8794 tree
8795 find_tm_attribute (tree list)
8797 for (; list ; list = TREE_CHAIN (list))
8799 tree name = TREE_PURPOSE (list);
8800 if (tm_attr_to_mask (name) != 0)
8801 return name;
8803 return NULL_TREE;
8806 /* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8807 Here we accept only function types, and verify that none of the other
8808 function TM attributes are also applied. */
8809 /* ??? We need to accept class types for C++, but not C. This greatly
8810 complicates this function, since we can no longer rely on the extra
8811 processing given by function_type_required. */
8813 static tree
8814 handle_tm_attribute (tree *node, tree name, tree args,
8815 int flags, bool *no_add_attrs)
8817 /* Only one path adds the attribute; others don't. */
8818 *no_add_attrs = true;
8820 switch (TREE_CODE (*node))
8822 case RECORD_TYPE:
8823 case UNION_TYPE:
8824 /* Only tm_callable and tm_safe apply to classes. */
8825 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8826 goto ignored;
8827 /* FALLTHRU */
8829 case FUNCTION_TYPE:
8830 case METHOD_TYPE:
8832 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8833 if (old_name == name)
8835 else if (old_name != NULL_TREE)
8836 error ("type was previously declared %qE", old_name);
8837 else
8838 *no_add_attrs = false;
8840 break;
8842 case FUNCTION_DECL:
8844 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8845 want to set transaction_safe on the type. */
8846 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8847 if (!TYPE_P (DECL_CONTEXT (*node)))
8848 error_at (DECL_SOURCE_LOCATION (*node),
8849 "%<transaction_safe_dynamic%> may only be specified for "
8850 "a virtual function");
8851 *no_add_attrs = false;
8852 decl_attributes (&TREE_TYPE (*node),
8853 build_tree_list (get_identifier ("transaction_safe"),
8854 NULL_TREE),
8856 break;
8859 case POINTER_TYPE:
8861 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8862 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8864 tree fn_tmp = TREE_TYPE (*node);
8865 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8866 *node = build_pointer_type (fn_tmp);
8867 break;
8870 /* FALLTHRU */
8872 default:
8873 /* If a function is next, pass it on to be tried next. */
8874 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8875 return tree_cons (name, args, NULL);
8877 ignored:
8878 warning (OPT_Wattributes, "%qE attribute ignored", name);
8879 break;
8882 return NULL_TREE;
8885 /* Handle the TM_WRAP attribute; arguments as in
8886 struct attribute_spec.handler. */
8888 static tree
8889 handle_tm_wrap_attribute (tree *node, tree name, tree args,
8890 int ARG_UNUSED (flags), bool *no_add_attrs)
8892 tree decl = *node;
8894 /* We don't need the attribute even on success, since we
8895 record the entry in an external table. */
8896 *no_add_attrs = true;
8898 if (TREE_CODE (decl) != FUNCTION_DECL)
8899 warning (OPT_Wattributes, "%qE attribute ignored", name);
8900 else
8902 tree wrap_decl = TREE_VALUE (args);
8903 if (error_operand_p (wrap_decl))
8905 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
8906 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
8907 error ("%qE argument not an identifier", name);
8908 else
8910 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8911 wrap_decl = lookup_name (wrap_decl);
8912 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8914 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8915 TREE_TYPE (wrap_decl)))
8916 record_tm_replacement (wrap_decl, decl);
8917 else
8918 error ("%qD is not compatible with %qD", wrap_decl, decl);
8920 else
8921 error ("%qE argument is not a function", name);
8925 return NULL_TREE;
8928 /* Ignore the given attribute. Used when this attribute may be usefully
8929 overridden by the target, but is not used generically. */
8931 static tree
8932 ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8933 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8934 bool *no_add_attrs)
8936 *no_add_attrs = true;
8937 return NULL_TREE;
8940 /* Handle a "no vops" attribute; arguments as in
8941 struct attribute_spec.handler. */
8943 static tree
8944 handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8945 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8946 bool *ARG_UNUSED (no_add_attrs))
8948 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8949 DECL_IS_NOVOPS (*node) = 1;
8950 return NULL_TREE;
8953 /* Handle a "deprecated" attribute; arguments as in
8954 struct attribute_spec.handler. */
8956 static tree
8957 handle_deprecated_attribute (tree *node, tree name,
8958 tree args, int flags,
8959 bool *no_add_attrs)
8961 tree type = NULL_TREE;
8962 int warn = 0;
8963 tree what = NULL_TREE;
8965 if (!args)
8966 *no_add_attrs = true;
8967 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8969 error ("deprecated message is not a string");
8970 *no_add_attrs = true;
8973 if (DECL_P (*node))
8975 tree decl = *node;
8976 type = TREE_TYPE (decl);
8978 if (TREE_CODE (decl) == TYPE_DECL
8979 || TREE_CODE (decl) == PARM_DECL
8980 || VAR_OR_FUNCTION_DECL_P (decl)
8981 || TREE_CODE (decl) == FIELD_DECL
8982 || TREE_CODE (decl) == CONST_DECL
8983 || objc_method_decl (TREE_CODE (decl)))
8984 TREE_DEPRECATED (decl) = 1;
8985 else
8986 warn = 1;
8988 else if (TYPE_P (*node))
8990 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8991 *node = build_variant_type_copy (*node);
8992 TREE_DEPRECATED (*node) = 1;
8993 type = *node;
8995 else
8996 warn = 1;
8998 if (warn)
9000 *no_add_attrs = true;
9001 if (type && TYPE_NAME (type))
9003 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
9004 what = TYPE_NAME (*node);
9005 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9006 && DECL_NAME (TYPE_NAME (type)))
9007 what = DECL_NAME (TYPE_NAME (type));
9009 if (what)
9010 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
9011 else
9012 warning (OPT_Wattributes, "%qE attribute ignored", name);
9015 return NULL_TREE;
9018 /* Handle a "vector_size" attribute; arguments as in
9019 struct attribute_spec.handler. */
9021 static tree
9022 handle_vector_size_attribute (tree *node, tree name, tree args,
9023 int ARG_UNUSED (flags),
9024 bool *no_add_attrs)
9026 unsigned HOST_WIDE_INT vecsize, nunits;
9027 machine_mode orig_mode;
9028 tree type = *node, new_type, size;
9030 *no_add_attrs = true;
9032 size = TREE_VALUE (args);
9033 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9034 && TREE_CODE (size) != FUNCTION_DECL)
9035 size = default_conversion (size);
9037 if (!tree_fits_uhwi_p (size))
9039 warning (OPT_Wattributes, "%qE attribute ignored", name);
9040 return NULL_TREE;
9043 /* Get the vector size (in bytes). */
9044 vecsize = tree_to_uhwi (size);
9046 /* We need to provide for vector pointers, vector arrays, and
9047 functions returning vectors. For example:
9049 __attribute__((vector_size(16))) short *foo;
9051 In this case, the mode is SI, but the type being modified is
9052 HI, so we need to look further. */
9054 while (POINTER_TYPE_P (type)
9055 || TREE_CODE (type) == FUNCTION_TYPE
9056 || TREE_CODE (type) == METHOD_TYPE
9057 || TREE_CODE (type) == ARRAY_TYPE
9058 || TREE_CODE (type) == OFFSET_TYPE)
9059 type = TREE_TYPE (type);
9061 /* Get the mode of the type being modified. */
9062 orig_mode = TYPE_MODE (type);
9064 if ((!INTEGRAL_TYPE_P (type)
9065 && !SCALAR_FLOAT_TYPE_P (type)
9066 && !FIXED_POINT_TYPE_P (type))
9067 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9068 && GET_MODE_CLASS (orig_mode) != MODE_INT
9069 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
9070 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
9071 || TREE_CODE (type) == BOOLEAN_TYPE)
9073 error ("invalid vector type for attribute %qE", name);
9074 return NULL_TREE;
9077 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
9079 error ("vector size not an integral multiple of component size");
9080 return NULL;
9083 if (vecsize == 0)
9085 error ("zero vector size");
9086 return NULL;
9089 /* Calculate how many units fit in the vector. */
9090 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
9091 if (nunits & (nunits - 1))
9093 error ("number of components of the vector not a power of two");
9094 return NULL_TREE;
9097 new_type = build_vector_type (type, nunits);
9099 /* Build back pointers if needed. */
9100 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
9102 return NULL_TREE;
9105 /* Handle the "nonnull" attribute. */
9106 static tree
9107 handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9108 tree args, int ARG_UNUSED (flags),
9109 bool *no_add_attrs)
9111 tree type = *node;
9112 unsigned HOST_WIDE_INT attr_arg_num;
9114 /* If no arguments are specified, all pointer arguments should be
9115 non-null. Verify a full prototype is given so that the arguments
9116 will have the correct types when we actually check them later.
9117 Avoid diagnosing type-generic built-ins since those have no
9118 prototype. */
9119 if (!args)
9121 if (!prototype_p (type)
9122 && (!TYPE_ATTRIBUTES (type)
9123 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
9125 error ("nonnull attribute without arguments on a non-prototype");
9126 *no_add_attrs = true;
9128 return NULL_TREE;
9131 /* Argument list specified. Verify that each argument number references
9132 a pointer argument. */
9133 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
9135 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
9137 tree arg = TREE_VALUE (args);
9138 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9139 && TREE_CODE (arg) != FUNCTION_DECL)
9140 arg = default_conversion (arg);
9142 if (!get_nonnull_operand (arg, &arg_num))
9144 error ("nonnull argument has invalid operand number (argument %lu)",
9145 (unsigned long) attr_arg_num);
9146 *no_add_attrs = true;
9147 return NULL_TREE;
9150 if (prototype_p (type))
9152 function_args_iterator iter;
9153 tree argument;
9155 function_args_iter_init (&iter, type);
9156 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
9158 argument = function_args_iter_cond (&iter);
9159 if (argument == NULL_TREE || ck_num == arg_num)
9160 break;
9163 if (!argument
9164 || TREE_CODE (argument) == VOID_TYPE)
9166 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
9167 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9168 *no_add_attrs = true;
9169 return NULL_TREE;
9172 if (TREE_CODE (argument) != POINTER_TYPE)
9174 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
9175 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9176 *no_add_attrs = true;
9177 return NULL_TREE;
9182 return NULL_TREE;
9185 /* Check the argument list of a function call for null in argument slots
9186 that are marked as requiring a non-null pointer argument. The NARGS
9187 arguments are passed in the array ARGARRAY. */
9189 static void
9190 check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
9192 tree a;
9193 int i;
9195 attrs = lookup_attribute ("nonnull", attrs);
9196 if (attrs == NULL_TREE)
9197 return;
9199 a = attrs;
9200 /* See if any of the nonnull attributes has no arguments. If so,
9201 then every pointer argument is checked (in which case the check
9202 for pointer type is done in check_nonnull_arg). */
9203 if (TREE_VALUE (a) != NULL_TREE)
9205 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9206 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9208 if (a != NULL_TREE)
9209 for (i = 0; i < nargs; i++)
9210 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
9211 i + 1);
9212 else
9214 /* Walk the argument list. If we encounter an argument number we
9215 should check for non-null, do it. */
9216 for (i = 0; i < nargs; i++)
9218 for (a = attrs; ; a = TREE_CHAIN (a))
9220 a = lookup_attribute ("nonnull", a);
9221 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9222 break;
9225 if (a != NULL_TREE)
9226 check_function_arguments_recurse (check_nonnull_arg, &loc,
9227 argarray[i], i + 1);
9232 /* Check that the Nth argument of a function call (counting backwards
9233 from the end) is a (pointer)0. The NARGS arguments are passed in the
9234 array ARGARRAY. */
9236 static void
9237 check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
9239 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
9241 if (attr)
9243 int len = 0;
9244 int pos = 0;
9245 tree sentinel;
9246 function_args_iterator iter;
9247 tree t;
9249 /* Skip over the named arguments. */
9250 FOREACH_FUNCTION_ARGS (fntype, t, iter)
9252 if (len == nargs)
9253 break;
9254 len++;
9257 if (TREE_VALUE (attr))
9259 tree p = TREE_VALUE (TREE_VALUE (attr));
9260 pos = TREE_INT_CST_LOW (p);
9263 /* The sentinel must be one of the varargs, i.e.
9264 in position >= the number of fixed arguments. */
9265 if ((nargs - 1 - pos) < len)
9267 warning (OPT_Wformat_,
9268 "not enough variable arguments to fit a sentinel");
9269 return;
9272 /* Validate the sentinel. */
9273 sentinel = argarray[nargs - 1 - pos];
9274 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9275 || !integer_zerop (sentinel))
9276 /* Although __null (in C++) is only an integer we allow it
9277 nevertheless, as we are guaranteed that it's exactly
9278 as wide as a pointer, and we don't want to force
9279 users to cast the NULL they have written there.
9280 We warn with -Wstrict-null-sentinel, though. */
9281 && (warn_strict_null_sentinel || null_node != sentinel))
9282 warning (OPT_Wformat_, "missing sentinel in function call");
9286 /* Helper for check_function_nonnull; given a list of operands which
9287 must be non-null in ARGS, determine if operand PARAM_NUM should be
9288 checked. */
9290 static bool
9291 nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
9293 unsigned HOST_WIDE_INT arg_num = 0;
9295 for (; args; args = TREE_CHAIN (args))
9297 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9299 gcc_assert (found);
9301 if (arg_num == param_num)
9302 return true;
9304 return false;
9307 /* Check that the function argument PARAM (which is operand number
9308 PARAM_NUM) is non-null. This is called by check_function_nonnull
9309 via check_function_arguments_recurse. */
9311 static void
9312 check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
9314 location_t *ploc = (location_t *) ctx;
9316 /* Just skip checking the argument if it's not a pointer. This can
9317 happen if the "nonnull" attribute was given without an operand
9318 list (which means to check every pointer argument). */
9320 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9321 return;
9323 if (integer_zerop (param))
9324 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9325 "(argument %lu)", (unsigned long) param_num);
9328 /* Helper for nonnull attribute handling; fetch the operand number
9329 from the attribute argument list. */
9331 static bool
9332 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
9334 /* Verify the arg number is a small constant. */
9335 if (tree_fits_uhwi_p (arg_num_expr))
9337 *valp = TREE_INT_CST_LOW (arg_num_expr);
9338 return true;
9340 else
9341 return false;
9344 /* Handle a "nothrow" attribute; arguments as in
9345 struct attribute_spec.handler. */
9347 static tree
9348 handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9349 int ARG_UNUSED (flags), bool *no_add_attrs)
9351 if (TREE_CODE (*node) == FUNCTION_DECL)
9352 TREE_NOTHROW (*node) = 1;
9353 /* ??? TODO: Support types. */
9354 else
9356 warning (OPT_Wattributes, "%qE attribute ignored", name);
9357 *no_add_attrs = true;
9360 return NULL_TREE;
9363 /* Handle a "cleanup" attribute; arguments as in
9364 struct attribute_spec.handler. */
9366 static tree
9367 handle_cleanup_attribute (tree *node, tree name, tree args,
9368 int ARG_UNUSED (flags), bool *no_add_attrs)
9370 tree decl = *node;
9371 tree cleanup_id, cleanup_decl;
9373 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9374 for global destructors in C++. This requires infrastructure that
9375 we don't have generically at the moment. It's also not a feature
9376 we'd be missing too much, since we do have attribute constructor. */
9377 if (!VAR_P (decl) || TREE_STATIC (decl))
9379 warning (OPT_Wattributes, "%qE attribute ignored", name);
9380 *no_add_attrs = true;
9381 return NULL_TREE;
9384 /* Verify that the argument is a function in scope. */
9385 /* ??? We could support pointers to functions here as well, if
9386 that was considered desirable. */
9387 cleanup_id = TREE_VALUE (args);
9388 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9390 error ("cleanup argument not an identifier");
9391 *no_add_attrs = true;
9392 return NULL_TREE;
9394 cleanup_decl = lookup_name (cleanup_id);
9395 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9397 error ("cleanup argument not a function");
9398 *no_add_attrs = true;
9399 return NULL_TREE;
9402 /* That the function has proper type is checked with the
9403 eventual call to build_function_call. */
9405 return NULL_TREE;
9408 /* Handle a "warn_unused_result" attribute. No special handling. */
9410 static tree
9411 handle_warn_unused_result_attribute (tree *node, tree name,
9412 tree ARG_UNUSED (args),
9413 int ARG_UNUSED (flags), bool *no_add_attrs)
9415 /* Ignore the attribute for functions not returning any value. */
9416 if (VOID_TYPE_P (TREE_TYPE (*node)))
9418 warning (OPT_Wattributes, "%qE attribute ignored", name);
9419 *no_add_attrs = true;
9422 return NULL_TREE;
9425 /* Handle a "sentinel" attribute. */
9427 static tree
9428 handle_sentinel_attribute (tree *node, tree name, tree args,
9429 int ARG_UNUSED (flags), bool *no_add_attrs)
9431 if (!prototype_p (*node))
9433 warning (OPT_Wattributes,
9434 "%qE attribute requires prototypes with named arguments", name);
9435 *no_add_attrs = true;
9437 else
9439 if (!stdarg_p (*node))
9441 warning (OPT_Wattributes,
9442 "%qE attribute only applies to variadic functions", name);
9443 *no_add_attrs = true;
9447 if (args)
9449 tree position = TREE_VALUE (args);
9450 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9451 && TREE_CODE (position) != FUNCTION_DECL)
9452 position = default_conversion (position);
9454 if (TREE_CODE (position) != INTEGER_CST
9455 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
9457 warning (OPT_Wattributes,
9458 "requested position is not an integer constant");
9459 *no_add_attrs = true;
9461 else
9463 if (tree_int_cst_lt (position, integer_zero_node))
9465 warning (OPT_Wattributes,
9466 "requested position is less than zero");
9467 *no_add_attrs = true;
9472 return NULL_TREE;
9475 /* Handle a "type_generic" attribute. */
9477 static tree
9478 handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9479 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9480 bool * ARG_UNUSED (no_add_attrs))
9482 /* Ensure we have a function type. */
9483 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
9485 /* Ensure we have a variadic function. */
9486 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
9488 return NULL_TREE;
9491 /* Handle a "target" attribute. */
9493 static tree
9494 handle_target_attribute (tree *node, tree name, tree args, int flags,
9495 bool *no_add_attrs)
9497 /* Ensure we have a function type. */
9498 if (TREE_CODE (*node) != FUNCTION_DECL)
9500 warning (OPT_Wattributes, "%qE attribute ignored", name);
9501 *no_add_attrs = true;
9503 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9505 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9506 "with %qs attribute", name, "target_clones");
9507 *no_add_attrs = true;
9509 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
9510 flags))
9511 *no_add_attrs = true;
9513 return NULL_TREE;
9516 /* Handle a "target_clones" attribute. */
9518 static tree
9519 handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9520 int ARG_UNUSED (flags), bool *no_add_attrs)
9522 /* Ensure we have a function type. */
9523 if (TREE_CODE (*node) == FUNCTION_DECL)
9525 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9527 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9528 "with %qs attribute", name, "always_inline");
9529 *no_add_attrs = true;
9531 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9533 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9534 "with %qs attribute", name, "target");
9535 *no_add_attrs = true;
9537 else
9538 /* Do not inline functions with multiple clone targets. */
9539 DECL_UNINLINABLE (*node) = 1;
9541 else
9543 warning (OPT_Wattributes, "%qE attribute ignored", name);
9544 *no_add_attrs = true;
9546 return NULL_TREE;
9549 /* Arguments being collected for optimization. */
9550 typedef const char *const_char_p; /* For DEF_VEC_P. */
9551 static GTY(()) vec<const_char_p, va_gc> *optimize_args;
9554 /* Inner function to convert a TREE_LIST to argv string to parse the optimize
9555 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9556 false for #pragma GCC optimize. */
9558 bool
9559 parse_optimize_options (tree args, bool attr_p)
9561 bool ret = true;
9562 unsigned opt_argc;
9563 unsigned i;
9564 const char **opt_argv;
9565 struct cl_decoded_option *decoded_options;
9566 unsigned int decoded_options_count;
9567 tree ap;
9569 /* Build up argv vector. Just in case the string is stored away, use garbage
9570 collected strings. */
9571 vec_safe_truncate (optimize_args, 0);
9572 vec_safe_push (optimize_args, (const char *) NULL);
9574 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9576 tree value = TREE_VALUE (ap);
9578 if (TREE_CODE (value) == INTEGER_CST)
9580 char buffer[20];
9581 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9582 vec_safe_push (optimize_args, ggc_strdup (buffer));
9585 else if (TREE_CODE (value) == STRING_CST)
9587 /* Split string into multiple substrings. */
9588 size_t len = TREE_STRING_LENGTH (value);
9589 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9590 char *end = p + len;
9591 char *comma;
9592 char *next_p = p;
9594 while (next_p != NULL)
9596 size_t len2;
9597 char *q, *r;
9599 p = next_p;
9600 comma = strchr (p, ',');
9601 if (comma)
9603 len2 = comma - p;
9604 *comma = '\0';
9605 next_p = comma+1;
9607 else
9609 len2 = end - p;
9610 next_p = NULL;
9613 r = q = (char *) ggc_alloc_atomic (len2 + 3);
9615 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9616 options. */
9617 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9619 ret = false;
9620 if (attr_p)
9621 warning (OPT_Wattributes,
9622 "bad option %qs to attribute %<optimize%>", p);
9623 else
9624 warning (OPT_Wpragmas,
9625 "bad option %qs to pragma %<optimize%>", p);
9626 continue;
9629 if (*p != '-')
9631 *r++ = '-';
9633 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9634 itself is -Os, and any other switch begins with a -f. */
9635 if ((*p >= '0' && *p <= '9')
9636 || (p[0] == 's' && p[1] == '\0'))
9637 *r++ = 'O';
9638 else if (*p != 'O')
9639 *r++ = 'f';
9642 memcpy (r, p, len2);
9643 r[len2] = '\0';
9644 vec_safe_push (optimize_args, (const char *) q);
9650 opt_argc = optimize_args->length ();
9651 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9653 for (i = 1; i < opt_argc; i++)
9654 opt_argv[i] = (*optimize_args)[i];
9656 /* Now parse the options. */
9657 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9658 &decoded_options,
9659 &decoded_options_count);
9660 /* Drop non-Optimization options. */
9661 unsigned j = 1;
9662 for (i = 1; i < decoded_options_count; ++i)
9664 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
9666 ret = false;
9667 if (attr_p)
9668 warning (OPT_Wattributes,
9669 "bad option %qs to attribute %<optimize%>",
9670 decoded_options[i].orig_option_with_args_text);
9671 else
9672 warning (OPT_Wpragmas,
9673 "bad option %qs to pragma %<optimize%>",
9674 decoded_options[i].orig_option_with_args_text);
9675 continue;
9677 if (i != j)
9678 decoded_options[j] = decoded_options[i];
9679 j++;
9681 decoded_options_count = j;
9682 /* And apply them. */
9683 decode_options (&global_options, &global_options_set,
9684 decoded_options, decoded_options_count,
9685 input_location, global_dc);
9687 targetm.override_options_after_change();
9689 optimize_args->truncate (0);
9690 return ret;
9693 /* For handling "optimize" attribute. arguments as in
9694 struct attribute_spec.handler. */
9696 static tree
9697 handle_optimize_attribute (tree *node, tree name, tree args,
9698 int ARG_UNUSED (flags), bool *no_add_attrs)
9700 /* Ensure we have a function type. */
9701 if (TREE_CODE (*node) != FUNCTION_DECL)
9703 warning (OPT_Wattributes, "%qE attribute ignored", name);
9704 *no_add_attrs = true;
9706 else
9708 struct cl_optimization cur_opts;
9709 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9711 /* Save current options. */
9712 cl_optimization_save (&cur_opts, &global_options);
9714 /* If we previously had some optimization options, use them as the
9715 default. */
9716 if (old_opts)
9717 cl_optimization_restore (&global_options,
9718 TREE_OPTIMIZATION (old_opts));
9720 /* Parse options, and update the vector. */
9721 parse_optimize_options (args, true);
9722 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
9723 = build_optimization_node (&global_options);
9725 /* Restore current options. */
9726 cl_optimization_restore (&global_options, &cur_opts);
9729 return NULL_TREE;
9732 /* Handle a "no_split_stack" attribute. */
9734 static tree
9735 handle_no_split_stack_attribute (tree *node, tree name,
9736 tree ARG_UNUSED (args),
9737 int ARG_UNUSED (flags),
9738 bool *no_add_attrs)
9740 tree decl = *node;
9742 if (TREE_CODE (decl) != FUNCTION_DECL)
9744 error_at (DECL_SOURCE_LOCATION (decl),
9745 "%qE attribute applies only to functions", name);
9746 *no_add_attrs = true;
9748 else if (DECL_INITIAL (decl))
9750 error_at (DECL_SOURCE_LOCATION (decl),
9751 "can%'t set %qE attribute after definition", name);
9752 *no_add_attrs = true;
9755 return NULL_TREE;
9758 /* Handle a "returns_nonnull" attribute; arguments as in
9759 struct attribute_spec.handler. */
9761 static tree
9762 handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9763 bool *no_add_attrs)
9765 // Even without a prototype we still have a return type we can check.
9766 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9768 error ("returns_nonnull attribute on a function not returning a pointer");
9769 *no_add_attrs = true;
9771 return NULL_TREE;
9774 /* Handle a "designated_init" attribute; arguments as in
9775 struct attribute_spec.handler. */
9777 static tree
9778 handle_designated_init_attribute (tree *node, tree name, tree, int,
9779 bool *no_add_attrs)
9781 if (TREE_CODE (*node) != RECORD_TYPE)
9783 error ("%qE attribute is only valid on %<struct%> type", name);
9784 *no_add_attrs = true;
9786 return NULL_TREE;
9790 /* Check for valid arguments being passed to a function with FNTYPE.
9791 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9792 diagnostics. */
9793 void
9794 check_function_arguments (location_t loc, const_tree fntype, int nargs,
9795 tree *argarray)
9797 /* Check for null being passed in a pointer argument that must be
9798 non-null. We also need to do this if format checking is enabled. */
9800 if (warn_nonnull)
9801 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
9803 /* Check for errors in format strings. */
9805 if (warn_format || warn_suggest_attribute_format)
9806 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
9808 if (warn_format)
9809 check_function_sentinel (fntype, nargs, argarray);
9812 /* Generic argument checking recursion routine. PARAM is the argument to
9813 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9814 once the argument is resolved. CTX is context for the callback. */
9815 void
9816 check_function_arguments_recurse (void (*callback)
9817 (void *, tree, unsigned HOST_WIDE_INT),
9818 void *ctx, tree param,
9819 unsigned HOST_WIDE_INT param_num)
9821 if (CONVERT_EXPR_P (param)
9822 && (TYPE_PRECISION (TREE_TYPE (param))
9823 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
9825 /* Strip coercion. */
9826 check_function_arguments_recurse (callback, ctx,
9827 TREE_OPERAND (param, 0), param_num);
9828 return;
9831 if (TREE_CODE (param) == CALL_EXPR)
9833 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
9834 tree attrs;
9835 bool found_format_arg = false;
9837 /* See if this is a call to a known internationalization function
9838 that modifies a format arg. Such a function may have multiple
9839 format_arg attributes (for example, ngettext). */
9841 for (attrs = TYPE_ATTRIBUTES (type);
9842 attrs;
9843 attrs = TREE_CHAIN (attrs))
9844 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9846 tree inner_arg;
9847 tree format_num_expr;
9848 int format_num;
9849 int i;
9850 call_expr_arg_iterator iter;
9852 /* Extract the argument number, which was previously checked
9853 to be valid. */
9854 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
9856 format_num = tree_to_uhwi (format_num_expr);
9858 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9859 inner_arg != 0;
9860 inner_arg = next_call_expr_arg (&iter), i++)
9861 if (i == format_num)
9863 check_function_arguments_recurse (callback, ctx,
9864 inner_arg, param_num);
9865 found_format_arg = true;
9866 break;
9870 /* If we found a format_arg attribute and did a recursive check,
9871 we are done with checking this argument. Otherwise, we continue
9872 and this will be considered a non-literal. */
9873 if (found_format_arg)
9874 return;
9877 if (TREE_CODE (param) == COND_EXPR)
9879 /* Simplify to avoid warning for an impossible case. */
9880 param = fold_for_warn (param);
9881 if (TREE_CODE (param) == COND_EXPR)
9883 /* Check both halves of the conditional expression. */
9884 check_function_arguments_recurse (callback, ctx,
9885 TREE_OPERAND (param, 1),
9886 param_num);
9887 check_function_arguments_recurse (callback, ctx,
9888 TREE_OPERAND (param, 2),
9889 param_num);
9890 return;
9894 (*callback) (ctx, param, param_num);
9897 /* Checks for a builtin function FNDECL that the number of arguments
9898 NARGS against the required number REQUIRED and issues an error if
9899 there is a mismatch. Returns true if the number of arguments is
9900 correct, otherwise false. LOC is the location of FNDECL. */
9902 static bool
9903 builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
9904 int required)
9906 if (nargs < required)
9908 error_at (loc, "too few arguments to function %qE", fndecl);
9909 return false;
9911 else if (nargs > required)
9913 error_at (loc, "too many arguments to function %qE", fndecl);
9914 return false;
9916 return true;
9919 /* Helper macro for check_builtin_function_arguments. */
9920 #define ARG_LOCATION(N) \
9921 (arg_loc.is_empty () \
9922 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
9923 : expansion_point_location (arg_loc[(N)]))
9925 /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9926 Returns false if there was an error, otherwise true. LOC is the
9927 location of the function; ARG_LOC is a vector of locations of the
9928 arguments. */
9930 bool
9931 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
9932 tree fndecl, int nargs, tree *args)
9934 if (!DECL_BUILT_IN (fndecl)
9935 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9936 return true;
9938 switch (DECL_FUNCTION_CODE (fndecl))
9940 case BUILT_IN_ALLOCA_WITH_ALIGN:
9942 /* Get the requested alignment (in bits) if it's a constant
9943 integer expression. */
9944 unsigned HOST_WIDE_INT align
9945 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
9947 /* Determine if the requested alignment is a power of 2. */
9948 if ((align & (align - 1)))
9949 align = 0;
9951 /* The maximum alignment in bits corresponding to the same
9952 maximum in bytes enforced in check_user_alignment(). */
9953 unsigned maxalign = (UINT_MAX >> 1) + 1;
9955 /* Reject invalid alignments. */
9956 if (align < BITS_PER_UNIT || maxalign < align)
9958 error_at (ARG_LOCATION (1),
9959 "second argument to function %qE must be a constant "
9960 "integer power of 2 between %qi and %qu bits",
9961 fndecl, BITS_PER_UNIT, maxalign);
9962 return false;
9964 return true;
9967 case BUILT_IN_CONSTANT_P:
9968 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
9970 case BUILT_IN_ISFINITE:
9971 case BUILT_IN_ISINF:
9972 case BUILT_IN_ISINF_SIGN:
9973 case BUILT_IN_ISNAN:
9974 case BUILT_IN_ISNORMAL:
9975 case BUILT_IN_SIGNBIT:
9976 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
9978 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9980 error_at (ARG_LOCATION (0), "non-floating-point argument in "
9981 "call to function %qE", fndecl);
9982 return false;
9984 return true;
9986 return false;
9988 case BUILT_IN_ISGREATER:
9989 case BUILT_IN_ISGREATEREQUAL:
9990 case BUILT_IN_ISLESS:
9991 case BUILT_IN_ISLESSEQUAL:
9992 case BUILT_IN_ISLESSGREATER:
9993 case BUILT_IN_ISUNORDERED:
9994 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
9996 enum tree_code code0, code1;
9997 code0 = TREE_CODE (TREE_TYPE (args[0]));
9998 code1 = TREE_CODE (TREE_TYPE (args[1]));
9999 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10000 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10001 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10003 error_at (loc, "non-floating-point arguments in call to "
10004 "function %qE", fndecl);
10005 return false;
10007 return true;
10009 return false;
10011 case BUILT_IN_FPCLASSIFY:
10012 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
10014 for (unsigned int i = 0; i < 5; i++)
10015 if (TREE_CODE (args[i]) != INTEGER_CST)
10017 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
10018 "call to function %qE", i + 1, fndecl);
10019 return false;
10022 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10024 error_at (ARG_LOCATION (5), "non-floating-point argument in "
10025 "call to function %qE", fndecl);
10026 return false;
10028 return true;
10030 return false;
10032 case BUILT_IN_ASSUME_ALIGNED:
10033 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
10035 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10037 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
10038 "function %qE", fndecl);
10039 return false;
10041 return true;
10043 return false;
10045 case BUILT_IN_ADD_OVERFLOW:
10046 case BUILT_IN_SUB_OVERFLOW:
10047 case BUILT_IN_MUL_OVERFLOW:
10048 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
10050 unsigned i;
10051 for (i = 0; i < 2; i++)
10052 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10054 error_at (ARG_LOCATION (i), "argument %u in call to function "
10055 "%qE does not have integral type", i + 1, fndecl);
10056 return false;
10058 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10059 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
10061 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10062 "does not have pointer to integral type", fndecl);
10063 return false;
10065 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
10067 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10068 "has pointer to enumerated type", fndecl);
10069 return false;
10071 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
10073 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10074 "has pointer to boolean type", fndecl);
10075 return false;
10077 return true;
10079 return false;
10081 case BUILT_IN_ADD_OVERFLOW_P:
10082 case BUILT_IN_SUB_OVERFLOW_P:
10083 case BUILT_IN_MUL_OVERFLOW_P:
10084 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
10086 unsigned i;
10087 for (i = 0; i < 3; i++)
10088 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10090 error_at (ARG_LOCATION (i), "argument %u in call to function "
10091 "%qE does not have integral type", i + 1, fndecl);
10092 return false;
10094 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
10096 error_at (ARG_LOCATION (2), "argument 3 in call to function "
10097 "%qE has enumerated type", fndecl);
10098 return false;
10100 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
10102 error_at (ARG_LOCATION (2), "argument 3 in call to function "
10103 "%qE has boolean type", fndecl);
10104 return false;
10106 return true;
10108 return false;
10110 default:
10111 return true;
10115 /* Function to help qsort sort FIELD_DECLs by name order. */
10118 field_decl_cmp (const void *x_p, const void *y_p)
10120 const tree *const x = (const tree *const) x_p;
10121 const tree *const y = (const tree *const) y_p;
10123 if (DECL_NAME (*x) == DECL_NAME (*y))
10124 /* A nontype is "greater" than a type. */
10125 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10126 if (DECL_NAME (*x) == NULL_TREE)
10127 return -1;
10128 if (DECL_NAME (*y) == NULL_TREE)
10129 return 1;
10130 if (DECL_NAME (*x) < DECL_NAME (*y))
10131 return -1;
10132 return 1;
10135 static struct {
10136 gt_pointer_operator new_value;
10137 void *cookie;
10138 } resort_data;
10140 /* This routine compares two fields like field_decl_cmp but using the
10141 pointer operator in resort_data. */
10143 static int
10144 resort_field_decl_cmp (const void *x_p, const void *y_p)
10146 const tree *const x = (const tree *const) x_p;
10147 const tree *const y = (const tree *const) y_p;
10149 if (DECL_NAME (*x) == DECL_NAME (*y))
10150 /* A nontype is "greater" than a type. */
10151 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10152 if (DECL_NAME (*x) == NULL_TREE)
10153 return -1;
10154 if (DECL_NAME (*y) == NULL_TREE)
10155 return 1;
10157 tree d1 = DECL_NAME (*x);
10158 tree d2 = DECL_NAME (*y);
10159 resort_data.new_value (&d1, resort_data.cookie);
10160 resort_data.new_value (&d2, resort_data.cookie);
10161 if (d1 < d2)
10162 return -1;
10164 return 1;
10167 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10169 void
10170 resort_sorted_fields (void *obj,
10171 void * ARG_UNUSED (orig_obj),
10172 gt_pointer_operator new_value,
10173 void *cookie)
10175 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
10176 resort_data.new_value = new_value;
10177 resort_data.cookie = cookie;
10178 qsort (&sf->elts[0], sf->len, sizeof (tree),
10179 resort_field_decl_cmp);
10182 /* Subroutine of c_parse_error.
10183 Return the result of concatenating LHS and RHS. RHS is really
10184 a string literal, its first character is indicated by RHS_START and
10185 RHS_SIZE is its length (including the terminating NUL character).
10187 The caller is responsible for deleting the returned pointer. */
10189 static char *
10190 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10192 const int lhs_size = strlen (lhs);
10193 char *result = XNEWVEC (char, lhs_size + rhs_size);
10194 strncpy (result, lhs, lhs_size);
10195 strncpy (result + lhs_size, rhs_start, rhs_size);
10196 return result;
10199 /* Issue the error given by GMSGID, indicating that it occurred before
10200 TOKEN, which had the associated VALUE. */
10202 void
10203 c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
10204 tree value, unsigned char token_flags)
10206 #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10208 char *message = NULL;
10210 if (token_type == CPP_EOF)
10211 message = catenate_messages (gmsgid, " at end of input");
10212 else if (token_type == CPP_CHAR
10213 || token_type == CPP_WCHAR
10214 || token_type == CPP_CHAR16
10215 || token_type == CPP_CHAR32
10216 || token_type == CPP_UTF8CHAR)
10218 unsigned int val = TREE_INT_CST_LOW (value);
10219 const char *prefix;
10221 switch (token_type)
10223 default:
10224 prefix = "";
10225 break;
10226 case CPP_WCHAR:
10227 prefix = "L";
10228 break;
10229 case CPP_CHAR16:
10230 prefix = "u";
10231 break;
10232 case CPP_CHAR32:
10233 prefix = "U";
10234 break;
10235 case CPP_UTF8CHAR:
10236 prefix = "u8";
10237 break;
10240 if (val <= UCHAR_MAX && ISGRAPH (val))
10241 message = catenate_messages (gmsgid, " before %s'%c'");
10242 else
10243 message = catenate_messages (gmsgid, " before %s'\\x%x'");
10245 error (message, prefix, val);
10246 free (message);
10247 message = NULL;
10249 else if (token_type == CPP_CHAR_USERDEF
10250 || token_type == CPP_WCHAR_USERDEF
10251 || token_type == CPP_CHAR16_USERDEF
10252 || token_type == CPP_CHAR32_USERDEF
10253 || token_type == CPP_UTF8CHAR_USERDEF)
10254 message = catenate_messages (gmsgid,
10255 " before user-defined character literal");
10256 else if (token_type == CPP_STRING_USERDEF
10257 || token_type == CPP_WSTRING_USERDEF
10258 || token_type == CPP_STRING16_USERDEF
10259 || token_type == CPP_STRING32_USERDEF
10260 || token_type == CPP_UTF8STRING_USERDEF)
10261 message = catenate_messages (gmsgid, " before user-defined string literal");
10262 else if (token_type == CPP_STRING
10263 || token_type == CPP_WSTRING
10264 || token_type == CPP_STRING16
10265 || token_type == CPP_STRING32
10266 || token_type == CPP_UTF8STRING)
10267 message = catenate_messages (gmsgid, " before string constant");
10268 else if (token_type == CPP_NUMBER)
10269 message = catenate_messages (gmsgid, " before numeric constant");
10270 else if (token_type == CPP_NAME)
10272 message = catenate_messages (gmsgid, " before %qE");
10273 error (message, value);
10274 free (message);
10275 message = NULL;
10277 else if (token_type == CPP_PRAGMA)
10278 message = catenate_messages (gmsgid, " before %<#pragma%>");
10279 else if (token_type == CPP_PRAGMA_EOL)
10280 message = catenate_messages (gmsgid, " before end of line");
10281 else if (token_type == CPP_DECLTYPE)
10282 message = catenate_messages (gmsgid, " before %<decltype%>");
10283 else if (token_type < N_TTYPES)
10285 message = catenate_messages (gmsgid, " before %qs token");
10286 error (message, cpp_type2name (token_type, token_flags));
10287 free (message);
10288 message = NULL;
10290 else
10291 error (gmsgid);
10293 if (message)
10295 error (message);
10296 free (message);
10298 #undef catenate_messages
10301 /* Return the gcc option code associated with the reason for a cpp
10302 message, or 0 if none. */
10304 static int
10305 c_option_controlling_cpp_error (int reason)
10307 const struct cpp_reason_option_codes_t *entry;
10309 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
10311 if (entry->reason == reason)
10312 return entry->option_code;
10314 return 0;
10317 /* Callback from cpp_error for PFILE to print diagnostics from the
10318 preprocessor. The diagnostic is of type LEVEL, with REASON set
10319 to the reason code if LEVEL is represents a warning, at location
10320 RICHLOC unless this is after lexing and the compiler's location
10321 should be used instead; MSG is the translated message and AP
10322 the arguments. Returns true if a diagnostic was emitted, false
10323 otherwise. */
10325 bool
10326 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
10327 rich_location *richloc,
10328 const char *msg, va_list *ap)
10330 diagnostic_info diagnostic;
10331 diagnostic_t dlevel;
10332 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
10333 bool ret;
10335 switch (level)
10337 case CPP_DL_WARNING_SYSHDR:
10338 if (flag_no_output)
10339 return false;
10340 global_dc->dc_warn_system_headers = 1;
10341 /* Fall through. */
10342 case CPP_DL_WARNING:
10343 if (flag_no_output)
10344 return false;
10345 dlevel = DK_WARNING;
10346 break;
10347 case CPP_DL_PEDWARN:
10348 if (flag_no_output && !flag_pedantic_errors)
10349 return false;
10350 dlevel = DK_PEDWARN;
10351 break;
10352 case CPP_DL_ERROR:
10353 dlevel = DK_ERROR;
10354 break;
10355 case CPP_DL_ICE:
10356 dlevel = DK_ICE;
10357 break;
10358 case CPP_DL_NOTE:
10359 dlevel = DK_NOTE;
10360 break;
10361 case CPP_DL_FATAL:
10362 dlevel = DK_FATAL;
10363 break;
10364 default:
10365 gcc_unreachable ();
10367 if (done_lexing)
10368 richloc->set_range (line_table, 0, input_location, true);
10369 diagnostic_set_info_translated (&diagnostic, msg, ap,
10370 richloc, dlevel);
10371 diagnostic_override_option_index (&diagnostic,
10372 c_option_controlling_cpp_error (reason));
10373 ret = report_diagnostic (&diagnostic);
10374 if (level == CPP_DL_WARNING_SYSHDR)
10375 global_dc->dc_warn_system_headers = save_warn_system_headers;
10376 return ret;
10379 /* Convert a character from the host to the target execution character
10380 set. cpplib handles this, mostly. */
10382 HOST_WIDE_INT
10383 c_common_to_target_charset (HOST_WIDE_INT c)
10385 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10386 zero-extended under -fno-signed-char. cpplib insists that characters
10387 and character constants are always unsigned. Hence we must convert
10388 back and forth. */
10389 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10391 uc = cpp_host_to_exec_charset (parse_in, uc);
10393 if (flag_signed_char)
10394 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10395 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10396 else
10397 return uc;
10400 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
10401 references with an INDIRECT_REF of a constant at the bottom; much like the
10402 traditional rendering of offsetof as a macro. Return the folded result. */
10404 tree
10405 fold_offsetof_1 (tree expr, enum tree_code ctx)
10407 tree base, off, t;
10408 tree_code code = TREE_CODE (expr);
10409 switch (code)
10411 case ERROR_MARK:
10412 return expr;
10414 case VAR_DECL:
10415 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10416 return error_mark_node;
10418 case CALL_EXPR:
10419 case TARGET_EXPR:
10420 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10421 return error_mark_node;
10423 case NOP_EXPR:
10424 case INDIRECT_REF:
10425 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
10427 error ("cannot apply %<offsetof%> to a non constant address");
10428 return error_mark_node;
10430 return TREE_OPERAND (expr, 0);
10432 case COMPONENT_REF:
10433 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10434 if (base == error_mark_node)
10435 return base;
10437 t = TREE_OPERAND (expr, 1);
10438 if (DECL_C_BIT_FIELD (t))
10440 error ("attempt to take address of bit-field structure "
10441 "member %qD", t);
10442 return error_mark_node;
10444 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
10445 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
10446 / BITS_PER_UNIT));
10447 break;
10449 case ARRAY_REF:
10450 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
10451 if (base == error_mark_node)
10452 return base;
10454 t = TREE_OPERAND (expr, 1);
10456 /* Check if the offset goes beyond the upper bound of the array. */
10457 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
10459 tree upbound = array_ref_up_bound (expr);
10460 if (upbound != NULL_TREE
10461 && TREE_CODE (upbound) == INTEGER_CST
10462 && !tree_int_cst_equal (upbound,
10463 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10465 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10466 upbound = size_binop (PLUS_EXPR, upbound,
10467 build_int_cst (TREE_TYPE (upbound), 1));
10468 if (tree_int_cst_lt (upbound, t))
10470 tree v;
10472 for (v = TREE_OPERAND (expr, 0);
10473 TREE_CODE (v) == COMPONENT_REF;
10474 v = TREE_OPERAND (v, 0))
10475 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10476 == RECORD_TYPE)
10478 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10479 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
10480 if (TREE_CODE (fld_chain) == FIELD_DECL)
10481 break;
10483 if (fld_chain)
10484 break;
10486 /* Don't warn if the array might be considered a poor
10487 man's flexible array member with a very permissive
10488 definition thereof. */
10489 if (TREE_CODE (v) == ARRAY_REF
10490 || TREE_CODE (v) == COMPONENT_REF)
10491 warning (OPT_Warray_bounds,
10492 "index %E denotes an offset "
10493 "greater than size of %qT",
10494 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10499 t = convert (sizetype, t);
10500 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
10501 break;
10503 case COMPOUND_EXPR:
10504 /* Handle static members of volatile structs. */
10505 t = TREE_OPERAND (expr, 1);
10506 gcc_assert (VAR_P (t));
10507 return fold_offsetof_1 (t);
10509 default:
10510 gcc_unreachable ();
10513 return fold_build_pointer_plus (base, off);
10516 /* Likewise, but convert it to the return type of offsetof. */
10518 tree
10519 fold_offsetof (tree expr)
10521 return convert (size_type_node, fold_offsetof_1 (expr));
10524 /* Warn for A ?: C expressions (with B omitted) where A is a boolean
10525 expression, because B will always be true. */
10527 void
10528 warn_for_omitted_condop (location_t location, tree cond)
10530 if (truth_value_p (TREE_CODE (cond)))
10531 warning_at (location, OPT_Wparentheses,
10532 "the omitted middle operand in ?: will always be %<true%>, "
10533 "suggest explicit middle operand");
10536 /* Give an error for storing into ARG, which is 'const'. USE indicates
10537 how ARG was being used. */
10539 void
10540 readonly_error (location_t loc, tree arg, enum lvalue_use use)
10542 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10543 || use == lv_asm);
10544 /* Using this macro rather than (for example) arrays of messages
10545 ensures that all the format strings are checked at compile
10546 time. */
10547 #define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10548 : (use == lv_increment ? (I) \
10549 : (use == lv_decrement ? (D) : (AS))))
10550 if (TREE_CODE (arg) == COMPONENT_REF)
10552 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
10553 error_at (loc, READONLY_MSG (G_("assignment of member "
10554 "%qD in read-only object"),
10555 G_("increment of member "
10556 "%qD in read-only object"),
10557 G_("decrement of member "
10558 "%qD in read-only object"),
10559 G_("member %qD in read-only object "
10560 "used as %<asm%> output")),
10561 TREE_OPERAND (arg, 1));
10562 else
10563 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10564 G_("increment of read-only member %qD"),
10565 G_("decrement of read-only member %qD"),
10566 G_("read-only member %qD used as %<asm%> output")),
10567 TREE_OPERAND (arg, 1));
10569 else if (VAR_P (arg))
10570 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10571 G_("increment of read-only variable %qD"),
10572 G_("decrement of read-only variable %qD"),
10573 G_("read-only variable %qD used as %<asm%> output")),
10574 arg);
10575 else if (TREE_CODE (arg) == PARM_DECL)
10576 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10577 G_("increment of read-only parameter %qD"),
10578 G_("decrement of read-only parameter %qD"),
10579 G_("read-only parameter %qD use as %<asm%> output")),
10580 arg);
10581 else if (TREE_CODE (arg) == RESULT_DECL)
10583 gcc_assert (c_dialect_cxx ());
10584 error_at (loc, READONLY_MSG (G_("assignment of "
10585 "read-only named return value %qD"),
10586 G_("increment of "
10587 "read-only named return value %qD"),
10588 G_("decrement of "
10589 "read-only named return value %qD"),
10590 G_("read-only named return value %qD "
10591 "used as %<asm%>output")),
10592 arg);
10594 else if (TREE_CODE (arg) == FUNCTION_DECL)
10595 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10596 G_("increment of function %qD"),
10597 G_("decrement of function %qD"),
10598 G_("function %qD used as %<asm%> output")),
10599 arg);
10600 else
10601 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10602 G_("increment of read-only location %qE"),
10603 G_("decrement of read-only location %qE"),
10604 G_("read-only location %qE used as %<asm%> output")),
10605 arg);
10608 /* Print an error message for an invalid lvalue. USE says
10609 how the lvalue is being used and so selects the error message. LOC
10610 is the location for the error. */
10612 void
10613 lvalue_error (location_t loc, enum lvalue_use use)
10615 switch (use)
10617 case lv_assign:
10618 error_at (loc, "lvalue required as left operand of assignment");
10619 break;
10620 case lv_increment:
10621 error_at (loc, "lvalue required as increment operand");
10622 break;
10623 case lv_decrement:
10624 error_at (loc, "lvalue required as decrement operand");
10625 break;
10626 case lv_addressof:
10627 error_at (loc, "lvalue required as unary %<&%> operand");
10628 break;
10629 case lv_asm:
10630 error_at (loc, "lvalue required in asm statement");
10631 break;
10632 default:
10633 gcc_unreachable ();
10637 /* Print an error message for an invalid indirection of type TYPE.
10638 ERRSTRING is the name of the operator for the indirection. */
10640 void
10641 invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10643 switch (errstring)
10645 case RO_NULL:
10646 gcc_assert (c_dialect_cxx ());
10647 error_at (loc, "invalid type argument (have %qT)", type);
10648 break;
10649 case RO_ARRAY_INDEXING:
10650 error_at (loc,
10651 "invalid type argument of array indexing (have %qT)",
10652 type);
10653 break;
10654 case RO_UNARY_STAR:
10655 error_at (loc,
10656 "invalid type argument of unary %<*%> (have %qT)",
10657 type);
10658 break;
10659 case RO_ARROW:
10660 error_at (loc,
10661 "invalid type argument of %<->%> (have %qT)",
10662 type);
10663 break;
10664 case RO_ARROW_STAR:
10665 error_at (loc,
10666 "invalid type argument of %<->*%> (have %qT)",
10667 type);
10668 break;
10669 case RO_IMPLICIT_CONVERSION:
10670 error_at (loc,
10671 "invalid type argument of implicit conversion (have %qT)",
10672 type);
10673 break;
10674 default:
10675 gcc_unreachable ();
10679 /* *PTYPE is an incomplete array. Complete it with a domain based on
10680 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10681 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10682 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10685 complete_array_type (tree *ptype, tree initial_value, bool do_default)
10687 tree maxindex, type, main_type, elt, unqual_elt;
10688 int failure = 0, quals;
10689 hashval_t hashcode = 0;
10690 bool overflow_p = false;
10692 maxindex = size_zero_node;
10693 if (initial_value)
10695 if (TREE_CODE (initial_value) == STRING_CST)
10697 int eltsize
10698 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10699 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10701 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10703 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
10705 if (vec_safe_is_empty (v))
10707 if (pedantic)
10708 failure = 3;
10709 maxindex = ssize_int (-1);
10711 else
10713 tree curindex;
10714 unsigned HOST_WIDE_INT cnt;
10715 constructor_elt *ce;
10716 bool fold_p = false;
10718 if ((*v)[0].index)
10719 maxindex = (*v)[0].index, fold_p = true;
10721 curindex = maxindex;
10723 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
10725 bool curfold_p = false;
10726 if (ce->index)
10727 curindex = ce->index, curfold_p = true;
10728 else
10730 if (fold_p)
10732 /* Since we treat size types now as ordinary
10733 unsigned types, we need an explicit overflow
10734 check. */
10735 tree orig = curindex;
10736 curindex = fold_convert (sizetype, curindex);
10737 overflow_p |= tree_int_cst_lt (curindex, orig);
10739 curindex = size_binop (PLUS_EXPR, curindex,
10740 size_one_node);
10742 if (tree_int_cst_lt (maxindex, curindex))
10743 maxindex = curindex, fold_p = curfold_p;
10745 if (fold_p)
10747 tree orig = maxindex;
10748 maxindex = fold_convert (sizetype, maxindex);
10749 overflow_p |= tree_int_cst_lt (maxindex, orig);
10753 else
10755 /* Make an error message unless that happened already. */
10756 if (initial_value != error_mark_node)
10757 failure = 1;
10760 else
10762 failure = 2;
10763 if (!do_default)
10764 return failure;
10767 type = *ptype;
10768 elt = TREE_TYPE (type);
10769 quals = TYPE_QUALS (strip_array_types (elt));
10770 if (quals == 0)
10771 unqual_elt = elt;
10772 else
10773 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
10775 /* Using build_distinct_type_copy and modifying things afterward instead
10776 of using build_array_type to create a new type preserves all of the
10777 TYPE_LANG_FLAG_? bits that the front end may have set. */
10778 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10779 TREE_TYPE (main_type) = unqual_elt;
10780 TYPE_DOMAIN (main_type)
10781 = build_range_type (TREE_TYPE (maxindex),
10782 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
10783 layout_type (main_type);
10785 /* Make sure we have the canonical MAIN_TYPE. */
10786 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
10787 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
10788 hashcode);
10789 main_type = type_hash_canon (hashcode, main_type);
10791 /* Fix the canonical type. */
10792 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10793 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10794 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10795 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10796 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10797 != TYPE_DOMAIN (main_type)))
10798 TYPE_CANONICAL (main_type)
10799 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10800 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10801 else
10802 TYPE_CANONICAL (main_type) = main_type;
10804 if (quals == 0)
10805 type = main_type;
10806 else
10807 type = c_build_qualified_type (main_type, quals);
10809 if (COMPLETE_TYPE_P (type)
10810 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
10811 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
10813 error ("size of array is too large");
10814 /* If we proceed with the array type as it is, we'll eventually
10815 crash in tree_to_[su]hwi(). */
10816 type = error_mark_node;
10819 *ptype = type;
10820 return failure;
10823 /* Like c_mark_addressable but don't check register qualifier. */
10824 void
10825 c_common_mark_addressable_vec (tree t)
10827 while (handled_component_p (t))
10828 t = TREE_OPERAND (t, 0);
10829 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
10830 return;
10831 TREE_ADDRESSABLE (t) = 1;
10836 /* Used to help initialize the builtin-types.def table. When a type of
10837 the correct size doesn't exist, use error_mark_node instead of NULL.
10838 The later results in segfaults even when a decl using the type doesn't
10839 get invoked. */
10841 tree
10842 builtin_type_for_size (int size, bool unsignedp)
10844 tree type = c_common_type_for_size (size, unsignedp);
10845 return type ? type : error_mark_node;
10848 /* A helper function for resolve_overloaded_builtin in resolving the
10849 overloaded __sync_ builtins. Returns a positive power of 2 if the
10850 first operand of PARAMS is a pointer to a supported data type.
10851 Returns 0 if an error is encountered.
10852 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10853 built-ins. */
10855 static int
10856 sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
10858 /* Type of the argument. */
10859 tree argtype;
10860 /* Type the argument points to. */
10861 tree type;
10862 int size;
10864 if (vec_safe_is_empty (params))
10866 error ("too few arguments to function %qE", function);
10867 return 0;
10870 argtype = type = TREE_TYPE ((*params)[0]);
10871 if (TREE_CODE (type) == ARRAY_TYPE)
10873 /* Force array-to-pointer decay for C++. */
10874 gcc_assert (c_dialect_cxx());
10875 (*params)[0] = default_conversion ((*params)[0]);
10876 type = TREE_TYPE ((*params)[0]);
10878 if (TREE_CODE (type) != POINTER_TYPE)
10879 goto incompatible;
10881 type = TREE_TYPE (type);
10882 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10883 goto incompatible;
10885 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
10886 goto incompatible;
10888 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
10889 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
10890 return size;
10892 incompatible:
10893 /* Issue the diagnostic only if the argument is valid, otherwise
10894 it would be redundant at best and could be misleading. */
10895 if (argtype != error_mark_node)
10896 error ("operand type %qT is incompatible with argument %d of %qE",
10897 argtype, 1, function);
10898 return 0;
10901 /* A helper function for resolve_overloaded_builtin. Adds casts to
10902 PARAMS to make arguments match up with those of FUNCTION. Drops
10903 the variadic arguments at the end. Returns false if some error
10904 was encountered; true on success. */
10906 static bool
10907 sync_resolve_params (location_t loc, tree orig_function, tree function,
10908 vec<tree, va_gc> *params, bool orig_format)
10910 function_args_iterator iter;
10911 tree ptype;
10912 unsigned int parmnum;
10914 function_args_iter_init (&iter, TREE_TYPE (function));
10915 /* We've declared the implementation functions to use "volatile void *"
10916 as the pointer parameter, so we shouldn't get any complaints from the
10917 call to check_function_arguments what ever type the user used. */
10918 function_args_iter_next (&iter);
10919 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
10920 ptype = TYPE_MAIN_VARIANT (ptype);
10922 /* For the rest of the values, we need to cast these to FTYPE, so that we
10923 don't get warnings for passing pointer types, etc. */
10924 parmnum = 0;
10925 while (1)
10927 tree val, arg_type;
10929 arg_type = function_args_iter_cond (&iter);
10930 /* XXX void_type_node belies the abstraction. */
10931 if (arg_type == void_type_node)
10932 break;
10934 ++parmnum;
10935 if (params->length () <= parmnum)
10937 error_at (loc, "too few arguments to function %qE", orig_function);
10938 return false;
10941 /* Only convert parameters if arg_type is unsigned integer type with
10942 new format sync routines, i.e. don't attempt to convert pointer
10943 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10944 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10945 kinds). */
10946 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
10948 /* Ideally for the first conversion we'd use convert_for_assignment
10949 so that we get warnings for anything that doesn't match the pointer
10950 type. This isn't portable across the C and C++ front ends atm. */
10951 val = (*params)[parmnum];
10952 val = convert (ptype, val);
10953 val = convert (arg_type, val);
10954 (*params)[parmnum] = val;
10957 function_args_iter_next (&iter);
10960 /* __atomic routines are not variadic. */
10961 if (!orig_format && params->length () != parmnum + 1)
10963 error_at (loc, "too many arguments to function %qE", orig_function);
10964 return false;
10967 /* The definition of these primitives is variadic, with the remaining
10968 being "an optional list of variables protected by the memory barrier".
10969 No clue what that's supposed to mean, precisely, but we consider all
10970 call-clobbered variables to be protected so we're safe. */
10971 params->truncate (parmnum + 1);
10973 return true;
10976 /* A helper function for resolve_overloaded_builtin. Adds a cast to
10977 RESULT to make it match the type of the first pointer argument in
10978 PARAMS. */
10980 static tree
10981 sync_resolve_return (tree first_param, tree result, bool orig_format)
10983 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
10984 tree rtype = TREE_TYPE (result);
10985 ptype = TYPE_MAIN_VARIANT (ptype);
10987 /* New format doesn't require casting unless the types are the same size. */
10988 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10989 return convert (ptype, result);
10990 else
10991 return result;
10994 /* This function verifies the PARAMS to generic atomic FUNCTION.
10995 It returns the size if all the parameters are the same size, otherwise
10996 0 is returned if the parameters are invalid. */
10998 static int
10999 get_atomic_generic_size (location_t loc, tree function,
11000 vec<tree, va_gc> *params)
11002 unsigned int n_param;
11003 unsigned int n_model;
11004 unsigned int x;
11005 int size_0;
11006 tree type_0;
11008 /* Determine the parameter makeup. */
11009 switch (DECL_FUNCTION_CODE (function))
11011 case BUILT_IN_ATOMIC_EXCHANGE:
11012 n_param = 4;
11013 n_model = 1;
11014 break;
11015 case BUILT_IN_ATOMIC_LOAD:
11016 case BUILT_IN_ATOMIC_STORE:
11017 n_param = 3;
11018 n_model = 1;
11019 break;
11020 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11021 n_param = 6;
11022 n_model = 2;
11023 break;
11024 default:
11025 gcc_unreachable ();
11028 if (vec_safe_length (params) != n_param)
11030 error_at (loc, "incorrect number of arguments to function %qE", function);
11031 return 0;
11034 /* Get type of first parameter, and determine its size. */
11035 type_0 = TREE_TYPE ((*params)[0]);
11036 if (TREE_CODE (type_0) == ARRAY_TYPE)
11038 /* Force array-to-pointer decay for C++. */
11039 gcc_assert (c_dialect_cxx());
11040 (*params)[0] = default_conversion ((*params)[0]);
11041 type_0 = TREE_TYPE ((*params)[0]);
11043 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11045 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11046 function);
11047 return 0;
11050 /* Types must be compile time constant sizes. */
11051 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
11053 error_at (loc,
11054 "argument 1 of %qE must be a pointer to a constant size type",
11055 function);
11056 return 0;
11059 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
11061 /* Zero size objects are not allowed. */
11062 if (size_0 == 0)
11064 error_at (loc,
11065 "argument 1 of %qE must be a pointer to a nonzero size object",
11066 function);
11067 return 0;
11070 /* Check each other parameter is a pointer and the same size. */
11071 for (x = 0; x < n_param - n_model; x++)
11073 int size;
11074 tree type = TREE_TYPE ((*params)[x]);
11075 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
11076 if (n_param == 6 && x == 3)
11077 continue;
11078 if (!POINTER_TYPE_P (type))
11080 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11081 function);
11082 return 0;
11084 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11085 size = type_size ? tree_to_uhwi (type_size) : 0;
11086 if (size != size_0)
11088 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11089 function);
11090 return 0;
11094 /* Check memory model parameters for validity. */
11095 for (x = n_param - n_model ; x < n_param; x++)
11097 tree p = (*params)[x];
11098 if (TREE_CODE (p) == INTEGER_CST)
11100 int i = tree_to_uhwi (p);
11101 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
11103 warning_at (loc, OPT_Winvalid_memory_model,
11104 "invalid memory model argument %d of %qE", x + 1,
11105 function);
11108 else
11109 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11111 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11112 function);
11113 return 0;
11117 return size_0;
11121 /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11122 at the beginning of the parameter list PARAMS representing the size of the
11123 objects. This is to match the library ABI requirement. LOC is the location
11124 of the function call.
11125 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11126 returned to allow the external call to be constructed. */
11128 static tree
11129 add_atomic_size_parameter (unsigned n, location_t loc, tree function,
11130 vec<tree, va_gc> *params)
11132 tree size_node;
11134 /* Insert a SIZE_T parameter as the first param. If there isn't
11135 enough space, allocate a new vector and recursively re-build with that. */
11136 if (!params->space (1))
11138 unsigned int z, len;
11139 vec<tree, va_gc> *v;
11140 tree f;
11142 len = params->length ();
11143 vec_alloc (v, len + 1);
11144 v->quick_push (build_int_cst (size_type_node, n));
11145 for (z = 0; z < len; z++)
11146 v->quick_push ((*params)[z]);
11147 f = build_function_call_vec (loc, vNULL, function, v, NULL);
11148 vec_free (v);
11149 return f;
11152 /* Add the size parameter and leave as a function call for processing. */
11153 size_node = build_int_cst (size_type_node, n);
11154 params->quick_insert (0, size_node);
11155 return NULL_TREE;
11159 /* Return whether atomic operations for naturally aligned N-byte
11160 arguments are supported, whether inline or through libatomic. */
11161 static bool
11162 atomic_size_supported_p (int n)
11164 switch (n)
11166 case 1:
11167 case 2:
11168 case 4:
11169 case 8:
11170 return true;
11172 case 16:
11173 return targetm.scalar_mode_supported_p (TImode);
11175 default:
11176 return false;
11180 /* This will process an __atomic_exchange function call, determine whether it
11181 needs to be mapped to the _N variation, or turned into a library call.
11182 LOC is the location of the builtin call.
11183 FUNCTION is the DECL that has been invoked;
11184 PARAMS is the argument list for the call. The return value is non-null
11185 TRUE is returned if it is translated into the proper format for a call to the
11186 external library, and NEW_RETURN is set the tree for that function.
11187 FALSE is returned if processing for the _N variation is required, and
11188 NEW_RETURN is set to the return value the result is copied into. */
11189 static bool
11190 resolve_overloaded_atomic_exchange (location_t loc, tree function,
11191 vec<tree, va_gc> *params, tree *new_return)
11193 tree p0, p1, p2, p3;
11194 tree I_type, I_type_ptr;
11195 int n = get_atomic_generic_size (loc, function, params);
11197 /* Size of 0 is an error condition. */
11198 if (n == 0)
11200 *new_return = error_mark_node;
11201 return true;
11204 /* If not a lock-free size, change to the library generic format. */
11205 if (!atomic_size_supported_p (n))
11207 *new_return = add_atomic_size_parameter (n, loc, function, params);
11208 return true;
11211 /* Otherwise there is a lockfree match, transform the call from:
11212 void fn(T* mem, T* desired, T* return, model)
11213 into
11214 *return = (T) (fn (In* mem, (In) *desired, model)) */
11216 p0 = (*params)[0];
11217 p1 = (*params)[1];
11218 p2 = (*params)[2];
11219 p3 = (*params)[3];
11221 /* Create pointer to appropriate size. */
11222 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11223 I_type_ptr = build_pointer_type (I_type);
11225 /* Convert object pointer to required type. */
11226 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11227 (*params)[0] = p0;
11228 /* Convert new value to required type, and dereference it. */
11229 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11230 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11231 (*params)[1] = p1;
11233 /* Move memory model to the 3rd position, and end param list. */
11234 (*params)[2] = p3;
11235 params->truncate (3);
11237 /* Convert return pointer and dereference it for later assignment. */
11238 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11240 return false;
11244 /* This will process an __atomic_compare_exchange function call, determine
11245 whether it needs to be mapped to the _N variation, or turned into a lib call.
11246 LOC is the location of the builtin call.
11247 FUNCTION is the DECL that has been invoked;
11248 PARAMS is the argument list for the call. The return value is non-null
11249 TRUE is returned if it is translated into the proper format for a call to the
11250 external library, and NEW_RETURN is set the tree for that function.
11251 FALSE is returned if processing for the _N variation is required. */
11253 static bool
11254 resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
11255 vec<tree, va_gc> *params,
11256 tree *new_return)
11258 tree p0, p1, p2;
11259 tree I_type, I_type_ptr;
11260 int n = get_atomic_generic_size (loc, function, params);
11262 /* Size of 0 is an error condition. */
11263 if (n == 0)
11265 *new_return = error_mark_node;
11266 return true;
11269 /* If not a lock-free size, change to the library generic format. */
11270 if (!atomic_size_supported_p (n))
11272 /* The library generic format does not have the weak parameter, so
11273 remove it from the param list. Since a parameter has been removed,
11274 we can be sure that there is room for the SIZE_T parameter, meaning
11275 there will not be a recursive rebuilding of the parameter list, so
11276 there is no danger this will be done twice. */
11277 if (n > 0)
11279 (*params)[3] = (*params)[4];
11280 (*params)[4] = (*params)[5];
11281 params->truncate (5);
11283 *new_return = add_atomic_size_parameter (n, loc, function, params);
11284 return true;
11287 /* Otherwise, there is a match, so the call needs to be transformed from:
11288 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11289 into
11290 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11292 p0 = (*params)[0];
11293 p1 = (*params)[1];
11294 p2 = (*params)[2];
11296 /* Create pointer to appropriate size. */
11297 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11298 I_type_ptr = build_pointer_type (I_type);
11300 /* Convert object pointer to required type. */
11301 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11302 (*params)[0] = p0;
11304 /* Convert expected pointer to required type. */
11305 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
11306 (*params)[1] = p1;
11308 /* Convert desired value to required type, and dereference it. */
11309 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11310 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
11311 (*params)[2] = p2;
11313 /* The rest of the parameters are fine. NULL means no special return value
11314 processing.*/
11315 *new_return = NULL;
11316 return false;
11320 /* This will process an __atomic_load function call, determine whether it
11321 needs to be mapped to the _N variation, or turned into a library call.
11322 LOC is the location of the builtin call.
11323 FUNCTION is the DECL that has been invoked;
11324 PARAMS is the argument list for the call. The return value is non-null
11325 TRUE is returned if it is translated into the proper format for a call to the
11326 external library, and NEW_RETURN is set the tree for that function.
11327 FALSE is returned if processing for the _N variation is required, and
11328 NEW_RETURN is set to the return value the result is copied into. */
11330 static bool
11331 resolve_overloaded_atomic_load (location_t loc, tree function,
11332 vec<tree, va_gc> *params, tree *new_return)
11334 tree p0, p1, p2;
11335 tree I_type, I_type_ptr;
11336 int n = get_atomic_generic_size (loc, function, params);
11338 /* Size of 0 is an error condition. */
11339 if (n == 0)
11341 *new_return = error_mark_node;
11342 return true;
11345 /* If not a lock-free size, change to the library generic format. */
11346 if (!atomic_size_supported_p (n))
11348 *new_return = add_atomic_size_parameter (n, loc, function, params);
11349 return true;
11352 /* Otherwise, there is a match, so the call needs to be transformed from:
11353 void fn(T* mem, T* return, model)
11354 into
11355 *return = (T) (fn ((In *) mem, model)) */
11357 p0 = (*params)[0];
11358 p1 = (*params)[1];
11359 p2 = (*params)[2];
11361 /* Create pointer to appropriate size. */
11362 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11363 I_type_ptr = build_pointer_type (I_type);
11365 /* Convert object pointer to required type. */
11366 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11367 (*params)[0] = p0;
11369 /* Move memory model to the 2nd position, and end param list. */
11370 (*params)[1] = p2;
11371 params->truncate (2);
11373 /* Convert return pointer and dereference it for later assignment. */
11374 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11376 return false;
11380 /* This will process an __atomic_store function call, determine whether it
11381 needs to be mapped to the _N variation, or turned into a library call.
11382 LOC is the location of the builtin call.
11383 FUNCTION is the DECL that has been invoked;
11384 PARAMS is the argument list for the call. The return value is non-null
11385 TRUE is returned if it is translated into the proper format for a call to the
11386 external library, and NEW_RETURN is set the tree for that function.
11387 FALSE is returned if processing for the _N variation is required, and
11388 NEW_RETURN is set to the return value the result is copied into. */
11390 static bool
11391 resolve_overloaded_atomic_store (location_t loc, tree function,
11392 vec<tree, va_gc> *params, tree *new_return)
11394 tree p0, p1;
11395 tree I_type, I_type_ptr;
11396 int n = get_atomic_generic_size (loc, function, params);
11398 /* Size of 0 is an error condition. */
11399 if (n == 0)
11401 *new_return = error_mark_node;
11402 return true;
11405 /* If not a lock-free size, change to the library generic format. */
11406 if (!atomic_size_supported_p (n))
11408 *new_return = add_atomic_size_parameter (n, loc, function, params);
11409 return true;
11412 /* Otherwise, there is a match, so the call needs to be transformed from:
11413 void fn(T* mem, T* value, model)
11414 into
11415 fn ((In *) mem, (In) *value, model) */
11417 p0 = (*params)[0];
11418 p1 = (*params)[1];
11420 /* Create pointer to appropriate size. */
11421 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11422 I_type_ptr = build_pointer_type (I_type);
11424 /* Convert object pointer to required type. */
11425 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
11426 (*params)[0] = p0;
11428 /* Convert new value to required type, and dereference it. */
11429 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11430 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
11431 (*params)[1] = p1;
11433 /* The memory model is in the right spot already. Return is void. */
11434 *new_return = NULL_TREE;
11436 return false;
11440 /* Some builtin functions are placeholders for other expressions. This
11441 function should be called immediately after parsing the call expression
11442 before surrounding code has committed to the type of the expression.
11444 LOC is the location of the builtin call.
11446 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11447 PARAMS is the argument list for the call. The return value is non-null
11448 when expansion is complete, and null if normal processing should
11449 continue. */
11451 tree
11452 resolve_overloaded_builtin (location_t loc, tree function,
11453 vec<tree, va_gc> *params)
11455 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
11457 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11458 Those are not valid to call with a pointer to _Bool (or C++ bool)
11459 and so must be rejected. */
11460 bool fetch_op = true;
11461 bool orig_format = true;
11462 tree new_return = NULL_TREE;
11464 switch (DECL_BUILT_IN_CLASS (function))
11466 case BUILT_IN_NORMAL:
11467 break;
11468 case BUILT_IN_MD:
11469 if (targetm.resolve_overloaded_builtin)
11470 return targetm.resolve_overloaded_builtin (loc, function, params);
11471 else
11472 return NULL_TREE;
11473 default:
11474 return NULL_TREE;
11477 /* Handle BUILT_IN_NORMAL here. */
11478 switch (orig_code)
11480 case BUILT_IN_ATOMIC_EXCHANGE:
11481 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11482 case BUILT_IN_ATOMIC_LOAD:
11483 case BUILT_IN_ATOMIC_STORE:
11485 /* Handle these 4 together so that they can fall through to the next
11486 case if the call is transformed to an _N variant. */
11487 switch (orig_code)
11489 case BUILT_IN_ATOMIC_EXCHANGE:
11491 if (resolve_overloaded_atomic_exchange (loc, function, params,
11492 &new_return))
11493 return new_return;
11494 /* Change to the _N variant. */
11495 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11496 break;
11499 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11501 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11502 params,
11503 &new_return))
11504 return new_return;
11505 /* Change to the _N variant. */
11506 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11507 break;
11509 case BUILT_IN_ATOMIC_LOAD:
11511 if (resolve_overloaded_atomic_load (loc, function, params,
11512 &new_return))
11513 return new_return;
11514 /* Change to the _N variant. */
11515 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11516 break;
11518 case BUILT_IN_ATOMIC_STORE:
11520 if (resolve_overloaded_atomic_store (loc, function, params,
11521 &new_return))
11522 return new_return;
11523 /* Change to the _N variant. */
11524 orig_code = BUILT_IN_ATOMIC_STORE_N;
11525 break;
11527 default:
11528 gcc_unreachable ();
11530 /* Fallthrough to the normal processing. */
11532 case BUILT_IN_ATOMIC_EXCHANGE_N:
11533 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11534 case BUILT_IN_ATOMIC_LOAD_N:
11535 case BUILT_IN_ATOMIC_STORE_N:
11537 fetch_op = false;
11538 /* Fallthrough to further processing. */
11540 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11541 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11542 case BUILT_IN_ATOMIC_AND_FETCH_N:
11543 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11544 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11545 case BUILT_IN_ATOMIC_OR_FETCH_N:
11546 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11547 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11548 case BUILT_IN_ATOMIC_FETCH_AND_N:
11549 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11550 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11551 case BUILT_IN_ATOMIC_FETCH_OR_N:
11553 orig_format = false;
11554 /* Fallthru for parameter processing. */
11556 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11557 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11558 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11559 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11560 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11561 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11562 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11563 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11564 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11565 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11566 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11567 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11568 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11569 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11570 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11571 case BUILT_IN_SYNC_LOCK_RELEASE_N:
11573 /* The following are not _FETCH_OPs and must be accepted with
11574 pointers to _Bool (or C++ bool). */
11575 if (fetch_op)
11576 fetch_op =
11577 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11578 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11579 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11580 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11582 int n = sync_resolve_size (function, params, fetch_op);
11583 tree new_function, first_param, result;
11584 enum built_in_function fncode;
11586 if (n == 0)
11587 return error_mark_node;
11589 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11590 new_function = builtin_decl_explicit (fncode);
11591 if (!sync_resolve_params (loc, function, new_function, params,
11592 orig_format))
11593 return error_mark_node;
11595 first_param = (*params)[0];
11596 result = build_function_call_vec (loc, vNULL, new_function, params,
11597 NULL);
11598 if (result == error_mark_node)
11599 return result;
11600 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11601 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11602 && orig_code != BUILT_IN_ATOMIC_STORE_N
11603 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
11604 result = sync_resolve_return (first_param, result, orig_format);
11606 if (fetch_op)
11607 /* Prevent -Wunused-value warning. */
11608 TREE_USED (result) = true;
11610 /* If new_return is set, assign function to that expr and cast the
11611 result to void since the generic interface returned void. */
11612 if (new_return)
11614 /* Cast function result from I{1,2,4,8,16} to the required type. */
11615 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11616 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11617 result);
11618 TREE_SIDE_EFFECTS (result) = 1;
11619 protected_set_expr_location (result, loc);
11620 result = convert (void_type_node, result);
11622 return result;
11625 default:
11626 return NULL_TREE;
11630 /* vector_types_compatible_elements_p is used in type checks of vectors
11631 values used as operands of binary operators. Where it returns true, and
11632 the other checks of the caller succeed (being vector types in he first
11633 place, and matching number of elements), we can just treat the types
11634 as essentially the same.
11635 Contrast with vector_targets_convertible_p, which is used for vector
11636 pointer types, and vector_types_convertible_p, which will allow
11637 language-specific matches under the control of flag_lax_vector_conversions,
11638 and might still require a conversion. */
11639 /* True if vector types T1 and T2 can be inputs to the same binary
11640 operator without conversion.
11641 We don't check the overall vector size here because some of our callers
11642 want to give different error messages when the vectors are compatible
11643 except for the element count. */
11645 bool
11646 vector_types_compatible_elements_p (tree t1, tree t2)
11648 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11649 t1 = TREE_TYPE (t1);
11650 t2 = TREE_TYPE (t2);
11652 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11654 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11655 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11656 || c2 == FIXED_POINT_TYPE));
11658 t1 = c_common_signed_type (t1);
11659 t2 = c_common_signed_type (t2);
11660 /* Equality works here because c_common_signed_type uses
11661 TYPE_MAIN_VARIANT. */
11662 if (t1 == t2)
11663 return true;
11664 if (opaque && c1 == c2
11665 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11666 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11667 return true;
11668 return false;
11671 /* Check for missing format attributes on function pointers. LTYPE is
11672 the new type or left-hand side type. RTYPE is the old type or
11673 right-hand side type. Returns TRUE if LTYPE is missing the desired
11674 attribute. */
11676 bool
11677 check_missing_format_attribute (tree ltype, tree rtype)
11679 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11680 tree ra;
11682 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11683 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11684 break;
11685 if (ra)
11687 tree la;
11688 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11689 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11690 break;
11691 return !la;
11693 else
11694 return false;
11697 /* Subscripting with type char is likely to lose on a machine where
11698 chars are signed. So warn on any machine, but optionally. Don't
11699 warn for unsigned char since that type is safe. Don't warn for
11700 signed char because anyone who uses that must have done so
11701 deliberately. Furthermore, we reduce the false positive load by
11702 warning only for non-constant value of type char. */
11704 void
11705 warn_array_subscript_with_type_char (location_t loc, tree index)
11707 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11708 && TREE_CODE (index) != INTEGER_CST)
11709 warning_at (loc, OPT_Wchar_subscripts,
11710 "array subscript has type %<char%>");
11713 /* Implement -Wparentheses for the unexpected C precedence rules, to
11714 cover cases like x + y << z which readers are likely to
11715 misinterpret. We have seen an expression in which CODE is a binary
11716 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11717 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11718 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11719 expression was not formed using a binary or unary operator, or it
11720 was enclosed in parentheses. */
11722 void
11723 warn_about_parentheses (location_t loc, enum tree_code code,
11724 enum tree_code code_left, tree arg_left,
11725 enum tree_code code_right, tree arg_right)
11727 if (!warn_parentheses)
11728 return;
11730 /* This macro tests that the expression ARG with original tree code
11731 CODE appears to be a boolean expression. or the result of folding a
11732 boolean expression. */
11733 #define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11734 (truth_value_p (TREE_CODE (ARG)) \
11735 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11736 /* Folding may create 0 or 1 integers from other expressions. */ \
11737 || ((CODE) != INTEGER_CST \
11738 && (integer_onep (ARG) || integer_zerop (ARG))))
11740 switch (code)
11742 case LSHIFT_EXPR:
11743 if (code_left == PLUS_EXPR)
11744 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11745 "suggest parentheses around %<+%> inside %<<<%>");
11746 else if (code_right == PLUS_EXPR)
11747 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11748 "suggest parentheses around %<+%> inside %<<<%>");
11749 else if (code_left == MINUS_EXPR)
11750 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11751 "suggest parentheses around %<-%> inside %<<<%>");
11752 else if (code_right == MINUS_EXPR)
11753 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11754 "suggest parentheses around %<-%> inside %<<<%>");
11755 return;
11757 case RSHIFT_EXPR:
11758 if (code_left == PLUS_EXPR)
11759 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11760 "suggest parentheses around %<+%> inside %<>>%>");
11761 else if (code_right == PLUS_EXPR)
11762 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11763 "suggest parentheses around %<+%> inside %<>>%>");
11764 else if (code_left == MINUS_EXPR)
11765 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11766 "suggest parentheses around %<-%> inside %<>>%>");
11767 else if (code_right == MINUS_EXPR)
11768 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11769 "suggest parentheses around %<-%> inside %<>>%>");
11770 return;
11772 case TRUTH_ORIF_EXPR:
11773 if (code_left == TRUTH_ANDIF_EXPR)
11774 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11775 "suggest parentheses around %<&&%> within %<||%>");
11776 else if (code_right == TRUTH_ANDIF_EXPR)
11777 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11778 "suggest parentheses around %<&&%> within %<||%>");
11779 return;
11781 case BIT_IOR_EXPR:
11782 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
11783 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11784 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11785 "suggest parentheses around arithmetic in operand of %<|%>");
11786 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11787 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11788 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11789 "suggest parentheses around arithmetic in operand of %<|%>");
11790 /* Check cases like x|y==z */
11791 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11792 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11793 "suggest parentheses around comparison in operand of %<|%>");
11794 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11795 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11796 "suggest parentheses around comparison in operand of %<|%>");
11797 /* Check cases like !x | y */
11798 else if (code_left == TRUTH_NOT_EXPR
11799 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11800 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11801 "suggest parentheses around operand of "
11802 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
11803 return;
11805 case BIT_XOR_EXPR:
11806 if (code_left == BIT_AND_EXPR
11807 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11808 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11809 "suggest parentheses around arithmetic in operand of %<^%>");
11810 else if (code_right == BIT_AND_EXPR
11811 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11812 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11813 "suggest parentheses around arithmetic in operand of %<^%>");
11814 /* Check cases like x^y==z */
11815 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11816 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11817 "suggest parentheses around comparison in operand of %<^%>");
11818 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11819 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11820 "suggest parentheses around comparison in operand of %<^%>");
11821 return;
11823 case BIT_AND_EXPR:
11824 if (code_left == PLUS_EXPR)
11825 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11826 "suggest parentheses around %<+%> in operand of %<&%>");
11827 else if (code_right == PLUS_EXPR)
11828 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11829 "suggest parentheses around %<+%> in operand of %<&%>");
11830 else if (code_left == MINUS_EXPR)
11831 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11832 "suggest parentheses around %<-%> in operand of %<&%>");
11833 else if (code_right == MINUS_EXPR)
11834 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11835 "suggest parentheses around %<-%> in operand of %<&%>");
11836 /* Check cases like x&y==z */
11837 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11838 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11839 "suggest parentheses around comparison in operand of %<&%>");
11840 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11841 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11842 "suggest parentheses around comparison in operand of %<&%>");
11843 /* Check cases like !x & y */
11844 else if (code_left == TRUTH_NOT_EXPR
11845 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
11846 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11847 "suggest parentheses around operand of "
11848 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
11849 return;
11851 case EQ_EXPR:
11852 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11853 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11854 "suggest parentheses around comparison in operand of %<==%>");
11855 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11856 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11857 "suggest parentheses around comparison in operand of %<==%>");
11858 return;
11859 case NE_EXPR:
11860 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11861 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11862 "suggest parentheses around comparison in operand of %<!=%>");
11863 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11864 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11865 "suggest parentheses around comparison in operand of %<!=%>");
11866 return;
11868 default:
11869 if (TREE_CODE_CLASS (code) == tcc_comparison)
11871 if (TREE_CODE_CLASS (code_left) == tcc_comparison
11872 && code_left != NE_EXPR && code_left != EQ_EXPR
11873 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
11874 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11875 "comparisons like %<X<=Y<=Z%> do not "
11876 "have their mathematical meaning");
11877 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
11878 && code_right != NE_EXPR && code_right != EQ_EXPR
11879 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11880 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11881 "comparisons like %<X<=Y<=Z%> do not "
11882 "have their mathematical meaning");
11884 return;
11886 #undef NOT_A_BOOLEAN_EXPR_P
11889 /* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11891 void
11892 warn_for_unused_label (tree label)
11894 if (!TREE_USED (label))
11896 if (DECL_INITIAL (label))
11897 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11898 else
11899 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11903 /* Warn for division by zero according to the value of DIVISOR. LOC
11904 is the location of the division operator. */
11906 void
11907 warn_for_div_by_zero (location_t loc, tree divisor)
11909 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11910 about division by zero. Do not issue a warning if DIVISOR has a
11911 floating-point type, since we consider 0.0/0.0 a valid way of
11912 generating a NaN. */
11913 if (c_inhibit_evaluation_warnings == 0
11914 && (integer_zerop (divisor) || fixed_zerop (divisor)))
11915 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
11918 /* Warn for patterns where memset appears to be used incorrectly. The
11919 warning location should be LOC. ARG0, and ARG2 are the first and
11920 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
11921 each argument that was a literal zero. */
11923 void
11924 warn_for_memset (location_t loc, tree arg0, tree arg2,
11925 int literal_zero_mask)
11927 if (warn_memset_transposed_args
11928 && integer_zerop (arg2)
11929 && (literal_zero_mask & (1 << 2)) != 0
11930 && (literal_zero_mask & (1 << 1)) == 0)
11931 warning_at (loc, OPT_Wmemset_transposed_args,
11932 "%<memset%> used with constant zero length "
11933 "parameter; this could be due to transposed "
11934 "parameters");
11936 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
11938 STRIP_NOPS (arg0);
11939 if (TREE_CODE (arg0) == ADDR_EXPR)
11940 arg0 = TREE_OPERAND (arg0, 0);
11941 tree type = TREE_TYPE (arg0);
11942 if (TREE_CODE (type) == ARRAY_TYPE)
11944 tree elt_type = TREE_TYPE (type);
11945 tree domain = TYPE_DOMAIN (type);
11946 if (!integer_onep (TYPE_SIZE_UNIT (elt_type))
11947 && domain != NULL_TREE
11948 && TYPE_MAXVAL (domain)
11949 && TYPE_MINVAL (domain)
11950 && integer_zerop (TYPE_MINVAL (domain))
11951 && integer_onep (fold_build2 (MINUS_EXPR, domain,
11952 arg2,
11953 TYPE_MAXVAL (domain))))
11954 warning_at (loc, OPT_Wmemset_elt_size,
11955 "%<memset%> used with length equal to "
11956 "number of elements without multiplication "
11957 "by element size");
11962 /* Subroutine of build_binary_op. Give warnings for comparisons
11963 between signed and unsigned quantities that may fail. Do the
11964 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11965 so that casts will be considered, but default promotions won't
11968 LOCATION is the location of the comparison operator.
11970 The arguments of this function map directly to local variables
11971 of build_binary_op. */
11973 void
11974 warn_for_sign_compare (location_t location,
11975 tree orig_op0, tree orig_op1,
11976 tree op0, tree op1,
11977 tree result_type, enum tree_code resultcode)
11979 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11980 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11981 int unsignedp0, unsignedp1;
11983 /* In C++, check for comparison of different enum types. */
11984 if (c_dialect_cxx()
11985 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11986 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11987 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
11988 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
11990 warning_at (location,
11991 OPT_Wsign_compare, "comparison between types %qT and %qT",
11992 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
11995 /* Do not warn if the comparison is being done in a signed type,
11996 since the signed type will only be chosen if it can represent
11997 all the values of the unsigned type. */
11998 if (!TYPE_UNSIGNED (result_type))
11999 /* OK */;
12000 /* Do not warn if both operands are unsigned. */
12001 else if (op0_signed == op1_signed)
12002 /* OK */;
12003 else
12005 tree sop, uop, base_type;
12006 bool ovf;
12008 if (op0_signed)
12009 sop = orig_op0, uop = orig_op1;
12010 else
12011 sop = orig_op1, uop = orig_op0;
12013 STRIP_TYPE_NOPS (sop);
12014 STRIP_TYPE_NOPS (uop);
12015 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12016 ? TREE_TYPE (result_type) : result_type);
12018 /* Do not warn if the signed quantity is an unsuffixed integer
12019 literal (or some static constant expression involving such
12020 literals or a conditional expression involving such literals)
12021 and it is non-negative. */
12022 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12023 /* OK */;
12024 /* Do not warn if the comparison is an equality operation, the
12025 unsigned quantity is an integral constant, and it would fit
12026 in the result if the result were signed. */
12027 else if (TREE_CODE (uop) == INTEGER_CST
12028 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
12029 && int_fits_type_p (uop, c_common_signed_type (base_type)))
12030 /* OK */;
12031 /* In C, do not warn if the unsigned quantity is an enumeration
12032 constant and its maximum value would fit in the result if the
12033 result were signed. */
12034 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12035 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12036 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
12037 c_common_signed_type (base_type)))
12038 /* OK */;
12039 else
12040 warning_at (location,
12041 OPT_Wsign_compare,
12042 "comparison between signed and unsigned integer expressions");
12045 /* Warn if two unsigned values are being compared in a size larger
12046 than their original size, and one (and only one) is the result of
12047 a `~' operator. This comparison will always fail.
12049 Also warn if one operand is a constant, and the constant does not
12050 have all bits set that are set in the ~ operand when it is
12051 extended. */
12053 op0 = c_common_get_narrower (op0, &unsignedp0);
12054 op1 = c_common_get_narrower (op1, &unsignedp1);
12056 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12057 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12059 if (TREE_CODE (op0) == BIT_NOT_EXPR)
12060 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
12061 if (TREE_CODE (op1) == BIT_NOT_EXPR)
12062 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
12064 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
12066 tree primop;
12067 HOST_WIDE_INT constant, mask;
12068 int unsignedp;
12069 unsigned int bits;
12071 if (tree_fits_shwi_p (op0))
12073 primop = op1;
12074 unsignedp = unsignedp1;
12075 constant = tree_to_shwi (op0);
12077 else
12079 primop = op0;
12080 unsignedp = unsignedp0;
12081 constant = tree_to_shwi (op1);
12084 bits = TYPE_PRECISION (TREE_TYPE (primop));
12085 if (bits < TYPE_PRECISION (result_type)
12086 && bits < HOST_BITS_PER_LONG && unsignedp)
12088 mask = HOST_WIDE_INT_M1U << bits;
12089 if ((mask & constant) != mask)
12091 if (constant == 0)
12092 warning_at (location, OPT_Wsign_compare,
12093 "promoted ~unsigned is always non-zero");
12094 else
12095 warning_at (location, OPT_Wsign_compare,
12096 "comparison of promoted ~unsigned with constant");
12100 else if (unsignedp0 && unsignedp1
12101 && (TYPE_PRECISION (TREE_TYPE (op0))
12102 < TYPE_PRECISION (result_type))
12103 && (TYPE_PRECISION (TREE_TYPE (op1))
12104 < TYPE_PRECISION (result_type)))
12105 warning_at (location, OPT_Wsign_compare,
12106 "comparison of promoted ~unsigned with unsigned");
12110 /* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12111 type via c_common_type. If -Wdouble-promotion is in use, and the
12112 conditions for warning have been met, issue a warning. GMSGID is
12113 the warning message. It must have two %T specifiers for the type
12114 that was converted (generally "float") and the type to which it was
12115 converted (generally "double), respectively. LOC is the location
12116 to which the awrning should refer. */
12118 void
12119 do_warn_double_promotion (tree result_type, tree type1, tree type2,
12120 const char *gmsgid, location_t loc)
12122 tree source_type;
12124 if (!warn_double_promotion)
12125 return;
12126 /* If the conversion will not occur at run-time, there is no need to
12127 warn about it. */
12128 if (c_inhibit_evaluation_warnings)
12129 return;
12130 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12131 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12132 return;
12133 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12134 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12135 source_type = type1;
12136 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12137 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12138 source_type = type2;
12139 else
12140 return;
12141 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12144 /* Possibly warn about unused parameters. */
12146 void
12147 do_warn_unused_parameter (tree fn)
12149 tree decl;
12151 for (decl = DECL_ARGUMENTS (fn);
12152 decl; decl = DECL_CHAIN (decl))
12153 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12154 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12155 && !TREE_NO_WARNING (decl))
12156 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12157 "unused parameter %qD", decl);
12161 /* Setup a TYPE_DECL node as a typedef representation.
12163 X is a TYPE_DECL for a typedef statement. Create a brand new
12164 ..._TYPE node (which will be just a variant of the existing
12165 ..._TYPE node with identical properties) and then install X
12166 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12168 The whole point here is to end up with a situation where each
12169 and every ..._TYPE node the compiler creates will be uniquely
12170 associated with AT MOST one node representing a typedef name.
12171 This way, even though the compiler substitutes corresponding
12172 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12173 early on, later parts of the compiler can always do the reverse
12174 translation and get back the corresponding typedef name. For
12175 example, given:
12177 typedef struct S MY_TYPE;
12178 MY_TYPE object;
12180 Later parts of the compiler might only know that `object' was of
12181 type `struct S' if it were not for code just below. With this
12182 code however, later parts of the compiler see something like:
12184 struct S' == struct S
12185 typedef struct S' MY_TYPE;
12186 struct S' object;
12188 And they can then deduce (from the node for type struct S') that
12189 the original object declaration was:
12191 MY_TYPE object;
12193 Being able to do this is important for proper support of protoize,
12194 and also for generating precise symbolic debugging information
12195 which takes full account of the programmer's (typedef) vocabulary.
12197 Obviously, we don't want to generate a duplicate ..._TYPE node if
12198 the TYPE_DECL node that we are now processing really represents a
12199 standard built-in type. */
12201 void
12202 set_underlying_type (tree x)
12204 if (x == error_mark_node)
12205 return;
12206 if (DECL_IS_BUILTIN (x))
12208 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12209 TYPE_NAME (TREE_TYPE (x)) = x;
12211 else if (TREE_TYPE (x) != error_mark_node
12212 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12214 tree tt = TREE_TYPE (x);
12215 DECL_ORIGINAL_TYPE (x) = tt;
12216 tt = build_variant_type_copy (tt);
12217 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12218 TYPE_NAME (tt) = x;
12219 TREE_USED (tt) = TREE_USED (x);
12220 TREE_TYPE (x) = tt;
12224 /* Record the types used by the current global variable declaration
12225 being parsed, so that we can decide later to emit their debug info.
12226 Those types are in types_used_by_cur_var_decl, and we are going to
12227 store them in the types_used_by_vars_hash hash table.
12228 DECL is the declaration of the global variable that has been parsed. */
12230 void
12231 record_types_used_by_current_var_decl (tree decl)
12233 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12235 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
12237 tree type = types_used_by_cur_var_decl->pop ();
12238 types_used_by_var_decl_insert (type, decl);
12242 /* If DECL is a typedef that is declared in the current function,
12243 record it for the purpose of -Wunused-local-typedefs. */
12245 void
12246 record_locally_defined_typedef (tree decl)
12248 struct c_language_function *l;
12250 if (!warn_unused_local_typedefs
12251 || cfun == NULL
12252 /* if this is not a locally defined typedef then we are not
12253 interested. */
12254 || !is_typedef_decl (decl)
12255 || !decl_function_context (decl))
12256 return;
12258 l = (struct c_language_function *) cfun->language;
12259 vec_safe_push (l->local_typedefs, decl);
12262 /* If T is a TYPE_DECL declared locally, mark it as used. */
12264 void
12265 maybe_record_typedef_use (tree t)
12267 if (!is_typedef_decl (t))
12268 return;
12270 TREE_USED (t) = true;
12273 /* Warn if there are some unused locally defined typedefs in the
12274 current function. */
12276 void
12277 maybe_warn_unused_local_typedefs (void)
12279 int i;
12280 tree decl;
12281 /* The number of times we have emitted -Wunused-local-typedefs
12282 warnings. If this is different from errorcount, that means some
12283 unrelated errors have been issued. In which case, we'll avoid
12284 emitting "unused-local-typedefs" warnings. */
12285 static int unused_local_typedefs_warn_count;
12286 struct c_language_function *l;
12288 if (cfun == NULL)
12289 return;
12291 if ((l = (struct c_language_function *) cfun->language) == NULL)
12292 return;
12294 if (warn_unused_local_typedefs
12295 && errorcount == unused_local_typedefs_warn_count)
12297 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
12298 if (!TREE_USED (decl))
12299 warning_at (DECL_SOURCE_LOCATION (decl),
12300 OPT_Wunused_local_typedefs,
12301 "typedef %qD locally defined but not used", decl);
12302 unused_local_typedefs_warn_count = errorcount;
12305 vec_free (l->local_typedefs);
12308 /* Warn about boolean expression compared with an integer value different
12309 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12310 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12311 are the operands of the comparison. The caller must ensure that
12312 either operand is a boolean expression. */
12314 void
12315 maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12316 tree op1)
12318 if (TREE_CODE_CLASS (code) != tcc_comparison)
12319 return;
12321 tree f, cst;
12322 if (f = fold_for_warn (op0),
12323 TREE_CODE (f) == INTEGER_CST)
12324 cst = op0 = f;
12325 else if (f = fold_for_warn (op1),
12326 TREE_CODE (f) == INTEGER_CST)
12327 cst = op1 = f;
12328 else
12329 return;
12331 if (!integer_zerop (cst) && !integer_onep (cst))
12333 int sign = (TREE_CODE (op0) == INTEGER_CST
12334 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
12335 if (code == EQ_EXPR
12336 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12337 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12338 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12339 "with boolean expression is always false", cst);
12340 else
12341 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12342 "with boolean expression is always true", cst);
12344 else if (integer_zerop (cst) || integer_onep (cst))
12346 /* If the non-constant operand isn't of a boolean type, we
12347 don't want to warn here. */
12348 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12349 /* Handle booleans promoted to integers. */
12350 if (CONVERT_EXPR_P (noncst)
12351 && TREE_TYPE (noncst) == integer_type_node
12352 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12353 /* Warn. */;
12354 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12355 && !truth_value_p (TREE_CODE (noncst)))
12356 return;
12357 /* Do some magic to get the right diagnostics. */
12358 bool flag = TREE_CODE (op0) == INTEGER_CST;
12359 flag = integer_zerop (cst) ? flag : !flag;
12360 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12361 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12362 "with boolean expression is always true", cst);
12363 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12364 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12365 "with boolean expression is always false", cst);
12369 /* Warn if signed left shift overflows. We don't warn
12370 about left-shifting 1 into the sign bit in C++14; cf.
12371 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12372 LOC is a location of the shift; OP0 and OP1 are the operands.
12373 Return true if an overflow is detected, false otherwise. */
12375 bool
12376 maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12378 if (TREE_CODE (op0) != INTEGER_CST
12379 || TREE_CODE (op1) != INTEGER_CST)
12380 return false;
12382 tree type0 = TREE_TYPE (op0);
12383 unsigned int prec0 = TYPE_PRECISION (type0);
12385 /* Left-hand operand must be signed. */
12386 if (TYPE_UNSIGNED (type0))
12387 return false;
12389 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12390 + TREE_INT_CST_LOW (op1));
12391 /* Handle the case of left-shifting 1 into the sign bit.
12392 * However, shifting 1 _out_ of the sign bit, as in
12393 * INT_MIN << 1, is considered an overflow.
12395 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
12397 /* Never warn for C++14 onwards. */
12398 if (cxx_dialect >= cxx14)
12399 return false;
12400 /* Otherwise only if -Wshift-overflow=2. But return
12401 true to signal an overflow for the sake of integer
12402 constant expressions. */
12403 if (warn_shift_overflow < 2)
12404 return true;
12407 bool overflowed = min_prec > prec0;
12408 if (overflowed && c_inhibit_evaluation_warnings == 0)
12409 warning_at (loc, OPT_Wshift_overflow_,
12410 "result of %qE requires %u bits to represent, "
12411 "but %qT only has %u bits",
12412 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12413 min_prec, type0, prec0);
12415 return overflowed;
12418 /* The C and C++ parsers both use vectors to hold function arguments.
12419 For efficiency, we keep a cache of unused vectors. This is the
12420 cache. */
12422 typedef vec<tree, va_gc> *tree_gc_vec;
12423 static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
12425 /* Return a new vector from the cache. If the cache is empty,
12426 allocate a new vector. These vectors are GC'ed, so it is OK if the
12427 pointer is not released.. */
12429 vec<tree, va_gc> *
12430 make_tree_vector (void)
12432 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12433 return tree_vector_cache->pop ();
12434 else
12436 /* Passing 0 to vec::alloc returns NULL, and our callers require
12437 that we always return a non-NULL value. The vector code uses
12438 4 when growing a NULL vector, so we do too. */
12439 vec<tree, va_gc> *v;
12440 vec_alloc (v, 4);
12441 return v;
12445 /* Release a vector of trees back to the cache. */
12447 void
12448 release_tree_vector (vec<tree, va_gc> *vec)
12450 if (vec != NULL)
12452 vec->truncate (0);
12453 vec_safe_push (tree_vector_cache, vec);
12457 /* Get a new tree vector holding a single tree. */
12459 vec<tree, va_gc> *
12460 make_tree_vector_single (tree t)
12462 vec<tree, va_gc> *ret = make_tree_vector ();
12463 ret->quick_push (t);
12464 return ret;
12467 /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12469 vec<tree, va_gc> *
12470 make_tree_vector_from_list (tree list)
12472 vec<tree, va_gc> *ret = make_tree_vector ();
12473 for (; list; list = TREE_CHAIN (list))
12474 vec_safe_push (ret, TREE_VALUE (list));
12475 return ret;
12478 /* Get a new tree vector which is a copy of an existing one. */
12480 vec<tree, va_gc> *
12481 make_tree_vector_copy (const vec<tree, va_gc> *orig)
12483 vec<tree, va_gc> *ret;
12484 unsigned int ix;
12485 tree t;
12487 ret = make_tree_vector ();
12488 vec_safe_reserve (ret, vec_safe_length (orig));
12489 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12490 ret->quick_push (t);
12491 return ret;
12494 /* Return true if KEYWORD starts a type specifier. */
12496 bool
12497 keyword_begins_type_specifier (enum rid keyword)
12499 switch (keyword)
12501 case RID_AUTO_TYPE:
12502 case RID_INT:
12503 case RID_CHAR:
12504 case RID_FLOAT:
12505 case RID_DOUBLE:
12506 case RID_VOID:
12507 case RID_UNSIGNED:
12508 case RID_LONG:
12509 case RID_SHORT:
12510 case RID_SIGNED:
12511 case RID_DFLOAT32:
12512 case RID_DFLOAT64:
12513 case RID_DFLOAT128:
12514 case RID_FRACT:
12515 case RID_ACCUM:
12516 case RID_BOOL:
12517 case RID_WCHAR:
12518 case RID_CHAR16:
12519 case RID_CHAR32:
12520 case RID_SAT:
12521 case RID_COMPLEX:
12522 case RID_TYPEOF:
12523 case RID_STRUCT:
12524 case RID_CLASS:
12525 case RID_UNION:
12526 case RID_ENUM:
12527 return true;
12528 default:
12529 if (keyword >= RID_FIRST_INT_N
12530 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12531 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12532 return true;
12533 return false;
12537 /* Return true if KEYWORD names a type qualifier. */
12539 bool
12540 keyword_is_type_qualifier (enum rid keyword)
12542 switch (keyword)
12544 case RID_CONST:
12545 case RID_VOLATILE:
12546 case RID_RESTRICT:
12547 case RID_ATOMIC:
12548 return true;
12549 default:
12550 return false;
12554 /* Return true if KEYWORD names a storage class specifier.
12556 RID_TYPEDEF is not included in this list despite `typedef' being
12557 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12558 such for syntactic convenience only. */
12560 bool
12561 keyword_is_storage_class_specifier (enum rid keyword)
12563 switch (keyword)
12565 case RID_STATIC:
12566 case RID_EXTERN:
12567 case RID_REGISTER:
12568 case RID_AUTO:
12569 case RID_MUTABLE:
12570 case RID_THREAD:
12571 return true;
12572 default:
12573 return false;
12577 /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12579 static bool
12580 keyword_is_function_specifier (enum rid keyword)
12582 switch (keyword)
12584 case RID_INLINE:
12585 case RID_NORETURN:
12586 case RID_VIRTUAL:
12587 case RID_EXPLICIT:
12588 return true;
12589 default:
12590 return false;
12594 /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12595 declaration-specifier (C99 6.7). */
12597 bool
12598 keyword_is_decl_specifier (enum rid keyword)
12600 if (keyword_is_storage_class_specifier (keyword)
12601 || keyword_is_type_qualifier (keyword)
12602 || keyword_is_function_specifier (keyword))
12603 return true;
12605 switch (keyword)
12607 case RID_TYPEDEF:
12608 case RID_FRIEND:
12609 case RID_CONSTEXPR:
12610 return true;
12611 default:
12612 return false;
12616 /* Initialize language-specific-bits of tree_contains_struct. */
12618 void
12619 c_common_init_ts (void)
12621 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12622 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
12623 MARK_TS_TYPED (ARRAY_NOTATION_REF);
12626 /* Build a user-defined numeric literal out of an integer constant type VALUE
12627 with identifier SUFFIX. */
12629 tree
12630 build_userdef_literal (tree suffix_id, tree value,
12631 enum overflow_type overflow, tree num_string)
12633 tree literal = make_node (USERDEF_LITERAL);
12634 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12635 USERDEF_LITERAL_VALUE (literal) = value;
12636 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
12637 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12638 return literal;
12641 /* For vector[index], convert the vector to an array of the underlying type.
12642 Return true if the resulting ARRAY_REF should not be an lvalue. */
12644 bool
12645 convert_vector_to_array_for_subscript (location_t loc,
12646 tree *vecp, tree index)
12648 bool ret = false;
12649 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
12651 tree type = TREE_TYPE (*vecp);
12653 ret = !lvalue_p (*vecp);
12655 if (TREE_CODE (index) == INTEGER_CST)
12656 if (!tree_fits_uhwi_p (index)
12657 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
12658 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12660 /* We are building an ARRAY_REF so mark the vector as addressable
12661 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
12662 for function parameters. */
12663 c_common_mark_addressable_vec (*vecp);
12665 *vecp = build1 (VIEW_CONVERT_EXPR,
12666 build_array_type_nelts (TREE_TYPE (type),
12667 TYPE_VECTOR_SUBPARTS (type)),
12668 *vecp);
12670 return ret;
12673 /* Determine which of the operands, if any, is a scalar that needs to be
12674 converted to a vector, for the range of operations. */
12675 enum stv_conv
12676 scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12677 bool complain)
12679 tree type0 = TREE_TYPE (op0);
12680 tree type1 = TREE_TYPE (op1);
12681 bool integer_only_op = false;
12682 enum stv_conv ret = stv_firstarg;
12684 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
12685 switch (code)
12687 /* Most GENERIC binary expressions require homogeneous arguments.
12688 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12689 argument that is a vector and a second one that is a scalar, so
12690 we never return stv_secondarg for them. */
12691 case RSHIFT_EXPR:
12692 case LSHIFT_EXPR:
12693 if (TREE_CODE (type0) == INTEGER_TYPE
12694 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12696 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12698 if (complain)
12699 error_at (loc, "conversion of scalar %qT to vector %qT "
12700 "involves truncation", type0, type1);
12701 return stv_error;
12703 else
12704 return stv_firstarg;
12706 break;
12708 case BIT_IOR_EXPR:
12709 case BIT_XOR_EXPR:
12710 case BIT_AND_EXPR:
12711 integer_only_op = true;
12712 /* ... fall through ... */
12714 case VEC_COND_EXPR:
12716 case PLUS_EXPR:
12717 case MINUS_EXPR:
12718 case MULT_EXPR:
12719 case TRUNC_DIV_EXPR:
12720 case CEIL_DIV_EXPR:
12721 case FLOOR_DIV_EXPR:
12722 case ROUND_DIV_EXPR:
12723 case EXACT_DIV_EXPR:
12724 case TRUNC_MOD_EXPR:
12725 case FLOOR_MOD_EXPR:
12726 case RDIV_EXPR:
12727 case EQ_EXPR:
12728 case NE_EXPR:
12729 case LE_EXPR:
12730 case GE_EXPR:
12731 case LT_EXPR:
12732 case GT_EXPR:
12733 /* What about UNLT_EXPR? */
12734 if (VECTOR_TYPE_P (type0))
12736 ret = stv_secondarg;
12737 std::swap (type0, type1);
12738 std::swap (op0, op1);
12741 if (TREE_CODE (type0) == INTEGER_TYPE
12742 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12744 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12746 if (complain)
12747 error_at (loc, "conversion of scalar %qT to vector %qT "
12748 "involves truncation", type0, type1);
12749 return stv_error;
12751 return ret;
12753 else if (!integer_only_op
12754 /* Allow integer --> real conversion if safe. */
12755 && (TREE_CODE (type0) == REAL_TYPE
12756 || TREE_CODE (type0) == INTEGER_TYPE)
12757 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12759 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
12761 if (complain)
12762 error_at (loc, "conversion of scalar %qT to vector %qT "
12763 "involves truncation", type0, type1);
12764 return stv_error;
12766 return ret;
12768 default:
12769 break;
12772 return stv_nothing;
12775 /* Return true iff ALIGN is an integral constant that is a fundamental
12776 alignment, as defined by [basic.align] in the c++-11
12777 specifications.
12779 That is:
12781 [A fundamental alignment is represented by an alignment less than or
12782 equal to the greatest alignment supported by the implementation
12783 in all contexts, which is equal to
12784 alignof(max_align_t)]. */
12786 bool
12787 cxx_fundamental_alignment_p (unsigned align)
12789 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12790 TYPE_ALIGN (long_double_type_node)));
12793 /* Return true if T is a pointer to a zero-sized aggregate. */
12795 bool
12796 pointer_to_zero_sized_aggr_p (tree t)
12798 if (!POINTER_TYPE_P (t))
12799 return false;
12800 t = TREE_TYPE (t);
12801 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12804 /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12805 with no library fallback or for an ADDR_EXPR whose operand is such type
12806 issues an error pointing to the location LOC.
12807 Returns true when the expression has been diagnosed and false
12808 otherwise. */
12809 bool
12810 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12812 if (TREE_CODE (expr) == ADDR_EXPR)
12813 expr = TREE_OPERAND (expr, 0);
12815 if (TREE_TYPE (expr)
12816 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12817 && TREE_CODE (expr) == FUNCTION_DECL
12818 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12819 false positives for user-declared built-ins such as abs or
12820 strlen, and for C++ operators new and delete.
12821 The c_decl_implicit() test avoids false positives for implicitly
12822 declared built-ins with library fallbacks (such as abs). */
12823 && DECL_BUILT_IN (expr)
12824 && DECL_IS_BUILTIN (expr)
12825 && !c_decl_implicit (expr)
12826 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12828 if (loc == UNKNOWN_LOCATION)
12829 loc = EXPR_LOC_OR_LOC (expr, input_location);
12831 /* Reject arguments that are built-in functions with
12832 no library fallback. */
12833 error_at (loc, "built-in function %qE must be directly called", expr);
12835 return true;
12838 return false;
12841 /* If we're creating an if-else-if condition chain, first see if we
12842 already have this COND in the CHAIN. If so, warn and don't add COND
12843 into the vector, otherwise add the COND there. LOC is the location
12844 of COND. */
12846 void
12847 warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12849 /* No chain has been created yet. Do nothing. */
12850 if (*chain == NULL)
12851 return;
12853 if (TREE_SIDE_EFFECTS (cond))
12855 /* Uh-oh! This condition has a side-effect, thus invalidates
12856 the whole chain. */
12857 delete *chain;
12858 *chain = NULL;
12859 return;
12862 unsigned int ix;
12863 tree t;
12864 bool found = false;
12865 FOR_EACH_VEC_ELT (**chain, ix, t)
12866 if (operand_equal_p (cond, t, 0))
12868 if (warning_at (loc, OPT_Wduplicated_cond,
12869 "duplicated %<if%> condition"))
12870 inform (EXPR_LOCATION (t), "previously used here");
12871 found = true;
12872 break;
12875 if (!found
12876 && !CONSTANT_CLASS_P (cond)
12877 /* Don't infinitely grow the chain. */
12878 && (*chain)->length () < 512)
12879 (*chain)->safe_push (cond);
12882 /* Check if array size calculations overflow or if the array covers more
12883 than half of the address space. Return true if the size of the array
12884 is valid, false otherwise. TYPE is the type of the array and NAME is
12885 the name of the array, or NULL_TREE for unnamed arrays. */
12887 bool
12888 valid_array_size_p (location_t loc, tree type, tree name)
12890 if (type != error_mark_node
12891 && COMPLETE_TYPE_P (type)
12892 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12893 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
12895 if (name)
12896 error_at (loc, "size of array %qE is too large", name);
12897 else
12898 error_at (loc, "size of unnamed array is too large");
12899 return false;
12901 return true;
12904 /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
12905 timestamp to replace embedded current dates to get reproducible
12906 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
12908 time_t
12909 cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
12911 char *source_date_epoch;
12912 int64_t epoch;
12913 char *endptr;
12915 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
12916 if (!source_date_epoch)
12917 return (time_t) -1;
12919 errno = 0;
12920 #if defined(INT64_T_IS_LONG)
12921 epoch = strtol (source_date_epoch, &endptr, 10);
12922 #else
12923 epoch = strtoll (source_date_epoch, &endptr, 10);
12924 #endif
12925 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
12926 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
12928 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
12929 "expand to a non-negative integer less than or equal to %wd",
12930 MAX_SOURCE_DATE_EPOCH);
12931 return (time_t) -1;
12934 return (time_t) epoch;
12937 /* Check and possibly warn if two declarations have contradictory
12938 attributes, such as always_inline vs. noinline. */
12940 bool
12941 diagnose_mismatched_attributes (tree olddecl, tree newdecl)
12943 bool warned = false;
12945 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
12946 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
12947 /* An optimization attribute applied on a declaration after the
12948 definition is likely not what the user wanted. */
12949 if (a2 != NULL_TREE
12950 && DECL_SAVED_TREE (olddecl) != NULL_TREE
12951 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
12952 warned |= warning (OPT_Wattributes,
12953 "optimization attribute on %qD follows "
12954 "definition but the attribute doesn%'t match",
12955 newdecl);
12957 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
12958 if (DECL_DECLARED_INLINE_P (newdecl)
12959 && DECL_UNINLINABLE (olddecl)
12960 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
12961 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
12962 "declaration with attribute noinline", newdecl);
12963 else if (DECL_DECLARED_INLINE_P (olddecl)
12964 && DECL_UNINLINABLE (newdecl)
12965 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
12966 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12967 "noinline follows inline declaration ", newdecl);
12968 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
12969 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
12970 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12971 "%qs follows declaration with attribute %qs",
12972 newdecl, "noinline", "always_inline");
12973 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
12974 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
12975 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12976 "%qs follows declaration with attribute %qs",
12977 newdecl, "always_inline", "noinline");
12978 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
12979 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
12980 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12981 "%qs follows declaration with attribute %qs",
12982 newdecl, "cold", "hot");
12983 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
12984 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
12985 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12986 "%qs follows declaration with attribute %qs",
12987 newdecl, "hot", "cold");
12988 return warned;
12991 #include "gt-c-family-c-common.h"